From 0df61a9ac079d43ebfdfd99b0b8ff023064ebc11 Mon Sep 17 00:00:00 2001 From: Leo Agent Date: Fri, 20 Mar 2026 14:28:52 +0700 Subject: [PATCH] refactor(commands): extract executeNewSession and executeCancelSession --- src/adapters/telegram/commands.ts | 66 ++++++++++++++++++++++++++++++- 1 file changed, 65 insertions(+), 1 deletion(-) diff --git a/src/adapters/telegram/commands.ts b/src/adapters/telegram/commands.ts index 5483dde..438ef1e 100644 --- a/src/adapters/telegram/commands.ts +++ b/src/adapters/telegram/commands.ts @@ -1,8 +1,9 @@ import type { Bot, Context } from "grammy"; import { InlineKeyboard } from "grammy"; import type { OpenACPCore } from "../../core/index.js"; +import type { Session } from "../../core/session.js"; import { escapeHtml } from "./formatting.js"; -import { createSessionTopic } from "./topics.js"; +import { createSessionTopic, renameSessionTopic } from "./topics.js"; import { createChildLogger } from "../../core/log.js"; import { nanoid } from "nanoid"; import type { AgentCommand } from "../../core/index.js"; @@ -351,6 +352,69 @@ export function setupSkillCallbacks(bot: Bot, core: OpenACPCore): void { }); } +export async function executeNewSession( + bot: Bot, + core: OpenACPCore, + chatId: number, + agentName?: string, + workspace?: string, +): Promise<{ session: Session; threadId: number }> { + // Create topic with generic name first (same as original handleNew) + const threadId = await createSessionTopic(bot, chatId, "🔄 New Session"); + + await bot.api.sendMessage(chatId, "⏳ Setting up session, please wait...", { + message_thread_id: threadId, + parse_mode: "HTML", + }); + + try { + // core.handleNewSession() already wires events internally — do NOT call wireSessionEvents again + const session = await core.handleNewSession( + "telegram", + agentName, + workspace, + ); + session.threadId = String(threadId); + + await core.sessionManager.updateSessionPlatform(session.id, { + topicId: threadId, + }); + + // Rename topic with agent name after session is created + const finalName = `🔄 ${session.agentName} — New Session`; + await renameSessionTopic(bot, chatId, threadId, finalName); + + // Warm up model cache in background while user types + session.warmup().catch((err) => log.error({ err }, "Warm-up error")); + + return { session, threadId }; + } catch (err) { + // Clean up orphaned topic on failure + try { + await bot.api.deleteForumTopic(chatId, threadId); + } catch { + /* best effort */ + } + throw err; + } +} + +export async function executeCancelSession( + core: OpenACPCore, + excludeSessionId?: string, +): Promise { + const sessions = core.sessionManager + .listSessions("telegram") + .filter((s) => s.status === "active" && s.id !== excludeSessionId) + .sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime()); + + const session = sessions[0]; + if (!session) return null; + + await session.cancel(); + return session; +} + export const STATIC_COMMANDS = [ { command: "new", description: "Create new session" }, { command: "new_chat", description: "New chat, same agent & workspace" },