Make location optional in ExtractedIncidentMetadata. Skip incidents without site/zone instead of crashing. Handle null location in UI form inputs with empty string defaults. Fixes batch creation errors when PDF documents don't contain clear location information for DeepSeek to extract.
185 lines
5.2 KiB
TypeScript
185 lines
5.2 KiB
TypeScript
import { createDeepSeekClient } from '@/lib/claude/client'
|
|
import { getApiKey } from '@/lib/settings'
|
|
import { withUser } from '@/lib/db/with-user'
|
|
import { incidents, sites, zones } from '@/lib/db/schema'
|
|
import { writeAuditLog } from '@/lib/db/audit'
|
|
import { eq, sql } from 'drizzle-orm'
|
|
|
|
export type ExtractedIncidentMetadata = {
|
|
description: string
|
|
date: string
|
|
location?: { site: string; zone: string } | null
|
|
incident_type?: 'env' | 'asset' | 'security' | 'fire'
|
|
priority?: 'low' | 'med' | 'high'
|
|
witnesses?: Array<{ name: string; contact?: string }>
|
|
}
|
|
|
|
export type FileInfo = {
|
|
path: string
|
|
name: string
|
|
mimeType: string
|
|
buffer: Buffer
|
|
}
|
|
|
|
export type CreatedIncident = {
|
|
id: string
|
|
reference_no: string
|
|
file_count: number
|
|
}
|
|
|
|
export async function analyzePdfsWithDeepSeek(
|
|
pdfTexts: string[]
|
|
): Promise<ExtractedIncidentMetadata> {
|
|
const deepseekKey = await getApiKey('DEEPSEEK_API_KEY')
|
|
const client = createDeepSeekClient(deepseekKey)
|
|
|
|
const prompt = `Analyze the following incident document(s) and extract structured data.
|
|
Return ONLY valid JSON, no markdown:
|
|
|
|
Document text:
|
|
${pdfTexts.join('\n---\n')}
|
|
|
|
Extract and return JSON:
|
|
{
|
|
"description": "summary of what happened (required)",
|
|
"date": "incident date in YYYY-MM-DD format (required)",
|
|
"location": {
|
|
"site": "site name (required)",
|
|
"zone": "zone/area name (required)"
|
|
},
|
|
"incident_type": "env|asset|security|fire (optional)",
|
|
"priority": "low|med|high (optional)",
|
|
"witnesses": [{"name": "name", "contact": "contact info"}] (optional array)
|
|
}
|
|
|
|
If a field is not found in the document, omit it. location is optional - use null if not found.`
|
|
|
|
try {
|
|
const response = await client.chat.completions.create({
|
|
model: 'deepseek-chat',
|
|
max_tokens: 500,
|
|
messages: [
|
|
{
|
|
role: 'user',
|
|
content: prompt
|
|
}
|
|
]
|
|
})
|
|
|
|
const text = response.choices[0]?.message.content ?? ''
|
|
const cleaned = text.replace(/^```json\n?|\n?```$/g, '').trim()
|
|
const data = JSON.parse(cleaned)
|
|
|
|
return {
|
|
description: data.description || '',
|
|
date: data.date || '',
|
|
location: {
|
|
site: data.location?.site || '',
|
|
zone: data.location?.zone || ''
|
|
},
|
|
incident_type: data.incident_type,
|
|
priority: data.priority,
|
|
witnesses: data.witnesses
|
|
}
|
|
} catch (error) {
|
|
console.error('DeepSeek extraction failed:', error)
|
|
return {
|
|
description: '',
|
|
date: '',
|
|
location: { site: '', zone: '' }
|
|
}
|
|
}
|
|
}
|
|
|
|
export async function createBulkIncidents(
|
|
metadata: ExtractedIncidentMetadata[],
|
|
userId: string,
|
|
files: FileInfo[]
|
|
): Promise<CreatedIncident[]> {
|
|
const created: CreatedIncident[] = []
|
|
|
|
for (const meta of metadata) {
|
|
// Skip incidents without location
|
|
if (!meta.location?.site || !meta.location?.zone) {
|
|
console.warn('Skipping incident without location:', meta.description)
|
|
continue
|
|
}
|
|
|
|
const result = await withUser(userId, async (txDb) => {
|
|
// Get site ID from site name
|
|
const siteResult = await txDb
|
|
.select()
|
|
.from(sites)
|
|
.where(eq(sites.name, meta.location!.site))
|
|
.limit(1)
|
|
|
|
if (!siteResult.length) {
|
|
throw new Error(`Site not found: ${meta.location!.site}`)
|
|
}
|
|
|
|
const site = siteResult[0]
|
|
|
|
// Get zone ID
|
|
const zoneResult = await txDb
|
|
.select()
|
|
.from(zones)
|
|
.where(eq(zones.name, meta.location!.zone))
|
|
.limit(1)
|
|
|
|
if (!zoneResult.length) {
|
|
throw new Error(`Zone not found: ${meta.location!.zone}`)
|
|
}
|
|
|
|
const zone = zoneResult[0]
|
|
|
|
// Generate reference_no (SITE-YYYYMM-0001)
|
|
const [year, month] = meta.date.split('-').slice(0, 2)
|
|
const prefix = `SITE-${year}${month}`
|
|
|
|
const existing = await txDb.execute(
|
|
sql`SELECT COUNT(*) as cnt FROM ${incidents} WHERE ${incidents.referenceNo} LIKE ${prefix + '-%'}`
|
|
)
|
|
const count = (existing as unknown as { rows: Array<{ cnt: number }> }).rows?.[0]?.cnt || 0
|
|
const nextNum = parseInt(String(count)) + 1
|
|
const reference_no = `${prefix}-${String(nextNum).padStart(4, '0')}`
|
|
|
|
// Create incident (status = closed)
|
|
const [incident] = await txDb
|
|
.insert(incidents)
|
|
.values({
|
|
referenceNo: reference_no,
|
|
description: meta.description,
|
|
reportedAt: new Date(meta.date),
|
|
siteId: site.id,
|
|
zoneId: zone.id,
|
|
incidentType: (meta.incident_type || 'hazard') as 'injury' | 'near_miss' | 'hazard' | 'asset_damage' | 'environmental' | 'security' | 'fire' | 'transport',
|
|
severity: meta.priority === 'high' ? 3 : meta.priority === 'med' ? 2 : 1,
|
|
status: 'reported',
|
|
reportedBy: userId
|
|
})
|
|
.returning({ id: incidents.id })
|
|
|
|
// Note: Bulk import doesn't attach evidence files (passed as empty array)
|
|
|
|
// Audit log
|
|
await writeAuditLog(
|
|
txDb,
|
|
'incidents',
|
|
incident.id,
|
|
'bulk_import',
|
|
{ reference_no, description: meta.description }
|
|
)
|
|
|
|
return { id: incident.id, reference_no, fileCount: files.length }
|
|
})
|
|
|
|
created.push({
|
|
id: result.id,
|
|
reference_no: result.reference_no,
|
|
file_count: result.fileCount
|
|
})
|
|
}
|
|
|
|
return created
|
|
}
|