openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
205 lines (204 loc) • 7.15 kB
JavaScript
import { t as freezeJsonSnapshot } from "./immutable-data-Y7xVjZoy.mjs";
import { c as normalizeToolPolicyName } from "./tool-policy-shared-BHvBopvF.mjs";
import { a as getCodeModeExecBeforeHookMetadataForToolKind, o as isCodeModeControlTool } from "./code-mode-control-tools-2pBIuXDg.mjs";
import { g as wrapToolWithGatewayCallerIdentity } from "./gateway-caller-context-BNidAW8G.mjs";
import { s as attachInternalToolExecutionPreparer, v as getInternalToolExecutionPreparer } from "./internal-hooks-Ch3rqxs-.mjs";
import { i as getPluginToolMeta } from "./tool-metadata-rqUpUHlb.mjs";
import { r as markToolExecutionNotStarted } from "./tool-effect-receipt-BbbEwqBS.mjs";
import { a as copyAgentToolMetadata, s as getAgentToolActionDescriptor } from "./agent-tool-metadata-nrQgRaqv.mjs";
import { o as rewrapToolWithBeforeToolCallHook } from "./agent-tools.before-tool-call.wrapper-Dh3maIc2.mjs";
import { t as captureAgentWorkspaceReadiness } from "./workspace-readiness-ClUjI0tp.mjs";
import "./tool-result-error-B2GHe83l.mjs";
import { s as runWithAgentToolSourceExecutionScope, t as bindAgentToolSourceExecutionGuard } from "./agent-tool-source-execution-guard-CD-ezY7E.mjs";
import "./agent-tools.before-tool-call-CCkSq-k3.mjs";
import { n as wrapToolWithAbortSignal } from "./agent-tools.abort-BN-GwRlX.mjs";
import { isPathInsideWithRealpath } from "@openclaw/fs-safe/path";
//#region src/agents/harness/host-snapshot.ts
/** Host capabilities retain immutable input facts independently of caller mutation. */
function cloneHostSnapshot(e) {
return freezeJsonSnapshot(structuredClone(e));
}
//#endregion
//#region src/agents/harness/host-capability-workspace.ts
const WORKSPACE_TOOL_NAMES = Object.freeze([
"exec",
"process",
"read",
"write",
"edit",
"ls",
"grep",
"find",
"apply_patch",
"view_image",
"pdf"
]);
const workspaceToolNames = new Set(WORKSPACE_TOOL_NAMES);
const nativeWorkspaceToolNames = {
exec_command: "exec",
write_stdin: "process",
read_file: "read",
list_dir: "ls",
grep_files: "grep",
glob: "find"
};
/** Preserve omission and fence retained local-workspace preparation at both boundaries. */
function bindHostContinuationRetryCount(e, t) {
return e ? { prepareContinuationRetryCount: (n) => {
t();
e(n);
t();
} } : {};
}
function isWorkspaceToolName(e) {
const t = normalizeToolPolicyName(e);
return workspaceToolNames.has(nativeWorkspaceToolNames[t] ?? t);
}
function isWorkspaceTool(e) {
if (isCodeModeControlTool(e)) return false;
const t = getPluginToolMeta(e);
if (t) return t.workspaceAccess !== false;
const n = getAgentToolActionDescriptor(e)?.operation;
return n === "filesystem" || n === "process" || isWorkspaceToolName(e.name);
}
/** Bind one captured workspace owner to native admission and the host tool surface. */
function prepareHostWorkspaceReadiness(e, t) {
const n = captureAgentWorkspaceReadiness(e);
const r = n ? async (e) => {
e();
n.assertCurrent();
await n.waitUntilReady();
e();
n.assertCurrent();
} : void 0;
return {
readiness: n,
capability: r ? Object.freeze({
toolNames: WORKSPACE_TOOL_NAMES,
waitUntilReady: () => r(t)
}) : void 0,
beforeToolCall: r ? async (e, t) => {
if (!getCodeModeExecBeforeHookMetadataForToolKind({
toolKind: e.toolKind,
params: e.params
}) && isWorkspaceToolName(e.toolName)) await r(t);
} : void 0
};
}
function captureRequiredWorkspaceToolFloor(e, t, n) {
if (e.requireWorkspaceOnly !== true) return;
const r = {
workspaceDir: e.workspaceDir,
cwd: e.cwd ?? e.workspaceDir,
root: e.sandbox?.enabled ? e.sandbox.workspaceDir : e.sessionRoot ?? e.workspaceDir,
sandbox: e.sandbox ? Object.freeze({
...e.sandbox,
tools: cloneHostSnapshot(e.sandbox.tools)
}) : void 0,
permissionMode: e.permissionMode
};
if (!r.workspaceDir || !r.root) throw new Error("required workspace tool surface has no captured root");
const i = r.root;
const apply = (e) => {
const a = e?.sessionPermissionPolicy?.root;
if (a && a !== i && !isPathInsideWithRealpath(i, a)) throw new Error("tool permission root escapes the captured required workspace");
return {
config: n,
workspaceDir: r.workspaceDir,
cwd: r.cwd,
sandbox: r.sandbox,
requireWorkspaceOnly: true,
sessionPermissionPolicy: r.permissionMode || e?.sessionPermissionPolicy ? {
root: a ?? i,
mode: r.permissionMode === "read-only" || e?.sessionPermissionPolicy?.mode === "read-only" ? "read-only" : r.permissionMode ?? e.sessionPermissionPolicy.mode
} : void 0,
...t === "codex" ? {
exec: {
...e?.exec,
mode: "deny"
},
toolConstructionPlan: {
...e?.toolConstructionPlan ?? {
includeBaseCodingTools: e?.includeCoreTools !== false,
includeChannelTools: e?.includeCoreTools !== false,
includeOpenClawTools: e?.includeCoreTools !== false,
includePluginTools: true
},
includeShellTools: false
}
} : {}
};
};
return {
root: i,
apply
};
}
//#endregion
//#region src/agents/harness/host-bound-tool.ts
/** Assemble each host tool's policy, caller and readiness gates in their execution order. */
function bindHostToolSurface(e, t) {
t.assertActive();
return e.map((e) => bindAgentToolSourceExecutionGuard(e, t.assertActive)).map((e) => rewrapToolWithBeforeToolCallHook(e, t.hookContext)).map((e) => t.callerIdentity ? wrapToolWithGatewayCallerIdentity(e, t.callerIdentity) : e).map((e) => wrapToolWithAbortSignal(e, t.abortSignal)).map((e) => gateBoundTool(e, t.assertActive, t.observeResult, isWorkspaceTool(e) ? t.workspaceReadiness : void 0));
}
function gateBoundTool(e, t, n, r) {
const i = e.execute;
const a = getInternalToolExecutionPreparer(e);
if (!i && !a) return e;
const assertCurrent = () => runWithAgentToolSourceExecutionScope(t, async () => {
t();
r?.assertCurrent();
});
const acceptResult = (e) => {
(t.assertScopeCurrent ?? t)();
r?.assertCurrent();
n(e);
return e;
};
const awaitWorkspace = async () => {
await assertCurrent();
if (r) {
await r.waitUntilReady();
await assertCurrent();
}
};
const o = {
...e,
...i ? { execute: async (...e) => {
try {
if (r) await awaitWorkspace();
else await assertCurrent();
} catch (e) {
throw markToolExecutionNotStarted(e);
}
return acceptResult(await i(...e));
} } : {}
};
copyAgentToolMetadata(e, o, (e) => gateBoundTool(e, t, n, r));
if (a) attachInternalToolExecutionPreparer(o, async (e) => {
if (r) await awaitWorkspace();
else await assertCurrent();
const t = await a(e);
try {
await assertCurrent();
} catch (e) {
t.dispose();
throw e;
}
if (t.kind === "immediate") {
if (t.outcome.kind === "result") n(t.outcome.result);
return t;
}
return {
...t,
execute: async (e) => {
if (r) await awaitWorkspace();
else await assertCurrent();
return acceptResult(await t.execute(e));
}
};
});
return o;
}
//#endregion
export { isWorkspaceTool as a, captureRequiredWorkspaceToolFloor as i, gateBoundTool as n, prepareHostWorkspaceReadiness as o, bindHostContinuationRetryCount as r, cloneHostSnapshot as s, bindHostToolSurface as t };