openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
5 lines • 6.25 kB
JavaScript
import{t as createLazyImportLoader}from"./lazy-promise-CismJCQR.mjs";import{y as isAcpSessionKey}from"./session-key-BPgKfU2m.mjs";import{a as resolveAgentDir,x as resolveSessionAgentId}from"./agent-scope-config-CG9CFpGX.mjs";import"./agent-scope-Dw5XRSEh.mjs";import{a as isInternalMessageChannel}from"./message-channel-514k57tV.mjs";import{o as copyReplyPayloadMetadata}from"./reply-payload-C7eQHHoS.mjs";import{n as isResetAuthorizedForContext}from"./command-auth-CCzMsjar.mjs";import"./session-accessor-CY8UYP4m.mjs";import{t as clearAllCliSessions}from"./cli-session-binding-uwQzKK6C.mjs";import{c as updateSessionEntry}from"./session-accessor.entry-mutation-CQIwywNM.mjs";import{r as logVerbose}from"./globals-l9I53ZKA.mjs";import"./cli-session-DVohIxV9.mjs";import{u as resolveCommandTurnTargetSessionKey}from"./command-turn-context-vtXH8Cqw.mjs";import{t as shouldHandleTextCommands}from"./commands-text-routing-CDTqRuxF.mjs";import"./commands-registry-Dh5K81S2.mjs";import{t as parseSoftResetCommand}from"./commands-reset-mode-By3wCk7E.mjs";import{t as applyCommandTextToContext}from"./command-context-rewrite-B53-2J5F.mjs";import{n as commandReply}from"./command-gates-BMLoLImo.mjs";import{t as buildCommandContext}from"./commands-context-c5lPk2Z8.mjs";import{t as emitResetCommandHooks}from"./commands-reset-hooks-BTxwuqYZ.mjs";import{n as resetConfiguredBindingTargetInPlace}from"./binding-targets-B5Kz5NpS.mjs";import{n as resolveBoundAcpThreadSessionKey}from"./targets-q0eTYMpe.mjs";import{n as buildStatusReply}from"./commands-status-BKfAl9dR.mjs";
//!src/auto-reply/reply/commands-reset.ts
function applyAcpResetTailContext(e,t){applyCommandTextToContext(e,t);e.AcpDispatchTailAfterReset=true}async function isResetAuthorized(e){return isResetAuthorizedForContext({ctx:e.ctx,cfg:e.cfg,commandAuthorized:e.command.isAuthorizedSender||e.ctx.CommandAuthorized===true})}async function maybeHandleResetCommand(e){const t=e.command.commandBodyNormalized.match(/^\/(new|reset)(?:\s|$)/i);if(!t)return null;if(!await isResetAuthorized(e)){logVerbose(`Ignoring /${t[1]} from unauthorized sender: ${e.command.senderId||`<unknown>`}`);return isInternalMessageChannel(e.ctx.Provider||e.ctx.Surface)&&isInternalMessageChannel(e.command.channel)?commandReply(`⚠️ You are not authorized to reset this session. Gateway resets require operator.admin and command access. Ask your administrator to reset it, or send your message without the command.`):{shouldContinue:false}}const n=resolveCommandTurnTargetSessionKey(e.ctx);const r=parseSoftResetCommand(e.command.commandBodyNormalized);const i=t[1]?.toLowerCase()===`reset`?`reset`:`new`;const a=e.command.commandBodyNormalized.slice(t[0].length).trimStart();const o=await resolveBoundAcpThreadSessionKey(e,n);e.opts?.abortSignal?.throwIfAborted();const s=o&&isAcpSessionKey(o)?o.trim():void 0;if(r.matched){if(s)return commandReply(`Usage: /reset soft is not available for ACP-bound sessions yet.`);const t=e.sessionStore?.[e.sessionKey]??e.sessionEntry;const n=e.previousSessionEntry??(t?{...t}:void 0);if(t){const n=Date.now();clearAllCliSessions(t);if(e.sessionEntry&&e.sessionEntry!==t){clearAllCliSessions(e.sessionEntry);e.sessionEntry.updatedAt=n;e.sessionEntry.lastInteractionAt=n}t.updatedAt=n;t.lastInteractionAt=n;if(e.sessionStore&&e.sessionKey)e.sessionStore[e.sessionKey]=t;if(e.storePath&&e.sessionKey)await updateSessionEntry({storePath:e.storePath,sessionKey:e.sessionKey},e=>{const t={...e};clearAllCliSessions(t);return{cliSessionBindings:t.cliSessionBindings,cliSessionIds:t.cliSessionIds,claudeCliSessionId:t.claudeCliSessionId,updatedAt:n,lastInteractionAt:n}},{consumePendingReset:true})}await emitResetCommandHooks({...e,action:`reset`,sessionEntry:t,previousSessionEntry:n,onObservedReplyDelivery:e.opts?.onObservedReplyDelivery});e.command.softResetTriggered=true;e.command.softResetTail=r.tail;return null}if(s){const t=await resetConfiguredBindingTargetInPlace({cfg:e.cfg,sessionKey:s,reason:i,commandSource:`${e.command.surface}:${e.ctx.CommandSource??`text`}`});if(!t.ok)logVerbose(`acp reset failed for ${s}: ${t.error??`unknown error`}`);if(t.ok){if(t.sessionId)e.opts?.onSessionPrepared?.({sessionKey:t.sessionKey??s,sessionId:t.sessionId,lifecycleRevision:t.lifecycleRevision,storePath:t.storePath});e.command.resetHookTriggered=true;if(a){applyAcpResetTailContext(e.ctx,a);if(e.rootCtx&&e.rootCtx!==e.ctx)applyAcpResetTailContext(e.rootCtx,a);return{shouldContinue:false}}}return commandReply({text:t.ok?`✅ ACP session reset in place.`:`⚠️ ACP session reset failed. Check /acp status and try again.`,isStatusNotice:true})}const c=e.sessionStore?.[e.sessionKey]??e.sessionEntry;const l=await emitResetCommandHooks({...e,action:i,sessionEntry:c,onObservedReplyDelivery:e.opts?.onObservedReplyDelivery});if(!a)return l.routedReply?{shouldContinue:false}:commandReply({text:i===`reset`?`✅ Session reset.`:`✅ New session started.`,isStatusNotice:true});return null}
//!src/auto-reply/reply/commands-core.ts
const commandHandlersRuntimeLoader=createLazyImportLoader(async()=>(await import(`./commands-handlers.runtime-TI43wI2a.mjs`)).loadCommandHandlers());function normalizeCommandHandlerResult(e){if(!e.reply)return e;return{...e,reply:copyReplyPayloadMetadata(e.reply,{...e.reply,replyToId:void 0,replyToCurrent:false})}}async function handleCommands(e){if(e.ctx.CommandInterpretationSuppressed===true)return{shouldContinue:true};const t=e.allowCreateSessionEntry===true;const n=e.initialSessionEntry??(t?void 0:e.sessionEntry?{...e.sessionEntry}:void 0);const r=resolveSessionAgentId({sessionKey:e.sessionKey,config:e.cfg,fallbackAgentId:e.agentId});const{resolveModelLevels:i,...a}=e;const o={...a,agentId:r,agentDir:r===e.agentId?e.agentDir:resolveAgentDir(e.cfg,r),initialSessionEntry:n,allowCreateSessionEntry:t};const s=await maybeHandleResetCommand(o);if(s)return normalizeCommandHandlerResult(s);const c={...o,...await i()};const l=await commandHandlersRuntimeLoader.load();const u=shouldHandleTextCommands({cfg:e.cfg,surface:e.command.surface,commandSource:e.ctx.CommandSource});for(const e of l){const t=await e(c,u);if(t)return normalizeCommandHandlerResult(t)}return{shouldContinue:true}}export{buildCommandContext,buildStatusReply,handleCommands};