Skip to content

Commit ddd3d4f

Browse files
authored
refactor(agents): split before-tool-call policy pipeline (#113885)
* refactor(agents): split before-tool-call pipeline * refactor(agents): isolate before-tool call types * fix(agents): restore approval resolution type import
1 parent 16badb1 commit ddd3d4f

9 files changed

Lines changed: 2324 additions & 2177 deletions

config/max-lines-baseline.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,6 @@ src/agents/agent-hooks/compaction-safeguard.ts
361361
src/agents/agent-scope.test.ts
362362
src/agents/agent-tools.before-tool-call.e2e.test.ts
363363
src/agents/agent-tools.before-tool-call.integration.e2e.test.ts
364-
src/agents/agent-tools.before-tool-call.ts
365364
src/agents/agent-tools.create-openclaw-coding-tools.test.ts
366365
src/agents/agent-tools.read.ts
367366
src/agents/agent-tools.schema.test.ts

docs/.generated/plugin-sdk-api-baseline.sha256

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ cbf4e2c3088f8886a7c9ea91325a66e0f0846cea21f0b2891f36399b4811306c module/account
44
6b674e7aa4006240227c16eb5c74360f1d67639e8f5580ad72499d6ed28283c4 module/account-resolution
55
e5e67ddf3cab38fcbf9220bc3160715897e2709d9a9ff6ff36f1ecc9453c2367 module/agent-config-primitives
66
74daa746deb548379d3f0d6eac3c4d082df1034c4360cc03bf51fee0f10a2e4d module/agent-harness
7-
e62f3ed1976586cf85ed2a3395b2e255968d57389ec97999e06b2b3a2b978a33 module/agent-harness-runtime
7+
b80e26a4dcf2c246d01755b8da4bee272637f11ced1689fa561397c1979613e5 module/agent-harness-runtime
88
5168648cd946abad8a92822889f13ceacc87ed502314a66190d0b1eb8ebe76ea module/agent-media-payload
99
8b406b7a50b0b4f088be869ea6a899c0d33229a5c73d3f82bda76ce0be0941b0 module/agent-runtime
1010
56b6d5fb6af3d95af1200065aca2e7d4f59e5fa59740505fe6ff433077ef6646 module/allow-from

src/agents/agent-tool-definition-adapter.ts

Lines changed: 10 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@ import {
1616
recordStructuredReplayTrustForToolCall,
1717
runBeforeToolCallHook,
1818
} from "./agent-tools.before-tool-call.js";
19+
import {
20+
finalizeBeforeToolCallExecutionParams,
21+
prepareBeforeToolCallExecutionParams,
22+
} from "./agent-tools.before-tool-call.wrapper.js";
1923
import {
2024
getCodeModeExecBeforeHookMetadata,
2125
normalizeCodeModeExecBeforeHookParams,
22-
reconcileCodeModeExecBeforeHookParams,
2326
} from "./code-mode-control-tools.js";
2427
import { sanitizeForConsole } from "./console-sanitize.js";
2528
import type { ClientToolDefinition } from "./embedded-agent-runner/run/params.js";
@@ -29,13 +32,6 @@ import { normalizeToolName } from "./tool-policy.js";
2932
import { jsonResult, payloadTextResult } from "./tools/common.js";
3033

3134
type AnyAgentTool = AgentTool;
32-
type BeforeToolCallPreparingTool = AnyAgentTool & {
33-
prepareBeforeToolCallParams?: (
34-
params: unknown,
35-
ctx: { toolCallId?: string; hookContext?: HookContext; signal?: AbortSignal },
36-
) => unknown;
37-
finalizeBeforeToolCallParams?: (params: unknown, preparedParams: unknown) => unknown;
38-
};
3935

4036
type ToolExecuteArgsCurrent = [
4137
string,
@@ -282,32 +278,6 @@ function splitToolExecuteArgs(args: ToolExecuteArgsAny): {
282278
};
283279
}
284280

285-
async function prepareToolParamsBeforeHook(params: {
286-
tool: AnyAgentTool;
287-
rawParams: unknown;
288-
toolCallId?: string;
289-
hookContext?: HookContext;
290-
signal?: AbortSignal;
291-
}): Promise<unknown> {
292-
const prepare = (params.tool as BeforeToolCallPreparingTool).prepareBeforeToolCallParams;
293-
return prepare
294-
? await prepare(params.rawParams, {
295-
...(params.toolCallId ? { toolCallId: params.toolCallId } : {}),
296-
...(params.hookContext ? { hookContext: params.hookContext } : {}),
297-
...(params.signal ? { signal: params.signal } : {}),
298-
})
299-
: params.rawParams;
300-
}
301-
302-
function finalizeToolParamsBeforeExecute(params: {
303-
tool: AnyAgentTool;
304-
executeParams: unknown;
305-
preparedParams: unknown;
306-
}): unknown {
307-
const finalize = (params.tool as BeforeToolCallPreparingTool).finalizeBeforeToolCallParams;
308-
return finalize ? finalize(params.executeParams, params.preparedParams) : params.executeParams;
309-
}
310-
311281
const CLIENT_TOOL_NAME_CONFLICT_PREFIX = "client tool name conflict:";
312282

313283
/** Find client-hosted tool names that collide with runtime or sibling tools. */
@@ -378,11 +348,11 @@ export function toToolDefinitions(
378348
let executeParams = params;
379349
try {
380350
if (!beforeHookWrapped) {
381-
const preparedParams = await prepareToolParamsBeforeHook({
351+
const preparedParams = await prepareBeforeToolCallExecutionParams({
382352
tool,
383-
rawParams: params,
353+
params,
384354
...(toolCallId ? { toolCallId } : {}),
385-
...(hookContext ? { hookContext } : {}),
355+
...(hookContext ? { ctx: hookContext } : {}),
386356
...(signal ? { signal } : {}),
387357
});
388358
const hookParams = normalizeCodeModeExecBeforeHookParams({
@@ -411,16 +381,12 @@ export function toToolDefinitions(
411381
}
412382
throw new Error(hookOutcome.reason);
413383
}
414-
executeParams = reconcileCodeModeExecBeforeHookParams({
384+
executeParams = finalizeBeforeToolCallExecutionParams({
415385
tool,
416-
originalParams: preparedParams,
386+
preparedParams,
417387
hookParams,
418388
adjustedParams: hookOutcome.params,
419-
});
420-
executeParams = finalizeToolParamsBeforeExecute({
421-
tool,
422-
executeParams,
423-
preparedParams,
389+
finalizerMode: "adapter",
424390
});
425391
recordAdjustedParamsForToolCall(toolCallId, executeParams, hookContext?.runId);
426392
}

0 commit comments

Comments
 (0)