@terreno/ai 57.9.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/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 +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 +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 +934 -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 +17 -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/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.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 +1095 -0
- package/src/types/index.ts +209 -13
- package/src/types/observability.ts +344 -0
- package/src/types/sanitize-html.d.ts +25 -0
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
import {afterEach, describe, expect, it} from "bun:test";
|
|
2
|
+
import {z} from "@terreno/api";
|
|
3
|
+
import type mongoose from "mongoose";
|
|
4
|
+
|
|
5
|
+
import {AIRequest} from "../models/aiRequest";
|
|
6
|
+
import {GptHistory} from "../models/gptHistory";
|
|
7
|
+
import {buildApp, createScriptedModel} from "../tests/chatHarness";
|
|
8
|
+
import {authAsUser, ensureTestUsers} from "../tests/helpers";
|
|
9
|
+
import {HOST_ACTION_TIMEOUT_MS} from "./gptActions";
|
|
10
|
+
|
|
11
|
+
const model = createScriptedModel({steps: [[]]});
|
|
12
|
+
|
|
13
|
+
const validBlocks = (): Record<string, unknown> => {
|
|
14
|
+
const doc: Record<string, unknown> = {};
|
|
15
|
+
doc.v = 1;
|
|
16
|
+
doc.blocks = [{status: "info", text: "Exporting", type: "badge"}];
|
|
17
|
+
return doc;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
describe("POST /gpt/actions", () => {
|
|
21
|
+
afterEach(async () => {
|
|
22
|
+
await AIRequest.deleteMany({});
|
|
23
|
+
await GptHistory.deleteMany({});
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it("keeps the default handler cap at 10 seconds", () => {
|
|
27
|
+
expect(HOST_ACTION_TIMEOUT_MS).toBe(10_000);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it("is not mounted when uiBlocks is off", async () => {
|
|
31
|
+
await ensureTestUsers();
|
|
32
|
+
const app = buildApp({model});
|
|
33
|
+
const agent = await authAsUser(app, "notAdmin");
|
|
34
|
+
const res = await agent.post("/gpt/actions").send({name: "export_csv"});
|
|
35
|
+
expect(res.status).toBe(404);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("rejects an unknown name, a bad payload, and a non-owner", async () => {
|
|
39
|
+
const created = await ensureTestUsers();
|
|
40
|
+
const userId = (created[1] as {_id: mongoose.Types.ObjectId})._id;
|
|
41
|
+
const history = await GptHistory.create({
|
|
42
|
+
prompts: [{text: "Hello", type: "user"}],
|
|
43
|
+
userId,
|
|
44
|
+
});
|
|
45
|
+
const app = buildApp({
|
|
46
|
+
model,
|
|
47
|
+
uiBlocks: {
|
|
48
|
+
hostActions: {
|
|
49
|
+
export_csv: {
|
|
50
|
+
handler: async () => ({blocks: validBlocks(), replace: "block" as const}),
|
|
51
|
+
payload: z.object({format: z.literal("csv")}).strict(),
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
},
|
|
55
|
+
});
|
|
56
|
+
const agent = await authAsUser(app, "notAdmin");
|
|
57
|
+
const body = {
|
|
58
|
+
blockId: "row",
|
|
59
|
+
elementId: "run_btn",
|
|
60
|
+
historyId: history._id.toString(),
|
|
61
|
+
messageId: "m1",
|
|
62
|
+
name: "export_csv",
|
|
63
|
+
payload: {format: "csv"},
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
const unknown = await agent.post("/gpt/actions").send({...body, name: "missing"});
|
|
67
|
+
expect(unknown.status).toBe(404);
|
|
68
|
+
expect(unknown.body.title).toBe("Unknown action");
|
|
69
|
+
|
|
70
|
+
const invalid = await agent.post("/gpt/actions").send({...body, payload: {format: "pdf"}});
|
|
71
|
+
expect(invalid.status).toBe(400);
|
|
72
|
+
expect(invalid.body.title).toBe("Invalid payload");
|
|
73
|
+
expect(invalid.body.meta.fields.format).toEqual(expect.any(String));
|
|
74
|
+
|
|
75
|
+
const other = await authAsUser(app, "admin");
|
|
76
|
+
const denied = await other.post("/gpt/actions").send(body);
|
|
77
|
+
expect(denied.status).toBe(403);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it("returns the handler document and logs ui_action", async () => {
|
|
81
|
+
const created = await ensureTestUsers();
|
|
82
|
+
const userId = (created[1] as {_id: mongoose.Types.ObjectId})._id;
|
|
83
|
+
const history = await GptHistory.create({
|
|
84
|
+
prompts: [{text: "Hello", type: "user"}],
|
|
85
|
+
userId,
|
|
86
|
+
});
|
|
87
|
+
const app = buildApp({
|
|
88
|
+
model,
|
|
89
|
+
uiBlocks: {
|
|
90
|
+
hostActions: {
|
|
91
|
+
export_csv: {
|
|
92
|
+
handler: async ({payload}) => {
|
|
93
|
+
expect(payload).toEqual({format: "csv"});
|
|
94
|
+
return {blocks: validBlocks(), replace: "block" as const};
|
|
95
|
+
},
|
|
96
|
+
payload: z.object({format: z.literal("csv")}).strict(),
|
|
97
|
+
},
|
|
98
|
+
},
|
|
99
|
+
},
|
|
100
|
+
});
|
|
101
|
+
const agent = await authAsUser(app, "notAdmin");
|
|
102
|
+
const res = await agent.post("/gpt/actions").send({
|
|
103
|
+
blockId: "row",
|
|
104
|
+
elementId: "run_btn",
|
|
105
|
+
historyId: history._id.toString(),
|
|
106
|
+
messageId: "m1",
|
|
107
|
+
name: "export_csv",
|
|
108
|
+
payload: {format: "csv"},
|
|
109
|
+
});
|
|
110
|
+
expect(res.status).toBe(200);
|
|
111
|
+
expect(res.body.data.replace).toBe("block");
|
|
112
|
+
expect(res.body.data.blocks.blocks[0].text).toBe("Exporting");
|
|
113
|
+
const logged = await AIRequest.findExactlyOne({requestType: "ui_action"});
|
|
114
|
+
expect(logged?.requestType).toBe("ui_action");
|
|
115
|
+
expect(logged?.error).toBeUndefined();
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it("returns 504 and logs the timeout", async () => {
|
|
119
|
+
const created = await ensureTestUsers();
|
|
120
|
+
const userId = (created[1] as {_id: mongoose.Types.ObjectId})._id;
|
|
121
|
+
const history = await GptHistory.create({
|
|
122
|
+
prompts: [{text: "Hello", type: "user"}],
|
|
123
|
+
userId,
|
|
124
|
+
});
|
|
125
|
+
const app = buildApp({
|
|
126
|
+
model,
|
|
127
|
+
uiBlocks: {
|
|
128
|
+
actionTimeoutMs: 30,
|
|
129
|
+
hostActions: {
|
|
130
|
+
export_csv: {
|
|
131
|
+
handler: () => new Promise(() => {}),
|
|
132
|
+
},
|
|
133
|
+
},
|
|
134
|
+
},
|
|
135
|
+
});
|
|
136
|
+
const agent = await authAsUser(app, "notAdmin");
|
|
137
|
+
const res = await agent.post("/gpt/actions").send({
|
|
138
|
+
blockId: "row",
|
|
139
|
+
elementId: "run_btn",
|
|
140
|
+
historyId: history._id.toString(),
|
|
141
|
+
messageId: "m1",
|
|
142
|
+
name: "export_csv",
|
|
143
|
+
});
|
|
144
|
+
expect(res.status).toBe(504);
|
|
145
|
+
const logged = await AIRequest.findExactlyOne({requestType: "ui_action"});
|
|
146
|
+
expect(logged?.error).toBe("Action timed out");
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it("returns 500 when the handler's document is invalid", async () => {
|
|
150
|
+
const created = await ensureTestUsers();
|
|
151
|
+
const userId = (created[1] as {_id: mongoose.Types.ObjectId})._id;
|
|
152
|
+
const history = await GptHistory.create({
|
|
153
|
+
prompts: [{text: "Hello", type: "user"}],
|
|
154
|
+
userId,
|
|
155
|
+
});
|
|
156
|
+
const app = buildApp({
|
|
157
|
+
model,
|
|
158
|
+
uiBlocks: {
|
|
159
|
+
hostActions: {
|
|
160
|
+
export_csv: {
|
|
161
|
+
handler: async () => {
|
|
162
|
+
const doc: Record<string, unknown> = {};
|
|
163
|
+
doc.v = 1;
|
|
164
|
+
doc.blocks = [{color: "primary", text: "Hi", type: "heading"}];
|
|
165
|
+
return {blocks: doc, replace: "block" as const};
|
|
166
|
+
},
|
|
167
|
+
},
|
|
168
|
+
},
|
|
169
|
+
},
|
|
170
|
+
});
|
|
171
|
+
const agent = await authAsUser(app, "notAdmin");
|
|
172
|
+
const res = await agent.post("/gpt/actions").send({
|
|
173
|
+
blockId: "row",
|
|
174
|
+
elementId: "run_btn",
|
|
175
|
+
historyId: history._id.toString(),
|
|
176
|
+
messageId: "m1",
|
|
177
|
+
name: "export_csv",
|
|
178
|
+
});
|
|
179
|
+
expect(res.status).toBe(500);
|
|
180
|
+
expect(res.body.title).toBe("Action returned an invalid document");
|
|
181
|
+
expect(res.body.meta.fields["blocks[0].color"]).toEqual(expect.stringContaining("UNKNOWN_KEY"));
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
it("rejects a missing body, a bad history id, and a history that does not exist", async () => {
|
|
185
|
+
await ensureTestUsers();
|
|
186
|
+
const app = buildApp({
|
|
187
|
+
model,
|
|
188
|
+
uiBlocks: {hostActions: {export_csv: {handler: async () => ({text: "ok"})}}},
|
|
189
|
+
});
|
|
190
|
+
const agent = await authAsUser(app, "notAdmin");
|
|
191
|
+
const missingBody = await agent.post("/gpt/actions").send({});
|
|
192
|
+
expect(missingBody.status).toBe(400);
|
|
193
|
+
|
|
194
|
+
const body = {
|
|
195
|
+
blockId: "row",
|
|
196
|
+
elementId: "run_btn",
|
|
197
|
+
historyId: "not-an-id",
|
|
198
|
+
messageId: "m1",
|
|
199
|
+
name: "export_csv",
|
|
200
|
+
};
|
|
201
|
+
const badId = await agent.post("/gpt/actions").send(body);
|
|
202
|
+
expect(badId.status).toBe(400);
|
|
203
|
+
expect(badId.body.title).toBe("historyId is not valid");
|
|
204
|
+
|
|
205
|
+
const missing = await agent.post("/gpt/actions").send({
|
|
206
|
+
...body,
|
|
207
|
+
historyId: "000000000000000000000000",
|
|
208
|
+
});
|
|
209
|
+
expect(missing.status).toBe(404);
|
|
210
|
+
expect(missing.body.title).toBe("History not found");
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
it("rejects a replace value the client does not understand", async () => {
|
|
214
|
+
const created = await ensureTestUsers();
|
|
215
|
+
const userId = (created[1] as {_id: mongoose.Types.ObjectId})._id;
|
|
216
|
+
const history = await GptHistory.create({
|
|
217
|
+
prompts: [{text: "Hello", type: "user"}],
|
|
218
|
+
userId,
|
|
219
|
+
});
|
|
220
|
+
const app = buildApp({
|
|
221
|
+
model,
|
|
222
|
+
uiBlocks: {
|
|
223
|
+
hostActions: {
|
|
224
|
+
export_csv: {
|
|
225
|
+
handler: async () => ({replace: "page" as "block", text: "nope"}),
|
|
226
|
+
},
|
|
227
|
+
},
|
|
228
|
+
},
|
|
229
|
+
});
|
|
230
|
+
const agent = await authAsUser(app, "notAdmin");
|
|
231
|
+
const res = await agent.post("/gpt/actions").send({
|
|
232
|
+
blockId: "row",
|
|
233
|
+
elementId: "run_btn",
|
|
234
|
+
historyId: history._id.toString(),
|
|
235
|
+
messageId: "m1",
|
|
236
|
+
name: "export_csv",
|
|
237
|
+
});
|
|
238
|
+
expect(res.status).toBe(500);
|
|
239
|
+
expect(res.body.title).toBe("Action returned an unknown replace value");
|
|
240
|
+
});
|
|
241
|
+
});
|
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
import {
|
|
2
|
+
APIError,
|
|
3
|
+
asyncHandler,
|
|
4
|
+
authenticateMiddleware,
|
|
5
|
+
createOpenApiBuilder,
|
|
6
|
+
logger,
|
|
7
|
+
} from "@terreno/api";
|
|
8
|
+
import {type BlocksDocument, validateBlocks} from "@terreno/blocks";
|
|
9
|
+
import type express from "express";
|
|
10
|
+
import {DateTime} from "luxon";
|
|
11
|
+
import mongoose from "mongoose";
|
|
12
|
+
|
|
13
|
+
import {AIRequest} from "../models/aiRequest";
|
|
14
|
+
import {GptHistory} from "../models/gptHistory";
|
|
15
|
+
import type {GptRouteOptions, HostActionResult} from "../types";
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Host callbacks give up after this long unless `uiBlocks.actionTimeoutMs` sets another cap.
|
|
19
|
+
* @internal Test seam. Production reads it only inside this module.
|
|
20
|
+
*/
|
|
21
|
+
export const HOST_ACTION_TIMEOUT_MS = 10_000;
|
|
22
|
+
|
|
23
|
+
const requestUserId = (req: express.Request): mongoose.Types.ObjectId | undefined =>
|
|
24
|
+
(req.user as {_id?: mongoose.Types.ObjectId} | undefined)?._id;
|
|
25
|
+
|
|
26
|
+
const requiredString = (value: unknown): value is string =>
|
|
27
|
+
typeof value === "string" && value.length > 0;
|
|
28
|
+
|
|
29
|
+
const payloadFields = (
|
|
30
|
+
issues: {message: string; path: PropertyKey[]}[]
|
|
31
|
+
): Record<string, string> => {
|
|
32
|
+
const fields: Record<string, string> = {};
|
|
33
|
+
for (const issue of issues) {
|
|
34
|
+
const path = issue.path.map(String).join(".") || "payload";
|
|
35
|
+
fields[path] = issue.message;
|
|
36
|
+
}
|
|
37
|
+
return fields;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const withTimeout = async <T>(work: Promise<T>, timeoutMs: number): Promise<T> => {
|
|
41
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
42
|
+
try {
|
|
43
|
+
return await Promise.race([
|
|
44
|
+
work,
|
|
45
|
+
new Promise<T>((_resolve, reject) => {
|
|
46
|
+
timer = setTimeout(() => {
|
|
47
|
+
reject(new APIError({status: 504, title: "Action timed out"}));
|
|
48
|
+
}, timeoutMs);
|
|
49
|
+
}),
|
|
50
|
+
]);
|
|
51
|
+
} finally {
|
|
52
|
+
if (timer !== undefined) {
|
|
53
|
+
clearTimeout(timer);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
const assertReturnedBlocks = (blocks: unknown): void => {
|
|
59
|
+
let validated: ReturnType<typeof validateBlocks>;
|
|
60
|
+
try {
|
|
61
|
+
validated = validateBlocks(blocks as BlocksDocument);
|
|
62
|
+
} catch (error) {
|
|
63
|
+
throw new APIError({
|
|
64
|
+
detail: error instanceof Error ? error.message : String(error),
|
|
65
|
+
status: 500,
|
|
66
|
+
title: "Action returned an invalid document",
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
if (!validated.ok) {
|
|
70
|
+
throw new APIError({
|
|
71
|
+
meta: {
|
|
72
|
+
fields: Object.fromEntries(
|
|
73
|
+
validated.errors.map((error) => [error.path, `${error.code} ${error.message}`])
|
|
74
|
+
),
|
|
75
|
+
},
|
|
76
|
+
status: 500,
|
|
77
|
+
title: "Action returned an invalid document",
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
const logAction = async ({
|
|
83
|
+
error,
|
|
84
|
+
prompt,
|
|
85
|
+
response,
|
|
86
|
+
responseTime,
|
|
87
|
+
userId,
|
|
88
|
+
}: {
|
|
89
|
+
error?: string;
|
|
90
|
+
prompt: string;
|
|
91
|
+
response?: string;
|
|
92
|
+
responseTime: number;
|
|
93
|
+
userId: mongoose.Types.ObjectId | undefined;
|
|
94
|
+
}): Promise<void> => {
|
|
95
|
+
try {
|
|
96
|
+
await AIRequest.logRequest({
|
|
97
|
+
aiModel: "host-action",
|
|
98
|
+
error,
|
|
99
|
+
prompt,
|
|
100
|
+
requestType: "ui_action",
|
|
101
|
+
response,
|
|
102
|
+
responseTime,
|
|
103
|
+
userId,
|
|
104
|
+
});
|
|
105
|
+
} catch (logError) {
|
|
106
|
+
logger.warn("Failed to log ui_action", {
|
|
107
|
+
error: logError instanceof Error ? logError.message : String(logError),
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
/** Owner callback route. Mounted only when `uiBlocks` is on, under `/gpt` with the other chat routes. */
|
|
113
|
+
export const addGptActionRoutes = (router: express.Router, options: GptRouteOptions): void => {
|
|
114
|
+
const uiBlocks = options.uiBlocks === true || !options.uiBlocks ? {} : options.uiBlocks;
|
|
115
|
+
const timeoutMs = uiBlocks.actionTimeoutMs ?? HOST_ACTION_TIMEOUT_MS;
|
|
116
|
+
|
|
117
|
+
router.post(
|
|
118
|
+
"/gpt/actions",
|
|
119
|
+
[
|
|
120
|
+
authenticateMiddleware(),
|
|
121
|
+
createOpenApiBuilder(options.openApiOptions ?? {})
|
|
122
|
+
.withTags(["gpt"])
|
|
123
|
+
.withSummary("Run a host callback from a block button")
|
|
124
|
+
.withRequestBody({
|
|
125
|
+
blockId: {type: "string"},
|
|
126
|
+
elementId: {type: "string"},
|
|
127
|
+
historyId: {type: "string"},
|
|
128
|
+
messageId: {type: "string"},
|
|
129
|
+
name: {type: "string"},
|
|
130
|
+
payload: {type: "object"},
|
|
131
|
+
})
|
|
132
|
+
.withResponse(200, {
|
|
133
|
+
blocks: {type: "object"},
|
|
134
|
+
replace: {type: "string"},
|
|
135
|
+
text: {type: "string"},
|
|
136
|
+
})
|
|
137
|
+
.build(),
|
|
138
|
+
],
|
|
139
|
+
asyncHandler(async (req: express.Request, res: express.Response) => {
|
|
140
|
+
const body = (req.body ?? {}) as Record<string, unknown>;
|
|
141
|
+
const {blockId, elementId, historyId, messageId, name, payload} = body;
|
|
142
|
+
if (
|
|
143
|
+
!requiredString(historyId) ||
|
|
144
|
+
!requiredString(messageId) ||
|
|
145
|
+
!requiredString(blockId) ||
|
|
146
|
+
!requiredString(elementId) ||
|
|
147
|
+
!requiredString(name)
|
|
148
|
+
) {
|
|
149
|
+
throw new APIError({
|
|
150
|
+
status: 400,
|
|
151
|
+
title: "historyId, messageId, blockId, elementId, and name are required",
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
if (!mongoose.Types.ObjectId.isValid(historyId)) {
|
|
156
|
+
throw new APIError({status: 400, title: "historyId is not valid"});
|
|
157
|
+
}
|
|
158
|
+
const userId = requestUserId(req);
|
|
159
|
+
const history = await GptHistory.findOneOrNone({_id: historyId, deleted: false});
|
|
160
|
+
if (!history) {
|
|
161
|
+
throw new APIError({status: 404, title: "History not found"});
|
|
162
|
+
}
|
|
163
|
+
if (history.userId.toString() !== userId?.toString()) {
|
|
164
|
+
throw new APIError({status: 403, title: "Not authorized to access this history"});
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
const action = uiBlocks.hostActions?.[name];
|
|
168
|
+
if (!action?.handler) {
|
|
169
|
+
throw new APIError({status: 404, title: "Unknown action"});
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
let parsedPayload: unknown = payload ?? {};
|
|
173
|
+
if (action.payload) {
|
|
174
|
+
const parsed = action.payload.safeParse(parsedPayload);
|
|
175
|
+
if (!parsed.success) {
|
|
176
|
+
throw new APIError({
|
|
177
|
+
meta: {fields: payloadFields(parsed.error.issues)},
|
|
178
|
+
status: 400,
|
|
179
|
+
title: "Invalid payload",
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
parsedPayload = parsed.data;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
const started = DateTime.now();
|
|
186
|
+
const prompt = JSON.stringify({blockId, elementId, historyId, messageId, name});
|
|
187
|
+
let result: HostActionResult | undefined;
|
|
188
|
+
try {
|
|
189
|
+
result = await withTimeout(
|
|
190
|
+
Promise.resolve(
|
|
191
|
+
action.handler({
|
|
192
|
+
blockId,
|
|
193
|
+
elementId,
|
|
194
|
+
history,
|
|
195
|
+
messageId,
|
|
196
|
+
payload: parsedPayload,
|
|
197
|
+
user: {_id: userId},
|
|
198
|
+
})
|
|
199
|
+
),
|
|
200
|
+
timeoutMs
|
|
201
|
+
);
|
|
202
|
+
} catch (error) {
|
|
203
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
204
|
+
await logAction({
|
|
205
|
+
error: message,
|
|
206
|
+
prompt,
|
|
207
|
+
responseTime: DateTime.now().toMillis() - started.toMillis(),
|
|
208
|
+
userId,
|
|
209
|
+
});
|
|
210
|
+
throw error;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
if (
|
|
214
|
+
result?.replace !== undefined &&
|
|
215
|
+
result.replace !== "block" &&
|
|
216
|
+
result.replace !== "message"
|
|
217
|
+
) {
|
|
218
|
+
await logAction({
|
|
219
|
+
error: "Action returned an unknown replace value",
|
|
220
|
+
prompt,
|
|
221
|
+
responseTime: DateTime.now().toMillis() - started.toMillis(),
|
|
222
|
+
userId,
|
|
223
|
+
});
|
|
224
|
+
throw new APIError({status: 500, title: "Action returned an unknown replace value"});
|
|
225
|
+
}
|
|
226
|
+
if (result?.blocks !== undefined) {
|
|
227
|
+
try {
|
|
228
|
+
assertReturnedBlocks(result.blocks);
|
|
229
|
+
} catch (error) {
|
|
230
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
231
|
+
await logAction({
|
|
232
|
+
error: message,
|
|
233
|
+
prompt,
|
|
234
|
+
responseTime: DateTime.now().toMillis() - started.toMillis(),
|
|
235
|
+
userId,
|
|
236
|
+
});
|
|
237
|
+
throw error;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
const data = {
|
|
242
|
+
...(result?.blocks !== undefined ? {blocks: result.blocks} : {}),
|
|
243
|
+
...(result?.replace !== undefined ? {replace: result.replace} : {}),
|
|
244
|
+
...(result?.text !== undefined ? {text: result.text} : {}),
|
|
245
|
+
};
|
|
246
|
+
await logAction({
|
|
247
|
+
prompt,
|
|
248
|
+
response: JSON.stringify(data),
|
|
249
|
+
responseTime: DateTime.now().toMillis() - started.toMillis(),
|
|
250
|
+
userId,
|
|
251
|
+
});
|
|
252
|
+
return res.json({data});
|
|
253
|
+
})
|
|
254
|
+
);
|
|
255
|
+
};
|