UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

5 lines (4 loc) • 3.7 kB
import{l as isRecord}from"./record-coerce-BTbqdJ3-.mjs";import{N as parseAgentSessionKey}from"./session-key-BPgKfU2m.mjs";import{_ as resolveDefaultAgentId}from"./agent-scope-config-CG9CFpGX.mjs";import"./agent-scope-Dw5XRSEh.mjs";import{o as resolveSessionStorePathForScope}from"./session-store-path-Co-EGagp.mjs";import{l as triggerInternalHook,n as createInternalHookEvent}from"./internal-hooks-C8TThetJ.mjs";import{t as formatSqliteSessionFileMarker}from"./legacy-sqlite-marker-BOXSzMrL.mjs";import{f as selectSessionTranscriptLeafControlledPath}from"./transcript-tree-Dwc2Zlw1.mjs";import{x as withSessionTranscriptHydrationCaller}from"./session-transcript-worker-resources-BCJ3nyGg.mjs";import"./session-accessor-CY8UYP4m.mjs";import{t as loadSessionTranscriptEvents}from"./session-transcript-events-C7eeuw86.mjs";import{r as logVerbose}from"./globals-l9I53ZKA.mjs";import{n as getGlobalHookRunner}from"./hook-runner-global-DNCi6wD6.mjs"; //!src/auto-reply/reply/commands-reset-hooks.ts function parseTranscriptMessages(e){return(selectSessionTranscriptLeafControlledPath(e)??e).flatMap(e=>isRecord(e)&&e.type===`message`&&e.message?[e.message]:[])}async function readBeforeResetMessages(e){const{sessionId:t,sessionKey:n,storePath:r}=e;if(!t||!n||!r){logVerbose(`before_reset: no session identity available, firing hook with empty messages`);return[]}try{return parseTranscriptMessages(await withSessionTranscriptHydrationCaller(`reset-hook`,()=>loadSessionTranscriptEvents({...e.agentId?{agentId:e.agentId}:{},sessionId:t,sessionKey:n,storePath:r})))}catch(t){logVerbose(`before_reset: failed to read transcript identity ${e.sessionKey}/${e.sessionId}; firing hook with empty messages (${String(t)})`);return[]}}async function emitResetCommandHooks(e){const t=parseAgentSessionKey(e.sessionKey)?.agentId??e.agentId??resolveDefaultAgentId(e.cfg);const n=t&&e.storePath?resolveSessionStorePathForScope({agentId:t,sessionKey:e.sessionKey,storePath:e.storePath}):e.storePath;const r=createInternalHookEvent(`command`,e.action,e.sessionKey??``,{agentId:t,sessionEntry:e.sessionEntry,previousSessionEntry:e.previousSessionEntry,previousSessionMemory:e.previousSessionMemory,commandSource:e.command.surface,senderId:e.command.senderId,workspaceDir:e.workspaceDir,storePath:n,cfg:e.cfg});await triggerInternalHook(r);e.command.resetHookTriggered=true;let i=false;if(r.messages.length>0){const n=e.ctx.OriginatingChannel||e.command.channel;const a=e.ctx.OriginatingTo||e.command.from||e.command.to;if(n&&a){const{routeReply:o}=await import(`./route-reply-CTRzSl4t.mjs`);const s=await o({payload:{text:r.messages.join(` `)},channel:n,to:a,agentId:t,sessionKey:e.sessionKey,accountId:e.ctx.AccountId,requesterSenderId:e.command.senderId,requesterSenderName:e.ctx.SenderName,requesterSenderUsername:e.ctx.SenderUsername,requesterSenderE164:e.ctx.SenderE164,threadId:e.ctx.MessageThreadId,cfg:e.cfg,replyKind:`final`});if(s.delivered)await e.onObservedReplyDelivery?.();i=s.delivered||s.suppressed===true}}const a=getGlobalHookRunner();if(a?.hasHooks(`before_reset`)){const r=e.previousSessionEntry;const i=t;const o=n;const s=i&&r?.sessionId&&o?formatSqliteSessionFileMarker({agentId:i,sessionId:r.sessionId,storePath:o}):e.sessionKey;const c=e.previousSessionResetMessages??await readBeforeResetMessages({agentId:i,sessionId:r?.sessionId,sessionKey:e.sessionKey,storePath:o});(async()=>{try{await a.runBeforeReset({sessionFile:s,messages:c,reason:e.action},{agentId:i,sessionKey:e.sessionKey,sessionId:r?.sessionId,workspaceDir:e.workspaceDir})}catch(e){logVerbose(`before_reset hook failed: ${String(e)}`)}})()}return{routedReply:i}}export{readBeforeResetMessages as n,emitResetCommandHooks as t};