deictic 0.1.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (166) hide show
  1. package/API.md +303 -0
  2. package/README.md +56 -104
  3. package/dist/annotations/index.d.ts +1 -1
  4. package/dist/astro.d.ts +1 -1
  5. package/dist/bun.d.ts +1 -1
  6. package/dist/bun.js +2 -2
  7. package/dist/client/index.d.ts +4 -2
  8. package/dist/client/index.d.ts.map +1 -1
  9. package/dist/client/index.js +130 -16
  10. package/dist/client/index.js.map +1 -1
  11. package/dist/index.d.ts +21 -218
  12. package/dist/index.js.map +1 -1
  13. package/dist/loader.js +1 -1
  14. package/dist/logs/client.d.ts +4 -0
  15. package/dist/logs/client.d.ts.map +1 -0
  16. package/dist/logs/client.js +16 -0
  17. package/dist/logs/client.js.map +1 -0
  18. package/dist/logs/index.d.ts +18 -0
  19. package/dist/logs/index.d.ts.map +1 -0
  20. package/dist/logs/index.js +21 -0
  21. package/dist/logs/index.js.map +1 -0
  22. package/dist/network/client.d.ts +4 -0
  23. package/dist/network/client.d.ts.map +1 -0
  24. package/dist/network/client.js +29 -0
  25. package/dist/network/client.js.map +1 -0
  26. package/dist/network/index.d.ts +2 -0
  27. package/dist/network/index.js +39 -0
  28. package/dist/network/index.js.map +1 -0
  29. package/dist/next.d.ts +1 -1
  30. package/dist/next.js +2 -2
  31. package/dist/recommended/index.d.ts +15 -5
  32. package/dist/recommended/index.d.ts.map +1 -1
  33. package/dist/recommended/index.js +10 -3
  34. package/dist/recommended/index.js.map +1 -1
  35. package/dist/recordings/client.d.ts +1 -1
  36. package/dist/recordings/client.d.ts.map +1 -1
  37. package/dist/recordings/client.js +177 -48
  38. package/dist/recordings/client.js.map +1 -1
  39. package/dist/recordings/index.d.ts +2 -2
  40. package/dist/recordings/index.js +3 -3
  41. package/dist/screenshots/client.d.ts +7 -1
  42. package/dist/screenshots/client.d.ts.map +1 -1
  43. package/dist/screenshots/client.js +36 -4
  44. package/dist/screenshots/client.js.map +1 -1
  45. package/dist/screenshots/dom.d.ts +1 -1
  46. package/dist/screenshots/dom.d.ts.map +1 -1
  47. package/dist/screenshots/dom.js +1 -28
  48. package/dist/screenshots/dom.js.map +1 -1
  49. package/dist/screenshots/index.d.ts +1 -1
  50. package/dist/screenshots/index.js +2 -2
  51. package/dist/screenshots/index.js.map +1 -1
  52. package/dist/screenshots/rrweb.d.ts +2 -2
  53. package/dist/screenshots/rrweb.js +5 -1
  54. package/dist/screenshots/rrweb.js.map +1 -1
  55. package/dist/{endpoints.js → shared/endpoints.js} +1 -1
  56. package/dist/shared/endpoints.js.map +1 -0
  57. package/dist/shared/index.d.ts +238 -0
  58. package/dist/shared/index.d.ts.map +1 -0
  59. package/dist/{index3.d.ts → shared/index2.d.ts} +3 -1
  60. package/dist/shared/index2.d.ts.map +1 -0
  61. package/dist/shared/index3.d.ts +112 -0
  62. package/dist/shared/index3.d.ts.map +1 -0
  63. package/dist/shared/index4.d.ts +64 -0
  64. package/dist/shared/index4.d.ts.map +1 -0
  65. package/dist/shared/index5.d.ts +220 -0
  66. package/dist/shared/index5.d.ts.map +1 -0
  67. package/dist/{index5.d.ts → shared/index6.d.ts} +2 -2
  68. package/dist/shared/index6.d.ts.map +1 -0
  69. package/dist/{integration.d.ts → shared/integration.d.ts} +4 -3
  70. package/dist/shared/integration.d.ts.map +1 -0
  71. package/dist/{integration.js → shared/integration.js} +5 -3
  72. package/dist/shared/integration.js.map +1 -0
  73. package/dist/{kept.js → shared/kept.js} +2 -2
  74. package/dist/shared/kept.js.map +1 -0
  75. package/dist/{marks.js → shared/marks.js} +6 -1
  76. package/dist/shared/marks.js.map +1 -0
  77. package/dist/shared/mask.js +107 -0
  78. package/dist/shared/mask.js.map +1 -0
  79. package/dist/{playwright.js → shared/playwright.js} +19 -4
  80. package/dist/shared/playwright.js.map +1 -0
  81. package/dist/shared/recordings.js +437 -0
  82. package/dist/shared/recordings.js.map +1 -0
  83. package/dist/shared/shortcut.js.map +1 -0
  84. package/dist/shared/tasks.js +632 -0
  85. package/dist/shared/tasks.js.map +1 -0
  86. package/dist/shared/waveform.js.map +1 -0
  87. package/dist/tailwind/client.d.ts +1 -1
  88. package/dist/tailwind/index.d.ts +1 -1
  89. package/dist/tasks/client.d.ts +1 -1
  90. package/dist/tasks/client.d.ts.map +1 -1
  91. package/dist/tasks/client.js +272 -57
  92. package/dist/tasks/client.js.map +1 -1
  93. package/dist/tasks/index.d.ts +2 -159
  94. package/dist/tasks/index.js +1 -348
  95. package/dist/vite.d.ts +11 -12
  96. package/dist/vite.d.ts.map +1 -1
  97. package/dist/vite.js +21 -4
  98. package/dist/vite.js.map +1 -1
  99. package/dist/voice/client.d.ts.map +1 -1
  100. package/dist/voice/client.js +36 -15
  101. package/dist/voice/client.js.map +1 -1
  102. package/dist/voice/index.d.ts +6 -1
  103. package/dist/voice/index.d.ts.map +1 -1
  104. package/dist/voice/index.js +13 -3
  105. package/dist/voice/index.js.map +1 -1
  106. package/dist/webpack.d.ts +1 -1
  107. package/dist/webpack.js +2 -2
  108. package/package.json +41 -2
  109. package/skills/deictic/SKILL.md +20 -14
  110. package/skills/deictic/references/install.md +32 -10
  111. package/skills/deictic/references/recordings.md +29 -0
  112. package/skills/deictic/references/tasks.md +49 -21
  113. package/skills/deictic/references/watch.md +38 -11
  114. package/skills/deictic/scripts/lib.mjs +88 -4
  115. package/skills/deictic/scripts/recording.mjs +84 -4
  116. package/skills/deictic/scripts/task.mjs +36 -7
  117. package/skills/deictic/scripts/vb.mjs +12 -0
  118. package/skills/deictic/scripts/watch.mjs +74 -10
  119. package/src/client/away.ts +105 -0
  120. package/src/client/index.ts +60 -16
  121. package/src/client/style.ts +1 -0
  122. package/src/index.ts +21 -2
  123. package/src/plugins/logs/client.ts +18 -0
  124. package/src/plugins/logs/index.ts +55 -0
  125. package/src/plugins/network/client.ts +30 -0
  126. package/src/plugins/network/index.ts +69 -0
  127. package/src/plugins/network/mask.ts +133 -0
  128. package/src/plugins/recommended/index.ts +14 -1
  129. package/src/plugins/recordings/client.ts +220 -40
  130. package/src/plugins/recordings/index.ts +111 -25
  131. package/src/plugins/recordings/marks.ts +6 -0
  132. package/src/plugins/recordings/replay.ts +159 -10
  133. package/src/plugins/screenshots/client.ts +38 -2
  134. package/src/plugins/screenshots/dom.ts +1 -32
  135. package/src/plugins/screenshots/index.ts +2 -0
  136. package/src/plugins/screenshots/playwright.ts +15 -4
  137. package/src/plugins/screenshots/rrweb.ts +2 -1
  138. package/src/plugins/tasks/client.ts +273 -41
  139. package/src/plugins/tasks/index.ts +207 -47
  140. package/src/plugins/tasks/local.ts +170 -0
  141. package/src/plugins/tasks/watchers.ts +36 -0
  142. package/src/plugins/voice/client.ts +33 -10
  143. package/src/plugins/voice/index.ts +21 -4
  144. package/src/tools/integration.ts +5 -3
  145. package/src/tools/kept.ts +2 -2
  146. package/src/vite.ts +23 -4
  147. package/dist/endpoints.js.map +0 -1
  148. package/dist/index.d.ts.map +0 -1
  149. package/dist/index2.d.ts +0 -23
  150. package/dist/index3.d.ts.map +0 -1
  151. package/dist/index4.d.ts +0 -74
  152. package/dist/index4.d.ts.map +0 -1
  153. package/dist/index5.d.ts.map +0 -1
  154. package/dist/integration.d.ts.map +0 -1
  155. package/dist/integration.js.map +0 -1
  156. package/dist/kept.js.map +0 -1
  157. package/dist/marks.js.map +0 -1
  158. package/dist/playwright.js.map +0 -1
  159. package/dist/recordings.js +0 -235
  160. package/dist/recordings.js.map +0 -1
  161. package/dist/shortcut.js.map +0 -1
  162. package/dist/tasks/index.d.ts.map +0 -1
  163. package/dist/tasks/index.js.map +0 -1
  164. package/dist/waveform.js.map +0 -1
  165. /package/dist/{shortcut.js → shared/shortcut.js} +0 -0
  166. /package/dist/{waveform.js → shared/waveform.js} +0 -0
