mirror of https://github.com/openclaw/openclaw.git
refactor: share session tool context setup
This commit is contained in:
parent
d9a604f15f
commit
42d6e35cb4
|
|
@ -12,6 +12,7 @@ export {
|
||||||
resolveSandboxedSessionToolContext,
|
resolveSandboxedSessionToolContext,
|
||||||
resolveSessionToolsVisibility,
|
resolveSessionToolsVisibility,
|
||||||
} from "./sessions-access.js";
|
} from "./sessions-access.js";
|
||||||
|
import { resolveSandboxedSessionToolContext } from "./sessions-access.js";
|
||||||
export type { SessionReferenceResolution } from "./sessions-resolution.js";
|
export type { SessionReferenceResolution } from "./sessions-resolution.js";
|
||||||
export {
|
export {
|
||||||
isRequesterSpawnedSessionVisible,
|
isRequesterSpawnedSessionVisible,
|
||||||
|
|
@ -27,6 +28,7 @@ export {
|
||||||
shouldResolveSessionIdInput,
|
shouldResolveSessionIdInput,
|
||||||
shouldVerifyRequesterSpawnedSessionVisibility,
|
shouldVerifyRequesterSpawnedSessionVisibility,
|
||||||
} from "./sessions-resolution.js";
|
} from "./sessions-resolution.js";
|
||||||
|
import { type OpenClawConfig, loadConfig } from "../../config/config.js";
|
||||||
import { extractTextFromChatContent } from "../../shared/chat-content.js";
|
import { extractTextFromChatContent } from "../../shared/chat-content.js";
|
||||||
import { sanitizeUserFacingText } from "../pi-embedded-helpers.js";
|
import { sanitizeUserFacingText } from "../pi-embedded-helpers.js";
|
||||||
import {
|
import {
|
||||||
|
|
@ -73,6 +75,22 @@ function normalizeKey(value?: string) {
|
||||||
return trimmed ? trimmed : undefined;
|
return trimmed ? trimmed : undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function resolveSessionToolContext(opts?: {
|
||||||
|
agentSessionKey?: string;
|
||||||
|
sandboxed?: boolean;
|
||||||
|
config?: OpenClawConfig;
|
||||||
|
}) {
|
||||||
|
const cfg = opts?.config ?? loadConfig();
|
||||||
|
return {
|
||||||
|
cfg,
|
||||||
|
...resolveSandboxedSessionToolContext({
|
||||||
|
cfg,
|
||||||
|
agentSessionKey: opts?.agentSessionKey,
|
||||||
|
sandboxed: opts?.sandboxed,
|
||||||
|
}),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
export function classifySessionKind(params: {
|
export function classifySessionKind(params: {
|
||||||
key: string;
|
key: string;
|
||||||
gatewayKind?: string | null;
|
gatewayKind?: string | null;
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
import crypto from "node:crypto";
|
import crypto from "node:crypto";
|
||||||
import { Type } from "@sinclair/typebox";
|
import { Type } from "@sinclair/typebox";
|
||||||
import { type OpenClawConfig, loadConfig } from "../../config/config.js";
|
import type { OpenClawConfig } from "../../config/config.js";
|
||||||
import { callGateway } from "../../gateway/call.js";
|
import { callGateway } from "../../gateway/call.js";
|
||||||
import { normalizeAgentId, resolveAgentIdFromSessionKey } from "../../routing/session-key.js";
|
import { normalizeAgentId, resolveAgentIdFromSessionKey } from "../../routing/session-key.js";
|
||||||
import { SESSION_LABEL_MAX_LENGTH } from "../../sessions/session-label.js";
|
import { SESSION_LABEL_MAX_LENGTH } from "../../sessions/session-label.js";
|
||||||
|
|
@ -17,7 +17,7 @@ import {
|
||||||
extractAssistantText,
|
extractAssistantText,
|
||||||
resolveEffectiveSessionToolsVisibility,
|
resolveEffectiveSessionToolsVisibility,
|
||||||
resolveSessionReference,
|
resolveSessionReference,
|
||||||
resolveSandboxedSessionToolContext,
|
resolveSessionToolContext,
|
||||||
resolveVisibleSessionReference,
|
resolveVisibleSessionReference,
|
||||||
stripToolMessages,
|
stripToolMessages,
|
||||||
} from "./sessions-helpers.js";
|
} from "./sessions-helpers.js";
|
||||||
|
|
@ -32,6 +32,36 @@ const SessionsSendToolSchema = Type.Object({
|
||||||
timeoutSeconds: Type.Optional(Type.Number({ minimum: 0 })),
|
timeoutSeconds: Type.Optional(Type.Number({ minimum: 0 })),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
async function startAgentRun(params: {
|
||||||
|
runId: string;
|
||||||
|
sendParams: Record<string, unknown>;
|
||||||
|
sessionKey: string;
|
||||||
|
}): Promise<{ ok: true; runId: string } | { ok: false; result: ReturnType<typeof jsonResult> }> {
|
||||||
|
try {
|
||||||
|
const response = await callGateway<{ runId: string }>({
|
||||||
|
method: "agent",
|
||||||
|
params: params.sendParams,
|
||||||
|
timeoutMs: 10_000,
|
||||||
|
});
|
||||||
|
return {
|
||||||
|
ok: true,
|
||||||
|
runId: typeof response?.runId === "string" && response.runId ? response.runId : params.runId,
|
||||||
|
};
|
||||||
|
} catch (err) {
|
||||||
|
const messageText =
|
||||||
|
err instanceof Error ? err.message : typeof err === "string" ? err : "error";
|
||||||
|
return {
|
||||||
|
ok: false,
|
||||||
|
result: jsonResult({
|
||||||
|
runId: params.runId,
|
||||||
|
status: "error",
|
||||||
|
error: messageText,
|
||||||
|
sessionKey: params.sessionKey,
|
||||||
|
}),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export function createSessionsSendTool(opts?: {
|
export function createSessionsSendTool(opts?: {
|
||||||
agentSessionKey?: string;
|
agentSessionKey?: string;
|
||||||
agentChannel?: GatewayMessageChannel;
|
agentChannel?: GatewayMessageChannel;
|
||||||
|
|
@ -47,13 +77,8 @@ export function createSessionsSendTool(opts?: {
|
||||||
execute: async (_toolCallId, args) => {
|
execute: async (_toolCallId, args) => {
|
||||||
const params = args as Record<string, unknown>;
|
const params = args as Record<string, unknown>;
|
||||||
const message = readStringParam(params, "message", { required: true });
|
const message = readStringParam(params, "message", { required: true });
|
||||||
const cfg = opts?.config ?? loadConfig();
|
const { cfg, mainKey, alias, effectiveRequesterKey, restrictToSpawned } =
|
||||||
const { mainKey, alias, effectiveRequesterKey, restrictToSpawned } =
|
resolveSessionToolContext(opts);
|
||||||
resolveSandboxedSessionToolContext({
|
|
||||||
cfg,
|
|
||||||
agentSessionKey: opts?.agentSessionKey,
|
|
||||||
sandboxed: opts?.sandboxed,
|
|
||||||
});
|
|
||||||
|
|
||||||
const a2aPolicy = createAgentToAgentPolicy(cfg);
|
const a2aPolicy = createAgentToAgentPolicy(cfg);
|
||||||
const sessionVisibility = resolveEffectiveSessionToolsVisibility({
|
const sessionVisibility = resolveEffectiveSessionToolsVisibility({
|
||||||
|
|
@ -252,54 +277,34 @@ export function createSessionsSendTool(opts?: {
|
||||||
};
|
};
|
||||||
|
|
||||||
if (timeoutSeconds === 0) {
|
if (timeoutSeconds === 0) {
|
||||||
try {
|
const start = await startAgentRun({
|
||||||
const response = await callGateway<{ runId: string }>({
|
|
||||||
method: "agent",
|
|
||||||
params: sendParams,
|
|
||||||
timeoutMs: 10_000,
|
|
||||||
});
|
|
||||||
if (typeof response?.runId === "string" && response.runId) {
|
|
||||||
runId = response.runId;
|
|
||||||
}
|
|
||||||
startA2AFlow(undefined, runId);
|
|
||||||
return jsonResult({
|
|
||||||
runId,
|
|
||||||
status: "accepted",
|
|
||||||
sessionKey: displayKey,
|
|
||||||
delivery,
|
|
||||||
});
|
|
||||||
} catch (err) {
|
|
||||||
const messageText =
|
|
||||||
err instanceof Error ? err.message : typeof err === "string" ? err : "error";
|
|
||||||
return jsonResult({
|
|
||||||
runId,
|
|
||||||
status: "error",
|
|
||||||
error: messageText,
|
|
||||||
sessionKey: displayKey,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
const response = await callGateway<{ runId: string }>({
|
|
||||||
method: "agent",
|
|
||||||
params: sendParams,
|
|
||||||
timeoutMs: 10_000,
|
|
||||||
});
|
|
||||||
if (typeof response?.runId === "string" && response.runId) {
|
|
||||||
runId = response.runId;
|
|
||||||
}
|
|
||||||
} catch (err) {
|
|
||||||
const messageText =
|
|
||||||
err instanceof Error ? err.message : typeof err === "string" ? err : "error";
|
|
||||||
return jsonResult({
|
|
||||||
runId,
|
runId,
|
||||||
status: "error",
|
sendParams,
|
||||||
error: messageText,
|
|
||||||
sessionKey: displayKey,
|
sessionKey: displayKey,
|
||||||
});
|
});
|
||||||
|
if (!start.ok) {
|
||||||
|
return start.result;
|
||||||
|
}
|
||||||
|
runId = start.runId;
|
||||||
|
startA2AFlow(undefined, runId);
|
||||||
|
return jsonResult({
|
||||||
|
runId,
|
||||||
|
status: "accepted",
|
||||||
|
sessionKey: displayKey,
|
||||||
|
delivery,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const start = await startAgentRun({
|
||||||
|
runId,
|
||||||
|
sendParams,
|
||||||
|
sessionKey: displayKey,
|
||||||
|
});
|
||||||
|
if (!start.ok) {
|
||||||
|
return start.result;
|
||||||
|
}
|
||||||
|
runId = start.runId;
|
||||||
|
|
||||||
let waitStatus: string | undefined;
|
let waitStatus: string | undefined;
|
||||||
let waitError: string | undefined;
|
let waitError: string | undefined;
|
||||||
try {
|
try {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue