fix(commands): wire /stop across chat commands
This commit is contained in:
@@ -121,38 +121,6 @@ export function createTelegramBot(opts: TelegramBotOptions) {
|
||||
const mediaGroupBuffer = new Map<string, MediaGroupEntry>();
|
||||
|
||||
const cfg = loadConfig();
|
||||
|
||||
// Handle /stop command - abort current agent run
|
||||
// Registered before bot.on("message") to ensure it runs first via grammy's middleware chain
|
||||
bot.command("stop", async (ctx) => {
|
||||
const { abortEmbeddedPiRun, isEmbeddedPiRunActive } = await import(
|
||||
"../agents/pi-embedded.js"
|
||||
);
|
||||
const msg = ctx.message;
|
||||
const chatId = msg?.chat.id;
|
||||
const isGroup =
|
||||
msg?.chat.type === "group" || msg?.chat.type === "supergroup";
|
||||
const messageThreadId = (msg as { message_thread_id?: number })
|
||||
?.message_thread_id;
|
||||
|
||||
// Build session key matching the format used by the reply flow
|
||||
const sessionCfg = cfg.session;
|
||||
const mainKey = (sessionCfg?.mainKey ?? "main").trim() || "main";
|
||||
const sessionId = isGroup
|
||||
? messageThreadId
|
||||
? `telegram:group:${chatId}:topic:${messageThreadId}`
|
||||
: `telegram:group:${chatId}`
|
||||
: mainKey;
|
||||
|
||||
const wasActive = isEmbeddedPiRunActive(sessionId);
|
||||
const aborted = abortEmbeddedPiRun(sessionId);
|
||||
const statusMsg = wasActive
|
||||
? aborted
|
||||
? "🛑 stopped"
|
||||
: "🛑 stop requested (finishing current step)"
|
||||
: "nothing running";
|
||||
await ctx.reply(statusMsg);
|
||||
});
|
||||
const textLimit = resolveTextChunkLimit(cfg, "telegram");
|
||||
const dmPolicy = cfg.telegram?.dmPolicy ?? "pairing";
|
||||
const allowFrom = opts.allowFrom ?? cfg.telegram?.allowFrom;
|
||||
@@ -602,6 +570,14 @@ export function createTelegramBot(opts: TelegramBotOptions) {
|
||||
}
|
||||
|
||||
const prompt = buildCommandText(command.name, ctx.match ?? "");
|
||||
const route = resolveAgentRoute({
|
||||
cfg,
|
||||
provider: "telegram",
|
||||
peer: {
|
||||
kind: isGroup ? "group" : "dm",
|
||||
id: String(chatId),
|
||||
},
|
||||
});
|
||||
const ctxPayload = {
|
||||
Body: prompt,
|
||||
From: isGroup ? `group:${chatId}` : `telegram:${chatId}`,
|
||||
@@ -618,6 +594,7 @@ export function createTelegramBot(opts: TelegramBotOptions) {
|
||||
CommandAuthorized: commandAuthorized,
|
||||
CommandSource: "native" as const,
|
||||
SessionKey: `telegram:slash:${senderId || chatId}`,
|
||||
CommandTargetSessionKey: route.sessionKey,
|
||||
};
|
||||
|
||||
const replyResult = await getReplyFromConfig(
|
||||
|
||||
Reference in New Issue
Block a user