a4phone 1.0.0 → 1.1.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/README.md CHANGED
@@ -49,9 +49,10 @@ a4p status # 查看当前模式
49
49
 
50
50
  ## Codex
51
51
 
52
- 使用 Codex 的读者在 `~/.codex/config.toml` 手动配置:
52
+ `a4p setup` 会同时自动配置 Claude Code 和 Codex。Codex 配置追加到 `~/.codex/config.toml` 末尾(保留原有设置),结构如下:
53
53
 
54
54
  ```toml
55
+ [features]
55
56
  hooks = true
56
57
 
57
58
  [[hooks.Stop]]
@@ -71,6 +72,8 @@ type = "command"
71
72
  command = "a4p hook"
72
73
  ```
73
74
 
75
+ > 注意:启用项必须放在 `[features]` 表内(`[features] hooks = true`),不能写成根级别的裸 `hooks = true`,否则与 `[[hooks.*]]` 冲突导致 TOML 解析错误。Codex 会话中需运行 `/hooks` 并手动信任新 Hook。
76
+
74
77
  ## 原理
75
78
 
76
79
  Claude Code Hook 拦截事件后,通过 ntfy.sh 推送带按钮的通知到手机;手机点击后,决策经响应话题回传并注入会话。
package/bin/a4p.mjs CHANGED
@@ -41,11 +41,12 @@ if (cmd === 'setup') {
41
41
  process.stdout.write(JSON.stringify(result) + '\n');
42
42
  }
43
43
  } else if (cmd === 'uninstall') {
44
- const { unregisterHooks } = await import('../src/setup.mjs');
44
+ const { unregisterHooks, unconfigureCodex } = await import('../src/setup.mjs');
45
45
  unregisterHooks(SETTINGS_PATH);
46
+ unconfigureCodex();
46
47
  try { fs.unlinkSync(path.join(os.homedir(), '.a4phone.json')); } catch {}
47
48
  try { fs.unlinkSync(path.join(os.homedir(), '.a4phone-mode.json')); } catch {}
48
- console.log('a4phone 已卸载:Hook 已移除,配置已删除。');
49
+ console.log('a4phone 已卸载:Claude Code / Codex Hook 已移除,配置已删除。');
49
50
  } else if (cmd === 'test') {
50
51
  const { loadConfig } = await import('../src/config.mjs');
51
52
  const { sendNotification } = await import('../src/ntfy.mjs');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "a4phone",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "description": "Claude Code 远程手机交互:任务完成通知、AskUserQuestion 提问与 PermissionRequest 权限请求手机点选(ntfy.sh)",
5
5
  "type": "module",
6
6
  "bin": {
package/src/setup.mjs CHANGED
@@ -42,6 +42,70 @@ function hookCommand() {
42
42
  return 'a4p hook';
43
43
  }
44
44
 
45
+ // Codex 配置路径与 a4phone 标记
46
+ const CODEX_PATH = path.join(os.homedir(), '.codex', 'config.toml');
47
+ const CODEX_MARKER_START = '# ===== a4phone hooks (auto-generated) =====';
48
+ const CODEX_MARKER_END = '# ===== end a4phone hooks =====';
49
+
50
+ // 生成 Codex 的 Hook 定义(数组嵌套表格,根级别)
51
+ function codexHooksDefs() {
52
+ return `[[hooks.Stop]]
53
+ [[hooks.Stop.hooks]]
54
+ type = "command"
55
+ command = "a4p hook"
56
+
57
+ [[hooks.PreToolUse]]
58
+ matcher = "AskUserQuestion"
59
+ [[hooks.PreToolUse.hooks]]
60
+ type = "command"
61
+ command = "a4p hook"
62
+
63
+ [[hooks.PermissionRequest]]
64
+ [[hooks.PermissionRequest.hooks]]
65
+ type = "command"
66
+ command = "a4p hook"`;
67
+ }
68
+
69
+ // 写入 Codex 配置(保留既有内容,幂等)
70
+ // 追加到文件末尾,用显式表头包裹,避免根级键被吸收
71
+ export function configureCodex() {
72
+ let content = '';
73
+ try {
74
+ content = fs.readFileSync(CODEX_PATH, 'utf-8');
75
+ } catch {}
76
+ if (content.includes(CODEX_MARKER_START)) return false; // 已配置
77
+
78
+ fs.mkdirSync(path.dirname(CODEX_PATH), { recursive: true });
79
+
80
+ const defs = `${CODEX_MARKER_START}\n${codexHooksDefs()}\n${CODEX_MARKER_END}`;
81
+
82
+ if (/^\[features\]\s*$/m.test(content)) {
83
+ // 已有 [features] 表:把 hooks = true 插入该表,末尾追加 Hook 定义
84
+ content = content.replace(/^\[features\]\s*$/m, '[features]\nhooks = true');
85
+ fs.writeFileSync(CODEX_PATH, content.trimEnd() + '\n\n' + defs + '\n');
86
+ } else {
87
+ // 无 [features]:末尾追加完整块([features] 显式表头 + Hook 定义)
88
+ const block = `${CODEX_MARKER_START}\n[features]\nhooks = true\n\n${codexHooksDefs()}\n${CODEX_MARKER_END}`;
89
+ fs.writeFileSync(CODEX_PATH, (content ? content.trimEnd() + '\n\n' : '') + block + '\n');
90
+ }
91
+ return true;
92
+ }
93
+
94
+ // 移除 Codex 配置中的 a4phone Hook 段
95
+ export function unconfigureCodex() {
96
+ let content = '';
97
+ try {
98
+ content = fs.readFileSync(CODEX_PATH, 'utf-8');
99
+ } catch {}
100
+ const start = content.indexOf(CODEX_MARKER_START);
101
+ const end = content.indexOf(CODEX_MARKER_END);
102
+ if (start !== -1 && end !== -1) {
103
+ content = content.slice(0, start) + content.slice(end + CODEX_MARKER_END.length);
104
+ content = content.replace(/\n{3,}/g, '\n\n').trim();
105
+ fs.writeFileSync(CODEX_PATH, content + '\n');
106
+ }
107
+ }
108
+
45
109
  // setup 主流程
46
110
  export async function runSetup({ generateQR }) {
47
111
  const config = loadConfig();
@@ -52,6 +116,7 @@ export async function runSetup({ generateQR }) {
52
116
  saveConfig(config);
53
117
 
54
118
  registerHooks(SETTINGS_PATH, hookCommand());
119
+ const codexConfigured = configureCodex();
55
120
 
56
121
  const subscribeUrl = `${config.server}/${config.topic}`;
57
122
  const ntfyUrl = `ntfy://${new URL(config.server).host}/${config.topic}`;
@@ -66,7 +131,8 @@ export async function runSetup({ generateQR }) {
66
131
  }
67
132
 
68
133
  process.stdout.write('\n在 ntfy App 添加订阅,输入话题名称或扫描上方二维码。\n');
134
+ process.stdout.write(`Codex 配置:${codexConfigured ? '已自动写入 ~/.codex/config.toml' : '已存在,跳过'}\n`);
69
135
  process.stdout.write('模式切换:a4p out(外出/手机优先) / a4p home(终端优先) / a4p status\n');
70
- process.stdout.write('重启 Claude Code 会话后 Hook 生效。\n');
136
+ process.stdout.write('重启 Claude Code / Codex 会话后 Hook 生效。\n');
71
137
  return config;
72
138
  }