Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- **fix(sse):** keep an explicit client thinking type instead of overwriting it with the connection default ([#18114](https://github.com/cheaper-inference/OmniRoute/pull/18114)) — thanks @maxmad64bis
23 changes: 18 additions & 5 deletions open-sse/executors/glm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,16 +129,24 @@ function isGlmThinkingModel(model: string): boolean {
*/
const GLM_THINKING_DEFAULT_MAX_TOKENS = 131072;

function applyGlmRequestDefaults(body: unknown, defaults?: JsonRecord | null): unknown {
function applyGlmRequestDefaults(
body: unknown,
defaults?: JsonRecord | null,
clientThinkingType?: unknown
): unknown {
const record = asRecord(body);
if (!record || !defaults) return body;

const next = { ...(applyProviderRequestDefaults(record, defaults) as JsonRecord) };
const thinkingType = typeof defaults.thinkingType === "string" ? defaults.thinkingType : null;

if (thinkingType && next.thinking === undefined) {
next.thinking = { type: thinkingType };
} else if (thinkingType && asRecord(next.thinking)?.type === "enabled") {
// Apply the connection default only when the client did not set a thinking
// type of its own; other thinking fields are merged and preserved.
if (
thinkingType &&
clientThinkingType === undefined &&
(next.thinking === undefined || asRecord(next.thinking))
) {
next.thinking = { ...asRecord(next.thinking), type: thinkingType };
}

Expand Down Expand Up @@ -310,8 +318,13 @@ export class GlmExecutor extends DefaultExecutor {
stream: boolean,
credentials: ProviderCredentials
) {
const clientThinkingType = asRecord(asRecord(body)?.thinking)?.type;
const cleanedBody = super.transformRequest(model, body, stream, credentials);
return applyGlmRequestDefaults(cleanedBody, this.config.requestDefaults as JsonRecord | null);
return applyGlmRequestDefaults(
cleanedBody,
this.config.requestDefaults as JsonRecord | null,
clientThinkingType
);
}

transformForTransport(
Expand Down
54 changes: 54 additions & 0 deletions tests/unit/glm-thinking-type-explicit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import test from "node:test";
import assert from "node:assert/strict";

import { GlmExecutor } from "../../open-sse/executors/glm.ts";

type ThinkingBody = { thinking?: unknown };

test("GlmExecutor keeps explicit enabled thinking type over adaptive default", () => {
const executor = new GlmExecutor("glmt");
const body = {
messages: [{ role: "user", content: "hi" }],
thinking: { type: "enabled", budget_tokens: 1000 },
};

const transformed = executor.transformRequest("glm-5.1", body, true, {
apiKey: "glm-key",
}) as ThinkingBody;

assert.deepEqual(transformed.thinking, { type: "enabled", budget_tokens: 1000 });
});

test("GlmExecutor applies adaptive default when thinking type is missing", () => {
const executor = new GlmExecutor("glmt");

const withoutThinking = executor.transformRequest(
"glm-5.1",
{ messages: [{ role: "user", content: "hi" }] },
true,
{ apiKey: "glm-key" }
) as ThinkingBody;
assert.deepEqual(withoutThinking.thinking, { type: "adaptive", budget_tokens: 24_576 });

const typeless = executor.transformRequest(
"glm-5.1",
{ messages: [{ role: "user", content: "hi" }], thinking: { budget_tokens: 1000 } },
true,
{ apiKey: "glm-key" }
) as ThinkingBody;
assert.deepEqual(typeless.thinking, { type: "adaptive", budget_tokens: 1000 });
});

test("GlmExecutor leaves thinking untouched without connection default", () => {
const executor = new GlmExecutor("glm");
const body = {
messages: [{ role: "user", content: "hi" }],
thinking: { type: "enabled", budget_tokens: 1000 },
};

const transformed = executor.transformRequest("glm-5.1", body, false, {
apiKey: "glm-key",
}) as ThinkingBody;

assert.deepEqual(transformed.thinking, { type: "enabled", budget_tokens: 1000 });
});
Loading