import type { ResponseStreamingMode } from "@t3tools/contracts";
import type * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter";
// List fences can open at any indentation; closers may be at most three spaces deeper.
const MARKDOWN_FENCE_PATTERN = /^( *)(`{3,}|~{3,})/;
// CommonMark blank lines contain only spaces and tabs.
const BLANK_LINE_PATTERN = /^[ \t]*$/;
// Require trailing space so partial list markers cannot match during streaming.
const LIST_ITEM_START_PATTERN = /^[ \t]*(?:[-*+]|\d{1,9}[.)])[ \t]/;
const SECTION_TITLE_PATTERN = /^ {0,3}(?:#{1,6}(?:[ \t]|$)|\*\*(?:[^*]|\*(?!\*))+\*\*:?$)/;
// Unindented ATX headings end preceding paragraphs and lists without blank lines; bold lines do not.
const TOP_LEVEL_HEADING_PATTERN = /^#{1,6}(?:[ \t]|$)/;
/** Emit terminated lines only, except partial list-start lookahead for tight lists. Hold section titles until content follows. */
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) };
}
export function makeAssistantStreamingFilter(mode: ResponseStreamingMode) {
const delivered = new Map<string, { text: string; at: number }>();
return (
event: ProviderAdapter.ProviderAdapterV2Event,
now: number,
): ProviderAdapter.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;
};
}