@howaboua/pi-agent-comms 0.0.0-stage → 0.0.1-dev.13.1.gef1ce49691d6

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.
Files changed (141) hide show
  1. package/INTEGRATION.md +31 -0
  2. package/LICENSE +21 -0
  3. package/PCC-INTEGRATION.md +17 -0
  4. package/README.md +53 -2
  5. package/dist/viewer/archive.js +249 -0
  6. package/dist/viewer/config.js +85 -0
  7. package/dist/viewer/contracts.js +1 -0
  8. package/dist/viewer/server.js +177 -0
  9. package/dist/viewer/source.js +29 -0
  10. package/dist/viewer/web/app.js +88 -0
  11. package/dist/viewer/web/index.html +41 -0
  12. package/dist/viewer/web/navigation.js +72 -0
  13. package/dist/viewer/web/posts.js +105 -0
  14. package/dist/viewer/web/render.js +147 -0
  15. package/dist/viewer/web/session.js +86 -0
  16. package/dist/viewer/web/style.css +517 -0
  17. package/dist/viewer/web/updates.js +106 -0
  18. package/docs/BOARD-CHANGELOG.md +49 -0
  19. package/docs/BOARD.md +124 -0
  20. package/docs/NOTES-CHANGELOG.md +127 -0
  21. package/docs/NOTES.md +122 -0
  22. package/index.ts +21 -0
  23. package/package.json +75 -5
  24. package/src/board/AGENTS.md +12 -0
  25. package/src/board/activity.ts +274 -0
  26. package/src/board/archive.ts +118 -0
  27. package/src/board/awareness.ts +179 -0
  28. package/src/board/briefing.ts +25 -0
  29. package/src/board/config.ts +192 -0
  30. package/src/board/consolidation.ts +32 -0
  31. package/src/board/context-briefing.ts +245 -0
  32. package/src/board/contract.ts +254 -0
  33. package/src/board/delivery.ts +85 -0
  34. package/src/board/feature.ts +91 -0
  35. package/src/board/identity.ts +238 -0
  36. package/src/board/integration.ts +55 -0
  37. package/src/board/paging.ts +54 -0
  38. package/src/board/protocol.ts +78 -0
  39. package/src/board/queries.ts +168 -0
  40. package/src/board/response.ts +82 -0
  41. package/src/board/runtime/activation.ts +50 -0
  42. package/src/board/runtime/catchup.ts +96 -0
  43. package/src/board/runtime/coordinator.ts +311 -0
  44. package/src/board/runtime/engine.ts +40 -0
  45. package/src/board/runtime/index.ts +63 -0
  46. package/src/board/runtime/owner.ts +168 -0
  47. package/src/board/runtime/routing.ts +152 -0
  48. package/src/board/runtime/turns.ts +109 -0
  49. package/src/board/search.ts +95 -0
  50. package/src/board/settings/panel.ts +193 -0
  51. package/src/board/store.ts +427 -0
  52. package/src/board/summaries.ts +124 -0
  53. package/src/board/summarizer.ts +201 -0
  54. package/src/board/tool.ts +27 -0
  55. package/src/board/viewer/AGENTS.md +1 -0
  56. package/src/board/viewer/archive.ts +308 -0
  57. package/src/board/viewer/config.ts +111 -0
  58. package/src/board/viewer/contracts.ts +98 -0
  59. package/src/board/viewer/index.ts +162 -0
  60. package/src/board/viewer/server.ts +204 -0
  61. package/src/board/viewer/source.ts +33 -0
  62. package/src/board/viewer/web/app.js +88 -0
  63. package/src/board/viewer/web/index.html +41 -0
  64. package/src/board/viewer/web/navigation.js +72 -0
  65. package/src/board/viewer/web/posts.js +105 -0
  66. package/src/board/viewer/web/render.js +147 -0
  67. package/src/board/viewer/web/session.js +86 -0
  68. package/src/board/viewer/web/style.css +517 -0
  69. package/src/board/viewer/web/updates.js +106 -0
  70. package/src/guidance.ts +121 -0
  71. package/src/notes/AGENTS.md +27 -0
  72. package/src/notes/activation.ts +150 -0
  73. package/src/notes/agent-identity.ts +221 -0
  74. package/src/notes/attachment-sharing.ts +165 -0
  75. package/src/notes/attachment-snapshot.ts +113 -0
  76. package/src/notes/binding-startup.ts +30 -0
  77. package/src/notes/bridge.ts +130 -0
  78. package/src/notes/compaction-projection.ts +88 -0
  79. package/src/notes/context-owner.ts +99 -0
  80. package/src/notes/context-sharing.ts +178 -0
  81. package/src/notes/extension-hooks.ts +200 -0
  82. package/src/notes/extension-sharing.ts +133 -0
  83. package/src/notes/feature.ts +168 -0
  84. package/src/notes/fresh-checkpoint.ts +143 -0
  85. package/src/notes/fresh-notes.ts +323 -0
  86. package/src/notes/history-search-protocol.ts +39 -0
  87. package/src/notes/history-search-pruning.mjs +120 -0
  88. package/src/notes/history-search-worker.mjs +258 -0
  89. package/src/notes/history-search.ts +269 -0
  90. package/src/notes/history-windows.ts +221 -0
  91. package/src/notes/history.ts +158 -0
  92. package/src/notes/idle-admission.ts +201 -0
  93. package/src/notes/index.ts +125 -0
  94. package/src/notes/integrations.ts +174 -0
  95. package/src/notes/lifecycle.ts +308 -0
  96. package/src/notes/model-role-config.ts +79 -0
  97. package/src/notes/model-role-screen.ts +74 -0
  98. package/src/notes/model-roles.ts +225 -0
  99. package/src/notes/note-query.ts +148 -0
  100. package/src/notes/note-result.ts +88 -0
  101. package/src/notes/note-save-marker.ts +128 -0
  102. package/src/notes/notes-lookup.ts +115 -0
  103. package/src/notes/notes.ts +302 -0
  104. package/src/notes/query-executor.ts +242 -0
  105. package/src/notes/query-input.ts +126 -0
  106. package/src/notes/remote.ts +376 -0
  107. package/src/notes/rollover.ts +224 -0
  108. package/src/notes/saved-session-access.ts +117 -0
  109. package/src/notes/saved-session-index.mjs +83 -0
  110. package/src/notes/saved-session.mjs +241 -0
  111. package/src/notes/saved-session.ts +51 -0
  112. package/src/notes/saved-sharing-records.ts +135 -0
  113. package/src/notes/session-pruning-content.ts +237 -0
  114. package/src/notes/session-pruning-plan.ts +243 -0
  115. package/src/notes/session-pruning-policy.ts +20 -0
  116. package/src/notes/session-pruning-screen.ts +204 -0
  117. package/src/notes/session-pruning-source.ts +44 -0
  118. package/src/notes/session-pruning-transaction.ts +141 -0
  119. package/src/notes/session-pruning.ts +155 -0
  120. package/src/notes/settings-command.ts +95 -0
  121. package/src/notes/settings-controls.ts +199 -0
  122. package/src/notes/settings-screen.ts +113 -0
  123. package/src/notes/settings.ts +287 -0
  124. package/src/notes/shared-notes.ts +411 -0
  125. package/src/notes/shared-session-reader.ts +143 -0
  126. package/src/notes/sharing-service.ts +282 -0
  127. package/src/notes/sqlite.mjs +33 -0
  128. package/src/notes/store.ts +283 -0
  129. package/src/notes/task-boundary.ts +37 -0
  130. package/src/notes/tool-contracts.ts +312 -0
  131. package/src/notes/tools.ts +148 -0
  132. package/src/notes/tree-archive-manifest.mjs +27 -0
  133. package/src/notes/tree-archive.ts +129 -0
  134. package/src/notes/tree-checkpoint.ts +170 -0
  135. package/src/notes/tree-handoff-read.ts +192 -0
  136. package/src/notes/window-checkpoint.ts +105 -0
  137. package/src/notes/window-message.ts +185 -0
  138. package/src/notes/windows.ts +272 -0
  139. package/src/settings-navigation.ts +11 -0
  140. package/src/timestamps.ts +3 -0
  141. package/tsconfig.json +9 -0
