Skip to content

Commit 42d6e35

Browse files
committed
refactor: share session tool context setup
1 parent d9a604f commit 42d6e35

2 files changed

Lines changed: 72 additions & 49 deletions

File tree

‎src/agents/tools/sessions-helpers.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export {
1212
resolveSandboxedSessionToolContext,
1313
resolveSessionToolsVisibility,
1414
} from "./sessions-access.js";
15+
import { resolveSandboxedSessionToolContext } from "./sessions-access.js";
1516
export type { SessionReferenceResolution } from "./sessions-resolution.js";
1617
export {
1718
isRequesterSpawnedSessionVisible,
@@ -27,6 +28,7 @@ export {
2728
shouldResolveSessionIdInput,
2829
shouldVerifyRequesterSpawnedSessionVisibility,
2930
} from "./sessions-resolution.js";
31+
import { type OpenClawConfig, loadConfig } from "../../config/config.js";
3032
import { extractTextFromChatContent } from "../../shared/chat-content.js";
3133
import { sanitizeUserFacingText } from "../pi-embedded-helpers.js";
3234
import {
@@ -73,6 +75,22 @@ function normalizeKey(value?: string) {
7375
return trimmed ? trimmed : undefined;
7476
}
7577

78+
export function resolveSessionToolContext(opts?: {
79+
agentSessionKey?: string;
80+
sandboxed?: boolean;
81+
config?: OpenClawConfig;
82+
}) {
83+
const cfg = opts?.config ?? loadConfig();
84+
return {
85+
cfg,
86+
...resolveSandboxedSessionToolContext({
87+
cfg,
88+
agentSessionKey: opts?.agentSessionKey,
89+
sandboxed: opts?.sandboxed,
90+
}),
91+
};
92+
}
93+
7694
export function classifySessionKind(params: {
7795
key: string;
7896
gatewayKind?: string | null;

‎src/agents/tools/sessions-send-tool.ts‎

Lines changed: 54 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import crypto from "node:crypto";
22
import { Type } from "@sinclair/typebox";
3-
import { type OpenClawConfig, loadConfig } from "../../config/config.js";
3+
import type { OpenClawConfig } from "../../config/config.js";
44
import { callGateway } from "../../gateway/call.js";
55
import { normalizeAgentId, resolveAgentIdFromSessionKey } from "../../routing/session-key.js";
66
import { SESSION_LABEL_MAX_LENGTH } from "../../sessions/session-label.js";
@@ -17,7 +17,7 @@ import {
1717
extractAssistantText,
1818
resolveEffectiveSessionToolsVisibility,
1919
resolveSessionReference,
20-
resolveSandboxedSessionToolContext,
20+
resolveSessionToolContext,
2121
resolveVisibleSessionReference,
2222
stripToolMessages,
2323
} from "./sessions-helpers.js";
@@ -32,6 +32,36 @@ const SessionsSendToolSchema = Type.Object({
3232
timeoutSeconds: Type.Optional(Type.Number({ minimum: 0 })),
3333
});
3434

35+
async function startAgentRun(params: {
36+
runId: string;
37+
sendParams: Record<string, unknown>;
38+
sessionKey: string;
39+
}): Promise<{ ok: true; runId: string } | { ok: false; result: ReturnType<typeof jsonResult> }> {
40+
try {
41+
const response = await callGateway<{ runId: string }>({
42+
method: "agent",
43+
params: params.sendParams,
44+
timeoutMs: 10_000,
45+
});
46+
return {
47+
ok: true,
48+
runId: typeof response?.runId === "string" && response.runId ? response.runId : params.runId,
49+
};
50+
} catch (err) {
51+
const messageText =
52+
err instanceof Error ? err.message : typeof err === "string" ? err : "error";
53+
return {
54+
ok: false,
55+
result: jsonResult({
56+
runId: params.runId,
57+
status: "error",
58+
error: messageText,
59+
sessionKey: params.sessionKey,
60+
}),
61+
};
62+
}
63+
}
64+
3565
export function createSessionsSendTool(opts?: {
3666
agentSessionKey?: string;
3767
agentChannel?: GatewayMessageChannel;
@@ -47,13 +77,8 @@ export function createSessionsSendTool(opts?: {
4777
execute: async (_toolCallId, args) => {
4878
const params = args as Record<string, unknown>;
4979
const message = readStringParam(params, "message", { required: true });
50-
const cfg = opts?.config ?? loadConfig();
51-
const { mainKey, alias, effectiveRequesterKey, restrictToSpawned } =
52-
resolveSandboxedSessionToolContext({
53-
cfg,
54-
agentSessionKey: opts?.agentSessionKey,
55-
sandboxed: opts?.sandboxed,
56-
});
80+
const { cfg, mainKey, alias, effectiveRequesterKey, restrictToSpawned } =
81+
resolveSessionToolContext(opts);
5782

5883
const a2aPolicy = createAgentToAgentPolicy(cfg);
5984
const sessionVisibility = resolveEffectiveSessionToolsVisibility({
@@ -252,54 +277,34 @@ export function createSessionsSendTool(opts?: {
252277
};
253278

254279
if (timeoutSeconds === 0) {
255-
try {
256-
const response = await callGateway<{ runId: string }>({
257-
method: "agent",
258-
params: sendParams,
259-
timeoutMs: 10_000,
260-
});
261-
if (typeof response?.runId === "string" && response.runId) {
262-
runId = response.runId;
263-
}
264-
startA2AFlow(undefined, runId);
265-
return jsonResult({
266-
runId,
267-
status: "accepted",
268-
sessionKey: displayKey,
269-
delivery,
270-
});
271-
} catch (err) {
272-
const messageText =
273-
err instanceof Error ? err.message : typeof err === "string" ? err : "error";
274-
return jsonResult({
275-
runId,
276-
status: "error",
277-
error: messageText,
278-
sessionKey: displayKey,
279-
});
280-
}
281-
}
282-
283-
try {
284-
const response = await callGateway<{ runId: string }>({
285-
method: "agent",
286-
params: sendParams,
287-
timeoutMs: 10_000,
280+
const start = await startAgentRun({
281+
runId,
282+
sendParams,
283+
sessionKey: displayKey,
288284
});
289-
if (typeof response?.runId === "string" && response.runId) {
290-
runId = response.runId;
285+
if (!start.ok) {
286+
return start.result;
291287
}
292-
} catch (err) {
293-
const messageText =
294-
err instanceof Error ? err.message : typeof err === "string" ? err : "error";
288+
runId = start.runId;
289+
startA2AFlow(undefined, runId);
295290
return jsonResult({
296291
runId,
297-
status: "error",
298-
error: messageText,
292+
status: "accepted",
299293
sessionKey: displayKey,
294+
delivery,
300295
});
301296
}
302297

298+
const start = await startAgentRun({
299+
runId,
300+
sendParams,
301+
sessionKey: displayKey,
302+
});
303+
if (!start.ok) {
304+
return start.result;
305+
}
306+
runId = start.runId;
307+
303308
let waitStatus: string | undefined;
304309
let waitError: string | undefined;
305310
try {

0 commit comments

Comments
 (0)