a4phone 1.0.0 → 1.1.1
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 +4 -1
- package/bin/a4p.mjs +6 -3
- package/package.json +1 -1
- package/src/asku.mjs +2 -2
- package/src/hook.mjs +12 -6
- package/src/permission.mjs +2 -2
- package/src/setup.mjs +68 -1
- package/src/stop.mjs +3 -3
package/README.md
CHANGED
|
@@ -49,9 +49,10 @@ a4p status # 查看当前模式
|
|
|
49
49
|
|
|
50
50
|
## Codex
|
|
51
51
|
|
|
52
|
-
|
|
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
|
@@ -35,17 +35,20 @@ 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
|
}
|
|
43
45
|
} else if (cmd === 'uninstall') {
|
|
44
|
-
const { unregisterHooks } = await import('../src/setup.mjs');
|
|
46
|
+
const { unregisterHooks, unconfigureCodex } = await import('../src/setup.mjs');
|
|
45
47
|
unregisterHooks(SETTINGS_PATH);
|
|
48
|
+
unconfigureCodex();
|
|
46
49
|
try { fs.unlinkSync(path.join(os.homedir(), '.a4phone.json')); } catch {}
|
|
47
50
|
try { fs.unlinkSync(path.join(os.homedir(), '.a4phone-mode.json')); } catch {}
|
|
48
|
-
console.log('a4phone 已卸载:Hook 已移除,配置已删除。');
|
|
51
|
+
console.log('a4phone 已卸载:Claude Code / Codex Hook 已移除,配置已删除。');
|
|
49
52
|
} else if (cmd === 'test') {
|
|
50
53
|
const { loadConfig } = await import('../src/config.mjs');
|
|
51
54
|
const { sendNotification } = await import('../src/ntfy.mjs');
|
package/package.json
CHANGED
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:
|
|
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(
|
|
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(
|
|
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;
|
package/src/permission.mjs
CHANGED
|
@@ -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:
|
|
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
|
@@ -42,6 +42,71 @@ 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
|
+
// 命令带 codex 标识,便于 a4phone 动态显示触发方名称
|
|
52
|
+
function codexHooksDefs() {
|
|
53
|
+
return `[[hooks.Stop]]
|
|
54
|
+
[[hooks.Stop.hooks]]
|
|
55
|
+
type = "command"
|
|
56
|
+
command = "a4p hook codex"
|
|
57
|
+
|
|
58
|
+
[[hooks.PreToolUse]]
|
|
59
|
+
matcher = "AskUserQuestion"
|
|
60
|
+
[[hooks.PreToolUse.hooks]]
|
|
61
|
+
type = "command"
|
|
62
|
+
command = "a4p hook codex"
|
|
63
|
+
|
|
64
|
+
[[hooks.PermissionRequest]]
|
|
65
|
+
[[hooks.PermissionRequest.hooks]]
|
|
66
|
+
type = "command"
|
|
67
|
+
command = "a4p hook codex"`;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// 写入 Codex 配置(保留既有内容,幂等)
|
|
71
|
+
// 追加到文件末尾,用显式表头包裹,避免根级键被吸收
|
|
72
|
+
export function configureCodex() {
|
|
73
|
+
let content = '';
|
|
74
|
+
try {
|
|
75
|
+
content = fs.readFileSync(CODEX_PATH, 'utf-8');
|
|
76
|
+
} catch {}
|
|
77
|
+
if (content.includes(CODEX_MARKER_START)) return false; // 已配置
|
|
78
|
+
|
|
79
|
+
fs.mkdirSync(path.dirname(CODEX_PATH), { recursive: true });
|
|
80
|
+
|
|
81
|
+
const defs = `${CODEX_MARKER_START}\n${codexHooksDefs()}\n${CODEX_MARKER_END}`;
|
|
82
|
+
|
|
83
|
+
if (/^\[features\]\s*$/m.test(content)) {
|
|
84
|
+
// 已有 [features] 表:把 hooks = true 插入该表,末尾追加 Hook 定义
|
|
85
|
+
content = content.replace(/^\[features\]\s*$/m, '[features]\nhooks = true');
|
|
86
|
+
fs.writeFileSync(CODEX_PATH, content.trimEnd() + '\n\n' + defs + '\n');
|
|
87
|
+
} else {
|
|
88
|
+
// 无 [features]:末尾追加完整块([features] 显式表头 + Hook 定义)
|
|
89
|
+
const block = `${CODEX_MARKER_START}\n[features]\nhooks = true\n\n${codexHooksDefs()}\n${CODEX_MARKER_END}`;
|
|
90
|
+
fs.writeFileSync(CODEX_PATH, (content ? content.trimEnd() + '\n\n' : '') + block + '\n');
|
|
91
|
+
}
|
|
92
|
+
return true;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// 移除 Codex 配置中的 a4phone Hook 段
|
|
96
|
+
export function unconfigureCodex() {
|
|
97
|
+
let content = '';
|
|
98
|
+
try {
|
|
99
|
+
content = fs.readFileSync(CODEX_PATH, 'utf-8');
|
|
100
|
+
} catch {}
|
|
101
|
+
const start = content.indexOf(CODEX_MARKER_START);
|
|
102
|
+
const end = content.indexOf(CODEX_MARKER_END);
|
|
103
|
+
if (start !== -1 && end !== -1) {
|
|
104
|
+
content = content.slice(0, start) + content.slice(end + CODEX_MARKER_END.length);
|
|
105
|
+
content = content.replace(/\n{3,}/g, '\n\n').trim();
|
|
106
|
+
fs.writeFileSync(CODEX_PATH, content + '\n');
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
45
110
|
// setup 主流程
|
|
46
111
|
export async function runSetup({ generateQR }) {
|
|
47
112
|
const config = loadConfig();
|
|
@@ -52,6 +117,7 @@ export async function runSetup({ generateQR }) {
|
|
|
52
117
|
saveConfig(config);
|
|
53
118
|
|
|
54
119
|
registerHooks(SETTINGS_PATH, hookCommand());
|
|
120
|
+
const codexConfigured = configureCodex();
|
|
55
121
|
|
|
56
122
|
const subscribeUrl = `${config.server}/${config.topic}`;
|
|
57
123
|
const ntfyUrl = `ntfy://${new URL(config.server).host}/${config.topic}`;
|
|
@@ -66,7 +132,8 @@ export async function runSetup({ generateQR }) {
|
|
|
66
132
|
}
|
|
67
133
|
|
|
68
134
|
process.stdout.write('\n在 ntfy App 添加订阅,输入话题名称或扫描上方二维码。\n');
|
|
135
|
+
process.stdout.write(`Codex 配置:${codexConfigured ? '已自动写入 ~/.codex/config.toml' : '已存在,跳过'}\n`);
|
|
69
136
|
process.stdout.write('模式切换:a4p out(外出/手机优先) / a4p home(终端优先) / a4p status\n');
|
|
70
|
-
process.stdout.write('重启 Claude Code 会话后 Hook 生效。\n');
|
|
137
|
+
process.stdout.write('重启 Claude Code / Codex 会话后 Hook 生效。\n');
|
|
71
138
|
return config;
|
|
72
139
|
}
|
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(
|
|
11
|
+
await systemNotify(agentName, '任务已完成');
|
|
12
12
|
if (config.topic) {
|
|
13
13
|
await sendNotification({
|
|
14
14
|
...config,
|
|
15
|
-
title:
|
|
15
|
+
title: agentName,
|
|
16
16
|
message: details,
|
|
17
17
|
});
|
|
18
18
|
}
|