fix: avoid threaded replies for agent output

This commit is contained in:
Peter Steinberger
2025-12-24 22:36:30 +01:00
parent 009fbeb543
commit cf8d1cf0e7
4 changed files with 9 additions and 34 deletions

View File

@@ -148,7 +148,7 @@ describe("createTelegramBot", () => {
expect(payload.ReplyToSender).toBe("Ada");
});
it("sends replies as native replies without chaining", async () => {
it("sends replies without native reply threading", async () => {
onSpy.mockReset();
sendMessageSpy.mockReset();
const replySpy = replyModule.__replySpy as unknown as ReturnType<
@@ -174,7 +174,7 @@ describe("createTelegramBot", () => {
expect(sendMessageSpy.mock.calls.length).toBeGreaterThan(1);
for (const call of sendMessageSpy.mock.calls) {
expect(call[2]?.reply_to_message_id).toBe(101);
expect(call[2]?.reply_to_message_id).toBeUndefined();
}
});
});

View File

@@ -199,7 +199,6 @@ export function createTelegramBot(opts: TelegramBotOptions) {
token: opts.token,
runtime,
bot,
replyToMessageId: msg.message_id,
});
} catch (err) {
runtime.error?.(danger(`Telegram handler failed: ${String(err)}`));
@@ -222,10 +221,8 @@ async function deliverReplies(params: {
token: string;
runtime: RuntimeEnv;
bot: Bot;
replyToMessageId?: number;
}) {
const { replies, chatId, runtime, bot } = params;
const replyTarget = params.replyToMessageId;
for (const reply of replies) {
if (!reply?.text && !reply?.mediaUrl && !(reply?.mediaUrls?.length ?? 0)) {
runtime.error?.(danger("Telegram reply missing text/media"));
@@ -236,14 +233,9 @@ async function deliverReplies(params: {
: reply.mediaUrl
? [reply.mediaUrl]
: [];
if (replyTarget && isVerbose()) {
logVerbose(
`telegram reply-send: chatId=${chatId} replyToMessageId=${replyTarget} kind=${mediaList.length ? "media" : "text"}`,
);
}
if (mediaList.length === 0) {
for (const chunk of chunkText(reply.text || "", 4000)) {
await sendTelegramText(bot, chatId, chunk, runtime, replyTarget);
await sendTelegramText(bot, chatId, chunk, runtime);
}
continue;
}
@@ -255,18 +247,14 @@ async function deliverReplies(params: {
const file = new InputFile(media.buffer, media.fileName ?? "file");
const caption = first ? (reply.text ?? undefined) : undefined;
first = false;
const replyOpts = replyTarget ? { reply_to_message_id: replyTarget } : {};
if (kind === "image") {
await bot.api.sendPhoto(chatId, file, { caption, ...replyOpts });
await bot.api.sendPhoto(chatId, file, { caption });
} else if (kind === "video") {
await bot.api.sendVideo(chatId, file, { caption, ...replyOpts });
await bot.api.sendVideo(chatId, file, { caption });
} else if (kind === "audio") {
await bot.api.sendAudio(chatId, file, { caption, ...replyOpts });
await bot.api.sendAudio(chatId, file, { caption });
} else {
await bot.api.sendDocument(chatId, file, {
caption,
...replyOpts,
});
await bot.api.sendDocument(chatId, file, { caption });
}
}
}
@@ -363,12 +351,10 @@ async function sendTelegramText(
chatId: string,
text: string,
runtime: RuntimeEnv,
replyToMessageId?: number,
): Promise<number | undefined> {
try {
const res = await bot.api.sendMessage(chatId, text, {
parse_mode: "Markdown",
reply_to_message_id: replyToMessageId,
});
return res.message_id;
} catch (err) {
@@ -378,7 +364,6 @@ async function sendTelegramText(
`telegram markdown parse failed; retrying without formatting: ${errText}`,
);
const res = await bot.api.sendMessage(chatId, text, {
reply_to_message_id: replyToMessageId,
});
return res.message_id;
}

View File

@@ -223,10 +223,10 @@ export async function monitorWebInbox(options: {
}
};
const reply = async (text: string) => {
await sock.sendMessage(chatJid, { text }, { quoted: msg });
await sock.sendMessage(chatJid, { text });
};
const sendMedia = async (payload: AnyMessageContent) => {
await sock.sendMessage(chatJid, payload, { quoted: msg });
await sock.sendMessage(chatJid, payload);
};
const timestamp = msg.messageTimestamp
? Number(msg.messageTimestamp) * 1000

View File

@@ -112,11 +112,6 @@ describe("web monitor inbox", () => {
expect(sock.sendMessage).toHaveBeenCalledWith(
"999@s.whatsapp.net",
{ text: "pong" },
{
quoted: expect.objectContaining({
key: expect.objectContaining({ id: "abc" }),
}),
},
);
await listener.close();
@@ -200,11 +195,6 @@ describe("web monitor inbox", () => {
expect(sock.sendMessage).toHaveBeenCalledWith(
"999@s.whatsapp.net",
{ text: "pong" },
{
quoted: expect.objectContaining({
key: expect.objectContaining({ id: "abc" }),
}),
},
);
await listener.close();