a4phone 1.1.0 → 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/bin/a4p.mjs CHANGED
@@ -35,8 +35,10 @@ if (cmd === 'setup') {
35
35
  } catch {
36
36
  process.exit(0);
37
37
  }
38
+ // 可选的 agent 标识:a4p hook [codex|claude],默认 claude
39
+ const agent = process.argv[3] === 'codex' ? 'codex' : 'claude';
38
40
  const { dispatchHook } = await import('../src/hook.mjs');
39
- const result = await dispatchHook(input);
41
+ const result = await dispatchHook(input, agent);
40
42
  if (result) {
41
43
  process.stdout.write(JSON.stringify(result) + '\n');
42
44
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "a4phone",
3
- "version": "1.1.0",
3
+ "version": "1.1.2",
4
4
  "description": "Claude Code 远程手机交互:任务完成通知、AskUserQuestion 提问与 PermissionRequest 权限请求手机点选(ntfy.sh)",
5
5
  "type": "module",
6
6
  "bin": {
package/src/asku.mjs CHANGED
@@ -3,7 +3,7 @@ import crypto from 'crypto';
3
3
  import { loadConfig } from './config.mjs';
4
4
  import { sendNotification, waitForResponse } from './ntfy.mjs';
5
5
 
6
- export async function handleAskUserQuestion(input) {
6
+ export async function handleAskUserQuestion(input, agentName) {
7
7
  const config = loadConfig();
8
8
  const questions = input.tool_input?.questions || [];
9
9
  if (!config.topic || !questions.length) return null;
@@ -25,7 +25,7 @@ export async function handleAskUserQuestion(input) {
25
25
  options.forEach((o, i) => lines.push(`${i + 1}. ${o.label}`));
26
26
  const sent = await sendNotification({
27
27
  ...config,
28
- title: `Claude Code: ${q.header || 'Question'}`,
28
+ title: `${agentName}: ${q.header || 'Question'}`,
29
29
  message: lines.join('\n'),
30
30
  actions,
31
31
  });
package/src/hook.mjs CHANGED
@@ -5,25 +5,31 @@ import { handleStop } from './stop.mjs';
5
5
  import { handleAskUserQuestion } from './asku.mjs';
6
6
  import { handlePermissionRequest } from './permission.mjs';
7
7
 
8
+ // 根据 agent 标识返回显示名称
9
+ export function agentName(agent) {
10
+ return agent === 'codex' ? 'Codex' : 'Claude Code';
11
+ }
12
+
8
13
  // 返回 hook 输出对象,或 null(null = 不干预,走终端默认流程)
9
- export async function dispatchHook(input) {
14
+ export async function dispatchHook(input, agent) {
10
15
  const event = input.hook_event_name;
11
16
  const isOut = readMode() === 'out';
17
+ const name = agentName(agent);
12
18
 
13
19
  if (event === 'Stop') {
14
- return handleStop(input);
20
+ return handleStop(input, name);
15
21
  }
16
22
 
17
23
  if (event === 'PreToolUse' && input.tool_name === 'AskUserQuestion') {
18
- await systemNotify('Claude Code', '有提问需要处理');
24
+ await systemNotify(name, '有提问需要处理');
19
25
  if (!isOut) return null; // 终端优先:不阻塞
20
- return handleAskUserQuestion(input);
26
+ return handleAskUserQuestion(input, name);
21
27
  }
22
28
 
23
29
  if (event === 'PermissionRequest') {
24
- await systemNotify('Claude Code', '有权限请求需要处理');
30
+ await systemNotify(name, '有权限请求需要处理');
25
31
  if (!isOut) return null; // 终端优先:不阻塞
26
- return handlePermissionRequest(input);
32
+ return handlePermissionRequest(input, name);
27
33
  }
28
34
 
29
35
  return null;
@@ -27,7 +27,7 @@ function formatMessage(tool_name, tool_input) {
27
27
  return message.length > MESSAGE_MAX_LENGTH ? message.slice(0, MESSAGE_MAX_LENGTH) + '...' : message;
28
28
  }
29
29
 
30
- export async function handlePermissionRequest(input) {
30
+ export async function handlePermissionRequest(input, agentName) {
31
31
  const config = loadConfig();
32
32
  if (!config.topic) return null;
33
33
 
@@ -43,7 +43,7 @@ export async function handlePermissionRequest(input) {
43
43
  actions.splice(1, 0, { action: 'http', label: 'Always Approve', url, method: 'POST', clear: false, body: JSON.stringify({ requestId, approved: true, alwaysAllow: true }) });
44
44
  }
45
45
 
46
- const sent = await sendNotification({ ...config, title: `Claude Code: ${tool_name}`, message, actions });
46
+ const sent = await sendNotification({ ...config, title: `${agentName}: ${tool_name}`, message, actions });
47
47
  if (!sent) return null;
48
48
 
49
49
  const isPlan = tool_name === 'ExitPlanMode';
package/src/setup.mjs CHANGED
@@ -13,12 +13,17 @@ function generateTopic() {
13
13
  }
14
14
 
15
15
  // 把 Hook 合并写入 settings.json(保留既有配置)
16
+ // 返回 true=新写入,false=已存在跳过
16
17
  export function registerHooks(settingsPath, hookCommand) {
17
18
  let settings = {};
18
19
  try {
19
20
  settings = JSON.parse(fs.readFileSync(settingsPath, 'utf-8'));
20
21
  } catch {}
21
22
 
23
+ // 已配置相同 Hook 则跳过
24
+ const existing = settings.hooks?.Stop?.[0]?.hooks?.[0]?.command;
25
+ if (existing === hookCommand) return false;
26
+
22
27
  settings.hooks = {
23
28
  Stop: [{ matcher: '*', hooks: [{ type: 'command', command: hookCommand }] }],
24
29
  PreToolUse: [{ matcher: 'AskUserQuestion', hooks: [{ type: 'command', command: hookCommand }] }],
@@ -26,6 +31,7 @@ export function registerHooks(settingsPath, hookCommand) {
26
31
  };
27
32
  fs.mkdirSync(path.dirname(settingsPath), { recursive: true });
28
33
  fs.writeFileSync(settingsPath, JSON.stringify(settings, null, 2));
34
+ return true;
29
35
  }
30
36
 
31
37
  export function unregisterHooks(settingsPath) {
@@ -48,22 +54,23 @@ const CODEX_MARKER_START = '# ===== a4phone hooks (auto-generated) =====';
48
54
  const CODEX_MARKER_END = '# ===== end a4phone hooks =====';
49
55
 
50
56
  // 生成 Codex 的 Hook 定义(数组嵌套表格,根级别)
57
+ // 命令带 codex 标识,便于 a4phone 动态显示触发方名称
51
58
  function codexHooksDefs() {
52
59
  return `[[hooks.Stop]]
53
60
  [[hooks.Stop.hooks]]
54
61
  type = "command"
55
- command = "a4p hook"
62
+ command = "a4p hook codex"
56
63
 
57
64
  [[hooks.PreToolUse]]
58
65
  matcher = "AskUserQuestion"
59
66
  [[hooks.PreToolUse.hooks]]
60
67
  type = "command"
61
- command = "a4p hook"
68
+ command = "a4p hook codex"
62
69
 
63
70
  [[hooks.PermissionRequest]]
64
71
  [[hooks.PermissionRequest.hooks]]
65
72
  type = "command"
66
- command = "a4p hook"`;
73
+ command = "a4p hook codex"`;
67
74
  }
68
75
 
69
76
  // 写入 Codex 配置(保留既有内容,幂等)
@@ -115,7 +122,7 @@ export async function runSetup({ generateQR }) {
115
122
  }
116
123
  saveConfig(config);
117
124
 
118
- registerHooks(SETTINGS_PATH, hookCommand());
125
+ const claudeConfigured = registerHooks(SETTINGS_PATH, hookCommand());
119
126
  const codexConfigured = configureCodex();
120
127
 
121
128
  const subscribeUrl = `${config.server}/${config.topic}`;
@@ -131,6 +138,7 @@ export async function runSetup({ generateQR }) {
131
138
  }
132
139
 
133
140
  process.stdout.write('\n在 ntfy App 添加订阅,输入话题名称或扫描上方二维码。\n');
141
+ process.stdout.write(`Claude Code 配置:${claudeConfigured ? '已自动写入 ~/.claude/settings.json' : '已存在,跳过'}\n`);
134
142
  process.stdout.write(`Codex 配置:${codexConfigured ? '已自动写入 ~/.codex/config.toml' : '已存在,跳过'}\n`);
135
143
  process.stdout.write('模式切换:a4p out(外出/手机优先) / a4p home(终端优先) / a4p status\n');
136
144
  process.stdout.write('重启 Claude Code / Codex 会话后 Hook 生效。\n');
package/src/stop.mjs CHANGED
@@ -3,16 +3,16 @@ import { loadConfig } from './config.mjs';
3
3
  import { sendNotification } from './ntfy.mjs';
4
4
  import { systemNotify } from './notify.mjs';
5
5
 
6
- export async function handleStop(input) {
6
+ export async function handleStop(input, agentName) {
7
7
  const config = loadConfig();
8
8
  const cwd = input.cwd || '未知目录';
9
9
  const session = input.session_id ? input.session_id.slice(0, 8) : '—';
10
10
  const details = `任务已完成\n目录: ${cwd}\n会话: ${session}`;
11
- await systemNotify('Claude Code', '任务已完成');
11
+ await systemNotify(agentName, '任务已完成');
12
12
  if (config.topic) {
13
13
  await sendNotification({
14
14
  ...config,
15
- title: 'Claude Code',
15
+ title: agentName,
16
16
  message: details,
17
17
  });
18
18
  }