@@ -0,0 +1,632 @@
1
+ import { readFileSync, readdirSync, rmSync, writeFileSync } from "node:fs";
2
+ import { dirname, join, relative, resolve } from "node:path";
3
+ import { Refusal, definePlugin } from "../index.js";
4
+ import { spawn } from "node:child_process";
5
+ import { createRequire } from "node:module";
6
+ import { createServer } from "node:net";
7
+ import { t as keptIn } from "./kept.js";
8
+ // src/plugins/tasks/local.ts
9
+ /**
10
+ * The board tasks go to when nothing names one: `vb serve`, `@vercel/board`'s own, started with the dev server
11
+ * and stopped with it, its log in `.deictic/board`. So Alt-click adds a task with nothing else to run, and
12
+ * whoever works the tasks reaches the same board at the URL `.deictic/board.json` holds.
13
+ */
14
+ /** `vb serve`'s own default, so `http://127.0.0.1:4100/api/board` is the board wherever it's free. */
15
+ const PORT = 4100;
16
+ /** How long a board may take to answer after it's started, or to be gone after it's stopped. */
17
+ const PATIENCE = 2e4;
18
+ /**
19
+ * The project's own board on this machine, as the tasks plugin's `board`: what it uses anyway with no
20
+ * `BOARD_URL`, here whatever the environment says, and with options.
21
+ */
22
+ const local = (options = {}) => ({ local: options });
23
+ /**
24
+ * The project's own board, started unless it's running: a dev server that restarts in its process (Vite, when
25
+ * its config changes) asks again, and so does a second dev server of the same project, and two boards mustn't
26
+ * write one log. One running with other options is stopped first, for the same reason.
27
+ */
28
+ async function localBoard(root, options = {}) {
29
+ const file = keptIn(root, "board.json");
30
+ const dir = options.dir === void 0 ? keptIn(root, "board") : resolve(root, options.dir);
31
+ const flags = [...options.inspect === false ? ["--no-inspect"] : [], ...options.args ?? []];
32
+ const as = JSON.stringify([
33
+ options.port ?? null,
34
+ dir,
35
+ flags
36
+ ]);
37
+ const running = read(file);
38
+ if (running !== void 0 && alive(running.pid) && await answers(running.url)) {
39
+ if (running.as === as) return {
40
+ url: running.url,
41
+ fresh: false,
42
+ dir,
43
+ stop: () => {}
44
+ };
45
+ process.kill(running.pid);
46
+ await gone(running.url);
47
+ }
48
+ const port = options.port ?? await free(PORT);
49
+ const url = `http://127.0.0.1:${port}/api/board`;
50
+ const child = spawn(process.execPath, [
51
+ cli(),
52
+ "serve",
53
+ "--port",
54
+ String(port),
55
+ "--no-open",
56
+ "--dir",
57
+ dir,
58
+ ...flags
59
+ ], { stdio: [
60
+ "ignore",
61
+ "ignore",
62
+ "pipe"
63
+ ] });
64
+ let said = "";
65
+ child.stderr.on("data", (chunk) => said += chunk.toString());
66
+ const stop = () => void child.kill();
67
+ process.once("exit", stop);
68
+ try {
69
+ await started(child, url);
70
+ } catch (e) {
71
+ stop();
72
+ const fix = options.port === void 0 ? "set BOARD_URL to a board of your own" : `is port ${port} taken? name another, or none`;
73
+ throw new Error(`the board didn't start (${said.trim() || e.message}): ${fix}`, { cause: e });
74
+ }
75
+ writeFileSync(file, `${JSON.stringify({
76
+ url,
77
+ pid: child.pid,
78
+ as
79
+ }, null, 2)}\n`);
80
+ child.unref();
81
+ child.stderr.unref?.();
82
+ return {
83
+ url,
84
+ fresh: true,
85
+ dir,
86
+ stop
87
+ };
88
+ }
89
+ /** `vb`'s script in the project's `@vercel/board`, run with this process's own runtime: no shim, no shell. */
90
+ function cli() {
91
+ return join(dirname(createRequire(import.meta.url).resolve("@vercel/board/package.json")), "dist", "cli.mjs");
92
+ }
93
+ function read(file) {
94
+ try {
95
+ const { url, pid, as } = JSON.parse(readFileSync(file, "utf8"));
96
+ return typeof url === "string" && typeof pid === "number" ? {
97
+ url,
98
+ pid,
99
+ as: as ?? ""
100
+ } : void 0;
101
+ } catch {
102
+ return;
103
+ }
104
+ }
105
+ function alive(pid) {
106
+ try {
107
+ process.kill(pid, 0);
108
+ return true;
109
+ } catch {
110
+ return false;
111
+ }
112
+ }
113
+ /** Whether a board answers there: its discovery, which names its own URL. */
114
+ async function answers(url) {
115
+ try {
116
+ const res = await fetch(url, { signal: AbortSignal.timeout(2e3) });
117
+ return res.ok && (await res.json()).url === url;
118
+ } catch {
119
+ return false;
120
+ }
121
+ }
122
+ /** `wanted` where nothing listens on it (another project's board may), or else a port the system picks. */
123
+ async function free(wanted) {
124
+ const listen = (port) => new Promise((done, failed) => {
125
+ const server = createServer();
126
+ server.once("error", failed);
127
+ server.listen(port, "127.0.0.1", () => {
128
+ const { port: got } = server.address();
129
+ server.close(() => done(got));
130
+ });
131
+ });
132
+ return listen(wanted).catch(() => listen(0));
133
+ }
134
+ const nap = () => new Promise((wake) => setTimeout(wake, 100));
135
+ /** Waits for the board to answer, or for its process to end without having. */
136
+ async function started(child, url) {
137
+ let ended;
138
+ child.once("error", (e) => ended = e.message);
139
+ child.once("exit", (code) => ended ??= `exit ${code}`);
140
+ for (const until = Date.now() + PATIENCE; Date.now() < until; await nap()) {
141
+ if (ended !== void 0) throw new Error(ended);
142
+ if (await answers(url)) return;
143
+ }
144
+ throw new Error(`no answer at ${url} after ${PATIENCE / 1e3}s`);
145
+ }
146
+ /** Waits for a stopped board to let go of its port and its log. */
147
+ async function gone(url) {
148
+ for (const until = Date.now() + PATIENCE; Date.now() < until; await nap()) if (!await answers(url)) return;
149
+ throw new Error(`the board at ${url} is still running: stop it, then restart the dev server`);
150
+ }
151
+ // src/plugins/tasks/watchers.ts
152
+ /**
153
+ * Who watches the channel for tasks. A board can't say (it knows who claimed a task, not who's listening), so a
154
+ * watch says it itself: the skill's `watch.mjs` keeps a file under `.deictic/watchers` while it runs, written
155
+ * again every 20 seconds, and the Task tab shows the names, so a person knows whether a task they add is taken up.
156
+ * (An agent holding a claim is one more, which the board does say: `agentsOf`, in `index.ts`.)
157
+ */
158
+ /** How old a watch's last word may be: three of its beats, as one killed says nothing more. */
159
+ const FRESH = 6e4;
160
+ /** The names watching `channel` from this project now, each once, in order. */
161
+ function watchersOf(root, channel, now = Date.now()) {
162
+ const dir = keptIn(root, "watchers");
163
+ let files;
164
+ try {
165
+ files = readdirSync(dir);
166
+ } catch {
167
+ return [];
168
+ }
169
+ const names = /* @__PURE__ */ new Set();
170
+ for (const file of files) {
171
+ const path = join(dir, file);
172
+ try {
173
+ const { name, channel: of, at } = JSON.parse(readFileSync(path, "utf8"));
174
+ if (typeof at !== "number" || now - at > FRESH) rmSync(path, { force: true });
175
+ else if (of === channel && typeof name === "string") names.add(name);
176
+ } catch {}
177
+ }
178
+ return [...names].sort();
179
+ }
180
+ // src/plugins/tasks/index.ts
181
+ /**
182
+ * `deictic/tasks`: open its tab on an element to add a task about it to a Vercel Board
183
+ * (`@vercel/board`). The task says what to change, in the words typed, and where: the element's file,
184
+ * line and tag, the page and the project, all as labels beside the short facts (viewport, how many, where
185
+ * it is in the shot), and after the words what a label can't hold: the chain of source it rendered through, from
186
+ * its own line out to the files that use its component. Its HTML as it rendered is a file attached, `element.html`, for whoever
187
+ * wants more than the task says: a task stays short. Whatever works the board's tasks (a person, or an agent of theirs claiming them) takes it
188
+ * from there; deictic only adds them.
189
+ *
190
+ * Adding one is a change like any other: undo cancels the task, while no one has claimed it, and redo
191
+ * adds it again, as a new task. Each task carries `deictic.at=<file:line:column>` and `deictic.file`, so
192
+ * the panel lists the ones about the element clicked, and after them the rest of its file's. One whose
193
+ * place is no longer the element's is still its own where the tag and the line it saw (`deictic.line`) are
194
+ * the element's, and that line is in the file once: the element has moved, as lines above it changed.
195
+ */
196
+ const AT = "deictic.at";
197
+ const FILE = "deictic.file";
198
+ /** What parts what was typed from the rest of a task's text. */
199
+ const REST = "\n---\n";
200
+ const OPEN = /* @__PURE__ */ new Set([
201
+ "waiting",
202
+ "ready",
203
+ "blocked"
204
+ ]);
205
+ function tasks(options = {}) {
206
+ const channel = options.channel ?? "deictic";
207
+ let team;
208
+ let root;
209
+ const own = options.board !== void 0 && "local" in options.board ? options.board.local : void 0;
210
+ const found = async () => {
211
+ if (options.board !== void 0 && !("local" in options.board)) return options.board;
212
+ const { board } = await import("@vercel/board");
213
+ if (own === void 0 && process.env.BOARD_URL !== void 0) return board();
214
+ const base = root ?? process.cwd();
215
+ const { url, fresh, dir } = await localBoard(base, own);
216
+ if (fresh) {
217
+ const kept = relative(base, dir).replaceAll("\\", "/");
218
+ console.log(`deictic/tasks: tasks go to a board on this machine, ${url.replace(/\/api\/board$/, "/")} (kept in ${kept})`);
219
+ }
220
+ const before = process.env.BOARD_URL;
221
+ process.env.BOARD_URL = url;
222
+ try {
223
+ return board();
224
+ } finally {
225
+ if (before === void 0) delete process.env.BOARD_URL;
226
+ else process.env.BOARD_URL = before;
227
+ }
228
+ };
229
+ const boardOf = () => team ??= found().catch((e) => {
230
+ team = void 0;
231
+ throw e;
232
+ });
233
+ let link;
234
+ const linkOf = () => link ??= (options.link ? Promise.resolve(options.link(channel)) : boardOf().then((b) => own?.inspect === false ? void 0 : localPage(b, channel))).catch(() => {
235
+ link = void 0;
236
+ });
237
+ /** A task's page: what `link` says, or its thread on the channel's own page on this machine. */
238
+ const taskLink = (page, id) => options.link ? options.link(channel, id) : page === void 0 ? void 0 : `${page}?thread=${encodeURIComponent(id)}`;
239
+ /**
240
+ * One of this channel's tasks about an element, while it's still open: the page names it by ID, so
241
+ * nothing else on the board is its to change, nor a task someone has taken up.
242
+ */
243
+ const openTask = async (team, id, verb) => {
244
+ const { message } = await team.thread(id);
245
+ if (message.channel !== channel || message.labels[AT] === void 0) throw new Refusal(`#${id} isn't a task of deictic's in #${channel}: reload the page for the ones that are`);
246
+ const { task } = message;
247
+ if (task !== void 0 && !OPEN.has(task.state)) throw new Refusal(`task ${id} is ${task.state}${task.claim ? ` by ${task.claim.actor}` : ""} now: ${verb} it on the board if you still mean to`);
248
+ return message;
249
+ };
250
+ /** Cancels an open task of ours, and answers it as it was. */
251
+ const cancel = async (id, reason) => {
252
+ const team = await boardOf();
253
+ const message = await openTask(team, id, "cancel");
254
+ await team.tasks.cancel(id, reason);
255
+ return message;
256
+ };
257
+ /** Puts a task's text from one to the other, unless it reads as neither any more. */
258
+ const rewrite = async (id, from, to) => {
259
+ const team = await boardOf();
260
+ const message = await openTask(team, id, "edit");
261
+ if (message.text === to) return;
262
+ if (message.text !== from) throw new Refusal(`task ${id} has been edited since: change it on the board`);
263
+ await team.edit(id, to, { revision: message.revision });
264
+ };
265
+ const create = async (record) => {
266
+ const { task } = await (await boardOf()).tasks.create(record.channel, {
267
+ text: record.text,
268
+ labels: record.labels,
269
+ ...record.attachments === void 0 ? {} : { attachments: record.attachments }
270
+ });
271
+ return {
272
+ ...record,
273
+ id: task.id
274
+ };
275
+ };
276
+ let context;
277
+ let project = options.project;
278
+ /** The screenshots, recordings and the element's HTML to attach, uploaded to the board, or nothing when there are none. */
279
+ const attach = async (op) => {
280
+ const files = [
281
+ ...(op.screenshots ?? []).map(({ id }) => {
282
+ const shot = context?.use("screenshots")?.read(id);
283
+ if (shot === void 0) throw new Refusal(`screenshot ${id} isn't kept any more: take it again`);
284
+ return shot;
285
+ }),
286
+ ...(op.recordings ?? []).map(({ id }) => {
287
+ const recording = context?.use("recordings")?.read(id);
288
+ if (recording === void 0) throw new Refusal(`recording ${id} isn't kept any more: record it again`);
289
+ return recording;
290
+ }),
291
+ ...[...(op.recordings ?? []).flatMap((r) => r.voice ?? []), ...(op.voice ?? []).map((v) => v.id)].map((id) => {
292
+ const clip = context?.use("voice")?.read(id);
293
+ if (clip === void 0) throw new Refusal(`voice clip ${id} isn't kept any more: record it again`);
294
+ return clip;
295
+ }),
296
+ ...typeof op.html !== "string" || op.html === "" ? [] : [{
297
+ data: new TextEncoder().encode(op.html.slice(0, HTML)),
298
+ name: "element.html",
299
+ type: "text/html"
300
+ }]
301
+ ];
302
+ if (files.length === 0) return void 0;
303
+ const team = await boardOf();
304
+ return Promise.all(files.map(async (file) => (await team.upload(file)).id));
305
+ };
306
+ return definePlugin({
307
+ name: "tasks",
308
+ client: "deictic/tasks/client",
309
+ ...options.html === false ? { clientOptions: { html: false } } : {},
310
+ shortcut: options.shortcut,
311
+ setup: (given) => {
312
+ context = given;
313
+ root = given.root;
314
+ project ??= nameOf(given.root);
315
+ boardOf().catch(() => {});
316
+ },
317
+ read: (source, target) => asked(async () => {
318
+ const team = await boardOf();
319
+ const labelled = (label) => team.tasks.list({
320
+ channel,
321
+ label,
322
+ state: "all"
323
+ });
324
+ const [here, inFile, page, held] = await Promise.all([
325
+ labelled({ [AT]: atOf(target) }),
326
+ labelled({ [FILE]: clip(target.loc.file, LABEL) }),
327
+ linkOf(),
328
+ team.tasks.list({
329
+ channel,
330
+ state: "claimed"
331
+ })
332
+ ]);
333
+ const listed = new Set(inFile.tasks.map(({ message }) => message.id));
334
+ const line = lineOf(source);
335
+ const once = source.code.split(line).length === 2;
336
+ const tasks = [];
337
+ const elsewhere = [];
338
+ for (const { message } of [...here.tasks.filter((t) => !listed.has(t.message.id)), ...inFile.tasks]) {
339
+ const state = message.task?.state ?? "ready";
340
+ if (state === "canceled") continue;
341
+ const text = message.text.split(REST, 1)[0];
342
+ const to = taskLink(page, message.id);
343
+ const task = {
344
+ id: message.id,
345
+ title: text.split("\n", 1)[0],
346
+ text,
347
+ state,
348
+ open: OPEN.has(state),
349
+ ...to === void 0 ? {} : { link: to }
350
+ };
351
+ const at = placeIn(message.labels);
352
+ if (at === void 0 || message.labels[AT] === atOf(target)) tasks.push(task);
353
+ else if (once && at.tag === target.tag && message.labels["deictic.line"] === clip(line, LABEL)) tasks.push({
354
+ ...task,
355
+ at
356
+ });
357
+ else elsewhere.push({
358
+ ...task,
359
+ at
360
+ });
361
+ }
362
+ const working = await agentsOf(team, held.tasks.flatMap(({ message }) => message.task?.claim ?? []));
363
+ const watching = [.../* @__PURE__ */ new Set([...watchersOf(root ?? process.cwd(), channel), ...working])].sort();
364
+ const ours = own !== void 0 || options.board === void 0 && process.env.BOARD_URL === void 0;
365
+ return {
366
+ channel,
367
+ ...page === void 0 ? {} : { link: page },
368
+ ...watching.length > 0 || ours ? { watching } : {},
369
+ tasks,
370
+ elsewhere: elsewhere.sort((a, b) => a.at.line === b.at.line ? a.at.column - b.at.column : a.at.line - b.at.line)
371
+ };
372
+ }),
373
+ apply: (source, target, op) => {
374
+ if ("remove" in op) return asked(async () => {
375
+ const message = await cancel(op.remove, "deleted in deictic");
376
+ const labels = Object.fromEntries(Object.entries(message.labels).filter(([key]) => !key.startsWith("board.")));
377
+ const attachments = message.attachments.map((a) => a.id);
378
+ return { done: {
379
+ kind: "removed",
380
+ channel,
381
+ id: op.remove,
382
+ text: message.text,
383
+ labels,
384
+ ...attachments.length === 0 ? {} : { attachments }
385
+ } };
386
+ });
387
+ if (op.text.trim() === "" && ("edit" in op || shown(op) === void 0)) throw new Refusal("say what should change first");
388
+ if ("edit" in op) return asked(async () => {
389
+ const before = (await openTask(await boardOf(), op.edit, "edit")).text;
390
+ const rest = before.includes(REST) ? before.slice(before.indexOf(REST)) : "";
391
+ const after = clip(op.text.trim(), 1e4 - rest.length) + rest;
392
+ await rewrite(op.edit, before, after);
393
+ return { done: {
394
+ kind: "edited",
395
+ id: op.edit,
396
+ before,
397
+ after
398
+ } };
399
+ });
400
+ const labels = {
401
+ ...options.labels,
402
+ ...project === void 0 ? {} : { "deictic.project": clip(project, LABEL) },
403
+ ...labelsOf(source, target, op)
404
+ };
405
+ return asked(async () => {
406
+ const attachments = await attach(op);
407
+ return { done: await create({
408
+ channel,
409
+ text: textOf(op, target),
410
+ labels,
411
+ ...attachments === void 0 ? {} : { attachments }
412
+ }) };
413
+ });
414
+ },
415
+ undo: (record) => asked(async () => {
416
+ if (!("kind" in record)) {
417
+ await cancel(record.id, "undone in deictic");
418
+ return;
419
+ }
420
+ if (record.kind === "edited") {
421
+ await rewrite(record.id, record.after, record.before);
422
+ return;
423
+ }
424
+ const { kind, id: _, ...task } = record;
425
+ return {
426
+ kind,
427
+ ...await create(task)
428
+ };
429
+ }),
430
+ redo: (record) => asked(async () => {
431
+ if (!("kind" in record)) {
432
+ const { id: _, ...added } = record;
433
+ return create(added);
434
+ }
435
+ if (record.kind === "edited") {
436
+ await rewrite(record.id, record.before, record.after);
437
+ return record;
438
+ }
439
+ await cancel(record.id, "deleted in deictic");
440
+ return record;
441
+ }),
442
+ describe: (op, before) => {
443
+ const named = (id) => `task "${clip(before.tasks.find((t) => t.id === id)?.title ?? `#${id}`, 60)}"`;
444
+ if ("remove" in op) return `delete ${named(op.remove)}`;
445
+ if ("edit" in op) return `edit ${named(op.edit)}`;
446
+ return `task "${clip(wordsOf(op).split("\n", 1)[0], 60)}"`;
447
+ }
448
+ });
449
+ }
450
+ /**
451
+ * The channel's page on a board `vb serve` runs on this machine, which serves its pages beside its API:
452
+ * `/api/board` there, `/c/<channel>` beside it. Nothing for any other board.
453
+ */
454
+ async function localPage(team, channel) {
455
+ const url = new URL((await team.discovery()).url);
456
+ if (![
457
+ "127.0.0.1",
458
+ "localhost",
459
+ "[::1]"
460
+ ].includes(url.hostname) || !url.pathname.endsWith("/api/board")) return void 0;
461
+ return `${url.origin}${url.pathname.slice(0, -10)}/c/${channel.split("/").map(encodeURIComponent).join("/")}`;
462
+ }
463
+ const plural = (count, word) => `${count} ${word}${count === 1 ? "" : "s"}`;
464
+ const said = (notes) => notes.length === 0 ? "" : `; the notes say: ${notes.map((n) => JSON.stringify(n)).join(", ")}`;
465
+ /** What was marked on the page: how many marks, and what the notes say. */
466
+ function annotated({ count, notes }, shots, recordings) {
467
+ const where = [...shots === 0 ? [] : [shots === 1 ? "the screenshot" : "the screenshots"], ...recordings === 0 ? [] : [recordings === 1 ? "the recording" : "the recordings"]];
468
+ const drawn = where.length === 0 ? "" : `, drawn in ${where.join(" and ")}`;
469
+ return `${plural(count, "mark")} on the page${drawn}${said(notes)}`;
470
+ }
471
+ /** How long each recording ran, and where it plays back, while the dev server keeps it. */
472
+ function recordingsOf(recordings) {
473
+ return recordings.map(({ duration, replay, voice }, i) => {
474
+ const name = recordings.length === 1 ? "A recording" : `Recording ${i + 1}`;
475
+ const spoken = voice === void 0 || voice.length === 0 ? "" : `, with the voice (${voice.map((id) => `voice-${id}`).join(", ")})`;
476
+ return `${name}, ${clock(duration)} long, attached as rrweb events${spoken}; it replays at ${replay}`;
477
+ });
478
+ }
479
+ /** How long each voice note runs, and the attachment it's in. */
480
+ function voicesOf(voice) {
481
+ return voice.map(({ id, duration }, i) => {
482
+ return `${voice.length === 1 ? "A voice note" : `Voice note ${i + 1}`}, ${clock(duration)} long, attached as voice-${id}`;
483
+ });
484
+ }
485
+ const clock = (ms) => {
486
+ const s = Math.round(ms / 1e3);
487
+ return `${Math.floor(s / 60)}:${String(s % 60).padStart(2, "0")}`;
488
+ };
489
+ /**
490
+ * What's drawn on each shot (the element's outline, the person's marks), and with several, where the element is
491
+ * in each. One shot's place is the `deictic.position` and `deictic.size` labels, so a shot with nothing drawn
492
+ * on it has no line.
493
+ */
494
+ function shotsOf(shots) {
495
+ const [only] = shots;
496
+ if (shots.length === 1 && only !== void 0) {
497
+ const { marks, outlined } = only;
498
+ const has = [...outlined === true ? ["the element outlined in dashes"] : [], ...marks === void 0 || marks.count === 0 ? [] : [`${plural(marks.count, "mark")} drawn on it${said(marks.notes)}`]];
499
+ return has.length === 0 ? [] : [`The screenshot has ${has.join(" and ")}`];
500
+ }
501
+ return shots.map(({ box, marks, outlined }, i) => {
502
+ const drawn = marks === void 0 || marks.count === 0 ? "" : `, with ${plural(marks.count, "mark")} drawn on it${said(marks.notes)}`;
503
+ const [x, y, width, height] = boxOf(box);
504
+ return `In screenshot ${i + 1} at x ${x}, y ${y}, ${width}×${height}${outlined === true ? ", outlined in dashes" : ""}${drawn}`;
505
+ });
506
+ }
507
+ /** `Source: src/ui/label.tsx:6 ← src/Field.tsx:20 ← src/Panel.tsx:880`: from the element's line out, as the page said it. */
508
+ function chainOf({ loc }, chain) {
509
+ const places = chain.slice(0, CHAIN).map((place) => clip(String(place), LABEL));
510
+ return places.length === 0 ? [] : [`Source: ${[`${loc.file}:${loc.line}`, ...places].join(" ← ")}`];
511
+ }
512
+ /** How many places a chain names after the element's own: the first few say which use of a component it is. */
513
+ const CHAIN = 5;
514
+ /** A box as `x,y,width,height`, in whole CSS pixels from the shot's top left. */
515
+ const boxOf = ({ x, y, width, height }) => [
516
+ x,
517
+ y,
518
+ width,
519
+ height
520
+ ].map(Math.round);
521
+ /** What `element.html` holds of an element at most, in characters: a table's rows, not a whole page. */
522
+ const HTML = 1e5;
523
+ /** A label's value: a board holds 256 characters. */
524
+ const LABEL = 256;
525
+ /** The project's name: the `name` in the root's `package.json`, or nothing where it has none. */
526
+ function nameOf(root) {
527
+ try {
528
+ const { name } = JSON.parse(readFileSync(join(root, "package.json"), "utf8"));
529
+ return typeof name === "string" && name !== "" ? name : void 0;
530
+ } catch {
531
+ return;
532
+ }
533
+ }
534
+ /**
535
+ * The facts about the element, as labels: short values a script reads without parsing the text, and a watch
536
+ * filters by. `deictic.line` is the opening tag's first line as the task saw it, to tell later that it moved.
537
+ * `deictic.url` is the page's whole URL, where a label holds it: `textOf` says a longer one.
538
+ */
539
+ function labelsOf({ code, element }, target, op) {
540
+ const [shot] = op.screenshots ?? [];
541
+ return {
542
+ [AT]: atOf(target),
543
+ [FILE]: clip(target.loc.file, LABEL),
544
+ "deictic.tag": target.tag,
545
+ "deictic.line": clip(lineOf({
546
+ code,
547
+ element
548
+ }), LABEL),
549
+ "deictic.page": clip(new URL(op.page).pathname, LABEL),
550
+ ...op.page.length <= LABEL ? { "deictic.url": op.page } : {},
551
+ "deictic.viewport": `${op.viewport.width}x${op.viewport.height}`,
552
+ "deictic.count": String(op.count),
553
+ ...op.screenshots?.length === 1 && shot !== void 0 ? placeOf(shot.box) : {}
554
+ };
555
+ }
556
+ /** Where the element is in the one shot, from its top left, and how big: `87,387` and `138x32`, as the viewport is written. */
557
+ function placeOf(box) {
558
+ const [x, y, width, height] = boxOf(box);
559
+ return {
560
+ "deictic.position": `${x},${y}`,
561
+ "deictic.size": `${width}x${height}`
562
+ };
563
+ }
564
+ /**
565
+ * The agents holding these claims, by name, while their leases run. An actor's ID isn't read for it (each board
566
+ * writes them its own way), so the board is asked who they are, and only when a claim is held. A person holding
567
+ * one is left out: they needn't be told they're there. (Whatever else holds one works tasks: an agent, a workflow.)
568
+ */
569
+ async function agentsOf(team, claims) {
570
+ const holders = new Set(claims.filter((claim) => Date.parse(claim.until) > Date.now()).map((claim) => claim.actor));
571
+ if (holders.size === 0) return [];
572
+ return (await team.actors({ id: [...holders] })).filter((actor) => holders.has(actor.id) && actor.kind !== "person").map((actor) => actor.name);
573
+ }
574
+ const atOf = ({ loc }) => `${loc.file}:${loc.line}:${loc.column}`;
575
+ /** The opening tag's first line, as written. */
576
+ const lineOf = ({ code, element }) => code.slice(element.start, element.end).split("\n", 1)[0].trim();
577
+ /** Where a task says its element is, from its labels. */
578
+ function placeIn(labels) {
579
+ const [, line, column] = /:(\d+):(\d+)$/.exec(labels[AT] ?? "") ?? [];
580
+ if (line === void 0 || column === void 0) return void 0;
581
+ return {
582
+ line: Number(line),
583
+ column: Number(column),
584
+ tag: labels["deictic.tag"] ?? ""
585
+ };
586
+ }
587
+ /**
588
+ * The task's text: what was typed, and after a `---` only what no label holds: what's attached and drawn, the
589
+ * source it rendered through, what was pointed at inside it, and a URL too long for a label.
590
+ * A board holds 10,000 characters.
591
+ */
592
+ function textOf(op, target) {
593
+ const more = [
594
+ ...op.page.length <= LABEL ? [] : [op.page],
595
+ ...shotsOf(op.screenshots ?? []),
596
+ ...recordingsOf(op.recordings ?? []),
597
+ ...voicesOf(op.voice ?? []),
598
+ ...op.annotations === void 0 ? [] : [annotated(op.annotations, op.screenshots?.length ?? 0, op.recordings?.length ?? 0)],
599
+ ...chainOf(target, op.chain ?? []),
600
+ ...op.pointed === void 0 ? [] : [`Pointed at, inside it: \`${op.pointed.path}\``, fence(op.pointed.html, "html")]
601
+ ];
602
+ const tail = more.length === 0 ? "" : REST + more.join("\n");
603
+ return clip(wordsOf(op), 1e4 - tail.length) + tail;
604
+ }
605
+ /** The task's words: what was typed, or where nothing was, a line that sends the reader to what's attached. */
606
+ const wordsOf = (op) => op.text.trim() === "" ? shown(op) ?? "" : op.text.trim();
607
+ /** `See the recording and the voice note`: what stands for the words of a task sent with none, where something is attached. */
608
+ function shown(op) {
609
+ const named = (count, word) => count === void 0 || count === 0 ? [] : [`the ${word}${count === 1 ? "" : "s"}`];
610
+ const attached = [
611
+ ...named(op.screenshots?.length, "screenshot"),
612
+ ...named(op.recordings?.length, "recording"),
613
+ ...named(op.voice?.length, "voice note")
614
+ ];
615
+ return attached.length === 0 ? void 0 : `See ${new Intl.ListFormat("en", { type: "conjunction" }).format(attached)}`;
616
+ }
617
+ const fence = (text, lang) => `\`\`\`\`${lang}\n${clip(text, 1500)}\n\`\`\`\``;
618
+ const clip = (text, max) => text.length <= max ? text : `${text.slice(0, max - 1)}…`;
619
+ /** The board's answer, or a `Refusal` saying what to do when there's no board to ask or it said no. */
620
+ async function asked(ask) {
621
+ try {
622
+ return await ask();
623
+ } catch (e) {
624
+ if (e instanceof Refusal) throw e;
625
+ const { code, message, hint } = e;
626
+ if (typeof code === "string") throw new Refusal(`the board says: ${message}${hint === void 0 ? "" : ` (${hint})`}`, { cause: e });
627
+ throw new Refusal(`can't reach the board (${message ?? String(e)}): restart the dev server, which starts its own, or check the BOARD_URL or \`board\` it was given`, { cause: e });
628
+ }
629
+ }
630
+ export { local as n, tasks as t };
631
+
632
+ //# sourceMappingURL=tasks.js.map