@@ -20,6 +20,10 @@ import { pipeline } from "stream/promises";
20
20
21
21
import { runAction } from "@app/lib/actions/server" ;
22
22
import config from "@app/lib/api/config" ;
23
+ import type {
24
+ UpsertDocumentArgs ,
25
+ UpsertTableArgs ,
26
+ } from "@app/lib/api/data_sources" ;
23
27
import { upsertDocument , upsertTable } from "@app/lib/api/data_sources" ;
24
28
import type { Authenticator } from "@app/lib/auth" ;
25
29
import type { DustError } from "@app/lib/error" ;
@@ -208,7 +212,11 @@ const upsertDocumentToDatasource: ProcessingFunction = async ({
208
212
} ) => {
209
213
// Use the file id as the document id to make it easy to track the document back to the file.
210
214
const sourceUrl = file . getPrivateUrl ( auth ) ;
211
- const documentId = upsertArgs ?. document_id ?? file . sId ; // Use the file sId as a fallback to make it easy to track the table back to the file.
215
+ let documentId = file . sId ;
216
+ if ( upsertArgs && "document_id" in upsertArgs ) {
217
+ documentId = upsertArgs . document_id ;
218
+ }
219
+ const { title : upsertTitle , ...restArgs } = upsertArgs ?? { } ;
212
220
const upsertDocumentRes = await upsertDocument ( {
213
221
document_id : documentId ,
214
222
source_url : sourceUrl ,
@@ -219,10 +227,10 @@ const upsertDocumentToDatasource: ProcessingFunction = async ({
219
227
dataSource,
220
228
auth,
221
229
mime_type : file . contentType ,
222
- title : file . fileName ,
230
+ title : upsertTitle ?? file . fileName ,
223
231
224
232
// Used to override defaults.
225
- ...( upsertArgs ?? { } ) ,
233
+ ...restArgs ,
226
234
} ) ;
227
235
228
236
if ( upsertDocumentRes . isErr ( ) ) {
@@ -244,7 +252,12 @@ const upsertTableToDatasource: ProcessingFunction = async ({
244
252
dataSource,
245
253
upsertArgs,
246
254
} ) => {
247
- const tableId = upsertArgs ?. tableId ?? file . sId ; // Use the file sId as a fallback for the table_id to make it easy to track the table back to the file.
255
+ // Use the file sId as the table id to make it easy to track the table back to the file.
256
+ let tableId = file . sId ;
257
+ if ( upsertArgs && "tableId" in upsertArgs ) {
258
+ tableId = upsertArgs . tableId ?? tableId ;
259
+ }
260
+ const { title : upsertTitle , ...restArgs } = upsertArgs ?? { } ;
248
261
const upsertTableRes = await upsertTable ( {
249
262
tableId,
250
263
name : slugify ( file . fileName ) ,
@@ -257,11 +270,11 @@ const upsertTableToDatasource: ProcessingFunction = async ({
257
270
dataSource,
258
271
auth,
259
272
useAppForHeaderDetection : true ,
260
- title : file . fileName ,
273
+ title : upsertTitle ?? file . fileName ,
261
274
mimeType : file . contentType ,
262
275
263
276
// Used to override defaults, for manual file uploads where some fields are user-defined.
264
- ...( upsertArgs ?? { } ) ,
277
+ ...restArgs ,
265
278
} ) ;
266
279
267
280
if ( upsertTableRes . isErr ( ) ) {
@@ -287,7 +300,17 @@ type ProcessingFunction = ({
287
300
file : FileResource ;
288
301
content : string ;
289
302
dataSource : DataSourceResource ;
290
- upsertArgs ?: Record < string , string > ;
303
+ upsertArgs ?:
304
+ | Pick < UpsertDocumentArgs , "document_id" | "title" | "tags" >
305
+ | Pick <
306
+ UpsertTableArgs ,
307
+ | "name"
308
+ | "title"
309
+ | "description"
310
+ | "tableId"
311
+ | "tags"
312
+ | "useAppForHeaderDetection"
313
+ > ;
291
314
} ) => Promise < Result < undefined , Error > > ;
292
315
293
316
const getProcessingFunction = ( {
@@ -403,7 +426,17 @@ export async function processAndUpsertToDataSource(
403
426
} : {
404
427
file : FileResource ;
405
428
optionalContent ?: string ;
406
- upsertArgs ?: Record < string , string > ;
429
+ upsertArgs ?:
430
+ | Pick < UpsertDocumentArgs , "document_id" | "title" | "tags" >
431
+ | Pick <
432
+ UpsertTableArgs ,
433
+ | "name"
434
+ | "title"
435
+ | "description"
436
+ | "tableId"
437
+ | "tags"
438
+ | "useAppForHeaderDetection"
439
+ > ;
407
440
}
408
441
) : Promise <
409
442
Result <
0 commit comments