openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
184 lines (183 loc) • 7.37 kB
JavaScript
import { y as isAcpSessionKey } from "./session-key-CqAyZeVk.mjs";
import { a as resolveAgentDir, x as resolveSessionAgentId } from "./agent-scope-config-DmXgFGKP.mjs";
import { t as createLazyImportLoader } from "./lazy-promise-DiCvjz9w.mjs";
import "./agent-scope-BwB8YH6y.mjs";
import { a as isInternalMessageChannel } from "./message-channel-DTYVLJT1.mjs";
import { o as copyReplyPayloadMetadata } from "./reply-payload-k5WsQWxi.mjs";
import "./session-accessor-CizTbDQj.mjs";
import { t as clearAllCliSessions } from "./cli-session-binding-BEOgFSDg.mjs";
import { c as updateSessionEntry } from "./session-accessor.entry-mutation-IJ4DYZ4a.mjs";
import { r as logVerbose } from "./globals-CgepmiqE.mjs";
import { n as isResetAuthorizedForContext } from "./command-auth-CC1Z19Eh.mjs";
import "./cli-session-BsEOjZRc.mjs";
import { u as resolveCommandTurnTargetSessionKey } from "./command-turn-context-BaN4kAo-.mjs";
import "./commands-registry-CwAxsNJ9.mjs";
import { t as shouldHandleTextCommands } from "./commands-text-routing-BYEg7XMZ.mjs";
import { t as parseSoftResetCommand } from "./commands-reset-mode-DFGlMKnP.mjs";
import { t as applyCommandTextToContext } from "./command-context-rewrite-Ak_4dOLJ.mjs";
import { n as commandReply } from "./command-gates-BZ-IYgiD.mjs";
import { t as buildCommandContext } from "./commands-context-1C0vIajQ.mjs";
import { t as emitResetCommandHooks } from "./commands-reset-hooks-QDcTu3Fs.mjs";
import { n as resetConfiguredBindingTargetInPlace } from "./binding-targets-DfrM3oJ_.mjs";
import { n as resolveBoundAcpThreadSessionKey } from "./targets-BmjrxBCj.mjs";
import { n as buildStatusReply } from "./commands-status-3g9MDyxC.mjs";
//#region src/auto-reply/reply/commands-reset.ts
/** Handles /new and /reset command flows, including soft reset and ACP-bound sessions. */
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
});
}
/** Handles reset/new commands or returns null when another command handler should continue. */
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;
}
//#endregion
//#region src/auto-reply/reply/commands-core.ts
const commandHandlersRuntimeLoader = createLazyImportLoader(async () => (await import("./commands-handlers.runtime.js")).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 };
}
//#endregion
export { buildCommandContext, buildStatusReply, handleCommands };