a4phone 1.1.4 → 1.1.6
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 +109 -22
- package/bin/a4p.mjs +69 -6
- package/dsh/lib/index.js +183 -0
- package/dsh/lib/phone-hooks.mjs +151 -0
- package/dsh/package.json +23 -0
- package/package.json +7 -3
- package/src/asku.mjs +31 -8
- package/src/config.mjs +26 -7
- package/src/daemon.mjs +108 -0
- package/src/hook.mjs +10 -4
- package/src/listen.mjs +121 -0
- package/src/mode.mjs +4 -4
- package/src/ntfy.mjs +14 -3
- package/src/paths.mjs +58 -0
- package/src/resume.mjs +261 -0
- package/src/setup.mjs +264 -23
- package/src/stop.mjs +16 -3
- package/src/transcript.mjs +48 -0
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
// 从会话记录(transcript)抽取"AI 最后输出的一段话"
|
|
2
|
+
// Claude Code:JSONL,type="assistant" 的 message.content[] 中 type="text" 的 text 块
|
|
3
|
+
// Codex:JSONL,type="response_item" 且 payload.type="message" 的 assistant 消息——它随消息即时写入,
|
|
4
|
+
// 而 task_complete 事件比消息晚 ~1.4s 才落盘,Codex 触发 Stop 时读取往往还没写入,
|
|
5
|
+
// 所以必须先取 message 事件,task_complete 仅作文件稳定时的兜底。
|
|
6
|
+
import fs from 'fs';
|
|
7
|
+
|
|
8
|
+
const MAX_LENGTH = 1000; // 推送内容截断长度(中文约 3000 字节,留足 ntfy 4KB 上限余量)
|
|
9
|
+
|
|
10
|
+
export function extractLastOutput(transcriptPath) {
|
|
11
|
+
if (!transcriptPath) return null;
|
|
12
|
+
let text = '';
|
|
13
|
+
try {
|
|
14
|
+
const content = fs.readFileSync(transcriptPath, 'utf-8');
|
|
15
|
+
for (const line of content.split('\n')) {
|
|
16
|
+
if (!line.trim()) continue;
|
|
17
|
+
let o;
|
|
18
|
+
try {
|
|
19
|
+
o = JSON.parse(line);
|
|
20
|
+
} catch {
|
|
21
|
+
continue; // 非 JSON 行(如 Codex 的纯文本记录)跳过
|
|
22
|
+
}
|
|
23
|
+
// Claude Code:assistant 消息的 text 块(多轮时取最后一条)
|
|
24
|
+
if (o?.type === 'assistant' && Array.isArray(o.message?.content)) {
|
|
25
|
+
const parts = o.message.content
|
|
26
|
+
.filter((b) => b?.type === 'text' && b.text)
|
|
27
|
+
.map((b) => b.text);
|
|
28
|
+
if (parts.length) text = parts.join('\n');
|
|
29
|
+
}
|
|
30
|
+
// Codex:response_item 的 assistant 消息(随消息即时写入,Stop 时已可用)
|
|
31
|
+
if (o?.type === 'response_item' && o.payload?.type === 'message' && o.payload?.role === 'assistant') {
|
|
32
|
+
const parts = (o.payload.content || [])
|
|
33
|
+
.filter((b) => b?.type === 'output_text' && b.text)
|
|
34
|
+
.map((b) => b.text);
|
|
35
|
+
if (parts.length) text = parts.join('\n');
|
|
36
|
+
}
|
|
37
|
+
// Codex:task_complete 兜底(自带最后一条 agent 消息;比 message 晚 ~1.4s 写入,仅文件稳定时可用)
|
|
38
|
+
if (o?.type === 'event_msg' && o.payload?.type === 'task_complete' && o.payload?.last_agent_message) {
|
|
39
|
+
text = o.payload.last_agent_message;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
} catch {
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
const cleaned = (text || '').trim();
|
|
46
|
+
if (!cleaned) return null;
|
|
47
|
+
return cleaned.length > MAX_LENGTH ? cleaned.slice(0, MAX_LENGTH) + '...' : cleaned;
|
|
48
|
+
}
|