@pikecode/api-key-manager 2.2.4 → 2.3.0

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/CHANGELOG.md CHANGED
@@ -2,9 +2,24 @@
2
2
 
3
3
  记录项目的功能、修复和兼容性变化。以下历史条目依据 Git 提交补录,不代表已核实 npm 发布状态。
4
4
 
5
- ## 未发布(当前目标版本 2.2.4)
5
+ ## 未发布
6
6
 
7
- ### 修复
7
+ 暂无。
8
+
9
+ ## 2.3.0(待发布)
10
+
11
+ ### 功能
12
+
13
+ - Claude 供应商支持结构化 `compatibility.thinkingMode`(`default` / `disabled`),添加、编辑、克隆及导入导出保留该字段。禁用模式仅向该账号的 Claude 子进程注入 `MAX_THINKING_TOKENS=0`,不再需要按供应商域名打补丁。
14
+ - 默认模式保留用户显式 shell 设置;旧配置无需迁移即可保持原行为。非法兼容字段被 Schema 拒绝,不开放任意环境变量注入。
15
+
16
+ ### 配置兼容修复
17
+
18
+ - Claude 冲突检查聚合项目、用户和显式设置文件,修复首个文件无冲突时遗漏其他文件的问题,并支持 `CLAUDE_CONFIG_DIR`。
19
+ - 先确认所有冲突文件,再执行清理。取消启动不会写入设置。清理前重新读取文件,基于同一份内容备份和更新,检测到文件再次变化时停止。备份保留来源文件名、使用唯一标识避免覆盖,Unix 权限为 `0600`,保留 Hooks、插件及无关变量。
20
+ - 新增供应商兼容配置与使用说明。新增兼容功能,版本由 2.2.3 调整为 2.3.0;旧版程序不能读取带新字段的配置,降级前需移除 `compatibility` 字段或恢复备份。
21
+
22
+ ### Codex 历史交互
8
23
 
9
24
  - 历史分支确认区分同一供应商配置、不同供应商配置和来源无法确认,避免每次重复显示固定的跨供应商提醒。
10
25
  - 第三方新对话使用独立模型供应商标识记录来源,名称、端点或认证变化后视为不同配置;不修改原历史。旧 `akm` 历史及官方 `openai` 账号无法精确区分时明确提示未知,不猜测归属。
@@ -12,13 +27,6 @@
12
27
  - 历史列表和确认页将内部来源标识映射为可读供应商名称,无法匹配时明确标为来源未确认,不猜测官方账号。
13
28
  - 确认页选择“否”返回原列表,保留项目分组、已加载批次和选中项;ESC 取消启动。
14
29
 
15
- ### 验证
16
-
17
- - 61 个测试套件、643 项测试、Lint 和覆盖率门槛通过,覆盖来源分支文案及独立模型供应商启动参数。原生 Codex 0.166.0 app-server 离线分支验证通过:目标标识写入新历史,原文件未变化。使用临时目录和模拟历史,未执行真实模型请求。
18
-
19
- ## 2.2.3
20
-
21
-
22
30
  ### 修复
23
31
 
24
32
  - 配置添加、激活、切换、参数记忆、统计、重置和导入使用候选配置,保存成功后才更新内存,防止失败修改被后续操作写入。
@@ -38,10 +46,10 @@
38
46
 
39
47
  ### 验证
40
48
 
41
- - 60 个测试套件、632 项测试、Lint 和项目覆盖率门槛通过。
49
+ - 本次验证:63 个测试套件、652 项测试、Lint 和项目覆盖率门槛通过。已覆盖确认期间外部编辑、写入前再次变化以及取消后续文件时不写入。包括真实替身 Claude 子进程的环境/参数验证、多文件冲突、受限备份及兼容字段导入导出回归。未发送真实供应商请求,未验证工具调用和完整交互会话。
42
50
  - 伪终端使用真实 Inquirer 验证 ESC 取消、下一菜单连续下移跨页及回车,返回预期条目;未覆盖所有终端模拟器与动态缩放。
