@tenonai/yew 0.0.0-stage → 1.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 (133) hide show
  1. package/CHANGELOG.md +273 -0
  2. package/LICENSE +21 -0
  3. package/README.md +64 -2
  4. package/THIRD-PARTY-NOTICES.md +122 -0
  5. package/bin/yew.js +9 -0
  6. package/dist/cli/app.js +211 -0
  7. package/dist/cli/export.js +57 -0
  8. package/dist/cli/io.js +65 -0
  9. package/dist/cli/key.js +113 -0
  10. package/dist/cli/main.js +58 -0
  11. package/dist/cli/mcp.js +76 -0
  12. package/dist/cli/remote.js +116 -0
  13. package/dist/cli/run.js +72 -0
  14. package/dist/cli/schedule.js +123 -0
  15. package/dist/cli/send.js +127 -0
  16. package/dist/cli/serve.js +301 -0
  17. package/dist/cli/service.js +268 -0
  18. package/dist/cli/system.js +44 -0
  19. package/dist/remote/access.js +107 -0
  20. package/dist/remote/connector.js +278 -0
  21. package/dist/remote/identity.js +71 -0
  22. package/dist/remote/noise.js +287 -0
  23. package/dist/remote/protocol.js +51 -0
  24. package/dist/remote/push.js +37 -0
  25. package/dist/server/agent/compaction.js +242 -0
  26. package/dist/server/agent/context.js +120 -0
  27. package/dist/server/agent/held.js +48 -0
  28. package/dist/server/agent/helper.js +122 -0
  29. package/dist/server/agent/mailroom.js +510 -0
  30. package/dist/server/agent/prompt.js +268 -0
  31. package/dist/server/agent/runner.js +1159 -0
  32. package/dist/server/artifacts.js +216 -0
  33. package/dist/server/budget.js +88 -0
  34. package/dist/server/data-export.js +105 -0
  35. package/dist/server/data-usage.js +144 -0
  36. package/dist/server/db/database.js +49 -0
  37. package/dist/server/db/migrations.js +343 -0
  38. package/dist/server/errors.js +16 -0
  39. package/dist/server/events.js +17 -0
  40. package/dist/server/export.js +378 -0
  41. package/dist/server/folder-picker.js +32 -0
  42. package/dist/server/from-jig.js +98 -0
  43. package/dist/server/http/agents.js +78 -0
  44. package/dist/server/http/api.js +465 -0
  45. package/dist/server/http/app.js +111 -0
  46. package/dist/server/http/artifacts.js +53 -0
  47. package/dist/server/http/data.js +54 -0
  48. package/dist/server/http/devices.js +47 -0
  49. package/dist/server/http/events.js +134 -0
  50. package/dist/server/http/idempotency.js +53 -0
  51. package/dist/server/http/input.js +106 -0
  52. package/dist/server/http/mail.js +52 -0
  53. package/dist/server/http/mcp.js +134 -0
  54. package/dist/server/http/notes.js +91 -0
  55. package/dist/server/http/problem.js +55 -0
  56. package/dist/server/http/providers.js +131 -0
  57. package/dist/server/http/schedules.js +58 -0
  58. package/dist/server/http/search.js +42 -0
  59. package/dist/server/http/security.js +168 -0
  60. package/dist/server/http/skills.js +53 -0
  61. package/dist/server/http/system.js +26 -0
  62. package/dist/server/ids.js +30 -0
  63. package/dist/server/keys.js +70 -0
  64. package/dist/server/mail/envelope.js +152 -0
  65. package/dist/server/mail/post.js +603 -0
  66. package/dist/server/mail/store.js +279 -0
  67. package/dist/server/manage.js +601 -0
  68. package/dist/server/mcp-builtin.js +180 -0
  69. package/dist/server/mcp-config.js +305 -0
  70. package/dist/server/mcp-oauth.js +448 -0
  71. package/dist/server/mcp.js +668 -0
  72. package/dist/server/model/gateway.js +410 -0
  73. package/dist/server/paths.js +43 -0
  74. package/dist/server/permissions/engine.js +249 -0
  75. package/dist/server/permissions/paths.js +93 -0
  76. package/dist/server/permissions/shell.js +635 -0
  77. package/dist/server/push.js +345 -0
  78. package/dist/server/reveal.js +36 -0
  79. package/dist/server/runtime.js +17 -0
  80. package/dist/server/schedules.js +356 -0
  81. package/dist/server/settings.js +77 -0
  82. package/dist/server/skills.js +263 -0
  83. package/dist/server/store.js +1077 -0
  84. package/dist/server/tools/files.js +318 -0
  85. package/dist/server/tools/html.js +221 -0
  86. package/dist/server/tools/jobs.js +38 -0
  87. package/dist/server/tools/mail.js +680 -0
  88. package/dist/server/tools/match.js +81 -0
  89. package/dist/server/tools/misc.js +253 -0
  90. package/dist/server/tools/process.js +128 -0
  91. package/dist/server/tools/read-state.js +40 -0
  92. package/dist/server/tools/registry.js +43 -0
  93. package/dist/server/tools/search.js +175 -0
  94. package/dist/server/tools/shell.js +124 -0
  95. package/dist/server/tools/types.js +43 -0
  96. package/dist/server/tools/web-search.js +198 -0
  97. package/dist/server/tools/web.js +240 -0
  98. package/dist/shared/cron.js +105 -0
  99. package/dist/shared/mcp-presets.js +170 -0
  100. package/dist/shared/messages.js +45 -0
  101. package/dist/shared/policy.js +42 -0
  102. package/dist/shared/types.js +34 -0
  103. package/dist/shared/version.js +8 -0
  104. package/mcp-builtin/package-lock.json +3398 -0
  105. package/mcp-builtin/package.json +14 -0
  106. package/mcp-builtin/presets/gitee/package-lock.json +105 -0
  107. package/mcp-builtin/presets/gitee/package.json +10 -0
  108. package/mcp-builtin/presets/notion/package-lock.json +2243 -0
  109. package/mcp-builtin/presets/notion/package.json +10 -0
  110. package/mcp-builtin/presets/sentry/package-lock.json +28 -0
  111. package/mcp-builtin/presets/sentry/package.json +10 -0
  112. package/package.json +75 -5
  113. package/web/dist/THIRD-PARTY-NOTICES.txt +1944 -0
  114. package/web/dist/assets/index-BtK_SzpF.css +1 -0
  115. package/web/dist/assets/index-C0rnUFI-.js +68 -0
  116. package/web/dist/icons/app-icon.png +0 -0
  117. package/web/dist/icons/apple-touch-icon.png +0 -0
  118. package/web/dist/icons/favicon-16.png +0 -0
  119. package/web/dist/icons/favicon-32.png +0 -0
  120. package/web/dist/icons/favicon-64.png +0 -0
  121. package/web/dist/icons/tenon-mark.svg +1 -0
  122. package/web/dist/icons/yew-mark.svg +1 -0
  123. package/web/dist/index.html +18 -0
  124. package/web/dist/mascot/gift.webp +0 -0
  125. package/web/dist/mascot/greeting.webp +0 -0
  126. package/web/dist/mascot/happy.webp +0 -0
  127. package/web/dist/mascot/lost.webp +0 -0
  128. package/web/dist/mascot/shush.webp +0 -0
  129. package/web/dist/mascot/sleepy.webp +0 -0
  130. package/web/dist/mascot/sorry.webp +0 -0
  131. package/web/dist/mascot/thinking.webp +0 -0
  132. package/web/dist/mascot/waking.webp +0 -0
  133. package/web/dist/mascot/writing.webp +0 -0
