import { buildZaiModelDefinition, resolveZaiBaseUrl, ZAI_DEFAULT_MODEL_ID, } from "openclaw/plugin-sdk/provider-models"; import { applyAgentDefaultModelPrimary, applyProviderConfigWithModelCatalog, type OpenClawConfig, } from "openclaw/plugin-sdk/provider-onboard"; export const ZAI_DEFAULT_MODEL_REF = `zai/${ZAI_DEFAULT_MODEL_ID}`; const ZAI_DEFAULT_MODELS = [ buildZaiModelDefinition({ id: "glm-5" }), buildZaiModelDefinition({ id: "glm-5-turbo" }), buildZaiModelDefinition({ id: "glm-4.7" }), buildZaiModelDefinition({ id: "glm-4.7-flash" }), buildZaiModelDefinition({ id: "glm-4.7-flashx" }), ]; export function applyZaiProviderConfig( cfg: OpenClawConfig, params?: { endpoint?: string; modelId?: string }, ): OpenClawConfig { const modelId = params?.modelId?.trim() || ZAI_DEFAULT_MODEL_ID; const modelRef = `zai/${modelId}`; const existingProvider = cfg.models?.providers?.zai; const models = { ...cfg.agents?.defaults?.models }; models[modelRef] = { ...models[modelRef], alias: models[modelRef]?.alias ?? "GLM", }; const existingBaseUrl = typeof existingProvider?.baseUrl === "string" ? existingProvider.baseUrl.trim() : ""; const baseUrl = params?.endpoint ? resolveZaiBaseUrl(params.endpoint) : existingBaseUrl || resolveZaiBaseUrl(); return applyProviderConfigWithModelCatalog(cfg, { agentModels: models, providerId: "zai", api: "openai-completions", baseUrl, catalogModels: ZAI_DEFAULT_MODELS, }); } export function applyZaiConfig( cfg: OpenClawConfig, params?: { endpoint?: string; modelId?: string }, ): OpenClawConfig { const modelId = params?.modelId?.trim() || ZAI_DEFAULT_MODEL_ID; const modelRef = modelId === ZAI_DEFAULT_MODEL_ID ? ZAI_DEFAULT_MODEL_REF : `zai/${modelId}`; return applyAgentDefaultModelPrimary(applyZaiProviderConfig(cfg, params), modelRef); }