@terreno/ai 57.8.0 → 57.10.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.cursorrules +2 -2
- package/README.md +2 -1
- package/bunfig.toml +1 -1
- package/dist/aiApp.d.ts +16 -5
- package/dist/aiApp.js +17 -7
- package/dist/harness/adminScreens.d.ts +6 -0
- package/dist/harness/adminScreens.js +11 -0
- package/dist/harness/agentLoop.d.ts +61 -0
- package/dist/harness/agentLoop.js +798 -0
- package/dist/harness/agentTaskNames.d.ts +4 -0
- package/dist/harness/agentTaskNames.js +7 -0
- package/dist/harness/approvalPolicy.d.ts +3 -0
- package/dist/harness/approvalPolicy.js +23 -0
- package/dist/harness/approvals.d.ts +101 -0
- package/dist/harness/approvals.js +468 -0
- package/dist/harness/commit.d.ts +238 -0
- package/dist/harness/commit.js +678 -0
- package/dist/harness/conversation.d.ts +75 -0
- package/dist/harness/conversation.js +397 -0
- package/dist/harness/defineAgent.d.ts +9 -0
- package/dist/harness/defineAgent.js +65 -0
- package/dist/harness/defineTask.d.ts +11 -0
- package/dist/harness/defineTask.js +59 -0
- package/dist/harness/defineTool.d.ts +7 -0
- package/dist/harness/defineTool.js +36 -0
- package/dist/harness/definitionError.d.ts +8 -0
- package/dist/harness/definitionError.js +15 -0
- package/dist/harness/errors.d.ts +127 -0
- package/dist/harness/errors.js +101 -0
- package/dist/harness/eventHub.d.ts +45 -0
- package/dist/harness/eventHub.js +135 -0
- package/dist/harness/events.d.ts +95 -0
- package/dist/harness/events.js +267 -0
- package/dist/harness/executionEnv.d.ts +31 -0
- package/dist/harness/executionEnv.js +2 -0
- package/dist/harness/extensions.d.ts +82 -0
- package/dist/harness/extensions.js +274 -0
- package/dist/harness/harness.d.ts +163 -0
- package/dist/harness/harness.js +520 -0
- package/dist/harness/harnessApp.d.ts +44 -0
- package/dist/harness/harnessApp.js +62 -0
- package/dist/harness/internalRuntime.d.ts +16 -0
- package/dist/harness/internalRuntime.js +18 -0
- package/dist/harness/leases.d.ts +35 -0
- package/dist/harness/leases.js +86 -0
- package/dist/harness/memo.d.ts +24 -0
- package/dist/harness/memo.js +78 -0
- package/dist/harness/modelCall.d.ts +45 -0
- package/dist/harness/modelCall.js +147 -0
- package/dist/harness/models/harnessApproval.d.ts +2 -0
- package/dist/harness/models/harnessApproval.js +100 -0
- package/dist/harness/models/harnessConversation.d.ts +2 -0
- package/dist/harness/models/harnessConversation.js +117 -0
- package/dist/harness/models/harnessEvent.d.ts +3 -0
- package/dist/harness/models/harnessEvent.js +73 -0
- package/dist/harness/models/harnessInboxEvent.d.ts +2 -0
- package/dist/harness/models/harnessInboxEvent.js +58 -0
- package/dist/harness/models/harnessMemo.d.ts +2 -0
- package/dist/harness/models/harnessMemo.js +42 -0
- package/dist/harness/models/harnessMessage.d.ts +2 -0
- package/dist/harness/models/harnessMessage.js +73 -0
- package/dist/harness/models/harnessOwner.d.ts +2 -0
- package/dist/harness/models/harnessOwner.js +36 -0
- package/dist/harness/models/harnessTask.d.ts +2 -0
- package/dist/harness/models/harnessTask.js +186 -0
- package/dist/harness/ownership.d.ts +89 -0
- package/dist/harness/ownership.js +438 -0
- package/dist/harness/registry.d.ts +29 -0
- package/dist/harness/registry.js +93 -0
- package/dist/harness/retryBackoff.d.ts +31 -0
- package/dist/harness/retryBackoff.js +50 -0
- package/dist/harness/routes/approvals.d.ts +13 -0
- package/dist/harness/routes/approvals.js +93 -0
- package/dist/harness/routes/conversations.d.ts +11 -0
- package/dist/harness/routes/conversations.js +66 -0
- package/dist/harness/routes/events.d.ts +19 -0
- package/dist/harness/routes/events.js +215 -0
- package/dist/harness/routes/tasks.d.ts +11 -0
- package/dist/harness/routes/tasks.js +108 -0
- package/dist/harness/runners/inProcessRunner.d.ts +57 -0
- package/dist/harness/runners/inProcessRunner.js +196 -0
- package/dist/harness/runtime.d.ts +34 -0
- package/dist/harness/runtime.js +551 -0
- package/dist/harness/subagent.d.ts +53 -0
- package/dist/harness/subagent.js +134 -0
- package/dist/harness/suspend.d.ts +8 -0
- package/dist/harness/suspend.js +16 -0
- package/dist/harness/transaction.d.ts +3 -0
- package/dist/harness/transaction.js +22 -0
- package/dist/harness/waits.d.ts +100 -0
- package/dist/harness/waits.js +214 -0
- package/dist/index.d.ts +19 -1
- package/dist/index.js +45 -2
- package/dist/models/aiDataset.d.ts +2 -0
- package/dist/models/aiDataset.js +67 -0
- package/dist/models/aiRequest.js +1 -1
- package/dist/models/gptHistory.js +88 -2
- package/dist/observability/adminScreens.d.ts +5 -0
- package/dist/observability/adminScreens.js +32 -0
- package/dist/observability/compileTemplate.d.ts +1 -0
- package/dist/observability/compileTemplate.js +13 -0
- package/dist/observability/datasetImport.d.ts +11 -0
- package/dist/observability/datasetImport.js +185 -0
- package/dist/observability/evaluate.d.ts +31 -0
- package/dist/observability/evaluate.js +200 -0
- package/dist/observability/evaluatorTemplates.d.ts +17 -0
- package/dist/observability/evaluatorTemplates.js +111 -0
- package/dist/observability/jsonPath.d.ts +2 -0
- package/dist/observability/jsonPath.js +57 -0
- package/dist/observability/local/datasetStore.d.ts +87 -0
- package/dist/observability/local/datasetStore.js +326 -0
- package/dist/observability/local/evaluatorStore.d.ts +44 -0
- package/dist/observability/local/evaluatorStore.js +182 -0
- package/dist/observability/local/experimentRunner.d.ts +96 -0
- package/dist/observability/local/experimentRunner.js +419 -0
- package/dist/observability/local/localPlugin.d.ts +22 -0
- package/dist/observability/local/localPlugin.js +76 -0
- package/dist/observability/local/models/obsDataset.d.ts +2 -0
- package/dist/observability/local/models/obsDataset.js +37 -0
- package/dist/observability/local/models/obsDatasetItem.d.ts +2 -0
- package/dist/observability/local/models/obsDatasetItem.js +76 -0
- package/dist/observability/local/models/obsEvaluator.d.ts +2 -0
- package/dist/observability/local/models/obsEvaluator.js +98 -0
- package/dist/observability/local/models/obsExperiment.d.ts +2 -0
- package/dist/observability/local/models/obsExperiment.js +87 -0
- package/dist/observability/local/models/obsExperimentItem.d.ts +2 -0
- package/dist/observability/local/models/obsExperimentItem.js +45 -0
- package/dist/observability/local/models/obsPrompt.d.ts +2 -0
- package/dist/observability/local/models/obsPrompt.js +42 -0
- package/dist/observability/local/models/obsPromptLabel.d.ts +2 -0
- package/dist/observability/local/models/obsPromptLabel.js +39 -0
- package/dist/observability/local/models/obsPromptVersion.d.ts +2 -0
- package/dist/observability/local/models/obsPromptVersion.js +96 -0
- package/dist/observability/local/models/obsReviewItem.d.ts +2 -0
- package/dist/observability/local/models/obsReviewItem.js +75 -0
- package/dist/observability/local/models/obsScore.d.ts +2 -0
- package/dist/observability/local/models/obsScore.js +58 -0
- package/dist/observability/local/models/obsSpan.d.ts +2 -0
- package/dist/observability/local/models/obsSpan.js +64 -0
- package/dist/observability/local/models/obsTrace.d.ts +2 -0
- package/dist/observability/local/models/obsTrace.js +93 -0
- package/dist/observability/local/promptStore.d.ts +180 -0
- package/dist/observability/local/promptStore.js +444 -0
- package/dist/observability/local/reviewStore.d.ts +50 -0
- package/dist/observability/local/reviewStore.js +203 -0
- package/dist/observability/local/traceStore.d.ts +103 -0
- package/dist/observability/local/traceStore.js +325 -0
- package/dist/observability/observabilityApp.d.ts +24 -0
- package/dist/observability/observabilityApp.js +157 -0
- package/dist/observability/observabilityAppRegistry.d.ts +14 -0
- package/dist/observability/observabilityAppRegistry.js +16 -0
- package/dist/observability/permissions.d.ts +7 -0
- package/dist/observability/permissions.js +60 -0
- package/dist/observability/reviewPanels.d.ts +18 -0
- package/dist/observability/reviewPanels.js +46 -0
- package/dist/observability/routes/datasets.d.ts +7 -0
- package/dist/observability/routes/datasets.js +149 -0
- package/dist/observability/routes/evaluators.d.ts +7 -0
- package/dist/observability/routes/evaluators.js +123 -0
- package/dist/observability/routes/experiments.d.ts +7 -0
- package/dist/observability/routes/experiments.js +95 -0
- package/dist/observability/routes/observabilityRouteAccess.d.ts +13 -0
- package/dist/observability/routes/observabilityRouteAccess.js +67 -0
- package/dist/observability/routes/prompts.d.ts +12 -0
- package/dist/observability/routes/prompts.js +172 -0
- package/dist/observability/routes/requestAiService.d.ts +13 -0
- package/dist/observability/routes/requestAiService.js +21 -0
- package/dist/observability/routes/review.d.ts +7 -0
- package/dist/observability/routes/review.js +79 -0
- package/dist/observability/routes/status.d.ts +5 -0
- package/dist/observability/routes/status.js +31 -0
- package/dist/observability/routes/testMultiStage.d.ts +9 -0
- package/dist/observability/routes/testMultiStage.js +67 -0
- package/dist/observability/routes/traces.d.ts +7 -0
- package/dist/observability/routes/traces.js +129 -0
- package/dist/observability/schemaValidation.d.ts +5 -0
- package/dist/observability/schemaValidation.js +26 -0
- package/dist/observability/sopThresholds.d.ts +2 -0
- package/dist/observability/sopThresholds.js +21 -0
- package/dist/observability/status.d.ts +23 -0
- package/dist/observability/status.js +34 -0
- package/dist/observability/testMultiStageSchemas.d.ts +10 -0
- package/dist/observability/testMultiStageSchemas.js +79 -0
- package/dist/observability/testMultiStageWorkflow.d.ts +20 -0
- package/dist/observability/testMultiStageWorkflow.js +218 -0
- package/dist/observability/types.d.ts +159 -0
- package/dist/observability/types.js +67 -0
- package/dist/routes/files.js +20 -7
- package/dist/routes/gpt.js +179 -488
- package/dist/routes/gptActions.d.ts +9 -0
- package/dist/routes/gptActions.js +212 -0
- package/dist/routes/gptDatasets.d.ts +5 -0
- package/dist/routes/gptDatasets.js +94 -0
- package/dist/routes/gptHistories.d.ts +3 -1
- package/dist/routes/gptHistories.js +188 -2
- package/dist/service/aiDatasets.d.ts +57 -0
- package/dist/service/aiDatasets.js +253 -0
- package/dist/service/aiService.d.ts +50 -1
- package/dist/service/aiService.js +431 -77
- package/dist/service/askFiles.d.ts +49 -0
- package/dist/service/askFiles.js +162 -0
- package/dist/service/asks.d.ts +76 -0
- package/dist/service/asks.js +284 -0
- package/dist/service/chatTurn.d.ts +128 -0
- package/dist/service/chatTurn.js +1674 -0
- package/dist/service/fileStorage.d.ts +4 -0
- package/dist/service/fileStorage.js +7 -2
- package/dist/service/fileUploadsGate.d.ts +8 -0
- package/dist/service/fileUploadsGate.js +18 -0
- package/dist/service/fillActionIds.d.ts +6 -0
- package/dist/service/fillActionIds.js +79 -0
- package/dist/service/gemini.d.ts +3 -3
- package/dist/service/gemini.js +3 -3
- package/dist/service/jsonFenceModel.d.ts +7 -0
- package/dist/service/jsonFenceModel.js +45 -0
- package/dist/service/prompts.d.ts +52 -0
- package/dist/service/prompts.js +96 -1
- package/dist/service/sanitizeHtml.d.ts +11 -0
- package/dist/service/sanitizeHtml.js +108 -0
- package/dist/service/vertex.d.ts +6 -6
- package/dist/service/vertex.js +5 -5
- package/dist/types/harness.d.ts +934 -0
- package/dist/types/harness.js +133 -0
- package/dist/types/index.d.ts +219 -13
- package/dist/types/index.js +2 -0
- package/dist/types/observability.d.ts +288 -0
- package/dist/types/observability.js +2 -0
- package/package.json +17 -5
- package/src/aiApp.test.ts +207 -1
- package/src/aiApp.ts +31 -8
- package/src/harness/adminScreens.test.ts +34 -0
- package/src/harness/adminScreens.ts +11 -0
- package/src/harness/agentLoop.test.ts +1034 -0
- package/src/harness/agentLoop.ts +1082 -0
- package/src/harness/agentRecovery.test.ts +380 -0
- package/src/harness/agentTaskNames.ts +4 -0
- package/src/harness/approvalPolicy.ts +28 -0
- package/src/harness/approvals.test.ts +1094 -0
- package/src/harness/approvals.ts +644 -0
- package/src/harness/commit.ts +1049 -0
- package/src/harness/conversation.ts +503 -0
- package/src/harness/defineAgent.test.ts +92 -0
- package/src/harness/defineAgent.ts +82 -0
- package/src/harness/defineTask.test.ts +59 -0
- package/src/harness/defineTask.ts +76 -0
- package/src/harness/defineTool.ts +45 -0
- package/src/harness/definitionError.ts +13 -0
- package/src/harness/errors.test.ts +90 -0
- package/src/harness/errors.ts +119 -0
- package/src/harness/eventHub.test.ts +124 -0
- package/src/harness/eventHub.ts +159 -0
- package/src/harness/events.test.ts +757 -0
- package/src/harness/events.ts +384 -0
- package/src/harness/executionEnv.ts +34 -0
- package/src/harness/extensions.test.ts +1218 -0
- package/src/harness/extensions.ts +382 -0
- package/src/harness/harness.test.ts +693 -0
- package/src/harness/harness.ts +767 -0
- package/src/harness/harnessApp.ts +76 -0
- package/src/harness/internalRuntime.ts +44 -0
- package/src/harness/leases.ts +132 -0
- package/src/harness/memo.ts +107 -0
- package/src/harness/modelCall.test.ts +134 -0
- package/src/harness/modelCall.ts +200 -0
- package/src/harness/models/harnessApproval.ts +109 -0
- package/src/harness/models/harnessConversation.ts +142 -0
- package/src/harness/models/harnessEvent.ts +92 -0
- package/src/harness/models/harnessInboxEvent.ts +73 -0
- package/src/harness/models/harnessMemo.ts +43 -0
- package/src/harness/models/harnessMessage.ts +85 -0
- package/src/harness/models/harnessModels.test.ts +60 -0
- package/src/harness/models/harnessOwner.ts +40 -0
- package/src/harness/models/harnessTask.ts +201 -0
- package/src/harness/ownership.test.ts +1087 -0
- package/src/harness/ownership.ts +618 -0
- package/src/harness/recovery.test.ts +912 -0
- package/src/harness/registry.ts +124 -0
- package/src/harness/retries.test.ts +292 -0
- package/src/harness/retryBackoff.test.ts +100 -0
- package/src/harness/retryBackoff.ts +77 -0
- package/src/harness/routes/approvals.ts +123 -0
- package/src/harness/routes/conversations.test.ts +518 -0
- package/src/harness/routes/conversations.ts +102 -0
- package/src/harness/routes/events.ts +280 -0
- package/src/harness/routes/tasks.test.ts +198 -0
- package/src/harness/routes/tasks.ts +146 -0
- package/src/harness/runners/inProcessRunner.test.ts +402 -0
- package/src/harness/runners/inProcessRunner.ts +220 -0
- package/src/harness/runtime.ts +714 -0
- package/src/harness/subagent.test.ts +696 -0
- package/src/harness/subagent.ts +192 -0
- package/src/harness/suspend.ts +13 -0
- package/src/harness/transaction.ts +17 -0
- package/src/harness/versioning.test.ts +288 -0
- package/src/harness/waits.test.ts +693 -0
- package/src/harness/waits.ts +356 -0
- package/src/index.test.ts +13 -0
- package/src/index.ts +70 -0
- package/src/isolated/adminScreensConfig.isolated.ts +143 -0
- package/src/models/aiDataset.test.ts +34 -0
- package/src/models/aiDataset.ts +78 -0
- package/src/models/aiRequest.test.ts +2 -2
- package/src/models/aiRequest.ts +2 -1
- package/src/models/gptHistory.test.ts +156 -0
- package/src/models/gptHistory.ts +106 -4
- package/src/observability/adminScreens.test.ts +71 -0
- package/src/observability/adminScreens.ts +42 -0
- package/src/observability/compileTemplate.test.ts +14 -0
- package/src/observability/compileTemplate.ts +10 -0
- package/src/observability/datasetImport.test.ts +95 -0
- package/src/observability/datasetImport.ts +220 -0
- package/src/observability/evaluate.test.ts +549 -0
- package/src/observability/evaluate.ts +254 -0
- package/src/observability/evaluatorTemplates.ts +124 -0
- package/src/observability/jsonPath.test.ts +52 -0
- package/src/observability/jsonPath.ts +60 -0
- package/src/observability/local/datasetStore.test.ts +230 -0
- package/src/observability/local/datasetStore.ts +452 -0
- package/src/observability/local/evaluatorStore.test.ts +138 -0
- package/src/observability/local/evaluatorStore.ts +224 -0
- package/src/observability/local/experimentRunner.test.ts +489 -0
- package/src/observability/local/experimentRunner.ts +548 -0
- package/src/observability/local/localPlugin.ts +88 -0
- package/src/observability/local/models/obsDataset.ts +38 -0
- package/src/observability/local/models/obsDatasetItem.ts +80 -0
- package/src/observability/local/models/obsEvaluator.ts +102 -0
- package/src/observability/local/models/obsExperiment.ts +91 -0
- package/src/observability/local/models/obsExperimentItem.ts +52 -0
- package/src/observability/local/models/obsPrompt.ts +43 -0
- package/src/observability/local/models/obsPromptLabel.ts +43 -0
- package/src/observability/local/models/obsPromptVersion.ts +107 -0
- package/src/observability/local/models/obsReviewItem.ts +79 -0
- package/src/observability/local/models/obsScore.ts +59 -0
- package/src/observability/local/models/obsSpan.ts +65 -0
- package/src/observability/local/models/obsTrace.ts +94 -0
- package/src/observability/local/obsModels.test.ts +119 -0
- package/src/observability/local/promptStore.test.ts +351 -0
- package/src/observability/local/promptStore.ts +665 -0
- package/src/observability/local/reviewStore.test.ts +164 -0
- package/src/observability/local/reviewStore.ts +264 -0
- package/src/observability/local/traceStore.test.ts +264 -0
- package/src/observability/local/traceStore.ts +440 -0
- package/src/observability/observabilityApp.test.ts +92 -0
- package/src/observability/observabilityApp.ts +185 -0
- package/src/observability/observabilityAppRegistry.test.ts +30 -0
- package/src/observability/observabilityAppRegistry.ts +35 -0
- package/src/observability/permissions.test.ts +96 -0
- package/src/observability/permissions.ts +78 -0
- package/src/observability/reviewPanels.test.ts +35 -0
- package/src/observability/reviewPanels.ts +64 -0
- package/src/observability/routes/datasets.test.ts +176 -0
- package/src/observability/routes/datasets.ts +252 -0
- package/src/observability/routes/evaluators.test.ts +119 -0
- package/src/observability/routes/evaluators.ts +207 -0
- package/src/observability/routes/experiments.test.ts +282 -0
- package/src/observability/routes/experiments.ts +152 -0
- package/src/observability/routes/observabilityRbac.test.ts +767 -0
- package/src/observability/routes/observabilityRouteAccess.ts +106 -0
- package/src/observability/routes/prompts.test.ts +320 -0
- package/src/observability/routes/prompts.ts +263 -0
- package/src/observability/routes/requestAiService.ts +36 -0
- package/src/observability/routes/review.test.ts +120 -0
- package/src/observability/routes/review.ts +125 -0
- package/src/observability/routes/status.test.ts +116 -0
- package/src/observability/routes/status.ts +47 -0
- package/src/observability/routes/testMultiStage.test.ts +214 -0
- package/src/observability/routes/testMultiStage.ts +96 -0
- package/src/observability/routes/traces.test.ts +167 -0
- package/src/observability/routes/traces.ts +172 -0
- package/src/observability/schemaValidation.ts +29 -0
- package/src/observability/sopThresholds.ts +20 -0
- package/src/observability/status.test.ts +38 -0
- package/src/observability/status.ts +56 -0
- package/src/observability/testMultiStageSchemas.ts +95 -0
- package/src/observability/testMultiStageWorkflow.test.ts +114 -0
- package/src/observability/testMultiStageWorkflow.ts +307 -0
- package/src/observability/types.ts +262 -0
- package/src/routes/files.test.ts +84 -9
- package/src/routes/files.ts +23 -7
- package/src/routes/gpt.test.ts +3279 -0
- package/src/routes/gpt.ts +214 -552
- package/src/routes/gptActions.test.ts +241 -0
- package/src/routes/gptActions.ts +255 -0
- package/src/routes/gptApprovals.test.ts +762 -0
- package/src/routes/gptDatasets.test.ts +86 -0
- package/src/routes/gptDatasets.ts +104 -0
- package/src/routes/gptFiles.test.ts +589 -0
- package/src/routes/gptHistories.test.ts +933 -0
- package/src/routes/gptHistories.ts +266 -5
- package/src/routes/routes.test.ts +676 -5
- package/src/service/aiDatasets.test.ts +123 -0
- package/src/service/aiDatasets.ts +316 -0
- package/src/service/aiService.test.ts +614 -0
- package/src/service/aiService.ts +575 -111
- package/src/service/askFiles.test.ts +96 -0
- package/src/service/askFiles.ts +222 -0
- package/src/service/asks.test.ts +946 -0
- package/src/service/asks.ts +423 -0
- package/src/service/chatTurn.ts +2287 -0
- package/src/service/fileStorage.test.ts +11 -0
- package/src/service/fileStorage.ts +10 -2
- package/src/service/fileUploadsGate.ts +23 -0
- package/src/service/fillActionIds.test.ts +65 -0
- package/src/service/fillActionIds.ts +98 -0
- package/src/service/gemini.test.ts +16 -10
- package/src/service/gemini.ts +3 -3
- package/src/service/jsonFenceModel.test.ts +47 -0
- package/src/service/jsonFenceModel.ts +53 -0
- package/src/service/prompts.test.ts +21 -0
- package/src/service/prompts.ts +147 -0
- package/src/service/sanitizeHtml.test.ts +61 -0
- package/src/service/sanitizeHtml.ts +109 -0
- package/src/service/vertex.test.ts +49 -49
- package/src/service/vertex.ts +7 -8
- package/src/tests/bunSetup.test.ts +7 -4
- package/src/tests/bunSetup.ts +7 -7
- package/src/tests/chatHarness.ts +478 -0
- package/src/tests/generateStream.ts +49 -0
- package/src/tests/harnessErrors.ts +22 -0
- package/src/tests/harnessStreaming.ts +227 -0
- package/src/tests/helpers.ts +9 -2
- package/src/types/harness.ts +1095 -0
- package/src/types/index.ts +237 -12
- package/src/types/observability.ts +344 -0
- package/src/types/sanitize-html.d.ts +25 -0
|
@@ -0,0 +1,2287 @@
|
|
|
1
|
+
import {randomUUID} from "node:crypto";
|
|
2
|
+
|
|
3
|
+
import {APIError, logger} from "@terreno/api";
|
|
4
|
+
import {
|
|
5
|
+
ASK_CANCEL_REASONS,
|
|
6
|
+
ASK_SURFACES,
|
|
7
|
+
type Ask,
|
|
8
|
+
type AskKind,
|
|
9
|
+
type AskResponse,
|
|
10
|
+
type AskSurface,
|
|
11
|
+
askKindsForSurface,
|
|
12
|
+
type BlockError,
|
|
13
|
+
type FilesAnswer,
|
|
14
|
+
parseBlocks,
|
|
15
|
+
type SimpleCard,
|
|
16
|
+
type TurnResult,
|
|
17
|
+
toSimpleCard,
|
|
18
|
+
validateAskResponse,
|
|
19
|
+
validateBlocks,
|
|
20
|
+
} from "@terreno/blocks";
|
|
21
|
+
import type {ModelMessage, Tool, ToolResultPart} from "ai";
|
|
22
|
+
import {stepCountIs, streamText} from "ai";
|
|
23
|
+
import type express from "express";
|
|
24
|
+
import {DateTime} from "luxon";
|
|
25
|
+
import type mongoose from "mongoose";
|
|
26
|
+
|
|
27
|
+
import {createTelemetryConfig, preparePromptForAI} from "../langfuseVercelAi";
|
|
28
|
+
import {AIRequest} from "../models/aiRequest";
|
|
29
|
+
import {GptHistory} from "../models/gptHistory";
|
|
30
|
+
import {Project} from "../models/project";
|
|
31
|
+
import type {SpanRecord} from "../observability/types";
|
|
32
|
+
import type {
|
|
33
|
+
AskOrigin,
|
|
34
|
+
GptHistoryAskStatus,
|
|
35
|
+
GptHistoryDocument,
|
|
36
|
+
GptHistoryPendingAsk,
|
|
37
|
+
GptHistoryPrompt,
|
|
38
|
+
GptRouteOptions,
|
|
39
|
+
MessageContentPart,
|
|
40
|
+
UiBlocksOptions,
|
|
41
|
+
} from "../types";
|
|
42
|
+
import {AIService} from "./aiService";
|
|
43
|
+
import {
|
|
44
|
+
askFilesModelOutput,
|
|
45
|
+
invalidAskResponseError,
|
|
46
|
+
resolveAskFiles,
|
|
47
|
+
storedFilesAnswer,
|
|
48
|
+
} from "./askFiles";
|
|
49
|
+
import {
|
|
50
|
+
approvalAskInput,
|
|
51
|
+
askKindFromToolName,
|
|
52
|
+
askToolName,
|
|
53
|
+
buildAsksSystemPrompt,
|
|
54
|
+
completePausedTurn,
|
|
55
|
+
createAskTools,
|
|
56
|
+
deniedApprovalReasons,
|
|
57
|
+
parseAsk,
|
|
58
|
+
resolveAskKinds,
|
|
59
|
+
resolveMaxFileSizeBytes,
|
|
60
|
+
toStoredMessages,
|
|
61
|
+
withoutReservedToolNames,
|
|
62
|
+
} from "./asks";
|
|
63
|
+
import {assertFileUploadsEnabled} from "./fileUploadsGate";
|
|
64
|
+
import {fillMissingActionIds} from "./fillActionIds";
|
|
65
|
+
import {
|
|
66
|
+
COMPACT_SURFACE_SYSTEM_PROMPT,
|
|
67
|
+
TITLE_GENERATION_PROMPT,
|
|
68
|
+
UI_BLOCKS_REPAIR_SYSTEM_PROMPT,
|
|
69
|
+
uiBlocksSystemPrompt,
|
|
70
|
+
} from "./prompts";
|
|
71
|
+
import {sanitizeBlocksText} from "./sanitizeHtml";
|
|
72
|
+
|
|
73
|
+
export const DEMO_RESPONSE =
|
|
74
|
+
"This is demo mode. To use AI features, paste your Gemini API key in Settings.";
|
|
75
|
+
|
|
76
|
+
interface ChatTurnAskEvent {
|
|
77
|
+
ask: AskCall & {simple: SimpleCard};
|
|
78
|
+
/** The conversation that waits on the ask, so a client can answer before `{done}` arrives. */
|
|
79
|
+
historyId: string;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
interface ChatTurnAskResolvedEvent {
|
|
83
|
+
askResolved: {action: AskResponse["action"]; toolCallId: string};
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
interface ChatTurnDoneEvent {
|
|
87
|
+
done: true;
|
|
88
|
+
historyId?: string;
|
|
89
|
+
pendingAsk?: {toolCallId: string};
|
|
90
|
+
title?: string;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
interface ChatTurnErrorEvent {
|
|
94
|
+
error: string;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
interface ChatTurnFileEvent {
|
|
98
|
+
file: {filename: string; mimeType: string; url: string};
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
interface ChatTurnImageEvent {
|
|
102
|
+
image: {mimeType: string; url: string};
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
interface ChatTurnBlocksEvent {
|
|
106
|
+
blocks: {errors: BlockError[]; ok: boolean; warnings: BlockError[]};
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
interface ChatTurnStartedEvent {
|
|
110
|
+
historyId: string;
|
|
111
|
+
started: true;
|
|
112
|
+
streamId: string;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
interface ChatTurnTextEvent {
|
|
116
|
+
text: string;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
interface ChatTurnReplaceTextEvent {
|
|
120
|
+
replace: "text";
|
|
121
|
+
text: string;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
interface ChatTurnToolCallEvent {
|
|
125
|
+
toolCall: {args: unknown; toolCallId: string; toolName: string};
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
interface ChatTurnToolResultEvent {
|
|
129
|
+
toolResult: {result: unknown; toolCallId: string; toolName: string};
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
type ChatTurnEvent =
|
|
133
|
+
| ChatTurnAskEvent
|
|
134
|
+
| ChatTurnAskResolvedEvent
|
|
135
|
+
| ChatTurnBlocksEvent
|
|
136
|
+
| ChatTurnDoneEvent
|
|
137
|
+
| ChatTurnReplaceTextEvent
|
|
138
|
+
| ChatTurnErrorEvent
|
|
139
|
+
| ChatTurnFileEvent
|
|
140
|
+
| ChatTurnImageEvent
|
|
141
|
+
| ChatTurnStartedEvent
|
|
142
|
+
| ChatTurnTextEvent
|
|
143
|
+
| ChatTurnToolCallEvent
|
|
144
|
+
| ChatTurnToolResultEvent;
|
|
145
|
+
|
|
146
|
+
const DEFAULT_STREAM_PERSIST_INTERVAL_MS = 1000;
|
|
147
|
+
// Bump `updated` even without new text so long tool calls do not look stale to resumers.
|
|
148
|
+
const STREAM_HEARTBEAT_MS = 10_000;
|
|
149
|
+
const DURABLE_URL_PATTERN = /^(https?:|data:)/i;
|
|
150
|
+
const DATA_URL_PATTERN = /^data:([^;,]*)((?:;[^;,]*)*?)(;base64)?,(.*)$/s;
|
|
151
|
+
|
|
152
|
+
/** Receives a turn's events. `open` runs once, before the first event. */
|
|
153
|
+
export interface ChatTurnSink {
|
|
154
|
+
emit: (event: ChatTurnEvent) => void;
|
|
155
|
+
open: () => void;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
interface ChatAttachment {
|
|
159
|
+
filename?: string;
|
|
160
|
+
mimeType?: string;
|
|
161
|
+
type?: string;
|
|
162
|
+
url?: string;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
interface GeneratedImageFile {
|
|
166
|
+
base64: string;
|
|
167
|
+
mediaType: string;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
interface GeneratedImage {
|
|
171
|
+
mimeType: string;
|
|
172
|
+
url: string;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
interface AskAnswer {
|
|
176
|
+
response: Record<string, unknown>;
|
|
177
|
+
toolCallId: string;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* An ask the turn can pause on. An approval ask (`origin: "approval"`) is made by the server for
|
|
182
|
+
* a host tool call that needs approval: its `toolCallId` is the AI SDK `approvalId` and
|
|
183
|
+
* `toolName` is the host tool.
|
|
184
|
+
*/
|
|
185
|
+
type AskCall = Ask & {origin?: AskOrigin; toolCallId: string; toolName?: string};
|
|
186
|
+
|
|
187
|
+
interface ResolvedAsk {
|
|
188
|
+
action: AskResponse["action"];
|
|
189
|
+
kind: AskKind;
|
|
190
|
+
origin?: AskOrigin;
|
|
191
|
+
toolCallId: string;
|
|
192
|
+
toolName?: string;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/** A checked answer to the pending ask, resolved once the turn's tools and prompt are ready. */
|
|
196
|
+
interface PendingAnswer {
|
|
197
|
+
/** The pending ask as stored, put back when the turn fails before the client sees any output. */
|
|
198
|
+
pendingAsk: GptHistoryPendingAsk;
|
|
199
|
+
result: AskResponse;
|
|
200
|
+
status: GptHistoryAskStatus;
|
|
201
|
+
toolCallId: string;
|
|
202
|
+
toolName: string;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
interface TurnStart {
|
|
206
|
+
answer?: PendingAnswer;
|
|
207
|
+
history: GptHistoryDocument;
|
|
208
|
+
/** True when the turn starts a conversation, which is inserted with the turn's rows. */
|
|
209
|
+
isNewHistory: boolean;
|
|
210
|
+
logPrompt: string;
|
|
211
|
+
messages: ModelMessage[];
|
|
212
|
+
/**
|
|
213
|
+
* The paused turn's `promptIndex` when this turn answers its ask. A prompt turn's is known only
|
|
214
|
+
* once its rows are appended.
|
|
215
|
+
*/
|
|
216
|
+
promptIndex?: number;
|
|
217
|
+
replayedMessages: ModelMessage[];
|
|
218
|
+
resolvedAsk?: ResolvedAsk;
|
|
219
|
+
titlePrompt: string;
|
|
220
|
+
/** The turn's new rows so far: the user's message for a prompt, none for an answer. */
|
|
221
|
+
rows: GptHistoryPrompt[];
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/** What the turn streamed so far. The rows are appended together when the turn ends or fails. */
|
|
225
|
+
interface TurnRecord {
|
|
226
|
+
askCalls: AskCall[];
|
|
227
|
+
fullResponse: string;
|
|
228
|
+
generatedImages: GeneratedImage[];
|
|
229
|
+
/** True once the client was sent any of the model's output. Errors do not count. */
|
|
230
|
+
hasOutput: boolean;
|
|
231
|
+
rows: GptHistoryPrompt[];
|
|
232
|
+
/** True once assistant text was sent. A later rewrite then uses `{replace}` instead of `{text}`. */
|
|
233
|
+
textDelivered: boolean;
|
|
234
|
+
/** The last error the model stream reported, which it streams instead of throwing. */
|
|
235
|
+
streamError?: string;
|
|
236
|
+
/**
|
|
237
|
+
* Text a reload should show right now: committed reply text plus the current step, unless that
|
|
238
|
+
* step is calling a tool.
|
|
239
|
+
*/
|
|
240
|
+
streamingText: string;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
type StreamPart = {type: string; [key: string]: unknown};
|
|
244
|
+
|
|
245
|
+
const isGeneratedImageFile = (value: unknown): value is GeneratedImageFile =>
|
|
246
|
+
typeof value === "object" &&
|
|
247
|
+
value !== null &&
|
|
248
|
+
"base64" in value &&
|
|
249
|
+
typeof value.base64 === "string" &&
|
|
250
|
+
"mediaType" in value &&
|
|
251
|
+
typeof value.mediaType === "string" &&
|
|
252
|
+
value.mediaType.startsWith("image/");
|
|
253
|
+
|
|
254
|
+
const isRecord = (value: unknown): value is Record<string, unknown> =>
|
|
255
|
+
typeof value === "object" && value !== null && !Array.isArray(value);
|
|
256
|
+
|
|
257
|
+
/** The tool name an ask's rows store: the host tool for an approval ask, else `ask_<kind>`. */
|
|
258
|
+
const askRowToolName = (ask: {kind: AskKind; toolName?: string}): string =>
|
|
259
|
+
ask.toolName ?? askToolName(ask.kind);
|
|
260
|
+
|
|
261
|
+
/** The fields that mark an ask as an approval, or none for an ask the model made. */
|
|
262
|
+
const approvalFieldsOf = (ask: {
|
|
263
|
+
origin?: AskOrigin;
|
|
264
|
+
toolName?: string;
|
|
265
|
+
}): {origin?: AskOrigin; toolName?: string} =>
|
|
266
|
+
ask.origin === "approval" ? {origin: ask.origin, toolName: ask.toolName} : {};
|
|
267
|
+
|
|
268
|
+
const trailingActionBlob = /\{[\s\S]*?"action"[\s\S]*?\}\s*$/;
|
|
269
|
+
|
|
270
|
+
// Drop a trailing JSON action blob the model leaked after its reply. A reply that is
|
|
271
|
+
// itself the object, including a JSON block document, stays intact.
|
|
272
|
+
const cleanStepText = (text: string): string => {
|
|
273
|
+
const trimmed = text.trim();
|
|
274
|
+
const match = trailingActionBlob.exec(trimmed);
|
|
275
|
+
if (!match || match.index === undefined) {
|
|
276
|
+
return trimmed;
|
|
277
|
+
}
|
|
278
|
+
const before = trimmed.slice(0, match.index).trim();
|
|
279
|
+
if (!before || parseBlocks(match[0]).ok) {
|
|
280
|
+
return trimmed;
|
|
281
|
+
}
|
|
282
|
+
return before;
|
|
283
|
+
};
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* Resolve the AIService for a request. Priority:
|
|
287
|
+
* 1. Per-request API key via `x-ai-api-key` header (creates a temporary AIService via createModelFn)
|
|
288
|
+
* 2. Specific model requested + server-side model factory (creates a temporary AIService via createServerModelFn)
|
|
289
|
+
* 3. Pre-configured aiService from route options
|
|
290
|
+
* 4. undefined (triggers demo mode response)
|
|
291
|
+
*/
|
|
292
|
+
export const resolveAiService = (
|
|
293
|
+
req: express.Request,
|
|
294
|
+
options: GptRouteOptions,
|
|
295
|
+
modelId?: string
|
|
296
|
+
): AIService | undefined => {
|
|
297
|
+
const perRequestKey = req.headers["x-ai-api-key"] as string | undefined;
|
|
298
|
+
if (perRequestKey && options.createModelFn) {
|
|
299
|
+
return new AIService({model: options.createModelFn(perRequestKey, modelId)});
|
|
300
|
+
}
|
|
301
|
+
if (modelId && options.createServerModelFn) {
|
|
302
|
+
const serverModel = options.createServerModelFn(modelId);
|
|
303
|
+
if (serverModel) {
|
|
304
|
+
return new AIService({model: serverModel});
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
return options.aiService;
|
|
308
|
+
};
|
|
309
|
+
|
|
310
|
+
/** Generate a short title for a conversation using a cheap model call. */
|
|
311
|
+
const generateTitle = async (
|
|
312
|
+
prompt: string,
|
|
313
|
+
response: string,
|
|
314
|
+
aiService: AIService,
|
|
315
|
+
options: GptRouteOptions,
|
|
316
|
+
perRequestApiKey?: string
|
|
317
|
+
): Promise<string | undefined> => {
|
|
318
|
+
try {
|
|
319
|
+
let titleService = aiService;
|
|
320
|
+
if (options.titleModelId) {
|
|
321
|
+
if (options.createModelFn && perRequestApiKey) {
|
|
322
|
+
titleService = new AIService({
|
|
323
|
+
model: options.createModelFn(perRequestApiKey, options.titleModelId),
|
|
324
|
+
});
|
|
325
|
+
} else if (options.createServerModelFn) {
|
|
326
|
+
const titleModel = options.createServerModelFn(options.titleModelId);
|
|
327
|
+
if (titleModel) {
|
|
328
|
+
titleService = new AIService({model: titleModel});
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
const conversationSnippet = `User: ${prompt}\nAssistant: ${response.substring(0, 500)}`;
|
|
333
|
+
const title = await titleService.generateText({
|
|
334
|
+
prompt: conversationSnippet,
|
|
335
|
+
skipTrace: true,
|
|
336
|
+
systemPrompt: TITLE_GENERATION_PROMPT,
|
|
337
|
+
temperature: 0.3,
|
|
338
|
+
});
|
|
339
|
+
return title.trim().replace(/^["']|["']$/g, "");
|
|
340
|
+
} catch {
|
|
341
|
+
return undefined;
|
|
342
|
+
}
|
|
343
|
+
};
|
|
344
|
+
|
|
345
|
+
const parseAskAnswer = (value: unknown): AskAnswer => {
|
|
346
|
+
if (!isRecord(value)) {
|
|
347
|
+
throw new APIError({status: 400, title: "askResponse must be an object"});
|
|
348
|
+
}
|
|
349
|
+
const {toolCallId, ...response} = value;
|
|
350
|
+
if (typeof toolCallId !== "string" || toolCallId.length === 0) {
|
|
351
|
+
throw new APIError({status: 400, title: "askResponse.toolCallId is required"});
|
|
352
|
+
}
|
|
353
|
+
return {response, toolCallId};
|
|
354
|
+
};
|
|
355
|
+
|
|
356
|
+
/** Checks which kind of turn the body asks for. Throws a 400 when it is neither or both. */
|
|
357
|
+
const parseTurnBody = ({
|
|
358
|
+
askKinds,
|
|
359
|
+
body,
|
|
360
|
+
}: {
|
|
361
|
+
askKinds: AskKind[];
|
|
362
|
+
body: Record<string, unknown>;
|
|
363
|
+
}): AskAnswer | undefined => {
|
|
364
|
+
const {askResponse, attachments, historyId, prompt} = body;
|
|
365
|
+
const hasPrompt = typeof prompt === "string" && prompt.length > 0;
|
|
366
|
+
const askAnswer =
|
|
367
|
+
askKinds.length > 0 && askResponse !== undefined && askResponse !== null
|
|
368
|
+
? parseAskAnswer(askResponse)
|
|
369
|
+
: undefined;
|
|
370
|
+
if (!askAnswer) {
|
|
371
|
+
if (!hasPrompt) {
|
|
372
|
+
throw new APIError({status: 400, title: "prompt is required"});
|
|
373
|
+
}
|
|
374
|
+
return undefined;
|
|
375
|
+
}
|
|
376
|
+
if (hasPrompt) {
|
|
377
|
+
throw new APIError({status: 400, title: "Send either prompt or askResponse, not both"});
|
|
378
|
+
}
|
|
379
|
+
if (Array.isArray(attachments) && attachments.length > 0) {
|
|
380
|
+
throw new APIError({status: 400, title: "attachments cannot be sent with askResponse"});
|
|
381
|
+
}
|
|
382
|
+
if (!historyId) {
|
|
383
|
+
throw new APIError({status: 400, title: "historyId is required with askResponse"});
|
|
384
|
+
}
|
|
385
|
+
return askAnswer;
|
|
386
|
+
};
|
|
387
|
+
|
|
388
|
+
/** The body's `surface`, `"full"` when it sends none. Throws a 400 for any other value. */
|
|
389
|
+
const parseSurface = (surface: unknown): AskSurface => {
|
|
390
|
+
if (surface === undefined || surface === null) {
|
|
391
|
+
return "full";
|
|
392
|
+
}
|
|
393
|
+
const knownSurface = ASK_SURFACES.find((candidate) => candidate === surface);
|
|
394
|
+
if (!knownSurface) {
|
|
395
|
+
throw new APIError({
|
|
396
|
+
detail: `Send one of: ${ASK_SURFACES.join(", ")}.`,
|
|
397
|
+
status: 400,
|
|
398
|
+
title: "surface is not a known surface",
|
|
399
|
+
});
|
|
400
|
+
}
|
|
401
|
+
return knownSurface;
|
|
402
|
+
};
|
|
403
|
+
|
|
404
|
+
const loadHistory = async ({
|
|
405
|
+
historyId,
|
|
406
|
+
projectId,
|
|
407
|
+
userId,
|
|
408
|
+
}: {
|
|
409
|
+
historyId: unknown;
|
|
410
|
+
projectId: unknown;
|
|
411
|
+
userId: mongoose.Types.ObjectId | undefined;
|
|
412
|
+
}): Promise<GptHistoryDocument> => {
|
|
413
|
+
if (!historyId) {
|
|
414
|
+
return new GptHistory({prompts: [], userId, ...(projectId ? {projectId} : {})});
|
|
415
|
+
}
|
|
416
|
+
const history = await GptHistory.findById(historyId);
|
|
417
|
+
if (!history) {
|
|
418
|
+
throw new APIError({status: 404, title: "History not found"});
|
|
419
|
+
}
|
|
420
|
+
if (history.userId.toString() !== userId?.toString()) {
|
|
421
|
+
throw new APIError({status: 403, title: "Not authorized to access this history"});
|
|
422
|
+
}
|
|
423
|
+
return history;
|
|
424
|
+
};
|
|
425
|
+
|
|
426
|
+
/** The 409 for an answer that names a tool call the conversation is not waiting on. */
|
|
427
|
+
export const staleAskError = (toolCallId: string): APIError =>
|
|
428
|
+
new APIError({
|
|
429
|
+
detail: `Tool call ${toolCallId} is not the ask this conversation is waiting on.`,
|
|
430
|
+
status: 409,
|
|
431
|
+
title: "This ask is no longer pending",
|
|
432
|
+
});
|
|
433
|
+
|
|
434
|
+
/*
|
|
435
|
+
* The ask updates below are pipeline updates because MongoDB rejects `$push` and a positional
|
|
436
|
+
* `$set` on the same array in one update. Values go in through `$literal` so a string that starts
|
|
437
|
+
* with `$` is not read as a field path.
|
|
438
|
+
*/
|
|
439
|
+
|
|
440
|
+
/** Inside a `$map` over `prompts` as `row`: true for the ask's call row while it has `status`. */
|
|
441
|
+
const isAskCallRow = ({
|
|
442
|
+
status,
|
|
443
|
+
toolCallId,
|
|
444
|
+
}: {
|
|
445
|
+
status: GptHistoryAskStatus;
|
|
446
|
+
toolCallId: string;
|
|
447
|
+
}): Record<string, unknown> => ({
|
|
448
|
+
$and: [
|
|
449
|
+
{$eq: ["$$row.type", "tool-call"]},
|
|
450
|
+
{$eq: ["$$row.toolCallId", {$literal: toolCallId}]},
|
|
451
|
+
{$eq: ["$$row.ask.status", {$literal: status}]},
|
|
452
|
+
],
|
|
453
|
+
});
|
|
454
|
+
|
|
455
|
+
/** A `$map` over `rows` that sets the ask's call row from status `from` to status `to`. */
|
|
456
|
+
const withAskCallStatus = ({
|
|
457
|
+
from,
|
|
458
|
+
rows,
|
|
459
|
+
to,
|
|
460
|
+
toolCallId,
|
|
461
|
+
}: {
|
|
462
|
+
from: GptHistoryAskStatus;
|
|
463
|
+
rows: unknown;
|
|
464
|
+
to: GptHistoryAskStatus;
|
|
465
|
+
toolCallId: string;
|
|
466
|
+
}): Record<string, unknown> => {
|
|
467
|
+
const markedRow = {
|
|
468
|
+
$mergeObjects: ["$$row", {ask: {$mergeObjects: ["$$row.ask", {status: {$literal: to}}]}}],
|
|
469
|
+
};
|
|
470
|
+
return {
|
|
471
|
+
$map: {
|
|
472
|
+
as: "row",
|
|
473
|
+
in: {$cond: [isAskCallRow({status: from, toolCallId}), markedRow, "$$row"]},
|
|
474
|
+
input: rows,
|
|
475
|
+
},
|
|
476
|
+
};
|
|
477
|
+
};
|
|
478
|
+
|
|
479
|
+
/** `prompts` with the ask's call row marked `status` and its result row appended. */
|
|
480
|
+
const promptsWithAskResult = ({
|
|
481
|
+
result,
|
|
482
|
+
status,
|
|
483
|
+
toolCallId,
|
|
484
|
+
toolName,
|
|
485
|
+
}: {
|
|
486
|
+
result: AskResponse;
|
|
487
|
+
status: GptHistoryAskStatus;
|
|
488
|
+
toolCallId: string;
|
|
489
|
+
toolName: string;
|
|
490
|
+
}): Record<string, unknown> => {
|
|
491
|
+
const resultRow: GptHistoryPrompt = {
|
|
492
|
+
result,
|
|
493
|
+
text: `Tool result: ${toolName}`,
|
|
494
|
+
toolCallId,
|
|
495
|
+
toolName,
|
|
496
|
+
type: "tool-result",
|
|
497
|
+
};
|
|
498
|
+
const markedRows = withAskCallStatus({
|
|
499
|
+
from: "pending",
|
|
500
|
+
rows: "$prompts",
|
|
501
|
+
to: status,
|
|
502
|
+
toolCallId,
|
|
503
|
+
});
|
|
504
|
+
return {$concatArrays: [markedRows, [{$literal: resultRow}]]};
|
|
505
|
+
};
|
|
506
|
+
|
|
507
|
+
/**
|
|
508
|
+
* Clears the pending ask, stores its result row, and marks its call row in one atomic update, so
|
|
509
|
+
* an ask resolves once even when two requests answer it at the same time. Returns null when the
|
|
510
|
+
* conversation no longer waits on this ask.
|
|
511
|
+
*/
|
|
512
|
+
const resolvePendingAsk = async ({
|
|
513
|
+
history,
|
|
514
|
+
result,
|
|
515
|
+
status,
|
|
516
|
+
toolCallId,
|
|
517
|
+
toolName,
|
|
518
|
+
}: {
|
|
519
|
+
history: GptHistoryDocument;
|
|
520
|
+
result: AskResponse;
|
|
521
|
+
status: GptHistoryAskStatus;
|
|
522
|
+
toolCallId: string;
|
|
523
|
+
toolName: string;
|
|
524
|
+
}): Promise<GptHistoryDocument | null> =>
|
|
525
|
+
GptHistory.findOneAndUpdate(
|
|
526
|
+
{_id: history._id, "pendingAsk.toolCallId": toolCallId},
|
|
527
|
+
[
|
|
528
|
+
{
|
|
529
|
+
$set: {
|
|
530
|
+
prompts: promptsWithAskResult({result, status, toolCallId, toolName}),
|
|
531
|
+
updated: {$literal: DateTime.now().toJSDate()},
|
|
532
|
+
},
|
|
533
|
+
},
|
|
534
|
+
{$unset: "pendingAsk"},
|
|
535
|
+
],
|
|
536
|
+
{returnDocument: "after", updatePipeline: true}
|
|
537
|
+
);
|
|
538
|
+
|
|
539
|
+
/**
|
|
540
|
+
* Undoes `resolvePendingAsk` for an answer whose turn failed before the client saw any output, so
|
|
541
|
+
* the user can answer again. Only applies while the conversation is as the answer left it: no
|
|
542
|
+
* ask is pending and it still has `rowCount` rows, the last being the answer's result row.
|
|
543
|
+
* Returns whether it applied.
|
|
544
|
+
*/
|
|
545
|
+
const restorePendingAsk = async ({
|
|
546
|
+
answer,
|
|
547
|
+
history,
|
|
548
|
+
rowCount,
|
|
549
|
+
}: {
|
|
550
|
+
answer: PendingAnswer;
|
|
551
|
+
history: GptHistoryDocument;
|
|
552
|
+
/** The row count right after the answer was stored. */
|
|
553
|
+
rowCount: number;
|
|
554
|
+
}): Promise<boolean> => {
|
|
555
|
+
try {
|
|
556
|
+
const restored = await GptHistory.findOneAndUpdate(
|
|
557
|
+
{_id: history._id, pendingAsk: null, prompts: {$size: rowCount}},
|
|
558
|
+
[
|
|
559
|
+
{
|
|
560
|
+
$set: {
|
|
561
|
+
pendingAsk: {$literal: answer.pendingAsk},
|
|
562
|
+
prompts: withAskCallStatus({
|
|
563
|
+
from: answer.status,
|
|
564
|
+
rows: {$slice: ["$prompts", rowCount - 1]},
|
|
565
|
+
to: "pending",
|
|
566
|
+
toolCallId: answer.toolCallId,
|
|
567
|
+
}),
|
|
568
|
+
},
|
|
569
|
+
},
|
|
570
|
+
],
|
|
571
|
+
{returnDocument: "after", updatePipeline: true}
|
|
572
|
+
);
|
|
573
|
+
return restored !== null;
|
|
574
|
+
} catch (error) {
|
|
575
|
+
logger.error("Failed to put back an ask whose answer's turn failed", {
|
|
576
|
+
error: error instanceof Error ? error.message : String(error),
|
|
577
|
+
historyId: history._id.toString(),
|
|
578
|
+
toolCallId: answer.toolCallId,
|
|
579
|
+
});
|
|
580
|
+
return false;
|
|
581
|
+
}
|
|
582
|
+
};
|
|
583
|
+
|
|
584
|
+
/**
|
|
585
|
+
* Cancels an ask whose call row was saved pending by a turn that failed before making it the
|
|
586
|
+
* pending ask, so no row waits on an ask the conversation does not.
|
|
587
|
+
*/
|
|
588
|
+
const cancelUnclaimedAsk = async ({
|
|
589
|
+
ask,
|
|
590
|
+
history,
|
|
591
|
+
}: {
|
|
592
|
+
ask: AskCall;
|
|
593
|
+
history: GptHistoryDocument;
|
|
594
|
+
}): Promise<void> => {
|
|
595
|
+
const {toolCallId} = ask;
|
|
596
|
+
try {
|
|
597
|
+
await GptHistory.findOneAndUpdate(
|
|
598
|
+
{
|
|
599
|
+
_id: history._id,
|
|
600
|
+
"pendingAsk.toolCallId": {$ne: toolCallId},
|
|
601
|
+
prompts: {$elemMatch: {"ask.status": "pending", toolCallId, type: "tool-call"}},
|
|
602
|
+
},
|
|
603
|
+
[
|
|
604
|
+
{
|
|
605
|
+
$set: {
|
|
606
|
+
prompts: promptsWithAskResult({
|
|
607
|
+
result: {action: "cancel"},
|
|
608
|
+
status: "cancelled",
|
|
609
|
+
toolCallId,
|
|
610
|
+
toolName: askRowToolName(ask),
|
|
611
|
+
}),
|
|
612
|
+
},
|
|
613
|
+
},
|
|
614
|
+
],
|
|
615
|
+
{updatePipeline: true}
|
|
616
|
+
);
|
|
617
|
+
} catch (error) {
|
|
618
|
+
logger.error("Failed to cancel an ask a failed turn left pending", {
|
|
619
|
+
error: error instanceof Error ? error.message : String(error),
|
|
620
|
+
historyId: history._id.toString(),
|
|
621
|
+
toolCallId,
|
|
622
|
+
});
|
|
623
|
+
}
|
|
624
|
+
};
|
|
625
|
+
|
|
626
|
+
/** How many times a new message retries cancelling an ask that other requests keep replacing. */
|
|
627
|
+
const MAX_CANCEL_ATTEMPTS = 3;
|
|
628
|
+
|
|
629
|
+
/**
|
|
630
|
+
* Cancels the pending ask because the user sent a message instead of answering. When another
|
|
631
|
+
* request resolves the ask first, the message goes ahead as a normal prompt on the reloaded
|
|
632
|
+
* history, cancelling any ask that took its place.
|
|
633
|
+
*/
|
|
634
|
+
const cancelPendingAsk = async (
|
|
635
|
+
loaded: GptHistoryDocument
|
|
636
|
+
): Promise<{history: GptHistoryDocument; resolvedAsk?: ResolvedAsk}> => {
|
|
637
|
+
let history = loaded;
|
|
638
|
+
for (let attempt = 0; attempt < MAX_CANCEL_ATTEMPTS && history.pendingAsk; attempt++) {
|
|
639
|
+
const pending = history.pendingAsk;
|
|
640
|
+
const {kind, toolCallId} = pending;
|
|
641
|
+
const cancelled = await resolvePendingAsk({
|
|
642
|
+
history,
|
|
643
|
+
result: {action: "cancel", reason: ASK_CANCEL_REASONS.userSentMessage},
|
|
644
|
+
status: "cancelled",
|
|
645
|
+
toolCallId,
|
|
646
|
+
toolName: askRowToolName(pending),
|
|
647
|
+
});
|
|
648
|
+
if (cancelled) {
|
|
649
|
+
return {
|
|
650
|
+
history: cancelled,
|
|
651
|
+
resolvedAsk: {action: "cancel", kind, toolCallId, ...approvalFieldsOf(pending)},
|
|
652
|
+
};
|
|
653
|
+
}
|
|
654
|
+
const reloaded = await GptHistory.findById(history._id);
|
|
655
|
+
if (!reloaded) {
|
|
656
|
+
throw new APIError({status: 404, title: "History not found"});
|
|
657
|
+
}
|
|
658
|
+
history = reloaded;
|
|
659
|
+
}
|
|
660
|
+
if (history.pendingAsk) {
|
|
661
|
+
logger.warn("Sent a message without cancelling an ask that other requests kept replacing", {
|
|
662
|
+
historyId: history._id.toString(),
|
|
663
|
+
toolCallId: history.pendingAsk.toolCallId,
|
|
664
|
+
});
|
|
665
|
+
}
|
|
666
|
+
return {history};
|
|
667
|
+
};
|
|
668
|
+
|
|
669
|
+
/**
|
|
670
|
+
* Appends the turn's rows in one atomic `$push`, so rows another turn saved meanwhile are kept.
|
|
671
|
+
* A new conversation is inserted with its rows instead. Returns the saved history and the index
|
|
672
|
+
* of the turn's first row.
|
|
673
|
+
*/
|
|
674
|
+
const appendTurnRows = async ({
|
|
675
|
+
history,
|
|
676
|
+
isNewHistory,
|
|
677
|
+
projectId,
|
|
678
|
+
rows,
|
|
679
|
+
}: {
|
|
680
|
+
history: GptHistoryDocument;
|
|
681
|
+
isNewHistory: boolean;
|
|
682
|
+
projectId: unknown;
|
|
683
|
+
rows: GptHistoryPrompt[];
|
|
684
|
+
}): Promise<{firstRowIndex: number; history: GptHistoryDocument}> => {
|
|
685
|
+
if (isNewHistory) {
|
|
686
|
+
history.prompts.push(...rows);
|
|
687
|
+
await history.save();
|
|
688
|
+
return {firstRowIndex: 0, history};
|
|
689
|
+
}
|
|
690
|
+
if (projectId && !history.projectId) {
|
|
691
|
+
await GptHistory.updateOne({_id: history._id, projectId: null}, {$set: {projectId}});
|
|
692
|
+
}
|
|
693
|
+
if (rows.length === 0) {
|
|
694
|
+
return {firstRowIndex: history.prompts.length, history};
|
|
695
|
+
}
|
|
696
|
+
const saved = await GptHistory.findOneAndUpdate(
|
|
697
|
+
{_id: history._id},
|
|
698
|
+
{$push: {prompts: {$each: rows}}, $set: {updated: DateTime.now().toJSDate()}},
|
|
699
|
+
{returnDocument: "after"}
|
|
700
|
+
);
|
|
701
|
+
if (!saved) {
|
|
702
|
+
throw new APIError({status: 404, title: "History not found"});
|
|
703
|
+
}
|
|
704
|
+
return {firstRowIndex: saved.prompts.length - rows.length, history: saved};
|
|
705
|
+
};
|
|
706
|
+
|
|
707
|
+
/**
|
|
708
|
+
* Stores the generated title unless another turn titled the conversation first, and returns the
|
|
709
|
+
* title the conversation ends up with.
|
|
710
|
+
*/
|
|
711
|
+
const saveTitle = async ({
|
|
712
|
+
history,
|
|
713
|
+
title,
|
|
714
|
+
}: {
|
|
715
|
+
history: GptHistoryDocument;
|
|
716
|
+
title: string;
|
|
717
|
+
}): Promise<string | undefined> => {
|
|
718
|
+
const titled = await GptHistory.findOneAndUpdate(
|
|
719
|
+
{_id: history._id, title: {$in: [null, ""]}},
|
|
720
|
+
{$set: {title}},
|
|
721
|
+
{returnDocument: "after"}
|
|
722
|
+
);
|
|
723
|
+
if (titled) {
|
|
724
|
+
return titled.title;
|
|
725
|
+
}
|
|
726
|
+
const current = await GptHistory.findById(history._id);
|
|
727
|
+
return current?.title ?? undefined;
|
|
728
|
+
};
|
|
729
|
+
|
|
730
|
+
/**
|
|
731
|
+
* Makes the ask the conversation's pending ask in one atomic update, unless another turn's ask is
|
|
732
|
+
* already pending: then the ask is answered with `cancel` (`one_ask_at_a_time`) instead, so two
|
|
733
|
+
* turns that pause at the same time cannot overwrite each other. The ask's call row must already
|
|
734
|
+
* be saved. Returns whether the ask became the pending ask.
|
|
735
|
+
*/
|
|
736
|
+
const claimPendingAsk = async ({
|
|
737
|
+
ask,
|
|
738
|
+
history,
|
|
739
|
+
promptIndex,
|
|
740
|
+
responseMessages,
|
|
741
|
+
}: {
|
|
742
|
+
ask: AskCall & {simple: SimpleCard};
|
|
743
|
+
history: GptHistoryDocument;
|
|
744
|
+
/** How many leading rows replay as the paused turn's history, its user message included. */
|
|
745
|
+
promptIndex: number;
|
|
746
|
+
responseMessages: ModelMessage[];
|
|
747
|
+
}): Promise<boolean> => {
|
|
748
|
+
const {input, kind, simple, toolCallId} = ask;
|
|
749
|
+
const waitsOnNoAsk = {$eq: [{$ifNull: ["$pendingAsk", null]}, null]};
|
|
750
|
+
const approval = approvalFieldsOf(ask);
|
|
751
|
+
const pendingAsk = {
|
|
752
|
+
created: {$literal: DateTime.now().toJSDate()},
|
|
753
|
+
input: {$literal: input},
|
|
754
|
+
kind: {$literal: kind},
|
|
755
|
+
promptIndex: {$literal: promptIndex},
|
|
756
|
+
responseMessages: {$literal: responseMessages},
|
|
757
|
+
simple: {$literal: simple},
|
|
758
|
+
toolCallId: {$literal: toolCallId},
|
|
759
|
+
...(approval.origin
|
|
760
|
+
? {
|
|
761
|
+
approvalId: {$literal: toolCallId},
|
|
762
|
+
origin: {$literal: approval.origin},
|
|
763
|
+
toolName: {$literal: approval.toolName},
|
|
764
|
+
}
|
|
765
|
+
: {}),
|
|
766
|
+
};
|
|
767
|
+
const cancelledPrompts = promptsWithAskResult({
|
|
768
|
+
result: {action: "cancel", reason: ASK_CANCEL_REASONS.oneAskAtATime},
|
|
769
|
+
status: "cancelled",
|
|
770
|
+
toolCallId,
|
|
771
|
+
toolName: askRowToolName(ask),
|
|
772
|
+
});
|
|
773
|
+
const saved = await GptHistory.findOneAndUpdate(
|
|
774
|
+
{_id: history._id},
|
|
775
|
+
[
|
|
776
|
+
{
|
|
777
|
+
$set: {
|
|
778
|
+
pendingAsk: {$cond: [waitsOnNoAsk, pendingAsk, "$pendingAsk"]},
|
|
779
|
+
prompts: {$cond: [waitsOnNoAsk, "$prompts", cancelledPrompts]},
|
|
780
|
+
},
|
|
781
|
+
},
|
|
782
|
+
],
|
|
783
|
+
{returnDocument: "after", updatePipeline: true}
|
|
784
|
+
);
|
|
785
|
+
return saved?.pendingAsk?.toolCallId === toolCallId;
|
|
786
|
+
};
|
|
787
|
+
|
|
788
|
+
/**
|
|
789
|
+
* Reject attachment URLs that only exist on the client (blob:, file:, content:, ph:, ...).
|
|
790
|
+
* They cannot be read by the model provider or reloaded from history later.
|
|
791
|
+
*/
|
|
792
|
+
const validateAttachments = (attachments: unknown): ChatAttachment[] => {
|
|
793
|
+
if (attachments === undefined || attachments === null) {
|
|
794
|
+
return [];
|
|
795
|
+
}
|
|
796
|
+
if (!Array.isArray(attachments)) {
|
|
797
|
+
throw new APIError({status: 400, title: "attachments must be an array"});
|
|
798
|
+
}
|
|
799
|
+
for (const attachment of attachments as ChatAttachment[]) {
|
|
800
|
+
if (attachment?.type !== "image" && attachment?.type !== "file") {
|
|
801
|
+
continue;
|
|
802
|
+
}
|
|
803
|
+
if (typeof attachment.url !== "string" || !DURABLE_URL_PATTERN.test(attachment.url)) {
|
|
804
|
+
const scheme =
|
|
805
|
+
typeof attachment.url === "string" ? (attachment.url.split(":")[0] ?? "") : "missing";
|
|
806
|
+
throw new APIError({
|
|
807
|
+
detail:
|
|
808
|
+
`Attachment "${attachment.filename ?? attachment.type}" uses a "${scheme}" URL. ` +
|
|
809
|
+
"Upload the file first (POST /files/upload) or send it as a data: URL.",
|
|
810
|
+
status: 400,
|
|
811
|
+
title: "Attachment URL must be an http(s) or data: URL",
|
|
812
|
+
});
|
|
813
|
+
}
|
|
814
|
+
}
|
|
815
|
+
return attachments as ChatAttachment[];
|
|
816
|
+
};
|
|
817
|
+
|
|
818
|
+
const decodeDataUrl = (url: string): {buffer: Buffer; mimeType?: string} | undefined => {
|
|
819
|
+
const match = DATA_URL_PATTERN.exec(url);
|
|
820
|
+
if (!match) {
|
|
821
|
+
return undefined;
|
|
822
|
+
}
|
|
823
|
+
const [, mimeType, , base64Flag, payload] = match;
|
|
824
|
+
const buffer = base64Flag
|
|
825
|
+
? Buffer.from(payload, "base64")
|
|
826
|
+
: Buffer.from(decodeURIComponent(payload), "utf8");
|
|
827
|
+
return {buffer, mimeType: mimeType || undefined};
|
|
828
|
+
};
|
|
829
|
+
|
|
830
|
+
/** Build model and history content parts, uploading data: attachments when storage is configured. */
|
|
831
|
+
const buildAttachmentParts = async ({
|
|
832
|
+
attachments,
|
|
833
|
+
fileStorageService,
|
|
834
|
+
userId,
|
|
835
|
+
}: {
|
|
836
|
+
attachments: ChatAttachment[];
|
|
837
|
+
fileStorageService?: GptRouteOptions["fileStorageService"];
|
|
838
|
+
userId?: mongoose.Types.ObjectId;
|
|
839
|
+
}): Promise<{modelParts: MessageContentPart[]; storedParts: MessageContentPart[]}> => {
|
|
840
|
+
const modelParts: MessageContentPart[] = [];
|
|
841
|
+
const storedParts: MessageContentPart[] = [];
|
|
842
|
+
for (const attachment of attachments) {
|
|
843
|
+
if (attachment.type !== "image" && attachment.type !== "file") {
|
|
844
|
+
continue;
|
|
845
|
+
}
|
|
846
|
+
const url = attachment.url as string;
|
|
847
|
+
const modelPart: MessageContentPart =
|
|
848
|
+
attachment.type === "image"
|
|
849
|
+
? {mimeType: attachment.mimeType, type: "image", url}
|
|
850
|
+
: {
|
|
851
|
+
filename: attachment.filename,
|
|
852
|
+
mimeType: attachment.mimeType as string,
|
|
853
|
+
type: "file",
|
|
854
|
+
url,
|
|
855
|
+
};
|
|
856
|
+
modelParts.push(modelPart);
|
|
857
|
+
|
|
858
|
+
const decoded = url.startsWith("data:") ? decodeDataUrl(url) : undefined;
|
|
859
|
+
if (!fileStorageService || !("upload" in fileStorageService) || !userId || !decoded) {
|
|
860
|
+
storedParts.push(modelPart);
|
|
861
|
+
continue;
|
|
862
|
+
}
|
|
863
|
+
const mimeType = attachment.mimeType ?? decoded.mimeType ?? "application/octet-stream";
|
|
864
|
+
const extension = mimeType.split("/")[1]?.split("+")[0] ?? "bin";
|
|
865
|
+
try {
|
|
866
|
+
const uploaded = await fileStorageService.upload({
|
|
867
|
+
buffer: decoded.buffer,
|
|
868
|
+
filename: attachment.filename ?? `attachment-${DateTime.now().toMillis()}.${extension}`,
|
|
869
|
+
mimeType,
|
|
870
|
+
userId,
|
|
871
|
+
});
|
|
872
|
+
storedParts.push({...modelPart, gcsKey: uploaded.gcsKey, url: uploaded.url});
|
|
873
|
+
} catch (error) {
|
|
874
|
+
throw new APIError({
|
|
875
|
+
cause: error,
|
|
876
|
+
detail: error instanceof Error ? error.message : String(error),
|
|
877
|
+
status: 502,
|
|
878
|
+
title: "Attachment upload failed",
|
|
879
|
+
});
|
|
880
|
+
}
|
|
881
|
+
}
|
|
882
|
+
return {modelParts, storedParts};
|
|
883
|
+
};
|
|
884
|
+
|
|
885
|
+
const toPlainPrompt = (prompt: GptHistoryPrompt): GptHistoryPrompt => {
|
|
886
|
+
const maybeSubdoc = prompt as GptHistoryPrompt & {toObject?: () => GptHistoryPrompt};
|
|
887
|
+
return typeof maybeSubdoc.toObject === "function" ? maybeSubdoc.toObject() : prompt;
|
|
888
|
+
};
|
|
889
|
+
|
|
890
|
+
/** Swap stored attachment references for short-lived signed URLs the model provider can fetch. */
|
|
891
|
+
const resolveStoredAttachmentUrls = async (
|
|
892
|
+
prompts: GptHistoryPrompt[],
|
|
893
|
+
fileStorageService?: GptRouteOptions["fileStorageService"]
|
|
894
|
+
): Promise<GptHistoryPrompt[]> => {
|
|
895
|
+
if (!fileStorageService || !("getSignedUrl" in fileStorageService)) {
|
|
896
|
+
return prompts;
|
|
897
|
+
}
|
|
898
|
+
return Promise.all(
|
|
899
|
+
prompts.map(async (prompt) => {
|
|
900
|
+
if (!prompt.content?.some((part) => part.type !== "text" && part.gcsKey)) {
|
|
901
|
+
return prompt;
|
|
902
|
+
}
|
|
903
|
+
const plain = toPlainPrompt(prompt);
|
|
904
|
+
const content = await Promise.all(
|
|
905
|
+
(plain.content ?? []).map(async (part) => {
|
|
906
|
+
if (part.type === "text" || !part.gcsKey) {
|
|
907
|
+
return part;
|
|
908
|
+
}
|
|
909
|
+
try {
|
|
910
|
+
return {...part, url: await fileStorageService.getSignedUrl(part.gcsKey)};
|
|
911
|
+
} catch (error) {
|
|
912
|
+
logger.warn("Could not sign stored attachment URL", {
|
|
913
|
+
error: error instanceof Error ? error.message : String(error),
|
|
914
|
+
gcsKey: part.gcsKey,
|
|
915
|
+
});
|
|
916
|
+
return part;
|
|
917
|
+
}
|
|
918
|
+
})
|
|
919
|
+
);
|
|
920
|
+
return {...plain, content};
|
|
921
|
+
})
|
|
922
|
+
);
|
|
923
|
+
};
|
|
924
|
+
|
|
925
|
+
/**
|
|
926
|
+
* The answer as the conversation stores it, and the tool result the model sees this turn. An
|
|
927
|
+
* accepted `files` answer has its files loaded and checked: the model sees them, and the stored
|
|
928
|
+
* answer keeps only their metadata.
|
|
929
|
+
*/
|
|
930
|
+
const prepareAnswer = async ({
|
|
931
|
+
answer,
|
|
932
|
+
fileStorageService,
|
|
933
|
+
kind,
|
|
934
|
+
maxFileSizeBytes,
|
|
935
|
+
userId,
|
|
936
|
+
}: {
|
|
937
|
+
answer: AskResponse;
|
|
938
|
+
fileStorageService: GptRouteOptions["fileStorageService"];
|
|
939
|
+
kind: AskKind;
|
|
940
|
+
maxFileSizeBytes: number;
|
|
941
|
+
userId: mongoose.Types.ObjectId | undefined;
|
|
942
|
+
}): Promise<{answerOutput?: ToolResultPart["output"]; storedAnswer: AskResponse}> => {
|
|
943
|
+
if (kind !== "files" || answer.action !== "accept") {
|
|
944
|
+
return {storedAnswer: answer};
|
|
945
|
+
}
|
|
946
|
+
const files = await resolveAskFiles({
|
|
947
|
+
fileStorageService,
|
|
948
|
+
files: (answer.content as FilesAnswer).files,
|
|
949
|
+
maxFileSizeBytes,
|
|
950
|
+
userId,
|
|
951
|
+
});
|
|
952
|
+
return {answerOutput: askFilesModelOutput(files), storedAnswer: storedFilesAnswer(files)};
|
|
953
|
+
};
|
|
954
|
+
|
|
955
|
+
/**
|
|
956
|
+
* Loads the history and prepares the turn's messages. A new prompt first cancels any pending
|
|
957
|
+
* ask. An answer is checked and replays the paused turn with the answer appended; `runChatTurn`
|
|
958
|
+
* resolves the pending ask once the turn's tools and system prompt are ready.
|
|
959
|
+
*/
|
|
960
|
+
const startTurn = async ({
|
|
961
|
+
aiService,
|
|
962
|
+
askAnswer,
|
|
963
|
+
body,
|
|
964
|
+
options,
|
|
965
|
+
req,
|
|
966
|
+
userId,
|
|
967
|
+
}: {
|
|
968
|
+
aiService: AIService;
|
|
969
|
+
askAnswer: AskAnswer | undefined;
|
|
970
|
+
body: Record<string, unknown>;
|
|
971
|
+
options: GptRouteOptions;
|
|
972
|
+
req: express.Request;
|
|
973
|
+
userId: mongoose.Types.ObjectId | undefined;
|
|
974
|
+
}): Promise<TurnStart> => {
|
|
975
|
+
const {attachments, historyId, projectId, prompt} = body;
|
|
976
|
+
const loaded = await loadHistory({historyId, projectId, userId});
|
|
977
|
+
const isNewHistory = !historyId;
|
|
978
|
+
|
|
979
|
+
if (askAnswer) {
|
|
980
|
+
const history = loaded;
|
|
981
|
+
const pending = history.pendingAsk;
|
|
982
|
+
if (!pending || pending.toolCallId !== askAnswer.toolCallId) {
|
|
983
|
+
throw staleAskError(askAnswer.toolCallId);
|
|
984
|
+
}
|
|
985
|
+
const maxFileSizeBytes = resolveMaxFileSizeBytes(options.asks);
|
|
986
|
+
const fields = validateAskResponse({
|
|
987
|
+
input: pending.input,
|
|
988
|
+
kind: pending.kind,
|
|
989
|
+
maxFileSizeBytes,
|
|
990
|
+
response: askAnswer.response,
|
|
991
|
+
});
|
|
992
|
+
if (fields.length > 0) {
|
|
993
|
+
throw invalidAskResponseError(fields);
|
|
994
|
+
}
|
|
995
|
+
const answer = askAnswer.response as AskResponse;
|
|
996
|
+
const {approvalId, kind, promptIndex, responseMessages, toolCallId} = pending;
|
|
997
|
+
const {answerOutput, storedAnswer} = await prepareAnswer({
|
|
998
|
+
answer,
|
|
999
|
+
fileStorageService: options.fileStorageService,
|
|
1000
|
+
kind,
|
|
1001
|
+
maxFileSizeBytes,
|
|
1002
|
+
userId,
|
|
1003
|
+
});
|
|
1004
|
+
// `minimize: false` keeps the empty objects the replayed messages need, such as `input: {}`.
|
|
1005
|
+
const storedPendingAsk = history.toObject({minimize: false, virtuals: false})
|
|
1006
|
+
.pendingAsk as GptHistoryPendingAsk;
|
|
1007
|
+
// Rows are only ever appended, and the paused turn's rows start at `promptIndex`, so these
|
|
1008
|
+
// rows are the same once the ask is resolved.
|
|
1009
|
+
const turnHistory = history.prompts.slice(0, promptIndex);
|
|
1010
|
+
const replayedMessages = completePausedTurn({
|
|
1011
|
+
answer: storedAnswer,
|
|
1012
|
+
approvalId,
|
|
1013
|
+
responseMessages,
|
|
1014
|
+
toolCallId,
|
|
1015
|
+
});
|
|
1016
|
+
const modelMessages = answerOutput
|
|
1017
|
+
? completePausedTurn({
|
|
1018
|
+
answer: storedAnswer,
|
|
1019
|
+
answerOutput,
|
|
1020
|
+
approvalId,
|
|
1021
|
+
responseMessages,
|
|
1022
|
+
toolCallId,
|
|
1023
|
+
})
|
|
1024
|
+
: replayedMessages;
|
|
1025
|
+
return {
|
|
1026
|
+
answer: {
|
|
1027
|
+
pendingAsk: storedPendingAsk,
|
|
1028
|
+
result: storedAnswer,
|
|
1029
|
+
status: answer.action === "cancel" ? "cancelled" : "answered",
|
|
1030
|
+
toolCallId,
|
|
1031
|
+
toolName: askRowToolName(pending),
|
|
1032
|
+
},
|
|
1033
|
+
history,
|
|
1034
|
+
isNewHistory: false,
|
|
1035
|
+
logPrompt: JSON.stringify(storedAnswer),
|
|
1036
|
+
messages: [...aiService.buildMessages(turnHistory), ...modelMessages],
|
|
1037
|
+
promptIndex,
|
|
1038
|
+
replayedMessages,
|
|
1039
|
+
resolvedAsk: {action: answer.action, kind, toolCallId, ...approvalFieldsOf(pending)},
|
|
1040
|
+
rows: [],
|
|
1041
|
+
titlePrompt: lastUserText(turnHistory),
|
|
1042
|
+
};
|
|
1043
|
+
}
|
|
1044
|
+
|
|
1045
|
+
if (Array.isArray(attachments) && attachments.length > 0) {
|
|
1046
|
+
await assertFileUploadsEnabled(req, options.fileUploadsEnabled);
|
|
1047
|
+
}
|
|
1048
|
+
const validAttachments = validateAttachments(attachments);
|
|
1049
|
+
const {history, resolvedAsk} = await cancelPendingAsk(loaded);
|
|
1050
|
+
|
|
1051
|
+
// If history doesn't have a projectId yet but one was provided, associate it
|
|
1052
|
+
if (isNewHistory && projectId) {
|
|
1053
|
+
history.projectId = projectId as mongoose.Types.ObjectId;
|
|
1054
|
+
}
|
|
1055
|
+
|
|
1056
|
+
const promptText = prompt as string;
|
|
1057
|
+
logger.debug("Processing attachments", {
|
|
1058
|
+
count: validAttachments.length,
|
|
1059
|
+
types: validAttachments.map((attachment) => ({
|
|
1060
|
+
mimeType: attachment.mimeType,
|
|
1061
|
+
type: attachment.type,
|
|
1062
|
+
urlLength: attachment.url?.length ?? 0,
|
|
1063
|
+
})),
|
|
1064
|
+
});
|
|
1065
|
+
const earlierPrompts = await resolveStoredAttachmentUrls(
|
|
1066
|
+
history.prompts,
|
|
1067
|
+
options.fileStorageService
|
|
1068
|
+
);
|
|
1069
|
+
const {modelParts, storedParts} = await buildAttachmentParts({
|
|
1070
|
+
attachments: validAttachments,
|
|
1071
|
+
fileStorageService: options.fileStorageService,
|
|
1072
|
+
userId,
|
|
1073
|
+
});
|
|
1074
|
+
const hasAttachments = storedParts.length > 0;
|
|
1075
|
+
const userRow: GptHistoryPrompt = {
|
|
1076
|
+
text: promptText,
|
|
1077
|
+
type: "user",
|
|
1078
|
+
...(hasAttachments ? {content: [{text: promptText, type: "text"}, ...storedParts]} : {}),
|
|
1079
|
+
};
|
|
1080
|
+
const modelUserRow: GptHistoryPrompt = hasAttachments
|
|
1081
|
+
? {...userRow, content: [{text: promptText, type: "text"}, ...modelParts]}
|
|
1082
|
+
: userRow;
|
|
1083
|
+
|
|
1084
|
+
logger.debug("Building messages", {
|
|
1085
|
+
attachmentCount: storedParts.length,
|
|
1086
|
+
historyLength: history.prompts.length + 1,
|
|
1087
|
+
});
|
|
1088
|
+
const messages = aiService.buildMessages([...earlierPrompts, modelUserRow]);
|
|
1089
|
+
logger.debug("Messages built", {messageCount: messages.length});
|
|
1090
|
+
return {
|
|
1091
|
+
history,
|
|
1092
|
+
isNewHistory,
|
|
1093
|
+
logPrompt: promptText,
|
|
1094
|
+
messages,
|
|
1095
|
+
replayedMessages: [],
|
|
1096
|
+
resolvedAsk,
|
|
1097
|
+
rows: [userRow],
|
|
1098
|
+
titlePrompt: promptText,
|
|
1099
|
+
};
|
|
1100
|
+
};
|
|
1101
|
+
|
|
1102
|
+
const lastUserText = (prompts: GptHistoryPrompt[]): string =>
|
|
1103
|
+
prompts.findLast((prompt) => prompt.type === "user")?.text ?? "";
|
|
1104
|
+
|
|
1105
|
+
/** The request's system prompt with project context and the Langfuse prompt prepended. */
|
|
1106
|
+
const buildSystemPrompt = async ({
|
|
1107
|
+
history,
|
|
1108
|
+
options,
|
|
1109
|
+
projectId,
|
|
1110
|
+
systemPrompt,
|
|
1111
|
+
userId,
|
|
1112
|
+
}: {
|
|
1113
|
+
history: GptHistoryDocument;
|
|
1114
|
+
options: GptRouteOptions;
|
|
1115
|
+
projectId: unknown;
|
|
1116
|
+
systemPrompt: unknown;
|
|
1117
|
+
userId: mongoose.Types.ObjectId | undefined;
|
|
1118
|
+
}): Promise<string | undefined> => {
|
|
1119
|
+
const effectiveProjectId = projectId ?? history.projectId;
|
|
1120
|
+
let effectiveSystemPrompt = systemPrompt as string | undefined;
|
|
1121
|
+
if (effectiveProjectId) {
|
|
1122
|
+
try {
|
|
1123
|
+
const project = await Project.findById(effectiveProjectId);
|
|
1124
|
+
if (project && project.userId.toString() === userId?.toString()) {
|
|
1125
|
+
const parts: string[] = [];
|
|
1126
|
+
if (project.systemContext) {
|
|
1127
|
+
parts.push(project.systemContext);
|
|
1128
|
+
}
|
|
1129
|
+
if (project.memories.length > 0) {
|
|
1130
|
+
parts.push(
|
|
1131
|
+
`## Relevant Memories\n${project.memories.map((m) => `- ${m.text}`).join("\n")}`
|
|
1132
|
+
);
|
|
1133
|
+
}
|
|
1134
|
+
if (parts.length > 0) {
|
|
1135
|
+
const projectContext = parts.join("\n\n");
|
|
1136
|
+
effectiveSystemPrompt = effectiveSystemPrompt
|
|
1137
|
+
? `${projectContext}\n\n${effectiveSystemPrompt}`
|
|
1138
|
+
: projectContext;
|
|
1139
|
+
}
|
|
1140
|
+
}
|
|
1141
|
+
} catch {
|
|
1142
|
+
// Project loading failure should not block the request
|
|
1143
|
+
}
|
|
1144
|
+
}
|
|
1145
|
+
|
|
1146
|
+
if (options.langfuseSystemPromptName) {
|
|
1147
|
+
try {
|
|
1148
|
+
const langfuseResult = await preparePromptForAI({
|
|
1149
|
+
promptName: options.langfuseSystemPromptName,
|
|
1150
|
+
userId: userId?.toString(),
|
|
1151
|
+
});
|
|
1152
|
+
const langfusePrompt =
|
|
1153
|
+
typeof langfuseResult.prompt === "string" ? langfuseResult.prompt : undefined;
|
|
1154
|
+
if (langfusePrompt) {
|
|
1155
|
+
effectiveSystemPrompt = effectiveSystemPrompt
|
|
1156
|
+
? `${langfusePrompt}\n\n${effectiveSystemPrompt}`
|
|
1157
|
+
: langfusePrompt;
|
|
1158
|
+
}
|
|
1159
|
+
} catch (err) {
|
|
1160
|
+
logger.debug(`Langfuse system prompt skipped: ${(err as Error).message}`);
|
|
1161
|
+
}
|
|
1162
|
+
}
|
|
1163
|
+
return effectiveSystemPrompt;
|
|
1164
|
+
};
|
|
1165
|
+
|
|
1166
|
+
/** Route, per-request, and MCP tools, plus the ask tools when asks are offered. */
|
|
1167
|
+
const collectTools = async ({
|
|
1168
|
+
askKinds,
|
|
1169
|
+
options,
|
|
1170
|
+
req,
|
|
1171
|
+
supportsTools,
|
|
1172
|
+
surface,
|
|
1173
|
+
}: {
|
|
1174
|
+
askKinds: AskKind[];
|
|
1175
|
+
options: GptRouteOptions;
|
|
1176
|
+
req: express.Request;
|
|
1177
|
+
supportsTools: boolean;
|
|
1178
|
+
surface: AskSurface;
|
|
1179
|
+
}): Promise<Record<string, Tool> | undefined> => {
|
|
1180
|
+
const {createRequestTools, mcpService, tools: routeTools} = options;
|
|
1181
|
+
const requestTools = createRequestTools ? createRequestTools(req) : undefined;
|
|
1182
|
+
if (!supportsTools || (!routeTools && !requestTools && !mcpService && askKinds.length === 0)) {
|
|
1183
|
+
return undefined;
|
|
1184
|
+
}
|
|
1185
|
+
const allTools: Record<string, Tool> = {...(routeTools ?? {}), ...(requestTools ?? {})};
|
|
1186
|
+
if (mcpService) {
|
|
1187
|
+
try {
|
|
1188
|
+
const mcpTools = await mcpService.getTools();
|
|
1189
|
+
Object.assign(allTools, mcpTools);
|
|
1190
|
+
} catch {
|
|
1191
|
+
// MCP tool discovery failure should not block the request
|
|
1192
|
+
}
|
|
1193
|
+
}
|
|
1194
|
+
if (askKinds.length === 0) {
|
|
1195
|
+
return allTools;
|
|
1196
|
+
}
|
|
1197
|
+
return {...withoutReservedToolNames(allTools), ...createAskTools({kinds: askKinds, surface})};
|
|
1198
|
+
};
|
|
1199
|
+
|
|
1200
|
+
const resolveUiBlocks = (uiBlocks: GptRouteOptions["uiBlocks"]): UiBlocksOptions | undefined => {
|
|
1201
|
+
if (!uiBlocks) {
|
|
1202
|
+
return undefined;
|
|
1203
|
+
}
|
|
1204
|
+
if (uiBlocks === true) {
|
|
1205
|
+
return {};
|
|
1206
|
+
}
|
|
1207
|
+
return uiBlocks;
|
|
1208
|
+
};
|
|
1209
|
+
|
|
1210
|
+
/**
|
|
1211
|
+
* The system prompt plus the asks section, the blocks document section, and the compact line
|
|
1212
|
+
* when those options are on. With none of them, it is the request's system prompt unchanged.
|
|
1213
|
+
*/
|
|
1214
|
+
const withTurnSystemPrompt = ({
|
|
1215
|
+
askKinds,
|
|
1216
|
+
blocksPrompt,
|
|
1217
|
+
surface,
|
|
1218
|
+
systemPrompt,
|
|
1219
|
+
}: {
|
|
1220
|
+
askKinds: AskKind[];
|
|
1221
|
+
blocksPrompt?: string;
|
|
1222
|
+
surface: AskSurface;
|
|
1223
|
+
systemPrompt: string | undefined;
|
|
1224
|
+
}): string | undefined => {
|
|
1225
|
+
const sections = [
|
|
1226
|
+
...(askKinds.length > 0 ? [buildAsksSystemPrompt({kinds: askKinds, surface})] : []),
|
|
1227
|
+
...(blocksPrompt ? [blocksPrompt] : []),
|
|
1228
|
+
...(surface === "compact" ? [COMPACT_SURFACE_SYSTEM_PROMPT] : []),
|
|
1229
|
+
];
|
|
1230
|
+
if (sections.length === 0) {
|
|
1231
|
+
return systemPrompt;
|
|
1232
|
+
}
|
|
1233
|
+
return [systemPrompt, ...sections].filter(Boolean).join("\n\n");
|
|
1234
|
+
};
|
|
1235
|
+
|
|
1236
|
+
const checkBlockDocument = (
|
|
1237
|
+
text: string,
|
|
1238
|
+
hostActions: readonly string[] | undefined,
|
|
1239
|
+
allowHtml: boolean,
|
|
1240
|
+
imageHosts?: readonly string[]
|
|
1241
|
+
): {errors: BlockError[]; ok: boolean; warnings: BlockError[]} => {
|
|
1242
|
+
const parsed = parseBlocks(text);
|
|
1243
|
+
if (!parsed.ok) {
|
|
1244
|
+
return {errors: parsed.errors, ok: false, warnings: []};
|
|
1245
|
+
}
|
|
1246
|
+
const validated = validateBlocks(parsed.value, {
|
|
1247
|
+
...(allowHtml ? {allowHtml: true} : {}),
|
|
1248
|
+
...(hostActions ? {hostActions} : {}),
|
|
1249
|
+
...(imageHosts ? {imageHosts} : {}),
|
|
1250
|
+
});
|
|
1251
|
+
return {
|
|
1252
|
+
errors: validated.ok ? [] : validated.errors,
|
|
1253
|
+
ok: validated.ok,
|
|
1254
|
+
warnings: validated.warnings,
|
|
1255
|
+
};
|
|
1256
|
+
};
|
|
1257
|
+
|
|
1258
|
+
const storedBlockText = (text: string, errors: BlockError[]): string => {
|
|
1259
|
+
if (errors.length === 0) {
|
|
1260
|
+
return text;
|
|
1261
|
+
}
|
|
1262
|
+
const lines = errors.map((error) => `- ${error.path} ${error.code} ${error.message}`);
|
|
1263
|
+
return `${text}\n\nBlock validation errors:\n${lines.join("\n")}`;
|
|
1264
|
+
};
|
|
1265
|
+
|
|
1266
|
+
const repairBlockDocument = async ({
|
|
1267
|
+
aiService,
|
|
1268
|
+
errors,
|
|
1269
|
+
text,
|
|
1270
|
+
}: {
|
|
1271
|
+
aiService: AIService;
|
|
1272
|
+
errors: BlockError[];
|
|
1273
|
+
text: string;
|
|
1274
|
+
}): Promise<string | undefined> => {
|
|
1275
|
+
try {
|
|
1276
|
+
const errorLines = errors.map((error) => `- ${error.path} ${error.code} ${error.message}`);
|
|
1277
|
+
const repaired = await aiService.generateText({
|
|
1278
|
+
prompt: `Document:\n${text}\n\nErrors:\n${errorLines.join("\n")}`,
|
|
1279
|
+
systemPrompt: UI_BLOCKS_REPAIR_SYSTEM_PROMPT,
|
|
1280
|
+
});
|
|
1281
|
+
const trimmed = repaired.trim();
|
|
1282
|
+
return trimmed.length > 0 ? trimmed : undefined;
|
|
1283
|
+
} catch (error) {
|
|
1284
|
+
logger.warn("Block document repair failed", {
|
|
1285
|
+
error: error instanceof Error ? error.message : String(error),
|
|
1286
|
+
});
|
|
1287
|
+
return undefined;
|
|
1288
|
+
}
|
|
1289
|
+
};
|
|
1290
|
+
|
|
1291
|
+
/** What the turn needs to turn a host tool's approval request into an approval ask. */
|
|
1292
|
+
interface ApprovalContext {
|
|
1293
|
+
asks: GptRouteOptions["asks"];
|
|
1294
|
+
surface: AskSurface;
|
|
1295
|
+
tools: Record<string, Tool>;
|
|
1296
|
+
}
|
|
1297
|
+
|
|
1298
|
+
/**
|
|
1299
|
+
* Forwards the model stream to the sink and records what it sent in `record`, which stays valid
|
|
1300
|
+
* when the stream fails partway. Ask calls are collected, not forwarded: an invalid one goes back
|
|
1301
|
+
* to the model as a tool error, and a valid one pauses the turn. So is each approval request for
|
|
1302
|
+
* a host tool, as an approval ask; without `approvals` (asks are off) the tool just does not run.
|
|
1303
|
+
* The collected asks keep the order of their tool calls, so the first one the model made pauses
|
|
1304
|
+
* the turn. `deniedReasons` holds why each approval this turn resumes with was denied.
|
|
1305
|
+
*/
|
|
1306
|
+
const consumeStream = async ({
|
|
1307
|
+
approvals,
|
|
1308
|
+
askKinds,
|
|
1309
|
+
deniedReasons,
|
|
1310
|
+
holdAssistantText = false,
|
|
1311
|
+
onStreamStart,
|
|
1312
|
+
record,
|
|
1313
|
+
result,
|
|
1314
|
+
sink,
|
|
1315
|
+
}: {
|
|
1316
|
+
approvals?: ApprovalContext;
|
|
1317
|
+
askKinds: AskKind[];
|
|
1318
|
+
deniedReasons: Map<string, string | undefined>;
|
|
1319
|
+
/**
|
|
1320
|
+
* Record assistant text without sending it. The caller sends one document after id-fill,
|
|
1321
|
+
* repair, and sanitizing, so the client never paints a draft that those steps will change.
|
|
1322
|
+
*/
|
|
1323
|
+
holdAssistantText?: boolean;
|
|
1324
|
+
/** Saves the reply row once the model yields a part, after concurrent turns have loaded history. */
|
|
1325
|
+
onStreamStart?: () => Promise<void>;
|
|
1326
|
+
record: TurnRecord;
|
|
1327
|
+
result: ReturnType<typeof streamText>;
|
|
1328
|
+
sink: ChatTurnSink;
|
|
1329
|
+
}): Promise<void> => {
|
|
1330
|
+
const {askCalls, generatedImages} = record;
|
|
1331
|
+
const callOrder = new Map<string, number>();
|
|
1332
|
+
const askOrder = new Map<string, number>();
|
|
1333
|
+
// Stream file parts and result.files report the same images; emit each data URL once.
|
|
1334
|
+
const sendGeneratedImage = (file: GeneratedImageFile): void => {
|
|
1335
|
+
const url = `data:${file.mediaType};base64,${file.base64}`;
|
|
1336
|
+
if (generatedImages.some((img) => img.url === url)) {
|
|
1337
|
+
return;
|
|
1338
|
+
}
|
|
1339
|
+
generatedImages.push({mimeType: file.mediaType, url});
|
|
1340
|
+
sink.emit({image: {mimeType: file.mediaType, url}});
|
|
1341
|
+
};
|
|
1342
|
+
let partCount = 0;
|
|
1343
|
+
let hasStreamStarted = false;
|
|
1344
|
+
const startStream = async (): Promise<void> => {
|
|
1345
|
+
if (hasStreamStarted || !onStreamStart) {
|
|
1346
|
+
return;
|
|
1347
|
+
}
|
|
1348
|
+
hasStreamStarted = true;
|
|
1349
|
+
await onStreamStart();
|
|
1350
|
+
};
|
|
1351
|
+
// Buffer text per step so we can discard reasoning text when a tool call follows.
|
|
1352
|
+
// A block document in that step is kept only when the turn would otherwise have no text:
|
|
1353
|
+
// models often write the reply beside the tool call and then stop.
|
|
1354
|
+
let stepTextBuffer = "";
|
|
1355
|
+
let stepHasToolCall = false;
|
|
1356
|
+
let toolStepDocument = "";
|
|
1357
|
+
|
|
1358
|
+
const publishStreamingText = (): void => {
|
|
1359
|
+
record.streamingText = record.fullResponse + (stepHasToolCall ? "" : stepTextBuffer);
|
|
1360
|
+
};
|
|
1361
|
+
|
|
1362
|
+
const commitStepText = (): void => {
|
|
1363
|
+
const cleaned = cleanStepText(stepTextBuffer);
|
|
1364
|
+
if (!stepHasToolCall) {
|
|
1365
|
+
if (cleaned) {
|
|
1366
|
+
toolStepDocument = "";
|
|
1367
|
+
record.fullResponse += cleaned;
|
|
1368
|
+
if (!holdAssistantText) {
|
|
1369
|
+
record.textDelivered = true;
|
|
1370
|
+
sink.emit({text: cleaned});
|
|
1371
|
+
}
|
|
1372
|
+
}
|
|
1373
|
+
return;
|
|
1374
|
+
}
|
|
1375
|
+
if (cleaned && parseBlocks(cleaned).ok) {
|
|
1376
|
+
toolStepDocument = cleaned;
|
|
1377
|
+
}
|
|
1378
|
+
};
|
|
1379
|
+
|
|
1380
|
+
for await (const part of result.fullStream as AsyncIterable<StreamPart>) {
|
|
1381
|
+
// The AI SDK emits `start` before it calls the model. Saving there would run before
|
|
1382
|
+
// a concurrent turn has loaded this history. The reply row waits for a model part.
|
|
1383
|
+
if (part.type !== "start") {
|
|
1384
|
+
await startStream();
|
|
1385
|
+
}
|
|
1386
|
+
partCount++;
|
|
1387
|
+
if (partCount <= 5 || part.type === "error" || part.type === "file") {
|
|
1388
|
+
logger.debug("Stream part", {
|
|
1389
|
+
partCount,
|
|
1390
|
+
type: part.type,
|
|
1391
|
+
...(part.type === "file" && isGeneratedImageFile(part.file)
|
|
1392
|
+
? {mediaType: part.file.mediaType}
|
|
1393
|
+
: {}),
|
|
1394
|
+
...(part.type === "error" ? {error: String(part.error ?? part)} : {}),
|
|
1395
|
+
});
|
|
1396
|
+
}
|
|
1397
|
+
|
|
1398
|
+
// Track step boundaries to discard reasoning text from tool-call steps
|
|
1399
|
+
if (part.type === "start-step") {
|
|
1400
|
+
stepTextBuffer = "";
|
|
1401
|
+
stepHasToolCall = false;
|
|
1402
|
+
publishStreamingText();
|
|
1403
|
+
continue;
|
|
1404
|
+
}
|
|
1405
|
+
if (part.type === "finish-step") {
|
|
1406
|
+
commitStepText();
|
|
1407
|
+
stepTextBuffer = "";
|
|
1408
|
+
stepHasToolCall = false;
|
|
1409
|
+
publishStreamingText();
|
|
1410
|
+
continue;
|
|
1411
|
+
}
|
|
1412
|
+
|
|
1413
|
+
if (part.type === "error") {
|
|
1414
|
+
const errMsg =
|
|
1415
|
+
part.error instanceof Error
|
|
1416
|
+
? part.error.message
|
|
1417
|
+
: String(part.error ?? "Unknown stream error");
|
|
1418
|
+
logger.error("AI stream error part", {error: errMsg});
|
|
1419
|
+
record.streamError = errMsg;
|
|
1420
|
+
sink.emit({error: errMsg});
|
|
1421
|
+
continue;
|
|
1422
|
+
}
|
|
1423
|
+
if (part.type === "file") {
|
|
1424
|
+
if (isGeneratedImageFile(part.file)) {
|
|
1425
|
+
sendGeneratedImage(part.file);
|
|
1426
|
+
logger.debug("Sent inline image from stream");
|
|
1427
|
+
}
|
|
1428
|
+
continue;
|
|
1429
|
+
}
|
|
1430
|
+
if (part.type === "text-delta") {
|
|
1431
|
+
const textChunk = (part.text ?? "") as string;
|
|
1432
|
+
if (textChunk) {
|
|
1433
|
+
stepTextBuffer += textChunk;
|
|
1434
|
+
publishStreamingText();
|
|
1435
|
+
}
|
|
1436
|
+
} else if (part.type === "tool-call") {
|
|
1437
|
+
stepHasToolCall = true;
|
|
1438
|
+
publishStreamingText();
|
|
1439
|
+
const toolName = part.toolName as string;
|
|
1440
|
+
const toolCallId = part.toolCallId as string;
|
|
1441
|
+
const order = callOrder.size;
|
|
1442
|
+
callOrder.set(toolCallId, order);
|
|
1443
|
+
const askKind = askKindFromToolName(toolName, askKinds);
|
|
1444
|
+
if (askKind) {
|
|
1445
|
+
if (part.invalid) {
|
|
1446
|
+
logger.debug("Model sent an invalid ask; it receives the errors as a tool error", {
|
|
1447
|
+
toolCallId,
|
|
1448
|
+
toolName,
|
|
1449
|
+
});
|
|
1450
|
+
} else {
|
|
1451
|
+
askOrder.set(toolCallId, order);
|
|
1452
|
+
askCalls.push({...parseAsk({input: part.input, kind: askKind}), toolCallId});
|
|
1453
|
+
}
|
|
1454
|
+
continue;
|
|
1455
|
+
}
|
|
1456
|
+
sink.emit({toolCall: {args: part.input, toolCallId, toolName}});
|
|
1457
|
+
record.rows.push({
|
|
1458
|
+
args: part.input as Record<string, unknown>,
|
|
1459
|
+
text: `Tool call: ${toolName}`,
|
|
1460
|
+
toolCallId,
|
|
1461
|
+
toolName,
|
|
1462
|
+
type: "tool-call",
|
|
1463
|
+
});
|
|
1464
|
+
} else if (part.type === "tool-approval-request") {
|
|
1465
|
+
const approvalId = part.approvalId as string;
|
|
1466
|
+
const toolCall = part.toolCall as {input: unknown; toolCallId: string; toolName: string};
|
|
1467
|
+
if (!approvals) {
|
|
1468
|
+
logger.warn("A host tool needs approval, but asks are off, so it did not run", {
|
|
1469
|
+
toolName: toolCall.toolName,
|
|
1470
|
+
});
|
|
1471
|
+
continue;
|
|
1472
|
+
}
|
|
1473
|
+
askOrder.set(approvalId, callOrder.get(toolCall.toolCallId) ?? callOrder.size);
|
|
1474
|
+
askCalls.push({
|
|
1475
|
+
input: approvalAskInput({
|
|
1476
|
+
asks: approvals.asks,
|
|
1477
|
+
description: approvals.tools[toolCall.toolName]?.description,
|
|
1478
|
+
input: toolCall.input,
|
|
1479
|
+
surface: approvals.surface,
|
|
1480
|
+
toolName: toolCall.toolName,
|
|
1481
|
+
}),
|
|
1482
|
+
kind: "confirm",
|
|
1483
|
+
origin: "approval",
|
|
1484
|
+
toolCallId: approvalId,
|
|
1485
|
+
toolName: toolCall.toolName,
|
|
1486
|
+
});
|
|
1487
|
+
} else if (part.type === "tool-output-denied") {
|
|
1488
|
+
const toolName = part.toolName as string;
|
|
1489
|
+
const toolCallId = part.toolCallId as string;
|
|
1490
|
+
const reason = deniedReasons.get(toolCallId);
|
|
1491
|
+
const denied = {approved: false, ...(reason ? {reason} : {})};
|
|
1492
|
+
sink.emit({toolResult: {result: denied, toolCallId, toolName}});
|
|
1493
|
+
record.rows.push({
|
|
1494
|
+
result: denied,
|
|
1495
|
+
text: `Tool result: ${toolName}`,
|
|
1496
|
+
toolCallId,
|
|
1497
|
+
toolName,
|
|
1498
|
+
type: "tool-result",
|
|
1499
|
+
});
|
|
1500
|
+
} else if (part.type === "tool-result") {
|
|
1501
|
+
const toolResult = part.output as Record<string, unknown> | undefined;
|
|
1502
|
+
|
|
1503
|
+
// If the tool result contains file data, send it as a separate file SSE event
|
|
1504
|
+
if (toolResult?.fileData && typeof toolResult.fileData === "string") {
|
|
1505
|
+
sink.emit({
|
|
1506
|
+
file: {
|
|
1507
|
+
filename: (toolResult.filename as string | undefined) ?? "document",
|
|
1508
|
+
mimeType: (toolResult.mimeType as string | undefined) ?? "application/octet-stream",
|
|
1509
|
+
url: toolResult.fileData,
|
|
1510
|
+
},
|
|
1511
|
+
});
|
|
1512
|
+
logger.debug("Sent generated file from tool result", {
|
|
1513
|
+
filename: toolResult.filename,
|
|
1514
|
+
mimeType: toolResult.mimeType,
|
|
1515
|
+
});
|
|
1516
|
+
}
|
|
1517
|
+
|
|
1518
|
+
// Strip fileData from result before sending/storing to avoid bloating the SSE and DB
|
|
1519
|
+
const {fileData: _fileData, ...cleanResult} = toolResult ?? {};
|
|
1520
|
+
const toolName = part.toolName as string;
|
|
1521
|
+
const toolCallId = part.toolCallId as string;
|
|
1522
|
+
sink.emit({toolResult: {result: cleanResult, toolCallId, toolName}});
|
|
1523
|
+
record.rows.push({
|
|
1524
|
+
result: cleanResult as unknown,
|
|
1525
|
+
text: `Tool result: ${toolName}`,
|
|
1526
|
+
toolCallId,
|
|
1527
|
+
toolName,
|
|
1528
|
+
type: "tool-result",
|
|
1529
|
+
});
|
|
1530
|
+
}
|
|
1531
|
+
}
|
|
1532
|
+
|
|
1533
|
+
await startStream();
|
|
1534
|
+
// Flush any remaining buffered text from the last step
|
|
1535
|
+
commitStepText();
|
|
1536
|
+
publishStreamingText();
|
|
1537
|
+
if (record.fullResponse.trim() === "" && toolStepDocument) {
|
|
1538
|
+
record.fullResponse = toolStepDocument;
|
|
1539
|
+
record.streamingText = toolStepDocument;
|
|
1540
|
+
if (!holdAssistantText) {
|
|
1541
|
+
record.textDelivered = true;
|
|
1542
|
+
sink.emit({text: toolStepDocument});
|
|
1543
|
+
}
|
|
1544
|
+
}
|
|
1545
|
+
|
|
1546
|
+
logger.debug("Stream completed", {fullResponseLength: record.fullResponse.length, partCount});
|
|
1547
|
+
askCalls.sort(
|
|
1548
|
+
(a, b) =>
|
|
1549
|
+
(askOrder.get(a.toolCallId) ?? askOrder.size) - (askOrder.get(b.toolCallId) ?? askOrder.size)
|
|
1550
|
+
);
|
|
1551
|
+
|
|
1552
|
+
// Check for generated images (e.g. from gemini-2.5-flash-image)
|
|
1553
|
+
try {
|
|
1554
|
+
const files = await result.files;
|
|
1555
|
+
if (files && files.length > 0) {
|
|
1556
|
+
for (const file of files) {
|
|
1557
|
+
if (isGeneratedImageFile(file)) {
|
|
1558
|
+
sendGeneratedImage(file);
|
|
1559
|
+
}
|
|
1560
|
+
}
|
|
1561
|
+
logger.debug("Sent generated images", {count: generatedImages.length});
|
|
1562
|
+
}
|
|
1563
|
+
} catch (fileErr) {
|
|
1564
|
+
logger.debug("No files in response", {
|
|
1565
|
+
error: fileErr instanceof Error ? fileErr.message : String(fileErr),
|
|
1566
|
+
});
|
|
1567
|
+
}
|
|
1568
|
+
};
|
|
1569
|
+
|
|
1570
|
+
/** The assistant's reply row, or none when the turn produced no text or images. */
|
|
1571
|
+
const assistantRows = ({
|
|
1572
|
+
fullResponse,
|
|
1573
|
+
generatedImages,
|
|
1574
|
+
modelId,
|
|
1575
|
+
}: {
|
|
1576
|
+
fullResponse: string;
|
|
1577
|
+
generatedImages: GeneratedImage[];
|
|
1578
|
+
modelId: string | undefined;
|
|
1579
|
+
}): GptHistoryPrompt[] => {
|
|
1580
|
+
if (!fullResponse && generatedImages.length === 0) {
|
|
1581
|
+
return [];
|
|
1582
|
+
}
|
|
1583
|
+
const contentParts: MessageContentPart[] = generatedImages.map((img) => ({
|
|
1584
|
+
mimeType: img.mimeType,
|
|
1585
|
+
type: "image" as const,
|
|
1586
|
+
url: img.url,
|
|
1587
|
+
}));
|
|
1588
|
+
return [
|
|
1589
|
+
{
|
|
1590
|
+
model: modelId,
|
|
1591
|
+
text: fullResponse,
|
|
1592
|
+
type: "assistant",
|
|
1593
|
+
...(contentParts.length > 0 ? {content: contentParts} : {}),
|
|
1594
|
+
},
|
|
1595
|
+
];
|
|
1596
|
+
};
|
|
1597
|
+
|
|
1598
|
+
const askCallRow = (call: AskCall, status: GptHistoryAskStatus): GptHistoryPrompt => ({
|
|
1599
|
+
args: call.input,
|
|
1600
|
+
ask: {kind: call.kind, status, ...(call.origin ? {origin: call.origin} : {})},
|
|
1601
|
+
text: `Tool call: ${askRowToolName(call)}`,
|
|
1602
|
+
toolCallId: call.toolCallId,
|
|
1603
|
+
toolName: askRowToolName(call),
|
|
1604
|
+
type: "tool-call",
|
|
1605
|
+
});
|
|
1606
|
+
|
|
1607
|
+
const droppedAskResultRow = (call: AskCall): GptHistoryPrompt => ({
|
|
1608
|
+
result: {action: "cancel", reason: ASK_CANCEL_REASONS.oneAskAtATime},
|
|
1609
|
+
text: `Tool result: ${askRowToolName(call)}`,
|
|
1610
|
+
toolCallId: call.toolCallId,
|
|
1611
|
+
toolName: askRowToolName(call),
|
|
1612
|
+
type: "tool-result",
|
|
1613
|
+
});
|
|
1614
|
+
|
|
1615
|
+
const answeredAskMetadata = (resolvedAsk: ResolvedAsk | undefined): Record<string, unknown> =>
|
|
1616
|
+
resolvedAsk
|
|
1617
|
+
? {
|
|
1618
|
+
ask: {
|
|
1619
|
+
action: resolvedAsk.action,
|
|
1620
|
+
kind: resolvedAsk.kind,
|
|
1621
|
+
phase: "answered",
|
|
1622
|
+
toolCallId: resolvedAsk.toolCallId,
|
|
1623
|
+
...approvalFieldsOf(resolvedAsk),
|
|
1624
|
+
},
|
|
1625
|
+
}
|
|
1626
|
+
: {};
|
|
1627
|
+
|
|
1628
|
+
const toolSpansFromRows = ({
|
|
1629
|
+
rows,
|
|
1630
|
+
startTime,
|
|
1631
|
+
}: {
|
|
1632
|
+
rows: GptHistoryPrompt[];
|
|
1633
|
+
startTime: number;
|
|
1634
|
+
}): SpanRecord[] => {
|
|
1635
|
+
const resultsByCallId = new Map(
|
|
1636
|
+
rows
|
|
1637
|
+
.filter((row) => row.type === "tool-result" && row.toolCallId)
|
|
1638
|
+
.map((row) => [row.toolCallId as string, row.result])
|
|
1639
|
+
);
|
|
1640
|
+
const endedAt = DateTime.now().toMillis();
|
|
1641
|
+
return rows
|
|
1642
|
+
.filter((row) => row.type === "tool-call" && row.toolCallId && row.toolName)
|
|
1643
|
+
.map((row, index) => ({
|
|
1644
|
+
durationMs: 0,
|
|
1645
|
+
endedAt: DateTime.fromMillis(endedAt, {zone: "utc"}).toISO() ?? "",
|
|
1646
|
+
id: `${row.toolCallId}-span`,
|
|
1647
|
+
input: row.args,
|
|
1648
|
+
kind: "TOOL" as const,
|
|
1649
|
+
name: row.toolName as string,
|
|
1650
|
+
output: resultsByCallId.get(row.toolCallId as string),
|
|
1651
|
+
startedAt: DateTime.fromMillis(startTime + index, {zone: "utc"}).toISO() ?? "",
|
|
1652
|
+
startOffsetMs: index,
|
|
1653
|
+
status: "ok" as const,
|
|
1654
|
+
}));
|
|
1655
|
+
};
|
|
1656
|
+
|
|
1657
|
+
/**
|
|
1658
|
+
* Runs one chat turn: a new prompt, or the user's answer to the pending ask (`askResponse`).
|
|
1659
|
+
* `surface: "compact"` offers only the asks a small screen can show and asks for short replies.
|
|
1660
|
+
* Request errors throw an APIError before `sink.open()`; after that, failures are sent as an
|
|
1661
|
+
* `{error}` event. `/gpt/prompt` drives it with a server-sent events sink and the headless
|
|
1662
|
+
* `turn` action with `runBufferedChatTurn`.
|
|
1663
|
+
*/
|
|
1664
|
+
export const runChatTurn = async ({
|
|
1665
|
+
body,
|
|
1666
|
+
options,
|
|
1667
|
+
req,
|
|
1668
|
+
sink,
|
|
1669
|
+
}: {
|
|
1670
|
+
body: Record<string, unknown>;
|
|
1671
|
+
options: GptRouteOptions;
|
|
1672
|
+
req: express.Request;
|
|
1673
|
+
sink: ChatTurnSink;
|
|
1674
|
+
}): Promise<void> => {
|
|
1675
|
+
const {toolChoice, maxSteps} = options;
|
|
1676
|
+
const {model: requestModel, projectId, systemPrompt} = body;
|
|
1677
|
+
const userId = (req.user as {_id?: mongoose.Types.ObjectId} | undefined)?._id;
|
|
1678
|
+
const promptName = typeof body.promptName === "string" ? body.promptName : undefined;
|
|
1679
|
+
const promptLabel = typeof body.promptLabel === "string" ? body.promptLabel : undefined;
|
|
1680
|
+
const isAdmin = (req.user as {admin?: boolean} | undefined)?.admin === true;
|
|
1681
|
+
if ((promptName || promptLabel) && !isAdmin) {
|
|
1682
|
+
throw new APIError({status: 403, title: "Prompt registry selection requires admin access"});
|
|
1683
|
+
}
|
|
1684
|
+
const sessionId =
|
|
1685
|
+
(typeof body.sessionId === "string" ? body.sessionId : undefined) ??
|
|
1686
|
+
req.header("x-ai-session-id") ??
|
|
1687
|
+
undefined;
|
|
1688
|
+
const askKinds = resolveAskKinds(options.asks);
|
|
1689
|
+
const askAnswer = parseTurnBody({askKinds, body});
|
|
1690
|
+
const surface = parseSurface(body.surface);
|
|
1691
|
+
|
|
1692
|
+
// Resolve AI service (per-request key takes priority, then configured service)
|
|
1693
|
+
logger.debug("Resolving AI service", {
|
|
1694
|
+
hasConfiguredService: !!options.aiService,
|
|
1695
|
+
hasCreateModelFn: !!options.createModelFn,
|
|
1696
|
+
hasPerRequestKey: !!req.headers["x-ai-api-key"],
|
|
1697
|
+
});
|
|
1698
|
+
const aiService = resolveAiService(req, options, requestModel as string | undefined);
|
|
1699
|
+
if (!aiService) {
|
|
1700
|
+
logger.debug("No AI service available, sending demo response");
|
|
1701
|
+
sink.open();
|
|
1702
|
+
sink.emit({text: DEMO_RESPONSE});
|
|
1703
|
+
sink.emit({done: true});
|
|
1704
|
+
return;
|
|
1705
|
+
}
|
|
1706
|
+
|
|
1707
|
+
const turn = await startTurn({aiService, askAnswer, body, options, req, userId});
|
|
1708
|
+
const {answer, isNewHistory, logPrompt, messages, replayedMessages, resolvedAsk, titlePrompt} =
|
|
1709
|
+
turn;
|
|
1710
|
+
let {history} = turn;
|
|
1711
|
+
let effectiveSystemPrompt = await buildSystemPrompt({
|
|
1712
|
+
history,
|
|
1713
|
+
options,
|
|
1714
|
+
projectId,
|
|
1715
|
+
systemPrompt,
|
|
1716
|
+
userId,
|
|
1717
|
+
});
|
|
1718
|
+
const observability = await aiService.resolveGenerateObservability({
|
|
1719
|
+
promptLabel,
|
|
1720
|
+
promptName,
|
|
1721
|
+
sensitive: body.sensitive === true ? true : undefined,
|
|
1722
|
+
sessionId,
|
|
1723
|
+
systemPrompt: effectiveSystemPrompt,
|
|
1724
|
+
});
|
|
1725
|
+
effectiveSystemPrompt = observability.systemPrompt;
|
|
1726
|
+
|
|
1727
|
+
// Some models (e.g. gemini-2.5-flash-image) don't support tool calling
|
|
1728
|
+
const modelId = aiService.modelId;
|
|
1729
|
+
const supportsTools = !modelId?.includes("image");
|
|
1730
|
+
const offeredAskKinds = supportsTools ? askKindsForSurface({kinds: askKinds, surface}) : [];
|
|
1731
|
+
const allTools = await collectTools({
|
|
1732
|
+
askKinds: offeredAskKinds,
|
|
1733
|
+
options,
|
|
1734
|
+
req,
|
|
1735
|
+
supportsTools,
|
|
1736
|
+
surface,
|
|
1737
|
+
});
|
|
1738
|
+
const uiBlocks = resolveUiBlocks(options.uiBlocks);
|
|
1739
|
+
const system = withTurnSystemPrompt({
|
|
1740
|
+
askKinds: offeredAskKinds,
|
|
1741
|
+
blocksPrompt: uiBlocks
|
|
1742
|
+
? uiBlocksSystemPrompt(
|
|
1743
|
+
Object.keys(uiBlocks.hostActions ?? {}),
|
|
1744
|
+
uiBlocks.html === true,
|
|
1745
|
+
uiBlocks.imageHosts ?? []
|
|
1746
|
+
)
|
|
1747
|
+
: undefined,
|
|
1748
|
+
surface,
|
|
1749
|
+
systemPrompt: effectiveSystemPrompt,
|
|
1750
|
+
});
|
|
1751
|
+
|
|
1752
|
+
// Rows saved after the answer mean the turn moved on, so a failure keeps the answer.
|
|
1753
|
+
let answeredRowCount = 0;
|
|
1754
|
+
if (answer) {
|
|
1755
|
+
const resolved = await resolvePendingAsk({history, ...answer});
|
|
1756
|
+
if (!resolved) {
|
|
1757
|
+
throw staleAskError(answer.toolCallId);
|
|
1758
|
+
}
|
|
1759
|
+
history = resolved;
|
|
1760
|
+
answeredRowCount = resolved.prompts.length;
|
|
1761
|
+
}
|
|
1762
|
+
|
|
1763
|
+
const startTime = DateTime.now().toMillis();
|
|
1764
|
+
const record: TurnRecord = {
|
|
1765
|
+
askCalls: [],
|
|
1766
|
+
fullResponse: "",
|
|
1767
|
+
generatedImages: [],
|
|
1768
|
+
hasOutput: false,
|
|
1769
|
+
rows: [...turn.rows],
|
|
1770
|
+
streamingText: "",
|
|
1771
|
+
textDelivered: false,
|
|
1772
|
+
};
|
|
1773
|
+
const outputSink: ChatTurnSink = {
|
|
1774
|
+
emit: (event) => {
|
|
1775
|
+
if (!("error" in event)) {
|
|
1776
|
+
record.hasOutput = true;
|
|
1777
|
+
}
|
|
1778
|
+
sink.emit(event);
|
|
1779
|
+
},
|
|
1780
|
+
open: sink.open,
|
|
1781
|
+
};
|
|
1782
|
+
let isSaved = false;
|
|
1783
|
+
let historyIsNew = isNewHistory;
|
|
1784
|
+
const saveRows = async (rows: GptHistoryPrompt[]): Promise<number> => {
|
|
1785
|
+
const saved = await appendTurnRows({
|
|
1786
|
+
history,
|
|
1787
|
+
isNewHistory: historyIsNew,
|
|
1788
|
+
projectId,
|
|
1789
|
+
rows,
|
|
1790
|
+
});
|
|
1791
|
+
history = saved.history;
|
|
1792
|
+
historyIsNew = false;
|
|
1793
|
+
isSaved = true;
|
|
1794
|
+
return saved.firstRowIndex;
|
|
1795
|
+
};
|
|
1796
|
+
// An ask whose call row is saved pending but that is not yet the conversation's pending ask.
|
|
1797
|
+
let unclaimedAsk: AskCall | undefined;
|
|
1798
|
+
const streamId = randomUUID();
|
|
1799
|
+
let pausedPromptIndex = turn.promptIndex;
|
|
1800
|
+
let persistTimer: ReturnType<typeof setInterval> | undefined;
|
|
1801
|
+
let persistQueue: Promise<void> = Promise.resolve();
|
|
1802
|
+
const stopPersisting = async (): Promise<void> => {
|
|
1803
|
+
if (persistTimer !== undefined) {
|
|
1804
|
+
clearInterval(persistTimer);
|
|
1805
|
+
persistTimer = undefined;
|
|
1806
|
+
}
|
|
1807
|
+
await persistQueue;
|
|
1808
|
+
};
|
|
1809
|
+
const removeStreamingPlaceholder = async (): Promise<void> => {
|
|
1810
|
+
await GptHistory.updateOne({_id: history._id}, {$pull: {prompts: {streamId}}});
|
|
1811
|
+
};
|
|
1812
|
+
|
|
1813
|
+
// Open before the model runs so a failure that yields no part is still an SSE error.
|
|
1814
|
+
// The reply row is saved on the first part, after concurrent turns have loaded this history.
|
|
1815
|
+
sink.open();
|
|
1816
|
+
let lastPersistedText = "";
|
|
1817
|
+
let lastPersistedAt = DateTime.now();
|
|
1818
|
+
const persistIntervalMs = options.streamPersistIntervalMs ?? DEFAULT_STREAM_PERSIST_INTERVAL_MS;
|
|
1819
|
+
const beginStreaming = async (): Promise<void> => {
|
|
1820
|
+
const earlyRows = await saveRows([
|
|
1821
|
+
...record.rows,
|
|
1822
|
+
{
|
|
1823
|
+
model: aiService.modelId,
|
|
1824
|
+
status: "streaming",
|
|
1825
|
+
streamId,
|
|
1826
|
+
text: "",
|
|
1827
|
+
type: "assistant",
|
|
1828
|
+
},
|
|
1829
|
+
]);
|
|
1830
|
+
if (pausedPromptIndex === undefined) {
|
|
1831
|
+
pausedPromptIndex = earlyRows + 1;
|
|
1832
|
+
}
|
|
1833
|
+
record.rows = [];
|
|
1834
|
+
sink.emit({historyId: history._id.toString(), started: true, streamId});
|
|
1835
|
+
persistTimer = setInterval(() => {
|
|
1836
|
+
const text = record.streamingText;
|
|
1837
|
+
const isHeartbeatDue = DateTime.now().diff(lastPersistedAt).toMillis() >= STREAM_HEARTBEAT_MS;
|
|
1838
|
+
if (text === lastPersistedText && !isHeartbeatDue) {
|
|
1839
|
+
return;
|
|
1840
|
+
}
|
|
1841
|
+
lastPersistedText = text;
|
|
1842
|
+
lastPersistedAt = DateTime.now();
|
|
1843
|
+
persistQueue = persistQueue
|
|
1844
|
+
.then(async () => {
|
|
1845
|
+
await GptHistory.updateOne(
|
|
1846
|
+
{_id: history._id, "prompts.streamId": streamId},
|
|
1847
|
+
{
|
|
1848
|
+
$set: {
|
|
1849
|
+
"prompts.$.text": text,
|
|
1850
|
+
updated: DateTime.now().toJSDate(),
|
|
1851
|
+
},
|
|
1852
|
+
}
|
|
1853
|
+
);
|
|
1854
|
+
})
|
|
1855
|
+
.catch((persistErr: unknown) => {
|
|
1856
|
+
logger.warn("Failed to persist partial GPT response", {
|
|
1857
|
+
error: persistErr instanceof Error ? persistErr.message : String(persistErr),
|
|
1858
|
+
});
|
|
1859
|
+
});
|
|
1860
|
+
}, persistIntervalMs);
|
|
1861
|
+
};
|
|
1862
|
+
|
|
1863
|
+
/**
|
|
1864
|
+
* Ends a failed turn with `{error}` then `{done}`. An answer's turn that failed before the
|
|
1865
|
+
* client saw any output puts the ask back, so the user can answer again.
|
|
1866
|
+
*/
|
|
1867
|
+
const endFailedTurn = async ({
|
|
1868
|
+
errorMessage,
|
|
1869
|
+
isErrorSent,
|
|
1870
|
+
}: {
|
|
1871
|
+
errorMessage: string;
|
|
1872
|
+
isErrorSent: boolean;
|
|
1873
|
+
}): Promise<void> => {
|
|
1874
|
+
await stopPersisting();
|
|
1875
|
+
// The streaming placeholder is not part of the answer. Remove it before putting the ask
|
|
1876
|
+
// back, which only applies while the row count is still what the answer stored.
|
|
1877
|
+
if (!record.hasOutput && record.streamingText === "" && record.rows.length === 0) {
|
|
1878
|
+
try {
|
|
1879
|
+
await removeStreamingPlaceholder();
|
|
1880
|
+
} catch (saveErr) {
|
|
1881
|
+
logger.error("Failed to remove a streaming placeholder", {
|
|
1882
|
+
error: saveErr instanceof Error ? saveErr.message : String(saveErr),
|
|
1883
|
+
historyId: history._id.toString(),
|
|
1884
|
+
});
|
|
1885
|
+
}
|
|
1886
|
+
}
|
|
1887
|
+
const isAskRestored =
|
|
1888
|
+
answer && !record.hasOutput
|
|
1889
|
+
? await restorePendingAsk({answer, history, rowCount: answeredRowCount})
|
|
1890
|
+
: false;
|
|
1891
|
+
const metadata = isAskRestored ? {} : answeredAskMetadata(resolvedAsk);
|
|
1892
|
+
try {
|
|
1893
|
+
await AIRequest.logRequest({
|
|
1894
|
+
aiModel: modelId ?? "unknown",
|
|
1895
|
+
error: errorMessage,
|
|
1896
|
+
prompt: logPrompt,
|
|
1897
|
+
requestType: "general",
|
|
1898
|
+
responseTime: DateTime.now().toMillis() - startTime,
|
|
1899
|
+
userId: userId ?? undefined,
|
|
1900
|
+
...(Object.keys(metadata).length > 0 ? {metadata} : {}),
|
|
1901
|
+
});
|
|
1902
|
+
} catch (logErr) {
|
|
1903
|
+
logger.warn("Failed to log AIRequest error", {
|
|
1904
|
+
error: logErr instanceof Error ? logErr.message : String(logErr),
|
|
1905
|
+
});
|
|
1906
|
+
}
|
|
1907
|
+
|
|
1908
|
+
if (!isErrorSent) {
|
|
1909
|
+
sink.emit({error: errorMessage});
|
|
1910
|
+
}
|
|
1911
|
+
if (unclaimedAsk) {
|
|
1912
|
+
await cancelUnclaimedAsk({ask: unclaimedAsk, history});
|
|
1913
|
+
}
|
|
1914
|
+
|
|
1915
|
+
// Keep what the user saw before the failure. Asks are dropped: the turn cannot pause on them.
|
|
1916
|
+
try {
|
|
1917
|
+
if (isSaved) {
|
|
1918
|
+
const partial = record.streamingText;
|
|
1919
|
+
if (partial) {
|
|
1920
|
+
await GptHistory.updateOne(
|
|
1921
|
+
{_id: history._id, "prompts.streamId": streamId},
|
|
1922
|
+
{
|
|
1923
|
+
$set: {
|
|
1924
|
+
"prompts.$.status": "error",
|
|
1925
|
+
"prompts.$.text": partial,
|
|
1926
|
+
updated: DateTime.now().toJSDate(),
|
|
1927
|
+
},
|
|
1928
|
+
}
|
|
1929
|
+
);
|
|
1930
|
+
} else {
|
|
1931
|
+
await removeStreamingPlaceholder();
|
|
1932
|
+
}
|
|
1933
|
+
if (record.rows.length > 0) {
|
|
1934
|
+
await saveRows(record.rows);
|
|
1935
|
+
record.rows = [];
|
|
1936
|
+
}
|
|
1937
|
+
} else if (!isAskRestored) {
|
|
1938
|
+
await saveRows([
|
|
1939
|
+
...record.rows,
|
|
1940
|
+
...assistantRows({
|
|
1941
|
+
fullResponse: record.fullResponse,
|
|
1942
|
+
generatedImages: record.generatedImages,
|
|
1943
|
+
modelId: aiService.modelId,
|
|
1944
|
+
}),
|
|
1945
|
+
]);
|
|
1946
|
+
}
|
|
1947
|
+
} catch (saveErr) {
|
|
1948
|
+
logger.error("Failed to save a failed turn's rows", {
|
|
1949
|
+
error: saveErr instanceof Error ? saveErr.message : String(saveErr),
|
|
1950
|
+
historyId: history._id.toString(),
|
|
1951
|
+
});
|
|
1952
|
+
}
|
|
1953
|
+
sink.emit({
|
|
1954
|
+
done: true,
|
|
1955
|
+
...(isSaved || !isNewHistory ? {historyId: history._id.toString()} : {}),
|
|
1956
|
+
...(history.title ? {title: history.title} : {}),
|
|
1957
|
+
...(isAskRestored && answer ? {pendingAsk: {toolCallId: answer.toolCallId}} : {}),
|
|
1958
|
+
});
|
|
1959
|
+
};
|
|
1960
|
+
|
|
1961
|
+
try {
|
|
1962
|
+
if (resolvedAsk) {
|
|
1963
|
+
sink.emit({askResolved: {action: resolvedAsk.action, toolCallId: resolvedAsk.toolCallId}});
|
|
1964
|
+
}
|
|
1965
|
+
logger.debug("Starting streamText", {model: modelId, supportsTools});
|
|
1966
|
+
const telemetry = createTelemetryConfig({
|
|
1967
|
+
functionId: "gpt-prompt",
|
|
1968
|
+
metadata: {
|
|
1969
|
+
...(options.langfuseSystemPromptName
|
|
1970
|
+
? {langfusePromptName: options.langfuseSystemPromptName}
|
|
1971
|
+
: {}),
|
|
1972
|
+
},
|
|
1973
|
+
userId: userId?.toString(),
|
|
1974
|
+
});
|
|
1975
|
+
const result = streamText({
|
|
1976
|
+
experimental_telemetry: telemetry,
|
|
1977
|
+
messages,
|
|
1978
|
+
model: aiService.model,
|
|
1979
|
+
providerOptions: !supportsTools
|
|
1980
|
+
? {
|
|
1981
|
+
google: {responseModalities: ["TEXT", "IMAGE"]},
|
|
1982
|
+
vertex: {responseModalities: ["TEXT", "IMAGE"]},
|
|
1983
|
+
}
|
|
1984
|
+
: undefined,
|
|
1985
|
+
stopWhen: allTools ? stepCountIs(maxSteps ?? 5) : stepCountIs(1),
|
|
1986
|
+
system: system ?? undefined,
|
|
1987
|
+
temperature: aiService.defaultTemperature,
|
|
1988
|
+
toolChoice: allTools ? (toolChoice ?? "auto") : undefined,
|
|
1989
|
+
tools: allTools,
|
|
1990
|
+
});
|
|
1991
|
+
|
|
1992
|
+
await consumeStream({
|
|
1993
|
+
approvals:
|
|
1994
|
+
askKinds.length > 0 && allTools
|
|
1995
|
+
? {asks: options.asks, surface, tools: allTools}
|
|
1996
|
+
: undefined,
|
|
1997
|
+
askKinds: offeredAskKinds,
|
|
1998
|
+
deniedReasons: deniedApprovalReasons(messages),
|
|
1999
|
+
holdAssistantText: uiBlocks !== undefined,
|
|
2000
|
+
onStreamStart: beginStreaming,
|
|
2001
|
+
record,
|
|
2002
|
+
result,
|
|
2003
|
+
sink: outputSink,
|
|
2004
|
+
});
|
|
2005
|
+
if (answer && !record.hasOutput && record.streamError !== undefined) {
|
|
2006
|
+
await endFailedTurn({errorMessage: record.streamError, isErrorSent: true});
|
|
2007
|
+
return;
|
|
2008
|
+
}
|
|
2009
|
+
const {askCalls, generatedImages} = record;
|
|
2010
|
+
let {fullResponse} = record;
|
|
2011
|
+
const streamedResponse = fullResponse;
|
|
2012
|
+
const hostActionNames =
|
|
2013
|
+
uiBlocks?.hostActions === undefined ? undefined : Object.keys(uiBlocks.hostActions);
|
|
2014
|
+
const allowHtml = uiBlocks?.html === true;
|
|
2015
|
+
const imageHosts = uiBlocks?.imageHosts;
|
|
2016
|
+
if (uiBlocks && fullResponse.trim() !== "") {
|
|
2017
|
+
const filled = fillMissingActionIds(fullResponse);
|
|
2018
|
+
if (filled !== undefined) {
|
|
2019
|
+
fullResponse = filled;
|
|
2020
|
+
}
|
|
2021
|
+
}
|
|
2022
|
+
let blocksCheck =
|
|
2023
|
+
uiBlocks && fullResponse.trim() !== ""
|
|
2024
|
+
? checkBlockDocument(fullResponse, hostActionNames, allowHtml, imageHosts)
|
|
2025
|
+
: undefined;
|
|
2026
|
+
let repaired = false;
|
|
2027
|
+
if (blocksCheck && !blocksCheck.ok && uiBlocks?.repair === true) {
|
|
2028
|
+
const next = await repairBlockDocument({
|
|
2029
|
+
aiService,
|
|
2030
|
+
errors: blocksCheck.errors,
|
|
2031
|
+
text: fullResponse,
|
|
2032
|
+
});
|
|
2033
|
+
if (next !== undefined) {
|
|
2034
|
+
fullResponse = next;
|
|
2035
|
+
repaired = true;
|
|
2036
|
+
blocksCheck = checkBlockDocument(fullResponse, hostActionNames, allowHtml, imageHosts);
|
|
2037
|
+
}
|
|
2038
|
+
}
|
|
2039
|
+
if (allowHtml && blocksCheck?.ok) {
|
|
2040
|
+
const sanitized = sanitizeBlocksText(fullResponse);
|
|
2041
|
+
if (sanitized.changed) {
|
|
2042
|
+
fullResponse = sanitized.text;
|
|
2043
|
+
blocksCheck = checkBlockDocument(fullResponse, hostActionNames, allowHtml, imageHosts);
|
|
2044
|
+
}
|
|
2045
|
+
}
|
|
2046
|
+
const storedResponse =
|
|
2047
|
+
blocksCheck && !blocksCheck.ok
|
|
2048
|
+
? storedBlockText(fullResponse, blocksCheck.errors)
|
|
2049
|
+
: fullResponse;
|
|
2050
|
+
|
|
2051
|
+
// Send the document the client should paint before `{ask}`. With uiBlocks the stream held
|
|
2052
|
+
// the draft, so this is the first `{text}` and it already includes id-fill, repair, and
|
|
2053
|
+
// sanitizing. A draft that was already sent is replaced instead.
|
|
2054
|
+
if (fullResponse.trim() !== "") {
|
|
2055
|
+
if (record.textDelivered) {
|
|
2056
|
+
if (fullResponse !== streamedResponse) {
|
|
2057
|
+
outputSink.emit({replace: "text", text: fullResponse});
|
|
2058
|
+
}
|
|
2059
|
+
} else if (uiBlocks) {
|
|
2060
|
+
outputSink.emit({text: fullResponse});
|
|
2061
|
+
record.textDelivered = true;
|
|
2062
|
+
}
|
|
2063
|
+
}
|
|
2064
|
+
|
|
2065
|
+
// The first ask in the step pauses the turn. Any other ask the model made in that step is
|
|
2066
|
+
// cancelled now; any other approval is denied when the turn resumes, so its tool never runs.
|
|
2067
|
+
const [asked, ...otherAsks] = askCalls;
|
|
2068
|
+
const droppedAsks = otherAsks.filter((call) => call.origin !== "approval");
|
|
2069
|
+
const ask = asked ? {...asked, simple: toSimpleCard(asked)} : undefined;
|
|
2070
|
+
await stopPersisting();
|
|
2071
|
+
const finalRows = [
|
|
2072
|
+
...record.rows,
|
|
2073
|
+
...assistantRows({
|
|
2074
|
+
fullResponse: storedResponse,
|
|
2075
|
+
generatedImages,
|
|
2076
|
+
modelId: aiService.modelId,
|
|
2077
|
+
}).map((row) => ({...row, status: "complete" as const, streamId})),
|
|
2078
|
+
...(ask
|
|
2079
|
+
? [
|
|
2080
|
+
askCallRow(ask, "pending"),
|
|
2081
|
+
...droppedAsks.map((call) => askCallRow(call, "cancelled")),
|
|
2082
|
+
...droppedAsks.map(droppedAskResultRow),
|
|
2083
|
+
]
|
|
2084
|
+
: []),
|
|
2085
|
+
];
|
|
2086
|
+
// A text reply replaces the placeholder in place, so a concurrent turn cannot land between
|
|
2087
|
+
// the user message and this reply. Tool and ask rows are appended after the placeholder goes.
|
|
2088
|
+
const [onlyRow] = finalRows;
|
|
2089
|
+
const replacesPlaceholder =
|
|
2090
|
+
finalRows.length === 1 && onlyRow?.type === "assistant" && onlyRow.streamId === streamId;
|
|
2091
|
+
if (replacesPlaceholder) {
|
|
2092
|
+
await GptHistory.updateOne(
|
|
2093
|
+
{_id: history._id, "prompts.streamId": streamId},
|
|
2094
|
+
{
|
|
2095
|
+
$set: {
|
|
2096
|
+
"prompts.$.model": onlyRow.model,
|
|
2097
|
+
"prompts.$.status": "complete",
|
|
2098
|
+
"prompts.$.text": onlyRow.text,
|
|
2099
|
+
...(onlyRow.content ? {"prompts.$.content": onlyRow.content} : {}),
|
|
2100
|
+
updated: DateTime.now().toJSDate(),
|
|
2101
|
+
},
|
|
2102
|
+
}
|
|
2103
|
+
);
|
|
2104
|
+
} else {
|
|
2105
|
+
await removeStreamingPlaceholder();
|
|
2106
|
+
if (finalRows.length > 0) {
|
|
2107
|
+
await saveRows(finalRows);
|
|
2108
|
+
}
|
|
2109
|
+
}
|
|
2110
|
+
unclaimedAsk = ask;
|
|
2111
|
+
const isPaused = ask
|
|
2112
|
+
? await claimPendingAsk({
|
|
2113
|
+
ask,
|
|
2114
|
+
history,
|
|
2115
|
+
// A prompt turn's history ends with its own user message, the first row it appended.
|
|
2116
|
+
promptIndex: pausedPromptIndex ?? 1,
|
|
2117
|
+
responseMessages: toStoredMessages([
|
|
2118
|
+
...replayedMessages,
|
|
2119
|
+
...(await result.response).messages,
|
|
2120
|
+
]),
|
|
2121
|
+
})
|
|
2122
|
+
: false;
|
|
2123
|
+
unclaimedAsk = undefined;
|
|
2124
|
+
const pausedAsk = isPaused ? ask : undefined;
|
|
2125
|
+
if (ask && !pausedAsk) {
|
|
2126
|
+
logger.warn("Cancelled an ask because another turn's ask is already pending", {
|
|
2127
|
+
historyId: history._id.toString(),
|
|
2128
|
+
toolCallId: ask.toolCallId,
|
|
2129
|
+
});
|
|
2130
|
+
}
|
|
2131
|
+
if (pausedAsk) {
|
|
2132
|
+
sink.emit({ask: pausedAsk, historyId: history._id.toString()});
|
|
2133
|
+
}
|
|
2134
|
+
|
|
2135
|
+
const metadata = {
|
|
2136
|
+
...answeredAskMetadata(resolvedAsk),
|
|
2137
|
+
...(pausedAsk
|
|
2138
|
+
? {
|
|
2139
|
+
[resolvedAsk ? "nextAsk" : "ask"]: {
|
|
2140
|
+
kind: pausedAsk.kind,
|
|
2141
|
+
phase: "asked",
|
|
2142
|
+
toolCallId: pausedAsk.toolCallId,
|
|
2143
|
+
...approvalFieldsOf(pausedAsk),
|
|
2144
|
+
},
|
|
2145
|
+
}
|
|
2146
|
+
: {}),
|
|
2147
|
+
...(blocksCheck
|
|
2148
|
+
? {
|
|
2149
|
+
uiBlocks: {
|
|
2150
|
+
errorCodes: blocksCheck.errors.map((error) => error.code),
|
|
2151
|
+
ok: blocksCheck.ok,
|
|
2152
|
+
repaired,
|
|
2153
|
+
warningCodes: blocksCheck.warnings.map((error) => error.code),
|
|
2154
|
+
},
|
|
2155
|
+
}
|
|
2156
|
+
: {}),
|
|
2157
|
+
};
|
|
2158
|
+
try {
|
|
2159
|
+
await aiService.recordGenerate({
|
|
2160
|
+
childSpans: toolSpansFromRows({rows: record.rows, startTime}),
|
|
2161
|
+
metadata,
|
|
2162
|
+
observability,
|
|
2163
|
+
prompt: logPrompt,
|
|
2164
|
+
requestType: "general",
|
|
2165
|
+
response: storedResponse,
|
|
2166
|
+
responseTime: DateTime.now().toMillis() - startTime,
|
|
2167
|
+
startTime,
|
|
2168
|
+
userId: userId ?? undefined,
|
|
2169
|
+
});
|
|
2170
|
+
} catch (logErr) {
|
|
2171
|
+
logger.warn("Failed to log AIRequest", {
|
|
2172
|
+
error: logErr instanceof Error ? logErr.message : String(logErr),
|
|
2173
|
+
});
|
|
2174
|
+
}
|
|
2175
|
+
|
|
2176
|
+
// Generate a title for new conversations using a cheap model call
|
|
2177
|
+
let title = history.title;
|
|
2178
|
+
if (!title && fullResponse) {
|
|
2179
|
+
const perRequestApiKey = req.headers["x-ai-api-key"] as string | undefined;
|
|
2180
|
+
const generatedTitle = await generateTitle(
|
|
2181
|
+
titlePrompt,
|
|
2182
|
+
fullResponse,
|
|
2183
|
+
aiService,
|
|
2184
|
+
options,
|
|
2185
|
+
perRequestApiKey
|
|
2186
|
+
);
|
|
2187
|
+
if (generatedTitle) {
|
|
2188
|
+
title = await saveTitle({history, title: generatedTitle});
|
|
2189
|
+
}
|
|
2190
|
+
}
|
|
2191
|
+
|
|
2192
|
+
logger.debug("Sending done event", {
|
|
2193
|
+
fullResponseLength: fullResponse.length,
|
|
2194
|
+
historyId: history._id.toString(),
|
|
2195
|
+
});
|
|
2196
|
+
if (blocksCheck) {
|
|
2197
|
+
sink.emit({blocks: blocksCheck});
|
|
2198
|
+
}
|
|
2199
|
+
sink.emit({
|
|
2200
|
+
done: true,
|
|
2201
|
+
historyId: history._id.toString(),
|
|
2202
|
+
...(title ? {title} : {}),
|
|
2203
|
+
...(pausedAsk ? {pendingAsk: {toolCallId: pausedAsk.toolCallId}} : {}),
|
|
2204
|
+
});
|
|
2205
|
+
} catch (error) {
|
|
2206
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
2207
|
+
logger.error("Error in GPT stream", {error: errorMessage});
|
|
2208
|
+
try {
|
|
2209
|
+
await aiService.recordGenerate({
|
|
2210
|
+
childSpans: toolSpansFromRows({rows: record.rows, startTime}),
|
|
2211
|
+
error: errorMessage,
|
|
2212
|
+
observability,
|
|
2213
|
+
prompt: logPrompt,
|
|
2214
|
+
requestType: "general",
|
|
2215
|
+
response: record.fullResponse || undefined,
|
|
2216
|
+
responseTime: DateTime.now().toMillis() - startTime,
|
|
2217
|
+
startTime,
|
|
2218
|
+
userId: userId ?? undefined,
|
|
2219
|
+
});
|
|
2220
|
+
} catch (logError) {
|
|
2221
|
+
logger.warn("Failed to log AIRequest error", {
|
|
2222
|
+
error: logError instanceof Error ? logError.message : String(logError),
|
|
2223
|
+
});
|
|
2224
|
+
}
|
|
2225
|
+
await endFailedTurn({
|
|
2226
|
+
errorMessage: error instanceof Error ? error.message : "Unknown error",
|
|
2227
|
+
isErrorSent: false,
|
|
2228
|
+
});
|
|
2229
|
+
}
|
|
2230
|
+
};
|
|
2231
|
+
|
|
2232
|
+
interface BufferedTurn {
|
|
2233
|
+
ask?: ChatTurnAskEvent["ask"];
|
|
2234
|
+
done?: ChatTurnDoneEvent;
|
|
2235
|
+
errors: string[];
|
|
2236
|
+
texts: string[];
|
|
2237
|
+
}
|
|
2238
|
+
|
|
2239
|
+
/**
|
|
2240
|
+
* Runs a turn to completion and returns it as one JSON result, for clients that do not read
|
|
2241
|
+
* server-sent events. Nothing is written to the response while the turn runs, so a client that
|
|
2242
|
+
* disconnects does not stop it: the turn still finishes and saves.
|
|
2243
|
+
*/
|
|
2244
|
+
export const runBufferedChatTurn = async ({
|
|
2245
|
+
body,
|
|
2246
|
+
options,
|
|
2247
|
+
req,
|
|
2248
|
+
}: {
|
|
2249
|
+
body: Record<string, unknown> & {historyId: string};
|
|
2250
|
+
options: GptRouteOptions;
|
|
2251
|
+
req: express.Request;
|
|
2252
|
+
}): Promise<TurnResult> => {
|
|
2253
|
+
const turn: BufferedTurn = {errors: [], texts: []};
|
|
2254
|
+
await runChatTurn({
|
|
2255
|
+
body,
|
|
2256
|
+
options,
|
|
2257
|
+
req,
|
|
2258
|
+
sink: {
|
|
2259
|
+
emit: (event) => {
|
|
2260
|
+
if ("replace" in event && event.replace === "text") {
|
|
2261
|
+
turn.texts = [event.text];
|
|
2262
|
+
} else if ("text" in event) {
|
|
2263
|
+
turn.texts.push(event.text);
|
|
2264
|
+
} else if ("error" in event) {
|
|
2265
|
+
turn.errors.push(event.error);
|
|
2266
|
+
} else if ("ask" in event) {
|
|
2267
|
+
turn.ask = event.ask;
|
|
2268
|
+
} else if ("done" in event) {
|
|
2269
|
+
turn.done = event;
|
|
2270
|
+
}
|
|
2271
|
+
},
|
|
2272
|
+
open: () => {},
|
|
2273
|
+
},
|
|
2274
|
+
});
|
|
2275
|
+
const {done, errors, texts} = turn;
|
|
2276
|
+
const historyId = done?.historyId ?? body.historyId;
|
|
2277
|
+
// A failed answer's turn puts its ask back without streaming it again.
|
|
2278
|
+
const ask =
|
|
2279
|
+
turn.ask ?? (done?.pendingAsk ? (await GptHistory.findById(historyId))?.pendingAsk : undefined);
|
|
2280
|
+
return {
|
|
2281
|
+
...(errors.length > 0 ? {error: errors.join("\n")} : {}),
|
|
2282
|
+
historyId,
|
|
2283
|
+
...(ask ? {pendingAsk: {kind: ask.kind, simple: ask.simple, toolCallId: ask.toolCallId}} : {}),
|
|
2284
|
+
text: texts.join(""),
|
|
2285
|
+
...(done?.title ? {title: done.title} : {}),
|
|
2286
|
+
};
|
|
2287
|
+
};
|