openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
8 lines (7 loc) • 5.2 kB
JavaScript
import{r as defaultRuntime}from"../../runtime-DIcLIHcp.mjs";import{t as formatErrorMessage}from"../../errors-D37BJTfI.mjs";import{l as resolveAgentIdFromSessionKey}from"../../session-key-BPgKfU2m.mjs";import{i as listAgentIds}from"../../agent-roster-4XrgUgPK.mjs";import{l as resolveSessionStorePathCore}from"../../paths-CcxgiC3g.mjs";import{f as resolveAgentWorkspaceDir}from"../../agent-scope-config-CG9CFpGX.mjs";import{t as createSubsystemLogger}from"../../subsystem-DLCDFvCH.mjs";import{t as dedupeByKey}from"../../dedupe-by-key-Bdyq9B5M.mjs";import"../../agent-scope-Dw5XRSEh.mjs";import{n as SILENT_REPLY_TOKEN}from"../../tokens-DD0Y_anD.mjs";import{a as isGatewayStartupEvent}from"../../internal-hooks-C8TThetJ.mjs";import{a as resolveMainSessionKey,r as resolveAgentMainSessionKey}from"../../main-session-C6FGqx0x.mjs";import{o as escapeInternalRuntimeContextDelimiters}from"../../internal-runtime-context-DervRrPi.mjs";import"../../session-accessor-CY8UYP4m.mjs";import{g as applySessionEntryLifecycleMutation}from"../../session-accessor.entry-mutation-CQIwywNM.mjs";import{t as createDefaultDeps}from"../../deps-DA-Pf4pS.mjs";import{i as agentCommandFromSystem}from"../../agent-command-ORwedrZc.mjs";import{r as setBootEchoContextForSession,t as clearBootEchoContextForSession}from"../../boot-echo-guard-DcCihBUI.mjs";import"../../agent-Chuyz2w5.mjs";import{t as runStartupTasks}from"../../startup-tasks-CBna_rkI.mjs";import path from"node:path";import crypto from"node:crypto";import fs from"node:fs/promises";import{readRegularFile}from"@openclaw/fs-safe/advanced";
//!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(`
`)}];return{message:`Run the workspace boot check now.`,runtimeContextFragments:t,echoContext:t.map(e=>e.text).join(`
`)}}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`}}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(`; `)}}
//!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})};export{runBootChecklist as default};