@terreno/ai 57.9.0 → 57.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -0
- package/bunfig.toml +1 -1
- package/dist/aiApp.d.ts +8 -2
- package/dist/aiApp.js +12 -6
- package/dist/harness/adminScreens.d.ts +6 -0
- package/dist/harness/adminScreens.js +11 -0
- package/dist/harness/agentLoop.d.ts +61 -0
- package/dist/harness/agentLoop.js +798 -0
- package/dist/harness/agentTaskNames.d.ts +4 -0
- package/dist/harness/agentTaskNames.js +7 -0
- package/dist/harness/approvalPolicy.d.ts +3 -0
- package/dist/harness/approvalPolicy.js +23 -0
- package/dist/harness/approvals.d.ts +101 -0
- package/dist/harness/approvals.js +468 -0
- package/dist/harness/commit.d.ts +238 -0
- package/dist/harness/commit.js +678 -0
- package/dist/harness/conversation.d.ts +75 -0
- package/dist/harness/conversation.js +397 -0
- package/dist/harness/defineAgent.d.ts +9 -0
- package/dist/harness/defineAgent.js +65 -0
- package/dist/harness/defineTask.d.ts +11 -0
- package/dist/harness/defineTask.js +59 -0
- package/dist/harness/defineTool.d.ts +7 -0
- package/dist/harness/defineTool.js +36 -0
- package/dist/harness/definitionError.d.ts +8 -0
- package/dist/harness/definitionError.js +15 -0
- package/dist/harness/errors.d.ts +133 -0
- package/dist/harness/errors.js +107 -0
- package/dist/harness/eventHub.d.ts +45 -0
- package/dist/harness/eventHub.js +135 -0
- package/dist/harness/events.d.ts +95 -0
- package/dist/harness/events.js +267 -0
- package/dist/harness/executionEnv.d.ts +31 -0
- package/dist/harness/executionEnv.js +2 -0
- package/dist/harness/extensions.d.ts +82 -0
- package/dist/harness/extensions.js +274 -0
- package/dist/harness/harness.d.ts +163 -0
- package/dist/harness/harness.js +522 -0
- package/dist/harness/harnessApp.d.ts +44 -0
- package/dist/harness/harnessApp.js +62 -0
- package/dist/harness/internalRuntime.d.ts +16 -0
- package/dist/harness/internalRuntime.js +18 -0
- package/dist/harness/leases.d.ts +35 -0
- package/dist/harness/leases.js +86 -0
- package/dist/harness/memo.d.ts +24 -0
- package/dist/harness/memo.js +78 -0
- package/dist/harness/modelCall.d.ts +45 -0
- package/dist/harness/modelCall.js +147 -0
- package/dist/harness/models/harnessApproval.d.ts +2 -0
- package/dist/harness/models/harnessApproval.js +100 -0
- package/dist/harness/models/harnessConversation.d.ts +2 -0
- package/dist/harness/models/harnessConversation.js +117 -0
- package/dist/harness/models/harnessEvent.d.ts +3 -0
- package/dist/harness/models/harnessEvent.js +73 -0
- package/dist/harness/models/harnessInboxEvent.d.ts +2 -0
- package/dist/harness/models/harnessInboxEvent.js +58 -0
- package/dist/harness/models/harnessMemo.d.ts +2 -0
- package/dist/harness/models/harnessMemo.js +42 -0
- package/dist/harness/models/harnessMessage.d.ts +2 -0
- package/dist/harness/models/harnessMessage.js +73 -0
- package/dist/harness/models/harnessOwner.d.ts +2 -0
- package/dist/harness/models/harnessOwner.js +36 -0
- package/dist/harness/models/harnessTask.d.ts +2 -0
- package/dist/harness/models/harnessTask.js +191 -0
- package/dist/harness/ownership.d.ts +89 -0
- package/dist/harness/ownership.js +438 -0
- package/dist/harness/registry.d.ts +29 -0
- package/dist/harness/registry.js +93 -0
- package/dist/harness/retryBackoff.d.ts +31 -0
- package/dist/harness/retryBackoff.js +50 -0
- package/dist/harness/routes/approvals.d.ts +13 -0
- package/dist/harness/routes/approvals.js +93 -0
- package/dist/harness/routes/conversations.d.ts +11 -0
- package/dist/harness/routes/conversations.js +66 -0
- package/dist/harness/routes/events.d.ts +19 -0
- package/dist/harness/routes/events.js +215 -0
- package/dist/harness/routes/tasks.d.ts +11 -0
- package/dist/harness/routes/tasks.js +108 -0
- package/dist/harness/runners/inProcessRunner.d.ts +64 -0
- package/dist/harness/runners/inProcessRunner.js +227 -0
- package/dist/harness/runners/jobsRunner.d.ts +62 -0
- package/dist/harness/runners/jobsRunner.js +212 -0
- package/dist/harness/runtime.d.ts +52 -0
- package/dist/harness/runtime.js +604 -0
- package/dist/harness/subagent.d.ts +53 -0
- package/dist/harness/subagent.js +134 -0
- package/dist/harness/suspend.d.ts +8 -0
- package/dist/harness/suspend.js +16 -0
- package/dist/harness/transaction.d.ts +3 -0
- package/dist/harness/transaction.js +22 -0
- package/dist/harness/waits.d.ts +100 -0
- package/dist/harness/waits.js +214 -0
- package/dist/index.d.ts +19 -1
- package/dist/index.js +45 -2
- package/dist/models/aiDataset.d.ts +2 -0
- package/dist/models/aiDataset.js +67 -0
- package/dist/models/aiRequest.js +1 -1
- package/dist/models/gptHistory.js +73 -0
- package/dist/observability/adminScreens.d.ts +5 -0
- package/dist/observability/adminScreens.js +32 -0
- package/dist/observability/compileTemplate.d.ts +1 -0
- package/dist/observability/compileTemplate.js +13 -0
- package/dist/observability/datasetImport.d.ts +11 -0
- package/dist/observability/datasetImport.js +185 -0
- package/dist/observability/evaluate.d.ts +31 -0
- package/dist/observability/evaluate.js +200 -0
- package/dist/observability/evaluatorTemplates.d.ts +17 -0
- package/dist/observability/evaluatorTemplates.js +111 -0
- package/dist/observability/jsonPath.d.ts +2 -0
- package/dist/observability/jsonPath.js +57 -0
- package/dist/observability/local/datasetStore.d.ts +87 -0
- package/dist/observability/local/datasetStore.js +326 -0
- package/dist/observability/local/evaluatorStore.d.ts +44 -0
- package/dist/observability/local/evaluatorStore.js +182 -0
- package/dist/observability/local/experimentRunner.d.ts +96 -0
- package/dist/observability/local/experimentRunner.js +419 -0
- package/dist/observability/local/localPlugin.d.ts +22 -0
- package/dist/observability/local/localPlugin.js +76 -0
- package/dist/observability/local/models/obsDataset.d.ts +2 -0
- package/dist/observability/local/models/obsDataset.js +37 -0
- package/dist/observability/local/models/obsDatasetItem.d.ts +2 -0
- package/dist/observability/local/models/obsDatasetItem.js +76 -0
- package/dist/observability/local/models/obsEvaluator.d.ts +2 -0
- package/dist/observability/local/models/obsEvaluator.js +98 -0
- package/dist/observability/local/models/obsExperiment.d.ts +2 -0
- package/dist/observability/local/models/obsExperiment.js +87 -0
- package/dist/observability/local/models/obsExperimentItem.d.ts +2 -0
- package/dist/observability/local/models/obsExperimentItem.js +45 -0
- package/dist/observability/local/models/obsPrompt.d.ts +2 -0
- package/dist/observability/local/models/obsPrompt.js +42 -0
- package/dist/observability/local/models/obsPromptLabel.d.ts +2 -0
- package/dist/observability/local/models/obsPromptLabel.js +39 -0
- package/dist/observability/local/models/obsPromptVersion.d.ts +2 -0
- package/dist/observability/local/models/obsPromptVersion.js +96 -0
- package/dist/observability/local/models/obsReviewItem.d.ts +2 -0
- package/dist/observability/local/models/obsReviewItem.js +75 -0
- package/dist/observability/local/models/obsScore.d.ts +2 -0
- package/dist/observability/local/models/obsScore.js +58 -0
- package/dist/observability/local/models/obsSpan.d.ts +2 -0
- package/dist/observability/local/models/obsSpan.js +64 -0
- package/dist/observability/local/models/obsTrace.d.ts +2 -0
- package/dist/observability/local/models/obsTrace.js +93 -0
- package/dist/observability/local/promptStore.d.ts +180 -0
- package/dist/observability/local/promptStore.js +444 -0
- package/dist/observability/local/reviewStore.d.ts +50 -0
- package/dist/observability/local/reviewStore.js +203 -0
- package/dist/observability/local/traceStore.d.ts +103 -0
- package/dist/observability/local/traceStore.js +325 -0
- package/dist/observability/observabilityApp.d.ts +24 -0
- package/dist/observability/observabilityApp.js +157 -0
- package/dist/observability/observabilityAppRegistry.d.ts +14 -0
- package/dist/observability/observabilityAppRegistry.js +16 -0
- package/dist/observability/permissions.d.ts +7 -0
- package/dist/observability/permissions.js +60 -0
- package/dist/observability/reviewPanels.d.ts +18 -0
- package/dist/observability/reviewPanels.js +46 -0
- package/dist/observability/routes/datasets.d.ts +7 -0
- package/dist/observability/routes/datasets.js +149 -0
- package/dist/observability/routes/evaluators.d.ts +7 -0
- package/dist/observability/routes/evaluators.js +123 -0
- package/dist/observability/routes/experiments.d.ts +7 -0
- package/dist/observability/routes/experiments.js +95 -0
- package/dist/observability/routes/observabilityRouteAccess.d.ts +13 -0
- package/dist/observability/routes/observabilityRouteAccess.js +67 -0
- package/dist/observability/routes/prompts.d.ts +12 -0
- package/dist/observability/routes/prompts.js +172 -0
- package/dist/observability/routes/requestAiService.d.ts +13 -0
- package/dist/observability/routes/requestAiService.js +21 -0
- package/dist/observability/routes/review.d.ts +7 -0
- package/dist/observability/routes/review.js +79 -0
- package/dist/observability/routes/status.d.ts +5 -0
- package/dist/observability/routes/status.js +31 -0
- package/dist/observability/routes/testMultiStage.d.ts +9 -0
- package/dist/observability/routes/testMultiStage.js +67 -0
- package/dist/observability/routes/traces.d.ts +7 -0
- package/dist/observability/routes/traces.js +129 -0
- package/dist/observability/schemaValidation.d.ts +5 -0
- package/dist/observability/schemaValidation.js +26 -0
- package/dist/observability/sopThresholds.d.ts +2 -0
- package/dist/observability/sopThresholds.js +21 -0
- package/dist/observability/status.d.ts +23 -0
- package/dist/observability/status.js +34 -0
- package/dist/observability/testMultiStageSchemas.d.ts +10 -0
- package/dist/observability/testMultiStageSchemas.js +79 -0
- package/dist/observability/testMultiStageWorkflow.d.ts +20 -0
- package/dist/observability/testMultiStageWorkflow.js +218 -0
- package/dist/observability/types.d.ts +159 -0
- package/dist/observability/types.js +67 -0
- package/dist/routes/files.js +18 -7
- package/dist/routes/gpt.js +79 -689
- package/dist/routes/gptActions.d.ts +9 -0
- package/dist/routes/gptActions.js +212 -0
- package/dist/routes/gptDatasets.d.ts +5 -0
- package/dist/routes/gptDatasets.js +94 -0
- package/dist/routes/gptHistories.d.ts +3 -1
- package/dist/routes/gptHistories.js +188 -2
- package/dist/service/aiDatasets.d.ts +57 -0
- package/dist/service/aiDatasets.js +253 -0
- package/dist/service/aiService.d.ts +50 -1
- package/dist/service/aiService.js +426 -77
- package/dist/service/askFiles.d.ts +49 -0
- package/dist/service/askFiles.js +162 -0
- package/dist/service/asks.d.ts +76 -0
- package/dist/service/asks.js +284 -0
- package/dist/service/chatTurn.d.ts +128 -0
- package/dist/service/chatTurn.js +1674 -0
- package/dist/service/fileStorage.d.ts +4 -0
- package/dist/service/fileStorage.js +7 -2
- package/dist/service/fillActionIds.d.ts +6 -0
- package/dist/service/fillActionIds.js +79 -0
- package/dist/service/jsonFenceModel.d.ts +7 -0
- package/dist/service/jsonFenceModel.js +45 -0
- package/dist/service/prompts.d.ts +52 -0
- package/dist/service/prompts.js +96 -1
- package/dist/service/sanitizeHtml.d.ts +11 -0
- package/dist/service/sanitizeHtml.js +108 -0
- package/dist/types/harness.d.ts +952 -0
- package/dist/types/harness.js +133 -0
- package/dist/types/index.d.ts +192 -14
- package/dist/types/index.js +2 -0
- package/dist/types/observability.d.ts +288 -0
- package/dist/types/observability.js +2 -0
- package/package.json +28 -5
- package/src/aiApp.test.ts +207 -1
- package/src/aiApp.ts +19 -6
- package/src/harness/adminScreens.test.ts +34 -0
- package/src/harness/adminScreens.ts +11 -0
- package/src/harness/agentLoop.test.ts +1034 -0
- package/src/harness/agentLoop.ts +1082 -0
- package/src/harness/agentRecovery.test.ts +380 -0
- package/src/harness/agentTaskNames.ts +4 -0
- package/src/harness/approvalPolicy.ts +28 -0
- package/src/harness/approvals.test.ts +1094 -0
- package/src/harness/approvals.ts +644 -0
- package/src/harness/commit.ts +1049 -0
- package/src/harness/concurrency.test.ts +155 -0
- package/src/harness/conversation.ts +503 -0
- package/src/harness/defineAgent.test.ts +92 -0
- package/src/harness/defineAgent.ts +82 -0
- package/src/harness/defineTask.test.ts +59 -0
- package/src/harness/defineTask.ts +76 -0
- package/src/harness/defineTool.ts +45 -0
- package/src/harness/definitionError.ts +13 -0
- package/src/harness/errors.test.ts +90 -0
- package/src/harness/errors.ts +125 -0
- package/src/harness/eventHub.test.ts +124 -0
- package/src/harness/eventHub.ts +159 -0
- package/src/harness/events.test.ts +769 -0
- package/src/harness/events.ts +384 -0
- package/src/harness/executionEnv.ts +34 -0
- package/src/harness/extensions.test.ts +1218 -0
- package/src/harness/extensions.ts +382 -0
- package/src/harness/harness.test.ts +693 -0
- package/src/harness/harness.ts +778 -0
- package/src/harness/harnessApp.ts +76 -0
- package/src/harness/internalRuntime.ts +44 -0
- package/src/harness/leases.ts +132 -0
- package/src/harness/memo.ts +107 -0
- package/src/harness/modelCall.test.ts +134 -0
- package/src/harness/modelCall.ts +200 -0
- package/src/harness/models/harnessApproval.ts +109 -0
- package/src/harness/models/harnessConversation.ts +142 -0
- package/src/harness/models/harnessEvent.ts +92 -0
- package/src/harness/models/harnessInboxEvent.ts +73 -0
- package/src/harness/models/harnessMemo.ts +43 -0
- package/src/harness/models/harnessMessage.ts +85 -0
- package/src/harness/models/harnessModels.test.ts +60 -0
- package/src/harness/models/harnessOwner.ts +40 -0
- package/src/harness/models/harnessTask.ts +206 -0
- package/src/harness/ownership.test.ts +1087 -0
- package/src/harness/ownership.ts +618 -0
- package/src/harness/recovery.test.ts +907 -0
- package/src/harness/registry.ts +124 -0
- package/src/harness/retries.test.ts +292 -0
- package/src/harness/retryBackoff.test.ts +100 -0
- package/src/harness/retryBackoff.ts +77 -0
- package/src/harness/routes/approvals.ts +123 -0
- package/src/harness/routes/conversations.test.ts +518 -0
- package/src/harness/routes/conversations.ts +102 -0
- package/src/harness/routes/events.ts +280 -0
- package/src/harness/routes/tasks.test.ts +198 -0
- package/src/harness/routes/tasks.ts +146 -0
- package/src/harness/runners/inProcessRunner.test.ts +530 -0
- package/src/harness/runners/inProcessRunner.ts +260 -0
- package/src/harness/runners/jobsRunner.test.ts +709 -0
- package/src/harness/runners/jobsRunner.ts +252 -0
- package/src/harness/runtime.ts +802 -0
- package/src/harness/subagent.test.ts +696 -0
- package/src/harness/subagent.ts +192 -0
- package/src/harness/suspend.ts +13 -0
- package/src/harness/transaction.ts +17 -0
- package/src/harness/versioning.test.ts +288 -0
- package/src/harness/waits.test.ts +693 -0
- package/src/harness/waits.ts +356 -0
- package/src/index.test.ts +13 -0
- package/src/index.ts +70 -0
- package/src/isolated/adminScreensConfig.isolated.ts +143 -0
- package/src/models/aiDataset.test.ts +34 -0
- package/src/models/aiDataset.ts +78 -0
- package/src/models/aiRequest.ts +2 -1
- package/src/models/gptHistory.test.ts +156 -0
- package/src/models/gptHistory.ts +89 -1
- package/src/observability/adminScreens.test.ts +71 -0
- package/src/observability/adminScreens.ts +42 -0
- package/src/observability/compileTemplate.test.ts +14 -0
- package/src/observability/compileTemplate.ts +10 -0
- package/src/observability/datasetImport.test.ts +95 -0
- package/src/observability/datasetImport.ts +220 -0
- package/src/observability/evaluate.test.ts +549 -0
- package/src/observability/evaluate.ts +254 -0
- package/src/observability/evaluatorTemplates.ts +124 -0
- package/src/observability/jsonPath.test.ts +52 -0
- package/src/observability/jsonPath.ts +60 -0
- package/src/observability/local/datasetStore.test.ts +230 -0
- package/src/observability/local/datasetStore.ts +452 -0
- package/src/observability/local/evaluatorStore.test.ts +138 -0
- package/src/observability/local/evaluatorStore.ts +224 -0
- package/src/observability/local/experimentRunner.test.ts +489 -0
- package/src/observability/local/experimentRunner.ts +548 -0
- package/src/observability/local/localPlugin.ts +88 -0
- package/src/observability/local/models/obsDataset.ts +38 -0
- package/src/observability/local/models/obsDatasetItem.ts +80 -0
- package/src/observability/local/models/obsEvaluator.ts +102 -0
- package/src/observability/local/models/obsExperiment.ts +91 -0
- package/src/observability/local/models/obsExperimentItem.ts +52 -0
- package/src/observability/local/models/obsPrompt.ts +43 -0
- package/src/observability/local/models/obsPromptLabel.ts +43 -0
- package/src/observability/local/models/obsPromptVersion.ts +107 -0
- package/src/observability/local/models/obsReviewItem.ts +79 -0
- package/src/observability/local/models/obsScore.ts +59 -0
- package/src/observability/local/models/obsSpan.ts +65 -0
- package/src/observability/local/models/obsTrace.ts +94 -0
- package/src/observability/local/obsModels.test.ts +119 -0
- package/src/observability/local/promptStore.test.ts +351 -0
- package/src/observability/local/promptStore.ts +665 -0
- package/src/observability/local/reviewStore.test.ts +164 -0
- package/src/observability/local/reviewStore.ts +264 -0
- package/src/observability/local/traceStore.test.ts +264 -0
- package/src/observability/local/traceStore.ts +440 -0
- package/src/observability/observabilityApp.test.ts +92 -0
- package/src/observability/observabilityApp.ts +185 -0
- package/src/observability/observabilityAppRegistry.test.ts +30 -0
- package/src/observability/observabilityAppRegistry.ts +35 -0
- package/src/observability/permissions.test.ts +96 -0
- package/src/observability/permissions.ts +78 -0
- package/src/observability/reviewPanels.test.ts +35 -0
- package/src/observability/reviewPanels.ts +64 -0
- package/src/observability/routes/datasets.test.ts +176 -0
- package/src/observability/routes/datasets.ts +252 -0
- package/src/observability/routes/evaluators.test.ts +119 -0
- package/src/observability/routes/evaluators.ts +207 -0
- package/src/observability/routes/experiments.test.ts +282 -0
- package/src/observability/routes/experiments.ts +152 -0
- package/src/observability/routes/observabilityRbac.test.ts +767 -0
- package/src/observability/routes/observabilityRouteAccess.ts +106 -0
- package/src/observability/routes/prompts.test.ts +320 -0
- package/src/observability/routes/prompts.ts +263 -0
- package/src/observability/routes/requestAiService.ts +36 -0
- package/src/observability/routes/review.test.ts +120 -0
- package/src/observability/routes/review.ts +125 -0
- package/src/observability/routes/status.test.ts +116 -0
- package/src/observability/routes/status.ts +47 -0
- package/src/observability/routes/testMultiStage.test.ts +214 -0
- package/src/observability/routes/testMultiStage.ts +96 -0
- package/src/observability/routes/traces.test.ts +167 -0
- package/src/observability/routes/traces.ts +172 -0
- package/src/observability/schemaValidation.ts +29 -0
- package/src/observability/sopThresholds.ts +20 -0
- package/src/observability/status.test.ts +38 -0
- package/src/observability/status.ts +56 -0
- package/src/observability/testMultiStageSchemas.ts +95 -0
- package/src/observability/testMultiStageWorkflow.test.ts +114 -0
- package/src/observability/testMultiStageWorkflow.ts +307 -0
- package/src/observability/types.ts +262 -0
- package/src/routes/files.test.ts +63 -9
- package/src/routes/files.ts +21 -7
- package/src/routes/gpt.test.ts +3279 -0
- package/src/routes/gpt.ts +91 -816
- package/src/routes/gptActions.test.ts +241 -0
- package/src/routes/gptActions.ts +255 -0
- package/src/routes/gptApprovals.test.ts +762 -0
- package/src/routes/gptDatasets.test.ts +86 -0
- package/src/routes/gptDatasets.ts +104 -0
- package/src/routes/gptFiles.test.ts +589 -0
- package/src/routes/gptHistories.test.ts +933 -0
- package/src/routes/gptHistories.ts +266 -5
- package/src/routes/routes.test.ts +222 -2
- package/src/service/aiDatasets.test.ts +123 -0
- package/src/service/aiDatasets.ts +316 -0
- package/src/service/aiService.test.ts +593 -0
- package/src/service/aiService.ts +568 -111
- package/src/service/askFiles.test.ts +96 -0
- package/src/service/askFiles.ts +222 -0
- package/src/service/asks.test.ts +946 -0
- package/src/service/asks.ts +423 -0
- package/src/service/chatTurn.ts +2287 -0
- package/src/service/fileStorage.test.ts +11 -0
- package/src/service/fileStorage.ts +10 -2
- package/src/service/fillActionIds.test.ts +65 -0
- package/src/service/fillActionIds.ts +98 -0
- package/src/service/jsonFenceModel.test.ts +47 -0
- package/src/service/jsonFenceModel.ts +53 -0
- package/src/service/prompts.test.ts +21 -0
- package/src/service/prompts.ts +147 -0
- package/src/service/sanitizeHtml.test.ts +61 -0
- package/src/service/sanitizeHtml.ts +109 -0
- package/src/tests/bunSetup.test.ts +7 -4
- package/src/tests/bunSetup.ts +7 -7
- package/src/tests/chatHarness.ts +478 -0
- package/src/tests/generateStream.ts +49 -0
- package/src/tests/harnessErrors.ts +22 -0
- package/src/tests/harnessStreaming.ts +227 -0
- package/src/tests/helpers.ts +9 -2
- package/src/types/harness.ts +1119 -0
- package/src/types/index.ts +209 -13
- package/src/types/observability.ts +344 -0
- package/src/types/sanitize-html.d.ts +25 -0
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
/** Name of the built-in task that runs one agent turn on a conversation. */
|
|
2
|
+
export declare const AGENT_TURN_TASK_NAME = "terreno.agent.turn";
|
|
3
|
+
/** Name of the built-in task that runs one tool call of a turn. */
|
|
4
|
+
export declare const AGENT_TOOL_TASK_NAME = "terreno.agent.tool";
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AGENT_TOOL_TASK_NAME = exports.AGENT_TURN_TASK_NAME = void 0;
|
|
4
|
+
/** Name of the built-in task that runs one agent turn on a conversation. */
|
|
5
|
+
exports.AGENT_TURN_TASK_NAME = "terreno.agent.turn";
|
|
6
|
+
/** Name of the built-in task that runs one tool call of a turn. */
|
|
7
|
+
exports.AGENT_TOOL_TASK_NAME = "terreno.agent.tool";
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import type { HarnessApprovalPolicy } from "../types/harness";
|
|
2
|
+
/** Validate an approval policy map (`defineTask` / `defineExtension` `approvals`). */
|
|
3
|
+
export declare const assertValidApprovalPolicies: (label: string, approvals: Readonly<Record<string, HarnessApprovalPolicy>> | undefined) => void;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.assertValidApprovalPolicies = void 0;
|
|
4
|
+
const definitionError_1 = require("./definitionError");
|
|
5
|
+
/** Validate an approval policy map (`defineTask` / `defineExtension` `approvals`). */
|
|
6
|
+
const assertValidApprovalPolicies = (label, approvals) => {
|
|
7
|
+
if (approvals === undefined) {
|
|
8
|
+
return;
|
|
9
|
+
}
|
|
10
|
+
if (typeof approvals !== "object" || approvals === null || Array.isArray(approvals)) {
|
|
11
|
+
throw new definitionError_1.HarnessDefinitionError(`${label}: approvals must be an object keyed by approval key`);
|
|
12
|
+
}
|
|
13
|
+
for (const [key, policy] of Object.entries(approvals)) {
|
|
14
|
+
if (!key.trim()) {
|
|
15
|
+
throw new definitionError_1.HarnessDefinitionError(`${label}: approval keys must be non-empty`);
|
|
16
|
+
}
|
|
17
|
+
if (!Array.isArray(policy?.approvers) ||
|
|
18
|
+
policy.approvers.some((approver) => typeof approver !== "function")) {
|
|
19
|
+
throw new definitionError_1.HarnessDefinitionError(`${label}: approvals.${key}.approvers must be an array of permission functions`);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
exports.assertValidApprovalPolicies = assertValidApprovalPolicies;
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { APIError, type RESTMethod, type User } from "@terreno/api";
|
|
2
|
+
import { type Duration, type DurationLike } from "luxon";
|
|
3
|
+
import mongoose from "mongoose";
|
|
4
|
+
import type { AnyHarnessToolDefinition, HarnessApprovalDocument, HarnessApprovalOptions, HarnessApprovalResult, HarnessApprover, HarnessDecideApprovalOptions, HarnessExtensionDefinition, HarnessTaskDefinition, HarnessTaskDocument } from "../types/harness";
|
|
5
|
+
import type { HarnessModels } from "./commit";
|
|
6
|
+
import { type WaitCallPlan } from "./waits";
|
|
7
|
+
/** Event names `harness.sendEvent` refuses: the harness sends these itself. */
|
|
8
|
+
export declare const HARNESS_RESERVED_EVENT_PREFIX = "terreno.";
|
|
9
|
+
/** Who may decide an approval whose definition or extension declares no policy for its key. */
|
|
10
|
+
export declare const HARNESS_DEFAULT_APPROVERS: ReadonlyArray<HarnessApprover>;
|
|
11
|
+
/** The approval can no longer be decided: already decided, expired, or its task ended. */
|
|
12
|
+
export declare class HarnessApprovalConflictError extends APIError {
|
|
13
|
+
constructor(detail: string);
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Check `rt.approval(key, options)` arguments; returns the parsed timeout. Misuse throws
|
|
17
|
+
* `HarnessDefinitionError` (fails the task, no retry).
|
|
18
|
+
*/
|
|
19
|
+
export declare const parseApprovalRequest: ({ key, label, options, }: {
|
|
20
|
+
key: string;
|
|
21
|
+
label: string;
|
|
22
|
+
options: HarnessApprovalOptions;
|
|
23
|
+
}) => Duration | undefined;
|
|
24
|
+
/** One `rt.approval` call as a wait, plus how to read its result once it resolves. */
|
|
25
|
+
export interface ApprovalWaitPlan extends WaitCallPlan {
|
|
26
|
+
result: () => Promise<HarnessApprovalResult>;
|
|
27
|
+
}
|
|
28
|
+
export declare const approvalWaitPlan: ({ callKey, extension, key, models, options, task, timeout, }: {
|
|
29
|
+
callKey: string;
|
|
30
|
+
extension?: string;
|
|
31
|
+
key: string;
|
|
32
|
+
models: HarnessModels;
|
|
33
|
+
options: HarnessApprovalOptions;
|
|
34
|
+
task: HarnessTaskDocument;
|
|
35
|
+
timeout?: Duration;
|
|
36
|
+
}) => ApprovalWaitPlan;
|
|
37
|
+
/** Where approvers are looked up: the registry of the process serving the request. */
|
|
38
|
+
export interface ApprovalRegistry {
|
|
39
|
+
definitions: Map<string, HarnessTaskDefinition>;
|
|
40
|
+
extensions: Map<string, HarnessExtensionDefinition>;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Whether `user` passes every approver of `approval` (AND, like modelRouter). Fails
|
|
44
|
+
* closed: no approvers resolved, an empty list, or an approver that throws all deny.
|
|
45
|
+
*/
|
|
46
|
+
export declare const userMayApprove: ({ approval, method, registry, user, }: {
|
|
47
|
+
approval: HarnessApprovalDocument;
|
|
48
|
+
method: RESTMethod;
|
|
49
|
+
registry: ApprovalRegistry;
|
|
50
|
+
user?: User;
|
|
51
|
+
}) => Promise<boolean>;
|
|
52
|
+
/**
|
|
53
|
+
* Pending, unexpired approvals of live tasks that `user` may approve, oldest first. Every
|
|
54
|
+
* open approval is checked in memory (open sets are small) with `method: "update"`, the
|
|
55
|
+
* method approve / reject use, so the inbox never lists what the caller cannot decide.
|
|
56
|
+
*/
|
|
57
|
+
export declare const listApprovableApprovals: ({ models, registry, user, }: {
|
|
58
|
+
models: HarnessModels;
|
|
59
|
+
registry: ApprovalRegistry;
|
|
60
|
+
user?: User;
|
|
61
|
+
}) => Promise<HarnessApprovalDocument[]>;
|
|
62
|
+
/**
|
|
63
|
+
* Decide a pending approval: in one transaction, set its status, `decidedBy`, `decidedAt`,
|
|
64
|
+
* and `reason`, send its event to the waiting task (waking it), and write the
|
|
65
|
+
* `approval:<key>` span. Throws `HarnessApprovalConflictError` when it is no longer
|
|
66
|
+
* pending, has expired, or its task ended. Does not check approvers.
|
|
67
|
+
*/
|
|
68
|
+
export declare const decideApprovalRecords: ({ approvalId, models, options, }: {
|
|
69
|
+
approvalId: mongoose.Types.ObjectId | string;
|
|
70
|
+
models: HarnessModels;
|
|
71
|
+
options: HarnessDecideApprovalOptions;
|
|
72
|
+
}) => Promise<{
|
|
73
|
+
approval: HarnessApprovalDocument;
|
|
74
|
+
isWoken: boolean;
|
|
75
|
+
}>;
|
|
76
|
+
/** The input of the task that requested `approval`, for approver checks that need it. */
|
|
77
|
+
export declare const approvalTaskInput: <In = unknown>(approval: Pick<HarnessApprovalDocument, "taskId">) => Promise<In>;
|
|
78
|
+
export interface ApprovalGateOptions {
|
|
79
|
+
/** Who may approve calls of these tools. Default `[Permissions.IsAdmin]`. */
|
|
80
|
+
approvers?: ReadonlyArray<HarnessApprover>;
|
|
81
|
+
/** Extension name; default `approvalGate:<tool names, comma-separated>`. */
|
|
82
|
+
name?: string;
|
|
83
|
+
/** Called once per approval after it is stored (see `rt.approval`). */
|
|
84
|
+
notify?: HarnessApprovalOptions["notify"];
|
|
85
|
+
/** Expire the approval (blocking the call) when nobody decides within this long. */
|
|
86
|
+
timeout?: DurationLike;
|
|
87
|
+
/** Approval title; default `Run tool "<name>"`. */
|
|
88
|
+
title?: string | ((call: {
|
|
89
|
+
args: unknown;
|
|
90
|
+
toolName: string;
|
|
91
|
+
}) => string);
|
|
92
|
+
/** Tools (or tool names) that need an approval before each call. */
|
|
93
|
+
tools: ReadonlyArray<AnyHarnessToolDefinition | string>;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* An extension whose `beforeTool` hook requires a human approval before each call of the
|
|
97
|
+
* named tools. The approval's key is the tool name and its payload `{toolName, toolCallId,
|
|
98
|
+
* args}`. The decision is memoized by `toolCallId` in the turn, so it survives restarts and
|
|
99
|
+
* re-runs of the call. A rejection or expiry blocks the call; the reason reaches the model.
|
|
100
|
+
*/
|
|
101
|
+
export declare const approvalGate: (options: ApprovalGateOptions) => HarnessExtensionDefinition;
|
|
@@ -0,0 +1,468 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.approvalGate = exports.approvalTaskInput = exports.decideApprovalRecords = exports.listApprovableApprovals = exports.userMayApprove = exports.approvalWaitPlan = exports.parseApprovalRequest = exports.HarnessApprovalConflictError = exports.HARNESS_DEFAULT_APPROVERS = exports.HARNESS_RESERVED_EVENT_PREFIX = void 0;
|
|
7
|
+
const api_1 = require("@terreno/api");
|
|
8
|
+
const luxon_1 = require("luxon");
|
|
9
|
+
const mongoose_1 = __importDefault(require("mongoose"));
|
|
10
|
+
const harness_1 = require("../types/harness");
|
|
11
|
+
const defineTask_1 = require("./defineTask");
|
|
12
|
+
const definitionError_1 = require("./definitionError");
|
|
13
|
+
const errors_1 = require("./errors");
|
|
14
|
+
const events_1 = require("./events");
|
|
15
|
+
const extensions_1 = require("./extensions");
|
|
16
|
+
const harnessTask_1 = require("./models/harnessTask");
|
|
17
|
+
const transaction_1 = require("./transaction");
|
|
18
|
+
const waits_1 = require("./waits");
|
|
19
|
+
/** Event names `harness.sendEvent` refuses: the harness sends these itself. */
|
|
20
|
+
exports.HARNESS_RESERVED_EVENT_PREFIX = "terreno.";
|
|
21
|
+
/** Prefix of the inbox event a decision sends: `terreno.approval:<key>:<step>:<n>`. */
|
|
22
|
+
const APPROVAL_EVENT_PREFIX = `${exports.HARNESS_RESERVED_EVENT_PREFIX}approval`;
|
|
23
|
+
/** Who may decide an approval whose definition or extension declares no policy for its key. */
|
|
24
|
+
exports.HARNESS_DEFAULT_APPROVERS = Object.freeze([
|
|
25
|
+
api_1.Permissions.IsAdmin,
|
|
26
|
+
]);
|
|
27
|
+
/** The approval can no longer be decided: already decided, expired, or its task ended. */
|
|
28
|
+
class HarnessApprovalConflictError extends api_1.APIError {
|
|
29
|
+
constructor(detail) {
|
|
30
|
+
super({ ...errors_1.HARNESS_ERRORS.approvalNotPending, detail });
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
exports.HarnessApprovalConflictError = HarnessApprovalConflictError;
|
|
34
|
+
const toObjectId = (id) => {
|
|
35
|
+
if (id === undefined) {
|
|
36
|
+
return undefined;
|
|
37
|
+
}
|
|
38
|
+
return typeof id === "string" ? new mongoose_1.default.Types.ObjectId(id) : id;
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* Check `rt.approval(key, options)` arguments; returns the parsed timeout. Misuse throws
|
|
42
|
+
* `HarnessDefinitionError` (fails the task, no retry).
|
|
43
|
+
*/
|
|
44
|
+
const parseApprovalRequest = ({ key, label, options, }) => {
|
|
45
|
+
if (typeof key !== "string" || !key.trim()) {
|
|
46
|
+
throw new definitionError_1.HarnessDefinitionError(`${label}: rt.approval requires a key`);
|
|
47
|
+
}
|
|
48
|
+
if (typeof options?.title !== "string" || !options.title.trim()) {
|
|
49
|
+
throw new definitionError_1.HarnessDefinitionError(`${label}: rt.approval("${key}") requires a title`);
|
|
50
|
+
}
|
|
51
|
+
if (options.summary !== undefined && typeof options.summary !== "string") {
|
|
52
|
+
throw new definitionError_1.HarnessDefinitionError(`${label}: rt.approval("${key}") summary must be a string`);
|
|
53
|
+
}
|
|
54
|
+
if ("approvers" in options) {
|
|
55
|
+
throw new definitionError_1.HarnessDefinitionError(`${label}: rt.approval("${key}") does not take approvers; declare them in defineTask({approvals: {"${key}": {approvers}}}) so every process can resolve them`);
|
|
56
|
+
}
|
|
57
|
+
if (options.notify !== undefined && typeof options.notify !== "function") {
|
|
58
|
+
throw new definitionError_1.HarnessDefinitionError(`${label}: rt.approval("${key}") notify must be a function`);
|
|
59
|
+
}
|
|
60
|
+
if (options.timeout === undefined) {
|
|
61
|
+
return undefined;
|
|
62
|
+
}
|
|
63
|
+
const timeout = (0, waits_1.parseWaitDuration)(options.timeout, `${label}: rt.approval("${key}") timeout`);
|
|
64
|
+
if (timeout.toMillis() <= 0) {
|
|
65
|
+
throw new definitionError_1.HarnessDefinitionError(`${label}: rt.approval("${key}") timeout must be positive`);
|
|
66
|
+
}
|
|
67
|
+
return timeout;
|
|
68
|
+
};
|
|
69
|
+
exports.parseApprovalRequest = parseApprovalRequest;
|
|
70
|
+
/** The `approval:<key>` CHAIN span recording a decision or an expiry. */
|
|
71
|
+
const createApprovalSpan = async ({ approval, decidedBy, decision, endedAt, models, reason, session, task, traceStartedAt, }) => {
|
|
72
|
+
const startedAt = luxon_1.DateTime.fromJSDate(approval.created);
|
|
73
|
+
await models.span.create([
|
|
74
|
+
{
|
|
75
|
+
durationMs: endedAt.diff(startedAt).toMillis(),
|
|
76
|
+
endedAt: endedAt.toJSDate(),
|
|
77
|
+
input: {
|
|
78
|
+
approvalId: String(approval._id),
|
|
79
|
+
definitionKey: approval.definitionKey,
|
|
80
|
+
title: approval.title,
|
|
81
|
+
},
|
|
82
|
+
kind: "CHAIN",
|
|
83
|
+
name: `approval:${approval.key}`,
|
|
84
|
+
output: {
|
|
85
|
+
decision,
|
|
86
|
+
...(decidedBy === undefined ? {} : { decidedBy }),
|
|
87
|
+
...(reason === undefined ? {} : { reason }),
|
|
88
|
+
},
|
|
89
|
+
parentSpanId: task.rootSpanId,
|
|
90
|
+
startedAt: startedAt.toJSDate(),
|
|
91
|
+
startOffsetMs: startedAt.diff(traceStartedAt).toMillis(),
|
|
92
|
+
status: "ok",
|
|
93
|
+
traceId: task.traceId,
|
|
94
|
+
},
|
|
95
|
+
], { session });
|
|
96
|
+
};
|
|
97
|
+
/** What `rt.approval` returns for a settled approval. */
|
|
98
|
+
const approvalResult = (approval) => {
|
|
99
|
+
const approvalId = String(approval._id);
|
|
100
|
+
if (approval.status === harness_1.HARNESS_APPROVAL_STATUSES.expired) {
|
|
101
|
+
return { approvalId, approved: false, expired: true };
|
|
102
|
+
}
|
|
103
|
+
if (approval.status === harness_1.HARNESS_APPROVAL_STATUSES.pending) {
|
|
104
|
+
// Only a decision may wake an approval wait; a hand-sent event must not approve.
|
|
105
|
+
throw (0, errors_1.harnessError)({
|
|
106
|
+
detail: `Approval ${approvalId} received its event without a recorded decision`,
|
|
107
|
+
kind: "internal",
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
const result = {
|
|
111
|
+
approvalId,
|
|
112
|
+
approved: approval.status === harness_1.HARNESS_APPROVAL_STATUSES.approved,
|
|
113
|
+
};
|
|
114
|
+
const decidedAt = approval.decidedAt ? luxon_1.DateTime.fromJSDate(approval.decidedAt).toISO() : null;
|
|
115
|
+
if (decidedAt) {
|
|
116
|
+
result.decidedAt = decidedAt;
|
|
117
|
+
}
|
|
118
|
+
if (approval.decidedBy) {
|
|
119
|
+
result.decidedBy = String(approval.decidedBy);
|
|
120
|
+
}
|
|
121
|
+
if (approval.reason !== undefined) {
|
|
122
|
+
result.reason = approval.reason;
|
|
123
|
+
}
|
|
124
|
+
return result;
|
|
125
|
+
};
|
|
126
|
+
/**
|
|
127
|
+
* Build the wait behind one `rt.approval` call. Parking upserts the `HarnessApproval`
|
|
128
|
+
* (unique per task and call key) in the waiting commit, so a re-run of the phase visit
|
|
129
|
+
* finds the same approval. A decision sends the approval's own event; a timeout marks
|
|
130
|
+
* it `expired` and writes its span in the timeout's transaction.
|
|
131
|
+
*/
|
|
132
|
+
/** What `approval.*` events carry: enough to render the request and its decision. */
|
|
133
|
+
const approvalEventPayload = (approval) => ({
|
|
134
|
+
approvalId: String(approval._id),
|
|
135
|
+
decidedAt: approval.decidedAt?.toISOString(),
|
|
136
|
+
decidedBy: approval.decidedBy ? String(approval.decidedBy) : undefined,
|
|
137
|
+
expiresAt: approval.expiresAt?.toISOString(),
|
|
138
|
+
key: approval.key,
|
|
139
|
+
reason: approval.reason,
|
|
140
|
+
status: approval.status,
|
|
141
|
+
summary: approval.summary,
|
|
142
|
+
taskId: String(approval.taskId),
|
|
143
|
+
title: approval.title,
|
|
144
|
+
});
|
|
145
|
+
const approvalWaitPlan = ({ callKey, extension, key, models, options, task, timeout, }) => {
|
|
146
|
+
const filter = { callKey, taskId: task._id };
|
|
147
|
+
let isInserted = false;
|
|
148
|
+
return {
|
|
149
|
+
afterPark: async () => {
|
|
150
|
+
if (!isInserted || !options.notify) {
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
try {
|
|
154
|
+
await options.notify(await models.approval.findExactlyOne(filter));
|
|
155
|
+
}
|
|
156
|
+
catch (error) {
|
|
157
|
+
api_1.logger.error(`Harness approval "${key}" of task ${task._id}: notify failed: ${(0, errors_1.errorMessage)(error)}`);
|
|
158
|
+
}
|
|
159
|
+
},
|
|
160
|
+
onPark: async ({ session, task: parked }) => {
|
|
161
|
+
const now = luxon_1.DateTime.now().toJSDate();
|
|
162
|
+
const written = await models.approval.updateOne(filter, {
|
|
163
|
+
// createdUpdatedPlugin's updateOne hook sets `updated`.
|
|
164
|
+
$setOnInsert: {
|
|
165
|
+
callKey,
|
|
166
|
+
created: now,
|
|
167
|
+
definitionKey: `${(0, defineTask_1.taskDefinitionKey)(task)}:${key}`,
|
|
168
|
+
event: `${APPROVAL_EVENT_PREFIX}:${key}:${callKey}`,
|
|
169
|
+
expiresAt: parked.waits?.[callKey]?.timeoutAt,
|
|
170
|
+
...(extension === undefined ? {} : { extension }),
|
|
171
|
+
key,
|
|
172
|
+
payload: options.payload,
|
|
173
|
+
rootTaskId: task.rootTaskId,
|
|
174
|
+
status: harness_1.HARNESS_APPROVAL_STATUSES.pending,
|
|
175
|
+
...(options.summary === undefined ? {} : { summary: options.summary }),
|
|
176
|
+
taskId: task._id,
|
|
177
|
+
title: options.title,
|
|
178
|
+
traceId: task.traceId,
|
|
179
|
+
},
|
|
180
|
+
}, { session, upsert: true });
|
|
181
|
+
isInserted = written.upsertedCount > 0;
|
|
182
|
+
if (isInserted) {
|
|
183
|
+
const [approval] = await models.approval.find(filter, null, { session });
|
|
184
|
+
await (0, events_1.appendTaskEvents)({
|
|
185
|
+
events: [
|
|
186
|
+
{
|
|
187
|
+
payload: approval ? approvalEventPayload(approval) : { key },
|
|
188
|
+
type: harness_1.HARNESS_EVENT_TYPES.approvalRequested,
|
|
189
|
+
},
|
|
190
|
+
],
|
|
191
|
+
models,
|
|
192
|
+
session,
|
|
193
|
+
task,
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
},
|
|
197
|
+
request: {
|
|
198
|
+
duration: timeout,
|
|
199
|
+
event: `${APPROVAL_EVENT_PREFIX}:${key}:${callKey}`,
|
|
200
|
+
kind: harness_1.HARNESS_WAIT_KINDS.event,
|
|
201
|
+
timeoutWrites: async ({ session, task: current, traceStartedAt }) => {
|
|
202
|
+
const expired = await models.approval.findOneAndUpdate({ ...filter, status: harness_1.HARNESS_APPROVAL_STATUSES.pending }, {
|
|
203
|
+
$set: {
|
|
204
|
+
status: harness_1.HARNESS_APPROVAL_STATUSES.expired,
|
|
205
|
+
updated: luxon_1.DateTime.now().toJSDate(),
|
|
206
|
+
},
|
|
207
|
+
}, { returnDocument: "after", session });
|
|
208
|
+
if (!expired) {
|
|
209
|
+
// A decision committed first; its event is in the inbox.
|
|
210
|
+
throw new waits_1.HarnessWaitRaceError();
|
|
211
|
+
}
|
|
212
|
+
await (0, events_1.appendTaskEvents)({
|
|
213
|
+
events: [
|
|
214
|
+
{ payload: approvalEventPayload(expired), type: harness_1.HARNESS_EVENT_TYPES.approvalDecided },
|
|
215
|
+
],
|
|
216
|
+
models,
|
|
217
|
+
session,
|
|
218
|
+
task: current,
|
|
219
|
+
});
|
|
220
|
+
await createApprovalSpan({
|
|
221
|
+
approval: expired,
|
|
222
|
+
decision: "expired",
|
|
223
|
+
endedAt: luxon_1.DateTime.now(),
|
|
224
|
+
models,
|
|
225
|
+
session,
|
|
226
|
+
task: current,
|
|
227
|
+
traceStartedAt,
|
|
228
|
+
});
|
|
229
|
+
},
|
|
230
|
+
},
|
|
231
|
+
result: async () => approvalResult(await models.approval.findExactlyOne(filter)),
|
|
232
|
+
};
|
|
233
|
+
};
|
|
234
|
+
exports.approvalWaitPlan = approvalWaitPlan;
|
|
235
|
+
/**
|
|
236
|
+
* The approvers of `approval`, from the extension that requested it or else its task
|
|
237
|
+
* definition (`name@version`). A registered owner without a policy for the key gets
|
|
238
|
+
* `HARNESS_DEFAULT_APPROVERS`; an owner this registry lacks gets `undefined` (nobody).
|
|
239
|
+
*/
|
|
240
|
+
const resolveApprovers = ({ approval, registry, }) => {
|
|
241
|
+
if (approval.extension !== undefined) {
|
|
242
|
+
const extension = registry.extensions.get(approval.extension);
|
|
243
|
+
if (!extension) {
|
|
244
|
+
return undefined;
|
|
245
|
+
}
|
|
246
|
+
return extension.approvals[approval.key]?.approvers ?? exports.HARNESS_DEFAULT_APPROVERS;
|
|
247
|
+
}
|
|
248
|
+
const suffix = `:${approval.key}`;
|
|
249
|
+
if (!approval.definitionKey.endsWith(suffix)) {
|
|
250
|
+
return undefined;
|
|
251
|
+
}
|
|
252
|
+
const definition = registry.definitions.get(approval.definitionKey.slice(0, -suffix.length));
|
|
253
|
+
if (!definition) {
|
|
254
|
+
return undefined;
|
|
255
|
+
}
|
|
256
|
+
return definition.approvals?.[approval.key]?.approvers ?? exports.HARNESS_DEFAULT_APPROVERS;
|
|
257
|
+
};
|
|
258
|
+
/**
|
|
259
|
+
* Whether `user` passes every approver of `approval` (AND, like modelRouter). Fails
|
|
260
|
+
* closed: no approvers resolved, an empty list, or an approver that throws all deny.
|
|
261
|
+
*/
|
|
262
|
+
const userMayApprove = async ({ approval, method, registry, user, }) => {
|
|
263
|
+
const approvers = resolveApprovers({ approval, registry });
|
|
264
|
+
if (!approvers || approvers.length === 0) {
|
|
265
|
+
return false;
|
|
266
|
+
}
|
|
267
|
+
try {
|
|
268
|
+
return await (0, api_1.checkPermissions)(method, [...approvers], user, approval);
|
|
269
|
+
}
|
|
270
|
+
catch (error) {
|
|
271
|
+
api_1.logger.warn(`Harness approval ${approval._id}: an approver threw; denying: ${(0, errors_1.errorMessage)(error)}`);
|
|
272
|
+
return false;
|
|
273
|
+
}
|
|
274
|
+
};
|
|
275
|
+
exports.userMayApprove = userMayApprove;
|
|
276
|
+
const isOpenFilter = (now) => ({
|
|
277
|
+
$or: [{ expiresAt: { $exists: false } }, { expiresAt: null }, { expiresAt: { $gt: now } }],
|
|
278
|
+
status: harness_1.HARNESS_APPROVAL_STATUSES.pending,
|
|
279
|
+
});
|
|
280
|
+
/**
|
|
281
|
+
* Pending, unexpired approvals of live tasks that `user` may approve, oldest first. Every
|
|
282
|
+
* open approval is checked in memory (open sets are small) with `method: "update"`, the
|
|
283
|
+
* method approve / reject use, so the inbox never lists what the caller cannot decide.
|
|
284
|
+
*/
|
|
285
|
+
const listApprovableApprovals = async ({ models, registry, user, }) => {
|
|
286
|
+
const open = await models.approval
|
|
287
|
+
.find(isOpenFilter(luxon_1.DateTime.now().toJSDate()))
|
|
288
|
+
.sort({ _id: 1, created: 1 });
|
|
289
|
+
if (open.length === 0) {
|
|
290
|
+
return [];
|
|
291
|
+
}
|
|
292
|
+
const liveTasks = await models.task
|
|
293
|
+
.find({
|
|
294
|
+
_id: { $in: [...new Set(open.map((approval) => String(approval.taskId)))] },
|
|
295
|
+
status: { $nin: [...harness_1.HARNESS_TERMINAL_STATUSES] },
|
|
296
|
+
}, { _id: 1 })
|
|
297
|
+
.lean();
|
|
298
|
+
const live = new Set(liveTasks.map((task) => String(task._id)));
|
|
299
|
+
const approvable = [];
|
|
300
|
+
for (const approval of open) {
|
|
301
|
+
if (!live.has(String(approval.taskId))) {
|
|
302
|
+
continue;
|
|
303
|
+
}
|
|
304
|
+
if (await (0, exports.userMayApprove)({ approval, method: "update", registry, user })) {
|
|
305
|
+
approvable.push(approval);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
return approvable;
|
|
309
|
+
};
|
|
310
|
+
exports.listApprovableApprovals = listApprovableApprovals;
|
|
311
|
+
/**
|
|
312
|
+
* Decide a pending approval: in one transaction, set its status, `decidedBy`, `decidedAt`,
|
|
313
|
+
* and `reason`, send its event to the waiting task (waking it), and write the
|
|
314
|
+
* `approval:<key>` span. Throws `HarnessApprovalConflictError` when it is no longer
|
|
315
|
+
* pending, has expired, or its task ended. Does not check approvers.
|
|
316
|
+
*/
|
|
317
|
+
const decideApprovalRecords = async ({ approvalId, models, options, }) => {
|
|
318
|
+
const approval = await models.approval.findExactlyOne({ _id: approvalId });
|
|
319
|
+
const now = luxon_1.DateTime.now();
|
|
320
|
+
const notDecidable = (why) => new HarnessApprovalConflictError(`Approval ${approvalId} ${why}`);
|
|
321
|
+
if (approval.status !== harness_1.HARNESS_APPROVAL_STATUSES.pending) {
|
|
322
|
+
throw notDecidable(`is already ${approval.status}`);
|
|
323
|
+
}
|
|
324
|
+
if (approval.expiresAt && luxon_1.DateTime.fromJSDate(approval.expiresAt) <= now) {
|
|
325
|
+
throw notDecidable("has expired");
|
|
326
|
+
}
|
|
327
|
+
const task = await models.task.findExactlyOne({ _id: approval.taskId });
|
|
328
|
+
if (harness_1.HARNESS_TERMINAL_STATUSES.has(task.status)) {
|
|
329
|
+
throw notDecidable(`belongs to a task that is already ${task.status}`);
|
|
330
|
+
}
|
|
331
|
+
const trace = await models.trace.findExactlyOne({ _id: task.traceId });
|
|
332
|
+
const status = options.approved
|
|
333
|
+
? harness_1.HARNESS_APPROVAL_STATUSES.approved
|
|
334
|
+
: harness_1.HARNESS_APPROVAL_STATUSES.rejected;
|
|
335
|
+
const decidedBy = toObjectId(options.userId);
|
|
336
|
+
const reason = options.reason?.trim() ? options.reason.trim() : undefined;
|
|
337
|
+
try {
|
|
338
|
+
return await (0, transaction_1.inTransaction)(async (session) => {
|
|
339
|
+
const decided = await models.approval.findOneAndUpdate({ _id: approval._id, ...isOpenFilter(now.toJSDate()) }, {
|
|
340
|
+
$set: {
|
|
341
|
+
decidedAt: now.toJSDate(),
|
|
342
|
+
status,
|
|
343
|
+
updated: now.toJSDate(),
|
|
344
|
+
...(decidedBy ? { decidedBy } : {}),
|
|
345
|
+
...(reason === undefined ? {} : { reason }),
|
|
346
|
+
},
|
|
347
|
+
}, { returnDocument: "after", session });
|
|
348
|
+
if (!decided) {
|
|
349
|
+
throw notDecidable("is no longer pending");
|
|
350
|
+
}
|
|
351
|
+
await (0, events_1.appendTaskEvents)({
|
|
352
|
+
events: [
|
|
353
|
+
{ payload: approvalEventPayload(decided), type: harness_1.HARNESS_EVENT_TYPES.approvalDecided },
|
|
354
|
+
],
|
|
355
|
+
models,
|
|
356
|
+
session,
|
|
357
|
+
task,
|
|
358
|
+
});
|
|
359
|
+
const sent = await (0, waits_1.appendInboxEvent)({
|
|
360
|
+
event: approval.event,
|
|
361
|
+
models,
|
|
362
|
+
payload: {
|
|
363
|
+
approvalId: String(approval._id),
|
|
364
|
+
approved: options.approved,
|
|
365
|
+
decidedAt: now.toISO(),
|
|
366
|
+
...(decidedBy ? { decidedBy: String(decidedBy) } : {}),
|
|
367
|
+
...(reason === undefined ? {} : { reason }),
|
|
368
|
+
},
|
|
369
|
+
session,
|
|
370
|
+
taskId: task._id,
|
|
371
|
+
});
|
|
372
|
+
await createApprovalSpan({
|
|
373
|
+
approval: decided,
|
|
374
|
+
decidedBy: decidedBy ? String(decidedBy) : undefined,
|
|
375
|
+
decision: status,
|
|
376
|
+
endedAt: now,
|
|
377
|
+
models,
|
|
378
|
+
reason,
|
|
379
|
+
session,
|
|
380
|
+
task,
|
|
381
|
+
traceStartedAt: luxon_1.DateTime.fromJSDate(trace.startedAt),
|
|
382
|
+
});
|
|
383
|
+
return { approval: decided, isWoken: sent.isWoken };
|
|
384
|
+
});
|
|
385
|
+
}
|
|
386
|
+
catch (error) {
|
|
387
|
+
if (error instanceof HarnessApprovalConflictError) {
|
|
388
|
+
throw error;
|
|
389
|
+
}
|
|
390
|
+
// The task finished between the check above and the transaction.
|
|
391
|
+
const current = await models.task.findExactlyOne({ _id: task._id });
|
|
392
|
+
if (harness_1.HARNESS_TERMINAL_STATUSES.has(current.status)) {
|
|
393
|
+
throw notDecidable(`belongs to a task that is already ${current.status}`);
|
|
394
|
+
}
|
|
395
|
+
throw error;
|
|
396
|
+
}
|
|
397
|
+
};
|
|
398
|
+
exports.decideApprovalRecords = decideApprovalRecords;
|
|
399
|
+
/** The input of the task that requested `approval`, for approver checks that need it. */
|
|
400
|
+
const approvalTaskInput = async (approval) => {
|
|
401
|
+
const task = await (0, harnessTask_1.registerHarnessTask)().findExactlyOne({ _id: approval.taskId });
|
|
402
|
+
return task.input;
|
|
403
|
+
};
|
|
404
|
+
exports.approvalTaskInput = approvalTaskInput;
|
|
405
|
+
/**
|
|
406
|
+
* An extension whose `beforeTool` hook requires a human approval before each call of the
|
|
407
|
+
* named tools. The approval's key is the tool name and its payload `{toolName, toolCallId,
|
|
408
|
+
* args}`. The decision is memoized by `toolCallId` in the turn, so it survives restarts and
|
|
409
|
+
* re-runs of the call. A rejection or expiry blocks the call; the reason reaches the model.
|
|
410
|
+
*/
|
|
411
|
+
const approvalGate = (options) => {
|
|
412
|
+
if (!Array.isArray(options?.tools) || options.tools.length === 0) {
|
|
413
|
+
throw new definitionError_1.HarnessDefinitionError("approvalGate: tools must list at least one tool");
|
|
414
|
+
}
|
|
415
|
+
const toolNames = options.tools.map((tool) => (typeof tool === "string" ? tool : tool?.name));
|
|
416
|
+
if (toolNames.some((name) => typeof name !== "string" || !name.trim())) {
|
|
417
|
+
throw new definitionError_1.HarnessDefinitionError("approvalGate: every tool must be a defineTool tool or a tool name");
|
|
418
|
+
}
|
|
419
|
+
const gated = new Set(toolNames);
|
|
420
|
+
const name = options.name ?? `approvalGate:${toolNames.join(",")}`;
|
|
421
|
+
const approvers = options.approvers ?? exports.HARNESS_DEFAULT_APPROVERS;
|
|
422
|
+
if (options.title !== undefined && !["function", "string"].includes(typeof options.title)) {
|
|
423
|
+
throw new definitionError_1.HarnessDefinitionError(`approvalGate(${name}): title must be a string or a function`);
|
|
424
|
+
}
|
|
425
|
+
if (options.timeout !== undefined) {
|
|
426
|
+
(0, waits_1.parseWaitDuration)(options.timeout, `approvalGate(${name}): timeout`);
|
|
427
|
+
}
|
|
428
|
+
return (0, extensions_1.defineExtension)({
|
|
429
|
+
approvals: Object.fromEntries([...gated].map((toolName) => [toolName, { approvers }])),
|
|
430
|
+
hooks: [
|
|
431
|
+
(0, extensions_1.hook)("beforeTool", async (call, api) => {
|
|
432
|
+
if (!gated.has(call.toolName)) {
|
|
433
|
+
return undefined;
|
|
434
|
+
}
|
|
435
|
+
const memoKey = `approvalGate:${name}:${call.toolCallId}`;
|
|
436
|
+
let decision = await api.memo(memoKey);
|
|
437
|
+
if (!decision) {
|
|
438
|
+
const title = typeof options.title === "function"
|
|
439
|
+
? options.title({ args: call.args, toolName: call.toolName })
|
|
440
|
+
: (options.title ?? `Run tool "${call.toolName}"`);
|
|
441
|
+
const result = await api.approval(call.toolName, {
|
|
442
|
+
notify: options.notify,
|
|
443
|
+
payload: { args: call.args, toolCallId: call.toolCallId, toolName: call.toolName },
|
|
444
|
+
timeout: options.timeout,
|
|
445
|
+
title,
|
|
446
|
+
});
|
|
447
|
+
decision = await api.memo(memoKey, {
|
|
448
|
+
approved: result.approved,
|
|
449
|
+
...(result.decidedBy === undefined ? {} : { decidedBy: result.decidedBy }),
|
|
450
|
+
...(result.expired ? { expired: true } : {}),
|
|
451
|
+
...(result.reason === undefined ? {} : { reason: result.reason }),
|
|
452
|
+
});
|
|
453
|
+
}
|
|
454
|
+
if (decision.approved) {
|
|
455
|
+
return undefined;
|
|
456
|
+
}
|
|
457
|
+
if (decision.expired) {
|
|
458
|
+
return { block: `Approval to run "${call.toolName}" expired before anyone decided` };
|
|
459
|
+
}
|
|
460
|
+
return {
|
|
461
|
+
block: `Approval to run "${call.toolName}" was rejected: ${decision.reason ?? "no reason given"}`,
|
|
462
|
+
};
|
|
463
|
+
}),
|
|
464
|
+
],
|
|
465
|
+
name,
|
|
466
|
+
});
|
|
467
|
+
};
|
|
468
|
+
exports.approvalGate = approvalGate;
|