mirror of https://github.com/openclaw/openclaw.git
refactor: share onboard provider merge helpers
This commit is contained in:
parent
7d69579634
commit
e08dc6f0af
|
|
@ -85,6 +85,29 @@ import {
|
||||||
MODELSTUDIO_DEFAULT_MODEL_REF,
|
MODELSTUDIO_DEFAULT_MODEL_REF,
|
||||||
} from "./onboard-auth.models.js";
|
} from "./onboard-auth.models.js";
|
||||||
|
|
||||||
|
function mergeProviderModels<T extends { id: string }>(
|
||||||
|
existingProvider: Record<string, unknown> | undefined,
|
||||||
|
defaultModels: T[],
|
||||||
|
): T[] {
|
||||||
|
const existingModels = Array.isArray(existingProvider?.models)
|
||||||
|
? (existingProvider.models as T[])
|
||||||
|
: [];
|
||||||
|
const mergedModels = [...existingModels];
|
||||||
|
const seen = new Set(existingModels.map((model) => model.id));
|
||||||
|
for (const model of defaultModels) {
|
||||||
|
if (!seen.has(model.id)) {
|
||||||
|
mergedModels.push(model);
|
||||||
|
seen.add(model.id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return mergedModels;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getNormalizedProviderApiKey(existingProvider: Record<string, unknown> | undefined) {
|
||||||
|
const { apiKey } = (existingProvider ?? {}) as { apiKey?: string };
|
||||||
|
return typeof apiKey === "string" ? apiKey.trim() || undefined : undefined;
|
||||||
|
}
|
||||||
|
|
||||||
export function applyZaiProviderConfig(
|
export function applyZaiProviderConfig(
|
||||||
cfg: OpenClawConfig,
|
cfg: OpenClawConfig,
|
||||||
params?: { endpoint?: string; modelId?: string },
|
params?: { endpoint?: string; modelId?: string },
|
||||||
|
|
@ -100,7 +123,6 @@ export function applyZaiProviderConfig(
|
||||||
|
|
||||||
const providers = { ...cfg.models?.providers };
|
const providers = { ...cfg.models?.providers };
|
||||||
const existingProvider = providers.zai;
|
const existingProvider = providers.zai;
|
||||||
const existingModels = Array.isArray(existingProvider?.models) ? existingProvider.models : [];
|
|
||||||
|
|
||||||
const defaultModels = [
|
const defaultModels = [
|
||||||
buildZaiModelDefinition({ id: "glm-5" }),
|
buildZaiModelDefinition({ id: "glm-5" }),
|
||||||
|
|
@ -109,21 +131,13 @@ export function applyZaiProviderConfig(
|
||||||
buildZaiModelDefinition({ id: "glm-4.7-flashx" }),
|
buildZaiModelDefinition({ id: "glm-4.7-flashx" }),
|
||||||
];
|
];
|
||||||
|
|
||||||
const mergedModels = [...existingModels];
|
const mergedModels = mergeProviderModels(existingProvider, defaultModels);
|
||||||
const seen = new Set(existingModels.map((m) => m.id));
|
|
||||||
for (const model of defaultModels) {
|
|
||||||
if (!seen.has(model.id)) {
|
|
||||||
mergedModels.push(model);
|
|
||||||
seen.add(model.id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const { apiKey: existingApiKey, ...existingProviderRest } = (existingProvider ?? {}) as Record<
|
const { apiKey: _existingApiKey, ...existingProviderRest } = (existingProvider ?? {}) as Record<
|
||||||
string,
|
string,
|
||||||
unknown
|
unknown
|
||||||
> as { apiKey?: string };
|
> as { apiKey?: string };
|
||||||
const resolvedApiKey = typeof existingApiKey === "string" ? existingApiKey : undefined;
|
const normalizedApiKey = getNormalizedProviderApiKey(existingProvider);
|
||||||
const normalizedApiKey = resolvedApiKey?.trim();
|
|
||||||
|
|
||||||
const baseUrl = params?.endpoint
|
const baseUrl = params?.endpoint
|
||||||
? resolveZaiBaseUrl(params.endpoint)
|
? resolveZaiBaseUrl(params.endpoint)
|
||||||
|
|
@ -256,12 +270,11 @@ export function applySyntheticProviderConfig(cfg: OpenClawConfig): OpenClawConfi
|
||||||
(model) => !existingModels.some((existing) => existing.id === model.id),
|
(model) => !existingModels.some((existing) => existing.id === model.id),
|
||||||
),
|
),
|
||||||
];
|
];
|
||||||
const { apiKey: existingApiKey, ...existingProviderRest } = (existingProvider ?? {}) as Record<
|
const { apiKey: _existingApiKey, ...existingProviderRest } = (existingProvider ?? {}) as Record<
|
||||||
string,
|
string,
|
||||||
unknown
|
unknown
|
||||||
> as { apiKey?: string };
|
> as { apiKey?: string };
|
||||||
const resolvedApiKey = typeof existingApiKey === "string" ? existingApiKey : undefined;
|
const normalizedApiKey = getNormalizedProviderApiKey(existingProvider);
|
||||||
const normalizedApiKey = resolvedApiKey?.trim();
|
|
||||||
providers.synthetic = {
|
providers.synthetic = {
|
||||||
...existingProviderRest,
|
...existingProviderRest,
|
||||||
baseUrl: SYNTHETIC_BASE_URL,
|
baseUrl: SYNTHETIC_BASE_URL,
|
||||||
|
|
@ -609,7 +622,6 @@ function applyModelStudioProviderConfigWithBaseUrl(
|
||||||
|
|
||||||
const providers = { ...cfg.models?.providers };
|
const providers = { ...cfg.models?.providers };
|
||||||
const existingProvider = providers.modelstudio;
|
const existingProvider = providers.modelstudio;
|
||||||
const existingModels = Array.isArray(existingProvider?.models) ? existingProvider.models : [];
|
|
||||||
|
|
||||||
const defaultModels = [
|
const defaultModels = [
|
||||||
buildModelStudioModelDefinition({ id: "qwen3.5-plus" }),
|
buildModelStudioModelDefinition({ id: "qwen3.5-plus" }),
|
||||||
|
|
@ -622,21 +634,13 @@ function applyModelStudioProviderConfigWithBaseUrl(
|
||||||
buildModelStudioModelDefinition({ id: "kimi-k2.5" }),
|
buildModelStudioModelDefinition({ id: "kimi-k2.5" }),
|
||||||
];
|
];
|
||||||
|
|
||||||
const mergedModels = [...existingModels];
|
const mergedModels = mergeProviderModels(existingProvider, defaultModels);
|
||||||
const seen = new Set(existingModels.map((m) => m.id));
|
|
||||||
for (const model of defaultModels) {
|
|
||||||
if (!seen.has(model.id)) {
|
|
||||||
mergedModels.push(model);
|
|
||||||
seen.add(model.id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const { apiKey: existingApiKey, ...existingProviderRest } = (existingProvider ?? {}) as Record<
|
const { apiKey: _existingApiKey, ...existingProviderRest } = (existingProvider ?? {}) as Record<
|
||||||
string,
|
string,
|
||||||
unknown
|
unknown
|
||||||
> as { apiKey?: string };
|
> as { apiKey?: string };
|
||||||
const resolvedApiKey = typeof existingApiKey === "string" ? existingApiKey : undefined;
|
const normalizedApiKey = getNormalizedProviderApiKey(existingProvider);
|
||||||
const normalizedApiKey = resolvedApiKey?.trim();
|
|
||||||
|
|
||||||
providers.modelstudio = {
|
providers.modelstudio = {
|
||||||
...existingProviderRest,
|
...existingProviderRest,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue