@andares/pi-toolkits 0.12.4 → 0.14.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/CHANGELOG.md +43 -0
- package/README.md +134 -17
- package/README.zh-CN.md +115 -16
- package/package.json +24 -11
- package/src/features/ask/index.ts +6 -0
- package/src/features/init/apply.ts +32 -12
- package/src/features/init/audit-uat.test.ts +103 -0
- package/src/features/init/audit.ts +148 -0
- package/src/features/init/detect.ts +4 -0
- package/src/features/init/index.ts +47 -2
- package/src/features/init/plan.ts +11 -0
- package/src/features/init/refresh.test.ts +208 -0
- package/src/features/init/refresh.ts +255 -0
- package/src/features/init/spec.ts +20 -0
- package/src/features/init/templates/agents-skeleton.md +2 -0
- package/src/features/init/templates/uat-readme.md +53 -0
- package/src/features/init/templates/workflow-block.md +21 -0
- package/src/features/init/templates.test.ts +3 -1
- package/src/features/init/templates.ts +2 -0
- package/src/features/init/types.ts +12 -1
- package/src/features/permission/index.ts +32 -0
- package/src/features/permission/policy.test.ts +10 -0
- package/src/features/permission/units.test.ts +157 -0
- package/src/features/permission/units.ts +317 -15
- package/src/features/plan/assets/index-C1YLIi4I.js +1 -0
- package/src/features/plan/assets/index-DMEpYC3V.css +1 -0
- package/src/features/plan/assets/index.html +14 -0
- package/src/features/plan/assets.test.ts +64 -0
- package/src/features/plan/blocks.test.ts +77 -0
- package/src/features/plan/blocks.ts +153 -0
- package/src/features/plan/diff.test.ts +144 -0
- package/src/features/plan/diff.ts +193 -0
- package/src/features/plan/feedback.test.ts +233 -0
- package/src/features/plan/feedback.ts +182 -0
- package/src/features/plan/index.test.ts +385 -0
- package/src/features/plan/index.ts +433 -0
- package/src/features/plan/prompt.test.ts +91 -0
- package/src/features/plan/prompt.ts +110 -0
- package/src/features/plan/render.test.ts +94 -0
- package/src/features/plan/render.ts +131 -0
- package/src/features/plan/server.test.ts +374 -0
- package/src/features/plan/server.ts +272 -0
- package/src/features/plan/store.test.ts +145 -0
- package/src/features/plan/store.ts +244 -0
- package/src/features/review/index.ts +54 -28
- package/src/features/review/review.test.ts +46 -1
- package/src/features/uat/assets/index-D-W9zj0w.css +1 -0
- package/src/features/uat/assets/index-DCBQCsli.js +1 -0
- package/src/features/uat/assets/index.html +14 -0
- package/src/features/uat/constants.ts +23 -0
- package/src/features/uat/index.test.ts +404 -0
- package/src/features/uat/index.ts +329 -0
- package/src/features/uat/item-tool.ts +280 -0
- package/src/features/uat/ledger.test.ts +249 -0
- package/src/features/uat/ledger.ts +504 -0
- package/src/features/uat/prompt.ts +78 -0
- package/src/features/uat/record-tool.ts +167 -0
- package/src/features/uat/records.test.ts +145 -0
- package/src/features/uat/records.ts +268 -0
- package/src/features/uat/server.ts +181 -0
- package/src/features/uat/state.test.ts +41 -0
- package/src/features/uat/state.ts +67 -0
- package/src/features/whats-new/changelog.ts +104 -0
- package/src/features/whats-new/constants.ts +35 -0
- package/src/features/whats-new/index.ts +146 -0
- package/src/features/whats-new/notice.ts +68 -0
- package/src/features/whats-new/paths.ts +29 -0
- package/src/features/whats-new/store.ts +114 -0
- package/src/features/whats-new/version.ts +43 -0
- package/src/features/whats-new/whats-new.test.ts +460 -0
- package/src/index.ts +13 -1
- package/src/lib/ask-control.ts +25 -0
- package/src/lib/opener.test.ts +72 -0
- package/src/lib/opener.ts +121 -0
- package/src/lib/permission-control.ts +39 -0
- package/src/lib/startup.test.ts +1 -1
- package/src/lib/web-review.ts +341 -0
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* UAT server — the acceptance page, served from inside the pi process.
|
|
3
|
+
*
|
|
4
|
+
* Routes (all behind the shared token/Host/Origin shell):
|
|
5
|
+
* GET /api/state → derived acceptance state (items, pending records, counts)
|
|
6
|
+
* POST /api/toggle → {item, checked}: append a check/uncheck event
|
|
7
|
+
* GET /api/report → raw text of one change record (evidence in a drawer)
|
|
8
|
+
* GET /api/events → SSE; any change (from this process or another) arrives
|
|
9
|
+
* as a `state` event
|
|
10
|
+
*
|
|
11
|
+
* There is no submit endpoint on purpose: a checkbox writes its event
|
|
12
|
+
* immediately, so closing the page cannot lose a decision. The watcher exists
|
|
13
|
+
* because the ledger is a file: another pi session may append to it while this
|
|
14
|
+
* page is open, and the page must converge without a reload.
|
|
15
|
+
*/
|
|
16
|
+
import { statSync } from "node:fs";
|
|
17
|
+
import { join } from "node:path";
|
|
18
|
+
import { fileURLToPath } from "node:url";
|
|
19
|
+
import { UAT_WATCH_MS } from "./constants.js";
|
|
20
|
+
import {
|
|
21
|
+
appendEvent,
|
|
22
|
+
ledgerPath,
|
|
23
|
+
newEventId,
|
|
24
|
+
type UatState,
|
|
25
|
+
} from "./ledger.js";
|
|
26
|
+
import { readRecordText } from "./records.js";
|
|
27
|
+
import { loadSnapshot } from "./state.js";
|
|
28
|
+
import { bindHost, isWsl, urlHost } from "../../lib/opener.js";
|
|
29
|
+
import { startWebServer, type WebServerHandle } from "../../lib/web-review.js";
|
|
30
|
+
|
|
31
|
+
export interface UatPayload extends UatState {
|
|
32
|
+
root: string;
|
|
33
|
+
/** Malformed ledger lines (a corrupt stream must not look like an empty one). */
|
|
34
|
+
skipped: number;
|
|
35
|
+
/** Minimal record index for the evidence drawer. */
|
|
36
|
+
records: Array<{ id: string; title: string; at?: string }>;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function buildUatPayload(root: string): UatPayload {
|
|
40
|
+
const snapshot = loadSnapshot(root);
|
|
41
|
+
return {
|
|
42
|
+
...snapshot.state,
|
|
43
|
+
root,
|
|
44
|
+
skipped: snapshot.skipped,
|
|
45
|
+
records: snapshot.records.map((record) => ({
|
|
46
|
+
id: record.id,
|
|
47
|
+
title: record.title,
|
|
48
|
+
at: record.at,
|
|
49
|
+
})),
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export interface StartUatServerOptions {
|
|
54
|
+
root: string;
|
|
55
|
+
assetsDir?: string;
|
|
56
|
+
portStart?: number;
|
|
57
|
+
portAttempts?: number;
|
|
58
|
+
bindHost?: string;
|
|
59
|
+
urlHost?: string;
|
|
60
|
+
/** Clock injection (tests). */
|
|
61
|
+
now?: () => Date;
|
|
62
|
+
/** Called on every request, for the idle timeout. */
|
|
63
|
+
onActivity?: () => void;
|
|
64
|
+
watchMs?: number;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export interface UatServerHandle {
|
|
68
|
+
url: string;
|
|
69
|
+
port: number;
|
|
70
|
+
token: string;
|
|
71
|
+
close: () => Promise<void>;
|
|
72
|
+
broadcast: (event: string, data: unknown) => void;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export async function startUatServer(options: StartUatServerOptions): Promise<UatServerHandle> {
|
|
76
|
+
const assetsDir = options.assetsDir ?? fileURLToPath(new URL("./assets/", import.meta.url));
|
|
77
|
+
const now = options.now ?? (() => new Date());
|
|
78
|
+
const watchMs = options.watchMs ?? UAT_WATCH_MS;
|
|
79
|
+
const touch = options.onActivity ?? (() => {});
|
|
80
|
+
|
|
81
|
+
const signature = (): string => {
|
|
82
|
+
const parts: string[] = [];
|
|
83
|
+
try {
|
|
84
|
+
const stat = statSync(ledgerPath(options.root));
|
|
85
|
+
parts.push(`l:${stat.size}:${stat.mtimeMs}`);
|
|
86
|
+
} catch {
|
|
87
|
+
parts.push("l:none");
|
|
88
|
+
}
|
|
89
|
+
try {
|
|
90
|
+
const stat = statSync(join(options.root, ".agents/implementations"));
|
|
91
|
+
parts.push(`r:${stat.mtimeMs}`);
|
|
92
|
+
} catch {
|
|
93
|
+
parts.push("r:none");
|
|
94
|
+
}
|
|
95
|
+
return parts.join("|");
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
let handle: WebServerHandle | undefined;
|
|
99
|
+
let lastSignature = signature();
|
|
100
|
+
|
|
101
|
+
const server = await startWebServer({
|
|
102
|
+
assetsDir,
|
|
103
|
+
portStart: options.portStart,
|
|
104
|
+
portAttempts: options.portAttempts,
|
|
105
|
+
bindHost: options.bindHost ?? bindHost(isWsl()),
|
|
106
|
+
urlHost: options.urlHost ?? urlHost(isWsl()),
|
|
107
|
+
routes: async (ctx) => {
|
|
108
|
+
touch();
|
|
109
|
+
if (ctx.url.pathname === "/api/state" && ctx.req.method === "GET") {
|
|
110
|
+
ctx.json(buildUatPayload(options.root));
|
|
111
|
+
return true;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
if (ctx.url.pathname === "/api/toggle" && ctx.req.method === "POST") {
|
|
115
|
+
const body = (await ctx.readBody()) as { item?: unknown; checked?: unknown };
|
|
116
|
+
const item = typeof body.item === "string" ? body.item : "";
|
|
117
|
+
if (typeof body.checked !== "boolean") {
|
|
118
|
+
ctx.json({ error: "checked must be a boolean" }, 400);
|
|
119
|
+
return true;
|
|
120
|
+
}
|
|
121
|
+
const checked = body.checked;
|
|
122
|
+
const snapshot = loadSnapshot(options.root);
|
|
123
|
+
const known = snapshot.state.items.some((entry) => entry.id === item);
|
|
124
|
+
if (!known) {
|
|
125
|
+
ctx.json({ error: "unknown item" }, 404);
|
|
126
|
+
return true;
|
|
127
|
+
}
|
|
128
|
+
const at = now();
|
|
129
|
+
appendEvent(ledgerPath(options.root), {
|
|
130
|
+
v: 1,
|
|
131
|
+
id: newEventId(at),
|
|
132
|
+
ts: at.toISOString(),
|
|
133
|
+
type: checked ? "check" : "uncheck",
|
|
134
|
+
item,
|
|
135
|
+
});
|
|
136
|
+
const payload = buildUatPayload(options.root);
|
|
137
|
+
lastSignature = signature();
|
|
138
|
+
ctx.broadcast("state", { reason: "toggle" });
|
|
139
|
+
ctx.json({ ok: true, state: payload });
|
|
140
|
+
return true;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
if (ctx.url.pathname === "/api/report" && ctx.req.method === "GET") {
|
|
144
|
+
const id = ctx.url.searchParams.get("id") ?? "";
|
|
145
|
+
const text = readRecordText(options.root, id);
|
|
146
|
+
if (text === undefined) {
|
|
147
|
+
ctx.json({ error: "unknown record" }, 404);
|
|
148
|
+
return true;
|
|
149
|
+
}
|
|
150
|
+
ctx.json({ id, text });
|
|
151
|
+
return true;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
return false;
|
|
155
|
+
},
|
|
156
|
+
});
|
|
157
|
+
handle = server;
|
|
158
|
+
|
|
159
|
+
// Another process may append to the ledger (or add a record) while the page
|
|
160
|
+
// is open; converge without asking the operator to reload.
|
|
161
|
+
const watcher = setInterval(() => {
|
|
162
|
+
const next = signature();
|
|
163
|
+
if (next === lastSignature) return;
|
|
164
|
+
lastSignature = next;
|
|
165
|
+
handle?.broadcast("state", { reason: "external" });
|
|
166
|
+
}, watchMs);
|
|
167
|
+
watcher.unref();
|
|
168
|
+
|
|
169
|
+
const close = async (): Promise<void> => {
|
|
170
|
+
clearInterval(watcher);
|
|
171
|
+
await handle?.close();
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
return {
|
|
175
|
+
url: server.url,
|
|
176
|
+
port: server.port,
|
|
177
|
+
token: server.token,
|
|
178
|
+
close,
|
|
179
|
+
broadcast: server.broadcast,
|
|
180
|
+
};
|
|
181
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { execFileSync } from "node:child_process";
|
|
2
|
+
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { afterEach, describe, expect, it } from "vitest";
|
|
6
|
+
import { resetProjectRootCache, resolveProjectRoot } from "./state.js";
|
|
7
|
+
|
|
8
|
+
const roots: string[] = [];
|
|
9
|
+
|
|
10
|
+
afterEach(() => {
|
|
11
|
+
resetProjectRootCache();
|
|
12
|
+
for (const root of roots.splice(0)) rmSync(root, { recursive: true, force: true });
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
function git(cwd: string, args: string[]): void {
|
|
16
|
+
execFileSync("git", ["-C", cwd, ...args], { stdio: "ignore" });
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
describe("resolveProjectRoot", () => {
|
|
20
|
+
it("uses the cwd itself outside git", () => {
|
|
21
|
+
const root = mkdtempSync(join(tmpdir(), "uat-nongit-"));
|
|
22
|
+
roots.push(root);
|
|
23
|
+
expect(resolveProjectRoot(root)).toBe(root);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it("resolves a linked worktree to the main checkout", () => {
|
|
27
|
+
const main = mkdtempSync(join(tmpdir(), "uat-main-"));
|
|
28
|
+
const parent = mkdtempSync(join(tmpdir(), "uat-worktrees-"));
|
|
29
|
+
const linked = join(parent, "linked");
|
|
30
|
+
roots.push(main, parent);
|
|
31
|
+
git(main, ["init", "-q"]);
|
|
32
|
+
writeFileSync(join(main, "file.txt"), "one\n", "utf8");
|
|
33
|
+
git(main, ["add", "file.txt"]);
|
|
34
|
+
git(main, ["-c", "user.email=test@example.com", "-c", "user.name=test", "commit", "-qm", "init"]);
|
|
35
|
+
git(main, ["worktree", "add", "-qb", "uat-review", linked]);
|
|
36
|
+
|
|
37
|
+
resetProjectRootCache();
|
|
38
|
+
expect(resolveProjectRoot(linked)).toBe(main);
|
|
39
|
+
expect(resolveProjectRoot(main)).toBe(main);
|
|
40
|
+
});
|
|
41
|
+
});
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* UAT feature — where the project lives and what the ledger currently says.
|
|
3
|
+
*
|
|
4
|
+
* **The ledger belongs to the main checkout.** A linked git worktree has its
|
|
5
|
+
* own working tree, and writing acceptance history into it would split the
|
|
6
|
+
* record the moment a second agent session uses `git worktree add`. So the root
|
|
7
|
+
* is resolved through `git worktree list` (first entry = the main worktree) and
|
|
8
|
+
* every read/write of `.agents/uat/` and `.agents/implementations/` uses it.
|
|
9
|
+
*
|
|
10
|
+
* Resolution is cached per cwd: the page polls, `/ask-uat` reads, and nothing
|
|
11
|
+
* changed in between. The cache is the reason a UI refresh does not spawn git.
|
|
12
|
+
*/
|
|
13
|
+
import { execFileSync } from "node:child_process";
|
|
14
|
+
import { existsSync } from "node:fs";
|
|
15
|
+
import { join, resolve } from "node:path";
|
|
16
|
+
import { STATE_FILE } from "../init/spec.js";
|
|
17
|
+
import { deriveState, ledgerPath, readEvents, type UatState } from "./ledger.js";
|
|
18
|
+
import { listRecords, type RecordEntry } from "./records.js";
|
|
19
|
+
|
|
20
|
+
const rootCache = new Map<string, string>();
|
|
21
|
+
|
|
22
|
+
/** The main worktree of the repository containing `cwd` (or `cwd` itself). */
|
|
23
|
+
export function resolveProjectRoot(cwd: string): string {
|
|
24
|
+
const start = resolve(cwd);
|
|
25
|
+
const cached = rootCache.get(start);
|
|
26
|
+
if (cached) return cached;
|
|
27
|
+
|
|
28
|
+
let root = start;
|
|
29
|
+
try {
|
|
30
|
+
const output = execFileSync("git", ["-C", start, "worktree", "list", "--porcelain"], {
|
|
31
|
+
encoding: "utf8",
|
|
32
|
+
timeout: 3000,
|
|
33
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
34
|
+
});
|
|
35
|
+
const line = output.split("\n").find((entry) => entry.startsWith("worktree "));
|
|
36
|
+
const first = line?.slice("worktree ".length).trim();
|
|
37
|
+
if (first) root = resolve(first);
|
|
38
|
+
} catch {
|
|
39
|
+
// Not a git checkout (or git is missing): the cwd is the project.
|
|
40
|
+
}
|
|
41
|
+
rootCache.set(start, root);
|
|
42
|
+
return root;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Test seam: forget the memoized roots. */
|
|
46
|
+
export function resetProjectRootCache(): void {
|
|
47
|
+
rootCache.clear();
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** The UAT flow only exists in a project that installed the framework. */
|
|
51
|
+
export function frameworkInstalled(root: string): boolean {
|
|
52
|
+
return existsSync(join(root, STATE_FILE));
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export interface UatSnapshot {
|
|
56
|
+
root: string;
|
|
57
|
+
records: RecordEntry[];
|
|
58
|
+
state: UatState;
|
|
59
|
+
/** Malformed ledger lines; surfaced so a corrupt file is not silently "empty". */
|
|
60
|
+
skipped: number;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function loadSnapshot(root: string): UatSnapshot {
|
|
64
|
+
const records = listRecords(root);
|
|
65
|
+
const { events, skipped } = readEvents(ledgerPath(root));
|
|
66
|
+
return { root, records, state: deriveState(records, events), skipped };
|
|
67
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* whats-new feature — `CHANGELOG.md` parsing.
|
|
3
|
+
*
|
|
4
|
+
* Contract with the release script (scripts/lib/changelog.mjs):
|
|
5
|
+
*
|
|
6
|
+
* - one `## <version>` section per released version, optionally decorated
|
|
7
|
+
* (`## [0.14.0] — 2026-11-12`); the first `X.Y.Z` in the heading wins;
|
|
8
|
+
* - `## Unreleased` holds the notes for the next release; `pnpm release`
|
|
9
|
+
* renames that heading to the version it is about to publish;
|
|
10
|
+
* - top-level bullets are the notes shown in the notice; nested bullets are
|
|
11
|
+
* detail for humans reading the file and are not repeated in the TUI.
|
|
12
|
+
*
|
|
13
|
+
* Parsing is tolerant on purpose: a malformed section is skipped, never fatal.
|
|
14
|
+
* A missing file degrades to "no notes" with one warning — an extension must
|
|
15
|
+
* not break a session because a documentation file moved.
|
|
16
|
+
*/
|
|
17
|
+
import { readFileSync } from "node:fs";
|
|
18
|
+
import { defaultChangelogFile } from "./paths.js";
|
|
19
|
+
import { compareVersions, parseVersion } from "./version.js";
|
|
20
|
+
|
|
21
|
+
export interface ReleaseNote {
|
|
22
|
+
version: string;
|
|
23
|
+
notes: string[];
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const HEADING_RE = /^##\s+(.+?)\s*$/;
|
|
27
|
+
const VERSION_IN_HEADING_RE = /\[?v?(\d+\.\d+\.\d+)\]?/;
|
|
28
|
+
const BULLET_RE = /^[-*]\s+(.+?)\s*$/;
|
|
29
|
+
|
|
30
|
+
/** Split a changelog into version sections, in document order. */
|
|
31
|
+
export function parseChangelog(markdown: string): ReleaseNote[] {
|
|
32
|
+
const releases: ReleaseNote[] = [];
|
|
33
|
+
let current: ReleaseNote | undefined;
|
|
34
|
+
|
|
35
|
+
for (const line of markdown.split(/\r?\n/)) {
|
|
36
|
+
const heading = HEADING_RE.exec(line);
|
|
37
|
+
if (heading) {
|
|
38
|
+
const match = VERSION_IN_HEADING_RE.exec(heading[1]);
|
|
39
|
+
current = match ? { version: match[1], notes: [] } : undefined;
|
|
40
|
+
if (current) releases.push(current);
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
if (!current) continue;
|
|
44
|
+
const bullet = BULLET_RE.exec(line);
|
|
45
|
+
if (bullet) current.notes.push(bullet[1]);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return releases;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Newest first. */
|
|
52
|
+
export function sortReleasesDescending(releases: ReleaseNote[]): ReleaseNote[] {
|
|
53
|
+
return [...releases].sort((a, b) => compareVersions(b.version, a.version));
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Notes for a `from → to` upgrade: `from < version <= to`, newest first.
|
|
58
|
+
* Equal and older versions are excluded so a downgrade never replays notes.
|
|
59
|
+
*/
|
|
60
|
+
export function releasesForUpgrade(
|
|
61
|
+
releases: ReleaseNote[],
|
|
62
|
+
from: string,
|
|
63
|
+
to: string,
|
|
64
|
+
): ReleaseNote[] {
|
|
65
|
+
return sortReleasesDescending(releases).filter(
|
|
66
|
+
(release) =>
|
|
67
|
+
compareVersions(release.version, from) > 0 &&
|
|
68
|
+
compareVersions(release.version, to) <= 0,
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Paths already warned about, so a missing file warns once per process. */
|
|
73
|
+
const warned = new Set<string>();
|
|
74
|
+
|
|
75
|
+
/** Read and parse the notes; a missing/invalid file becomes an empty list. */
|
|
76
|
+
export function readChangelog(file: string = defaultChangelogFile()): ReleaseNote[] {
|
|
77
|
+
try {
|
|
78
|
+
return parseChangelog(readFileSync(file, "utf8"));
|
|
79
|
+
} catch (error) {
|
|
80
|
+
if (!warned.has(file)) {
|
|
81
|
+
warned.add(file);
|
|
82
|
+
// pi-lens-ignore: no-console-except-error,console-statement, — deliberate degradation log
|
|
83
|
+
console.warn(
|
|
84
|
+
"[pi-toolkits/whats-new] Failed to read release notes:",
|
|
85
|
+
error instanceof Error ? error.message : String(error),
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
return [];
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** The version this copy of the package was published as, if readable. */
|
|
93
|
+
export function readPackageVersion(file: string): string | undefined {
|
|
94
|
+
try {
|
|
95
|
+
const parsed: unknown = JSON.parse(readFileSync(file, "utf8"));
|
|
96
|
+
if (!parsed || typeof parsed !== "object") return undefined;
|
|
97
|
+
const version = (parsed as { version?: unknown }).version;
|
|
98
|
+
return typeof version === "string" && parseVersion(version) !== undefined
|
|
99
|
+
? version
|
|
100
|
+
: undefined;
|
|
101
|
+
} catch {
|
|
102
|
+
return undefined;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* whats-new feature — constants and the reasoning behind the notice shape.
|
|
3
|
+
*
|
|
4
|
+
* The feature answers one question — "what changed in the version I just
|
|
5
|
+
* installed?" — and answers it exactly once per upgrade. Release notes live in
|
|
6
|
+
* the repository's `CHANGELOG.md`, which ships inside the npm package and is
|
|
7
|
+
* promoted from `## Unreleased` by `pnpm release` (see scripts/publish.mjs), so
|
|
8
|
+
* the text is written at release time instead of being reconstructed from
|
|
9
|
+
* commit subjects at runtime.
|
|
10
|
+
*
|
|
11
|
+
* Why a custom session entry instead of `ctx.ui.notify`: a startup notice needs
|
|
12
|
+
* its own text and must survive `ui.invalidate()` — the same reason the
|
|
13
|
+
* permission feature renders its autostart notice as an entry (see
|
|
14
|
+
* src/features/permission/index.ts). An entry also stays out of the model
|
|
15
|
+
* context, so release notes never cost a token.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
/** State file in pi's agent dir (same convention as the other feature stores). */
|
|
19
|
+
export const WHATS_NEW_FILE = "pi-toolkits-whats-new.json";
|
|
20
|
+
|
|
21
|
+
/** Custom entry type; also the renderer key. */
|
|
22
|
+
export const NOTICE_ENTRY = "whats-new:notice";
|
|
23
|
+
|
|
24
|
+
/** Release notes file shipped at the package root. */
|
|
25
|
+
export const CHANGELOG_FILENAME = "CHANGELOG.md";
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Newest N versions shown in the startup notice. An operator upgrading from a
|
|
29
|
+
* much older release gets the versions that actually matter, not a wall of
|
|
30
|
+
* text; the complete notes stay in `CHANGELOG.md`.
|
|
31
|
+
*/
|
|
32
|
+
export const MAX_VERSIONS_IN_NOTICE = 4;
|
|
33
|
+
|
|
34
|
+
/** Notes per version in the startup notice; the rest stay in `CHANGELOG.md`. */
|
|
35
|
+
export const MAX_NOTES_PER_VERSION = 6;
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* whats-new feature — release notes on the first start after an upgrade.
|
|
3
|
+
*
|
|
4
|
+
* Behavior:
|
|
5
|
+
* - `session_start(reason="startup")` in the TUI is the only trigger. A
|
|
6
|
+
* `reload` / `resume` / `new` / `fork` is an in-process transition and must
|
|
7
|
+
* not replay notes the operator already saw. print/json/rpc sessions have no
|
|
8
|
+
* transcript to append to, so they leave the notice for the first interactive
|
|
9
|
+
* start instead of silently consuming it.
|
|
10
|
+
* - The store records the highest version already handled. A missing record
|
|
11
|
+
* means a fresh install: the running version is recorded silently, because
|
|
12
|
+
* there is no "previous version" to report from. A version at or below the
|
|
13
|
+
* record (downgrade, reinstall) is not an upgrade and shows nothing.
|
|
14
|
+
* - Notes come from the bundled `CHANGELOG.md`, filtered to
|
|
15
|
+
* `lastSeen < version <= running`. A version with no section reports the
|
|
16
|
+
* version change and says that no notes are bundled.
|
|
17
|
+
* - The notice is a custom session entry — own text, immune to
|
|
18
|
+
* `ui.invalidate()` and out of the model context (same reason as the
|
|
19
|
+
* permission feature's autostart notice; see that module's header).
|
|
20
|
+
* - Deliberately no global slash command: this package is a plugin, and the
|
|
21
|
+
* automatic notice must not claim command namespace from the host.
|
|
22
|
+
*/
|
|
23
|
+
import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
24
|
+
import { Text } from "@earendil-works/pi-tui";
|
|
25
|
+
import {
|
|
26
|
+
readChangelog,
|
|
27
|
+
readPackageVersion,
|
|
28
|
+
releasesForUpgrade,
|
|
29
|
+
} from "./changelog.js";
|
|
30
|
+
import { NOTICE_ENTRY } from "./constants.js";
|
|
31
|
+
import { buildUpgradeNotice, type NoticeData } from "./notice.js";
|
|
32
|
+
import { defaultChangelogFile, defaultPackageFile } from "./paths.js";
|
|
33
|
+
import { WhatsNewStore } from "./store.js";
|
|
34
|
+
import { compareVersions } from "./version.js";
|
|
35
|
+
|
|
36
|
+
export interface WhatsNewOptions {
|
|
37
|
+
/** State file; defaults to `<agent-dir>/pi-toolkits-whats-new.json`. */
|
|
38
|
+
readonly storeFile?: string;
|
|
39
|
+
/** Release notes file; defaults to the packaged `CHANGELOG.md`. */
|
|
40
|
+
readonly changelogFile?: string;
|
|
41
|
+
/** Package manifest to read the running version from. */
|
|
42
|
+
readonly packageFile?: string;
|
|
43
|
+
/** Explicit running version (tests); overrides `packageFile`. */
|
|
44
|
+
readonly version?: string;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
interface WhatsNewRuntime {
|
|
48
|
+
readonly store: WhatsNewStore;
|
|
49
|
+
readonly changelogFile: string;
|
|
50
|
+
readonly packageFile: string;
|
|
51
|
+
readonly version?: string;
|
|
52
|
+
/** Versions already handled in this process (a store write may fail). */
|
|
53
|
+
readonly announced: Set<string>;
|
|
54
|
+
present(ctx: ExtensionContext, data: NoticeData): void;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function runningVersion(runtime: WhatsNewRuntime): string | undefined {
|
|
58
|
+
return runtime.version ?? readPackageVersion(runtime.packageFile);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Show the notes for a genuine upgrade, then record the running version. */
|
|
62
|
+
function announceUpgrade(runtime: WhatsNewRuntime, ctx: ExtensionContext): void {
|
|
63
|
+
const version = runningVersion(runtime);
|
|
64
|
+
if (version === undefined || runtime.announced.has(version)) return;
|
|
65
|
+
|
|
66
|
+
const { store } = runtime;
|
|
67
|
+
const from = store.get().lastSeenVersion;
|
|
68
|
+
if (from !== undefined && compareVersions(version, from) <= 0) return;
|
|
69
|
+
|
|
70
|
+
// A fresh install has no previous version to report from.
|
|
71
|
+
if (from !== undefined) {
|
|
72
|
+
try {
|
|
73
|
+
const releases = releasesForUpgrade(
|
|
74
|
+
readChangelog(runtime.changelogFile),
|
|
75
|
+
from,
|
|
76
|
+
version,
|
|
77
|
+
);
|
|
78
|
+
runtime.present(ctx, buildUpgradeNotice(from, version, releases));
|
|
79
|
+
} catch (error) {
|
|
80
|
+
// pi-lens-ignore: no-console-except-error,console-statement, — deliberate degradation log
|
|
81
|
+
console.warn(
|
|
82
|
+
"[pi-toolkits/whats-new] Failed to render release notes:",
|
|
83
|
+
error instanceof Error ? error.message : String(error),
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// Marked even if rendering failed: a broken notice must not repeat on every
|
|
89
|
+
// start (the notes remain in the package's CHANGELOG.md).
|
|
90
|
+
store.markSeen(version);
|
|
91
|
+
runtime.announced.add(version);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export function registerWhatsNew(
|
|
95
|
+
pi: ExtensionAPI,
|
|
96
|
+
options: WhatsNewOptions = {},
|
|
97
|
+
): void {
|
|
98
|
+
const supportsEntryRenderer =
|
|
99
|
+
typeof pi.registerEntryRenderer === "function";
|
|
100
|
+
if (supportsEntryRenderer) {
|
|
101
|
+
pi.registerEntryRenderer<NoticeData>(NOTICE_ENTRY, (entry, _options, theme) => {
|
|
102
|
+
const data = entry.data;
|
|
103
|
+
if (!data || !Array.isArray(data.lines)) return undefined;
|
|
104
|
+
return new Text(
|
|
105
|
+
[
|
|
106
|
+
theme.bold(data.title),
|
|
107
|
+
...data.lines.map((line) => theme.fg("dim", line)),
|
|
108
|
+
].join("\n"),
|
|
109
|
+
1,
|
|
110
|
+
0,
|
|
111
|
+
);
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const runtime: WhatsNewRuntime = {
|
|
116
|
+
store: new WhatsNewStore(options.storeFile),
|
|
117
|
+
changelogFile: options.changelogFile ?? defaultChangelogFile(),
|
|
118
|
+
packageFile: options.packageFile ?? defaultPackageFile(),
|
|
119
|
+
version: options.version,
|
|
120
|
+
announced: new Set<string>(),
|
|
121
|
+
/** Entry when the host can render one, one status notice otherwise. */
|
|
122
|
+
present(ctx, data) {
|
|
123
|
+
if (supportsEntryRenderer) {
|
|
124
|
+
pi.appendEntry<NoticeData>(NOTICE_ENTRY, data);
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
ctx.ui.notify([data.title, ...data.lines].join("\n"), "info");
|
|
128
|
+
},
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
let currentSessionId: string | undefined;
|
|
132
|
+
|
|
133
|
+
pi.on("session_start", (event, ctx) => {
|
|
134
|
+
if (event.reason !== "startup") return;
|
|
135
|
+
// No transcript to append to (print/json/rpc): keep the notice for the
|
|
136
|
+
// first interactive start rather than burning it here.
|
|
137
|
+
if (ctx.mode !== "tui") return;
|
|
138
|
+
|
|
139
|
+
// A runtime rebind can emit session_start again for the same session.
|
|
140
|
+
const sessionId = ctx.sessionManager.getSessionId();
|
|
141
|
+
if (sessionId !== undefined && sessionId === currentSessionId) return;
|
|
142
|
+
currentSessionId = sessionId;
|
|
143
|
+
|
|
144
|
+
announceUpgrade(runtime, ctx);
|
|
145
|
+
});
|
|
146
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* whats-new feature — startup notice text.
|
|
3
|
+
*
|
|
4
|
+
* Pure string building, capped so an upgrade across many releases cannot push
|
|
5
|
+
* the first prompt off the screen. The full text always stays in the bundled
|
|
6
|
+
* `CHANGELOG.md`.
|
|
7
|
+
*/
|
|
8
|
+
import type { ReleaseNote } from "./changelog.js";
|
|
9
|
+
import {
|
|
10
|
+
MAX_NOTES_PER_VERSION,
|
|
11
|
+
MAX_VERSIONS_IN_NOTICE,
|
|
12
|
+
} from "./constants.js";
|
|
13
|
+
|
|
14
|
+
/** Data carried by the `whats-new:notice` session entry. */
|
|
15
|
+
export interface NoticeData {
|
|
16
|
+
title: string;
|
|
17
|
+
lines: string[];
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface SectionOptions {
|
|
21
|
+
maxVersions?: number;
|
|
22
|
+
maxNotesPerVersion?: number;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** `version` heading plus indented bullets, newest first. */
|
|
26
|
+
export function renderReleaseSections(
|
|
27
|
+
releases: ReleaseNote[],
|
|
28
|
+
options: SectionOptions = {},
|
|
29
|
+
): string[] {
|
|
30
|
+
const maxVersions = options.maxVersions ?? MAX_VERSIONS_IN_NOTICE;
|
|
31
|
+
const maxNotes = options.maxNotesPerVersion ?? MAX_NOTES_PER_VERSION;
|
|
32
|
+
const shown = releases.slice(0, maxVersions);
|
|
33
|
+
const lines: string[] = [];
|
|
34
|
+
|
|
35
|
+
for (const release of shown) {
|
|
36
|
+
lines.push(release.version);
|
|
37
|
+
for (const note of release.notes.slice(0, maxNotes)) {
|
|
38
|
+
lines.push(` - ${note}`);
|
|
39
|
+
}
|
|
40
|
+
const hiddenNotes = release.notes.length - Math.min(release.notes.length, maxNotes);
|
|
41
|
+
if (hiddenNotes > 0) {
|
|
42
|
+
lines.push(` - (+${hiddenNotes} more in CHANGELOG.md)`);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const hiddenVersions = releases.length - shown.length;
|
|
47
|
+
if (hiddenVersions > 0) {
|
|
48
|
+
lines.push(
|
|
49
|
+
`(+${hiddenVersions} older version${hiddenVersions === 1 ? "" : "s"} — see CHANGELOG.md)`,
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return lines;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Startup notice: what changed between the recorded version and this one. */
|
|
57
|
+
export function buildUpgradeNotice(
|
|
58
|
+
from: string,
|
|
59
|
+
to: string,
|
|
60
|
+
releases: ReleaseNote[],
|
|
61
|
+
): NoticeData {
|
|
62
|
+
const sections = renderReleaseSections(releases);
|
|
63
|
+
const lines =
|
|
64
|
+
sections.length > 0
|
|
65
|
+
? sections
|
|
66
|
+
: ["No release notes are bundled for this version."];
|
|
67
|
+
return { title: `pi-toolkits updated ${from} → ${to}`, lines };
|
|
68
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* whats-new feature — locates files shipped next to this module.
|
|
3
|
+
*
|
|
4
|
+
* The published package contains `src/` plus `CHANGELOG.md` (package.json
|
|
5
|
+
* `files`), so both are reachable by URL relative to this module. The host
|
|
6
|
+
* loads extensions through jiti, which preserves `import.meta.url` (verified:
|
|
7
|
+
* a probed extension reports `file:///…/src/…`), so the resolution works in a
|
|
8
|
+
* checkout and in an installed package alike.
|
|
9
|
+
*
|
|
10
|
+
* Everything here is best-effort: callers treat a missing file as "no notes"
|
|
11
|
+
* rather than failing the feature.
|
|
12
|
+
*/
|
|
13
|
+
import { fileURLToPath } from "node:url";
|
|
14
|
+
import { CHANGELOG_FILENAME } from "./constants.js";
|
|
15
|
+
|
|
16
|
+
/** Absolute path of a file at the package root. */
|
|
17
|
+
export function packageAsset(name: string): string {
|
|
18
|
+
return fileURLToPath(new URL(`../../../${name}`, import.meta.url));
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** `package.json` — the runtime source of the version being run. */
|
|
22
|
+
export function defaultPackageFile(): string {
|
|
23
|
+
return packageAsset("package.json");
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** `CHANGELOG.md` — the bundled release notes. */
|
|
27
|
+
export function defaultChangelogFile(): string {
|
|
28
|
+
return packageAsset(CHANGELOG_FILENAME);
|
|
29
|
+
}
|