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