@botbotgo/agent-harness 0.0.474 → 0.0.476
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -1234
- package/README.zh.md +3 -1191
- package/dist/acp.js +1 -1
- package/dist/api.js +1 -404
- package/dist/benchmark/checkpoint-resume-cost-benchmark.js +1 -55
- package/dist/benchmark/deepagent-local-model-benchmark.js +2 -35
- package/dist/benchmark/upstream-runtime-ab-benchmark.js +1 -179
- package/dist/cli/chat-interactive.js +25 -244
- package/dist/cli/chat-rendering.js +6 -100
- package/dist/cli/chat-stream.js +23 -512
- package/dist/cli/chat-ui.js +21 -199
- package/dist/cli/chat-workspace.js +2 -210
- package/dist/cli/main.js +21 -428
- package/dist/cli/managed-service-commands.js +9 -63
- package/dist/cli/managed-service.js +2 -137
- package/dist/cli/options-init-chat.js +1 -108
- package/dist/cli/options-runtime.js +1 -158
- package/dist/cli/options-serve.js +1 -282
- package/dist/cli/options.js +2 -19
- package/dist/cli/process-guards.js +1 -139
- package/dist/cli/request-tree.js +7 -296
- package/dist/cli/runtime-commands.js +12 -258
- package/dist/cli/runtime-output.js +16 -155
- package/dist/cli/server-commands.js +16 -270
- package/dist/cli/workspace.js +1 -67
- package/dist/cli.js +1 -7
- package/dist/client/acp.js +1 -1
- package/dist/client/in-process.js +1 -67
- package/dist/client/index.js +1 -2
- package/dist/client/types.js +0 -1
- package/dist/client.js +1 -1
- package/dist/contracts/core.js +1 -1
- package/dist/contracts/runtime-evaluation.js +0 -1
- package/dist/contracts/runtime-memory.js +0 -1
- package/dist/contracts/runtime-observability.js +0 -1
- package/dist/contracts/runtime-requests.js +0 -1
- package/dist/contracts/runtime-scheduling.js +0 -1
- package/dist/contracts/runtime.js +1 -27
- package/dist/contracts/types.js +1 -3
- package/dist/contracts/workspace.js +0 -1
- package/dist/flow/build-flow-graph.js +1 -50
- package/dist/flow/export-mermaid.js +2 -464
- package/dist/flow/export-sequence-mermaid.js +2 -325
- package/dist/flow/flow-graph-normalization.js +1 -214
- package/dist/flow/flow-graph-runtime.js +1 -107
- package/dist/flow/flow-graph-upstream.js +1 -494
- package/dist/flow/index.js +1 -3
- package/dist/flow/types.js +0 -1
- package/dist/index.js +1 -5
- package/dist/init-project.js +1 -1
- package/dist/knowledge/config.js +1 -32
- package/dist/knowledge/contracts.js +0 -1
- package/dist/knowledge/index.js +1 -2
- package/dist/knowledge/module.js +12 -909
- package/dist/knowledge/procedural/config.js +1 -125
- package/dist/knowledge/procedural/index.js +1 -2
- package/dist/knowledge/procedural/manager.js +9 -345
- package/dist/mcp.js +1 -2
- package/dist/package-version.d.ts +2 -2
- package/dist/package-version.js +1 -2
- package/dist/persistence/file-store.js +3 -758
- package/dist/persistence/sqlite-request-context-store.js +5 -54
- package/dist/persistence/sqlite-request-queue-store.js +10 -108
- package/dist/persistence/sqlite-runtime.js +1 -86
- package/dist/persistence/sqlite-store.js +62 -810
- package/dist/persistence/types.js +0 -1
- package/dist/projections/presentation.js +37 -206
- package/dist/projections/request-events.js +2 -502
- package/dist/projections/upstream-events.js +1 -201
- package/dist/protocol/a2a/http-discovery.js +1 -178
- package/dist/protocol/a2a/http-rpc.js +6 -622
- package/dist/protocol/a2a/http.js +1 -138
- package/dist/protocol/a2a/task-state.js +3 -317
- package/dist/protocol/acp/client.js +8 -294
- package/dist/protocol/acp/harness-client.js +1 -218
- package/dist/protocol/acp/http.js +5 -130
- package/dist/protocol/acp/server.js +1 -310
- package/dist/protocol/acp/stdio.js +2 -69
- package/dist/protocol/ag-ui/http.js +3 -378
- package/dist/protocol/mcp/server.js +1 -428
- package/dist/resource/backend/workspace-scoped-backend.js +1 -319
- package/dist/resource/isolation.js +1 -237
- package/dist/resource/mcp/tool-support.js +3 -296
- package/dist/resource/mcp-tool-support.js +1 -2
- package/dist/resource/providers/resource-provider.js +1 -215
- package/dist/resource/resource-impl.js +1 -3
- package/dist/resource/resource-types.js +0 -1
- package/dist/resource/resource.js +1 -1
- package/dist/resource/sources.js +1 -247
- package/dist/resource/tools/function-tool-resolver.js +2 -272
- package/dist/runtime/adapter/compat/deepagent-compat.js +1 -29
- package/dist/runtime/adapter/compat/openai-compatible.js +1 -55
- package/dist/runtime/adapter/direct-builtin-utility.js +2 -90
- package/dist/runtime/adapter/flow/execution-context.js +1 -71
- package/dist/runtime/adapter/flow/invocation-flow.js +8 -411
- package/dist/runtime/adapter/flow/invoke-runtime.js +1 -20
- package/dist/runtime/adapter/flow/stream-runtime.js +11 -1395
- package/dist/runtime/adapter/invocation-result.js +2 -473
- package/dist/runtime/adapter/local-tool-invocation.js +6 -638
- package/dist/runtime/adapter/middleware/context-hygiene.js +1 -83
- package/dist/runtime/adapter/middleware-assembly.js +5 -477
- package/dist/runtime/adapter/model/invocation-request.js +3 -183
- package/dist/runtime/adapter/model/message-assembly.js +1 -28
- package/dist/runtime/adapter/model/model-providers.js +23 -1089
- package/dist/runtime/adapter/model/prompted-json-tool-call-capture.js +1 -40
- package/dist/runtime/adapter/model/prompted-json-tool-policy.js +1 -22
- package/dist/runtime/adapter/resilience.js +1 -104
- package/dist/runtime/adapter/runtime-adapter-support.js +3 -141
- package/dist/runtime/adapter/runtime-shell.js +5 -166
- package/dist/runtime/adapter/stream-event-projection.js +2 -622
- package/dist/runtime/adapter/stream-text-consumption.js +1 -18
- package/dist/runtime/adapter/terminal-status.js +2 -67
- package/dist/runtime/adapter/tool/builtin-middleware-tools.js +6 -627
- package/dist/runtime/adapter/tool/declared-middleware.js +1 -154
- package/dist/runtime/adapter/tool/interrupt-policy.js +1 -34
- package/dist/runtime/adapter/tool/provider-tool.js +1 -25
- package/dist/runtime/adapter/tool/resolved-tool.js +1 -225
- package/dist/runtime/adapter/tool/tool-arguments.js +3 -486
- package/dist/runtime/adapter/tool/tool-hitl.js +1 -346
- package/dist/runtime/adapter/tool/tool-name-mapping.js +1 -128
- package/dist/runtime/adapter/tool/tool-output-artifacts.js +2 -88
- package/dist/runtime/adapter/tool/tool-replay.js +1 -37
- package/dist/runtime/adapter/tool-resolution.js +1 -86
- package/dist/runtime/adapter/upstream-configurable-keys.js +1 -2
- package/dist/runtime/agent-runtime-adapter.js +60 -2338
- package/dist/runtime/agent-runtime-assembly.js +7 -249
- package/dist/runtime/env/runtime-env.js +1 -62
- package/dist/runtime/harness/background-runtime.js +1 -8
- package/dist/runtime/harness/bindings.js +1 -58
- package/dist/runtime/harness/events/event-bus.js +1 -16
- package/dist/runtime/harness/events/event-sink.js +1 -61
- package/dist/runtime/harness/events/events.js +1 -80
- package/dist/runtime/harness/events/listener-runtime.js +1 -13
- package/dist/runtime/harness/events/runtime-event-operations.js +1 -9
- package/dist/runtime/harness/events/streaming.js +1 -100
- package/dist/runtime/harness/events/timeline.js +1 -52
- package/dist/runtime/harness/public-shapes.js +1 -186
- package/dist/runtime/harness/run/artifact-paths.js +1 -15
- package/dist/runtime/harness/run/governance.js +1 -295
- package/dist/runtime/harness/run/helpers.js +1 -71
- package/dist/runtime/harness/run/inspection.js +1 -409
- package/dist/runtime/harness/run/operator-overview.js +1 -80
- package/dist/runtime/harness/run/queue-diagnostics.js +1 -15
- package/dist/runtime/harness/run/recovery.js +1 -162
- package/dist/runtime/harness/run/resources.js +1 -60
- package/dist/runtime/harness/run/resume.js +1 -56
- package/dist/runtime/harness/run/routing.js +1 -48
- package/dist/runtime/harness/run/run-lifecycle.js +1 -66
- package/dist/runtime/harness/run/run-operations.js +1 -217
- package/dist/runtime/harness/run/run-queue.js +1 -43
- package/dist/runtime/harness/run/run-slot-acquisition.js +1 -157
- package/dist/runtime/harness/run/session-records.js +1 -97
- package/dist/runtime/harness/run/start-run.js +1 -120
- package/dist/runtime/harness/run/startup-runtime.js +1 -69
- package/dist/runtime/harness/run/stream-run.js +8 -1418
- package/dist/runtime/harness/run/surface-semantics.js +1 -79
- package/dist/runtime/harness/runtime-defaults.js +1 -39
- package/dist/runtime/harness/system/boundary-analysis.js +1 -234
- package/dist/runtime/harness/system/health-monitor.js +1 -258
- package/dist/runtime/harness/system/inventory.js +1 -129
- package/dist/runtime/harness/system/mem0-ingestion-sync.js +5 -345
- package/dist/runtime/harness/system/policy-engine.js +1 -175
- package/dist/runtime/harness/system/runtime-memory-candidates.js +4 -110
- package/dist/runtime/harness/system/runtime-memory-consolidation.js +1 -51
- package/dist/runtime/harness/system/runtime-memory-manager.js +10 -693
- package/dist/runtime/harness/system/runtime-memory-policy.js +1 -155
- package/dist/runtime/harness/system/runtime-memory-records.js +11 -577
- package/dist/runtime/harness/system/runtime-memory-sync.js +5 -206
- package/dist/runtime/harness/system/session-memory-sync.js +3 -113
- package/dist/runtime/harness/system/skill-requirements.js +1 -112
- package/dist/runtime/harness/system/store.js +9 -365
- package/dist/runtime/harness/tool-gateway/index.js +1 -2
- package/dist/runtime/harness/tool-gateway/policy.js +1 -45
- package/dist/runtime/harness/tool-gateway/validation.js +1 -176
- package/dist/runtime/harness/tool-schema.js +1 -3
- package/dist/runtime/harness.js +3 -1490
- package/dist/runtime/index.js +1 -3
- package/dist/runtime/layout/runtime-layout.js +1 -31
- package/dist/runtime/maintenance/checkpoint-maintenance.js +2 -178
- package/dist/runtime/maintenance/file-checkpoint-saver.js +1 -106
- package/dist/runtime/maintenance/runtime-record-maintenance.js +2 -169
- package/dist/runtime/maintenance/sqlite-checkpoint-saver.js +4 -289
- package/dist/runtime/parsing/output-content.js +10 -550
- package/dist/runtime/parsing/output-parsing.js +1 -4
- package/dist/runtime/parsing/output-recovery.js +3 -213
- package/dist/runtime/parsing/output-tool-args.js +7 -663
- package/dist/runtime/parsing/stream-event-parsing.js +3 -362
- package/dist/runtime/prompts/runtime-prompts.js +4 -73
- package/dist/runtime/scheduling/system-schedule-manager.js +11 -532
- package/dist/runtime/skills/skill-metadata.js +1 -197
- package/dist/runtime/startup-tracing.js +2 -37
- package/dist/runtime/support/compiled-binding.js +1 -290
- package/dist/runtime/support/embedding-models.js +1 -118
- package/dist/runtime/support/harness-support.js +5 -137
- package/dist/runtime/support/llamaindex.js +1 -108
- package/dist/runtime/support/runtime-adapter-options.js +1 -29
- package/dist/runtime/support/runtime-factories.js +1 -51
- package/dist/runtime/support/vector-stores.js +9 -270
- package/dist/scaffold/init-project.js +54 -233
- package/dist/tooling/extensions.js +1 -311
- package/dist/tooling/module-loader.js +1 -55
- package/dist/tools.js +1 -176
- package/dist/utils/agent-display.js +1 -18
- package/dist/utils/bundled-text.js +4 -39
- package/dist/utils/compiled-binding.js +1 -33
- package/dist/utils/fs.js +2 -45
- package/dist/utils/id.js +1 -9
- package/dist/utils/message-content.js +1 -30
- package/dist/utils/object.js +1 -6
- package/dist/workspace/agent-binding-compiler.js +3 -613
- package/dist/workspace/compile.js +1 -472
- package/dist/workspace/framework-contract-validation.js +2 -322
- package/dist/workspace/index.js +1 -1
- package/dist/workspace/object-loader-paths.js +1 -71
- package/dist/workspace/object-loader-readers.js +1 -187
- package/dist/workspace/object-loader.js +1 -754
- package/dist/workspace/resource-compilers.js +1 -374
- package/dist/workspace/support/agent-capabilities.js +1 -37
- package/dist/workspace/support/agent-execution-config.js +1 -44
- package/dist/workspace/support/discovery.js +1 -147
- package/dist/workspace/support/source-collectors.js +1 -30
- package/dist/workspace/support/source-protocols.js +2 -192
- package/dist/workspace/support/workspace-ref-utils.js +1 -362
- package/dist/workspace/tool-hydration.js +1 -280
- package/dist/workspace/validate.js +1 -99
- package/dist/workspace/yaml-object-reader.js +1 -285
- package/package.json +7 -3
|
@@ -1,346 +1 @@
|
|
|
1
|
-
import
|
|
2
|
-
import path from "node:path";
|
|
3
|
-
import { validateToolGatewayInput } from "../../harness/tool-gateway/index.js";
|
|
4
|
-
const DEDUPED_REMOTE_TOOL_NAMES = new Set(["builtin.fetch_url", "builtin.web_search"]);
|
|
5
|
-
const PATH_LIKE_INPUT_KEYS = new Set(["path", "root", "dir", "directory", "cwd"]);
|
|
6
|
-
const ROOT_SCOPING_INPUT_KEYS = new Set(["root", "dir", "directory", "cwd"]);
|
|
7
|
-
const SENSITIVE_MEMORY_LEVELS = new Set(["high", "sensitive", "restricted", "secret", "confidential"]);
|
|
8
|
-
const NON_THREAD_MEMORY_SCOPES = new Set(["workspace", "agent", "user", "project"]);
|
|
9
|
-
function asRecord(value) {
|
|
10
|
-
return typeof value === "object" && value !== null && !Array.isArray(value)
|
|
11
|
-
? value
|
|
12
|
-
: undefined;
|
|
13
|
-
}
|
|
14
|
-
function asString(value) {
|
|
15
|
-
return typeof value === "string" && value.trim().length > 0 ? value.trim() : undefined;
|
|
16
|
-
}
|
|
17
|
-
function readApprovalDecisionMode(value) {
|
|
18
|
-
if (value === "none" ||
|
|
19
|
-
value === "manual" ||
|
|
20
|
-
value === "auto-approve" ||
|
|
21
|
-
value === "auto-reject" ||
|
|
22
|
-
value === "deny-and-continue") {
|
|
23
|
-
return value;
|
|
24
|
-
}
|
|
25
|
-
return undefined;
|
|
26
|
-
}
|
|
27
|
-
function readApprovalOverride(compiledTool) {
|
|
28
|
-
if (compiledTool.config?.requireApproval === true) {
|
|
29
|
-
return true;
|
|
30
|
-
}
|
|
31
|
-
const approval = asRecord(compiledTool.config?.approval);
|
|
32
|
-
if (approval?.required === true || approval?.enabled === true) {
|
|
33
|
-
return true;
|
|
34
|
-
}
|
|
35
|
-
const mcpConfig = asRecord(compiledTool.config?.mcp);
|
|
36
|
-
return mcpConfig?.requireApproval === true || mcpConfig?.approvalRequired === true;
|
|
37
|
-
}
|
|
38
|
-
function requiresApprovalForSensitiveMemoryWrite(compiledTool) {
|
|
39
|
-
const memoryConfig = asRecord(compiledTool.config?.memory);
|
|
40
|
-
if (memoryConfig?.enabled !== true) {
|
|
41
|
-
return false;
|
|
42
|
-
}
|
|
43
|
-
if (memoryConfig.requireApproval === true || memoryConfig.approvalRequired === true) {
|
|
44
|
-
return true;
|
|
45
|
-
}
|
|
46
|
-
const scope = asString(memoryConfig.scope)?.toLowerCase() ?? "session";
|
|
47
|
-
const sensitivity = asString(memoryConfig.sensitivity)?.toLowerCase();
|
|
48
|
-
if (sensitivity && SENSITIVE_MEMORY_LEVELS.has(sensitivity)) {
|
|
49
|
-
return true;
|
|
50
|
-
}
|
|
51
|
-
return NON_THREAD_MEMORY_SCOPES.has(scope) && sensitivity !== "public" && sensitivity !== "low";
|
|
52
|
-
}
|
|
53
|
-
function requiresApprovalForHighRiskMcpWrite(compiledTool) {
|
|
54
|
-
if (compiledTool.type !== "mcp") {
|
|
55
|
-
return false;
|
|
56
|
-
}
|
|
57
|
-
if (readApprovalOverride(compiledTool)) {
|
|
58
|
-
return true;
|
|
59
|
-
}
|
|
60
|
-
const mcpConfig = asRecord(compiledTool.config?.mcp);
|
|
61
|
-
const mcpServer = asRecord(compiledTool.config?.mcpServer);
|
|
62
|
-
const access = asString(mcpServer?.access ?? mcpConfig?.access);
|
|
63
|
-
const approvalPolicy = asString(mcpServer?.approvalPolicy ?? mcpConfig?.approvalPolicy);
|
|
64
|
-
if (approvalPolicy === "always") {
|
|
65
|
-
return true;
|
|
66
|
-
}
|
|
67
|
-
if (approvalPolicy === "never") {
|
|
68
|
-
return false;
|
|
69
|
-
}
|
|
70
|
-
return access === "read-write";
|
|
71
|
-
}
|
|
72
|
-
function matchesGovernanceToolPolicy(rule, compiledTool) {
|
|
73
|
-
const match = asRecord(rule.match) ?? rule;
|
|
74
|
-
const toolName = asString(match.toolName);
|
|
75
|
-
const category = asString(match.category);
|
|
76
|
-
const toolType = asString(match.toolType);
|
|
77
|
-
const compiledCategory = compiledTool.type === "mcp"
|
|
78
|
-
? "mcp"
|
|
79
|
-
: compiledTool.type === "backend"
|
|
80
|
-
? "backend"
|
|
81
|
-
: compiledTool.type === "provider"
|
|
82
|
-
? "provider-native"
|
|
83
|
-
: "local";
|
|
84
|
-
return (!toolName || toolName === compiledTool.name)
|
|
85
|
-
&& (!category || category === compiledCategory)
|
|
86
|
-
&& (!toolType || toolType === compiledTool.type);
|
|
87
|
-
}
|
|
88
|
-
function readToolApprovalModeFromGovernance(compiledTool, binding) {
|
|
89
|
-
const governance = asRecord(binding?.harnessRuntime.governance);
|
|
90
|
-
const overrides = Array.isArray(governance?.toolPolicies) ? governance.toolPolicies : [];
|
|
91
|
-
for (const rule of overrides) {
|
|
92
|
-
const typedRule = asRecord(rule);
|
|
93
|
-
if (!typedRule || !matchesGovernanceToolPolicy(typedRule, compiledTool)) {
|
|
94
|
-
continue;
|
|
95
|
-
}
|
|
96
|
-
const overrideMode = readApprovalDecisionMode(typedRule.decisionMode ?? typedRule.approvalMode);
|
|
97
|
-
if (overrideMode) {
|
|
98
|
-
return overrideMode;
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
return undefined;
|
|
102
|
-
}
|
|
103
|
-
function readToolApprovalModeFromConfig(compiledTool) {
|
|
104
|
-
const approval = asRecord(compiledTool.config?.approval);
|
|
105
|
-
return readApprovalDecisionMode(approval?.mode ??
|
|
106
|
-
approval?.decisionMode ??
|
|
107
|
-
compiledTool.config?.approvalMode);
|
|
108
|
-
}
|
|
109
|
-
export function resolveToolApprovalDecisionMode(compiledTool, binding) {
|
|
110
|
-
if (compiledTool.hitl?.enabled === true) {
|
|
111
|
-
return "manual";
|
|
112
|
-
}
|
|
113
|
-
const governanceMode = readToolApprovalModeFromGovernance(compiledTool, binding);
|
|
114
|
-
if (governanceMode) {
|
|
115
|
-
return governanceMode;
|
|
116
|
-
}
|
|
117
|
-
const configuredMode = readToolApprovalModeFromConfig(compiledTool);
|
|
118
|
-
if (configuredMode) {
|
|
119
|
-
return configuredMode;
|
|
120
|
-
}
|
|
121
|
-
if (readApprovalOverride(compiledTool) || requiresApprovalForSensitiveMemoryWrite(compiledTool) || requiresApprovalForHighRiskMcpWrite(compiledTool)) {
|
|
122
|
-
return "manual";
|
|
123
|
-
}
|
|
124
|
-
return "none";
|
|
125
|
-
}
|
|
126
|
-
export function toolRequiresRuntimeApproval(compiledTool) {
|
|
127
|
-
return resolveToolApprovalDecisionMode(compiledTool) === "manual";
|
|
128
|
-
}
|
|
129
|
-
function toolApprovalReason(compiledTool) {
|
|
130
|
-
if (compiledTool.hitl?.enabled === true) {
|
|
131
|
-
return undefined;
|
|
132
|
-
}
|
|
133
|
-
if (requiresApprovalForSensitiveMemoryWrite(compiledTool)) {
|
|
134
|
-
return "sensitive-memory-write";
|
|
135
|
-
}
|
|
136
|
-
if (requiresApprovalForHighRiskMcpWrite(compiledTool)) {
|
|
137
|
-
return "high-risk-mcp-write";
|
|
138
|
-
}
|
|
139
|
-
if (readApprovalOverride(compiledTool)) {
|
|
140
|
-
return "runtime-policy";
|
|
141
|
-
}
|
|
142
|
-
return undefined;
|
|
143
|
-
}
|
|
144
|
-
function toInputPreview(input) {
|
|
145
|
-
if (typeof input === "object" && input && !Array.isArray(input)) {
|
|
146
|
-
return input;
|
|
147
|
-
}
|
|
148
|
-
return { input };
|
|
149
|
-
}
|
|
150
|
-
function resolveApprovedInput(originalInput, resumeValue) {
|
|
151
|
-
if (resumeValue === "reject") {
|
|
152
|
-
return Symbol.for("agent-harness.hitl.reject");
|
|
153
|
-
}
|
|
154
|
-
if (typeof resumeValue === "object" && resumeValue && "decision" in resumeValue) {
|
|
155
|
-
const typed = resumeValue;
|
|
156
|
-
if (typed.decision === "reject") {
|
|
157
|
-
return Symbol.for("agent-harness.hitl.reject");
|
|
158
|
-
}
|
|
159
|
-
if (typed.decision === "edit" && typed.editedInput !== undefined) {
|
|
160
|
-
return typed.editedInput;
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
return originalInput;
|
|
164
|
-
}
|
|
165
|
-
export function wrapToolForHumanInTheLoop(resolvedTool, compiledTool, binding, interruptFn = interrupt) {
|
|
166
|
-
const decisionMode = resolveToolApprovalDecisionMode(compiledTool, binding);
|
|
167
|
-
if (decisionMode === "none" || typeof resolvedTool !== "object" || resolvedTool === null) {
|
|
168
|
-
return resolvedTool;
|
|
169
|
-
}
|
|
170
|
-
const target = resolvedTool;
|
|
171
|
-
const runWithApproval = async (input, config, invokeOriginal) => {
|
|
172
|
-
const gateway = validateToolGatewayInput({
|
|
173
|
-
toolName: compiledTool.name,
|
|
174
|
-
schema: target.schema,
|
|
175
|
-
args: input,
|
|
176
|
-
requiresApproval: true,
|
|
177
|
-
});
|
|
178
|
-
if (!gateway.ok) {
|
|
179
|
-
return gateway.error;
|
|
180
|
-
}
|
|
181
|
-
if (decisionMode === "auto-approve") {
|
|
182
|
-
return invokeOriginal(gateway.input, config);
|
|
183
|
-
}
|
|
184
|
-
if (decisionMode === "auto-reject" || decisionMode === "deny-and-continue") {
|
|
185
|
-
return "Tool execution denied by runtime policy.";
|
|
186
|
-
}
|
|
187
|
-
const resumed = interruptFn({
|
|
188
|
-
toolName: compiledTool.name,
|
|
189
|
-
toolId: compiledTool.id,
|
|
190
|
-
allowedDecisions: compiledTool.hitl?.allow ?? ["approve", "edit", "reject"],
|
|
191
|
-
inputPreview: toInputPreview(gateway.input),
|
|
192
|
-
decisionMode,
|
|
193
|
-
...(toolApprovalReason(compiledTool) ? { approvalReason: toolApprovalReason(compiledTool) } : {}),
|
|
194
|
-
});
|
|
195
|
-
const approvedInput = resolveApprovedInput(gateway.input, resumed);
|
|
196
|
-
if (approvedInput === Symbol.for("agent-harness.hitl.reject")) {
|
|
197
|
-
return "Tool execution rejected by human reviewer.";
|
|
198
|
-
}
|
|
199
|
-
return invokeOriginal(approvedInput, config);
|
|
200
|
-
};
|
|
201
|
-
return new Proxy(target, {
|
|
202
|
-
get(obj, prop, receiver) {
|
|
203
|
-
const value = Reflect.get(obj, prop, receiver);
|
|
204
|
-
if (prop === "invoke" && typeof value === "function") {
|
|
205
|
-
return (input, config) => runWithApproval(input, config, (approvedInput, approvedConfig) => obj.invoke(approvedInput, approvedConfig));
|
|
206
|
-
}
|
|
207
|
-
if (prop === "call" && typeof value === "function") {
|
|
208
|
-
return (input, config) => runWithApproval(input, config, (approvedInput, approvedConfig) => obj.call(approvedInput, approvedConfig));
|
|
209
|
-
}
|
|
210
|
-
if (prop === "func" && typeof value === "function") {
|
|
211
|
-
return (input, config) => runWithApproval(input, config, (approvedInput, approvedConfig) => obj.func(approvedInput, approvedConfig));
|
|
212
|
-
}
|
|
213
|
-
return value;
|
|
214
|
-
},
|
|
215
|
-
});
|
|
216
|
-
}
|
|
217
|
-
function stableStringify(value) {
|
|
218
|
-
if (value === null || typeof value !== "object") {
|
|
219
|
-
return JSON.stringify(value);
|
|
220
|
-
}
|
|
221
|
-
if (Array.isArray(value)) {
|
|
222
|
-
return `[${value.map((item) => stableStringify(item)).join(",")}]`;
|
|
223
|
-
}
|
|
224
|
-
const entries = Object.entries(value).sort(([left], [right]) => left.localeCompare(right));
|
|
225
|
-
return `{${entries.map(([key, nested]) => `${JSON.stringify(key)}:${stableStringify(nested)}`).join(",")}}`;
|
|
226
|
-
}
|
|
227
|
-
function createDedupedToolKey(toolName, input) {
|
|
228
|
-
return `${toolName}:${stableStringify(input)}`;
|
|
229
|
-
}
|
|
230
|
-
function wrapToolWithDedupe(resolvedTool, compiledTool) {
|
|
231
|
-
if (!DEDUPED_REMOTE_TOOL_NAMES.has(compiledTool.name) || typeof resolvedTool !== "object" || resolvedTool === null) {
|
|
232
|
-
return resolvedTool;
|
|
233
|
-
}
|
|
234
|
-
const target = resolvedTool;
|
|
235
|
-
const cache = new Map();
|
|
236
|
-
const dedupeCall = (input, config, invokeOriginal) => {
|
|
237
|
-
const cacheKey = createDedupedToolKey(compiledTool.name, input);
|
|
238
|
-
const cached = cache.get(cacheKey);
|
|
239
|
-
if (cached) {
|
|
240
|
-
return cached;
|
|
241
|
-
}
|
|
242
|
-
const pending = Promise.resolve(invokeOriginal(input, config)).catch((error) => {
|
|
243
|
-
cache.delete(cacheKey);
|
|
244
|
-
throw error;
|
|
245
|
-
});
|
|
246
|
-
cache.set(cacheKey, pending);
|
|
247
|
-
return pending;
|
|
248
|
-
};
|
|
249
|
-
return new Proxy(target, {
|
|
250
|
-
get(obj, prop, receiver) {
|
|
251
|
-
const value = Reflect.get(obj, prop, receiver);
|
|
252
|
-
if (prop === "invoke" && typeof value === "function") {
|
|
253
|
-
return (input, config) => dedupeCall(input, config, (approvedInput, approvedConfig) => obj.invoke(approvedInput, approvedConfig));
|
|
254
|
-
}
|
|
255
|
-
if (prop === "call" && typeof value === "function") {
|
|
256
|
-
return (input, config) => dedupeCall(input, config, (approvedInput, approvedConfig) => obj.call(approvedInput, approvedConfig));
|
|
257
|
-
}
|
|
258
|
-
if (prop === "func" && typeof value === "function") {
|
|
259
|
-
return (input, config) => dedupeCall(input, config, (approvedInput, approvedConfig) => obj.func(approvedInput, approvedConfig));
|
|
260
|
-
}
|
|
261
|
-
return value;
|
|
262
|
-
},
|
|
263
|
-
});
|
|
264
|
-
}
|
|
265
|
-
function shouldConstrainWorkspacePaths(compiledTool) {
|
|
266
|
-
return compiledTool.type !== "mcp";
|
|
267
|
-
}
|
|
268
|
-
function guardWorkspaceBoundToolInput(input, compiledTool, binding) {
|
|
269
|
-
const workspaceRoot = binding?.harnessRuntime.workspaceRoot;
|
|
270
|
-
if (!workspaceRoot || typeof input !== "object" || !input || Array.isArray(input) || !shouldConstrainWorkspacePaths(compiledTool)) {
|
|
271
|
-
return input;
|
|
272
|
-
}
|
|
273
|
-
const record = { ...input };
|
|
274
|
-
for (const [key, value] of Object.entries(record)) {
|
|
275
|
-
if (!PATH_LIKE_INPUT_KEYS.has(key) || typeof value !== "string") {
|
|
276
|
-
continue;
|
|
277
|
-
}
|
|
278
|
-
const candidate = value.trim();
|
|
279
|
-
if (!candidate || /^[a-z]+:\/\//i.test(candidate)) {
|
|
280
|
-
continue;
|
|
281
|
-
}
|
|
282
|
-
const absolute = path.resolve(workspaceRoot, candidate);
|
|
283
|
-
const relative = path.relative(workspaceRoot, absolute);
|
|
284
|
-
if (relative.startsWith("..") || path.isAbsolute(relative)) {
|
|
285
|
-
if (ROOT_SCOPING_INPUT_KEYS.has(key)) {
|
|
286
|
-
record[key] = workspaceRoot;
|
|
287
|
-
continue;
|
|
288
|
-
}
|
|
289
|
-
throw new Error(`Tool ${compiledTool.name} input ${key} resolves outside the workspace root`);
|
|
290
|
-
}
|
|
291
|
-
}
|
|
292
|
-
return record;
|
|
293
|
-
}
|
|
294
|
-
export function wrapToolForExecution(resolvedTool, compiledTool, binding, interruptFn = interrupt) {
|
|
295
|
-
const guardedTool = typeof resolvedTool === "object" && resolvedTool !== null
|
|
296
|
-
? new Proxy(resolvedTool, {
|
|
297
|
-
get(obj, prop, receiver) {
|
|
298
|
-
const value = Reflect.get(obj, prop, receiver);
|
|
299
|
-
if (prop === "invoke" && typeof value === "function") {
|
|
300
|
-
return (input, config) => {
|
|
301
|
-
const gateway = validateToolGatewayInput({
|
|
302
|
-
toolName: compiledTool.name,
|
|
303
|
-
schema: obj.schema,
|
|
304
|
-
args: input,
|
|
305
|
-
requiresApproval: resolveToolApprovalDecisionMode(compiledTool, binding) !== "none",
|
|
306
|
-
});
|
|
307
|
-
if (!gateway.ok) {
|
|
308
|
-
return gateway.error;
|
|
309
|
-
}
|
|
310
|
-
return obj.invoke(guardWorkspaceBoundToolInput(gateway.input, compiledTool, binding), config);
|
|
311
|
-
};
|
|
312
|
-
}
|
|
313
|
-
if (prop === "call" && typeof value === "function") {
|
|
314
|
-
return (input, config) => {
|
|
315
|
-
const gateway = validateToolGatewayInput({
|
|
316
|
-
toolName: compiledTool.name,
|
|
317
|
-
schema: obj.schema,
|
|
318
|
-
args: input,
|
|
319
|
-
requiresApproval: resolveToolApprovalDecisionMode(compiledTool, binding) !== "none",
|
|
320
|
-
});
|
|
321
|
-
if (!gateway.ok) {
|
|
322
|
-
return gateway.error;
|
|
323
|
-
}
|
|
324
|
-
return obj.call(guardWorkspaceBoundToolInput(gateway.input, compiledTool, binding), config);
|
|
325
|
-
};
|
|
326
|
-
}
|
|
327
|
-
if (prop === "func" && typeof value === "function") {
|
|
328
|
-
return (input, config) => {
|
|
329
|
-
const gateway = validateToolGatewayInput({
|
|
330
|
-
toolName: compiledTool.name,
|
|
331
|
-
schema: obj.schema,
|
|
332
|
-
args: input,
|
|
333
|
-
requiresApproval: resolveToolApprovalDecisionMode(compiledTool, binding) !== "none",
|
|
334
|
-
});
|
|
335
|
-
if (!gateway.ok) {
|
|
336
|
-
return gateway.error;
|
|
337
|
-
}
|
|
338
|
-
return obj.func(guardWorkspaceBoundToolInput(gateway.input, compiledTool, binding), config);
|
|
339
|
-
};
|
|
340
|
-
}
|
|
341
|
-
return value;
|
|
342
|
-
},
|
|
343
|
-
})
|
|
344
|
-
: resolvedTool;
|
|
345
|
-
return wrapToolWithDedupe(wrapToolForHumanInTheLoop(guardedTool, compiledTool, binding, interruptFn), compiledTool);
|
|
346
|
-
}
|
|
1
|
+
import{interrupt as R}from"@langchain/langgraph";import m from"node:path";import{validateToolGatewayInput as l}from"../../harness/tool-gateway/index.js";const _=new Set(["builtin.fetch_url","builtin.web_search"]),I=new Set(["path","root","dir","directory","cwd"]),O=new Set(["root","dir","directory","cwd"]),q=new Set(["high","sensitive","restricted","secret","confidential"]),C=new Set(["workspace","agent","user","project"]);function d(e){return typeof e=="object"&&e!==null&&!Array.isArray(e)?e:void 0}function h(e){return typeof e=="string"&&e.trim().length>0?e.trim():void 0}function k(e){if(e==="none"||e==="manual"||e==="auto-approve"||e==="auto-reject"||e==="deny-and-continue")return e}function A(e){if(e.config?.requireApproval===!0)return!0;const r=d(e.config?.approval);if(r?.required===!0||r?.enabled===!0)return!0;const t=d(e.config?.mcp);return t?.requireApproval===!0||t?.approvalRequired===!0}function P(e){const r=d(e.config?.memory);if(r?.enabled!==!0)return!1;if(r.requireApproval===!0||r.approvalRequired===!0)return!0;const t=h(r.scope)?.toLowerCase()??"session",n=h(r.sensitivity)?.toLowerCase();return n&&q.has(n)?!0:C.has(t)&&n!=="public"&&n!=="low"}function E(e){if(e.type!=="mcp")return!1;if(A(e))return!0;const r=d(e.config?.mcp),t=d(e.config?.mcpServer),n=h(t?.access??r?.access),c=h(t?.approvalPolicy??r?.approvalPolicy);return c==="always"?!0:c==="never"?!1:n==="read-write"}function b(e,r){const t=d(e.match)??e,n=h(t.toolName),c=h(t.category),o=h(t.toolType),u=r.type==="mcp"?"mcp":r.type==="backend"?"backend":r.type==="provider"?"provider-native":"local";return(!n||n===r.name)&&(!c||c===u)&&(!o||o===r.type)}function j(e,r){const t=d(r?.harnessRuntime.governance),n=Array.isArray(t?.toolPolicies)?t.toolPolicies:[];for(const c of n){const o=d(c);if(!o||!b(o,e))continue;const u=k(o.decisionMode??o.approvalMode);if(u)return u}}function x(e){const r=d(e.config?.approval);return k(r?.mode??r?.decisionMode??e.config?.approvalMode)}function w(e,r){if(e.hitl?.enabled===!0)return"manual";const t=j(e,r);if(t)return t;const n=x(e);return n||(A(e)||P(e)||E(e)?"manual":"none")}function U(e){return w(e)==="manual"}function N(e){if(e.hitl?.enabled!==!0){if(P(e))return"sensitive-memory-write";if(E(e))return"high-risk-mcp-write";if(A(e))return"runtime-policy"}}function D(e){return typeof e=="object"&&e&&!Array.isArray(e)?e:{input:e}}function $(e,r){if(r==="reject")return Symbol.for("agent-harness.hitl.reject");if(typeof r=="object"&&r&&"decision"in r){const t=r;if(t.decision==="reject")return Symbol.for("agent-harness.hitl.reject");if(t.decision==="edit"&&t.editedInput!==void 0)return t.editedInput}return e}function L(e,r,t,n=R){const c=w(r,t);if(c==="none"||typeof e!="object"||e===null)return e;const o=e,u=async(p,s,f)=>{const a=l({toolName:r.name,schema:o.schema,args:p,requiresApproval:!0});if(!a.ok)return a.error;if(c==="auto-approve")return f(a.input,s);if(c==="auto-reject"||c==="deny-and-continue")return"Tool execution denied by runtime policy.";const i=n({toolName:r.name,toolId:r.id,allowedDecisions:r.hitl?.allow??["approve","edit","reject"],inputPreview:D(a.input),decisionMode:c,...N(r)?{approvalReason:N(r)}:{}}),y=$(a.input,i);return y===Symbol.for("agent-harness.hitl.reject")?"Tool execution rejected by human reviewer.":f(y,s)};return new Proxy(o,{get(p,s,f){const a=Reflect.get(p,s,f);return s==="invoke"&&typeof a=="function"?(i,y)=>u(i,y,(v,g)=>p.invoke(v,g)):s==="call"&&typeof a=="function"?(i,y)=>u(i,y,(v,g)=>p.call(v,g)):s==="func"&&typeof a=="function"?(i,y)=>u(i,y,(v,g)=>p.func(v,g)):a}})}function S(e){return e===null||typeof e!="object"?JSON.stringify(e):Array.isArray(e)?`[${e.map(t=>S(t)).join(",")}]`:`{${Object.entries(e).sort(([t],[n])=>t.localeCompare(n)).map(([t,n])=>`${JSON.stringify(t)}:${S(n)}`).join(",")}}`}function W(e,r){return`${e}:${S(r)}`}function F(e,r){if(!_.has(r.name)||typeof e!="object"||e===null)return e;const t=e,n=new Map,c=(o,u,p)=>{const s=W(r.name,o),f=n.get(s);if(f)return f;const a=Promise.resolve(p(o,u)).catch(i=>{throw n.delete(s),i});return n.set(s,a),a};return new Proxy(t,{get(o,u,p){const s=Reflect.get(o,u,p);return u==="invoke"&&typeof s=="function"?(f,a)=>c(f,a,(i,y)=>o.invoke(i,y)):u==="call"&&typeof s=="function"?(f,a)=>c(f,a,(i,y)=>o.call(i,y)):u==="func"&&typeof s=="function"?(f,a)=>c(f,a,(i,y)=>o.func(i,y)):s}})}function K(e){return e.type!=="mcp"}function M(e,r,t){const n=t?.harnessRuntime.workspaceRoot;if(!n||typeof e!="object"||!e||Array.isArray(e)||!K(r))return e;const c={...e};for(const[o,u]of Object.entries(c)){if(!I.has(o)||typeof u!="string")continue;const p=u.trim();if(!p||/^[a-z]+:\/\//i.test(p))continue;const s=m.resolve(n,p),f=m.relative(n,s);if(f.startsWith("..")||m.isAbsolute(f)){if(O.has(o)){c[o]=n;continue}throw new Error(`Tool ${r.name} input ${o} resolves outside the workspace root`)}}return c}function J(e,r,t,n=R){const c=typeof e=="object"&&e!==null?new Proxy(e,{get(o,u,p){const s=Reflect.get(o,u,p);return u==="invoke"&&typeof s=="function"?(f,a)=>{const i=l({toolName:r.name,schema:o.schema,args:f,requiresApproval:w(r,t)!=="none"});return i.ok?o.invoke(M(i.input,r,t),a):i.error}:u==="call"&&typeof s=="function"?(f,a)=>{const i=l({toolName:r.name,schema:o.schema,args:f,requiresApproval:w(r,t)!=="none"});return i.ok?o.call(M(i.input,r,t),a):i.error}:u==="func"&&typeof s=="function"?(f,a)=>{const i=l({toolName:r.name,schema:o.schema,args:f,requiresApproval:w(r,t)!=="none"});return i.ok?o.func(M(i.input,r,t),a):i.error}:s}}):e;return F(L(c,r,t,n),r)}export{w as resolveToolApprovalDecisionMode,U as toolRequiresRuntimeApproval,J as wrapToolForExecution,L as wrapToolForHumanInTheLoop};
|
|
@@ -1,128 +1 @@
|
|
|
1
|
-
const
|
|
2
|
-
const PROVIDER_TOOL_CALL_PREFIXES = ["tool_call_", "tool_call.", "tool_call-", "tool.call_", "tool.call.", "tool-call-"];
|
|
3
|
-
export function stripProviderToolCallPrefix(toolName) {
|
|
4
|
-
const trimmed = toolName.trim();
|
|
5
|
-
for (const prefix of PROVIDER_TOOL_CALL_PREFIXES) {
|
|
6
|
-
if (trimmed.startsWith(prefix)) {
|
|
7
|
-
return trimmed.substring(prefix.length);
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
return null;
|
|
11
|
-
}
|
|
12
|
-
export function createProviderToolCallAliasName(toolName) {
|
|
13
|
-
return `tool_call_${sanitizeToolNameForModel(toolName)}`;
|
|
14
|
-
}
|
|
15
|
-
export function sanitizeToolNameForModel(name) {
|
|
16
|
-
const withoutNamespace = name.includes(".") ? name.split(".").at(-1) ?? name : name;
|
|
17
|
-
const sanitized = withoutNamespace
|
|
18
|
-
.replaceAll(".", "__")
|
|
19
|
-
.replace(/[^a-zA-Z0-9_-]+/g, "_")
|
|
20
|
-
.replace(/^_+|_+$/g, "");
|
|
21
|
-
return sanitized || "tool";
|
|
22
|
-
}
|
|
23
|
-
export function buildToolNameMapping(tools) {
|
|
24
|
-
const originalToModelFacing = new Map();
|
|
25
|
-
const modelFacingToOriginal = new Map();
|
|
26
|
-
const seen = new Set();
|
|
27
|
-
for (const tool of tools) {
|
|
28
|
-
const originalName = tool.name;
|
|
29
|
-
const baseName = MODEL_SAFE_TOOL_NAME_PATTERN.test(originalName) ? originalName : sanitizeToolNameForModel(originalName);
|
|
30
|
-
let candidate = baseName;
|
|
31
|
-
let suffix = 2;
|
|
32
|
-
while (seen.has(candidate)) {
|
|
33
|
-
candidate = `${baseName}_${suffix}`;
|
|
34
|
-
suffix += 1;
|
|
35
|
-
}
|
|
36
|
-
seen.add(candidate);
|
|
37
|
-
originalToModelFacing.set(originalName, candidate);
|
|
38
|
-
modelFacingToOriginal.set(candidate, originalName);
|
|
39
|
-
}
|
|
40
|
-
return { originalToModelFacing, modelFacingToOriginal };
|
|
41
|
-
}
|
|
42
|
-
export function createModelFacingToolNameCandidates(toolName) {
|
|
43
|
-
const trimmed = toolName.trim();
|
|
44
|
-
if (!trimmed) {
|
|
45
|
-
return [];
|
|
46
|
-
}
|
|
47
|
-
const lastSegment = trimmed.split(".").at(-1) ?? "";
|
|
48
|
-
const candidates = new Set([
|
|
49
|
-
trimmed,
|
|
50
|
-
sanitizeToolNameForModel(trimmed),
|
|
51
|
-
lastSegment,
|
|
52
|
-
lastSegment.trim() ? sanitizeToolNameForModel(lastSegment) : "",
|
|
53
|
-
trimmed.replace(/[^a-zA-Z0-9_]+/g, ""),
|
|
54
|
-
]);
|
|
55
|
-
candidates.delete("");
|
|
56
|
-
return [...candidates];
|
|
57
|
-
}
|
|
58
|
-
export function createModelFacingToolNameLookupCandidates(toolName) {
|
|
59
|
-
const trimmed = toolName.trim();
|
|
60
|
-
if (!trimmed) {
|
|
61
|
-
return [];
|
|
62
|
-
}
|
|
63
|
-
const candidates = new Set();
|
|
64
|
-
const add = (value) => {
|
|
65
|
-
for (const candidate of createModelFacingToolNameCandidates(value)) {
|
|
66
|
-
candidates.add(candidate);
|
|
67
|
-
}
|
|
68
|
-
};
|
|
69
|
-
const addSuffixStripped = (value) => {
|
|
70
|
-
const stripped = value.includes(".") ? value.split(".").at(-1) ?? value : value;
|
|
71
|
-
if (stripped !== value) {
|
|
72
|
-
add(stripped);
|
|
73
|
-
}
|
|
74
|
-
};
|
|
75
|
-
const addProviderPrefixStripped = (value) => {
|
|
76
|
-
const stripped = stripProviderToolCallPrefix(value);
|
|
77
|
-
if (stripped) {
|
|
78
|
-
add(stripped);
|
|
79
|
-
addSuffixStripped(stripped);
|
|
80
|
-
}
|
|
81
|
-
};
|
|
82
|
-
add(trimmed);
|
|
83
|
-
addProviderPrefixStripped(trimmed);
|
|
84
|
-
if (trimmed.startsWith("functions.")) {
|
|
85
|
-
const stripped = trimmed.substring("functions.".length);
|
|
86
|
-
add(stripped);
|
|
87
|
-
addSuffixStripped(stripped);
|
|
88
|
-
addProviderPrefixStripped(stripped);
|
|
89
|
-
}
|
|
90
|
-
else if (trimmed.startsWith("function.")) {
|
|
91
|
-
const stripped = trimmed.substring("function.".length);
|
|
92
|
-
add(stripped);
|
|
93
|
-
addSuffixStripped(stripped);
|
|
94
|
-
addProviderPrefixStripped(stripped);
|
|
95
|
-
}
|
|
96
|
-
else {
|
|
97
|
-
addSuffixStripped(trimmed);
|
|
98
|
-
}
|
|
99
|
-
return [...candidates];
|
|
100
|
-
}
|
|
101
|
-
export function resolveModelFacingToolName(toolName, mapping, tools) {
|
|
102
|
-
const candidateNames = createModelFacingToolNameLookupCandidates(toolName);
|
|
103
|
-
const candidateSet = new Set(candidateNames);
|
|
104
|
-
for (const candidate of candidateNames) {
|
|
105
|
-
const mappedToolName = mapping.modelFacingToOriginal.get(candidate);
|
|
106
|
-
if (mappedToolName) {
|
|
107
|
-
return mappedToolName;
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
for (const candidate of candidateNames) {
|
|
111
|
-
const directMatch = tools.find((tool) => tool.name === candidate);
|
|
112
|
-
if (directMatch) {
|
|
113
|
-
return directMatch.name;
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
const candidateMatches = tools.filter((tool) => {
|
|
117
|
-
const modelFacingCandidates = createModelFacingToolNameCandidates(tool.name);
|
|
118
|
-
return modelFacingCandidates.some((candidate) => candidateSet.has(candidate));
|
|
119
|
-
});
|
|
120
|
-
if (candidateMatches.length === 1) {
|
|
121
|
-
return candidateMatches[0].name;
|
|
122
|
-
}
|
|
123
|
-
const providerPrefixStripped = stripProviderToolCallPrefix(toolName);
|
|
124
|
-
if (providerPrefixStripped) {
|
|
125
|
-
return sanitizeToolNameForModel(providerPrefixStripped);
|
|
126
|
-
}
|
|
127
|
-
return toolName;
|
|
128
|
-
}
|
|
1
|
+
const u=/^[a-zA-Z0-9_-]+$/,m=["tool_call_","tool_call.","tool_call-","tool.call_","tool.call.","tool-call-"];function f(o){const t=o.trim();for(const i of m)if(t.startsWith(i))return t.substring(i.length);return null}function _(o){return`tool_call_${l(o)}`}function l(o){return(o.includes(".")?o.split(".").at(-1)??o:o).replaceAll(".","__").replace(/[^a-zA-Z0-9_-]+/g,"_").replace(/^_+|_+$/g,"")||"tool"}function T(o){const t=new Map,i=new Map,a=new Set;for(const c of o){const r=c.name,e=u.test(r)?r:l(r);let n=e,s=2;for(;a.has(n);)n=`${e}_${s}`,s+=1;a.add(n),t.set(r,n),i.set(n,r)}return{originalToModelFacing:t,modelFacingToOriginal:i}}function p(o){const t=o.trim();if(!t)return[];const i=t.split(".").at(-1)??"",a=new Set([t,l(t),i,i.trim()?l(i):"",t.replace(/[^a-zA-Z0-9_]+/g,"")]);return a.delete(""),[...a]}function g(o){const t=o.trim();if(!t)return[];const i=new Set,a=e=>{for(const n of p(e))i.add(n)},c=e=>{const n=e.includes(".")?e.split(".").at(-1)??e:e;n!==e&&a(n)},r=e=>{const n=f(e);n&&(a(n),c(n))};if(a(t),r(t),t.startsWith("functions.")){const e=t.substring(10);a(e),c(e),r(e)}else if(t.startsWith("function.")){const e=t.substring(9);a(e),c(e),r(e)}else c(t);return[...i]}function h(o,t,i){const a=g(o),c=new Set(a);for(const n of a){const s=t.modelFacingToOriginal.get(n);if(s)return s}for(const n of a){const s=i.find(d=>d.name===n);if(s)return s.name}const r=i.filter(n=>p(n.name).some(d=>c.has(d)));if(r.length===1)return r[0].name;const e=f(o);return e?l(e):o}export{T as buildToolNameMapping,p as createModelFacingToolNameCandidates,g as createModelFacingToolNameLookupCandidates,_ as createProviderToolCallAliasName,h as resolveModelFacingToolName,l as sanitizeToolNameForModel,f as stripProviderToolCallPrefix};
|
|
@@ -1,88 +1,2 @@
|
|
|
1
|
-
function
|
|
2
|
-
|
|
3
|
-
? value
|
|
4
|
-
: undefined;
|
|
5
|
-
}
|
|
6
|
-
function toSerializedToolOutput(output) {
|
|
7
|
-
if (typeof output === "string") {
|
|
8
|
-
return output;
|
|
9
|
-
}
|
|
10
|
-
try {
|
|
11
|
-
return JSON.stringify(output, null, 2);
|
|
12
|
-
}
|
|
13
|
-
catch {
|
|
14
|
-
return String(output);
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
function truncatePreview(text, maxChars) {
|
|
18
|
-
if (text.length <= maxChars) {
|
|
19
|
-
return text;
|
|
20
|
-
}
|
|
21
|
-
return `${text.slice(0, Math.max(0, maxChars - 15))}\n...[truncated]`;
|
|
22
|
-
}
|
|
23
|
-
function sanitizeArtifactToolName(toolName) {
|
|
24
|
-
return toolName.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "") || "tool";
|
|
25
|
-
}
|
|
26
|
-
export function resolveToolRuntimeContext(toolConfig, fallback) {
|
|
27
|
-
const dynamic = asRecord(toolConfig?.toolRuntimeContext);
|
|
28
|
-
const configurable = asRecord(toolConfig?.configurable);
|
|
29
|
-
const fallbackRuntime = asRecord(asRecord(fallback)?.runtime);
|
|
30
|
-
const fallbackCurrent = asRecord(fallbackRuntime?.current);
|
|
31
|
-
const resolved = (dynamic ?? fallback);
|
|
32
|
-
const runtime = asRecord(resolved?.runtime);
|
|
33
|
-
if (!runtime) {
|
|
34
|
-
return resolved;
|
|
35
|
-
}
|
|
36
|
-
return {
|
|
37
|
-
...resolved,
|
|
38
|
-
runtime: {
|
|
39
|
-
...runtime,
|
|
40
|
-
current: {
|
|
41
|
-
...fallbackCurrent,
|
|
42
|
-
...asRecord(runtime.current),
|
|
43
|
-
...(typeof configurable?.thread_id === "string" ? { sessionId: configurable.thread_id } : {}),
|
|
44
|
-
...(typeof configurable?.request_id === "string" ? { requestId: configurable.request_id } : {}),
|
|
45
|
-
},
|
|
46
|
-
},
|
|
47
|
-
};
|
|
48
|
-
}
|
|
49
|
-
export async function maybePersistLargeToolOutput(input) {
|
|
50
|
-
const maxInlineChars = input.maxInlineChars ?? 12_000;
|
|
51
|
-
const previewChars = input.previewChars ?? 2_000;
|
|
52
|
-
const serialized = toSerializedToolOutput(input.output);
|
|
53
|
-
if (serialized.length <= maxInlineChars) {
|
|
54
|
-
return input.output;
|
|
55
|
-
}
|
|
56
|
-
const runtimeContext = input.toolRuntimeContext;
|
|
57
|
-
const runtime = runtimeContext?.runtime;
|
|
58
|
-
const sessionId = runtime?.current?.sessionId;
|
|
59
|
-
const requestId = runtime?.current?.requestId;
|
|
60
|
-
const createArtifact = runtime?.artifacts?.create;
|
|
61
|
-
const createdAt = new Date().toISOString();
|
|
62
|
-
const safeToolName = sanitizeArtifactToolName(input.toolName);
|
|
63
|
-
let artifact;
|
|
64
|
-
if (typeof createArtifact === "function" && typeof sessionId === "string" && typeof requestId === "string") {
|
|
65
|
-
artifact = await Promise.resolve(createArtifact({
|
|
66
|
-
sessionId,
|
|
67
|
-
requestId,
|
|
68
|
-
kind: "tool-output",
|
|
69
|
-
path: `artifacts/tool-output-${safeToolName}-${Date.now()}.json`,
|
|
70
|
-
createdAt,
|
|
71
|
-
content: {
|
|
72
|
-
toolName: input.toolName,
|
|
73
|
-
createdAt,
|
|
74
|
-
output: input.output,
|
|
75
|
-
},
|
|
76
|
-
}));
|
|
77
|
-
}
|
|
78
|
-
return {
|
|
79
|
-
truncated: true,
|
|
80
|
-
toolName: input.toolName,
|
|
81
|
-
message: artifact
|
|
82
|
-
? "Full tool output was stored as a request artifact because it exceeded the inline size limit."
|
|
83
|
-
: "Tool output exceeded the inline size limit and was truncated for runtime stability.",
|
|
84
|
-
originalSizeChars: serialized.length,
|
|
85
|
-
preview: truncatePreview(serialized, previewChars),
|
|
86
|
-
...(artifact ? { artifact } : {}),
|
|
87
|
-
};
|
|
88
|
-
}
|
|
1
|
+
function n(t){return typeof t=="object"&&t!==null&&!Array.isArray(t)?t:void 0}function m(t){if(typeof t=="string")return t;try{return JSON.stringify(t,null,2)}catch{return String(t)}}function g(t,r){return t.length<=r?t:`${t.slice(0,Math.max(0,r-15))}
|
|
2
|
+
...[truncated]`}function y(t){return t.toLowerCase().replace(/[^a-z0-9]+/g,"-").replace(/^-+|-+$/g,"")||"tool"}function h(t,r){const u=n(t?.toolRuntimeContext),e=n(t?.configurable),c=n(n(r)?.runtime),s=n(c?.current),o=u??r,i=n(o?.runtime);return i?{...o,runtime:{...i,current:{...s,...n(i.current),...typeof e?.thread_id=="string"?{sessionId:e.thread_id}:{},...typeof e?.request_id=="string"?{requestId:e.request_id}:{}}}}:o}async function p(t){const r=t.maxInlineChars??12e3,u=t.previewChars??2e3,e=m(t.output);if(e.length<=r)return t.output;const s=t.toolRuntimeContext?.runtime,o=s?.current?.sessionId,i=s?.current?.requestId,l=s?.artifacts?.create,f=new Date().toISOString(),d=y(t.toolName);let a;return typeof l=="function"&&typeof o=="string"&&typeof i=="string"&&(a=await Promise.resolve(l({sessionId:o,requestId:i,kind:"tool-output",path:`artifacts/tool-output-${d}-${Date.now()}.json`,createdAt:f,content:{toolName:t.toolName,createdAt:f,output:t.output}}))),{truncated:!0,toolName:t.toolName,message:a?"Full tool output was stored as a request artifact because it exceeded the inline size limit.":"Tool output exceeded the inline size limit and was truncated for runtime stability.",originalSizeChars:e.length,preview:g(e,u),...a?{artifact:a}:{}}}export{p as maybePersistLargeToolOutput,h as resolveToolRuntimeContext};
|
|
@@ -1,37 +1 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { isLangChainBinding } from "../../support/compiled-binding.js";
|
|
3
|
-
const DEEPAGENTS_LOCAL_REPLAY_COMPATIBILITY_TOOLS = new Set([
|
|
4
|
-
"write_todos",
|
|
5
|
-
"read_todos",
|
|
6
|
-
"ls",
|
|
7
|
-
"read_file",
|
|
8
|
-
"write_file",
|
|
9
|
-
"edit_file",
|
|
10
|
-
"glob",
|
|
11
|
-
"grep",
|
|
12
|
-
"execute",
|
|
13
|
-
]);
|
|
14
|
-
function isDeepAgentsLocalReplayCompatibilityTool(toolName) {
|
|
15
|
-
return DEEPAGENTS_LOCAL_REPLAY_COMPATIBILITY_TOOLS.has(toolName.trim().toLowerCase());
|
|
16
|
-
}
|
|
17
|
-
export function canReplayToolCallsLocally(binding, toolCalls, primaryTools, toolNameMapping, executableTools, builtinExecutableTools) {
|
|
18
|
-
if (toolCalls.length === 0) {
|
|
19
|
-
return false;
|
|
20
|
-
}
|
|
21
|
-
if (isLangChainBinding(binding)) {
|
|
22
|
-
return true;
|
|
23
|
-
}
|
|
24
|
-
return toolCalls.every((toolCall) => {
|
|
25
|
-
const resolvedToolName = resolveModelFacingToolName(toolCall.name, toolNameMapping, primaryTools);
|
|
26
|
-
if (!isDeepAgentsLocalReplayCompatibilityTool(toolCall.name)
|
|
27
|
-
&& !isDeepAgentsLocalReplayCompatibilityTool(resolvedToolName)) {
|
|
28
|
-
return false;
|
|
29
|
-
}
|
|
30
|
-
const builtinExecutable = builtinExecutableTools.get(toolCall.name) ??
|
|
31
|
-
builtinExecutableTools.get(resolvedToolName) ??
|
|
32
|
-
createModelFacingToolNameLookupCandidates(toolCall.name)
|
|
33
|
-
.map((candidate) => builtinExecutableTools.get(candidate))
|
|
34
|
-
.find((candidate) => candidate !== undefined);
|
|
35
|
-
return builtinExecutable !== undefined;
|
|
36
|
-
});
|
|
37
|
-
}
|
|
1
|
+
import{createModelFacingToolNameLookupCandidates as s,resolveModelFacingToolName as m}from"./tool-name-mapping.js";import{isLangChainBinding as u}from"../../support/compiled-binding.js";const c=new Set(["write_todos","read_todos","ls","read_file","write_file","edit_file","glob","grep","execute"]);function a(n){return c.has(n.trim().toLowerCase())}function _(n,i,d,f,l,t){return i.length===0?!1:u(n)?!0:i.every(e=>{const o=m(e.name,f,d);return!a(e.name)&&!a(o)?!1:(t.get(e.name)??t.get(o)??s(e.name).map(r=>t.get(r)).find(r=>r!==void 0))!==void 0})}export{_ as canReplayToolCallsLocally};
|