|
1 | 1 | import { AuditAction, AuditResourceType } from '@sim/audit' |
2 | 2 | import type { Principal } from '@sim/auth/principal' |
3 | | -import { createLogger } from '@sim/logger' |
4 | 3 | import type { V2KnowledgeDocumentUploadMetadata } from '@/lib/api/contracts/v2/knowledge' |
5 | 4 | import { v2KnowledgeDocumentUploadMetadataSchema } from '@/lib/api/contracts/v2/knowledge' |
6 | 5 | import { checkAttributedUsageLimits } from '@/lib/billing/core/billing-attribution' |
@@ -39,7 +38,14 @@ import { |
39 | 38 | } from '@/lib/uploads/upload-session/service' |
40 | 39 | import { validateFileType } from '@/lib/uploads/utils/validation' |
41 | 40 |
|
42 | | -const logger = createLogger('KnowledgeUploadApplication') |
| 41 | +const PROCESSING_DISPATCH_FAILURE_MESSAGE = 'Knowledge document processing dispatch failed' |
| 42 | + |
| 43 | +class KnowledgeDocumentProcessingDispatchError extends Error { |
| 44 | + constructor(cause: unknown) { |
| 45 | + super(PROCESSING_DISPATCH_FAILURE_MESSAGE, { cause }) |
| 46 | + this.name = 'KnowledgeDocumentProcessingDispatchError' |
| 47 | + } |
| 48 | +} |
43 | 49 |
|
44 | 50 | export class KnowledgeDocumentUnsupportedMediaTypeError extends Error { |
45 | 51 | constructor(message: string) { |
@@ -253,6 +259,22 @@ export const completeKnowledgeDocumentUpload = defineAuthorizedKnowledgeUseCase( |
253 | 259 | ) |
254 | 260 | } |
255 | 261 | if (bound.status === 'bound') { |
| 262 | + if ( |
| 263 | + claimed.error === PROCESSING_DISPATCH_FAILURE_MESSAGE && |
| 264 | + bound.document.processingStatus === 'pending' |
| 265 | + ) { |
| 266 | + const billingAttribution = await resolveKnowledgeBillingAttribution( |
| 267 | + principal, |
| 268 | + freshContext |
| 269 | + ) |
| 270 | + await dispatchKnowledgeDocumentProcessing( |
| 271 | + bound.document, |
| 272 | + freshContext.knowledgeBaseId, |
| 273 | + processingOptions, |
| 274 | + requestId, |
| 275 | + billingAttribution |
| 276 | + ) |
| 277 | + } |
256 | 278 | return { |
257 | 279 | value: { |
258 | 280 | document: bound.document, |
@@ -315,26 +337,13 @@ export const completeKnowledgeDocumentUpload = defineAuthorizedKnowledgeUseCase( |
315 | 337 | throw error |
316 | 338 | } |
317 | 339 |
|
318 | | - const processingDocument: DocumentData = { |
319 | | - documentId: created.id, |
320 | | - filename: created.filename, |
321 | | - fileUrl: created.fileUrl, |
322 | | - fileSize: created.fileSize, |
323 | | - mimeType: created.mimeType, |
324 | | - } |
325 | | - processDocumentsWithQueue( |
326 | | - [processingDocument], |
| 340 | + await dispatchKnowledgeDocumentProcessing( |
| 341 | + created, |
327 | 342 | registrationContext.knowledgeBaseId, |
328 | | - processingOptions ?? {}, |
| 343 | + processingOptions, |
329 | 344 | requestId, |
330 | 345 | billingAttribution |
331 | | - ).catch((error: unknown) => { |
332 | | - logger.error('Knowledge document processing pipeline failed', { |
333 | | - knowledgeBaseId: registrationContext.knowledgeBaseId, |
334 | | - documentId: created.id, |
335 | | - error, |
336 | | - }) |
337 | | - }) |
| 346 | + ) |
338 | 347 | return { |
339 | 348 | value: { |
340 | 349 | document: created, |
@@ -372,6 +381,33 @@ export const completeKnowledgeDocumentUpload = defineAuthorizedKnowledgeUseCase( |
372 | 381 | }, |
373 | 382 | }) |
374 | 383 |
|
| 384 | +async function dispatchKnowledgeDocumentProcessing( |
| 385 | + document: CreatedKnowledgeDocument, |
| 386 | + knowledgeBaseId: string, |
| 387 | + processingOptions: V2KnowledgeDocumentUploadMetadata['processingOptions'], |
| 388 | + requestId: string, |
| 389 | + billingAttribution: Awaited<ReturnType<typeof resolveKnowledgeBillingAttribution>> |
| 390 | +): Promise<void> { |
| 391 | + const processingDocument: DocumentData = { |
| 392 | + documentId: document.id, |
| 393 | + filename: document.filename, |
| 394 | + fileUrl: document.fileUrl, |
| 395 | + fileSize: document.fileSize, |
| 396 | + mimeType: document.mimeType, |
| 397 | + } |
| 398 | + try { |
| 399 | + await processDocumentsWithQueue( |
| 400 | + [processingDocument], |
| 401 | + knowledgeBaseId, |
| 402 | + processingOptions ?? {}, |
| 403 | + requestId, |
| 404 | + billingAttribution |
| 405 | + ) |
| 406 | + } catch (error) { |
| 407 | + throw new KnowledgeDocumentProcessingDispatchError(error) |
| 408 | + } |
| 409 | +} |
| 410 | + |
375 | 411 | async function loadBoundKnowledgeDocumentUpload( |
376 | 412 | principal: Principal, |
377 | 413 | input: KnowledgeDocumentUploadControlInput, |
|
0 commit comments