@@ -32,94 +32,6 @@ export async function duplicateCheck(
3232 return response . json ( ) ;
3333}
3434
35- export async function uploadFileForContext (
36- file : File ,
37- ) : Promise < UploadFileResult > {
38- window . dispatchEvent (
39- new CustomEvent ( "fileUploadStart" , {
40- detail : { filename : file . name } ,
41- } ) ,
42- ) ;
43-
44- try {
45- const formData = new FormData ( ) ;
46- formData . append ( "file" , file ) ;
47-
48- const uploadResponse = await fetch ( "/api/upload_context" , {
49- method : "POST" ,
50- body : formData ,
51- } ) ;
52-
53- let payload : unknown ;
54- try {
55- payload = await uploadResponse . json ( ) ;
56- } catch ( _error ) {
57- throw new Error ( "Upload failed: unable to parse server response" ) ;
58- }
59-
60- const uploadJson =
61- typeof payload === "object" && payload !== null ? payload : { } ;
62-
63- if ( ! uploadResponse . ok ) {
64- const errorMessage =
65- ( uploadJson as { error ?: string } ) . error || "Upload failed" ;
66- throw new Error ( errorMessage ) ;
67- }
68-
69- const fileId =
70- ( uploadJson as { response_id ?: string } ) . response_id || "uploaded" ;
71- const filePath =
72- ( uploadJson as { filename ?: string } ) . filename || file . name ;
73- const pages = ( uploadJson as { pages ?: number } ) . pages ;
74- const contentLength = ( uploadJson as { content_length ?: number } )
75- . content_length ;
76- const confirmation = ( uploadJson as { confirmation ?: string } ) . confirmation ;
77-
78- const result : UploadFileResult = {
79- fileId,
80- filePath,
81- run : null ,
82- deletion : null ,
83- unified : false ,
84- raw : uploadJson ,
85- } ;
86-
87- window . dispatchEvent (
88- new CustomEvent ( "fileUploaded" , {
89- detail : {
90- file,
91- result : {
92- file_id : fileId ,
93- file_path : filePath ,
94- filename : filePath ,
95- pages : pages ,
96- content_length : contentLength ,
97- confirmation : confirmation ,
98- response_id : fileId ,
99- run : null ,
100- deletion : null ,
101- unified : false ,
102- } ,
103- } ,
104- } ) ,
105- ) ;
106-
107- return result ;
108- } catch ( error ) {
109- window . dispatchEvent (
110- new CustomEvent ( "fileUploadError" , {
111- detail : {
112- filename : file . name ,
113- error : error instanceof Error ? error . message : "Upload failed" ,
114- } ,
115- } ) ,
116- ) ;
117- throw error ;
118- } finally {
119- window . dispatchEvent ( new CustomEvent ( "fileUploadComplete" ) ) ;
120- }
121- }
122-
12335export async function uploadFiles (
12436 files : File [ ] ,
12537 replace = false ,
@@ -160,10 +72,16 @@ export async function uploadFiles(
16072 return { taskId, fileCount } ;
16173}
16274
75+ export interface UploadFileCallbacks {
76+ onComplete ?: ( ) => void ;
77+ onError ?: ( filename : string , error : string ) => void ;
78+ }
79+
16380export async function uploadFile (
16481 file : File ,
16582 replace = false ,
16683 createFilter = false ,
84+ callbacks ?: UploadFileCallbacks ,
16785) : Promise < UploadFileResult > {
16886 try {
16987 const formData = new FormData ( ) ;
@@ -243,16 +161,20 @@ export async function uploadFile(
243161
244162 return result ;
245163 } catch ( error ) {
246- window . dispatchEvent (
247- new CustomEvent ( "fileUploadError" , {
248- detail : {
249- filename : file . name ,
250- error : error instanceof Error ? error . message : "Upload failed" ,
251- } ,
252- } ) ,
253- ) ;
164+ try {
165+ callbacks ?. onError ?. (
166+ file . name ,
167+ error instanceof Error ? error . message : "Upload failed" ,
168+ ) ;
169+ } catch ( cbErr ) {
170+ console . warn ( "uploadFile: onError callback threw" , cbErr ) ;
171+ }
254172 throw error ;
255173 } finally {
256- window . dispatchEvent ( new CustomEvent ( "fileUploadComplete" ) ) ;
174+ try {
175+ callbacks ?. onComplete ?.( ) ;
176+ } catch ( cbErr ) {
177+ console . warn ( "uploadFile: onComplete callback threw" , cbErr ) ;
178+ }
257179 }
258180}
0 commit comments