1- import fs from "fs" ;
2- import path from "path" ;
1+ import fs from "node: fs" ;
2+ import path from "node: path" ;
33import { v4 as uuidv4 } from "uuid" ;
44import type Database from "better-sqlite3" ;
55import { extractFromFhirJson } from "./pipelines/fhir" ;
@@ -18,7 +18,7 @@ function detectKind(filePath: string, rawText: string): "fhir" | "hl7" | "pdf" |
1818 const ext = path . extname ( filePath ) . toLowerCase ( ) ;
1919 if ( ext === ".json" || ext === ".xml" ) {
2020 const t = rawText . trimStart ( ) ;
21- if ( t . startsWith ( "{" ) && ( t . includes ( '"resourceType"' ) || t . includes ( '"resourceType"' ) ) ) return "fhir" ;
21+ if ( t . startsWith ( "{" ) && t . includes ( '"resourceType"' ) ) return "fhir" ;
2222 }
2323 if ( ext === ".hl7" || ext === ".txt" ) {
2424 if ( looksLikeHl7 ( rawText ) ) return "hl7" ;
@@ -90,6 +90,13 @@ export async function ingestFile(db: Database.Database, filePath: string): Promi
9090 }
9191
9292 const kind = ext === ".pdf" ? "pdf" : detectKind ( normalized , rawText ) ;
93+
94+ // Write a 'processing' row immediately so the UI shows the file right away.
95+ db . prepare (
96+ `INSERT INTO documents (id, file_path, file_name, source_type, status)
97+ VALUES (?, ?, ?, ?, 'processing')` ,
98+ ) . run ( id , normalized , fileName , kind ) ;
99+
93100 const ctx = await buildContext ( kind , normalized , rawText ) ;
94101
95102 const llmBase = getSetting ( db , "llm_base_url" ) ?? process . env . SIFT_LLM_BASE_URL ?? DEFAULT_LLM_BASE_URL ;
@@ -115,10 +122,10 @@ export async function ingestFile(db: Database.Database, filePath: string): Promi
115122 }
116123
117124 db . prepare (
118- `INSERT INTO documents (
119- id, file_path, file_name, source_type, status, raw_preview , summary_text, confidence, error_message
120- ) VALUES (?, ?, ?, ?, 'complete', ?, ?, ?, ?) ` ,
121- ) . run ( id , normalized , fileName , kind , ctx . preview . slice ( 0 , 65000 ) , summary , confidence , err ) ;
125+ `UPDATE documents
126+ SET status = 'complete', raw_preview = ? , summary_text = ? , confidence = ? , error_message = ?
127+ WHERE id = ? ` ,
128+ ) . run ( ctx . preview . slice ( 0 , 65000 ) , summary , confidence , err , id ) ;
122129
123130 return { documentId : id , status : "complete" } ;
124131}
0 commit comments