@andares/pi-toolkits 0.13.0 → 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 +36 -4
- package/README.zh-CN.md +23 -4
- package/package.json +8 -4
- 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/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 +3 -0
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* whats-new feature — persisted "which version has the operator seen" state.
|
|
3
|
+
*
|
|
4
|
+
* State lives in `<agent-dir>/pi-toolkits-whats-new.json` (agent-dir from
|
|
5
|
+
* getAgentDir(), same convention as the other feature stores):
|
|
6
|
+
*
|
|
7
|
+
* { "lastSeenVersion": "0.13.0" }
|
|
8
|
+
*
|
|
9
|
+
* `lastSeenVersion` is the highest version whose notes were already handled —
|
|
10
|
+
* shown, or recorded silently because this is a fresh install. A missing field
|
|
11
|
+
* means a fresh install: there is no previous version to report from.
|
|
12
|
+
*
|
|
13
|
+
* Robustness: a missing file means defaults, a corrupted file falls back to
|
|
14
|
+
* defaults with one warning, writes are atomic (tmp + rename) and never throw.
|
|
15
|
+
* A failed write only means the notice may appear once more; it must never
|
|
16
|
+
* block a session.
|
|
17
|
+
*/
|
|
18
|
+
import {
|
|
19
|
+
existsSync,
|
|
20
|
+
mkdirSync,
|
|
21
|
+
readFileSync,
|
|
22
|
+
renameSync,
|
|
23
|
+
writeFileSync,
|
|
24
|
+
} from "node:fs";
|
|
25
|
+
import { dirname, join } from "node:path";
|
|
26
|
+
import { getAgentDir } from "@earendil-works/pi-coding-agent";
|
|
27
|
+
import { WHATS_NEW_FILE } from "./constants.js";
|
|
28
|
+
import { parseVersion } from "./version.js";
|
|
29
|
+
|
|
30
|
+
export interface WhatsNewState {
|
|
31
|
+
/** Highest version already handled; absent on a fresh install. */
|
|
32
|
+
lastSeenVersion?: string;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export const DEFAULT_STATE: WhatsNewState = {};
|
|
36
|
+
|
|
37
|
+
/** Coerce an unknown JSON blob into state (defaults for gaps). */
|
|
38
|
+
export function normalizeState(input: unknown): WhatsNewState {
|
|
39
|
+
const state: WhatsNewState = { ...DEFAULT_STATE };
|
|
40
|
+
if (!input || typeof input !== "object") return state;
|
|
41
|
+
const raw = input as Partial<WhatsNewState>;
|
|
42
|
+
if (
|
|
43
|
+
typeof raw.lastSeenVersion === "string" &&
|
|
44
|
+
parseVersion(raw.lastSeenVersion) !== undefined
|
|
45
|
+
) {
|
|
46
|
+
state.lastSeenVersion = raw.lastSeenVersion;
|
|
47
|
+
}
|
|
48
|
+
return state;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export class WhatsNewStore {
|
|
52
|
+
private state: WhatsNewState = { ...DEFAULT_STATE };
|
|
53
|
+
private readonly file: string;
|
|
54
|
+
private loaded = false;
|
|
55
|
+
|
|
56
|
+
constructor(file: string = join(getAgentDir(), WHATS_NEW_FILE)) {
|
|
57
|
+
this.file = file;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** Path of the JSON file. */
|
|
61
|
+
get path(): string {
|
|
62
|
+
return this.file;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
private ensureLoaded(): void {
|
|
66
|
+
if (this.loaded) return;
|
|
67
|
+
this.loaded = true;
|
|
68
|
+
try {
|
|
69
|
+
if (!existsSync(this.file)) return;
|
|
70
|
+
const parsed: unknown = JSON.parse(readFileSync(this.file, "utf8"));
|
|
71
|
+
this.state = normalizeState(parsed);
|
|
72
|
+
} catch (error) {
|
|
73
|
+
// pi-lens-ignore: no-console-except-error,console-statement, — deliberate degradation log
|
|
74
|
+
console.warn(
|
|
75
|
+
"[pi-toolkits/whats-new] Failed to load state, using defaults:",
|
|
76
|
+
error instanceof Error ? error.message : String(error),
|
|
77
|
+
);
|
|
78
|
+
this.state = { ...DEFAULT_STATE };
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** Current state (a copy). */
|
|
83
|
+
get(): WhatsNewState {
|
|
84
|
+
this.ensureLoaded();
|
|
85
|
+
return { ...this.state };
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** Remember that `version` was handled. */
|
|
89
|
+
markSeen(version: string): void {
|
|
90
|
+
this.ensureLoaded();
|
|
91
|
+
if (parseVersion(version) === undefined) return;
|
|
92
|
+
this.state.lastSeenVersion = version;
|
|
93
|
+
this.save();
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** Atomically persist (tmp + rename). Never throws. */
|
|
97
|
+
private save(): void {
|
|
98
|
+
try {
|
|
99
|
+
const dir = dirname(this.file);
|
|
100
|
+
if (!existsSync(dir)) {
|
|
101
|
+
mkdirSync(dir, { recursive: true });
|
|
102
|
+
}
|
|
103
|
+
const tmp = `${this.file}.tmp`;
|
|
104
|
+
writeFileSync(tmp, `${JSON.stringify(this.state, null, 2)}\n`, "utf8");
|
|
105
|
+
renameSync(tmp, this.file);
|
|
106
|
+
} catch (error) {
|
|
107
|
+
// pi-lens-ignore: no-console-except-error,console-statement, — deliberate degradation log
|
|
108
|
+
console.warn(
|
|
109
|
+
"[pi-toolkits/whats-new] Failed to save state:",
|
|
110
|
+
error instanceof Error ? error.message : String(error),
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* whats-new feature — strict `X.Y.Z` versions.
|
|
3
|
+
*
|
|
4
|
+
* Package versions in this repo are always plain `X.Y.Z` (the release script
|
|
5
|
+
* rejects anything else), so prerelease/build metadata is deliberately not
|
|
6
|
+
* parsed. An unparseable version yields `undefined`, and callers treat that as
|
|
7
|
+
* "do not announce" instead of guessing an ordering — a wrong comparison would
|
|
8
|
+
* replay notes the operator already saw.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
export interface VersionParts {
|
|
12
|
+
major: number;
|
|
13
|
+
minor: number;
|
|
14
|
+
patch: number;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const VERSION_RE = /^v?(\d+)\.(\d+)\.(\d+)$/;
|
|
18
|
+
|
|
19
|
+
/** Parse `X.Y.Z` (an optional leading `v` is tolerated). */
|
|
20
|
+
export function parseVersion(value: string): VersionParts | undefined {
|
|
21
|
+
const match = VERSION_RE.exec(value.trim());
|
|
22
|
+
if (!match) return undefined;
|
|
23
|
+
return {
|
|
24
|
+
major: Number(match[1]),
|
|
25
|
+
minor: Number(match[2]),
|
|
26
|
+
patch: Number(match[3]),
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Numeric ordering: negative when `a < b`, positive when `a > b`, 0 when equal
|
|
32
|
+
* or when either side is unparseable (never announce on an unknown ordering).
|
|
33
|
+
*/
|
|
34
|
+
export function compareVersions(a: string, b: string): number {
|
|
35
|
+
const left = parseVersion(a);
|
|
36
|
+
const right = parseVersion(b);
|
|
37
|
+
if (!left || !right) return 0;
|
|
38
|
+
return (
|
|
39
|
+
left.major - right.major ||
|
|
40
|
+
left.minor - right.minor ||
|
|
41
|
+
left.patch - right.patch
|
|
42
|
+
);
|
|
43
|
+
}
|