a4phone 1.2.1 → 1.3.0
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 +3 -1
- package/bin/a4p.mjs +15 -0
- package/package.json +1 -1
- package/src/config.mjs +3 -1
- package/src/listen.mjs +16 -0
- package/src/paths.mjs +2 -0
- package/src/update-check.mjs +169 -0
package/README.md
CHANGED
|
@@ -11,6 +11,7 @@ Claude Code / Codex / DSH(DeepSeek Harness)远程手机交互包。通过 [n
|
|
|
11
11
|
- **远程续聊**:守护进程监听主话题,手机发文字即可与当前会话交流,形成完整远程对话闭环(Claude Code / Codex / DSH)
|
|
12
12
|
- **DSH 远程续聊**:手机发文字直接注入 DSH 正在运行的会话(`agent.followup`),手机消息与 AI 回复**实时出现在桌面端会话里**,无需另起进程、无会话锁冲突
|
|
13
13
|
- **后台守护进程**:`a4p listen` 无窗口后台运行,日志写入文件
|
|
14
|
+
- **自动更新提醒**:守护进程周期检查 npm 新版本并**推送手机提醒**;其他命令在终端提示(默认开启,可在配置关闭)
|
|
14
15
|
- **双模式**:外出模式(手机优先)/ 终端优先模式,一键切换
|
|
15
16
|
- **零第三方依赖**:仅依赖 ntfy.sh 免费服务,无 Google 服务依赖
|
|
16
17
|
|
|
@@ -197,7 +198,8 @@ AI助手触发事件 → a4p hook → ntfy.sh 推送手机 → 手机点选/文
|
|
|
197
198
|
- **ntfy.sh 免费托管服务有发布速率/消息保留限制**:短时间高频测试可能触发 `limited` 提示,建议降低推送频率(续聊结果已去重推送,不再重复通知)
|
|
198
199
|
- ntfy.sh 公共话题可被知晓话题名的人读写,重要场景建议自建 ntfy 服务或使用访问令牌
|
|
199
200
|
- ntfy.sh 为国外服务,国内网络下长连接可能不稳定
|
|
200
|
-
-
|
|
201
|
+
- **自动更新提醒**:默认开启(`checkUpdates: true`)。守护进程(`a4p listen`)启动时及每 `updateIntervalHours` 小时(默认 6)检查一次 npm registry(npmmirror 优先、官方兜底),发现新版本推送手机提醒;其他命令(`a4p status`/`a4p resume` 等)在终端提示。同一新版本只提醒一次,查询失败静默跳过,不影响正常功能;可在 `~/.a4phone/config.json` 设 `checkUpdates: false` 关闭
|
|
202
|
+
- 配置存储于 `~/.a4phone/config.json`,最近会话存储于 `~/.a4phone/last.json`,模式存储于 `~/.a4phone/mode.json`,守护进程信息存储于 `~/.a4phone/daemon.json`,日志写入 `~/.a4phone/daemon.log`;DSH Hook 日志写入 `~/.a4phone/dsh-logs/`,DSH 续聊队列位于 `~/.a4phone/dsh-jobs/`,心跳位于 `~/.a4phone/dsh-heartbeat.json`,更新检查缓存位于 `~/.a4phone/update-cache.json`
|
|
201
203
|
|
|
202
204
|
## 开发
|
|
203
205
|
|
package/bin/a4p.mjs
CHANGED
|
@@ -40,6 +40,21 @@ function showHelp() {
|
|
|
40
40
|
}
|
|
41
41
|
|
|
42
42
|
const cmd = process.argv[2];
|
|
43
|
+
const sub = process.argv[3];
|
|
44
|
+
|
|
45
|
+
// ── 版本更新检查(默认开启)───────────────────────────────────────────
|
|
46
|
+
// 守护进程(listen --daemon-child)自己有周期检查 + 手机推送,这里跳过;
|
|
47
|
+
// hook 是 AI 每次事件都调用的热路径,跳过避免延迟;version/help 是脚本/查询场景跳过。
|
|
48
|
+
// 其余命令在终端提示新版本;限频与去重由 update-check 缓存保证(未到期零开销)。
|
|
49
|
+
if (cmd && !['hook', '--version', '-v', 'version', 'help', '--help', '-h'].includes(cmd)
|
|
50
|
+
&& !(cmd === 'listen' && sub === '--daemon-child')) {
|
|
51
|
+
const { checkForUpdate, makeConsoleNotifier } = await import('../src/update-check.mjs');
|
|
52
|
+
const { loadConfig } = await import('../src/config.mjs');
|
|
53
|
+
checkForUpdate({
|
|
54
|
+
config: loadConfig(),
|
|
55
|
+
notify: makeConsoleNotifier(),
|
|
56
|
+
}).catch(() => {});
|
|
57
|
+
}
|
|
43
58
|
|
|
44
59
|
if (cmd === '--version' || cmd === '-v' || cmd === 'version') {
|
|
45
60
|
const pkg = JSON.parse(fs.readFileSync(new URL('../package.json', import.meta.url), 'utf-8'));
|
package/package.json
CHANGED
package/src/config.mjs
CHANGED
|
@@ -15,9 +15,11 @@ export function loadConfig() {
|
|
|
15
15
|
timeout: cfg.timeout ?? 60,
|
|
16
16
|
planTimeout: cfg.planTimeout ?? 300,
|
|
17
17
|
resumeTimeout: cfg.resumeTimeout ?? 1800, // 续聊超时(秒),默认 30 分钟
|
|
18
|
+
checkUpdates: cfg.checkUpdates ?? true, // 版本更新检查(守护进程推手机 / 命令终端提示)
|
|
19
|
+
updateIntervalHours: cfg.updateIntervalHours ?? 6, // 检查间隔(小时)
|
|
18
20
|
};
|
|
19
21
|
} catch {
|
|
20
|
-
return { topic: '', server: 'https://ntfy.sh', timeout: 60, planTimeout: 300, resumeTimeout: 1800 };
|
|
22
|
+
return { topic: '', server: 'https://ntfy.sh', timeout: 60, planTimeout: 300, resumeTimeout: 1800, checkUpdates: true, updateIntervalHours: 6 };
|
|
21
23
|
}
|
|
22
24
|
}
|
|
23
25
|
|
package/src/listen.mjs
CHANGED
|
@@ -11,6 +11,7 @@ import path from 'path';
|
|
|
11
11
|
import { loadConfig, loadLastSession } from './config.mjs';
|
|
12
12
|
import { runResume } from './resume.mjs';
|
|
13
13
|
import { createBatcher } from './batcher.mjs';
|
|
14
|
+
import { checkForUpdate, makePhoneNotifier } from './update-check.mjs';
|
|
14
15
|
import { PENDING_PATH, ensureDir } from './paths.mjs';
|
|
15
16
|
|
|
16
17
|
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
@@ -105,6 +106,21 @@ export async function runListen({ onLog = (s) => console.log(s) } = {}) {
|
|
|
105
106
|
}
|
|
106
107
|
} catch {}
|
|
107
108
|
|
|
109
|
+
// ── 版本更新检查:启动时 + 周期性(默认 6 小时),发现新版本推送手机提醒 ──
|
|
110
|
+
// 限频与去重由 update-check 的缓存保证(同一新版本只提醒一次,未到期不访问网络)
|
|
111
|
+
const checkUpdate = async () => {
|
|
112
|
+
try {
|
|
113
|
+
await checkForUpdate({
|
|
114
|
+
config,
|
|
115
|
+
notify: makePhoneNotifier(config, { onLog }),
|
|
116
|
+
onLog,
|
|
117
|
+
});
|
|
118
|
+
} catch {}
|
|
119
|
+
};
|
|
120
|
+
await checkUpdate();
|
|
121
|
+
const intervalMs = (config.updateIntervalHours ?? 6) * 3600 * 1000;
|
|
122
|
+
setInterval(() => { checkUpdate().catch(() => {}); }, intervalMs).unref();
|
|
123
|
+
|
|
108
124
|
while (true) {
|
|
109
125
|
try {
|
|
110
126
|
const since = `since=${lastTime + 1}`; // 从最后收到的消息之后继续
|
package/src/paths.mjs
CHANGED
|
@@ -23,6 +23,8 @@ export const DSH_HEARTBEAT = path.join(A4P_DIR, 'dsh-heartbeat.json');
|
|
|
23
23
|
// 续聊积压批次持久化:长轮次期间到达的消息写入该文件,守护进程重启后恢复,
|
|
24
24
|
// 避免积压消息在重启时丢失
|
|
25
25
|
export const PENDING_PATH = path.join(A4P_DIR, 'pending-batch.json');
|
|
26
|
+
// 版本更新检查缓存:{ lastCheck, knownLatest },用于限频与"同一新版本只提醒一次"
|
|
27
|
+
export const UPDATE_CACHE_PATH = path.join(A4P_DIR, 'update-cache.json');
|
|
26
28
|
|
|
27
29
|
// 旧版散落在家目录的单文件 → 迁移到 ~/.a4phone/(新路径不存在时才移动)
|
|
28
30
|
const LEGACY_PATHS = {
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
// update-check.mjs — a4phone 版本更新检查
|
|
2
|
+
//
|
|
3
|
+
// 查询 npm registry 最新版本,与本地版本比对,发现新版本时提醒用户:
|
|
4
|
+
// - 守护进程(a4p listen):经 ntfy 推送手机提醒
|
|
5
|
+
// - 其他命令(a4p status/setup/resume 等):终端打印提示
|
|
6
|
+
// - 缓存(~/.a4phone/update-cache.json)保证同一新版本只提醒一次 + 限频
|
|
7
|
+
// (默认 6 小时查一次,避免每次命令都访问网络)
|
|
8
|
+
// - registry 查询失败静默跳过(国内网络可能连不上,不影响正常功能)
|
|
9
|
+
//
|
|
10
|
+
// 纯函数(版本比对、缓存、到期判断)便于单元测试。
|
|
11
|
+
import fs from 'fs';
|
|
12
|
+
import { UPDATE_CACHE_PATH, ensureDir } from './paths.mjs';
|
|
13
|
+
import { sendNotification } from './ntfy.mjs';
|
|
14
|
+
|
|
15
|
+
/** 默认检查间隔(小时):守护进程周期检查与命令级限频共用 */
|
|
16
|
+
export const DEFAULT_INTERVAL_HOURS = 6;
|
|
17
|
+
|
|
18
|
+
/** 查询源:国内镜像优先(与用户 npm 配置一致,国内网络更稳),官方兜底 */
|
|
19
|
+
const REGISTRIES = [
|
|
20
|
+
'https://registry.npmmirror.com',
|
|
21
|
+
'https://registry.npmjs.org',
|
|
22
|
+
];
|
|
23
|
+
|
|
24
|
+
/** 读取本地包版本号;读不到返回 '0.0.0' */
|
|
25
|
+
export function getLocalVersion() {
|
|
26
|
+
try {
|
|
27
|
+
const pkg = JSON.parse(fs.readFileSync(new URL('../package.json', import.meta.url), 'utf-8'));
|
|
28
|
+
return typeof pkg.version === 'string' ? pkg.version : '0.0.0';
|
|
29
|
+
} catch {
|
|
30
|
+
return '0.0.0';
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* 简单 semver 数值比较(无第三方依赖):
|
|
36
|
+
* compareVersions('1.2.10', '1.2.9') > 0
|
|
37
|
+
* @param {string} a
|
|
38
|
+
* @param {string} b
|
|
39
|
+
* @returns {number} 1 | 0 | -1
|
|
40
|
+
*/
|
|
41
|
+
export function compareVersions(a, b) {
|
|
42
|
+
const pa = String(a).split('.').map((n) => Number(n) || 0);
|
|
43
|
+
const pb = String(b).split('.').map((n) => Number(n) || 0);
|
|
44
|
+
for (let i = 0; i < Math.max(pa.length, pb.length); i++) {
|
|
45
|
+
const da = pa[i] || 0;
|
|
46
|
+
const db = pb[i] || 0;
|
|
47
|
+
if (da !== db) return da > db ? 1 : -1;
|
|
48
|
+
}
|
|
49
|
+
return 0;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** 读取更新检查缓存;无/损坏返回空对象 */
|
|
53
|
+
export function loadCache(cachePath = UPDATE_CACHE_PATH) {
|
|
54
|
+
try {
|
|
55
|
+
const cache = JSON.parse(fs.readFileSync(cachePath, 'utf-8'));
|
|
56
|
+
return cache && typeof cache === 'object' ? cache : {};
|
|
57
|
+
} catch {
|
|
58
|
+
return {};
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** 写缓存(原子写) */
|
|
63
|
+
export function saveCache(cache, cachePath = UPDATE_CACHE_PATH) {
|
|
64
|
+
try {
|
|
65
|
+
ensureDir();
|
|
66
|
+
const tmp = `${cachePath}.${process.pid}.${Date.now()}.tmp`;
|
|
67
|
+
fs.writeFileSync(tmp, JSON.stringify(cache));
|
|
68
|
+
fs.renameSync(tmp, cachePath);
|
|
69
|
+
} catch {}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* 查询 npm registry 最新版本号;全部源失败返回 null。
|
|
74
|
+
* @param {{timeoutMs?: number}} [opts]
|
|
75
|
+
* @returns {Promise<string|null>}
|
|
76
|
+
*/
|
|
77
|
+
export async function fetchLatestVersion({ timeoutMs = 8000 } = {}) {
|
|
78
|
+
for (const base of REGISTRIES) {
|
|
79
|
+
try {
|
|
80
|
+
const controller = new AbortController();
|
|
81
|
+
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
82
|
+
const res = await fetch(`${base}/a4phone/latest`, { signal: controller.signal });
|
|
83
|
+
clearTimeout(timer);
|
|
84
|
+
if (!res.ok) continue;
|
|
85
|
+
const data = await res.json();
|
|
86
|
+
if (typeof data?.version === 'string' && data.version) return data.version;
|
|
87
|
+
} catch {}
|
|
88
|
+
}
|
|
89
|
+
return null;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* 检查一次更新(含限频与提醒去重)。未到期/未开启/查询失败均静默跳过。
|
|
94
|
+
* @param {object} [opts]
|
|
95
|
+
* @param {object} [opts.config] a4phone 配置(checkUpdates=false 则跳过;updateIntervalHours 调整间隔)
|
|
96
|
+
* @param {(latest:string)=>Promise<void>|void} [opts.notify] 发现新版本且未提醒过时的回调(推送/终端提示)
|
|
97
|
+
* @param {(msg:string)=>void} [opts.onLog]
|
|
98
|
+
* @param {number} [opts.now] 注入当前时间(测试用)
|
|
99
|
+
* @param {string} [opts.cachePath] 注入缓存路径(测试用)
|
|
100
|
+
* @param {number} [opts.minIntervalMs] 最小检查间隔(默认 updateIntervalHours 或 6 小时)
|
|
101
|
+
* @param {string} [opts.localVersion] 注入本地版本(测试用)
|
|
102
|
+
* @param {string|null} [opts.latestVersion] 注入最新版本(测试用;undefined 则实际查询,null 视为查询失败)
|
|
103
|
+
* @returns {Promise<{checked:boolean, hasUpdate:boolean, local:string, latest:string|null, notified:boolean}>}
|
|
104
|
+
*/
|
|
105
|
+
export async function checkForUpdate({
|
|
106
|
+
config = null,
|
|
107
|
+
notify = null,
|
|
108
|
+
onLog = () => {},
|
|
109
|
+
now = Date.now(),
|
|
110
|
+
cachePath = UPDATE_CACHE_PATH,
|
|
111
|
+
minIntervalMs = null,
|
|
112
|
+
localVersion = getLocalVersion(),
|
|
113
|
+
latestVersion = undefined,
|
|
114
|
+
} = {}) {
|
|
115
|
+
const cfg = config || {};
|
|
116
|
+
if (cfg.checkUpdates === false) {
|
|
117
|
+
return { checked: false, hasUpdate: false, local: localVersion, latest: null, notified: false };
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const cache = loadCache(cachePath);
|
|
121
|
+
const intervalMs = minIntervalMs ?? (cfg.updateIntervalHours ?? DEFAULT_INTERVAL_HOURS) * 3600 * 1000;
|
|
122
|
+
if (now - (cache.lastCheck || 0) < intervalMs) {
|
|
123
|
+
// 未到期:限频跳过(不访问网络)
|
|
124
|
+
return { checked: false, hasUpdate: false, local: localVersion, latest: cache.knownLatest || null, notified: false };
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const latest = latestVersion !== undefined ? latestVersion : await fetchLatestVersion();
|
|
128
|
+
if (!latest) {
|
|
129
|
+
// 查询失败:静默跳过(不记录 lastCheck,下次再试)
|
|
130
|
+
return { checked: true, hasUpdate: false, local: localVersion, latest: null, notified: false };
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const hasUpdate = compareVersions(latest, localVersion) > 0;
|
|
134
|
+
const isNewVersion = hasUpdate && cache.knownLatest !== latest;
|
|
135
|
+
|
|
136
|
+
if (isNewVersion && notify) {
|
|
137
|
+
try {
|
|
138
|
+
await notify(latest);
|
|
139
|
+
} catch (error) {
|
|
140
|
+
onLog(`更新提醒发送失败(将在下次检查重试): ${String(error?.message ?? error)}`);
|
|
141
|
+
// 提醒失败:不更新 knownLatest,下次检查会重试;只记录检查时间
|
|
142
|
+
saveCache({ lastCheck: now, knownLatest: cache.knownLatest }, cachePath);
|
|
143
|
+
return { checked: true, hasUpdate, local: localVersion, latest, notified: false };
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
saveCache({ lastCheck: now, knownLatest: hasUpdate ? latest : cache.knownLatest }, cachePath);
|
|
148
|
+
return { checked: true, hasUpdate, local: localVersion, latest, notified: isNewVersion };
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** 守护进程专用:发现新版本 → 推送手机提醒 */
|
|
152
|
+
export function makePhoneNotifier(config, { localVersion = getLocalVersion(), onLog = () => {} } = {}) {
|
|
153
|
+
return async (latest) => {
|
|
154
|
+
onLog(`检测到新版本 ${latest}(当前 ${localVersion}),已推送手机提醒`);
|
|
155
|
+
const ok = await sendNotification({
|
|
156
|
+
...config,
|
|
157
|
+
title: 'a4phone',
|
|
158
|
+
message: `检测到新版本 ${latest}(当前 ${localVersion})\n\n请运行:npm install -g a4phone 升级`,
|
|
159
|
+
});
|
|
160
|
+
if (!ok) throw new Error('手机推送失败');
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/** 命令级专用:发现新版本 → 终端打印提示 */
|
|
165
|
+
export function makeConsoleNotifier({ localVersion = getLocalVersion() } = {}) {
|
|
166
|
+
return async (latest) => {
|
|
167
|
+
console.log(`\n[提醒] a4phone 有新版本 ${latest}(当前 ${localVersion}),可运行 npm install -g a4phone 升级。\n`);
|
|
168
|
+
};
|
|
169
|
+
}
|