UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

183 lines (182 loc) • 6.11 kB
import { r as defaultRuntime } from "../../runtime-B-SLYP-m.mjs"; import { t as formatErrorMessage } from "../../errors-BKzCeTYb.mjs"; import { l as resolveAgentIdFromSessionKey } from "../../session-key-CqAyZeVk.mjs"; import { i as listAgentIds } from "../../agent-roster-t1ooUdXp.mjs"; import { l as resolveSessionStorePathCore } from "../../paths-DvQ5AJvD.mjs"; import { f as resolveAgentWorkspaceDir } from "../../agent-scope-config-DmXgFGKP.mjs"; import { t as createSubsystemLogger } from "../../subsystem-BHJiy01V.mjs"; import { t as dedupeByKey } from "../../dedupe-by-key-Cpowmfct.mjs"; import "../../agent-scope-BwB8YH6y.mjs"; import { n as SILENT_REPLY_TOKEN } from "../../tokens-CcE6ESvH.mjs"; import { a as resolveMainSessionKey, r as resolveAgentMainSessionKey } from "../../main-session-BNL3-mRV.mjs"; import { s as escapeInternalRuntimeContextDelimiters } from "../../internal-runtime-context-BEUlDNuI.mjs"; import "../../session-accessor-CizTbDQj.mjs"; import { g as applySessionEntryLifecycleMutation } from "../../session-accessor.entry-mutation-IJ4DYZ4a.mjs"; import { a as isGatewayStartupEvent } from "../../internal-hooks-D6nf-Q3g.mjs"; import { t as createDefaultDeps } from "../../deps-CeXkTZma.mjs"; import { i as agentCommandFromSystem } from "../../agent-command-FXkX3hj5.mjs"; import { r as setBootEchoContextForSession, t as clearBootEchoContextForSession } from "../../boot-echo-guard-BY7eG-FN.mjs"; import "../../agent-WNO-h9tH.mjs"; import { t as runStartupTasks } from "../../startup-tasks-B2QP2bXs.mjs"; import path from "node:path"; import crypto from "node:crypto"; import fs from "node:fs/promises"; import { readRegularFile } from "@openclaw/fs-safe/advanced"; //#region src/gateway/boot.ts function generateBootSessionId() { return `boot-${(/* @__PURE__ */ new Date()).toISOString().replace(/[:.]/g, "-").replace("T", "_").replace("Z", "")}-${crypto.randomUUID().slice(0, 8)}`; } const log$1 = createSubsystemLogger("gateway/boot"); const BOOT_FILENAME = "BOOT.md"; function buildBootPrompt(e) { const t = [ { kind: "runtime-instruction", text: "You are running a boot check. Follow BOOT.md instructions exactly." }, { kind: "runtime-instruction", text: `BOOT.md:\n${escapeInternalRuntimeContextDelimiters(e)}` }, { kind: "runtime-instruction", text: [ "If BOOT.md asks you to send a message, use the message tool (action=send with channel + target).", "Use the `target` field (not `to`) for message tool destinations.", `After sending with the message tool, reply with exactly ${SILENT_REPLY_TOKEN} as the entire final answer.`, `If nothing needs attention, reply with exactly ${SILENT_REPLY_TOKEN} as the entire final answer.`, `${SILENT_REPLY_TOKEN} discards anything written this turn that was not sent, so send confirmations before ending silently.` ].join("\n") } ]; return { message: "Run the workspace boot check now.", runtimeContextFragments: t, echoContext: t.map((e) => e.text).join("\n\n") }; } const MAX_BOOT_FILE_BYTES = 16777216; async function loadBootFile(e) { const t = path.join(e, BOOT_FILENAME); let n; try { const e = await fs.realpath(t); ({buffer: n} = await readRegularFile({ filePath: e, maxBytes: MAX_BOOT_FILE_BYTES })); } catch (e) { if (e.code === "ENOENT") return { status: "missing" }; throw e; } const r = n.toString("utf-8").trim(); return r ? { status: "ok", content: r } : { status: "empty" }; } /** Result of attempting to run a workspace BOOT.md check. */ async function runBootOnce(e) { const t = { log: () => {}, error: (e) => log$1.error(String(e)), exit: defaultRuntime.exit }; let n; try { n = await loadBootFile(e.workspaceDir); } catch (e) { const t = formatErrorMessage(e); log$1.error(`boot: failed to read ${BOOT_FILENAME}: ${t}`); return { status: "failed", reason: t }; } if (n.status !== "ok") return { status: "skipped", reason: n.status }; const r = e.agentId ? resolveAgentMainSessionKey({ cfg: e.cfg, agentId: e.agentId }) : resolveMainSessionKey(e.cfg); const i = buildBootPrompt(n.content); const a = generateBootSessionId(); const o = resolveAgentIdFromSessionKey(r); const s = `agent:${o}:boot:${a}`; const c = resolveSessionStorePathCore(e.cfg.session?.store, { agentId: o }); let l = a; let u; let d; setBootEchoContextForSession(s, i.echoContext); try { await agentCommandFromSystem({ message: i.message, runtimeContextFragments: i.runtimeContextFragments, sessionKey: s, sessionId: a, deliver: false, suppressPromptPersistence: true, onSessionIdChanged: (e) => { l = e; } }, t, e.deps); } catch (e) { u = formatErrorMessage(e); log$1.error(`boot: agent run failed: ${u}`); } finally { clearBootEchoContextForSession(s); try { await applySessionEntryLifecycleMutation({ agentId: o, storePath: c, removals: [{ sessionKey: s, expectedSessionId: l, archiveRemovedTranscript: false }], skipMaintenance: true }); } catch (e) { d = formatErrorMessage(e); log$1.error(`boot: failed to clean up session: ${d}`); } } if (!u && !d) return { status: "ran" }; return { status: "failed", reason: [u ? `agent run failed: ${u}` : void 0, d ? `session cleanup failed: ${d}` : void 0].filter((e) => Boolean(e)).join("; ") }; } //#endregion //#region src/hooks/bundled/boot-md/handler.ts const log = createSubsystemLogger("hooks/boot-md"); const runBootChecklist = async (e) => { if (!isGatewayStartupEvent(e)) return; if (!e.context.cfg) return; const t = e.context.cfg; const n = e.context.deps ?? createDefaultDeps(); const r = dedupeByKey(listAgentIds(t).map((e) => { return { agentId: e, workspaceDir: resolveAgentWorkspaceDir(t, e) }; }), ({ workspaceDir: e }) => e).map(({ agentId: e, workspaceDir: r }) => ({ source: "boot-md", agentId: e, workspaceDir: r, run: () => runBootOnce({ cfg: t, deps: n, workspaceDir: r, agentId: e }) })); await runStartupTasks({ tasks: r, log }); }; //#endregion export { runBootChecklist as default };