mirror of
https://github.com/Nezumi-2711/9router.git
synced 2026-09-22 13:38:31 +00:00
fix(kimchi): strip reasoning_content echo to bound multi-turn input tokens
Clients echo full message history each turn including reasoning_content, which the Kimchi OpenAI gateway counts as input tokens. Multi-turn convos balloon to 100k+ tokens and the model returns empty content. KimchiExecutor.transformRequest now strips reasoning_content from assistant messages when it exceeds an 8-char threshold, preserving the 1-char placeholder injectReasoningContent sets and keeping content intact. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
committed by
decolua
co-authored by
Cursor
parent
a5363b83b5
commit
7afaecd617
@@ -67,6 +67,25 @@ function stripToolArtifacts(body) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Strip `reasoning_content` echoed by clients on assistant messages — but
|
||||||
|
// only when it's a real thinking block. `DefaultExecutor.transformRequest`
|
||||||
|
// runs `injectReasoningContent` first and may inject a 1-char placeholder
|
||||||
|
// (" ") for upstream validation; the placeholder is small (no token cost
|
||||||
|
// worth stripping) and stripping it would re-trigger upstream to complain
|
||||||
|
// about missing reasoning on the next turn. Threshold matches the
|
||||||
|
// placeholder length with a safety margin.
|
||||||
|
const REASONING_PLACEHOLDER_MAX_LEN = 8;
|
||||||
|
|
||||||
|
export function stripReasoningContent(body) {
|
||||||
|
if (!Array.isArray(body?.messages)) return;
|
||||||
|
for (const msg of body.messages) {
|
||||||
|
if (msg && msg.role === "assistant" && typeof msg.reasoning_content === "string"
|
||||||
|
&& msg.reasoning_content.length > REASONING_PLACEHOLDER_MAX_LEN) {
|
||||||
|
delete msg.reasoning_content;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function isAnthropicBackedKimchiModel(model) {
|
function isAnthropicBackedKimchiModel(model) {
|
||||||
const meta = getCachedKimchiModelMetadata(model);
|
const meta = getCachedKimchiModelMetadata(model);
|
||||||
if (meta?.provider === "anthropic" || meta?.upstreamProvider === "anthropic") return true;
|
if (meta?.provider === "anthropic" || meta?.upstreamProvider === "anthropic") return true;
|
||||||
@@ -96,6 +115,7 @@ export class KimchiExecutor extends DefaultExecutor {
|
|||||||
|
|
||||||
stripMessageArtifacts(transformed);
|
stripMessageArtifacts(transformed);
|
||||||
stripToolArtifacts(transformed);
|
stripToolArtifacts(transformed);
|
||||||
|
stripReasoningContent(transformed);
|
||||||
return transformed;
|
return transformed;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,128 @@
|
|||||||
|
/**
|
||||||
|
* Kimchi executor: strip reasoning_content echoed by clients.
|
||||||
|
*
|
||||||
|
* Background: when 9Router streams a thinking model (deepseek-r1,
|
||||||
|
* minimax-m3) to a client, the response carries `reasoning_content`.
|
||||||
|
* Most OpenAI-compatible SDKs echo the whole history on the next turn,
|
||||||
|
* so Kimchi's upstream counts the scratch block as input tokens.
|
||||||
|
* Multi-turn conversations balloon to 100k+ input tokens and the model
|
||||||
|
* starts returning empty content.
|
||||||
|
*
|
||||||
|
* `stripReasoningContent` is intentionally conservative: it only strips
|
||||||
|
* `reasoning_content` that is clearly a real thinking block. The 1-char
|
||||||
|
* placeholder that `injectReasoningContent` (in `DefaultExecutor`) may
|
||||||
|
* insert for upstream validation is preserved — stripping it would
|
||||||
|
* re-trigger upstream complaints about missing reasoning on the next
|
||||||
|
* turn.
|
||||||
|
*/
|
||||||
|
import { describe, it } from "node:test";
|
||||||
|
import assert from "node:assert/strict";
|
||||||
|
|
||||||
|
import KimchiExecutor, { stripReasoningContent } from "../../open-sse/executors/kimchi.js";
|
||||||
|
import DefaultExecutor from "../../open-sse/executors/default.js";
|
||||||
|
|
||||||
|
describe("kimchi stripReasoningContent", () => {
|
||||||
|
it("removes long reasoning_content from assistant messages but keeps content", () => {
|
||||||
|
const body = {
|
||||||
|
messages: [
|
||||||
|
{ role: "user", content: "solve x+5=12" },
|
||||||
|
{
|
||||||
|
role: "assistant",
|
||||||
|
content: "x = 7",
|
||||||
|
reasoning_content: "subtract 5 from both sides ... (long reasoning block)",
|
||||||
|
},
|
||||||
|
{ role: "user", content: "now try x+10=20" },
|
||||||
|
],
|
||||||
|
};
|
||||||
|
stripReasoningContent(body);
|
||||||
|
assert.equal(body.messages[1].reasoning_content, undefined);
|
||||||
|
assert.equal(body.messages[1].content, "x = 7");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("preserves the 1-char placeholder that injectReasoningContent sets", () => {
|
||||||
|
// `injectReasoningContent` may insert " " (single space) on assistant
|
||||||
|
// messages so the upstream's validation doesn't complain about missing
|
||||||
|
// reasoning. Stripping that placeholder would defeat its purpose.
|
||||||
|
const body = {
|
||||||
|
messages: [
|
||||||
|
{ role: "user", content: "hi" },
|
||||||
|
{ role: "assistant", content: "hello", reasoning_content: " " },
|
||||||
|
],
|
||||||
|
};
|
||||||
|
stripReasoningContent(body);
|
||||||
|
assert.equal(body.messages[1].reasoning_content, " ");
|
||||||
|
assert.equal(body.messages[1].content, "hello");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("preserves short custom reasoning under the threshold", () => {
|
||||||
|
// Anything ≤8 chars is treated as a placeholder-shaped value, kept
|
||||||
|
// verbatim. Real thinking content from a thinking model is always
|
||||||
|
// well above this threshold.
|
||||||
|
const body = {
|
||||||
|
messages: [
|
||||||
|
{ role: "assistant", content: "ok", reasoning_content: "short" },
|
||||||
|
],
|
||||||
|
};
|
||||||
|
stripReasoningContent(body);
|
||||||
|
assert.equal(body.messages[0].reasoning_content, "short");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("leaves non-assistant messages untouched", () => {
|
||||||
|
const body = {
|
||||||
|
messages: [
|
||||||
|
{ role: "user", content: "hi" },
|
||||||
|
{ role: "system", content: "be helpful" },
|
||||||
|
],
|
||||||
|
};
|
||||||
|
stripReasoningContent(body);
|
||||||
|
assert.equal(body.messages[0].content, "hi");
|
||||||
|
assert.equal(body.messages[1].content, "be helpful");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns early on missing/empty messages array", () => {
|
||||||
|
assert.doesNotThrow(() => stripReasoningContent({}));
|
||||||
|
assert.doesNotThrow(() => stripReasoningContent({ messages: null }));
|
||||||
|
assert.doesNotThrow(() => stripReasoningContent({ messages: [] }));
|
||||||
|
});
|
||||||
|
|
||||||
|
it("ignores assistant messages that have no reasoning_content", () => {
|
||||||
|
const body = {
|
||||||
|
messages: [
|
||||||
|
{ role: "user", content: "hi" },
|
||||||
|
{ role: "assistant", content: "hello" },
|
||||||
|
],
|
||||||
|
};
|
||||||
|
stripReasoningContent(body);
|
||||||
|
assert.deepEqual(body.messages[1], { role: "assistant", content: "hello" });
|
||||||
|
});
|
||||||
|
|
||||||
|
it("handles multi-turn: strips old turns, keeps recent one", () => {
|
||||||
|
const LONG = "x".repeat(1000);
|
||||||
|
const body = {
|
||||||
|
messages: [
|
||||||
|
{ role: "user", content: "q1" },
|
||||||
|
{ role: "assistant", content: "a1", reasoning_content: LONG },
|
||||||
|
{ role: "user", content: "q2" },
|
||||||
|
{ role: "assistant", content: "a2", reasoning_content: " " }, // placeholder
|
||||||
|
],
|
||||||
|
};
|
||||||
|
stripReasoningContent(body);
|
||||||
|
assert.equal(body.messages[1].reasoning_content, undefined);
|
||||||
|
assert.equal(body.messages[3].reasoning_content, " ");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("kimchi executor wiring", () => {
|
||||||
|
it("KimchiExecutor extends DefaultExecutor via prototype chain", () => {
|
||||||
|
const inst = new KimchiExecutor();
|
||||||
|
assert.ok(
|
||||||
|
inst instanceof DefaultExecutor,
|
||||||
|
"KimchiExecutor must extend DefaultExecutor so transformRequest runs through super",
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("default export is KimchiExecutor class", () => {
|
||||||
|
assert.equal(typeof KimchiExecutor, "function");
|
||||||
|
assert.equal(KimchiExecutor.name, "KimchiExecutor");
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user