The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 03:37 UTC
| 1 | import type { ResponseStreamingMode } from "@t3tools/contracts"; |
| 2 | import type * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter"; |
| 3 | |
| 4 | // List fences can open at any indentation; closers may be at most three spaces deeper. |
| 5 | // A backtick fence's info string cannot contain a backtick, so "```npm test``` runs" is inline code. |
| 6 | const MARKDOWN_FENCE_PATTERN = /^( *)(`{3,}(?![^`\r\n]*`)|~{3,})/; |
| 7 | // CommonMark blank lines contain only spaces and tabs. |
| 8 | const BLANK_LINE_PATTERN = /^[ \t]*$/; |
| 9 | // Require trailing space so partial list markers cannot match during streaming. |
| 10 | const LIST_ITEM_START_PATTERN = /^[ \t]*(?:[-*+]|\d{1,9}[.)])[ \t]/; |
| 11 | const SECTION_TITLE_PATTERN = /^ {0,3}(?:#{1,6}(?:[ \t]|$)|\*\*(?:[^*]|\*(?!\*))+\*\*:?$)/; |
| 12 | // Unindented ATX headings end preceding paragraphs and lists without blank lines; bold lines do not. |
| 13 | const TOP_LEVEL_HEADING_PATTERN = /^#{1,6}(?:[ \t]|$)/; |
| 14 | |
| 15 | // Matches LIST_ITEM_START_PATTERN on the trimmed partial line without copying it. |
| 16 | const PARTIAL_LIST_ITEM_START_PATTERN = /[ \t]*(?:[-*+]|\d{1,9}[.)])[ \t][ \t\r]*[^ \t\r]/y; |
| 17 | |
| 18 | interface AssistantTextScan { |
| 19 | readonly text: string; |
| 20 | // Start of the partial line; every line before it has been scanned. |
| 21 | readonly lineStart: number; |
| 22 | readonly openFence: { readonly marker: string; readonly indent: number } | null; |
| 23 | readonly boundary: number; |
| 24 | readonly titleAwaitingContent: boolean; |
| 25 | } |
| 26 | |
| 27 | const EMPTY_SCAN: AssistantTextScan = { |
| 28 | text: "", |
| 29 | lineStart: 0, |
| 30 | openFence: null, |
| 31 | boundary: -1, |
| 32 | titleAwaitingContent: false, |
| 33 | }; |
| 34 | |
| 35 | function trimLineEnd(text: string, start: number, end: number): string { |
| 36 | while (end > start) { |
| 37 | const char = text.charCodeAt(end - 1); |
| 38 | if (char !== 32 && char !== 9 && char !== 13) break; |
| 39 | end--; |
| 40 | } |
| 41 | return text.slice(start, end); |
| 42 | } |
| 43 | |
| 44 | /** Scans the terminated lines of `text`, resuming after `previous` when `text` extends it. */ |
| 45 | function advanceScan(previous: AssistantTextScan, text: string): AssistantTextScan { |
| 46 | const resumed = text.startsWith(previous.text); |
| 47 | let { openFence, boundary, lineStart, titleAwaitingContent } = resumed ? previous : EMPTY_SCAN; |
| 48 | let searchFrom = resumed ? previous.text.length : 0; |
| 49 | for (;;) { |
| 50 | const newline = text.indexOf("\n", searchFrom); |
| 51 | if (newline === -1) break; |
| 52 | const line = trimLineEnd(text, lineStart, newline); |
| 53 | if ( |
| 54 | openFence === null && |
| 55 | lineStart > 0 && |
| 56 | !titleAwaitingContent && |
| 57 | LIST_ITEM_START_PATTERN.test(line) |
| 58 | ) { |
| 59 | boundary = lineStart; |
| 60 | } |
| 61 | const fenceMatch = MARKDOWN_FENCE_PATTERN.exec(line); |
| 62 | if (fenceMatch) { |
| 63 | const indent = fenceMatch[1]!.length; |
| 64 | const marker = fenceMatch[2]!; |
| 65 | if (openFence === null) { |
| 66 | openFence = { marker, indent }; |
| 67 | titleAwaitingContent = false; |
| 68 | } else if ( |
| 69 | marker[0] === openFence.marker[0] && |
| 70 | marker.length >= openFence.marker.length && |
| 71 | indent <= openFence.indent + 3 && |
| 72 | line.length === indent + marker.length |
| 73 | ) { |
| 74 | // CommonMark: a closing fence carries no info string. |
| 75 | openFence = null; |
| 76 | boundary = newline + 1; |
| 77 | } |
| 78 | } else if (openFence === null && BLANK_LINE_PATTERN.test(line) && lineStart > 0) { |
| 79 | if (!titleAwaitingContent) { |
| 80 | boundary = newline + 1; |
| 81 | } |
| 82 | } else if (openFence === null) { |
| 83 | if (lineStart > 0 && !titleAwaitingContent && TOP_LEVEL_HEADING_PATTERN.test(line)) { |
| 84 | boundary = lineStart; |
| 85 | } |
| 86 | titleAwaitingContent = SECTION_TITLE_PATTERN.test(line); |
| 87 | } |
| 88 | lineStart = newline + 1; |
| 89 | searchFrom = lineStart; |
| 90 | } |
| 91 | return { text, lineStart, openFence, boundary, titleAwaitingContent }; |
| 92 | } |
| 93 | |
| 94 | // The partial line may still change, so its list item start is checked here, not kept in the scan. |
| 95 | function readyBoundary(scan: AssistantTextScan): number { |
| 96 | if (scan.openFence === null && scan.lineStart > 0 && !scan.titleAwaitingContent) { |
| 97 | PARTIAL_LIST_ITEM_START_PATTERN.lastIndex = scan.lineStart; |
| 98 | if (PARTIAL_LIST_ITEM_START_PATTERN.test(scan.text)) return scan.lineStart; |
| 99 | } |
| 100 | return scan.boundary; |
| 101 | } |
| 102 | |
| 103 | /** Emit terminated lines only, except partial list-start lookahead for tight lists. Hold section titles until content follows. */ |
| 104 | export function splitBufferedAssistantText(text: string): { ready: string; rest: string } { |
| 105 | const boundary = readyBoundary(advanceScan(EMPTY_SCAN, text)); |
| 106 | if (boundary === -1) { |
| 107 | return { ready: "", rest: text }; |
| 108 | } |
| 109 | return { ready: text.slice(0, boundary), rest: text.slice(boundary) }; |
| 110 | } |
| 111 | |
| 112 | // Long enough to ride out normal gaps between chunks; short enough that text written before a |
| 113 | // long tool call or background wait does not look lost. |
| 114 | const ASSISTANT_IDLE_FLUSH_MS = 800; |
| 115 | |
| 116 | type StreamedMessage = { |
| 117 | readonly id: string; |
| 118 | readonly text: string; |
| 119 | readonly streaming: boolean; |
| 120 | }; |
| 121 | |
| 122 | interface StreamTrack { |
| 123 | readonly text: string; |
| 124 | readonly at: number; |
| 125 | // Latest snapshot whose text has not all been delivered yet. |
| 126 | readonly held: ProviderAdapter.ProviderAdapterV2Event | null; |
| 127 | readonly dueAt: number; |
| 128 | readonly scan: AssistantTextScan; |
| 129 | } |
| 130 | |
| 131 | function streamedMessage(event: ProviderAdapter.ProviderAdapterV2Event): StreamedMessage | null { |
| 132 | if (event.type === "message.updated" && event.message.role === "assistant") return event.message; |
| 133 | if ( |
| 134 | event.type === "turn_item.updated" && |
| 135 | (event.turnItem.type === "assistant_message" || event.turnItem.type === "reasoning") |
| 136 | ) |
| 137 | return event.turnItem; |
| 138 | return null; |
| 139 | } |
| 140 | |
| 141 | function withText( |
| 142 | event: ProviderAdapter.ProviderAdapterV2Event, |
| 143 | text: string, |
| 144 | ): ProviderAdapter.ProviderAdapterV2Event { |
| 145 | if (event.type === "message.updated") return { ...event, message: { ...event.message, text } }; |
| 146 | if ( |
| 147 | event.type === "turn_item.updated" && |
| 148 | (event.turnItem.type === "assistant_message" || event.turnItem.type === "reasoning") |
| 149 | ) |
| 150 | return { ...event, turnItem: { ...event.turnItem, text } }; |
| 151 | return event; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Paragraph mode delivers streamed text at Markdown boundaries, at most every 400ms. A snapshot |
| 156 | * held back that way is delivered whole once its message has been idle for `idleFlushMs`, so a |
| 157 | * provider that pauses mid-turn still shows everything it wrote. |
| 158 | */ |
| 159 | export function makeAssistantStreamingFilter( |
| 160 | mode: ResponseStreamingMode, |
| 161 | idleFlushMs = ASSISTANT_IDLE_FLUSH_MS, |
| 162 | ) { |
| 163 | const tracks = new Map<string, StreamTrack>(); |
| 164 | // Earliest deadline of any held snapshot, or earlier: a newer snapshot only pushes its own |
| 165 | // deadline later, so the cache stays O(1) per event and is recomputed when it comes due. |
| 166 | let nextDueAt: number | null = null; |
| 167 | const track = (key: string, value: StreamTrack) => { |
| 168 | tracks.set(key, value); |
| 169 | if (value.held !== null && (nextDueAt === null || value.dueAt < nextDueAt)) |
| 170 | nextDueAt = value.dueAt; |
| 171 | }; |
| 172 | return { |
| 173 | filter( |
| 174 | event: ProviderAdapter.ProviderAdapterV2Event, |
| 175 | now: number, |
| 176 | ): ProviderAdapter.ProviderAdapterV2Event | null { |
| 177 | if ( |
| 178 | event.type === "node.updated" && |
| 179 | event.node.kind === "assistant_message" && |
| 180 | event.node.status === "running" |
| 181 | ) |
| 182 | return null; |
| 183 | const message = streamedMessage(event); |
| 184 | if (!message) return event; |
| 185 | const key = `${event.type}:${message.id}`; |
| 186 | if (!message.streaming) { |
| 187 | tracks.delete(key); |
| 188 | return event; |
| 189 | } |
| 190 | if (mode === "turn") return null; |
| 191 | const previous = tracks.get(key); |
| 192 | const delivered = previous?.text ?? ""; |
| 193 | const hold = (scan = previous?.scan ?? EMPTY_SCAN) => { |
| 194 | track(key, { |
| 195 | text: delivered, |
| 196 | at: previous?.at ?? Number.NEGATIVE_INFINITY, |
| 197 | held: message.text !== delivered ? event : null, |
| 198 | dueAt: now + idleFlushMs, |
| 199 | scan, |
| 200 | }); |
| 201 | return null; |
| 202 | }; |
| 203 | if (previous && now - previous.at < 400) return hold(); |
| 204 | const scan = advanceScan(previous?.scan ?? EMPTY_SCAN, message.text); |
| 205 | const boundary = readyBoundary(scan); |
| 206 | const ready = boundary === -1 ? "" : message.text.slice(0, boundary); |
| 207 | // An idle delivery may already show more than the next boundary; never shrink it. A |
| 208 | // provider rewrite or truncation is not a prefix and replaces it. |
| 209 | if (delivered.startsWith(ready)) return hold(scan); |
| 210 | track(key, { |
| 211 | text: ready, |
| 212 | at: now, |
| 213 | held: ready !== message.text ? event : null, |
| 214 | dueAt: now + idleFlushMs, |
| 215 | scan, |
| 216 | }); |
| 217 | return withText(event, ready); |
| 218 | }, |
| 219 | nextIdleFlushAt: (): number | null => nextDueAt, |
| 220 | /** Returns the held snapshots due by `now` and counts them as delivered. */ |
| 221 | takeIdleFlushes(now: number): ReadonlyArray<ProviderAdapter.ProviderAdapterV2Event> { |
| 222 | const due: Array<ProviderAdapter.ProviderAdapterV2Event> = []; |
| 223 | nextDueAt = null; |
| 224 | for (const [key, current] of tracks) { |
| 225 | const event = current.held; |
| 226 | if (event === null) continue; |
| 227 | if (current.dueAt > now) { |
| 228 | if (nextDueAt === null || current.dueAt < nextDueAt) nextDueAt = current.dueAt; |
| 229 | continue; |
| 230 | } |
| 231 | due.push(event); |
| 232 | tracks.set(key, { |
| 233 | text: streamedMessage(event)?.text ?? current.text, |
| 234 | at: now, |
| 235 | held: null, |
| 236 | dueAt: now, |
| 237 | scan: current.scan, |
| 238 | }); |
| 239 | } |
| 240 | return due; |
| 241 | }, |
| 242 | }; |
| 243 | } |