mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-08-01 04:42:10 +03:00
* fix(volcengine): clamp Kimi max_tokens to Ark endpoint cap VolcEngine Ark's Kimi coding-plan endpoint (ark.cn-beijing.volces.com) enforces max_tokens <= 32768 server-side and returns 400 "integer above maximum value, expected a value <= 32768" for anything over that ceiling. OmniRoute's StripRule only supported dropping params outright, with no numeric clamp mechanism, so a client sending a larger max_tokens (common default, e.g. 65536) 400s outright against volcengine's kimi-k2-5-260127. The 32768 cap is independently confirmed against two live-endpoint bug reports hitting this exact Ark endpoint for both kimi-k2.5 and kimi-k2.7-code (NousResearch/hermes-agent#51773, MoonshotAI/kimi-cli#1124), not just upstream's own value — same cap upstream 9router#2460 uses. StripRule gains two optional fields: `clampToModelMaxOutput` (clamp to the model's own catalog maxOutputTokens ceiling, when set) and `maxOutputCap` (a fixed endpoint-imposed ceiling); when both apply, the lower wins. The new rule is scoped to the literal id `kimi-k2-5-260127` (OmniRoute's real volcengine Kimi model, not upstream's `Kimi-K2.7-Code`), not a broad /kimi/i regex, so it can never clamp an unrelated future Kimi listing whose Ark cap may differ. glm-4-7-251222 (the other volcengine model) is unaffected. Inspired-by: https://github.com/decolua/9router/pull/2460 Co-authored-by: whale9820 <whale9820@users.noreply.github.com> * chore(6712): re-sync onto release tip; CHANGELOG → changelog.d fragment (fragments-first) --------- Co-authored-by: whale9820 <whale9820@users.noreply.github.com>
204 lines
9.3 KiB
TypeScript
204 lines
9.3 KiB
TypeScript
// Unit tests for stripUnsupportedParams: per-(provider,model) strip of params
|
|
// the upstream rejects (HTTP 400). Port from 9router#7ae9fff6 (fixes #1748).
|
|
//
|
|
// Rule coverage:
|
|
// 1. claude-opus-4 series: temperature deprecated → Anthropic 400.
|
|
// 2. github + gpt-5.4: temperature unsupported.
|
|
// 3. github + Claude (except opus/sonnet 4.6): thinking + reasoning_effort rejected.
|
|
// 4. nvidia + z-ai/glm-5.2: reasoning rejected → NVIDIA 400.
|
|
// 5. volcengine + kimi-k2-5-260127: max_tokens clamped to the Ark endpoint cap
|
|
// (32768), confirmed independently against two live-endpoint reports for the
|
|
// same Volcengine Ark Kimi coding-plan endpoint (decolua/9router#2460;
|
|
// NousResearch/hermes-agent#51773; MoonshotAI/kimi-cli#1124).
|
|
|
|
import { test } from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import {
|
|
stripUnsupportedParams,
|
|
__STRIP_RULES_FOR_TEST,
|
|
} from "../../open-sse/translator/paramSupport.ts";
|
|
|
|
test("stripUnsupportedParams: drops temperature for claude-opus-4 models (any provider)", () => {
|
|
const body = { model: "claude-opus-4-20250514", temperature: 0.7, max_tokens: 100 };
|
|
stripUnsupportedParams("anthropic", "claude-opus-4-20250514", body);
|
|
assert.equal(body.temperature, undefined, "temperature must be stripped");
|
|
assert.equal(body.max_tokens, 100, "other params must survive");
|
|
assert.equal(body.model, "claude-opus-4-20250514", "model must not be touched");
|
|
});
|
|
|
|
test("stripUnsupportedParams: drops temperature for claude-opus-4-1 (case insensitive)", () => {
|
|
const body: Record<string, unknown> = { temperature: 0.5 };
|
|
stripUnsupportedParams("anthropic", "CLAUDE-OPUS-4-1-20250805", body);
|
|
assert.equal(body.temperature, undefined);
|
|
});
|
|
|
|
test("stripUnsupportedParams: keeps temperature for claude-sonnet-4 (only opus-4 affected)", () => {
|
|
const body: Record<string, unknown> = { temperature: 0.7 };
|
|
stripUnsupportedParams("anthropic", "claude-sonnet-4-20250514", body);
|
|
assert.equal(body.temperature, 0.7, "sonnet-4 still accepts temperature");
|
|
});
|
|
|
|
test("stripUnsupportedParams: keeps temperature for claude-opus-3 (regression guard)", () => {
|
|
const body: Record<string, unknown> = { temperature: 0.7 };
|
|
stripUnsupportedParams("anthropic", "claude-3-opus-20240229", body);
|
|
assert.equal(body.temperature, 0.7);
|
|
});
|
|
|
|
test("stripUnsupportedParams: github + gpt-5.4 strips temperature", () => {
|
|
const body: Record<string, unknown> = { temperature: 1, max_completion_tokens: 200 };
|
|
stripUnsupportedParams("github", "gpt-5.4", body);
|
|
assert.equal(body.temperature, undefined);
|
|
assert.equal(body.max_completion_tokens, 200);
|
|
});
|
|
|
|
test("stripUnsupportedParams: github + gpt-5 (non-5.4) keeps temperature", () => {
|
|
const body: Record<string, unknown> = { temperature: 1 };
|
|
stripUnsupportedParams("github", "gpt-5", body);
|
|
assert.equal(body.temperature, 1);
|
|
});
|
|
|
|
test("stripUnsupportedParams: github + Claude strips thinking + reasoning_effort", () => {
|
|
const body: Record<string, unknown> = {
|
|
thinking: { type: "enabled" },
|
|
reasoning_effort: "high",
|
|
temperature: 0.5,
|
|
};
|
|
stripUnsupportedParams("github", "claude-3-5-sonnet", body);
|
|
assert.equal(body.thinking, undefined, "Copilot rejects Claude-style thinking");
|
|
assert.equal(body.reasoning_effort, undefined);
|
|
assert.equal(body.temperature, 0.5, "non-targeted params survive");
|
|
});
|
|
|
|
test("stripUnsupportedParams: github + claude opus 4.6 KEEPS thinking + reasoning_effort", () => {
|
|
const body: Record<string, unknown> = {
|
|
thinking: { type: "enabled" },
|
|
reasoning_effort: "high",
|
|
};
|
|
stripUnsupportedParams("github", "claude-opus-4.6", body);
|
|
assert.equal(body.reasoning_effort, "high", "opus-4.6 supports reasoning_effort");
|
|
// Note: thinking survives too on opus-4.6 per upstream rule.
|
|
assert.deepEqual(body.thinking, { type: "enabled" });
|
|
});
|
|
|
|
test("stripUnsupportedParams: github + claude sonnet 4.6 KEEPS reasoning_effort", () => {
|
|
const body: Record<string, unknown> = { reasoning_effort: "low" };
|
|
stripUnsupportedParams("github", "claude-sonnet-4.6", body);
|
|
assert.equal(body.reasoning_effort, "low");
|
|
});
|
|
|
|
test("stripUnsupportedParams: non-github provider + Claude does NOT strip thinking", () => {
|
|
// The github-Claude rule is provider-scoped.
|
|
const body: Record<string, unknown> = { thinking: { type: "enabled" } };
|
|
stripUnsupportedParams("anthropic", "claude-3-5-sonnet", body);
|
|
assert.deepEqual(body.thinking, { type: "enabled" });
|
|
});
|
|
|
|
test("stripUnsupportedParams: only deletes when the key is present (never adds undefined)", () => {
|
|
const body: Record<string, unknown> = { max_tokens: 50 };
|
|
stripUnsupportedParams("anthropic", "claude-opus-4", body);
|
|
// No `temperature` key should be introduced.
|
|
assert.equal("temperature" in body, false);
|
|
});
|
|
|
|
test("stripUnsupportedParams: returns the same body reference (in-place mutation)", () => {
|
|
const body: Record<string, unknown> = { temperature: 0.7 };
|
|
const out = stripUnsupportedParams("anthropic", "claude-opus-4", body);
|
|
assert.equal(out, body);
|
|
});
|
|
|
|
test("stripUnsupportedParams: null/undefined body is a no-op", () => {
|
|
assert.equal(stripUnsupportedParams("anthropic", "claude-opus-4", null), null);
|
|
assert.equal(stripUnsupportedParams("anthropic", "claude-opus-4", undefined), undefined);
|
|
});
|
|
|
|
test("stripUnsupportedParams: missing model is a no-op", () => {
|
|
const body: Record<string, unknown> = { temperature: 0.7 };
|
|
stripUnsupportedParams("anthropic", "", body);
|
|
assert.equal(body.temperature, 0.7);
|
|
});
|
|
|
|
test("stripUnsupportedParams: drops reasoning for nvidia z-ai/glm-5.2", () => {
|
|
const body: Record<string, unknown> = {
|
|
model: "z-ai/glm-5.2",
|
|
reasoning: { effort: "high" },
|
|
temperature: 0.7,
|
|
};
|
|
stripUnsupportedParams("nvidia", "z-ai/glm-5.2", body);
|
|
assert.equal(body.reasoning, undefined, "reasoning must be stripped");
|
|
assert.equal(body.temperature, 0.7, "other params must survive");
|
|
assert.equal(body.model, "z-ai/glm-5.2", "model must not be touched");
|
|
});
|
|
|
|
test("stripUnsupportedParams: nvidia z-ai/glm-5.1 keeps reasoning (rule is 5.2-only)", () => {
|
|
const body: Record<string, unknown> = {
|
|
model: "z-ai/glm-5.1",
|
|
reasoning: { effort: "medium" },
|
|
max_tokens: 100,
|
|
};
|
|
stripUnsupportedParams("nvidia", "z-ai/glm-5.1", body);
|
|
assert.ok(body.reasoning !== undefined, "reasoning must survive for glm-5.1");
|
|
assert.equal(body.max_tokens, 100, "other params must survive");
|
|
});
|
|
|
|
test("stripUnsupportedParams: nvidia glm-5 rule is provider-scoped (no-op for other providers)", () => {
|
|
const body: Record<string, unknown> = {
|
|
model: "z-ai/glm-5.2",
|
|
reasoning: { effort: "high" },
|
|
};
|
|
stripUnsupportedParams("openai", "z-ai/glm-5.2", body);
|
|
assert.ok(body.reasoning !== undefined, "reasoning must survive for non-nvidia provider");
|
|
});
|
|
|
|
test("stripUnsupportedParams: nvidia non-glm-5 model keeps reasoning", () => {
|
|
const body: Record<string, unknown> = {
|
|
model: "deepseek-ai/deepseek-v4-pro",
|
|
reasoning: { effort: "high" },
|
|
};
|
|
stripUnsupportedParams("nvidia", "deepseek-ai/deepseek-v4-pro", body);
|
|
assert.ok(body.reasoning !== undefined, "reasoning must survive for non-glm-5 nvidia model");
|
|
});
|
|
|
|
test("STRIP_RULES is non-empty and every rule has a drop list or a clamp mechanism", () => {
|
|
assert.ok(__STRIP_RULES_FOR_TEST.length > 0);
|
|
for (const rule of __STRIP_RULES_FOR_TEST) {
|
|
const hasDrop = Array.isArray(rule.drop) && rule.drop.length > 0;
|
|
const hasClamp = rule.clampToModelMaxOutput === true || Number.isFinite(rule.maxOutputCap);
|
|
assert.ok(hasDrop || hasClamp, "rule must either drop params or clamp max output");
|
|
assert.ok(typeof rule.match === "function" || rule.match instanceof RegExp);
|
|
}
|
|
});
|
|
|
|
test("stripUnsupportedParams: volcengine kimi-k2-5-260127 clamps max_tokens above the Ark endpoint cap (32768)", () => {
|
|
const body: Record<string, unknown> = { max_tokens: 65536 };
|
|
stripUnsupportedParams("volcengine", "kimi-k2-5-260127", body);
|
|
assert.equal(body.max_tokens, 32768, "oversized max_tokens must be clamped to the Ark cap");
|
|
});
|
|
|
|
test("stripUnsupportedParams: volcengine kimi-k2-5-260127 leaves max_tokens under the cap unchanged", () => {
|
|
const body: Record<string, unknown> = { max_tokens: 8000 };
|
|
stripUnsupportedParams("volcengine", "kimi-k2-5-260127", body);
|
|
assert.equal(body.max_tokens, 8000, "max_tokens under the cap must not be modified");
|
|
});
|
|
|
|
test("stripUnsupportedParams: volcengine kimi-k2-5-260127 also clamps max_completion_tokens/max_output_tokens", () => {
|
|
const body: Record<string, unknown> = {
|
|
max_completion_tokens: 100000,
|
|
max_output_tokens: 50000,
|
|
};
|
|
stripUnsupportedParams("volcengine", "kimi-k2-5-260127", body);
|
|
assert.equal(body.max_completion_tokens, 32768);
|
|
assert.equal(body.max_output_tokens, 32768);
|
|
});
|
|
|
|
test("stripUnsupportedParams: volcengine non-kimi model (glm-4-7-251222) is NOT clamped by the kimi rule", () => {
|
|
const body: Record<string, unknown> = { max_tokens: 65536 };
|
|
stripUnsupportedParams("volcengine", "glm-4-7-251222", body);
|
|
assert.equal(body.max_tokens, 65536, "kimi-specific cap must not apply to other volcengine models");
|
|
});
|
|
|
|
test("stripUnsupportedParams: kimi rule is provider-scoped (no-op for non-volcengine providers)", () => {
|
|
const body: Record<string, unknown> = { max_tokens: 65536 };
|
|
stripUnsupportedParams("kimi", "kimi-k2-5-260127", body);
|
|
assert.equal(body.max_tokens, 65536, "the Ark-specific cap must not leak to other kimi-hosting providers");
|
|
});
|