Skip to content

Commit d3c9ac5

Browse files
mushuiyu886vincentkoc
authored andcommitted
fix(models): report primary OpenRouter limits in scans
1 parent a67c526 commit d3c9ac5

2 files changed

Lines changed: 112 additions & 10 deletions

File tree

‎src/agents/model-scan.test.ts‎

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,115 @@ describe("scanOpenRouterModels", () => {
7474
if (byPricing === undefined) {
7575
throw new Error("Expected pricing-based model result.");
7676
}
77+
expect(byPricing.contextLength).toBe(16_384);
78+
expect(byPricing.maxCompletionTokens).toBe(1024);
7779
expect(byPricing.supportsToolsMeta).toBe(true);
7880
expect(byPricing.supportedParametersCount).toBe(3);
7981
expect(byPricing.isFree).toBe(true);
8082
expect(byPricing.tool.skipped).toBe(true);
8183
expect(byPricing.image.skipped).toBe(true);
8284
});
8385

86+
it("uses OpenRouter top-provider limits for scan metadata", async () => {
87+
const fetchImpl = createFetchFixture({
88+
data: [
89+
{
90+
id: "acme/provider-limited:free",
91+
name: "Provider Limited",
92+
context_length: 32_768,
93+
top_provider: {
94+
context_length: 16_384,
95+
max_completion_tokens: 4096,
96+
},
97+
supported_parameters: [],
98+
pricing: { prompt: "0", completion: "0" },
99+
},
100+
],
101+
});
102+
103+
const [result] = await scanOpenRouterModels({
104+
fetchImpl,
105+
probe: false,
106+
});
107+
108+
expect(result?.contextLength).toBe(16_384);
109+
expect(result?.maxCompletionTokens).toBe(4096);
110+
});
111+
112+
it("falls back when top-provider limits are malformed", async () => {
113+
for (const topProvider of [
114+
{ context_length: 0, max_completion_tokens: 0 },
115+
{ context_length: -1, max_completion_tokens: -1 },
116+
{ context_length: 8192.5, max_completion_tokens: 8192.5 },
117+
{
118+
context_length: Number.MAX_SAFE_INTEGER + 1,
119+
max_completion_tokens: Number.MAX_SAFE_INTEGER + 1,
120+
},
121+
{ context_length: "8192", max_completion_tokens: "1024" },
122+
{ context_length: null, max_completion_tokens: null },
123+
]) {
124+
const fetchImpl = createFetchFixture({
125+
data: [
126+
{
127+
id: "acme/provider-limited:free",
128+
name: "Provider Limited",
129+
context_length: 32_768,
130+
max_completion_tokens: 4096,
131+
top_provider: topProvider,
132+
supported_parameters: [],
133+
pricing: { prompt: "0", completion: "0" },
134+
},
135+
],
136+
});
137+
138+
const [result] = await scanOpenRouterModels({ fetchImpl, probe: false });
139+
140+
expect(result?.contextLength).toBe(32_768);
141+
expect(result?.maxCompletionTokens).toBe(4096);
142+
}
143+
});
144+
145+
it("mixes provider context length with a top-level completion cap", async () => {
146+
const fetchImpl = createFetchFixture({
147+
data: [
148+
{
149+
id: "acme/provider-limited:free",
150+
name: "Provider Limited",
151+
context_length: 32_768,
152+
max_completion_tokens: 4096,
153+
top_provider: { context_length: 16_384, max_completion_tokens: 0 },
154+
supported_parameters: [],
155+
pricing: { prompt: "0", completion: "0" },
156+
},
157+
],
158+
});
159+
160+
const [result] = await scanOpenRouterModels({ fetchImpl, probe: false });
161+
162+
expect(result?.contextLength).toBe(16_384);
163+
expect(result?.maxCompletionTokens).toBe(4096);
164+
});
165+
166+
it("falls back to top-level max_output_tokens", async () => {
167+
const fetchImpl = createFetchFixture({
168+
data: [
169+
{
170+
id: "acme/output-limited:free",
171+
name: "Output Limited",
172+
context_length: 32_768,
173+
max_output_tokens: 2048,
174+
top_provider: { max_completion_tokens: 0 },
175+
supported_parameters: [],
176+
pricing: { prompt: "0", completion: "0" },
177+
},
178+
],
179+
});
180+
181+
const [result] = await scanOpenRouterModels({ fetchImpl, probe: false });
182+
183+
expect(result?.maxCompletionTokens).toBe(2048);
184+
});
185+
84186
it("drops out-of-range OpenRouter created_at timestamps", async () => {
85187
const fetchImpl = createFetchFixture({
86188
data: [

‎src/agents/model-scan.ts‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ import { registerBuiltInApiProviders } from "@openclaw/ai/providers";
55
import { normalizeProviderId } from "@openclaw/model-catalog-core/provider-id";
66
import {
77
asDateTimestampMs,
8+
asPositiveSafeInteger,
89
resolveTimerTimeoutMs,
910
} from "@openclaw/normalization-core/number-coercion";
11+
import { asOptionalRecord } from "@openclaw/normalization-core/record-coerce";
1012
import {
1113
normalizeLowercaseStringOrEmpty,
1214
normalizeOptionalString,
@@ -233,20 +235,18 @@ async function fetchOpenRouterModels(
233235
return null;
234236
}
235237
const name = typeof obj.name === "string" && obj.name.trim() ? obj.name.trim() : id;
238+
const topProvider = asOptionalRecord(obj.top_provider);
236239

237240
const contextLength =
238-
typeof obj.context_length === "number" && Number.isFinite(obj.context_length)
239-
? obj.context_length
240-
: null;
241+
asPositiveSafeInteger(topProvider?.context_length) ??
242+
asPositiveSafeInteger(obj.context_length) ??
243+
null;
241244

242245
const maxCompletionTokens =
243-
typeof obj.max_completion_tokens === "number" &&
244-
Number.isFinite(obj.max_completion_tokens)
245-
? obj.max_completion_tokens
246-
: typeof obj.max_output_tokens === "number" &&
247-
Number.isFinite(obj.max_output_tokens)
248-
? obj.max_output_tokens
249-
: null;
246+
asPositiveSafeInteger(topProvider?.max_completion_tokens) ??
247+
asPositiveSafeInteger(obj.max_completion_tokens) ??
248+
asPositiveSafeInteger(obj.max_output_tokens) ??
249+
null;
250250

251251
const supportedParameters = Array.isArray(obj.supported_parameters)
252252
? normalizeStringEntries(

0 commit comments

Comments
 (0)