@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
package/src/routes/gpt.ts
CHANGED
|
@@ -3,115 +3,91 @@ import {
|
|
|
3
3
|
asyncHandler,
|
|
4
4
|
authenticateMiddleware,
|
|
5
5
|
createOpenApiBuilder,
|
|
6
|
+
isAPIError,
|
|
6
7
|
logger,
|
|
8
|
+
type OpenApiSchemaProperty,
|
|
7
9
|
} from "@terreno/api";
|
|
8
|
-
import
|
|
9
|
-
import {stepCountIs, streamText} from "ai";
|
|
10
|
+
import {ASK_SURFACES, askSurfaceSchema} from "@terreno/blocks";
|
|
10
11
|
import type express from "express";
|
|
11
12
|
import {DateTime} from "luxon";
|
|
12
13
|
import type mongoose from "mongoose";
|
|
13
|
-
import {createTelemetryConfig, preparePromptForAI} from "../langfuseVercelAi";
|
|
14
14
|
|
|
15
|
-
import {AIRequest} from "../models/aiRequest";
|
|
16
15
|
import {GptHistory} from "../models/gptHistory";
|
|
17
|
-
import {
|
|
18
|
-
import {
|
|
19
|
-
import {
|
|
20
|
-
import type {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
value !== null &&
|
|
35
|
-
"base64" in value &&
|
|
36
|
-
typeof value.base64 === "string" &&
|
|
37
|
-
"mediaType" in value &&
|
|
38
|
-
typeof value.mediaType === "string" &&
|
|
39
|
-
value.mediaType.startsWith("image/");
|
|
40
|
-
|
|
41
|
-
const DEMO_RESPONSE =
|
|
42
|
-
"This is demo mode. To use AI features, paste your Gemini API key in Settings.";
|
|
43
|
-
|
|
44
|
-
/** Send a canned SSE demo response when no AI service is available. */
|
|
45
|
-
const sendDemoResponse = (res: express.Response, historyId?: string): void => {
|
|
46
|
-
res.setHeader("Content-Type", "text/event-stream");
|
|
47
|
-
res.setHeader("Cache-Control", "no-cache");
|
|
48
|
-
res.setHeader("Connection", "keep-alive");
|
|
49
|
-
res.write(`data: ${JSON.stringify({text: DEMO_RESPONSE})}\n\n`);
|
|
50
|
-
res.write(`data: ${JSON.stringify({done: true, ...(historyId ? {historyId} : {})})}\n\n`);
|
|
51
|
-
res.end();
|
|
16
|
+
import {configureAiDatasets} from "../service/aiDatasets";
|
|
17
|
+
import {assertNoReservedToolNames, resolveAskKinds} from "../service/asks";
|
|
18
|
+
import {type ChatTurnSink, DEMO_RESPONSE, resolveAiService, runChatTurn} from "../service/chatTurn";
|
|
19
|
+
import type {GptRouteOptions} from "../types";
|
|
20
|
+
import {addGptActionRoutes} from "./gptActions";
|
|
21
|
+
import {addGptDatasetRoutes} from "./gptDatasets";
|
|
22
|
+
|
|
23
|
+
const ASK_RESPONSE_BODY: OpenApiSchemaProperty = {
|
|
24
|
+
description:
|
|
25
|
+
"The user's answer to the conversation's pending ask. Send it with historyId instead of prompt.",
|
|
26
|
+
properties: {
|
|
27
|
+
action: {enum: ["accept", "decline", "cancel"], type: "string"},
|
|
28
|
+
content: {description: "The answer, when action is accept", type: "object"},
|
|
29
|
+
reason: {description: "Why the ask was cancelled, when action is cancel", type: "string"},
|
|
30
|
+
toolCallId: {description: "The pending ask's toolCallId", type: "string"},
|
|
31
|
+
},
|
|
32
|
+
type: "object",
|
|
52
33
|
};
|
|
53
34
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
* 3. Pre-configured aiService from route options
|
|
59
|
-
* 4. undefined (triggers demo mode response)
|
|
60
|
-
*/
|
|
61
|
-
const resolveAiService = (
|
|
62
|
-
req: express.Request,
|
|
63
|
-
options: GptRouteOptions,
|
|
64
|
-
modelId?: string
|
|
65
|
-
): AIService | undefined => {
|
|
66
|
-
const perRequestKey = req.headers["x-ai-api-key"] as string | undefined;
|
|
67
|
-
if (perRequestKey && options.createModelFn) {
|
|
68
|
-
return new AIService({model: options.createModelFn(perRequestKey, modelId)});
|
|
69
|
-
}
|
|
70
|
-
if (modelId && options.createServerModelFn) {
|
|
71
|
-
const serverModel = options.createServerModelFn(modelId);
|
|
72
|
-
if (serverModel) {
|
|
73
|
-
return new AIService({model: serverModel});
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
return options.aiService;
|
|
35
|
+
const SURFACE_BODY: OpenApiSchemaProperty = {
|
|
36
|
+
description: askSurfaceSchema.description,
|
|
37
|
+
enum: [...ASK_SURFACES],
|
|
38
|
+
type: "string",
|
|
77
39
|
};
|
|
78
40
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
}
|
|
109
|
-
return undefined;
|
|
110
|
-
}
|
|
41
|
+
interface SseSink extends ChatTurnSink {
|
|
42
|
+
isOpen: () => boolean;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const DEFAULT_STREAM_RESUME_POLL_INTERVAL_MS = 500;
|
|
46
|
+
const DEFAULT_STREAM_STALE_AFTER_MS = 60_000;
|
|
47
|
+
|
|
48
|
+
const sseEvent = (payload: Record<string, unknown>): string =>
|
|
49
|
+
`data: ${JSON.stringify(payload)}\n\n`;
|
|
50
|
+
|
|
51
|
+
const sleep = (ms: number): Promise<void> =>
|
|
52
|
+
new Promise((resolve) => {
|
|
53
|
+
setTimeout(resolve, ms);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
/** Writes turn events as server-sent events. Headers go out when the turn opens the sink. */
|
|
57
|
+
const createSseSink = (res: express.Response): SseSink => {
|
|
58
|
+
let isOpen = false;
|
|
59
|
+
return {
|
|
60
|
+
emit: (event) => {
|
|
61
|
+
res.write(`data: ${JSON.stringify(event)}\n\n`);
|
|
62
|
+
},
|
|
63
|
+
isOpen: () => isOpen,
|
|
64
|
+
open: () => {
|
|
65
|
+
res.setHeader("Content-Type", "text/event-stream");
|
|
66
|
+
res.setHeader("Cache-Control", "no-cache");
|
|
67
|
+
res.setHeader("Connection", "keep-alive");
|
|
68
|
+
isOpen = true;
|
|
69
|
+
},
|
|
70
|
+
};
|
|
111
71
|
};
|
|
112
72
|
|
|
113
73
|
export const addGptRoutes = (router: express.Router, options: GptRouteOptions): void => {
|
|
114
|
-
const {mcpService, tools: routeTools, createRequestTools
|
|
74
|
+
const {mcpService, tools: routeTools, createRequestTools} = options;
|
|
75
|
+
const askKinds = resolveAskKinds(options.asks);
|
|
76
|
+
if (options.uiBlocks) {
|
|
77
|
+
const uiBlocks = options.uiBlocks === true ? {} : options.uiBlocks;
|
|
78
|
+
configureAiDatasets({
|
|
79
|
+
datasetMaxRows: uiBlocks.datasetMaxRows,
|
|
80
|
+
datasetTtlDays: uiBlocks.datasetTtlDays,
|
|
81
|
+
});
|
|
82
|
+
addGptDatasetRoutes(router, {openApiOptions: options.openApiOptions});
|
|
83
|
+
addGptActionRoutes(router, options);
|
|
84
|
+
}
|
|
85
|
+
if (askKinds.length > 0) {
|
|
86
|
+
assertNoReservedToolNames(routeTools);
|
|
87
|
+
}
|
|
88
|
+
const resumePollIntervalMs =
|
|
89
|
+
options.streamResumePollIntervalMs ?? DEFAULT_STREAM_RESUME_POLL_INTERVAL_MS;
|
|
90
|
+
const staleAfterMs = options.streamStaleAfterMs ?? DEFAULT_STREAM_STALE_AFTER_MS;
|
|
115
91
|
|
|
116
92
|
router.post(
|
|
117
93
|
"/gpt/prompt",
|
|
@@ -121,6 +97,7 @@ export const addGptRoutes = (router: express.Router, options: GptRouteOptions):
|
|
|
121
97
|
.withTags(["gpt"])
|
|
122
98
|
.withSummary("Stream a GPT chat response")
|
|
123
99
|
.withRequestBody({
|
|
100
|
+
...(askKinds.length > 0 ? {askResponse: ASK_RESPONSE_BODY} : {}),
|
|
124
101
|
attachments: {
|
|
125
102
|
items: {
|
|
126
103
|
properties: {
|
|
@@ -137,6 +114,7 @@ export const addGptRoutes = (router: express.Router, options: GptRouteOptions):
|
|
|
137
114
|
model: {type: "string"},
|
|
138
115
|
projectId: {type: "string"},
|
|
139
116
|
prompt: {type: "string"},
|
|
117
|
+
surface: SURFACE_BODY,
|
|
140
118
|
systemPrompt: {type: "string"},
|
|
141
119
|
})
|
|
142
120
|
.withResponse(200, {data: {type: "string"}})
|
|
@@ -145,476 +123,155 @@ export const addGptRoutes = (router: express.Router, options: GptRouteOptions):
|
|
|
145
123
|
// Use a raw handler instead of asyncHandler so we can control error responses
|
|
146
124
|
// for both pre-stream (JSON) and mid-stream (SSE) errors.
|
|
147
125
|
async (req: express.Request, res: express.Response) => {
|
|
148
|
-
|
|
126
|
+
const sink = createSseSink(res);
|
|
149
127
|
try {
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
const userId = (req.user as {_id?: mongoose.Types.ObjectId} | undefined)?._id;
|
|
159
|
-
|
|
160
|
-
if (!prompt || typeof prompt !== "string") {
|
|
161
|
-
throw new APIError({status: 400, title: "prompt is required"});
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
// Resolve AI service (per-request key takes priority, then configured service)
|
|
165
|
-
const hasPerRequestKey = !!req.headers["x-ai-api-key"];
|
|
166
|
-
const hasCreateModelFn = !!options.createModelFn;
|
|
167
|
-
const hasConfiguredService = !!options.aiService;
|
|
168
|
-
logger.debug("Resolving AI service", {
|
|
169
|
-
hasConfiguredService,
|
|
170
|
-
hasCreateModelFn,
|
|
171
|
-
hasPerRequestKey,
|
|
172
|
-
});
|
|
128
|
+
await runChatTurn({body: req.body ?? {}, options, req, sink});
|
|
129
|
+
res.end();
|
|
130
|
+
} catch (error) {
|
|
131
|
+
// Catch-all for errors thrown before or after SSE streaming
|
|
132
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
133
|
+
const status = isAPIError(error) ? error.status : 500;
|
|
134
|
+
const stack = error instanceof Error ? error.stack : undefined;
|
|
135
|
+
logger.error("GPT prompt handler error", {error: message, stack, status});
|
|
173
136
|
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
137
|
+
if (sink.isOpen()) {
|
|
138
|
+
// Already sent SSE headers — send error as SSE event
|
|
139
|
+
res.write(`data: ${JSON.stringify({error: message})}\n\n`);
|
|
140
|
+
res.end();
|
|
141
|
+
return;
|
|
178
142
|
}
|
|
143
|
+
// Haven't started SSE — send a normal JSON error response
|
|
144
|
+
const fields = isAPIError(error) ? error.meta?.fields : undefined;
|
|
145
|
+
res.status(status).json({
|
|
146
|
+
detail: (isAPIError(error) ? error.detail : undefined) ?? message,
|
|
147
|
+
...(fields ? {fields} : {}),
|
|
148
|
+
status,
|
|
149
|
+
title: isAPIError(error) ? error.title : "Internal server error",
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
);
|
|
179
154
|
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
155
|
+
// Re-attach to a reply that /gpt/prompt is still streaming (e.g. after a reload or remount).
|
|
156
|
+
// Polls the persisted partial output, so it works across server instances.
|
|
157
|
+
router.get(
|
|
158
|
+
"/gpt/histories/:id/stream",
|
|
159
|
+
[
|
|
160
|
+
authenticateMiddleware(),
|
|
161
|
+
createOpenApiBuilder(options.openApiOptions ?? {})
|
|
162
|
+
.withTags(["gpt"])
|
|
163
|
+
.withSummary("Resume an in-flight GPT reply as SSE")
|
|
164
|
+
.withPathParameter("id", {type: "string"})
|
|
165
|
+
.withQueryParameter(
|
|
166
|
+
"streamId",
|
|
167
|
+
{type: "string"},
|
|
168
|
+
{description: "Reply to follow. Defaults to the latest streaming reply."}
|
|
169
|
+
)
|
|
170
|
+
.withQueryParameter(
|
|
171
|
+
"offset",
|
|
172
|
+
{type: "number"},
|
|
173
|
+
{description: "Characters of the reply the client already shows. Defaults to 0."}
|
|
174
|
+
)
|
|
175
|
+
.withResponse(200, {data: {type: "string"}})
|
|
176
|
+
.build(),
|
|
177
|
+
],
|
|
178
|
+
asyncHandler(async (req: express.Request, res: express.Response) => {
|
|
179
|
+
const {id} = req.params;
|
|
180
|
+
const userId = (req.user as {_id?: mongoose.Types.ObjectId} | undefined)?._id;
|
|
181
|
+
const requestedStreamId =
|
|
182
|
+
typeof req.query.streamId === "string" ? req.query.streamId : undefined;
|
|
183
|
+
const parsedOffset = Number(req.query.offset ?? 0);
|
|
184
|
+
const offset = Number.isFinite(parsedOffset) && parsedOffset > 0 ? parsedOffset : 0;
|
|
193
185
|
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
186
|
+
const history = await GptHistory.findById(id);
|
|
187
|
+
if (!history) {
|
|
188
|
+
throw new APIError({status: 404, title: "History not found"});
|
|
189
|
+
}
|
|
190
|
+
if (history.userId.toString() !== userId?.toString()) {
|
|
191
|
+
throw new APIError({status: 403, title: "Not authorized to access this history"});
|
|
192
|
+
}
|
|
198
193
|
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
try {
|
|
204
|
-
const project = await Project.findById(effectiveProjectId);
|
|
205
|
-
if (project && project.userId.toString() === userId?.toString()) {
|
|
206
|
-
const parts: string[] = [];
|
|
207
|
-
if (project.systemContext) {
|
|
208
|
-
parts.push(project.systemContext);
|
|
209
|
-
}
|
|
210
|
-
if (project.memories.length > 0) {
|
|
211
|
-
parts.push(
|
|
212
|
-
`## Relevant Memories\n${project.memories.map((m) => `- ${m.text}`).join("\n")}`
|
|
213
|
-
);
|
|
214
|
-
}
|
|
215
|
-
if (parts.length > 0) {
|
|
216
|
-
const projectContext = parts.join("\n\n");
|
|
217
|
-
effectiveSystemPrompt = effectiveSystemPrompt
|
|
218
|
-
? `${projectContext}\n\n${effectiveSystemPrompt}`
|
|
219
|
-
: projectContext;
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
} catch {
|
|
223
|
-
// Project loading failure should not block the request
|
|
224
|
-
}
|
|
225
|
-
}
|
|
194
|
+
const reply = requestedStreamId
|
|
195
|
+
? history.prompts.find((p) => p.streamId === requestedStreamId)
|
|
196
|
+
: history.prompts.findLast((p) => p.status === "streaming");
|
|
197
|
+
const streamId = reply?.streamId;
|
|
226
198
|
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
promptName: options.langfuseSystemPromptName,
|
|
232
|
-
userId: userId?.toString(),
|
|
233
|
-
});
|
|
234
|
-
const langfusePrompt =
|
|
235
|
-
typeof langfuseResult.prompt === "string" ? langfuseResult.prompt : undefined;
|
|
236
|
-
if (langfusePrompt) {
|
|
237
|
-
effectiveSystemPrompt = effectiveSystemPrompt
|
|
238
|
-
? `${langfusePrompt}\n\n${effectiveSystemPrompt}`
|
|
239
|
-
: langfusePrompt;
|
|
240
|
-
}
|
|
241
|
-
} catch (err) {
|
|
242
|
-
logger.debug(`Langfuse system prompt skipped: ${(err as Error).message}`);
|
|
243
|
-
}
|
|
244
|
-
}
|
|
199
|
+
res.setHeader("Content-Type", "text/event-stream");
|
|
200
|
+
res.setHeader("Cache-Control", "no-cache");
|
|
201
|
+
res.setHeader("Connection", "keep-alive");
|
|
202
|
+
res.write(sseEvent({historyId: id, resumed: true, ...(streamId ? {streamId} : {})}));
|
|
245
203
|
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
count: attachments.length,
|
|
251
|
-
types: attachments.map((a: {mimeType?: string; type?: string; url?: string}) => ({
|
|
252
|
-
mimeType: a.mimeType,
|
|
253
|
-
type: a.type,
|
|
254
|
-
urlLength: a.url?.length ?? 0,
|
|
255
|
-
})),
|
|
256
|
-
});
|
|
257
|
-
for (const attachment of attachments) {
|
|
258
|
-
if (attachment.type === "image") {
|
|
259
|
-
contentParts.push({
|
|
260
|
-
mimeType: attachment.mimeType,
|
|
261
|
-
type: "image",
|
|
262
|
-
url: attachment.url,
|
|
263
|
-
});
|
|
264
|
-
} else if (attachment.type === "file") {
|
|
265
|
-
contentParts.push({
|
|
266
|
-
filename: attachment.filename,
|
|
267
|
-
mimeType: attachment.mimeType,
|
|
268
|
-
type: "file",
|
|
269
|
-
url: attachment.url,
|
|
270
|
-
});
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
}
|
|
204
|
+
let isClosed = false;
|
|
205
|
+
req.on("close", () => {
|
|
206
|
+
isClosed = true;
|
|
207
|
+
});
|
|
274
208
|
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
// Build messages from history using AIService helper
|
|
285
|
-
logger.debug("Building messages", {
|
|
286
|
-
attachmentCount: contentParts.length - 1,
|
|
287
|
-
historyLength: history.prompts.length,
|
|
288
|
-
});
|
|
289
|
-
const messages = aiService.buildMessages(history.prompts);
|
|
290
|
-
logger.debug("Messages built", {messageCount: messages.length});
|
|
291
|
-
|
|
292
|
-
// Some models (e.g. gemini-2.5-flash-image) don't support tool calling
|
|
293
|
-
const modelId = aiService.modelId;
|
|
294
|
-
const supportsTools = !modelId?.includes("image");
|
|
295
|
-
|
|
296
|
-
// Merge tools from route config, per-request tools, and MCP service
|
|
297
|
-
let allTools: Record<string, Tool> | undefined;
|
|
298
|
-
const requestTools = createRequestTools ? createRequestTools(req) : undefined;
|
|
299
|
-
if (supportsTools && (routeTools || requestTools || mcpService)) {
|
|
300
|
-
allTools = {...(routeTools ?? {}), ...(requestTools ?? {})};
|
|
301
|
-
if (mcpService) {
|
|
302
|
-
try {
|
|
303
|
-
const mcpTools = await mcpService.getTools();
|
|
304
|
-
Object.assign(allTools, mcpTools);
|
|
305
|
-
} catch {
|
|
306
|
-
// MCP tool discovery failure should not block the request
|
|
307
|
-
}
|
|
209
|
+
let isInitialPoll = true;
|
|
210
|
+
let sentText: string | undefined;
|
|
211
|
+
let sentLength = offset;
|
|
212
|
+
try {
|
|
213
|
+
while (!isClosed) {
|
|
214
|
+
const current = streamId ? await GptHistory.findById(id) : history;
|
|
215
|
+
if (!current) {
|
|
216
|
+
res.write(sseEvent({error: "History not found"}));
|
|
217
|
+
break;
|
|
308
218
|
}
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
if (generatedImages.some((img) => img.url === url)) {
|
|
323
|
-
return;
|
|
219
|
+
const currentReply = streamId
|
|
220
|
+
? current.prompts.find((p) => p.streamId === streamId)
|
|
221
|
+
: undefined;
|
|
222
|
+
const text = currentReply?.text ?? "";
|
|
223
|
+
|
|
224
|
+
if (isInitialPoll && offset > 0) {
|
|
225
|
+
// The client can have loaded a stale placeholder before reconnecting.
|
|
226
|
+
res.write(sseEvent({replace: true, text}));
|
|
227
|
+
} else if (sentText !== undefined && !text.startsWith(sentText)) {
|
|
228
|
+
// Text from a step that turned into a tool call was discarded; resend the reply
|
|
229
|
+
res.write(sseEvent({replace: true, text}));
|
|
230
|
+
} else if (text.length > sentLength) {
|
|
231
|
+
res.write(sseEvent({text: text.slice(sentLength)}));
|
|
324
232
|
}
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
: {}
|
|
337
|
-
},
|
|
338
|
-
userId: userId?.toString(),
|
|
339
|
-
});
|
|
340
|
-
const result = streamText({
|
|
341
|
-
experimental_telemetry: telemetry,
|
|
342
|
-
messages,
|
|
343
|
-
model: aiService.model,
|
|
344
|
-
providerOptions: !supportsTools
|
|
345
|
-
? {
|
|
346
|
-
google: {responseModalities: ["TEXT", "IMAGE"]},
|
|
347
|
-
vertex: {responseModalities: ["TEXT", "IMAGE"]},
|
|
348
|
-
}
|
|
349
|
-
: undefined,
|
|
350
|
-
stopWhen: allTools ? stepCountIs(maxSteps ?? 5) : stepCountIs(1),
|
|
351
|
-
system: effectiveSystemPrompt ?? undefined,
|
|
352
|
-
temperature: aiService.defaultTemperature,
|
|
353
|
-
toolChoice: allTools ? (toolChoice ?? "auto") : undefined,
|
|
354
|
-
tools: allTools,
|
|
355
|
-
});
|
|
356
|
-
|
|
357
|
-
let partCount = 0;
|
|
358
|
-
// Buffer text per step so we can discard reasoning text when a tool call follows
|
|
359
|
-
let stepTextBuffer = "";
|
|
360
|
-
let stepHasToolCall = false;
|
|
361
|
-
|
|
362
|
-
for await (const part of result.fullStream as AsyncIterable<{
|
|
363
|
-
type: string;
|
|
364
|
-
[key: string]: unknown;
|
|
365
|
-
}>) {
|
|
366
|
-
partCount++;
|
|
367
|
-
if (partCount <= 5 || part.type === "error" || part.type === "file") {
|
|
368
|
-
logger.debug("Stream part", {
|
|
369
|
-
partCount,
|
|
370
|
-
type: part.type,
|
|
371
|
-
...(part.type === "file" && isGeneratedImageFile(part.file)
|
|
372
|
-
? {mediaType: part.file.mediaType}
|
|
373
|
-
: {}),
|
|
374
|
-
...(part.type === "error" ? {error: String(part.error ?? part)} : {}),
|
|
375
|
-
});
|
|
376
|
-
}
|
|
377
|
-
|
|
378
|
-
// Track step boundaries to discard reasoning text from tool-call steps
|
|
379
|
-
if (part.type === "start-step") {
|
|
380
|
-
stepTextBuffer = "";
|
|
381
|
-
stepHasToolCall = false;
|
|
382
|
-
continue;
|
|
383
|
-
}
|
|
384
|
-
if (part.type === "finish-step") {
|
|
385
|
-
// Only emit buffered text if no tool call happened in this step
|
|
386
|
-
if (!stepHasToolCall && stepTextBuffer) {
|
|
387
|
-
// Strip model reasoning that leaks as JSON action blobs
|
|
388
|
-
const cleaned = stepTextBuffer
|
|
389
|
-
.replace(/\{[\s\S]*?"action"[\s\S]*?\}\s*$/g, "")
|
|
390
|
-
.trim();
|
|
391
|
-
if (cleaned) {
|
|
392
|
-
fullResponse += cleaned;
|
|
393
|
-
res.write(`data: ${JSON.stringify({text: cleaned})}\n\n`);
|
|
394
|
-
}
|
|
395
|
-
}
|
|
396
|
-
stepTextBuffer = "";
|
|
397
|
-
stepHasToolCall = false;
|
|
398
|
-
continue;
|
|
399
|
-
}
|
|
400
|
-
|
|
401
|
-
if (part.type === "error") {
|
|
402
|
-
const errMsg =
|
|
403
|
-
part.error instanceof Error
|
|
404
|
-
? part.error.message
|
|
405
|
-
: String(part.error ?? "Unknown stream error");
|
|
406
|
-
logger.error("AI stream error part", {error: errMsg});
|
|
407
|
-
res.write(`data: ${JSON.stringify({error: errMsg})}\n\n`);
|
|
408
|
-
continue;
|
|
409
|
-
}
|
|
410
|
-
if (part.type === "file") {
|
|
411
|
-
if (isGeneratedImageFile(part.file)) {
|
|
412
|
-
sendGeneratedImage(part.file);
|
|
413
|
-
logger.debug("Sent inline image from stream");
|
|
414
|
-
}
|
|
415
|
-
continue;
|
|
416
|
-
}
|
|
417
|
-
if (part.type === "text-delta") {
|
|
418
|
-
const textChunk = (part.text ?? "") as string;
|
|
419
|
-
if (textChunk) {
|
|
420
|
-
stepTextBuffer += textChunk;
|
|
421
|
-
}
|
|
422
|
-
} else if (part.type === "tool-call") {
|
|
423
|
-
stepHasToolCall = true;
|
|
424
|
-
res.write(
|
|
425
|
-
`data: ${JSON.stringify({
|
|
426
|
-
toolCall: {
|
|
427
|
-
args: part.input,
|
|
428
|
-
toolCallId: part.toolCallId,
|
|
429
|
-
toolName: part.toolName,
|
|
430
|
-
},
|
|
431
|
-
})}\n\n`
|
|
432
|
-
);
|
|
433
|
-
// Persist tool call in history
|
|
434
|
-
history.prompts.push({
|
|
435
|
-
args: part.input as Record<string, unknown>,
|
|
436
|
-
text: `Tool call: ${part.toolName as string}`,
|
|
437
|
-
toolCallId: part.toolCallId as string,
|
|
438
|
-
toolName: part.toolName as string,
|
|
439
|
-
type: "tool-call",
|
|
440
|
-
});
|
|
441
|
-
} else if (part.type === "tool-result") {
|
|
442
|
-
const toolResult = part.output as Record<string, unknown> | undefined;
|
|
443
|
-
|
|
444
|
-
// If the tool result contains file data, send it as a separate file SSE event
|
|
445
|
-
if (toolResult?.fileData && typeof toolResult.fileData === "string") {
|
|
446
|
-
res.write(
|
|
447
|
-
`data: ${JSON.stringify({
|
|
448
|
-
file: {
|
|
449
|
-
filename: toolResult.filename ?? "document",
|
|
450
|
-
mimeType: toolResult.mimeType ?? "application/octet-stream",
|
|
451
|
-
url: toolResult.fileData,
|
|
452
|
-
},
|
|
453
|
-
})}\n\n`
|
|
454
|
-
);
|
|
455
|
-
logger.debug("Sent generated file from tool result", {
|
|
456
|
-
filename: toolResult.filename,
|
|
457
|
-
mimeType: toolResult.mimeType,
|
|
458
|
-
});
|
|
459
|
-
}
|
|
460
|
-
|
|
461
|
-
// Strip fileData from result before sending/storing to avoid bloating the SSE and DB
|
|
462
|
-
const {fileData: _fileData, ...cleanResult} = toolResult ?? {};
|
|
463
|
-
|
|
464
|
-
res.write(
|
|
465
|
-
`data: ${JSON.stringify({
|
|
466
|
-
toolResult: {
|
|
467
|
-
result: cleanResult,
|
|
468
|
-
toolCallId: part.toolCallId,
|
|
469
|
-
toolName: part.toolName,
|
|
470
|
-
},
|
|
471
|
-
})}\n\n`
|
|
233
|
+
isInitialPoll = false;
|
|
234
|
+
sentText = text;
|
|
235
|
+
sentLength = text.length;
|
|
236
|
+
|
|
237
|
+
if (currentReply?.status === "streaming") {
|
|
238
|
+
const sinceUpdateMs = DateTime.now()
|
|
239
|
+
.diff(DateTime.fromJSDate(current.updated))
|
|
240
|
+
.toMillis();
|
|
241
|
+
if (sinceUpdateMs > staleAfterMs) {
|
|
242
|
+
await GptHistory.updateOne(
|
|
243
|
+
{_id: current._id, prompts: {$elemMatch: {status: "streaming", streamId}}},
|
|
244
|
+
{$set: {"prompts.$.status": "error"}}
|
|
472
245
|
);
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
text: `Tool result: ${part.toolName as string}`,
|
|
477
|
-
toolCallId: part.toolCallId as string,
|
|
478
|
-
toolName: part.toolName as string,
|
|
479
|
-
type: "tool-result",
|
|
480
|
-
});
|
|
246
|
+
res.write(sseEvent({error: "The reply was interrupted before it finished"}));
|
|
247
|
+
res.write(sseEvent({done: true, historyId: id}));
|
|
248
|
+
break;
|
|
481
249
|
}
|
|
250
|
+
await sleep(resumePollIntervalMs);
|
|
251
|
+
continue;
|
|
482
252
|
}
|
|
483
253
|
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
if (cleaned) {
|
|
488
|
-
fullResponse += cleaned;
|
|
489
|
-
res.write(`data: ${JSON.stringify({text: cleaned})}\n\n`);
|
|
490
|
-
}
|
|
491
|
-
}
|
|
492
|
-
|
|
493
|
-
logger.debug("Stream completed", {fullResponseLength: fullResponse.length, partCount});
|
|
494
|
-
|
|
495
|
-
// Catch generated images the stream did not report as file parts
|
|
496
|
-
try {
|
|
497
|
-
const files = await result.files;
|
|
498
|
-
if (files && files.length > 0) {
|
|
499
|
-
for (const file of files) {
|
|
500
|
-
if (isGeneratedImageFile(file)) {
|
|
501
|
-
sendGeneratedImage(file);
|
|
502
|
-
}
|
|
503
|
-
}
|
|
504
|
-
logger.debug("Sent generated images", {count: generatedImages.length});
|
|
505
|
-
}
|
|
506
|
-
} catch (fileErr) {
|
|
507
|
-
logger.debug("No files in response", {
|
|
508
|
-
error: fileErr instanceof Error ? fileErr.message : String(fileErr),
|
|
509
|
-
});
|
|
510
|
-
}
|
|
511
|
-
|
|
512
|
-
// Save assistant response to history
|
|
513
|
-
if (fullResponse || generatedImages.length > 0) {
|
|
514
|
-
const contentParts: MessageContentPart[] = generatedImages.map((img) => ({
|
|
515
|
-
mimeType: img.mimeType,
|
|
516
|
-
type: "image" as const,
|
|
517
|
-
url: img.url,
|
|
518
|
-
}));
|
|
519
|
-
const assistantPrompt: GptHistoryPrompt = {
|
|
520
|
-
model: aiService.modelId,
|
|
521
|
-
text: fullResponse,
|
|
522
|
-
type: "assistant",
|
|
523
|
-
...(contentParts.length > 0 ? {content: contentParts} : {}),
|
|
524
|
-
};
|
|
525
|
-
history.prompts.push(assistantPrompt);
|
|
526
|
-
}
|
|
527
|
-
await history.save();
|
|
528
|
-
|
|
529
|
-
try {
|
|
530
|
-
await AIRequest.logRequest({
|
|
531
|
-
aiModel: modelId ?? "unknown",
|
|
532
|
-
prompt,
|
|
533
|
-
requestType: "general",
|
|
534
|
-
response: fullResponse,
|
|
535
|
-
responseTime: DateTime.now().toMillis() - startTime,
|
|
536
|
-
userId: userId ?? undefined,
|
|
537
|
-
});
|
|
538
|
-
} catch (logErr) {
|
|
539
|
-
logger.warn("Failed to log AIRequest", {
|
|
540
|
-
error: logErr instanceof Error ? logErr.message : String(logErr),
|
|
541
|
-
});
|
|
542
|
-
}
|
|
543
|
-
|
|
544
|
-
// Generate a title for new conversations using a cheap model call
|
|
545
|
-
if (!history.title && fullResponse) {
|
|
546
|
-
const perRequestApiKey = req.headers["x-ai-api-key"] as string | undefined;
|
|
547
|
-
const title = await generateTitle(
|
|
548
|
-
prompt,
|
|
549
|
-
fullResponse,
|
|
550
|
-
aiService,
|
|
551
|
-
options,
|
|
552
|
-
perRequestApiKey
|
|
553
|
-
);
|
|
554
|
-
if (title) {
|
|
555
|
-
history.title = title;
|
|
556
|
-
await history.save();
|
|
254
|
+
for (const part of currentReply?.content ?? []) {
|
|
255
|
+
if (part.type === "image") {
|
|
256
|
+
res.write(sseEvent({image: {mimeType: part.mimeType, url: part.url}}));
|
|
557
257
|
}
|
|
558
258
|
}
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
fullResponseLength: fullResponse.length,
|
|
562
|
-
historyId: history._id.toString(),
|
|
563
|
-
});
|
|
564
|
-
res.write(
|
|
565
|
-
`data: ${JSON.stringify({
|
|
566
|
-
done: true,
|
|
567
|
-
historyId: history._id.toString(),
|
|
568
|
-
...(history.title ? {title: history.title} : {}),
|
|
569
|
-
})}\n\n`
|
|
570
|
-
);
|
|
571
|
-
res.end();
|
|
572
|
-
} catch (error) {
|
|
573
|
-
logger.error("Error in GPT stream", {
|
|
574
|
-
error: error instanceof Error ? error.message : String(error),
|
|
575
|
-
});
|
|
576
|
-
|
|
577
|
-
try {
|
|
578
|
-
await AIRequest.logRequest({
|
|
579
|
-
aiModel: modelId ?? "unknown",
|
|
580
|
-
error: error instanceof Error ? error.message : String(error),
|
|
581
|
-
prompt,
|
|
582
|
-
requestType: "general",
|
|
583
|
-
responseTime: DateTime.now().toMillis() - startTime,
|
|
584
|
-
userId: userId ?? undefined,
|
|
585
|
-
});
|
|
586
|
-
} catch (logErr) {
|
|
587
|
-
logger.warn("Failed to log AIRequest error", {
|
|
588
|
-
error: logErr instanceof Error ? logErr.message : String(logErr),
|
|
589
|
-
});
|
|
259
|
+
if (currentReply?.status === "error") {
|
|
260
|
+
res.write(sseEvent({error: "The reply was interrupted before it finished"}));
|
|
590
261
|
}
|
|
591
|
-
|
|
592
262
|
res.write(
|
|
593
|
-
|
|
263
|
+
sseEvent({done: true, historyId: id, ...(current.title ? {title: current.title} : {})})
|
|
594
264
|
);
|
|
595
|
-
|
|
596
|
-
}
|
|
597
|
-
} catch (outerError) {
|
|
598
|
-
// Catch-all for errors thrown before or after SSE streaming
|
|
599
|
-
const message = outerError instanceof Error ? outerError.message : String(outerError);
|
|
600
|
-
const status = outerError instanceof APIError ? outerError.status : 500;
|
|
601
|
-
const stack = outerError instanceof Error ? outerError.stack : undefined;
|
|
602
|
-
logger.error("GPT prompt handler error", {error: message, stack, status});
|
|
603
|
-
|
|
604
|
-
if (sseStarted) {
|
|
605
|
-
// Already sent SSE headers — send error as SSE event
|
|
606
|
-
res.write(`data: ${JSON.stringify({error: message})}\n\n`);
|
|
607
|
-
res.end();
|
|
608
|
-
} else {
|
|
609
|
-
// Haven't started SSE — send a normal JSON error response
|
|
610
|
-
res.status(status).json({
|
|
611
|
-
detail: message,
|
|
612
|
-
status,
|
|
613
|
-
title: outerError instanceof APIError ? outerError.title : "Internal server error",
|
|
614
|
-
});
|
|
265
|
+
break;
|
|
615
266
|
}
|
|
267
|
+
} catch (error) {
|
|
268
|
+
logger.error("Error resuming GPT stream", {
|
|
269
|
+
error: error instanceof Error ? error.message : String(error),
|
|
270
|
+
});
|
|
271
|
+
res.write(sseEvent({error: error instanceof Error ? error.message : "Unknown error"}));
|
|
616
272
|
}
|
|
617
|
-
|
|
273
|
+
res.end();
|
|
274
|
+
})
|
|
618
275
|
);
|
|
619
276
|
|
|
620
277
|
router.patch(
|
|
@@ -637,7 +294,7 @@ export const addGptRoutes = (router: express.Router, options: GptRouteOptions):
|
|
|
637
294
|
const {promptIndex, rating} = req.body;
|
|
638
295
|
const userId = (req.user as {_id?: mongoose.Types.ObjectId} | undefined)?._id;
|
|
639
296
|
|
|
640
|
-
if (typeof promptIndex !== "number" || promptIndex < 0) {
|
|
297
|
+
if (typeof promptIndex !== "number" || !Number.isInteger(promptIndex) || promptIndex < 0) {
|
|
641
298
|
throw new APIError({status: 400, title: "promptIndex must be a non-negative number"});
|
|
642
299
|
}
|
|
643
300
|
if (rating !== null && rating !== "up" && rating !== "down") {
|
|
@@ -655,15 +312,14 @@ export const addGptRoutes = (router: express.Router, options: GptRouteOptions):
|
|
|
655
312
|
throw new APIError({status: 400, title: "promptIndex out of range"});
|
|
656
313
|
}
|
|
657
314
|
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
history.
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
await history.save();
|
|
315
|
+
// One field update, so rows a running turn appends meanwhile are kept.
|
|
316
|
+
const ratingPath = `prompts.${promptIndex}.rating`;
|
|
317
|
+
await GptHistory.updateOne(
|
|
318
|
+
{_id: history._id},
|
|
319
|
+
rating === null ? {$unset: {[ratingPath]: ""}} : {$set: {[ratingPath]: rating}}
|
|
320
|
+
);
|
|
665
321
|
|
|
666
|
-
return res.json({data: {promptIndex, rating
|
|
322
|
+
return res.json({data: {promptIndex, rating}});
|
|
667
323
|
})
|
|
668
324
|
);
|
|
669
325
|
|
|
@@ -683,10 +339,16 @@ export const addGptRoutes = (router: express.Router, options: GptRouteOptions):
|
|
|
683
339
|
asyncHandler(async (req: express.Request, res: express.Response) => {
|
|
684
340
|
const {text} = req.body;
|
|
685
341
|
const userId = (req.user as {_id?: mongoose.Types.ObjectId} | undefined)?._id;
|
|
342
|
+
const hasPromptReference =
|
|
343
|
+
typeof req.body.promptName === "string" || typeof req.body.promptLabel === "string";
|
|
344
|
+
const isAdmin = (req.user as {admin?: boolean} | undefined)?.admin === true;
|
|
686
345
|
|
|
687
346
|
if (!text || typeof text !== "string") {
|
|
688
347
|
throw new APIError({status: 400, title: "text is required"});
|
|
689
348
|
}
|
|
349
|
+
if (hasPromptReference && !isAdmin) {
|
|
350
|
+
throw new APIError({status: 403, title: "Prompt registry selection requires admin access"});
|
|
351
|
+
}
|
|
690
352
|
|
|
691
353
|
const aiService = resolveAiService(req, options);
|
|
692
354
|
if (!aiService) {
|