File size: 6,189 Bytes
fc93158 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 | import {
composeThinkingAndContent,
extractContentFromMessage,
extractThinkingFromMessage,
resolveFinalAssistantText,
} from "./tui-formatters.js";
type RunStreamState = {
thinkingText: string;
contentText: string;
contentBlocks: string[];
sawNonTextContentBlocks: boolean;
displayText: string;
};
type BoundaryDropMode = "off" | "streamed-only" | "streamed-or-incoming";
function extractTextBlocksAndSignals(message: unknown): {
textBlocks: string[];
sawNonTextContentBlocks: boolean;
} {
if (!message || typeof message !== "object") {
return { textBlocks: [], sawNonTextContentBlocks: false };
}
const record = message as Record<string, unknown>;
const content = record.content;
if (typeof content === "string") {
const text = content.trim();
return {
textBlocks: text ? [text] : [],
sawNonTextContentBlocks: false,
};
}
if (!Array.isArray(content)) {
return { textBlocks: [], sawNonTextContentBlocks: false };
}
const textBlocks: string[] = [];
let sawNonTextContentBlocks = false;
for (const block of content) {
if (!block || typeof block !== "object") {
continue;
}
const rec = block as Record<string, unknown>;
if (rec.type === "text" && typeof rec.text === "string") {
const text = rec.text.trim();
if (text) {
textBlocks.push(text);
}
continue;
}
if (typeof rec.type === "string" && rec.type !== "thinking") {
sawNonTextContentBlocks = true;
}
}
return { textBlocks, sawNonTextContentBlocks };
}
function isDroppedBoundaryTextBlockSubset(params: {
streamedTextBlocks: string[];
finalTextBlocks: string[];
}): boolean {
const { streamedTextBlocks, finalTextBlocks } = params;
if (finalTextBlocks.length === 0 || finalTextBlocks.length >= streamedTextBlocks.length) {
return false;
}
const prefixMatches = finalTextBlocks.every(
(block, index) => streamedTextBlocks[index] === block,
);
if (prefixMatches) {
return true;
}
const suffixStart = streamedTextBlocks.length - finalTextBlocks.length;
return finalTextBlocks.every((block, index) => streamedTextBlocks[suffixStart + index] === block);
}
function shouldPreserveBoundaryDroppedText(params: {
boundaryDropMode: BoundaryDropMode;
streamedSawNonTextContentBlocks: boolean;
incomingSawNonTextContentBlocks: boolean;
streamedTextBlocks: string[];
nextContentBlocks: string[];
}) {
if (params.boundaryDropMode === "off") {
return false;
}
const sawEligibleNonTextContent =
params.boundaryDropMode === "streamed-or-incoming"
? params.streamedSawNonTextContentBlocks || params.incomingSawNonTextContentBlocks
: params.streamedSawNonTextContentBlocks;
if (!sawEligibleNonTextContent) {
return false;
}
return isDroppedBoundaryTextBlockSubset({
streamedTextBlocks: params.streamedTextBlocks,
finalTextBlocks: params.nextContentBlocks,
});
}
export class TuiStreamAssembler {
private runs = new Map<string, RunStreamState>();
private getOrCreateRun(runId: string): RunStreamState {
let state = this.runs.get(runId);
if (!state) {
state = {
thinkingText: "",
contentText: "",
contentBlocks: [],
sawNonTextContentBlocks: false,
displayText: "",
};
this.runs.set(runId, state);
}
return state;
}
private updateRunState(
state: RunStreamState,
message: unknown,
showThinking: boolean,
opts?: { boundaryDropMode?: BoundaryDropMode },
) {
const thinkingText = extractThinkingFromMessage(message);
const contentText = extractContentFromMessage(message);
const { textBlocks, sawNonTextContentBlocks } = extractTextBlocksAndSignals(message);
if (thinkingText) {
state.thinkingText = thinkingText;
}
if (contentText) {
const nextContentBlocks = textBlocks.length > 0 ? textBlocks : [contentText];
const boundaryDropMode = opts?.boundaryDropMode ?? "off";
const shouldKeepStreamedBoundaryText = shouldPreserveBoundaryDroppedText({
boundaryDropMode,
streamedSawNonTextContentBlocks: state.sawNonTextContentBlocks,
incomingSawNonTextContentBlocks: sawNonTextContentBlocks,
streamedTextBlocks: state.contentBlocks,
nextContentBlocks,
});
if (!shouldKeepStreamedBoundaryText) {
state.contentText = contentText;
state.contentBlocks = nextContentBlocks;
}
}
if (sawNonTextContentBlocks) {
state.sawNonTextContentBlocks = true;
}
const displayText = composeThinkingAndContent({
thinkingText: state.thinkingText,
contentText: state.contentText,
showThinking,
});
state.displayText = displayText;
}
ingestDelta(runId: string, message: unknown, showThinking: boolean): string | null {
const state = this.getOrCreateRun(runId);
const previousDisplayText = state.displayText;
this.updateRunState(state, message, showThinking, {
boundaryDropMode: "streamed-or-incoming",
});
if (!state.displayText || state.displayText === previousDisplayText) {
return null;
}
return state.displayText;
}
finalize(runId: string, message: unknown, showThinking: boolean, errorMessage?: string): string {
const state = this.getOrCreateRun(runId);
const streamedDisplayText = state.displayText;
const streamedTextBlocks = [...state.contentBlocks];
const streamedSawNonTextContentBlocks = state.sawNonTextContentBlocks;
this.updateRunState(state, message, showThinking, {
boundaryDropMode: "streamed-only",
});
const finalComposed = state.displayText;
const shouldKeepStreamedText =
streamedSawNonTextContentBlocks &&
isDroppedBoundaryTextBlockSubset({
streamedTextBlocks,
finalTextBlocks: state.contentBlocks,
});
const finalText = resolveFinalAssistantText({
finalText: shouldKeepStreamedText ? streamedDisplayText : finalComposed,
streamedText: streamedDisplayText,
errorMessage,
});
this.runs.delete(runId);
return finalText;
}
drop(runId: string) {
this.runs.delete(runId);
}
}
|