fix(telegram): prevent stuck typing after tool runs

This commit is contained in:
Peter Steinberger
2026-01-06 18:56:40 +00:00
parent 72ab9f3f42
commit 31dbc62bdd
3 changed files with 44 additions and 1 deletions

View File

@@ -314,6 +314,9 @@ export async function runReplyAgent(params: {
shouldEmitToolResult,
onToolResult: opts?.onToolResult
? (payload) => {
// `subscribeEmbeddedPiSession` may invoke tool callbacks without awaiting them.
// If a tool callback starts typing after the run finalized, we can end up with
// a typing loop that never sees a matching markRunComplete(). Track and drain.
const task = (async () => {
let text = payload.text;
if (!isHeartbeat && text?.includes("HEARTBEAT_OK")) {
@@ -384,13 +387,16 @@ export async function runReplyAgent(params: {
}
const payloadArray = runResult.payloads ?? [];
if (payloadArray.length === 0) return finalizeWithFollowup(undefined);
if (pendingBlockTasks.size > 0) {
await Promise.allSettled(pendingBlockTasks);
}
if (pendingToolTasks.size > 0) {
await Promise.allSettled(pendingToolTasks);
}
// Drain any late tool/block deliveries before deciding there's "nothing to send".
// Otherwise, a late typing trigger (e.g. from a tool callback) can outlive the run and
// keep the typing indicator stuck.
if (payloadArray.length === 0) return finalizeWithFollowup(undefined);
const sanitizedPayloads = isHeartbeat
? payloadArray