111 lines
3.8 KiB
TypeScript
Raw Normal View History

import { resolveSessionAgentId } from "../../agents/agent-scope.js";
2026-01-10 01:26:20 +01:00
import { abortEmbeddedPiRun } from "../../agents/pi-embedded.js";
import type { ClawdbotConfig } from "../../config/config.js";
import {
loadSessionStore,
resolveStorePath,
type SessionEntry,
updateSessionStore,
2026-01-10 01:26:20 +01:00
} from "../../config/sessions.js";
import { parseAgentSessionKey } from "../../routing/session-key.js";
2026-01-10 01:26:20 +01:00
import { resolveCommandAuthorization } from "../command-auth.js";
import { normalizeCommandBody } from "../commands-registry.js";
2026-01-10 01:26:20 +01:00
import type { MsgContext } from "../templating.js";
import { stripMentions, stripStructuralPrefixes } from "./mentions.js";
const ABORT_TRIGGERS = new Set(["stop", "esc", "abort", "wait", "exit", "interrupt"]);
2026-01-04 05:47:21 +01:00
const ABORT_MEMORY = new Map<string, boolean>();
export function isAbortTrigger(text?: string): boolean {
if (!text) return false;
const normalized = text.trim().toLowerCase();
return ABORT_TRIGGERS.has(normalized);
}
export function getAbortMemory(key: string): boolean | undefined {
return ABORT_MEMORY.get(key);
}
export function setAbortMemory(key: string, value: boolean): void {
ABORT_MEMORY.set(key, value);
}
2026-01-10 01:26:20 +01:00
function resolveSessionEntryForKey(
store: Record<string, SessionEntry> | undefined,
2026-01-10 01:26:20 +01:00
sessionKey: string | undefined,
) {
if (!store || !sessionKey) return {};
const direct = store[sessionKey];
if (direct) return { entry: direct, key: sessionKey };
const parsed = parseAgentSessionKey(sessionKey);
const legacyKey = parsed?.rest;
if (legacyKey && store[legacyKey]) {
return { entry: store[legacyKey], key: legacyKey };
}
return {};
}
function resolveAbortTargetKey(ctx: MsgContext): string | undefined {
const target = ctx.CommandTargetSessionKey?.trim();
if (target) return target;
const sessionKey = ctx.SessionKey?.trim();
return sessionKey || undefined;
}
export async function tryFastAbortFromMessage(params: {
ctx: MsgContext;
cfg: ClawdbotConfig;
}): Promise<{ handled: boolean; aborted: boolean }> {
const { ctx, cfg } = params;
const commandAuthorized = ctx.CommandAuthorized ?? true;
const auth = resolveCommandAuthorization({
ctx,
cfg,
commandAuthorized,
});
if (!auth.isAuthorizedSender) return { handled: false, aborted: false };
const targetKey = resolveAbortTargetKey(ctx);
const agentId = resolveSessionAgentId({
sessionKey: targetKey ?? ctx.SessionKey ?? "",
config: cfg,
});
2026-01-10 18:53:33 +01:00
// Use RawBody/CommandBody for abort detection (clean message without structural context).
const raw = stripStructuralPrefixes(ctx.CommandBody ?? ctx.RawBody ?? ctx.Body ?? "");
2026-01-10 01:26:20 +01:00
const isGroup = ctx.ChatType?.trim().toLowerCase() === "group";
const stripped = isGroup ? stripMentions(raw, ctx, cfg, agentId) : raw;
const normalized = normalizeCommandBody(stripped);
const abortRequested = normalized === "/stop" || isAbortTrigger(stripped);
if (!abortRequested) return { handled: false, aborted: false };
const abortKey = targetKey ?? auth.from ?? auth.to;
if (targetKey) {
const storePath = resolveStorePath(cfg.session?.store, { agentId });
const store = loadSessionStore(storePath);
const { entry, key } = resolveSessionEntryForKey(store, targetKey);
const sessionId = entry?.sessionId;
const aborted = sessionId ? abortEmbeddedPiRun(sessionId) : false;
if (entry && key) {
entry.abortedLastRun = true;
entry.updatedAt = Date.now();
store[key] = entry;
await updateSessionStore(storePath, (nextStore) => {
const nextEntry = nextStore[key] ?? entry;
if (!nextEntry) return;
nextEntry.abortedLastRun = true;
nextEntry.updatedAt = Date.now();
nextStore[key] = nextEntry;
});
2026-01-10 01:26:20 +01:00
} else if (abortKey) {
setAbortMemory(abortKey, true);
}
return { handled: true, aborted };
}
if (abortKey) {
setAbortMemory(abortKey, true);
}
return { handled: true, aborted: false };
}