mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-21 22:21:33 +00:00
refactor(deadcode): remove duplicate barrels and helper shims
This commit is contained in:
@@ -1,195 +0,0 @@
|
||||
import { expect, it } from "vitest";
|
||||
import type { OpenClawConfig } from "../../config/config.js";
|
||||
import type { ProviderPlugin, WebFetchProviderPlugin, WebSearchProviderPlugin } from "../types.js";
|
||||
|
||||
type Lazy<T> = T | (() => T);
|
||||
|
||||
function resolveLazy<T>(value: Lazy<T>): T {
|
||||
return typeof value === "function" ? (value as () => T)() : value;
|
||||
}
|
||||
|
||||
export function installProviderPluginContractSuite(params: { provider: Lazy<ProviderPlugin> }) {
|
||||
it("satisfies the base provider plugin contract", () => {
|
||||
const provider = resolveLazy(params.provider);
|
||||
const authIds = provider.auth.map((method) => method.id);
|
||||
const wizardChoiceIds = new Set<string>();
|
||||
|
||||
expect(provider.id).toMatch(/^[a-z0-9][a-z0-9-]*$/);
|
||||
expect(provider.label.trim()).not.toBe("");
|
||||
|
||||
if (provider.docsPath) {
|
||||
expect(provider.docsPath.startsWith("/")).toBe(true);
|
||||
}
|
||||
if (provider.aliases) {
|
||||
expect(provider.aliases).toEqual([...new Set(provider.aliases)]);
|
||||
}
|
||||
if (provider.envVars) {
|
||||
expect(provider.envVars).toEqual([...new Set(provider.envVars)]);
|
||||
expect(provider.envVars.every((entry) => entry.trim().length > 0)).toBe(true);
|
||||
}
|
||||
|
||||
expect(Array.isArray(provider.auth)).toBe(true);
|
||||
expect(authIds).toEqual([...new Set(authIds)]);
|
||||
for (const method of provider.auth) {
|
||||
expect(method.id.trim()).not.toBe("");
|
||||
expect(method.label.trim()).not.toBe("");
|
||||
if (method.hint !== undefined) {
|
||||
expect(method.hint.trim()).not.toBe("");
|
||||
}
|
||||
if (method.wizard) {
|
||||
if (method.wizard.choiceId) {
|
||||
expect(method.wizard.choiceId.trim()).not.toBe("");
|
||||
expect(wizardChoiceIds.has(method.wizard.choiceId)).toBe(false);
|
||||
wizardChoiceIds.add(method.wizard.choiceId);
|
||||
}
|
||||
if (method.wizard.methodId) {
|
||||
expect(authIds).toContain(method.wizard.methodId);
|
||||
}
|
||||
if (method.wizard.modelAllowlist?.allowedKeys) {
|
||||
expect(method.wizard.modelAllowlist.allowedKeys).toEqual([
|
||||
...new Set(method.wizard.modelAllowlist.allowedKeys),
|
||||
]);
|
||||
}
|
||||
if (method.wizard.modelAllowlist?.initialSelections) {
|
||||
expect(method.wizard.modelAllowlist.initialSelections).toEqual([
|
||||
...new Set(method.wizard.modelAllowlist.initialSelections),
|
||||
]);
|
||||
}
|
||||
}
|
||||
expect(typeof method.run).toBe("function");
|
||||
}
|
||||
|
||||
if (provider.wizard?.setup || provider.wizard?.modelPicker) {
|
||||
expect(provider.auth.length).toBeGreaterThan(0);
|
||||
}
|
||||
if (provider.wizard?.setup) {
|
||||
if (provider.wizard.setup.choiceId) {
|
||||
expect(provider.wizard.setup.choiceId.trim()).not.toBe("");
|
||||
expect(wizardChoiceIds.has(provider.wizard.setup.choiceId)).toBe(false);
|
||||
}
|
||||
if (provider.wizard.setup.methodId) {
|
||||
expect(authIds).toContain(provider.wizard.setup.methodId);
|
||||
}
|
||||
if (provider.wizard.setup.modelAllowlist?.allowedKeys) {
|
||||
expect(provider.wizard.setup.modelAllowlist.allowedKeys).toEqual([
|
||||
...new Set(provider.wizard.setup.modelAllowlist.allowedKeys),
|
||||
]);
|
||||
}
|
||||
if (provider.wizard.setup.modelAllowlist?.initialSelections) {
|
||||
expect(provider.wizard.setup.modelAllowlist.initialSelections).toEqual([
|
||||
...new Set(provider.wizard.setup.modelAllowlist.initialSelections),
|
||||
]);
|
||||
}
|
||||
}
|
||||
if (provider.wizard?.modelPicker?.methodId) {
|
||||
expect(authIds).toContain(provider.wizard.modelPicker.methodId);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export function installWebSearchProviderContractSuite(params: {
|
||||
provider: Lazy<WebSearchProviderPlugin>;
|
||||
credentialValue: Lazy<unknown>;
|
||||
}) {
|
||||
it("satisfies the base web search provider contract", () => {
|
||||
const provider = resolveLazy(params.provider);
|
||||
const credentialValue = resolveLazy(params.credentialValue);
|
||||
|
||||
expect(provider.id).toMatch(/^[a-z0-9][a-z0-9-]*$/);
|
||||
expect(provider.label.trim()).not.toBe("");
|
||||
expect(provider.hint.trim()).not.toBe("");
|
||||
expect(provider.placeholder.trim()).not.toBe("");
|
||||
expect(provider.signupUrl.startsWith("https://")).toBe(true);
|
||||
if (provider.docsUrl) {
|
||||
expect(provider.docsUrl.startsWith("http")).toBe(true);
|
||||
}
|
||||
|
||||
expect(provider.envVars).toEqual([...new Set(provider.envVars)]);
|
||||
expect(provider.envVars.every((entry) => entry.trim().length > 0)).toBe(true);
|
||||
|
||||
const searchConfigTarget: Record<string, unknown> = {};
|
||||
provider.setCredentialValue(searchConfigTarget, credentialValue);
|
||||
expect(provider.getCredentialValue(searchConfigTarget)).toEqual(credentialValue);
|
||||
|
||||
const config = {
|
||||
tools: {
|
||||
web: {
|
||||
search: {
|
||||
provider: provider.id,
|
||||
...searchConfigTarget,
|
||||
},
|
||||
},
|
||||
},
|
||||
} as OpenClawConfig;
|
||||
const tool = provider.createTool({ config, searchConfig: searchConfigTarget });
|
||||
|
||||
expect(tool).not.toBeNull();
|
||||
expect(tool?.description.trim()).not.toBe("");
|
||||
expect(tool?.parameters).toEqual(expect.any(Object));
|
||||
expect(typeof tool?.execute).toBe("function");
|
||||
if (provider.runSetup) {
|
||||
expect(typeof provider.runSetup).toBe("function");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export function installWebFetchProviderContractSuite(params: {
|
||||
provider: Lazy<WebFetchProviderPlugin>;
|
||||
credentialValue: Lazy<unknown>;
|
||||
pluginId?: string;
|
||||
}) {
|
||||
it("satisfies the base web fetch provider contract", () => {
|
||||
const provider = resolveLazy(params.provider);
|
||||
const credentialValue = resolveLazy(params.credentialValue);
|
||||
|
||||
expect(provider.id).toMatch(/^[a-z0-9][a-z0-9-]*$/);
|
||||
expect(provider.label.trim()).not.toBe("");
|
||||
expect(provider.hint.trim()).not.toBe("");
|
||||
expect(provider.placeholder.trim()).not.toBe("");
|
||||
expect(provider.signupUrl.startsWith("https://")).toBe(true);
|
||||
if (provider.docsUrl) {
|
||||
expect(provider.docsUrl.startsWith("http")).toBe(true);
|
||||
}
|
||||
|
||||
expect(provider.envVars).toEqual([...new Set(provider.envVars)]);
|
||||
expect(provider.envVars.every((entry) => entry.trim().length > 0)).toBe(true);
|
||||
expect(provider.credentialPath.trim()).not.toBe("");
|
||||
if (provider.inactiveSecretPaths) {
|
||||
expect(provider.inactiveSecretPaths).toEqual([...new Set(provider.inactiveSecretPaths)]);
|
||||
// Runtime inactive-path classification uses inactiveSecretPaths as the complete list.
|
||||
expect(provider.inactiveSecretPaths).toContain(provider.credentialPath);
|
||||
}
|
||||
|
||||
const fetchConfigTarget: Record<string, unknown> = {};
|
||||
provider.setCredentialValue(fetchConfigTarget, credentialValue);
|
||||
expect(provider.getCredentialValue(fetchConfigTarget)).toEqual(credentialValue);
|
||||
|
||||
if (provider.setConfiguredCredentialValue && provider.getConfiguredCredentialValue) {
|
||||
const configTarget = {} as OpenClawConfig;
|
||||
provider.setConfiguredCredentialValue(configTarget, credentialValue);
|
||||
expect(provider.getConfiguredCredentialValue(configTarget)).toEqual(credentialValue);
|
||||
}
|
||||
|
||||
if (provider.applySelectionConfig && params.pluginId) {
|
||||
const applied = provider.applySelectionConfig({} as OpenClawConfig);
|
||||
expect(applied.plugins?.entries?.[params.pluginId]?.enabled).toBe(true);
|
||||
}
|
||||
|
||||
const config = {
|
||||
tools: {
|
||||
web: {
|
||||
fetch: {
|
||||
provider: provider.id,
|
||||
...fetchConfigTarget,
|
||||
},
|
||||
},
|
||||
},
|
||||
} as OpenClawConfig;
|
||||
const tool = provider.createTool({ config, fetchConfig: fetchConfigTarget });
|
||||
|
||||
expect(tool).not.toBeNull();
|
||||
expect(tool?.description.trim()).not.toBe("");
|
||||
expect(tool?.parameters).toEqual(expect.any(Object));
|
||||
expect(typeof tool?.execute).toBe("function");
|
||||
});
|
||||
}
|
||||
@@ -1,174 +0,0 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import type { OpenClawConfig } from "../config/config.js";
|
||||
import type { ModelDefinitionConfig } from "../config/types.models.js";
|
||||
import {
|
||||
createDefaultModelPresetAppliers,
|
||||
createDefaultModelsPresetAppliers,
|
||||
createModelCatalogPresetAppliers,
|
||||
} from "./provider-onboarding-config.js";
|
||||
|
||||
function createModel(id: string, name: string): ModelDefinitionConfig {
|
||||
return {
|
||||
id,
|
||||
name,
|
||||
reasoning: false,
|
||||
input: ["text"],
|
||||
cost: {
|
||||
input: 0,
|
||||
output: 0,
|
||||
cacheRead: 0,
|
||||
cacheWrite: 0,
|
||||
},
|
||||
contextWindow: 128_000,
|
||||
maxTokens: 8_192,
|
||||
};
|
||||
}
|
||||
|
||||
function expectPrimaryModel(cfg: OpenClawConfig, primary: string) {
|
||||
expect(cfg.agents?.defaults?.model).toEqual({
|
||||
primary,
|
||||
});
|
||||
}
|
||||
|
||||
function expectPrimaryModelAlias(cfg: OpenClawConfig, modelRef: string, alias: string) {
|
||||
expect(cfg.agents?.defaults?.models).toMatchObject({
|
||||
[modelRef]: {
|
||||
alias,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
function expectProviderModels(
|
||||
cfg: OpenClawConfig,
|
||||
providerId: string,
|
||||
expected: Record<string, unknown>,
|
||||
) {
|
||||
const providers = cfg.models?.providers as Record<string, unknown> | undefined;
|
||||
expect(providers?.[providerId]).toMatchObject(expected);
|
||||
}
|
||||
|
||||
function resolveAliasObjects(aliases: Array<string | { modelRef: string; alias: string }>) {
|
||||
return aliases.filter(
|
||||
(alias): alias is { modelRef: string; alias: string } => typeof alias !== "string",
|
||||
);
|
||||
}
|
||||
|
||||
function createDemoProviderParams(params?: {
|
||||
providerId?: string;
|
||||
baseUrl?: string;
|
||||
aliases?: Array<string | { modelRef: string; alias: string }>;
|
||||
models?: ModelDefinitionConfig[];
|
||||
}) {
|
||||
const providerId = params?.providerId ?? "demo";
|
||||
const baseUrl = params?.baseUrl ?? "https://demo.test/v1";
|
||||
const models = params?.models ?? [createModel("demo-default", "Demo Default")];
|
||||
return {
|
||||
providerId,
|
||||
api: "openai-completions" as const,
|
||||
baseUrl,
|
||||
aliases: params?.aliases ?? [
|
||||
{ modelRef: `${providerId}/${models[0]?.id ?? "demo-default"}`, alias: "Demo" },
|
||||
],
|
||||
models,
|
||||
};
|
||||
}
|
||||
|
||||
describe("provider onboarding preset appliers", () => {
|
||||
it.each([
|
||||
{
|
||||
name: "creates provider and primary-model appliers for a default model preset",
|
||||
kind: "default-model",
|
||||
},
|
||||
{
|
||||
name: "passes variant args through default-models resolvers",
|
||||
kind: "default-models",
|
||||
},
|
||||
{
|
||||
name: "creates model-catalog appliers that preserve existing aliases",
|
||||
kind: "catalog-models",
|
||||
},
|
||||
] as const)("$name", ({ kind }) => {
|
||||
if (kind === "default-model") {
|
||||
const params = createDemoProviderParams();
|
||||
const appliers = createDefaultModelPresetAppliers({
|
||||
primaryModelRef: "demo/demo-default",
|
||||
resolveParams: () => ({
|
||||
providerId: params.providerId,
|
||||
api: params.api,
|
||||
baseUrl: params.baseUrl,
|
||||
defaultModel: params.models[0],
|
||||
defaultModelId: params.models[0]?.id ?? "demo-default",
|
||||
aliases: resolveAliasObjects(params.aliases),
|
||||
}),
|
||||
});
|
||||
|
||||
const providerOnly = appliers.applyProviderConfig({});
|
||||
expectPrimaryModelAlias(providerOnly, "demo/demo-default", "Demo");
|
||||
expect(providerOnly.agents?.defaults?.model).toBeUndefined();
|
||||
|
||||
const withPrimary = appliers.applyConfig({});
|
||||
expectPrimaryModel(withPrimary, "demo/demo-default");
|
||||
return;
|
||||
}
|
||||
|
||||
if (kind === "default-models") {
|
||||
const params = createDemoProviderParams({
|
||||
models: [createModel("a", "Model A"), createModel("b", "Model B")],
|
||||
aliases: [{ modelRef: "demo/a", alias: "Demo A" }],
|
||||
});
|
||||
const appliers = createDefaultModelsPresetAppliers<[string]>({
|
||||
primaryModelRef: "demo/a",
|
||||
resolveParams: (_cfg, baseUrl) => ({
|
||||
providerId: params.providerId,
|
||||
api: params.api,
|
||||
baseUrl,
|
||||
defaultModels: params.models,
|
||||
aliases: resolveAliasObjects(params.aliases),
|
||||
}),
|
||||
});
|
||||
|
||||
const cfg = appliers.applyConfig({}, "https://alt.test/v1");
|
||||
expectProviderModels(cfg, "demo", {
|
||||
baseUrl: "https://alt.test/v1",
|
||||
models: [
|
||||
{ id: "a", name: "Model A" },
|
||||
{ id: "b", name: "Model B" },
|
||||
],
|
||||
});
|
||||
expectPrimaryModel(cfg, "demo/a");
|
||||
return;
|
||||
}
|
||||
|
||||
const params = createDemoProviderParams({
|
||||
providerId: "catalog",
|
||||
baseUrl: "https://catalog.test/v1",
|
||||
models: [createModel("default", "Catalog Default"), createModel("backup", "Catalog Backup")],
|
||||
aliases: ["catalog/default", { modelRef: "catalog/default", alias: "Catalog Default" }],
|
||||
});
|
||||
const appliers = createModelCatalogPresetAppliers({
|
||||
primaryModelRef: "catalog/default",
|
||||
resolveParams: () => ({
|
||||
providerId: params.providerId,
|
||||
api: params.api,
|
||||
baseUrl: params.baseUrl,
|
||||
catalogModels: params.models,
|
||||
aliases: params.aliases,
|
||||
}),
|
||||
});
|
||||
|
||||
const cfg = appliers.applyConfig({
|
||||
agents: {
|
||||
defaults: {
|
||||
models: {
|
||||
"catalog/default": {
|
||||
alias: "Existing Alias",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
expectPrimaryModelAlias(cfg, "catalog/default", "Existing Alias");
|
||||
expectPrimaryModel(cfg, "catalog/default");
|
||||
});
|
||||
});
|
||||
@@ -1,413 +0,0 @@
|
||||
import { findNormalizedProviderKey } from "../agents/provider-id.js";
|
||||
import type { OpenClawConfig } from "../config/config.js";
|
||||
import type { AgentModelEntryConfig } from "../config/types.agent-defaults.js";
|
||||
import type {
|
||||
ModelApi,
|
||||
ModelDefinitionConfig,
|
||||
ModelProviderConfig,
|
||||
} from "../config/types.models.js";
|
||||
|
||||
function extractAgentDefaultModelFallbacks(model: unknown): string[] | undefined {
|
||||
if (!model || typeof model !== "object") {
|
||||
return undefined;
|
||||
}
|
||||
if (!("fallbacks" in model)) {
|
||||
return undefined;
|
||||
}
|
||||
const fallbacks = (model as { fallbacks?: unknown }).fallbacks;
|
||||
return Array.isArray(fallbacks) ? fallbacks.map((v) => String(v)) : undefined;
|
||||
}
|
||||
|
||||
export type AgentModelAliasEntry =
|
||||
| string
|
||||
| {
|
||||
modelRef: string;
|
||||
alias?: string;
|
||||
};
|
||||
|
||||
function normalizeAgentModelAliasEntry(entry: AgentModelAliasEntry): {
|
||||
modelRef: string;
|
||||
alias?: string;
|
||||
} {
|
||||
if (typeof entry === "string") {
|
||||
return { modelRef: entry };
|
||||
}
|
||||
return entry;
|
||||
}
|
||||
|
||||
export function withAgentModelAliases(
|
||||
existing: Record<string, AgentModelEntryConfig> | undefined,
|
||||
aliases: readonly AgentModelAliasEntry[],
|
||||
): Record<string, AgentModelEntryConfig> {
|
||||
const next = { ...existing };
|
||||
for (const entry of aliases) {
|
||||
const normalized = normalizeAgentModelAliasEntry(entry);
|
||||
next[normalized.modelRef] = {
|
||||
...next[normalized.modelRef],
|
||||
...(normalized.alias ? { alias: next[normalized.modelRef]?.alias ?? normalized.alias } : {}),
|
||||
};
|
||||
}
|
||||
return next;
|
||||
}
|
||||
|
||||
export function applyOnboardAuthAgentModelsAndProviders(
|
||||
cfg: OpenClawConfig,
|
||||
params: {
|
||||
agentModels: Record<string, AgentModelEntryConfig>;
|
||||
providers: Record<string, ModelProviderConfig>;
|
||||
},
|
||||
): OpenClawConfig {
|
||||
return {
|
||||
...cfg,
|
||||
agents: {
|
||||
...cfg.agents,
|
||||
defaults: {
|
||||
...cfg.agents?.defaults,
|
||||
models: params.agentModels,
|
||||
},
|
||||
},
|
||||
models: {
|
||||
mode: cfg.models?.mode ?? "merge",
|
||||
providers: params.providers,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export function applyAgentDefaultModelPrimary(
|
||||
cfg: OpenClawConfig,
|
||||
primary: string,
|
||||
): OpenClawConfig {
|
||||
const existingFallbacks = extractAgentDefaultModelFallbacks(cfg.agents?.defaults?.model);
|
||||
return {
|
||||
...cfg,
|
||||
agents: {
|
||||
...cfg.agents,
|
||||
defaults: {
|
||||
...cfg.agents?.defaults,
|
||||
model: {
|
||||
...(existingFallbacks ? { fallbacks: existingFallbacks } : undefined),
|
||||
primary,
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export function applyProviderConfigWithDefaultModels(
|
||||
cfg: OpenClawConfig,
|
||||
params: {
|
||||
agentModels: Record<string, AgentModelEntryConfig>;
|
||||
providerId: string;
|
||||
api: ModelApi;
|
||||
baseUrl: string;
|
||||
defaultModels: ModelDefinitionConfig[];
|
||||
defaultModelId?: string;
|
||||
},
|
||||
): OpenClawConfig {
|
||||
const providerState = resolveProviderModelMergeState(cfg, params.providerId);
|
||||
|
||||
const defaultModels = params.defaultModels;
|
||||
const defaultModelId = params.defaultModelId ?? defaultModels[0]?.id;
|
||||
const hasDefaultModel = defaultModelId
|
||||
? providerState.existingModels.some((model) => model.id === defaultModelId)
|
||||
: true;
|
||||
const mergedModels =
|
||||
providerState.existingModels.length > 0
|
||||
? hasDefaultModel || defaultModels.length === 0
|
||||
? providerState.existingModels
|
||||
: [...providerState.existingModels, ...defaultModels]
|
||||
: defaultModels;
|
||||
return applyProviderConfigWithMergedModels(cfg, {
|
||||
agentModels: params.agentModels,
|
||||
providerId: params.providerId,
|
||||
providerState,
|
||||
api: params.api,
|
||||
baseUrl: params.baseUrl,
|
||||
mergedModels,
|
||||
fallbackModels: defaultModels,
|
||||
});
|
||||
}
|
||||
|
||||
export function applyProviderConfigWithDefaultModel(
|
||||
cfg: OpenClawConfig,
|
||||
params: {
|
||||
agentModels: Record<string, AgentModelEntryConfig>;
|
||||
providerId: string;
|
||||
api: ModelApi;
|
||||
baseUrl: string;
|
||||
defaultModel: ModelDefinitionConfig;
|
||||
defaultModelId?: string;
|
||||
},
|
||||
): OpenClawConfig {
|
||||
return applyProviderConfigWithDefaultModels(cfg, {
|
||||
agentModels: params.agentModels,
|
||||
providerId: params.providerId,
|
||||
api: params.api,
|
||||
baseUrl: params.baseUrl,
|
||||
defaultModels: [params.defaultModel],
|
||||
defaultModelId: params.defaultModelId ?? params.defaultModel.id,
|
||||
});
|
||||
}
|
||||
|
||||
export function applyProviderConfigWithDefaultModelPreset(
|
||||
cfg: OpenClawConfig,
|
||||
params: {
|
||||
providerId: string;
|
||||
api: ModelApi;
|
||||
baseUrl: string;
|
||||
defaultModel: ModelDefinitionConfig;
|
||||
defaultModelId?: string;
|
||||
aliases?: readonly AgentModelAliasEntry[];
|
||||
primaryModelRef?: string;
|
||||
},
|
||||
): OpenClawConfig {
|
||||
const next = applyProviderConfigWithDefaultModel(cfg, {
|
||||
agentModels: withAgentModelAliases(cfg.agents?.defaults?.models, params.aliases ?? []),
|
||||
providerId: params.providerId,
|
||||
api: params.api,
|
||||
baseUrl: params.baseUrl,
|
||||
defaultModel: params.defaultModel,
|
||||
defaultModelId: params.defaultModelId,
|
||||
});
|
||||
return params.primaryModelRef
|
||||
? applyAgentDefaultModelPrimary(next, params.primaryModelRef)
|
||||
: next;
|
||||
}
|
||||
|
||||
export type ProviderOnboardPresetAppliers<TArgs extends unknown[]> = {
|
||||
applyProviderConfig: (cfg: OpenClawConfig, ...args: TArgs) => OpenClawConfig;
|
||||
applyConfig: (cfg: OpenClawConfig, ...args: TArgs) => OpenClawConfig;
|
||||
};
|
||||
|
||||
function createProviderPresetAppliers<
|
||||
TArgs extends unknown[],
|
||||
TParams extends {
|
||||
primaryModelRef?: string;
|
||||
},
|
||||
>(params: {
|
||||
resolveParams: (
|
||||
cfg: OpenClawConfig,
|
||||
...args: TArgs
|
||||
) => Omit<TParams, "primaryModelRef"> | null | undefined;
|
||||
applyPreset: (cfg: OpenClawConfig, preset: TParams) => OpenClawConfig;
|
||||
primaryModelRef: string;
|
||||
}): ProviderOnboardPresetAppliers<TArgs> {
|
||||
return {
|
||||
applyProviderConfig(cfg, ...args) {
|
||||
const resolved = params.resolveParams(cfg, ...args);
|
||||
return resolved ? params.applyPreset(cfg, resolved as TParams) : cfg;
|
||||
},
|
||||
applyConfig(cfg, ...args) {
|
||||
const resolved = params.resolveParams(cfg, ...args);
|
||||
if (!resolved) {
|
||||
return cfg;
|
||||
}
|
||||
return params.applyPreset(cfg, {
|
||||
...(resolved as TParams),
|
||||
primaryModelRef: params.primaryModelRef,
|
||||
});
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export function createDefaultModelPresetAppliers<TArgs extends unknown[]>(params: {
|
||||
resolveParams: (
|
||||
cfg: OpenClawConfig,
|
||||
...args: TArgs
|
||||
) =>
|
||||
| Omit<Parameters<typeof applyProviderConfigWithDefaultModelPreset>[1], "primaryModelRef">
|
||||
| null
|
||||
| undefined;
|
||||
primaryModelRef: string;
|
||||
}): ProviderOnboardPresetAppliers<TArgs> {
|
||||
return createProviderPresetAppliers({
|
||||
resolveParams: params.resolveParams,
|
||||
applyPreset: applyProviderConfigWithDefaultModelPreset,
|
||||
primaryModelRef: params.primaryModelRef,
|
||||
});
|
||||
}
|
||||
|
||||
export function applyProviderConfigWithDefaultModelsPreset(
|
||||
cfg: OpenClawConfig,
|
||||
params: {
|
||||
providerId: string;
|
||||
api: ModelApi;
|
||||
baseUrl: string;
|
||||
defaultModels: ModelDefinitionConfig[];
|
||||
defaultModelId?: string;
|
||||
aliases?: readonly AgentModelAliasEntry[];
|
||||
primaryModelRef?: string;
|
||||
},
|
||||
): OpenClawConfig {
|
||||
const next = applyProviderConfigWithDefaultModels(cfg, {
|
||||
agentModels: withAgentModelAliases(cfg.agents?.defaults?.models, params.aliases ?? []),
|
||||
providerId: params.providerId,
|
||||
api: params.api,
|
||||
baseUrl: params.baseUrl,
|
||||
defaultModels: params.defaultModels,
|
||||
defaultModelId: params.defaultModelId,
|
||||
});
|
||||
return params.primaryModelRef
|
||||
? applyAgentDefaultModelPrimary(next, params.primaryModelRef)
|
||||
: next;
|
||||
}
|
||||
|
||||
export function createDefaultModelsPresetAppliers<TArgs extends unknown[]>(params: {
|
||||
resolveParams: (
|
||||
cfg: OpenClawConfig,
|
||||
...args: TArgs
|
||||
) =>
|
||||
| Omit<Parameters<typeof applyProviderConfigWithDefaultModelsPreset>[1], "primaryModelRef">
|
||||
| null
|
||||
| undefined;
|
||||
primaryModelRef: string;
|
||||
}): ProviderOnboardPresetAppliers<TArgs> {
|
||||
return createProviderPresetAppliers({
|
||||
resolveParams: params.resolveParams,
|
||||
applyPreset: applyProviderConfigWithDefaultModelsPreset,
|
||||
primaryModelRef: params.primaryModelRef,
|
||||
});
|
||||
}
|
||||
|
||||
export function applyProviderConfigWithModelCatalog(
|
||||
cfg: OpenClawConfig,
|
||||
params: {
|
||||
agentModels: Record<string, AgentModelEntryConfig>;
|
||||
providerId: string;
|
||||
api: ModelApi;
|
||||
baseUrl: string;
|
||||
catalogModels: ModelDefinitionConfig[];
|
||||
},
|
||||
): OpenClawConfig {
|
||||
const providerState = resolveProviderModelMergeState(cfg, params.providerId);
|
||||
const catalogModels = params.catalogModels;
|
||||
const mergedModels =
|
||||
providerState.existingModels.length > 0
|
||||
? [
|
||||
...providerState.existingModels,
|
||||
...catalogModels.filter(
|
||||
(model) => !providerState.existingModels.some((existing) => existing.id === model.id),
|
||||
),
|
||||
]
|
||||
: catalogModels;
|
||||
return applyProviderConfigWithMergedModels(cfg, {
|
||||
agentModels: params.agentModels,
|
||||
providerId: params.providerId,
|
||||
providerState,
|
||||
api: params.api,
|
||||
baseUrl: params.baseUrl,
|
||||
mergedModels,
|
||||
fallbackModels: catalogModels,
|
||||
});
|
||||
}
|
||||
|
||||
export function applyProviderConfigWithModelCatalogPreset(
|
||||
cfg: OpenClawConfig,
|
||||
params: {
|
||||
providerId: string;
|
||||
api: ModelApi;
|
||||
baseUrl: string;
|
||||
catalogModels: ModelDefinitionConfig[];
|
||||
aliases?: readonly AgentModelAliasEntry[];
|
||||
primaryModelRef?: string;
|
||||
},
|
||||
): OpenClawConfig {
|
||||
const next = applyProviderConfigWithModelCatalog(cfg, {
|
||||
agentModels: withAgentModelAliases(cfg.agents?.defaults?.models, params.aliases ?? []),
|
||||
providerId: params.providerId,
|
||||
api: params.api,
|
||||
baseUrl: params.baseUrl,
|
||||
catalogModels: params.catalogModels,
|
||||
});
|
||||
return params.primaryModelRef
|
||||
? applyAgentDefaultModelPrimary(next, params.primaryModelRef)
|
||||
: next;
|
||||
}
|
||||
|
||||
export function createModelCatalogPresetAppliers<TArgs extends unknown[]>(params: {
|
||||
resolveParams: (
|
||||
cfg: OpenClawConfig,
|
||||
...args: TArgs
|
||||
) =>
|
||||
| Omit<Parameters<typeof applyProviderConfigWithModelCatalogPreset>[1], "primaryModelRef">
|
||||
| null
|
||||
| undefined;
|
||||
primaryModelRef: string;
|
||||
}): ProviderOnboardPresetAppliers<TArgs> {
|
||||
return createProviderPresetAppliers({
|
||||
resolveParams: params.resolveParams,
|
||||
applyPreset: applyProviderConfigWithModelCatalogPreset,
|
||||
primaryModelRef: params.primaryModelRef,
|
||||
});
|
||||
}
|
||||
|
||||
type ProviderModelMergeState = {
|
||||
providers: Record<string, ModelProviderConfig>;
|
||||
existingProvider?: ModelProviderConfig;
|
||||
existingModels: ModelDefinitionConfig[];
|
||||
};
|
||||
|
||||
function resolveProviderModelMergeState(
|
||||
cfg: OpenClawConfig,
|
||||
providerId: string,
|
||||
): ProviderModelMergeState {
|
||||
const providers = { ...cfg.models?.providers } as Record<string, ModelProviderConfig>;
|
||||
const existingProviderKey = findNormalizedProviderKey(providers, providerId);
|
||||
const existingProvider =
|
||||
existingProviderKey !== undefined
|
||||
? (providers[existingProviderKey] as ModelProviderConfig | undefined)
|
||||
: undefined;
|
||||
const existingModels: ModelDefinitionConfig[] = Array.isArray(existingProvider?.models)
|
||||
? existingProvider.models
|
||||
: [];
|
||||
if (existingProviderKey && existingProviderKey !== providerId) {
|
||||
delete providers[existingProviderKey];
|
||||
}
|
||||
return { providers, existingProvider, existingModels };
|
||||
}
|
||||
|
||||
function applyProviderConfigWithMergedModels(
|
||||
cfg: OpenClawConfig,
|
||||
params: {
|
||||
agentModels: Record<string, AgentModelEntryConfig>;
|
||||
providerId: string;
|
||||
providerState: ProviderModelMergeState;
|
||||
api: ModelApi;
|
||||
baseUrl: string;
|
||||
mergedModels: ModelDefinitionConfig[];
|
||||
fallbackModels: ModelDefinitionConfig[];
|
||||
},
|
||||
): OpenClawConfig {
|
||||
params.providerState.providers[params.providerId] = buildProviderConfig({
|
||||
existingProvider: params.providerState.existingProvider,
|
||||
api: params.api,
|
||||
baseUrl: params.baseUrl,
|
||||
mergedModels: params.mergedModels,
|
||||
fallbackModels: params.fallbackModels,
|
||||
});
|
||||
return applyOnboardAuthAgentModelsAndProviders(cfg, {
|
||||
agentModels: params.agentModels,
|
||||
providers: params.providerState.providers,
|
||||
});
|
||||
}
|
||||
|
||||
function buildProviderConfig(params: {
|
||||
existingProvider: ModelProviderConfig | undefined;
|
||||
api: ModelApi;
|
||||
baseUrl: string;
|
||||
mergedModels: ModelDefinitionConfig[];
|
||||
fallbackModels: ModelDefinitionConfig[];
|
||||
}): ModelProviderConfig {
|
||||
const { apiKey: existingApiKey, ...existingProviderRest } = (params.existingProvider ?? {}) as {
|
||||
apiKey?: string;
|
||||
};
|
||||
const normalizedApiKey = typeof existingApiKey === "string" ? existingApiKey.trim() : undefined;
|
||||
|
||||
return {
|
||||
...existingProviderRest,
|
||||
baseUrl: params.baseUrl,
|
||||
api: params.api,
|
||||
...(normalizedApiKey ? { apiKey: normalizedApiKey } : {}),
|
||||
models: params.mergedModels.length > 0 ? params.mergedModels : params.fallbackModels,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user