fix(telegram): notify users when agent returns empty response
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
2ad550abe8
commit
6aeb79e0db
@ -199,6 +199,7 @@ export const dispatchTelegramMessage = async ({
|
|||||||
chunkMode,
|
chunkMode,
|
||||||
onVoiceRecording: sendRecordVoice,
|
onVoiceRecording: sendRecordVoice,
|
||||||
linkPreview: telegramCfg.linkPreview,
|
linkPreview: telegramCfg.linkPreview,
|
||||||
|
notifyEmptyResponse: info.kind === "final",
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
onError: (err, info) => {
|
onError: (err, info) => {
|
||||||
|
|||||||
@ -484,7 +484,7 @@ export const registerTelegramNativeCommands = ({
|
|||||||
cfg,
|
cfg,
|
||||||
dispatcherOptions: {
|
dispatcherOptions: {
|
||||||
responsePrefix: resolveEffectiveMessagesConfig(cfg, route.agentId).responsePrefix,
|
responsePrefix: resolveEffectiveMessagesConfig(cfg, route.agentId).responsePrefix,
|
||||||
deliver: async (payload) => {
|
deliver: async (payload, info) => {
|
||||||
await deliverReplies({
|
await deliverReplies({
|
||||||
replies: [payload],
|
replies: [payload],
|
||||||
chatId: String(chatId),
|
chatId: String(chatId),
|
||||||
@ -497,6 +497,7 @@ export const registerTelegramNativeCommands = ({
|
|||||||
tableMode,
|
tableMode,
|
||||||
chunkMode,
|
chunkMode,
|
||||||
linkPreview: telegramCfg.linkPreview,
|
linkPreview: telegramCfg.linkPreview,
|
||||||
|
notifyEmptyResponse: info.kind === "final",
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
onError: (err, info) => {
|
onError: (err, info) => {
|
||||||
|
|||||||
@ -42,12 +42,15 @@ export async function deliverReplies(params: {
|
|||||||
onVoiceRecording?: () => Promise<void> | void;
|
onVoiceRecording?: () => Promise<void> | void;
|
||||||
/** Controls whether link previews are shown. Default: true (previews enabled). */
|
/** Controls whether link previews are shown. Default: true (previews enabled). */
|
||||||
linkPreview?: boolean;
|
linkPreview?: boolean;
|
||||||
}) {
|
/** If true, send a fallback message when all replies are empty. Default: false */
|
||||||
|
notifyEmptyResponse?: boolean;
|
||||||
|
}): Promise<{ delivered: boolean }> {
|
||||||
const { replies, chatId, runtime, bot, replyToMode, textLimit, messageThreadId, linkPreview } =
|
const { replies, chatId, runtime, bot, replyToMode, textLimit, messageThreadId, linkPreview } =
|
||||||
params;
|
params;
|
||||||
const chunkMode = params.chunkMode ?? "length";
|
const chunkMode = params.chunkMode ?? "length";
|
||||||
const threadParams = buildTelegramThreadParams(messageThreadId);
|
const threadParams = buildTelegramThreadParams(messageThreadId);
|
||||||
let hasReplied = false;
|
let hasReplied = false;
|
||||||
|
let skippedEmpty = 0;
|
||||||
const chunkText = (markdown: string) => {
|
const chunkText = (markdown: string) => {
|
||||||
const markdownChunks =
|
const markdownChunks =
|
||||||
chunkMode === "newline"
|
chunkMode === "newline"
|
||||||
@ -75,6 +78,7 @@ export async function deliverReplies(params: {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
runtime.error?.(danger("reply missing text/media"));
|
runtime.error?.(danger("reply missing text/media"));
|
||||||
|
skippedEmpty++;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
const replyToId = replyToMode === "off" ? undefined : resolveTelegramReplyId(reply.replyToId);
|
const replyToId = replyToMode === "off" ? undefined : resolveTelegramReplyId(reply.replyToId);
|
||||||
@ -255,6 +259,17 @@ export async function deliverReplies(params: {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If all replies were empty and notifyEmptyResponse is enabled, send a fallback message
|
||||||
|
if (!hasReplied && skippedEmpty > 0 && params.notifyEmptyResponse) {
|
||||||
|
const fallbackText = "No response generated. Please try again.";
|
||||||
|
await sendTelegramText(bot, chatId, fallbackText, runtime, {
|
||||||
|
messageThreadId,
|
||||||
|
});
|
||||||
|
hasReplied = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return { delivered: hasReplied };
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function resolveMedia(
|
export async function resolveMedia(
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user