Files
t3code/apps/server/src/orchestration-v2/assistantStreaming.ts
T
+36 de34391427 feat(orchestrator): introduce new orchestrator (#2829)
Co-authored-by: maria-rcks <[email protected]>
Co-authored-by: Bilal Bakr <[email protected]>
Co-authored-by: shivam <[email protected]>
Co-authored-by: Vitalii Yehorov <[email protected]>
Co-authored-by: Jake Leventhal <[email protected]>
Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
Co-authored-by: Alex Southwell <[email protected]>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Nicholas Wasmiller <[email protected]>
Co-authored-by: PB <[email protected]>
Co-authored-by: Exotic <[email protected]>
Co-authored-by: Yash Singh <[email protected]>
Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: Theo Browne <[email protected]>
Co-authored-by: Gabriel De Andrade <[email protected]>
Co-authored-by: Dara Adedeji <[email protected]>
Co-authored-by: scratchyone <[email protected]>
Co-authored-by: Dominic Roy <[email protected]>
Co-authored-by: chukfinley <[email protected]>
Co-authored-by: Primož Ajdišek <[email protected]>
Co-authored-by: benthecarman <[email protected]>
Co-authored-by: NaveDanan <[email protected]>
Co-authored-by: aaditagrawal <[email protected]>
Co-authored-by: Aditya Garud <[email protected]>
Co-authored-by: Nick Anisimov <[email protected]>
Co-authored-by: MacKinley Smith <[email protected]>
Co-authored-by: Yordis Prieto <[email protected]>
Co-authored-by: t3-code[bot] <269035359+t3-code[bot]@users.noreply.github.com>
Co-authored-by: AKolenda <[email protected]>
Co-authored-by: Guillermo Casanova <[email protected]>
Co-authored-by: Otavio Salvador <[email protected]>
Co-authored-by: Shirish Pothi <[email protected]>
Co-authored-by: Ishaan Kothari <[email protected]>
Co-authored-by: Bob Fowler <[email protected]>
Co-authored-by: Anton Bezdenezhnykh <[email protected]>
Co-authored-by: ValeraZSD <[email protected]>
Co-authored-by: Ephraim <[email protected]>
Co-authored-by: Ryan Ilano <[email protected]>
Co-authored-by: Alex <[email protected]>
Co-authored-by: maco <[email protected]>
Co-authored-by: Cursor <[email protected]>
Co-authored-by: Tristan Knight <[email protected]>
Co-authored-by: PR Batch Tester <[email protected]>
Co-authored-by: oliver <[email protected]>
Co-authored-by: kamkm <[email protected]>
Signed-off-by: Yordis Prieto <[email protected]>
2026-10-02 12:22:22 -07:00

130 lines
5.3 KiB
TypeScript

import type { ResponseStreamingMode } from "@t3tools/contracts";
import type { ProviderAdapterV2Event } from "./ProviderAdapter.ts";
// An opening fence may sit at any indentation, since fences inside list
// items are indented past the marker. A closing fence may be indented at most
// three spaces more than its opener. Deeper lines are content in the block.
const MARKDOWN_FENCE_PATTERN = /^( *)(`{3,}|~{3,})/;
// CommonMark blank lines hold only spaces and tabs. Other whitespace, such as
// a no-break space, is paragraph content.
const BLANK_LINE_PATTERN = /^[ \t]*$/;
// A bullet or ordered marker followed by whitespace, at any indentation so
// nested items count. The trailing space is required, so a partial `-` or
// `1.` never matches before the model finishes the marker.
const LIST_ITEM_START_PATTERN = /^[ \t]*(?:[-*+]|\d{1,9}[.)])[ \t]/;
// A section title: an ATX heading, or a line of only bold text, which models
// often use as a heading.
const SECTION_TITLE_PATTERN = /^ {0,3}(?:#{1,6}(?:[ \t]|$)|\*\*(?:[^*]|\*(?!\*))+\*\*:?$)/;
// An unindented ATX heading ends the paragraph or list above it, even with no
// blank line between them. A bold line would continue the paragraph instead.
const TOP_LEVEL_HEADING_PATTERN = /^#{1,6}(?:[ \t]|$)/;
/**
* Splits buffered assistant text at the last blank line, closing code fence,
* or list item start that is not inside an open fenced code block. `ready` is
* safe to deliver now because the markdown before it will not change shape as
* more text arrives. `rest` stays buffered until the next boundary or
* completion. Only fully terminated lines count, so a trailing partial line
* never leaks; a list item start is the one lookahead that may sit on the
* partial line, since tight lists have no blank lines between items and would
* otherwise land all at once.
*
* A section title holds the boundary until a content line follows it, so a
* title never lands alone and waits above a block that is still streaming.
*/
export function splitBufferedAssistantText(text: string): { ready: string; rest: string } {
let openFence: { marker: string; indent: number } | null = null;
let boundary = -1;
let lineStart = 0;
let titleAwaitingContent = false;
for (;;) {
const newline = text.indexOf("\n", lineStart);
const line = text
.slice(lineStart, newline === -1 ? text.length : newline)
.replace(/[ \t\r]+$/, "");
if (
openFence === null &&
lineStart > 0 &&
!titleAwaitingContent &&
LIST_ITEM_START_PATTERN.test(line)
) {
boundary = lineStart;
}
if (newline === -1) {
break;
}
const fenceMatch = MARKDOWN_FENCE_PATTERN.exec(line);
if (fenceMatch) {
const indent = fenceMatch[1]!.length;
const marker = fenceMatch[2]!;
if (openFence === null) {
openFence = { marker, indent };
titleAwaitingContent = false;
} else if (
marker[0] === openFence.marker[0] &&
marker.length >= openFence.marker.length &&
indent <= openFence.indent + 3 &&
line.length === indent + marker.length
) {
// CommonMark: a closing fence carries no info string.
openFence = null;
boundary = newline + 1;
}
} else if (openFence === null && BLANK_LINE_PATTERN.test(line) && lineStart > 0) {
if (!titleAwaitingContent) {
boundary = newline + 1;
}
} else if (openFence === null) {
if (lineStart > 0 && !titleAwaitingContent && TOP_LEVEL_HEADING_PATTERN.test(line)) {
boundary = lineStart;
}
titleAwaitingContent = SECTION_TITLE_PATTERN.test(line);
}
lineStart = newline + 1;
}
if (boundary === -1) {
return { ready: "", rest: text };
}
return { ready: text.slice(0, boundary), rest: text.slice(boundary) };
}
/** Buffer assistant messages and reasoning alike, keeping unfinished markdown out of the read model. */
export function makeAssistantStreamingFilter(mode: ResponseStreamingMode) {
const delivered = new Map<string, { text: string; at: number }>();
return (event: ProviderAdapterV2Event, now: number): ProviderAdapterV2Event | null => {
if (
event.type === "node.updated" &&
event.node.kind === "assistant_message" &&
event.node.status === "running"
)
return null;
const message =
event.type === "message.updated" && event.message.role === "assistant"
? event.message
: event.type === "turn_item.updated" &&
(event.turnItem.type === "assistant_message" || event.turnItem.type === "reasoning")
? event.turnItem
: null;
if (!message) return event;
const key = `${event.type}:${message.id}`;
if (!message.streaming) {
delivered.delete(key);
return event;
}
if (mode === "turn") return null;
const previous = delivered.get(key);
if (previous && now - previous.at < 400) return null;
const { ready } = splitBufferedAssistantText(message.text);
if (!ready || ready === previous?.text) return null;
delivered.set(key, { text: ready, at: now });
if (event.type === "message.updated")
return { ...event, message: { ...event.message, text: ready } };
if (
event.type === "turn_item.updated" &&
(event.turnItem.type === "assistant_message" || event.turnItem.type === "reasoning")
)
return { ...event, turnItem: { ...event.turnItem, text: ready } };
return event;
};
}