@pikecode/api-key-manager 1.0.36 → 1.0.37

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.0.36",
3
+ "version": "1.0.37",
4
4
  "description": "A CLI tool for managing and switching multiple API provider configurations",
5
5
  "main": "src/index.js",
6
6
  "bin": {
@@ -110,11 +110,88 @@ async function applyCodexProfile(profile, options = {}) {
110
110
  return { codexHome, backupDir };
111
111
  }
112
112
 
113
+ /**
114
+ * 确保 config.toml 中设置了 preferred_auth_method = "apikey"
115
+ * @param {string} configToml - 现有的 config.toml 内容
116
+ * @returns {string} 更新后的 config.toml 内容
117
+ */
118
+ function ensureApiKeyAuthMethod(configToml) {
119
+ if (!configToml) {
120
+ // 如果没有 config.toml,创建一个最小配置
121
+ return 'preferred_auth_method = "apikey"\n';
122
+ }
123
+
124
+ // 检查是否已经设置了 preferred_auth_method
125
+ const authMethodRegex = /^preferred_auth_method\s*=\s*["']?([^"'\n]+)["']?\s*$/m;
126
+ const match = configToml.match(authMethodRegex);
127
+
128
+ if (match) {
129
+ if (match[1].trim() === 'apikey') {
130
+ // 已经是 apikey,无需修改
131
+ return configToml;
132
+ }
133
+ // 替换为 apikey
134
+ return configToml.replace(authMethodRegex, 'preferred_auth_method = "apikey"');
135
+ }
136
+
137
+ // 没有找到 preferred_auth_method,在文件开头添加
138
+ return 'preferred_auth_method = "apikey"\n' + configToml;
139
+ }
140
+
141
+ /**
142
+ * 构建 auth.json 内容
143
+ * @param {string} apiKey - API Key
144
+ * @returns {string} auth.json 内容
145
+ */
146
+ function buildAuthJson(apiKey) {
147
+ return JSON.stringify({ api_key: apiKey }, null, 2);
148
+ }
149
+
150
+ /**
151
+ * 应用 Codex 配置(写入 config.toml 和 auth.json)
152
+ * 用于切换供应商时确保配置文件正确
153
+ * @param {object} config - 供应商配置
154
+ * @param {object} options - 选项
155
+ * @returns {Promise<{codexHome: string, backupDir: string|null}>}
156
+ */
157
+ async function applyCodexConfig(config, options = {}) {
158
+ if (!config || !config.authToken) {
159
+ throw new Error('Codex 配置缺少 API Key');
160
+ }
161
+
162
+ const codexHome = await ensureCodexHome(options.codexHome);
163
+ const { configTomlPath, authJsonPath } = buildCodexPaths(codexHome);
164
+
165
+ // 备份现有配置
166
+ const backupDir = await backupCodexFiles(codexHome);
167
+
168
+ // 读取现有 config.toml
169
+ let existingConfigToml = null;
170
+ if (await fs.pathExists(configTomlPath)) {
171
+ existingConfigToml = await fs.readFile(configTomlPath, 'utf8');
172
+ }
173
+
174
+ // 确保设置了 preferred_auth_method = "apikey"
175
+ const updatedConfigToml = ensureApiKeyAuthMethod(existingConfigToml);
176
+ await fs.writeFile(configTomlPath, updatedConfigToml, 'utf8');
177
+ await setSecurePermissions(configTomlPath);
178
+
179
+ // 写入 auth.json
180
+ const authJsonContent = buildAuthJson(config.authToken);
181
+ await fs.writeFile(authJsonPath, authJsonContent, 'utf8');
182
+ await setSecurePermissions(authJsonPath);
183
+
184
+ return { codexHome, backupDir };
185
+ }
186
+
113
187
  module.exports = {
114
188
  resolveCodexHome,
115
189
  buildCodexPaths,
116
190
  readCodexFiles,
117
191
  applyCodexProfile,
118
- backupCodexFiles
192
+ applyCodexConfig,
193
+ backupCodexFiles,
194
+ ensureApiKeyAuthMethod,
195
+ buildAuthJson
119
196
  };
120
197
 
@@ -1,5 +1,6 @@
1
1
  const spawn = require('cross-spawn');
2
2
  const { sanitizeEnvValue, clearTerminal } = require('./env-utils');
3
+ const { applyCodexConfig } = require('./codex-files');
3
4
 
4
5
  /**
5
6
  * 构建 Codex CLI 环境变量
@@ -45,6 +46,10 @@ async function executeCodexWithEnv(config, launchArgs = []) {
45
46
  throw new Error(`供应商 '${config.name}' 未配置 API Key,请使用 'akm edit ${config.name}' 添加`);
46
47
  }
47
48
 
49
+ // 写入 ~/.codex/config.toml 和 ~/.codex/auth.json
50
+ // 确保 Codex CLI 使用 API Key 认证方式
51
+ await applyCodexConfig(config);
52
+
48
53
  const env = buildCodexEnvVariables(config);
49
54
 
50
55
  // 处理参数:子命令放前面,选项放后面