a4phone 1.6.1 → 1.6.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "a4phone",
3
- "version": "1.6.1",
3
+ "version": "1.6.2",
4
4
  "description": "Claude Code / Codex / ZCode / DSH 远程手机交互:任务完成通知(含 AI 最后输出)、提问/权限请求手机点选或文字作答、远程续聊(ntfy.sh,含 DSH 会话续聊)",
5
5
  "type": "module",
6
6
  "bin": {
package/src/stop.mjs CHANGED
@@ -2,7 +2,7 @@
2
2
  import { loadConfig, saveLastSession } from './config.mjs';
3
3
  import { sendNotification } from './ntfy.mjs';
4
4
  import { systemNotify } from './notify.mjs';
5
- import { extractLastOutput } from './transcript.mjs';
5
+ import { resolveLastOutput } from './transcript.mjs';
6
6
 
7
7
  export async function handleStop(input, agentName) {
8
8
  const config = loadConfig();
@@ -21,7 +21,8 @@ export async function handleStop(input, agentName) {
21
21
  await systemNotify(agentName, '任务已完成');
22
22
 
23
23
  if (config.topic) {
24
- const lastOutput = extractLastOutput(input.transcript_path);
24
+ // ZCode 在 Stop 载荷直送 last_assistant_message,其余 agent 从 transcript 文件抽取
25
+ const lastOutput = resolveLastOutput(input);
25
26
  const message = lastOutput ? `${details}\n\nAI 最后输出:\n${lastOutput}` : details;
26
27
  await sendNotification({
27
28
  ...config,
@@ -3,10 +3,21 @@
3
3
  // Codex:JSONL,type="response_item" 且 payload.type="message" 的 assistant 消息——它随消息即时写入,
4
4
  // 而 task_complete 事件比消息晚 ~1.4s 才落盘,Codex 触发 Stop 时读取往往还没写入,
5
5
  // 所以必须先取 message 事件,task_complete 仅作文件稳定时的兜底。
6
+ // ZCode:hook 触发时把合成 transcript 写入临时文件,Stop 时只有一行伪 assistant 消息
7
+ // (结构与 Claude Code 相同但顶层无 type,role 在 message.role);该临时文件在
8
+ // hook 进程退出即被 ZCode 清理,且 Stop 载荷已直送 last_assistant_message,
9
+ // 因此优先走 resolveLastOutput,此解析仅作兜底。
6
10
  import fs from 'fs';
7
11
 
8
12
  const MAX_LENGTH = 1000; // 推送内容截断长度(中文约 3000 字节,留足 ntfy 4KB 上限余量)
9
13
 
14
+ // 截断/清洗:空文本返回 null(extractLastOutput 与直送字段共用同一上限)
15
+ export function clampOutput(text) {
16
+ const cleaned = (text || '').trim();
17
+ if (!cleaned) return null;
18
+ return cleaned.length > MAX_LENGTH ? cleaned.slice(0, MAX_LENGTH) + '...' : cleaned;
19
+ }
20
+
10
21
  export function extractLastOutput(transcriptPath) {
11
22
  if (!transcriptPath) return null;
12
23
  let text = '';
@@ -21,7 +32,8 @@ export function extractLastOutput(transcriptPath) {
21
32
  continue; // 非 JSON 行(如 Codex 的纯文本记录)跳过
22
33
  }
23
34
  // Claude Code:assistant 消息的 text 块(多轮时取最后一条)
24
- if (o?.type === 'assistant' && Array.isArray(o.message?.content)) {
35
+ // ZCode:合成的单行伪 transcript 结构相同,只是顶层没有 type,role 在 message.role
36
+ if ((o?.type === 'assistant' || o?.message?.role === 'assistant') && Array.isArray(o.message?.content)) {
25
37
  const parts = o.message.content
26
38
  .filter((b) => b?.type === 'text' && b.text)
27
39
  .map((b) => b.text);
@@ -42,7 +54,11 @@ export function extractLastOutput(transcriptPath) {
42
54
  } catch {
43
55
  return null;
44
56
  }
45
- const cleaned = (text || '').trim();
46
- if (!cleaned) return null;
47
- return cleaned.length > MAX_LENGTH ? cleaned.slice(0, MAX_LENGTH) + '...' : cleaned;
57
+ return clampOutput(text);
58
+ }
59
+
60
+ // Stop 载荷 → AI 最后输出:ZCode 在 stdin 直送 last_assistant_message(首选——免文件
61
+ // 解析,且其临时 transcript 在 hook 退出即被清理);其余 agent 无此字段,回退读 transcript 文件
62
+ export function resolveLastOutput({ last_assistant_message, transcript_path } = {}) {
63
+ return clampOutput(last_assistant_message) ?? extractLastOutput(transcript_path);
48
64
  }