@bigmistqke/workpad 0.0.0-stage → 0.18.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +781 -2
- package/bin/workpad.ts +1526 -0
- package/dist/_virtual/_@oxc-project_runtime@0.156.0/helpers/esm/taggedTemplateLiteral.js +5 -0
- package/dist/bin/workpad.js +1202 -0
- package/dist/client/runtime.js +1194 -0
- package/dist/define.js +27 -0
- package/dist/plugins/alert/README.md +18 -0
- package/dist/plugins/alert/index.js +40 -0
- package/dist/plugins/alert/public/style.css +40 -0
- package/dist/plugins/ansi/README.md +36 -0
- package/dist/plugins/ansi/index.js +63 -0
- package/dist/plugins/ansi/public/style.css +28 -0
- package/dist/plugins/badge/README.md +32 -0
- package/dist/plugins/badge/index.js +28 -0
- package/dist/plugins/badge/public/style.css +30 -0
- package/dist/plugins/chat/README.md +81 -0
- package/dist/plugins/chat/index.js +96 -0
- package/dist/plugins/chat/public/client.js +3 -0
- package/dist/plugins/chat/public/style.css +143 -0
- package/dist/plugins/claude/README.md +116 -0
- package/dist/plugins/claude/channel.js +130 -0
- package/dist/plugins/claude/doctor.js +316 -0
- package/dist/plugins/claude/index.js +199 -0
- package/dist/plugins/claude/session.js +68 -0
- package/dist/plugins/code/README.md +54 -0
- package/dist/plugins/code/diff.js +75 -0
- package/dist/plugins/code/index.js +105 -0
- package/dist/plugins/code/public/client.js +1 -0
- package/dist/plugins/code/public/style.css +99 -0
- package/dist/plugins/comments/README.md +30 -0
- package/dist/plugins/comments/index.js +39 -0
- package/dist/plugins/comments/public/client.js +24 -0
- package/dist/plugins/comments/public/style.css +224 -0
- package/dist/plugins/details/README.md +24 -0
- package/dist/plugins/details/index.js +28 -0
- package/dist/plugins/details/public/style.css +7 -0
- package/dist/plugins/draft/README.md +14 -0
- package/dist/plugins/draft/index.js +27 -0
- package/dist/plugins/draft/public/style.css +21 -0
- package/dist/plugins/forge/README.md +22 -0
- package/dist/plugins/forge/index.js +83 -0
- package/dist/plugins/form/README.md +126 -0
- package/dist/plugins/form/index.js +179 -0
- package/dist/plugins/form/public/style.css +172 -0
- package/dist/plugins/grid/README.md +45 -0
- package/dist/plugins/grid/index.js +35 -0
- package/dist/plugins/grid/public/style.css +98 -0
- package/dist/plugins/iframe/README.md +18 -0
- package/dist/plugins/iframe/index.js +49 -0
- package/dist/plugins/iframe/public/style.css +28 -0
- package/dist/plugins/index.js +44 -0
- package/dist/plugins/lightbox/README.md +24 -0
- package/dist/plugins/lightbox/index.js +20 -0
- package/dist/plugins/lightbox/public/client.js +4 -0
- package/dist/plugins/lightbox/public/style.css +66 -0
- package/dist/plugins/mermaid/README.md +21 -0
- package/dist/plugins/mermaid/index.js +42 -0
- package/dist/plugins/mermaid/public/style.css +21 -0
- package/dist/plugins/opencode/README.md +48 -0
- package/dist/plugins/opencode/channel.js +92 -0
- package/dist/plugins/opencode/index.js +64 -0
- package/dist/plugins/opencode/shim.js +150 -0
- package/dist/plugins/quiet/README.md +26 -0
- package/dist/plugins/quiet/index.js +23 -0
- package/dist/plugins/tabs/README.md +44 -0
- package/dist/plugins/tabs/index.js +75 -0
- package/dist/plugins/tabs/public/client.js +1 -0
- package/dist/plugins/tabs/public/style.css +98 -0
- package/dist/plugins/terminal/README.md +63 -0
- package/dist/plugins/terminal/host.js +338 -0
- package/dist/plugins/terminal/index.js +170 -0
- package/dist/plugins/terminal/modes.js +32 -0
- package/dist/plugins/terminal/public/client.js +41 -0
- package/dist/plugins/terminal/public/style.css +511 -0
- package/dist/plugins/terminal/server.js +256 -0
- package/dist/plugins/terminal/sizes.js +58 -0
- package/dist/plugins/timeline/README.md +48 -0
- package/dist/plugins/timeline/index.js +83 -0
- package/dist/plugins/timeline/public/style.css +138 -0
- package/dist/plugins/toc/README.md +40 -0
- package/dist/plugins/toc/index.js +29 -0
- package/dist/plugins/toc/public/client.js +1 -0
- package/dist/plugins/toc/public/style.css +117 -0
- package/dist/plugins/window/README.md +51 -0
- package/dist/plugins/window/index.js +91 -0
- package/dist/solid/index.js +41 -0
- package/dist/src/agents.js +81 -0
- package/dist/src/anchors.js +52 -0
- package/dist/src/api.js +282 -0
- package/dist/src/archive.js +49 -0
- package/dist/src/auth.js +67 -0
- package/dist/src/build.js +159 -0
- package/dist/src/client/page/base.css +1762 -0
- package/dist/src/client/page/main.js +2121 -0
- package/dist/src/config.js +76 -0
- package/dist/src/document.js +93 -0
- package/dist/src/entries.js +264 -0
- package/dist/src/events.js +322 -0
- package/dist/src/files.js +47 -0
- package/dist/src/formstate.js +65 -0
- package/dist/src/help.js +213 -0
- package/dist/src/html.js +34 -0
- package/dist/src/hub.js +348 -0
- package/dist/src/icons.js +6 -0
- package/dist/src/listen.js +132 -0
- package/dist/src/listeners.js +64 -0
- package/dist/src/load.js +81 -0
- package/dist/src/loader.js +179 -0
- package/dist/src/migrate.js +314 -0
- package/dist/src/package.js +52 -0
- package/dist/src/pages/blocks.js +67 -0
- package/dist/src/pages/board.js +59 -0
- package/dist/src/pages/index.js +27 -0
- package/dist/src/pages/markdown.js +863 -0
- package/dist/src/pages/render.js +761 -0
- package/dist/src/pages/window.js +29 -0
- package/dist/src/plugins.js +283 -0
- package/dist/src/public.js +24 -0
- package/dist/src/rename.js +130 -0
- package/dist/src/running.js +146 -0
- package/dist/src/serve.js +188 -0
- package/dist/src/server.js +1105 -0
- package/dist/src/site.js +179 -0
- package/dist/src/state.js +155 -0
- package/dist/src/status.js +323 -0
- package/dist/src/thread.js +68 -0
- package/dist/src/watchdog.js +38 -0
- package/dist/src/ws.js +128 -0
- package/package.json +154 -4
- package/plugins/README.md +304 -0
- package/plugins/alert/README.md +18 -0
- package/plugins/alert/index.ts +50 -0
- package/plugins/alert/public/style.css +40 -0
- package/plugins/ansi/README.md +36 -0
- package/plugins/ansi/index.ts +95 -0
- package/plugins/ansi/public/style.css +28 -0
- package/plugins/badge/README.md +32 -0
- package/plugins/badge/index.ts +36 -0
- package/plugins/badge/public/style.css +30 -0
- package/plugins/chat/README.md +81 -0
- package/plugins/chat/autosize.ts +139 -0
- package/plugins/chat/client.tsx +187 -0
- package/plugins/chat/index.ts +146 -0
- package/plugins/chat/public/style.css +143 -0
- package/plugins/claude/README.md +116 -0
- package/plugins/claude/channel.ts +136 -0
- package/plugins/claude/doctor.ts +274 -0
- package/plugins/claude/index.ts +248 -0
- package/plugins/claude/session.ts +70 -0
- package/plugins/code/README.md +54 -0
- package/plugins/code/client.ts +73 -0
- package/plugins/code/diff.ts +96 -0
- package/plugins/code/index.ts +136 -0
- package/plugins/code/public/style.css +99 -0
- package/plugins/comments/README.md +30 -0
- package/plugins/comments/client.ts +723 -0
- package/plugins/comments/index.ts +48 -0
- package/plugins/comments/public/style.css +224 -0
- package/plugins/details/README.md +24 -0
- package/plugins/details/index.ts +30 -0
- package/plugins/details/public/style.css +7 -0
- package/plugins/draft/README.md +14 -0
- package/plugins/draft/index.ts +23 -0
- package/plugins/draft/public/style.css +21 -0
- package/plugins/forge/README.md +22 -0
- package/plugins/forge/index.ts +84 -0
- package/plugins/form/README.md +126 -0
- package/plugins/form/index.ts +199 -0
- package/plugins/form/public/style.css +172 -0
- package/plugins/grid/README.md +45 -0
- package/plugins/grid/index.ts +41 -0
- package/plugins/grid/public/style.css +98 -0
- package/plugins/iframe/README.md +18 -0
- package/plugins/iframe/index.ts +53 -0
- package/plugins/iframe/public/style.css +28 -0
- package/plugins/index.ts +49 -0
- package/plugins/lightbox/README.md +24 -0
- package/plugins/lightbox/client.ts +109 -0
- package/plugins/lightbox/index.ts +25 -0
- package/plugins/lightbox/public/style.css +66 -0
- package/plugins/mermaid/README.md +21 -0
- package/plugins/mermaid/index.ts +46 -0
- package/plugins/mermaid/public/style.css +21 -0
- package/plugins/opencode/README.md +48 -0
- package/plugins/opencode/channel.ts +112 -0
- package/plugins/opencode/index.ts +91 -0
- package/plugins/opencode/shim.ts +166 -0
- package/plugins/quiet/README.md +26 -0
- package/plugins/quiet/index.ts +28 -0
- package/plugins/tabs/README.md +44 -0
- package/plugins/tabs/client.ts +96 -0
- package/plugins/tabs/index.ts +103 -0
- package/plugins/tabs/public/style.css +98 -0
- package/plugins/terminal/README.md +63 -0
- package/plugins/terminal/client.ts +391 -0
- package/plugins/terminal/host.ts +434 -0
- package/plugins/terminal/index.ts +199 -0
- package/plugins/terminal/modes.ts +30 -0
- package/plugins/terminal/public/style.css +225 -0
- package/plugins/terminal/server.ts +266 -0
- package/plugins/terminal/sizes.ts +72 -0
- package/plugins/timeline/README.md +48 -0
- package/plugins/timeline/index.ts +82 -0
- package/plugins/timeline/public/style.css +138 -0
- package/plugins/toc/README.md +40 -0
- package/plugins/toc/client.ts +236 -0
- package/plugins/toc/index.ts +55 -0
- package/plugins/toc/public/style.css +117 -0
- package/plugins/window/README.md +51 -0
- package/plugins/window/index.ts +92 -0
- package/skills/workpad/SKILL.md +592 -0
- package/solid/index.tsx +80 -0
- package/src/agents.ts +134 -0
- package/src/anchors.ts +85 -0
- package/src/api.ts +444 -0
- package/src/archive.ts +75 -0
- package/src/auth.ts +99 -0
- package/src/build.ts +213 -0
- package/src/client/base.ts +19 -0
- package/src/client/forms.ts +420 -0
- package/src/client/markdown.ts +147 -0
- package/src/client/page/base.css +1762 -0
- package/src/client/page/board.ts +166 -0
- package/src/client/page/elsewhere.ts +153 -0
- package/src/client/page/fresh.ts +343 -0
- package/src/client/page/heading-link.ts +32 -0
- package/src/client/page/live.ts +134 -0
- package/src/client/page/main.ts +398 -0
- package/src/client/page/orphans.ts +64 -0
- package/src/client/page/place.ts +260 -0
- package/src/client/page/sort.ts +49 -0
- package/src/client/page/sticky.ts +58 -0
- package/src/client/page/theme.ts +36 -0
- package/src/client/page/view.ts +9 -0
- package/src/client/page/waiting.ts +278 -0
- package/src/client/page/window.ts +40 -0
- package/src/client/reactive.ts +19 -0
- package/src/client/read.ts +111 -0
- package/src/client/runtime.ts +499 -0
- package/src/client/scroll.ts +90 -0
- package/src/client/toast.ts +99 -0
- package/src/config.ts +111 -0
- package/src/define.ts +997 -0
- package/src/document.ts +191 -0
- package/src/entries.ts +352 -0
- package/src/events.ts +463 -0
- package/src/files.ts +74 -0
- package/src/formstate.ts +72 -0
- package/src/help.ts +240 -0
- package/src/html.ts +44 -0
- package/src/hub.ts +435 -0
- package/src/icons.ts +18 -0
- package/src/listen.ts +171 -0
- package/src/listeners.ts +89 -0
- package/src/load.ts +113 -0
- package/src/loader.ts +227 -0
- package/src/migrate.ts +352 -0
- package/src/package.ts +62 -0
- package/src/pages/blocks.ts +106 -0
- package/src/pages/board.ts +89 -0
- package/src/pages/index.ts +79 -0
- package/src/pages/markdown.ts +976 -0
- package/src/pages/render.ts +845 -0
- package/src/pages/window.ts +69 -0
- package/src/plugins.ts +319 -0
- package/src/public.ts +39 -0
- package/src/rename.ts +147 -0
- package/src/running.ts +171 -0
- package/src/serve.ts +221 -0
- package/src/server.ts +1385 -0
- package/src/site.ts +170 -0
- package/src/state.ts +327 -0
- package/src/status.ts +466 -0
- package/src/thread.ts +98 -0
- package/src/watchdog.ts +53 -0
- package/src/ws.ts +155 -0
package/dist/src/site.js
ADDED
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import { fileFilter } from "./files.js";
|
|
2
|
+
import fs from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { parse } from "yaml";
|
|
5
|
+
// src/site.ts
|
|
6
|
+
/** A page's frontmatter: YAML between `---` lines, at the very start. */
|
|
7
|
+
const FRONTMATTER = /^---\r?\n([\s\S]*?)\r?\n---\r?\n/;
|
|
8
|
+
/** Is `file` the folder `dir` or inside it? */
|
|
9
|
+
const within = (file, dir) => file === dir || file.startsWith(dir + path.sep);
|
|
10
|
+
/** Resolve a path inside a base folder, refusing anything that escapes it. */
|
|
11
|
+
function inside(base, rel) {
|
|
12
|
+
const p = path.resolve(base, "." + path.posix.normalize("/" + rel));
|
|
13
|
+
return within(p, base) ? p : null;
|
|
14
|
+
}
|
|
15
|
+
/** A page's frontmatter, parsed as YAML; empty, with the reason, when it isn't fields. */
|
|
16
|
+
function parseFrontmatter(source) {
|
|
17
|
+
const m = FRONTMATTER.exec(source);
|
|
18
|
+
if (!m) return {
|
|
19
|
+
data: {},
|
|
20
|
+
error: null
|
|
21
|
+
};
|
|
22
|
+
let data;
|
|
23
|
+
try {
|
|
24
|
+
data = parse(m[1]);
|
|
25
|
+
} catch (err) {
|
|
26
|
+
return {
|
|
27
|
+
data: {},
|
|
28
|
+
error: err.message
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
if (data == null) return {
|
|
32
|
+
data: {},
|
|
33
|
+
error: null
|
|
34
|
+
};
|
|
35
|
+
if (typeof data !== "object" || Array.isArray(data)) return {
|
|
36
|
+
data: {},
|
|
37
|
+
error: "it must be fields (`key: value` lines), not a single value or a list"
|
|
38
|
+
};
|
|
39
|
+
return {
|
|
40
|
+
data,
|
|
41
|
+
error: null
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
/** A markdown file as a page: its source, the body after its frontmatter, and the frontmatter parsed. */
|
|
45
|
+
function pageSource(id, file) {
|
|
46
|
+
const source = fs.readFileSync(file, "utf8");
|
|
47
|
+
const { data, error } = parseFrontmatter(source);
|
|
48
|
+
return {
|
|
49
|
+
kind: "page",
|
|
50
|
+
id,
|
|
51
|
+
file,
|
|
52
|
+
mtime: fs.statSync(file).mtimeMs,
|
|
53
|
+
source,
|
|
54
|
+
body: source.replace(FRONTMATTER, ""),
|
|
55
|
+
frontmatter: data,
|
|
56
|
+
frontmatterError: error
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
/** Text files show as files; anything with a NUL byte near the start is binary. */
|
|
60
|
+
function isText(file) {
|
|
61
|
+
const fd = fs.openSync(file, "r");
|
|
62
|
+
const buf = Buffer.alloc(8192);
|
|
63
|
+
const n = fs.readSync(fd, buf, 0, buf.length, 0);
|
|
64
|
+
fs.closeSync(fd);
|
|
65
|
+
return !buf.subarray(0, n).includes(0);
|
|
66
|
+
}
|
|
67
|
+
/** A repository file's id: `-/` and its path in the project (`-` for the project's folder). */
|
|
68
|
+
const isRepoId = (id) => id === "-" || id.startsWith("-/");
|
|
69
|
+
/**
|
|
70
|
+
* The project as its server serves it, below `base`. `files()` says whether it serves the project's
|
|
71
|
+
* own files (`-/…`), which the config can change while it runs.
|
|
72
|
+
*/
|
|
73
|
+
function servedSite(project, { base, files, filter = () => fileFilter() }) {
|
|
74
|
+
function pagesIn(dir) {
|
|
75
|
+
const out = [];
|
|
76
|
+
const walk = (d) => {
|
|
77
|
+
if (!fs.existsSync(d)) return;
|
|
78
|
+
for (const ent of fs.readdirSync(d, { withFileTypes: true })) {
|
|
79
|
+
if (ent.name.startsWith(".") || ent.name === "node_modules") continue;
|
|
80
|
+
const full = path.join(d, ent.name);
|
|
81
|
+
if (ent.isDirectory()) walk(full);
|
|
82
|
+
else if (ent.name.endsWith(".md")) {
|
|
83
|
+
const id = path.relative(project.pagesDir, full).split(path.sep).join("/").replace(/\.md$/, "");
|
|
84
|
+
out.push({
|
|
85
|
+
id,
|
|
86
|
+
file: full,
|
|
87
|
+
mtime: fs.statSync(full).mtimeMs,
|
|
88
|
+
lastEvent: null,
|
|
89
|
+
awaiting: 0
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
walk(dir);
|
|
95
|
+
return out.sort((a, b) => b.mtime - a.mtime);
|
|
96
|
+
}
|
|
97
|
+
function page(id) {
|
|
98
|
+
if (!id) return {
|
|
99
|
+
kind: "pages",
|
|
100
|
+
id: "",
|
|
101
|
+
file: project.pagesDir,
|
|
102
|
+
mtime: null,
|
|
103
|
+
pages: pagesIn(project.pagesDir)
|
|
104
|
+
};
|
|
105
|
+
if (isRepoId(id)) {
|
|
106
|
+
const target = inside(project.dir, id.slice(2));
|
|
107
|
+
if (!target || !fs.existsSync(target)) return null;
|
|
108
|
+
const rel = path.relative(project.dir, target).split(path.sep).join("/");
|
|
109
|
+
const stat = fs.statSync(target);
|
|
110
|
+
if (rel && !(stat.isDirectory() ? filter().mayHold(rel) : filter().shows(rel))) return null;
|
|
111
|
+
if (stat.isDirectory()) {
|
|
112
|
+
const entries = fs.readdirSync(target, { withFileTypes: true }).filter((e) => {
|
|
113
|
+
if (e.name === ".DS_Store") return false;
|
|
114
|
+
const child = rel ? `${rel}/${e.name}` : e.name;
|
|
115
|
+
return e.isDirectory() ? filter().mayHold(child) : filter().shows(child);
|
|
116
|
+
}).map((e) => ({
|
|
117
|
+
name: e.name,
|
|
118
|
+
dir: e.isDirectory()
|
|
119
|
+
})).sort((a, b) => Number(b.dir) - Number(a.dir) || a.name.localeCompare(b.name));
|
|
120
|
+
return {
|
|
121
|
+
kind: "folder",
|
|
122
|
+
id,
|
|
123
|
+
file: target,
|
|
124
|
+
mtime: stat.mtimeMs,
|
|
125
|
+
entries
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
if (/\.(md|markdown)$/i.test(target)) return pageSource(id, target);
|
|
129
|
+
if (stat.size > 1e6 || !isText(target)) return { raw: target };
|
|
130
|
+
return {
|
|
131
|
+
kind: "file",
|
|
132
|
+
id,
|
|
133
|
+
file: target,
|
|
134
|
+
mtime: stat.mtimeMs,
|
|
135
|
+
source: fs.readFileSync(target, "utf8")
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
const target = inside(project.pagesDir, id);
|
|
139
|
+
if (!target) return null;
|
|
140
|
+
if (path.extname(id) && fs.existsSync(target) && fs.statSync(target).isFile()) return { raw: target };
|
|
141
|
+
const file = target + ".md";
|
|
142
|
+
if (fs.existsSync(target) && fs.statSync(target).isDirectory()) {
|
|
143
|
+
const index = path.join(target, "index.md");
|
|
144
|
+
if (fs.existsSync(index)) return pageSource(id, index);
|
|
145
|
+
if (fs.existsSync(file)) return pageSource(id, file);
|
|
146
|
+
return {
|
|
147
|
+
kind: "pages",
|
|
148
|
+
id,
|
|
149
|
+
file: target,
|
|
150
|
+
mtime: null,
|
|
151
|
+
pages: pagesIn(target)
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
return fs.existsSync(file) ? pageSource(id, file) : null;
|
|
155
|
+
}
|
|
156
|
+
function urlOf(abs) {
|
|
157
|
+
const rel = (dir) => path.relative(dir, abs).split(path.sep).map(encodeURIComponent).join("/");
|
|
158
|
+
if (within(abs, project.pagesDir)) return `${base}/${rel(project.pagesDir).replace(/\.md$/, "")}`;
|
|
159
|
+
if (files() && within(abs, project.dir)) {
|
|
160
|
+
if (!filter().shows(path.relative(project.dir, abs).split(path.sep).join("/"))) return null;
|
|
161
|
+
return `${base}/-/${rel(project.dir)}`;
|
|
162
|
+
}
|
|
163
|
+
return null;
|
|
164
|
+
}
|
|
165
|
+
function urlFor(abs) {
|
|
166
|
+
for (const candidate of [abs, abs + ".md"]) if (fs.existsSync(candidate)) {
|
|
167
|
+
const url = urlOf(candidate);
|
|
168
|
+
if (url) return url + (fs.statSync(candidate).isDirectory() && !url.endsWith("/") ? "/" : "");
|
|
169
|
+
}
|
|
170
|
+
return null;
|
|
171
|
+
}
|
|
172
|
+
return {
|
|
173
|
+
page,
|
|
174
|
+
pagesIn,
|
|
175
|
+
urlOf,
|
|
176
|
+
urlFor
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
export { FRONTMATTER, inside, isRepoId, pageSource, parseFrontmatter, servedSite, within };
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { chatForms, formDetails } from "./pages/markdown.js";
|
|
2
|
+
import { fromReader, isQuestionName, namesOf, questionState, subscriptions } from "./status.js";
|
|
3
|
+
import { matches } from "./events.js";
|
|
4
|
+
// src/state.ts
|
|
5
|
+
const kindOf = (e) => e.type === "chat:message" ? "chat" : e.type.startsWith("comments:") ? "comment" : e.type.startsWith("workpad:") ? "mark" : isQuestionName(e.type) ? fromReader(e) ? "answer" : "other" : "other";
|
|
6
|
+
const list = (v) => v == null || v === "" ? [] : (Array.isArray(v) ? v : String(v).split(",")).map(String).filter(Boolean);
|
|
7
|
+
/** Does an event match a query? (`since` aside: it is a place in the log, see after.) */
|
|
8
|
+
function matchesQuery(e, q) {
|
|
9
|
+
const types = list(q.type);
|
|
10
|
+
const kinds = list(q.kind);
|
|
11
|
+
return matches(e, { page: q.page || null }) && (!types.length || types.includes(e.type)) && (!kinds.length || kinds.includes(kindOf(e)));
|
|
12
|
+
}
|
|
13
|
+
/** The events after `since`: an event's id (those after it in the log), or a time. */
|
|
14
|
+
function after(events, since) {
|
|
15
|
+
if (!since) return events;
|
|
16
|
+
const i = events.findIndex((e) => e.id === since);
|
|
17
|
+
return i >= 0 ? events.slice(i + 1) : events.filter((e) => e.ts > since);
|
|
18
|
+
}
|
|
19
|
+
const live = (e, project) => ({
|
|
20
|
+
...e,
|
|
21
|
+
project,
|
|
22
|
+
kind: kindOf(e)
|
|
23
|
+
});
|
|
24
|
+
/** A page's address on the server, from its root. */
|
|
25
|
+
const pageUrl = (base, page) => `${base}/${page.split("/").map(encodeURIComponent).join("/")}`;
|
|
26
|
+
const iso = (ms) => ms ? new Date(ms).toISOString() : null;
|
|
27
|
+
function pagesOf(s) {
|
|
28
|
+
return s.statuses.map((p) => ({
|
|
29
|
+
project: s.base,
|
|
30
|
+
page: p.page,
|
|
31
|
+
title: p.title,
|
|
32
|
+
url: pageUrl(s.base, p.page),
|
|
33
|
+
file: p.file,
|
|
34
|
+
modified: iso(s.times(p.page)?.modified),
|
|
35
|
+
subscribers: p.subscribers,
|
|
36
|
+
forms: p.asks,
|
|
37
|
+
awaiting: p.open.length + p.unread.length
|
|
38
|
+
}));
|
|
39
|
+
}
|
|
40
|
+
/** Every question of every page, with what it asks and where it stands. */
|
|
41
|
+
function formsOf(s) {
|
|
42
|
+
const marks = {
|
|
43
|
+
received: () => false,
|
|
44
|
+
handled: () => false
|
|
45
|
+
};
|
|
46
|
+
return s.statuses.flatMap((p) => {
|
|
47
|
+
const own = s.events.filter((e) => e.page === p.page);
|
|
48
|
+
const details = s.details(p.page);
|
|
49
|
+
const url = pageUrl(s.base, p.page);
|
|
50
|
+
return p.questions.map(({ name, state }) => {
|
|
51
|
+
const hold = p.held.find((h) => h.name === name);
|
|
52
|
+
const answer = questionState(name, own, [], marks).last ?? null;
|
|
53
|
+
const slash = name.indexOf("/");
|
|
54
|
+
if (slash >= 0) {
|
|
55
|
+
const chat = name.slice(0, slash);
|
|
56
|
+
const found = chatForms(s.events, p.page, chat).find((f) => f.name === name);
|
|
57
|
+
const detail = found ? formDetails(found.markdown)[0] : void 0;
|
|
58
|
+
const anchor = found ? `message:${found.message.id}` : `chat:${chat}`;
|
|
59
|
+
return {
|
|
60
|
+
project: s.base,
|
|
61
|
+
page: p.page,
|
|
62
|
+
name,
|
|
63
|
+
question: detail?.question ?? null,
|
|
64
|
+
options: detail?.options ?? [],
|
|
65
|
+
buttons: detail?.buttons ?? [],
|
|
66
|
+
anchor,
|
|
67
|
+
url: `${url}#${anchor}`,
|
|
68
|
+
askedAt: found?.message.ts ?? null,
|
|
69
|
+
state,
|
|
70
|
+
hold: hold ? {
|
|
71
|
+
reason: hold.reason,
|
|
72
|
+
since: hold.since,
|
|
73
|
+
by: hold.by
|
|
74
|
+
} : null,
|
|
75
|
+
answer,
|
|
76
|
+
chat
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
const detail = details.find((d) => d.name === name);
|
|
80
|
+
return {
|
|
81
|
+
project: s.base,
|
|
82
|
+
page: p.page,
|
|
83
|
+
name,
|
|
84
|
+
question: detail?.question ?? null,
|
|
85
|
+
options: detail?.options ?? [],
|
|
86
|
+
buttons: detail?.buttons ?? [],
|
|
87
|
+
anchor: `form:${name}`,
|
|
88
|
+
url: `${url}#form:${name}`,
|
|
89
|
+
askedAt: iso(s.times(p.page)?.born),
|
|
90
|
+
state,
|
|
91
|
+
hold: hold ? {
|
|
92
|
+
reason: hold.reason,
|
|
93
|
+
since: hold.since,
|
|
94
|
+
by: hold.by
|
|
95
|
+
} : null,
|
|
96
|
+
answer,
|
|
97
|
+
chat: name
|
|
98
|
+
};
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
/** The chats on each page: every `chat` name its events carry, in order of their first message. */
|
|
103
|
+
function chatsOf(s) {
|
|
104
|
+
const pages = new Set(s.statuses.map((p) => p.page));
|
|
105
|
+
const byChat = /* @__PURE__ */ new Map();
|
|
106
|
+
const replaced = /* @__PURE__ */ new Set();
|
|
107
|
+
for (const e of s.events) {
|
|
108
|
+
if (!pages.has(e.page) || e.type.startsWith("workpad:") || typeof e.data?.chat !== "string") continue;
|
|
109
|
+
if (e.data.replaces) replaced.add(String(e.data.replaces));
|
|
110
|
+
const key = `${e.page}\n${e.data.chat}`;
|
|
111
|
+
byChat.set(key, [...byChat.get(key) ?? [], e]);
|
|
112
|
+
}
|
|
113
|
+
return [...byChat].map(([key, messages]) => {
|
|
114
|
+
const [page, chat] = key.split("\n");
|
|
115
|
+
const shown = messages.filter((m) => !replaced.has(m.id));
|
|
116
|
+
return {
|
|
117
|
+
project: s.base,
|
|
118
|
+
page,
|
|
119
|
+
chat,
|
|
120
|
+
messages: shown,
|
|
121
|
+
awaitingReply: !!s.statuses.find((p) => p.page === page)?.awaitingReply.includes(chat)
|
|
122
|
+
};
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
function subscriptionsOf(s) {
|
|
126
|
+
return subscriptions(s.events).map((e) => ({
|
|
127
|
+
project: s.base,
|
|
128
|
+
agent: e.by,
|
|
129
|
+
page: e.page,
|
|
130
|
+
since: e.ts,
|
|
131
|
+
channel: !!e.data?.channel
|
|
132
|
+
}));
|
|
133
|
+
}
|
|
134
|
+
/** Everyone the board knows, in the order the log first names them, then those only listening. */
|
|
135
|
+
function agentsOf(s, now = /* @__PURE__ */ new Date()) {
|
|
136
|
+
const names = namesOf(s.events);
|
|
137
|
+
const seen = /* @__PURE__ */ new Map();
|
|
138
|
+
for (const e of s.events) if (e.by) seen.set(e.by, e.ts);
|
|
139
|
+
const subs = subscriptionsOf(s);
|
|
140
|
+
return [.../* @__PURE__ */ new Set([...seen.keys(), ...s.listeners.flatMap((l) => l.agent ? [l.agent] : [])])].map((id) => {
|
|
141
|
+
const listening = s.listeners.filter((l) => l.agent === id).map((l) => ({
|
|
142
|
+
page: l.page,
|
|
143
|
+
type: l.type
|
|
144
|
+
}));
|
|
145
|
+
return {
|
|
146
|
+
project: s.base,
|
|
147
|
+
id,
|
|
148
|
+
name: names.get(id) ?? null,
|
|
149
|
+
subscribed: [...new Set(subs.filter((x) => x.agent === id).map((x) => x.page))],
|
|
150
|
+
listening,
|
|
151
|
+
lastSeen: listening.length ? now.toISOString() : seen.get(id) ?? null
|
|
152
|
+
};
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
export { after, agentsOf, chatsOf, formsOf, kindOf, live, matchesQuery, pageUrl, pagesOf, subscriptionsOf };
|
|
@@ -0,0 +1,323 @@
|
|
|
1
|
+
// src/status.ts
|
|
2
|
+
/**
|
|
3
|
+
* Can this be a question's name, the type of its answers? Not with a colon: types with one belong
|
|
4
|
+
* to workpad (`workpad:handled`) or a plugin (`comments:reply`, `chat:message`),
|
|
5
|
+
* named after its owner, so a form's answers never mix with their events.
|
|
6
|
+
*/
|
|
7
|
+
const isQuestionName = (name) => !!name && !name.includes(":");
|
|
8
|
+
/** Does this event of an agent's name `name` among its `forms`? (SETTLE, HOLD, RELEASE) */
|
|
9
|
+
const names = (e, type, name) => e.type === type && !fromReader(e) && Array.isArray(e.data?.forms) && e.data.forms.includes(name);
|
|
10
|
+
/**
|
|
11
|
+
* The hold on question `name` in force, or null: the latest HOLD naming it, unless a RELEASE or a
|
|
12
|
+
* SETTLE came after it. Only the command line's count, as with SETTLE.
|
|
13
|
+
*/
|
|
14
|
+
function holdOf(name, events) {
|
|
15
|
+
for (let i = events.length - 1; i >= 0; i--) {
|
|
16
|
+
const e = events[i];
|
|
17
|
+
if (names(e, "workpad:release", name) || names(e, "workpad:settle", name)) return null;
|
|
18
|
+
if (names(e, "workpad:hold", name)) return {
|
|
19
|
+
reason: String(e.data.reason ?? ""),
|
|
20
|
+
since: e.ts,
|
|
21
|
+
by: e.by ?? null
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* The questions of a page with the nested ones settled: a form in a message in the chat under a form
|
|
28
|
+
* (`<form>/<name>`) that is still open once that form is decided (handled) asks nothing any more.
|
|
29
|
+
* The server and the page both go by it, so they never disagree.
|
|
30
|
+
*/
|
|
31
|
+
function settleNested(questions) {
|
|
32
|
+
const handled = new Set(questions.filter((q) => q.state === "handled").map((q) => q.name));
|
|
33
|
+
return questions.map((q) => {
|
|
34
|
+
const outer = q.name.includes("/") ? q.name.slice(0, q.name.indexOf("/")) : null;
|
|
35
|
+
return outer && q.state === "open" && handled.has(outer) ? {
|
|
36
|
+
...q,
|
|
37
|
+
state: "settled"
|
|
38
|
+
} : q;
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Was this event written by the reader (a form submit, a comment), not by an agent? Agents write
|
|
43
|
+
* through the command line, which marks its events `from: 'cli'`: an agent's reply, a claim, a mark.
|
|
44
|
+
*/
|
|
45
|
+
const fromReader = (event) => event.from !== "cli";
|
|
46
|
+
/** Does a listener's page filter cover `page`? An empty filter covers every page. */
|
|
47
|
+
const covers = (l, page) => !l.page || page === l.page || page.startsWith(l.page + "/");
|
|
48
|
+
/**
|
|
49
|
+
* Where the question `name` stands. `events` are the page's, `listeners` the running waits and
|
|
50
|
+
* watches that cover the page (only one asking for it by name counts: it asks again), `marks`
|
|
51
|
+
* what became of each answer. `last` is its latest answer: the reader's latest event of its name.
|
|
52
|
+
* Open, and settled (SETTLE) since that answer or without one: settled.
|
|
53
|
+
*/
|
|
54
|
+
function questionState(name, events, listeners, marks) {
|
|
55
|
+
const last = events.findLast((e) => e.type === name && fromReader(e));
|
|
56
|
+
const askedAgain = listeners.some((l) => l.type === name);
|
|
57
|
+
const settled = events.findLastIndex((e) => names(e, SETTLE, name));
|
|
58
|
+
const shut = settled >= 0 && settled > (last ? events.lastIndexOf(last) : -1);
|
|
59
|
+
const state = !last ? "open" : marks.handled(last.id) ? askedAgain ? "open" : "handled" : marks.received(last.id) ? "received" : "pending";
|
|
60
|
+
const hold = holdOf(name, last ? events.slice(events.lastIndexOf(last) + 1) : events);
|
|
61
|
+
return {
|
|
62
|
+
state: state !== "open" ? state : hold ? "held" : shut ? "settled" : "open",
|
|
63
|
+
last,
|
|
64
|
+
hold
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
/** Does anyone listen for the answer to question `name`? Not: it is unheard. */
|
|
68
|
+
const heard = (name, listeners) => listeners.some((l) => l.type === null || l.type === name);
|
|
69
|
+
/** The type of the event the page appends once messages were in view: `{ ids }`. */
|
|
70
|
+
const READ = "workpad:read";
|
|
71
|
+
/**
|
|
72
|
+
* The type of the event `workpad scroll` appends: show the reader a place on a page, `{ to }`. A
|
|
73
|
+
* page applies the latest one that came after it loaded, as it comes or once its live channel is
|
|
74
|
+
* back.
|
|
75
|
+
*/
|
|
76
|
+
const SCROLL = "workpad:scroll";
|
|
77
|
+
/**
|
|
78
|
+
* The agent's messages on a page the reader hasn't read: events from the agent of a type
|
|
79
|
+
* `isMessage` says is for the reader, not named by a `workpad:read` from the reader, not
|
|
80
|
+
* replaced by a later one (`replaces`), and, given the page's `chats`, not in a chat (`data.chat`)
|
|
81
|
+
* that is no longer among them.
|
|
82
|
+
*/
|
|
83
|
+
function unreadMessages(events, isMessage, chats) {
|
|
84
|
+
const read = /* @__PURE__ */ new Set();
|
|
85
|
+
const replaced = /* @__PURE__ */ new Set();
|
|
86
|
+
for (const e of events) {
|
|
87
|
+
if (e.type === "workpad:read" && fromReader(e)) for (const id of e.data?.ids ?? []) read.add(String(id));
|
|
88
|
+
if (e.data?.replaces) replaced.add(String(e.data.replaces));
|
|
89
|
+
}
|
|
90
|
+
const offPage = (e) => !!chats && typeof e.data?.chat === "string" && !chats.has(e.data.chat);
|
|
91
|
+
return events.filter((e) => !fromReader(e) && isMessage(e.type) && !read.has(e.id) && !replaced.has(e.id) && !offPage(e));
|
|
92
|
+
}
|
|
93
|
+
/** How many things on a page wait for the reader: its open questions and unread messages. */
|
|
94
|
+
const awaiting = (p) => p.open.length + p.unread.length;
|
|
95
|
+
/**
|
|
96
|
+
* The type of the event that says the agent handled another (`workpad handled`): `{ event: <its id> }`.
|
|
97
|
+
* Named for picking an answer up; older logs wrote it when a wait printed one, so those stay handled.
|
|
98
|
+
*/
|
|
99
|
+
const HANDLED = "workpad:handled";
|
|
100
|
+
/**
|
|
101
|
+
* The type of the event that settles questions (`workpad settle`): `{ forms: [<name>…] }`. An
|
|
102
|
+
* agent settles those a conclusion reached elsewhere (in a chat, the design moving on) answered:
|
|
103
|
+
* open, they'd stay "unanswered" for good. Only the command line's count, as with RECEIVED.
|
|
104
|
+
*/
|
|
105
|
+
const SETTLE = "workpad:settle";
|
|
106
|
+
/**
|
|
107
|
+
* The type of the event that holds questions (`workpad hold`): `{ forms: [<name>…], reason }`. A
|
|
108
|
+
* held question waits on something other than the reader (another merge request, a decision
|
|
109
|
+
* elsewhere): it doesn't count as waiting for them, and it isn't settled: once released (RELEASE)
|
|
110
|
+
* it is open again. The page shows it held, with the reason. Only the command line's count.
|
|
111
|
+
*/
|
|
112
|
+
const HOLD = "workpad:hold";
|
|
113
|
+
/** The type of the event that releases held questions (`workpad release`): `{ forms: [<name>…] }`. */
|
|
114
|
+
const RELEASE = "workpad:release";
|
|
115
|
+
/** The type of the event a wait or watch appends when it prints an answer: `{ event: <its id> }`. */
|
|
116
|
+
const RECEIVED = "workpad:received";
|
|
117
|
+
/**
|
|
118
|
+
* The type of the event that subscribes an identity to a page (its `by`): `workpad new` for the
|
|
119
|
+
* page it makes, `workpad listen --page` for the pages it listens on (and `listen --channel` without
|
|
120
|
+
* a page for every page, on the page `''`; a subscription covers the pages below it), when it is someone (`--agent`,
|
|
121
|
+
* `WORKPAD_AGENT`, a plugin's session). A subscription outlasts the command: what is left on the page
|
|
122
|
+
* is the subscriber's to see to (the turn-end check), and a channel follows it. The colon keeps it
|
|
123
|
+
* apart from form names, which are event types too.
|
|
124
|
+
*/
|
|
125
|
+
const SUBSCRIBE = "workpad:subscribe";
|
|
126
|
+
/**
|
|
127
|
+
* The type of the event that takes an agent's subscriptions off (`workpad agent unsubscribe`):
|
|
128
|
+
* `{ agent, page? }`, on no page; without a page, all of them. Its `by` is whoever ran it.
|
|
129
|
+
*/
|
|
130
|
+
const UNSUBSCRIBE = "workpad:unsubscribe";
|
|
131
|
+
/** The subscriptions in force, in the order they were made: each not taken off by a later UNSUBSCRIBE. */
|
|
132
|
+
function subscriptions(events) {
|
|
133
|
+
let live = [];
|
|
134
|
+
for (const e of events) if (e.type === "workpad:subscribe" && e.by) live.push(e);
|
|
135
|
+
else if (e.type === "workpad:unsubscribe" && typeof e.data?.agent === "string") {
|
|
136
|
+
const { agent, page } = e.data;
|
|
137
|
+
live = live.filter((s) => s.by !== agent || page !== void 0 && s.page !== page);
|
|
138
|
+
}
|
|
139
|
+
return live;
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* The type of the event that names an agent (its `by`): `{ name }`, on no page, so a page deleted
|
|
143
|
+
* keeps it. `workpad agent name` keeps one per agent: setting one drops the one before.
|
|
144
|
+
*/
|
|
145
|
+
const NAME = "workpad:name";
|
|
146
|
+
/** Each agent's name, by its id: the latest each gave itself. */
|
|
147
|
+
function namesOf(events) {
|
|
148
|
+
const names = /* @__PURE__ */ new Map();
|
|
149
|
+
for (const e of events) if (e.type === "workpad:name" && e.from === "cli" && e.by && typeof e.data?.name === "string") names.set(e.by, e.data.name);
|
|
150
|
+
return names;
|
|
151
|
+
}
|
|
152
|
+
/** The ids of the answers a wait or watch printed. Only the command line's marks count. */
|
|
153
|
+
function receivedIds(events) {
|
|
154
|
+
return new Set(events.filter((e) => e.type === "workpad:received" && !fromReader(e)).map((e) => String(e.data?.event)));
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Where a channel catches up from, for `agent` on `page`: the subscription it took the page on with
|
|
158
|
+
* through its channel (`workpad listen --channel`, data `{ channel: true }`, on the page, one above
|
|
159
|
+
* it, or every page `''`). Before it, what someone already received is the past, and is skipped
|
|
160
|
+
* (handOut's skipReceivedBefore); the reader's answers never are. Null when the page is the agent's
|
|
161
|
+
* otherwise too (it made it, or listened on it by name): then it has all of it.
|
|
162
|
+
*/
|
|
163
|
+
function channelSince(events, agent, page) {
|
|
164
|
+
let since = null;
|
|
165
|
+
for (const e of subscriptions(events)) {
|
|
166
|
+
if (e.by !== agent) continue;
|
|
167
|
+
if (!(e.page === "" || e.page === page || page.startsWith(`${e.page}/`))) continue;
|
|
168
|
+
if (!e.data?.channel) return null;
|
|
169
|
+
since ??= e.id;
|
|
170
|
+
}
|
|
171
|
+
return since;
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* The ids of the reader's events the agent handled: acknowledged (`workpad:handled`), or replied
|
|
175
|
+
* to after them (see the top of this file). The log is gone through once: each of the reader's
|
|
176
|
+
* events is filed under what a reply would name (its id, its thread, its chat, its form), and an
|
|
177
|
+
* agent's event takes out all filed under what it names.
|
|
178
|
+
*/
|
|
179
|
+
function handledIds(events) {
|
|
180
|
+
const done = /* @__PURE__ */ new Set();
|
|
181
|
+
const filed = /* @__PURE__ */ new Map();
|
|
182
|
+
const file = (key, id) => filed.set(key, [...filed.get(key) ?? [], id]);
|
|
183
|
+
const take = (key) => {
|
|
184
|
+
for (const id of filed.get(key) ?? []) done.add(id);
|
|
185
|
+
filed.delete(key);
|
|
186
|
+
};
|
|
187
|
+
for (const e of events) {
|
|
188
|
+
const d = e.data ?? {};
|
|
189
|
+
if (e.type === "workpad:handled") {
|
|
190
|
+
if (!fromReader(e)) done.add(String(d.event));
|
|
191
|
+
continue;
|
|
192
|
+
}
|
|
193
|
+
if (e.type.startsWith("workpad:")) continue;
|
|
194
|
+
if (fromReader(e)) {
|
|
195
|
+
file(`id:${e.id}`, e.id);
|
|
196
|
+
if (d.replyTo) file(`id:${d.replyTo}`, e.id);
|
|
197
|
+
if (typeof d.chat === "string") file(`chat:${d.chat}`, e.id);
|
|
198
|
+
else file(`chat:${e.type.includes("/") ? e.type.slice(0, e.type.indexOf("/")) : e.type}`, e.id);
|
|
199
|
+
continue;
|
|
200
|
+
}
|
|
201
|
+
if (d.replyTo) take(`id:${d.replyTo}`);
|
|
202
|
+
if (typeof d.chat === "string") take(`chat:${d.chat}`);
|
|
203
|
+
}
|
|
204
|
+
return done;
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* The chats on a page waiting for the agent: those whose latest message is the reader's. A chat is
|
|
208
|
+
* what events share a `chat` name.
|
|
209
|
+
*/
|
|
210
|
+
function awaitingReply(events) {
|
|
211
|
+
const lastBy = /* @__PURE__ */ new Map();
|
|
212
|
+
for (const e of events) if (typeof e.data?.chat === "string" && !e.type.startsWith("workpad:")) lastBy.set(e.data.chat, e);
|
|
213
|
+
return [...lastBy].filter(([, e]) => fromReader(e)).map(([chat]) => chat);
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* A project's status from its pages, its log and who listens: per page its questions and where each
|
|
217
|
+
* stands, what is open and unheard, the agent's messages unread, the reader's events not received,
|
|
218
|
+
* and who listens; which events were received; when each page was last answered. The log is
|
|
219
|
+
* gone through once.
|
|
220
|
+
*/
|
|
221
|
+
/**
|
|
222
|
+
* Who is subscribed to page `id`: to it, to a page above it (`design` covers `design/items`, as a
|
|
223
|
+
* listen on it does), or to every page (`''`, `workpad listen --channel` without a page), each once.
|
|
224
|
+
*/
|
|
225
|
+
function subscribersOf(subscribers, id) {
|
|
226
|
+
const all = /* @__PURE__ */ new Set();
|
|
227
|
+
for (const [page, who] of subscribers) if (page === "" || page === id || id.startsWith(`${page}/`)) for (const s of who) all.add(s);
|
|
228
|
+
return [...all];
|
|
229
|
+
}
|
|
230
|
+
function projectStatus({ pages, events, listeners, forAgent, forReader }) {
|
|
231
|
+
const received = receivedIds(events);
|
|
232
|
+
const handled = handledIds(events);
|
|
233
|
+
const marks = {
|
|
234
|
+
received: (id) => received.has(id),
|
|
235
|
+
handled: (id) => handled.has(id)
|
|
236
|
+
};
|
|
237
|
+
const byPage = /* @__PURE__ */ new Map();
|
|
238
|
+
const subscribers = /* @__PURE__ */ new Map();
|
|
239
|
+
const last = /* @__PURE__ */ new Map();
|
|
240
|
+
for (const e of events) {
|
|
241
|
+
let list = byPage.get(e.page);
|
|
242
|
+
if (!list) byPage.set(e.page, list = []);
|
|
243
|
+
list.push(e);
|
|
244
|
+
if (fromReader(e)) last.set(e.page, e.ts);
|
|
245
|
+
}
|
|
246
|
+
for (const e of subscriptions(events)) {
|
|
247
|
+
const now = subscribers.get(e.page) ?? [];
|
|
248
|
+
if (!now.includes(e.by)) subscribers.set(e.page, [...now, e.by]);
|
|
249
|
+
}
|
|
250
|
+
return {
|
|
251
|
+
received,
|
|
252
|
+
handled,
|
|
253
|
+
lastAnswered: (page) => last.get(page) ?? null,
|
|
254
|
+
pages: pages.map((p) => {
|
|
255
|
+
const own = byPage.get(p.id) ?? [];
|
|
256
|
+
const listening = listeners(p.id);
|
|
257
|
+
const states = p.asks.map((name) => ({
|
|
258
|
+
name,
|
|
259
|
+
...questionState(name, own, listening, marks)
|
|
260
|
+
}));
|
|
261
|
+
const questions = settleNested(states.map(({ name, state }) => ({
|
|
262
|
+
name,
|
|
263
|
+
state
|
|
264
|
+
})));
|
|
265
|
+
const open = questions.filter((q) => q.state === "open").map((q) => q.name);
|
|
266
|
+
const held = states.flatMap((q) => q.state === "held" && q.hold ? [{
|
|
267
|
+
name: q.name,
|
|
268
|
+
...q.hold
|
|
269
|
+
}] : []);
|
|
270
|
+
return {
|
|
271
|
+
page: p.id,
|
|
272
|
+
title: p.title,
|
|
273
|
+
file: p.file,
|
|
274
|
+
subscribers: subscribersOf(subscribers, p.id),
|
|
275
|
+
asks: p.asks,
|
|
276
|
+
questions,
|
|
277
|
+
open,
|
|
278
|
+
held,
|
|
279
|
+
unheard: open.filter((name) => !heard(name, listening)),
|
|
280
|
+
unread: unreadMessages(own, forReader, p.chats ? new Set(p.chats) : null),
|
|
281
|
+
unreceived: own.filter((e) => fromReader(e) && forAgent(e.type) && !received.has(e.id) && !handled.has(e.id)),
|
|
282
|
+
unhandled: own.filter((e) => fromReader(e) && forAgent(e.type) && received.has(e.id) && !handled.has(e.id)),
|
|
283
|
+
awaitingReply: awaitingReply(own),
|
|
284
|
+
listeners: listening
|
|
285
|
+
};
|
|
286
|
+
})
|
|
287
|
+
};
|
|
288
|
+
}
|
|
289
|
+
/**
|
|
290
|
+
* What is open on a page, one short line each; the command that picks it up comes last, once.
|
|
291
|
+
* `file` is how to name the page's file to the reader (relative to where they are). Without
|
|
292
|
+
* `waiting`, the questions an agent is already waiting on are left out.
|
|
293
|
+
*/
|
|
294
|
+
function statusLines(p, file, { waiting = true } = {}) {
|
|
295
|
+
const lines = [];
|
|
296
|
+
if (p.unreceived.length) lines.push(`${p.unreceived.length} answer${p.unreceived.length > 1 ? "s" : ""} no agent received (${[...new Set(p.unreceived.map((e) => e.type))].join(", ")})`);
|
|
297
|
+
if (p.unheard.length) lines.push(`nobody listens for ${p.unheard.join(", ")}`);
|
|
298
|
+
if (lines.length) lines.push(`→ workpad listen --wait --page ${file}`);
|
|
299
|
+
if (p.unhandled.length) lines.push(`${p.unhandled.length} answer${p.unhandled.length > 1 ? "s" : ""} handed out and not handled → reply in its chat, or workpad handled ${p.unhandled.map((e) => e.id).join(" ")}`);
|
|
300
|
+
if (p.awaitingReply.length) lines.push(`waiting for a reply: the chat${p.awaitingReply.length > 1 ? "s" : ""} ${p.awaitingReply.join(", ")}`);
|
|
301
|
+
const listened = p.open.filter((t) => !p.unheard.includes(t));
|
|
302
|
+
if (waiting && listened.length) lines.push(`waiting for the reader: ${listened.join(", ")}`);
|
|
303
|
+
return lines;
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* Who is subscribed to a page, said to `me`: `you` among them, the others by the name they gave
|
|
307
|
+
* themselves (`names`), else their id, a long one (a session's) cut short.
|
|
308
|
+
*/
|
|
309
|
+
const whose = (p, me, names = /* @__PURE__ */ new Map()) => p.subscribers.length ? p.subscribers.map((s) => s === me ? "you" : names.get(s) ?? (s.length > 16 ? s.slice(0, 8) : s)).join(", ") : "no one subscribed";
|
|
310
|
+
/** The type of a chat message: `{ chat, text }` (the chat plugin's; the core shows the agent's as small pages). */
|
|
311
|
+
const CHAT_MESSAGE = "chat:message";
|
|
312
|
+
/** Is this a chat message of the agent's, which shows as a small page (forms and all)? */
|
|
313
|
+
const isAgentMessage = (e) => e.type === "chat:message" && !fromReader(e) && typeof e.data?.chat === "string";
|
|
314
|
+
/**
|
|
315
|
+
* The agent's messages in one chat of a page as they show, in order: those a later one replaced
|
|
316
|
+
* (`replaces`) left out.
|
|
317
|
+
*/
|
|
318
|
+
function agentMessagesIn(events, page, chat) {
|
|
319
|
+
const own = events.filter((e) => e.page === page && isAgentMessage(e) && e.data.chat === chat);
|
|
320
|
+
const replaced = new Set(own.map((e) => e.data.replaces).filter(Boolean).map(String));
|
|
321
|
+
return own.filter((e) => !replaced.has(e.id));
|
|
322
|
+
}
|
|
323
|
+
export { CHAT_MESSAGE, HANDLED, HOLD, NAME, READ, RECEIVED, RELEASE, SCROLL, SETTLE, SUBSCRIBE, UNSUBSCRIBE, agentMessagesIn, awaiting, awaitingReply, channelSince, covers, fromReader, handledIds, heard, holdOf, isAgentMessage, isQuestionName, namesOf, projectStatus, questionState, receivedIds, settleNested, statusLines, subscriptions, unreadMessages, whose };
|