Skip to content

Commit 6edf4a2

Browse files
steipetePeter Steinberger
andauthored
improve: reduce worker tool startup imports (#119644)
* perf(worker): narrow core tool imports * fix(worker): preserve generic tool factories --------- Co-authored-by: Peter Steinberger <[email protected]>
1 parent 6269be7 commit 6edf4a2

8 files changed

Lines changed: 520 additions & 406 deletions

‎src/agents/agent-tools.finalize.ts‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import type { ModelCompatConfig } from "../config/types.models.js";
2+
import { wrapToolWithAbortSignal } from "./agent-tools.abort.js";
3+
import type { HookContext } from "./agent-tools.before-tool-call.types.js";
4+
import {
5+
rewrapToolWithBeforeToolCallHook,
6+
wrapToolWithBeforeToolCallHook,
7+
} from "./agent-tools.before-tool-call.wrapper.js";
8+
import { applyDeferredFollowupToolDescriptions } from "./agent-tools.deferred-followup.js";
9+
import { normalizeToolParameters } from "./agent-tools.schema.js";
10+
import type { AnyAgentTool } from "./agent-tools.types.js";
11+
import { isToolWrappedWithBeforeToolCallHook } from "./before-tool-call-metadata.js";
12+
13+
type FinalizeAgentToolsOptions = {
14+
tools: AnyAgentTool[];
15+
modelProvider?: string;
16+
modelId?: string;
17+
modelCompat?: ModelCompatConfig;
18+
hookContext: HookContext;
19+
wrapBeforeToolCallHook?: boolean;
20+
emitBeforeToolCallDiagnostics?: boolean;
21+
approvalMode?: "request" | "report" | "deny";
22+
abortSignal?: AbortSignal;
23+
agentId?: string;
24+
recordToolPrepStage?: (name: string) => void;
25+
};
26+
27+
/** Apply the shared schema, hook, abort, and description wrappers to an authorized tool set. */
28+
export function finalizeAgentTools(options: FinalizeAgentToolsOptions): AnyAgentTool[] {
29+
const normalized = options.tools.map((tool) =>
30+
normalizeToolParameters(tool, {
31+
modelProvider: options.modelProvider,
32+
modelId: options.modelId,
33+
modelCompat: options.modelCompat,
34+
}),
35+
);
36+
options.recordToolPrepStage?.("schema-normalization");
37+
const hookOptions = {
38+
emitDiagnostics: options.emitBeforeToolCallDiagnostics,
39+
...(options.approvalMode ? { approvalMode: options.approvalMode } : {}),
40+
};
41+
const withHooks =
42+
options.wrapBeforeToolCallHook === false
43+
? normalized
44+
: normalized.map((tool) =>
45+
isToolWrappedWithBeforeToolCallHook(tool)
46+
? rewrapToolWithBeforeToolCallHook(tool, options.hookContext, hookOptions)
47+
: wrapToolWithBeforeToolCallHook(tool, options.hookContext, hookOptions),
48+
);
49+
options.recordToolPrepStage?.("tool-hooks");
50+
const abortSignal = options.abortSignal;
51+
const withAbort = abortSignal
52+
? withHooks.map((tool) => wrapToolWithAbortSignal(tool, abortSignal))
53+
: withHooks;
54+
options.recordToolPrepStage?.("abort-wrappers");
55+
const finalized = applyDeferredFollowupToolDescriptions(withAbort, {
56+
agentId: options.agentId,
57+
});
58+
options.recordToolPrepStage?.("deferred-followup-descriptions");
59+
return finalized;
60+
}

‎src/agents/agent-tools.read.host-operations.test.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@ const mocks = vi.hoisted(() => ({
2929
writeOps: undefined as CapturedWriteOperations | undefined,
3030
}));
3131

32-
vi.mock("./sessions/index.js", async () => {
33-
const actual = await vi.importActual<typeof import("./sessions/index.js")>("./sessions/index.js");
32+
vi.mock("./sessions/tools/index.js", async () => {
33+
const actual = await vi.importActual<typeof import("./sessions/tools/index.js")>(
34+
"./sessions/tools/index.js",
35+
);
3436
return {
3537
...actual,
3638
createEditTool: (_cwd: string, options?: { operations?: CapturedEditOperations }) => {

‎src/agents/agent-tools.read.ts‎

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ import {
4848
createWriteTool,
4949
type ReadToolDetails,
5050
type ReadToolTruncationDetails,
51-
} from "./sessions/index.js";
51+
} from "./sessions/tools/index.js";
5252
import { sanitizeToolResultImages } from "./tool-images.js";
5353

5454
// NOTE(steipete): Upstream read now does file-magic MIME detection; we keep the wrapper
@@ -890,8 +890,10 @@ type SandboxToolParams = {
890890
};
891891

892892
/** Create a sandbox-backed read tool with OpenClaw result normalization. */
893-
export function createSandboxedReadTool(params: SandboxToolParams) {
894-
const base = createReadTool(params.root, {
893+
export function createSandboxedReadTool(
894+
params: SandboxToolParams & { createTool?: typeof createReadTool },
895+
) {
896+
const base = (params.createTool ?? createReadTool)(params.root, {
895897
operations: createSandboxReadOperations(params),
896898
}) as unknown as AnyAgentTool;
897899
return createOpenClawReadTool(base, {
@@ -901,16 +903,20 @@ export function createSandboxedReadTool(params: SandboxToolParams) {
901903
}
902904

903905
/** Create a sandbox-backed write tool with required-parameter validation. */
904-
export function createSandboxedWriteTool(params: SandboxToolParams) {
905-
const base = createWriteTool(params.root, {
906+
export function createSandboxedWriteTool(
907+
params: SandboxToolParams & { createTool?: typeof createWriteTool },
908+
) {
909+
const base = (params.createTool ?? createWriteTool)(params.root, {
906910
operations: createSandboxWriteOperations(params),
907911
}) as unknown as AnyAgentTool;
908912
return wrapToolParamValidation(base, REQUIRED_PARAM_GROUPS.write);
909913
}
910914

911915
/** Create a sandbox-backed edit tool with required-parameter validation. */
912-
export function createSandboxedEditTool(params: SandboxToolParams) {
913-
const base = createEditTool(params.root, {
916+
export function createSandboxedEditTool(
917+
params: SandboxToolParams & { createTool?: typeof createEditTool },
918+
) {
919+
const base = (params.createTool ?? createEditTool)(params.root, {
914920
operations: createSandboxEditOperations(params),
915921
}) as unknown as AnyAgentTool;
916922
return wrapToolParamValidation(base, REQUIRED_PARAM_GROUPS.edit);
@@ -922,9 +928,10 @@ export function createHostWorkspaceWriteTool(
922928
options?: {
923929
workspaceOnly?: boolean;
924930
memoryWriteProvenance?: MemoryWriteProvenanceObserver;
931+
createTool?: typeof createWriteTool;
925932
},
926933
) {
927-
const base = createWriteTool(root, {
934+
const base = (options?.createTool ?? createWriteTool)(root, {
928935
operations: createHostWriteOperations(root, options),
929936
}) as unknown as AnyAgentTool;
930937
return wrapToolParamValidation(base, REQUIRED_PARAM_GROUPS.write);
@@ -936,9 +943,10 @@ export function createHostWorkspaceEditTool(
936943
options?: {
937944
workspaceOnly?: boolean;
938945
memoryWriteProvenance?: MemoryWriteProvenanceObserver;
946+
createTool?: typeof createEditTool;
939947
},
940948
) {
941-
const base = createEditTool(root, {
949+
const base = (options?.createTool ?? createEditTool)(root, {
942950
operations: createHostEditOperations(root, options),
943951
}) as unknown as AnyAgentTool;
944952
return wrapToolParamValidation(base, REQUIRED_PARAM_GROUPS.edit);

‎src/agents/agent-tools.read.workspace-root-lazy.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ vi.mock("../infra/fs-safe.js", async (importOriginal) => {
2626

2727
// Capture the operations object handed to the underlying write/edit tools so the
2828
// regression can drive a single workspace write operation directly.
29-
vi.mock("./sessions/index.js", async (importOriginal) => {
30-
const actual = await importOriginal<typeof import("./sessions/index.js")>();
29+
vi.mock("./sessions/tools/index.js", async (importOriginal) => {
30+
const actual = await importOriginal<typeof import("./sessions/tools/index.js")>();
3131
const stub = (name: string) => ({
3232
name,
3333
description: `test ${name} tool`,

0 commit comments

Comments
 (0)