@terreno/ai 57.9.0 → 57.11.0
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/README.md +1 -0
- package/bunfig.toml +1 -1
- package/dist/aiApp.d.ts +8 -2
- package/dist/aiApp.js +12 -6
- 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 +133 -0
- package/dist/harness/errors.js +107 -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 +522 -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 +191 -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 +64 -0
- package/dist/harness/runners/inProcessRunner.js +227 -0
- package/dist/harness/runners/jobsRunner.d.ts +62 -0
- package/dist/harness/runners/jobsRunner.js +212 -0
- package/dist/harness/runtime.d.ts +52 -0
- package/dist/harness/runtime.js +604 -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 +73 -0
- 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 +18 -7
- package/dist/routes/gpt.js +79 -689
- 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 +426 -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/fillActionIds.d.ts +6 -0
- package/dist/service/fillActionIds.js +79 -0
- 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/types/harness.d.ts +952 -0
- package/dist/types/harness.js +133 -0
- package/dist/types/index.d.ts +192 -14
- 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 +28 -5
- package/src/aiApp.test.ts +207 -1
- package/src/aiApp.ts +19 -6
- 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/concurrency.test.ts +155 -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 +125 -0
- package/src/harness/eventHub.test.ts +124 -0
- package/src/harness/eventHub.ts +159 -0
- package/src/harness/events.test.ts +769 -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 +778 -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 +206 -0
- package/src/harness/ownership.test.ts +1087 -0
- package/src/harness/ownership.ts +618 -0
- package/src/harness/recovery.test.ts +907 -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 +530 -0
- package/src/harness/runners/inProcessRunner.ts +260 -0
- package/src/harness/runners/jobsRunner.test.ts +709 -0
- package/src/harness/runners/jobsRunner.ts +252 -0
- package/src/harness/runtime.ts +802 -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.ts +2 -1
- package/src/models/gptHistory.test.ts +156 -0
- package/src/models/gptHistory.ts +89 -1
- 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 +63 -9
- package/src/routes/files.ts +21 -7
- package/src/routes/gpt.test.ts +3279 -0
- package/src/routes/gpt.ts +91 -816
- 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 +222 -2
- package/src/service/aiDatasets.test.ts +123 -0
- package/src/service/aiDatasets.ts +316 -0
- package/src/service/aiService.test.ts +593 -0
- package/src/service/aiService.ts +568 -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/fillActionIds.test.ts +65 -0
- package/src/service/fillActionIds.ts +98 -0
- 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/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 +1119 -0
- package/src/types/index.ts +209 -13
- package/src/types/observability.ts +344 -0
- package/src/types/sanitize-html.d.ts +25 -0
package/src/service/aiService.ts
CHANGED
|
@@ -1,7 +1,17 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
1
|
+
import {randomUUID} from "node:crypto";
|
|
2
|
+
import {APIError, logger} from "@terreno/api";
|
|
3
|
+
import {type BlocksDocument, blocksJsonSchema, validateBlocks} from "@terreno/blocks";
|
|
4
|
+
import type {
|
|
5
|
+
DataContent,
|
|
6
|
+
JSONValue,
|
|
7
|
+
LanguageModel,
|
|
8
|
+
ModelMessage,
|
|
9
|
+
ToolCallPart,
|
|
10
|
+
ToolResultPart,
|
|
11
|
+
} from "ai";
|
|
3
12
|
import {
|
|
4
13
|
generateText as aiGenerateText,
|
|
14
|
+
jsonSchema,
|
|
5
15
|
NoObjectGeneratedError,
|
|
6
16
|
Output,
|
|
7
17
|
stepCountIs,
|
|
@@ -11,6 +21,8 @@ import {DateTime} from "luxon";
|
|
|
11
21
|
import type mongoose from "mongoose";
|
|
12
22
|
|
|
13
23
|
import {AIRequest} from "../models/aiRequest";
|
|
24
|
+
import {getObservabilityApp} from "../observability/observabilityAppRegistry";
|
|
25
|
+
import type {ModelPrice, PromptVersionRef, SpanRecord, TraceRecord} from "../observability/types";
|
|
14
26
|
import type {
|
|
15
27
|
AIRequestType,
|
|
16
28
|
AIServiceOptions,
|
|
@@ -18,6 +30,7 @@ import type {
|
|
|
18
30
|
GenerateJsonArrayOptions,
|
|
19
31
|
GenerateJsonObjectOptions,
|
|
20
32
|
GenerateJsonValueOptions,
|
|
33
|
+
GenerateObservabilityOptions,
|
|
21
34
|
GenerateStreamOptions,
|
|
22
35
|
GenerateTextOptions,
|
|
23
36
|
GptHistoryPrompt,
|
|
@@ -25,12 +38,13 @@ import type {
|
|
|
25
38
|
SummaryOptions,
|
|
26
39
|
TranslateOptions,
|
|
27
40
|
} from "../types";
|
|
28
|
-
import {
|
|
41
|
+
import {withStrippedJsonFencesModel} from "./jsonFenceModel";
|
|
29
42
|
import {
|
|
30
43
|
CONTENT_SUMMARY_PROMPT,
|
|
31
44
|
DEFAULT_GPT_MEMORY,
|
|
32
45
|
JSON_VALUE_SYSTEM_PROMPT,
|
|
33
46
|
REMIX_PROMPT,
|
|
47
|
+
TERRENO_UI_BLOCKS_SYSTEM_PROMPT,
|
|
34
48
|
TRANSLATION_PROMPT,
|
|
35
49
|
} from "./prompts";
|
|
36
50
|
|
|
@@ -44,62 +58,88 @@ export const TemperaturePresets = {
|
|
|
44
58
|
} as const;
|
|
45
59
|
|
|
46
60
|
/**
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
* before Vercel `Output.*` parsing.
|
|
61
|
+
* True for the call row of an ask the model made. Approval asks are display-only: the model did
|
|
62
|
+
* not call a tool by that name with those arguments, so they are not replayed.
|
|
50
63
|
*/
|
|
51
|
-
const
|
|
64
|
+
const isModelAskCall = (prompt: GptHistoryPrompt): boolean =>
|
|
65
|
+
prompt.type === "tool-call" && prompt.ask !== undefined && prompt.ask.origin !== "approval";
|
|
66
|
+
|
|
67
|
+
/** Result rows of the model's ask tool calls, by tool call id. */
|
|
68
|
+
const collectAskResults = (prompts: GptHistoryPrompt[]): Map<string, GptHistoryPrompt> => {
|
|
69
|
+
const askCallIds = new Set(
|
|
70
|
+
prompts
|
|
71
|
+
.filter((prompt) => isModelAskCall(prompt) && prompt.toolCallId)
|
|
72
|
+
.map((prompt) => prompt.toolCallId)
|
|
73
|
+
);
|
|
74
|
+
return new Map(
|
|
75
|
+
prompts
|
|
76
|
+
.filter(
|
|
77
|
+
(prompt): prompt is GptHistoryPrompt & {toolCallId: string} =>
|
|
78
|
+
prompt.type === "tool-result" &&
|
|
79
|
+
prompt.toolCallId !== undefined &&
|
|
80
|
+
askCallIds.has(prompt.toolCallId)
|
|
81
|
+
)
|
|
82
|
+
.map((prompt) => [prompt.toolCallId, prompt])
|
|
83
|
+
);
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
const getModelId = (model: LanguageModel): string => {
|
|
52
87
|
if (typeof model === "string") {
|
|
53
88
|
return model;
|
|
54
89
|
}
|
|
90
|
+
return (model as {modelId?: string}).modelId ?? "unknown";
|
|
91
|
+
};
|
|
55
92
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
const original = Reflect.get(target, prop, receiver);
|
|
60
|
-
if (typeof original !== "function") {
|
|
61
|
-
return original;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
const boundGenerate = original as (options: unknown) => PromiseLike<{
|
|
65
|
-
content: Array<{text?: string; type: string; [key: string]: unknown}>;
|
|
66
|
-
[key: string]: unknown;
|
|
67
|
-
}>;
|
|
68
|
-
|
|
69
|
-
return async (options: unknown) => {
|
|
70
|
-
const result = await Promise.resolve(boundGenerate.call(target, options));
|
|
71
|
-
if (!result?.content || !Array.isArray(result.content)) {
|
|
72
|
-
return result;
|
|
73
|
-
}
|
|
93
|
+
const toIsoUtc = (millis: number): string => {
|
|
94
|
+
return DateTime.fromMillis(millis, {zone: "utc"}).toISO() ?? "";
|
|
95
|
+
};
|
|
74
96
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
...part,
|
|
84
|
-
text: normalizeLlmJsonTextForStructuredOutput(part.text),
|
|
85
|
-
};
|
|
86
|
-
}),
|
|
87
|
-
};
|
|
88
|
-
};
|
|
97
|
+
const readTokenUsage = (
|
|
98
|
+
usage:
|
|
99
|
+
| {
|
|
100
|
+
completionTokens?: number;
|
|
101
|
+
inputTokens?: number;
|
|
102
|
+
outputTokens?: number;
|
|
103
|
+
promptTokens?: number;
|
|
104
|
+
totalTokens?: number;
|
|
89
105
|
}
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
106
|
+
| undefined
|
|
107
|
+
): {inputTokens?: number; outputTokens?: number; totalTokens?: number} => {
|
|
108
|
+
return {
|
|
109
|
+
inputTokens: usage?.inputTokens ?? usage?.promptTokens,
|
|
110
|
+
outputTokens: usage?.outputTokens ?? usage?.completionTokens,
|
|
111
|
+
totalTokens: usage?.totalTokens,
|
|
112
|
+
};
|
|
94
113
|
};
|
|
95
114
|
|
|
96
|
-
const
|
|
97
|
-
|
|
98
|
-
|
|
115
|
+
const computeCostUsd = (params: {
|
|
116
|
+
inputTokens?: number;
|
|
117
|
+
modelId: string;
|
|
118
|
+
outputTokens?: number;
|
|
119
|
+
priceMap?: Record<string, ModelPrice>;
|
|
120
|
+
}): number | undefined => {
|
|
121
|
+
if (!params.priceMap) {
|
|
122
|
+
return undefined;
|
|
99
123
|
}
|
|
100
|
-
|
|
124
|
+
const price = params.priceMap[params.modelId];
|
|
125
|
+
if (!price || params.inputTokens === undefined || params.outputTokens === undefined) {
|
|
126
|
+
return undefined;
|
|
127
|
+
}
|
|
128
|
+
return (
|
|
129
|
+
(price.inputPerMTok * params.inputTokens + price.outputPerMTok * params.outputTokens) /
|
|
130
|
+
1_000_000
|
|
131
|
+
);
|
|
101
132
|
};
|
|
102
133
|
|
|
134
|
+
interface ResolvedObservability {
|
|
135
|
+
priceMap?: Record<string, ModelPrice>;
|
|
136
|
+
promptRef?: PromptVersionRef;
|
|
137
|
+
sensitive: boolean;
|
|
138
|
+
sessionId?: string;
|
|
139
|
+
skipTrace: boolean;
|
|
140
|
+
systemPrompt?: string;
|
|
141
|
+
}
|
|
142
|
+
|
|
103
143
|
export class AIService {
|
|
104
144
|
readonly model: LanguageModel;
|
|
105
145
|
readonly defaultTemperature: number;
|
|
@@ -154,9 +194,11 @@ export class AIService {
|
|
|
154
194
|
|
|
155
195
|
private async logStructuredJsonFailure(params: {
|
|
156
196
|
error: unknown;
|
|
197
|
+
observability: ResolvedObservability;
|
|
157
198
|
prompt: string;
|
|
158
199
|
requestType: AIRequestType;
|
|
159
200
|
responseTime: number;
|
|
201
|
+
startTime: number;
|
|
160
202
|
system: string;
|
|
161
203
|
userId?: mongoose.Types.ObjectId;
|
|
162
204
|
}): Promise<void> {
|
|
@@ -184,8 +226,7 @@ export class AIService {
|
|
|
184
226
|
system: params.system,
|
|
185
227
|
});
|
|
186
228
|
|
|
187
|
-
await this.
|
|
188
|
-
aiModel: getModelId(this.model),
|
|
229
|
+
await this.logRequestAndTrace({
|
|
189
230
|
error: errorDescription,
|
|
190
231
|
metadata: {
|
|
191
232
|
errorStack,
|
|
@@ -193,10 +234,12 @@ export class AIService {
|
|
|
193
234
|
rawModelTextCaptured: Boolean(rawText && rawText.length > 0),
|
|
194
235
|
system: params.system,
|
|
195
236
|
},
|
|
237
|
+
observability: params.observability,
|
|
196
238
|
prompt: params.prompt,
|
|
197
239
|
requestType: params.requestType,
|
|
198
240
|
response: responseForLog,
|
|
199
241
|
responseTime: params.responseTime,
|
|
242
|
+
startTime: params.startTime,
|
|
200
243
|
userId: params.userId,
|
|
201
244
|
});
|
|
202
245
|
}
|
|
@@ -219,8 +262,243 @@ export class AIService {
|
|
|
219
262
|
}
|
|
220
263
|
}
|
|
221
264
|
|
|
265
|
+
private async resolveObservability(
|
|
266
|
+
options: GenerateObservabilityOptions & {systemPrompt?: string}
|
|
267
|
+
): Promise<ResolvedObservability> {
|
|
268
|
+
let promptRef: PromptVersionRef | undefined;
|
|
269
|
+
let systemPrompt = options.systemPrompt;
|
|
270
|
+
let sensitive = options.sensitive ?? false;
|
|
271
|
+
|
|
272
|
+
if (options.promptName) {
|
|
273
|
+
const registry = getObservabilityApp()?.promptRegistry;
|
|
274
|
+
if (!registry) {
|
|
275
|
+
throw new APIError({status: 400, title: "Prompt registry is not configured"});
|
|
276
|
+
}
|
|
277
|
+
const label = options.promptLabel ?? "production";
|
|
278
|
+
const version = await registry.get({label, name: options.promptName});
|
|
279
|
+
if (!version) {
|
|
280
|
+
throw new APIError({
|
|
281
|
+
status: 400,
|
|
282
|
+
title: `Unknown prompt "${options.promptName}" with label "${label}"`,
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
promptRef = version;
|
|
286
|
+
systemPrompt = version.body;
|
|
287
|
+
if (options.sensitive === undefined) {
|
|
288
|
+
sensitive = Boolean(version.sensitive);
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
return {
|
|
293
|
+
priceMap: options.priceMap,
|
|
294
|
+
promptRef,
|
|
295
|
+
sensitive,
|
|
296
|
+
sessionId: options.sessionId,
|
|
297
|
+
skipTrace: options.skipTrace === true,
|
|
298
|
+
systemPrompt,
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
private observabilityFields(options: GenerateObservabilityOptions): GenerateObservabilityOptions {
|
|
303
|
+
return {
|
|
304
|
+
priceMap: options.priceMap,
|
|
305
|
+
promptLabel: options.promptLabel,
|
|
306
|
+
promptName: options.promptName,
|
|
307
|
+
sensitive: options.sensitive,
|
|
308
|
+
sessionId: options.sessionId,
|
|
309
|
+
skipTrace: options.skipTrace,
|
|
310
|
+
};
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
private buildTraceRecord(params: {
|
|
314
|
+
error?: string;
|
|
315
|
+
childSpans?: SpanRecord[];
|
|
316
|
+
inputTokens?: number;
|
|
317
|
+
metadata?: Record<string, unknown>;
|
|
318
|
+
observability: ResolvedObservability;
|
|
319
|
+
outputTokens?: number;
|
|
320
|
+
prompt: string;
|
|
321
|
+
requestType: AIRequestType;
|
|
322
|
+
response?: string;
|
|
323
|
+
responseTime: number;
|
|
324
|
+
startTime: number;
|
|
325
|
+
userId?: mongoose.Types.ObjectId;
|
|
326
|
+
}): TraceRecord {
|
|
327
|
+
const modelId = getModelId(this.model);
|
|
328
|
+
const priceMap = params.observability.priceMap;
|
|
329
|
+
const app = getObservabilityApp();
|
|
330
|
+
const effectivePriceMap = priceMap ?? app?.priceMap;
|
|
331
|
+
const costUsd = computeCostUsd({
|
|
332
|
+
inputTokens: params.inputTokens,
|
|
333
|
+
modelId,
|
|
334
|
+
outputTokens: params.outputTokens,
|
|
335
|
+
priceMap: effectivePriceMap,
|
|
336
|
+
});
|
|
337
|
+
const usage = {
|
|
338
|
+
inputTokens: params.inputTokens,
|
|
339
|
+
model: modelId,
|
|
340
|
+
outputTokens: params.outputTokens,
|
|
341
|
+
...(costUsd === undefined ? {} : {costUsd}),
|
|
342
|
+
};
|
|
343
|
+
const status = params.error ? "error" : "ok";
|
|
344
|
+
const startedAt = toIsoUtc(params.startTime);
|
|
345
|
+
const endedAt = toIsoUtc(params.startTime + params.responseTime);
|
|
346
|
+
const spanName = params.observability.promptRef?.name ?? params.requestType;
|
|
347
|
+
const childSpans = params.childSpans ?? [];
|
|
348
|
+
let spans: SpanRecord[];
|
|
349
|
+
|
|
350
|
+
if (childSpans.length > 0) {
|
|
351
|
+
const rootId = randomUUID();
|
|
352
|
+
const rootSpan: SpanRecord = {
|
|
353
|
+
durationMs: params.responseTime,
|
|
354
|
+
endedAt,
|
|
355
|
+
id: rootId,
|
|
356
|
+
input: params.prompt,
|
|
357
|
+
kind: "CHAIN",
|
|
358
|
+
name: spanName,
|
|
359
|
+
output: params.response,
|
|
360
|
+
startedAt,
|
|
361
|
+
status,
|
|
362
|
+
usage,
|
|
363
|
+
...(params.error ? {error: params.error} : {}),
|
|
364
|
+
};
|
|
365
|
+
spans = [
|
|
366
|
+
rootSpan,
|
|
367
|
+
...childSpans.map((child) => {
|
|
368
|
+
return {
|
|
369
|
+
...child,
|
|
370
|
+
parentSpanId: child.parentSpanId ?? rootId,
|
|
371
|
+
};
|
|
372
|
+
}),
|
|
373
|
+
];
|
|
374
|
+
} else {
|
|
375
|
+
spans = [
|
|
376
|
+
{
|
|
377
|
+
durationMs: params.responseTime,
|
|
378
|
+
endedAt,
|
|
379
|
+
id: randomUUID(),
|
|
380
|
+
input: params.prompt,
|
|
381
|
+
kind: "LLM",
|
|
382
|
+
name: spanName,
|
|
383
|
+
output: params.response,
|
|
384
|
+
startedAt,
|
|
385
|
+
status,
|
|
386
|
+
usage,
|
|
387
|
+
...(params.error ? {error: params.error} : {}),
|
|
388
|
+
},
|
|
389
|
+
];
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
const promptRef = params.observability.promptRef;
|
|
393
|
+
return {
|
|
394
|
+
endedAt,
|
|
395
|
+
id: randomUUID(),
|
|
396
|
+
input: params.prompt,
|
|
397
|
+
name: spanName,
|
|
398
|
+
output: params.response,
|
|
399
|
+
prompts: promptRef
|
|
400
|
+
? [{label: promptRef.label, name: promptRef.name, version: promptRef.version}]
|
|
401
|
+
: [],
|
|
402
|
+
sensitive: params.observability.sensitive,
|
|
403
|
+
sessionId: params.observability.sessionId,
|
|
404
|
+
spans,
|
|
405
|
+
startedAt,
|
|
406
|
+
status,
|
|
407
|
+
usage,
|
|
408
|
+
userId: params.userId?.toString(),
|
|
409
|
+
...(params.error ? {errorSummary: params.error} : {}),
|
|
410
|
+
};
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
private async logRequestAndTrace(params: {
|
|
414
|
+
error?: string;
|
|
415
|
+
childSpans?: SpanRecord[];
|
|
416
|
+
inputTokens?: number;
|
|
417
|
+
metadata?: Record<string, unknown>;
|
|
418
|
+
observability: ResolvedObservability;
|
|
419
|
+
outputTokens?: number;
|
|
420
|
+
prompt: string;
|
|
421
|
+
requestType: AIRequestType;
|
|
422
|
+
response?: string;
|
|
423
|
+
responseTime: number;
|
|
424
|
+
startTime: number;
|
|
425
|
+
tokensUsed?: number;
|
|
426
|
+
userId?: mongoose.Types.ObjectId;
|
|
427
|
+
}): Promise<void> {
|
|
428
|
+
await this.logRequest({
|
|
429
|
+
aiModel: getModelId(this.model),
|
|
430
|
+
error: params.error,
|
|
431
|
+
metadata: {
|
|
432
|
+
...params.metadata,
|
|
433
|
+
...(params.inputTokens === undefined ? {} : {inputTokens: params.inputTokens}),
|
|
434
|
+
...(params.outputTokens === undefined ? {} : {outputTokens: params.outputTokens}),
|
|
435
|
+
},
|
|
436
|
+
prompt: params.prompt,
|
|
437
|
+
requestType: params.requestType,
|
|
438
|
+
response: params.response,
|
|
439
|
+
responseTime: params.responseTime,
|
|
440
|
+
tokensUsed: params.tokensUsed,
|
|
441
|
+
userId: params.userId,
|
|
442
|
+
});
|
|
443
|
+
|
|
444
|
+
const app = getObservabilityApp();
|
|
445
|
+
if (!app || params.observability.skipTrace) {
|
|
446
|
+
return;
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
const trace = this.buildTraceRecord({
|
|
450
|
+
childSpans: params.childSpans,
|
|
451
|
+
error: params.error,
|
|
452
|
+
inputTokens: params.inputTokens,
|
|
453
|
+
observability: params.observability,
|
|
454
|
+
outputTokens: params.outputTokens,
|
|
455
|
+
prompt: params.prompt,
|
|
456
|
+
requestType: params.requestType,
|
|
457
|
+
response: params.response,
|
|
458
|
+
responseTime: params.responseTime,
|
|
459
|
+
startTime: params.startTime,
|
|
460
|
+
userId: params.userId,
|
|
461
|
+
});
|
|
462
|
+
|
|
463
|
+
const results = await Promise.allSettled(
|
|
464
|
+
app.traceSinks.map((sink) => {
|
|
465
|
+
return sink.export(trace);
|
|
466
|
+
})
|
|
467
|
+
);
|
|
468
|
+
for (const result of results) {
|
|
469
|
+
if (result.status === "rejected") {
|
|
470
|
+
logger.error("Observability TraceSink.export failed", {error: result.reason});
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
async resolveGenerateObservability(
|
|
476
|
+
options: GenerateObservabilityOptions & {systemPrompt?: string}
|
|
477
|
+
): Promise<ResolvedObservability> {
|
|
478
|
+
return this.resolveObservability(options);
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
async recordGenerate(params: {
|
|
482
|
+
childSpans?: SpanRecord[];
|
|
483
|
+
error?: string;
|
|
484
|
+
inputTokens?: number;
|
|
485
|
+
metadata?: Record<string, unknown>;
|
|
486
|
+
observability: ResolvedObservability;
|
|
487
|
+
outputTokens?: number;
|
|
488
|
+
prompt: string;
|
|
489
|
+
requestType: AIRequestType;
|
|
490
|
+
response?: string;
|
|
491
|
+
responseTime: number;
|
|
492
|
+
startTime: number;
|
|
493
|
+
tokensUsed?: number;
|
|
494
|
+
userId?: mongoose.Types.ObjectId;
|
|
495
|
+
}): Promise<void> {
|
|
496
|
+
await this.logRequestAndTrace(params);
|
|
497
|
+
}
|
|
498
|
+
|
|
222
499
|
async generateText(options: GenerateTextOptions): Promise<string> {
|
|
223
|
-
const
|
|
500
|
+
const observability = await this.resolveObservability(options);
|
|
501
|
+
const {prompt, temperature, maxOutputTokens, userId} = options;
|
|
224
502
|
const startTime = DateTime.now().toMillis();
|
|
225
503
|
|
|
226
504
|
try {
|
|
@@ -229,49 +507,165 @@ export class AIService {
|
|
|
229
507
|
maxOutputTokens,
|
|
230
508
|
model: this.model,
|
|
231
509
|
prompt,
|
|
232
|
-
system: systemPrompt,
|
|
510
|
+
system: observability.systemPrompt,
|
|
233
511
|
temperature: temperature ?? this.defaultTemperature,
|
|
234
512
|
});
|
|
235
513
|
|
|
236
514
|
const responseTime = DateTime.now().toMillis() - startTime;
|
|
237
|
-
|
|
238
|
-
|
|
515
|
+
const tokens = readTokenUsage(result.usage);
|
|
516
|
+
await this.logRequestAndTrace({
|
|
517
|
+
inputTokens: tokens.inputTokens,
|
|
518
|
+
observability,
|
|
519
|
+
outputTokens: tokens.outputTokens,
|
|
239
520
|
prompt,
|
|
240
521
|
requestType: "general",
|
|
241
522
|
response: result.text,
|
|
242
523
|
responseTime,
|
|
243
|
-
|
|
524
|
+
startTime,
|
|
525
|
+
tokensUsed: tokens.totalTokens,
|
|
244
526
|
userId,
|
|
245
527
|
});
|
|
246
528
|
|
|
247
529
|
return result.text;
|
|
248
530
|
} catch (error) {
|
|
249
531
|
const responseTime = DateTime.now().toMillis() - startTime;
|
|
250
|
-
await this.
|
|
251
|
-
aiModel: getModelId(this.model),
|
|
532
|
+
await this.logRequestAndTrace({
|
|
252
533
|
error: error instanceof Error ? error.message : String(error),
|
|
534
|
+
observability,
|
|
253
535
|
prompt,
|
|
254
536
|
requestType: "general",
|
|
255
537
|
responseTime,
|
|
538
|
+
startTime,
|
|
256
539
|
userId,
|
|
257
540
|
});
|
|
258
541
|
throw error;
|
|
259
542
|
}
|
|
260
543
|
}
|
|
261
544
|
|
|
545
|
+
/**
|
|
546
|
+
* One block document. Uses deterministic temperature. A failed check is repaired once unless
|
|
547
|
+
* `repair` is false. A second failure throws 422 and logs `metadata.errorCodes`.
|
|
548
|
+
*/
|
|
549
|
+
async generateBlocks(options: {
|
|
550
|
+
prompt: string;
|
|
551
|
+
repair?: boolean;
|
|
552
|
+
systemPrompt?: string;
|
|
553
|
+
userId?: mongoose.Types.ObjectId;
|
|
554
|
+
}): Promise<BlocksDocument> {
|
|
555
|
+
const {prompt, repair = true, systemPrompt, userId} = options;
|
|
556
|
+
const system = systemPrompt ?? TERRENO_UI_BLOCKS_SYSTEM_PROMPT;
|
|
557
|
+
const startTime = DateTime.now().toMillis();
|
|
558
|
+
|
|
559
|
+
const requestDocument = async (
|
|
560
|
+
userPrompt: string
|
|
561
|
+
): Promise<{document?: BlocksDocument; error?: string}> => {
|
|
562
|
+
try {
|
|
563
|
+
const result = await aiGenerateText({
|
|
564
|
+
experimental_telemetry: {functionId: "generate-blocks", isEnabled: true},
|
|
565
|
+
model: this.getModelForStructuredJson(),
|
|
566
|
+
output: Output.object({
|
|
567
|
+
schema: jsonSchema<BlocksDocument>(blocksJsonSchema as never),
|
|
568
|
+
}),
|
|
569
|
+
prompt: userPrompt,
|
|
570
|
+
system,
|
|
571
|
+
temperature: TemperaturePresets.DETERMINISTIC,
|
|
572
|
+
});
|
|
573
|
+
return {document: result.output};
|
|
574
|
+
} catch (error) {
|
|
575
|
+
return {error: error instanceof Error ? error.message : String(error)};
|
|
576
|
+
}
|
|
577
|
+
};
|
|
578
|
+
|
|
579
|
+
const finish = async ({
|
|
580
|
+
error,
|
|
581
|
+
errorCodes,
|
|
582
|
+
response,
|
|
583
|
+
}: {
|
|
584
|
+
error?: string;
|
|
585
|
+
errorCodes?: string[];
|
|
586
|
+
response?: string;
|
|
587
|
+
}): Promise<void> => {
|
|
588
|
+
await this.logRequest({
|
|
589
|
+
aiModel: getModelId(this.model),
|
|
590
|
+
error,
|
|
591
|
+
metadata: errorCodes ? {errorCodes} : undefined,
|
|
592
|
+
prompt,
|
|
593
|
+
requestType: "ui_blocks",
|
|
594
|
+
response,
|
|
595
|
+
responseTime: DateTime.now().toMillis() - startTime,
|
|
596
|
+
userId,
|
|
597
|
+
});
|
|
598
|
+
};
|
|
599
|
+
|
|
600
|
+
const first = await requestDocument(prompt);
|
|
601
|
+
if (!first.document) {
|
|
602
|
+
await finish({error: first.error ?? "Block generation failed"});
|
|
603
|
+
throw new APIError({status: 502, title: "Block generation failed"});
|
|
604
|
+
}
|
|
605
|
+
const firstCheck = validateBlocks(first.document);
|
|
606
|
+
if (first.document && firstCheck?.ok) {
|
|
607
|
+
await finish({response: JSON.stringify(first.document)});
|
|
608
|
+
return first.document;
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
const firstCodes =
|
|
612
|
+
firstCheck && !firstCheck.ok ? firstCheck.errors.map((error) => error.code) : [];
|
|
613
|
+
const firstDetail =
|
|
614
|
+
firstCheck && !firstCheck.ok
|
|
615
|
+
? firstCheck.errors
|
|
616
|
+
.map((error) => `${error.path} ${error.code} ${error.message}`)
|
|
617
|
+
.join("\n")
|
|
618
|
+
: first.error;
|
|
619
|
+
if (!repair) {
|
|
620
|
+
await finish({
|
|
621
|
+
error: firstDetail ?? "Block document failed validation",
|
|
622
|
+
errorCodes: firstCodes,
|
|
623
|
+
});
|
|
624
|
+
throw new APIError({
|
|
625
|
+
meta: {fields: Object.fromEntries(firstCodes.map((code) => [code, code]))},
|
|
626
|
+
status: 422,
|
|
627
|
+
title: "Block document failed validation",
|
|
628
|
+
});
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
const second = await requestDocument(
|
|
632
|
+
`${prompt}\n\nErrors:\n${firstDetail ?? "invalid document"}`
|
|
633
|
+
);
|
|
634
|
+
if (!second.document) {
|
|
635
|
+
await finish({error: second.error ?? "Block generation failed"});
|
|
636
|
+
throw new APIError({status: 502, title: "Block generation failed"});
|
|
637
|
+
}
|
|
638
|
+
const secondCheck = validateBlocks(second.document);
|
|
639
|
+
if (second.document && secondCheck?.ok) {
|
|
640
|
+
await finish({response: JSON.stringify(second.document)});
|
|
641
|
+
return second.document;
|
|
642
|
+
}
|
|
643
|
+
const errorCodes =
|
|
644
|
+
secondCheck && !secondCheck.ok ? secondCheck.errors.map((error) => error.code) : firstCodes;
|
|
645
|
+
await finish({
|
|
646
|
+
error: "Block document failed validation",
|
|
647
|
+
errorCodes,
|
|
648
|
+
});
|
|
649
|
+
throw new APIError({
|
|
650
|
+
meta: {
|
|
651
|
+
fields: Object.fromEntries(
|
|
652
|
+
(errorCodes.length > 0 ? errorCodes : ["invalid"]).map((code) => [code, code])
|
|
653
|
+
),
|
|
654
|
+
},
|
|
655
|
+
status: 422,
|
|
656
|
+
title: "Block document failed validation",
|
|
657
|
+
});
|
|
658
|
+
}
|
|
659
|
+
|
|
262
660
|
/** Any JSON value (object, array, primitive, or null) via the AI SDK `Output.json()` parser. */
|
|
263
661
|
async generateJsonValue(options: GenerateJsonValueOptions): Promise<JSONValue> {
|
|
264
|
-
const {
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
systemPrompt,
|
|
270
|
-
temperature,
|
|
271
|
-
userId,
|
|
272
|
-
} = options;
|
|
662
|
+
const observability = await this.resolveObservability({
|
|
663
|
+
...options,
|
|
664
|
+
systemPrompt: options.systemPrompt ?? JSON_VALUE_SYSTEM_PROMPT,
|
|
665
|
+
});
|
|
666
|
+
const {maxOutputTokens, outputDescription, outputName, prompt, temperature, userId} = options;
|
|
273
667
|
const startTime = DateTime.now().toMillis();
|
|
274
|
-
const system = systemPrompt ?? JSON_VALUE_SYSTEM_PROMPT;
|
|
668
|
+
const system = observability.systemPrompt ?? JSON_VALUE_SYSTEM_PROMPT;
|
|
275
669
|
|
|
276
670
|
try {
|
|
277
671
|
const result = await aiGenerateText({
|
|
@@ -285,13 +679,17 @@ export class AIService {
|
|
|
285
679
|
});
|
|
286
680
|
|
|
287
681
|
const responseTime = DateTime.now().toMillis() - startTime;
|
|
288
|
-
|
|
289
|
-
|
|
682
|
+
const tokens = readTokenUsage(result.usage);
|
|
683
|
+
await this.logRequestAndTrace({
|
|
684
|
+
inputTokens: tokens.inputTokens,
|
|
685
|
+
observability,
|
|
686
|
+
outputTokens: tokens.outputTokens,
|
|
290
687
|
prompt,
|
|
291
688
|
requestType: "json_value",
|
|
292
689
|
response: JSON.stringify(result.output),
|
|
293
690
|
responseTime,
|
|
294
|
-
|
|
691
|
+
startTime,
|
|
692
|
+
tokensUsed: tokens.totalTokens,
|
|
295
693
|
userId,
|
|
296
694
|
});
|
|
297
695
|
|
|
@@ -300,9 +698,11 @@ export class AIService {
|
|
|
300
698
|
const responseTime = DateTime.now().toMillis() - startTime;
|
|
301
699
|
await this.logStructuredJsonFailure({
|
|
302
700
|
error,
|
|
701
|
+
observability,
|
|
303
702
|
prompt,
|
|
304
703
|
requestType: "json_value",
|
|
305
704
|
responseTime,
|
|
705
|
+
startTime,
|
|
306
706
|
system,
|
|
307
707
|
userId,
|
|
308
708
|
});
|
|
@@ -312,18 +712,14 @@ export class AIService {
|
|
|
312
712
|
|
|
313
713
|
/** Typed object from a Zod schema, `jsonSchema(...)`, or other `FlexibleSchema` (`Output.object()`). */
|
|
314
714
|
async generateJsonObject<OBJECT>(options: GenerateJsonObjectOptions<OBJECT>): Promise<OBJECT> {
|
|
315
|
-
const {
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
systemPrompt,
|
|
322
|
-
temperature,
|
|
323
|
-
userId,
|
|
324
|
-
} = options;
|
|
715
|
+
const observability = await this.resolveObservability({
|
|
716
|
+
...options,
|
|
717
|
+
systemPrompt: options.systemPrompt ?? JSON_VALUE_SYSTEM_PROMPT,
|
|
718
|
+
});
|
|
719
|
+
const {maxOutputTokens, prompt, schema, schemaDescription, schemaName, temperature, userId} =
|
|
720
|
+
options;
|
|
325
721
|
const startTime = DateTime.now().toMillis();
|
|
326
|
-
const system = systemPrompt ?? JSON_VALUE_SYSTEM_PROMPT;
|
|
722
|
+
const system = observability.systemPrompt ?? JSON_VALUE_SYSTEM_PROMPT;
|
|
327
723
|
|
|
328
724
|
try {
|
|
329
725
|
const result = await aiGenerateText({
|
|
@@ -341,13 +737,17 @@ export class AIService {
|
|
|
341
737
|
});
|
|
342
738
|
|
|
343
739
|
const responseTime = DateTime.now().toMillis() - startTime;
|
|
344
|
-
|
|
345
|
-
|
|
740
|
+
const tokens = readTokenUsage(result.usage);
|
|
741
|
+
await this.logRequestAndTrace({
|
|
742
|
+
inputTokens: tokens.inputTokens,
|
|
743
|
+
observability,
|
|
744
|
+
outputTokens: tokens.outputTokens,
|
|
346
745
|
prompt,
|
|
347
746
|
requestType: "json_object",
|
|
348
747
|
response: JSON.stringify(result.output),
|
|
349
748
|
responseTime,
|
|
350
|
-
|
|
749
|
+
startTime,
|
|
750
|
+
tokensUsed: tokens.totalTokens,
|
|
351
751
|
userId,
|
|
352
752
|
});
|
|
353
753
|
|
|
@@ -356,9 +756,11 @@ export class AIService {
|
|
|
356
756
|
const responseTime = DateTime.now().toMillis() - startTime;
|
|
357
757
|
await this.logStructuredJsonFailure({
|
|
358
758
|
error,
|
|
759
|
+
observability,
|
|
359
760
|
prompt,
|
|
360
761
|
requestType: "json_object",
|
|
361
762
|
responseTime,
|
|
763
|
+
startTime,
|
|
362
764
|
system,
|
|
363
765
|
userId,
|
|
364
766
|
});
|
|
@@ -373,18 +775,14 @@ export class AIService {
|
|
|
373
775
|
async generateJsonArray<ELEMENT>(
|
|
374
776
|
options: GenerateJsonArrayOptions<ELEMENT>
|
|
375
777
|
): Promise<Array<ELEMENT>> {
|
|
376
|
-
const {
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
systemPrompt,
|
|
383
|
-
temperature,
|
|
384
|
-
userId,
|
|
385
|
-
} = options;
|
|
778
|
+
const observability = await this.resolveObservability({
|
|
779
|
+
...options,
|
|
780
|
+
systemPrompt: options.systemPrompt ?? JSON_VALUE_SYSTEM_PROMPT,
|
|
781
|
+
});
|
|
782
|
+
const {element, maxOutputTokens, outputDescription, outputName, prompt, temperature, userId} =
|
|
783
|
+
options;
|
|
386
784
|
const startTime = DateTime.now().toMillis();
|
|
387
|
-
const system = systemPrompt ?? JSON_VALUE_SYSTEM_PROMPT;
|
|
785
|
+
const system = observability.systemPrompt ?? JSON_VALUE_SYSTEM_PROMPT;
|
|
388
786
|
|
|
389
787
|
try {
|
|
390
788
|
const result = await aiGenerateText({
|
|
@@ -402,13 +800,17 @@ export class AIService {
|
|
|
402
800
|
});
|
|
403
801
|
|
|
404
802
|
const responseTime = DateTime.now().toMillis() - startTime;
|
|
405
|
-
|
|
406
|
-
|
|
803
|
+
const tokens = readTokenUsage(result.usage);
|
|
804
|
+
await this.logRequestAndTrace({
|
|
805
|
+
inputTokens: tokens.inputTokens,
|
|
806
|
+
observability,
|
|
807
|
+
outputTokens: tokens.outputTokens,
|
|
407
808
|
prompt,
|
|
408
809
|
requestType: "json_array",
|
|
409
810
|
response: JSON.stringify(result.output),
|
|
410
811
|
responseTime,
|
|
411
|
-
|
|
812
|
+
startTime,
|
|
813
|
+
tokensUsed: tokens.totalTokens,
|
|
412
814
|
userId,
|
|
413
815
|
});
|
|
414
816
|
|
|
@@ -417,9 +819,11 @@ export class AIService {
|
|
|
417
819
|
const responseTime = DateTime.now().toMillis() - startTime;
|
|
418
820
|
await this.logStructuredJsonFailure({
|
|
419
821
|
error,
|
|
822
|
+
observability,
|
|
420
823
|
prompt,
|
|
421
824
|
requestType: "json_array",
|
|
422
825
|
responseTime,
|
|
826
|
+
startTime,
|
|
423
827
|
system,
|
|
424
828
|
userId,
|
|
425
829
|
});
|
|
@@ -428,7 +832,8 @@ export class AIService {
|
|
|
428
832
|
}
|
|
429
833
|
|
|
430
834
|
async *generateTextStream(options: GenerateStreamOptions): AsyncGenerator<string> {
|
|
431
|
-
const
|
|
835
|
+
const observability = await this.resolveObservability(options);
|
|
836
|
+
const {prompt, temperature, maxOutputTokens, userId} = options;
|
|
432
837
|
const startTime = DateTime.now().toMillis();
|
|
433
838
|
let fullResponse = "";
|
|
434
839
|
|
|
@@ -438,7 +843,7 @@ export class AIService {
|
|
|
438
843
|
maxOutputTokens,
|
|
439
844
|
model: this.model,
|
|
440
845
|
prompt,
|
|
441
|
-
system: systemPrompt,
|
|
846
|
+
system: observability.systemPrompt,
|
|
442
847
|
temperature: temperature ?? this.defaultTemperature,
|
|
443
848
|
});
|
|
444
849
|
|
|
@@ -449,23 +854,28 @@ export class AIService {
|
|
|
449
854
|
|
|
450
855
|
const responseTime = DateTime.now().toMillis() - startTime;
|
|
451
856
|
const usage = await result.usage;
|
|
452
|
-
|
|
453
|
-
|
|
857
|
+
const tokens = readTokenUsage(usage);
|
|
858
|
+
await this.logRequestAndTrace({
|
|
859
|
+
inputTokens: tokens.inputTokens,
|
|
860
|
+
observability,
|
|
861
|
+
outputTokens: tokens.outputTokens,
|
|
454
862
|
prompt,
|
|
455
863
|
requestType: "general",
|
|
456
864
|
response: fullResponse,
|
|
457
865
|
responseTime,
|
|
458
|
-
|
|
866
|
+
startTime,
|
|
867
|
+
tokensUsed: tokens.totalTokens,
|
|
459
868
|
userId,
|
|
460
869
|
});
|
|
461
870
|
} catch (error) {
|
|
462
871
|
const responseTime = DateTime.now().toMillis() - startTime;
|
|
463
|
-
await this.
|
|
464
|
-
aiModel: getModelId(this.model),
|
|
872
|
+
await this.logRequestAndTrace({
|
|
465
873
|
error: error instanceof Error ? error.message : String(error),
|
|
874
|
+
observability,
|
|
466
875
|
prompt,
|
|
467
876
|
requestType: "general",
|
|
468
877
|
responseTime,
|
|
878
|
+
startTime,
|
|
469
879
|
userId,
|
|
470
880
|
});
|
|
471
881
|
throw error;
|
|
@@ -474,6 +884,7 @@ export class AIService {
|
|
|
474
884
|
|
|
475
885
|
async generateRemix(options: RemixOptions): Promise<string> {
|
|
476
886
|
return this.generateText({
|
|
887
|
+
...this.observabilityFields(options),
|
|
477
888
|
prompt: options.text,
|
|
478
889
|
systemPrompt: REMIX_PROMPT,
|
|
479
890
|
temperature: TemperaturePresets.BALANCED,
|
|
@@ -483,6 +894,7 @@ export class AIService {
|
|
|
483
894
|
|
|
484
895
|
async generateSummary(options: SummaryOptions): Promise<string> {
|
|
485
896
|
return this.generateText({
|
|
897
|
+
...this.observabilityFields(options),
|
|
486
898
|
prompt: options.text,
|
|
487
899
|
systemPrompt: CONTENT_SUMMARY_PROMPT,
|
|
488
900
|
temperature: TemperaturePresets.LOW,
|
|
@@ -498,6 +910,7 @@ export class AIService {
|
|
|
498
910
|
);
|
|
499
911
|
|
|
500
912
|
return this.generateText({
|
|
913
|
+
...this.observabilityFields(options),
|
|
501
914
|
prompt: text,
|
|
502
915
|
systemPrompt,
|
|
503
916
|
temperature: TemperaturePresets.LOW,
|
|
@@ -505,10 +918,45 @@ export class AIService {
|
|
|
505
918
|
});
|
|
506
919
|
}
|
|
507
920
|
|
|
921
|
+
/**
|
|
922
|
+
* Converts history rows to model messages. Ask call/result pairs are kept so the model sees what
|
|
923
|
+
* it asked and how the user answered; other tool rows, approval asks, and asks still waiting for
|
|
924
|
+
* an answer are skipped. Consecutive ask calls came from one step and share one assistant message.
|
|
925
|
+
*/
|
|
508
926
|
buildMessages(prompts: GptHistoryPrompt[]): ModelMessage[] {
|
|
509
927
|
const messages: ModelMessage[] = [];
|
|
928
|
+
const askResults = collectAskResults(prompts);
|
|
929
|
+
let askStep: {calls: ToolCallPart[]; results: ToolResultPart[]} | undefined;
|
|
510
930
|
|
|
511
931
|
for (const prompt of prompts) {
|
|
932
|
+
if (isModelAskCall(prompt)) {
|
|
933
|
+
const askResult = prompt.toolCallId ? askResults.get(prompt.toolCallId) : undefined;
|
|
934
|
+
if (!askResult || !prompt.toolCallId || !prompt.toolName) {
|
|
935
|
+
continue;
|
|
936
|
+
}
|
|
937
|
+
if (!askStep) {
|
|
938
|
+
askStep = {calls: [], results: []};
|
|
939
|
+
messages.push(
|
|
940
|
+
{content: askStep.calls, role: "assistant"},
|
|
941
|
+
{content: askStep.results, role: "tool"}
|
|
942
|
+
);
|
|
943
|
+
}
|
|
944
|
+
askStep.calls.push({
|
|
945
|
+
input: prompt.args ?? {},
|
|
946
|
+
toolCallId: prompt.toolCallId,
|
|
947
|
+
toolName: prompt.toolName,
|
|
948
|
+
type: "tool-call",
|
|
949
|
+
});
|
|
950
|
+
askStep.results.push({
|
|
951
|
+
output: {type: "json", value: (askResult.result ?? null) as JSONValue},
|
|
952
|
+
toolCallId: prompt.toolCallId,
|
|
953
|
+
toolName: prompt.toolName,
|
|
954
|
+
type: "tool-result",
|
|
955
|
+
});
|
|
956
|
+
continue;
|
|
957
|
+
}
|
|
958
|
+
askStep = undefined;
|
|
959
|
+
|
|
512
960
|
if (prompt.type === "tool-call" || prompt.type === "tool-result") {
|
|
513
961
|
continue;
|
|
514
962
|
}
|
|
@@ -567,7 +1015,11 @@ export class AIService {
|
|
|
567
1015
|
}
|
|
568
1016
|
|
|
569
1017
|
async *generateChatStream(options: GenerateChatStreamOptions): AsyncGenerator<string> {
|
|
570
|
-
const
|
|
1018
|
+
const observability = await this.resolveObservability({
|
|
1019
|
+
...options,
|
|
1020
|
+
systemPrompt: options.systemPrompt ?? DEFAULT_GPT_MEMORY,
|
|
1021
|
+
});
|
|
1022
|
+
const {messages, tools, toolChoice, stopWhen, userId} = options;
|
|
571
1023
|
const startTime = DateTime.now().toMillis();
|
|
572
1024
|
let fullResponse = "";
|
|
573
1025
|
|
|
@@ -579,7 +1031,7 @@ export class AIService {
|
|
|
579
1031
|
messages: messages.map((m) => ({content: m.content, role: m.role})),
|
|
580
1032
|
model: this.model,
|
|
581
1033
|
stopWhen: stopWhen ?? stepCountIs(1),
|
|
582
|
-
system: systemPrompt ?? DEFAULT_GPT_MEMORY,
|
|
1034
|
+
system: observability.systemPrompt ?? DEFAULT_GPT_MEMORY,
|
|
583
1035
|
temperature: this.defaultTemperature,
|
|
584
1036
|
toolChoice,
|
|
585
1037
|
tools,
|
|
@@ -592,23 +1044,28 @@ export class AIService {
|
|
|
592
1044
|
|
|
593
1045
|
const responseTime = DateTime.now().toMillis() - startTime;
|
|
594
1046
|
const usage = await result.usage;
|
|
595
|
-
|
|
596
|
-
|
|
1047
|
+
const tokens = readTokenUsage(usage);
|
|
1048
|
+
await this.logRequestAndTrace({
|
|
1049
|
+
inputTokens: tokens.inputTokens,
|
|
1050
|
+
observability,
|
|
1051
|
+
outputTokens: tokens.outputTokens,
|
|
597
1052
|
prompt: promptText,
|
|
598
1053
|
requestType: "general",
|
|
599
1054
|
response: fullResponse,
|
|
600
1055
|
responseTime,
|
|
601
|
-
|
|
1056
|
+
startTime,
|
|
1057
|
+
tokensUsed: tokens.totalTokens,
|
|
602
1058
|
userId,
|
|
603
1059
|
});
|
|
604
1060
|
} catch (error) {
|
|
605
1061
|
const responseTime = DateTime.now().toMillis() - startTime;
|
|
606
|
-
await this.
|
|
607
|
-
aiModel: getModelId(this.model),
|
|
1062
|
+
await this.logRequestAndTrace({
|
|
608
1063
|
error: error instanceof Error ? error.message : String(error),
|
|
1064
|
+
observability,
|
|
609
1065
|
prompt: promptText,
|
|
610
1066
|
requestType: "general",
|
|
611
1067
|
responseTime,
|
|
1068
|
+
startTime,
|
|
612
1069
|
userId,
|
|
613
1070
|
});
|
|
614
1071
|
throw error;
|