refactor(models): share auth helpers and forward-compat list fallbacks

This commit is contained in:
Peter Steinberger
2026-02-14 01:07:19 +01:00
parent 363a56ab87
commit cf2524b8b9
8 changed files with 175 additions and 233 deletions

View File

@@ -10,7 +10,10 @@ import {
resolveAwsSdkEnvVarName,
resolveEnvApiKey,
} from "../../agents/model-auth.js";
import { resolveForwardCompatModel } from "../../agents/model-forward-compat.js";
import {
ANTIGRAVITY_OPUS_46_FORWARD_COMPAT_CANDIDATES,
resolveForwardCompatModel,
} from "../../agents/model-forward-compat.js";
import { ensureOpenClawModelsJson } from "../../agents/models-config.js";
import { discoverAuthStorage, discoverModels } from "../../agents/pi-model-discovery.js";
import {
@@ -18,23 +21,7 @@ import {
MODEL_AVAILABILITY_UNAVAILABLE_CODE,
shouldFallbackToAuthHeuristics,
} from "./list.errors.js";
import { modelKey } from "./shared.js";
const isLocalBaseUrl = (baseUrl: string) => {
try {
const url = new URL(baseUrl);
const host = url.hostname.toLowerCase();
return (
host === "localhost" ||
host === "127.0.0.1" ||
host === "0.0.0.0" ||
host === "::1" ||
host.endsWith(".local")
);
} catch {
return false;
}
};
import { isLocalBaseUrl, modelKey } from "./shared.js";
const hasAuthForProvider = (
provider: string,
@@ -147,34 +134,17 @@ export async function loadModelRegistry(cfg: OpenClawConfig) {
type SynthesizedForwardCompat = {
key: string;
templatePrefixes: string[];
templatePrefixes: readonly string[];
};
function appendAntigravityForwardCompatModels(
models: Model<Api>[],
modelRegistry: ModelRegistry,
): { models: Model<Api>[]; synthesizedForwardCompat: SynthesizedForwardCompat[] } {
const candidates = [
{
id: "claude-opus-4-6-thinking",
templatePrefixes: [
"google-antigravity/claude-opus-4-5-thinking",
"google-antigravity/claude-opus-4.5-thinking",
],
},
{
id: "claude-opus-4-6",
templatePrefixes: [
"google-antigravity/claude-opus-4-5",
"google-antigravity/claude-opus-4.5",
],
},
];
const nextModels = [...models];
const synthesizedForwardCompat: SynthesizedForwardCompat[] = [];
for (const candidate of candidates) {
for (const candidate of ANTIGRAVITY_OPUS_46_FORWARD_COMPAT_CANDIDATES) {
const key = modelKey("google-antigravity", candidate.id);
const hasForwardCompat = nextModels.some((model) => modelKey(model.provider, model.id) === key);
if (hasForwardCompat) {
@@ -196,7 +166,10 @@ function appendAntigravityForwardCompatModels(
return { models: nextModels, synthesizedForwardCompat };
}
function hasAvailableTemplate(availableKeys: Set<string>, templatePrefixes: string[]): boolean {
function hasAvailableTemplate(
availableKeys: Set<string>,
templatePrefixes: readonly string[],
): boolean {
for (const key of availableKeys) {
if (templatePrefixes.some((prefix) => key.startsWith(prefix))) {
return true;