UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

7 lines • 5.72 kB
import{t as freezeJsonSnapshot}from"./immutable-data-CyaF4H_G.mjs";import{c as normalizeToolPolicyName}from"./tool-policy-shared-KKJW93v9.mjs";import{a as getCodeModeExecBeforeHookMetadataForToolKind,o as isCodeModeControlTool}from"./code-mode-control-tools-CNDO-Zgv.mjs";import{g as wrapToolWithGatewayCallerIdentity}from"./gateway-caller-context-Dw9IijqV.mjs";import{h as getInternalToolExecutionPreparer,i as attachInternalToolExecutionPreparer}from"./internal-hooks-Bn77tst6.mjs";import{i as getPluginToolMeta}from"./tool-metadata-Cb5nBZYD.mjs";import{r as markToolExecutionNotStarted}from"./tool-effect-receipt-B6D3MzWJ.mjs";import{c as getAgentToolActionDescriptor,o as copyAgentToolMetadata}from"./agent-tool-metadata-D7ctyGhj.mjs";import{t as captureAgentWorkspaceReadiness}from"./workspace-readiness-NfUrYLg7.mjs";import{o as rewrapToolWithBeforeToolCallHook}from"./agent-tools.before-tool-call.wrapper-82hH05Yk.mjs";import{c as runWithAgentToolSourceExecutionScope,n as bindAgentToolSourceExecutionGuard}from"./agent-tool-source-execution-guard-D2z6vTFX.mjs";import"./agent-tools.before-tool-call-BkUHN8cH.mjs";import{i as wrapToolWithAbortSignal}from"./agent-tools.abort-Dyi0u_im.mjs";import{isPathInsideWithRealpath}from"@openclaw/fs-safe/path"; //!src/agents/harness/host-snapshot.ts function cloneHostSnapshot(e){return freezeJsonSnapshot(structuredClone(e))} //!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`};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)}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}} //!src/agents/harness/host-bound-tool.ts 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}export{isWorkspaceTool as a,captureRequiredWorkspaceToolFloor as i,gateBoundTool as n,prepareHostWorkspaceReadiness as o,bindHostContinuationRetryCount as r,cloneHostSnapshot as s,bindHostToolSurface as t};