merge: resolve conflict with upstream/develop

Entire-Checkpoint: 807ef9bbad32
This commit is contained in:
Leo Agent 2026-03-23 16:59:51 +07:00
commit 8b0863f6d4
4 changed files with 38 additions and 15 deletions

View File

@ -367,6 +367,7 @@ export class TelegramAdapter extends ChannelAdapter<OpenACPCore> {
private setupRoutes(): void {
this.bot.on("message:text", async (ctx) => {
const threadId = ctx.message.message_thread_id;
const text = ctx.message.text;
// Check for pending workspace input from interactive /new flow
if (await handlePendingWorkspaceInput(ctx, this.core, this.telegramConfig.chatId, this.assistantTopicId)) {
@ -386,6 +387,11 @@ export class TelegramAdapter extends ChannelAdapter<OpenACPCore> {
// Notification topic → ignore
if (threadId === this.notificationTopicId) return;
// Strip leading "/" from unrecognized commands — registered commands
// (e.g. /new, /cancel) are already handled by bot.command() above.
// Unrecognized slash commands can cause agent subprocesses to hang.
const forwardText = text.startsWith("/") ? text.slice(1) : text;
// Assistant topic → forward to assistant session
if (threadId === this.assistantTopicId) {
if (!this.assistantSession) {
@ -394,7 +400,7 @@ export class TelegramAdapter extends ChannelAdapter<OpenACPCore> {
}
await this.draftManager.finalize(this.assistantSession.id, this.assistantSession.id);
ctx.replyWithChatAction("typing").catch(() => {});
handleAssistantMessage(this.assistantSession, ctx.message.text).catch(
handleAssistantMessage(this.assistantSession, forwardText).catch(
(err) => log.error({ err }, "Assistant error"),
);
return;
@ -413,7 +419,7 @@ export class TelegramAdapter extends ChannelAdapter<OpenACPCore> {
channelId: "telegram",
threadId: String(threadId),
userId: String(ctx.from.id),
text: ctx.message.text,
text: forwardText,
})
.catch((err) => log.error({ err }, "handleMessage error"));
});
@ -854,7 +860,8 @@ export class TelegramAdapter extends ChannelAdapter<OpenACPCore> {
const sessionId = this.resolveSessionId(threadId) || "unknown";
const att = await this.fileService.saveFile(sessionId, fileName, buffer, mimeType);
const text = caption || `[${att.type === "image" ? "Photo" : att.type === "audio" ? "Audio" : "File"}: ${att.fileName}]`;
const rawText = caption || `[${att.type === "image" ? "Photo" : att.type === "audio" ? "Audio" : "File"}: ${att.fileName}]`;
const text = rawText.startsWith("/") ? rawText.slice(1) : rawText;
// Assistant topic
if (threadId === this.assistantTopicId) {

View File

@ -31,18 +31,18 @@ export async function handleCancel(
String(threadId),
);
if (session) {
log.info({ sessionId: session.id }, "Cancel session command");
log.info({ sessionId: session.id }, "Abort prompt command");
await session.abortPrompt();
await ctx.reply("⛔ Session cancelled.", { parse_mode: "HTML" });
await ctx.reply("⛔ Prompt aborted. Session is still active — send a new message to continue.", { parse_mode: "HTML" });
return;
}
// Fallback: cancel from store when session not in memory (e.g. after restart)
// Fallback: session not in memory — nothing to abort, but session can
// still be resumed when the user sends a new message.
const record = core.sessionManager.getRecordByThread("telegram", String(threadId));
if (record && record.status !== "cancelled" && record.status !== "error") {
log.info({ sessionId: record.sessionId }, "Cancel session command (from store)");
await core.sessionManager.cancelSession(record.sessionId);
await ctx.reply("⛔ Session cancelled.", { parse_mode: "HTML" });
if (record && record.status !== "error") {
log.info({ sessionId: record.sessionId, status: record.status }, "Cancel command — no active prompt to abort");
await ctx.reply("ℹ️ No active prompt to cancel. Send a new message to resume the session.", { parse_mode: "HTML" });
}
}

View File

@ -474,15 +474,15 @@ export class OpenACPCore {
return null;
}
// Don't resume cancelled/error sessions
if (record.status === "cancelled" || record.status === "error") {
// Don't resume errored sessions (cancelled sessions can still be resumed)
if (record.status === "error") {
log.debug(
{
threadId: message.threadId,
sessionId: record.sessionId,
status: record.status,
},
"Skipping resume of cancelled/error session",
"Skipping resume of error session",
);
return null;
}

View File

@ -6,6 +6,7 @@ import type { Attachment } from './types.js'
export class PromptQueue {
private queue: Array<{ text: string; attachments?: Attachment[]; resolve: () => void }> = []
private processing = false
private abortController: AbortController | null = null
constructor(
private processor: (text: string, attachments?: Attachment[]) => Promise<void>,
@ -23,11 +24,22 @@ export class PromptQueue {
private async process(text: string, attachments?: Attachment[]): Promise<void> {
this.processing = true
this.abortController = new AbortController()
const { signal } = this.abortController
try {
await this.processor(text, attachments)
await Promise.race([
this.processor(text, attachments),
new Promise<never>((_, reject) => {
signal.addEventListener('abort', () => reject(new Error('Prompt aborted')), { once: true })
}),
])
} catch (err) {
this.onError?.(err)
// Only forward non-abort errors to onError handler
if (!(err instanceof Error && err.message === 'Prompt aborted')) {
this.onError?.(err)
}
} finally {
this.abortController = null
this.processing = false
this.drainNext()
}
@ -41,6 +53,10 @@ export class PromptQueue {
}
clear(): void {
// Abort the currently running prompt so the queue can drain
if (this.abortController) {
this.abortController.abort()
}
// Resolve pending promises so callers don't hang
for (const item of this.queue) {
item.resolve()