@howaboua/pi-agent-board 0.0.3 → 0.0.5
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 +12 -3
- package/INTEGRATION.md +7 -7
- package/README.md +12 -8
- package/dist/viewer/server.js +5 -0
- package/dist/viewer/web/app.js +29 -456
- package/dist/viewer/web/navigation.js +72 -0
- package/dist/viewer/web/posts.js +106 -0
- package/dist/viewer/web/render.js +147 -0
- package/dist/viewer/web/session.js +86 -0
- package/dist/viewer/web/updates.js +106 -0
- package/index.ts +3 -80
- package/package.json +6 -8
- package/src/archive.ts +17 -7
- package/src/awareness.ts +2 -1
- package/src/briefing.ts +6 -8
- package/src/context-briefing.ts +49 -31
- package/src/contract.ts +25 -17
- package/src/identity.ts +81 -17
- package/src/integration.ts +1 -0
- package/src/protocol.ts +2 -2
- package/src/queries.ts +1 -2
- package/src/runtime/catchup.ts +96 -0
- package/src/runtime/coordinator.ts +278 -0
- package/src/runtime/index.ts +113 -0
- package/src/runtime/owner.ts +164 -0
- package/src/runtime/routing.ts +152 -0
- package/src/{turns.ts → runtime/turns.ts} +3 -3
- package/src/search.ts +6 -4
- package/src/settings/panel.ts +87 -0
- package/src/store.ts +40 -5
- package/src/tool.ts +1 -1
- package/src/viewer/server.ts +5 -0
- package/src/viewer/web/app.js +29 -456
- package/src/viewer/web/navigation.js +72 -0
- package/src/viewer/web/posts.js +106 -0
- package/src/viewer/web/render.js +147 -0
- package/src/viewer/web/session.js +86 -0
- package/src/viewer/web/updates.js +106 -0
- package/changelog.ts +0 -337
- package/src/runtime.ts +0 -610
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { $, state, run, selectionURL } from "./session.js";
|
|
2
|
+
import {
|
|
3
|
+
node,
|
|
4
|
+
shortName,
|
|
5
|
+
author,
|
|
6
|
+
date,
|
|
7
|
+
empty,
|
|
8
|
+
statusLabel,
|
|
9
|
+
renderPost,
|
|
10
|
+
} from "./posts.js";
|
|
11
|
+
|
|
12
|
+
export function createBoardRenderer(loadList, openThread) {
|
|
13
|
+
function renderBoard() {
|
|
14
|
+
const detail = state.detail;
|
|
15
|
+
const channels = [
|
|
16
|
+
{ name: "", label: "All activity" },
|
|
17
|
+
...detail.channels.map((channel) => ({
|
|
18
|
+
name: channel.name,
|
|
19
|
+
label: channel.name,
|
|
20
|
+
posts: channel.posts,
|
|
21
|
+
})),
|
|
22
|
+
];
|
|
23
|
+
$("channels").replaceChildren(
|
|
24
|
+
...channels.map((channel) => {
|
|
25
|
+
const button = node("button", "", channel.label);
|
|
26
|
+
button.type = "button";
|
|
27
|
+
button.dataset.channel = channel.name;
|
|
28
|
+
button.setAttribute(
|
|
29
|
+
"aria-current",
|
|
30
|
+
String(state.channel === channel.name),
|
|
31
|
+
);
|
|
32
|
+
if (channel.posts !== undefined)
|
|
33
|
+
button.append(node("small", "", String(channel.posts)));
|
|
34
|
+
button.onclick = () =>
|
|
35
|
+
run(async () => {
|
|
36
|
+
state.channel = channel.name;
|
|
37
|
+
await loadList();
|
|
38
|
+
renderBoard();
|
|
39
|
+
});
|
|
40
|
+
return button;
|
|
41
|
+
}),
|
|
42
|
+
);
|
|
43
|
+
// Polling never replaces focused controls when their options have not changed.
|
|
44
|
+
const options = [
|
|
45
|
+
{ name: "", label: "All agents" },
|
|
46
|
+
...detail.agents.map((agent) => ({
|
|
47
|
+
name: agent.name,
|
|
48
|
+
label: shortName(agent.name),
|
|
49
|
+
})),
|
|
50
|
+
];
|
|
51
|
+
const signature = JSON.stringify(options);
|
|
52
|
+
if ($("author").dataset.options !== signature) {
|
|
53
|
+
$("author").replaceChildren(
|
|
54
|
+
...options.map((option) => {
|
|
55
|
+
const element = node("option", "", option.label);
|
|
56
|
+
element.value = option.name;
|
|
57
|
+
element.title = option.name;
|
|
58
|
+
return element;
|
|
59
|
+
}),
|
|
60
|
+
);
|
|
61
|
+
$("author").dataset.options = signature;
|
|
62
|
+
}
|
|
63
|
+
$("author").value = state.author;
|
|
64
|
+
const watching = detail.subscriptions.filter(
|
|
65
|
+
(subscription) => subscription.enabled,
|
|
66
|
+
);
|
|
67
|
+
$("subscriptions").replaceChildren(
|
|
68
|
+
...watching.map((subscription) => {
|
|
69
|
+
const line = node("p");
|
|
70
|
+
line.append(
|
|
71
|
+
author(subscription.agent),
|
|
72
|
+
document.createTextNode(` watches ${subscription.target}`),
|
|
73
|
+
);
|
|
74
|
+
return line;
|
|
75
|
+
}),
|
|
76
|
+
);
|
|
77
|
+
$("watching").hidden = !watching.length;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function renderThreads() {
|
|
81
|
+
$("list-title").textContent = state.channel || "All activity";
|
|
82
|
+
$("threads").replaceChildren(
|
|
83
|
+
...state.page.threads.map((thread) => {
|
|
84
|
+
const link = node("a", "thread");
|
|
85
|
+
link.href = selectionURL(thread.id);
|
|
86
|
+
link.dataset.thread = thread.id;
|
|
87
|
+
link.setAttribute("aria-current", String(thread.id === state.thread));
|
|
88
|
+
const meta = node("div", "thread-meta");
|
|
89
|
+
meta.append(
|
|
90
|
+
author(thread.author),
|
|
91
|
+
node("span", "", date(thread.lastActivity)),
|
|
92
|
+
);
|
|
93
|
+
const foot = node("div", "thread-foot");
|
|
94
|
+
foot.append(
|
|
95
|
+
statusLabel(thread.status),
|
|
96
|
+
node("span", "", thread.channel),
|
|
97
|
+
node(
|
|
98
|
+
"span",
|
|
99
|
+
"",
|
|
100
|
+
`${thread.replies} ${thread.replies === 1 ? "reply" : "replies"}`,
|
|
101
|
+
),
|
|
102
|
+
);
|
|
103
|
+
if (thread.summarized) foot.append(node("span", "", "Summary"));
|
|
104
|
+
link.append(meta, node("p", "thread-preview", thread.preview), foot);
|
|
105
|
+
link.onclick = (event) => {
|
|
106
|
+
if (event.metaKey || event.ctrlKey || event.shiftKey || event.altKey)
|
|
107
|
+
return;
|
|
108
|
+
event.preventDefault();
|
|
109
|
+
run(() => openThread(thread.id));
|
|
110
|
+
};
|
|
111
|
+
return link;
|
|
112
|
+
}),
|
|
113
|
+
);
|
|
114
|
+
if (!state.page.threads.length)
|
|
115
|
+
empty(
|
|
116
|
+
$("threads"),
|
|
117
|
+
state.query || state.author ? "No matches" : "No posts",
|
|
118
|
+
);
|
|
119
|
+
$("more-threads").hidden = !state.page.nextCursor;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function renderConversation() {
|
|
123
|
+
const conversation = state.conversation;
|
|
124
|
+
const summary = conversation.summary;
|
|
125
|
+
$("thread-status").replaceChildren(statusLabel(conversation.status));
|
|
126
|
+
$("discussion-mode").hidden = !summary;
|
|
127
|
+
$("full-discussion").checked = state.full;
|
|
128
|
+
const snapshot = $("summary-snapshot");
|
|
129
|
+
snapshot.hidden = !summary;
|
|
130
|
+
snapshot.textContent = summary
|
|
131
|
+
? `Summary saved ${date(summary.createdAt)} · ${summary.provider}/${summary.model} · ${summary.reasoning} · Through ${summary.throughMessageId}`
|
|
132
|
+
: "";
|
|
133
|
+
if (summary?.hasNewerPosts)
|
|
134
|
+
snapshot.prepend(node("strong", "", "New replies after this summary. "));
|
|
135
|
+
$("posts").replaceChildren(
|
|
136
|
+
...(conversation.root
|
|
137
|
+
? [renderPost(conversation.root, true)]
|
|
138
|
+
: summary
|
|
139
|
+
? [renderPost(summary, false, true)]
|
|
140
|
+
: []),
|
|
141
|
+
...conversation.replies.map((post) => renderPost(post, false)),
|
|
142
|
+
);
|
|
143
|
+
$("more-replies").hidden = !conversation.nextCursor;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
return { renderBoard, renderThreads, renderConversation };
|
|
147
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
export const $ = (id) => document.getElementById(id);
|
|
2
|
+
export const fragment = new URLSearchParams(location.hash.slice(1));
|
|
3
|
+
export const token = fragment.get("view");
|
|
4
|
+
export const state = {
|
|
5
|
+
board: "",
|
|
6
|
+
channel: "",
|
|
7
|
+
author: "",
|
|
8
|
+
query: "",
|
|
9
|
+
searchFull: false,
|
|
10
|
+
full: false,
|
|
11
|
+
thread: "",
|
|
12
|
+
detail: null,
|
|
13
|
+
page: null,
|
|
14
|
+
conversation: null,
|
|
15
|
+
firstPage: null,
|
|
16
|
+
firstConversation: null,
|
|
17
|
+
threadPages: 1,
|
|
18
|
+
replyPages: 1,
|
|
19
|
+
};
|
|
20
|
+
let queue = Promise.resolve();
|
|
21
|
+
export let busy = 0;
|
|
22
|
+
export let changed = false;
|
|
23
|
+
|
|
24
|
+
export function run(action) {
|
|
25
|
+
busy++;
|
|
26
|
+
queue = queue.then(async () => {
|
|
27
|
+
try {
|
|
28
|
+
await action();
|
|
29
|
+
$("status").textContent = "";
|
|
30
|
+
} catch (error) {
|
|
31
|
+
$("status").textContent = error.message;
|
|
32
|
+
} finally {
|
|
33
|
+
busy--;
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
return queue;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export async function api(path, params = {}) {
|
|
40
|
+
const url = new URL(path, location.origin);
|
|
41
|
+
for (const [key, value] of Object.entries(params))
|
|
42
|
+
if (value) url.searchParams.set(key, value);
|
|
43
|
+
const response = await fetch(url, {
|
|
44
|
+
headers: { Authorization: `Bearer ${token}` },
|
|
45
|
+
cache: "no-store",
|
|
46
|
+
signal: AbortSignal.timeout(10_000),
|
|
47
|
+
});
|
|
48
|
+
const data = await response.json();
|
|
49
|
+
if (!response.ok) {
|
|
50
|
+
if (response.status === 401) $("live").checked = false;
|
|
51
|
+
throw new Error(
|
|
52
|
+
typeof data.error === "string"
|
|
53
|
+
? data.error
|
|
54
|
+
: `Request failed (${response.status}).`,
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
return data;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function selectionURL(thread = state.thread) {
|
|
61
|
+
const params = new URLSearchParams();
|
|
62
|
+
params.set("view", token);
|
|
63
|
+
if (state.board) params.set("board", state.board);
|
|
64
|
+
if (thread) params.set("thread", thread);
|
|
65
|
+
return `#${params}`;
|
|
66
|
+
}
|
|
67
|
+
export function saveSelection() {
|
|
68
|
+
history.replaceState(null, "", selectionURL());
|
|
69
|
+
}
|
|
70
|
+
export function threadParams() {
|
|
71
|
+
return {
|
|
72
|
+
boardId: state.board,
|
|
73
|
+
channel: state.channel,
|
|
74
|
+
author: state.author,
|
|
75
|
+
q: state.query,
|
|
76
|
+
full: state.searchFull ? "true" : "",
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
export function same(a, b) {
|
|
80
|
+
return JSON.stringify(a) === JSON.stringify(b);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export function setChanged(value) {
|
|
84
|
+
changed = value;
|
|
85
|
+
$("refresh").textContent = value ? "New activity · Refresh" : "Refresh";
|
|
86
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { $, state, api, threadParams, setChanged, same } from "./session.js";
|
|
2
|
+
import { renderPost } from "./posts.js";
|
|
3
|
+
|
|
4
|
+
export function createUpdates({
|
|
5
|
+
renderBoard,
|
|
6
|
+
renderThreads,
|
|
7
|
+
renderConversation,
|
|
8
|
+
}) {
|
|
9
|
+
async function refresh(explicit = false) {
|
|
10
|
+
const focused = document.activeElement;
|
|
11
|
+
const focusKey =
|
|
12
|
+
focused?.dataset?.channel !== undefined
|
|
13
|
+
? ["channel", focused.dataset.channel]
|
|
14
|
+
: focused?.dataset?.thread
|
|
15
|
+
? ["thread", focused.dataset.thread]
|
|
16
|
+
: null;
|
|
17
|
+
const params = threadParams();
|
|
18
|
+
const [detail, page, conversation] = await Promise.all([
|
|
19
|
+
api("/api/board", { boardId: state.board }),
|
|
20
|
+
api("/api/threads", params),
|
|
21
|
+
state.thread
|
|
22
|
+
? api("/api/thread", {
|
|
23
|
+
boardId: state.board,
|
|
24
|
+
id: state.thread,
|
|
25
|
+
full: state.full ? "true" : "",
|
|
26
|
+
})
|
|
27
|
+
: Promise.resolve(null),
|
|
28
|
+
]);
|
|
29
|
+
if (!explicit && (state.threadPages > 1 || state.replyPages > 1)) {
|
|
30
|
+
// Compare only loaded first pages. Never splice fresh cursors into an old tail.
|
|
31
|
+
if (
|
|
32
|
+
!same(detail, state.detail) ||
|
|
33
|
+
!same(page, state.firstPage) ||
|
|
34
|
+
!same(conversation, state.firstConversation)
|
|
35
|
+
)
|
|
36
|
+
setChanged(true);
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
if (!same(detail, state.detail)) {
|
|
40
|
+
state.detail = detail;
|
|
41
|
+
renderBoard();
|
|
42
|
+
}
|
|
43
|
+
if (!same(page, state.page)) {
|
|
44
|
+
state.page = page;
|
|
45
|
+
renderThreads();
|
|
46
|
+
}
|
|
47
|
+
if (conversation && !same(conversation, state.conversation)) {
|
|
48
|
+
state.conversation = conversation;
|
|
49
|
+
renderConversation();
|
|
50
|
+
}
|
|
51
|
+
state.threadPages = 1;
|
|
52
|
+
state.replyPages = 1;
|
|
53
|
+
state.firstPage = page;
|
|
54
|
+
state.firstConversation = conversation;
|
|
55
|
+
setChanged(false);
|
|
56
|
+
if (focusKey && !focused.isConnected) {
|
|
57
|
+
const target = [
|
|
58
|
+
...document.querySelectorAll(`[data-${focusKey[0]}]`),
|
|
59
|
+
].find((element) => element.dataset[focusKey[0]] === focusKey[1]);
|
|
60
|
+
target?.focus({ preventScroll: true });
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
async function moreThreads() {
|
|
65
|
+
if (!state.page.nextCursor) return;
|
|
66
|
+
const page = await api("/api/threads", {
|
|
67
|
+
...threadParams(),
|
|
68
|
+
cursor: state.page.nextCursor,
|
|
69
|
+
});
|
|
70
|
+
if (state.threadPages === 1) state.firstPage = state.page;
|
|
71
|
+
state.page = {
|
|
72
|
+
threads: [
|
|
73
|
+
...state.page.threads,
|
|
74
|
+
...page.threads.filter(
|
|
75
|
+
(thread) => !state.page.threads.some((old) => old.id === thread.id),
|
|
76
|
+
),
|
|
77
|
+
],
|
|
78
|
+
nextCursor: page.nextCursor,
|
|
79
|
+
};
|
|
80
|
+
state.threadPages++;
|
|
81
|
+
renderThreads();
|
|
82
|
+
}
|
|
83
|
+
async function moreReplies() {
|
|
84
|
+
if (!state.conversation.nextCursor) return;
|
|
85
|
+
const page = await api("/api/thread", {
|
|
86
|
+
boardId: state.board,
|
|
87
|
+
id: state.thread,
|
|
88
|
+
full: state.full ? "true" : "",
|
|
89
|
+
cursor: state.conversation.nextCursor,
|
|
90
|
+
});
|
|
91
|
+
if (state.replyPages === 1) state.firstConversation = state.conversation;
|
|
92
|
+
const replies = page.replies.filter(
|
|
93
|
+
(post) => !state.conversation.replies.some((old) => old.id === post.id),
|
|
94
|
+
);
|
|
95
|
+
state.conversation = {
|
|
96
|
+
...state.conversation,
|
|
97
|
+
replies: [...state.conversation.replies, ...replies],
|
|
98
|
+
nextCursor: page.nextCursor,
|
|
99
|
+
};
|
|
100
|
+
state.replyPages++;
|
|
101
|
+
$("posts").append(...replies.map((post) => renderPost(post, false)));
|
|
102
|
+
$("more-replies").hidden = !page.nextCursor;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return { refresh, moreThreads, moreReplies };
|
|
106
|
+
}
|
package/changelog.ts
DELETED
|
@@ -1,337 +0,0 @@
|
|
|
1
|
-
// Generated extension packages copy this file during changelog:extensions.
|
|
2
|
-
|
|
3
|
-
import { randomUUID } from "node:crypto";
|
|
4
|
-
import { readFileSync } from "node:fs";
|
|
5
|
-
import { mkdir, readFile, rename, rm, stat, writeFile } from "node:fs/promises";
|
|
6
|
-
import { dirname, join } from "node:path";
|
|
7
|
-
import { fileURLToPath } from "node:url";
|
|
8
|
-
import {
|
|
9
|
-
DynamicBorder,
|
|
10
|
-
type ExtensionAPI,
|
|
11
|
-
getAgentDir,
|
|
12
|
-
getMarkdownTheme,
|
|
13
|
-
} from "@earendil-works/pi-coding-agent";
|
|
14
|
-
import { Container, Markdown, Spacer, Text } from "@earendil-works/pi-tui";
|
|
15
|
-
|
|
16
|
-
const ENTRY_TYPE = "@howaboua/pi-stuff/changelog";
|
|
17
|
-
const REGISTRATION_CHANNEL = "@howaboua/pi-stuff/changelog/register/v1";
|
|
18
|
-
const STATE_FILENAME = "howaboua-pi-stuff-changelog.json";
|
|
19
|
-
const SUPPRESS_KEY = "suppress";
|
|
20
|
-
const LOCK_STALE_MS = 30_000;
|
|
21
|
-
const LOCK_TIMEOUT_MS = 2_000;
|
|
22
|
-
|
|
23
|
-
interface PackageMetadata {
|
|
24
|
-
name: string;
|
|
25
|
-
version: string;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
interface PackageRegistration extends PackageMetadata {
|
|
29
|
-
changelogPath: string;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
interface RegistrationEvent {
|
|
33
|
-
accept: () => void;
|
|
34
|
-
registration: PackageRegistration;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
interface ChangelogEntry {
|
|
38
|
-
content: string;
|
|
39
|
-
version: string;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
interface ChangelogEntryData {
|
|
43
|
-
markdown: string;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
type ChangelogState = Record<string, boolean | string>;
|
|
47
|
-
|
|
48
|
-
function packageRegistration(): PackageRegistration {
|
|
49
|
-
const packageDirectory = fileURLToPath(new URL(".", import.meta.url));
|
|
50
|
-
const metadata = JSON.parse(
|
|
51
|
-
readFileSync(join(packageDirectory, "package.json"), "utf8"),
|
|
52
|
-
) as Partial<PackageMetadata>;
|
|
53
|
-
if (typeof metadata.name !== "string" || typeof metadata.version !== "string")
|
|
54
|
-
throw new Error(`Invalid package metadata in ${packageDirectory}`);
|
|
55
|
-
return {
|
|
56
|
-
name: metadata.name,
|
|
57
|
-
version: metadata.version,
|
|
58
|
-
changelogPath: join(packageDirectory, "CHANGELOG.md"),
|
|
59
|
-
};
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
function isRegistrationEvent(value: unknown): value is RegistrationEvent {
|
|
63
|
-
if (!value || typeof value !== "object") return false;
|
|
64
|
-
const event = value as Partial<RegistrationEvent>;
|
|
65
|
-
return (
|
|
66
|
-
typeof event.accept === "function" &&
|
|
67
|
-
typeof event.registration?.name === "string" &&
|
|
68
|
-
typeof event.registration.version === "string" &&
|
|
69
|
-
typeof event.registration.changelogPath === "string"
|
|
70
|
-
);
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
function parseChangelog(markdown: string): ChangelogEntry[] {
|
|
74
|
-
const entries: ChangelogEntry[] = [];
|
|
75
|
-
let currentLines: string[] = [];
|
|
76
|
-
let currentVersion: string | undefined;
|
|
77
|
-
|
|
78
|
-
for (const line of markdown.split("\n")) {
|
|
79
|
-
const version = line.match(/^##\s+\[?(\d+\.\d+\.\d+)\]?(?:\s.*)?$/)?.[1];
|
|
80
|
-
if (version) {
|
|
81
|
-
if (currentVersion)
|
|
82
|
-
entries.push({
|
|
83
|
-
content: currentLines.join("\n").trim(),
|
|
84
|
-
version: currentVersion,
|
|
85
|
-
});
|
|
86
|
-
currentVersion = version;
|
|
87
|
-
currentLines = [line];
|
|
88
|
-
} else if (currentVersion) {
|
|
89
|
-
currentLines.push(line);
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
if (currentVersion)
|
|
94
|
-
entries.push({
|
|
95
|
-
content: currentLines.join("\n").trim(),
|
|
96
|
-
version: currentVersion,
|
|
97
|
-
});
|
|
98
|
-
return entries;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
function compareVersions(left: string, right: string): number {
|
|
102
|
-
const leftParts = left.split(".").map(Number);
|
|
103
|
-
const rightParts = right.split(".").map(Number);
|
|
104
|
-
for (let index = 0; index < 3; index++) {
|
|
105
|
-
const difference = (leftParts[index] ?? 0) - (rightParts[index] ?? 0);
|
|
106
|
-
if (difference !== 0) return difference;
|
|
107
|
-
}
|
|
108
|
-
return 0;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
function isStableVersion(version: string): boolean {
|
|
112
|
-
return /^\d+\.\d+\.\d+$/.test(version);
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
function statePath(): string {
|
|
116
|
-
return join(getAgentDir(), STATE_FILENAME);
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
async function readState(path: string): Promise<ChangelogState> {
|
|
120
|
-
let text: string;
|
|
121
|
-
try {
|
|
122
|
-
text = await readFile(path, "utf8");
|
|
123
|
-
} catch (error) {
|
|
124
|
-
if ((error as NodeJS.ErrnoException).code === "ENOENT") return {};
|
|
125
|
-
throw error;
|
|
126
|
-
}
|
|
127
|
-
const parsed = JSON.parse(text) as unknown;
|
|
128
|
-
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed))
|
|
129
|
-
throw new Error(`${path} must contain a JSON object`);
|
|
130
|
-
for (const [name, value] of Object.entries(parsed)) {
|
|
131
|
-
if (name === SUPPRESS_KEY) {
|
|
132
|
-
if (typeof value !== "boolean")
|
|
133
|
-
throw new Error(`${path} ${SUPPRESS_KEY} must be a boolean`);
|
|
134
|
-
} else if (typeof value !== "string") {
|
|
135
|
-
throw new Error(`${path} version for ${name} must be a string`);
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
return parsed as ChangelogState;
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
async function acquireLock(path: string): Promise<() => Promise<void>> {
|
|
142
|
-
const deadline = Date.now() + LOCK_TIMEOUT_MS;
|
|
143
|
-
while (true) {
|
|
144
|
-
try {
|
|
145
|
-
await mkdir(path);
|
|
146
|
-
return () => rm(path, { force: true, recursive: true });
|
|
147
|
-
} catch (error) {
|
|
148
|
-
if ((error as NodeJS.ErrnoException).code !== "EEXIST") throw error;
|
|
149
|
-
const info = await stat(path).catch(() => undefined);
|
|
150
|
-
if (info && Date.now() - info.mtimeMs > LOCK_STALE_MS) {
|
|
151
|
-
await rm(path, { force: true, recursive: true });
|
|
152
|
-
continue;
|
|
153
|
-
}
|
|
154
|
-
if (Date.now() >= deadline)
|
|
155
|
-
throw new Error(`Timed out waiting for ${path}`);
|
|
156
|
-
await new Promise((resolve) => setTimeout(resolve, 20));
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
async function writeState(path: string, state: ChangelogState): Promise<void> {
|
|
162
|
-
await mkdir(dirname(path), { mode: 0o700, recursive: true });
|
|
163
|
-
const temporaryPath = `${path}.${process.pid}.${randomUUID()}.tmp`;
|
|
164
|
-
try {
|
|
165
|
-
const ordered = Object.fromEntries(
|
|
166
|
-
Object.entries(state).sort(([left], [right]) =>
|
|
167
|
-
left.localeCompare(right),
|
|
168
|
-
),
|
|
169
|
-
);
|
|
170
|
-
await writeFile(temporaryPath, `${JSON.stringify(ordered, null, 2)}\n`, {
|
|
171
|
-
encoding: "utf8",
|
|
172
|
-
mode: 0o600,
|
|
173
|
-
});
|
|
174
|
-
await rename(temporaryPath, path);
|
|
175
|
-
} catch (error) {
|
|
176
|
-
await rm(temporaryPath, { force: true });
|
|
177
|
-
throw error;
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
function packageMarkdown(
|
|
182
|
-
registration: PackageRegistration,
|
|
183
|
-
entries: ChangelogEntry[],
|
|
184
|
-
): string {
|
|
185
|
-
const body = (entry: ChangelogEntry) =>
|
|
186
|
-
entry.content
|
|
187
|
-
.replace(/^##[^\n]*\n*/, "")
|
|
188
|
-
.replace(/^###\s+(?:Changes|Minor Changes|Patch Changes)\s*\n*/gim, "")
|
|
189
|
-
.replace(/^###\s+(.+)$/gm, "**$1**");
|
|
190
|
-
if (entries.length === 1) {
|
|
191
|
-
const [entry] = entries;
|
|
192
|
-
if (!entry) return "";
|
|
193
|
-
return `## ${registration.name} ${entry.version}\n\n${body(entry)}`;
|
|
194
|
-
}
|
|
195
|
-
return [
|
|
196
|
-
`## ${registration.name}`,
|
|
197
|
-
...entries.map((entry) => `**${entry.version}**\n\n${body(entry)}`),
|
|
198
|
-
].join("\n\n");
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
async function claimUpdates(
|
|
202
|
-
registrations: Iterable<PackageRegistration>,
|
|
203
|
-
): Promise<{ errors: string[]; markdown?: string }> {
|
|
204
|
-
const stableRegistrations = [...registrations]
|
|
205
|
-
.filter((registration) => isStableVersion(registration.version))
|
|
206
|
-
.sort((left, right) => left.name.localeCompare(right.name));
|
|
207
|
-
if (stableRegistrations.length === 0) return { errors: [] };
|
|
208
|
-
const path = statePath();
|
|
209
|
-
await mkdir(dirname(path), { mode: 0o700, recursive: true });
|
|
210
|
-
const release = await acquireLock(`${path}.lock`);
|
|
211
|
-
try {
|
|
212
|
-
const state = await readState(path);
|
|
213
|
-
const errors: string[] = [];
|
|
214
|
-
const sections: string[] = [];
|
|
215
|
-
let changed = false;
|
|
216
|
-
if (state[SUPPRESS_KEY] === true) {
|
|
217
|
-
for (const registration of stableRegistrations) {
|
|
218
|
-
const seenVersion = state[registration.name];
|
|
219
|
-
if (
|
|
220
|
-
typeof seenVersion !== "string" ||
|
|
221
|
-
compareVersions(registration.version, seenVersion) > 0
|
|
222
|
-
) {
|
|
223
|
-
state[registration.name] = registration.version;
|
|
224
|
-
changed = true;
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
if (changed) await writeState(path, state);
|
|
228
|
-
return { errors };
|
|
229
|
-
}
|
|
230
|
-
for (const registration of stableRegistrations) {
|
|
231
|
-
const seenVersion = state[registration.name];
|
|
232
|
-
if (
|
|
233
|
-
typeof seenVersion === "string" &&
|
|
234
|
-
compareVersions(registration.version, seenVersion) <= 0
|
|
235
|
-
)
|
|
236
|
-
continue;
|
|
237
|
-
try {
|
|
238
|
-
const entries = parseChangelog(
|
|
239
|
-
await readFile(registration.changelogPath, "utf8"),
|
|
240
|
-
).filter(
|
|
241
|
-
(entry) =>
|
|
242
|
-
compareVersions(entry.version, registration.version) <= 0 &&
|
|
243
|
-
(typeof seenVersion === "string"
|
|
244
|
-
? compareVersions(entry.version, seenVersion) > 0
|
|
245
|
-
: entry.version === registration.version),
|
|
246
|
-
);
|
|
247
|
-
if (entries.length === 0) {
|
|
248
|
-
errors.push(
|
|
249
|
-
`No ${registration.version} changelog entry found for ${registration.name}`,
|
|
250
|
-
);
|
|
251
|
-
continue;
|
|
252
|
-
}
|
|
253
|
-
sections.push(packageMarkdown(registration, entries));
|
|
254
|
-
state[registration.name] = registration.version;
|
|
255
|
-
changed = true;
|
|
256
|
-
} catch (error) {
|
|
257
|
-
errors.push(
|
|
258
|
-
`Could not read the ${registration.name} changelog: ${error instanceof Error ? error.message : String(error)}`,
|
|
259
|
-
);
|
|
260
|
-
}
|
|
261
|
-
}
|
|
262
|
-
if (changed) await writeState(path, state);
|
|
263
|
-
return {
|
|
264
|
-
errors,
|
|
265
|
-
...(sections.length > 0 ? { markdown: sections.join("\n\n") } : {}),
|
|
266
|
-
};
|
|
267
|
-
} finally {
|
|
268
|
-
await release();
|
|
269
|
-
}
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
function registerCoordinator(
|
|
273
|
-
pi: ExtensionAPI,
|
|
274
|
-
registration: PackageRegistration,
|
|
275
|
-
): void {
|
|
276
|
-
let accepted = false;
|
|
277
|
-
pi.events.emit(REGISTRATION_CHANNEL, {
|
|
278
|
-
registration,
|
|
279
|
-
accept: () => {
|
|
280
|
-
accepted = true;
|
|
281
|
-
},
|
|
282
|
-
} satisfies RegistrationEvent);
|
|
283
|
-
if (accepted) return;
|
|
284
|
-
|
|
285
|
-
const registrations = new Map([[registration.name, registration]]);
|
|
286
|
-
pi.events.on(REGISTRATION_CHANNEL, (value) => {
|
|
287
|
-
if (!isRegistrationEvent(value)) return;
|
|
288
|
-
registrations.set(value.registration.name, value.registration);
|
|
289
|
-
value.accept();
|
|
290
|
-
});
|
|
291
|
-
pi.registerEntryRenderer<ChangelogEntryData>(
|
|
292
|
-
ENTRY_TYPE,
|
|
293
|
-
(entry, _options, theme) => {
|
|
294
|
-
if (!entry.data) return undefined;
|
|
295
|
-
const container = new Container();
|
|
296
|
-
container.addChild(new DynamicBorder());
|
|
297
|
-
container.addChild(
|
|
298
|
-
new Text(theme.bold(theme.fg("accent", "What's New")), 1, 0),
|
|
299
|
-
);
|
|
300
|
-
container.addChild(new Spacer(1));
|
|
301
|
-
container.addChild(
|
|
302
|
-
new Markdown(entry.data.markdown.trim(), 1, 0, getMarkdownTheme()),
|
|
303
|
-
);
|
|
304
|
-
container.addChild(new Spacer(1));
|
|
305
|
-
container.addChild(new DynamicBorder());
|
|
306
|
-
return container;
|
|
307
|
-
},
|
|
308
|
-
);
|
|
309
|
-
pi.on("session_start", async (event, ctx) => {
|
|
310
|
-
if (event.reason === "reload" || ctx.mode !== "tui") return;
|
|
311
|
-
if (
|
|
312
|
-
ctx.sessionManager
|
|
313
|
-
.getEntries()
|
|
314
|
-
.some((entry) =>
|
|
315
|
-
["branch_summary", "compaction", "message"].includes(entry.type),
|
|
316
|
-
)
|
|
317
|
-
)
|
|
318
|
-
return;
|
|
319
|
-
try {
|
|
320
|
-
const update = await claimUpdates(registrations.values());
|
|
321
|
-
if (update.markdown)
|
|
322
|
-
pi.appendEntry<ChangelogEntryData>(ENTRY_TYPE, {
|
|
323
|
-
markdown: update.markdown,
|
|
324
|
-
});
|
|
325
|
-
for (const error of update.errors) ctx.ui.notify(error, "warning");
|
|
326
|
-
} catch (error) {
|
|
327
|
-
ctx.ui.notify(
|
|
328
|
-
`Could not update Howaboua changelog state: ${error instanceof Error ? error.message : String(error)}`,
|
|
329
|
-
"warning",
|
|
330
|
-
);
|
|
331
|
-
}
|
|
332
|
-
});
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
export default function howabouaPackageChangelog(pi: ExtensionAPI): void {
|
|
336
|
-
registerCoordinator(pi, packageRegistration());
|
|
337
|
-
}
|