Skip to content

Commit afe024e

Browse files
authored
fix(ai): assistant text blocks are run together on replay (#115743)
Chat Completions replay flattened every assistant text block with an empty separator, so two distinct blocks came back as one word-joined sentence. The same message shape survives distinctly on the Anthropic, Responses and Mistral lanes, and the string-content flattener for strict OpenAI-compatible servers already joins with a newline. Two blocks arise routinely: streaming opens a new text block after a tool call, and cross-model replay converts a thinking block into a text block.
1 parent fde5420 commit afe024e

2 files changed

Lines changed: 55 additions & 1 deletion

File tree

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { describe, expect, it } from "vitest";
2+
import { convertMessages } from "./openai-completions-messages.js";
3+
import { resolveOpenAICompletionsCompat } from "./transports/openai-completions-compat.js";
4+
import type { AssistantMessage, Context, Model } from "./types.js";
5+
6+
const model: Model<"openai-completions"> = {
7+
id: "test-model",
8+
name: "Test model",
9+
api: "openai-completions",
10+
provider: "custom-openai-compatible",
11+
baseUrl: "https://proxy.example/v1",
12+
reasoning: false,
13+
input: ["text"],
14+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
15+
contextWindow: 128_000,
16+
maxTokens: 4_096,
17+
};
18+
19+
const emptyUsage = {
20+
input: 0,
21+
output: 0,
22+
cacheRead: 0,
23+
cacheWrite: 0,
24+
totalTokens: 0,
25+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
26+
};
27+
28+
describe("convertMessages assistant text replay", () => {
29+
it("keeps separate assistant text blocks apart", () => {
30+
const assistant: AssistantMessage = {
31+
role: "assistant",
32+
api: model.api,
33+
provider: model.provider,
34+
model: model.id,
35+
content: [
36+
{ type: "text", text: "Let me check the file." },
37+
{ type: "text", text: "The file contains X." },
38+
],
39+
usage: emptyUsage,
40+
stopReason: "stop",
41+
timestamp: 2,
42+
};
43+
const context: Context = {
44+
messages: [{ role: "user", content: "hello", timestamp: 1 }, assistant],
45+
};
46+
47+
const converted = convertMessages(model, context, resolveOpenAICompletionsCompat(model));
48+
49+
const replayed = converted.find((message) => message.role === "assistant");
50+
expect(replayed?.content).toBe("Let me check the file.\nThe file contains X.");
51+
});
52+
});

‎packages/ai/src/openai-completions-messages.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,9 @@ export function convertMessages(
141141
text: sanitizeSurrogates(block.text),
142142
}) satisfies ChatCompletionContentPartText,
143143
);
144-
const assistantText = assistantTextParts.map((part) => part.text).join("");
144+
// Separate content blocks are distinct utterances, so replay them the way
145+
// the string-content flattener does rather than running them together.
146+
const assistantText = assistantTextParts.map((part) => part.text).join("\n");
145147

146148
const nonEmptyThinkingBlocks = msg.content
147149
.filter(isThinkingContentBlock)

0 commit comments

Comments
 (0)