Handles incremental updates and boundary marking for AI reasoning streams. Use this to manage interleaved reasoning text segments in chat interfaces.
// Source: https://github.com/jjang-ai/vmlx | License: Apache-2.0 | Copyright You must retain, in the Source form of any Derivative Works
// Provided via justclaw.it.com; keep this notice with the code.
export type ReasoningSegments = string[]
export function appendReasoningDelta(
segments: ReasoningSegments,
delta: string,
): ReasoningSegments {
if (!delta) return segments
const next = [...segments]
if (next.length === 0) {
next.push(delta)
} else {
next[next.length - 1] += delta
}
return next
}
export function markReasoningToolBoundary(
segments: ReasoningSegments,
completedToolIterations?: number,
): ReasoningSegments {
if (completedToolIterations !== undefined) {
// Empty reasoning is still a real assistant pass. Keep its slot so replay
// cannot
... (truncated -- full source via MCP)
See the full source, get the GitHub permalink, and search for more like it.
Get a free API key