Files
openclaw/src/config/model-input.ts
T
zhang-guiping 7d5afcbb3f fix #84745: scope Google preview model normalization to Google providers only (#84762)
Summary:
- The branch scopes config-time Google Gemini preview model normalization to Google providers or nested `google/` proxy suffixes, adds model-picker regression coverage, and adds a changelog entry.
- Reproducibility: yes. by source inspection. Current main sends every provider suffix through the Google prev ... i-3-flash` deterministically becomes `litellm/gemini-3-flash-preview`; I did not run a live cron preflight.

Automerge notes:
- PR branch already contained follow-up commit before automerge: fix(config): scope Google preview model normalization to Google provi…
- PR branch already contained follow-up commit before automerge: fix #84745: scope Google preview model normalization to Google provid…
- PR branch already contained follow-up commit before automerge: fix #84745: preserve proxy Google model normalization

Validation:
- ClawSweeper review passed for head c59163c809.
- Required merge gates passed before the squash merge.

Prepared head SHA: c59163c809
Review: https://github.com/openclaw/openclaw/pull/84762#issuecomment-4504169062

Co-authored-by: zhang-guiping <zhang.guiping@xydigit.com>
Co-authored-by: clawsweeper <274271284+clawsweeper[bot]@users.noreply.github.com>
Co-authored-by: 张贵萍0668001030 <zhang.guiping@xydigit.com>
Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
Co-authored-by: clawsweeper[bot] <274271284+clawsweeper[bot]@users.noreply.github.com>
2026-05-21 17:45:57 +00:00

116 lines
3.7 KiB
TypeScript

import { normalizeProviderId } from "../agents/provider-id.js";
import { normalizeGooglePreviewModelId } from "../plugin-sdk/provider-model-id-normalize.js";
import {
normalizeLowercaseStringOrEmpty,
normalizeOptionalString,
resolvePrimaryStringValue,
} from "../shared/string-coerce.js";
import type { AgentModelConfig, AgentToolModelConfig } from "./types.agents-shared.js";
type AgentModelListLike = {
primary?: string;
fallbacks?: string[];
};
function isPlainRecord(value: unknown): value is Record<string, unknown> {
return Boolean(value && typeof value === "object" && !Array.isArray(value));
}
function modelKeyForConfig(provider: string, model: string): string {
const providerId = provider.trim();
const modelId = model.trim();
if (!providerId) {
return modelId;
}
if (!modelId) {
return providerId;
}
return normalizeLowercaseStringOrEmpty(modelId).startsWith(
`${normalizeLowercaseStringOrEmpty(providerId)}/`,
)
? modelId
: `${providerId}/${modelId}`;
}
type AgentModelInput = AgentModelConfig | AgentToolModelConfig;
export function resolveAgentModelPrimaryValue(model?: AgentModelInput): string | undefined {
return resolvePrimaryStringValue(model);
}
export function resolveAgentModelFallbackValues(model?: AgentModelInput): string[] {
if (!model || typeof model !== "object") {
return [];
}
return Array.isArray(model.fallbacks) ? model.fallbacks : [];
}
export function resolveAgentModelTimeoutMsValue(model?: AgentToolModelConfig): number | undefined {
if (!model || typeof model !== "object") {
return undefined;
}
return typeof model.timeoutMs === "number" &&
Number.isFinite(model.timeoutMs) &&
model.timeoutMs > 0
? Math.floor(model.timeoutMs)
: undefined;
}
export function toAgentModelListLike(model?: AgentModelConfig): AgentModelListLike | undefined {
if (typeof model === "string") {
const primary = normalizeOptionalString(model);
return primary ? { primary } : undefined;
}
if (!model || typeof model !== "object") {
return undefined;
}
return model;
}
const GOOGLE_PROVIDER_IDS = new Set(["google", "google-gemini-cli", "google-vertex"]);
export function normalizeAgentModelRefForConfig(model: string): string {
const trimmed = model.trim();
const slash = trimmed.indexOf("/");
if (slash <= 0 || slash >= trimmed.length - 1) {
return trimmed;
}
const provider = normalizeProviderId(trimmed.slice(0, slash));
const modelSuffix = trimmed.slice(slash + 1);
const normalizedModel =
GOOGLE_PROVIDER_IDS.has(provider) || modelSuffix.startsWith("google/")
? normalizeGooglePreviewModelId(modelSuffix)
: modelSuffix;
return modelKeyForConfig(provider, normalizedModel);
}
function mergeAgentModelEntryForConfig(existing: unknown, incoming: unknown): unknown {
if (!isPlainRecord(existing) || !isPlainRecord(incoming)) {
return incoming;
}
const existingParams = isPlainRecord(existing.params) ? existing.params : undefined;
const incomingParams = isPlainRecord(incoming.params) ? incoming.params : undefined;
return {
...existing,
...incoming,
...(existingParams || incomingParams
? { params: { ...existingParams, ...incomingParams } }
: undefined),
};
}
export function normalizeAgentModelMapForConfig<T extends Record<string, unknown>>(models: T): T {
let mutated = false;
const next: Record<string, unknown> = {};
for (const [key, entry] of Object.entries(models)) {
const normalizedKey = normalizeAgentModelRefForConfig(key);
if (normalizedKey !== key || Object.prototype.hasOwnProperty.call(next, normalizedKey)) {
mutated = true;
}
next[normalizedKey] = mergeAgentModelEntryForConfig(next[normalizedKey], entry);
}
return (mutated ? next : models) as T;
}