Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
Back to tool/Back to issues
#806·flue

`appendDelta` splits a live message into several text parts when a provider interleaves reasoning and text deltas

Author: rikbrownCreated Oct 1, 2026Updated Oct 1, 2026

Versions

@flue/runtime 2.1.1 and @flue/sdk 2.1.1. appendDelta in @flue/sdk 2.2.2 is the same.

What happens

A model that streams reasoning_content and content deltas interleaved shows its live reply as several text parts, so the text breaks into separate paragraphs. A reload shows one text part.

Observed with xai/grok-4.3 through the Workers AI binding provider (cloudflareBindingProvider, chat completions). The stream sends reasoning "…Six connections are", then text "You", then reasoning " active. Five are…", then text " have these connected:…".

  • Live parts (@flue/sdk): [reasoning, text "You", reasoning, text " have these connected:…"]. A renderer that draws each text part as a block shows "You", a paragraph break, then "have these connected:".
  • Stored parts after a reload: [reasoning, text "You have these connected:…"]. The runtime provider (workers-ai-provider.mjs) keeps one text block and one thinking block per assistant message, so its history has no split.

Cause

appendDelta adds a delta only to the last part, and only when that part has the same kind and is streaming. Otherwise it closes the last part and opens a new one, so each change of kind is a block boundary. The message-delta chunk has no block ID, so the client cannot tell interleaved deltas of one block from a new block.

Fix that we use (local patch)

appendDelta adds a delta to the latest streaming part of its kind when only text and reasoning parts come after that part. A change of kind does not close the other part. A tool call or the completion closes every streaming text and reasoning part:

javascript
function appendDelta(state, chunk) {
	return mutateMessages(state, (messages) => {
		const index = messages.findIndex((message) => message.id === chunk.messageId);
		if (index < 0) return messages;
		const message = messages[index];
		const parts = [...message.parts];
		const target = streamingPartOfKind(parts, chunk.kind);
		if (target >= 0) parts[target] = { ...parts[target], text: parts[target].text + chunk.delta };
		else parts.push({ type: chunk.kind, text: chunk.delta, state: "streaming" });
		const next = [...messages];
		next[index] = { ...message, parts };
		return next;
	});
}

// The latest part of `kind`, if it is streaming and only text and reasoning parts follow it.
function streamingPartOfKind(parts, kind) {
	for (let partIndex = parts.length - 1; partIndex >= 0; partIndex--) {
		const part = parts[partIndex];
		if (part.type !== "text" && part.type !== "reasoning") return -1;
		if (part.type === kind) return part.state === "streaming" ? partIndex : -1;
	}
	return -1;
}

In appendToolInput, the map that closes the last streaming text or reasoning part closes every one, because a text part and a reasoning part can both stream at the tail:

javascript
const parts = message.parts.map((part) =>
	(part.type === "text" || part.type === "reasoning") && part.state === "streaming" ? { ...part, state: "done" } : part
);

completeMessage already closes every text and reasoning part.

A consequence: a streaming part can be one that is not the last. Our status checks ask whether any part streams, so they do not change.

Alternative

The runtime's canonical events (assistant_text_delta, assistant_reasoning_delta) carry a blockId. If the message-delta chunk sends that ID, appendDelta can key the part by block, and the live parts match the stored blocks for every provider. That removes the need for the rule above.

We can share the full patch file if that helps.

Source: withastro/flue

View original on GitHubView discussion on GitHub