plugin-agent-orchestrator 1.1.8 → 1.2.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/LICENSE +201 -0
- package/README.md +207 -204
- package/client-v2.d.ts +2 -2
- package/client-v2.js +1 -1
- package/client.d.ts +2 -2
- package/client.js +1 -1
- package/dist/client/117.6c69b6a87751dd18.js +10 -0
- package/dist/client/253.52f62e1bc0e9520a.js +10 -0
- package/dist/client/325.8f7f509bdb35e142.js +10 -0
- package/dist/client/404.86593f98c9768ed5.js +10 -0
- package/dist/client/453.fa2d169576262213.js +10 -0
- package/dist/client/53.567db3ae078650c6.js +10 -0
- package/dist/client/619.01713281e5e951d3.js +10 -0
- package/dist/client/652.69da1598312b3a3f.js +10 -0
- package/dist/client/773.648965568ff476f9.js +10 -0
- package/dist/client/981.37e7aeecdb1ef6fa.js +10 -0
- package/dist/client/993.ac89f3852990918d.js +10 -0
- package/dist/client/index.js +1 -1
- package/dist/client-v2/117.c982d79ff68738f5.js +10 -0
- package/dist/client-v2/214.94c4257bf2838577.js +10 -0
- package/dist/client-v2/253.8e19956d2a44dfb8.js +10 -0
- package/dist/client-v2/264.80e8e358c8c6c2e5.js +10 -0
- package/dist/client-v2/325.fafe5a6e83ba9cb3.js +10 -0
- package/dist/client-v2/404.ab032b79c95dab6f.js +10 -0
- package/dist/client-v2/{41.3deb9e57a0f64fc4.js → 41.cd5610fcf7446127.js} +1 -1
- package/dist/client-v2/453.42c6d70628d56cc2.js +10 -0
- package/dist/client-v2/{53.349fff8db80b19d4.js → 53.37c08089a648f89a.js} +1 -1
- package/dist/client-v2/{619.0a2144aaffefe018.js → 619.6e9618550101e819.js} +1 -1
- package/dist/client-v2/652.eeba4e0c38367321.js +10 -0
- package/dist/client-v2/{70.ffff2997c10fd4b0.js → 70.332698e53eb59677.js} +1 -1
- package/dist/client-v2/773.4de2ba5acb07318c.js +10 -0
- package/dist/client-v2/981.76060f466e6f7c11.js +10 -0
- package/dist/client-v2/993.f8cdb5172566ce75.js +10 -0
- package/dist/client-v2/index.js +1 -1
- package/dist/externalVersion.js +13 -7
- package/dist/locale/en-US.json +308 -1
- package/dist/locale/vi-VN.json +5 -1
- package/dist/locale/zh-CN.json +308 -1
- package/dist/node_modules/acorn/LICENSE +21 -0
- package/dist/node_modules/acorn/bin/acorn +4 -0
- package/dist/node_modules/acorn/dist/acorn.d.mts +857 -0
- package/dist/node_modules/acorn/dist/acorn.d.ts +857 -0
- package/dist/node_modules/acorn/dist/acorn.js +1 -0
- package/dist/node_modules/acorn/dist/bin.js +90 -0
- package/dist/node_modules/acorn/package.json +1 -0
- package/dist/node_modules/acorn-walk/LICENSE +21 -0
- package/dist/node_modules/acorn-walk/dist/walk.d.mts +177 -0
- package/dist/node_modules/acorn-walk/dist/walk.d.ts +177 -0
- package/dist/node_modules/acorn-walk/dist/walk.js +1 -0
- package/dist/node_modules/acorn-walk/node_modules/.bin/acorn +15 -0
- package/dist/node_modules/acorn-walk/node_modules/.bin/acorn.cmd +7 -0
- package/dist/node_modules/acorn-walk/node_modules/acorn/bin/acorn +4 -0
- package/dist/node_modules/acorn-walk/node_modules/acorn/dist/acorn.d.mts +866 -0
- package/dist/node_modules/acorn-walk/node_modules/acorn/dist/acorn.d.ts +866 -0
- package/dist/node_modules/acorn-walk/node_modules/acorn/dist/acorn.js +6174 -0
- package/dist/node_modules/acorn-walk/node_modules/acorn/dist/bin.js +90 -0
- package/dist/node_modules/acorn-walk/node_modules/acorn/package.json +50 -0
- package/dist/node_modules/acorn-walk/package.json +1 -0
- package/dist/node_modules/adm-zip/adm-zip.js +1 -1
- package/dist/node_modules/adm-zip/package.json +1 -1
- package/dist/node_modules/ajv/package.json +1 -1
- package/dist/node_modules/zod/index.cjs +1 -1
- package/dist/node_modules/zod/package.json +1 -1
- package/dist/server/collections/agent-auto-approval-checkpoints.js +47 -0
- package/dist/server/collections/agent-execution-spans.js +148 -9
- package/dist/server/collections/agent-governance-audit-events.js +47 -0
- package/dist/server/collections/agent-governance-policies.js +47 -0
- package/dist/server/collections/agent-governance-policy-versions.js +48 -0
- package/dist/server/collections/agent-governance-prompt-bindings.js +50 -0
- package/dist/server/collections/agent-hierarchy-invocations.js +59 -0
- package/dist/server/collections/agent-loop-runs.js +23 -5
- package/dist/server/collections/agent-routing-graph-locks.js +40 -0
- package/dist/server/collections/agent-session-auto-approvals.js +45 -0
- package/dist/server/collections/orchestrator-config.js +17 -0
- package/dist/server/migrations/20261110000000-add-usage-pricing-snapshots.js +117 -0
- package/dist/server/migrations/20261110010000-add-usage-department-attribution.js +54 -0
- package/dist/server/migrations/20261119000000-widen-native-employee-identifiers.js +59 -0
- package/dist/server/plugin.js +169 -33
- package/dist/server/resources/agent-auto-approval.js +80 -0
- package/dist/server/resources/agent-governance.js +192 -0
- package/dist/server/resources/agent-harness-profile.js +11 -3
- package/dist/server/resources/agent-monitor.js +109 -339
- package/dist/server/resources/agent-usage.js +423 -0
- package/dist/server/resources/hierarchical-routing.js +60 -0
- package/dist/server/resources/native-monitor-format.js +179 -0
- package/dist/server/resources/tracing.js +35 -12
- package/dist/server/services/AgentGovernanceService.js +500 -0
- package/dist/server/services/AutoApprovalCheckpoint.js +133 -0
- package/dist/server/services/CodeValidator.js +8 -4
- package/dist/server/services/DepartmentAttribution.js +65 -0
- package/dist/server/services/ExecutionSpanService.js +96 -3
- package/dist/server/services/GovernanceAdmission.js +271 -0
- package/dist/server/services/HarnessCapabilities.js +118 -0
- package/dist/server/services/HarnessCompiler.js +3 -0
- package/dist/server/services/HarnessSchema.js +13 -1
- package/dist/server/services/HierarchicalAgentRuntime.js +925 -0
- package/dist/server/services/HierarchyEnrollmentGuard.js +216 -0
- package/dist/server/services/HierarchyInvocationJournal.js +157 -0
- package/dist/server/services/HierarchyRequestCancellation.js +150 -0
- package/dist/server/services/LoopBudgetService.js +29 -11
- package/dist/server/services/LoopRunRepository.js +16 -7
- package/dist/server/services/LoopRunStateMachine.js +20 -9
- package/dist/server/services/LoopTriggerService.js +1 -1
- package/dist/server/services/LoopWorkerService.js +71 -37
- package/dist/server/services/ManagedPromptProtection.js +99 -0
- package/dist/server/services/NativeAgentMonitorSyncService.js +584 -0
- package/dist/server/services/NativeSubAgentObserver.js +141 -508
- package/dist/server/services/NativeUsageAttribution.js +97 -0
- package/dist/server/services/PluginAiRuntimeAdapter.js +107 -9
- package/dist/server/services/RoutingBindingRepository.js +156 -0
- package/dist/server/services/RoutingBindingValidation.js +161 -0
- package/dist/server/services/SandboxRunner.js +19 -0
- package/dist/server/services/SessionAutoApproval.js +173 -0
- package/dist/server/services/TokenTracker.js +787 -104
- package/dist/server/services/TracingRetentionPolicy.js +66 -0
- package/dist/server/services/VerificationService.js +20 -10
- package/dist/server/skill-hub/plugin.js +17 -0
- package/dist/server/skill-hub/sandbox-config.json +6 -6
- package/dist/server/skill-hub/services/RegistrySkillInstallationService.js +2 -2
- package/dist/server/skill-hub/services/registry-node-package.js +191 -0
- package/dist/server/skill-hub/tasks/SkillExecutionTask.js +21 -6
- package/dist/server/tools/skill-execute.js +1 -0
- package/dist/server/utils/ensure-indexes.js +15 -5
- package/package.json +66 -53
- package/server.d.ts +2 -2
- package/server.js +1 -1
- package/src/client/AgentRunsTab.tsx +2 -2
- package/src/client/ApprovalsTab.tsx +2 -2
- package/src/client/HarnessProfilesTab.tsx +2 -2
- package/src/client/OrchestratorSettings.tsx +152 -152
- package/src/client/SkillPages.tsx +34 -0
- package/src/client/__tests__/embedded-settings.test.tsx +225 -0
- package/src/client/hooks/useRunEventStream.ts +76 -76
- package/src/client/index.tsx +2 -2
- package/src/client/plugin.tsx +71 -33
- package/src/client/skill-hub/components/ExecutionHistory.tsx +200 -200
- package/src/client/skill-hub/components/GitSkillImport.tsx +562 -562
- package/src/client/skill-hub/components/LoopSettings.tsx +331 -331
- package/src/client/skill-hub/components/SkillEditor.tsx +460 -460
- package/src/client/skill-hub/components/SkillManager.tsx +194 -194
- package/src/client/skill-hub/components/SkillMetrics.tsx +162 -162
- package/src/client/skill-hub/components/SkillTestPanel.tsx +141 -141
- package/src/client/skill-hub/index.tsx +6 -6
- package/src/client/skill-hub/locale.ts +16 -16
- package/src/client/skill-hub/tools/InteractionSchemasProvider.tsx +147 -132
- package/src/client/skill-hub/tools/SkillHubCard.tsx +110 -104
- package/src/client/skill-hub/tools/__tests__/skill-hub-card.test.tsx +255 -0
- package/src/client/skill-hub/tools/loopTemplates.ts +52 -52
- package/src/client/skill-hub/tools/registerSkillLoopCards.ts +80 -71
- package/src/client/skill-hub/utils/jsonFields.ts +41 -41
- package/src/client-v2/__tests__/embedded-settings.test.tsx +202 -0
- package/src/client-v2/__tests__/hierarchical-routing-settings.test.tsx +350 -0
- package/src/client-v2/auto-approval/AutoApprovalBridge.tsx +513 -0
- package/src/client-v2/auto-approval/AutoApprovalProvider.tsx +399 -0
- package/src/client-v2/auto-approval/__tests__/session-auto-approval.test.tsx +1676 -0
- package/src/client-v2/components/AIEmployeeSelect.tsx +47 -47
- package/src/client-v2/components/AIEmployeesContext.tsx +97 -97
- package/src/client-v2/components/AgentBindingsTab.tsx +541 -269
- package/src/client-v2/components/AgentRunsTab.tsx +85 -83
- package/src/client-v2/components/ApprovalsTab.tsx +337 -337
- package/src/client-v2/components/EmployeeGovernanceTab.tsx +695 -0
- package/src/client-v2/components/HarnessProfilesTab.tsx +700 -541
- package/src/client-v2/components/HarnessSettingsForm.tsx +221 -0
- package/src/client-v2/components/KnowledgeAccessTab.tsx +116 -116
- package/src/client-v2/components/RetrievalTraceTab.tsx +162 -162
- package/src/client-v2/components/UsageGovernanceTab.tsx +379 -0
- package/src/client-v2/index.tsx +1 -1
- package/src/client-v2/pages/AgentBindingsPage.tsx +15 -13
- package/src/client-v2/pages/ApprovalsPage.tsx +12 -10
- package/src/client-v2/pages/EmployeeGovernancePage.tsx +15 -0
- package/src/client-v2/pages/ExecutionHistoryPage.tsx +12 -10
- package/src/client-v2/pages/HarnessProfilesPage.tsx +12 -10
- package/src/client-v2/pages/KnowledgeAccessPage.tsx +12 -10
- package/src/client-v2/pages/LoopSettingsPage.tsx +12 -10
- package/src/client-v2/pages/MemoryInspectorPage.tsx +15 -13
- package/src/client-v2/pages/RetrievalTracePage.tsx +12 -10
- package/src/client-v2/pages/RulesPage.tsx +15 -13
- package/src/client-v2/pages/SkillDefinitionsPage.tsx +12 -10
- package/src/client-v2/pages/SkillMetricsPage.tsx +12 -10
- package/src/client-v2/pages/TracingPage.tsx +15 -13
- package/src/client-v2/pages/UsageGovernancePage.tsx +15 -0
- package/src/client-v2/plugin.tsx +134 -111
- package/src/client-v2/skill-hub/components/ExecutionHistory.tsx +196 -196
- package/src/client-v2/skill-hub/components/FileLinkList.tsx +37 -37
- package/src/client-v2/skill-hub/components/GitSkillImport.tsx +539 -539
- package/src/client-v2/skill-hub/components/LoopSettings.tsx +331 -331
- package/src/client-v2/skill-hub/components/SkillEditor.tsx +453 -453
- package/src/client-v2/skill-hub/components/SkillManager.tsx +191 -174
- package/src/client-v2/skill-hub/components/SkillMetrics.tsx +162 -162
- package/src/client-v2/skill-hub/components/SkillTestPanel.tsx +135 -135
- package/src/client-v2/skill-hub/locale.ts +13 -13
- package/src/client-v2/skill-hub/tools/InteractionSchemasProvider.tsx +62 -0
- package/src/client-v2/skill-hub/tools/SkillHubCard.tsx +104 -0
- package/src/client-v2/skill-hub/tools/__tests__/interactions.test.ts +126 -0
- package/src/client-v2/skill-hub/tools/interactions.ts +137 -0
- package/src/client-v2/skill-hub/tools/loopTemplates.ts +52 -52
- package/src/client-v2/skill-hub/utils/jsonFields.ts +41 -41
- package/src/client-v2/utils/jsonFields.ts +41 -41
- package/src/index.ts +2 -2
- package/src/locale/en-US.json +308 -1
- package/src/locale/vi-VN.json +5 -1
- package/src/locale/zh-CN.json +308 -1
- package/src/server/__tests__/agent-execution-context.test.ts +18 -18
- package/src/server/__tests__/agent-governance.integration.test.ts +385 -0
- package/src/server/__tests__/agent-knowledge-insights.test.ts +123 -123
- package/src/server/__tests__/agent-monitor-acl.integration.test.ts +267 -0
- package/src/server/__tests__/agent-registry-service.test.ts +147 -147
- package/src/server/__tests__/agent-runtime-lifecycle.test.ts +46 -46
- package/src/server/__tests__/agent-usage-report.integration.test.ts +335 -0
- package/src/server/__tests__/auto-approval-checkpoint.integration.test.ts +295 -0
- package/src/server/__tests__/code-validator.test.ts +63 -63
- package/src/server/__tests__/external-rag-search.test.ts +107 -107
- package/src/server/__tests__/harness-capabilities.test.ts +88 -0
- package/src/server/__tests__/harness-compiler.test.ts +278 -214
- package/src/server/__tests__/harness-profile-service.test.ts +167 -167
- package/src/server/__tests__/harness-runtime-policy.test.ts +293 -293
- package/src/server/__tests__/harness-tool-plan.test.ts +175 -172
- package/src/server/__tests__/hierarchical-routing.integration.test.ts +2318 -0
- package/src/server/__tests__/hierarchy-invocation-journal.integration.test.ts +119 -0
- package/src/server/__tests__/input-schema-validator.test.ts +26 -26
- package/src/server/__tests__/loop-approval-decision.test.ts +624 -512
- package/src/server/__tests__/loop-approval-expiry.test.ts +258 -258
- package/src/server/__tests__/loop-budget-service.test.ts +29 -0
- package/src/server/__tests__/loop-pattern-schema.test.ts +73 -73
- package/src/server/__tests__/loop-run-repository.test.ts +25 -0
- package/src/server/__tests__/loop-run-state-machine.test.ts +101 -101
- package/src/server/__tests__/loop-trigger-service.test.ts +185 -148
- package/src/server/__tests__/loop-worker-claim.test.ts +110 -10
- package/src/server/__tests__/loop-worker-execution.test.ts +375 -317
- package/src/server/__tests__/native-agent-monitor-sync.test.ts +343 -0
- package/src/server/__tests__/native-hierarchy-probe.integration.test.ts +527 -0
- package/src/server/__tests__/native-sub-agent-observer.test.ts +463 -662
- package/src/server/__tests__/native-usage-attribution.integration.test.ts +168 -0
- package/src/server/__tests__/plugin-ai-runtime-adapter.test.ts +403 -0
- package/src/server/__tests__/registry-node-package.test.ts +299 -0
- package/src/server/__tests__/routing-binding-graph.integration.test.ts +217 -0
- package/src/server/__tests__/routing-binding-validation.test.ts +256 -0
- package/src/server/__tests__/session-auto-approval.integration.test.ts +329 -0
- package/src/server/__tests__/skill-access-service.test.ts +64 -64
- package/src/server/__tests__/skill-execute.test.ts +81 -70
- package/src/server/__tests__/skill-execution-task-policy.test.ts +140 -140
- package/src/server/__tests__/skill-review-guard.test.ts +171 -0
- package/src/server/__tests__/skill-sandbox-worker-mode.test.ts +15 -15
- package/src/server/__tests__/skill-settings.test.ts +63 -63
- package/src/server/__tests__/skill-tool-name.test.ts +23 -23
- package/src/server/__tests__/token-tracker.test.ts +632 -0
- package/src/server/__tests__/tool-loop-detection.test.ts +276 -276
- package/src/server/__tests__/tracing-retention-policy.test.ts +30 -0
- package/src/server/__tests__/usage-ledger.integration.test.ts +187 -0
- package/src/server/collections/agent-auto-approval-checkpoints.ts +18 -0
- package/src/server/collections/agent-execution-spans.ts +386 -245
- package/src/server/collections/agent-governance-audit-events.ts +19 -0
- package/src/server/collections/agent-governance-policies.ts +19 -0
- package/src/server/collections/agent-governance-policy-versions.ts +20 -0
- package/src/server/collections/agent-governance-prompt-bindings.ts +22 -0
- package/src/server/collections/agent-hierarchy-invocations.ts +30 -0
- package/src/server/collections/agent-loop-action-approvals.ts +39 -39
- package/src/server/collections/agent-loop-runs.ts +389 -369
- package/src/server/collections/agent-loop-steps.ts +220 -220
- package/src/server/collections/agent-memory-contexts.ts +95 -95
- package/src/server/collections/agent-routing-graph-locks.ts +11 -0
- package/src/server/collections/agent-session-auto-approvals.ts +16 -0
- package/src/server/collections/orchestrator-config.ts +113 -87
- package/src/server/collections/orchestrator-logs.ts +116 -116
- package/src/server/collections/skill-definitions.ts +159 -159
- package/src/server/collections/skill-executions.ts +154 -154
- package/src/server/collections/skill-loop-configs.ts +65 -65
- package/src/server/collections/skill-metrics.ts +36 -36
- package/src/server/collections/skill-registry-installations.ts +41 -41
- package/src/server/collections/skill-worker-configs.ts +86 -86
- package/src/server/index.ts +1 -1
- package/src/server/migrations/20260423000000-add-progress-fields.ts +50 -50
- package/src/server/migrations/20260425000000-add-interaction-schema.ts +37 -37
- package/src/server/migrations/20260427000000-add-tracing-detail-fields.ts +41 -41
- package/src/server/migrations/20260427000001-change-other-json-to-text.ts +61 -61
- package/src/server/migrations/20260524001000-add-plan-approval-and-harness-profiles.ts +145 -145
- package/src/server/migrations/20260601000000-add-token-fields.ts +89 -89
- package/src/server/migrations/20260615000000-normalize-ai-employee-tool-bindings.ts +39 -39
- package/src/server/migrations/20260621000000-native-policy-profile-defaults.ts +190 -190
- package/src/server/migrations/20260727000000-remove-unused-knowledge-scopes.ts +25 -25
- package/src/server/migrations/20260727010000-backfill-skill-tool-names.ts +45 -45
- package/src/server/migrations/20261110000000-add-usage-pricing-snapshots.ts +110 -0
- package/src/server/migrations/20261110010000-add-usage-department-attribution.ts +28 -0
- package/src/server/migrations/20261119000000-widen-native-employee-identifiers.ts +37 -0
- package/src/server/plugin.ts +724 -573
- package/src/server/resources/agent-auto-approval.ts +51 -0
- package/src/server/resources/agent-governance.ts +161 -0
- package/src/server/resources/agent-harness-profile.ts +103 -94
- package/src/server/resources/agent-knowledge-insights.ts +306 -306
- package/src/server/resources/agent-loop-approval.ts +84 -84
- package/src/server/resources/agent-loop-events-stream.ts +108 -108
- package/src/server/resources/agent-loop.ts +252 -252
- package/src/server/resources/agent-monitor.ts +208 -482
- package/src/server/resources/agent-usage.ts +413 -0
- package/src/server/resources/hierarchical-routing.ts +39 -0
- package/src/server/resources/native-monitor-format.ts +150 -0
- package/src/server/resources/tracing.ts +296 -267
- package/src/server/services/AgentExecutionContext.ts +23 -23
- package/src/server/services/AgentGovernanceService.ts +541 -0
- package/src/server/services/AgentMemoryContextService.ts +344 -344
- package/src/server/services/AgentRegistryService.ts +220 -220
- package/src/server/services/AgentRuntimeLifecycle.ts +64 -64
- package/src/server/services/AutoApprovalCheckpoint.ts +140 -0
- package/src/server/services/CodeValidator.ts +257 -238
- package/src/server/services/DepartmentAttribution.ts +47 -0
- package/src/server/services/DurableCircuitBreakerService.ts +235 -235
- package/src/server/services/ExecutionSpanService.ts +271 -146
- package/src/server/services/FileManager.ts +159 -159
- package/src/server/services/GovernanceAdmission.ts +314 -0
- package/src/server/services/HarnessCapabilities.ts +112 -0
- package/src/server/services/HarnessCompiler.ts +182 -170
- package/src/server/services/HarnessProfileService.ts +235 -235
- package/src/server/services/HarnessSchema.ts +224 -194
- package/src/server/services/HierarchicalAgentRuntime.ts +1052 -0
- package/src/server/services/HierarchyEnrollmentGuard.ts +211 -0
- package/src/server/services/HierarchyInvocationJournal.ts +172 -0
- package/src/server/services/HierarchyRequestCancellation.ts +132 -0
- package/src/server/services/InputSchemaValidator.ts +44 -44
- package/src/server/services/LoopBudgetService.ts +325 -271
- package/src/server/services/LoopControlService.ts +120 -120
- package/src/server/services/LoopPatternSchema.ts +231 -231
- package/src/server/services/LoopPatternService.ts +171 -171
- package/src/server/services/LoopRunRepository.ts +343 -332
- package/src/server/services/LoopRunStateMachine.ts +538 -525
- package/src/server/services/LoopSchedulerService.ts +195 -195
- package/src/server/services/LoopTriggerService.ts +162 -159
- package/src/server/services/LoopWorkerService.ts +975 -912
- package/src/server/services/ManagedPromptProtection.ts +81 -0
- package/src/server/services/NativeAgentMonitorSyncService.ts +684 -0
- package/src/server/services/NativeSubAgentObserver.ts +324 -757
- package/src/server/services/NativeUsageAttribution.ts +91 -0
- package/src/server/services/PathLockService.ts +317 -317
- package/src/server/services/PluginAiRuntimeAdapter.ts +518 -387
- package/src/server/services/RoutingBindingRepository.ts +146 -0
- package/src/server/services/RoutingBindingValidation.ts +161 -0
- package/src/server/services/RunEventBus.ts +58 -58
- package/src/server/services/SandboxRunner.ts +526 -492
- package/src/server/services/SessionAutoApproval.ts +167 -0
- package/src/server/services/SkillAccessService.ts +175 -175
- package/src/server/services/SkillManager.ts +585 -585
- package/src/server/services/SkillRepositoryService.ts +236 -236
- package/src/server/services/TokenTracker.ts +1151 -287
- package/src/server/services/ToolLoopDetectionService.ts +210 -210
- package/src/server/services/TracingRetentionPolicy.ts +49 -0
- package/src/server/services/VerificationSchema.ts +31 -31
- package/src/server/services/VerificationService.ts +325 -313
- package/src/server/services/WorkerEnvManager.ts +198 -198
- package/src/server/services/__tests__/SkillRepositoryService.test.ts +33 -33
- package/src/server/services/harness-runtime-policy.ts +134 -134
- package/src/server/services/pythonGuardTemplate.ts +87 -87
- package/src/server/services/sandboxCapabilities.ts +348 -348
- package/src/server/skill-hub/actions/__tests__/git-import.test.ts +563 -563
- package/src/server/skill-hub/actions/git-import.ts +936 -936
- package/src/server/skill-hub/mcp/McpController.ts +107 -107
- package/src/server/skill-hub/middlewares/git-import-request-method-policy.ts +51 -51
- package/src/server/skill-hub/plugin.ts +1272 -1232
- package/src/server/skill-hub/sandbox-config.json +6 -6
- package/src/server/skill-hub/services/RegistrySkillInstallationService.ts +291 -281
- package/src/server/skill-hub/services/__tests__/RegistrySkillInstallationService.test.ts +295 -295
- package/src/server/skill-hub/services/registry-node-package.ts +191 -0
- package/src/server/skill-hub/tasks/SkillExecutionTask.ts +597 -568
- package/src/server/skill-hub/utils/json-fields.ts +57 -57
- package/src/server/tools/delegate-task.ts +30 -30
- package/src/server/tools/external-rag-search.ts +329 -329
- package/src/server/tools/skill-execute.ts +226 -216
- package/src/server/types.ts +21 -21
- package/src/server/utils/ai-manager.ts +24 -24
- package/src/server/utils/ctx-utils.ts +132 -132
- package/src/server/utils/ensure-indexes.ts +113 -96
- package/src/server/utils/ha-lock.ts +80 -80
- package/src/server/utils/loop-pattern-context.ts +84 -84
- package/src/server/utils/record-utils.ts +19 -19
- package/src/server/utils/skill-settings.ts +135 -135
- package/src/server/utils/skill-tool-name.ts +58 -58
- package/dist/client-v2/117.7565cff57c39318a.js +0 -10
- package/dist/client-v2/214.106635011859dede.js +0 -10
- package/dist/client-v2/253.59577b40d9875be0.js +0 -10
- package/dist/client-v2/264.7ed8bee71fa5935c.js +0 -10
- package/dist/client-v2/418.41ed200e7d6da332.js +0 -10
- package/dist/client-v2/794.a67a2614405ba627.js +0 -10
- package/dist/client-v2/981.e4a1f41cfc94a856.js +0 -10
- package/dist/client-v2/993.b0688fdc5ccb000a.js +0 -10
- package/dist/node_modules/simple-git/dist/cjs/index.js +0 -7399
- package/dist/node_modules/simple-git/dist/esm/index.js +0 -4745
- package/dist/node_modules/simple-git/dist/esm/package.json +0 -3
- package/dist/node_modules/simple-git/dist/src/lib/api.d.ts +0 -13
- package/dist/node_modules/simple-git/dist/src/lib/args/log-format.d.ts +0 -9
- package/dist/node_modules/simple-git/dist/src/lib/errors/git-construct-error.d.ts +0 -15
- package/dist/node_modules/simple-git/dist/src/lib/errors/git-error.d.ts +0 -30
- package/dist/node_modules/simple-git/dist/src/lib/errors/git-plugin-error.d.ts +0 -7
- package/dist/node_modules/simple-git/dist/src/lib/errors/git-response-error.d.ts +0 -32
- package/dist/node_modules/simple-git/dist/src/lib/errors/task-configuration-error.d.ts +0 -12
- package/dist/node_modules/simple-git/dist/src/lib/git-factory.d.ts +0 -15
- package/dist/node_modules/simple-git/dist/src/lib/git-logger.d.ts +0 -21
- package/dist/node_modules/simple-git/dist/src/lib/parsers/parse-branch-delete.d.ts +0 -5
- package/dist/node_modules/simple-git/dist/src/lib/parsers/parse-branch.d.ts +0 -2
- package/dist/node_modules/simple-git/dist/src/lib/parsers/parse-commit.d.ts +0 -2
- package/dist/node_modules/simple-git/dist/src/lib/parsers/parse-diff-summary.d.ts +0 -3
- package/dist/node_modules/simple-git/dist/src/lib/parsers/parse-fetch.d.ts +0 -2
- package/dist/node_modules/simple-git/dist/src/lib/parsers/parse-list-log-summary.d.ts +0 -6
- package/dist/node_modules/simple-git/dist/src/lib/parsers/parse-merge.d.ts +0 -11
- package/dist/node_modules/simple-git/dist/src/lib/parsers/parse-move.d.ts +0 -2
- package/dist/node_modules/simple-git/dist/src/lib/parsers/parse-pull.d.ts +0 -6
- package/dist/node_modules/simple-git/dist/src/lib/parsers/parse-push.d.ts +0 -4
- package/dist/node_modules/simple-git/dist/src/lib/parsers/parse-remote-messages.d.ts +0 -5
- package/dist/node_modules/simple-git/dist/src/lib/parsers/parse-remote-objects.d.ts +0 -3
- package/dist/node_modules/simple-git/dist/src/lib/plugins/abort-plugin.d.ts +0 -3
- package/dist/node_modules/simple-git/dist/src/lib/plugins/block-unsafe-operations-plugin.d.ts +0 -3
- package/dist/node_modules/simple-git/dist/src/lib/plugins/command-config-prefixing-plugin.d.ts +0 -2
- package/dist/node_modules/simple-git/dist/src/lib/plugins/completion-detection.plugin.d.ts +0 -3
- package/dist/node_modules/simple-git/dist/src/lib/plugins/custom-binary.plugin.d.ts +0 -3
- package/dist/node_modules/simple-git/dist/src/lib/plugins/error-detection.plugin.d.ts +0 -7
- package/dist/node_modules/simple-git/dist/src/lib/plugins/index.d.ts +0 -11
- package/dist/node_modules/simple-git/dist/src/lib/plugins/plugin-store.d.ts +0 -11
- package/dist/node_modules/simple-git/dist/src/lib/plugins/progress-monitor-plugin.d.ts +0 -3
- package/dist/node_modules/simple-git/dist/src/lib/plugins/simple-git-plugin.d.ts +0 -48
- package/dist/node_modules/simple-git/dist/src/lib/plugins/spawn-options-plugin.d.ts +0 -3
- package/dist/node_modules/simple-git/dist/src/lib/plugins/suffix-paths.plugin.d.ts +0 -2
- package/dist/node_modules/simple-git/dist/src/lib/plugins/timout-plugin.d.ts +0 -3
- package/dist/node_modules/simple-git/dist/src/lib/responses/BranchDeleteSummary.d.ts +0 -12
- package/dist/node_modules/simple-git/dist/src/lib/responses/BranchSummary.d.ts +0 -14
- package/dist/node_modules/simple-git/dist/src/lib/responses/CheckIgnore.d.ts +0 -4
- package/dist/node_modules/simple-git/dist/src/lib/responses/CleanSummary.d.ts +0 -9
- package/dist/node_modules/simple-git/dist/src/lib/responses/ConfigList.d.ts +0 -13
- package/dist/node_modules/simple-git/dist/src/lib/responses/DiffSummary.d.ts +0 -10
- package/dist/node_modules/simple-git/dist/src/lib/responses/FileStatusSummary.d.ts +0 -9
- package/dist/node_modules/simple-git/dist/src/lib/responses/GetRemoteSummary.d.ts +0 -11
- package/dist/node_modules/simple-git/dist/src/lib/responses/InitSummary.d.ts +0 -9
- package/dist/node_modules/simple-git/dist/src/lib/responses/MergeSummary.d.ts +0 -16
- package/dist/node_modules/simple-git/dist/src/lib/responses/PullSummary.d.ts +0 -25
- package/dist/node_modules/simple-git/dist/src/lib/responses/StatusSummary.d.ts +0 -19
- package/dist/node_modules/simple-git/dist/src/lib/responses/TagList.d.ts +0 -7
- package/dist/node_modules/simple-git/dist/src/lib/runners/git-executor-chain.d.ts +0 -25
- package/dist/node_modules/simple-git/dist/src/lib/runners/git-executor.d.ts +0 -14
- package/dist/node_modules/simple-git/dist/src/lib/runners/promise-wrapped.d.ts +0 -2
- package/dist/node_modules/simple-git/dist/src/lib/runners/scheduler.d.ts +0 -11
- package/dist/node_modules/simple-git/dist/src/lib/runners/tasks-pending-queue.d.ts +0 -23
- package/dist/node_modules/simple-git/dist/src/lib/simple-git-api.d.ts +0 -20
- package/dist/node_modules/simple-git/dist/src/lib/task-callback.d.ts +0 -2
- package/dist/node_modules/simple-git/dist/src/lib/tasks/apply-patch.d.ts +0 -3
- package/dist/node_modules/simple-git/dist/src/lib/tasks/branch.d.ts +0 -7
- package/dist/node_modules/simple-git/dist/src/lib/tasks/change-working-directory.d.ts +0 -2
- package/dist/node_modules/simple-git/dist/src/lib/tasks/check-ignore.d.ts +0 -2
- package/dist/node_modules/simple-git/dist/src/lib/tasks/check-is-repo.d.ts +0 -9
- package/dist/node_modules/simple-git/dist/src/lib/tasks/checkout.d.ts +0 -2
- package/dist/node_modules/simple-git/dist/src/lib/tasks/clean.d.ts +0 -25
- package/dist/node_modules/simple-git/dist/src/lib/tasks/clone.d.ts +0 -9
- package/dist/node_modules/simple-git/dist/src/lib/tasks/commit.d.ts +0 -4
- package/dist/node_modules/simple-git/dist/src/lib/tasks/config.d.ts +0 -8
- package/dist/node_modules/simple-git/dist/src/lib/tasks/count-objects.d.ts +0 -12
- package/dist/node_modules/simple-git/dist/src/lib/tasks/diff-name-status.d.ts +0 -12
- package/dist/node_modules/simple-git/dist/src/lib/tasks/diff.d.ts +0 -5
- package/dist/node_modules/simple-git/dist/src/lib/tasks/fetch.d.ts +0 -4
- package/dist/node_modules/simple-git/dist/src/lib/tasks/first-commit.d.ts +0 -2
- package/dist/node_modules/simple-git/dist/src/lib/tasks/grep.d.ts +0 -12
- package/dist/node_modules/simple-git/dist/src/lib/tasks/hash-object.d.ts +0 -5
- package/dist/node_modules/simple-git/dist/src/lib/tasks/init.d.ts +0 -3
- package/dist/node_modules/simple-git/dist/src/lib/tasks/log.d.ts +0 -32
- package/dist/node_modules/simple-git/dist/src/lib/tasks/merge.d.ts +0 -4
- package/dist/node_modules/simple-git/dist/src/lib/tasks/move.d.ts +0 -3
- package/dist/node_modules/simple-git/dist/src/lib/tasks/pull.d.ts +0 -3
- package/dist/node_modules/simple-git/dist/src/lib/tasks/push.d.ts +0 -9
- package/dist/node_modules/simple-git/dist/src/lib/tasks/remote.d.ts +0 -8
- package/dist/node_modules/simple-git/dist/src/lib/tasks/reset.d.ts +0 -11
- package/dist/node_modules/simple-git/dist/src/lib/tasks/show.d.ts +0 -2
- package/dist/node_modules/simple-git/dist/src/lib/tasks/stash-list.d.ts +0 -4
- package/dist/node_modules/simple-git/dist/src/lib/tasks/status.d.ts +0 -3
- package/dist/node_modules/simple-git/dist/src/lib/tasks/sub-module.d.ts +0 -5
- package/dist/node_modules/simple-git/dist/src/lib/tasks/tag.d.ts +0 -18
- package/dist/node_modules/simple-git/dist/src/lib/tasks/task.d.ts +0 -14
- package/dist/node_modules/simple-git/dist/src/lib/tasks/version.d.ts +0 -9
- package/dist/node_modules/simple-git/dist/src/lib/types/handlers.d.ts +0 -21
- package/dist/node_modules/simple-git/dist/src/lib/types/index.d.ts +0 -136
- package/dist/node_modules/simple-git/dist/src/lib/types/tasks.d.ts +0 -19
- package/dist/node_modules/simple-git/dist/src/lib/utils/argument-filters.d.ts +0 -14
- package/dist/node_modules/simple-git/dist/src/lib/utils/exit-codes.d.ts +0 -10
- package/dist/node_modules/simple-git/dist/src/lib/utils/git-output-streams.d.ts +0 -7
- package/dist/node_modules/simple-git/dist/src/lib/utils/index.d.ts +0 -8
- package/dist/node_modules/simple-git/dist/src/lib/utils/line-parser.d.ts +0 -15
- package/dist/node_modules/simple-git/dist/src/lib/utils/simple-git-options.d.ts +0 -2
- package/dist/node_modules/simple-git/dist/src/lib/utils/task-options.d.ts +0 -13
- package/dist/node_modules/simple-git/dist/src/lib/utils/task-parser.d.ts +0 -5
- package/dist/node_modules/simple-git/dist/src/lib/utils/util.d.ts +0 -47
- package/dist/node_modules/simple-git/dist/typings/errors.d.ts +0 -5
- package/dist/node_modules/simple-git/dist/typings/index.d.ts +0 -14
- package/dist/node_modules/simple-git/dist/typings/response.d.ts +0 -556
- package/dist/node_modules/simple-git/dist/typings/simple-git.d.ts +0 -1033
- package/dist/node_modules/simple-git/dist/typings/types.d.ts +0 -22
- package/dist/node_modules/simple-git/node_modules/debug/package.json +0 -64
- package/dist/node_modules/simple-git/node_modules/debug/src/browser.js +0 -272
- package/dist/node_modules/simple-git/node_modules/debug/src/common.js +0 -292
- package/dist/node_modules/simple-git/node_modules/debug/src/index.js +0 -10
- package/dist/node_modules/simple-git/node_modules/debug/src/node.js +0 -263
- package/dist/node_modules/simple-git/package.json +0 -1
- package/dist/node_modules/simple-git/promise.js +0 -17
|
@@ -0,0 +1,1676 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
|
|
3
|
+
import React from 'react';
|
|
4
|
+
import { act, cleanup, fireEvent, render, screen, waitFor, within } from '@testing-library/react';
|
|
5
|
+
import userEvent from '@testing-library/user-event';
|
|
6
|
+
import * as ClientV2 from '@nocobase/client-v2';
|
|
7
|
+
import { FlowEngineProvider, observer, useFlowEngineContext, type FlowModel } from '@nocobase/flow-engine';
|
|
8
|
+
import {
|
|
9
|
+
ChatBoxRuntimeProvider,
|
|
10
|
+
getGlobalChatBoxRuntime,
|
|
11
|
+
type ChatBoxRuntime,
|
|
12
|
+
type Message,
|
|
13
|
+
} from '@nocobase/plugin-ai/client-v2';
|
|
14
|
+
import type { ToolCall } from '@nocobase/client-v2';
|
|
15
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
16
|
+
import { createChatBoxRuntime } from '../../../../../@nocobase/plugin-ai/src/client-v2/ai-employees/chatbox/stores/runtime';
|
|
17
|
+
import { useToolCallActions } from '../../../../../@nocobase/plugin-ai/src/client-v2/ai-employees/chatbox/hooks/useToolCallActions';
|
|
18
|
+
import { AutoApprovalProvider } from '../AutoApprovalProvider';
|
|
19
|
+
import {
|
|
20
|
+
AutoApprovalBridge,
|
|
21
|
+
AutoApprovalRequests,
|
|
22
|
+
AutoApprovalRequestsContext,
|
|
23
|
+
useAutoApprovalInterceptor,
|
|
24
|
+
} from '../AutoApprovalBridge';
|
|
25
|
+
import V1PluginAgentOrchestratorClient from '../../../client/plugin';
|
|
26
|
+
import V2PluginAgentOrchestratorClient from '../../plugin';
|
|
27
|
+
|
|
28
|
+
const { resumeToolCallMock } = vi.hoisted(() => ({ resumeToolCallMock: vi.fn() }));
|
|
29
|
+
|
|
30
|
+
vi.mock('../../../../../@nocobase/plugin-ai/src/client-v2/ai-employees/chatbox/hooks/useChatMessageActions', () => ({
|
|
31
|
+
useChatMessageActions: () => ({ resumeToolCall: resumeToolCallMock }),
|
|
32
|
+
}));
|
|
33
|
+
|
|
34
|
+
type RequestBody = Record<string, unknown>;
|
|
35
|
+
const emptyToolCalls: ToolCall[] = [];
|
|
36
|
+
|
|
37
|
+
function readRequestBody(data: unknown): RequestBody {
|
|
38
|
+
const value: unknown = typeof data === 'string' ? JSON.parse(data) : data;
|
|
39
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) return {};
|
|
40
|
+
return value as RequestBody;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function makeToolbar(className: string, includeSearch = true) {
|
|
44
|
+
const toolbar = document.createElement('div');
|
|
45
|
+
toolbar.className = className;
|
|
46
|
+
if (includeSearch) {
|
|
47
|
+
const search = document.createElement('button');
|
|
48
|
+
const icon = document.createElement('span');
|
|
49
|
+
icon.className = 'anticon-global';
|
|
50
|
+
search.append(icon);
|
|
51
|
+
toolbar.append(search);
|
|
52
|
+
}
|
|
53
|
+
const submit = document.createElement('button');
|
|
54
|
+
submit.className = 'ai-chat-sender-submit';
|
|
55
|
+
toolbar.append(submit);
|
|
56
|
+
return toolbar;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function attachRoot(runtime: ChatBoxRuntime, root: HTMLElement) {
|
|
60
|
+
runtime.chatBoxModel.setChatBoxRef({ current: root });
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function resetRuntime(runtime: ChatBoxRuntime) {
|
|
64
|
+
runtime.chatBoxModel.setChatBoxRef({ current: null });
|
|
65
|
+
runtime.chatBoxModel.setCurrentEmployee(null);
|
|
66
|
+
runtime.chatBoxModel.setReadonly(false);
|
|
67
|
+
runtime.chatConversationModel.setCurrentConversation(undefined);
|
|
68
|
+
runtime.chatMessageModel.resetSessionState();
|
|
69
|
+
runtime.chatToolCallModel.resetSessionState('__draft__');
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function setRuntimeCalls(
|
|
73
|
+
runtime: ChatBoxRuntime,
|
|
74
|
+
sessionId: string,
|
|
75
|
+
messageId: string,
|
|
76
|
+
calls: ToolCall[],
|
|
77
|
+
from?: 'sub-agent',
|
|
78
|
+
) {
|
|
79
|
+
runtime.chatConversationModel.setCurrentConversation(sessionId);
|
|
80
|
+
runtime.chatBoxModel.setCurrentEmployee({ username: 'native-agent' });
|
|
81
|
+
runtime.chatMessageModel.setSessionMessages(sessionId, [
|
|
82
|
+
{
|
|
83
|
+
role: 'assistant',
|
|
84
|
+
content: {
|
|
85
|
+
content: '',
|
|
86
|
+
messageId,
|
|
87
|
+
metadata: { interruptId: `interrupt-${sessionId}` },
|
|
88
|
+
...(from ? { from } : {}),
|
|
89
|
+
tool_calls: calls.map((call) => ({ ...call })),
|
|
90
|
+
},
|
|
91
|
+
} as Message,
|
|
92
|
+
]);
|
|
93
|
+
for (const call of calls) {
|
|
94
|
+
if (call.invokeStatus === 'interrupted' || call.invokeStatus === 'waiting') {
|
|
95
|
+
runtime.chatToolCallModel.updateToolCallInvokeStatus(sessionId, messageId, call.id, call.invokeStatus);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function hydrateRuntimeCalls(runtime: ChatBoxRuntime, sessionId: string, messageId: string, calls: ToolCall[]) {
|
|
101
|
+
runtime.chatMessageModel.setSessionMessages(sessionId, (entries) =>
|
|
102
|
+
entries.map((entry) =>
|
|
103
|
+
String(entry.content?.messageId) === messageId
|
|
104
|
+
? { ...entry, content: { ...entry.content, tool_calls: calls.map((call) => ({ ...call })) } }
|
|
105
|
+
: entry,
|
|
106
|
+
),
|
|
107
|
+
);
|
|
108
|
+
for (const call of calls) {
|
|
109
|
+
if (call.invokeStatus === 'interrupted' || call.invokeStatus === 'waiting') {
|
|
110
|
+
runtime.chatToolCallModel.updateToolCallInvokeStatus(sessionId, messageId, call.id, call.invokeStatus);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function InterceptorScope({ requests, children }: { requests: AutoApprovalRequests; children: React.ReactNode }) {
|
|
116
|
+
useAutoApprovalInterceptor(requests);
|
|
117
|
+
return <AutoApprovalRequestsContext.Provider value={requests}>{children}</AutoApprovalRequestsContext.Provider>;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function DecisionControls({
|
|
121
|
+
runtime,
|
|
122
|
+
sessionId,
|
|
123
|
+
messageId,
|
|
124
|
+
actions,
|
|
125
|
+
}: {
|
|
126
|
+
runtime: ChatBoxRuntime;
|
|
127
|
+
sessionId: string;
|
|
128
|
+
messageId: string;
|
|
129
|
+
actions: Array<{ toolCallId: string; testId: string; label: string; onComplete?: () => void }>;
|
|
130
|
+
}) {
|
|
131
|
+
const { getDecisionActions } = useToolCallActions({ messageId, runtime });
|
|
132
|
+
const calls = runtime.chatMessageModel.sessions[sessionId]?.messages.find(
|
|
133
|
+
(entry) => String(entry.content?.messageId) === messageId,
|
|
134
|
+
)?.content.tool_calls;
|
|
135
|
+
const approve = async (call: ToolCall, onComplete?: () => void) => {
|
|
136
|
+
await getDecisionActions(call).approve();
|
|
137
|
+
onComplete?.();
|
|
138
|
+
};
|
|
139
|
+
return (
|
|
140
|
+
<>
|
|
141
|
+
{actions.map(({ toolCallId, testId, label, onComplete }) => {
|
|
142
|
+
const call = calls?.find((item) => item.id === toolCallId);
|
|
143
|
+
return (
|
|
144
|
+
<button key={testId} type="button" data-testid={testId} onClick={() => call && approve(call, onComplete)}>
|
|
145
|
+
{label}
|
|
146
|
+
</button>
|
|
147
|
+
);
|
|
148
|
+
})}
|
|
149
|
+
</>
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function renderProvider(app: ReturnType<typeof ClientV2.createMockClient>, children?: React.ReactNode) {
|
|
154
|
+
return render(
|
|
155
|
+
<FlowEngineProvider engine={app.flowEngine}>
|
|
156
|
+
<AutoApprovalProvider>{children}</AutoApprovalProvider>
|
|
157
|
+
</FlowEngineProvider>,
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function makeDecisionBody(sessionId: string, messageId: string, call: ToolCall) {
|
|
162
|
+
return {
|
|
163
|
+
values: {
|
|
164
|
+
sessionId,
|
|
165
|
+
messageId,
|
|
166
|
+
toolCallId: call.id,
|
|
167
|
+
userDecision: { type: 'approve' },
|
|
168
|
+
},
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
let app: ReturnType<typeof ClientV2.createMockClient>;
|
|
173
|
+
let globalRuntime: ChatBoxRuntime;
|
|
174
|
+
const additionalApps: ReturnType<typeof ClientV2.createMockClient>[] = [];
|
|
175
|
+
const additionalRuntimes: ChatBoxRuntime[] = [];
|
|
176
|
+
|
|
177
|
+
beforeEach(() => {
|
|
178
|
+
app = ClientV2.createMockClient();
|
|
179
|
+
globalRuntime = getGlobalChatBoxRuntime();
|
|
180
|
+
resetRuntime(globalRuntime);
|
|
181
|
+
vi.spyOn(ClientV2, 'useApp').mockReturnValue(app);
|
|
182
|
+
vi.spyOn(window, 'matchMedia').mockImplementation((media) => ({
|
|
183
|
+
matches: false,
|
|
184
|
+
media,
|
|
185
|
+
onchange: null,
|
|
186
|
+
addListener: () => {},
|
|
187
|
+
removeListener: () => {},
|
|
188
|
+
addEventListener: () => {},
|
|
189
|
+
removeEventListener: () => {},
|
|
190
|
+
dispatchEvent: () => false,
|
|
191
|
+
}));
|
|
192
|
+
app.flowEngine.context.defineProperty('aiConfigRepository', {
|
|
193
|
+
value: {
|
|
194
|
+
aiTools: [],
|
|
195
|
+
aiToolsLoading: false,
|
|
196
|
+
getAITools: vi.fn().mockResolvedValue([]),
|
|
197
|
+
},
|
|
198
|
+
});
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
afterEach(() => {
|
|
202
|
+
cleanup();
|
|
203
|
+
resetRuntime(globalRuntime);
|
|
204
|
+
for (const runtime of additionalRuntimes) resetRuntime(runtime);
|
|
205
|
+
app.apiMock.restore();
|
|
206
|
+
for (const additionalApp of additionalApps) additionalApp.apiMock.restore();
|
|
207
|
+
additionalApps.length = 0;
|
|
208
|
+
additionalRuntimes.length = 0;
|
|
209
|
+
vi.restoreAllMocks();
|
|
210
|
+
document.body.replaceChildren();
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
describe('session automatic approval UI and native approval bridge', () => {
|
|
214
|
+
it('registers the provider from both client plugin entries', async () => {
|
|
215
|
+
const use = vi.spyOn(app, 'use');
|
|
216
|
+
await new V2PluginAgentOrchestratorClient({}, app).load();
|
|
217
|
+
expect(use).toHaveBeenCalledWith(AutoApprovalProvider);
|
|
218
|
+
|
|
219
|
+
const legacyUse = vi.fn();
|
|
220
|
+
const legacyApp = {
|
|
221
|
+
use: legacyUse,
|
|
222
|
+
pluginSettingsManager: { add: vi.fn() },
|
|
223
|
+
eventBus: { addEventListener: vi.fn() },
|
|
224
|
+
aiManager: { toolsManager: { registerTools: vi.fn() } },
|
|
225
|
+
apiClient: { auth: { getToken: () => '' } },
|
|
226
|
+
} as unknown as ConstructorParameters<typeof V1PluginAgentOrchestratorClient>[1];
|
|
227
|
+
const legacyPlugin = Object.assign(Object.create(V1PluginAgentOrchestratorClient.prototype), {
|
|
228
|
+
app: legacyApp,
|
|
229
|
+
t: (value: string) => value,
|
|
230
|
+
}) as V1PluginAgentOrchestratorClient;
|
|
231
|
+
await legacyPlugin.load();
|
|
232
|
+
expect(legacyUse).toHaveBeenCalledWith(AutoApprovalProvider);
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
it('portals independent global and block controls into normal and compact native toolbars', async () => {
|
|
236
|
+
const blockRuntime = createChatBoxRuntime({ mode: 'block' });
|
|
237
|
+
resetRuntime(blockRuntime);
|
|
238
|
+
globalRuntime.chatConversationModel.setCurrentConversation('session-global');
|
|
239
|
+
blockRuntime.chatConversationModel.setCurrentConversation('session-block');
|
|
240
|
+
globalRuntime.chatBoxModel.setCurrentEmployee({ username: 'global-agent' });
|
|
241
|
+
blockRuntime.chatBoxModel.setCurrentEmployee({ username: 'block-agent' });
|
|
242
|
+
|
|
243
|
+
const globalRoot = document.createElement('div');
|
|
244
|
+
const globalToolbar = makeToolbar('ai-chat-sender-tools');
|
|
245
|
+
const blockRoot = document.createElement('div');
|
|
246
|
+
const blockToolbar = makeToolbar('ai-chat-sender-compact-toolbar', false);
|
|
247
|
+
globalRoot.append(globalToolbar, blockRoot);
|
|
248
|
+
blockRoot.append(blockToolbar);
|
|
249
|
+
document.body.append(globalRoot);
|
|
250
|
+
attachRoot(globalRuntime, globalRoot);
|
|
251
|
+
attachRoot(blockRuntime, blockRoot);
|
|
252
|
+
|
|
253
|
+
vi.spyOn(app.flowEngine, 'forEachModel').mockImplementation((visit) => {
|
|
254
|
+
visit({ chatBoxRuntime: blockRuntime } as unknown as FlowModel);
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
app.apiMock.onPost(/agentAutoApproval:get/).reply((config) => {
|
|
258
|
+
const { sessionId } = readRequestBody(config.data);
|
|
259
|
+
return [200, { data: { enabled: sessionId === 'session-global' || sessionId === 'session-next' } }];
|
|
260
|
+
});
|
|
261
|
+
const setRequests: RequestBody[] = [];
|
|
262
|
+
app.apiMock.onPost(/agentAutoApproval:set/).reply((config) => {
|
|
263
|
+
const body = readRequestBody(config.data);
|
|
264
|
+
setRequests.push(body);
|
|
265
|
+
return [200, { data: { enabled: body.enabled === true } }];
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
renderProvider(app);
|
|
269
|
+
|
|
270
|
+
await within(globalToolbar).findByRole('button', { name: 'Disable automatic approval' });
|
|
271
|
+
await within(blockToolbar).findByRole('button', { name: 'Enable automatic approval' });
|
|
272
|
+
expect(
|
|
273
|
+
within(globalToolbar).getByRole('button', { name: 'Disable automatic approval' }).getAttribute('aria-pressed'),
|
|
274
|
+
).toBe('true');
|
|
275
|
+
expect(
|
|
276
|
+
within(blockToolbar).getByRole('button', { name: 'Enable automatic approval' }).getAttribute('aria-pressed'),
|
|
277
|
+
).toBe('false');
|
|
278
|
+
expect(globalToolbar.children[1]?.getAttribute('data-agent-auto-approval')).toBe('true');
|
|
279
|
+
expect(blockToolbar.children[0]?.getAttribute('data-agent-auto-approval')).toBe('true');
|
|
280
|
+
|
|
281
|
+
const user = userEvent.setup();
|
|
282
|
+
await user.tab();
|
|
283
|
+
await user.tab();
|
|
284
|
+
expect(document.activeElement).toBe(
|
|
285
|
+
within(globalToolbar).getByRole('button', { name: 'Disable automatic approval' }),
|
|
286
|
+
);
|
|
287
|
+
act(() => fireEvent.click(within(globalToolbar).getByRole('button', { name: 'Disable automatic approval' })));
|
|
288
|
+
await waitFor(() =>
|
|
289
|
+
expect(
|
|
290
|
+
within(globalToolbar).getByRole('button', { name: 'Enable automatic approval' }).getAttribute('aria-pressed'),
|
|
291
|
+
).toBe('false'),
|
|
292
|
+
);
|
|
293
|
+
expect(setRequests).toContainEqual({ sessionId: 'session-global', enabled: false });
|
|
294
|
+
|
|
295
|
+
act(() => blockRuntime.chatConversationModel.setCurrentConversation('session-next'));
|
|
296
|
+
await within(blockToolbar).findByRole('button', {
|
|
297
|
+
name: 'Disable automatic approval',
|
|
298
|
+
});
|
|
299
|
+
expect(
|
|
300
|
+
within(blockToolbar).getByRole('button', { name: 'Disable automatic approval' }).getAttribute('aria-pressed'),
|
|
301
|
+
).toBe('true');
|
|
302
|
+
await waitFor(() => {
|
|
303
|
+
expect(
|
|
304
|
+
app.apiMock.history.post.some((request) => readRequestBody(request.data).sessionId === 'session-next'),
|
|
305
|
+
).toBe(true);
|
|
306
|
+
});
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
it('creates a conversation from the draft before persisting automatic approval', async () => {
|
|
310
|
+
globalRuntime.chatBoxModel.setCurrentEmployee({ username: 'draft-agent' });
|
|
311
|
+
globalRuntime.chatMessageModel.setSessionSystemMessage(undefined, 'Draft system prompt');
|
|
312
|
+
globalRuntime.chatMessageModel.setSessionSkillSettings(undefined, { tools: ['read_file'], skills: [] });
|
|
313
|
+
globalRuntime.chatBoxModel.setModel({ llmService: 'openai', model: 'gpt-fast' });
|
|
314
|
+
const root = document.createElement('div');
|
|
315
|
+
const toolbar = makeToolbar('ai-chat-sender-compact-toolbar', false);
|
|
316
|
+
root.append(toolbar);
|
|
317
|
+
document.body.append(root);
|
|
318
|
+
attachRoot(globalRuntime, root);
|
|
319
|
+
|
|
320
|
+
let createdValues: RequestBody | undefined;
|
|
321
|
+
app.apiMock.onPost(/aiConversations:create/).reply((config) => {
|
|
322
|
+
const body = readRequestBody(config.data);
|
|
323
|
+
const values = body.values;
|
|
324
|
+
createdValues = values && typeof values === 'object' && !Array.isArray(values) ? (values as RequestBody) : body;
|
|
325
|
+
return [200, { data: { sessionId: 'draft-session-created' } }];
|
|
326
|
+
});
|
|
327
|
+
let automaticApprovalEnabled = false;
|
|
328
|
+
app.apiMock.onPost(/agentAutoApproval:get/).reply(() => [200, { data: { enabled: automaticApprovalEnabled } }]);
|
|
329
|
+
const setRequests: RequestBody[] = [];
|
|
330
|
+
app.apiMock.onPost(/agentAutoApproval:set/).reply((config) => {
|
|
331
|
+
const body = readRequestBody(config.data);
|
|
332
|
+
setRequests.push(body);
|
|
333
|
+
automaticApprovalEnabled = body.enabled === true;
|
|
334
|
+
return [200, { data: { enabled: automaticApprovalEnabled } }];
|
|
335
|
+
});
|
|
336
|
+
|
|
337
|
+
renderProvider(app);
|
|
338
|
+
await within(toolbar).findByRole('button', { name: 'Enable automatic approval' });
|
|
339
|
+
act(() => fireEvent.click(within(toolbar).getByRole('button', { name: 'Enable automatic approval' })));
|
|
340
|
+
|
|
341
|
+
await waitFor(() => expect(globalRuntime.chatConversationModel.currentConversation).toBe('draft-session-created'));
|
|
342
|
+
expect(createdValues).toMatchObject({
|
|
343
|
+
aiEmployee: { username: 'draft-agent' },
|
|
344
|
+
systemMessage: 'Draft system prompt',
|
|
345
|
+
skillSettings: { tools: ['read_file'], skills: [] },
|
|
346
|
+
modelSettings: { llmService: 'openai', model: 'gpt-fast' },
|
|
347
|
+
});
|
|
348
|
+
expect(setRequests).toContainEqual({ sessionId: 'draft-session-created', enabled: true });
|
|
349
|
+
expect(globalRuntime.chatMessageModel.sessions['draft-session-created']?.systemMessage).toBe('Draft system prompt');
|
|
350
|
+
await waitFor(
|
|
351
|
+
() =>
|
|
352
|
+
expect(
|
|
353
|
+
within(toolbar).getByRole('button', { name: 'Disable automatic approval' }).getAttribute('aria-pressed'),
|
|
354
|
+
).toBe('true'),
|
|
355
|
+
{ timeout: 2000 },
|
|
356
|
+
);
|
|
357
|
+
});
|
|
358
|
+
|
|
359
|
+
it('refreshes same-session runtimes together while keeping another session isolated and refreshing on focus', async () => {
|
|
360
|
+
const sameSessionRuntime = createChatBoxRuntime({ mode: 'block' });
|
|
361
|
+
const otherSessionRuntime = createChatBoxRuntime({ mode: 'block' });
|
|
362
|
+
resetRuntime(sameSessionRuntime);
|
|
363
|
+
resetRuntime(otherSessionRuntime);
|
|
364
|
+
globalRuntime.chatConversationModel.setCurrentConversation('shared-session');
|
|
365
|
+
sameSessionRuntime.chatConversationModel.setCurrentConversation('shared-session');
|
|
366
|
+
otherSessionRuntime.chatConversationModel.setCurrentConversation('other-session');
|
|
367
|
+
for (const runtime of [globalRuntime, sameSessionRuntime, otherSessionRuntime]) {
|
|
368
|
+
runtime.chatBoxModel.setCurrentEmployee({ username: 'test-agent' });
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
const sharedRoot = document.createElement('div');
|
|
372
|
+
const sameSessionRoot = document.createElement('div');
|
|
373
|
+
const otherSessionRoot = document.createElement('div');
|
|
374
|
+
const globalToolbar = makeToolbar('ai-chat-sender-tools');
|
|
375
|
+
const sameSessionToolbar = makeToolbar('ai-chat-sender-compact-toolbar', false);
|
|
376
|
+
const otherSessionToolbar = makeToolbar('ai-chat-sender-tools');
|
|
377
|
+
sharedRoot.append(globalToolbar);
|
|
378
|
+
sameSessionRoot.append(sameSessionToolbar);
|
|
379
|
+
otherSessionRoot.append(otherSessionToolbar);
|
|
380
|
+
document.body.append(sharedRoot, sameSessionRoot, otherSessionRoot);
|
|
381
|
+
attachRoot(globalRuntime, sharedRoot);
|
|
382
|
+
attachRoot(sameSessionRuntime, sameSessionRoot);
|
|
383
|
+
attachRoot(otherSessionRuntime, otherSessionRoot);
|
|
384
|
+
|
|
385
|
+
vi.spyOn(app.flowEngine, 'forEachModel').mockImplementation((visit) => {
|
|
386
|
+
visit({ chatBoxRuntime: sameSessionRuntime } as unknown as FlowModel);
|
|
387
|
+
visit({ chatBoxRuntime: otherSessionRuntime } as unknown as FlowModel);
|
|
388
|
+
});
|
|
389
|
+
|
|
390
|
+
const persisted = new Map([
|
|
391
|
+
['shared-session', false],
|
|
392
|
+
['other-session', false],
|
|
393
|
+
]);
|
|
394
|
+
const reads = new Map<string, number>();
|
|
395
|
+
app.apiMock.onPost(/agentAutoApproval:get/).reply((config) => {
|
|
396
|
+
const { sessionId } = readRequestBody(config.data);
|
|
397
|
+
if (typeof sessionId !== 'string') return [400, {}];
|
|
398
|
+
reads.set(sessionId, (reads.get(sessionId) ?? 0) + 1);
|
|
399
|
+
return [200, { data: { enabled: persisted.get(sessionId) === true } }];
|
|
400
|
+
});
|
|
401
|
+
app.apiMock.onPost(/agentAutoApproval:set/).reply((config) => {
|
|
402
|
+
const body = readRequestBody(config.data);
|
|
403
|
+
const sessionId = body.sessionId;
|
|
404
|
+
if (typeof sessionId !== 'string') return [400, {}];
|
|
405
|
+
persisted.set(sessionId, body.enabled === true);
|
|
406
|
+
return [200, { data: { enabled: persisted.get(sessionId) === true } }];
|
|
407
|
+
});
|
|
408
|
+
|
|
409
|
+
renderProvider(app);
|
|
410
|
+
await within(globalToolbar).findByRole('button', { name: 'Enable automatic approval' });
|
|
411
|
+
await within(sameSessionToolbar).findByRole('button', {
|
|
412
|
+
name: 'Enable automatic approval',
|
|
413
|
+
});
|
|
414
|
+
await within(otherSessionToolbar).findByRole('button', {
|
|
415
|
+
name: 'Enable automatic approval',
|
|
416
|
+
});
|
|
417
|
+
const initialOtherReads = reads.get('other-session');
|
|
418
|
+
|
|
419
|
+
await userEvent.setup().click(within(globalToolbar).getByRole('button', { name: 'Enable automatic approval' }));
|
|
420
|
+
await waitFor(() =>
|
|
421
|
+
expect(
|
|
422
|
+
within(globalToolbar).getByRole('button', { name: 'Disable automatic approval' }).getAttribute('aria-pressed'),
|
|
423
|
+
).toBe('true'),
|
|
424
|
+
);
|
|
425
|
+
await waitFor(() =>
|
|
426
|
+
expect(
|
|
427
|
+
within(sameSessionToolbar)
|
|
428
|
+
.getByRole('button', { name: 'Disable automatic approval' })
|
|
429
|
+
.getAttribute('aria-pressed'),
|
|
430
|
+
).toBe('true'),
|
|
431
|
+
);
|
|
432
|
+
expect(
|
|
433
|
+
within(otherSessionToolbar)
|
|
434
|
+
.getByRole('button', { name: 'Enable automatic approval' })
|
|
435
|
+
.getAttribute('aria-pressed'),
|
|
436
|
+
).toBe('false');
|
|
437
|
+
expect(reads.get('other-session')).toBe(initialOtherReads);
|
|
438
|
+
|
|
439
|
+
persisted.set('other-session', true);
|
|
440
|
+
act(() => window.dispatchEvent(new Event('focus')));
|
|
441
|
+
await waitFor(() =>
|
|
442
|
+
expect(
|
|
443
|
+
within(otherSessionToolbar)
|
|
444
|
+
.getByRole('button', { name: 'Disable automatic approval' })
|
|
445
|
+
.getAttribute('aria-pressed'),
|
|
446
|
+
).toBe('true'),
|
|
447
|
+
);
|
|
448
|
+
});
|
|
449
|
+
|
|
450
|
+
it('runs the native sequential approval effect through the real bridge request identity', async () => {
|
|
451
|
+
const sessionId = 'session-native-approval';
|
|
452
|
+
const messageId = 'message-native-approval';
|
|
453
|
+
const toolCallId = 'tool-call-native-approval';
|
|
454
|
+
globalRuntime.chatConversationModel.setCurrentConversation(sessionId);
|
|
455
|
+
globalRuntime.chatBoxModel.setCurrentEmployee({ username: 'native-agent' });
|
|
456
|
+
const call: ToolCall = {
|
|
457
|
+
id: toolCallId,
|
|
458
|
+
type: 'function',
|
|
459
|
+
name: 'read_file',
|
|
460
|
+
args: { path: 'notes.md' },
|
|
461
|
+
invokeStatus: 'interrupted',
|
|
462
|
+
auto: false,
|
|
463
|
+
};
|
|
464
|
+
const message: Message = {
|
|
465
|
+
role: 'assistant',
|
|
466
|
+
content: { content: '', messageId, tool_calls: [call] },
|
|
467
|
+
};
|
|
468
|
+
globalRuntime.chatMessageModel.setSessionMessages(sessionId, [message]);
|
|
469
|
+
globalRuntime.chatToolCallModel.updateToolCallInvokeStatus(sessionId, messageId, toolCallId, 'interrupted');
|
|
470
|
+
|
|
471
|
+
const root = document.createElement('div');
|
|
472
|
+
const toolbar = makeToolbar('ai-chat-sender-tools');
|
|
473
|
+
root.append(toolbar);
|
|
474
|
+
document.body.append(root);
|
|
475
|
+
attachRoot(globalRuntime, root);
|
|
476
|
+
|
|
477
|
+
app.apiMock.onPost(/agentAutoApproval:get/).reply(200, { data: { enabled: true } });
|
|
478
|
+
app.apiMock.onPost(/agentAutoApproval:eligible/).reply(200, { data: { eligible: true } });
|
|
479
|
+
let decisionRequest: RequestBody | undefined;
|
|
480
|
+
app.apiMock.onPost(/aiConversations:updateUserDecision/).reply((config) => {
|
|
481
|
+
decisionRequest = readRequestBody(config.data);
|
|
482
|
+
return [200, { data: { updated: 0 } }];
|
|
483
|
+
});
|
|
484
|
+
|
|
485
|
+
const NativeToolCardHarness = observer(() => {
|
|
486
|
+
const currentMessage = globalRuntime.chatMessageModel.sessions[sessionId]?.messages[0];
|
|
487
|
+
const calls = currentMessage?.content.tool_calls ?? emptyToolCalls;
|
|
488
|
+
React.useEffect(() => {
|
|
489
|
+
const approveAutomatically = async () => {
|
|
490
|
+
for (const toolCall of calls) {
|
|
491
|
+
if (toolCall.invokeStatus === 'interrupted' && toolCall.auto === true) {
|
|
492
|
+
await app.apiClient
|
|
493
|
+
.resource('aiConversations')
|
|
494
|
+
.updateUserDecision(makeDecisionBody(sessionId, messageId, toolCall));
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
};
|
|
498
|
+
approveAutomatically();
|
|
499
|
+
}, [calls]);
|
|
500
|
+
return null;
|
|
501
|
+
});
|
|
502
|
+
|
|
503
|
+
renderProvider(
|
|
504
|
+
app,
|
|
505
|
+
<ChatBoxRuntimeProvider runtime={globalRuntime}>
|
|
506
|
+
<NativeToolCardHarness />
|
|
507
|
+
</ChatBoxRuntimeProvider>,
|
|
508
|
+
);
|
|
509
|
+
|
|
510
|
+
await waitFor(() =>
|
|
511
|
+
expect(decisionRequest).toMatchObject({
|
|
512
|
+
sessionId,
|
|
513
|
+
messageId,
|
|
514
|
+
toolCallId,
|
|
515
|
+
userDecision: { type: 'approve' },
|
|
516
|
+
agentAutoApproval: true,
|
|
517
|
+
}),
|
|
518
|
+
);
|
|
519
|
+
await waitFor(() => {
|
|
520
|
+
expect(globalRuntime.chatMessageModel.sessions[sessionId]?.messages[0]?.content.tool_calls?.[0]?.auto).toBe(
|
|
521
|
+
false,
|
|
522
|
+
);
|
|
523
|
+
});
|
|
524
|
+
});
|
|
525
|
+
|
|
526
|
+
it('approves the next queued ASK after the native model replaces toolCalls and resumes once all are waiting', async () => {
|
|
527
|
+
const sessionId = 'session-native-sibling-asks';
|
|
528
|
+
const messageId = 'message-native-sibling-asks';
|
|
529
|
+
globalRuntime.chatConversationModel.setCurrentConversation(sessionId);
|
|
530
|
+
globalRuntime.chatBoxModel.setCurrentEmployee({ username: 'native-agent' });
|
|
531
|
+
const calls: ToolCall[] = [
|
|
532
|
+
{
|
|
533
|
+
id: 'call-a',
|
|
534
|
+
type: 'function',
|
|
535
|
+
name: 'read_file',
|
|
536
|
+
args: { path: 'a.md' },
|
|
537
|
+
invokeStatus: 'interrupted',
|
|
538
|
+
auto: false,
|
|
539
|
+
},
|
|
540
|
+
{
|
|
541
|
+
id: 'call-b',
|
|
542
|
+
type: 'function',
|
|
543
|
+
name: 'search_files',
|
|
544
|
+
args: { query: 'B' },
|
|
545
|
+
invokeStatus: 'interrupted',
|
|
546
|
+
auto: false,
|
|
547
|
+
},
|
|
548
|
+
];
|
|
549
|
+
globalRuntime.chatMessageModel.setSessionMessages(sessionId, [
|
|
550
|
+
{ role: 'assistant', content: { content: '', messageId, tool_calls: calls } },
|
|
551
|
+
]);
|
|
552
|
+
for (const call of calls) {
|
|
553
|
+
globalRuntime.chatToolCallModel.updateToolCallInvokeStatus(sessionId, messageId, call.id, 'interrupted');
|
|
554
|
+
}
|
|
555
|
+
const modelSession = globalRuntime.chatToolCallModel.sessions[sessionId];
|
|
556
|
+
const initialToolCallsMap = modelSession?.toolCalls;
|
|
557
|
+
expect(modelSession).toBeDefined();
|
|
558
|
+
|
|
559
|
+
const root = document.createElement('div');
|
|
560
|
+
const toolbar = makeToolbar('ai-chat-sender-tools');
|
|
561
|
+
root.append(toolbar);
|
|
562
|
+
document.body.append(root);
|
|
563
|
+
attachRoot(globalRuntime, root);
|
|
564
|
+
|
|
565
|
+
app.apiMock.onPost(/agentAutoApproval:get/).reply(200, { data: { enabled: true } });
|
|
566
|
+
app.apiMock.onPost(/agentAutoApproval:eligible/).reply(200, { data: { eligible: true } });
|
|
567
|
+
const decisionRequests: RequestBody[] = [];
|
|
568
|
+
app.apiMock.onPost(/aiConversations:updateUserDecision/).reply((config) => {
|
|
569
|
+
decisionRequests.push(readRequestBody(config.data));
|
|
570
|
+
return [
|
|
571
|
+
200,
|
|
572
|
+
{
|
|
573
|
+
data: {
|
|
574
|
+
updated: 1,
|
|
575
|
+
toolCalls: calls.map((call) => ({ ...call, userDecision: { type: 'approve' } })),
|
|
576
|
+
},
|
|
577
|
+
},
|
|
578
|
+
];
|
|
579
|
+
});
|
|
580
|
+
resumeToolCallMock.mockClear();
|
|
581
|
+
|
|
582
|
+
const NativeToolCardHarness = observer(() => {
|
|
583
|
+
const currentMessage = globalRuntime.chatMessageModel.sessions[sessionId]?.messages[0];
|
|
584
|
+
const toolCalls = currentMessage?.content.tool_calls ?? emptyToolCalls;
|
|
585
|
+
const requested = React.useRef(new Set<string>());
|
|
586
|
+
const { getDecisionActions } = useToolCallActions({ messageId, runtime: globalRuntime });
|
|
587
|
+
|
|
588
|
+
React.useEffect(() => {
|
|
589
|
+
const approveAutomatically = async () => {
|
|
590
|
+
for (const toolCall of toolCalls) {
|
|
591
|
+
if (
|
|
592
|
+
toolCall.invokeStatus === 'interrupted' &&
|
|
593
|
+
toolCall.auto === true &&
|
|
594
|
+
!requested.current.has(toolCall.id)
|
|
595
|
+
) {
|
|
596
|
+
requested.current.add(toolCall.id);
|
|
597
|
+
await getDecisionActions(toolCall).approve();
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
};
|
|
601
|
+
approveAutomatically();
|
|
602
|
+
}, [getDecisionActions, toolCalls]);
|
|
603
|
+
|
|
604
|
+
return null;
|
|
605
|
+
});
|
|
606
|
+
|
|
607
|
+
renderProvider(
|
|
608
|
+
app,
|
|
609
|
+
<ChatBoxRuntimeProvider runtime={globalRuntime}>
|
|
610
|
+
<NativeToolCardHarness />
|
|
611
|
+
</ChatBoxRuntimeProvider>,
|
|
612
|
+
);
|
|
613
|
+
|
|
614
|
+
await waitFor(() => expect(decisionRequests).toHaveLength(2));
|
|
615
|
+
expect(decisionRequests.map((request) => request.toolCallId)).toEqual(['call-a', 'call-b']);
|
|
616
|
+
expect(decisionRequests.every((request) => request.agentAutoApproval === true)).toBe(true);
|
|
617
|
+
await waitFor(() => expect(globalRuntime.chatToolCallModel.isAllWaiting(sessionId, messageId)).toBe(true));
|
|
618
|
+
expect(globalRuntime.chatToolCallModel.sessions[sessionId]).toBe(modelSession);
|
|
619
|
+
expect(modelSession?.toolCalls).not.toBe(initialToolCallsMap);
|
|
620
|
+
expect(globalRuntime.chatToolCallModel.getInvokeStatus(sessionId, messageId, 'call-a')).toBe('waiting');
|
|
621
|
+
expect(globalRuntime.chatToolCallModel.getInvokeStatus(sessionId, messageId, 'call-b')).toBe('waiting');
|
|
622
|
+
expect(resumeToolCallMock).toHaveBeenCalledTimes(1);
|
|
623
|
+
expect(resumeToolCallMock).toHaveBeenCalledWith(
|
|
624
|
+
expect.objectContaining({ sessionId, messageId, toolCallIds: ['call-a', 'call-b'], toolCallResults: [] }),
|
|
625
|
+
);
|
|
626
|
+
});
|
|
627
|
+
|
|
628
|
+
it('does not approve the second queued ASK after auto-approval is disabled during the first request', async () => {
|
|
629
|
+
const sessionId = 'session-two-asks';
|
|
630
|
+
const messageId = 'message-two-asks';
|
|
631
|
+
globalRuntime.chatConversationModel.setCurrentConversation(sessionId);
|
|
632
|
+
globalRuntime.chatBoxModel.setCurrentEmployee({ username: 'native-agent' });
|
|
633
|
+
const calls: ToolCall[] = [
|
|
634
|
+
{
|
|
635
|
+
id: 'call-a',
|
|
636
|
+
type: 'function',
|
|
637
|
+
name: 'read_file',
|
|
638
|
+
args: { path: 'a.md' },
|
|
639
|
+
invokeStatus: 'interrupted',
|
|
640
|
+
auto: false,
|
|
641
|
+
},
|
|
642
|
+
{
|
|
643
|
+
id: 'call-b',
|
|
644
|
+
type: 'function',
|
|
645
|
+
name: 'search_files',
|
|
646
|
+
args: { query: 'B' },
|
|
647
|
+
invokeStatus: 'interrupted',
|
|
648
|
+
auto: false,
|
|
649
|
+
},
|
|
650
|
+
];
|
|
651
|
+
globalRuntime.chatMessageModel.setSessionMessages(sessionId, [
|
|
652
|
+
{ role: 'assistant', content: { content: '', messageId, tool_calls: calls } },
|
|
653
|
+
]);
|
|
654
|
+
for (const call of calls) {
|
|
655
|
+
globalRuntime.chatToolCallModel.updateToolCallInvokeStatus(sessionId, messageId, call.id, 'interrupted');
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
const root = document.createElement('div');
|
|
659
|
+
const toolbar = makeToolbar('ai-chat-sender-tools');
|
|
660
|
+
root.append(toolbar);
|
|
661
|
+
document.body.append(root);
|
|
662
|
+
attachRoot(globalRuntime, root);
|
|
663
|
+
|
|
664
|
+
let enabled = true;
|
|
665
|
+
app.apiMock.onPost(/agentAutoApproval:get/).reply(() => [200, { data: { enabled } }]);
|
|
666
|
+
app.apiMock.onPost(/agentAutoApproval:set/).reply((config) => {
|
|
667
|
+
enabled = readRequestBody(config.data).enabled === true;
|
|
668
|
+
return [200, { data: { enabled } }];
|
|
669
|
+
});
|
|
670
|
+
app.apiMock.onPost(/agentAutoApproval:eligible/).reply(200, { data: { eligible: true } });
|
|
671
|
+
|
|
672
|
+
const decisionRequests: RequestBody[] = [];
|
|
673
|
+
let manualBUpdated = false;
|
|
674
|
+
let releaseFirstRequest: (() => void) | undefined;
|
|
675
|
+
app.apiMock.onPost(/aiConversations:updateUserDecision/).reply((config) => {
|
|
676
|
+
const body = readRequestBody(config.data);
|
|
677
|
+
decisionRequests.push(body);
|
|
678
|
+
if (body.toolCallId === 'call-a' && body.agentAutoApproval === true && !releaseFirstRequest) {
|
|
679
|
+
return new Promise((resolve) => {
|
|
680
|
+
releaseFirstRequest = () => resolve([200, { data: { updated: 0 } }]);
|
|
681
|
+
});
|
|
682
|
+
}
|
|
683
|
+
const staleAutomaticRequest = body.agentAutoApproval === true && !enabled;
|
|
684
|
+
if (body.toolCallId === 'call-b' && body.agentAutoApproval !== true && !enabled) manualBUpdated = true;
|
|
685
|
+
return [200, { data: { updated: staleAutomaticRequest ? 0 : 1 } }];
|
|
686
|
+
});
|
|
687
|
+
|
|
688
|
+
const NativeToolCardHarness = observer(() => {
|
|
689
|
+
const currentMessage = globalRuntime.chatMessageModel.sessions[sessionId]?.messages[0];
|
|
690
|
+
const toolCalls = currentMessage?.content.tool_calls ?? emptyToolCalls;
|
|
691
|
+
const automaticRequests = React.useRef(new Set<string>());
|
|
692
|
+
React.useEffect(() => {
|
|
693
|
+
const approveAutomatically = async () => {
|
|
694
|
+
for (const toolCall of toolCalls) {
|
|
695
|
+
if (
|
|
696
|
+
toolCall.invokeStatus === 'interrupted' &&
|
|
697
|
+
toolCall.auto === true &&
|
|
698
|
+
!automaticRequests.current.has(toolCall.id)
|
|
699
|
+
) {
|
|
700
|
+
automaticRequests.current.add(toolCall.id);
|
|
701
|
+
await app.apiClient
|
|
702
|
+
.resource('aiConversations')
|
|
703
|
+
.updateUserDecision(makeDecisionBody(sessionId, messageId, toolCall));
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
};
|
|
707
|
+
approveAutomatically();
|
|
708
|
+
}, [toolCalls]);
|
|
709
|
+
const approveManually = async () => {
|
|
710
|
+
const callB = toolCalls.find((toolCall) => toolCall.id === 'call-b');
|
|
711
|
+
if (callB?.invokeStatus === 'interrupted') {
|
|
712
|
+
await app.apiClient
|
|
713
|
+
.resource('aiConversations')
|
|
714
|
+
.updateUserDecision(makeDecisionBody(sessionId, messageId, callB));
|
|
715
|
+
}
|
|
716
|
+
};
|
|
717
|
+
return (
|
|
718
|
+
<button type="button" onClick={approveManually}>
|
|
719
|
+
Allow use
|
|
720
|
+
</button>
|
|
721
|
+
);
|
|
722
|
+
});
|
|
723
|
+
|
|
724
|
+
renderProvider(
|
|
725
|
+
app,
|
|
726
|
+
<ChatBoxRuntimeProvider runtime={globalRuntime}>
|
|
727
|
+
<NativeToolCardHarness />
|
|
728
|
+
</ChatBoxRuntimeProvider>,
|
|
729
|
+
);
|
|
730
|
+
|
|
731
|
+
await within(toolbar).findByRole('button', { name: 'Disable automatic approval' });
|
|
732
|
+
await waitFor(() => {
|
|
733
|
+
const marked = globalRuntime.chatMessageModel.sessions[sessionId]?.messages[0]?.content.tool_calls ?? [];
|
|
734
|
+
expect(marked.find((call) => call.id === 'call-a')?.auto).toBe(true);
|
|
735
|
+
expect(marked.find((call) => call.id === 'call-b')?.auto).toBe(false);
|
|
736
|
+
});
|
|
737
|
+
await waitFor(() => expect(releaseFirstRequest).toBeDefined());
|
|
738
|
+
|
|
739
|
+
const user = userEvent.setup();
|
|
740
|
+
await user.click(within(toolbar).getByRole('button', { name: 'Disable automatic approval' }));
|
|
741
|
+
await waitFor(
|
|
742
|
+
() =>
|
|
743
|
+
expect(
|
|
744
|
+
within(toolbar).getByRole('button', { name: 'Enable automatic approval' }).getAttribute('aria-pressed'),
|
|
745
|
+
).toBe('false'),
|
|
746
|
+
{ timeout: 2000 },
|
|
747
|
+
);
|
|
748
|
+
await waitFor(() => {
|
|
749
|
+
const restored = globalRuntime.chatMessageModel.sessions[sessionId]?.messages[0]?.content.tool_calls ?? [];
|
|
750
|
+
expect(restored.every((call) => call.auto === false)).toBe(true);
|
|
751
|
+
});
|
|
752
|
+
|
|
753
|
+
releaseFirstRequest?.();
|
|
754
|
+
await waitFor(() => expect(decisionRequests.some((request) => request.toolCallId === 'call-a')).toBe(true));
|
|
755
|
+
await act(async () => Promise.resolve());
|
|
756
|
+
const automaticB = decisionRequests.filter((request) => request.toolCallId === 'call-b');
|
|
757
|
+
expect(
|
|
758
|
+
automaticB.every((request) => request.agentAutoApproval === true),
|
|
759
|
+
'a second automatic request must be suppressed or retain its marker so the disabled server policy rejects it',
|
|
760
|
+
).toBe(true);
|
|
761
|
+
|
|
762
|
+
await user.click(screen.getByRole('button', { name: 'Allow use' }));
|
|
763
|
+
await waitFor(() => {
|
|
764
|
+
expect(
|
|
765
|
+
decisionRequests.some((request) => request.toolCallId === 'call-b' && !('agentAutoApproval' in request)),
|
|
766
|
+
).toBe(true);
|
|
767
|
+
expect(manualBUpdated).toBe(true);
|
|
768
|
+
});
|
|
769
|
+
});
|
|
770
|
+
|
|
771
|
+
it('keeps the automatic origin authorized when the embedded runtime leaves before the winner response', async () => {
|
|
772
|
+
const sessionId = 'session-shared-app-runtimes';
|
|
773
|
+
const messageId = 'message-shared-app-runtimes';
|
|
774
|
+
const interruptId = `interrupt-${sessionId}`;
|
|
775
|
+
const calls: ToolCall[] = [
|
|
776
|
+
{
|
|
777
|
+
id: 'call-auto-origin',
|
|
778
|
+
type: 'function',
|
|
779
|
+
name: 'tool-a',
|
|
780
|
+
args: { value: 'A' },
|
|
781
|
+
invokeStatus: 'interrupted',
|
|
782
|
+
auto: false,
|
|
783
|
+
willInterrupt: true,
|
|
784
|
+
},
|
|
785
|
+
{
|
|
786
|
+
id: 'call-manual-winner',
|
|
787
|
+
type: 'function',
|
|
788
|
+
name: 'tool-b',
|
|
789
|
+
args: { value: 'B' },
|
|
790
|
+
invokeStatus: 'interrupted',
|
|
791
|
+
auto: false,
|
|
792
|
+
willInterrupt: true,
|
|
793
|
+
},
|
|
794
|
+
];
|
|
795
|
+
const embeddedRuntime = createChatBoxRuntime({ mode: 'block' });
|
|
796
|
+
additionalRuntimes.push(embeddedRuntime);
|
|
797
|
+
setRuntimeCalls(globalRuntime, sessionId, messageId, calls);
|
|
798
|
+
setRuntimeCalls(embeddedRuntime, sessionId, messageId, calls);
|
|
799
|
+
const requests = new AutoApprovalRequests();
|
|
800
|
+
const serverCalls = calls.map((call) => ({ ...call }));
|
|
801
|
+
const decisionRequests: RequestBody[] = [];
|
|
802
|
+
let enrolled = false;
|
|
803
|
+
let issued = false;
|
|
804
|
+
let releaseAutomaticPartial: (() => void) | undefined;
|
|
805
|
+
let releaseManualWinner: (() => void) | undefined;
|
|
806
|
+
let automaticPayload: RequestBody | undefined;
|
|
807
|
+
const invokeA = vi.fn(async () => ({ status: 'success', content: 'A result' }));
|
|
808
|
+
const invokeB = vi.fn(async () => ({ status: 'success', content: 'B result' }));
|
|
809
|
+
|
|
810
|
+
app.aiManager.toolsManager.registerTools('tool-a', { invoke: invokeA });
|
|
811
|
+
app.aiManager.toolsManager.registerTools('tool-b', { invoke: invokeB });
|
|
812
|
+
app.apiMock.onPost(/agentAutoApproval:get/).reply(200, { data: { enabled: true } });
|
|
813
|
+
app.apiMock.onPost(/agentAutoApproval:eligible/).reply((config) => {
|
|
814
|
+
const request = readRequestBody(config.data);
|
|
815
|
+
return [200, { data: { eligible: request.toolCallId === 'call-auto-origin' } }];
|
|
816
|
+
});
|
|
817
|
+
app.apiMock.onPost(/aiConversations:updateUserDecision/).reply((config) => {
|
|
818
|
+
const request = readRequestBody(config.data);
|
|
819
|
+
decisionRequests.push(request);
|
|
820
|
+
const selected = serverCalls.find((call) => call.id === request.toolCallId);
|
|
821
|
+
let nativeUpdated = 0;
|
|
822
|
+
if (selected?.invokeStatus === 'interrupted') {
|
|
823
|
+
selected.invokeStatus = 'waiting';
|
|
824
|
+
selected.userDecision = request.userDecision;
|
|
825
|
+
nativeUpdated = 1;
|
|
826
|
+
if (request.agentAutoApproval === true) enrolled = true;
|
|
827
|
+
}
|
|
828
|
+
const ready =
|
|
829
|
+
enrolled && serverCalls.filter((call) => call.willInterrupt).every((call) => call.invokeStatus === 'waiting');
|
|
830
|
+
const canFinalize = nativeUpdated === 1 && ready && !issued;
|
|
831
|
+
if (canFinalize) issued = true;
|
|
832
|
+
const payload: RequestBody = {
|
|
833
|
+
updated: enrolled ? (canFinalize ? 1 : 0) : nativeUpdated,
|
|
834
|
+
toolCalls: serverCalls.map((call) => ({ ...call })),
|
|
835
|
+
...(enrolled
|
|
836
|
+
? {
|
|
837
|
+
agentAutoApprovalCheckpoint: {
|
|
838
|
+
interruptId,
|
|
839
|
+
decisionRecorded: nativeUpdated === 1,
|
|
840
|
+
canFinalize,
|
|
841
|
+
validSnapshot: true,
|
|
842
|
+
},
|
|
843
|
+
}
|
|
844
|
+
: {}),
|
|
845
|
+
};
|
|
846
|
+
if (request.toolCallId === 'call-auto-origin' && request.agentAutoApproval === true) {
|
|
847
|
+
automaticPayload = payload;
|
|
848
|
+
return new Promise((resolve) => {
|
|
849
|
+
releaseAutomaticPartial = () => resolve([200, { data: payload }]);
|
|
850
|
+
});
|
|
851
|
+
}
|
|
852
|
+
if (request.toolCallId === 'call-manual-winner') {
|
|
853
|
+
return new Promise((resolve) => {
|
|
854
|
+
releaseManualWinner = () => resolve([200, { data: payload }]);
|
|
855
|
+
});
|
|
856
|
+
}
|
|
857
|
+
return [200, { data: payload }];
|
|
858
|
+
});
|
|
859
|
+
|
|
860
|
+
const AppHarness = () => {
|
|
861
|
+
const [showEmbedded, setShowEmbedded] = React.useState(true);
|
|
862
|
+
return (
|
|
863
|
+
<InterceptorScope requests={requests}>
|
|
864
|
+
<AutoApprovalBridge runtime={globalRuntime} enabled />
|
|
865
|
+
{showEmbedded && <AutoApprovalBridge runtime={embeddedRuntime} enabled />}
|
|
866
|
+
<button type="button" data-testid="unmount-embedded-runtime" onClick={() => setShowEmbedded(false)}>
|
|
867
|
+
Leave embedded chat
|
|
868
|
+
</button>
|
|
869
|
+
<ChatBoxRuntimeProvider runtime={globalRuntime}>
|
|
870
|
+
<DecisionControls
|
|
871
|
+
runtime={globalRuntime}
|
|
872
|
+
sessionId={sessionId}
|
|
873
|
+
messageId={messageId}
|
|
874
|
+
actions={[
|
|
875
|
+
{ toolCallId: 'call-auto-origin', testId: 'approve-origin-call', label: 'Approve A' },
|
|
876
|
+
{ toolCallId: 'call-manual-winner', testId: 'approve-winner-call', label: 'Approve B' },
|
|
877
|
+
]}
|
|
878
|
+
/>
|
|
879
|
+
</ChatBoxRuntimeProvider>
|
|
880
|
+
</InterceptorScope>
|
|
881
|
+
);
|
|
882
|
+
};
|
|
883
|
+
|
|
884
|
+
resumeToolCallMock.mockClear();
|
|
885
|
+
render(
|
|
886
|
+
<FlowEngineProvider engine={app.flowEngine}>
|
|
887
|
+
<AppHarness />
|
|
888
|
+
</FlowEngineProvider>,
|
|
889
|
+
);
|
|
890
|
+
|
|
891
|
+
const identity = { sessionId, messageId, toolCallId: 'call-auto-origin' };
|
|
892
|
+
await waitFor(() => {
|
|
893
|
+
expect(globalRuntime.chatMessageModel.sessions[sessionId]?.messages[0]?.content.tool_calls?.[0]?.auto).toBe(true);
|
|
894
|
+
expect(requests.matchingRuntimes(identity)).toHaveLength(2);
|
|
895
|
+
});
|
|
896
|
+
await userEvent.click(screen.getByTestId('approve-origin-call'));
|
|
897
|
+
await waitFor(() => expect(releaseAutomaticPartial).toBeDefined());
|
|
898
|
+
await userEvent.click(screen.getByTestId('approve-winner-call'));
|
|
899
|
+
await waitFor(() => expect(releaseManualWinner).toBeDefined());
|
|
900
|
+
expect(decisionRequests).toHaveLength(2);
|
|
901
|
+
expect(decisionRequests[0].agentAutoApproval).toBe(true);
|
|
902
|
+
expect('agentAutoApproval' in decisionRequests[1]).toBe(false);
|
|
903
|
+
expect(automaticPayload?.agentAutoApprovalCheckpoint).toMatchObject({ canFinalize: false, validSnapshot: true });
|
|
904
|
+
|
|
905
|
+
await userEvent.click(screen.getByTestId('unmount-embedded-runtime'));
|
|
906
|
+
await waitFor(() => expect(requests.matchingRuntimes(identity)).toEqual([globalRuntime]));
|
|
907
|
+
await act(async () => {
|
|
908
|
+
releaseManualWinner?.();
|
|
909
|
+
});
|
|
910
|
+
|
|
911
|
+
await waitFor(() => expect(resumeToolCallMock).toHaveBeenCalledTimes(1));
|
|
912
|
+
expect(invokeA).toHaveBeenCalledTimes(1);
|
|
913
|
+
expect(invokeB).toHaveBeenCalledTimes(1);
|
|
914
|
+
expect(resumeToolCallMock).toHaveBeenCalledWith(
|
|
915
|
+
expect.objectContaining({ sessionId, messageId, toolCallIds: ['call-auto-origin', 'call-manual-winner'] }),
|
|
916
|
+
);
|
|
917
|
+
|
|
918
|
+
await act(async () => {
|
|
919
|
+
releaseAutomaticPartial?.();
|
|
920
|
+
});
|
|
921
|
+
await waitFor(() => expect(automaticPayload?.updated).toBe(0));
|
|
922
|
+
expect(globalRuntime.chatToolCallModel.isAllWaiting(sessionId, messageId)).toBe(true);
|
|
923
|
+
expect(invokeA).toHaveBeenCalledTimes(1);
|
|
924
|
+
expect(invokeB).toHaveBeenCalledTimes(1);
|
|
925
|
+
expect(resumeToolCallMock).toHaveBeenCalledTimes(1);
|
|
926
|
+
});
|
|
927
|
+
|
|
928
|
+
it.each(['switches away', 'is replaced'] as const)(
|
|
929
|
+
'does not run an automatic winner after its origin runtime %s even when an embedded runtime matches',
|
|
930
|
+
async (originChange) => {
|
|
931
|
+
const sessionId = `session-origin-${originChange.replace(/ /g, '-')}`;
|
|
932
|
+
const messageId = `message-origin-${originChange.replace(/ /g, '-')}`;
|
|
933
|
+
const interruptId = `interrupt-${sessionId}`;
|
|
934
|
+
const calls: ToolCall[] = [
|
|
935
|
+
{
|
|
936
|
+
id: 'call-origin-auto',
|
|
937
|
+
type: 'function',
|
|
938
|
+
name: 'tool-a',
|
|
939
|
+
args: { value: 'A' },
|
|
940
|
+
invokeStatus: 'interrupted',
|
|
941
|
+
auto: false,
|
|
942
|
+
willInterrupt: true,
|
|
943
|
+
},
|
|
944
|
+
{
|
|
945
|
+
id: 'call-already-waiting',
|
|
946
|
+
type: 'function',
|
|
947
|
+
name: 'tool-b',
|
|
948
|
+
args: { value: 'B' },
|
|
949
|
+
invokeStatus: 'waiting',
|
|
950
|
+
auto: false,
|
|
951
|
+
willInterrupt: true,
|
|
952
|
+
userDecision: { type: 'approve' },
|
|
953
|
+
},
|
|
954
|
+
];
|
|
955
|
+
const embeddedRuntime = createChatBoxRuntime({ mode: 'block' });
|
|
956
|
+
additionalRuntimes.push(embeddedRuntime);
|
|
957
|
+
setRuntimeCalls(globalRuntime, sessionId, messageId, calls);
|
|
958
|
+
setRuntimeCalls(embeddedRuntime, sessionId, messageId, calls);
|
|
959
|
+
const requests = new AutoApprovalRequests();
|
|
960
|
+
const serverCalls = calls.map((call) => ({ ...call }));
|
|
961
|
+
let releaseWinner: (() => void) | undefined;
|
|
962
|
+
let autoDecisionRequest: RequestBody | undefined;
|
|
963
|
+
let originDecisionCompleted = false;
|
|
964
|
+
const invokeA = vi.fn(async () => ({ status: 'success', content: 'A result' }));
|
|
965
|
+
const invokeB = vi.fn(async () => ({ status: 'success', content: 'B result' }));
|
|
966
|
+
app.aiManager.toolsManager.registerTools('tool-a', { invoke: invokeA });
|
|
967
|
+
app.aiManager.toolsManager.registerTools('tool-b', { invoke: invokeB });
|
|
968
|
+
app.apiMock.onPost(/agentAutoApproval:get/).reply(200, { data: { enabled: true } });
|
|
969
|
+
app.apiMock.onPost(/agentAutoApproval:eligible/).reply((config) => {
|
|
970
|
+
return [200, { data: { eligible: readRequestBody(config.data).toolCallId === 'call-origin-auto' } }];
|
|
971
|
+
});
|
|
972
|
+
app.apiMock.onPost(/aiConversations:updateUserDecision/).reply((config) => {
|
|
973
|
+
const request = readRequestBody(config.data);
|
|
974
|
+
autoDecisionRequest = request;
|
|
975
|
+
const selected = serverCalls.find((call) => call.id === request.toolCallId);
|
|
976
|
+
if (selected?.invokeStatus === 'interrupted') {
|
|
977
|
+
selected.invokeStatus = 'waiting';
|
|
978
|
+
selected.userDecision = request.userDecision;
|
|
979
|
+
}
|
|
980
|
+
const payload: RequestBody = {
|
|
981
|
+
updated: 1,
|
|
982
|
+
toolCalls: serverCalls.map((call) => ({ ...call })),
|
|
983
|
+
agentAutoApprovalCheckpoint: {
|
|
984
|
+
interruptId,
|
|
985
|
+
decisionRecorded: true,
|
|
986
|
+
canFinalize: true,
|
|
987
|
+
validSnapshot: true,
|
|
988
|
+
},
|
|
989
|
+
};
|
|
990
|
+
return new Promise((resolve) => {
|
|
991
|
+
releaseWinner = () => resolve([200, { data: payload }]);
|
|
992
|
+
});
|
|
993
|
+
});
|
|
994
|
+
|
|
995
|
+
const AppHarness = () => {
|
|
996
|
+
const [showOriginBridge, setShowOriginBridge] = React.useState(true);
|
|
997
|
+
return (
|
|
998
|
+
<InterceptorScope requests={requests}>
|
|
999
|
+
{showOriginBridge && <AutoApprovalBridge runtime={globalRuntime} enabled />}
|
|
1000
|
+
<AutoApprovalBridge runtime={embeddedRuntime} enabled />
|
|
1001
|
+
<button type="button" data-testid="replace-origin-bridge" onClick={() => setShowOriginBridge(false)}>
|
|
1002
|
+
Replace origin runtime
|
|
1003
|
+
</button>
|
|
1004
|
+
<button
|
|
1005
|
+
type="button"
|
|
1006
|
+
data-testid="switch-origin-session"
|
|
1007
|
+
onClick={() => globalRuntime.chatConversationModel.setCurrentConversation('another-session')}
|
|
1008
|
+
>
|
|
1009
|
+
Switch origin session
|
|
1010
|
+
</button>
|
|
1011
|
+
<ChatBoxRuntimeProvider runtime={globalRuntime}>
|
|
1012
|
+
<DecisionControls
|
|
1013
|
+
runtime={globalRuntime}
|
|
1014
|
+
sessionId={sessionId}
|
|
1015
|
+
messageId={messageId}
|
|
1016
|
+
actions={[
|
|
1017
|
+
{
|
|
1018
|
+
toolCallId: 'call-origin-auto',
|
|
1019
|
+
testId: 'approve-auto-origin',
|
|
1020
|
+
label: 'Approve A',
|
|
1021
|
+
onComplete: () => {
|
|
1022
|
+
originDecisionCompleted = true;
|
|
1023
|
+
},
|
|
1024
|
+
},
|
|
1025
|
+
]}
|
|
1026
|
+
/>
|
|
1027
|
+
</ChatBoxRuntimeProvider>
|
|
1028
|
+
</InterceptorScope>
|
|
1029
|
+
);
|
|
1030
|
+
};
|
|
1031
|
+
|
|
1032
|
+
resumeToolCallMock.mockClear();
|
|
1033
|
+
render(
|
|
1034
|
+
<FlowEngineProvider engine={app.flowEngine}>
|
|
1035
|
+
<AppHarness />
|
|
1036
|
+
</FlowEngineProvider>,
|
|
1037
|
+
);
|
|
1038
|
+
|
|
1039
|
+
const identity = { sessionId, messageId, toolCallId: 'call-origin-auto' };
|
|
1040
|
+
await waitFor(() => {
|
|
1041
|
+
expect(globalRuntime.chatMessageModel.sessions[sessionId]?.messages[0]?.content.tool_calls?.[0]?.auto).toBe(
|
|
1042
|
+
true,
|
|
1043
|
+
);
|
|
1044
|
+
expect(requests.matchingRuntimes(identity)).toHaveLength(2);
|
|
1045
|
+
});
|
|
1046
|
+
await userEvent.click(screen.getByTestId('approve-auto-origin'));
|
|
1047
|
+
await waitFor(() => expect(releaseWinner).toBeDefined());
|
|
1048
|
+
|
|
1049
|
+
if (originChange === 'switches away') {
|
|
1050
|
+
await userEvent.click(screen.getByTestId('switch-origin-session'));
|
|
1051
|
+
} else {
|
|
1052
|
+
await userEvent.click(screen.getByTestId('replace-origin-bridge'));
|
|
1053
|
+
}
|
|
1054
|
+
await waitFor(() => expect(requests.matchingRuntimes(identity)).toEqual([embeddedRuntime]));
|
|
1055
|
+
await act(async () => {
|
|
1056
|
+
releaseWinner?.();
|
|
1057
|
+
});
|
|
1058
|
+
|
|
1059
|
+
await waitFor(() => expect(originDecisionCompleted).toBe(true));
|
|
1060
|
+
await waitFor(() => expect(embeddedRuntime.chatToolCallModel.isAllWaiting(sessionId, messageId)).toBe(true));
|
|
1061
|
+
expect(autoDecisionRequest?.agentAutoApproval).toBe(true);
|
|
1062
|
+
expect(invokeA).not.toHaveBeenCalled();
|
|
1063
|
+
expect(invokeB).not.toHaveBeenCalled();
|
|
1064
|
+
expect(resumeToolCallMock).not.toHaveBeenCalled();
|
|
1065
|
+
},
|
|
1066
|
+
);
|
|
1067
|
+
|
|
1068
|
+
it('advances a tracked unmanaged partial and lets a manual sibling decision finish the turn', async () => {
|
|
1069
|
+
const sessionId = 'session-unmanaged-partial';
|
|
1070
|
+
const messageId = 'message-unmanaged-partial';
|
|
1071
|
+
const calls: ToolCall[] = [
|
|
1072
|
+
{
|
|
1073
|
+
id: 'call-partial-a',
|
|
1074
|
+
type: 'function',
|
|
1075
|
+
name: 'tool-a',
|
|
1076
|
+
args: { value: 'A' },
|
|
1077
|
+
invokeStatus: 'interrupted',
|
|
1078
|
+
auto: false,
|
|
1079
|
+
willInterrupt: true,
|
|
1080
|
+
},
|
|
1081
|
+
{
|
|
1082
|
+
id: 'call-manual-b',
|
|
1083
|
+
type: 'function',
|
|
1084
|
+
name: 'tool-b',
|
|
1085
|
+
args: { value: 'B' },
|
|
1086
|
+
invokeStatus: 'interrupted',
|
|
1087
|
+
auto: false,
|
|
1088
|
+
willInterrupt: true,
|
|
1089
|
+
},
|
|
1090
|
+
];
|
|
1091
|
+
setRuntimeCalls(globalRuntime, sessionId, messageId, calls);
|
|
1092
|
+
const requests = new AutoApprovalRequests();
|
|
1093
|
+
const serverCalls = calls.map((call) => ({ ...call }));
|
|
1094
|
+
const decisionRequests: RequestBody[] = [];
|
|
1095
|
+
const invokeA = vi.fn(async () => ({ status: 'success', content: 'A result' }));
|
|
1096
|
+
const invokeB = vi.fn(async () => ({ status: 'success', content: 'B result' }));
|
|
1097
|
+
app.aiManager.toolsManager.registerTools('tool-a', { invoke: invokeA });
|
|
1098
|
+
app.aiManager.toolsManager.registerTools('tool-b', { invoke: invokeB });
|
|
1099
|
+
app.apiMock.onPost(/aiConversations:updateUserDecision/).reply((config) => {
|
|
1100
|
+
const request = readRequestBody(config.data);
|
|
1101
|
+
decisionRequests.push(request);
|
|
1102
|
+
const selected = serverCalls.find((call) => call.id === request.toolCallId);
|
|
1103
|
+
if (selected?.invokeStatus === 'interrupted') {
|
|
1104
|
+
selected.invokeStatus = 'waiting';
|
|
1105
|
+
selected.userDecision = request.userDecision;
|
|
1106
|
+
}
|
|
1107
|
+
return [
|
|
1108
|
+
200,
|
|
1109
|
+
{
|
|
1110
|
+
data: {
|
|
1111
|
+
updated: 1,
|
|
1112
|
+
toolCalls: serverCalls.map((call) => ({ ...call })),
|
|
1113
|
+
},
|
|
1114
|
+
},
|
|
1115
|
+
];
|
|
1116
|
+
});
|
|
1117
|
+
|
|
1118
|
+
render(
|
|
1119
|
+
<FlowEngineProvider engine={app.flowEngine}>
|
|
1120
|
+
<InterceptorScope requests={requests}>
|
|
1121
|
+
<AutoApprovalBridge runtime={globalRuntime} enabled={false} />
|
|
1122
|
+
<ChatBoxRuntimeProvider runtime={globalRuntime}>
|
|
1123
|
+
<DecisionControls
|
|
1124
|
+
runtime={globalRuntime}
|
|
1125
|
+
sessionId={sessionId}
|
|
1126
|
+
messageId={messageId}
|
|
1127
|
+
actions={[
|
|
1128
|
+
{ toolCallId: 'call-partial-a', testId: 'manual-partial-a', label: 'Approve A' },
|
|
1129
|
+
{ toolCallId: 'call-manual-b', testId: 'manual-partial-b', label: 'Approve B' },
|
|
1130
|
+
]}
|
|
1131
|
+
/>
|
|
1132
|
+
</ChatBoxRuntimeProvider>
|
|
1133
|
+
</InterceptorScope>
|
|
1134
|
+
</FlowEngineProvider>,
|
|
1135
|
+
);
|
|
1136
|
+
|
|
1137
|
+
const identity = { sessionId, messageId, toolCallId: 'call-partial-a' };
|
|
1138
|
+
await waitFor(() => expect(requests.matchingRuntimes(identity)).toEqual([globalRuntime]));
|
|
1139
|
+
await userEvent.click(screen.getByTestId('manual-partial-a'));
|
|
1140
|
+
await waitFor(() => {
|
|
1141
|
+
expect(globalRuntime.chatToolCallModel.getInvokeStatus(sessionId, messageId, 'call-partial-a')).toBe('waiting');
|
|
1142
|
+
expect(globalRuntime.chatToolCallModel.getInvokeStatus(sessionId, messageId, 'call-manual-b')).toBe(
|
|
1143
|
+
'interrupted',
|
|
1144
|
+
);
|
|
1145
|
+
});
|
|
1146
|
+
expect(decisionRequests).toHaveLength(1);
|
|
1147
|
+
expect('agentAutoApproval' in decisionRequests[0]).toBe(false);
|
|
1148
|
+
expect(invokeA).not.toHaveBeenCalled();
|
|
1149
|
+
expect(invokeB).not.toHaveBeenCalled();
|
|
1150
|
+
expect(resumeToolCallMock).not.toHaveBeenCalled();
|
|
1151
|
+
|
|
1152
|
+
await userEvent.click(screen.getByTestId('manual-partial-b'));
|
|
1153
|
+
await waitFor(() => expect(resumeToolCallMock).toHaveBeenCalledTimes(1));
|
|
1154
|
+
expect(decisionRequests).toHaveLength(2);
|
|
1155
|
+
expect(decisionRequests.every((request) => !('agentAutoApproval' in request))).toBe(true);
|
|
1156
|
+
expect(globalRuntime.chatToolCallModel.isAllWaiting(sessionId, messageId)).toBe(true);
|
|
1157
|
+
expect(invokeA).toHaveBeenCalledTimes(1);
|
|
1158
|
+
expect(invokeB).toHaveBeenCalledTimes(1);
|
|
1159
|
+
});
|
|
1160
|
+
|
|
1161
|
+
it('preserves an unmanaged manual sub-agent partial response when no root runtime is registered', async () => {
|
|
1162
|
+
const sessionId = 'session-untracked-sub-agent';
|
|
1163
|
+
const messageId = 'message-untracked-sub-agent';
|
|
1164
|
+
const subAgentRuntime = createChatBoxRuntime({ mode: 'block' });
|
|
1165
|
+
additionalRuntimes.push(subAgentRuntime);
|
|
1166
|
+
const calls: ToolCall[] = [
|
|
1167
|
+
{
|
|
1168
|
+
id: 'call-sub-agent-a',
|
|
1169
|
+
type: 'function',
|
|
1170
|
+
name: 'tool-a',
|
|
1171
|
+
args: { value: 'A' },
|
|
1172
|
+
invokeStatus: 'interrupted',
|
|
1173
|
+
auto: false,
|
|
1174
|
+
willInterrupt: true,
|
|
1175
|
+
},
|
|
1176
|
+
{
|
|
1177
|
+
id: 'call-sub-agent-b',
|
|
1178
|
+
type: 'function',
|
|
1179
|
+
name: 'tool-b',
|
|
1180
|
+
args: { value: 'B' },
|
|
1181
|
+
invokeStatus: 'interrupted',
|
|
1182
|
+
auto: false,
|
|
1183
|
+
willInterrupt: true,
|
|
1184
|
+
},
|
|
1185
|
+
];
|
|
1186
|
+
setRuntimeCalls(subAgentRuntime, sessionId, messageId, calls, 'sub-agent');
|
|
1187
|
+
const requests = new AutoApprovalRequests();
|
|
1188
|
+
const decisionRequests: RequestBody[] = [];
|
|
1189
|
+
app.apiMock.onPost(/aiConversations:updateUserDecision/).reply((config) => {
|
|
1190
|
+
decisionRequests.push(readRequestBody(config.data));
|
|
1191
|
+
return [
|
|
1192
|
+
200,
|
|
1193
|
+
{
|
|
1194
|
+
data: {
|
|
1195
|
+
updated: 1,
|
|
1196
|
+
toolCalls: [
|
|
1197
|
+
{ ...calls[0], invokeStatus: 'waiting', userDecision: { type: 'approve' } },
|
|
1198
|
+
{ ...calls[1], invokeStatus: 'interrupted' },
|
|
1199
|
+
],
|
|
1200
|
+
},
|
|
1201
|
+
},
|
|
1202
|
+
];
|
|
1203
|
+
});
|
|
1204
|
+
|
|
1205
|
+
render(
|
|
1206
|
+
<FlowEngineProvider engine={app.flowEngine}>
|
|
1207
|
+
<InterceptorScope requests={requests}>
|
|
1208
|
+
<ChatBoxRuntimeProvider runtime={subAgentRuntime}>
|
|
1209
|
+
<DecisionControls
|
|
1210
|
+
runtime={subAgentRuntime}
|
|
1211
|
+
sessionId={sessionId}
|
|
1212
|
+
messageId={messageId}
|
|
1213
|
+
actions={[{ toolCallId: 'call-sub-agent-a', testId: 'approve-sub-agent-a', label: 'Approve A' }]}
|
|
1214
|
+
/>
|
|
1215
|
+
</ChatBoxRuntimeProvider>
|
|
1216
|
+
</InterceptorScope>
|
|
1217
|
+
</FlowEngineProvider>,
|
|
1218
|
+
);
|
|
1219
|
+
|
|
1220
|
+
const identity = { sessionId, messageId, toolCallId: 'call-sub-agent-a' };
|
|
1221
|
+
expect(requests.matchingRuntimes(identity)).toHaveLength(0);
|
|
1222
|
+
await userEvent.click(screen.getByTestId('approve-sub-agent-a'));
|
|
1223
|
+
await waitFor(() => {
|
|
1224
|
+
expect(subAgentRuntime.chatToolCallModel.getInvokeStatus(sessionId, messageId, 'call-sub-agent-a')).toBe(
|
|
1225
|
+
'waiting',
|
|
1226
|
+
);
|
|
1227
|
+
expect(subAgentRuntime.chatToolCallModel.getInvokeStatus(sessionId, messageId, 'call-sub-agent-b')).toBe(
|
|
1228
|
+
'interrupted',
|
|
1229
|
+
);
|
|
1230
|
+
});
|
|
1231
|
+
expect(decisionRequests).toHaveLength(1);
|
|
1232
|
+
expect('agentAutoApproval' in decisionRequests[0]).toBe(false);
|
|
1233
|
+
expect(requests.matchingRuntimes(identity)).toHaveLength(0);
|
|
1234
|
+
expect(resumeToolCallMock).not.toHaveBeenCalled();
|
|
1235
|
+
});
|
|
1236
|
+
|
|
1237
|
+
it('suppresses a pre-enrollment partial after another tab wins and the sender runtime hydrates all calls', async () => {
|
|
1238
|
+
const appTwo = ClientV2.createMockClient();
|
|
1239
|
+
additionalApps.push(appTwo);
|
|
1240
|
+
const sessionId = 'session-pre-enrollment-partial';
|
|
1241
|
+
const messageId = 'message-pre-enrollment-partial';
|
|
1242
|
+
const interruptId = `interrupt-${sessionId}`;
|
|
1243
|
+
const calls: ToolCall[] = [
|
|
1244
|
+
{
|
|
1245
|
+
id: 'call-stale-a',
|
|
1246
|
+
type: 'function',
|
|
1247
|
+
name: 'tool-a',
|
|
1248
|
+
args: { value: 'A' },
|
|
1249
|
+
invokeStatus: 'interrupted',
|
|
1250
|
+
auto: false,
|
|
1251
|
+
willInterrupt: true,
|
|
1252
|
+
},
|
|
1253
|
+
{
|
|
1254
|
+
id: 'call-auto-b',
|
|
1255
|
+
type: 'function',
|
|
1256
|
+
name: 'tool-b',
|
|
1257
|
+
args: { value: 'B' },
|
|
1258
|
+
invokeStatus: 'interrupted',
|
|
1259
|
+
auto: false,
|
|
1260
|
+
willInterrupt: true,
|
|
1261
|
+
},
|
|
1262
|
+
];
|
|
1263
|
+
const winnerRuntime = createChatBoxRuntime({ mode: 'block' });
|
|
1264
|
+
additionalRuntimes.push(winnerRuntime);
|
|
1265
|
+
setRuntimeCalls(globalRuntime, sessionId, messageId, calls);
|
|
1266
|
+
const senderRequests = new AutoApprovalRequests();
|
|
1267
|
+
const winnerRequests = new AutoApprovalRequests();
|
|
1268
|
+
const serverCalls = calls.map((call) => ({ ...call }));
|
|
1269
|
+
const senderDecisionRequests: RequestBody[] = [];
|
|
1270
|
+
const winnerDecisionRequests: RequestBody[] = [];
|
|
1271
|
+
let releaseStalePartial: (() => void) | undefined;
|
|
1272
|
+
let stalePayload: RequestBody | undefined;
|
|
1273
|
+
let staleActionCompleted = false;
|
|
1274
|
+
let checkpointEnrolled = false;
|
|
1275
|
+
let checkpointIssued = false;
|
|
1276
|
+
const senderInvokeA = vi.fn(async () => ({ status: 'success', content: 'sender A' }));
|
|
1277
|
+
const senderInvokeB = vi.fn(async () => ({ status: 'success', content: 'sender B' }));
|
|
1278
|
+
const winnerInvokeA = vi.fn(async () => ({ status: 'success', content: 'winner A' }));
|
|
1279
|
+
const winnerInvokeB = vi.fn(async () => ({ status: 'success', content: 'winner B' }));
|
|
1280
|
+
app.aiManager.toolsManager.registerTools('tool-a', { invoke: senderInvokeA });
|
|
1281
|
+
app.aiManager.toolsManager.registerTools('tool-b', { invoke: senderInvokeB });
|
|
1282
|
+
appTwo.aiManager.toolsManager.registerTools('tool-a', { invoke: winnerInvokeA });
|
|
1283
|
+
appTwo.aiManager.toolsManager.registerTools('tool-b', { invoke: winnerInvokeB });
|
|
1284
|
+
|
|
1285
|
+
app.apiMock.onPost(/aiConversations:updateUserDecision/).reply((config) => {
|
|
1286
|
+
const request = readRequestBody(config.data);
|
|
1287
|
+
senderDecisionRequests.push(request);
|
|
1288
|
+
const selected = serverCalls.find((call) => call.id === request.toolCallId);
|
|
1289
|
+
let nativeUpdated = 0;
|
|
1290
|
+
if (selected?.invokeStatus === 'interrupted') {
|
|
1291
|
+
selected.invokeStatus = 'waiting';
|
|
1292
|
+
selected.userDecision = request.userDecision;
|
|
1293
|
+
nativeUpdated = 1;
|
|
1294
|
+
}
|
|
1295
|
+
const payload: RequestBody = {
|
|
1296
|
+
updated: nativeUpdated,
|
|
1297
|
+
toolCalls: serverCalls.map((call) => ({ ...call })),
|
|
1298
|
+
};
|
|
1299
|
+
if (request.toolCallId === 'call-stale-a') {
|
|
1300
|
+
stalePayload = payload;
|
|
1301
|
+
return new Promise((resolve) => {
|
|
1302
|
+
releaseStalePartial = () => resolve([200, { data: payload }]);
|
|
1303
|
+
});
|
|
1304
|
+
}
|
|
1305
|
+
return [200, { data: payload }];
|
|
1306
|
+
});
|
|
1307
|
+
appTwo.apiMock.onPost(/agentAutoApproval:get/).reply(200, { data: { enabled: true } });
|
|
1308
|
+
appTwo.apiMock.onPost(/agentAutoApproval:eligible/).reply((config) => {
|
|
1309
|
+
const request = readRequestBody(config.data);
|
|
1310
|
+
return [200, { data: { eligible: request.toolCallId === 'call-auto-b' } }];
|
|
1311
|
+
});
|
|
1312
|
+
appTwo.apiMock.onPost(/aiConversations:updateUserDecision/).reply((config) => {
|
|
1313
|
+
const request = readRequestBody(config.data);
|
|
1314
|
+
winnerDecisionRequests.push(request);
|
|
1315
|
+
const selected = serverCalls.find((call) => call.id === request.toolCallId);
|
|
1316
|
+
let nativeUpdated = 0;
|
|
1317
|
+
if (selected?.invokeStatus === 'interrupted') {
|
|
1318
|
+
selected.invokeStatus = 'waiting';
|
|
1319
|
+
selected.userDecision = request.userDecision;
|
|
1320
|
+
nativeUpdated = 1;
|
|
1321
|
+
}
|
|
1322
|
+
if (request.agentAutoApproval === true) checkpointEnrolled = true;
|
|
1323
|
+
const ready =
|
|
1324
|
+
checkpointEnrolled && serverCalls.every((call) => !call.willInterrupt || call.invokeStatus === 'waiting');
|
|
1325
|
+
const canFinalize = nativeUpdated === 1 && ready && !checkpointIssued;
|
|
1326
|
+
if (canFinalize) checkpointIssued = true;
|
|
1327
|
+
return [
|
|
1328
|
+
200,
|
|
1329
|
+
{
|
|
1330
|
+
data: {
|
|
1331
|
+
updated: checkpointEnrolled ? (canFinalize ? 1 : 0) : nativeUpdated,
|
|
1332
|
+
toolCalls: serverCalls.map((call) => ({ ...call })),
|
|
1333
|
+
...(checkpointEnrolled
|
|
1334
|
+
? {
|
|
1335
|
+
agentAutoApprovalCheckpoint: {
|
|
1336
|
+
interruptId,
|
|
1337
|
+
decisionRecorded: nativeUpdated === 1,
|
|
1338
|
+
canFinalize,
|
|
1339
|
+
validSnapshot: true,
|
|
1340
|
+
},
|
|
1341
|
+
}
|
|
1342
|
+
: {}),
|
|
1343
|
+
},
|
|
1344
|
+
},
|
|
1345
|
+
];
|
|
1346
|
+
});
|
|
1347
|
+
vi.spyOn(ClientV2, 'useApp').mockImplementation(
|
|
1348
|
+
() => useFlowEngineContext().app as ReturnType<typeof ClientV2.createMockClient>,
|
|
1349
|
+
);
|
|
1350
|
+
|
|
1351
|
+
render(
|
|
1352
|
+
<FlowEngineProvider engine={app.flowEngine}>
|
|
1353
|
+
<InterceptorScope requests={senderRequests}>
|
|
1354
|
+
<AutoApprovalBridge runtime={globalRuntime} enabled={false} />
|
|
1355
|
+
<ChatBoxRuntimeProvider runtime={globalRuntime}>
|
|
1356
|
+
<DecisionControls
|
|
1357
|
+
runtime={globalRuntime}
|
|
1358
|
+
sessionId={sessionId}
|
|
1359
|
+
messageId={messageId}
|
|
1360
|
+
actions={[
|
|
1361
|
+
{
|
|
1362
|
+
toolCallId: 'call-stale-a',
|
|
1363
|
+
testId: 'send-stale-a',
|
|
1364
|
+
label: 'Approve A',
|
|
1365
|
+
onComplete: () => {
|
|
1366
|
+
staleActionCompleted = true;
|
|
1367
|
+
},
|
|
1368
|
+
},
|
|
1369
|
+
]}
|
|
1370
|
+
/>
|
|
1371
|
+
</ChatBoxRuntimeProvider>
|
|
1372
|
+
</InterceptorScope>
|
|
1373
|
+
</FlowEngineProvider>,
|
|
1374
|
+
);
|
|
1375
|
+
|
|
1376
|
+
const staleIdentity = { sessionId, messageId, toolCallId: 'call-stale-a' };
|
|
1377
|
+
await waitFor(() => expect(senderRequests.matchingRuntimes(staleIdentity)).toEqual([globalRuntime]));
|
|
1378
|
+
await userEvent.click(screen.getByTestId('send-stale-a'));
|
|
1379
|
+
await waitFor(() => expect(releaseStalePartial).toBeDefined());
|
|
1380
|
+
expect(senderDecisionRequests).toHaveLength(1);
|
|
1381
|
+
expect('agentAutoApproval' in senderDecisionRequests[0]).toBe(false);
|
|
1382
|
+
expect(checkpointEnrolled).toBe(false);
|
|
1383
|
+
expect(stalePayload?.agentAutoApprovalCheckpoint).toBeUndefined();
|
|
1384
|
+
expect(stalePayload?.toolCalls).toMatchObject([
|
|
1385
|
+
{ id: 'call-stale-a', invokeStatus: 'waiting', willInterrupt: true },
|
|
1386
|
+
{ id: 'call-auto-b', invokeStatus: 'interrupted', willInterrupt: true },
|
|
1387
|
+
]);
|
|
1388
|
+
|
|
1389
|
+
const [staleCall, automaticCall] = calls;
|
|
1390
|
+
if (!staleCall || !automaticCall) throw new Error('The two-call stale response fixture is incomplete.');
|
|
1391
|
+
const hydratedCalls: ToolCall[] = [
|
|
1392
|
+
{ ...staleCall, invokeStatus: 'waiting', userDecision: { type: 'approve' } },
|
|
1393
|
+
{ ...automaticCall, invokeStatus: 'interrupted' },
|
|
1394
|
+
];
|
|
1395
|
+
setRuntimeCalls(winnerRuntime, sessionId, messageId, hydratedCalls);
|
|
1396
|
+
render(
|
|
1397
|
+
<FlowEngineProvider engine={appTwo.flowEngine}>
|
|
1398
|
+
<InterceptorScope requests={winnerRequests}>
|
|
1399
|
+
<AutoApprovalBridge runtime={winnerRuntime} enabled />
|
|
1400
|
+
<ChatBoxRuntimeProvider runtime={winnerRuntime}>
|
|
1401
|
+
<DecisionControls
|
|
1402
|
+
runtime={winnerRuntime}
|
|
1403
|
+
sessionId={sessionId}
|
|
1404
|
+
messageId={messageId}
|
|
1405
|
+
actions={[{ toolCallId: 'call-auto-b', testId: 'approve-winner-b', label: 'Approve B' }]}
|
|
1406
|
+
/>
|
|
1407
|
+
</ChatBoxRuntimeProvider>
|
|
1408
|
+
</InterceptorScope>
|
|
1409
|
+
</FlowEngineProvider>,
|
|
1410
|
+
);
|
|
1411
|
+
|
|
1412
|
+
await waitFor(() => {
|
|
1413
|
+
expect(winnerRuntime.chatMessageModel.sessions[sessionId]?.messages[0]?.content.tool_calls?.[1]?.auto).toBe(true);
|
|
1414
|
+
});
|
|
1415
|
+
await userEvent.click(screen.getByTestId('approve-winner-b'));
|
|
1416
|
+
await waitFor(() => expect(resumeToolCallMock).toHaveBeenCalledTimes(1));
|
|
1417
|
+
expect(checkpointEnrolled).toBe(true);
|
|
1418
|
+
expect(checkpointIssued).toBe(true);
|
|
1419
|
+
expect(winnerDecisionRequests).toHaveLength(1);
|
|
1420
|
+
expect(winnerDecisionRequests[0].agentAutoApproval).toBe(true);
|
|
1421
|
+
expect(winnerRuntime.chatToolCallModel.isAllWaiting(sessionId, messageId)).toBe(true);
|
|
1422
|
+
expect(winnerInvokeA).toHaveBeenCalledTimes(1);
|
|
1423
|
+
expect(winnerInvokeB).toHaveBeenCalledTimes(1);
|
|
1424
|
+
|
|
1425
|
+
const reloadedCalls: ToolCall[] = calls.map((call) => ({
|
|
1426
|
+
...call,
|
|
1427
|
+
invokeStatus: 'waiting',
|
|
1428
|
+
userDecision: { type: 'approve' },
|
|
1429
|
+
}));
|
|
1430
|
+
hydrateRuntimeCalls(globalRuntime, sessionId, messageId, reloadedCalls);
|
|
1431
|
+
expect(globalRuntime.chatToolCallModel.isAllWaiting(sessionId, messageId)).toBe(true);
|
|
1432
|
+
expect(
|
|
1433
|
+
senderRequests.hasCheckpoint(staleIdentity, [
|
|
1434
|
+
{ id: 'call-stale-a', willInterrupt: true, invokeStatus: 'waiting' },
|
|
1435
|
+
{ id: 'call-auto-b', willInterrupt: true, invokeStatus: 'waiting' },
|
|
1436
|
+
]),
|
|
1437
|
+
).toBe(false);
|
|
1438
|
+
|
|
1439
|
+
await act(async () => {
|
|
1440
|
+
releaseStalePartial?.();
|
|
1441
|
+
});
|
|
1442
|
+
await waitFor(() => expect(staleActionCompleted).toBe(true));
|
|
1443
|
+
expect(globalRuntime.chatToolCallModel.isAllWaiting(sessionId, messageId)).toBe(true);
|
|
1444
|
+
expect(senderInvokeA).not.toHaveBeenCalled();
|
|
1445
|
+
expect(senderInvokeB).not.toHaveBeenCalled();
|
|
1446
|
+
expect(winnerInvokeA).toHaveBeenCalledTimes(1);
|
|
1447
|
+
expect(winnerInvokeB).toHaveBeenCalledTimes(1);
|
|
1448
|
+
expect(resumeToolCallMock).toHaveBeenCalledTimes(1);
|
|
1449
|
+
});
|
|
1450
|
+
|
|
1451
|
+
it('finalizes a mixed auto and manual checkpoint once across independent app runtimes', async () => {
|
|
1452
|
+
const appOne = ClientV2.createMockClient();
|
|
1453
|
+
const appTwo = ClientV2.createMockClient();
|
|
1454
|
+
additionalApps.push(appTwo);
|
|
1455
|
+
const sessionId = 'session-two-tabs';
|
|
1456
|
+
const messageId = 'message-two-tabs';
|
|
1457
|
+
const interruptId = 'interrupt-two-tabs';
|
|
1458
|
+
const calls: ToolCall[] = [
|
|
1459
|
+
{
|
|
1460
|
+
id: 'call-auto',
|
|
1461
|
+
type: 'function',
|
|
1462
|
+
name: 'tool-a',
|
|
1463
|
+
args: { value: 'A' },
|
|
1464
|
+
invokeStatus: 'interrupted',
|
|
1465
|
+
auto: false,
|
|
1466
|
+
willInterrupt: true,
|
|
1467
|
+
},
|
|
1468
|
+
{
|
|
1469
|
+
id: 'call-manual',
|
|
1470
|
+
type: 'function',
|
|
1471
|
+
name: 'tool-b',
|
|
1472
|
+
args: { value: 'B' },
|
|
1473
|
+
invokeStatus: 'interrupted',
|
|
1474
|
+
auto: false,
|
|
1475
|
+
willInterrupt: true,
|
|
1476
|
+
},
|
|
1477
|
+
{
|
|
1478
|
+
id: 'call-backend-auto',
|
|
1479
|
+
type: 'function',
|
|
1480
|
+
name: 'backend-tool',
|
|
1481
|
+
args: { value: 'already done' },
|
|
1482
|
+
invokeStatus: 'done',
|
|
1483
|
+
status: 'success',
|
|
1484
|
+
auto: true,
|
|
1485
|
+
willInterrupt: false,
|
|
1486
|
+
},
|
|
1487
|
+
];
|
|
1488
|
+
const createRuntime = () => {
|
|
1489
|
+
const runtime = createChatBoxRuntime({ mode: 'global' });
|
|
1490
|
+
runtime.chatConversationModel.setCurrentConversation(sessionId);
|
|
1491
|
+
runtime.chatBoxModel.setCurrentEmployee({ username: 'native-agent' });
|
|
1492
|
+
runtime.chatMessageModel.setSessionMessages(sessionId, [
|
|
1493
|
+
{
|
|
1494
|
+
role: 'assistant',
|
|
1495
|
+
content: {
|
|
1496
|
+
content: '',
|
|
1497
|
+
messageId,
|
|
1498
|
+
metadata: { interruptId },
|
|
1499
|
+
tool_calls: calls.map((call) => ({ ...call })),
|
|
1500
|
+
},
|
|
1501
|
+
},
|
|
1502
|
+
]);
|
|
1503
|
+
for (const call of calls.filter((item) => item.willInterrupt)) {
|
|
1504
|
+
runtime.chatToolCallModel.updateToolCallInvokeStatus(sessionId, messageId, call.id, 'interrupted');
|
|
1505
|
+
}
|
|
1506
|
+
return runtime;
|
|
1507
|
+
};
|
|
1508
|
+
const runtimeOne = createRuntime();
|
|
1509
|
+
const runtimeTwo = createRuntime();
|
|
1510
|
+
|
|
1511
|
+
const invokeA = vi.fn(async () => ({ status: 'success', content: 'A result' }));
|
|
1512
|
+
const invokeB = vi.fn(async () => ({ status: 'success', content: 'B result' }));
|
|
1513
|
+
for (const client of [appOne, appTwo]) {
|
|
1514
|
+
client.aiManager.toolsManager.registerTools('tool-a', { invoke: invokeA });
|
|
1515
|
+
client.aiManager.toolsManager.registerTools('tool-b', { invoke: invokeB });
|
|
1516
|
+
}
|
|
1517
|
+
|
|
1518
|
+
let enabled = true;
|
|
1519
|
+
let checkpointEnrolled = false;
|
|
1520
|
+
let checkpointIssued = false;
|
|
1521
|
+
const serverCalls = calls.map((call) => ({ ...call }));
|
|
1522
|
+
const decisionRequests: Array<{ app: string; request: RequestBody }> = [];
|
|
1523
|
+
let releaseFirstResponse: (() => void) | undefined;
|
|
1524
|
+
|
|
1525
|
+
const makeApi = (client: typeof appOne, appName: string, eligibleCallId?: string) => {
|
|
1526
|
+
client.apiMock.onPost(/agentAutoApproval:eligible/).reply((config) => {
|
|
1527
|
+
const request = readRequestBody(config.data);
|
|
1528
|
+
return [200, { data: { eligible: request.toolCallId === eligibleCallId } }];
|
|
1529
|
+
});
|
|
1530
|
+
client.apiMock.onPost(/aiConversations:updateUserDecision/).reply((config) => {
|
|
1531
|
+
const request = readRequestBody(config.data);
|
|
1532
|
+
decisionRequests.push({ app: appName, request });
|
|
1533
|
+
const selected = serverCalls.find((call) => call.id === request.toolCallId);
|
|
1534
|
+
let nativeUpdated = 0;
|
|
1535
|
+
if (selected?.invokeStatus === 'interrupted') {
|
|
1536
|
+
selected.invokeStatus = 'waiting';
|
|
1537
|
+
selected.userDecision = request.userDecision;
|
|
1538
|
+
nativeUpdated = 1;
|
|
1539
|
+
if (request.agentAutoApproval === true) checkpointEnrolled = true;
|
|
1540
|
+
}
|
|
1541
|
+
|
|
1542
|
+
const interrupting = serverCalls.filter((call) => call.willInterrupt === true);
|
|
1543
|
+
const ready = checkpointEnrolled && interrupting.every((call) => call.invokeStatus === 'waiting');
|
|
1544
|
+
const canFinalize = nativeUpdated === 1 && ready && !checkpointIssued;
|
|
1545
|
+
if (canFinalize) checkpointIssued = true;
|
|
1546
|
+
const payload = {
|
|
1547
|
+
updated: checkpointEnrolled ? (canFinalize ? 1 : 0) : nativeUpdated,
|
|
1548
|
+
toolCalls: serverCalls.map((call) => ({ ...call })),
|
|
1549
|
+
...(checkpointEnrolled
|
|
1550
|
+
? {
|
|
1551
|
+
agentAutoApprovalCheckpoint: {
|
|
1552
|
+
interruptId,
|
|
1553
|
+
decisionRecorded: nativeUpdated === 1,
|
|
1554
|
+
canFinalize,
|
|
1555
|
+
validSnapshot: true,
|
|
1556
|
+
},
|
|
1557
|
+
}
|
|
1558
|
+
: {}),
|
|
1559
|
+
};
|
|
1560
|
+
|
|
1561
|
+
if (appName === 'tab-one' && request.toolCallId === 'call-auto' && request.agentAutoApproval === true) {
|
|
1562
|
+
return new Promise((resolve) => {
|
|
1563
|
+
releaseFirstResponse = () => resolve([200, { data: payload }]);
|
|
1564
|
+
});
|
|
1565
|
+
}
|
|
1566
|
+
return [200, { data: payload }];
|
|
1567
|
+
});
|
|
1568
|
+
};
|
|
1569
|
+
makeApi(appOne, 'tab-one', 'call-auto');
|
|
1570
|
+
makeApi(appTwo, 'tab-two');
|
|
1571
|
+
|
|
1572
|
+
const TabHarness = ({ runtime, enabled }: { runtime: ChatBoxRuntime; enabled: boolean }) => {
|
|
1573
|
+
const requests = React.useMemo(() => new AutoApprovalRequests(), []);
|
|
1574
|
+
useAutoApprovalInterceptor(requests);
|
|
1575
|
+
const { getDecisionActions } = useToolCallActions({ messageId, runtime });
|
|
1576
|
+
const currentCalls = runtime.chatMessageModel.sessions[sessionId]?.messages[0]?.content.tool_calls ?? [];
|
|
1577
|
+
const automaticCall = currentCalls.find((call) => call.id === 'call-auto');
|
|
1578
|
+
const manualCall = currentCalls.find((call) => call.id === 'call-manual');
|
|
1579
|
+
|
|
1580
|
+
return (
|
|
1581
|
+
<AutoApprovalRequestsContext.Provider value={requests}>
|
|
1582
|
+
<AutoApprovalBridge runtime={runtime} enabled={enabled} />
|
|
1583
|
+
<ChatBoxRuntimeProvider runtime={runtime}>
|
|
1584
|
+
<button
|
|
1585
|
+
type="button"
|
|
1586
|
+
data-testid={`approve-auto-${runtime === runtimeOne ? 'one' : 'two'}`}
|
|
1587
|
+
onClick={() => (automaticCall ? getDecisionActions(automaticCall).approve() : undefined)}
|
|
1588
|
+
>
|
|
1589
|
+
Approve automatic call
|
|
1590
|
+
</button>
|
|
1591
|
+
<button
|
|
1592
|
+
type="button"
|
|
1593
|
+
data-testid={`reject-manual-${runtime === runtimeOne ? 'one' : 'two'}`}
|
|
1594
|
+
onClick={() => (manualCall ? getDecisionActions(manualCall).reject('Skip B') : undefined)}
|
|
1595
|
+
>
|
|
1596
|
+
Reject manual call
|
|
1597
|
+
</button>
|
|
1598
|
+
</ChatBoxRuntimeProvider>
|
|
1599
|
+
</AutoApprovalRequestsContext.Provider>
|
|
1600
|
+
);
|
|
1601
|
+
};
|
|
1602
|
+
|
|
1603
|
+
const TwoApps = () => {
|
|
1604
|
+
const [automaticApprovalEnabled, setAutomaticApprovalEnabled] = React.useState(true);
|
|
1605
|
+
return (
|
|
1606
|
+
<>
|
|
1607
|
+
<button
|
|
1608
|
+
type="button"
|
|
1609
|
+
data-testid="disable-session-auto-approval"
|
|
1610
|
+
onClick={() => {
|
|
1611
|
+
enabled = false;
|
|
1612
|
+
setAutomaticApprovalEnabled(false);
|
|
1613
|
+
}}
|
|
1614
|
+
>
|
|
1615
|
+
Disable session auto-approval
|
|
1616
|
+
</button>
|
|
1617
|
+
<FlowEngineProvider engine={appOne.flowEngine}>
|
|
1618
|
+
<TabHarness runtime={runtimeOne} enabled={automaticApprovalEnabled} />
|
|
1619
|
+
</FlowEngineProvider>
|
|
1620
|
+
<FlowEngineProvider engine={appTwo.flowEngine}>
|
|
1621
|
+
<TabHarness runtime={runtimeTwo} enabled={automaticApprovalEnabled} />
|
|
1622
|
+
</FlowEngineProvider>
|
|
1623
|
+
</>
|
|
1624
|
+
);
|
|
1625
|
+
};
|
|
1626
|
+
|
|
1627
|
+
vi.spyOn(ClientV2, 'useApp').mockImplementation(
|
|
1628
|
+
() => useFlowEngineContext().app as ReturnType<typeof ClientV2.createMockClient>,
|
|
1629
|
+
);
|
|
1630
|
+
resumeToolCallMock.mockClear();
|
|
1631
|
+
render(<TwoApps />);
|
|
1632
|
+
|
|
1633
|
+
await waitFor(() => {
|
|
1634
|
+
const marked = runtimeOne.chatMessageModel.sessions[sessionId]?.messages[0]?.content.tool_calls ?? [];
|
|
1635
|
+
expect(marked.find((call) => call.id === 'call-auto')?.auto).toBe(true);
|
|
1636
|
+
});
|
|
1637
|
+
await userEvent.click(screen.getByTestId('approve-auto-one'));
|
|
1638
|
+
await waitFor(() => expect(releaseFirstResponse).toBeDefined());
|
|
1639
|
+
expect(checkpointIssued).toBe(false);
|
|
1640
|
+
expect(serverCalls.find((call) => call.id === 'call-auto')?.invokeStatus).toBe('waiting');
|
|
1641
|
+
expect(serverCalls.find((call) => call.id === 'call-manual')?.invokeStatus).toBe('interrupted');
|
|
1642
|
+
|
|
1643
|
+
await userEvent.click(screen.getByTestId('disable-session-auto-approval'));
|
|
1644
|
+
await userEvent.click(screen.getByTestId('reject-manual-two'));
|
|
1645
|
+
|
|
1646
|
+
await waitFor(() => expect(resumeToolCallMock).toHaveBeenCalledTimes(1));
|
|
1647
|
+
expect(checkpointIssued).toBe(true);
|
|
1648
|
+
expect(decisionRequests.map(({ app: appName, request }) => [appName, request.toolCallId])).toEqual([
|
|
1649
|
+
['tab-one', 'call-auto'],
|
|
1650
|
+
['tab-two', 'call-manual'],
|
|
1651
|
+
]);
|
|
1652
|
+
expect(decisionRequests[0].request.agentAutoApproval).toBe(true);
|
|
1653
|
+
expect('agentAutoApproval' in decisionRequests[1].request).toBe(false);
|
|
1654
|
+
expect(runtimeTwo.chatToolCallModel.isAllWaiting(sessionId, messageId)).toBe(true);
|
|
1655
|
+
expect(invokeA).toHaveBeenCalledTimes(1);
|
|
1656
|
+
expect(invokeB).not.toHaveBeenCalled();
|
|
1657
|
+
expect(resumeToolCallMock).toHaveBeenCalledWith(
|
|
1658
|
+
expect.objectContaining({
|
|
1659
|
+
sessionId,
|
|
1660
|
+
messageId,
|
|
1661
|
+
toolCallIds: ['call-auto', 'call-manual', 'call-backend-auto'],
|
|
1662
|
+
toolCallResults: [{ id: 'call-auto', result: { status: 'success', content: 'A result' } }],
|
|
1663
|
+
}),
|
|
1664
|
+
);
|
|
1665
|
+
|
|
1666
|
+
await act(async () => releaseFirstResponse?.());
|
|
1667
|
+
await waitFor(() =>
|
|
1668
|
+
expect(runtimeOne.chatToolCallModel.getInvokeStatus(sessionId, messageId, 'call-auto')).toBe('waiting'),
|
|
1669
|
+
);
|
|
1670
|
+
expect(runtimeOne.chatToolCallModel.getInvokeStatus(sessionId, messageId, 'call-manual')).toBe('interrupted');
|
|
1671
|
+
expect(invokeA).toHaveBeenCalledTimes(1);
|
|
1672
|
+
expect(resumeToolCallMock).toHaveBeenCalledTimes(1);
|
|
1673
|
+
expect(serverCalls.find((call) => call.id === 'call-backend-auto')?.willInterrupt).toBe(false);
|
|
1674
|
+
expect(enabled).toBe(false);
|
|
1675
|
+
});
|
|
1676
|
+
});
|