plugin-agent-orchestrator 1.2.0 → 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/dist/client/{117.2e4cb0477da3db1f.js → 117.6c69b6a87751dd18.js} +1 -1
- package/dist/client/{253.93deb9e0192e76a5.js → 253.52f62e1bc0e9520a.js} +1 -1
- package/dist/client/{404.498d2d9516dd1dce.js → 404.86593f98c9768ed5.js} +1 -1
- package/dist/client/{627.fdd60bcc0f3a9cf6.js → 453.fa2d169576262213.js} +1 -1
- package/dist/client/{53.774ff07505d521fa.js → 53.567db3ae078650c6.js} +1 -1
- package/dist/client/{619.e342b0fa467d39ca.js → 619.01713281e5e951d3.js} +1 -1
- package/dist/client/652.69da1598312b3a3f.js +10 -0
- package/dist/client/773.648965568ff476f9.js +10 -0
- package/dist/client/{981.e0546ffc34afedcd.js → 981.37e7aeecdb1ef6fa.js} +1 -1
- package/dist/client/{993.5d04b510835ab0d2.js → 993.ac89f3852990918d.js} +1 -1
- package/dist/client/index.js +1 -1
- package/dist/client-v2/{117.ccf43ae8631f7d74.js → 117.c982d79ff68738f5.js} +1 -1
- package/dist/client-v2/{214.cdf76752053a8990.js → 214.94c4257bf2838577.js} +1 -1
- package/dist/client-v2/{253.1198df22f5baf304.js → 253.8e19956d2a44dfb8.js} +1 -1
- package/dist/client-v2/{264.78887ff88f969467.js → 264.80e8e358c8c6c2e5.js} +1 -1
- package/dist/client-v2/{404.9ba964c28a151c81.js → 404.ab032b79c95dab6f.js} +1 -1
- package/dist/client-v2/{41.b28cf7a675a603d1.js → 41.cd5610fcf7446127.js} +1 -1
- package/dist/client-v2/{627.e16f4a0dd596c3e6.js → 453.42c6d70628d56cc2.js} +1 -1
- package/dist/client-v2/{53.282081f97e4308c6.js → 53.37c08089a648f89a.js} +1 -1
- package/dist/client-v2/{619.8c03913873785fb6.js → 619.6e9618550101e819.js} +1 -1
- package/dist/client-v2/652.eeba4e0c38367321.js +10 -0
- package/dist/client-v2/{70.9166ed70906d041f.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.ca5b28f1e2b4c1ef.js → 993.f8cdb5172566ce75.js} +1 -1
- package/dist/client-v2/index.js +1 -1
- package/dist/externalVersion.js +4 -1
- package/dist/locale/en-US.json +80 -1
- package/dist/locale/vi-VN.json +5 -1
- package/dist/locale/zh-CN.json +80 -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/package.json +1 -1
- package/dist/node_modules/ajv/package.json +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 +3 -3
- package/dist/server/collections/agent-hierarchy-invocations.js +59 -0
- 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/20261119000000-widen-native-employee-identifiers.js +59 -0
- package/dist/server/plugin.js +55 -0
- package/dist/server/resources/agent-auto-approval.js +80 -0
- package/dist/server/resources/hierarchical-routing.js +60 -0
- package/dist/server/services/AutoApprovalCheckpoint.js +133 -0
- package/dist/server/services/CodeValidator.js +8 -4
- package/dist/server/services/GovernanceAdmission.js +60 -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/LoopRunRepository.js +1 -1
- package/dist/server/services/LoopRunStateMachine.js +2 -2
- package/dist/server/services/LoopWorkerService.js +7 -7
- 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/VerificationService.js +2 -2
- package/dist/server/skill-hub/plugin.js +17 -0
- 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 +1 -0
- package/package.json +5 -2
- package/src/client/SkillPages.tsx +1 -8
- package/src/client/__tests__/embedded-settings.test.tsx +1 -8
- package/src/client/plugin.tsx +3 -8
- package/src/client/skill-hub/tools/InteractionSchemasProvider.tsx +21 -6
- package/src/client/skill-hub/tools/SkillHubCard.tsx +22 -16
- package/src/client/skill-hub/tools/__tests__/skill-hub-card.test.tsx +255 -0
- package/src/client/skill-hub/tools/registerSkillLoopCards.ts +12 -3
- package/src/client-v2/__tests__/embedded-settings.test.tsx +20 -11
- 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/AgentBindingsTab.tsx +275 -3
- package/src/client-v2/components/AgentRunsTab.tsx +1 -8
- package/src/client-v2/components/EmployeeGovernanceTab.tsx +1 -8
- package/src/client-v2/components/HarnessProfilesTab.tsx +4 -8
- package/src/client-v2/components/HarnessSettingsForm.tsx +221 -0
- package/src/client-v2/components/UsageGovernanceTab.tsx +1 -8
- package/src/client-v2/pages/AgentBindingsPage.tsx +1 -8
- package/src/client-v2/pages/ApprovalsPage.tsx +1 -8
- package/src/client-v2/pages/EmployeeGovernancePage.tsx +1 -8
- package/src/client-v2/pages/ExecutionHistoryPage.tsx +1 -8
- package/src/client-v2/pages/HarnessProfilesPage.tsx +1 -8
- package/src/client-v2/pages/KnowledgeAccessPage.tsx +1 -8
- package/src/client-v2/pages/LoopSettingsPage.tsx +1 -8
- package/src/client-v2/pages/MemoryInspectorPage.tsx +1 -8
- package/src/client-v2/pages/RetrievalTracePage.tsx +1 -8
- package/src/client-v2/pages/RulesPage.tsx +1 -8
- package/src/client-v2/pages/SkillDefinitionsPage.tsx +1 -8
- package/src/client-v2/pages/SkillMetricsPage.tsx +1 -8
- package/src/client-v2/pages/TracingPage.tsx +1 -8
- package/src/client-v2/pages/UsageGovernancePage.tsx +1 -8
- package/src/client-v2/plugin.tsx +3 -8
- package/src/client-v2/skill-hub/tools/__tests__/interactions.test.ts +17 -1
- package/src/client-v2/skill-hub/tools/interactions.ts +12 -3
- package/src/locale/en-US.json +80 -1
- package/src/locale/vi-VN.json +5 -1
- package/src/locale/zh-CN.json +80 -1
- package/src/server/__tests__/agent-governance.integration.test.ts +1 -8
- package/src/server/__tests__/agent-monitor-acl.integration.test.ts +1 -8
- package/src/server/__tests__/agent-usage-report.integration.test.ts +1 -8
- package/src/server/__tests__/auto-approval-checkpoint.integration.test.ts +295 -0
- package/src/server/__tests__/harness-capabilities.test.ts +1 -8
- package/src/server/__tests__/harness-compiler.test.ts +64 -0
- package/src/server/__tests__/harness-tool-plan.test.ts +1 -8
- 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__/loop-approval-decision.test.ts +59 -12
- package/src/server/__tests__/loop-budget-service.test.ts +1 -8
- package/src/server/__tests__/loop-run-repository.test.ts +1 -8
- package/src/server/__tests__/loop-trigger-service.test.ts +1 -8
- package/src/server/__tests__/loop-worker-claim.test.ts +46 -9
- package/src/server/__tests__/loop-worker-execution.test.ts +1 -8
- package/src/server/__tests__/native-agent-monitor-sync.test.ts +1 -8
- package/src/server/__tests__/native-hierarchy-probe.integration.test.ts +527 -0
- package/src/server/__tests__/native-sub-agent-observer.test.ts +1 -8
- package/src/server/__tests__/native-usage-attribution.integration.test.ts +1 -8
- 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-execute.test.ts +11 -0
- package/src/server/__tests__/skill-review-guard.test.ts +171 -0
- package/src/server/__tests__/token-tracker.test.ts +1 -8
- package/src/server/__tests__/tracing-retention-policy.test.ts +1 -8
- package/src/server/__tests__/usage-ledger.integration.test.ts +1 -8
- package/src/server/collections/agent-auto-approval-checkpoints.ts +18 -0
- package/src/server/collections/agent-execution-spans.ts +4 -11
- package/src/server/collections/agent-governance-audit-events.ts +1 -8
- package/src/server/collections/agent-governance-policies.ts +1 -8
- package/src/server/collections/agent-governance-policy-versions.ts +1 -8
- package/src/server/collections/agent-governance-prompt-bindings.ts +1 -8
- package/src/server/collections/agent-hierarchy-invocations.ts +30 -0
- package/src/server/collections/agent-loop-runs.ts +1 -8
- 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 +26 -0
- package/src/server/migrations/20261110000000-add-usage-pricing-snapshots.ts +1 -8
- package/src/server/migrations/20261110010000-add-usage-department-attribution.ts +1 -8
- package/src/server/migrations/20261119000000-widen-native-employee-identifiers.ts +37 -0
- package/src/server/plugin.ts +58 -8
- package/src/server/resources/agent-auto-approval.ts +51 -0
- package/src/server/resources/agent-governance.ts +1 -8
- package/src/server/resources/agent-harness-profile.ts +1 -8
- package/src/server/resources/agent-monitor.ts +1 -8
- package/src/server/resources/agent-usage.ts +1 -8
- package/src/server/resources/hierarchical-routing.ts +39 -0
- package/src/server/resources/native-monitor-format.ts +1 -8
- package/src/server/resources/tracing.ts +1 -8
- package/src/server/services/AgentGovernanceService.ts +1 -8
- package/src/server/services/AutoApprovalCheckpoint.ts +140 -0
- package/src/server/services/CodeValidator.ts +23 -4
- package/src/server/services/DepartmentAttribution.ts +1 -8
- package/src/server/services/ExecutionSpanService.ts +1 -8
- package/src/server/services/GovernanceAdmission.ts +83 -8
- package/src/server/services/HarnessCapabilities.ts +1 -8
- package/src/server/services/HarnessCompiler.ts +12 -0
- package/src/server/services/HarnessSchema.ts +30 -0
- 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/LoopBudgetService.ts +1 -8
- package/src/server/services/LoopRunRepository.ts +2 -9
- package/src/server/services/LoopRunStateMachine.ts +3 -10
- package/src/server/services/LoopTriggerService.ts +1 -8
- package/src/server/services/LoopWorkerService.ts +8 -19
- package/src/server/services/ManagedPromptProtection.ts +1 -8
- package/src/server/services/NativeAgentMonitorSyncService.ts +1 -8
- package/src/server/services/NativeSubAgentObserver.ts +1 -8
- package/src/server/services/NativeUsageAttribution.ts +1 -8
- package/src/server/services/PluginAiRuntimeAdapter.ts +140 -9
- package/src/server/services/RoutingBindingRepository.ts +146 -0
- package/src/server/services/RoutingBindingValidation.ts +161 -0
- package/src/server/services/SandboxRunner.ts +35 -1
- package/src/server/services/SessionAutoApproval.ts +167 -0
- package/src/server/services/TokenTracker.ts +1 -8
- package/src/server/services/TracingRetentionPolicy.ts +1 -8
- package/src/server/services/VerificationService.ts +3 -10
- package/src/server/skill-hub/plugin.ts +40 -0
- package/src/server/skill-hub/services/RegistrySkillInstallationService.ts +11 -1
- package/src/server/skill-hub/services/registry-node-package.ts +191 -0
- package/src/server/skill-hub/tasks/SkillExecutionTask.ts +34 -5
- package/src/server/tools/skill-execute.ts +10 -0
- package/src/server/utils/ensure-indexes.ts +2 -8
- package/dist/client/418.6f01da17774a5a25.js +0 -10
- package/dist/client/652.477e21bfff67bc1c.js +0 -10
- package/dist/client-v2/418.55b6b28cda8bb5f3.js +0 -10
- package/dist/client-v2/652.1a48364774d5383a.js +0 -10
- package/dist/client-v2/981.a81521f0296c7db8.js +0 -10
|
@@ -0,0 +1,513 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
|
|
3
|
+
import React, { createContext, useCallback, useContext, useEffect, useMemo, useRef, useState } from 'react';
|
|
4
|
+
import { useApp } from '@nocobase/client-v2';
|
|
5
|
+
import { observer } from '@nocobase/flow-engine';
|
|
6
|
+
import type { ChatBoxRuntime } from '@nocobase/plugin-ai/client-v2';
|
|
7
|
+
|
|
8
|
+
export type ApprovalIdentity = { sessionId: string; messageId: string; toolCallId: string };
|
|
9
|
+
export const approvalIdentityKey = (identity: ApprovalIdentity) => JSON.stringify(Object.values(identity));
|
|
10
|
+
|
|
11
|
+
type CheckpointToolCall = {
|
|
12
|
+
id: string;
|
|
13
|
+
willInterrupt: boolean;
|
|
14
|
+
invokeStatus?: string;
|
|
15
|
+
userDecision?: unknown;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
function checkpointCalls(value: unknown): CheckpointToolCall[] | null {
|
|
19
|
+
if (!Array.isArray(value)) return null;
|
|
20
|
+
const result: CheckpointToolCall[] = [];
|
|
21
|
+
for (const item of value) {
|
|
22
|
+
if (!item || typeof item !== 'object' || Array.isArray(item)) return null;
|
|
23
|
+
const call = item as Record<string, unknown>;
|
|
24
|
+
if (typeof call.id !== 'string' || typeof call.willInterrupt !== 'boolean') return null;
|
|
25
|
+
result.push({
|
|
26
|
+
id: call.id,
|
|
27
|
+
willInterrupt: call.willInterrupt,
|
|
28
|
+
invokeStatus: typeof call.invokeStatus === 'string' ? call.invokeStatus : undefined,
|
|
29
|
+
userDecision: call.userDecision,
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
return result;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export class AutoApprovalRequests {
|
|
36
|
+
private pending = new Map<
|
|
37
|
+
string,
|
|
38
|
+
{
|
|
39
|
+
identity: ApprovalIdentity;
|
|
40
|
+
owners: Map<object, { rejected: () => void; active: () => boolean; runtime?: ChatBoxRuntime }>;
|
|
41
|
+
}
|
|
42
|
+
>();
|
|
43
|
+
private groups = new Map<string, { key: string; owner: object }>();
|
|
44
|
+
private listeners = new Set<() => void>();
|
|
45
|
+
private runtimes = new Map<ChatBoxRuntime, number>();
|
|
46
|
+
private checkpoints = new Map<string, { interruptId: string; callIds: Set<string> }>();
|
|
47
|
+
private inFlight = new Map<string, number>();
|
|
48
|
+
|
|
49
|
+
private groupKey(identity: ApprovalIdentity) {
|
|
50
|
+
return JSON.stringify([identity.sessionId, identity.messageId]);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
reserve(identity: ApprovalIdentity, owner: object) {
|
|
54
|
+
const group = this.groupKey(identity);
|
|
55
|
+
if (this.groups.has(group)) return false;
|
|
56
|
+
this.groups.set(group, { key: approvalIdentityKey(identity), owner });
|
|
57
|
+
return true;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
release(identity: ApprovalIdentity, owner: object) {
|
|
61
|
+
const group = this.groupKey(identity);
|
|
62
|
+
const held = this.groups.get(group);
|
|
63
|
+
if (!held || held.key !== approvalIdentityKey(identity) || held.owner !== owner) return;
|
|
64
|
+
this.groups.delete(group);
|
|
65
|
+
for (const listener of this.listeners) listener();
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
subscribe(listener: () => void) {
|
|
69
|
+
this.listeners.add(listener);
|
|
70
|
+
return () => {
|
|
71
|
+
this.listeners.delete(listener);
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
registerRuntime(runtime: ChatBoxRuntime) {
|
|
76
|
+
this.runtimes.set(runtime, (this.runtimes.get(runtime) ?? 0) + 1);
|
|
77
|
+
return () => {
|
|
78
|
+
const count = this.runtimes.get(runtime) ?? 0;
|
|
79
|
+
if (count <= 1) this.runtimes.delete(runtime);
|
|
80
|
+
else this.runtimes.set(runtime, count - 1);
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
matchingRuntimes(identity: ApprovalIdentity) {
|
|
85
|
+
return [...this.runtimes.keys()].filter((runtime) => {
|
|
86
|
+
if (runtime.chatConversationModel.currentConversation !== identity.sessionId) return false;
|
|
87
|
+
const messages = runtime.chatMessageModel.sessions[identity.sessionId]?.messages;
|
|
88
|
+
const message = messages?.find((item) => String(item.content?.messageId) === identity.messageId);
|
|
89
|
+
return message?.content.tool_calls?.some((call) => call.id === identity.toolCallId);
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
beginRequest(identity: ApprovalIdentity) {
|
|
94
|
+
const group = this.groupKey(identity);
|
|
95
|
+
this.inFlight.set(group, (this.inFlight.get(group) ?? 0) + 1);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
endRequest(identity: ApprovalIdentity) {
|
|
99
|
+
const group = this.groupKey(identity);
|
|
100
|
+
const count = this.inFlight.get(group) ?? 0;
|
|
101
|
+
if (count <= 1) {
|
|
102
|
+
this.inFlight.delete(group);
|
|
103
|
+
this.checkpoints.delete(group);
|
|
104
|
+
} else {
|
|
105
|
+
this.inFlight.set(group, count - 1);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
rememberCheckpoint(identity: ApprovalIdentity, interruptId: string, calls: CheckpointToolCall[]) {
|
|
110
|
+
this.checkpoints.set(this.groupKey(identity), { interruptId, callIds: new Set(calls.map((call) => call.id)) });
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
hasCheckpoint(identity: ApprovalIdentity, calls: CheckpointToolCall[]) {
|
|
114
|
+
const checkpoint = this.checkpoints.get(this.groupKey(identity));
|
|
115
|
+
return (
|
|
116
|
+
!!checkpoint && checkpoint.callIds.size === calls.length && calls.every((call) => checkpoint.callIds.has(call.id))
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
synchronize(identity: ApprovalIdentity, interruptId: string | undefined, calls: CheckpointToolCall[]) {
|
|
121
|
+
const callIds = new Set(calls.map((call) => call.id));
|
|
122
|
+
const waiting = calls.filter((call) => call.willInterrupt && call.invokeStatus === 'waiting');
|
|
123
|
+
const matched = new Set<ChatBoxRuntime>();
|
|
124
|
+
for (const runtime of this.runtimes.keys()) {
|
|
125
|
+
if (runtime.chatConversationModel.currentConversation !== identity.sessionId) continue;
|
|
126
|
+
const messages = runtime.chatMessageModel.sessions[identity.sessionId]?.messages;
|
|
127
|
+
const message = messages?.find((item) => String(item.content?.messageId) === identity.messageId);
|
|
128
|
+
const current = message?.content.tool_calls;
|
|
129
|
+
const metadata = message?.content.metadata as Record<string, unknown> | undefined;
|
|
130
|
+
if (
|
|
131
|
+
!current ||
|
|
132
|
+
current.length !== callIds.size ||
|
|
133
|
+
current.some((item) => !callIds.has(item.id)) ||
|
|
134
|
+
(typeof interruptId === 'string' &&
|
|
135
|
+
typeof metadata?.interruptId === 'string' &&
|
|
136
|
+
metadata.interruptId !== interruptId) ||
|
|
137
|
+
waiting.some((call) => {
|
|
138
|
+
const status = runtime.chatToolCallModel.getInvokeStatus(identity.sessionId, identity.messageId, call.id);
|
|
139
|
+
return status !== undefined && status !== 'interrupted' && status !== 'waiting';
|
|
140
|
+
})
|
|
141
|
+
)
|
|
142
|
+
continue;
|
|
143
|
+
matched.add(runtime);
|
|
144
|
+
for (const call of waiting) {
|
|
145
|
+
const status = runtime.chatToolCallModel.getInvokeStatus(identity.sessionId, identity.messageId, call.id);
|
|
146
|
+
if (status === undefined || status === 'interrupted') {
|
|
147
|
+
runtime.chatToolCallModel.updateToolCallInvokeStatus(
|
|
148
|
+
identity.sessionId,
|
|
149
|
+
identity.messageId,
|
|
150
|
+
call.id,
|
|
151
|
+
'waiting',
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
runtime.chatMessageModel.setSessionMessages(identity.sessionId, (entries) =>
|
|
156
|
+
entries.map((entry) => {
|
|
157
|
+
if (String(entry.content?.messageId) !== identity.messageId) return entry;
|
|
158
|
+
return {
|
|
159
|
+
...entry,
|
|
160
|
+
content: {
|
|
161
|
+
...entry.content,
|
|
162
|
+
tool_calls: entry.content.tool_calls?.map((item) => {
|
|
163
|
+
const call = waiting.find((candidate) => candidate.id === item.id);
|
|
164
|
+
if (!call || item.invokeStatus !== 'interrupted') return item;
|
|
165
|
+
return { ...item, invokeStatus: 'waiting', userDecision: call.userDecision };
|
|
166
|
+
}),
|
|
167
|
+
},
|
|
168
|
+
};
|
|
169
|
+
}),
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
return matched;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
mark(
|
|
176
|
+
identity: ApprovalIdentity,
|
|
177
|
+
rejected: () => void,
|
|
178
|
+
owner: object = rejected,
|
|
179
|
+
active = () => true,
|
|
180
|
+
runtime?: ChatBoxRuntime,
|
|
181
|
+
) {
|
|
182
|
+
const key = approvalIdentityKey(identity);
|
|
183
|
+
let entry = this.pending.get(key);
|
|
184
|
+
if (!entry) {
|
|
185
|
+
entry = { identity, owners: new Map() };
|
|
186
|
+
this.pending.set(key, entry);
|
|
187
|
+
}
|
|
188
|
+
entry.owners.set(owner, { rejected, active, runtime });
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
has(identity: ApprovalIdentity) {
|
|
192
|
+
const owners = this.pending.get(approvalIdentityKey(identity))?.owners.values() ?? [];
|
|
193
|
+
return [...owners].some((entry) => entry.active());
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
automaticRuntime(identity: ApprovalIdentity) {
|
|
197
|
+
const owners = this.pending.get(approvalIdentityKey(identity))?.owners.values() ?? [];
|
|
198
|
+
return [...owners].find((entry) => entry.active())?.runtime;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
reject(identity: ApprovalIdentity) {
|
|
202
|
+
for (const entry of this.pending.get(approvalIdentityKey(identity))?.owners.values() ?? []) entry.rejected();
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
clear(identity: ApprovalIdentity, owner?: object) {
|
|
206
|
+
const key = approvalIdentityKey(identity);
|
|
207
|
+
const entry = this.pending.get(key);
|
|
208
|
+
if (!owner) this.pending.delete(key);
|
|
209
|
+
else if (entry) {
|
|
210
|
+
entry.owners.delete(owner);
|
|
211
|
+
if (!entry.owners.size) this.pending.delete(key);
|
|
212
|
+
}
|
|
213
|
+
if (owner) this.release(identity, owner);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
identities(owner?: object) {
|
|
217
|
+
return [...this.pending.values()].filter((item) => !owner || item.owners.has(owner)).map((item) => item.identity);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
export const AutoApprovalRequestsContext = createContext<AutoApprovalRequests | null>(null);
|
|
222
|
+
|
|
223
|
+
/** Mark only native approvals for which this plugin requested auto-approval. */
|
|
224
|
+
export function useAutoApprovalInterceptor(requests: AutoApprovalRequests) {
|
|
225
|
+
const { apiClient } = useApp();
|
|
226
|
+
useEffect(() => {
|
|
227
|
+
const outgoing = new WeakMap<
|
|
228
|
+
object,
|
|
229
|
+
{ identity: ApprovalIdentity; automatic: boolean; origin?: ChatBoxRuntime; runtimes: ChatBoxRuntime[] }
|
|
230
|
+
>();
|
|
231
|
+
const interceptor = apiClient.axios.interceptors.request.use((config) => {
|
|
232
|
+
if (!config.url?.split('?')[0].endsWith('aiConversations:updateUserDecision')) return config;
|
|
233
|
+
const data = config.data;
|
|
234
|
+
if (!data || typeof data !== 'object' || Array.isArray(data)) return config;
|
|
235
|
+
const { sessionId, messageId, toolCallId, userDecision } = data as Record<string, unknown>;
|
|
236
|
+
if (
|
|
237
|
+
typeof sessionId !== 'string' ||
|
|
238
|
+
(typeof messageId !== 'string' && typeof messageId !== 'number') ||
|
|
239
|
+
typeof toolCallId !== 'string' ||
|
|
240
|
+
!userDecision ||
|
|
241
|
+
typeof userDecision !== 'object'
|
|
242
|
+
)
|
|
243
|
+
return config;
|
|
244
|
+
const identity = { sessionId, messageId: String(messageId), toolCallId };
|
|
245
|
+
const automatic = (userDecision as { type?: unknown }).type === 'approve' && requests.has(identity);
|
|
246
|
+
if (automatic) {
|
|
247
|
+
config.data = { ...data, agentAutoApproval: true };
|
|
248
|
+
}
|
|
249
|
+
requests.beginRequest(identity);
|
|
250
|
+
outgoing.set(config, {
|
|
251
|
+
identity,
|
|
252
|
+
automatic,
|
|
253
|
+
origin: automatic ? requests.automaticRuntime(identity) : undefined,
|
|
254
|
+
runtimes: requests.matchingRuntimes(identity),
|
|
255
|
+
});
|
|
256
|
+
return config;
|
|
257
|
+
});
|
|
258
|
+
const responseInterceptor = apiClient.axios.interceptors.response.use(
|
|
259
|
+
(response) => {
|
|
260
|
+
const request = outgoing.get(response.config);
|
|
261
|
+
if (!request) return response;
|
|
262
|
+
const { identity, automatic } = request;
|
|
263
|
+
try {
|
|
264
|
+
const payload = response.data?.data as Record<string, unknown> | undefined;
|
|
265
|
+
const marker = payload?.agentAutoApprovalCheckpoint;
|
|
266
|
+
const checkpoint = marker && typeof marker === 'object' ? (marker as Record<string, unknown>) : undefined;
|
|
267
|
+
const calls = checkpointCalls(payload?.toolCalls);
|
|
268
|
+
if (checkpoint && typeof checkpoint.interruptId === 'string' && checkpoint.validSnapshot === true && calls) {
|
|
269
|
+
requests.rememberCheckpoint(identity, checkpoint.interruptId, calls);
|
|
270
|
+
const matched = requests.synchronize(identity, checkpoint.interruptId, calls);
|
|
271
|
+
if (
|
|
272
|
+
checkpoint.canFinalize !== true ||
|
|
273
|
+
(automatic
|
|
274
|
+
? !request.origin || !matched.has(request.origin)
|
|
275
|
+
: !request.runtimes.some((runtime) => matched.has(runtime)))
|
|
276
|
+
) {
|
|
277
|
+
if (payload) payload.updated = 0;
|
|
278
|
+
}
|
|
279
|
+
if (automatic && checkpoint.decisionRecorded !== true) {
|
|
280
|
+
const selected = calls.find((call) => call.id === identity.toolCallId);
|
|
281
|
+
if (selected?.invokeStatus !== 'waiting') requests.reject(identity);
|
|
282
|
+
}
|
|
283
|
+
} else if (
|
|
284
|
+
!checkpoint &&
|
|
285
|
+
calls &&
|
|
286
|
+
(request.runtimes.length > 0 ||
|
|
287
|
+
(automatic && !!request.origin) ||
|
|
288
|
+
requests.hasCheckpoint(identity, calls)) &&
|
|
289
|
+
calls.length > 1 &&
|
|
290
|
+
new Set(calls.map((call) => call.id)).size === calls.length &&
|
|
291
|
+
calls.some((call) => call.id === identity.toolCallId && call.willInterrupt) &&
|
|
292
|
+
calls.some((call) => call.willInterrupt && call.invokeStatus !== 'waiting')
|
|
293
|
+
) {
|
|
294
|
+
// A native partial response may arrive after another view has finished the checkpoint.
|
|
295
|
+
// Apply only decisions already persisted, and do not let this callback run the tools.
|
|
296
|
+
requests.synchronize(identity, undefined, calls);
|
|
297
|
+
if (automatic && payload?.updated === 0) {
|
|
298
|
+
const selected = calls.find((call) => call.id === identity.toolCallId);
|
|
299
|
+
if (selected?.invokeStatus !== 'waiting') requests.reject(identity);
|
|
300
|
+
}
|
|
301
|
+
if (payload) payload.updated = 0;
|
|
302
|
+
} else if (!checkpoint && calls && requests.hasCheckpoint(identity, calls)) {
|
|
303
|
+
// A manual request can have left the server just before this checkpoint was enrolled.
|
|
304
|
+
// Its delayed response must not run a second native finalizer in the same runtime.
|
|
305
|
+
if (payload) payload.updated = 0;
|
|
306
|
+
} else if (automatic && payload?.updated === 0) {
|
|
307
|
+
requests.reject(identity);
|
|
308
|
+
}
|
|
309
|
+
} finally {
|
|
310
|
+
requests.endRequest(identity);
|
|
311
|
+
}
|
|
312
|
+
return response;
|
|
313
|
+
},
|
|
314
|
+
(error) => {
|
|
315
|
+
const request = error?.config && outgoing.get(error.config);
|
|
316
|
+
if (request) {
|
|
317
|
+
if (request.automatic) requests.reject(request.identity);
|
|
318
|
+
requests.endRequest(request.identity);
|
|
319
|
+
}
|
|
320
|
+
return Promise.reject(error);
|
|
321
|
+
},
|
|
322
|
+
);
|
|
323
|
+
return () => {
|
|
324
|
+
apiClient.axios.interceptors.request.eject(interceptor);
|
|
325
|
+
apiClient.axios.interceptors.response.eject(responseInterceptor);
|
|
326
|
+
};
|
|
327
|
+
}, [apiClient, requests]);
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
export const AutoApprovalBridge = observer(({ runtime, enabled }: { runtime: ChatBoxRuntime; enabled: boolean }) => {
|
|
331
|
+
const { apiClient } = useApp();
|
|
332
|
+
const requests = useContext(AutoApprovalRequestsContext);
|
|
333
|
+
const sessionId = runtime.chatConversationModel.currentConversation;
|
|
334
|
+
const messages = runtime.chatMessageModel.sessions[sessionId || '__draft__']?.messages;
|
|
335
|
+
const toolCalls = runtime.chatToolCallModel.sessions[sessionId || '__draft__']?.toolCalls;
|
|
336
|
+
const checked = useRef(new Set<string>());
|
|
337
|
+
const marked = useRef(new Map<string, ApprovalIdentity>());
|
|
338
|
+
const reserved = useRef(new Map<string, ApprovalIdentity>());
|
|
339
|
+
const owner = useRef({});
|
|
340
|
+
const [queueVersion, setQueueVersion] = useState(0);
|
|
341
|
+
const enabledRef = useRef(enabled);
|
|
342
|
+
enabledRef.current = enabled;
|
|
343
|
+
|
|
344
|
+
useEffect(() => requests?.registerRuntime(runtime), [requests, runtime]);
|
|
345
|
+
useEffect(() => requests?.subscribe(() => setQueueVersion((value) => value + 1)), [requests]);
|
|
346
|
+
|
|
347
|
+
const restore = useCallback(
|
|
348
|
+
(identity: ApprovalIdentity) => {
|
|
349
|
+
runtime.chatMessageModel.setSessionMessages(identity.sessionId, (current) =>
|
|
350
|
+
current.map((entry) => {
|
|
351
|
+
if (String(entry.content?.messageId) !== identity.messageId) return entry;
|
|
352
|
+
return {
|
|
353
|
+
...entry,
|
|
354
|
+
content: {
|
|
355
|
+
...entry.content,
|
|
356
|
+
tool_calls: entry.content.tool_calls?.map((call) =>
|
|
357
|
+
call.id === identity.toolCallId ? { ...call, auto: false } : call,
|
|
358
|
+
),
|
|
359
|
+
},
|
|
360
|
+
};
|
|
361
|
+
}),
|
|
362
|
+
);
|
|
363
|
+
},
|
|
364
|
+
[runtime],
|
|
365
|
+
);
|
|
366
|
+
|
|
367
|
+
const candidates = useMemo(() => {
|
|
368
|
+
if (!sessionId || !messages || !toolCalls || !enabled) return [];
|
|
369
|
+
return messages.flatMap((entry) => {
|
|
370
|
+
const content = entry.content;
|
|
371
|
+
const messageId = content?.messageId;
|
|
372
|
+
if (content?.from === 'sub-agent' || !messageId) return [];
|
|
373
|
+
if (content.tool_calls?.some((call) => call.invokeStatus === 'interrupted' && call.auto === true)) return [];
|
|
374
|
+
return (content.tool_calls || [])
|
|
375
|
+
.filter(
|
|
376
|
+
(call) =>
|
|
377
|
+
call.invokeStatus === 'interrupted' &&
|
|
378
|
+
call.auto === false &&
|
|
379
|
+
runtime.chatToolCallModel.isInterrupted(sessionId, messageId, call.id),
|
|
380
|
+
)
|
|
381
|
+
.map((call) => ({ sessionId, messageId: String(messageId), toolCallId: call.id }));
|
|
382
|
+
});
|
|
383
|
+
}, [enabled, messages, runtime, sessionId, toolCalls]);
|
|
384
|
+
|
|
385
|
+
useEffect(() => {
|
|
386
|
+
if (!enabled || !sessionId || !requests) return;
|
|
387
|
+
for (const identity of candidates) {
|
|
388
|
+
const key = approvalIdentityKey(identity);
|
|
389
|
+
if (checked.current.has(key) || !requests.reserve(identity, owner.current)) continue;
|
|
390
|
+
checked.current.add(key);
|
|
391
|
+
reserved.current.set(key, identity);
|
|
392
|
+
apiClient
|
|
393
|
+
.request({
|
|
394
|
+
url: 'agentAutoApproval:eligible',
|
|
395
|
+
method: 'post',
|
|
396
|
+
data: identity,
|
|
397
|
+
skipNotify: true,
|
|
398
|
+
})
|
|
399
|
+
.then((response) => {
|
|
400
|
+
const live = runtime.chatMessageModel.sessions[identity.sessionId]?.messages.find(
|
|
401
|
+
(entry) => String(entry.content?.messageId) === identity.messageId && entry.content?.from !== 'sub-agent',
|
|
402
|
+
);
|
|
403
|
+
const calls = live?.content?.tool_calls;
|
|
404
|
+
const call = calls?.find((item) => item.id === identity.toolCallId);
|
|
405
|
+
if (
|
|
406
|
+
response.data?.data?.eligible !== true ||
|
|
407
|
+
!enabledRef.current ||
|
|
408
|
+
runtime.chatConversationModel.currentConversation !== identity.sessionId ||
|
|
409
|
+
!runtime.chatToolCallModel.isInterrupted(identity.sessionId, identity.messageId, identity.toolCallId) ||
|
|
410
|
+
!call ||
|
|
411
|
+
call.auto !== false ||
|
|
412
|
+
call.invokeStatus !== 'interrupted' ||
|
|
413
|
+
calls?.some((item) => item.invokeStatus === 'interrupted' && item.auto === true)
|
|
414
|
+
) {
|
|
415
|
+
requests.release(identity, owner.current);
|
|
416
|
+
reserved.current.delete(key);
|
|
417
|
+
return;
|
|
418
|
+
}
|
|
419
|
+
requests.mark(
|
|
420
|
+
identity,
|
|
421
|
+
() => {
|
|
422
|
+
enabledRef.current = false;
|
|
423
|
+
restore(identity);
|
|
424
|
+
window.dispatchEvent(new CustomEvent('agent-auto-approval-stale', { detail: identity.sessionId }));
|
|
425
|
+
},
|
|
426
|
+
owner.current,
|
|
427
|
+
() => enabledRef.current,
|
|
428
|
+
runtime,
|
|
429
|
+
);
|
|
430
|
+
marked.current.set(key, identity);
|
|
431
|
+
runtime.chatMessageModel.setSessionMessages(identity.sessionId, (current) =>
|
|
432
|
+
current.map((entry) => {
|
|
433
|
+
if (String(entry.content?.messageId) !== identity.messageId || entry.content?.from === 'sub-agent')
|
|
434
|
+
return entry;
|
|
435
|
+
return {
|
|
436
|
+
...entry,
|
|
437
|
+
content: {
|
|
438
|
+
...entry.content,
|
|
439
|
+
tool_calls: entry.content.tool_calls?.map((call) => {
|
|
440
|
+
if (call.id !== identity.toolCallId || call.invokeStatus !== 'interrupted') return call;
|
|
441
|
+
const guarded = { ...call };
|
|
442
|
+
Object.defineProperty(guarded, 'auto', {
|
|
443
|
+
enumerable: true,
|
|
444
|
+
configurable: true,
|
|
445
|
+
get: () =>
|
|
446
|
+
enabledRef.current &&
|
|
447
|
+
runtime.chatConversationModel.currentConversation === identity.sessionId &&
|
|
448
|
+
runtime.chatToolCallModel.isInterrupted(
|
|
449
|
+
identity.sessionId,
|
|
450
|
+
identity.messageId,
|
|
451
|
+
identity.toolCallId,
|
|
452
|
+
),
|
|
453
|
+
});
|
|
454
|
+
return guarded;
|
|
455
|
+
}),
|
|
456
|
+
},
|
|
457
|
+
};
|
|
458
|
+
}),
|
|
459
|
+
);
|
|
460
|
+
})
|
|
461
|
+
.catch(() => {
|
|
462
|
+
requests.release(identity, owner.current);
|
|
463
|
+
reserved.current.delete(key);
|
|
464
|
+
// Leave the normal approval controls available when eligibility cannot be checked.
|
|
465
|
+
});
|
|
466
|
+
}
|
|
467
|
+
}, [apiClient, candidates, enabled, queueVersion, requests, restore, runtime, sessionId]);
|
|
468
|
+
|
|
469
|
+
useEffect(() => {
|
|
470
|
+
if (enabled) return;
|
|
471
|
+
for (const identity of marked.current.values()) restore(identity);
|
|
472
|
+
checked.current.clear();
|
|
473
|
+
}, [enabled, restore]);
|
|
474
|
+
|
|
475
|
+
useEffect(() => {
|
|
476
|
+
for (const identity of requests?.identities(owner.current) ?? []) {
|
|
477
|
+
if (identity.sessionId !== sessionId) continue;
|
|
478
|
+
const entry = runtime.chatMessageModel.sessions[identity.sessionId]?.messages.find(
|
|
479
|
+
(item) => String(item.content?.messageId) === identity.messageId,
|
|
480
|
+
);
|
|
481
|
+
const call = entry?.content?.tool_calls?.find((item) => item.id === identity.toolCallId);
|
|
482
|
+
if (
|
|
483
|
+
call &&
|
|
484
|
+
Object.getOwnPropertyDescriptor(call, 'auto')?.get &&
|
|
485
|
+
!runtime.chatToolCallModel.isInterrupted(identity.sessionId, identity.messageId, identity.toolCallId)
|
|
486
|
+
) {
|
|
487
|
+
restore(identity);
|
|
488
|
+
} else if (!call || call.auto !== true || call.invokeStatus !== 'interrupted') {
|
|
489
|
+
requests?.clear(identity, owner.current);
|
|
490
|
+
marked.current.delete(approvalIdentityKey(identity));
|
|
491
|
+
reserved.current.delete(approvalIdentityKey(identity));
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
}, [messages, requests, restore, runtime, sessionId, toolCalls]);
|
|
495
|
+
|
|
496
|
+
useEffect(
|
|
497
|
+
() => () => {
|
|
498
|
+
enabledRef.current = false;
|
|
499
|
+
for (const identity of marked.current.values()) {
|
|
500
|
+
restore(identity);
|
|
501
|
+
setTimeout(() => requests?.clear(identity, owner.current), 0);
|
|
502
|
+
}
|
|
503
|
+
for (const [key, identity] of reserved.current) {
|
|
504
|
+
if (!marked.current.has(key)) requests?.release(identity, owner.current);
|
|
505
|
+
}
|
|
506
|
+
marked.current.clear();
|
|
507
|
+
reserved.current.clear();
|
|
508
|
+
},
|
|
509
|
+
[requests, restore],
|
|
510
|
+
);
|
|
511
|
+
|
|
512
|
+
return null;
|
|
513
|
+
});
|