fix: non-blocking Claude calls, validate directory exists before start
This commit is contained in:
+6
-7
@@ -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(
|
||||
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}`);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -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(
|
||||
// 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) => {
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user