a4phone 1.4.0 → 1.4.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.
@@ -106,6 +106,8 @@ export async function handleAskUserQuestion(exec) {
106
106
  ? `(也可直接向话题 ${cfg.topic}-response 发送文字作答)`
107
107
  : `(选项较多,请回复编号如「3」,或直接向话题 ${cfg.topic}-response 发送文字作答)`
108
108
  );
109
+ // 浏览器兜底:未订阅响应话题的用户可打开网页版直接回复(自由作答/编号回复都需要)
110
+ lines.push(`(如未订阅响应话题,可浏览器打开 ${cfg.server}/${cfg.topic}-response 直接回复)`);
109
111
  if (!useActions && options.length > 3) {
110
112
  process.stderr.write(`[dsh-hook] 选项 ${options.length} 个超 ntfy 按钮上限(3),已降级为文字作答\n`);
111
113
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "a4phone",
3
- "version": "1.4.0",
3
+ "version": "1.4.2",
4
4
  "description": "Claude Code / Codex / DSH 远程手机交互:任务完成通知(含 AI 最后输出)、提问/权限请求手机点选或文字作答、远程续聊(ntfy.sh,含 DSH 会话续聊)",
5
5
  "type": "module",
6
6
  "bin": {
package/src/asku.mjs CHANGED
@@ -61,6 +61,8 @@ export async function handleAskUserQuestion(input, agentName, agent = 'claude')
61
61
  ? `(也可直接向话题 ${config.topic}-response 发送文字作答)`
62
62
  : `(选项较多,请回复编号如「3」,或直接向话题 ${config.topic}-response 发送文字作答)`
63
63
  );
64
+ // 浏览器兜底:未订阅响应话题的用户可打开网页版直接回复(自由作答/编号回复都需要)
65
+ lines.push(`(如未订阅响应话题,可浏览器打开 ${config.server}/${config.topic}-response 直接回复)`);
64
66
  const sent = await sendNotification({
65
67
  ...config,
66
68
  title: `${agentName}: ${q.header || 'Question'}`,
package/src/listen.mjs CHANGED
@@ -12,6 +12,7 @@ import { loadConfig, loadLastSession } from './config.mjs';
12
12
  import { runResume } from './resume.mjs';
13
13
  import { createBatcher } from './batcher.mjs';
14
14
  import { checkForUpdate, makePhoneNotifier } from './update-check.mjs';
15
+ import { parseNtfyMessage } from './ntfy.mjs';
15
16
  import { PENDING_PATH, ensureDir } from './paths.mjs';
16
17
 
17
18
  const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
@@ -150,12 +151,8 @@ export async function runListen({ onLog = (s) => console.log(s) } = {}) {
150
151
  if (event.time) lastTime = Math.max(lastTime, Number(event.time));
151
152
  // 有 title 的是系统推送通知(Claude Code 回复),不是手机用户消息,跳过
152
153
  if (event.title) continue;
153
- let msg;
154
- try {
155
- msg = JSON.parse(event.message); // 系统推送为 JSON,忽略
156
- } catch {
157
- msg = event.message; // 纯文本:手机发来的续聊内容
158
- }
154
+ // 系统推送为 JSON 对象则忽略;纯文本/JSON 原始值(如手机发的单数字"3")作为续聊内容
155
+ const msg = parseNtfyMessage(event.message);
159
156
  if (typeof msg === 'string' && msg.trim()) {
160
157
  batcher.submit(msg.trim(), event.time ? Number(event.time) : null);
161
158
  persistSnapshot();
package/src/ntfy.mjs CHANGED
@@ -1,4 +1,23 @@
1
1
  // ntfy.sh 推送与响应订阅
2
+
3
+ /**
4
+ * 解析 ntfy 消息体:
5
+ * - JSON 对象(如按钮回执 { requestId, answer })→ 原样返回对象
6
+ * - JSON 原始值("4" / "true" / "null" 等合法 JSON 字面量)→ 按文本返回
7
+ * (修复:JSON.parse('4') 会返回数字 4,导致纯数字自由文本被误判为 JSON 对象而丢失)
8
+ * - 非 JSON 文本 → 原样返回
9
+ * @param {string} raw ntfy 事件的 message 字段
10
+ * @returns {object|string}
11
+ */
12
+ export function parseNtfyMessage(raw) {
13
+ try {
14
+ const parsed = JSON.parse(raw);
15
+ return parsed !== null && typeof parsed === 'object' ? parsed : raw;
16
+ } catch {
17
+ return raw;
18
+ }
19
+ }
20
+
2
21
  export async function sendNotification({ server, topic, title, message, actions }) {
3
22
  try {
4
23
  const res = await fetch(server, {
@@ -37,12 +56,8 @@ export async function waitForResponse({ server, topic, requestId, timeout }) {
37
56
  // ntfy /json 连接建立后会先发 open 事件、之后周期发 keepalive,
38
57
  // 这些事件没有 message 字段,跳过以免被误判为“手机自由文本”立即返回。
39
58
  if (event.event !== 'message' || event.message == null) continue;
40
- let msg;
41
- try {
42
- msg = JSON.parse(event.message);
43
- } catch {
44
- msg = { answer: event.message }; // 纯文本:手机自由作答
45
- }
59
+ const parsed = parseNtfyMessage(event.message);
60
+ const msg = typeof parsed === 'object' ? parsed : { answer: parsed };
46
61
  // action 回执:requestId 必须匹配;自由文本:无 requestId 且有 answer 才接受
47
62
  if (msg.requestId === requestId || (msg.requestId === undefined && msg.answer)) {
48
63
  clearTimeout(timer);
@@ -119,7 +119,10 @@ export async function checkForUpdate({
119
119
 
120
120
  const cache = loadCache(cachePath);
121
121
  const intervalMs = minIntervalMs ?? (cfg.updateIntervalHours ?? DEFAULT_INTERVAL_HOURS) * 3600 * 1000;
122
- if (now - (cache.lastCheck || 0) < intervalMs) {
122
+ // 本地版本升级后忽略限频:旧版本时代的 lastCheck 不应推迟新版本的首次检查
123
+ // (否则用户升级后要等一整个间隔才能收到新版本提醒)
124
+ const versionChanged = cache.version !== localVersion;
125
+ if (!versionChanged && now - (cache.lastCheck || 0) < intervalMs) {
123
126
  // 未到期:限频跳过(不访问网络)
124
127
  return { checked: false, hasUpdate: false, local: localVersion, latest: cache.knownLatest || null, notified: false };
125
128
  }
@@ -139,12 +142,12 @@ export async function checkForUpdate({
139
142
  } catch (error) {
140
143
  onLog(`更新提醒发送失败(将在下次检查重试): ${String(error?.message ?? error)}`);
141
144
  // 提醒失败:不更新 knownLatest,下次检查会重试;只记录检查时间
142
- saveCache({ lastCheck: now, knownLatest: cache.knownLatest }, cachePath);
145
+ saveCache({ lastCheck: now, knownLatest: cache.knownLatest, version: localVersion }, cachePath);
143
146
  return { checked: true, hasUpdate, local: localVersion, latest, notified: false };
144
147
  }
145
148
  }
146
149
 
147
- saveCache({ lastCheck: now, knownLatest: hasUpdate ? latest : cache.knownLatest }, cachePath);
150
+ saveCache({ lastCheck: now, knownLatest: hasUpdate ? latest : cache.knownLatest, version: localVersion }, cachePath);
148
151
  return { checked: true, hasUpdate, local: localVersion, latest, notified: isNewVersion };
149
152
  }
150
153