




















@@ -8,7 +8,6 @@ import {
88import type { MigrationItem, MigrationProviderContext } from "openclaw/plugin-sdk/plugin-entry";
99import {
1010applyAuthProfileConfig,
11-applyProviderAuthConfigPatch,
1211buildApiKeyCredential,
1312buildOauthProviderAuthResult,
1413readCodexCliCredentialsCached,
@@ -34,13 +33,18 @@ const CODEX_REASON_MISSING_AUTH_METADATA = "missing auth metadata";
3433const CODEX_CONFIG_PATCH_MODE_RETURN = "return";
35343635type CodexMigrationTargets = ReturnType<typeof resolveCodexMigrationTargets>;
36+type AgentDefaultModelConfigs = NonNullable<
37+NonNullable<NonNullable<OpenClawConfig["agents"]>["defaults"]>["models"]
38+>;
39+type AgentDefaultModelConfigEntry = AgentDefaultModelConfigs[string];
37403841type CodexAuthCredential =
3942| {
4043kind: "oauth";
4144provider: typeof OPENAI_CODEX_PROVIDER_ID;
4245profileId: string;
4346result: ProviderAuthResult;
47+modelConfigs: AgentDefaultModelConfigs;
4448}
4549| {
4650kind: "api_key";
@@ -170,6 +174,15 @@ async function readModelRefs(source: CodexSource): Promise<string[]> {
170174return [...refs].toSorted();
171175}
172176177+function readProviderAuthModelConfigs(result: ProviderAuthResult): AgentDefaultModelConfigs {
178+const models = result.configPatch?.agents?.defaults?.models;
179+if (isRecord(models)) {
180+return { ...models };
181+}
182+const defaultModel = readString(result.defaultModel) ?? OPENAI_CODEX_DEFAULT_MODEL;
183+return { [defaultModel]: {} };
184+}
185+173186async function buildCodexOAuthCredential(source: CodexSource): Promise<CodexAuthCredential | null> {
174187const credential = readCodexCliCredentialsCached({
175188codexHome: source.codexHome,
@@ -206,7 +219,13 @@ async function buildCodexOAuthCredential(source: CodexSource): Promise<CodexAuth
206219});
207220const profile = result.profiles[0];
208221return profile
209- ? { kind: "oauth", provider: OPENAI_CODEX_PROVIDER_ID, profileId: profile.profileId, result }
222+ ? {
223+kind: "oauth",
224+provider: OPENAI_CODEX_PROVIDER_ID,
225+profileId: profile.profileId,
226+ result,
227+modelConfigs: readProviderAuthModelConfigs(result),
228+}
210229 : null;
211230}
212231@@ -351,17 +370,47 @@ function applyDefaultModelIfMissing(cfg: OpenClawConfig): OpenClawConfig {
351370};
352371}
353372373+function mergeModelConfigEntry(
374+existing: AgentDefaultModelConfigEntry | undefined,
375+patch: AgentDefaultModelConfigEntry,
376+): AgentDefaultModelConfigEntry {
377+if (existing && isRecord(existing) && isRecord(patch)) {
378+return { ...existing, ...patch } as AgentDefaultModelConfigEntry;
379+}
380+return existing ?? patch;
381+}
382+383+function applyOAuthModelConfigsToConfig(
384+cfg: OpenClawConfig,
385+credential: Extract<CodexAuthCredential, { kind: "oauth" }>,
386+): OpenClawConfig {
387+const existingModels = cfg.agents?.defaults?.models ?? {};
388+const models: AgentDefaultModelConfigs = credential.result.replaceDefaultModels
389+ ? { ...credential.modelConfigs }
390+ : { ...existingModels };
391+if (!credential.result.replaceDefaultModels) {
392+for (const [modelRef, modelConfig] of Object.entries(credential.modelConfigs)) {
393+models[modelRef] = mergeModelConfigEntry(models[modelRef], modelConfig);
394+}
395+}
396+return {
397+ ...cfg,
398+agents: {
399+ ...cfg.agents,
400+defaults: {
401+ ...cfg.agents?.defaults,
402+ models,
403+},
404+},
405+};
406+}
407+354408function applyOAuthConfigToConfig(
355409cfg: OpenClawConfig,
356410credential: Extract<CodexAuthCredential, { kind: "oauth" }>,
357411profileId: string,
358412): OpenClawConfig {
359-let next = cfg;
360-if (credential.result.configPatch) {
361-next = applyProviderAuthConfigPatch(next, credential.result.configPatch, {
362-replaceDefaultModels: credential.result.replaceDefaultModels,
363-});
364-}
413+let next = applyOAuthModelConfigsToConfig(cfg, credential);
365414const profile = credential.result.profiles[0];
366415if (profile) {
367416next = applyAuthProfileConfig(next, {
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。