@terreno/ai 57.9.0 → 57.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -0
- package/bunfig.toml +1 -1
- package/dist/aiApp.d.ts +8 -2
- package/dist/aiApp.js +12 -6
- package/dist/harness/adminScreens.d.ts +6 -0
- package/dist/harness/adminScreens.js +11 -0
- package/dist/harness/agentLoop.d.ts +61 -0
- package/dist/harness/agentLoop.js +798 -0
- package/dist/harness/agentTaskNames.d.ts +4 -0
- package/dist/harness/agentTaskNames.js +7 -0
- package/dist/harness/approvalPolicy.d.ts +3 -0
- package/dist/harness/approvalPolicy.js +23 -0
- package/dist/harness/approvals.d.ts +101 -0
- package/dist/harness/approvals.js +468 -0
- package/dist/harness/commit.d.ts +238 -0
- package/dist/harness/commit.js +678 -0
- package/dist/harness/conversation.d.ts +75 -0
- package/dist/harness/conversation.js +397 -0
- package/dist/harness/defineAgent.d.ts +9 -0
- package/dist/harness/defineAgent.js +65 -0
- package/dist/harness/defineTask.d.ts +11 -0
- package/dist/harness/defineTask.js +59 -0
- package/dist/harness/defineTool.d.ts +7 -0
- package/dist/harness/defineTool.js +36 -0
- package/dist/harness/definitionError.d.ts +8 -0
- package/dist/harness/definitionError.js +15 -0
- package/dist/harness/errors.d.ts +133 -0
- package/dist/harness/errors.js +107 -0
- package/dist/harness/eventHub.d.ts +45 -0
- package/dist/harness/eventHub.js +135 -0
- package/dist/harness/events.d.ts +95 -0
- package/dist/harness/events.js +267 -0
- package/dist/harness/executionEnv.d.ts +31 -0
- package/dist/harness/executionEnv.js +2 -0
- package/dist/harness/extensions.d.ts +82 -0
- package/dist/harness/extensions.js +274 -0
- package/dist/harness/harness.d.ts +163 -0
- package/dist/harness/harness.js +522 -0
- package/dist/harness/harnessApp.d.ts +44 -0
- package/dist/harness/harnessApp.js +62 -0
- package/dist/harness/internalRuntime.d.ts +16 -0
- package/dist/harness/internalRuntime.js +18 -0
- package/dist/harness/leases.d.ts +35 -0
- package/dist/harness/leases.js +86 -0
- package/dist/harness/memo.d.ts +24 -0
- package/dist/harness/memo.js +78 -0
- package/dist/harness/modelCall.d.ts +45 -0
- package/dist/harness/modelCall.js +147 -0
- package/dist/harness/models/harnessApproval.d.ts +2 -0
- package/dist/harness/models/harnessApproval.js +100 -0
- package/dist/harness/models/harnessConversation.d.ts +2 -0
- package/dist/harness/models/harnessConversation.js +117 -0
- package/dist/harness/models/harnessEvent.d.ts +3 -0
- package/dist/harness/models/harnessEvent.js +73 -0
- package/dist/harness/models/harnessInboxEvent.d.ts +2 -0
- package/dist/harness/models/harnessInboxEvent.js +58 -0
- package/dist/harness/models/harnessMemo.d.ts +2 -0
- package/dist/harness/models/harnessMemo.js +42 -0
- package/dist/harness/models/harnessMessage.d.ts +2 -0
- package/dist/harness/models/harnessMessage.js +73 -0
- package/dist/harness/models/harnessOwner.d.ts +2 -0
- package/dist/harness/models/harnessOwner.js +36 -0
- package/dist/harness/models/harnessTask.d.ts +2 -0
- package/dist/harness/models/harnessTask.js +191 -0
- package/dist/harness/ownership.d.ts +89 -0
- package/dist/harness/ownership.js +438 -0
- package/dist/harness/registry.d.ts +29 -0
- package/dist/harness/registry.js +93 -0
- package/dist/harness/retryBackoff.d.ts +31 -0
- package/dist/harness/retryBackoff.js +50 -0
- package/dist/harness/routes/approvals.d.ts +13 -0
- package/dist/harness/routes/approvals.js +93 -0
- package/dist/harness/routes/conversations.d.ts +11 -0
- package/dist/harness/routes/conversations.js +66 -0
- package/dist/harness/routes/events.d.ts +19 -0
- package/dist/harness/routes/events.js +215 -0
- package/dist/harness/routes/tasks.d.ts +11 -0
- package/dist/harness/routes/tasks.js +108 -0
- package/dist/harness/runners/inProcessRunner.d.ts +64 -0
- package/dist/harness/runners/inProcessRunner.js +227 -0
- package/dist/harness/runners/jobsRunner.d.ts +62 -0
- package/dist/harness/runners/jobsRunner.js +212 -0
- package/dist/harness/runtime.d.ts +52 -0
- package/dist/harness/runtime.js +604 -0
- package/dist/harness/subagent.d.ts +53 -0
- package/dist/harness/subagent.js +134 -0
- package/dist/harness/suspend.d.ts +8 -0
- package/dist/harness/suspend.js +16 -0
- package/dist/harness/transaction.d.ts +3 -0
- package/dist/harness/transaction.js +22 -0
- package/dist/harness/waits.d.ts +100 -0
- package/dist/harness/waits.js +214 -0
- package/dist/index.d.ts +19 -1
- package/dist/index.js +45 -2
- package/dist/models/aiDataset.d.ts +2 -0
- package/dist/models/aiDataset.js +67 -0
- package/dist/models/aiRequest.js +1 -1
- package/dist/models/gptHistory.js +73 -0
- package/dist/observability/adminScreens.d.ts +5 -0
- package/dist/observability/adminScreens.js +32 -0
- package/dist/observability/compileTemplate.d.ts +1 -0
- package/dist/observability/compileTemplate.js +13 -0
- package/dist/observability/datasetImport.d.ts +11 -0
- package/dist/observability/datasetImport.js +185 -0
- package/dist/observability/evaluate.d.ts +31 -0
- package/dist/observability/evaluate.js +200 -0
- package/dist/observability/evaluatorTemplates.d.ts +17 -0
- package/dist/observability/evaluatorTemplates.js +111 -0
- package/dist/observability/jsonPath.d.ts +2 -0
- package/dist/observability/jsonPath.js +57 -0
- package/dist/observability/local/datasetStore.d.ts +87 -0
- package/dist/observability/local/datasetStore.js +326 -0
- package/dist/observability/local/evaluatorStore.d.ts +44 -0
- package/dist/observability/local/evaluatorStore.js +182 -0
- package/dist/observability/local/experimentRunner.d.ts +96 -0
- package/dist/observability/local/experimentRunner.js +419 -0
- package/dist/observability/local/localPlugin.d.ts +22 -0
- package/dist/observability/local/localPlugin.js +76 -0
- package/dist/observability/local/models/obsDataset.d.ts +2 -0
- package/dist/observability/local/models/obsDataset.js +37 -0
- package/dist/observability/local/models/obsDatasetItem.d.ts +2 -0
- package/dist/observability/local/models/obsDatasetItem.js +76 -0
- package/dist/observability/local/models/obsEvaluator.d.ts +2 -0
- package/dist/observability/local/models/obsEvaluator.js +98 -0
- package/dist/observability/local/models/obsExperiment.d.ts +2 -0
- package/dist/observability/local/models/obsExperiment.js +87 -0
- package/dist/observability/local/models/obsExperimentItem.d.ts +2 -0
- package/dist/observability/local/models/obsExperimentItem.js +45 -0
- package/dist/observability/local/models/obsPrompt.d.ts +2 -0
- package/dist/observability/local/models/obsPrompt.js +42 -0
- package/dist/observability/local/models/obsPromptLabel.d.ts +2 -0
- package/dist/observability/local/models/obsPromptLabel.js +39 -0
- package/dist/observability/local/models/obsPromptVersion.d.ts +2 -0
- package/dist/observability/local/models/obsPromptVersion.js +96 -0
- package/dist/observability/local/models/obsReviewItem.d.ts +2 -0
- package/dist/observability/local/models/obsReviewItem.js +75 -0
- package/dist/observability/local/models/obsScore.d.ts +2 -0
- package/dist/observability/local/models/obsScore.js +58 -0
- package/dist/observability/local/models/obsSpan.d.ts +2 -0
- package/dist/observability/local/models/obsSpan.js +64 -0
- package/dist/observability/local/models/obsTrace.d.ts +2 -0
- package/dist/observability/local/models/obsTrace.js +93 -0
- package/dist/observability/local/promptStore.d.ts +180 -0
- package/dist/observability/local/promptStore.js +444 -0
- package/dist/observability/local/reviewStore.d.ts +50 -0
- package/dist/observability/local/reviewStore.js +203 -0
- package/dist/observability/local/traceStore.d.ts +103 -0
- package/dist/observability/local/traceStore.js +325 -0
- package/dist/observability/observabilityApp.d.ts +24 -0
- package/dist/observability/observabilityApp.js +157 -0
- package/dist/observability/observabilityAppRegistry.d.ts +14 -0
- package/dist/observability/observabilityAppRegistry.js +16 -0
- package/dist/observability/permissions.d.ts +7 -0
- package/dist/observability/permissions.js +60 -0
- package/dist/observability/reviewPanels.d.ts +18 -0
- package/dist/observability/reviewPanels.js +46 -0
- package/dist/observability/routes/datasets.d.ts +7 -0
- package/dist/observability/routes/datasets.js +149 -0
- package/dist/observability/routes/evaluators.d.ts +7 -0
- package/dist/observability/routes/evaluators.js +123 -0
- package/dist/observability/routes/experiments.d.ts +7 -0
- package/dist/observability/routes/experiments.js +95 -0
- package/dist/observability/routes/observabilityRouteAccess.d.ts +13 -0
- package/dist/observability/routes/observabilityRouteAccess.js +67 -0
- package/dist/observability/routes/prompts.d.ts +12 -0
- package/dist/observability/routes/prompts.js +172 -0
- package/dist/observability/routes/requestAiService.d.ts +13 -0
- package/dist/observability/routes/requestAiService.js +21 -0
- package/dist/observability/routes/review.d.ts +7 -0
- package/dist/observability/routes/review.js +79 -0
- package/dist/observability/routes/status.d.ts +5 -0
- package/dist/observability/routes/status.js +31 -0
- package/dist/observability/routes/testMultiStage.d.ts +9 -0
- package/dist/observability/routes/testMultiStage.js +67 -0
- package/dist/observability/routes/traces.d.ts +7 -0
- package/dist/observability/routes/traces.js +129 -0
- package/dist/observability/schemaValidation.d.ts +5 -0
- package/dist/observability/schemaValidation.js +26 -0
- package/dist/observability/sopThresholds.d.ts +2 -0
- package/dist/observability/sopThresholds.js +21 -0
- package/dist/observability/status.d.ts +23 -0
- package/dist/observability/status.js +34 -0
- package/dist/observability/testMultiStageSchemas.d.ts +10 -0
- package/dist/observability/testMultiStageSchemas.js +79 -0
- package/dist/observability/testMultiStageWorkflow.d.ts +20 -0
- package/dist/observability/testMultiStageWorkflow.js +218 -0
- package/dist/observability/types.d.ts +159 -0
- package/dist/observability/types.js +67 -0
- package/dist/routes/files.js +18 -7
- package/dist/routes/gpt.js +79 -689
- package/dist/routes/gptActions.d.ts +9 -0
- package/dist/routes/gptActions.js +212 -0
- package/dist/routes/gptDatasets.d.ts +5 -0
- package/dist/routes/gptDatasets.js +94 -0
- package/dist/routes/gptHistories.d.ts +3 -1
- package/dist/routes/gptHistories.js +188 -2
- package/dist/service/aiDatasets.d.ts +57 -0
- package/dist/service/aiDatasets.js +253 -0
- package/dist/service/aiService.d.ts +50 -1
- package/dist/service/aiService.js +426 -77
- package/dist/service/askFiles.d.ts +49 -0
- package/dist/service/askFiles.js +162 -0
- package/dist/service/asks.d.ts +76 -0
- package/dist/service/asks.js +284 -0
- package/dist/service/chatTurn.d.ts +128 -0
- package/dist/service/chatTurn.js +1674 -0
- package/dist/service/fileStorage.d.ts +4 -0
- package/dist/service/fileStorage.js +7 -2
- package/dist/service/fillActionIds.d.ts +6 -0
- package/dist/service/fillActionIds.js +79 -0
- package/dist/service/jsonFenceModel.d.ts +7 -0
- package/dist/service/jsonFenceModel.js +45 -0
- package/dist/service/prompts.d.ts +52 -0
- package/dist/service/prompts.js +96 -1
- package/dist/service/sanitizeHtml.d.ts +11 -0
- package/dist/service/sanitizeHtml.js +108 -0
- package/dist/types/harness.d.ts +952 -0
- package/dist/types/harness.js +133 -0
- package/dist/types/index.d.ts +192 -14
- package/dist/types/index.js +2 -0
- package/dist/types/observability.d.ts +288 -0
- package/dist/types/observability.js +2 -0
- package/package.json +28 -5
- package/src/aiApp.test.ts +207 -1
- package/src/aiApp.ts +19 -6
- package/src/harness/adminScreens.test.ts +34 -0
- package/src/harness/adminScreens.ts +11 -0
- package/src/harness/agentLoop.test.ts +1034 -0
- package/src/harness/agentLoop.ts +1082 -0
- package/src/harness/agentRecovery.test.ts +380 -0
- package/src/harness/agentTaskNames.ts +4 -0
- package/src/harness/approvalPolicy.ts +28 -0
- package/src/harness/approvals.test.ts +1094 -0
- package/src/harness/approvals.ts +644 -0
- package/src/harness/commit.ts +1049 -0
- package/src/harness/concurrency.test.ts +155 -0
- package/src/harness/conversation.ts +503 -0
- package/src/harness/defineAgent.test.ts +92 -0
- package/src/harness/defineAgent.ts +82 -0
- package/src/harness/defineTask.test.ts +59 -0
- package/src/harness/defineTask.ts +76 -0
- package/src/harness/defineTool.ts +45 -0
- package/src/harness/definitionError.ts +13 -0
- package/src/harness/errors.test.ts +90 -0
- package/src/harness/errors.ts +125 -0
- package/src/harness/eventHub.test.ts +124 -0
- package/src/harness/eventHub.ts +159 -0
- package/src/harness/events.test.ts +769 -0
- package/src/harness/events.ts +384 -0
- package/src/harness/executionEnv.ts +34 -0
- package/src/harness/extensions.test.ts +1218 -0
- package/src/harness/extensions.ts +382 -0
- package/src/harness/harness.test.ts +693 -0
- package/src/harness/harness.ts +778 -0
- package/src/harness/harnessApp.ts +76 -0
- package/src/harness/internalRuntime.ts +44 -0
- package/src/harness/leases.ts +132 -0
- package/src/harness/memo.ts +107 -0
- package/src/harness/modelCall.test.ts +134 -0
- package/src/harness/modelCall.ts +200 -0
- package/src/harness/models/harnessApproval.ts +109 -0
- package/src/harness/models/harnessConversation.ts +142 -0
- package/src/harness/models/harnessEvent.ts +92 -0
- package/src/harness/models/harnessInboxEvent.ts +73 -0
- package/src/harness/models/harnessMemo.ts +43 -0
- package/src/harness/models/harnessMessage.ts +85 -0
- package/src/harness/models/harnessModels.test.ts +60 -0
- package/src/harness/models/harnessOwner.ts +40 -0
- package/src/harness/models/harnessTask.ts +206 -0
- package/src/harness/ownership.test.ts +1087 -0
- package/src/harness/ownership.ts +618 -0
- package/src/harness/recovery.test.ts +907 -0
- package/src/harness/registry.ts +124 -0
- package/src/harness/retries.test.ts +292 -0
- package/src/harness/retryBackoff.test.ts +100 -0
- package/src/harness/retryBackoff.ts +77 -0
- package/src/harness/routes/approvals.ts +123 -0
- package/src/harness/routes/conversations.test.ts +518 -0
- package/src/harness/routes/conversations.ts +102 -0
- package/src/harness/routes/events.ts +280 -0
- package/src/harness/routes/tasks.test.ts +198 -0
- package/src/harness/routes/tasks.ts +146 -0
- package/src/harness/runners/inProcessRunner.test.ts +530 -0
- package/src/harness/runners/inProcessRunner.ts +260 -0
- package/src/harness/runners/jobsRunner.test.ts +709 -0
- package/src/harness/runners/jobsRunner.ts +252 -0
- package/src/harness/runtime.ts +802 -0
- package/src/harness/subagent.test.ts +696 -0
- package/src/harness/subagent.ts +192 -0
- package/src/harness/suspend.ts +13 -0
- package/src/harness/transaction.ts +17 -0
- package/src/harness/versioning.test.ts +288 -0
- package/src/harness/waits.test.ts +693 -0
- package/src/harness/waits.ts +356 -0
- package/src/index.test.ts +13 -0
- package/src/index.ts +70 -0
- package/src/isolated/adminScreensConfig.isolated.ts +143 -0
- package/src/models/aiDataset.test.ts +34 -0
- package/src/models/aiDataset.ts +78 -0
- package/src/models/aiRequest.ts +2 -1
- package/src/models/gptHistory.test.ts +156 -0
- package/src/models/gptHistory.ts +89 -1
- package/src/observability/adminScreens.test.ts +71 -0
- package/src/observability/adminScreens.ts +42 -0
- package/src/observability/compileTemplate.test.ts +14 -0
- package/src/observability/compileTemplate.ts +10 -0
- package/src/observability/datasetImport.test.ts +95 -0
- package/src/observability/datasetImport.ts +220 -0
- package/src/observability/evaluate.test.ts +549 -0
- package/src/observability/evaluate.ts +254 -0
- package/src/observability/evaluatorTemplates.ts +124 -0
- package/src/observability/jsonPath.test.ts +52 -0
- package/src/observability/jsonPath.ts +60 -0
- package/src/observability/local/datasetStore.test.ts +230 -0
- package/src/observability/local/datasetStore.ts +452 -0
- package/src/observability/local/evaluatorStore.test.ts +138 -0
- package/src/observability/local/evaluatorStore.ts +224 -0
- package/src/observability/local/experimentRunner.test.ts +489 -0
- package/src/observability/local/experimentRunner.ts +548 -0
- package/src/observability/local/localPlugin.ts +88 -0
- package/src/observability/local/models/obsDataset.ts +38 -0
- package/src/observability/local/models/obsDatasetItem.ts +80 -0
- package/src/observability/local/models/obsEvaluator.ts +102 -0
- package/src/observability/local/models/obsExperiment.ts +91 -0
- package/src/observability/local/models/obsExperimentItem.ts +52 -0
- package/src/observability/local/models/obsPrompt.ts +43 -0
- package/src/observability/local/models/obsPromptLabel.ts +43 -0
- package/src/observability/local/models/obsPromptVersion.ts +107 -0
- package/src/observability/local/models/obsReviewItem.ts +79 -0
- package/src/observability/local/models/obsScore.ts +59 -0
- package/src/observability/local/models/obsSpan.ts +65 -0
- package/src/observability/local/models/obsTrace.ts +94 -0
- package/src/observability/local/obsModels.test.ts +119 -0
- package/src/observability/local/promptStore.test.ts +351 -0
- package/src/observability/local/promptStore.ts +665 -0
- package/src/observability/local/reviewStore.test.ts +164 -0
- package/src/observability/local/reviewStore.ts +264 -0
- package/src/observability/local/traceStore.test.ts +264 -0
- package/src/observability/local/traceStore.ts +440 -0
- package/src/observability/observabilityApp.test.ts +92 -0
- package/src/observability/observabilityApp.ts +185 -0
- package/src/observability/observabilityAppRegistry.test.ts +30 -0
- package/src/observability/observabilityAppRegistry.ts +35 -0
- package/src/observability/permissions.test.ts +96 -0
- package/src/observability/permissions.ts +78 -0
- package/src/observability/reviewPanels.test.ts +35 -0
- package/src/observability/reviewPanels.ts +64 -0
- package/src/observability/routes/datasets.test.ts +176 -0
- package/src/observability/routes/datasets.ts +252 -0
- package/src/observability/routes/evaluators.test.ts +119 -0
- package/src/observability/routes/evaluators.ts +207 -0
- package/src/observability/routes/experiments.test.ts +282 -0
- package/src/observability/routes/experiments.ts +152 -0
- package/src/observability/routes/observabilityRbac.test.ts +767 -0
- package/src/observability/routes/observabilityRouteAccess.ts +106 -0
- package/src/observability/routes/prompts.test.ts +320 -0
- package/src/observability/routes/prompts.ts +263 -0
- package/src/observability/routes/requestAiService.ts +36 -0
- package/src/observability/routes/review.test.ts +120 -0
- package/src/observability/routes/review.ts +125 -0
- package/src/observability/routes/status.test.ts +116 -0
- package/src/observability/routes/status.ts +47 -0
- package/src/observability/routes/testMultiStage.test.ts +214 -0
- package/src/observability/routes/testMultiStage.ts +96 -0
- package/src/observability/routes/traces.test.ts +167 -0
- package/src/observability/routes/traces.ts +172 -0
- package/src/observability/schemaValidation.ts +29 -0
- package/src/observability/sopThresholds.ts +20 -0
- package/src/observability/status.test.ts +38 -0
- package/src/observability/status.ts +56 -0
- package/src/observability/testMultiStageSchemas.ts +95 -0
- package/src/observability/testMultiStageWorkflow.test.ts +114 -0
- package/src/observability/testMultiStageWorkflow.ts +307 -0
- package/src/observability/types.ts +262 -0
- package/src/routes/files.test.ts +63 -9
- package/src/routes/files.ts +21 -7
- package/src/routes/gpt.test.ts +3279 -0
- package/src/routes/gpt.ts +91 -816
- package/src/routes/gptActions.test.ts +241 -0
- package/src/routes/gptActions.ts +255 -0
- package/src/routes/gptApprovals.test.ts +762 -0
- package/src/routes/gptDatasets.test.ts +86 -0
- package/src/routes/gptDatasets.ts +104 -0
- package/src/routes/gptFiles.test.ts +589 -0
- package/src/routes/gptHistories.test.ts +933 -0
- package/src/routes/gptHistories.ts +266 -5
- package/src/routes/routes.test.ts +222 -2
- package/src/service/aiDatasets.test.ts +123 -0
- package/src/service/aiDatasets.ts +316 -0
- package/src/service/aiService.test.ts +593 -0
- package/src/service/aiService.ts +568 -111
- package/src/service/askFiles.test.ts +96 -0
- package/src/service/askFiles.ts +222 -0
- package/src/service/asks.test.ts +946 -0
- package/src/service/asks.ts +423 -0
- package/src/service/chatTurn.ts +2287 -0
- package/src/service/fileStorage.test.ts +11 -0
- package/src/service/fileStorage.ts +10 -2
- package/src/service/fillActionIds.test.ts +65 -0
- package/src/service/fillActionIds.ts +98 -0
- package/src/service/jsonFenceModel.test.ts +47 -0
- package/src/service/jsonFenceModel.ts +53 -0
- package/src/service/prompts.test.ts +21 -0
- package/src/service/prompts.ts +147 -0
- package/src/service/sanitizeHtml.test.ts +61 -0
- package/src/service/sanitizeHtml.ts +109 -0
- package/src/tests/bunSetup.test.ts +7 -4
- package/src/tests/bunSetup.ts +7 -7
- package/src/tests/chatHarness.ts +478 -0
- package/src/tests/generateStream.ts +49 -0
- package/src/tests/harnessErrors.ts +22 -0
- package/src/tests/harnessStreaming.ts +227 -0
- package/src/tests/helpers.ts +9 -2
- package/src/types/harness.ts +1119 -0
- package/src/types/index.ts +209 -13
- package/src/types/observability.ts +344 -0
- package/src/types/sanitize-html.d.ts +25 -0
|
@@ -0,0 +1,589 @@
|
|
|
1
|
+
import {afterEach, beforeAll, describe, expect, it, mock} from "bun:test";
|
|
2
|
+
import mongoose from "mongoose";
|
|
3
|
+
|
|
4
|
+
import {AIRequest} from "../models/aiRequest";
|
|
5
|
+
import {FileAttachment} from "../models/fileAttachment";
|
|
6
|
+
import {GptHistory} from "../models/gptHistory";
|
|
7
|
+
import {
|
|
8
|
+
type Agent,
|
|
9
|
+
buildApp,
|
|
10
|
+
conversationOf,
|
|
11
|
+
createScriptedModel,
|
|
12
|
+
loadHistory,
|
|
13
|
+
modelCall,
|
|
14
|
+
onlyHistoryId,
|
|
15
|
+
pendingAskOf,
|
|
16
|
+
type ScriptedModel,
|
|
17
|
+
streamPrompt,
|
|
18
|
+
systemPromptOf,
|
|
19
|
+
textStep,
|
|
20
|
+
toolCallStep,
|
|
21
|
+
toolNamesOf,
|
|
22
|
+
} from "../tests/chatHarness";
|
|
23
|
+
import {authAsUser, ensureTestUsers, UserModel} from "../tests/helpers";
|
|
24
|
+
import type {AskFileDownloader, GptRouteOptions} from "../types";
|
|
25
|
+
|
|
26
|
+
const RECEIPT_ASK_INPUT = {
|
|
27
|
+
accept: ["image", "text", "csv"],
|
|
28
|
+
maxFiles: 3,
|
|
29
|
+
prompt: "Upload the receipt for this expense.",
|
|
30
|
+
title: "Receipt",
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
const RECEIPT_ASK_CALL = {
|
|
34
|
+
input: RECEIPT_ASK_INPUT,
|
|
35
|
+
toolCallId: "call_receipt",
|
|
36
|
+
toolName: "ask_files",
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const RECEIPT_CARD = {
|
|
40
|
+
buttons: [{id: "skip", label: "Skip", response: {action: "decline"}, style: "cancel"}],
|
|
41
|
+
handoff: true,
|
|
42
|
+
kind: "files",
|
|
43
|
+
text: "Upload the receipt for this expense.",
|
|
44
|
+
title: "Receipt",
|
|
45
|
+
toolCallId: "call_receipt",
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
const PNG_BYTES = Buffer.from([
|
|
49
|
+
0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d,
|
|
50
|
+
]);
|
|
51
|
+
const PNG_BASE64 = PNG_BYTES.toString("base64");
|
|
52
|
+
const RECEIPT_TEXT = "Coffee, 4.50\nBagel, 3.25\n";
|
|
53
|
+
const RECEIPT_TEXT_BASE64 = Buffer.from(RECEIPT_TEXT).toString("base64");
|
|
54
|
+
|
|
55
|
+
const dataUrl = (mimeType: string, bytes: Buffer): string =>
|
|
56
|
+
`data:${mimeType};base64,${bytes.toString("base64")}`;
|
|
57
|
+
|
|
58
|
+
const fakeDownloader = (files: Record<string, Buffer>): AskFileDownloader & {calls: string[]} => {
|
|
59
|
+
const calls: string[] = [];
|
|
60
|
+
return {
|
|
61
|
+
calls,
|
|
62
|
+
download: mock(async (gcsKey: string) => {
|
|
63
|
+
calls.push(gcsKey);
|
|
64
|
+
const bytes = files[gcsKey];
|
|
65
|
+
if (!bytes) {
|
|
66
|
+
expect.unreachable(`No stored file at ${gcsKey}`);
|
|
67
|
+
}
|
|
68
|
+
return bytes;
|
|
69
|
+
}),
|
|
70
|
+
};
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
const userIdOf = async (email: string): Promise<mongoose.Types.ObjectId> => {
|
|
74
|
+
const [user] = await UserModel.find({email});
|
|
75
|
+
if (!user) {
|
|
76
|
+
expect.unreachable(`No test user ${email}`);
|
|
77
|
+
}
|
|
78
|
+
return user._id as mongoose.Types.ObjectId;
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
const createUpload = async ({
|
|
82
|
+
filename,
|
|
83
|
+
gcsKey,
|
|
84
|
+
mimeType,
|
|
85
|
+
size,
|
|
86
|
+
userId,
|
|
87
|
+
}: {
|
|
88
|
+
filename: string;
|
|
89
|
+
gcsKey: string;
|
|
90
|
+
mimeType: string;
|
|
91
|
+
size: number;
|
|
92
|
+
userId: mongoose.Types.ObjectId;
|
|
93
|
+
}): Promise<string> => {
|
|
94
|
+
const upload = await FileAttachment.create({
|
|
95
|
+
filename,
|
|
96
|
+
gcsKey,
|
|
97
|
+
mimeType,
|
|
98
|
+
size,
|
|
99
|
+
url: `https://storage.googleapis.com/test-bucket/${gcsKey}`,
|
|
100
|
+
userId,
|
|
101
|
+
});
|
|
102
|
+
return upload._id.toString();
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
const pauseOnReceiptAsk = async ({
|
|
106
|
+
replyText = "Got your receipt.",
|
|
107
|
+
...routeOptions
|
|
108
|
+
}: {replyText?: string} & Partial<GptRouteOptions>): Promise<{
|
|
109
|
+
agent: Agent;
|
|
110
|
+
historyId: string;
|
|
111
|
+
model: ScriptedModel;
|
|
112
|
+
}> => {
|
|
113
|
+
const model = createScriptedModel({
|
|
114
|
+
steps: [toolCallStep(RECEIPT_ASK_CALL), textStep(replyText), textStep("Anything else?")],
|
|
115
|
+
});
|
|
116
|
+
const agent = await authAsUser(buildApp({asks: true, model, ...routeOptions}), "notAdmin");
|
|
117
|
+
const asked = await streamPrompt(agent, {prompt: "Log my lunch expense"});
|
|
118
|
+
const historyId = await onlyHistoryId();
|
|
119
|
+
expect(asked.events).toEqual([
|
|
120
|
+
{
|
|
121
|
+
ask: {
|
|
122
|
+
input: RECEIPT_ASK_INPUT,
|
|
123
|
+
kind: "files",
|
|
124
|
+
simple: RECEIPT_CARD,
|
|
125
|
+
toolCallId: "call_receipt",
|
|
126
|
+
},
|
|
127
|
+
historyId,
|
|
128
|
+
},
|
|
129
|
+
{done: true, historyId, pendingAsk: {toolCallId: "call_receipt"}},
|
|
130
|
+
]);
|
|
131
|
+
return {agent, historyId, model};
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
const answerFiles = async (
|
|
135
|
+
agent: Agent,
|
|
136
|
+
historyId: string,
|
|
137
|
+
files: Record<string, unknown>[]
|
|
138
|
+
): Promise<{body: Record<string, unknown>; status: number}> => {
|
|
139
|
+
const res = await agent.post("/gpt/prompt").send({
|
|
140
|
+
askResponse: {action: "accept", content: {files}, toolCallId: "call_receipt"},
|
|
141
|
+
historyId,
|
|
142
|
+
});
|
|
143
|
+
return {body: res.body as Record<string, unknown>, status: res.status};
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
const fieldCodes = (body: Record<string, unknown>): {code: string; path: string}[] =>
|
|
147
|
+
(body.fields as {code: string; path: string}[]).map(({code, path}) => ({code, path}));
|
|
148
|
+
|
|
149
|
+
/** The tool result the model saw on its `index`th call for the receipt ask. */
|
|
150
|
+
const receiptResultOf = (model: ScriptedModel, index: number): unknown => {
|
|
151
|
+
const toolMessage = conversationOf(modelCall(model, index)).find(
|
|
152
|
+
(message) =>
|
|
153
|
+
message.role === "tool" &&
|
|
154
|
+
Array.isArray(message.content) &&
|
|
155
|
+
message.content.some((part) => (part as {toolCallId?: string}).toolCallId === "call_receipt")
|
|
156
|
+
);
|
|
157
|
+
if (!toolMessage) {
|
|
158
|
+
expect.unreachable(`Model call ${index} has no result for the receipt ask`);
|
|
159
|
+
}
|
|
160
|
+
return (toolMessage.content as {output: unknown}[])[0].output;
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
const storedResultOf = async (historyId: string): Promise<unknown> => {
|
|
164
|
+
const history = await loadHistory(historyId);
|
|
165
|
+
const row = history.prompts.find(
|
|
166
|
+
(prompt) => prompt.type === "tool-result" && prompt.toolCallId === "call_receipt"
|
|
167
|
+
);
|
|
168
|
+
return JSON.parse(JSON.stringify(row?.result));
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
describe("/gpt/prompt files asks", () => {
|
|
172
|
+
let userId: mongoose.Types.ObjectId;
|
|
173
|
+
let otherUserId: mongoose.Types.ObjectId;
|
|
174
|
+
|
|
175
|
+
beforeAll(async () => {
|
|
176
|
+
await ensureTestUsers();
|
|
177
|
+
userId = await userIdOf("notAdmin@example.com");
|
|
178
|
+
otherUserId = await userIdOf("admin@example.com");
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
afterEach(async () => {
|
|
182
|
+
await AIRequest.deleteMany({});
|
|
183
|
+
await FileAttachment.deleteMany({});
|
|
184
|
+
await GptHistory.deleteMany({});
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
describe("with data URLs", () => {
|
|
188
|
+
it("sends an image as image-data and a text file as text, and stores only their metadata", async () => {
|
|
189
|
+
const {agent, historyId, model} = await pauseOnReceiptAsk({});
|
|
190
|
+
|
|
191
|
+
const {events} = await streamPrompt(agent, {
|
|
192
|
+
askResponse: {
|
|
193
|
+
action: "accept",
|
|
194
|
+
content: {
|
|
195
|
+
files: [
|
|
196
|
+
{
|
|
197
|
+
filename: "receipt.png",
|
|
198
|
+
mimeType: "image/png",
|
|
199
|
+
size: PNG_BYTES.length,
|
|
200
|
+
url: `data:image/png;base64,${PNG_BASE64}`,
|
|
201
|
+
},
|
|
202
|
+
{
|
|
203
|
+
filename: "items.txt",
|
|
204
|
+
mimeType: "text/plain",
|
|
205
|
+
size: RECEIPT_TEXT.length,
|
|
206
|
+
url: `data:text/plain;base64,${RECEIPT_TEXT_BASE64}`,
|
|
207
|
+
},
|
|
208
|
+
],
|
|
209
|
+
},
|
|
210
|
+
toolCallId: "call_receipt",
|
|
211
|
+
},
|
|
212
|
+
historyId,
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
expect(events).toEqual([
|
|
216
|
+
{askResolved: {action: "accept", toolCallId: "call_receipt"}},
|
|
217
|
+
{text: "Got your receipt."},
|
|
218
|
+
{done: true, historyId, title: "Workspace setup"},
|
|
219
|
+
]);
|
|
220
|
+
const storedAnswer = {
|
|
221
|
+
action: "accept",
|
|
222
|
+
content: {
|
|
223
|
+
files: [
|
|
224
|
+
{filename: "receipt.png", mimeType: "image/png", size: PNG_BYTES.length},
|
|
225
|
+
{filename: "items.txt", mimeType: "text/plain", size: RECEIPT_TEXT.length},
|
|
226
|
+
],
|
|
227
|
+
},
|
|
228
|
+
};
|
|
229
|
+
expect(receiptResultOf(model, 1)).toEqual({
|
|
230
|
+
type: "content",
|
|
231
|
+
value: [
|
|
232
|
+
{text: JSON.stringify(storedAnswer), type: "text"},
|
|
233
|
+
{text: "File 1 of 2: receipt.png (image/png, 12 bytes)", type: "text"},
|
|
234
|
+
{data: PNG_BASE64, mediaType: "image/png", type: "image-data"},
|
|
235
|
+
{text: "File 2 of 2: items.txt (text/plain, 25 bytes)", type: "text"},
|
|
236
|
+
{text: RECEIPT_TEXT, type: "text"},
|
|
237
|
+
],
|
|
238
|
+
});
|
|
239
|
+
expect(await storedResultOf(historyId)).toEqual(storedAnswer);
|
|
240
|
+
const history = await loadHistory(historyId);
|
|
241
|
+
expect(pendingAskOf(history)).toBeUndefined();
|
|
242
|
+
expect(JSON.stringify(history.toObject())).not.toContain(PNG_BASE64);
|
|
243
|
+
const [, logged] = await AIRequest.find({}).sort({created: 1});
|
|
244
|
+
expect(logged.prompt).toBe(JSON.stringify(storedAnswer));
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
it("replays only the stored metadata on the next turn", async () => {
|
|
248
|
+
const {agent, historyId, model} = await pauseOnReceiptAsk({});
|
|
249
|
+
await answerFiles(agent, historyId, [
|
|
250
|
+
{
|
|
251
|
+
filename: "receipt.png",
|
|
252
|
+
mimeType: "image/png",
|
|
253
|
+
size: PNG_BYTES.length,
|
|
254
|
+
url: `data:image/png;base64,${PNG_BASE64}`,
|
|
255
|
+
},
|
|
256
|
+
]);
|
|
257
|
+
|
|
258
|
+
await streamPrompt(agent, {historyId, prompt: "Thanks"});
|
|
259
|
+
|
|
260
|
+
expect(receiptResultOf(model, 2)).toEqual({
|
|
261
|
+
type: "json",
|
|
262
|
+
value: {
|
|
263
|
+
action: "accept",
|
|
264
|
+
content: {files: [{filename: "receipt.png", mimeType: "image/png", size: 12}]},
|
|
265
|
+
},
|
|
266
|
+
});
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
it("cuts a text file to its first 100 KB and says so", async () => {
|
|
270
|
+
const {agent, historyId, model} = await pauseOnReceiptAsk({});
|
|
271
|
+
const longText = Buffer.from(`${"a".repeat(99_999)}é${"b".repeat(50)}`);
|
|
272
|
+
|
|
273
|
+
await answerFiles(agent, historyId, [
|
|
274
|
+
{
|
|
275
|
+
filename: "ledger.csv",
|
|
276
|
+
mimeType: "text/csv",
|
|
277
|
+
size: longText.length,
|
|
278
|
+
url: dataUrl("text/csv", longText),
|
|
279
|
+
},
|
|
280
|
+
]);
|
|
281
|
+
|
|
282
|
+
const output = receiptResultOf(model, 1) as {value: {text?: string}[]};
|
|
283
|
+
expect(output.value.slice(1)).toEqual([
|
|
284
|
+
{text: "File 1 of 1: ledger.csv (text/csv, 100051 bytes)", type: "text"},
|
|
285
|
+
{text: "a".repeat(99_999), type: "text"},
|
|
286
|
+
{text: "[The file is cut to its first 99999 of 100051 bytes.]", type: "text"},
|
|
287
|
+
]);
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
it("returns 400 MIME_MISMATCH when the bytes are not the declared type, and keeps the ask pending", async () => {
|
|
291
|
+
const {agent, historyId, model} = await pauseOnReceiptAsk({});
|
|
292
|
+
|
|
293
|
+
const {body, status} = await answerFiles(agent, historyId, [
|
|
294
|
+
{
|
|
295
|
+
filename: "receipt.png",
|
|
296
|
+
mimeType: "image/png",
|
|
297
|
+
size: RECEIPT_TEXT.length,
|
|
298
|
+
url: `data:image/png;base64,${RECEIPT_TEXT_BASE64}`,
|
|
299
|
+
},
|
|
300
|
+
]);
|
|
301
|
+
|
|
302
|
+
expect(status).toBe(400);
|
|
303
|
+
expect(body.title).toBe("Invalid askResponse");
|
|
304
|
+
expect(body.fields).toEqual([
|
|
305
|
+
{
|
|
306
|
+
code: "MIME_MISMATCH",
|
|
307
|
+
fix: "Send the file with its real type, or a file that is image/png.",
|
|
308
|
+
message: "The file is declared as image/png, but its bytes are text.",
|
|
309
|
+
path: "content.files[0].mimeType",
|
|
310
|
+
},
|
|
311
|
+
]);
|
|
312
|
+
expect(model.doStream).toHaveBeenCalledTimes(1);
|
|
313
|
+
expect((await loadHistory(historyId)).pendingAsk?.toolCallId).toBe("call_receipt");
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
it("returns 400 FILE_TYPE_NOT_ACCEPTED and FILE_COUNT before loading any bytes", async () => {
|
|
317
|
+
const {agent, historyId, model} = await pauseOnReceiptAsk({});
|
|
318
|
+
const pdf = {
|
|
319
|
+
filename: "receipt.pdf",
|
|
320
|
+
mimeType: "application/pdf",
|
|
321
|
+
size: 5,
|
|
322
|
+
url: dataUrl("application/pdf", Buffer.from("%PDF-")),
|
|
323
|
+
};
|
|
324
|
+
|
|
325
|
+
const typed = await answerFiles(agent, historyId, [pdf]);
|
|
326
|
+
const counted = await answerFiles(agent, historyId, []);
|
|
327
|
+
|
|
328
|
+
expect(typed.status).toBe(400);
|
|
329
|
+
expect(fieldCodes(typed.body)).toEqual([
|
|
330
|
+
{code: "FILE_TYPE_NOT_ACCEPTED", path: "content.files[0].mimeType"},
|
|
331
|
+
]);
|
|
332
|
+
expect(counted.status).toBe(400);
|
|
333
|
+
expect(fieldCodes(counted.body)).toEqual([{code: "FILE_COUNT", path: "content.files"}]);
|
|
334
|
+
expect(model.doStream).toHaveBeenCalledTimes(1);
|
|
335
|
+
});
|
|
336
|
+
|
|
337
|
+
it("returns 400 FILE_TOO_LARGE against asks.maxFileSizeBytes", async () => {
|
|
338
|
+
const {agent, historyId} = await pauseOnReceiptAsk({asks: {maxFileSizeBytes: 16}});
|
|
339
|
+
|
|
340
|
+
const {body, status} = await answerFiles(agent, historyId, [
|
|
341
|
+
{
|
|
342
|
+
filename: "items.txt",
|
|
343
|
+
mimeType: "text/plain",
|
|
344
|
+
size: RECEIPT_TEXT.length,
|
|
345
|
+
url: `data:text/plain;base64,${RECEIPT_TEXT_BASE64}`,
|
|
346
|
+
},
|
|
347
|
+
]);
|
|
348
|
+
|
|
349
|
+
expect(status).toBe(400);
|
|
350
|
+
expect(fieldCodes(body)).toEqual([{code: "FILE_TOO_LARGE", path: "content.files[0].size"}]);
|
|
351
|
+
});
|
|
352
|
+
|
|
353
|
+
it("returns 400 FILE_NOT_OWNED for a fileId when the host has no file storage", async () => {
|
|
354
|
+
const {agent, historyId} = await pauseOnReceiptAsk({});
|
|
355
|
+
const fileId = await createUpload({
|
|
356
|
+
filename: "receipt.png",
|
|
357
|
+
gcsKey: "uploads/receipt.png",
|
|
358
|
+
mimeType: "image/png",
|
|
359
|
+
size: PNG_BYTES.length,
|
|
360
|
+
userId,
|
|
361
|
+
});
|
|
362
|
+
|
|
363
|
+
const {body, status} = await answerFiles(agent, historyId, [
|
|
364
|
+
{fileId, filename: "receipt.png", mimeType: "image/png", size: PNG_BYTES.length},
|
|
365
|
+
]);
|
|
366
|
+
|
|
367
|
+
expect(status).toBe(400);
|
|
368
|
+
expect(fieldCodes(body)).toEqual([{code: "FILE_NOT_OWNED", path: "content.files[0].fileId"}]);
|
|
369
|
+
});
|
|
370
|
+
|
|
371
|
+
it("stores a declined answer as sent", async () => {
|
|
372
|
+
const {agent, historyId, model} = await pauseOnReceiptAsk({replyText: "No receipt, noted."});
|
|
373
|
+
|
|
374
|
+
const {events} = await streamPrompt(agent, {
|
|
375
|
+
askResponse: {action: "decline", toolCallId: "call_receipt"},
|
|
376
|
+
historyId,
|
|
377
|
+
});
|
|
378
|
+
|
|
379
|
+
expect(events[1]).toEqual({text: "No receipt, noted."});
|
|
380
|
+
expect(receiptResultOf(model, 1)).toEqual({type: "json", value: {action: "decline"}});
|
|
381
|
+
expect(await storedResultOf(historyId)).toEqual({action: "decline"});
|
|
382
|
+
});
|
|
383
|
+
});
|
|
384
|
+
|
|
385
|
+
describe("with uploads", () => {
|
|
386
|
+
it("loads the caller's uploads and sends them to the model, storing their ids", async () => {
|
|
387
|
+
const downloader = fakeDownloader({
|
|
388
|
+
"uploads/items.txt": Buffer.from(RECEIPT_TEXT),
|
|
389
|
+
"uploads/receipt.png": PNG_BYTES,
|
|
390
|
+
});
|
|
391
|
+
const {agent, historyId, model} = await pauseOnReceiptAsk({fileStorageService: downloader});
|
|
392
|
+
const imageId = await createUpload({
|
|
393
|
+
filename: "receipt.png",
|
|
394
|
+
gcsKey: "uploads/receipt.png",
|
|
395
|
+
mimeType: "image/png",
|
|
396
|
+
size: PNG_BYTES.length,
|
|
397
|
+
userId,
|
|
398
|
+
});
|
|
399
|
+
const textId = await createUpload({
|
|
400
|
+
filename: "items.txt",
|
|
401
|
+
gcsKey: "uploads/items.txt",
|
|
402
|
+
mimeType: "text/plain",
|
|
403
|
+
size: RECEIPT_TEXT.length,
|
|
404
|
+
userId,
|
|
405
|
+
});
|
|
406
|
+
|
|
407
|
+
const {events} = await streamPrompt(agent, {
|
|
408
|
+
askResponse: {
|
|
409
|
+
action: "accept",
|
|
410
|
+
content: {
|
|
411
|
+
files: [
|
|
412
|
+
{fileId: imageId, filename: "receipt.png", mimeType: "image/png", size: 12},
|
|
413
|
+
{fileId: textId, filename: "items.txt", mimeType: "text/plain", size: 25},
|
|
414
|
+
],
|
|
415
|
+
},
|
|
416
|
+
toolCallId: "call_receipt",
|
|
417
|
+
},
|
|
418
|
+
historyId,
|
|
419
|
+
});
|
|
420
|
+
|
|
421
|
+
expect(events[1]).toEqual({text: "Got your receipt."});
|
|
422
|
+
expect(downloader.calls.sort()).toEqual(["uploads/items.txt", "uploads/receipt.png"]);
|
|
423
|
+
const storedAnswer = {
|
|
424
|
+
action: "accept",
|
|
425
|
+
content: {
|
|
426
|
+
files: [
|
|
427
|
+
{fileId: imageId, filename: "receipt.png", mimeType: "image/png", size: 12},
|
|
428
|
+
{fileId: textId, filename: "items.txt", mimeType: "text/plain", size: 25},
|
|
429
|
+
],
|
|
430
|
+
},
|
|
431
|
+
};
|
|
432
|
+
expect(receiptResultOf(model, 1)).toEqual({
|
|
433
|
+
type: "content",
|
|
434
|
+
value: [
|
|
435
|
+
{text: JSON.stringify(storedAnswer), type: "text"},
|
|
436
|
+
{text: "File 1 of 2: receipt.png (image/png, 12 bytes)", type: "text"},
|
|
437
|
+
{data: PNG_BASE64, mediaType: "image/png", type: "image-data"},
|
|
438
|
+
{text: "File 2 of 2: items.txt (text/plain, 25 bytes)", type: "text"},
|
|
439
|
+
{text: RECEIPT_TEXT, type: "text"},
|
|
440
|
+
],
|
|
441
|
+
});
|
|
442
|
+
expect(await storedResultOf(historyId)).toEqual(storedAnswer);
|
|
443
|
+
});
|
|
444
|
+
|
|
445
|
+
it("sends a PDF upload as file-data", async () => {
|
|
446
|
+
const pdfBytes = Buffer.from("%PDF-1.7\n%âãÏÓ\n");
|
|
447
|
+
const downloader = fakeDownloader({"uploads/receipt.pdf": pdfBytes});
|
|
448
|
+
const {agent, historyId, model} = await pauseOnReceiptAsk({
|
|
449
|
+
fileStorageService: downloader,
|
|
450
|
+
});
|
|
451
|
+
await GptHistory.updateOne({_id: historyId}, {$set: {"pendingAsk.input.accept": ["pdf"]}});
|
|
452
|
+
const fileId = await createUpload({
|
|
453
|
+
filename: "receipt.pdf",
|
|
454
|
+
gcsKey: "uploads/receipt.pdf",
|
|
455
|
+
mimeType: "application/pdf",
|
|
456
|
+
size: pdfBytes.length,
|
|
457
|
+
userId,
|
|
458
|
+
});
|
|
459
|
+
|
|
460
|
+
await answerFiles(agent, historyId, [
|
|
461
|
+
{fileId, filename: "receipt.pdf", mimeType: "application/pdf", size: pdfBytes.length},
|
|
462
|
+
]);
|
|
463
|
+
|
|
464
|
+
const output = receiptResultOf(model, 1) as {value: unknown[]};
|
|
465
|
+
expect(output.value.at(-1)).toEqual({
|
|
466
|
+
data: pdfBytes.toString("base64"),
|
|
467
|
+
filename: "receipt.pdf",
|
|
468
|
+
mediaType: "application/pdf",
|
|
469
|
+
type: "file-data",
|
|
470
|
+
});
|
|
471
|
+
});
|
|
472
|
+
|
|
473
|
+
it("returns 400 FILE_NOT_OWNED for another user's upload, a deleted upload, or an unknown id", async () => {
|
|
474
|
+
const downloader = fakeDownloader({"uploads/theirs.png": PNG_BYTES});
|
|
475
|
+
const {agent, historyId, model} = await pauseOnReceiptAsk({fileStorageService: downloader});
|
|
476
|
+
const theirs = await createUpload({
|
|
477
|
+
filename: "theirs.png",
|
|
478
|
+
gcsKey: "uploads/theirs.png",
|
|
479
|
+
mimeType: "image/png",
|
|
480
|
+
size: PNG_BYTES.length,
|
|
481
|
+
userId: otherUserId,
|
|
482
|
+
});
|
|
483
|
+
const deleted = await createUpload({
|
|
484
|
+
filename: "deleted.png",
|
|
485
|
+
gcsKey: "uploads/deleted.png",
|
|
486
|
+
mimeType: "image/png",
|
|
487
|
+
size: PNG_BYTES.length,
|
|
488
|
+
userId,
|
|
489
|
+
});
|
|
490
|
+
await FileAttachment.updateOne({_id: deleted}, {$set: {deleted: true}});
|
|
491
|
+
const ref = {filename: "receipt.png", mimeType: "image/png", size: 12};
|
|
492
|
+
|
|
493
|
+
const {body, status} = await answerFiles(agent, historyId, [
|
|
494
|
+
{...ref, fileId: theirs},
|
|
495
|
+
{...ref, fileId: deleted},
|
|
496
|
+
{...ref, fileId: new mongoose.Types.ObjectId().toString()},
|
|
497
|
+
]);
|
|
498
|
+
|
|
499
|
+
expect(status).toBe(400);
|
|
500
|
+
expect(body.fields).toEqual(
|
|
501
|
+
[0, 1, 2].map((index) => ({
|
|
502
|
+
code: "FILE_NOT_OWNED",
|
|
503
|
+
fix: "Upload the file with POST /files/upload and send the id it returns, or send a data: URL.",
|
|
504
|
+
message: "The file id does not name one of your uploads.",
|
|
505
|
+
path: `content.files[${index}].fileId`,
|
|
506
|
+
}))
|
|
507
|
+
);
|
|
508
|
+
expect(downloader.calls).toEqual([]);
|
|
509
|
+
expect(model.doStream).toHaveBeenCalledTimes(1);
|
|
510
|
+
expect((await loadHistory(historyId)).pendingAsk?.toolCallId).toBe("call_receipt");
|
|
511
|
+
});
|
|
512
|
+
|
|
513
|
+
it("returns 400 FILE_NOT_OWNED and keeps the ask pending when an upload cannot be downloaded", async () => {
|
|
514
|
+
const download = mock(async (gcsKey: string): Promise<Buffer> => {
|
|
515
|
+
throw new Error(`No such object: ${gcsKey}`);
|
|
516
|
+
});
|
|
517
|
+
const {agent, historyId, model} = await pauseOnReceiptAsk({
|
|
518
|
+
fileStorageService: {download},
|
|
519
|
+
});
|
|
520
|
+
const fileId = await createUpload({
|
|
521
|
+
filename: "receipt.png",
|
|
522
|
+
gcsKey: "uploads/receipt.png",
|
|
523
|
+
mimeType: "image/png",
|
|
524
|
+
size: PNG_BYTES.length,
|
|
525
|
+
userId,
|
|
526
|
+
});
|
|
527
|
+
|
|
528
|
+
const {body, status} = await answerFiles(agent, historyId, [
|
|
529
|
+
{fileId, filename: "receipt.png", mimeType: "image/png", size: PNG_BYTES.length},
|
|
530
|
+
]);
|
|
531
|
+
|
|
532
|
+
expect(status).toBe(400);
|
|
533
|
+
expect(fieldCodes(body)).toEqual([{code: "FILE_NOT_OWNED", path: "content.files[0].fileId"}]);
|
|
534
|
+
expect(download).toHaveBeenCalledWith("uploads/receipt.png");
|
|
535
|
+
expect(model.doStream).toHaveBeenCalledTimes(1);
|
|
536
|
+
expect((await loadHistory(historyId)).pendingAsk?.toolCallId).toBe("call_receipt");
|
|
537
|
+
});
|
|
538
|
+
|
|
539
|
+
it("returns 400 when an upload's bytes are larger than the cap or not its declared type", async () => {
|
|
540
|
+
const downloader = fakeDownloader({
|
|
541
|
+
"uploads/big.txt": Buffer.from("x".repeat(64)),
|
|
542
|
+
"uploads/fake.png": Buffer.from(RECEIPT_TEXT),
|
|
543
|
+
});
|
|
544
|
+
const {agent, historyId} = await pauseOnReceiptAsk({
|
|
545
|
+
asks: {maxFileSizeBytes: 32},
|
|
546
|
+
fileStorageService: downloader,
|
|
547
|
+
});
|
|
548
|
+
const big = await createUpload({
|
|
549
|
+
filename: "big.txt",
|
|
550
|
+
gcsKey: "uploads/big.txt",
|
|
551
|
+
mimeType: "text/plain",
|
|
552
|
+
size: 8,
|
|
553
|
+
userId,
|
|
554
|
+
});
|
|
555
|
+
const fake = await createUpload({
|
|
556
|
+
filename: "fake.png",
|
|
557
|
+
gcsKey: "uploads/fake.png",
|
|
558
|
+
mimeType: "image/png",
|
|
559
|
+
size: 12,
|
|
560
|
+
userId,
|
|
561
|
+
});
|
|
562
|
+
|
|
563
|
+
const {body, status} = await answerFiles(agent, historyId, [
|
|
564
|
+
{fileId: big, filename: "big.txt", mimeType: "text/plain", size: 8},
|
|
565
|
+
{fileId: fake, filename: "fake.png", mimeType: "image/png", size: 12},
|
|
566
|
+
]);
|
|
567
|
+
|
|
568
|
+
expect(status).toBe(400);
|
|
569
|
+
expect(fieldCodes(body)).toEqual([
|
|
570
|
+
{code: "FILE_TOO_LARGE", path: "content.files[0].size"},
|
|
571
|
+
{code: "MIME_MISMATCH", path: "content.files[1].mimeType"},
|
|
572
|
+
]);
|
|
573
|
+
});
|
|
574
|
+
});
|
|
575
|
+
|
|
576
|
+
describe("surfaces", () => {
|
|
577
|
+
it("offers ask_files on the full surface and not on the compact surface", async () => {
|
|
578
|
+
const model = createScriptedModel({steps: [textStep("Hello."), textStep("Hi.")]});
|
|
579
|
+
const agent = await authAsUser(buildApp({asks: true, model}), "notAdmin");
|
|
580
|
+
|
|
581
|
+
await streamPrompt(agent, {prompt: "Hi"});
|
|
582
|
+
await streamPrompt(agent, {prompt: "Hi", surface: "compact"});
|
|
583
|
+
|
|
584
|
+
expect(toolNamesOf(modelCall(model, 0))).toContain("ask_files");
|
|
585
|
+
expect(toolNamesOf(modelCall(model, 1))).toEqual(["ask_choice", "ask_confirm"]);
|
|
586
|
+
expect(systemPromptOf(modelCall(model, 1))).not.toContain("ask_files");
|
|
587
|
+
});
|
|
588
|
+
});
|
|
589
|
+
});
|