Skip to content

Commit 39db00f

Browse files
committed
fix: validate vercel gateway model token metadata
1 parent 423531d commit 39db00f

2 files changed

Lines changed: 52 additions & 6 deletions

File tree

‎extensions/vercel-ai-gateway/models.ts‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { readProviderJsonArrayFieldResponse } from "openclaw/plugin-sdk/provider
33
import type { ModelDefinitionConfig } from "openclaw/plugin-sdk/provider-model-shared";
44
import { createSubsystemLogger } from "openclaw/plugin-sdk/runtime-env";
55
import { fetchWithSsrFGuard } from "openclaw/plugin-sdk/ssrf-runtime";
6+
import { asPositiveSafeInteger } from "openclaw/plugin-sdk/string-coerce-runtime";
67

78
export const VERCEL_AI_GATEWAY_PROVIDER_ID = "vercel-ai-gateway";
89
export const VERCEL_AI_GATEWAY_BASE_URL = "https://ai-gateway.vercel.sh";
@@ -151,13 +152,13 @@ function buildDiscoveredModelDefinition(
151152

152153
const fallback = getStaticFallbackModel(id);
153154
const contextWindow =
154-
typeof model.context_window === "number" && Number.isFinite(model.context_window)
155-
? model.context_window
156-
: (fallback?.contextWindow ?? VERCEL_AI_GATEWAY_DEFAULT_CONTEXT_WINDOW);
155+
asPositiveSafeInteger(model.context_window) ??
156+
fallback?.contextWindow ??
157+
VERCEL_AI_GATEWAY_DEFAULT_CONTEXT_WINDOW;
157158
const maxTokens =
158-
typeof model.max_tokens === "number" && Number.isFinite(model.max_tokens)
159-
? model.max_tokens
160-
: (fallback?.maxTokens ?? VERCEL_AI_GATEWAY_DEFAULT_MAX_TOKENS);
159+
asPositiveSafeInteger(model.max_tokens) ??
160+
fallback?.maxTokens ??
161+
VERCEL_AI_GATEWAY_DEFAULT_MAX_TOKENS;
161162
const normalizedCost = normalizeCost(model.pricing);
162163

163164
return {

‎extensions/vercel-ai-gateway/provider-catalog.test.ts‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import {
1212
discoverVercelAiGatewayModels,
1313
getStaticVercelAiGatewayModelCatalog,
1414
VERCEL_AI_GATEWAY_BASE_URL,
15+
VERCEL_AI_GATEWAY_DEFAULT_CONTEXT_WINDOW,
16+
VERCEL_AI_GATEWAY_DEFAULT_MAX_TOKENS,
1517
} from "./api.js";
1618
import {
1719
buildStaticVercelAiGatewayProvider,
@@ -93,4 +95,47 @@ describe("vercel ai gateway provider catalog", () => {
9395
});
9496
}
9597
});
98+
99+
it("falls back from malformed live token metadata", async () => {
100+
fetchWithSsrFGuardMock.mockResolvedValueOnce({
101+
response: {
102+
ok: true,
103+
status: 200,
104+
json: async () => ({
105+
data: [
106+
{
107+
id: "anthropic/claude-opus-4.6",
108+
name: "Claude Opus 4.6",
109+
context_window: -1,
110+
max_tokens: 128_000.5,
111+
tags: ["vision", "reasoning"],
112+
},
113+
{
114+
id: "custom/provider-model",
115+
name: "Custom model",
116+
context_window: Number.POSITIVE_INFINITY,
117+
max_tokens: 0,
118+
tags: ["reasoning"],
119+
},
120+
],
121+
}),
122+
},
123+
release: async () => {},
124+
});
125+
126+
await withLiveDiscovery(async () => {
127+
const models = await discoverVercelAiGatewayModels();
128+
129+
expect(models[0]).toMatchObject({
130+
id: "anthropic/claude-opus-4.6",
131+
contextWindow: 1_000_000,
132+
maxTokens: 128_000,
133+
});
134+
expect(models[1]).toMatchObject({
135+
id: "custom/provider-model",
136+
contextWindow: VERCEL_AI_GATEWAY_DEFAULT_CONTEXT_WINDOW,
137+
maxTokens: VERCEL_AI_GATEWAY_DEFAULT_MAX_TOKENS,
138+
});
139+
});
140+
});
96141
});

0 commit comments

Comments
 (0)