UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

149 lines (148 loc) • 7.14 kB
import { l as isRecord } from "./record-coerce-CVPuDqcc.mjs"; import "./src-H3RMXyFL.mjs"; import { t as stableStringify } from "./stable-stringify-vqde_3tT.mjs"; import { n as sha256Hex } from "./node-crypto-B6yuTjmm.mjs"; import { o as composeSessionSourceAssertion } from "./session-source-authority-TlTpfRVE.mjs"; import { o as isToolExecutionAllowed } from "./tool-policy-shared-BHvBopvF.mjs"; import { n as emitSessionLifecycleEvent } from "./session-lifecycle-events-DWKmeEvQ.mjs"; import { n as ToolInputError } from "./tool-input-error-B1hgavSw.mjs"; import "./common-njQNtpnW.mjs"; import { s as isSubagentRunQueued } from "./subagent-run-liveness-D7qiCtn0.mjs"; import "./subagent-registry-read-DHkmVSx2.mjs"; import { o as runAgentToolSourceExecutionGuard, r as captureAgentToolSourceExecutionGuard, s as runWithAgentToolSourceExecutionScope } from "./agent-tool-source-execution-guard-CD-ezY7E.mjs"; import { o as resolveInternalSessionKey, s as resolveMainSessionAlias } from "./sessions-resolution-B1MUJxVg.mjs"; import { u as getSwarmRunByLaunchReplayKey } from "./subagent-registry-DXsif7FR.mjs"; import { t as resolveSwarmConfig } from "./swarm-config-Dp8TTyRG.mjs"; import { c as runWithJoinedCollectorSpawn, o as isCollectorSpawnTool } from "./swarm-collector-capability-BSHCyIjQ.mjs"; import { r as waitForCollectorCompletion } from "./agents-wait-tool-U0rRhq5r.mjs"; import { n as SWARM_CODE_MODE_REQUEST_FINGERPRINT, t as SWARM_CODE_MODE_IDEMPOTENCY_KEY } from "./swarm-code-mode-DXtHU4JN.mjs"; //#region src/agents/code-mode-swarm.runtime.ts function resolveCodeModeRequesterSessionKey(e) { const t = e.sessionKey?.trim(); if (!t) throw new ToolInputError("code mode swarm globals require session and run identity."); const { alias: n } = resolveMainSessionAlias(e.runtimeConfig ?? e.config ?? {}); return resolveInternalSessionKey({ key: t, alias: n }); } function resolveCodeModeSwarmGroupId(e) { const t = resolveCodeModeRequesterSessionKey(e); const n = e.runId?.trim(); if (!n) throw new ToolInputError("code mode swarm globals require session and run identity."); return `swarm:${t}:${n}`; } function replayedSpawnResult(e) { return { status: "accepted", runId: e.swarmRunId ?? e.runId, sessionKey: e.childSessionKey, ...e.label ? { label: e.label } : {} }; } function readOptionalStringOption(e, t) { const n = e[t]; if (n === void 0) return; if (typeof n !== "string" || !n.trim()) throw new ToolInputError(`agents.run ${t} must be a non-empty string.`); return n.trim(); } async function runAgentSpawnBridge(e) { const t = e.request.args[0]; const n = isRecord(e.request.args[1]) ? e.request.args[1] : {}; if (typeof t !== "string" || !t.trim()) throw new ToolInputError("agents.run prompt must be a non-empty string."); const r = n.fastMode; if (r !== void 0 && r !== true && r !== false && r !== "auto" && r !== "ultrafast") throw new ToolInputError("agents.run fastMode must be boolean, \"auto\", or \"ultrafast\"."); const i = n.schema; if (i !== void 0 && !isRecord(i)) throw new ToolInputError("agents.run schema must be a JSON schema object."); const a = readOptionalStringOption(n, "label"); const o = readOptionalStringOption(n, "model"); const s = readOptionalStringOption(n, "thinking"); const c = readOptionalStringOption(n, "agentId"); const l = e.ctx.catalogRef?.current; const u = l?.entries.find((e) => e.name === "sessions_spawn" && isCollectorSpawnTool(e.tool)); if (!l || !u || !isCollectorSpawnTool(u.tool)) throw new ToolInputError("agents.run requires the native sessions_spawn tool."); const d = u.tool; const f = captureAgentToolSourceExecutionGuard(e.signal); const p = composeSessionSourceAssertion([f], (t) => { t(); e.ctx.abortSignal?.throwIfAborted(); if (e.ctx.catalogRef?.current !== l || !l.entries.includes(u) || !resolveSwarmConfig(e.ctx.runtimeConfig ?? e.ctx.config, e.ctx.agentId).enabled || e.ctx.toolExecutionAllow && !isToolExecutionAllowed(e.ctx.toolExecutionAllow, "sessions_spawn")) throw new ToolInputError("Joined collector spawn catalog is no longer active."); runAgentToolSourceExecutionGuard(d); }); p(); const m = { task: t.trim(), collect: true, groupId: resolveCodeModeSwarmGroupId(e.ctx), ...a ? { label: a } : {}, ...o ? { model: o } : {}, ...s ? { thinking: s } : {}, ...c ? { agentId: c } : {}, ...r !== void 0 ? { fastMode: r } : {}, ...i ? { outputSchema: i } : {} }; const h = `sha256:${sha256Hex(stableStringify(m))}`; const g = `${e.codeModeRunId}:${e.request.id}`; const _ = resolveCodeModeRequesterSessionKey(e.ctx); const v = await getSwarmRunByLaunchReplayKey(g, _, e.ctx.agentId); if (v) { if (v.swarmLaunchRequestFingerprint !== h) throw new ToolInputError("agents.run replay request does not match the persisted collector."); if (v.swarmLaunchPending === true && !isSubagentRunQueued(v)) throw new ToolInputError("agents.run launch is no longer active; start a new request."); const e = v; return await runWithAgentToolSourceExecutionScope(p, async () => { p(); return replayedSpawnResult(e); }); } Object.defineProperty(m, SWARM_CODE_MODE_IDEMPOTENCY_KEY, { value: g }); Object.defineProperty(m, SWARM_CODE_MODE_REQUEST_FINGERPRINT, { value: h }); const y = await runWithJoinedCollectorSpawn(u.tool, p, () => e.runtime.callExactId(u.id, m, { parentToolCallId: e.parentToolCallId, signal: e.signal, onUpdate: e.onUpdate })); const b = isRecord(y.result) && "details" in y.result ? y.result.details : y.result; if (!isRecord(b) || b.status !== "accepted" || typeof b.runId !== "string") { const e = isRecord(b) && typeof b.error === "string" ? b.error : "collector spawn was not accepted"; throw new ToolInputError(`agents.run spawn failed: ${e}`); } return b; } async function runAgentWaitBridge(e) { const t = e.request.args[0]; if (typeof t !== "string" || !t.trim()) throw new ToolInputError("agentWait run id must be a non-empty string."); const n = e.ctx.sessionKey?.trim(); if (!n) throw new ToolInputError("agents.run wait requires session identity."); const r = resolveCodeModeRequesterSessionKey(e.ctx); return await waitForCollectorCompletion({ runId: t.trim(), currentSessionKeys: /* @__PURE__ */ new Set([n, r]), currentAgentId: e.ctx.agentId, config: e.ctx.runtimeConfig ?? e.ctx.config, signal: e.signal }); } function runSwarmNoteBridge(e) { const t = isRecord(e.request.args[0]) ? e.request.args[0] : void 0; const n = t?.kind; const r = t?.text; if (n !== "phase" && n !== "log" || typeof r !== "string" || !r.trim()) throw new ToolInputError("swarmNote requires phase/log kind and non-empty text."); const i = e.ctx.sessionKey?.trim(); if (!i) throw new ToolInputError("swarmNote requires session identity."); emitSessionLifecycleEvent({ sessionKey: i, reason: "swarm-note", scope: "runtime", swarmGroupId: resolveCodeModeSwarmGroupId(e.ctx), kind: n, text: r.trim() }); return { ok: true }; } const codeModeSwarmHandlers = { agentSpawn: runAgentSpawnBridge, agentWait: runAgentWaitBridge, swarmNote: runSwarmNoteBridge }; //#endregion export { codeModeSwarmHandlers };