From 0f8b6a347c1561d55cb48a67767aa10fc8ee834d Mon Sep 17 00:00:00 2001 From: neken Date: Fri, 10 Apr 2026 15:58:47 +0500 Subject: [PATCH] fix: non-blocking Claude calls, validate directory exists before start --- src/bot.ts | 17 ++++++++--------- src/handlers/commands.ts | 25 ++++++++++++++++--------- src/handlers/message.ts | 13 ++++++------- 3 files changed, 30 insertions(+), 25 deletions(-) diff --git a/src/bot.ts b/src/bot.ts index d74cead..0f7291b 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -54,16 +54,15 @@ export function createBot(token: string, ownerId: number): Bot { await ctx.answerCallbackQuery({ text: `Starting "${resolved.name}"...` }); await ctx.reply(`Starting session "${resolved.name}" in ${resolved.path}...`); - try { - const { sessionId, result } = await claude.createSession( - "You are now connected via Telegram. Briefly confirm you're ready and state the project directory.", - resolved.path, - ); + claude.createSession( + "You are now connected via Telegram. Briefly confirm you're ready and state the project directory.", + resolved.path, + ).then(({ sessionId, result }) => { manager.createSession(resolved.name, resolved.path, sessionId); - await ctx.reply(`Session "${resolved.name}" created.\n\n${result}`); - } catch (err) { - await ctx.reply(`Failed to create session: ${err}`); - } + ctx.reply(`Session "${resolved.name}" created.\n\n${result}`); + }).catch((err) => { + ctx.reply(`Failed to create session: ${err}`); + }); } }); diff --git a/src/handlers/commands.ts b/src/handlers/commands.ts index 9db8404..37ceee0 100644 --- a/src/handlers/commands.ts +++ b/src/handlers/commands.ts @@ -1,4 +1,5 @@ import { type Context, InlineKeyboard } from "grammy"; +import { existsSync } from "fs"; import { SessionManager } from "../services/session-manager.js"; import { ClaudeService } from "../services/claude.js"; @@ -20,6 +21,12 @@ export function registerCommands( return; } + // Validate that directory exists (skip for bookmarks — already validated on save) + if (!existsSync(resolved.path)) { + await ctx.reply(`Directory not found: ${resolved.path}`); + return; + } + if (manager.getAllSessions()[resolved.name]) { await ctx.reply( `Session "${resolved.name}" already exists. Use /switch ${resolved.name}`, @@ -29,16 +36,16 @@ export function registerCommands( await ctx.reply(`Starting session "${resolved.name}" in ${resolved.path}...`); - try { - const { sessionId, result } = await claude.createSession( - "You are now connected via Telegram. Briefly confirm you're ready and state the project directory.", - resolved.path, - ); + // Run Claude session creation without blocking the bot + claude.createSession( + "You are now connected via Telegram. Briefly confirm you're ready and state the project directory.", + resolved.path, + ).then(({ sessionId, result }) => { manager.createSession(resolved.name, resolved.path, sessionId); - await ctx.reply(`Session "${resolved.name}" created.\n\n${result}`); - } catch (err) { - await ctx.reply(`Failed to create session: ${err}`); - } + ctx.reply(`Session "${resolved.name}" created.\n\n${result}`); + }).catch((err) => { + ctx.reply(`Failed to create session: ${err}`); + }); }; onCommand["stop"] = async (ctx) => { diff --git a/src/handlers/message.ts b/src/handlers/message.ts index 805ec1b..6514576 100644 --- a/src/handlers/message.ts +++ b/src/handlers/message.ts @@ -28,18 +28,17 @@ export function createMessageHandler( manager.setStatus(activeName, "busy"); await ctx.replyWithChatAction("typing"); - try { - const result = await claude.sendMessage(text, session.sessionId); + // Run Claude call without blocking the bot's event loop + claude.sendMessage(text, session.sessionId).then(async (result) => { const response = result || "(empty response)"; const chunks = splitMessage(response); - for (const chunk of chunks) { await ctx.reply(chunk); } - } catch (err) { - await ctx.reply(`Error: ${err}`); - } finally { + }).catch((err) => { + ctx.reply(`Error: ${err}`); + }).finally(() => { manager.setStatus(activeName, "idle"); - } + }); }; }