43
51
  - 交互终端普通测试可自然退出;`--detectOpenHandles` 模式仍可能列出 stdin 的 `TTYWRAP`,但修复后进程也能自行结束。新增输入流三种初始状态的清理回归测试。
44
- - 版本元数据同步为 2.2.3;未执行发布。
52
+ - 版本元数据同步为 2.3.0;未执行发布。
45
53
 
46
54
  ## 2.2.2(历史补录)
47
55
 
package/README.md CHANGED
@@ -604,3 +604,18 @@ MIT License
604
604
  - [Issues](https://github.com/pikecode/api-key-manager/issues)
605
605
 
606
606
  历史恢复的确认文案会区分来源:同一第三方配置只提示创建新分支;不同配置提示上下文将发送给当前供应商;无法确定来源时明确提示未知。新建第三方对话会记录独立的模型供应商标识,旧历史不会被改写。多个官方账号仍共享原生 `openai` 标识,因此无法仅凭历史元数据判断账号是否相同。供应商名称、端点或认证改变后视为不同配置。所有情况仍需确认创建分支,默认选择“否”:返回历史列表并保留分组、已加载批次和选中项;确认页按 ESC 取消启动。历史来源可匹配现有配置时显示供应商名称,旧第三方、官方账号及已变更或删除的配置显示相应的来源未确认说明,不直接展示内部摘要标识。
607
+
608
+ ## Claude 第三方供应商兼容设置
609
+
610
+ 添加或编辑 Claude 供应商时,可选择 Thinking 兼容模式:
611
+
612
+ - `default`:保持客户端行为,保留启动 AKM 时 shell 中显式设置的 `MAX_THINKING_TOKENS`。
613
+ - `disabled`:仅为该供应商的 Claude 子进程设置 `MAX_THINKING_TOKENS=0`,用于拒绝显式 thinking 参数的兼容服务。此模式不代表支持 adaptive thinking。
614
+
615
+ 配置保存在供应商条目的 `compatibility.thinkingMode` 中,克隆、导入和导出都会保留;旧配置缺少此字段时按 `default` 运行。AKM 不修改父进程环境,切换其他账号不会继承由 AKM 注入的禁用设置。导入不支持任意环境变量。
616
+
617
+ Claude 地址通常填写站点 origin,例如 `https://provider.example`;不要直接照搬 OpenAI-compatible 的 `/v1` 地址。代理自定义路径请遵循供应商说明,AKM 不自动删除路径。主模型与快速模型应分别填写供应商支持的精确 ID。
618
+
619
+ 启动前会检查项目、用户目录、`CLAUDE_CONFIG_DIR` 及 `CLAUDE_SETTINGS_PATH` 指定文件中的连接覆盖,逐文件提供备份清理、忽略或取消选项。备份保留原文件名并附唯一标识,Unix 权限为 `0600`;清理保留其他环境变量、Hooks 和插件。所有文件确认后才写入,取消时保留设置。写入前会重新读取并检查文件变化。此检查不涵盖组织管理策略等所有 Claude 配置来源。
620
+
621
+ 源码中的修复不会自动替换全局 npm 安装。可在源码目录使用 `npm start -- edit <供应商名>` 设置模式,再用 `npm start -- <供应商名> -q` 启动;已运行的 Claude 需退出重启。
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pikecode/api-key-manager",
3
- "version": "2.2.4",
3
+ "version": "2.3.0",
4
4
  "description": "A CLI tool for managing and switching multiple API provider configurations",
5
5
  "main": "src/index.js",
6
6
  "bin": {
@@ -10,6 +10,7 @@ const { UIHelper } = require('../../utils/ui-helper');
10
10
  const { UI_MESSAGES } = require('../../constants/ui');
11
11
  const { Logger } = require('../../utils/logger');
12
12
  const { registry } = require('../../CommandRegistry');
13
+ const { thinkingModeQuestion } = require('../../utils/claude-compatibility');
13
14
  const { getCodexLaunchArgs } = require('../../utils/launch-args');
14
15
 
15
16
  /**
@@ -93,6 +94,10 @@ async function promptProviderInfo(adder) {
93
94
  },
94
95
  when: answers => (answers.ideName || adder.presetIdeName) !== 'codex'
95
96
  },
97
+ {
98
+ ...thinkingModeQuestion(),
99
+ when: answers => (answers.ideName || adder.presetIdeName) !== 'codex'
100
+ },
96
101
  // Codex‑specific prompts (manual entry)
97
102
  {
98
103
  type: 'input',
@@ -48,6 +48,7 @@ async function saveProvider(adder, answers) {
48
48
  baseUrl: answers.baseUrl,
49
49
  authToken: answers.authToken,
50
50
  authMode: answers.authMode,
51
+ compatibility: answers.ideName === 'codex' ? undefined : { thinkingMode: answers.thinkingMode || 'default' },
51
52
  codexFiles: answers.codexFiles || null,
52
53
  launchArgs,
53
54
  primaryModel: modelConfig.primaryModel,
@@ -231,6 +231,7 @@ class ProviderCloner extends BaseCommand {
231
231
  authToken: overrides.authToken !== undefined ? overrides.authToken : source.authToken,
232
232
  baseUrl,
233
233
  authMode,
234
+ compatibility: source.compatibility,
234
235
  launchArgs: overrides.launchArgs !== undefined ? overrides.launchArgs : (source.launchArgs || []),
235
236
  primaryModel: source.models?.primary || null,
236
237
  smallFastModel: source.models?.smallFast || null,
@@ -3,17 +3,33 @@ const { Logger } = require('../../utils/logger');
3
3
  const { UIHelper } = require('../../utils/ui-helper');
4
4
  const {
5
5
  findSettingsConflict,
6
- backupSettingsFile,
7
- clearConflictKeys,
8
- saveSettingsFile
6
+ applySettingsCleanup
9
7
  } = require('../../utils/claude-settings');
10
8
 
11
9
  async function ensureClaudeSettingsCompatibility(command, provider) {
12
- const conflict = await findSettingsConflict();
10
+ const conflict = await findSettingsConflict(provider);
13
11
  if (!conflict) {
14
12
  return true;
15
13
  }
16
14
 
15
+ const cleanups = [];
16
+ for (const entry of conflict.conflicts || [conflict]) {
17
+ const action = await handleSettingsConflict(command, provider, entry);
18
+ if (!action) return false;
19
+ if (action === 'fix') cleanups.push(entry);
20
+ }
21
+ for (const entry of cleanups) {
22
+ try {
23
+ const backupPath = await applySettingsCleanup(entry, provider);
24
+ if (backupPath) Logger.success(`已将 ${entry.filePath} 备份至 '${backupPath}' 并清空冲突变量。`);
25
+ } catch (error) {
26
+ throw new Error(`清理 Claude 设置文件失败: ${error.message}`);
27
+ }
28
+ }
29
+ return true;
30
+ }
31
+
32
+ async function handleSettingsConflict(command, provider, conflict) {
17
33
  const keyList = conflict.keys.map((key) => `• ${key}`).join('\n');
18
34
  const backupDir = path.dirname(conflict.filePath);
19
35
 
@@ -22,7 +38,7 @@ async function ensureClaudeSettingsCompatibility(command, provider) {
22
38
  console.log();
23
39
  console.log(UIHelper.createCard('冲突文件', conflict.filePath, UIHelper.icons.info));
24
40
  console.log();
25
- console.log(UIHelper.createCard('备份目录', `${backupDir}\n备份文件将命名为 settings.backup-YYYYMMDD_HHmmss.json`, UIHelper.icons.info));
41
+ console.log(UIHelper.createCard('备份目录', `${backupDir}\n备份文件将命名为 <原文件名>.backup-时间戳-唯一标识.json`, UIHelper.icons.info));
26
42
  console.log();
27
43
  console.log(UIHelper.createCard('可能覆盖的变量', keyList, UIHelper.icons.warning));
28
44
  console.log();
@@ -53,7 +69,7 @@ async function ensureClaudeSettingsCompatibility(command, provider) {
53
69
  }
54
70
 
55
71
  if (answer.action === 'fix') {
56
- return await fixSettingsConflict(command, conflict, backupDir);
72
+ return await confirmSettingsCleanup(command, backupDir) ? 'fix' : false;
57
73
  }
58
74
 
59
75
  if (answer.action === 'ignore') {
@@ -65,14 +81,14 @@ async function ensureClaudeSettingsCompatibility(command, provider) {
65
81
  return false;
66
82
  }
67
83
 
68
- async function fixSettingsConflict(command, conflict, backupDir) {
84
+ async function confirmSettingsCleanup(command, backupDir) {
69
85
  let confirmBackup;
70
86
  try {
71
87
  confirmBackup = await command.prompt([
72
88
  {
73
89
  type: 'confirm',
74
90
  name: 'confirmed',
75
- message: `将在 ${backupDir} 中创建备份文件 (settings.backup-YYYYMMDD_HHmmss.json),并清空冲突变量。是否继续?`,
91
+ message: `将在 ${backupDir} 中创建备份文件 (<原文件名>.backup-时间戳-唯一标识.json),并清空冲突变量。是否继续?`,
76
92
  default: true
77
93
  }
78
94
  ]);
@@ -89,25 +105,9 @@ async function fixSettingsConflict(command, conflict, backupDir) {
89
105
  return false;
90
106
  }
91
107
 
92
- try {
93
- const backupPath = await backupSettingsFile(conflict.filePath);
94
- const updatedSettings = clearConflictKeys(
95
- {
96
- ...conflict.settings,
97
- env: conflict.settings.env ? { ...conflict.settings.env } : undefined
98
- },
99
- conflict.keys
100
- );
101
- await saveSettingsFile(conflict.filePath, updatedSettings);
102
- Logger.success(`已将 ${conflict.filePath} 备份至 '${backupPath}' 并清空冲突变量。`);
103
- } catch (error) {
104
- throw new Error(`清理 Claude 设置文件失败: ${error.message}`);
105
- }
106
-
107
108
  return true;
108
109
  }
109
110
 
110
111
  module.exports = {
111
- ensureClaudeSettingsCompatibility,
112
- fixSettingsConflict
112
+ ensureClaudeSettingsCompatibility
113
113
  };
@@ -1,3 +1,5 @@
1
+ const { thinkingModeQuestion } = require('../../utils/claude-compatibility');
2
+
1
3
  class ProviderEditQuestionsHelper {
2
4
  static buildQuestions(provider, validator) {
3
5
  const isCodex = provider.ideName === 'codex';
@@ -7,7 +9,7 @@ class ProviderEditQuestionsHelper {
7
9
  ];
8
10
 
9
11
  if (!isCodex) {
10
- questions.push(...this.buildClaudeQuestions(provider, validator));
12
+ questions.push(...this.buildClaudeQuestions(provider, validator), thinkingModeQuestion(provider));
11
13
  } else {
12
14
  questions.push(this.buildCodexAuthModeQuestion(provider));
13
15
  }
@@ -132,6 +132,7 @@ class ProviderManager {
132
132
  };
133
133
 
134
134
  if (!isCodex) {
135
+ updatedProvider.compatibility = { thinkingMode: answers.thinkingMode || provider.compatibility?.thinkingMode || 'default' };
135
136
  updatedProvider.authMode = answers.authMode;
136
137
  updatedProvider.models = {
137
138
  ...(provider.models || {}),
package/src/config.js CHANGED
@@ -389,6 +389,9 @@ class ConfigManager {
389
389
  ? providerConfig.smallFastModel
390
390
  : (existing?.models?.smallFast ?? null);
391
391
 
392
+ newProvider.compatibility = providerConfig.compatibility !== undefined
393
+ ? providerConfig.compatibility
394
+ : (existing?.compatibility || { thinkingMode: 'default' });
392
395
  newProvider.authMode = authMode;
393
396
  newProvider.models = {
394
397
  primary: primaryModel,
@@ -22,6 +22,7 @@ async function writeFileAtomic(filePath, content, options = {}) {
22
22
  await handle.sync();
23
23
  await handle.close();
24
24
  handle = null;
25
+ if (options.beforeRename) await options.beforeRename();
25
26
  await nodeFs.promises.rename(tempPath, filePath);
26
27
  if (process.platform !== 'win32') {
27
28
  await nodeFs.promises.chmod(filePath, mode);
@@ -0,0 +1,14 @@
1
+ function thinkingModeQuestion(provider = {}) {
2
+ return {
3
+ type: 'list',
4
+ name: 'thinkingMode',
5
+ message: 'Thinking 兼容模式:',
6
+ choices: [
7
+ { name: '默认(保留客户端及 shell 设置)', value: 'default' },
8
+ { name: '禁用显式 thinking(用于不兼容的供应商)', value: 'disabled' }
9
+ ],
10
+ default: provider.compatibility?.thinkingMode || 'default'
11
+ };
12
+ }
13
+
14
+ module.exports = { thinkingModeQuestion };
@@ -1,7 +1,8 @@
1
1
  const fs = require('fs-extra');
2
2
  const path = require('path');
3
3
  const os = require('os');
4
- const { writeJsonAtomic } = require('./atomic-file');
4
+ const crypto = require('crypto');
5
+ const { writeFileAtomic, writeJsonAtomic } = require('./atomic-file');
5
6
 
6
7
  const CONFLICT_ENV_KEYS = [
7
8
  'ANTHROPIC_API_KEY',
@@ -48,7 +49,7 @@ function resolveCandidatePaths() {
48
49
  const homeDir = os.homedir();
49
50
  if (homeDir) {
50
51
  for (const fileName of SETTINGS_FILE_NAMES) {
51
- candidates.push(path.join(homeDir, '.claude', fileName));
52
+ candidates.push(path.join(process.env.CLAUDE_CONFIG_DIR || path.join(homeDir, '.claude'), fileName));
52
53
  }
53
54
  }
54
55
 
@@ -69,20 +70,21 @@ function resolveCandidatePaths() {
69
70
 
70
71
  async function loadSettingsFile() {
71
72
  const candidates = resolveCandidatePaths();
73
+ const files = [];
72
74
  for (const candidate of candidates) {
73
75
  try {
74
76
  if (await fs.pathExists(candidate)) {
75
77
  const data = await fs.readJson(candidate);
76
- return { path: candidate, data };
78
+ files.push({ path: candidate, data });
77
79
  }
78
80
  } catch (error) {
79
81
  throw new Error(`读取 Claude 设置文件失败 (${candidate}): ${error.message}`);
80
82
  }
81
83
  }
82
- return null;
84
+ return files;
83
85
  }
84
86
 
85
- function detectConflictKeys(settings) {
87
+ function detectConflictKeys(settings, provider) {
86
88
  if (!settings || typeof settings !== 'object') {
87
89
  return [];
88
90
  }
@@ -91,14 +93,17 @@ function detectConflictKeys(settings) {
91
93
  return [];
92
94
  }
93
95
 
94
- return CONFLICT_ENV_KEYS.filter(key => Object.prototype.hasOwnProperty.call(env, key));
96
+ const keys = provider?.compatibility?.thinkingMode === 'disabled'
97
+ ? [...CONFLICT_ENV_KEYS, 'MAX_THINKING_TOKENS']
98
+ : CONFLICT_ENV_KEYS;
99
+ return keys.filter(key => Object.prototype.hasOwnProperty.call(env, key));
95
100
  }
96
101
 
97
- async function backupSettingsFile(filePath) {
102
+ async function backupSettingsFile(filePath, snapshot = null) {
98
103
  const dir = path.dirname(filePath);
99
- const backupName = `settings.backup-${timestampSuffix()}.json`;
104
+ const backupName = `${path.basename(filePath, '.json')}.backup-${timestampSuffix()}-${crypto.randomUUID()}.json`;
100
105
  const backupPath = path.join(dir, backupName);
101
- await fs.copy(filePath, backupPath, { overwrite: false, errorOnExist: false });
106
+ await writeFileAtomic(backupPath, snapshot ?? await fs.readFile(filePath), { mode: 0o600 });
102
107
  return backupPath;
103
108
  }
104
109
 
@@ -124,27 +129,43 @@ async function saveSettingsFile(filePath, data) {
124
129
  await writeJsonAtomic(filePath, data, { spaces: 2, mode: 0o600 });
125
130
  }
126
131
 
127
- async function findSettingsConflict() {
128
- const fileInfo = await loadSettingsFile();
129
- if (!fileInfo) {
130
- return null;
131
- }
132
-
133
- const conflictKeys = detectConflictKeys(fileInfo.data);
134
- if (!conflictKeys.length) {
135
- return null;
132
+ async function applySettingsCleanup(conflict, provider) {
133
+ const { filePath } = conflict;
134
+ const snapshot = await fs.readFile(filePath);
135
+ const settings = JSON.parse(snapshot.toString('utf8').replace(/^\uFEFF/, ''));
136
+ const keys = detectConflictKeys(settings, provider);
137
+ if (keys.some(key => !conflict.keys.includes(key))) {
138
+ throw new Error(`Claude 设置新增了未经确认的冲突变量,请重新启动后再处理:${filePath}`);
136
139
  }
140
+ if (!keys.length) return null;
141
+
142
+ const backupPath = await backupSettingsFile(filePath, snapshot);
143
+ const updated = clearConflictKeys(settings, keys);
144
+ await writeFileAtomic(filePath, JSON.stringify(updated, null, 2), {
145
+ mode: 0o600,
146
+ beforeRename: async () => {
147
+ if (!snapshot.equals(await fs.readFile(filePath))) {
148
+ throw new Error(`Claude 设置在写入前发生变化,请重试:${filePath}`);
149
+ }
150
+ }
151
+ });
152
+ return backupPath;
153
+ }
137
154
 
138
- return {
155
+ async function findSettingsConflict(provider) {
156
+ const files = await loadSettingsFile();
157
+ const conflicts = files.map(fileInfo => ({
139
158
  filePath: fileInfo.path,
140
159
  settings: fileInfo.data,
141
- keys: conflictKeys
142
- };
160
+ keys: detectConflictKeys(fileInfo.data, provider)
161
+ })).filter(conflict => conflict.keys.length);
162
+ return conflicts.length ? { ...conflicts[0], conflicts } : null;
143
163
  }
144
164
 
145
165
  module.exports = {
146
166
  findSettingsConflict,
147
167
  backupSettingsFile,
148
168
  clearConflictKeys,
149
- saveSettingsFile
169
+ saveSettingsFile,
170
+ applySettingsCleanup
150
171
  };
@@ -43,6 +43,13 @@ function buildEnvVariables(config) {
43
43
  env.ANTHROPIC_SMALL_FAST_MODEL = sanitizeEnvValue(config.models.smallFast);
44
44
  }
45
45
 
46
+ const thinkingMode = config.compatibility?.thinkingMode || 'default';
47
+ if (!['default', 'disabled'].includes(thinkingMode)) {
48
+ throw new Error('不支持的 thinking 兼容模式');
49
+ }
50
+ // 仅修改子进程副本;default 保留用户显式设置的 shell 环境。
51
+ if (thinkingMode === 'disabled') env.MAX_THINKING_TOKENS = '0';
52
+
46
53
  return env;
47
54
  } catch (error) {
48
55
  throw new Error(`配置验证失败: ${error.message}\n请使用 'akm edit ${config.name}' 修复配置`);
@@ -32,6 +32,11 @@ const providerSchema = {
32
32
  smallFast: nullableString
33
33
  }
34
34
  },
35
+ compatibility: {
36
+ type: 'object',
37
+ additionalProperties: false,
38
+ properties: { thinkingMode: { enum: ['default', 'disabled'] } }
39
+ },
35
40
  launchArgs: launchArgsSchema,
36
41
  lastUsedArgs: {
37
42
  anyOf: [launchArgsSchema, { type: 'null' }]