@@ -0,0 +1,318 @@
1
+ import { existsSync, mkdirSync, readFileSync, statSync, writeFileSync } from "node:fs";
2
+ import { dirname, extname, relative } from "node:path";
3
+ import { createTwoFilesPatch } from "diff";
4
+ import { resolvePath } from "../permissions/paths.js";
5
+ import { countOccurrences, findUnique } from "./match.js";
6
+ import { MAX_BYTES, MAX_LINES } from "./process.js";
7
+ import { bool, CONFIRM_ARGUMENT, errorResult, int, str, ToolInputError, text, } from "./types.js";
8
+ const MAX_LINE_CHARS = 2000;
9
+ const MAX_FILE_BYTES = 20 * 1024 * 1024;
10
+ const MAX_IMAGE_BYTES = 5 * 1024 * 1024;
11
+ const MAX_DIFF_CHARS = 200_000;
12
+ const MAX_PREVIEW_CHARS = 40_000;
13
+ const IMAGE_TYPES = {
14
+ ".png": "image/png",
15
+ ".jpg": "image/jpeg",
16
+ ".jpeg": "image/jpeg",
17
+ ".gif": "image/gif",
18
+ ".webp": "image/webp",
19
+ };
20
+ export function pathArg(args, session, key = "path") {
21
+ const raw = str(args, key, true);
22
+ const path = resolvePath(raw, session.cwd);
23
+ if (!path)
24
+ throw new ToolInputError(`${key} must be an absolute path, or relative to the working folder.`);
25
+ return path;
26
+ }
27
+ /** Paths shown to the model and the person: relative inside the working folder, absolute outside. */
28
+ export function display(path, session) {
29
+ if (!session.cwd)
30
+ return path;
31
+ const rel = relative(session.cwd, path);
32
+ return rel === "" ? "." : rel.startsWith("..") ? path : rel;
33
+ }
34
+ function numbered(lines, first) {
35
+ const width = String(first + lines.length - 1).length;
36
+ return lines.map((line, index) => `${String(first + index).padStart(width)}\t${line}`).join("\n");
37
+ }
38
+ function diffDetail(path, before, after, created) {
39
+ let patch = createTwoFilesPatch(path, path, before, after, "", "", { context: 3 });
40
+ let truncated = false;
41
+ if (patch.length > MAX_DIFF_CHARS) {
42
+ patch = patch.slice(0, MAX_DIFF_CHARS);
43
+ truncated = true;
44
+ }
45
+ let added = 0;
46
+ let removed = 0;
47
+ for (const line of patch.split("\n")) {
48
+ if (line.startsWith("+") && !line.startsWith("+++"))
49
+ added++;
50
+ else if (line.startsWith("-") && !line.startsWith("---"))
51
+ removed++;
52
+ }
53
+ return { path, diff: patch, added, removed, created, truncated };
54
+ }
55
+ /** A preview patch for a permission card: the change without its file header, capped. */
56
+ function previewPatch(before, after) {
57
+ const patch = createTwoFilesPatch("", "", before, after, "", "", { context: 3 });
58
+ const hunks = patch.slice(Math.max(0, patch.indexOf("\n@@") + 1));
59
+ return hunks.length > MAX_PREVIEW_CHARS ? `${hunks.slice(0, MAX_PREVIEW_CHARS)}\n…` : hunks;
60
+ }
61
+ /**
62
+ * Replaces old_text in a file's text the way edit does: once, where it matches exactly one place, or
63
+ * everywhere with replace_all. Models write \n; a file's \r\n line endings are kept.
64
+ */
65
+ function replaceText(body, oldText, newText, replaceAll, shown) {
66
+ let from = oldText;
67
+ let to = newText;
68
+ if (body.includes("\r\n")) {
69
+ if (!from.includes("\r\n"))
70
+ from = from.replace(/\n/g, "\r\n");
71
+ if (!to.includes("\r\n"))
72
+ to = to.replace(/\n/g, "\r\n");
73
+ }
74
+ if (replaceAll) {
75
+ const count = countOccurrences(body, from);
76
+ if (count === 0) {
77
+ return {
78
+ ok: false,
79
+ error: `old_text was not found in ${shown}. Read the file again and copy the text exactly.`,
80
+ };
81
+ }
82
+ return { ok: true, updated: body.split(from).join(to), count, start: body.indexOf(from) };
83
+ }
84
+ const match = findUnique(body, from);
85
+ if (!match.ok) {
86
+ return {
87
+ ok: false,
88
+ error: match.reason === "ambiguous"
89
+ ? `old_text appears ${match.count} times in ${shown}. Include more surrounding lines so it matches once, or set replace_all.`
90
+ : `old_text was not found in ${shown}. Read the file again and copy the text exactly, including indentation.`,
91
+ };
92
+ }
93
+ return {
94
+ ok: true,
95
+ updated: body.slice(0, match.start) + to + body.slice(match.end),
96
+ count: 1,
97
+ start: match.start,
98
+ };
99
+ }
100
+ function splitLines(content) {
101
+ const lines = content.split(/\r?\n/);
102
+ if (lines.length > 1 && lines.at(-1) === "")
103
+ lines.pop();
104
+ return lines;
105
+ }
106
+ export const readTool = {
107
+ name: "read",
108
+ description: "Read a text file or an image. Text comes back with line numbers. A read stops at 2000 lines or 50 KB; use offset (first line, from 1) and limit (number of lines) to read more. Paths are absolute or relative to the working folder.",
109
+ parameters: {
110
+ type: "object",
111
+ properties: {
112
+ path: { type: "string", description: "The file to read." },
113
+ offset: { type: "integer", minimum: 1, description: "First line to read, counting from 1." },
114
+ limit: {
115
+ type: "integer",
116
+ minimum: 1,
117
+ maximum: MAX_LINES,
118
+ description: "Number of lines to read.",
119
+ },
120
+ },
121
+ required: ["path"],
122
+ additionalProperties: false,
123
+ },
124
+ readOnly: true,
125
+ needsFolder: true,
126
+ subject(args, session) {
127
+ const path = pathArg(args, session);
128
+ return { kind: "read", tool: "read", paths: [path], summary: display(path, session) };
129
+ },
130
+ async run(args, ctx) {
131
+ const path = pathArg(args, ctx.session);
132
+ const shown = display(path, ctx.session);
133
+ const offset = int(args, "offset", 1, Number.MAX_SAFE_INTEGER) ?? 1;
134
+ const limit = int(args, "limit", 1, MAX_LINES) ?? MAX_LINES;
135
+ if (!existsSync(path))
136
+ return errorResult(`There is no file at ${shown}.`);
137
+ const stat = statSync(path);
138
+ if (stat.isDirectory())
139
+ return errorResult(`${shown} is a folder. Use glob to list files in it.`);
140
+ const imageType = IMAGE_TYPES[extname(path).toLowerCase()];
141
+ if (imageType) {
142
+ if (!ctx.acceptsImages)
143
+ return errorResult("The current model cannot read images.");
144
+ if (stat.size > MAX_IMAGE_BYTES)
145
+ return errorResult(`${shown} is larger than 5 MB.`);
146
+ const data = readFileSync(path);
147
+ ctx.readState.record(path, data);
148
+ return {
149
+ content: [
150
+ text(`Image ${shown}:`),
151
+ { type: "image", data: data.toString("base64"), mimeType: imageType },
152
+ ],
153
+ };
154
+ }
155
+ if (stat.size > MAX_FILE_BYTES) {
156
+ return errorResult(`${shown} is larger than 20 MB. Use grep, or head and tail in the shell, to look inside it.`);
157
+ }
158
+ const data = readFileSync(path);
159
+ if (data.subarray(0, 8192).includes(0)) {
160
+ return errorResult(`${shown} looks like a binary file, so it was not read.`);
161
+ }
162
+ ctx.readState.record(path, data);
163
+ const lines = splitLines(data.toString("utf8").replace(/^/, ""));
164
+ if (lines.length === 0 || (lines.length === 1 && lines[0] === "")) {
165
+ return { content: [text(`${shown} is empty.`)] };
166
+ }
167
+ if (offset > lines.length) {
168
+ return errorResult(`${shown} has ${lines.length} lines; offset ${offset} is past the end.`);
169
+ }
170
+ const picked = [];
171
+ let bytes = 0;
172
+ for (let i = offset - 1; i < lines.length && picked.length < limit; i++) {
173
+ let line = lines[i];
174
+ if (line.length > MAX_LINE_CHARS)
175
+ line = `${line.slice(0, MAX_LINE_CHARS)} [line cut at ${MAX_LINE_CHARS} characters]`;
176
+ bytes += Buffer.byteLength(line) + 1;
177
+ if (bytes > MAX_BYTES && picked.length > 0)
178
+ break;
179
+ picked.push(line);
180
+ }
181
+ const last = offset + picked.length - 1;
182
+ let body = numbered(picked, offset);
183
+ if (last < lines.length) {
184
+ body += `\n[Showing lines ${offset}–${last} of ${lines.length}. Continue with offset ${last + 1}.]`;
185
+ }
186
+ return { content: [text(body)] };
187
+ },
188
+ };
189
+ export const writeTool = {
190
+ name: "write",
191
+ description: "Create a file, or replace a whole file. An existing file must have been read in this session first, and must not have changed since. To change part of a file, use edit.",
192
+ parameters: {
193
+ type: "object",
194
+ properties: {
195
+ path: { type: "string", description: "The file to write." },
196
+ content: { type: "string", description: "The complete new content." },
197
+ confirm: CONFIRM_ARGUMENT,
198
+ },
199
+ required: ["path", "content"],
200
+ additionalProperties: false,
201
+ },
202
+ readOnly: false,
203
+ needsFolder: true,
204
+ subject(args, session) {
205
+ const path = pathArg(args, session);
206
+ return { kind: "write", tool: "write", paths: [path], summary: display(path, session) };
207
+ },
208
+ preview(args, session) {
209
+ try {
210
+ const path = pathArg(args, session);
211
+ const content = str(args, "content", true);
212
+ const before = existsSync(path) && statSync(path).isFile() ? readFileSync(path, "utf8") : "";
213
+ return { diff: previewPatch(before, content) };
214
+ }
215
+ catch {
216
+ return {};
217
+ }
218
+ },
219
+ async run(args, ctx) {
220
+ const path = pathArg(args, ctx.session);
221
+ const shown = display(path, ctx.session);
222
+ const content = str(args, "content", true);
223
+ const exists = existsSync(path);
224
+ let before = "";
225
+ if (exists) {
226
+ if (statSync(path).isDirectory())
227
+ return errorResult(`${shown} is a folder.`);
228
+ const problem = ctx.readState.check(path);
229
+ if (problem)
230
+ return errorResult(problem);
231
+ before = readFileSync(path, "utf8");
232
+ }
233
+ mkdirSync(dirname(path), { recursive: true });
234
+ writeFileSync(path, content);
235
+ ctx.readState.record(path, content);
236
+ const lines = splitLines(content).length;
237
+ return {
238
+ content: [text(`${exists ? "Replaced" : "Created"} ${shown} (${lines} lines).`)],
239
+ detail: diffDetail(shown, before, content, !exists),
240
+ };
241
+ },
242
+ };
243
+ export const editTool = {
244
+ name: "edit",
245
+ description: "Replace one piece of text in a file. old_text must match exactly one place, including indentation; add surrounding lines if it is not unique, or set replace_all to change every occurrence. The file must have been read in this session and not changed since.",
246
+ parameters: {
247
+ type: "object",
248
+ properties: {
249
+ path: { type: "string", description: "The file to change." },
250
+ old_text: { type: "string", description: "The text to replace, copied from the file." },
251
+ new_text: { type: "string", description: "The text to put in its place." },
252
+ replace_all: { type: "boolean", description: "Replace every occurrence of old_text." },
253
+ confirm: CONFIRM_ARGUMENT,
254
+ },
255
+ required: ["path", "old_text", "new_text"],
256
+ additionalProperties: false,
257
+ },
258
+ readOnly: false,
259
+ needsFolder: true,
260
+ subject(args, session) {
261
+ const path = pathArg(args, session);
262
+ return { kind: "write", tool: "edit", paths: [path], summary: display(path, session) };
263
+ },
264
+ preview(args, session) {
265
+ try {
266
+ const path = pathArg(args, session);
267
+ const oldText = str(args, "old_text", true);
268
+ const newText = str(args, "new_text", true);
269
+ if (!oldText || oldText === newText)
270
+ return {};
271
+ const body = readFileSync(path, "utf8").replace(/^\uFEFF/, "");
272
+ const replaceAll = bool(args, "replace_all") ?? false;
273
+ const result = replaceText(body, oldText, newText, replaceAll, display(path, session));
274
+ return result.ok ? { diff: previewPatch(body, result.updated) } : {};
275
+ }
276
+ catch {
277
+ return {};
278
+ }
279
+ },
280
+ async run(args, ctx) {
281
+ const path = pathArg(args, ctx.session);
282
+ const shown = display(path, ctx.session);
283
+ const oldText = str(args, "old_text", true);
284
+ const newText = str(args, "new_text", true);
285
+ const replaceAll = bool(args, "replace_all") ?? false;
286
+ if (oldText.length === 0)
287
+ return errorResult("old_text is empty. Use write to create a file.");
288
+ if (oldText === newText)
289
+ return errorResult("old_text and new_text are the same.");
290
+ if (!existsSync(path))
291
+ return errorResult(`There is no file at ${shown}.`);
292
+ const problem = ctx.readState.check(path);
293
+ if (problem)
294
+ return errorResult(problem);
295
+ const raw = readFileSync(path, "utf8");
296
+ const bom = raw.startsWith("\uFEFF") ? "\uFEFF" : "";
297
+ const body = raw.slice(bom.length);
298
+ const replaced = replaceText(body, oldText, newText, replaceAll, shown);
299
+ if (!replaced.ok)
300
+ return errorResult(replaced.error);
301
+ const { updated, count, start } = replaced;
302
+ writeFileSync(path, bom + updated);
303
+ ctx.readState.record(path, bom + updated);
304
+ // Show the changed region so the model can check its work without reading the file again.
305
+ const lines = splitLines(updated);
306
+ const firstLine = body.slice(0, start).split("\n").length;
307
+ const changed = Math.max(1, newText.split("\n").length);
308
+ const from = Math.max(1, firstLine - 2);
309
+ const to = Math.min(lines.length, firstLine + changed + 1);
310
+ const region = numbered(lines.slice(from - 1, to), from);
311
+ return {
312
+ content: [
313
+ text(`Edited ${shown}: ${count === 1 ? "1 replacement" : `${count} replacements`}. Lines ${from}–${to} now read:\n${region}`),
314
+ ],
315
+ detail: diffDetail(shown, body, updated, false),
316
+ };
317
+ },
318
+ };
@@ -0,0 +1,221 @@
1
+ import { Parser } from "htmlparser2";
2
+ const SKIP = new Set([
3
+ "script",
4
+ "style",
5
+ "noscript",
6
+ "svg",
7
+ "template",
8
+ "iframe",
9
+ "canvas",
10
+ "object",
11
+ "head",
12
+ ]);
13
+ const BLOCK = new Set([
14
+ "p",
15
+ "div",
16
+ "section",
17
+ "article",
18
+ "main",
19
+ "header",
20
+ "footer",
21
+ "aside",
22
+ "nav",
23
+ "figure",
24
+ "figcaption",
25
+ "form",
26
+ "fieldset",
27
+ "details",
28
+ "summary",
29
+ "dl",
30
+ "dt",
31
+ "dd",
32
+ "address",
33
+ ]);
34
+ /**
35
+ * Converts HTML to readable Markdown with htmlparser2, without a browser engine (M1 plan, section 2).
36
+ * Scripts, styles and embedded frames are dropped; links and images keep absolute URLs.
37
+ */
38
+ export function htmlToMarkdown(html, baseUrl) {
39
+ const out = [];
40
+ let title = "";
41
+ let inTitle = false;
42
+ let skipDepth = 0;
43
+ let preDepth = 0;
44
+ const lists = [];
45
+ const links = [];
46
+ let row = null;
47
+ let cell = null;
48
+ let headerRow = false;
49
+ let tableRows = 0;
50
+ const write = (value) => {
51
+ if (cell)
52
+ cell.push(value);
53
+ else
54
+ out.push(value);
55
+ };
56
+ const newline = (count = 1) => {
57
+ if (cell) {
58
+ cell.push(" ");
59
+ return;
60
+ }
61
+ const tail = out.join("").slice(-2);
62
+ const have = tail.endsWith("\n\n") ? 2 : tail.endsWith("\n") ? 1 : 0;
63
+ if (out.length === 0)
64
+ return;
65
+ out.push("\n".repeat(Math.max(0, count - have)));
66
+ };
67
+ const absolute = (href) => {
68
+ try {
69
+ return new URL(href, baseUrl).toString();
70
+ }
71
+ catch {
72
+ return href;
73
+ }
74
+ };
75
+ const parser = new Parser({
76
+ onopentag(name, attrs) {
77
+ if (name === "title")
78
+ inTitle = true;
79
+ if (SKIP.has(name) || skipDepth > 0) {
80
+ if (SKIP.has(name))
81
+ skipDepth++;
82
+ return;
83
+ }
84
+ if (/^h[1-6]$/.test(name)) {
85
+ newline(2);
86
+ write(`${"#".repeat(Number(name[1]))} `);
87
+ }
88
+ else if (BLOCK.has(name))
89
+ newline(2);
90
+ else if (name === "br")
91
+ write(preDepth > 0 ? "\n" : " \n");
92
+ else if (name === "hr") {
93
+ newline(2);
94
+ write("---");
95
+ newline(2);
96
+ }
97
+ else if (name === "pre") {
98
+ newline(2);
99
+ write("```\n");
100
+ preDepth++;
101
+ }
102
+ else if (name === "code" && preDepth === 0)
103
+ write("`");
104
+ else if (name === "strong" || name === "b")
105
+ write("**");
106
+ else if (name === "em" || name === "i")
107
+ write("*");
108
+ else if (name === "blockquote") {
109
+ newline(2);
110
+ write("> ");
111
+ }
112
+ else if (name === "ul" || name === "ol") {
113
+ newline(1);
114
+ lists.push({ ordered: name === "ol", index: 0 });
115
+ }
116
+ else if (name === "li") {
117
+ newline(1);
118
+ const list = lists.at(-1);
119
+ const indent = " ".repeat(Math.max(0, lists.length - 1));
120
+ if (list?.ordered) {
121
+ list.index++;
122
+ write(`${indent}${list.index}. `);
123
+ }
124
+ else
125
+ write(`${indent}- `);
126
+ }
127
+ else if (name === "a") {
128
+ const href = attrs.href ?? "";
129
+ if (href && !href.startsWith("javascript:")) {
130
+ links.push({ href: absolute(href), start: out.length });
131
+ write("[");
132
+ }
133
+ }
134
+ else if (name === "img") {
135
+ const src = attrs.src;
136
+ if (src)
137
+ write(`![${attrs.alt ?? ""}](${absolute(src)})`);
138
+ }
139
+ else if (name === "table") {
140
+ newline(2);
141
+ tableRows = 0;
142
+ }
143
+ else if (name === "tr") {
144
+ row = [];
145
+ headerRow = false;
146
+ }
147
+ else if (name === "td" || name === "th") {
148
+ cell = [];
149
+ if (name === "th")
150
+ headerRow = true;
151
+ }
152
+ },
153
+ ontext(data) {
154
+ if (inTitle) {
155
+ title += data;
156
+ return;
157
+ }
158
+ if (skipDepth > 0)
159
+ return;
160
+ if (preDepth > 0)
161
+ write(data);
162
+ else
163
+ write(data.replace(/\s+/g, " "));
164
+ },
165
+ onclosetag(name) {
166
+ if (name === "title")
167
+ inTitle = false;
168
+ if (SKIP.has(name)) {
169
+ skipDepth = Math.max(0, skipDepth - 1);
170
+ return;
171
+ }
172
+ if (skipDepth > 0)
173
+ return;
174
+ if (/^h[1-6]$/.test(name) || BLOCK.has(name) || name === "blockquote")
175
+ newline(2);
176
+ else if (name === "pre") {
177
+ preDepth = Math.max(0, preDepth - 1);
178
+ write("\n```");
179
+ newline(2);
180
+ }
181
+ else if (name === "code" && preDepth === 0)
182
+ write("`");
183
+ else if (name === "strong" || name === "b")
184
+ write("**");
185
+ else if (name === "em" || name === "i")
186
+ write("*");
187
+ else if (name === "ul" || name === "ol") {
188
+ lists.pop();
189
+ newline(1);
190
+ }
191
+ else if (name === "a") {
192
+ const link = links.pop();
193
+ if (link)
194
+ write(`](${link.href})`);
195
+ }
196
+ else if ((name === "td" || name === "th") && cell && row) {
197
+ row.push(cell.join("").trim().replace(/\|/g, "\\|"));
198
+ cell = null;
199
+ }
200
+ else if (name === "tr" && row) {
201
+ if (row.length > 0) {
202
+ out.push(`| ${row.join(" | ")} |\n`);
203
+ if (tableRows === 0 && headerRow)
204
+ out.push(`|${row.map(() => " --- ").join("|")}|\n`);
205
+ tableRows++;
206
+ }
207
+ row = null;
208
+ }
209
+ else if (name === "table")
210
+ newline(2);
211
+ },
212
+ }, { decodeEntities: true, lowerCaseTags: true });
213
+ parser.write(html);
214
+ parser.end();
215
+ const markdown = out
216
+ .join("")
217
+ .replace(/[ \t]+\n/g, "\n")
218
+ .replace(/\n{3,}/g, "\n\n")
219
+ .trim();
220
+ return { title: title.replace(/\s+/g, " ").trim(), markdown };
221
+ }
@@ -0,0 +1,38 @@
1
+ import { startCommand } from "./process.js";
2
+ /** Commands a session started in the background. They keep running between turns until they end or are stopped. */
3
+ export class Jobs {
4
+ #jobs = new Map();
5
+ #next = 1;
6
+ #onFinish;
7
+ constructor(onFinish) {
8
+ this.#onFinish = onFinish;
9
+ }
10
+ start(options) {
11
+ const id = `job-${this.#next++}`;
12
+ const running = startCommand(options);
13
+ const job = {
14
+ id,
15
+ command: options.command,
16
+ running,
17
+ startedAt: Date.now(),
18
+ finished: null,
19
+ };
20
+ this.#jobs.set(id, job);
21
+ void running.done.then((result) => {
22
+ job.finished = { ...result, at: Date.now() };
23
+ this.#onFinish(job);
24
+ });
25
+ return job;
26
+ }
27
+ get(id) {
28
+ return this.#jobs.get(id);
29
+ }
30
+ list() {
31
+ return [...this.#jobs.values()];
32
+ }
33
+ stopAll() {
34
+ for (const job of this.#jobs.values())
35
+ if (!job.finished)
36
+ job.running.stop();
37
+ }
38
+ }