@duanz/pi-pi-web-patch-keeper 0.0.0-stage → 1.0.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.en.md ADDED
@@ -0,0 +1,65 @@
1
+ # pi-web-patch-keeper
2
+
3
+ <p align="center"><strong>English</strong> | <a href="./README.md">中文</a></p>
4
+
5
+ <p align="center"><img src="https://raw.githubusercontent.com/DuanZGit/D-Hub/master/docs/covers/pi-web-patch-keeper.png" alt="pi-web-patch-keeper cover" width="880"></p>
6
+
7
+ <p align="center"><strong>Local pi-web patches survive upgrades by replaying themselves on load</strong><br><sub>Checked on load · idempotent · rewrites only what the upgrade wiped</sub></p>
8
+
9
+ <p align="center">
10
+ <a href="https://www.npmjs.com/package/@duanz/pi-pi-web-patch-keeper"><img src="https://img.shields.io/npm/v/@duanz%2Fpi-pi-web-patch-keeper.svg?style=flat-square" alt="npm"></a>
11
+ <a href="https://github.com/DuanZGit/D-Hub/blob/master/LICENSE"><img src="https://img.shields.io/npm/l/@duanz%2Fpi-pi-web-patch-keeper.svg?style=flat-square" alt="license"></a>
12
+ <a href="https://github.com/DuanZGit/D-Hub"><img src="https://img.shields.io/badge/GitHub-DuanZGit%2FD--Hub-181717?style=flat-square&logo=github" alt="GitHub"></a>
13
+ <img src="https://img.shields.io/badge/pi-extension-34d399?style=flat-square" alt="pi extension">
14
+ </p>
15
+
16
+ <p align="center">
17
+ <code>pi install npm:@duanz/pi-pi-web-patch-keeper</code>
18
+ </p>
19
+
20
+ ## Why
21
+
22
+ Local pi-web changes (project and session names in completion notifications, UI copy) can only live in build artifacts: `.next/server/chunks`, `.next/static`, `public/sw.js`. An `npm update` or reinstall replaces those files wholesale, the patches vanish, and nothing warns you.
23
+
24
+ ## What it does
25
+
26
+ One thing: run `apply.py` when the extension loads.
27
+
28
+ - All changes are registered in `~/.pi/agent/.cn-patches/patches.json`
29
+ - `apply.py` replays them and is **idempotent**: applied patches are skipped; only ones wiped by an upgrade are written back
30
+ - When a replay happens, the next `agent_start` reminds you to restart pi-web
31
+
32
+ ## Usage
33
+
34
+ 1. Create the patch directory:
35
+
36
+ ```
37
+ ~/.pi/agent/.cn-patches/
38
+ ├── patches.json # patch registry
39
+ └── apply.py # replay script
40
+ ```
41
+
42
+ 2. Install the extension and restart pi-web.
43
+
44
+ The extension does not know how patches are written — only when to run them.
45
+
46
+ ## Configuration
47
+
48
+ | Variable | Default | Description |
49
+ | --- | --- | --- |
50
+ | `PI_WEB_PATCH_OFF` | empty | Set to `1` to disable |
51
+ | `PI_WEB_PATCH_INTERVAL_H` | `6` | Minimum hours between checks |
52
+
53
+ ## State and logs
54
+
55
+ - `~/.pi/agent/.cn-patches/keeper-state.json` — last check time
56
+ - `~/.pi/agent/.cn-patches/keeper.log` — each run and what it replayed
57
+
58
+ ## Limits
59
+
60
+ - You supply `patches.json` and `apply.py`; this is the shell, not the patches.
61
+ - A replay still needs a pi-web restart to take effect (the extension only reminds).
62
+
63
+ ## License
64
+
65
+ MIT © DuanZ
package/README.md CHANGED
@@ -1,3 +1,65 @@
1
- # Temporary Holding Version
1
+ # pi-web-patch-keeper
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ <p align="center"><a href="./README.en.md">English</a> | <strong>中文</strong></p>
4
+
5
+ <p align="center"><img src="https://raw.githubusercontent.com/DuanZGit/D-Hub/master/docs/covers/pi-web-patch-keeper.png" alt="pi-web-patch-keeper cover" width="880"></p>
6
+
7
+ <p align="center"><strong>pi-web 升级会整包覆盖,本地补丁在下次加载时自动重放</strong><br><sub>加载即检查 · 幂等 · 被冲掉才写回</sub></p>
8
+
9
+ <p align="center">
10
+ <a href="https://www.npmjs.com/package/@duanz/pi-pi-web-patch-keeper"><img src="https://img.shields.io/npm/v/@duanz%2Fpi-pi-web-patch-keeper.svg?style=flat-square" alt="npm"></a>
11
+ <a href="https://github.com/DuanZGit/D-Hub/blob/master/LICENSE"><img src="https://img.shields.io/npm/l/@duanz%2Fpi-pi-web-patch-keeper.svg?style=flat-square" alt="license"></a>
12
+ <a href="https://github.com/DuanZGit/D-Hub"><img src="https://img.shields.io/badge/GitHub-DuanZGit%2FD--Hub-181717?style=flat-square&logo=github" alt="GitHub"></a>
13
+ <img src="https://img.shields.io/badge/pi-extension-34d399?style=flat-square" alt="pi extension">
14
+ </p>
15
+
16
+ <p align="center">
17
+ <code>pi install npm:@duanz/pi-pi-web-patch-keeper</code>
18
+ </p>
19
+
20
+ ## 它解决什么
21
+
22
+ pi-web 的本地改动(完成通知显示项目名与会话名、界面文案等)只能改在编译产物里:`.next/server/chunks`、`.next/static`、`public/sw.js`。`npm update` 或重新安装会把这些文件整包覆盖,补丁全丢,而且没人提醒。
23
+
24
+ ## 它做什么
25
+
26
+ 只做一件事:扩展加载时跑一次 `apply.py`。
27
+
28
+ - 所有改动登记在 `~/.pi/agent/.cn-patches/patches.json`
29
+ - `apply.py` 负责重放,**幂等**:已应用的跳过,只有被升级冲掉的才写回
30
+ - 有重放发生时,在下一次 `agent_start` 提醒你重启 pi-web
31
+
32
+ ## 用法
33
+
34
+ 1. 建立补丁目录:
35
+
36
+ ```
37
+ ~/.pi/agent/.cn-patches/
38
+ ├── patches.json # 补丁登记表
39
+ └── apply.py # 重放脚本
40
+ ```
41
+
42
+ 2. 装扩展,重启 pi-web。
43
+
44
+ 扩展本身不判断补丁怎么写,只决定**什么时候跑**。
45
+
46
+ ## 配置
47
+
48
+ | 变量 | 默认 | 说明 |
49
+ | --- | --- | --- |
50
+ | `PI_WEB_PATCH_OFF` | 空 | 设为 `1` 关闭 |
51
+ | `PI_WEB_PATCH_INTERVAL_H` | `6` | 两次检查的最小间隔(小时) |
52
+
53
+ ## 状态与日志
54
+
55
+ - `~/.pi/agent/.cn-patches/keeper-state.json`:上次检查时间
56
+ - `~/.pi/agent/.cn-patches/keeper.log`:每次运行、重放了哪些补丁
57
+
58
+ ## 限制
59
+
60
+ - 需要你自己准备 `patches.json` 与 `apply.py`;这是通用外壳,不含具体补丁内容。
61
+ - 有重放后仍需重启 pi-web 才生效(扩展只提醒,不重启)。
62
+
63
+ ## License
64
+
65
+ MIT © DuanZ
package/index.ts ADDED
@@ -0,0 +1,93 @@
1
+ /**
2
+ * pi-web-patch-keeper —— pi-web 被升级冲掉的本地补丁,自动重放。
3
+ *
4
+ * 背景:pi-web 的「完成通知显示项目名 + 会话名」等改动只能改在编译产物里
5
+ * (.next/server/chunks、.next/static、public/sw.js),npm 升级会整包覆盖。
6
+ * 所有改动登记在 ~/.pi/agent/.cn-patches/patches.json,由 apply.py 重放。
7
+ *
8
+ * 本扩展只做一件事:扩展加载时跑一次 apply.py。它幂等——已应用的跳过,
9
+ * 只有被升级冲掉的才会被写回。有重放就在下个 agent_start 提醒你重启 pi-web。
10
+ *
11
+ * 环境变量:
12
+ * PI_WEB_PATCH_OFF=1 关闭
13
+ * PI_WEB_PATCH_INTERVAL_H 两次检查最小间隔小时数(默认 6)
14
+ *
15
+ * 日志:~/.pi/agent/.cn-patches/keeper.log
16
+ */
17
+
18
+ import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
19
+ import { execFile } from "node:child_process";
20
+ import { appendFileSync, existsSync, readFileSync, writeFileSync } from "node:fs";
21
+ import { homedir } from "node:os";
22
+ import { join } from "node:path";
23
+
24
+ const PATCH_DIR = join(homedir(), ".pi", "agent", ".cn-patches");
25
+ const APPLY = join(PATCH_DIR, "apply.py");
26
+ const STATE_FILE = join(PATCH_DIR, "keeper-state.json");
27
+ const LOG_FILE = join(PATCH_DIR, "keeper.log");
28
+
29
+ const INTERVAL_MS = Math.max(0, Number(process.env.PI_WEB_PATCH_INTERVAL_H ?? 6)) * 3600_000;
30
+
31
+ let pendingNote: string | null = null;
32
+
33
+ function log(line: string) {
34
+ try {
35
+ appendFileSync(LOG_FILE, `${new Date().toISOString()} ${line}\n`);
36
+ } catch {
37
+ // 日志写不进去不影响主流程
38
+ }
39
+ }
40
+
41
+ function runApply(): Promise<string> {
42
+ return new Promise((resolve, reject) => {
43
+ execFile("python3", [APPLY], { timeout: 60_000 }, (err, stdout, stderr) => {
44
+ if (err) reject(new Error(`${err.message} ${stderr}`.trim()));
45
+ else resolve(String(stdout));
46
+ });
47
+ });
48
+ }
49
+
50
+ function throttleElapsed(): boolean {
51
+ try {
52
+ const s = JSON.parse(readFileSync(STATE_FILE, "utf8"));
53
+ return Date.now() - Number(s.at ?? 0) >= INTERVAL_MS;
54
+ } catch {
55
+ return true;
56
+ }
57
+ }
58
+
59
+ async function checkAndReplay() {
60
+ if (process.env.PI_WEB_PATCH_OFF === "1") return;
61
+ if (!existsSync(APPLY)) return log("skip: apply.py 不存在");
62
+ if (!throttleElapsed()) return log("skip: 节流中");
63
+
64
+ try {
65
+ writeFileSync(STATE_FILE, `${JSON.stringify({ at: Date.now() })}\n`, "utf8");
66
+ } catch {
67
+ // 状态写不了就退化成每次都检查,仍然幂等
68
+ }
69
+
70
+ const output = await runApply();
71
+ const applied = Number(/(\d+) 处替换已处理/.exec(output)?.[1] ?? 0);
72
+ const broken = Number(/(\d+) 处失效/.exec(output)?.[1] ?? 0);
73
+ log(`applied=${applied} broken=${broken} :: ${output.trim().replace(/\n/g, " | ")}`);
74
+
75
+ if (applied > 0) {
76
+ pendingNote = `pi-web 补丁被升级冲掉 ${applied} 处,已自动重放。重启 pi-web 才生效:d_restart`;
77
+ }
78
+ }
79
+
80
+ export default function (pi: ExtensionAPI) {
81
+ void checkAndReplay().catch((err: Error) => log(`error ${err.message}`));
82
+
83
+ pi.on("agent_start", (_event, ctx: ExtensionContext) => {
84
+ if (!pendingNote) return;
85
+ const message = pendingNote;
86
+ pendingNote = null;
87
+ try {
88
+ ctx.ui.notify(message, "warning");
89
+ } catch {
90
+ // 没有 UI 就只留日志
91
+ }
92
+ });
93
+ }
package/package.json CHANGED
@@ -1,6 +1,33 @@
1
1
  {
2
2
  "name": "@duanz/pi-pi-web-patch-keeper",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "1.0.1",
4
+ "description": "pi-web 升级会覆盖本地补丁,这个扩展在启动时把它们重新打上",
5
+ "type": "module",
6
+ "keywords": [
7
+ "pi-package",
8
+ "pi-extension",
9
+ "pi-web"
10
+ ],
11
+ "license": "MIT",
12
+ "author": "DuanZ",
13
+ "homepage": "https://github.com/DuanZGit/D-Hub/tree/master/extensions/pi-web-patch-keeper",
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "git+https://github.com/DuanZGit/D-Hub.git",
17
+ "directory": "extensions/pi-web-patch-keeper"
18
+ },
19
+ "files": [
20
+ "index.ts",
21
+ "README.md",
22
+ "README.en.md"
23
+ ],
24
+ "pi": {
25
+ "extensions": [
26
+ "./index.ts"
27
+ ],
28
+ "image": "https://raw.githubusercontent.com/DuanZGit/D-Hub/master/docs/covers/pi-web-patch-keeper.png"
29
+ },
30
+ "engines": {
31
+ "node": ">=20"
32
+ }
33
+ }