fix(regression): normalize provider aliases in context window guard

This commit is contained in:
Tak Hoffman
2026-03-27 21:47:59 -05:00
parent 4ec51f2d5f
commit 01e3dd3508
2 changed files with 39 additions and 1 deletions

View File

@@ -85,6 +85,43 @@ describe("context-window-guard", () => {
expect(guard.shouldBlock).toBe(true);
});
it("normalizes provider aliases when reading models config context windows", () => {
const cfg = {
models: {
providers: {
"z.ai": {
baseUrl: "http://localhost",
apiKey: "x",
models: [
{
id: "glm-5",
name: "glm-5",
reasoning: false,
input: ["text"],
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
contextWindow: 12_000,
maxTokens: 256,
},
],
},
},
},
} satisfies OpenClawConfig;
const info = resolveContextWindowInfo({
cfg,
provider: "z-ai",
modelId: "glm-5",
modelContextWindow: 64_000,
defaultTokens: 200_000,
});
expect(info).toEqual({
source: "modelsConfig",
tokens: 12_000,
});
});
it("caps with agents.defaults.contextTokens", () => {
const cfg = {
agents: { defaults: { contextTokens: 20_000 } },

View File

@@ -1,4 +1,5 @@
import type { OpenClawConfig } from "../config/config.js";
import { findNormalizedProviderValue } from "./provider-id.js";
export const CONTEXT_WINDOW_HARD_MIN_TOKENS = 16_000;
export const CONTEXT_WINDOW_WARN_BELOW_TOKENS = 32_000;
@@ -29,7 +30,7 @@ export function resolveContextWindowInfo(params: {
const providers = params.cfg?.models?.providers as
| Record<string, { models?: Array<{ id?: string; contextWindow?: number }> }>
| undefined;
const providerEntry = providers?.[params.provider];
const providerEntry = findNormalizedProviderValue(providers, params.provider);
const models = Array.isArray(providerEntry?.models) ? providerEntry.models : [];
const match = models.find((m) => m?.id === params.modelId);
return normalizePositiveInt(match?.contextWindow);