type ParsedChunk = ReplyDirectiveParseResult & {
replyToExplicitId?: string;
};
type ConsumeOptions = { final?: boolean;
silentToken?: string;
};
type SplitTrailingDirectiveOptions = { final?: boolean;
};
// Holds back incomplete streaming-directive tails so parseChunk only ever sees // complete directives. Otherwise, upstream token boundaries can split markers // like `MEDIA:<path>` between chunks and cause the first half to be emitted as // plain text (e.g. the `MEDIA` token leaking into a channel reply while the // matching file path is silently dropped on the next chunk).
export const splitTrailingDirective = (
text: string,
options: SplitTrailingDirectiveOptions = {},
): { text: string; tail: string } => {
let bufferStart = text.length;
// 2. `MEDIA:` line without a trailing newline — the URL may still be // streaming. `splitMediaFromOutput` in src/media/parse.ts treats a // line as a media directive only when `line.trimStart()` begins with // `MEDIA:`, so we match the same shape here: only buffer when the // last line looks like an actual directive line (optional leading // whitespace, then `MEDIA:`). Prose such as // "See the MEDIA: section for details" does NOT qualify and is // flushed as ordinary text — otherwise it could sit in pendingTail // and be silently dropped if a stream-item boundary calls `reset()` // without a preceding `consume("", { final: true })`. const lastNewline = text.lastIndexOf("\n"); const lastLine = lastNewline < 0 ? text : text.slice(lastNewline + 1); if (/^\s*MEDIA:/i.test(lastLine)) { const mediaLineStart = lastNewline < 0 ? 0 : lastNewline + 1; if (mediaLineStart < bufferStart) {
bufferStart = mediaLineStart;
}
}
// 3. Trailing `M|ME|MED|MEDI|MEDIA` prefix (no colon yet) at the start of // a line — the next chunk might turn this into `MEDIA:<url>`. Only a // line-start anchor (`^` or immediately after `\n`) is accepted so // mid-prose tokens like "_M", "3ME", or "token MEDIA" are not // speculatively buffered and cannot accidentally be glued to a // following `:` into a synthetic directive. Matches the canonical // MEDIA directive placement (own line after `\n\n`). const prefixMatch = text.match(/(?:^|\n)(MEDIA|MEDI|MED|ME|M)$/i); if (prefixMatch) { const prefixStart = text.length - prefixMatch[1].length; if (prefixStart < bufferStart) {
bufferStart = prefixStart;
}
}
if (!hasRenderableContent(combinedResult)) { if (hasTag) {
pendingReply = {
explicitId,
sawCurrent,
hasTag,
};
} returnnull;
}
// Keep reply context sticky for the full assistant message so split/newline chunks // stay on the same native reply target until reset() is called for the next message.
activeReply = {
explicitId,
sawCurrent,
hasTag,
};
pendingReply = { sawCurrent: false, hasTag: false }; return combinedResult;
};
return {
consume,
reset,
};
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.11 Sekunden
(vorverarbeitet am 2026-06-10)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.