From 982f78af0f3e4e60eafbb79ef3227ed1381b7142 Mon Sep 17 00:00:00 2001 From: YoVinchen Date: Thu, 29 Jan 2026 18:38:02 +0800 Subject: [PATCH] fix(codex): fix config.toml not updated after extracting common config The handleExtract function assumed codexConfig was JSON format, but it's actually a pure TOML string from useCodexConfigState. JSON.parse() failed silently, causing onConfigChange not to be called. Changes: - Handle both pure TOML string and legacy JSON format - Add missing i18n key for zh.json --- .../forms/hooks/useCodexCommonConfig.ts | 39 ++++++++++++------- src/i18n/locales/zh.json | 1 + 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/src/components/providers/forms/hooks/useCodexCommonConfig.ts b/src/components/providers/forms/hooks/useCodexCommonConfig.ts index af262e9ba..e91a9c8ad 100644 --- a/src/components/providers/forms/hooks/useCodexCommonConfig.ts +++ b/src/components/providers/forms/hooks/useCodexCommonConfig.ts @@ -406,23 +406,34 @@ export function useCodexCommonConfig({ const customToml = extractConfigToml(codexConfig); const diffResult = extractTomlDifference(customToml, extracted); if (!diffResult.error) { - // 更新 codexConfig 中的 config 字段 + // 更新 codexConfig + // codexConfig 可能是两种格式: + // 1. 纯 TOML 字符串(来自 useCodexConfigState) + // 2. JSON 字符串 { auth: {...}, config: "..." }(旧格式) + let updated = false; try { const parsed = JSON.parse(codexConfig); - parsed.config = diffResult.customToml; - onConfigChange(JSON.stringify(parsed, null, 2)); - // Notify user that config was modified (提示用户需要保存) - toast.success( - t("codexConfig.extractSuccessNeedSave", { - defaultValue: "已提取通用配置,点击保存按钮完成保存", - }), - ); - } catch (parseError) { - console.warn( - "[Extract] Failed to update codexConfig after extract:", - parseError, - ); + if (typeof parsed?.config === "string") { + // JSON 格式,更新 config 字段 + parsed.config = diffResult.customToml; + onConfigChange(JSON.stringify(parsed, null, 2)); + updated = true; + } + } catch { + // JSON 解析失败,说明是纯 TOML 字符串 } + + // 如果是纯 TOML 字符串,直接更新 + if (!updated) { + onConfigChange(diffResult.customToml); + } + + // Notify user that config was modified (提示用户需要保存) + toast.success( + t("codexConfig.extractSuccessNeedSave", { + defaultValue: "已提取通用配置,点击保存按钮完成保存", + }), + ); } } catch (error) { console.error("提取 Codex 通用配置失败:", error); diff --git a/src/i18n/locales/zh.json b/src/i18n/locales/zh.json index 805309420..a41228861 100644 --- a/src/i18n/locales/zh.json +++ b/src/i18n/locales/zh.json @@ -565,6 +565,7 @@ "extractFromCurrent": "从编辑内容提取", "extractNoCommonConfig": "当前编辑内容没有可提取的通用配置", "extractFailed": "提取失败: {{error}}", + "extractSuccessNeedSave": "已提取通用配置,点击保存按钮完成保存", "saveFailed": "保存失败: {{error}}" }, "geminiConfig": {