Skip to content

Commit 0559f4d

Browse files
committed
fix(ollama): avoid double sanitizing kimi stream output
1 parent 9ef40db commit 0559f4d

5 files changed

Lines changed: 42 additions & 8 deletions

File tree

extensions/ollama/src/sanitizers/kimi-inline-reasoning.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,5 @@ export function createKimiInlineReasoningSanitizer(): OllamaVisibleContentSaniti
7070
const resolution = resolveInlineReasoningVisibleText({ text, final: true });
7171
return resolution.kind === "visible" ? resolution.text : text;
7272
},
73-
shouldSanitizeFinalMessage() {
74-
return !bypassInlineReasoning;
75-
},
7673
};
7774
}

extensions/ollama/src/sanitizers/visible-content-contract.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,4 @@ export type OllamaVisibleContentStreamResolution =
55
export type OllamaVisibleContentSanitizer = {
66
resolveStreamText(params: { text: string; final: boolean }): OllamaVisibleContentStreamResolution;
77
sanitizeFinalText(text: string): string;
8-
shouldSanitizeFinalMessage(): boolean;
98
};

extensions/ollama/src/sanitizers/visible-content.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ const noopVisibleContentSanitizer: OllamaVisibleContentSanitizer = {
1111
sanitizeFinalText(text) {
1212
return text;
1313
},
14-
shouldSanitizeFinalMessage() {
15-
return true;
16-
},
1714
};
1815

1916
export function createOllamaVisibleContentSanitizer(

extensions/ollama/src/stream-runtime.test.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2139,6 +2139,47 @@ describe("createOllamaStreamFn streaming events", () => {
21392139
);
21402140
});
21412141

2142+
it("does not re-sanitize visible Kimi stream output before done", async () => {
2143+
const visibleAnswer =
2144+
"This visible answer is intentionally long enough to look like a reasoning prefix if it is sanitized a second time. ️ keep this marker visible.";
2145+
await withMockNdjsonFetch(
2146+
[
2147+
JSON.stringify({
2148+
model: "kimi-k2.6:cloud",
2149+
created_at: "t",
2150+
message: {
2151+
role: "assistant",
2152+
content:
2153+
"I should think privately and not leak this planning text in the answer. I need to keep deciding what to say next. ️",
2154+
},
2155+
done: false,
2156+
}),
2157+
JSON.stringify({
2158+
model: "kimi-k2.6:cloud",
2159+
created_at: "t",
2160+
message: { role: "assistant", content: visibleAnswer },
2161+
done: false,
2162+
}),
2163+
'{"model":"kimi-k2.6:cloud","created_at":"t","message":{"role":"assistant","content":""},"done":true,"prompt_eval_count":3,"eval_count":4}',
2164+
],
2165+
async () => {
2166+
const stream = await createOllamaTestStream({
2167+
baseUrl: "http://ollama-host:11434",
2168+
model: { id: "kimi-k2.6:cloud", provider: "ollama" },
2169+
});
2170+
const events = await collectStreamEvents(stream);
2171+
const textEnd = events.find((event) => event.type === "text_end");
2172+
const doneEvent = events.at(-1);
2173+
2174+
expect(textEnd?.content).toBe(visibleAnswer);
2175+
expect(doneEvent?.type).toBe("done");
2176+
if (doneEvent?.type === "done") {
2177+
expect(doneEvent.message.content).toEqual([{ type: "text", text: visibleAnswer }]);
2178+
}
2179+
},
2180+
);
2181+
});
2182+
21422183
it("does not leak Kimi inline reasoning when a boundary is followed by tool calls only", async () => {
21432184
const hiddenPrefix =
21442185
"I should think privately and not leak this planning text in the answer. " +

extensions/ollama/src/stream.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1367,7 +1367,7 @@ export function createOllamaStreamFn(
13671367
};
13681368
const assistantMessage = buildAssistantMessage(finalResponse, modelInfo, usageFallback, {
13691369
...toolCallNameOptions,
1370-
sanitizeVisibleContent: visibleContentSanitizer.shouldSanitizeFinalMessage(),
1370+
sanitizeVisibleContent: false,
13711371
});
13721372
closeThinkingBlock();
13731373
closeTextBlock();

0 commit comments

Comments
 (0)