@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,946 @@
|
|
|
1
|
+
import {describe, expect, it} from "bun:test";
|
|
2
|
+
import {ASK_LIMITS, type AskKind, type AskResponse, askPromptSection} from "@terreno/blocks";
|
|
3
|
+
import {asSchema, jsonSchema, type ModelMessage, type Tool, tool} from "ai";
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
approvalAskInput,
|
|
7
|
+
askKindFromToolName,
|
|
8
|
+
askToolName,
|
|
9
|
+
assertNoReservedToolNames,
|
|
10
|
+
buildAsksSystemPrompt,
|
|
11
|
+
completePausedTurn,
|
|
12
|
+
createAskTools,
|
|
13
|
+
deniedApprovalReasons,
|
|
14
|
+
parseAsk,
|
|
15
|
+
resolveAskKinds,
|
|
16
|
+
toStoredMessages,
|
|
17
|
+
withoutReservedToolNames,
|
|
18
|
+
} from "./asks";
|
|
19
|
+
import {TERRENO_ASKS_SYSTEM_PROMPT} from "./prompts";
|
|
20
|
+
|
|
21
|
+
const PLAN_ASK_INPUT = {
|
|
22
|
+
options: [
|
|
23
|
+
{id: "starter", label: "Starter"},
|
|
24
|
+
{id: "team", label: "Team"},
|
|
25
|
+
],
|
|
26
|
+
prompt: "Which plan should I set up?",
|
|
27
|
+
select: "one",
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const TEAM_ANSWER = {action: "accept" as const, content: {selected: ["team"]}};
|
|
31
|
+
|
|
32
|
+
const hostTool = (): Tool =>
|
|
33
|
+
tool({
|
|
34
|
+
description: "Look up the plans",
|
|
35
|
+
execute: async () => ({plans: 2}),
|
|
36
|
+
inputSchema: jsonSchema<Record<string, never>>({properties: {}, type: "object"}),
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
const askCall = (toolCallId: string, input: unknown = PLAN_ASK_INPUT) => ({
|
|
40
|
+
input,
|
|
41
|
+
toolCallId,
|
|
42
|
+
toolName: "ask_choice",
|
|
43
|
+
type: "tool-call" as const,
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
const lookupCall = {
|
|
47
|
+
input: {},
|
|
48
|
+
toolCallId: "call_lookup",
|
|
49
|
+
toolName: "lookupPlans",
|
|
50
|
+
type: "tool-call" as const,
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
const lookupResult = {
|
|
54
|
+
output: {type: "json" as const, value: {plans: 2}},
|
|
55
|
+
toolCallId: "call_lookup",
|
|
56
|
+
toolName: "lookupPlans",
|
|
57
|
+
type: "tool-result" as const,
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
const answerResult = (toolCallId: string, value: unknown) => ({
|
|
61
|
+
output: {type: "json", value},
|
|
62
|
+
toolCallId,
|
|
63
|
+
toolName: "ask_choice",
|
|
64
|
+
type: "tool-result",
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
describe("asks", () => {
|
|
68
|
+
describe("resolveAskKinds", () => {
|
|
69
|
+
it.each([
|
|
70
|
+
{asks: undefined, expected: [], label: "undefined"},
|
|
71
|
+
{asks: false, expected: [], label: "false"},
|
|
72
|
+
{asks: true, expected: ["choice", "confirm", "markdown", "form", "files"], label: "true"},
|
|
73
|
+
{asks: {}, expected: ["choice", "confirm", "markdown", "form", "files"], label: "{}"},
|
|
74
|
+
{
|
|
75
|
+
asks: {kinds: ["confirm"] as AskKind[]},
|
|
76
|
+
expected: ["confirm"],
|
|
77
|
+
label: '{kinds: ["confirm"]}',
|
|
78
|
+
},
|
|
79
|
+
{asks: {kinds: []}, expected: [], label: "{kinds: []}"},
|
|
80
|
+
{
|
|
81
|
+
asks: {kinds: ["choice", "choice"] as AskKind[]},
|
|
82
|
+
expected: ["choice"],
|
|
83
|
+
label: '{kinds: ["choice", "choice"]}',
|
|
84
|
+
},
|
|
85
|
+
])("resolves $label", ({asks, expected}) => {
|
|
86
|
+
expect(resolveAskKinds(asks)).toEqual(expected);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it("throws on an unknown kind and names the known kinds", () => {
|
|
90
|
+
expect(() => resolveAskKinds({kinds: ["choice", "video"] as unknown as AskKind[]})).toThrow(
|
|
91
|
+
expect.objectContaining({
|
|
92
|
+
detail: "Unknown ask kinds: video. Known kinds: choice, confirm, markdown, form, files.",
|
|
93
|
+
message: "The asks option lists unknown ask kinds",
|
|
94
|
+
status: 500,
|
|
95
|
+
})
|
|
96
|
+
);
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
describe("tool names", () => {
|
|
101
|
+
it("names a kind's tool ask_<kind>", () => {
|
|
102
|
+
expect(askToolName("choice")).toBe("ask_choice");
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it.each([
|
|
106
|
+
{expected: "choice", kinds: undefined, toolName: "ask_choice"},
|
|
107
|
+
{expected: "confirm", kinds: undefined, toolName: "ask_confirm"},
|
|
108
|
+
{expected: undefined, kinds: ["choice"] as AskKind[], toolName: "ask_confirm"},
|
|
109
|
+
{expected: "markdown", kinds: undefined, toolName: "ask_markdown"},
|
|
110
|
+
{expected: "form", kinds: undefined, toolName: "ask_form"},
|
|
111
|
+
{expected: "files", kinds: undefined, toolName: "ask_files"},
|
|
112
|
+
{expected: undefined, kinds: undefined, toolName: "ask_video"},
|
|
113
|
+
{expected: undefined, kinds: undefined, toolName: "lookupPlans"},
|
|
114
|
+
{expected: undefined, kinds: [] as AskKind[], toolName: "ask_choice"},
|
|
115
|
+
])("maps $toolName with kinds $kinds to $expected", ({expected, kinds, toolName}) => {
|
|
116
|
+
expect(askKindFromToolName(toolName, kinds)).toBe(expected as AskKind | undefined);
|
|
117
|
+
});
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
describe("createAskTools", () => {
|
|
121
|
+
it("creates one tool without execute per kind, with the kind's schemas", async () => {
|
|
122
|
+
const tools = createAskTools({kinds: ["choice"]});
|
|
123
|
+
|
|
124
|
+
expect(Object.keys(tools)).toEqual(["ask_choice"]);
|
|
125
|
+
const askChoice = tools.ask_choice;
|
|
126
|
+
expect(askChoice.execute).toBeUndefined();
|
|
127
|
+
expect(askChoice.description).toBe(
|
|
128
|
+
"Ask the user to pick one or more options from a list you provide, optionally with an " +
|
|
129
|
+
"Other field for an answer of their own. The chat shows the options as a control and " +
|
|
130
|
+
"returns the user's answer as this tool's result. Use it instead of asking in plain text " +
|
|
131
|
+
"when the user must choose from options you can list."
|
|
132
|
+
);
|
|
133
|
+
const inputSchema = asSchema(askChoice.inputSchema);
|
|
134
|
+
expect(await inputSchema.validate?.(PLAN_ASK_INPUT)).toEqual({
|
|
135
|
+
success: true,
|
|
136
|
+
value: PLAN_ASK_INPUT,
|
|
137
|
+
});
|
|
138
|
+
expect(await inputSchema.validate?.({...PLAN_ASK_INPUT, url: "https://example.com"})).toEqual(
|
|
139
|
+
{error: expect.any(Error), success: false}
|
|
140
|
+
);
|
|
141
|
+
const outputSchema = asSchema(askChoice.outputSchema);
|
|
142
|
+
expect(await outputSchema.validate?.(TEAM_ANSWER)).toEqual({
|
|
143
|
+
success: true,
|
|
144
|
+
value: TEAM_ANSWER,
|
|
145
|
+
});
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
it("creates no tools when no kinds are enabled", () => {
|
|
149
|
+
expect(createAskTools({kinds: []})).toEqual({});
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it("accepts select many with Other on the full surface only", async () => {
|
|
153
|
+
const manyInput = {
|
|
154
|
+
...PLAN_ASK_INPUT,
|
|
155
|
+
allowOther: true,
|
|
156
|
+
default: ["team", "starter"],
|
|
157
|
+
maxSelected: 2,
|
|
158
|
+
select: "many",
|
|
159
|
+
};
|
|
160
|
+
expect(
|
|
161
|
+
await asSchema(createAskTools({kinds: ["choice"]}).ask_choice.inputSchema).validate?.(
|
|
162
|
+
manyInput
|
|
163
|
+
)
|
|
164
|
+
).toEqual({success: true, value: manyInput});
|
|
165
|
+
expect(
|
|
166
|
+
await asSchema(
|
|
167
|
+
createAskTools({kinds: ["choice"], surface: "compact"}).ask_choice.inputSchema
|
|
168
|
+
).validate?.(manyInput)
|
|
169
|
+
).toEqual({error: expect.any(Error), success: false});
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
it("describes only select one on the compact surface", () => {
|
|
173
|
+
expect(createAskTools({kinds: ["choice"], surface: "compact"}).ask_choice.description).toBe(
|
|
174
|
+
"Ask the user to pick one option from a list you provide. The chat shows the options as a " +
|
|
175
|
+
"control and returns the user's answer as this tool's result. Use it instead of asking in " +
|
|
176
|
+
"plain text when the user must choose between options you can list."
|
|
177
|
+
);
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
it("takes the compact input schema on the compact surface", async () => {
|
|
181
|
+
const inputSchema = asSchema(
|
|
182
|
+
createAskTools({kinds: ["choice"], surface: "compact"}).ask_choice.inputSchema
|
|
183
|
+
);
|
|
184
|
+
const fourOptions = {
|
|
185
|
+
...PLAN_ASK_INPUT,
|
|
186
|
+
options: ["a", "b", "c", "d"].map((id) => ({id, label: id.toUpperCase()})),
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
expect(await inputSchema.validate?.(PLAN_ASK_INPUT)).toEqual({
|
|
190
|
+
success: true,
|
|
191
|
+
value: PLAN_ASK_INPUT,
|
|
192
|
+
});
|
|
193
|
+
expect(await inputSchema.validate?.(fourOptions)).toEqual({
|
|
194
|
+
error: expect.any(Error),
|
|
195
|
+
success: false,
|
|
196
|
+
});
|
|
197
|
+
expect(
|
|
198
|
+
await asSchema(createAskTools({kinds: ["choice"]}).ask_choice.inputSchema).validate?.(
|
|
199
|
+
fourOptions
|
|
200
|
+
)
|
|
201
|
+
).toEqual({success: true, value: fourOptions});
|
|
202
|
+
});
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
describe("createAskTools confirm", () => {
|
|
206
|
+
const confirmInput = {
|
|
207
|
+
confirmLabel: "Delete 14 todos",
|
|
208
|
+
denyLabel: "Keep them",
|
|
209
|
+
destructive: true,
|
|
210
|
+
prompt: "Delete your 14 completed todos?",
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
it("creates ask_confirm on both surfaces, after ask_choice", () => {
|
|
214
|
+
expect(Object.keys(createAskTools({kinds: ["choice", "confirm"]}))).toEqual([
|
|
215
|
+
"ask_choice",
|
|
216
|
+
"ask_confirm",
|
|
217
|
+
]);
|
|
218
|
+
expect(
|
|
219
|
+
Object.keys(createAskTools({kinds: ["choice", "confirm"], surface: "compact"}))
|
|
220
|
+
).toEqual(["ask_choice", "ask_confirm"]);
|
|
221
|
+
expect(createAskTools({kinds: ["confirm"]}).ask_confirm.execute).toBeUndefined();
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
it("describes approving one action on the full and compact surfaces", () => {
|
|
225
|
+
expect(createAskTools({kinds: ["confirm"]}).ask_confirm.description).toBe(
|
|
226
|
+
"Ask the user to approve or deny one action you describe. The chat shows an approve " +
|
|
227
|
+
"button and a deny button and returns {confirmed: true} or {confirmed: false} as this " +
|
|
228
|
+
"tool's result. Call it before a tool that deletes data, sends something on the user's " +
|
|
229
|
+
"behalf, spends money, or cannot be undone."
|
|
230
|
+
);
|
|
231
|
+
expect(createAskTools({kinds: ["confirm"], surface: "compact"}).ask_confirm.description).toBe(
|
|
232
|
+
"Ask the user to approve or deny one action you describe, with two short buttons. The " +
|
|
233
|
+
"user's answer comes back as {confirmed: true} or {confirmed: false}. Call it before a " +
|
|
234
|
+
"tool that deletes data, sends something, spends money, or cannot be undone."
|
|
235
|
+
);
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
it.each(["full", "compact"] as const)(
|
|
239
|
+
"validates confirm input and answers on the %s surface",
|
|
240
|
+
async (surface) => {
|
|
241
|
+
const askConfirm = createAskTools({kinds: ["confirm"], surface}).ask_confirm;
|
|
242
|
+
const inputSchema = asSchema(askConfirm.inputSchema);
|
|
243
|
+
|
|
244
|
+
expect(await inputSchema.validate?.(confirmInput)).toEqual({
|
|
245
|
+
success: true,
|
|
246
|
+
value: confirmInput,
|
|
247
|
+
});
|
|
248
|
+
for (const invalid of [
|
|
249
|
+
{...confirmInput, confirmLabel: "Delete all fourteen todos"},
|
|
250
|
+
{...confirmInput, denyLabel: "Delete 14 todos"},
|
|
251
|
+
{...confirmInput, submitLabel: "Go"},
|
|
252
|
+
{confirmLabel: "Go"},
|
|
253
|
+
]) {
|
|
254
|
+
expect(await inputSchema.validate?.(invalid)).toEqual({
|
|
255
|
+
error: expect.any(Error),
|
|
256
|
+
success: false,
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
const outputSchema = asSchema(askConfirm.outputSchema);
|
|
260
|
+
const approved = {action: "accept", content: {confirmed: true}};
|
|
261
|
+
expect(await outputSchema.validate?.(approved)).toEqual({success: true, value: approved});
|
|
262
|
+
expect(
|
|
263
|
+
await outputSchema.validate?.({action: "accept", content: {confirmed: "yes"}})
|
|
264
|
+
).toEqual({error: expect.any(Error), success: false});
|
|
265
|
+
}
|
|
266
|
+
);
|
|
267
|
+
|
|
268
|
+
it("types a valid confirm input", () => {
|
|
269
|
+
expect(parseAsk({input: confirmInput, kind: "confirm"})).toEqual({
|
|
270
|
+
input: confirmInput,
|
|
271
|
+
kind: "confirm",
|
|
272
|
+
});
|
|
273
|
+
});
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
describe("createAskTools markdown", () => {
|
|
277
|
+
const markdownInput = {
|
|
278
|
+
initial: "# We're live\n\nToday we launched.",
|
|
279
|
+
maxLength: 2000,
|
|
280
|
+
prompt: "Here is a draft announcement. Edit anything, then send it back.",
|
|
281
|
+
};
|
|
282
|
+
|
|
283
|
+
it("creates ask_markdown after ask_confirm on the full surface only", () => {
|
|
284
|
+
expect(Object.keys(createAskTools({kinds: ["choice", "confirm", "markdown"]}))).toEqual([
|
|
285
|
+
"ask_choice",
|
|
286
|
+
"ask_confirm",
|
|
287
|
+
"ask_markdown",
|
|
288
|
+
]);
|
|
289
|
+
expect(
|
|
290
|
+
Object.keys(createAskTools({kinds: ["choice", "confirm", "markdown"], surface: "compact"}))
|
|
291
|
+
).toEqual(["ask_choice", "ask_confirm"]);
|
|
292
|
+
expect(createAskTools({kinds: ["markdown"], surface: "compact"})).toEqual({});
|
|
293
|
+
expect(createAskTools({kinds: ["markdown"]}).ask_markdown.execute).toBeUndefined();
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
it("describes editing a draft and the {markdown, changed} result", () => {
|
|
297
|
+
expect(createAskTools({kinds: ["markdown"]}).ask_markdown.description).toBe(
|
|
298
|
+
"Ask the user to edit a markdown draft you write, or to write one, and send it back. The " +
|
|
299
|
+
"chat shows a markdown editor with a preview and returns {markdown, changed} as this " +
|
|
300
|
+
"tool's result; changed is false when the user approved your draft as is. Use it when " +
|
|
301
|
+
"the user should review or rewrite text before you use it, such as an announcement, an " +
|
|
302
|
+
"email, or release notes."
|
|
303
|
+
);
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
it("validates markdown input and answers", async () => {
|
|
307
|
+
const askMarkdown = createAskTools({kinds: ["markdown"]}).ask_markdown;
|
|
308
|
+
const inputSchema = asSchema(askMarkdown.inputSchema);
|
|
309
|
+
|
|
310
|
+
expect(await inputSchema.validate?.(markdownInput)).toEqual({
|
|
311
|
+
success: true,
|
|
312
|
+
value: markdownInput,
|
|
313
|
+
});
|
|
314
|
+
for (const invalid of [
|
|
315
|
+
{...markdownInput, initial: "x".repeat(20_001)},
|
|
316
|
+
{...markdownInput, maxLength: 20_001},
|
|
317
|
+
{...markdownInput, minLength: 3000},
|
|
318
|
+
{...markdownInput, language: "md"},
|
|
319
|
+
]) {
|
|
320
|
+
expect(await inputSchema.validate?.(invalid)).toEqual({
|
|
321
|
+
error: expect.any(Error),
|
|
322
|
+
success: false,
|
|
323
|
+
});
|
|
324
|
+
}
|
|
325
|
+
const outputSchema = asSchema(askMarkdown.outputSchema);
|
|
326
|
+
const edited = {action: "accept", content: {changed: true, markdown: "# Live"}};
|
|
327
|
+
expect(await outputSchema.validate?.(edited)).toEqual({success: true, value: edited});
|
|
328
|
+
expect(
|
|
329
|
+
await outputSchema.validate?.({action: "accept", content: {markdown: "# Live"}})
|
|
330
|
+
).toEqual({error: expect.any(Error), success: false});
|
|
331
|
+
});
|
|
332
|
+
|
|
333
|
+
it("types a valid markdown input", () => {
|
|
334
|
+
expect(parseAsk({input: markdownInput, kind: "markdown"})).toEqual({
|
|
335
|
+
input: markdownInput,
|
|
336
|
+
kind: "markdown",
|
|
337
|
+
});
|
|
338
|
+
});
|
|
339
|
+
});
|
|
340
|
+
|
|
341
|
+
describe("createAskTools form", () => {
|
|
342
|
+
const formInput = {
|
|
343
|
+
fields: [
|
|
344
|
+
{id: "company", label: "Company name", maxLength: 120, required: true, type: "text"},
|
|
345
|
+
{id: "seats", integer: true, label: "Seats", max: 500, min: 1, type: "number"},
|
|
346
|
+
{id: "start", label: "Start date", type: "date"},
|
|
347
|
+
],
|
|
348
|
+
prompt: "A few details for the invoice.",
|
|
349
|
+
};
|
|
350
|
+
|
|
351
|
+
it("creates ask_form after ask_markdown on the full surface only", () => {
|
|
352
|
+
expect(
|
|
353
|
+
Object.keys(createAskTools({kinds: ["choice", "confirm", "markdown", "form"]}))
|
|
354
|
+
).toEqual(["ask_choice", "ask_confirm", "ask_markdown", "ask_form"]);
|
|
355
|
+
expect(
|
|
356
|
+
Object.keys(
|
|
357
|
+
createAskTools({kinds: ["choice", "confirm", "markdown", "form"], surface: "compact"})
|
|
358
|
+
)
|
|
359
|
+
).toEqual(["ask_choice", "ask_confirm"]);
|
|
360
|
+
expect(createAskTools({kinds: ["form"], surface: "compact"})).toEqual({});
|
|
361
|
+
expect(createAskTools({kinds: ["form"]}).ask_form.execute).toBeUndefined();
|
|
362
|
+
});
|
|
363
|
+
|
|
364
|
+
it("describes the fields and the {values} result", () => {
|
|
365
|
+
expect(createAskTools({kinds: ["form"]}).ask_form.description).toBe(
|
|
366
|
+
"Ask the user to fill in a few typed fields and submit them at once, such as the details " +
|
|
367
|
+
"for an invoice or a booking. The chat shows one input per field and returns {values}, " +
|
|
368
|
+
"keyed by field id, as this tool's result. Use it when you need several values together; " +
|
|
369
|
+
"for one pick from a list, use a choice instead."
|
|
370
|
+
);
|
|
371
|
+
});
|
|
372
|
+
|
|
373
|
+
it("validates form input and answers", async () => {
|
|
374
|
+
const askForm = createAskTools({kinds: ["form"]}).ask_form;
|
|
375
|
+
const inputSchema = asSchema(askForm.inputSchema);
|
|
376
|
+
|
|
377
|
+
expect(await inputSchema.validate?.(formInput)).toEqual({success: true, value: formInput});
|
|
378
|
+
for (const invalid of [
|
|
379
|
+
{...formInput, fields: []},
|
|
380
|
+
{...formInput, fields: [{id: "secret", label: "Password", type: "password"}]},
|
|
381
|
+
{...formInput, fields: [{default: 0, id: "seats", label: "Seats", min: 1, type: "number"}]},
|
|
382
|
+
{...formInput, fields: [{default: "2026-02-30", id: "d", label: "Due", type: "date"}]},
|
|
383
|
+
]) {
|
|
384
|
+
expect(await inputSchema.validate?.(invalid)).toEqual({
|
|
385
|
+
error: expect.any(Error),
|
|
386
|
+
success: false,
|
|
387
|
+
});
|
|
388
|
+
}
|
|
389
|
+
const outputSchema = asSchema(askForm.outputSchema);
|
|
390
|
+
const answer = {action: "accept", content: {values: {company: "Acme", seats: 12}}};
|
|
391
|
+
expect(await outputSchema.validate?.(answer)).toEqual({success: true, value: answer});
|
|
392
|
+
expect(await outputSchema.validate?.({action: "accept", content: {company: "Acme"}})).toEqual(
|
|
393
|
+
{error: expect.any(Error), success: false}
|
|
394
|
+
);
|
|
395
|
+
});
|
|
396
|
+
});
|
|
397
|
+
|
|
398
|
+
describe("parseAsk", () => {
|
|
399
|
+
it("types a valid ask input", () => {
|
|
400
|
+
expect(parseAsk({input: PLAN_ASK_INPUT, kind: "choice"})).toEqual({
|
|
401
|
+
input: PLAN_ASK_INPUT,
|
|
402
|
+
kind: "choice",
|
|
403
|
+
});
|
|
404
|
+
});
|
|
405
|
+
|
|
406
|
+
it("throws on an input the kind's schema rejects", () => {
|
|
407
|
+
expect(() => parseAsk({input: {...PLAN_ASK_INPUT, select: "all"}, kind: "choice"})).toThrow();
|
|
408
|
+
});
|
|
409
|
+
});
|
|
410
|
+
|
|
411
|
+
describe("reserved tool names", () => {
|
|
412
|
+
it("accepts host tools without the ask_ prefix, and no tools at all", () => {
|
|
413
|
+
expect(() => assertNoReservedToolNames({lookupPlans: hostTool()})).not.toThrow();
|
|
414
|
+
expect(() => assertNoReservedToolNames(undefined)).not.toThrow();
|
|
415
|
+
});
|
|
416
|
+
|
|
417
|
+
it("throws and lists every host tool that uses the ask_ prefix", () => {
|
|
418
|
+
expect(() =>
|
|
419
|
+
assertNoReservedToolNames({
|
|
420
|
+
ask_budget: hostTool(),
|
|
421
|
+
ask_region: hostTool(),
|
|
422
|
+
lookupPlans: hostTool(),
|
|
423
|
+
})
|
|
424
|
+
).toThrow(
|
|
425
|
+
expect.objectContaining({
|
|
426
|
+
detail: 'Tool names starting with "ask_" are reserved for asks: ask_budget, ask_region.',
|
|
427
|
+
message: "Host tool names use the prefix reserved for asks",
|
|
428
|
+
status: 500,
|
|
429
|
+
})
|
|
430
|
+
);
|
|
431
|
+
});
|
|
432
|
+
|
|
433
|
+
it("drops per-request tools that use the ask_ prefix and keeps the rest", () => {
|
|
434
|
+
const lookupPlans = hostTool();
|
|
435
|
+
|
|
436
|
+
expect(withoutReservedToolNames({ask_choice: hostTool(), lookupPlans})).toEqual({
|
|
437
|
+
lookupPlans,
|
|
438
|
+
});
|
|
439
|
+
});
|
|
440
|
+
|
|
441
|
+
it("returns the same tools when none use the ask_ prefix", () => {
|
|
442
|
+
const tools = {lookupPlans: hostTool()};
|
|
443
|
+
|
|
444
|
+
expect(withoutReservedToolNames(tools)).toBe(tools);
|
|
445
|
+
});
|
|
446
|
+
});
|
|
447
|
+
|
|
448
|
+
describe("buildAsksSystemPrompt", () => {
|
|
449
|
+
it("appends the compact section on the compact surface", () => {
|
|
450
|
+
expect(buildAsksSystemPrompt({kinds: ["choice"], surface: "compact"})).toBe(
|
|
451
|
+
`${TERRENO_ASKS_SYSTEM_PROMPT}\n\n${askPromptSection({kinds: ["choice"], surface: "compact"})}`
|
|
452
|
+
);
|
|
453
|
+
});
|
|
454
|
+
|
|
455
|
+
it("appends the section for the enabled kinds to the asks prompt", () => {
|
|
456
|
+
expect(buildAsksSystemPrompt({kinds: ["choice"]})).toBe(
|
|
457
|
+
[
|
|
458
|
+
TERRENO_ASKS_SYSTEM_PROMPT,
|
|
459
|
+
"Ask tools you can call: ask_choice.",
|
|
460
|
+
[
|
|
461
|
+
"Rules for every ask:",
|
|
462
|
+
"- prompt: required. Plain text with no markdown and no links, 1-500 characters.",
|
|
463
|
+
"- title: optional, at most 80 characters.",
|
|
464
|
+
"- Each ask kind below lists its other fields, including whether the user can skip it.",
|
|
465
|
+
].join("\n"),
|
|
466
|
+
[
|
|
467
|
+
"ask_choice: the user picks one or more options from a list you provide.",
|
|
468
|
+
'- select: "one" for exactly one option, or "many" to let the user pick several.',
|
|
469
|
+
"- options: 2-50 items, each {id, label, description?}.",
|
|
470
|
+
'- id: 1-64 lowercase letters, digits, "_", or "-", starting with a letter or digit. Unique within the ask.',
|
|
471
|
+
"- label: at most 120 characters. description: optional, at most 280 characters.",
|
|
472
|
+
'- default: optional list of option ids to preselect, each listed once. With "one", at most one id; with "many", at most maxSelected ids.',
|
|
473
|
+
'- minSelected, maxSelected: optional whole numbers, "many" only. The user picks from minSelected (default 1, at least 0) to maxSelected (default: every choice) choices.',
|
|
474
|
+
'- allowOther: optional, "many" only. true adds a text field where the user types an answer of their own, up to 500 characters. It counts as one choice. otherLabel: optional label for that field, at most 120 characters. For one option or Other, use "many" with maxSelected 1.',
|
|
475
|
+
"- submitLabel: optional label for the submit button, at most 24 characters.",
|
|
476
|
+
"- allowDecline: optional, default true (the user sees Skip). Set it to false only when you cannot continue without an answer.",
|
|
477
|
+
'- Prefer select "one" with at most 3 options with labels of 20 characters or fewer; small screens show those as buttons.',
|
|
478
|
+
'- An accepted answer looks like {"action": "accept", "content": {"selected": ["<id>"]}}.',
|
|
479
|
+
'- With Other, it looks like {"action": "accept", "content": {"selected": ["<id>"], "other": "<text the user typed>"}}.',
|
|
480
|
+
].join("\n"),
|
|
481
|
+
].join("\n\n")
|
|
482
|
+
);
|
|
483
|
+
});
|
|
484
|
+
|
|
485
|
+
it("tells the model when to ask without naming a kind that is not enabled", () => {
|
|
486
|
+
expect(TERRENO_ASKS_SYSTEM_PROMPT).toContain(
|
|
487
|
+
"- When you need an answer that one of the ask tools listed below can collect, call that tool instead of asking in plain text.\n"
|
|
488
|
+
);
|
|
489
|
+
for (const surface of ["full", "compact"] as const) {
|
|
490
|
+
const prompt = buildAsksSystemPrompt({kinds: ["choice"], surface});
|
|
491
|
+
expect(prompt).not.toContain("approve");
|
|
492
|
+
expect(prompt).not.toContain("confirm");
|
|
493
|
+
expect(buildAsksSystemPrompt({kinds: ["choice", "confirm"], surface})).toContain(
|
|
494
|
+
"ask_confirm: the user approves or denies one action you describe in prompt."
|
|
495
|
+
);
|
|
496
|
+
}
|
|
497
|
+
});
|
|
498
|
+
|
|
499
|
+
it("describes ask_form only on the full surface", () => {
|
|
500
|
+
const formRules = "ask_form: the user fills in a few fields and submits them at once.";
|
|
501
|
+
expect(buildAsksSystemPrompt({kinds: ["choice", "form"]})).toContain(formRules);
|
|
502
|
+
expect(buildAsksSystemPrompt({kinds: ["choice"]})).not.toContain("ask_form");
|
|
503
|
+
expect(buildAsksSystemPrompt({kinds: ["choice", "form"], surface: "compact"})).not.toContain(
|
|
504
|
+
"ask_form"
|
|
505
|
+
);
|
|
506
|
+
});
|
|
507
|
+
|
|
508
|
+
it("describes ask_markdown only on the full surface", () => {
|
|
509
|
+
const markdownRules = "ask_markdown: the user edits a markdown draft you write";
|
|
510
|
+
expect(buildAsksSystemPrompt({kinds: ["choice", "markdown"]})).toContain(markdownRules);
|
|
511
|
+
expect(buildAsksSystemPrompt({kinds: ["choice"]})).not.toContain("ask_markdown");
|
|
512
|
+
expect(
|
|
513
|
+
buildAsksSystemPrompt({kinds: ["choice", "markdown"], surface: "compact"})
|
|
514
|
+
).not.toContain("ask_markdown");
|
|
515
|
+
});
|
|
516
|
+
});
|
|
517
|
+
|
|
518
|
+
describe("toStoredMessages", () => {
|
|
519
|
+
it("drops undefined fields and keeps empty objects", () => {
|
|
520
|
+
const messages: ModelMessage[] = [
|
|
521
|
+
{
|
|
522
|
+
content: [{...lookupCall, providerExecuted: undefined, providerOptions: undefined}],
|
|
523
|
+
providerOptions: undefined,
|
|
524
|
+
role: "assistant",
|
|
525
|
+
},
|
|
526
|
+
];
|
|
527
|
+
|
|
528
|
+
expect(toStoredMessages(messages)).toStrictEqual([
|
|
529
|
+
{
|
|
530
|
+
content: [
|
|
531
|
+
{input: {}, toolCallId: "call_lookup", toolName: "lookupPlans", type: "tool-call"},
|
|
532
|
+
],
|
|
533
|
+
role: "assistant",
|
|
534
|
+
},
|
|
535
|
+
]);
|
|
536
|
+
});
|
|
537
|
+
|
|
538
|
+
it("leaves out the fileData a tool result carries, as the stored rows do", () => {
|
|
539
|
+
const messages: ModelMessage[] = [
|
|
540
|
+
{
|
|
541
|
+
content: [
|
|
542
|
+
{
|
|
543
|
+
output: {
|
|
544
|
+
type: "json",
|
|
545
|
+
value: {fileData: "data:application/pdf;base64,AAAA", filename: "plan.pdf"},
|
|
546
|
+
},
|
|
547
|
+
toolCallId: "call_export",
|
|
548
|
+
toolName: "exportPlan",
|
|
549
|
+
type: "tool-result",
|
|
550
|
+
},
|
|
551
|
+
],
|
|
552
|
+
role: "tool",
|
|
553
|
+
},
|
|
554
|
+
];
|
|
555
|
+
|
|
556
|
+
expect(toStoredMessages(messages)).toStrictEqual([
|
|
557
|
+
{
|
|
558
|
+
content: [
|
|
559
|
+
{
|
|
560
|
+
output: {type: "json", value: {filename: "plan.pdf"}},
|
|
561
|
+
toolCallId: "call_export",
|
|
562
|
+
toolName: "exportPlan",
|
|
563
|
+
type: "tool-result",
|
|
564
|
+
},
|
|
565
|
+
],
|
|
566
|
+
role: "tool",
|
|
567
|
+
},
|
|
568
|
+
]);
|
|
569
|
+
});
|
|
570
|
+
});
|
|
571
|
+
|
|
572
|
+
describe("completePausedTurn", () => {
|
|
573
|
+
it("adds a tool message with the answer when the ask was the step's only call", () => {
|
|
574
|
+
expect(
|
|
575
|
+
completePausedTurn({
|
|
576
|
+
answer: TEAM_ANSWER,
|
|
577
|
+
responseMessages: [{content: [askCall("call_plan")], role: "assistant"}],
|
|
578
|
+
toolCallId: "call_plan",
|
|
579
|
+
})
|
|
580
|
+
).toEqual([
|
|
581
|
+
{content: [askCall("call_plan")], role: "assistant"},
|
|
582
|
+
{content: [answerResult("call_plan", TEAM_ANSWER)], role: "tool"},
|
|
583
|
+
]);
|
|
584
|
+
});
|
|
585
|
+
|
|
586
|
+
it("merges the answer into the step's tool message, in call order", () => {
|
|
587
|
+
expect(
|
|
588
|
+
completePausedTurn({
|
|
589
|
+
answer: TEAM_ANSWER,
|
|
590
|
+
responseMessages: [
|
|
591
|
+
{content: [askCall("call_plan"), lookupCall], role: "assistant"},
|
|
592
|
+
{content: [lookupResult], role: "tool"},
|
|
593
|
+
],
|
|
594
|
+
toolCallId: "call_plan",
|
|
595
|
+
})
|
|
596
|
+
).toEqual([
|
|
597
|
+
{content: [askCall("call_plan"), lookupCall], role: "assistant"},
|
|
598
|
+
{content: [answerResult("call_plan", TEAM_ANSWER), lookupResult], role: "tool"},
|
|
599
|
+
]);
|
|
600
|
+
});
|
|
601
|
+
|
|
602
|
+
it("answers a second ask in the step with cancel one_ask_at_a_time", () => {
|
|
603
|
+
expect(
|
|
604
|
+
completePausedTurn({
|
|
605
|
+
answer: {action: "decline"},
|
|
606
|
+
responseMessages: [
|
|
607
|
+
{content: [askCall("call_plan"), askCall("call_region")], role: "assistant"},
|
|
608
|
+
],
|
|
609
|
+
toolCallId: "call_plan",
|
|
610
|
+
})
|
|
611
|
+
).toEqual([
|
|
612
|
+
{content: [askCall("call_plan"), askCall("call_region")], role: "assistant"},
|
|
613
|
+
{
|
|
614
|
+
content: [
|
|
615
|
+
answerResult("call_plan", {action: "decline"}),
|
|
616
|
+
answerResult("call_region", {action: "cancel", reason: "one_ask_at_a_time"}),
|
|
617
|
+
],
|
|
618
|
+
role: "tool",
|
|
619
|
+
},
|
|
620
|
+
]);
|
|
621
|
+
});
|
|
622
|
+
|
|
623
|
+
it("gives another tool call that did not run an error result, and skips provider-run calls", () => {
|
|
624
|
+
const pickFileCall = {
|
|
625
|
+
input: {},
|
|
626
|
+
toolCallId: "call_file",
|
|
627
|
+
toolName: "pickFile",
|
|
628
|
+
type: "tool-call" as const,
|
|
629
|
+
};
|
|
630
|
+
const searchCall = {
|
|
631
|
+
input: {query: "plans"},
|
|
632
|
+
providerExecuted: true,
|
|
633
|
+
toolCallId: "call_search",
|
|
634
|
+
toolName: "web_search",
|
|
635
|
+
type: "tool-call" as const,
|
|
636
|
+
};
|
|
637
|
+
|
|
638
|
+
expect(
|
|
639
|
+
completePausedTurn({
|
|
640
|
+
answer: TEAM_ANSWER,
|
|
641
|
+
responseMessages: [
|
|
642
|
+
{content: [pickFileCall, searchCall, askCall("call_plan")], role: "assistant"},
|
|
643
|
+
],
|
|
644
|
+
toolCallId: "call_plan",
|
|
645
|
+
})
|
|
646
|
+
).toEqual([
|
|
647
|
+
{content: [pickFileCall, searchCall, askCall("call_plan")], role: "assistant"},
|
|
648
|
+
{
|
|
649
|
+
content: [
|
|
650
|
+
{
|
|
651
|
+
output: {
|
|
652
|
+
type: "error-text",
|
|
653
|
+
value: "This tool call did not run, so it has no result.",
|
|
654
|
+
},
|
|
655
|
+
toolCallId: "call_file",
|
|
656
|
+
toolName: "pickFile",
|
|
657
|
+
type: "tool-result",
|
|
658
|
+
},
|
|
659
|
+
answerResult("call_plan", TEAM_ANSWER),
|
|
660
|
+
],
|
|
661
|
+
role: "tool",
|
|
662
|
+
},
|
|
663
|
+
]);
|
|
664
|
+
});
|
|
665
|
+
|
|
666
|
+
it("keeps parts other than tool results after the results", () => {
|
|
667
|
+
const approval = {
|
|
668
|
+
approvalId: "approval_1",
|
|
669
|
+
approved: true,
|
|
670
|
+
type: "tool-approval-response" as const,
|
|
671
|
+
};
|
|
672
|
+
|
|
673
|
+
expect(
|
|
674
|
+
completePausedTurn({
|
|
675
|
+
answer: TEAM_ANSWER,
|
|
676
|
+
responseMessages: [
|
|
677
|
+
{content: [lookupCall, askCall("call_plan")], role: "assistant"},
|
|
678
|
+
{content: [approval, lookupResult], role: "tool"},
|
|
679
|
+
],
|
|
680
|
+
toolCallId: "call_plan",
|
|
681
|
+
})
|
|
682
|
+
).toEqual([
|
|
683
|
+
{content: [lookupCall, askCall("call_plan")], role: "assistant"},
|
|
684
|
+
{content: [lookupResult, answerResult("call_plan", TEAM_ANSWER), approval], role: "tool"},
|
|
685
|
+
]);
|
|
686
|
+
});
|
|
687
|
+
|
|
688
|
+
it("answers only the last step when an earlier ask in the turn was already answered", () => {
|
|
689
|
+
const earlier: ModelMessage[] = [
|
|
690
|
+
{content: [askCall("call_plan")], role: "assistant"},
|
|
691
|
+
{content: [answerResult("call_plan", TEAM_ANSWER)], role: "tool"} as ModelMessage,
|
|
692
|
+
];
|
|
693
|
+
|
|
694
|
+
expect(
|
|
695
|
+
completePausedTurn({
|
|
696
|
+
answer: {action: "decline"},
|
|
697
|
+
responseMessages: [
|
|
698
|
+
...earlier,
|
|
699
|
+
{content: "Got it.", role: "assistant"},
|
|
700
|
+
{content: [askCall("call_region")], role: "assistant"},
|
|
701
|
+
],
|
|
702
|
+
toolCallId: "call_region",
|
|
703
|
+
})
|
|
704
|
+
).toEqual([
|
|
705
|
+
...earlier,
|
|
706
|
+
{content: "Got it.", role: "assistant"},
|
|
707
|
+
{content: [askCall("call_region")], role: "assistant"},
|
|
708
|
+
{content: [answerResult("call_region", {action: "decline"})], role: "tool"},
|
|
709
|
+
]);
|
|
710
|
+
});
|
|
711
|
+
|
|
712
|
+
it("returns the messages unchanged when the last step has no call without a result", () => {
|
|
713
|
+
const responseMessages: ModelMessage[] = [
|
|
714
|
+
{content: "Be brief.", role: "system"},
|
|
715
|
+
{content: [lookupCall], role: "assistant"},
|
|
716
|
+
{content: [lookupResult], role: "tool"},
|
|
717
|
+
{content: "There are 2 plans.", role: "assistant"},
|
|
718
|
+
];
|
|
719
|
+
|
|
720
|
+
expect(
|
|
721
|
+
completePausedTurn({answer: TEAM_ANSWER, responseMessages, toolCallId: "call_plan"})
|
|
722
|
+
).toBe(responseMessages);
|
|
723
|
+
});
|
|
724
|
+
});
|
|
725
|
+
|
|
726
|
+
describe("completePausedTurn approvals", () => {
|
|
727
|
+
const deleteCall = {
|
|
728
|
+
input: {},
|
|
729
|
+
toolCallId: "call_delete",
|
|
730
|
+
toolName: "deleteTodos",
|
|
731
|
+
type: "tool-call" as const,
|
|
732
|
+
};
|
|
733
|
+
const approvalRequest = (approvalId: string, toolCallId: string) => ({
|
|
734
|
+
approvalId,
|
|
735
|
+
toolCallId,
|
|
736
|
+
type: "tool-approval-request" as const,
|
|
737
|
+
});
|
|
738
|
+
const approvalResponse = (approvalId: string, approved: boolean, reason?: string) => ({
|
|
739
|
+
approvalId,
|
|
740
|
+
approved,
|
|
741
|
+
type: "tool-approval-response" as const,
|
|
742
|
+
...(reason ? {reason} : {}),
|
|
743
|
+
});
|
|
744
|
+
|
|
745
|
+
it("answers the pending approval and adds no result for its call", () => {
|
|
746
|
+
const assistant: ModelMessage = {
|
|
747
|
+
content: [deleteCall, approvalRequest("approval_delete", "call_delete")],
|
|
748
|
+
role: "assistant",
|
|
749
|
+
};
|
|
750
|
+
|
|
751
|
+
expect(
|
|
752
|
+
completePausedTurn({
|
|
753
|
+
answer: {action: "accept", content: {confirmed: true}},
|
|
754
|
+
approvalId: "approval_delete",
|
|
755
|
+
responseMessages: [assistant],
|
|
756
|
+
toolCallId: "approval_delete",
|
|
757
|
+
})
|
|
758
|
+
).toEqual([assistant, {content: [approvalResponse("approval_delete", true)], role: "tool"}]);
|
|
759
|
+
});
|
|
760
|
+
|
|
761
|
+
it("denies every other approval with one_ask_at_a_time and answers the pending ask", () => {
|
|
762
|
+
const assistant: ModelMessage = {
|
|
763
|
+
content: [
|
|
764
|
+
askCall("call_plan"),
|
|
765
|
+
deleteCall,
|
|
766
|
+
approvalRequest("approval_delete", "call_delete"),
|
|
767
|
+
],
|
|
768
|
+
role: "assistant",
|
|
769
|
+
};
|
|
770
|
+
|
|
771
|
+
expect(
|
|
772
|
+
completePausedTurn({
|
|
773
|
+
answer: TEAM_ANSWER,
|
|
774
|
+
responseMessages: [assistant],
|
|
775
|
+
toolCallId: "call_plan",
|
|
776
|
+
})
|
|
777
|
+
).toEqual([
|
|
778
|
+
assistant,
|
|
779
|
+
{
|
|
780
|
+
content: [
|
|
781
|
+
answerResult("call_plan", TEAM_ANSWER),
|
|
782
|
+
approvalResponse("approval_delete", false, "one_ask_at_a_time"),
|
|
783
|
+
],
|
|
784
|
+
role: "tool",
|
|
785
|
+
},
|
|
786
|
+
]);
|
|
787
|
+
});
|
|
788
|
+
|
|
789
|
+
it("adds the approval response to the step's tool message after its results", () => {
|
|
790
|
+
const assistant: ModelMessage = {
|
|
791
|
+
content: [lookupCall, deleteCall, approvalRequest("approval_delete", "call_delete")],
|
|
792
|
+
role: "assistant",
|
|
793
|
+
};
|
|
794
|
+
|
|
795
|
+
expect(
|
|
796
|
+
completePausedTurn({
|
|
797
|
+
answer: {action: "accept", content: {confirmed: false}},
|
|
798
|
+
approvalId: "approval_delete",
|
|
799
|
+
responseMessages: [assistant, {content: [lookupResult], role: "tool"}],
|
|
800
|
+
toolCallId: "approval_delete",
|
|
801
|
+
})
|
|
802
|
+
).toEqual([
|
|
803
|
+
assistant,
|
|
804
|
+
{
|
|
805
|
+
content: [lookupResult, approvalResponse("approval_delete", false, "user_denied")],
|
|
806
|
+
role: "tool",
|
|
807
|
+
},
|
|
808
|
+
]);
|
|
809
|
+
});
|
|
810
|
+
|
|
811
|
+
it("leaves an approval that already has a response alone", () => {
|
|
812
|
+
const responseMessages: ModelMessage[] = [
|
|
813
|
+
{
|
|
814
|
+
content: [deleteCall, approvalRequest("approval_delete", "call_delete")],
|
|
815
|
+
role: "assistant",
|
|
816
|
+
},
|
|
817
|
+
{content: [approvalResponse("approval_delete", true)], role: "tool"},
|
|
818
|
+
];
|
|
819
|
+
|
|
820
|
+
expect(
|
|
821
|
+
completePausedTurn({
|
|
822
|
+
answer: {action: "decline"},
|
|
823
|
+
approvalId: "approval_delete",
|
|
824
|
+
responseMessages,
|
|
825
|
+
toolCallId: "approval_delete",
|
|
826
|
+
})
|
|
827
|
+
).toBe(responseMessages);
|
|
828
|
+
});
|
|
829
|
+
});
|
|
830
|
+
|
|
831
|
+
describe("completePausedTurn approval responses", () => {
|
|
832
|
+
it.each([
|
|
833
|
+
[{action: "accept", content: {confirmed: true}}, {approved: true}],
|
|
834
|
+
[
|
|
835
|
+
{action: "accept", content: {confirmed: false}},
|
|
836
|
+
{approved: false, reason: "user_denied"},
|
|
837
|
+
],
|
|
838
|
+
[{action: "decline"}, {approved: false, reason: "user_declined"}],
|
|
839
|
+
[{action: "cancel"}, {approved: false, reason: "user_cancelled"}],
|
|
840
|
+
[
|
|
841
|
+
{action: "cancel", reason: "closed"},
|
|
842
|
+
{approved: false, reason: "closed"},
|
|
843
|
+
],
|
|
844
|
+
] as const)("answers %j with %j", (answer, expected) => {
|
|
845
|
+
const assistant: ModelMessage = {
|
|
846
|
+
content: [
|
|
847
|
+
{input: {}, toolCallId: "call_delete", toolName: "deleteTodos", type: "tool-call"},
|
|
848
|
+
{approvalId: "approval_delete", toolCallId: "call_delete", type: "tool-approval-request"},
|
|
849
|
+
],
|
|
850
|
+
role: "assistant",
|
|
851
|
+
};
|
|
852
|
+
|
|
853
|
+
expect(
|
|
854
|
+
completePausedTurn({
|
|
855
|
+
answer: answer as AskResponse,
|
|
856
|
+
approvalId: "approval_delete",
|
|
857
|
+
responseMessages: [assistant],
|
|
858
|
+
toolCallId: "approval_delete",
|
|
859
|
+
})
|
|
860
|
+
).toEqual([
|
|
861
|
+
assistant,
|
|
862
|
+
{
|
|
863
|
+
content: [{approvalId: "approval_delete", type: "tool-approval-response", ...expected}],
|
|
864
|
+
role: "tool",
|
|
865
|
+
},
|
|
866
|
+
]);
|
|
867
|
+
});
|
|
868
|
+
});
|
|
869
|
+
|
|
870
|
+
describe("approvalAskInput", () => {
|
|
871
|
+
it("defaults to Allow <toolName>? with the description, cut to the prompt limit", () => {
|
|
872
|
+
const input = approvalAskInput({
|
|
873
|
+
asks: true,
|
|
874
|
+
description: "x".repeat(600),
|
|
875
|
+
input: {},
|
|
876
|
+
surface: "full",
|
|
877
|
+
toolName: "deleteTodos",
|
|
878
|
+
});
|
|
879
|
+
|
|
880
|
+
expect(input).toMatchObject({confirmLabel: "Allow", denyLabel: "Deny"});
|
|
881
|
+
expect(input.prompt.startsWith("Allow deleteTodos? xxx")).toBe(true);
|
|
882
|
+
expect(input.prompt).toHaveLength(ASK_LIMITS.promptMaxLength);
|
|
883
|
+
expect(input.prompt.endsWith("…")).toBe(true);
|
|
884
|
+
});
|
|
885
|
+
|
|
886
|
+
it("asks without a description when the tool has none", () => {
|
|
887
|
+
expect(
|
|
888
|
+
approvalAskInput({asks: {}, input: {}, surface: "compact", toolName: "deleteTodos"})
|
|
889
|
+
).toEqual({confirmLabel: "Allow", denyLabel: "Deny", prompt: "Allow deleteTodos?"});
|
|
890
|
+
});
|
|
891
|
+
|
|
892
|
+
it("uses the host's input for the tool and passes it the call's input", () => {
|
|
893
|
+
const calls: unknown[] = [];
|
|
894
|
+
const input = approvalAskInput({
|
|
895
|
+
asks: {
|
|
896
|
+
approvals: {
|
|
897
|
+
deleteTodos: (callInput) => {
|
|
898
|
+
calls.push(callInput);
|
|
899
|
+
return {destructive: true, prompt: "Delete 3 todos?"};
|
|
900
|
+
},
|
|
901
|
+
},
|
|
902
|
+
},
|
|
903
|
+
input: {count: 3},
|
|
904
|
+
surface: "full",
|
|
905
|
+
toolName: "deleteTodos",
|
|
906
|
+
});
|
|
907
|
+
|
|
908
|
+
expect(input).toEqual({destructive: true, prompt: "Delete 3 todos?"});
|
|
909
|
+
expect(calls).toEqual([{count: 3}]);
|
|
910
|
+
});
|
|
911
|
+
});
|
|
912
|
+
|
|
913
|
+
describe("deniedApprovalReasons", () => {
|
|
914
|
+
it("maps each denied approval in the last tool message to its call and reason", () => {
|
|
915
|
+
const messages: ModelMessage[] = [
|
|
916
|
+
{
|
|
917
|
+
content: [
|
|
918
|
+
{input: {}, toolCallId: "call_a", toolName: "a", type: "tool-call"},
|
|
919
|
+
{approvalId: "approval_a", toolCallId: "call_a", type: "tool-approval-request"},
|
|
920
|
+
{input: {}, toolCallId: "call_b", toolName: "b", type: "tool-call"},
|
|
921
|
+
{approvalId: "approval_b", toolCallId: "call_b", type: "tool-approval-request"},
|
|
922
|
+
],
|
|
923
|
+
role: "assistant",
|
|
924
|
+
},
|
|
925
|
+
{
|
|
926
|
+
content: [
|
|
927
|
+
{approvalId: "approval_a", approved: true, type: "tool-approval-response"},
|
|
928
|
+
{
|
|
929
|
+
approvalId: "approval_b",
|
|
930
|
+
approved: false,
|
|
931
|
+
reason: "one_ask_at_a_time",
|
|
932
|
+
type: "tool-approval-response",
|
|
933
|
+
},
|
|
934
|
+
],
|
|
935
|
+
role: "tool",
|
|
936
|
+
},
|
|
937
|
+
];
|
|
938
|
+
|
|
939
|
+
expect(deniedApprovalReasons(messages)).toEqual(new Map([["call_b", "one_ask_at_a_time"]]));
|
|
940
|
+
});
|
|
941
|
+
|
|
942
|
+
it("is empty when the last message is not a tool message", () => {
|
|
943
|
+
expect(deniedApprovalReasons([{content: "Hi", role: "user"}])).toEqual(new Map());
|
|
944
|
+
});
|
|
945
|
+
});
|
|
946
|
+
});
|