@@ -0,0 +1,29 @@
1
+ import { isAbsolute } from "node:path";
2
+ export function parseSource(value) {
3
+ if (!value || typeof value !== "object" || Array.isArray(value))
4
+ throw new Error("Invalid viewer source");
5
+ const fields = value;
6
+ const required = (key) => {
7
+ const field = fields[key];
8
+ if (typeof field !== "string" ||
9
+ !field.length ||
10
+ field.length > 4096 ||
11
+ field.includes("\0"))
12
+ throw new Error(`Invalid viewer ${key}`);
13
+ return field;
14
+ };
15
+ const folder = required("folder");
16
+ const databasePath = required("databasePath");
17
+ if (!isAbsolute(folder) || !isAbsolute(databasePath))
18
+ throw new Error("Viewer source paths must be absolute");
19
+ const sessionName = fields["sessionName"];
20
+ if (sessionName !== null && typeof sessionName !== "string")
21
+ throw new Error("Invalid viewer session name");
22
+ return {
23
+ sessionId: required("sessionId"),
24
+ boardId: required("boardId"),
25
+ sessionName,
26
+ folder,
27
+ databasePath,
28
+ };
29
+ }
@@ -0,0 +1,88 @@
1
+ import {
2
+ $,
3
+ state,
4
+ token,
5
+ fragment,
6
+ run,
7
+ api,
8
+ busy,
9
+ changed,
10
+ } from "./session.js";
11
+ import { node, date } from "./posts.js";
12
+ import { createBoardRenderer } from "./render.js";
13
+ import { createNavigation } from "./navigation.js";
14
+ import { createUpdates } from "./updates.js";
15
+
16
+ const renderer = createBoardRenderer(
17
+ () => loadList(),
18
+ (...args) => openThread(...args),
19
+ );
20
+ const { loadList, openThread, loadBoard } = createNavigation(renderer);
21
+ const { refresh, moreThreads, moreReplies } = createUpdates(renderer);
22
+
23
+ $("boards").onchange = () => run(() => loadBoard($("boards").value));
24
+ $("author").onchange = () =>
25
+ run(async () => {
26
+ state.author = $("author").value;
27
+ await loadList();
28
+ });
29
+ $("search-form").onsubmit = (event) => {
30
+ event.preventDefault();
31
+ run(async () => {
32
+ state.query = $("search").value.trim();
33
+ state.searchFull = $("search-full").checked;
34
+ await loadList();
35
+ });
36
+ };
37
+ $("search-full").onchange = () =>
38
+ run(async () => {
39
+ state.searchFull = $("search-full").checked;
40
+ await loadList();
41
+ });
42
+ $("full-discussion").onchange = () =>
43
+ run(() => openThread(state.thread, $("full-discussion").checked));
44
+ $("refresh").onclick = () => run(() => refresh(true));
45
+ $("back").onclick = () => {
46
+ document.body.classList.remove("detail-open");
47
+ const selected = [...$("threads").querySelectorAll("a")].find(
48
+ (link) => link.dataset.thread === state.thread,
49
+ );
50
+ (selected || $("search")).focus();
51
+ };
52
+ $("more-threads").onclick = () => run(moreThreads);
53
+ $("more-replies").onclick = () => run(moreReplies);
54
+
55
+ async function start() {
56
+ if (!token)
57
+ throw new Error(
58
+ "This link has no viewing key. Open the viewer again with /board.",
59
+ );
60
+ const index = await api("/api/boards");
61
+ $("session").textContent = index.context.sessionName || "Current session";
62
+ $("session").title = index.context.folder;
63
+ $("boards").replaceChildren(
64
+ ...index.boards.map((board) => {
65
+ const label =
66
+ board.id === index.context.boardId
67
+ ? "Current session"
68
+ : `${board.createdAt ? date(board.createdAt) : "Saved board"} · ${board.id.slice(0, 8)}`;
69
+ const option = node("option", "", label);
70
+ option.value = board.id;
71
+ option.title = board.id;
72
+ return option;
73
+ }),
74
+ );
75
+ const requestedBoard = fragment.get("board");
76
+ const board = index.boards.some((item) => item.id === requestedBoard)
77
+ ? requestedBoard
78
+ : index.context.boardId;
79
+ $("boards").value = board;
80
+ await loadBoard(board);
81
+ const requestedThread = fragment.get("thread");
82
+ if (requestedThread) await openThread(requestedThread);
83
+ }
84
+ run(start);
85
+ setInterval(() => {
86
+ if (!document.hidden && $("live").checked && state.board && !busy && !changed)
87
+ run(() => refresh());
88
+ }, 5000);
@@ -0,0 +1,41 @@
1
+ <!doctype html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="utf-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1">
6
+ <meta name="referrer" content="no-referrer">
7
+ <title>Board</title>
8
+ <link rel="stylesheet" href="/style.css">
9
+ <script type="module" src="/app.js"></script>
10
+ </head>
11
+ <body>
12
+ <header class="masthead">
13
+ <h1>Board</h1>
14
+ <div class="live-controls"><label><input id="live" type="checkbox" checked> Live</label><button id="refresh" type="button">Refresh</button></div>
15
+ </header>
16
+ <div id="status" role="status" aria-live="polite"></div>
17
+ <div class="workspace">
18
+ <aside class="rail" aria-label="Board navigation">
19
+ <label for="boards">Board</label><select id="boards"></select>
20
+ <p id="session" class="context"></p>
21
+ <nav id="channels" aria-label="Channels"></nav>
22
+ <div class="agent-filter"><label for="author">Agent</label><select id="author"><option value="">All agents</option></select></div>
23
+ <details id="watching"><summary>Watching</summary><div id="subscriptions"></div></details>
24
+ </aside>
25
+ <section class="index" aria-labelledby="list-title">
26
+ <div class="index-head"><h2 id="list-title">All activity</h2>
27
+ <form id="search-form" role="search"><label for="search">Search threads</label><div class="search-row"><input id="search" type="search"><button type="submit">Search</button></div><label class="mode-control"><input id="search-full" type="checkbox"> Search full discussions</label><p class="search-hint">Searches summaries where available, otherwise posts.</p></form>
28
+ </div>
29
+ <div id="threads" class="thread-list"></div>
30
+ <button id="more-threads" class="page-button" hidden>More threads</button>
31
+ </section>
32
+ <main id="conversation" class="conversation" aria-label="Conversation">
33
+ <button id="back" type="button">Back to threads</button>
34
+ <div class="conversation-tools"><div id="thread-status"></div><label id="discussion-mode" class="mode-control" hidden><input id="full-discussion" type="checkbox"> Full discussion</label></div>
35
+ <p id="summary-snapshot" hidden></p>
36
+ <div id="posts"></div>
37
+ <button id="more-replies" class="page-button" hidden>More replies</button>
38
+ </main>
39
+ </div>
40
+ </body>
41
+ </html>
@@ -0,0 +1,72 @@
1
+ import {
2
+ $,
3
+ state,
4
+ api,
5
+ saveSelection,
6
+ threadParams,
7
+ setChanged,
8
+ } from "./session.js";
9
+
10
+ export function createNavigation({
11
+ renderBoard,
12
+ renderThreads,
13
+ renderConversation,
14
+ }) {
15
+ function clearConversation() {
16
+ state.thread = "";
17
+ state.conversation = null;
18
+ state.firstConversation = null;
19
+ state.replyPages = 1;
20
+ $("more-replies").hidden = true;
21
+ document.body.classList.remove("detail-open");
22
+ $("posts").replaceChildren();
23
+ $("thread-status").replaceChildren();
24
+ $("discussion-mode").hidden = true;
25
+ $("summary-snapshot").hidden = true;
26
+ saveSelection();
27
+ }
28
+ async function loadList() {
29
+ const page = await api("/api/threads", threadParams());
30
+ state.page = page;
31
+ state.firstPage = page;
32
+ state.threadPages = 1;
33
+ clearConversation();
34
+ renderThreads();
35
+ $("threads").closest(".index").scrollTop = 0;
36
+ setChanged(false);
37
+ if (page.threads[0]) await openThread(page.threads[0].id);
38
+ }
39
+ async function openThread(id, full = false) {
40
+ const conversation = await api("/api/thread", {
41
+ boardId: state.board,
42
+ id,
43
+ full: full ? "true" : "",
44
+ });
45
+ state.full = full;
46
+ state.thread = id;
47
+ state.conversation = conversation;
48
+ state.firstConversation = conversation;
49
+ state.replyPages = 1;
50
+ renderConversation();
51
+ for (const link of $("threads").querySelectorAll("a"))
52
+ link.setAttribute("aria-current", String(link.dataset.thread === id));
53
+ saveSelection();
54
+ document.body.classList.add("detail-open");
55
+ $("conversation").scrollTop = 0;
56
+ if (matchMedia("(max-width: 760px)").matches) {
57
+ window.scrollTo(0, 0);
58
+ $("back").focus();
59
+ }
60
+ }
61
+ async function loadBoard(id) {
62
+ const detail = await api("/api/board", { boardId: id });
63
+ state.board = id;
64
+ state.detail = detail;
65
+ state.channel = "";
66
+ state.author = "";
67
+ renderBoard();
68
+ await loadList();
69
+ }
70
+
71
+ return { clearConversation, loadList, openThread, loadBoard };
72
+ }
@@ -0,0 +1,105 @@
1
+ import { marked } from "/vendor/marked.js";
2
+ import DOMPurify from "/vendor/purify.js";
3
+
4
+ export function node(tag, className, text) {
5
+ const element = document.createElement(tag);
6
+ if (className) element.className = className;
7
+ if (text !== undefined) element.textContent = text;
8
+ return element;
9
+ }
10
+
11
+ export function shortName(name) {
12
+ return (name.split("/").filter(Boolean).at(-1) || name).replace(
13
+ /-[a-f0-9]{8}(?:-[a-f0-9]{4}){3}-[a-f0-9]{12}$/i,
14
+ "",
15
+ );
16
+ }
17
+ export function author(name) {
18
+ const element = node("span", "author", shortName(name));
19
+ element.title = name;
20
+ return element;
21
+ }
22
+ export function date(value) {
23
+ const parsed = new Date(value);
24
+ return Number.isNaN(parsed.getTime())
25
+ ? value
26
+ : parsed.toLocaleString(undefined, {
27
+ month: "short",
28
+ day: "numeric",
29
+ hour: "2-digit",
30
+ minute: "2-digit",
31
+ });
32
+ }
33
+ export function empty(target, text) {
34
+ target.replaceChildren(node("p", "empty", text));
35
+ }
36
+ export function statusLabel(status) {
37
+ return node(
38
+ "span",
39
+ `thread-status status-${status}`,
40
+ {
41
+ open: "Open",
42
+ completed: "Completed",
43
+ abandoned: "Abandoned",
44
+ }[status],
45
+ );
46
+ }
47
+
48
+ export function renderPost(post, root, summary = false) {
49
+ const article = node("article", "post");
50
+ if (root) article.append(node("div", "post-channel", post.channel));
51
+ const header = node("header", "post-header");
52
+ const time = node("time", "", date(post.createdAt));
53
+ time.dateTime = post.createdAt;
54
+ header.append(
55
+ summary ? node("strong", "", "Summary") : author(post.author),
56
+ time,
57
+ );
58
+ const body = node("div", "prose");
59
+ body.innerHTML = DOMPurify.sanitize(
60
+ marked.parse(post.text, { async: false }),
61
+ {
62
+ ALLOWED_TAGS: [
63
+ "p",
64
+ "br",
65
+ "hr",
66
+ "strong",
67
+ "em",
68
+ "del",
69
+ "blockquote",
70
+ "pre",
71
+ "code",
72
+ "ul",
73
+ "ol",
74
+ "li",
75
+ "h1",
76
+ "h2",
77
+ "h3",
78
+ "h4",
79
+ "h5",
80
+ "h6",
81
+ "a",
82
+ "table",
83
+ "thead",
84
+ "tbody",
85
+ "tr",
86
+ "th",
87
+ "td",
88
+ ],
89
+ ALLOWED_ATTR: ["href", "title", "start", "colspan", "rowspan"],
90
+ ALLOW_DATA_ATTR: false,
91
+ },
92
+ );
93
+ for (const link of body.querySelectorAll("a")) {
94
+ const href = link.getAttribute("href");
95
+ // Relative links could carry the capability fragment to another local page.
96
+ if (!href || !/^(https?:|mailto:)/i.test(href))
97
+ link.removeAttribute("href");
98
+ else {
99
+ link.rel = "noopener noreferrer";
100
+ link.target = "_blank";
101
+ }
102
+ }
103
+ article.append(header, body);
104
+ return article;
105
+ }
@@ -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
+ }