@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
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.AIService = exports.TemperaturePresets = void 0;
|
|
4
|
+
const node_crypto_1 = require("node:crypto");
|
|
4
5
|
const api_1 = require("@terreno/api");
|
|
6
|
+
const blocks_1 = require("@terreno/blocks");
|
|
5
7
|
const ai_1 = require("ai");
|
|
6
8
|
const luxon_1 = require("luxon");
|
|
7
9
|
const aiRequest_1 = require("../models/aiRequest");
|
|
8
|
-
const
|
|
10
|
+
const observabilityAppRegistry_1 = require("../observability/observabilityAppRegistry");
|
|
11
|
+
const jsonFenceModel_1 = require("./jsonFenceModel");
|
|
9
12
|
const prompts_1 = require("./prompts");
|
|
10
13
|
exports.TemperaturePresets = {
|
|
11
14
|
BALANCED: 0.7,
|
|
@@ -16,44 +19,20 @@ exports.TemperaturePresets = {
|
|
|
16
19
|
MAXIMUM: 2.0,
|
|
17
20
|
};
|
|
18
21
|
/**
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
* before Vercel `Output.*` parsing.
|
|
22
|
+
* True for the call row of an ask the model made. Approval asks are display-only: the model did
|
|
23
|
+
* not call a tool by that name with those arguments, so they are not replayed.
|
|
22
24
|
*/
|
|
23
|
-
const
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
const boundGenerate = original;
|
|
35
|
-
return async (options) => {
|
|
36
|
-
const result = await Promise.resolve(boundGenerate.call(target, options));
|
|
37
|
-
if (!result?.content || !Array.isArray(result.content)) {
|
|
38
|
-
return result;
|
|
39
|
-
}
|
|
40
|
-
return {
|
|
41
|
-
...result,
|
|
42
|
-
content: result.content.map((part) => {
|
|
43
|
-
if (part.type !== "text" || typeof part.text !== "string") {
|
|
44
|
-
return part;
|
|
45
|
-
}
|
|
46
|
-
return {
|
|
47
|
-
...part,
|
|
48
|
-
text: (0, parseAiJson_1.normalizeLlmJsonTextForStructuredOutput)(part.text),
|
|
49
|
-
};
|
|
50
|
-
}),
|
|
51
|
-
};
|
|
52
|
-
};
|
|
53
|
-
}
|
|
54
|
-
return Reflect.get(target, prop, receiver);
|
|
55
|
-
},
|
|
56
|
-
});
|
|
25
|
+
const isModelAskCall = (prompt) => prompt.type === "tool-call" && prompt.ask !== undefined && prompt.ask.origin !== "approval";
|
|
26
|
+
/** Result rows of the model's ask tool calls, by tool call id. */
|
|
27
|
+
const collectAskResults = (prompts) => {
|
|
28
|
+
const askCallIds = new Set(prompts
|
|
29
|
+
.filter((prompt) => isModelAskCall(prompt) && prompt.toolCallId)
|
|
30
|
+
.map((prompt) => prompt.toolCallId));
|
|
31
|
+
return new Map(prompts
|
|
32
|
+
.filter((prompt) => prompt.type === "tool-result" &&
|
|
33
|
+
prompt.toolCallId !== undefined &&
|
|
34
|
+
askCallIds.has(prompt.toolCallId))
|
|
35
|
+
.map((prompt) => [prompt.toolCallId, prompt]));
|
|
57
36
|
};
|
|
58
37
|
const getModelId = (model) => {
|
|
59
38
|
if (typeof model === "string") {
|
|
@@ -61,6 +40,27 @@ const getModelId = (model) => {
|
|
|
61
40
|
}
|
|
62
41
|
return model.modelId ?? "unknown";
|
|
63
42
|
};
|
|
43
|
+
const toIsoUtc = (millis) => {
|
|
44
|
+
return luxon_1.DateTime.fromMillis(millis, { zone: "utc" }).toISO() ?? "";
|
|
45
|
+
};
|
|
46
|
+
const readTokenUsage = (usage) => {
|
|
47
|
+
return {
|
|
48
|
+
inputTokens: usage?.inputTokens ?? usage?.promptTokens,
|
|
49
|
+
outputTokens: usage?.outputTokens ?? usage?.completionTokens,
|
|
50
|
+
totalTokens: usage?.totalTokens,
|
|
51
|
+
};
|
|
52
|
+
};
|
|
53
|
+
const computeCostUsd = (params) => {
|
|
54
|
+
if (!params.priceMap) {
|
|
55
|
+
return undefined;
|
|
56
|
+
}
|
|
57
|
+
const price = params.priceMap[params.modelId];
|
|
58
|
+
if (!price || params.inputTokens === undefined || params.outputTokens === undefined) {
|
|
59
|
+
return undefined;
|
|
60
|
+
}
|
|
61
|
+
return ((price.inputPerMTok * params.inputTokens + price.outputPerMTok * params.outputTokens) /
|
|
62
|
+
1_000_000);
|
|
63
|
+
};
|
|
64
64
|
class AIService {
|
|
65
65
|
model;
|
|
66
66
|
defaultTemperature;
|
|
@@ -74,7 +74,7 @@ class AIService {
|
|
|
74
74
|
}
|
|
75
75
|
getModelForStructuredJson() {
|
|
76
76
|
if (!this.structuredJsonModel) {
|
|
77
|
-
this.structuredJsonModel = withStrippedJsonFencesModel(this.model);
|
|
77
|
+
this.structuredJsonModel = (0, jsonFenceModel_1.withStrippedJsonFencesModel)(this.model);
|
|
78
78
|
}
|
|
79
79
|
return this.structuredJsonModel;
|
|
80
80
|
}
|
|
@@ -127,8 +127,7 @@ class AIService {
|
|
|
127
127
|
response: rawText ?? "",
|
|
128
128
|
system: params.system,
|
|
129
129
|
});
|
|
130
|
-
await this.
|
|
131
|
-
aiModel: getModelId(this.model),
|
|
130
|
+
await this.logRequestAndTrace({
|
|
132
131
|
error: errorDescription,
|
|
133
132
|
metadata: {
|
|
134
133
|
errorStack,
|
|
@@ -136,10 +135,12 @@ class AIService {
|
|
|
136
135
|
rawModelTextCaptured: Boolean(rawText && rawText.length > 0),
|
|
137
136
|
system: params.system,
|
|
138
137
|
},
|
|
138
|
+
observability: params.observability,
|
|
139
139
|
prompt: params.prompt,
|
|
140
140
|
requestType: params.requestType,
|
|
141
141
|
response: responseForLog,
|
|
142
142
|
responseTime: params.responseTime,
|
|
143
|
+
startTime: params.startTime,
|
|
143
144
|
userId: params.userId,
|
|
144
145
|
});
|
|
145
146
|
}
|
|
@@ -151,8 +152,184 @@ class AIService {
|
|
|
151
152
|
// Logging failures should not break the main flow
|
|
152
153
|
}
|
|
153
154
|
}
|
|
155
|
+
async resolveObservability(options) {
|
|
156
|
+
let promptRef;
|
|
157
|
+
let systemPrompt = options.systemPrompt;
|
|
158
|
+
let sensitive = options.sensitive ?? false;
|
|
159
|
+
if (options.promptName) {
|
|
160
|
+
const registry = (0, observabilityAppRegistry_1.getObservabilityApp)()?.promptRegistry;
|
|
161
|
+
if (!registry) {
|
|
162
|
+
throw new api_1.APIError({ status: 400, title: "Prompt registry is not configured" });
|
|
163
|
+
}
|
|
164
|
+
const label = options.promptLabel ?? "production";
|
|
165
|
+
const version = await registry.get({ label, name: options.promptName });
|
|
166
|
+
if (!version) {
|
|
167
|
+
throw new api_1.APIError({
|
|
168
|
+
status: 400,
|
|
169
|
+
title: `Unknown prompt "${options.promptName}" with label "${label}"`,
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
promptRef = version;
|
|
173
|
+
systemPrompt = version.body;
|
|
174
|
+
if (options.sensitive === undefined) {
|
|
175
|
+
sensitive = Boolean(version.sensitive);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
return {
|
|
179
|
+
priceMap: options.priceMap,
|
|
180
|
+
promptRef,
|
|
181
|
+
sensitive,
|
|
182
|
+
sessionId: options.sessionId,
|
|
183
|
+
skipTrace: options.skipTrace === true,
|
|
184
|
+
systemPrompt,
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
observabilityFields(options) {
|
|
188
|
+
return {
|
|
189
|
+
priceMap: options.priceMap,
|
|
190
|
+
promptLabel: options.promptLabel,
|
|
191
|
+
promptName: options.promptName,
|
|
192
|
+
sensitive: options.sensitive,
|
|
193
|
+
sessionId: options.sessionId,
|
|
194
|
+
skipTrace: options.skipTrace,
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
buildTraceRecord(params) {
|
|
198
|
+
const modelId = getModelId(this.model);
|
|
199
|
+
const priceMap = params.observability.priceMap;
|
|
200
|
+
const app = (0, observabilityAppRegistry_1.getObservabilityApp)();
|
|
201
|
+
const effectivePriceMap = priceMap ?? app?.priceMap;
|
|
202
|
+
const costUsd = computeCostUsd({
|
|
203
|
+
inputTokens: params.inputTokens,
|
|
204
|
+
modelId,
|
|
205
|
+
outputTokens: params.outputTokens,
|
|
206
|
+
priceMap: effectivePriceMap,
|
|
207
|
+
});
|
|
208
|
+
const usage = {
|
|
209
|
+
inputTokens: params.inputTokens,
|
|
210
|
+
model: modelId,
|
|
211
|
+
outputTokens: params.outputTokens,
|
|
212
|
+
...(costUsd === undefined ? {} : { costUsd }),
|
|
213
|
+
};
|
|
214
|
+
const status = params.error ? "error" : "ok";
|
|
215
|
+
const startedAt = toIsoUtc(params.startTime);
|
|
216
|
+
const endedAt = toIsoUtc(params.startTime + params.responseTime);
|
|
217
|
+
const spanName = params.observability.promptRef?.name ?? params.requestType;
|
|
218
|
+
const childSpans = params.childSpans ?? [];
|
|
219
|
+
let spans;
|
|
220
|
+
if (childSpans.length > 0) {
|
|
221
|
+
const rootId = (0, node_crypto_1.randomUUID)();
|
|
222
|
+
const rootSpan = {
|
|
223
|
+
durationMs: params.responseTime,
|
|
224
|
+
endedAt,
|
|
225
|
+
id: rootId,
|
|
226
|
+
input: params.prompt,
|
|
227
|
+
kind: "CHAIN",
|
|
228
|
+
name: spanName,
|
|
229
|
+
output: params.response,
|
|
230
|
+
startedAt,
|
|
231
|
+
status,
|
|
232
|
+
usage,
|
|
233
|
+
...(params.error ? { error: params.error } : {}),
|
|
234
|
+
};
|
|
235
|
+
spans = [
|
|
236
|
+
rootSpan,
|
|
237
|
+
...childSpans.map((child) => {
|
|
238
|
+
return {
|
|
239
|
+
...child,
|
|
240
|
+
parentSpanId: child.parentSpanId ?? rootId,
|
|
241
|
+
};
|
|
242
|
+
}),
|
|
243
|
+
];
|
|
244
|
+
}
|
|
245
|
+
else {
|
|
246
|
+
spans = [
|
|
247
|
+
{
|
|
248
|
+
durationMs: params.responseTime,
|
|
249
|
+
endedAt,
|
|
250
|
+
id: (0, node_crypto_1.randomUUID)(),
|
|
251
|
+
input: params.prompt,
|
|
252
|
+
kind: "LLM",
|
|
253
|
+
name: spanName,
|
|
254
|
+
output: params.response,
|
|
255
|
+
startedAt,
|
|
256
|
+
status,
|
|
257
|
+
usage,
|
|
258
|
+
...(params.error ? { error: params.error } : {}),
|
|
259
|
+
},
|
|
260
|
+
];
|
|
261
|
+
}
|
|
262
|
+
const promptRef = params.observability.promptRef;
|
|
263
|
+
return {
|
|
264
|
+
endedAt,
|
|
265
|
+
id: (0, node_crypto_1.randomUUID)(),
|
|
266
|
+
input: params.prompt,
|
|
267
|
+
name: spanName,
|
|
268
|
+
output: params.response,
|
|
269
|
+
prompts: promptRef
|
|
270
|
+
? [{ label: promptRef.label, name: promptRef.name, version: promptRef.version }]
|
|
271
|
+
: [],
|
|
272
|
+
sensitive: params.observability.sensitive,
|
|
273
|
+
sessionId: params.observability.sessionId,
|
|
274
|
+
spans,
|
|
275
|
+
startedAt,
|
|
276
|
+
status,
|
|
277
|
+
usage,
|
|
278
|
+
userId: params.userId?.toString(),
|
|
279
|
+
...(params.error ? { errorSummary: params.error } : {}),
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
async logRequestAndTrace(params) {
|
|
283
|
+
await this.logRequest({
|
|
284
|
+
aiModel: getModelId(this.model),
|
|
285
|
+
error: params.error,
|
|
286
|
+
metadata: {
|
|
287
|
+
...params.metadata,
|
|
288
|
+
...(params.inputTokens === undefined ? {} : { inputTokens: params.inputTokens }),
|
|
289
|
+
...(params.outputTokens === undefined ? {} : { outputTokens: params.outputTokens }),
|
|
290
|
+
},
|
|
291
|
+
prompt: params.prompt,
|
|
292
|
+
requestType: params.requestType,
|
|
293
|
+
response: params.response,
|
|
294
|
+
responseTime: params.responseTime,
|
|
295
|
+
tokensUsed: params.tokensUsed,
|
|
296
|
+
userId: params.userId,
|
|
297
|
+
});
|
|
298
|
+
const app = (0, observabilityAppRegistry_1.getObservabilityApp)();
|
|
299
|
+
if (!app || params.observability.skipTrace) {
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
302
|
+
const trace = this.buildTraceRecord({
|
|
303
|
+
childSpans: params.childSpans,
|
|
304
|
+
error: params.error,
|
|
305
|
+
inputTokens: params.inputTokens,
|
|
306
|
+
observability: params.observability,
|
|
307
|
+
outputTokens: params.outputTokens,
|
|
308
|
+
prompt: params.prompt,
|
|
309
|
+
requestType: params.requestType,
|
|
310
|
+
response: params.response,
|
|
311
|
+
responseTime: params.responseTime,
|
|
312
|
+
startTime: params.startTime,
|
|
313
|
+
userId: params.userId,
|
|
314
|
+
});
|
|
315
|
+
const results = await Promise.allSettled(app.traceSinks.map((sink) => {
|
|
316
|
+
return sink.export(trace);
|
|
317
|
+
}));
|
|
318
|
+
for (const result of results) {
|
|
319
|
+
if (result.status === "rejected") {
|
|
320
|
+
api_1.logger.error("Observability TraceSink.export failed", { error: result.reason });
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
async resolveGenerateObservability(options) {
|
|
325
|
+
return this.resolveObservability(options);
|
|
326
|
+
}
|
|
327
|
+
async recordGenerate(params) {
|
|
328
|
+
await this.logRequestAndTrace(params);
|
|
329
|
+
}
|
|
154
330
|
async generateText(options) {
|
|
155
|
-
const
|
|
331
|
+
const observability = await this.resolveObservability(options);
|
|
332
|
+
const { prompt, temperature, maxOutputTokens, userId } = options;
|
|
156
333
|
const startTime = luxon_1.DateTime.now().toMillis();
|
|
157
334
|
try {
|
|
158
335
|
const result = await (0, ai_1.generateText)({
|
|
@@ -160,39 +337,136 @@ class AIService {
|
|
|
160
337
|
maxOutputTokens,
|
|
161
338
|
model: this.model,
|
|
162
339
|
prompt,
|
|
163
|
-
system: systemPrompt,
|
|
340
|
+
system: observability.systemPrompt,
|
|
164
341
|
temperature: temperature ?? this.defaultTemperature,
|
|
165
342
|
});
|
|
166
343
|
const responseTime = luxon_1.DateTime.now().toMillis() - startTime;
|
|
167
|
-
|
|
168
|
-
|
|
344
|
+
const tokens = readTokenUsage(result.usage);
|
|
345
|
+
await this.logRequestAndTrace({
|
|
346
|
+
inputTokens: tokens.inputTokens,
|
|
347
|
+
observability,
|
|
348
|
+
outputTokens: tokens.outputTokens,
|
|
169
349
|
prompt,
|
|
170
350
|
requestType: "general",
|
|
171
351
|
response: result.text,
|
|
172
352
|
responseTime,
|
|
173
|
-
|
|
353
|
+
startTime,
|
|
354
|
+
tokensUsed: tokens.totalTokens,
|
|
174
355
|
userId,
|
|
175
356
|
});
|
|
176
357
|
return result.text;
|
|
177
358
|
}
|
|
178
359
|
catch (error) {
|
|
179
360
|
const responseTime = luxon_1.DateTime.now().toMillis() - startTime;
|
|
180
|
-
await this.
|
|
181
|
-
aiModel: getModelId(this.model),
|
|
361
|
+
await this.logRequestAndTrace({
|
|
182
362
|
error: error instanceof Error ? error.message : String(error),
|
|
363
|
+
observability,
|
|
183
364
|
prompt,
|
|
184
365
|
requestType: "general",
|
|
185
366
|
responseTime,
|
|
367
|
+
startTime,
|
|
186
368
|
userId,
|
|
187
369
|
});
|
|
188
370
|
throw error;
|
|
189
371
|
}
|
|
190
372
|
}
|
|
373
|
+
/**
|
|
374
|
+
* One block document. Uses deterministic temperature. A failed check is repaired once unless
|
|
375
|
+
* `repair` is false. A second failure throws 422 and logs `metadata.errorCodes`.
|
|
376
|
+
*/
|
|
377
|
+
async generateBlocks(options) {
|
|
378
|
+
const { prompt, repair = true, systemPrompt, userId } = options;
|
|
379
|
+
const system = systemPrompt ?? prompts_1.TERRENO_UI_BLOCKS_SYSTEM_PROMPT;
|
|
380
|
+
const startTime = luxon_1.DateTime.now().toMillis();
|
|
381
|
+
const requestDocument = async (userPrompt) => {
|
|
382
|
+
try {
|
|
383
|
+
const result = await (0, ai_1.generateText)({
|
|
384
|
+
experimental_telemetry: { functionId: "generate-blocks", isEnabled: true },
|
|
385
|
+
model: this.getModelForStructuredJson(),
|
|
386
|
+
output: ai_1.Output.object({
|
|
387
|
+
schema: (0, ai_1.jsonSchema)(blocks_1.blocksJsonSchema),
|
|
388
|
+
}),
|
|
389
|
+
prompt: userPrompt,
|
|
390
|
+
system,
|
|
391
|
+
temperature: exports.TemperaturePresets.DETERMINISTIC,
|
|
392
|
+
});
|
|
393
|
+
return { document: result.output };
|
|
394
|
+
}
|
|
395
|
+
catch (error) {
|
|
396
|
+
return { error: error instanceof Error ? error.message : String(error) };
|
|
397
|
+
}
|
|
398
|
+
};
|
|
399
|
+
const finish = async ({ error, errorCodes, response, }) => {
|
|
400
|
+
await this.logRequest({
|
|
401
|
+
aiModel: getModelId(this.model),
|
|
402
|
+
error,
|
|
403
|
+
metadata: errorCodes ? { errorCodes } : undefined,
|
|
404
|
+
prompt,
|
|
405
|
+
requestType: "ui_blocks",
|
|
406
|
+
response,
|
|
407
|
+
responseTime: luxon_1.DateTime.now().toMillis() - startTime,
|
|
408
|
+
userId,
|
|
409
|
+
});
|
|
410
|
+
};
|
|
411
|
+
const first = await requestDocument(prompt);
|
|
412
|
+
if (!first.document) {
|
|
413
|
+
await finish({ error: first.error ?? "Block generation failed" });
|
|
414
|
+
throw new api_1.APIError({ status: 502, title: "Block generation failed" });
|
|
415
|
+
}
|
|
416
|
+
const firstCheck = (0, blocks_1.validateBlocks)(first.document);
|
|
417
|
+
if (first.document && firstCheck?.ok) {
|
|
418
|
+
await finish({ response: JSON.stringify(first.document) });
|
|
419
|
+
return first.document;
|
|
420
|
+
}
|
|
421
|
+
const firstCodes = firstCheck && !firstCheck.ok ? firstCheck.errors.map((error) => error.code) : [];
|
|
422
|
+
const firstDetail = firstCheck && !firstCheck.ok
|
|
423
|
+
? firstCheck.errors
|
|
424
|
+
.map((error) => `${error.path} ${error.code} ${error.message}`)
|
|
425
|
+
.join("\n")
|
|
426
|
+
: first.error;
|
|
427
|
+
if (!repair) {
|
|
428
|
+
await finish({
|
|
429
|
+
error: firstDetail ?? "Block document failed validation",
|
|
430
|
+
errorCodes: firstCodes,
|
|
431
|
+
});
|
|
432
|
+
throw new api_1.APIError({
|
|
433
|
+
meta: { fields: Object.fromEntries(firstCodes.map((code) => [code, code])) },
|
|
434
|
+
status: 422,
|
|
435
|
+
title: "Block document failed validation",
|
|
436
|
+
});
|
|
437
|
+
}
|
|
438
|
+
const second = await requestDocument(`${prompt}\n\nErrors:\n${firstDetail ?? "invalid document"}`);
|
|
439
|
+
if (!second.document) {
|
|
440
|
+
await finish({ error: second.error ?? "Block generation failed" });
|
|
441
|
+
throw new api_1.APIError({ status: 502, title: "Block generation failed" });
|
|
442
|
+
}
|
|
443
|
+
const secondCheck = (0, blocks_1.validateBlocks)(second.document);
|
|
444
|
+
if (second.document && secondCheck?.ok) {
|
|
445
|
+
await finish({ response: JSON.stringify(second.document) });
|
|
446
|
+
return second.document;
|
|
447
|
+
}
|
|
448
|
+
const errorCodes = secondCheck && !secondCheck.ok ? secondCheck.errors.map((error) => error.code) : firstCodes;
|
|
449
|
+
await finish({
|
|
450
|
+
error: "Block document failed validation",
|
|
451
|
+
errorCodes,
|
|
452
|
+
});
|
|
453
|
+
throw new api_1.APIError({
|
|
454
|
+
meta: {
|
|
455
|
+
fields: Object.fromEntries((errorCodes.length > 0 ? errorCodes : ["invalid"]).map((code) => [code, code])),
|
|
456
|
+
},
|
|
457
|
+
status: 422,
|
|
458
|
+
title: "Block document failed validation",
|
|
459
|
+
});
|
|
460
|
+
}
|
|
191
461
|
/** Any JSON value (object, array, primitive, or null) via the AI SDK `Output.json()` parser. */
|
|
192
462
|
async generateJsonValue(options) {
|
|
193
|
-
const
|
|
463
|
+
const observability = await this.resolveObservability({
|
|
464
|
+
...options,
|
|
465
|
+
systemPrompt: options.systemPrompt ?? prompts_1.JSON_VALUE_SYSTEM_PROMPT,
|
|
466
|
+
});
|
|
467
|
+
const { maxOutputTokens, outputDescription, outputName, prompt, temperature, userId } = options;
|
|
194
468
|
const startTime = luxon_1.DateTime.now().toMillis();
|
|
195
|
-
const system = systemPrompt ?? prompts_1.JSON_VALUE_SYSTEM_PROMPT;
|
|
469
|
+
const system = observability.systemPrompt ?? prompts_1.JSON_VALUE_SYSTEM_PROMPT;
|
|
196
470
|
try {
|
|
197
471
|
const result = await (0, ai_1.generateText)({
|
|
198
472
|
experimental_telemetry: { functionId: "generate-json-value", isEnabled: true },
|
|
@@ -204,13 +478,17 @@ class AIService {
|
|
|
204
478
|
temperature: temperature ?? exports.TemperaturePresets.DETERMINISTIC,
|
|
205
479
|
});
|
|
206
480
|
const responseTime = luxon_1.DateTime.now().toMillis() - startTime;
|
|
207
|
-
|
|
208
|
-
|
|
481
|
+
const tokens = readTokenUsage(result.usage);
|
|
482
|
+
await this.logRequestAndTrace({
|
|
483
|
+
inputTokens: tokens.inputTokens,
|
|
484
|
+
observability,
|
|
485
|
+
outputTokens: tokens.outputTokens,
|
|
209
486
|
prompt,
|
|
210
487
|
requestType: "json_value",
|
|
211
488
|
response: JSON.stringify(result.output),
|
|
212
489
|
responseTime,
|
|
213
|
-
|
|
490
|
+
startTime,
|
|
491
|
+
tokensUsed: tokens.totalTokens,
|
|
214
492
|
userId,
|
|
215
493
|
});
|
|
216
494
|
return result.output;
|
|
@@ -219,9 +497,11 @@ class AIService {
|
|
|
219
497
|
const responseTime = luxon_1.DateTime.now().toMillis() - startTime;
|
|
220
498
|
await this.logStructuredJsonFailure({
|
|
221
499
|
error,
|
|
500
|
+
observability,
|
|
222
501
|
prompt,
|
|
223
502
|
requestType: "json_value",
|
|
224
503
|
responseTime,
|
|
504
|
+
startTime,
|
|
225
505
|
system,
|
|
226
506
|
userId,
|
|
227
507
|
});
|
|
@@ -230,9 +510,13 @@ class AIService {
|
|
|
230
510
|
}
|
|
231
511
|
/** Typed object from a Zod schema, `jsonSchema(...)`, or other `FlexibleSchema` (`Output.object()`). */
|
|
232
512
|
async generateJsonObject(options) {
|
|
233
|
-
const
|
|
513
|
+
const observability = await this.resolveObservability({
|
|
514
|
+
...options,
|
|
515
|
+
systemPrompt: options.systemPrompt ?? prompts_1.JSON_VALUE_SYSTEM_PROMPT,
|
|
516
|
+
});
|
|
517
|
+
const { maxOutputTokens, prompt, schema, schemaDescription, schemaName, temperature, userId } = options;
|
|
234
518
|
const startTime = luxon_1.DateTime.now().toMillis();
|
|
235
|
-
const system = systemPrompt ?? prompts_1.JSON_VALUE_SYSTEM_PROMPT;
|
|
519
|
+
const system = observability.systemPrompt ?? prompts_1.JSON_VALUE_SYSTEM_PROMPT;
|
|
236
520
|
try {
|
|
237
521
|
const result = await (0, ai_1.generateText)({
|
|
238
522
|
experimental_telemetry: { functionId: "generate-json-object", isEnabled: true },
|
|
@@ -248,13 +532,17 @@ class AIService {
|
|
|
248
532
|
temperature: temperature ?? exports.TemperaturePresets.DETERMINISTIC,
|
|
249
533
|
});
|
|
250
534
|
const responseTime = luxon_1.DateTime.now().toMillis() - startTime;
|
|
251
|
-
|
|
252
|
-
|
|
535
|
+
const tokens = readTokenUsage(result.usage);
|
|
536
|
+
await this.logRequestAndTrace({
|
|
537
|
+
inputTokens: tokens.inputTokens,
|
|
538
|
+
observability,
|
|
539
|
+
outputTokens: tokens.outputTokens,
|
|
253
540
|
prompt,
|
|
254
541
|
requestType: "json_object",
|
|
255
542
|
response: JSON.stringify(result.output),
|
|
256
543
|
responseTime,
|
|
257
|
-
|
|
544
|
+
startTime,
|
|
545
|
+
tokensUsed: tokens.totalTokens,
|
|
258
546
|
userId,
|
|
259
547
|
});
|
|
260
548
|
return result.output;
|
|
@@ -263,9 +551,11 @@ class AIService {
|
|
|
263
551
|
const responseTime = luxon_1.DateTime.now().toMillis() - startTime;
|
|
264
552
|
await this.logStructuredJsonFailure({
|
|
265
553
|
error,
|
|
554
|
+
observability,
|
|
266
555
|
prompt,
|
|
267
556
|
requestType: "json_object",
|
|
268
557
|
responseTime,
|
|
558
|
+
startTime,
|
|
269
559
|
system,
|
|
270
560
|
userId,
|
|
271
561
|
});
|
|
@@ -277,9 +567,13 @@ class AIService {
|
|
|
277
567
|
* and this method returns the plain array (`Output.array()`).
|
|
278
568
|
*/
|
|
279
569
|
async generateJsonArray(options) {
|
|
280
|
-
const
|
|
570
|
+
const observability = await this.resolveObservability({
|
|
571
|
+
...options,
|
|
572
|
+
systemPrompt: options.systemPrompt ?? prompts_1.JSON_VALUE_SYSTEM_PROMPT,
|
|
573
|
+
});
|
|
574
|
+
const { element, maxOutputTokens, outputDescription, outputName, prompt, temperature, userId } = options;
|
|
281
575
|
const startTime = luxon_1.DateTime.now().toMillis();
|
|
282
|
-
const system = systemPrompt ?? prompts_1.JSON_VALUE_SYSTEM_PROMPT;
|
|
576
|
+
const system = observability.systemPrompt ?? prompts_1.JSON_VALUE_SYSTEM_PROMPT;
|
|
283
577
|
try {
|
|
284
578
|
const result = await (0, ai_1.generateText)({
|
|
285
579
|
experimental_telemetry: { functionId: "generate-json-array", isEnabled: true },
|
|
@@ -295,13 +589,17 @@ class AIService {
|
|
|
295
589
|
temperature: temperature ?? exports.TemperaturePresets.DETERMINISTIC,
|
|
296
590
|
});
|
|
297
591
|
const responseTime = luxon_1.DateTime.now().toMillis() - startTime;
|
|
298
|
-
|
|
299
|
-
|
|
592
|
+
const tokens = readTokenUsage(result.usage);
|
|
593
|
+
await this.logRequestAndTrace({
|
|
594
|
+
inputTokens: tokens.inputTokens,
|
|
595
|
+
observability,
|
|
596
|
+
outputTokens: tokens.outputTokens,
|
|
300
597
|
prompt,
|
|
301
598
|
requestType: "json_array",
|
|
302
599
|
response: JSON.stringify(result.output),
|
|
303
600
|
responseTime,
|
|
304
|
-
|
|
601
|
+
startTime,
|
|
602
|
+
tokensUsed: tokens.totalTokens,
|
|
305
603
|
userId,
|
|
306
604
|
});
|
|
307
605
|
return result.output;
|
|
@@ -310,9 +608,11 @@ class AIService {
|
|
|
310
608
|
const responseTime = luxon_1.DateTime.now().toMillis() - startTime;
|
|
311
609
|
await this.logStructuredJsonFailure({
|
|
312
610
|
error,
|
|
611
|
+
observability,
|
|
313
612
|
prompt,
|
|
314
613
|
requestType: "json_array",
|
|
315
614
|
responseTime,
|
|
615
|
+
startTime,
|
|
316
616
|
system,
|
|
317
617
|
userId,
|
|
318
618
|
});
|
|
@@ -320,7 +620,8 @@ class AIService {
|
|
|
320
620
|
}
|
|
321
621
|
}
|
|
322
622
|
async *generateTextStream(options) {
|
|
323
|
-
const
|
|
623
|
+
const observability = await this.resolveObservability(options);
|
|
624
|
+
const { prompt, temperature, maxOutputTokens, userId } = options;
|
|
324
625
|
const startTime = luxon_1.DateTime.now().toMillis();
|
|
325
626
|
let fullResponse = "";
|
|
326
627
|
try {
|
|
@@ -329,7 +630,7 @@ class AIService {
|
|
|
329
630
|
maxOutputTokens,
|
|
330
631
|
model: this.model,
|
|
331
632
|
prompt,
|
|
332
|
-
system: systemPrompt,
|
|
633
|
+
system: observability.systemPrompt,
|
|
333
634
|
temperature: temperature ?? this.defaultTemperature,
|
|
334
635
|
});
|
|
335
636
|
for await (const chunk of result.textStream) {
|
|
@@ -338,24 +639,29 @@ class AIService {
|
|
|
338
639
|
}
|
|
339
640
|
const responseTime = luxon_1.DateTime.now().toMillis() - startTime;
|
|
340
641
|
const usage = await result.usage;
|
|
341
|
-
|
|
342
|
-
|
|
642
|
+
const tokens = readTokenUsage(usage);
|
|
643
|
+
await this.logRequestAndTrace({
|
|
644
|
+
inputTokens: tokens.inputTokens,
|
|
645
|
+
observability,
|
|
646
|
+
outputTokens: tokens.outputTokens,
|
|
343
647
|
prompt,
|
|
344
648
|
requestType: "general",
|
|
345
649
|
response: fullResponse,
|
|
346
650
|
responseTime,
|
|
347
|
-
|
|
651
|
+
startTime,
|
|
652
|
+
tokensUsed: tokens.totalTokens,
|
|
348
653
|
userId,
|
|
349
654
|
});
|
|
350
655
|
}
|
|
351
656
|
catch (error) {
|
|
352
657
|
const responseTime = luxon_1.DateTime.now().toMillis() - startTime;
|
|
353
|
-
await this.
|
|
354
|
-
aiModel: getModelId(this.model),
|
|
658
|
+
await this.logRequestAndTrace({
|
|
355
659
|
error: error instanceof Error ? error.message : String(error),
|
|
660
|
+
observability,
|
|
356
661
|
prompt,
|
|
357
662
|
requestType: "general",
|
|
358
663
|
responseTime,
|
|
664
|
+
startTime,
|
|
359
665
|
userId,
|
|
360
666
|
});
|
|
361
667
|
throw error;
|
|
@@ -363,6 +669,7 @@ class AIService {
|
|
|
363
669
|
}
|
|
364
670
|
async generateRemix(options) {
|
|
365
671
|
return this.generateText({
|
|
672
|
+
...this.observabilityFields(options),
|
|
366
673
|
prompt: options.text,
|
|
367
674
|
systemPrompt: prompts_1.REMIX_PROMPT,
|
|
368
675
|
temperature: exports.TemperaturePresets.BALANCED,
|
|
@@ -371,6 +678,7 @@ class AIService {
|
|
|
371
678
|
}
|
|
372
679
|
async generateSummary(options) {
|
|
373
680
|
return this.generateText({
|
|
681
|
+
...this.observabilityFields(options),
|
|
374
682
|
prompt: options.text,
|
|
375
683
|
systemPrompt: prompts_1.CONTENT_SUMMARY_PROMPT,
|
|
376
684
|
temperature: exports.TemperaturePresets.LOW,
|
|
@@ -381,18 +689,55 @@ class AIService {
|
|
|
381
689
|
const { text, targetLanguage, sourceLanguage = "auto-detect", userId } = options;
|
|
382
690
|
const systemPrompt = prompts_1.TRANSLATION_PROMPT.replace("{sourceLanguage}", sourceLanguage).replace("{targetLanguage}", targetLanguage);
|
|
383
691
|
return this.generateText({
|
|
692
|
+
...this.observabilityFields(options),
|
|
384
693
|
prompt: text,
|
|
385
694
|
systemPrompt,
|
|
386
695
|
temperature: exports.TemperaturePresets.LOW,
|
|
387
696
|
userId,
|
|
388
697
|
});
|
|
389
698
|
}
|
|
699
|
+
/**
|
|
700
|
+
* Converts history rows to model messages. Ask call/result pairs are kept so the model sees what
|
|
701
|
+
* it asked and how the user answered; other tool rows, approval asks, and asks still waiting for
|
|
702
|
+
* an answer are skipped. Consecutive ask calls came from one step and share one assistant message.
|
|
703
|
+
*/
|
|
390
704
|
buildMessages(prompts) {
|
|
391
705
|
const messages = [];
|
|
706
|
+
const askResults = collectAskResults(prompts);
|
|
707
|
+
let askStep;
|
|
392
708
|
for (const prompt of prompts) {
|
|
709
|
+
if (isModelAskCall(prompt)) {
|
|
710
|
+
const askResult = prompt.toolCallId ? askResults.get(prompt.toolCallId) : undefined;
|
|
711
|
+
if (!askResult || !prompt.toolCallId || !prompt.toolName) {
|
|
712
|
+
continue;
|
|
713
|
+
}
|
|
714
|
+
if (!askStep) {
|
|
715
|
+
askStep = { calls: [], results: [] };
|
|
716
|
+
messages.push({ content: askStep.calls, role: "assistant" }, { content: askStep.results, role: "tool" });
|
|
717
|
+
}
|
|
718
|
+
askStep.calls.push({
|
|
719
|
+
input: prompt.args ?? {},
|
|
720
|
+
toolCallId: prompt.toolCallId,
|
|
721
|
+
toolName: prompt.toolName,
|
|
722
|
+
type: "tool-call",
|
|
723
|
+
});
|
|
724
|
+
askStep.results.push({
|
|
725
|
+
output: { type: "json", value: (askResult.result ?? null) },
|
|
726
|
+
toolCallId: prompt.toolCallId,
|
|
727
|
+
toolName: prompt.toolName,
|
|
728
|
+
type: "tool-result",
|
|
729
|
+
});
|
|
730
|
+
continue;
|
|
731
|
+
}
|
|
732
|
+
askStep = undefined;
|
|
393
733
|
if (prompt.type === "tool-call" || prompt.type === "tool-result") {
|
|
394
734
|
continue;
|
|
395
735
|
}
|
|
736
|
+
// A reply still streaming (or one that failed before any output) is not a real turn yet
|
|
737
|
+
if (prompt.status === "streaming" ||
|
|
738
|
+
(prompt.status === "error" && !prompt.text && !prompt.content?.length)) {
|
|
739
|
+
continue;
|
|
740
|
+
}
|
|
396
741
|
const role = prompt.type;
|
|
397
742
|
if (prompt.content && prompt.content.length > 0 && role === "user") {
|
|
398
743
|
const parts = [];
|
|
@@ -434,7 +779,11 @@ class AIService {
|
|
|
434
779
|
return messages;
|
|
435
780
|
}
|
|
436
781
|
async *generateChatStream(options) {
|
|
437
|
-
const
|
|
782
|
+
const observability = await this.resolveObservability({
|
|
783
|
+
...options,
|
|
784
|
+
systemPrompt: options.systemPrompt ?? prompts_1.DEFAULT_GPT_MEMORY,
|
|
785
|
+
});
|
|
786
|
+
const { messages, tools, toolChoice, stopWhen, userId } = options;
|
|
438
787
|
const startTime = luxon_1.DateTime.now().toMillis();
|
|
439
788
|
let fullResponse = "";
|
|
440
789
|
const promptText = messages.map((m) => `${m.role}: ${m.content}`).join("\n");
|
|
@@ -444,7 +793,7 @@ class AIService {
|
|
|
444
793
|
messages: messages.map((m) => ({ content: m.content, role: m.role })),
|
|
445
794
|
model: this.model,
|
|
446
795
|
stopWhen: stopWhen ?? (0, ai_1.stepCountIs)(1),
|
|
447
|
-
system: systemPrompt ?? prompts_1.DEFAULT_GPT_MEMORY,
|
|
796
|
+
system: observability.systemPrompt ?? prompts_1.DEFAULT_GPT_MEMORY,
|
|
448
797
|
temperature: this.defaultTemperature,
|
|
449
798
|
toolChoice,
|
|
450
799
|
tools,
|
|
@@ -455,24 +804,29 @@ class AIService {
|
|
|
455
804
|
}
|
|
456
805
|
const responseTime = luxon_1.DateTime.now().toMillis() - startTime;
|
|
457
806
|
const usage = await result.usage;
|
|
458
|
-
|
|
459
|
-
|
|
807
|
+
const tokens = readTokenUsage(usage);
|
|
808
|
+
await this.logRequestAndTrace({
|
|
809
|
+
inputTokens: tokens.inputTokens,
|
|
810
|
+
observability,
|
|
811
|
+
outputTokens: tokens.outputTokens,
|
|
460
812
|
prompt: promptText,
|
|
461
813
|
requestType: "general",
|
|
462
814
|
response: fullResponse,
|
|
463
815
|
responseTime,
|
|
464
|
-
|
|
816
|
+
startTime,
|
|
817
|
+
tokensUsed: tokens.totalTokens,
|
|
465
818
|
userId,
|
|
466
819
|
});
|
|
467
820
|
}
|
|
468
821
|
catch (error) {
|
|
469
822
|
const responseTime = luxon_1.DateTime.now().toMillis() - startTime;
|
|
470
|
-
await this.
|
|
471
|
-
aiModel: getModelId(this.model),
|
|
823
|
+
await this.logRequestAndTrace({
|
|
472
824
|
error: error instanceof Error ? error.message : String(error),
|
|
825
|
+
observability,
|
|
473
826
|
prompt: promptText,
|
|
474
827
|
requestType: "general",
|
|
475
828
|
responseTime,
|
|
829
|
+
startTime,
|
|
476
830
|
userId,
|
|
477
831
|
});
|
|
478
832
|
throw error;
|