(args: CreateRunRendererArgs)
| 47 | * stream that has real partial content. |
| 48 | */ |
| 49 | export function createRunRenderer(args: CreateRunRendererArgs): RunRenderer { |
| 50 | const interruptEventNames = |
| 51 | args.interruptEventNames ?? new Set<string>(["on_interrupt"]); |
| 52 | const showToolStatus = args.showToolStatus ?? true; |
| 53 | |
| 54 | /** Per-AG-UI-message accumulated text (we accumulate deltas locally). */ |
| 55 | const buffers = new Map<string, string>(); |
| 56 | /** Per-AG-UI-message text stream. Lazily created on first content. */ |
| 57 | const streams = new Map<string, ChunkedEditStream>(); |
| 58 | /** Per-tool-call status message id so we can edit it on END. */ |
| 59 | const toolStatusIds = new Map<string, number>(); |
| 60 | /** |
| 61 | * Once a stream has been finalised (via TEXT_MESSAGE_END or markInterrupted) |
| 62 | * we drop it. Late-arriving events for the same messageId are ignored. |
| 63 | */ |
| 64 | const finalised = new Set<string>(); |
| 65 | /** |
| 66 | * Set when the caller intentionally aborted the run (a new turn arrived for |
| 67 | * the same conversation). Suppresses the RUN_ERROR notice and stops |
| 68 | * accepting further AG-UI events — the `_(interrupted)_` marker conveys the |
| 69 | * state visually. |
| 70 | */ |
| 71 | let aborted = false; |
| 72 | |
| 73 | /** Tool calls observed in this run, in event order. */ |
| 74 | const capturedToolCalls: CapturedToolCall[] = []; |
| 75 | |
| 76 | /** Interrupt observed via a matching `onCustomEvent`; read after runAgent. */ |
| 77 | let pendingInterrupt: CapturedInterrupt | undefined; |
| 78 | |
| 79 | const ensureStream = (messageId: string): ChunkedEditStream | undefined => { |
| 80 | if (finalised.has(messageId)) return undefined; |
| 81 | let s = streams.get(messageId); |
| 82 | if (!s) { |
| 83 | s = new ChunkedEditStream({ |
| 84 | postPlaceholder: args.postPlaceholder, |
| 85 | editAt: args.editAt, |
| 86 | transform: telegramHtml, |
| 87 | }); |
| 88 | streams.set(messageId, s); |
| 89 | } |
| 90 | return s; |
| 91 | }; |
| 92 | |
| 93 | /** |
| 94 | * BEST-EFFORT cleanup of tool-status placeholders that were posted on START |
| 95 | * but never resolved by an END (the run ended/errored/was interrupted while |
| 96 | * the tool call was still in flight). Edits each remaining placeholder to a |
| 97 | * terminal marker so the stale `🔧 using <tool>…` line doesn't linger. This |
| 98 | * is not a hard guarantee: each `editAt` can fail (message too old, deleted, |
| 99 | * not-modified, flood-wait) and on failure it is caught + logged, NOT |
| 100 | * retried — so a placeholder can still end up stranded if Telegram rejects |
| 101 | * the edit. `terminal` is the marker each placeholder is edited to (e.g. |
| 102 | * `✅` on finish, `⚠️ … (cancelled)` on error/interrupt). |
| 103 | */ |
| 104 | const drainToolStatuses = async ( |
| 105 | terminal: (toolName: string) => string, |
| 106 | ): Promise<void> => { |
no outgoing calls
no test coverage detected
searching dependent graphs…