@pikecode/api-key-manager 1.1.1 → 1.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pikecode/api-key-manager",
3
- "version": "1.1.1",
3
+ "version": "1.1.2",
4
4
  "description": "A CLI tool for managing and switching multiple API provider configurations",
5
5
  "main": "src/index.js",
6
6
  "bin": {
@@ -129,7 +129,41 @@ function extractBaseUrlFromConfigToml(configToml) {
129
129
  }
130
130
 
131
131
  /**
132
- * 构建 auth.json 内容
132
+ * 更新 config.toml 中的 model_provider 和对应的 [model_providers.akm] section
133
+ * 使用固定 key "akm" 管理 akm 切换的供应商,不影响用户其他自定义配置
134
+ * @param {string} configToml - 现有的 config.toml 内容
135
+ * @param {string} baseUrl - 新供应商的 base_url
136
+ * @returns {string} 更新后的 config.toml 内容
137
+ */
138
+ function updateModelProvider(configToml, baseUrl) {
139
+ if (!configToml) configToml = '';
140
+
141
+ const providerKey = 'akm';
142
+ const newSection = [
143
+ `[model_providers.${providerKey}]`,
144
+ `name = "${providerKey}"`,
145
+ `base_url = "${baseUrl}"`,
146
+ 'wire_api = "responses"',
147
+ 'requires_openai_auth = true'
148
+ ].join('\n');
149
+
150
+ // 更新顶层 model_provider 字段
151
+ let result = configToml.match(/^model_provider\s*=/m)
152
+ ? configToml.replace(/^model_provider\s*=\s*["'][^"'\n]*["']/m, `model_provider = "${providerKey}"`)
153
+ : `model_provider = "${providerKey}"\n` + configToml;
154
+
155
+ // 替换或追加 [model_providers.akm] section
156
+ const sectionRegex = /\[model_providers\.akm\](?:\n(?!\[)[^\n]*)*\n?/;
157
+ if (result.match(sectionRegex)) {
158
+ result = result.replace(sectionRegex, newSection + '\n');
159
+ } else {
160
+ result = result.trimEnd() + '\n\n' + newSection + '\n';
161
+ }
162
+
163
+ return result;
164
+ }
165
+
166
+ /**
133
167
  * @param {string} apiKey - API Key
134
168
  * @returns {string} auth.json 内容
135
169
  */
@@ -138,11 +172,11 @@ function buildAuthJson(apiKey) {
138
172
  }
139
173
 
140
174
  /**
141
- * 应用 Codex 配置(写入 auth.json,清理 config.toml 中的无效字段)
142
- * config.toml 由用户自己管理,akm 只负责:
175
+ * 应用 Codex 配置(写入 auth.json,更新 config.toml 中的 provider 路由)
176
+ * akm 负责:
143
177
  * 1. 写入 auth.json(API Key)
144
- * 2. 清理之前错误写入的顶层 api_base_url 字段
145
- * base_url 通过环境变量 OPENAI_BASE_URL 传递给 Codex 进程
178
+ * 2. 更新 config.toml 中的 model_provider 和 [model_providers.akm] section
179
+ * 3. 清理之前错误写入的顶层 api_base_url 字段
146
180
  * @param {object} config - 供应商配置
147
181
  * @param {object} options - 选项
148
182
  * @returns {Promise<{codexHome: string}>}
@@ -160,8 +194,17 @@ async function applyCodexConfig(config, options = {}) {
160
194
  await fs.writeFile(authJsonPath, authJsonContent, 'utf8');
161
195
  await setSecurePermissions(authJsonPath);
162
196
 
163
- // 清理 config.toml akm 之前错误写入的顶层 api_base_url 字段
164
- if (await fs.pathExists(configTomlPath)) {
197
+ // 更新 config.toml:设置 model_provider + [model_providers.akm],清理旧的顶层 api_base_url
198
+ if (config.baseUrl) {
199
+ const existingToml = await fs.pathExists(configTomlPath)
200
+ ? await fs.readFile(configTomlPath, 'utf8')
201
+ : '';
202
+ let updatedToml = removeTopLevelApiBaseUrl(existingToml);
203
+ updatedToml = updateModelProvider(updatedToml, config.baseUrl);
204
+ await fs.writeFile(configTomlPath, updatedToml, 'utf8');
205
+ await setSecurePermissions(configTomlPath);
206
+ } else if (await fs.pathExists(configTomlPath)) {
207
+ // 没有 baseUrl,只清理旧的顶层 api_base_url
165
208
  const existingToml = await fs.readFile(configTomlPath, 'utf8');
166
209
  const cleanedToml = removeTopLevelApiBaseUrl(existingToml);
167
210
  if (cleanedToml !== existingToml) {
@@ -181,5 +224,6 @@ module.exports = {
181
224
  backupCodexFiles,
182
225
  removeTopLevelApiBaseUrl,
183
226
  extractBaseUrlFromConfigToml,
227
+ updateModelProvider,
184
228
  buildAuthJson
185
229
  };