Skip to content

Commit a3304ed

Browse files
committed
fix(clawrouter): honor advertised reasoning efforts
1 parent 7ecdef6 commit a3304ed

4 files changed

Lines changed: 223 additions & 11 deletions

File tree

‎extensions/clawrouter/index.test.ts‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ describe("ClawRouter plugin", () => {
5757
prepareDynamicModel: expect.any(Function),
5858
preferRuntimeResolvedModel: expect.any(Function),
5959
resolveDynamicModel: expect.any(Function),
60+
resolveThinkingProfile: expect.any(Function),
6061
resolveUsageAuth: expect.any(Function),
6162
sanitizeReplayHistory: expect.any(Function),
6263
wrapSimpleCompletionStreamFn: expect.any(Function),
@@ -70,6 +71,73 @@ describe("ClawRouter plugin", () => {
7071
expect(provider?.wrapSimpleCompletionStreamFn).toBe(provider?.wrapStreamFn);
7172
});
7273

74+
it("resolves authoritative catalog thinking profiles without inventing minimal", async () => {
75+
const provider = await registerSingleProviderPlugin(plugin);
76+
const compat = {
77+
supportedReasoningEfforts: ["none", "low", "medium", "high", "xhigh", "max"],
78+
};
79+
const resolveLevels = (agentRuntime: string | undefined) =>
80+
provider
81+
?.resolveThinkingProfile?.({
82+
provider: "clawrouter",
83+
modelId: "opaque-route-id",
84+
agentRuntime,
85+
compat,
86+
} as never)
87+
?.levels.map((level) => level.id);
88+
89+
expect(resolveLevels("openclaw")).toEqual([
90+
"off",
91+
"low",
92+
"medium",
93+
"high",
94+
"xhigh",
95+
"max",
96+
"ultra",
97+
]);
98+
expect(resolveLevels("auto")).toEqual([
99+
"off",
100+
"low",
101+
"medium",
102+
"high",
103+
"xhigh",
104+
"max",
105+
"ultra",
106+
]);
107+
expect(resolveLevels("codex")).toEqual(["off", "low", "medium", "high", "xhigh", "max"]);
108+
expect(resolveLevels(undefined)).toEqual(["off", "low", "medium", "high", "xhigh", "max"]);
109+
expect(
110+
provider?.resolveThinkingProfile?.({
111+
provider: "clawrouter",
112+
modelId: "opaque-route-id",
113+
} as never),
114+
).toBeUndefined();
115+
});
116+
117+
it("bounds configured thinking metadata at the provider hook", async () => {
118+
const provider = await registerSingleProviderPlugin(plugin);
119+
120+
expect(
121+
provider
122+
?.resolveThinkingProfile?.({
123+
provider: "clawrouter",
124+
modelId: "opaque-route-id",
125+
agentRuntime: "openclaw",
126+
compat: {
127+
supportedReasoningEfforts: ["ultra", "custom", "none", "none", "max"],
128+
},
129+
} as never)
130+
?.levels.map((level) => level.id),
131+
).toEqual(["off", "max", "ultra"]);
132+
expect(
133+
provider?.resolveThinkingProfile?.({
134+
provider: "clawrouter",
135+
modelId: "opaque-route-id",
136+
compat: { supportedReasoningEfforts: ["ultra", "custom"] },
137+
} as never),
138+
).toBeUndefined();
139+
});
140+
73141
it("attaches the proxy key and native upstream id only at request dispatch", async () => {
74142
const provider = await registerSingleProviderPlugin(plugin);
75143
const calls: Array<Parameters<StreamFn>[0]> = [];

‎extensions/clawrouter/index.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
// ClawRouter plugin entrypoint registers credential-scoped model routing and quota reporting.
22
import type {
3+
ProviderDefaultThinkingPolicyContext,
34
ProviderResolveDynamicModelContext,
45
ProviderRuntimeModel,
6+
ProviderThinkingProfile,
57
} from "openclaw/plugin-sdk/plugin-entry";
68
import { defineSingleProviderPluginEntry } from "openclaw/plugin-sdk/provider-entry";
79
import { buildProviderReplayFamilyHooks } from "openclaw/plugin-sdk/provider-model-shared";
810
import { buildProviderToolCompatFamilyHooks } from "openclaw/plugin-sdk/provider-tools";
911
import manifest from "./openclaw.plugin.json" with { type: "json" };
1012
import {
1113
buildClawRouterProviderConfig,
14+
CLAWROUTER_REASONING_EFFORT_LEVELS,
1215
normalizeClawRouterApiBaseUrl,
16+
normalizeClawRouterReasoningEfforts,
1317
normalizeClawRouterRootUrl,
1418
normalizeClawRouterResolvedModel,
1519
} from "./provider-catalog.js";
@@ -36,6 +40,28 @@ const perplexityTools = {
3640
inspectToolSchemas: inspectPerplexityToolSchemas,
3741
};
3842

43+
function resolveClawRouterThinkingProfile(
44+
ctx: ProviderDefaultThinkingPolicyContext,
45+
): ProviderThinkingProfile | undefined {
46+
const efforts = normalizeClawRouterReasoningEfforts(ctx.compat?.supportedReasoningEfforts);
47+
if (!efforts) {
48+
return undefined;
49+
}
50+
const supported = new Set(efforts);
51+
const levels: Array<ProviderThinkingProfile["levels"][number]> =
52+
CLAWROUTER_REASONING_EFFORT_LEVELS.filter(([effort]) => supported.has(effort)).map(
53+
([, id]) => ({ id }),
54+
);
55+
const runtime = ctx.agentRuntime?.trim().toLowerCase();
56+
if (
57+
levels.some((level) => level.id === "max") &&
58+
(runtime === "openclaw" || runtime === "auto")
59+
) {
60+
levels.push({ id: "ultra" });
61+
}
62+
return { levels };
63+
}
64+
3965
function configuredBaseUrl(
4066
config: { models?: { providers?: Record<string, { baseUrl?: unknown }> } } | null | undefined,
4167
): string | undefined {
@@ -213,6 +239,7 @@ export default defineSingleProviderPluginEntry({
213239
ctx.modelApi === "google-generative-ai"
214240
? googleReplay.resolveReasoningOutputMode?.(ctx)
215241
: undefined,
242+
resolveThinkingProfile: resolveClawRouterThinkingProfile,
216243
normalizeToolSchemas: (ctx) => resolveToolFamily(ctx.modelId ?? "").normalizeToolSchemas(ctx),
217244
inspectToolSchemas: (ctx) => resolveToolFamily(ctx.modelId ?? "").inspectToolSchemas(ctx),
218245
isModernModelRef: () => true,

‎extensions/clawrouter/provider-catalog.test.ts‎

Lines changed: 80 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,10 @@ const CATALOG = {
3737
],
3838
models: [
3939
{
40-
id: "openai/gpt-5.5",
41-
upstream: "gpt-5.5",
40+
id: "openai/gpt-5.6",
41+
upstream: "gpt-5.6",
4242
capabilities: ["llm.responses", "llm.chat"],
43+
supportedReasoningEfforts: ["none", "low", "medium", "high", "xhigh", "max"],
4344
pricing: PRICING,
4445
},
4546
],
@@ -157,9 +158,10 @@ describe("ClawRouter provider catalog", () => {
157158
"anthropic/claude-sonnet-4-6",
158159
"deepseek/deepseek-v4-flash",
159160
"google/gemini-3.5-flash",
160-
"openai/gpt-5.5",
161+
"openai/gpt-5.6",
161162
]);
162-
expect(provider.models.find((model) => model.id === "openai/gpt-5.5")).toMatchObject({
163+
const openai = provider.models.find((model) => model.id === "openai/gpt-5.6");
164+
expect(openai).toMatchObject({
163165
api: "openai-responses",
164166
baseUrl: "https://clawrouter.example/v1",
165167
reasoning: true,
@@ -168,9 +170,23 @@ describe("ClawRouter provider catalog", () => {
168170
contextWindow: 1_000_000,
169171
maxTokens: 64_000,
170172
});
171-
expect(
172-
provider.models.find((model) => model.id === "deepseek/deepseek-v4-flash"),
173-
).toMatchObject({ api: "openai-completions" });
173+
expect(openai?.thinkingLevelMap).toEqual({
174+
off: "none",
175+
minimal: null,
176+
low: "low",
177+
medium: "medium",
178+
high: "high",
179+
xhigh: "xhigh",
180+
max: "max",
181+
});
182+
expect(openai?.compat).toEqual({
183+
supportsReasoningEffort: true,
184+
supportedReasoningEfforts: ["none", "low", "medium", "high", "xhigh", "max"],
185+
});
186+
const deepseek = provider.models.find((model) => model.id === "deepseek/deepseek-v4-flash");
187+
expect(deepseek).toMatchObject({ api: "openai-completions" });
188+
expect(deepseek?.compat).toBeUndefined();
189+
expect(deepseek?.thinkingLevelMap).toBeUndefined();
174190
expect(
175191
provider.models.find((model) => model.id === "anthropic/claude-sonnet-4-6"),
176192
).toMatchObject({
@@ -206,17 +222,71 @@ describe("ClawRouter provider catalog", () => {
206222
params: undefined,
207223
});
208224

209-
const openai = provider.models.find((model) => model.id === "openai/gpt-5.5");
225+
const openaiModel = provider.models.find((model) => model.id === "openai/gpt-5.6");
210226
const normalizedOpenAi = normalizeClawRouterResolvedModel({
211-
...openai,
227+
...openaiModel,
212228
baseUrl: provider.baseUrl,
213229
provider: "clawrouter",
214230
} as ProviderRuntimeModel);
215231
expect(prepareClawRouterRequestModel(normalizedOpenAi as ProviderRuntimeModel).id).toBe(
216-
"openai/gpt-5.5",
232+
"openai/gpt-5.6",
217233
);
218234
});
219235

236+
it("bounds reasoning effort metadata to exact canonical wire values", async () => {
237+
const catalog = structuredClone(CATALOG);
238+
const model = expectDefined(catalog.providers[0]?.models[0], "OpenAI ClawRouter model");
239+
(model as unknown as Record<string, unknown>).supportedReasoningEfforts = [
240+
"max",
241+
"none",
242+
"ultra",
243+
"low",
244+
"low",
245+
null,
246+
"xhigh",
247+
];
248+
const provider = await buildClawRouterProviderConfig({
249+
apiKey: "clawrouter-test-key",
250+
fetchGuard: buildFetchGuard(catalog).fetchGuard,
251+
});
252+
const bounded = provider.models.find((entry) => entry.id === "openai/gpt-5.6");
253+
254+
expect(bounded?.compat?.supportedReasoningEfforts).toEqual(["none", "low", "xhigh", "max"]);
255+
expect(bounded?.thinkingLevelMap).toEqual({
256+
off: "none",
257+
minimal: null,
258+
low: "low",
259+
medium: null,
260+
high: null,
261+
xhigh: "xhigh",
262+
max: "max",
263+
});
264+
265+
const malformedCatalog = structuredClone(CATALOG);
266+
const malformed = expectDefined(
267+
malformedCatalog.providers[0]?.models[0],
268+
"OpenAI ClawRouter model",
269+
);
270+
(malformed as unknown as Record<string, unknown>).supportedReasoningEfforts = [
271+
"none",
272+
"minimal",
273+
"low",
274+
"medium",
275+
"high",
276+
"xhigh",
277+
"max",
278+
"ultra",
279+
];
280+
clearLiveCatalogCacheForTests();
281+
const rejected = await buildClawRouterProviderConfig({
282+
apiKey: "clawrouter-test-key",
283+
fetchGuard: buildFetchGuard(malformedCatalog).fetchGuard,
284+
});
285+
const rejectedModel = rejected.models.find((entry) => entry.id === "openai/gpt-5.6");
286+
expect(rejectedModel?.compat).toBeUndefined();
287+
expect(rejectedModel?.thinkingLevelMap).toBeUndefined();
288+
});
289+
220290
it("caches catalog rows per credential scope", async () => {
221291
const { fetchGuard, fetchGuardMock } = buildFetchGuard();
222292
const params = {

‎extensions/clawrouter/provider-catalog.ts‎

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,17 @@ const DEFAULT_COST = {
2323
cacheWrite: 0,
2424
};
2525

26+
export const CLAWROUTER_REASONING_EFFORT_LEVELS = [
27+
["none", "off"],
28+
["minimal", "minimal"],
29+
["low", "low"],
30+
["medium", "medium"],
31+
["high", "high"],
32+
["xhigh", "xhigh"],
33+
["max", "max"],
34+
] as const;
35+
type CatalogReasoningEffort = (typeof CLAWROUTER_REASONING_EFFORT_LEVELS)[number][0];
36+
2637
type CatalogRoute = {
2738
path: string;
2839
requestFormat: string;
@@ -43,6 +54,7 @@ type CatalogModel = {
4354
id: string;
4455
upstream: string;
4556
capabilities: string[];
57+
supportedReasoningEfforts?: CatalogReasoningEffort[];
4658
pricing?: CatalogPricing;
4759
};
4860

@@ -77,6 +89,19 @@ function readStringArray(value: unknown): string[] {
7789
: [];
7890
}
7991

92+
export function normalizeClawRouterReasoningEfforts(
93+
value: unknown,
94+
): CatalogReasoningEffort[] | undefined {
95+
if (!Array.isArray(value) || value.length > CLAWROUTER_REASONING_EFFORT_LEVELS.length) {
96+
return undefined;
97+
}
98+
const advertised = new Set(value);
99+
const efforts = CLAWROUTER_REASONING_EFFORT_LEVELS.filter(([effort]) =>
100+
advertised.has(effort),
101+
).map(([effort]) => effort);
102+
return efforts.length > 0 ? efforts : undefined;
103+
}
104+
80105
function readNonNegativeNumber(value: unknown): number | undefined {
81106
return typeof value === "number" && Number.isFinite(value) && value >= 0 ? value : undefined;
82107
}
@@ -134,6 +159,7 @@ function parseCatalogModel(value: unknown): CatalogModel | undefined {
134159
id,
135160
upstream,
136161
capabilities: readStringArray(row?.capabilities),
162+
supportedReasoningEfforts: normalizeClawRouterReasoningEfforts(row?.supportedReasoningEfforts),
137163
pricing: parseCatalogPricing(row?.pricing),
138164
};
139165
}
@@ -221,6 +247,17 @@ function modelCost(pricing: CatalogPricing | undefined): ModelDefinitionConfig["
221247
};
222248
}
223249

250+
function buildThinkingLevelMap(
251+
efforts: readonly CatalogReasoningEffort[],
252+
): NonNullable<ModelDefinitionConfig["thinkingLevelMap"]> {
253+
const supported = new Set(efforts);
254+
const levelMap: NonNullable<ModelDefinitionConfig["thinkingLevelMap"]> = {};
255+
for (const [effort, level] of CLAWROUTER_REASONING_EFFORT_LEVELS) {
256+
levelMap[level] = supported.has(effort) ? effort : null;
257+
}
258+
return levelMap;
259+
}
260+
224261
function buildRoutedModel(
225262
rootUrl: string,
226263
provider: CatalogProvider,
@@ -268,7 +305,17 @@ function buildRoutedModel(
268305
name: `${provider.displayName} · ${model.id}`,
269306
api,
270307
baseUrl,
271-
reasoning: inferReasoning(provider.id, model.id),
308+
reasoning:
309+
model.supportedReasoningEfforts !== undefined || inferReasoning(provider.id, model.id),
310+
...(model.supportedReasoningEfforts
311+
? {
312+
thinkingLevelMap: buildThinkingLevelMap(model.supportedReasoningEfforts),
313+
compat: {
314+
supportsReasoningEffort: true,
315+
supportedReasoningEfforts: model.supportedReasoningEfforts,
316+
},
317+
}
318+
: {}),
272319
input: inferInput(provider.id, model.id),
273320
cost: modelCost(model.pricing),
274321
contextWindow: model.pricing?.maxInputTokens ?? DEFAULT_CONTEXT_WINDOW,

0 commit comments

Comments
 (0)