@terreno/ai 57.8.0 → 57.10.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.cursorrules +2 -2
- package/README.md +2 -1
- package/bunfig.toml +1 -1
- package/dist/aiApp.d.ts +16 -5
- package/dist/aiApp.js +17 -7
- package/dist/harness/adminScreens.d.ts +6 -0
- package/dist/harness/adminScreens.js +11 -0
- package/dist/harness/agentLoop.d.ts +61 -0
- package/dist/harness/agentLoop.js +798 -0
- package/dist/harness/agentTaskNames.d.ts +4 -0
- package/dist/harness/agentTaskNames.js +7 -0
- package/dist/harness/approvalPolicy.d.ts +3 -0
- package/dist/harness/approvalPolicy.js +23 -0
- package/dist/harness/approvals.d.ts +101 -0
- package/dist/harness/approvals.js +468 -0
- package/dist/harness/commit.d.ts +238 -0
- package/dist/harness/commit.js +678 -0
- package/dist/harness/conversation.d.ts +75 -0
- package/dist/harness/conversation.js +397 -0
- package/dist/harness/defineAgent.d.ts +9 -0
- package/dist/harness/defineAgent.js +65 -0
- package/dist/harness/defineTask.d.ts +11 -0
- package/dist/harness/defineTask.js +59 -0
- package/dist/harness/defineTool.d.ts +7 -0
- package/dist/harness/defineTool.js +36 -0
- package/dist/harness/definitionError.d.ts +8 -0
- package/dist/harness/definitionError.js +15 -0
- package/dist/harness/errors.d.ts +127 -0
- package/dist/harness/errors.js +101 -0
- package/dist/harness/eventHub.d.ts +45 -0
- package/dist/harness/eventHub.js +135 -0
- package/dist/harness/events.d.ts +95 -0
- package/dist/harness/events.js +267 -0
- package/dist/harness/executionEnv.d.ts +31 -0
- package/dist/harness/executionEnv.js +2 -0
- package/dist/harness/extensions.d.ts +82 -0
- package/dist/harness/extensions.js +274 -0
- package/dist/harness/harness.d.ts +163 -0
- package/dist/harness/harness.js +520 -0
- package/dist/harness/harnessApp.d.ts +44 -0
- package/dist/harness/harnessApp.js +62 -0
- package/dist/harness/internalRuntime.d.ts +16 -0
- package/dist/harness/internalRuntime.js +18 -0
- package/dist/harness/leases.d.ts +35 -0
- package/dist/harness/leases.js +86 -0
- package/dist/harness/memo.d.ts +24 -0
- package/dist/harness/memo.js +78 -0
- package/dist/harness/modelCall.d.ts +45 -0
- package/dist/harness/modelCall.js +147 -0
- package/dist/harness/models/harnessApproval.d.ts +2 -0
- package/dist/harness/models/harnessApproval.js +100 -0
- package/dist/harness/models/harnessConversation.d.ts +2 -0
- package/dist/harness/models/harnessConversation.js +117 -0
- package/dist/harness/models/harnessEvent.d.ts +3 -0
- package/dist/harness/models/harnessEvent.js +73 -0
- package/dist/harness/models/harnessInboxEvent.d.ts +2 -0
- package/dist/harness/models/harnessInboxEvent.js +58 -0
- package/dist/harness/models/harnessMemo.d.ts +2 -0
- package/dist/harness/models/harnessMemo.js +42 -0
- package/dist/harness/models/harnessMessage.d.ts +2 -0
- package/dist/harness/models/harnessMessage.js +73 -0
- package/dist/harness/models/harnessOwner.d.ts +2 -0
- package/dist/harness/models/harnessOwner.js +36 -0
- package/dist/harness/models/harnessTask.d.ts +2 -0
- package/dist/harness/models/harnessTask.js +186 -0
- package/dist/harness/ownership.d.ts +89 -0
- package/dist/harness/ownership.js +438 -0
- package/dist/harness/registry.d.ts +29 -0
- package/dist/harness/registry.js +93 -0
- package/dist/harness/retryBackoff.d.ts +31 -0
- package/dist/harness/retryBackoff.js +50 -0
- package/dist/harness/routes/approvals.d.ts +13 -0
- package/dist/harness/routes/approvals.js +93 -0
- package/dist/harness/routes/conversations.d.ts +11 -0
- package/dist/harness/routes/conversations.js +66 -0
- package/dist/harness/routes/events.d.ts +19 -0
- package/dist/harness/routes/events.js +215 -0
- package/dist/harness/routes/tasks.d.ts +11 -0
- package/dist/harness/routes/tasks.js +108 -0
- package/dist/harness/runners/inProcessRunner.d.ts +57 -0
- package/dist/harness/runners/inProcessRunner.js +196 -0
- package/dist/harness/runtime.d.ts +34 -0
- package/dist/harness/runtime.js +551 -0
- package/dist/harness/subagent.d.ts +53 -0
- package/dist/harness/subagent.js +134 -0
- package/dist/harness/suspend.d.ts +8 -0
- package/dist/harness/suspend.js +16 -0
- package/dist/harness/transaction.d.ts +3 -0
- package/dist/harness/transaction.js +22 -0
- package/dist/harness/waits.d.ts +100 -0
- package/dist/harness/waits.js +214 -0
- package/dist/index.d.ts +19 -1
- package/dist/index.js +45 -2
- package/dist/models/aiDataset.d.ts +2 -0
- package/dist/models/aiDataset.js +67 -0
- package/dist/models/aiRequest.js +1 -1
- package/dist/models/gptHistory.js +88 -2
- package/dist/observability/adminScreens.d.ts +5 -0
- package/dist/observability/adminScreens.js +32 -0
- package/dist/observability/compileTemplate.d.ts +1 -0
- package/dist/observability/compileTemplate.js +13 -0
- package/dist/observability/datasetImport.d.ts +11 -0
- package/dist/observability/datasetImport.js +185 -0
- package/dist/observability/evaluate.d.ts +31 -0
- package/dist/observability/evaluate.js +200 -0
- package/dist/observability/evaluatorTemplates.d.ts +17 -0
- package/dist/observability/evaluatorTemplates.js +111 -0
- package/dist/observability/jsonPath.d.ts +2 -0
- package/dist/observability/jsonPath.js +57 -0
- package/dist/observability/local/datasetStore.d.ts +87 -0
- package/dist/observability/local/datasetStore.js +326 -0
- package/dist/observability/local/evaluatorStore.d.ts +44 -0
- package/dist/observability/local/evaluatorStore.js +182 -0
- package/dist/observability/local/experimentRunner.d.ts +96 -0
- package/dist/observability/local/experimentRunner.js +419 -0
- package/dist/observability/local/localPlugin.d.ts +22 -0
- package/dist/observability/local/localPlugin.js +76 -0
- package/dist/observability/local/models/obsDataset.d.ts +2 -0
- package/dist/observability/local/models/obsDataset.js +37 -0
- package/dist/observability/local/models/obsDatasetItem.d.ts +2 -0
- package/dist/observability/local/models/obsDatasetItem.js +76 -0
- package/dist/observability/local/models/obsEvaluator.d.ts +2 -0
- package/dist/observability/local/models/obsEvaluator.js +98 -0
- package/dist/observability/local/models/obsExperiment.d.ts +2 -0
- package/dist/observability/local/models/obsExperiment.js +87 -0
- package/dist/observability/local/models/obsExperimentItem.d.ts +2 -0
- package/dist/observability/local/models/obsExperimentItem.js +45 -0
- package/dist/observability/local/models/obsPrompt.d.ts +2 -0
- package/dist/observability/local/models/obsPrompt.js +42 -0
- package/dist/observability/local/models/obsPromptLabel.d.ts +2 -0
- package/dist/observability/local/models/obsPromptLabel.js +39 -0
- package/dist/observability/local/models/obsPromptVersion.d.ts +2 -0
- package/dist/observability/local/models/obsPromptVersion.js +96 -0
- package/dist/observability/local/models/obsReviewItem.d.ts +2 -0
- package/dist/observability/local/models/obsReviewItem.js +75 -0
- package/dist/observability/local/models/obsScore.d.ts +2 -0
- package/dist/observability/local/models/obsScore.js +58 -0
- package/dist/observability/local/models/obsSpan.d.ts +2 -0
- package/dist/observability/local/models/obsSpan.js +64 -0
- package/dist/observability/local/models/obsTrace.d.ts +2 -0
- package/dist/observability/local/models/obsTrace.js +93 -0
- package/dist/observability/local/promptStore.d.ts +180 -0
- package/dist/observability/local/promptStore.js +444 -0
- package/dist/observability/local/reviewStore.d.ts +50 -0
- package/dist/observability/local/reviewStore.js +203 -0
- package/dist/observability/local/traceStore.d.ts +103 -0
- package/dist/observability/local/traceStore.js +325 -0
- package/dist/observability/observabilityApp.d.ts +24 -0
- package/dist/observability/observabilityApp.js +157 -0
- package/dist/observability/observabilityAppRegistry.d.ts +14 -0
- package/dist/observability/observabilityAppRegistry.js +16 -0
- package/dist/observability/permissions.d.ts +7 -0
- package/dist/observability/permissions.js +60 -0
- package/dist/observability/reviewPanels.d.ts +18 -0
- package/dist/observability/reviewPanels.js +46 -0
- package/dist/observability/routes/datasets.d.ts +7 -0
- package/dist/observability/routes/datasets.js +149 -0
- package/dist/observability/routes/evaluators.d.ts +7 -0
- package/dist/observability/routes/evaluators.js +123 -0
- package/dist/observability/routes/experiments.d.ts +7 -0
- package/dist/observability/routes/experiments.js +95 -0
- package/dist/observability/routes/observabilityRouteAccess.d.ts +13 -0
- package/dist/observability/routes/observabilityRouteAccess.js +67 -0
- package/dist/observability/routes/prompts.d.ts +12 -0
- package/dist/observability/routes/prompts.js +172 -0
- package/dist/observability/routes/requestAiService.d.ts +13 -0
- package/dist/observability/routes/requestAiService.js +21 -0
- package/dist/observability/routes/review.d.ts +7 -0
- package/dist/observability/routes/review.js +79 -0
- package/dist/observability/routes/status.d.ts +5 -0
- package/dist/observability/routes/status.js +31 -0
- package/dist/observability/routes/testMultiStage.d.ts +9 -0
- package/dist/observability/routes/testMultiStage.js +67 -0
- package/dist/observability/routes/traces.d.ts +7 -0
- package/dist/observability/routes/traces.js +129 -0
- package/dist/observability/schemaValidation.d.ts +5 -0
- package/dist/observability/schemaValidation.js +26 -0
- package/dist/observability/sopThresholds.d.ts +2 -0
- package/dist/observability/sopThresholds.js +21 -0
- package/dist/observability/status.d.ts +23 -0
- package/dist/observability/status.js +34 -0
- package/dist/observability/testMultiStageSchemas.d.ts +10 -0
- package/dist/observability/testMultiStageSchemas.js +79 -0
- package/dist/observability/testMultiStageWorkflow.d.ts +20 -0
- package/dist/observability/testMultiStageWorkflow.js +218 -0
- package/dist/observability/types.d.ts +159 -0
- package/dist/observability/types.js +67 -0
- package/dist/routes/files.js +20 -7
- package/dist/routes/gpt.js +179 -488
- package/dist/routes/gptActions.d.ts +9 -0
- package/dist/routes/gptActions.js +212 -0
- package/dist/routes/gptDatasets.d.ts +5 -0
- package/dist/routes/gptDatasets.js +94 -0
- package/dist/routes/gptHistories.d.ts +3 -1
- package/dist/routes/gptHistories.js +188 -2
- package/dist/service/aiDatasets.d.ts +57 -0
- package/dist/service/aiDatasets.js +253 -0
- package/dist/service/aiService.d.ts +50 -1
- package/dist/service/aiService.js +431 -77
- package/dist/service/askFiles.d.ts +49 -0
- package/dist/service/askFiles.js +162 -0
- package/dist/service/asks.d.ts +76 -0
- package/dist/service/asks.js +284 -0
- package/dist/service/chatTurn.d.ts +128 -0
- package/dist/service/chatTurn.js +1674 -0
- package/dist/service/fileStorage.d.ts +4 -0
- package/dist/service/fileStorage.js +7 -2
- package/dist/service/fileUploadsGate.d.ts +8 -0
- package/dist/service/fileUploadsGate.js +18 -0
- package/dist/service/fillActionIds.d.ts +6 -0
- package/dist/service/fillActionIds.js +79 -0
- package/dist/service/gemini.d.ts +3 -3
- package/dist/service/gemini.js +3 -3
- package/dist/service/jsonFenceModel.d.ts +7 -0
- package/dist/service/jsonFenceModel.js +45 -0
- package/dist/service/prompts.d.ts +52 -0
- package/dist/service/prompts.js +96 -1
- package/dist/service/sanitizeHtml.d.ts +11 -0
- package/dist/service/sanitizeHtml.js +108 -0
- package/dist/service/vertex.d.ts +6 -6
- package/dist/service/vertex.js +5 -5
- package/dist/types/harness.d.ts +934 -0
- package/dist/types/harness.js +133 -0
- package/dist/types/index.d.ts +219 -13
- package/dist/types/index.js +2 -0
- package/dist/types/observability.d.ts +288 -0
- package/dist/types/observability.js +2 -0
- package/package.json +17 -5
- package/src/aiApp.test.ts +207 -1
- package/src/aiApp.ts +31 -8
- package/src/harness/adminScreens.test.ts +34 -0
- package/src/harness/adminScreens.ts +11 -0
- package/src/harness/agentLoop.test.ts +1034 -0
- package/src/harness/agentLoop.ts +1082 -0
- package/src/harness/agentRecovery.test.ts +380 -0
- package/src/harness/agentTaskNames.ts +4 -0
- package/src/harness/approvalPolicy.ts +28 -0
- package/src/harness/approvals.test.ts +1094 -0
- package/src/harness/approvals.ts +644 -0
- package/src/harness/commit.ts +1049 -0
- package/src/harness/conversation.ts +503 -0
- package/src/harness/defineAgent.test.ts +92 -0
- package/src/harness/defineAgent.ts +82 -0
- package/src/harness/defineTask.test.ts +59 -0
- package/src/harness/defineTask.ts +76 -0
- package/src/harness/defineTool.ts +45 -0
- package/src/harness/definitionError.ts +13 -0
- package/src/harness/errors.test.ts +90 -0
- package/src/harness/errors.ts +119 -0
- package/src/harness/eventHub.test.ts +124 -0
- package/src/harness/eventHub.ts +159 -0
- package/src/harness/events.test.ts +757 -0
- package/src/harness/events.ts +384 -0
- package/src/harness/executionEnv.ts +34 -0
- package/src/harness/extensions.test.ts +1218 -0
- package/src/harness/extensions.ts +382 -0
- package/src/harness/harness.test.ts +693 -0
- package/src/harness/harness.ts +767 -0
- package/src/harness/harnessApp.ts +76 -0
- package/src/harness/internalRuntime.ts +44 -0
- package/src/harness/leases.ts +132 -0
- package/src/harness/memo.ts +107 -0
- package/src/harness/modelCall.test.ts +134 -0
- package/src/harness/modelCall.ts +200 -0
- package/src/harness/models/harnessApproval.ts +109 -0
- package/src/harness/models/harnessConversation.ts +142 -0
- package/src/harness/models/harnessEvent.ts +92 -0
- package/src/harness/models/harnessInboxEvent.ts +73 -0
- package/src/harness/models/harnessMemo.ts +43 -0
- package/src/harness/models/harnessMessage.ts +85 -0
- package/src/harness/models/harnessModels.test.ts +60 -0
- package/src/harness/models/harnessOwner.ts +40 -0
- package/src/harness/models/harnessTask.ts +201 -0
- package/src/harness/ownership.test.ts +1087 -0
- package/src/harness/ownership.ts +618 -0
- package/src/harness/recovery.test.ts +912 -0
- package/src/harness/registry.ts +124 -0
- package/src/harness/retries.test.ts +292 -0
- package/src/harness/retryBackoff.test.ts +100 -0
- package/src/harness/retryBackoff.ts +77 -0
- package/src/harness/routes/approvals.ts +123 -0
- package/src/harness/routes/conversations.test.ts +518 -0
- package/src/harness/routes/conversations.ts +102 -0
- package/src/harness/routes/events.ts +280 -0
- package/src/harness/routes/tasks.test.ts +198 -0
- package/src/harness/routes/tasks.ts +146 -0
- package/src/harness/runners/inProcessRunner.test.ts +402 -0
- package/src/harness/runners/inProcessRunner.ts +220 -0
- package/src/harness/runtime.ts +714 -0
- package/src/harness/subagent.test.ts +696 -0
- package/src/harness/subagent.ts +192 -0
- package/src/harness/suspend.ts +13 -0
- package/src/harness/transaction.ts +17 -0
- package/src/harness/versioning.test.ts +288 -0
- package/src/harness/waits.test.ts +693 -0
- package/src/harness/waits.ts +356 -0
- package/src/index.test.ts +13 -0
- package/src/index.ts +70 -0
- package/src/isolated/adminScreensConfig.isolated.ts +143 -0
- package/src/models/aiDataset.test.ts +34 -0
- package/src/models/aiDataset.ts +78 -0
- package/src/models/aiRequest.test.ts +2 -2
- package/src/models/aiRequest.ts +2 -1
- package/src/models/gptHistory.test.ts +156 -0
- package/src/models/gptHistory.ts +106 -4
- package/src/observability/adminScreens.test.ts +71 -0
- package/src/observability/adminScreens.ts +42 -0
- package/src/observability/compileTemplate.test.ts +14 -0
- package/src/observability/compileTemplate.ts +10 -0
- package/src/observability/datasetImport.test.ts +95 -0
- package/src/observability/datasetImport.ts +220 -0
- package/src/observability/evaluate.test.ts +549 -0
- package/src/observability/evaluate.ts +254 -0
- package/src/observability/evaluatorTemplates.ts +124 -0
- package/src/observability/jsonPath.test.ts +52 -0
- package/src/observability/jsonPath.ts +60 -0
- package/src/observability/local/datasetStore.test.ts +230 -0
- package/src/observability/local/datasetStore.ts +452 -0
- package/src/observability/local/evaluatorStore.test.ts +138 -0
- package/src/observability/local/evaluatorStore.ts +224 -0
- package/src/observability/local/experimentRunner.test.ts +489 -0
- package/src/observability/local/experimentRunner.ts +548 -0
- package/src/observability/local/localPlugin.ts +88 -0
- package/src/observability/local/models/obsDataset.ts +38 -0
- package/src/observability/local/models/obsDatasetItem.ts +80 -0
- package/src/observability/local/models/obsEvaluator.ts +102 -0
- package/src/observability/local/models/obsExperiment.ts +91 -0
- package/src/observability/local/models/obsExperimentItem.ts +52 -0
- package/src/observability/local/models/obsPrompt.ts +43 -0
- package/src/observability/local/models/obsPromptLabel.ts +43 -0
- package/src/observability/local/models/obsPromptVersion.ts +107 -0
- package/src/observability/local/models/obsReviewItem.ts +79 -0
- package/src/observability/local/models/obsScore.ts +59 -0
- package/src/observability/local/models/obsSpan.ts +65 -0
- package/src/observability/local/models/obsTrace.ts +94 -0
- package/src/observability/local/obsModels.test.ts +119 -0
- package/src/observability/local/promptStore.test.ts +351 -0
- package/src/observability/local/promptStore.ts +665 -0
- package/src/observability/local/reviewStore.test.ts +164 -0
- package/src/observability/local/reviewStore.ts +264 -0
- package/src/observability/local/traceStore.test.ts +264 -0
- package/src/observability/local/traceStore.ts +440 -0
- package/src/observability/observabilityApp.test.ts +92 -0
- package/src/observability/observabilityApp.ts +185 -0
- package/src/observability/observabilityAppRegistry.test.ts +30 -0
- package/src/observability/observabilityAppRegistry.ts +35 -0
- package/src/observability/permissions.test.ts +96 -0
- package/src/observability/permissions.ts +78 -0
- package/src/observability/reviewPanels.test.ts +35 -0
- package/src/observability/reviewPanels.ts +64 -0
- package/src/observability/routes/datasets.test.ts +176 -0
- package/src/observability/routes/datasets.ts +252 -0
- package/src/observability/routes/evaluators.test.ts +119 -0
- package/src/observability/routes/evaluators.ts +207 -0
- package/src/observability/routes/experiments.test.ts +282 -0
- package/src/observability/routes/experiments.ts +152 -0
- package/src/observability/routes/observabilityRbac.test.ts +767 -0
- package/src/observability/routes/observabilityRouteAccess.ts +106 -0
- package/src/observability/routes/prompts.test.ts +320 -0
- package/src/observability/routes/prompts.ts +263 -0
- package/src/observability/routes/requestAiService.ts +36 -0
- package/src/observability/routes/review.test.ts +120 -0
- package/src/observability/routes/review.ts +125 -0
- package/src/observability/routes/status.test.ts +116 -0
- package/src/observability/routes/status.ts +47 -0
- package/src/observability/routes/testMultiStage.test.ts +214 -0
- package/src/observability/routes/testMultiStage.ts +96 -0
- package/src/observability/routes/traces.test.ts +167 -0
- package/src/observability/routes/traces.ts +172 -0
- package/src/observability/schemaValidation.ts +29 -0
- package/src/observability/sopThresholds.ts +20 -0
- package/src/observability/status.test.ts +38 -0
- package/src/observability/status.ts +56 -0
- package/src/observability/testMultiStageSchemas.ts +95 -0
- package/src/observability/testMultiStageWorkflow.test.ts +114 -0
- package/src/observability/testMultiStageWorkflow.ts +307 -0
- package/src/observability/types.ts +262 -0
- package/src/routes/files.test.ts +84 -9
- package/src/routes/files.ts +23 -7
- package/src/routes/gpt.test.ts +3279 -0
- package/src/routes/gpt.ts +214 -552
- package/src/routes/gptActions.test.ts +241 -0
- package/src/routes/gptActions.ts +255 -0
- package/src/routes/gptApprovals.test.ts +762 -0
- package/src/routes/gptDatasets.test.ts +86 -0
- package/src/routes/gptDatasets.ts +104 -0
- package/src/routes/gptFiles.test.ts +589 -0
- package/src/routes/gptHistories.test.ts +933 -0
- package/src/routes/gptHistories.ts +266 -5
- package/src/routes/routes.test.ts +676 -5
- package/src/service/aiDatasets.test.ts +123 -0
- package/src/service/aiDatasets.ts +316 -0
- package/src/service/aiService.test.ts +614 -0
- package/src/service/aiService.ts +575 -111
- package/src/service/askFiles.test.ts +96 -0
- package/src/service/askFiles.ts +222 -0
- package/src/service/asks.test.ts +946 -0
- package/src/service/asks.ts +423 -0
- package/src/service/chatTurn.ts +2287 -0
- package/src/service/fileStorage.test.ts +11 -0
- package/src/service/fileStorage.ts +10 -2
- package/src/service/fileUploadsGate.ts +23 -0
- package/src/service/fillActionIds.test.ts +65 -0
- package/src/service/fillActionIds.ts +98 -0
- package/src/service/gemini.test.ts +16 -10
- package/src/service/gemini.ts +3 -3
- package/src/service/jsonFenceModel.test.ts +47 -0
- package/src/service/jsonFenceModel.ts +53 -0
- package/src/service/prompts.test.ts +21 -0
- package/src/service/prompts.ts +147 -0
- package/src/service/sanitizeHtml.test.ts +61 -0
- package/src/service/sanitizeHtml.ts +109 -0
- package/src/service/vertex.test.ts +49 -49
- package/src/service/vertex.ts +7 -8
- package/src/tests/bunSetup.test.ts +7 -4
- package/src/tests/bunSetup.ts +7 -7
- package/src/tests/chatHarness.ts +478 -0
- package/src/tests/generateStream.ts +49 -0
- package/src/tests/harnessErrors.ts +22 -0
- package/src/tests/harnessStreaming.ts +227 -0
- package/src/tests/helpers.ts +9 -2
- package/src/types/harness.ts +1095 -0
- package/src/types/index.ts +237 -12
- package/src/types/observability.ts +344 -0
- package/src/types/sanitize-html.d.ts +25 -0
|
@@ -0,0 +1,382 @@
|
|
|
1
|
+
import {createHash} from "node:crypto";
|
|
2
|
+
|
|
3
|
+
import type {
|
|
4
|
+
AnyHarnessToolDefinition,
|
|
5
|
+
HarnessAgentDefinition,
|
|
6
|
+
HarnessApprovalPolicy,
|
|
7
|
+
HarnessApprovalRequest,
|
|
8
|
+
HarnessBeforeToolResult,
|
|
9
|
+
HarnessExtensionDefinition,
|
|
10
|
+
HarnessExtensionDefinitionInput,
|
|
11
|
+
HarnessHook,
|
|
12
|
+
HarnessHookApi,
|
|
13
|
+
HarnessHookHandlers,
|
|
14
|
+
HarnessHookKind,
|
|
15
|
+
HarnessModelRequest,
|
|
16
|
+
HarnessSection,
|
|
17
|
+
HarnessSectionInput,
|
|
18
|
+
HarnessSystemPromptPart,
|
|
19
|
+
HarnessToolCall,
|
|
20
|
+
HarnessToolWrap,
|
|
21
|
+
} from "../types/harness";
|
|
22
|
+
import {HARNESS_HOOK_KINDS} from "../types/harness";
|
|
23
|
+
import {assertValidApprovalPolicies} from "./approvalPolicy";
|
|
24
|
+
import {HarnessDefinitionError} from "./definitionError";
|
|
25
|
+
import {errorMessage, harnessError} from "./errors";
|
|
26
|
+
import {isHarnessSuspendSignal} from "./suspend";
|
|
27
|
+
|
|
28
|
+
/** A hook that parked its task (an approval wait) must stop the phase, not fail it. */
|
|
29
|
+
const rethrowSuspend = (error: unknown): void => {
|
|
30
|
+
if (isHarnessSuspendSignal(error)) {
|
|
31
|
+
throw error;
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* A hook, a section, or a wrap threw. The step it ran in fails with this message: the
|
|
37
|
+
* turn for sections and `beforeModelRequest`, the tool call (reported to the model) for
|
|
38
|
+
* tool hooks and wraps.
|
|
39
|
+
*/
|
|
40
|
+
export class HarnessExtensionError extends Error {
|
|
41
|
+
readonly extension: string;
|
|
42
|
+
|
|
43
|
+
constructor(extension: string, what: string, cause: unknown) {
|
|
44
|
+
super(`Extension "${extension}" ${what} failed: ${errorMessage(cause)}`);
|
|
45
|
+
this.name = "HarnessExtensionError";
|
|
46
|
+
this.extension = extension;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** A named piece of the system prompt, rebuilt before every model request. */
|
|
51
|
+
export const section = (name: string, build: HarnessSection["build"]): HarnessSection => {
|
|
52
|
+
if (!name?.trim()) {
|
|
53
|
+
throw new HarnessDefinitionError("section: name is required");
|
|
54
|
+
}
|
|
55
|
+
if (typeof build !== "function") {
|
|
56
|
+
throw new HarnessDefinitionError(`section(${name}): build must be a function`);
|
|
57
|
+
}
|
|
58
|
+
return Object.freeze({build, kind: "section" as const, name});
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
/** A hook of `kind` (`beforeModelRequest`, `beforeTool`, `afterTool`). */
|
|
62
|
+
export const hook = <K extends HarnessHookKind>(
|
|
63
|
+
kind: K,
|
|
64
|
+
run: HarnessHookHandlers[K]
|
|
65
|
+
): HarnessHook => {
|
|
66
|
+
if (!Object.values(HARNESS_HOOK_KINDS).includes(kind)) {
|
|
67
|
+
throw new HarnessDefinitionError(
|
|
68
|
+
`hook: kind must be one of ${Object.values(HARNESS_HOOK_KINDS).join(", ")}, not "${kind}"`
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
if (typeof run !== "function") {
|
|
72
|
+
throw new HarnessDefinitionError(`hook(${kind}): handler must be a function`);
|
|
73
|
+
}
|
|
74
|
+
return Object.freeze({hook: kind, kind: "hook" as const, run}) as HarnessHook;
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Decorate whichever tool named `toolOrName` wins (agent or extension). `wrap` receives
|
|
79
|
+
* the tool and returns the tool to use instead, with the same name.
|
|
80
|
+
*/
|
|
81
|
+
export const wrapTool = (
|
|
82
|
+
toolOrName: AnyHarnessToolDefinition | string,
|
|
83
|
+
wrap: HarnessToolWrap["wrap"]
|
|
84
|
+
): HarnessToolWrap => {
|
|
85
|
+
const toolName = typeof toolOrName === "string" ? toolOrName : toolOrName?.name;
|
|
86
|
+
if (!toolName?.trim()) {
|
|
87
|
+
throw new HarnessDefinitionError("wrapTool: a tool or tool name is required");
|
|
88
|
+
}
|
|
89
|
+
if (typeof wrap !== "function") {
|
|
90
|
+
throw new HarnessDefinitionError(`wrapTool(${toolName}): wrap must be a function`);
|
|
91
|
+
}
|
|
92
|
+
return Object.freeze({kind: "wrap" as const, toolName, wrap});
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Declare an extension: prompt sections, tools, hooks, and tool wraps that agents and
|
|
97
|
+
* conversations use by name. List it in `Harness.open({registry})`.
|
|
98
|
+
*/
|
|
99
|
+
export const defineExtension = (
|
|
100
|
+
definition: HarnessExtensionDefinitionInput
|
|
101
|
+
): HarnessExtensionDefinition => {
|
|
102
|
+
const {name} = definition;
|
|
103
|
+
if (!name?.trim()) {
|
|
104
|
+
throw new HarnessDefinitionError("defineExtension: name is required");
|
|
105
|
+
}
|
|
106
|
+
const sections = definition.sections ?? [];
|
|
107
|
+
const sectionNames = new Set<string>();
|
|
108
|
+
for (const entry of sections) {
|
|
109
|
+
if (entry?.kind !== "section") {
|
|
110
|
+
throw new HarnessDefinitionError(
|
|
111
|
+
`defineExtension(${name}): every section must come from section()`
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
if (sectionNames.has(entry.name)) {
|
|
115
|
+
throw new HarnessDefinitionError(
|
|
116
|
+
`defineExtension(${name}): section "${entry.name}" is listed more than once`
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
sectionNames.add(entry.name);
|
|
120
|
+
}
|
|
121
|
+
const tools = definition.tools ?? [];
|
|
122
|
+
const toolNames = new Set<string>();
|
|
123
|
+
for (const tool of tools) {
|
|
124
|
+
if (tool?.kind !== "tool") {
|
|
125
|
+
throw new HarnessDefinitionError(
|
|
126
|
+
`defineExtension(${name}): every tool must come from defineTool`
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
if (toolNames.has(tool.name)) {
|
|
130
|
+
throw new HarnessDefinitionError(
|
|
131
|
+
`defineExtension(${name}): tool "${tool.name}" is listed more than once`
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
toolNames.add(tool.name);
|
|
135
|
+
}
|
|
136
|
+
const hooks = definition.hooks ?? [];
|
|
137
|
+
if (hooks.some((entry) => entry?.kind !== "hook")) {
|
|
138
|
+
throw new HarnessDefinitionError(`defineExtension(${name}): every hook must come from hook()`);
|
|
139
|
+
}
|
|
140
|
+
const wraps = definition.wraps ?? [];
|
|
141
|
+
if (wraps.some((entry) => entry?.kind !== "wrap")) {
|
|
142
|
+
throw new HarnessDefinitionError(
|
|
143
|
+
`defineExtension(${name}): every wrap must come from wrapTool()`
|
|
144
|
+
);
|
|
145
|
+
}
|
|
146
|
+
const approvals: Readonly<Record<string, HarnessApprovalPolicy>> = Object.freeze({
|
|
147
|
+
...(definition.approvals ?? {}),
|
|
148
|
+
});
|
|
149
|
+
assertValidApprovalPolicies(`defineExtension(${name})`, definition.approvals);
|
|
150
|
+
return Object.freeze({
|
|
151
|
+
approvals,
|
|
152
|
+
hooks,
|
|
153
|
+
kind: "extension" as const,
|
|
154
|
+
name,
|
|
155
|
+
sections,
|
|
156
|
+
tools,
|
|
157
|
+
wraps,
|
|
158
|
+
});
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
/** The extensions named by `names`, in order. Throws for a name the registry lacks. */
|
|
162
|
+
export const resolveExtensions = (
|
|
163
|
+
extensions: Map<string, HarnessExtensionDefinition>,
|
|
164
|
+
names: ReadonlyArray<string>
|
|
165
|
+
): HarnessExtensionDefinition[] =>
|
|
166
|
+
names.map((name) => {
|
|
167
|
+
const extension = extensions.get(name);
|
|
168
|
+
if (!extension) {
|
|
169
|
+
throw harnessError({
|
|
170
|
+
detail: `Extension "${name}" is not in this harness registry`,
|
|
171
|
+
kind: "notRegistered",
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
return extension;
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* The tools a conversation can call, by name: the agent's tools, then each extension's
|
|
179
|
+
* in order (a later tool with the same name replaces an earlier one), then every wrap in
|
|
180
|
+
* extension order applied to the winning tool (the first extension's wrap is innermost).
|
|
181
|
+
*/
|
|
182
|
+
export const resolveTools = (
|
|
183
|
+
agent: HarnessAgentDefinition,
|
|
184
|
+
extensions: ReadonlyArray<HarnessExtensionDefinition>
|
|
185
|
+
): Map<string, AnyHarnessToolDefinition> => {
|
|
186
|
+
const tools = new Map<string, AnyHarnessToolDefinition>();
|
|
187
|
+
for (const tool of agent.tools) {
|
|
188
|
+
tools.set(tool.name, tool);
|
|
189
|
+
}
|
|
190
|
+
for (const extension of extensions) {
|
|
191
|
+
for (const tool of extension.tools) {
|
|
192
|
+
tools.set(tool.name, tool);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
for (const extension of extensions) {
|
|
196
|
+
for (const {toolName, wrap} of extension.wraps) {
|
|
197
|
+
const winner = tools.get(toolName);
|
|
198
|
+
if (!winner) {
|
|
199
|
+
continue;
|
|
200
|
+
}
|
|
201
|
+
let wrapped: AnyHarnessToolDefinition;
|
|
202
|
+
try {
|
|
203
|
+
wrapped = wrap(winner);
|
|
204
|
+
} catch (error: unknown) {
|
|
205
|
+
throw new HarnessExtensionError(extension.name, `wrap of tool "${toolName}"`, error);
|
|
206
|
+
}
|
|
207
|
+
if (wrapped?.kind !== "tool" || wrapped.name !== toolName) {
|
|
208
|
+
throw new HarnessDefinitionError(
|
|
209
|
+
`Extension "${extension.name}" wrap of tool "${toolName}" must return a defineTool tool named "${toolName}"`
|
|
210
|
+
);
|
|
211
|
+
}
|
|
212
|
+
tools.set(toolName, wrapped);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
return tools;
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
/** The effective system prompt, and what it was built from. */
|
|
219
|
+
export interface SystemPrompt {
|
|
220
|
+
hash: string;
|
|
221
|
+
sections: HarnessSystemPromptPart["sections"];
|
|
222
|
+
text: string;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
export const hashText = (text: string): string => createHash("sha256").update(text).digest("hex");
|
|
226
|
+
|
|
227
|
+
/** Agent instructions, then each non-empty section in extension and section order. */
|
|
228
|
+
export const buildSystemPrompt = async ({
|
|
229
|
+
api,
|
|
230
|
+
extensions,
|
|
231
|
+
input,
|
|
232
|
+
instructions,
|
|
233
|
+
}: {
|
|
234
|
+
api: HarnessHookApi;
|
|
235
|
+
extensions: ReadonlyArray<HarnessExtensionDefinition>;
|
|
236
|
+
input: HarnessSectionInput;
|
|
237
|
+
instructions: string;
|
|
238
|
+
}): Promise<SystemPrompt> => {
|
|
239
|
+
const pieces: string[] = instructions ? [instructions] : [];
|
|
240
|
+
const sections: SystemPrompt["sections"] = [];
|
|
241
|
+
for (const extension of extensions) {
|
|
242
|
+
for (const entry of extension.sections) {
|
|
243
|
+
let text: string | undefined;
|
|
244
|
+
try {
|
|
245
|
+
text = await entry.build(input, api);
|
|
246
|
+
} catch (error: unknown) {
|
|
247
|
+
rethrowSuspend(error);
|
|
248
|
+
throw new HarnessExtensionError(extension.name, `section "${entry.name}"`, error);
|
|
249
|
+
}
|
|
250
|
+
if (text === undefined || text === null || !String(text).trim()) {
|
|
251
|
+
continue;
|
|
252
|
+
}
|
|
253
|
+
pieces.push(String(text));
|
|
254
|
+
sections.push({extension: extension.name, name: entry.name});
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
const text = pieces.join("\n\n");
|
|
258
|
+
return {hash: hashText(text), sections, text};
|
|
259
|
+
};
|
|
260
|
+
|
|
261
|
+
/** Hooks of `kind`, in extension order, each with the extension that declared it. */
|
|
262
|
+
const hooksOf = <K extends HarnessHookKind>(
|
|
263
|
+
extensions: ReadonlyArray<HarnessExtensionDefinition>,
|
|
264
|
+
kind: K
|
|
265
|
+
): Array<{extension: string; run: HarnessHookHandlers[K]}> =>
|
|
266
|
+
extensions.flatMap((extension) =>
|
|
267
|
+
extension.hooks
|
|
268
|
+
.filter((entry) => entry.hook === kind)
|
|
269
|
+
.map((entry) => ({extension: extension.name, run: entry.run as HarnessHookHandlers[K]}))
|
|
270
|
+
);
|
|
271
|
+
|
|
272
|
+
/** Hooks a model request went through, with the extensions whose hooks rewrote it. */
|
|
273
|
+
export interface RewrittenRequest {
|
|
274
|
+
request: HarnessModelRequest;
|
|
275
|
+
rewrittenBy: string[];
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/** Run every `beforeModelRequest` hook in order; each sees the previous one's result. */
|
|
279
|
+
export const runBeforeModelRequest = async ({
|
|
280
|
+
api,
|
|
281
|
+
extensions,
|
|
282
|
+
request,
|
|
283
|
+
}: {
|
|
284
|
+
api: HarnessHookApi;
|
|
285
|
+
extensions: ReadonlyArray<HarnessExtensionDefinition>;
|
|
286
|
+
request: HarnessModelRequest;
|
|
287
|
+
}): Promise<RewrittenRequest> => {
|
|
288
|
+
let current = request;
|
|
289
|
+
const rewrittenBy: string[] = [];
|
|
290
|
+
for (const {extension, run} of hooksOf(extensions, HARNESS_HOOK_KINDS.beforeModelRequest)) {
|
|
291
|
+
let next: HarnessModelRequest | undefined;
|
|
292
|
+
try {
|
|
293
|
+
next = await run(current, api);
|
|
294
|
+
} catch (error: unknown) {
|
|
295
|
+
rethrowSuspend(error);
|
|
296
|
+
throw new HarnessExtensionError(extension, "beforeModelRequest hook", error);
|
|
297
|
+
}
|
|
298
|
+
if (next === undefined) {
|
|
299
|
+
continue;
|
|
300
|
+
}
|
|
301
|
+
if (typeof next?.system !== "string" || !Array.isArray(next.messages)) {
|
|
302
|
+
throw new HarnessDefinitionError(
|
|
303
|
+
`Extension "${extension}" beforeModelRequest hook must return {system, messages} or undefined`
|
|
304
|
+
);
|
|
305
|
+
}
|
|
306
|
+
current = next;
|
|
307
|
+
rewrittenBy.push(extension);
|
|
308
|
+
}
|
|
309
|
+
return {request: current, rewrittenBy};
|
|
310
|
+
};
|
|
311
|
+
|
|
312
|
+
/** Result of the `beforeTool` hooks: the call is blocked, or runs with these args. */
|
|
313
|
+
export type BeforeToolOutcome = {args: unknown; blocked?: undefined} | {blocked: string};
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* Run every `beforeTool` hook in order. The first `{block}` stops the rest. Each hook's
|
|
317
|
+
* `api.approval` resolves approvers from that hook's own extension. A hook waiting on an
|
|
318
|
+
* approval suspends the tool call (the suspend signal passes through untouched).
|
|
319
|
+
*/
|
|
320
|
+
export const runBeforeTool = async ({
|
|
321
|
+
api,
|
|
322
|
+
approvalFor,
|
|
323
|
+
call,
|
|
324
|
+
extensions,
|
|
325
|
+
}: {
|
|
326
|
+
api: HarnessHookApi;
|
|
327
|
+
approvalFor: (extension: string) => HarnessApprovalRequest;
|
|
328
|
+
call: HarnessToolCall;
|
|
329
|
+
extensions: ReadonlyArray<HarnessExtensionDefinition>;
|
|
330
|
+
}): Promise<BeforeToolOutcome> => {
|
|
331
|
+
let {args} = call;
|
|
332
|
+
for (const {extension, run} of hooksOf(extensions, HARNESS_HOOK_KINDS.beforeTool)) {
|
|
333
|
+
let decision: HarnessBeforeToolResult;
|
|
334
|
+
try {
|
|
335
|
+
decision = await run({...call, args}, {...api, approval: approvalFor(extension)});
|
|
336
|
+
} catch (error: unknown) {
|
|
337
|
+
rethrowSuspend(error);
|
|
338
|
+
throw new HarnessExtensionError(extension, "beforeTool hook", error);
|
|
339
|
+
}
|
|
340
|
+
if (decision === undefined) {
|
|
341
|
+
continue;
|
|
342
|
+
}
|
|
343
|
+
if ("block" in decision) {
|
|
344
|
+
return {blocked: String(decision.block)};
|
|
345
|
+
}
|
|
346
|
+
if ("args" in decision) {
|
|
347
|
+
args = decision.args;
|
|
348
|
+
continue;
|
|
349
|
+
}
|
|
350
|
+
throw new HarnessDefinitionError(
|
|
351
|
+
`Extension "${extension}" beforeTool hook must return undefined, {block}, or {args}`
|
|
352
|
+
);
|
|
353
|
+
}
|
|
354
|
+
return {args};
|
|
355
|
+
};
|
|
356
|
+
|
|
357
|
+
/** Run every `afterTool` hook in order; each sees the previous one's result. */
|
|
358
|
+
export const runAfterTool = async ({
|
|
359
|
+
api,
|
|
360
|
+
call,
|
|
361
|
+
extensions,
|
|
362
|
+
result,
|
|
363
|
+
}: {
|
|
364
|
+
api: HarnessHookApi;
|
|
365
|
+
call: HarnessToolCall;
|
|
366
|
+
extensions: ReadonlyArray<HarnessExtensionDefinition>;
|
|
367
|
+
result: unknown;
|
|
368
|
+
}): Promise<unknown> => {
|
|
369
|
+
let current = result;
|
|
370
|
+
for (const {extension, run} of hooksOf(extensions, HARNESS_HOOK_KINDS.afterTool)) {
|
|
371
|
+
let next: unknown;
|
|
372
|
+
try {
|
|
373
|
+
next = await run(call, current, api);
|
|
374
|
+
} catch (error: unknown) {
|
|
375
|
+
throw new HarnessExtensionError(extension, "afterTool hook", error);
|
|
376
|
+
}
|
|
377
|
+
if (next !== undefined) {
|
|
378
|
+
current = next;
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
return current;
|
|
382
|
+
};
|