( createdDocuments: DocumentData[], knowledgeBaseId: string, processingOptions: ProcessingOptions, requestId: string )
| 401 | * partial failures are logged and recovered by the next sync's stuck-doc pass. |
| 402 | */ |
| 403 | export async function processDocumentsWithQueue( |
| 404 | createdDocuments: DocumentData[], |
| 405 | knowledgeBaseId: string, |
| 406 | processingOptions: ProcessingOptions, |
| 407 | requestId: string |
| 408 | ): Promise<void> { |
| 409 | if (createdDocuments.length === 0) return |
| 410 | |
| 411 | const jobPayloads = createdDocuments.map((doc) => |
| 412 | buildJobPayload(doc, knowledgeBaseId, processingOptions, requestId) |
| 413 | ) |
| 414 | |
| 415 | const useTrigger = isTriggerAvailable() |
| 416 | logger.info( |
| 417 | `[${requestId}] Dispatching background processing for ${jobPayloads.length} documents`, |
| 418 | { backend: useTrigger ? 'trigger-dev' : 'direct' } |
| 419 | ) |
| 420 | |
| 421 | const dispatched = useTrigger |
| 422 | ? await dispatchViaBatchTrigger(jobPayloads, requestId) |
| 423 | : await dispatchInProcess(jobPayloads, requestId) |
| 424 | |
| 425 | logger.info( |
| 426 | `[${requestId}] Document dispatch complete: ${dispatched}/${jobPayloads.length} succeeded` |
| 427 | ) |
| 428 | |
| 429 | if (dispatched === 0) { |
| 430 | throw new Error(`All ${jobPayloads.length} document processing dispatches failed`) |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | async function dispatchViaBatchTrigger( |
| 435 | jobPayloads: DocumentProcessingPayload[], |
no test coverage detected