refactor(commands): share provider catalog config helper

This commit is contained in:
Peter Steinberger
2026-02-15 12:54:09 +00:00
parent 108ea4336b
commit fcd2eca9c7
2 changed files with 76 additions and 104 deletions

View File

@@ -52,6 +52,7 @@ import {
applyAgentDefaultModelPrimary, applyAgentDefaultModelPrimary,
applyProviderConfigWithDefaultModel, applyProviderConfigWithDefaultModel,
applyProviderConfigWithDefaultModels, applyProviderConfigWithDefaultModels,
applyProviderConfigWithModelCatalog,
} from "./onboard-auth.config-shared.js"; } from "./onboard-auth.config-shared.js";
import { import {
buildZaiModelDefinition, buildZaiModelDefinition,
@@ -409,42 +410,14 @@ export function applyVeniceProviderConfig(cfg: OpenClawConfig): OpenClawConfig {
alias: models[VENICE_DEFAULT_MODEL_REF]?.alias ?? "Llama 3.3 70B", alias: models[VENICE_DEFAULT_MODEL_REF]?.alias ?? "Llama 3.3 70B",
}; };
const providers = { ...cfg.models?.providers };
const existingProvider = providers.venice;
const existingModels = Array.isArray(existingProvider?.models) ? existingProvider.models : [];
const veniceModels = VENICE_MODEL_CATALOG.map(buildVeniceModelDefinition); const veniceModels = VENICE_MODEL_CATALOG.map(buildVeniceModelDefinition);
const mergedModels = [ return applyProviderConfigWithModelCatalog(cfg, {
...existingModels, agentModels: models,
...veniceModels.filter((model) => !existingModels.some((existing) => existing.id === model.id)), providerId: "venice",
];
const { apiKey: existingApiKey, ...existingProviderRest } = (existingProvider ?? {}) as Record<
string,
unknown
> as { apiKey?: string };
const resolvedApiKey = typeof existingApiKey === "string" ? existingApiKey : undefined;
const normalizedApiKey = resolvedApiKey?.trim();
providers.venice = {
...existingProviderRest,
baseUrl: VENICE_BASE_URL,
api: "openai-completions", api: "openai-completions",
...(normalizedApiKey ? { apiKey: normalizedApiKey } : {}), baseUrl: VENICE_BASE_URL,
models: mergedModels.length > 0 ? mergedModels : veniceModels, catalogModels: veniceModels,
}; });
return {
...cfg,
agents: {
...cfg.agents,
defaults: {
...cfg.agents?.defaults,
models,
},
},
models: {
mode: cfg.models?.mode ?? "merge",
providers,
},
};
} }
/** /**
@@ -484,44 +457,14 @@ export function applyTogetherProviderConfig(cfg: OpenClawConfig): OpenClawConfig
alias: models[TOGETHER_DEFAULT_MODEL_REF]?.alias ?? "Together AI", alias: models[TOGETHER_DEFAULT_MODEL_REF]?.alias ?? "Together AI",
}; };
const providers = { ...cfg.models?.providers };
const existingProvider = providers.together;
const existingModels = Array.isArray(existingProvider?.models) ? existingProvider.models : [];
const togetherModels = TOGETHER_MODEL_CATALOG.map(buildTogetherModelDefinition); const togetherModels = TOGETHER_MODEL_CATALOG.map(buildTogetherModelDefinition);
const mergedModels = [ return applyProviderConfigWithModelCatalog(cfg, {
...existingModels, agentModels: models,
...togetherModels.filter( providerId: "together",
(model) => !existingModels.some((existing) => existing.id === model.id),
),
];
const { apiKey: existingApiKey, ...existingProviderRest } = (existingProvider ?? {}) as Record<
string,
unknown
> as { apiKey?: string };
const resolvedApiKey = typeof existingApiKey === "string" ? existingApiKey : undefined;
const normalizedApiKey = resolvedApiKey?.trim();
providers.together = {
...existingProviderRest,
baseUrl: TOGETHER_BASE_URL,
api: "openai-completions", api: "openai-completions",
...(normalizedApiKey ? { apiKey: normalizedApiKey } : {}), baseUrl: TOGETHER_BASE_URL,
models: mergedModels.length > 0 ? mergedModels : togetherModels, catalogModels: togetherModels,
}; });
return {
...cfg,
agents: {
...cfg.agents,
defaults: {
...cfg.agents?.defaults,
models,
},
},
models: {
mode: cfg.models?.mode ?? "merge",
providers,
},
};
} }
/** /**
@@ -560,42 +503,14 @@ export function applyHuggingfaceProviderConfig(cfg: OpenClawConfig): OpenClawCon
alias: models[HUGGINGFACE_DEFAULT_MODEL_REF]?.alias ?? "Hugging Face", alias: models[HUGGINGFACE_DEFAULT_MODEL_REF]?.alias ?? "Hugging Face",
}; };
const providers = { ...cfg.models?.providers };
const existingProvider = providers.huggingface;
const existingModels = Array.isArray(existingProvider?.models) ? existingProvider.models : [];
const hfModels = HUGGINGFACE_MODEL_CATALOG.map(buildHuggingfaceModelDefinition); const hfModels = HUGGINGFACE_MODEL_CATALOG.map(buildHuggingfaceModelDefinition);
const mergedModels = [ return applyProviderConfigWithModelCatalog(cfg, {
...existingModels, agentModels: models,
...hfModels.filter((model) => !existingModels.some((existing) => existing.id === model.id)), providerId: "huggingface",
];
const { apiKey: existingApiKey, ...existingProviderRest } = (existingProvider ?? {}) as Record<
string,
unknown
> as { apiKey?: string };
const resolvedApiKey = typeof existingApiKey === "string" ? existingApiKey : undefined;
const normalizedApiKey = resolvedApiKey?.trim();
providers.huggingface = {
...existingProviderRest,
baseUrl: HUGGINGFACE_BASE_URL,
api: "openai-completions", api: "openai-completions",
...(normalizedApiKey ? { apiKey: normalizedApiKey } : {}), baseUrl: HUGGINGFACE_BASE_URL,
models: mergedModels.length > 0 ? mergedModels : hfModels, catalogModels: hfModels,
}; });
return {
...cfg,
agents: {
...cfg.agents,
defaults: {
...cfg.agents?.defaults,
models,
},
},
models: {
mode: cfg.models?.mode ?? "merge",
providers,
},
};
} }
/** /**

View File

@@ -117,3 +117,60 @@ export function applyProviderConfigWithDefaultModel(
defaultModelId: params.defaultModelId ?? params.defaultModel.id, defaultModelId: params.defaultModelId ?? params.defaultModel.id,
}); });
} }
export function applyProviderConfigWithModelCatalog(
cfg: OpenClawConfig,
params: {
agentModels: Record<string, AgentModelEntryConfig>;
providerId: string;
api: ModelApi;
baseUrl: string;
catalogModels: ModelDefinitionConfig[];
},
): OpenClawConfig {
const providers = { ...cfg.models?.providers } as Record<string, ModelProviderConfig>;
const existingProvider = providers[params.providerId] as ModelProviderConfig | undefined;
const existingModels: ModelDefinitionConfig[] = Array.isArray(existingProvider?.models)
? existingProvider.models
: [];
const catalogModels = params.catalogModels;
const mergedModels =
existingModels.length > 0
? [
...existingModels,
...catalogModels.filter(
(model) => !existingModels.some((existing) => existing.id === model.id),
),
]
: catalogModels;
const { apiKey: existingApiKey, ...existingProviderRest } = (existingProvider ?? {}) as {
apiKey?: string;
};
const normalizedApiKey = typeof existingApiKey === "string" ? existingApiKey.trim() : undefined;
providers[params.providerId] = {
...existingProviderRest,
baseUrl: params.baseUrl,
api: params.api,
...(normalizedApiKey ? { apiKey: normalizedApiKey } : {}),
models: mergedModels.length > 0 ? mergedModels : catalogModels,
};
return {
...cfg,
agents: {
...cfg.agents,
defaults: {
...cfg.agents?.defaults,
models: params.agentModels,
},
},
models: {
mode: cfg.models?.mode ?? "merge",
providers,
},
};
}