@thready-web/dsh-bot-plugin 0.1.0-beta.11 → 0.1.0-beta.12
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/README.md +19 -2
- package/dist/approvals.js +19 -1
- package/dist/canvas-compose.js +73 -0
- package/dist/canvas-http.js +131 -0
- package/dist/canvas-store.js +227 -6
- package/dist/canvas-tools.js +199 -30
- package/dist/card-log.js +216 -0
- package/dist/context.js +5 -3
- package/dist/deleting.js +54 -0
- package/dist/desk-ledger.js +18 -0
- package/dist/desk.js +30 -5
- package/dist/http-plugin.js +2 -1
- package/dist/http.js +200 -4
- package/dist/inbox.js +24 -2
- package/dist/index.js +65 -13
- package/dist/questions.js +10 -0
- package/dist/runtime-registry.js +20 -0
- package/dist/session-delete-job.js +77 -0
- package/dist/session-files.js +33 -1
- package/dist/session-wipe.js +204 -0
- package/dist/store.js +46 -3
- package/dist/triggers.js +8 -0
- package/dist/work-admit.js +9 -20
- package/dist/work-child.js +27 -10
- package/dist/work-ledger.js +5 -1
- package/dist/work-mock.js +10 -0
- package/dist/work-tickets.js +5 -3
- package/dist/work-tools.js +11 -9
- package/dist/work-worker.js +240 -6
- package/dist/worker-handles.js +33 -0
- package/dist/workspace-files.js +98 -17
- package/package.json +1 -1
- package/public/assets/{SourceEditor-uu0G7jsM.js → SourceEditor-BjtoMvcX.js} +3 -3
- package/public/assets/{cssMode-C5DAFF5H.js → cssMode--96PQtWh.js} +1 -1
- package/public/assets/{freemarker2-DExNwSiZ.js → freemarker2-Bz-HlISL.js} +1 -1
- package/public/assets/{handlebars-BK7YAvpQ.js → handlebars-D9ScJ8AM.js} +1 -1
- package/public/assets/{html-CdDm_zae.js → html-Czc0jojJ.js} +1 -1
- package/public/assets/{htmlMode-CLdn9oa1.js → htmlMode-CCww5Z1w.js} +1 -1
- package/public/assets/index-DRCgKxWk.css +1 -0
- package/public/assets/index-ztUdW074.js +72 -0
- package/public/assets/{javascript-mLl83_3W.js → javascript-B-yRZSiz.js} +1 -1
- package/public/assets/{jsonMode-B1-bihFd.js → jsonMode-ASWsY833.js} +1 -1
- package/public/assets/{liquid-Cv8m0HUb.js → liquid-BGoox2vJ.js} +1 -1
- package/public/assets/{mdx-Q12b91rL.js → mdx-DHgvnl_U.js} +1 -1
- package/public/assets/{python-Kda811W0.js → python-BJvVD30_.js} +1 -1
- package/public/assets/{razor-CRuCpdOW.js → razor-0PwR35mu.js} +1 -1
- package/public/assets/{tsMode-BGHcz-M3.js → tsMode-3ETp8Gdh.js} +1 -1
- package/public/assets/{typescript-Db1e0DCT.js → typescript-BT0iTK6K.js} +1 -1
- package/public/assets/{xml-H3zzFmnd.js → xml-BTaNc_uT.js} +1 -1
- package/public/assets/{yaml-BQrKvmTF.js → yaml-Dr7LW3tK.js} +1 -1
- package/public/index.html +2 -2
- package/skills/ui/ui-design/SKILL.md +11 -5
- package/public/assets/index-BB-QDSf2.css +0 -1
- package/public/assets/index-CsX-_qbo.js +0 -62
package/README.md
CHANGED
|
@@ -83,7 +83,7 @@ Parked `needs_user` tickets still count toward the combined unfinished cap (`slo
|
|
|
83
83
|
|
|
84
84
|
Workers share the owner's workspace. There is no filesystem isolation; briefings should state file boundaries, or dependent work should stay serial.
|
|
85
85
|
|
|
86
|
-
Workers use the `foundations-files` preset: official file tools plus official `@deepseek-ai/dsh-tool-bash` and `@deepseek-ai/dsh-tool-jobs`. The host sandbox stays `workspace-write` with the existing approval policy. This does not mount the web preset, add writable roots, or switch the sandbox to `danger-full-access`. A reserved start that does not create a worker within `min(ticket wallMs, 10m)` fails with `reserve-wall` and frees the slot; a late `createChild` is cancelled instead of attached.
|
|
86
|
+
Workers use the `foundations-files` preset: official file tools plus official `@deepseek-ai/dsh-tool-bash` and `@deepseek-ai/dsh-tool-jobs`. A canvas worker uses `foundations-canvas` instead, which mounts the file tools with `write` and `edit` disabled and does not mount bash or jobs. Preview bytes go through `canvas_write`, not the workspace. The host sandbox stays `workspace-write` with the existing approval policy. This does not mount the web preset, add writable roots, or switch the sandbox to `danger-full-access`. A reserved start that does not create a worker within `min(ticket wallMs, 10m)` fails with `reserve-wall` and frees the slot; a late `createChild` is cancelled instead of attached.
|
|
87
87
|
|
|
88
88
|
Runnable examples (any one of these is enough; `config.work` wins over the environment):
|
|
89
89
|
|
|
@@ -117,7 +117,7 @@ A granted worker token/runtime cap pauses admission at the official asynchronous
|
|
|
117
117
|
|
|
118
118
|
The ticket retains its attempt, fencing token and child session. `budget_json.quota` durably records cumulative committed `usage.totalTokens`, deduplication sequence, active elapsed time, and requested/waiting phase. Active time includes finishing in-flight work until the safe boundary; suspended-boundary waiting time is excluded. Waiting is exempt from lease/enqueue expiry and has no hard-wall failure timer. It retains a worker slot to preserve capacity accounting; the main bot is not in this worker gate or slot pool and can always grant, deny or cancel. Explicit cancellation releases the slot normally.
|
|
119
119
|
|
|
120
|
-
A grant adds amounts, never resets usage, and must exceed both current token usage and active time. An insufficient grant leaves the gate closed without another noisy extension event; its response includes usage, required totals, and the minimum additional amounts. One slice is
|
|
120
|
+
A grant adds amounts, never resets usage, and must exceed both current token usage and active time. An insufficient grant leaves the gate closed without another noisy extension event; its response includes usage, required totals, and the minimum additional amounts. One slice is 2,000,000 tokens and 60 minutes. Every dispatch starts on that slice. A budget passed with the dispatch is ignored. Every later grant adds at least another slice. There is no cumulative ceiling and no limit on how many times a ticket can be extended. A refusal from the main session is ignored: another full slice is granted instead.
|
|
121
121
|
|
|
122
122
|
Budget requests are urgent next-turn inbox notices, bypassing stale notice/user/delivery admission barriers without steering or interrupting the main turn. Admission is single-flight per owner and rechecks grant/cancellation races before persisting a notice. Ordinary progress retains its existing admission policy. The workbench labels suspended workers “Waiting for budget”.
|
|
123
123
|
|
|
@@ -127,6 +127,23 @@ A process restart cannot preserve an in-memory DSH turn: the durable wait surviv
|
|
|
127
127
|
|
|
128
128
|
Workbench sessions start in `THREADY_SESSION_CWD` when set, otherwise in `<DSH_HOME>/workspace` (created on first use), never in `DSH_HOME` itself, which holds the owner token and profiles.
|
|
129
129
|
|
|
130
|
+
The file drawer's Home root is a directory you choose, separate from process `HOME`. Set an existing directory with the http plugin's `files.home` or with `THREADY_FILES_HOME` (config wins). Leave `HOME` and `DSH_HOME` unchanged. The home directory is rejected when it is missing, not a directory, `DSH_HOME`, anything inside `DSH_HOME`, `/`, or `/etc`. Symlinks are judged by their real path. An invalid explicit value is not replaced with process `HOME`. When neither value is set, process `HOME` is used only if it passes those checks.
|
|
131
|
+
|
|
132
|
+
A workspace that is a subdirectory of `DSH_HOME` stays readable. A workspace that is `DSH_HOME`, or that contains it, stays closed. The one exception is a session whose stored cwd is exactly `DSH_HOME`: the drawer shows the configured session workspace instead, and only when that path is itself a safe directory. The stored cwd, session id, and history are not rewritten. Any other overlapping cwd stays closed.
|
|
133
|
+
|
|
134
|
+
```yaml
|
|
135
|
+
- insert:
|
|
136
|
+
- id: session-foundations-http
|
|
137
|
+
name: "@thready-web/dsh-bot-plugin/http"
|
|
138
|
+
config:
|
|
139
|
+
files:
|
|
140
|
+
home: /opt/dsh-bot/files
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
```bash
|
|
144
|
+
THREADY_FILES_HOME=/opt/dsh-bot/files
|
|
145
|
+
```
|
|
146
|
+
|
|
130
147
|
Generated raster previews can be embedded at `/dsh-bot/<project>/preview/<image>` or `/dsh-bot/<project>/artifacts/<image>`. These are deliberate artifact directories under that configured workspace, not a general file server or a per-session sharing boundary: authenticated workbench users can view their images. Only JPEG, PNG, GIF and WebP with matching signatures are served, with private/no-store caching. Dotfiles, traversal, symlink escapes, other workspace locations and active HTML/SVG are rejected. With no configured workspace this fallback is disabled. Relative Markdown image paths resolve under `/dsh-bot/`; existing absolute URLs remain unchanged.
|
|
131
148
|
|
|
132
149
|
Markdown images use bounded, aspect-preserving thumbnails. Click to open a viewport-fit dialog, then choose Original size to scroll through screenshot details. Escape, the close button or the backdrop dismisses it, restoring focus and body scrolling. Linked images expose their link separately from the preview control. Preview and unavailable-state labels follow the English/Chinese UI catalog.
|
package/dist/approvals.js
CHANGED
|
@@ -42,7 +42,7 @@ function displayReasonOf(value) {
|
|
|
42
42
|
}
|
|
43
43
|
return display;
|
|
44
44
|
}
|
|
45
|
-
export function createApprovalAnswerer() {
|
|
45
|
+
export function createApprovalAnswerer(record) {
|
|
46
46
|
const pending = new Set();
|
|
47
47
|
const settled = [];
|
|
48
48
|
let nextId = 1;
|
|
@@ -91,6 +91,13 @@ export function createApprovalAnswerer() {
|
|
|
91
91
|
};
|
|
92
92
|
entry.onAbort = onAbort;
|
|
93
93
|
pending.add(entry);
|
|
94
|
+
record?.({
|
|
95
|
+
sessionId,
|
|
96
|
+
requestId: entry.requestId,
|
|
97
|
+
toolName,
|
|
98
|
+
reason: textOf(request.reason),
|
|
99
|
+
...(callId === '' ? {} : { callId }),
|
|
100
|
+
});
|
|
94
101
|
if (request.signal?.aborted) {
|
|
95
102
|
onAbort();
|
|
96
103
|
return;
|
|
@@ -120,6 +127,16 @@ export function createApprovalAnswerer() {
|
|
|
120
127
|
entry.resolve(input.outcome);
|
|
121
128
|
return { ok: true, path: 'approval', outcome: input.outcome, requestId: entry.requestId };
|
|
122
129
|
}
|
|
130
|
+
function abandonSession(sessionId) {
|
|
131
|
+
for (const entry of [...pending]) {
|
|
132
|
+
if (entry.sessionId !== sessionId)
|
|
133
|
+
continue;
|
|
134
|
+
if (!detach(entry))
|
|
135
|
+
continue;
|
|
136
|
+
remember(entry, 'unavailable');
|
|
137
|
+
entry.resolve('unavailable');
|
|
138
|
+
}
|
|
139
|
+
}
|
|
123
140
|
function snapshot(sessionId) {
|
|
124
141
|
const open = [...pending]
|
|
125
142
|
.filter(entry => !sessionId || entry.sessionId === sessionId)
|
|
@@ -152,6 +169,7 @@ export function createApprovalAnswerer() {
|
|
|
152
169
|
listen,
|
|
153
170
|
handle,
|
|
154
171
|
submit,
|
|
172
|
+
abandonSession,
|
|
155
173
|
snapshot,
|
|
156
174
|
pendingCount() {
|
|
157
175
|
return pending.size;
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { parseHTML } from 'linkedom';
|
|
2
|
+
const DROP = new Set(['script', 'iframe', 'object', 'embed', 'base', 'meta', 'svg', 'foreignobject']);
|
|
3
|
+
function isRelativeAsset(href) {
|
|
4
|
+
if (href === '' || href.length > 200)
|
|
5
|
+
return false;
|
|
6
|
+
if (/[\s\\]/.test(href) || href.includes('..'))
|
|
7
|
+
return false;
|
|
8
|
+
if (/^[a-z][a-z0-9+.-]*:/i.test(href) || href.startsWith('//') || href.startsWith('/'))
|
|
9
|
+
return false;
|
|
10
|
+
return true;
|
|
11
|
+
}
|
|
12
|
+
function escapeAttr(value) {
|
|
13
|
+
return value.replace(/&/g, '&').replace(/"/g, '"').replace(/</g, '<');
|
|
14
|
+
}
|
|
15
|
+
/** Keep a head <style> from closing itself and becoming markup. */
|
|
16
|
+
function escapeStyle(css) {
|
|
17
|
+
return css.replace(/<\/(style)/gi, '<\\/$1');
|
|
18
|
+
}
|
|
19
|
+
function scrub(el) {
|
|
20
|
+
for (const child of Array.from(el.children)) {
|
|
21
|
+
const tag = child.tagName.toLowerCase();
|
|
22
|
+
if (DROP.has(tag)) {
|
|
23
|
+
child.remove();
|
|
24
|
+
continue;
|
|
25
|
+
}
|
|
26
|
+
for (const attr of Array.from(child.attributes)) {
|
|
27
|
+
const name = attr.name.toLowerCase();
|
|
28
|
+
const value = attr.value.trim();
|
|
29
|
+
if (name.startsWith('on') || name === 'srcdoc')
|
|
30
|
+
child.removeAttribute(attr.name);
|
|
31
|
+
else if ((name === 'href' || name === 'src' || name === 'action' || name === 'formaction') &&
|
|
32
|
+
/^(javascript:|data:|https?:|\/\/)/i.test(value))
|
|
33
|
+
child.removeAttribute(attr.name);
|
|
34
|
+
}
|
|
35
|
+
scrub(child);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
/** Scriptless design plus the host inspector. CSP stays with the caller. */
|
|
39
|
+
export function composeInspect(html, options) {
|
|
40
|
+
const { document } = parseHTML(html);
|
|
41
|
+
const sheets = [];
|
|
42
|
+
for (const node of Array.from(document.querySelectorAll('link, style'))) {
|
|
43
|
+
if (document.body.contains(node))
|
|
44
|
+
continue;
|
|
45
|
+
if (node.tagName === 'STYLE') {
|
|
46
|
+
const media = node.getAttribute('media');
|
|
47
|
+
const mediaAttr = media && media.length <= 200 && !/[\r\n<>]/.test(media) ? ` media="${escapeAttr(media)}"` : '';
|
|
48
|
+
sheets.push(`<style${mediaAttr}>${escapeStyle(node.textContent ?? '')}</style>`);
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
const rel = (node.getAttribute('rel') ?? '').toLowerCase();
|
|
52
|
+
const href = (node.getAttribute('href') ?? '').trim();
|
|
53
|
+
if (!rel.split(/\s+/).includes('stylesheet') || !isRelativeAsset(href))
|
|
54
|
+
continue;
|
|
55
|
+
sheets.push(`<link rel="stylesheet" href="${escapeAttr(href)}">`);
|
|
56
|
+
}
|
|
57
|
+
scrub(document.body);
|
|
58
|
+
return `<!doctype html>
|
|
59
|
+
<html>
|
|
60
|
+
<head>
|
|
61
|
+
<meta charset="utf-8">
|
|
62
|
+
<base href="${escapeAttr(options.baseHref)}">
|
|
63
|
+
<meta name="dsh-inspect-nonce" content="${escapeAttr(options.nonce)}">
|
|
64
|
+
<meta name="dsh-parent-origin" content="${escapeAttr(options.parentOrigin)}">
|
|
65
|
+
${options.page ? `<meta name="dsh-inspect-page" content="${escapeAttr(options.page)}">` : ''}
|
|
66
|
+
${sheets.join('\n')}
|
|
67
|
+
</head>
|
|
68
|
+
<body>
|
|
69
|
+
${document.body.innerHTML}
|
|
70
|
+
<script src="${escapeAttr(options.inspectorSrc)}"></script>
|
|
71
|
+
</body>
|
|
72
|
+
</html>`;
|
|
73
|
+
}
|
package/dist/canvas-http.js
CHANGED
|
@@ -1,7 +1,13 @@
|
|
|
1
|
+
import { existsSync } from 'node:fs';
|
|
2
|
+
import { buildSync } from 'esbuild';
|
|
3
|
+
import { fileURLToPath } from 'node:url';
|
|
4
|
+
import { composeInspect } from "./canvas-compose.js";
|
|
1
5
|
import { asStoreFailure, toolError } from "./errors.js";
|
|
2
6
|
import { CANVAS_MAX_JSON_BYTES, normalizeCanvasPath } from "./canvas-store.js";
|
|
3
7
|
const API = '/session-foundations/api/canvas';
|
|
4
8
|
const MEDIA = '/dsh-bot/canvas-media/';
|
|
9
|
+
const INSPECT = '/dsh-bot/canvas-inspect/';
|
|
10
|
+
const INSPECTOR = '/dsh-bot/canvas-inspector.js';
|
|
5
11
|
const CANVAS_ID = /^cnv_[A-Za-z0-9_-]{16}$/;
|
|
6
12
|
/**
|
|
7
13
|
* Manual and test publish routes. They require the existing login gate.
|
|
@@ -246,3 +252,128 @@ function json(res, status, body) {
|
|
|
246
252
|
});
|
|
247
253
|
res.end(text);
|
|
248
254
|
}
|
|
255
|
+
const NONCE = /^[A-Za-z0-9_-]{16,80}$/;
|
|
256
|
+
let inspectorCode = null;
|
|
257
|
+
/** Token-only inspect document. The design stays scriptless; only the host inspector runs. */
|
|
258
|
+
export async function serveCanvasInspect(req, res, requestUrl, canvas) {
|
|
259
|
+
if (req.method !== 'GET' && req.method !== 'HEAD') {
|
|
260
|
+
json(res, 405, toolError('VALIDATION', 'canvas inspect is read-only.'));
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
const path = (requestUrl.split('?')[0] ?? '').replace(/\/+$/, '');
|
|
264
|
+
if (path === INSPECTOR) {
|
|
265
|
+
try {
|
|
266
|
+
const code = inspectorSource();
|
|
267
|
+
writeBody(res, req.method, 200, 'text/javascript; charset=utf-8', code, "default-src 'none'");
|
|
268
|
+
}
|
|
269
|
+
catch (error) {
|
|
270
|
+
json(res, 500, toolError('STORE_FAILURE', error instanceof Error ? error.message : 'inspector failed'));
|
|
271
|
+
}
|
|
272
|
+
return;
|
|
273
|
+
}
|
|
274
|
+
const parsed = parseCanvasInspect(requestUrl);
|
|
275
|
+
if (!parsed) {
|
|
276
|
+
json(res, 400, toolError('VALIDATION', 'canvas inspect path is not allowed.'));
|
|
277
|
+
return;
|
|
278
|
+
}
|
|
279
|
+
const nonce = new URL(requestUrl, 'http://127.0.0.1').searchParams.get('nonce');
|
|
280
|
+
if (!nonce || !NONCE.test(nonce)) {
|
|
281
|
+
json(res, 400, toolError('VALIDATION', 'canvas inspect nonce is required.'));
|
|
282
|
+
return;
|
|
283
|
+
}
|
|
284
|
+
if (!canvas) {
|
|
285
|
+
json(res, 404, toolError('NOT_FOUND', 'No such canvas file.'));
|
|
286
|
+
return;
|
|
287
|
+
}
|
|
288
|
+
const found = canvas.readMedia({ token: parsed.token, revision: parsed.revision, path: parsed.path });
|
|
289
|
+
if (found.ok === false) {
|
|
290
|
+
json(res, found.code === 'NOT_FOUND' ? 404 : found.code === 'VALIDATION' ? 400 : 401, found);
|
|
291
|
+
return;
|
|
292
|
+
}
|
|
293
|
+
if (found.type !== 'text/html') {
|
|
294
|
+
json(res, 404, toolError('NOT_FOUND', 'No such canvas file.'));
|
|
295
|
+
return;
|
|
296
|
+
}
|
|
297
|
+
const origin = requestOrigin(req);
|
|
298
|
+
if (origin === '') {
|
|
299
|
+
json(res, 400, toolError('VALIDATION', 'canvas inspect needs a host.'));
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
302
|
+
const prefix = `${origin}${MEDIA}${parsed.token}/${parsed.revision}/`;
|
|
303
|
+
const html = composeInspect(found.bytes.toString('utf8'), {
|
|
304
|
+
baseHref: prefix,
|
|
305
|
+
nonce,
|
|
306
|
+
parentOrigin: origin,
|
|
307
|
+
inspectorSrc: INSPECTOR,
|
|
308
|
+
page: parsed.path,
|
|
309
|
+
});
|
|
310
|
+
writeBody(res, req.method, 200, 'text/html; charset=utf-8', html, inspectPolicy(origin, prefix));
|
|
311
|
+
}
|
|
312
|
+
export function parseCanvasInspect(requestUrl) {
|
|
313
|
+
const pathname = (requestUrl.split('?')[0] ?? '').replace(/\/+$/, '');
|
|
314
|
+
if (!pathname.startsWith(INSPECT))
|
|
315
|
+
return null;
|
|
316
|
+
if (pathname.includes('\\') || pathname.includes('%') || pathname.includes('\0'))
|
|
317
|
+
return null;
|
|
318
|
+
if (pathname.split('/').includes('..') || pathname.split('/').includes('.'))
|
|
319
|
+
return null;
|
|
320
|
+
const parts = pathname.slice(INSPECT.length).split('/');
|
|
321
|
+
if (parts.length < 2)
|
|
322
|
+
return null;
|
|
323
|
+
const [token, revision, ...rest] = parts;
|
|
324
|
+
if (!token || !/^[A-Za-z0-9_-]{20,128}$/.test(token))
|
|
325
|
+
return null;
|
|
326
|
+
if (!revision || !/^[1-9]\d{0,8}$/.test(revision))
|
|
327
|
+
return null;
|
|
328
|
+
const path = normalizeCanvasPath(rest.length === 0 ? 'index.html' : rest.join('/'));
|
|
329
|
+
if (!path || !path.endsWith('.html'))
|
|
330
|
+
return null;
|
|
331
|
+
return { token, revision: Number(revision), path };
|
|
332
|
+
}
|
|
333
|
+
function inspectPolicy(origin, prefix) {
|
|
334
|
+
return [
|
|
335
|
+
"default-src 'none'",
|
|
336
|
+
`img-src ${prefix}`,
|
|
337
|
+
`style-src ${prefix} 'unsafe-inline'`,
|
|
338
|
+
`font-src ${prefix}`,
|
|
339
|
+
`script-src ${origin}${INSPECTOR}`,
|
|
340
|
+
`base-uri ${prefix}`,
|
|
341
|
+
`frame-ancestors ${origin}`,
|
|
342
|
+
"form-action 'none'",
|
|
343
|
+
'sandbox allow-scripts',
|
|
344
|
+
].join('; ');
|
|
345
|
+
}
|
|
346
|
+
function writeBody(res, method, status, type, body, policy) {
|
|
347
|
+
res.writeHead(status, {
|
|
348
|
+
'content-type': type,
|
|
349
|
+
'content-length': Buffer.byteLength(body),
|
|
350
|
+
'cache-control': 'private, no-store',
|
|
351
|
+
'x-content-type-options': 'nosniff',
|
|
352
|
+
'referrer-policy': 'no-referrer',
|
|
353
|
+
'content-security-policy': policy,
|
|
354
|
+
});
|
|
355
|
+
res.end(method === 'HEAD' ? undefined : body);
|
|
356
|
+
}
|
|
357
|
+
function inspectorSource() {
|
|
358
|
+
if (inspectorCode != null)
|
|
359
|
+
return inspectorCode;
|
|
360
|
+
// Same relative path from src/ (tests) and dist/ (built server): both sit next to ui/.
|
|
361
|
+
const entry = fileURLToPath(new URL('../ui/src/canvas-inspector.ts', import.meta.url));
|
|
362
|
+
if (!existsSync(entry))
|
|
363
|
+
throw new Error('canvas inspector source is missing');
|
|
364
|
+
const result = buildSync({
|
|
365
|
+
absWorkingDir: fileURLToPath(new URL('../ui/', import.meta.url)),
|
|
366
|
+
entryPoints: [entry],
|
|
367
|
+
bundle: true,
|
|
368
|
+
format: 'iife',
|
|
369
|
+
platform: 'browser',
|
|
370
|
+
target: 'es2022',
|
|
371
|
+
write: false,
|
|
372
|
+
minify: false,
|
|
373
|
+
logLevel: 'silent',
|
|
374
|
+
});
|
|
375
|
+
inspectorCode = result.outputFiles[0]?.text ?? '';
|
|
376
|
+
if (inspectorCode === '')
|
|
377
|
+
throw new Error('inspector bundle was empty');
|
|
378
|
+
return inspectorCode;
|
|
379
|
+
}
|
package/dist/canvas-store.js
CHANGED
|
@@ -89,6 +89,12 @@ CREATE TABLE IF NOT EXISTS canvas_drafts (
|
|
|
89
89
|
updated_at TEXT NOT NULL,
|
|
90
90
|
PRIMARY KEY (canvas_id, path)
|
|
91
91
|
);
|
|
92
|
+
|
|
93
|
+
CREATE TABLE IF NOT EXISTS canvas_ticket_publishes (
|
|
94
|
+
ticket_id TEXT PRIMARY KEY,
|
|
95
|
+
canvas_id TEXT NOT NULL,
|
|
96
|
+
revision INTEGER NOT NULL
|
|
97
|
+
);
|
|
92
98
|
`;
|
|
93
99
|
/**
|
|
94
100
|
* Relative canvas path. Rejects traversal, absolute paths, encoding, and dotfiles.
|
|
@@ -155,6 +161,15 @@ function activeContent(type, bytes) {
|
|
|
155
161
|
return 'Canvas stylesheets cannot use @import.';
|
|
156
162
|
return null;
|
|
157
163
|
}
|
|
164
|
+
function shorterThanCurrent(file, previous) {
|
|
165
|
+
if (file.type.toLowerCase() !== 'text/html')
|
|
166
|
+
return null;
|
|
167
|
+
if (previous == null || previous < 1)
|
|
168
|
+
return null;
|
|
169
|
+
if (file.bytes.length * 2 >= previous)
|
|
170
|
+
return null;
|
|
171
|
+
return `${file.path} is much shorter than the current revision (${file.bytes.length} bytes vs ${previous}).`;
|
|
172
|
+
}
|
|
158
173
|
function stageProblem(file) {
|
|
159
174
|
const path = normalizeCanvasPath(file?.path);
|
|
160
175
|
if (!path)
|
|
@@ -169,7 +184,12 @@ function stageProblem(file) {
|
|
|
169
184
|
return `${path} exceeds the 1.5 MiB file cap.`;
|
|
170
185
|
if (!rasterOk(type, file.bytes))
|
|
171
186
|
return `${path} does not match its image type.`;
|
|
172
|
-
|
|
187
|
+
const active = activeContent(type, file.bytes);
|
|
188
|
+
if (active)
|
|
189
|
+
return active;
|
|
190
|
+
if (type === 'text/html' && !/<\/html\s*>/i.test(file.bytes.toString('utf8')))
|
|
191
|
+
return `${path} is incomplete HTML. It needs a closing </html> tag.`;
|
|
192
|
+
return null;
|
|
173
193
|
}
|
|
174
194
|
function checkFiles(entry, files) {
|
|
175
195
|
const entryPath = normalizeCanvasPath(entry);
|
|
@@ -209,6 +229,9 @@ function migrateCanvas(db) {
|
|
|
209
229
|
if (!columns.some(column => column.name === 'idempotency_key')) {
|
|
210
230
|
db.exec('ALTER TABLE canvases ADD COLUMN idempotency_key TEXT');
|
|
211
231
|
}
|
|
232
|
+
if (!columns.some(column => column.name === 'writer_ticket_id')) {
|
|
233
|
+
db.exec('ALTER TABLE canvases ADD COLUMN writer_ticket_id TEXT');
|
|
234
|
+
}
|
|
212
235
|
db.exec('CREATE UNIQUE INDEX IF NOT EXISTS canvas_idempotency ON canvases(session_id, idempotency_key) WHERE idempotency_key IS NOT NULL');
|
|
213
236
|
}
|
|
214
237
|
/**
|
|
@@ -238,14 +261,58 @@ export class CanvasStore {
|
|
|
238
261
|
db;
|
|
239
262
|
tails;
|
|
240
263
|
notices;
|
|
264
|
+
publishListeners;
|
|
241
265
|
constructor(db) {
|
|
242
266
|
this.db = db;
|
|
243
267
|
this.tails = new Map();
|
|
244
268
|
this.notices = new Map();
|
|
269
|
+
this.publishListeners = new Set();
|
|
270
|
+
}
|
|
271
|
+
/** Fired after a preview revision commits. A listener must not fail the publish. */
|
|
272
|
+
onPublished(listener) {
|
|
273
|
+
this.publishListeners.add(listener);
|
|
274
|
+
return () => {
|
|
275
|
+
this.publishListeners.delete(listener);
|
|
276
|
+
};
|
|
245
277
|
}
|
|
246
278
|
close() {
|
|
247
279
|
this.db.close();
|
|
248
280
|
}
|
|
281
|
+
/** Drop canvases owned by these sessions, then blobs no remaining file uses. */
|
|
282
|
+
deleteSessions(sessionIds) {
|
|
283
|
+
const ids = [...new Set(sessionIds.filter(id => id !== ''))];
|
|
284
|
+
if (ids.length === 0)
|
|
285
|
+
return;
|
|
286
|
+
const marks = ids.map(() => '?').join(', ');
|
|
287
|
+
let canvasIds = [];
|
|
288
|
+
this.db.exec('BEGIN IMMEDIATE');
|
|
289
|
+
try {
|
|
290
|
+
canvasIds = this.db.prepare(`SELECT id FROM canvases WHERE session_id IN (${marks})`).all(...ids).map(row => row.id);
|
|
291
|
+
if (canvasIds.length > 0) {
|
|
292
|
+
const canvasMarks = canvasIds.map(() => '?').join(', ');
|
|
293
|
+
this.db.prepare(`DELETE FROM canvas_files WHERE canvas_id IN (${canvasMarks})`).run(...canvasIds);
|
|
294
|
+
this.db.prepare(`DELETE FROM canvas_drafts WHERE canvas_id IN (${canvasMarks})`).run(...canvasIds);
|
|
295
|
+
this.db.prepare(`DELETE FROM canvas_tokens WHERE canvas_id IN (${canvasMarks})`).run(...canvasIds);
|
|
296
|
+
this.db.prepare(`DELETE FROM canvas_revisions WHERE canvas_id IN (${canvasMarks})`).run(...canvasIds);
|
|
297
|
+
this.db.prepare(`DELETE FROM canvases WHERE id IN (${canvasMarks})`).run(...canvasIds);
|
|
298
|
+
}
|
|
299
|
+
this.db.prepare('DELETE FROM canvas_blobs WHERE hash NOT IN (SELECT hash FROM canvas_files)').run();
|
|
300
|
+
this.db.exec('COMMIT');
|
|
301
|
+
}
|
|
302
|
+
catch (error) {
|
|
303
|
+
try {
|
|
304
|
+
this.db.exec('ROLLBACK');
|
|
305
|
+
}
|
|
306
|
+
catch {
|
|
307
|
+
/* already rolled back */
|
|
308
|
+
}
|
|
309
|
+
throw error;
|
|
310
|
+
}
|
|
311
|
+
for (const [id, notice] of this.notices) {
|
|
312
|
+
if (ids.includes(notice.sessionId) || canvasIds.includes(id))
|
|
313
|
+
this.notices.delete(id);
|
|
314
|
+
}
|
|
315
|
+
}
|
|
249
316
|
/** Latest renderable revision per canvas, so a restarted process can still notify. */
|
|
250
317
|
seed() {
|
|
251
318
|
const rows = this.db
|
|
@@ -338,14 +405,29 @@ export class CanvasStore {
|
|
|
338
405
|
canvasIdForTicket(ticketId) {
|
|
339
406
|
if (!ticketId || ticketId.length > 80)
|
|
340
407
|
return null;
|
|
341
|
-
const row = this.db
|
|
408
|
+
const row = this.db
|
|
409
|
+
.prepare('SELECT id FROM canvases WHERE ticket_id = ? OR writer_ticket_id = ?')
|
|
410
|
+
.get(ticketId, ticketId);
|
|
342
411
|
return typeof row?.id === 'string' ? row.id : null;
|
|
343
412
|
}
|
|
413
|
+
/** The session that opened this preview. A follow-up ticket does not move the card. */
|
|
414
|
+
ownedBy(sessionId, canvasId) {
|
|
415
|
+
if (!CANVAS_ID.test(canvasId))
|
|
416
|
+
return false;
|
|
417
|
+
const row = this.db.prepare('SELECT session_id FROM canvases WHERE id = ?').get(canvasId);
|
|
418
|
+
return row?.session_id === sessionId;
|
|
419
|
+
}
|
|
420
|
+
attachWriter(canvasId, ticketId) {
|
|
421
|
+
if (!CANVAS_ID.test(canvasId) || ticketId.length < 1 || ticketId.length > 80)
|
|
422
|
+
return;
|
|
423
|
+
this.db.prepare('UPDATE canvases SET writer_ticket_id = ? WHERE id = ?').run(ticketId, canvasId);
|
|
424
|
+
}
|
|
344
425
|
writeDraft(canvasId, files, remove = []) {
|
|
345
426
|
if (!CANVAS_ID.test(canvasId))
|
|
346
427
|
return Promise.resolve(toolError('NOT_FOUND', 'No such canvas.'));
|
|
347
428
|
return this.locked(canvasId, () => {
|
|
348
|
-
|
|
429
|
+
const canvas = this.rawCanvas(canvasId);
|
|
430
|
+
if (!canvas)
|
|
349
431
|
return toolError('NOT_FOUND', 'No such canvas.');
|
|
350
432
|
const staged = [];
|
|
351
433
|
const seen = new Set();
|
|
@@ -357,6 +439,10 @@ export class CanvasStore {
|
|
|
357
439
|
if (!path || seen.has(path))
|
|
358
440
|
return toolError('VALIDATION', `Duplicate canvas path ${path ?? ''}.`);
|
|
359
441
|
seen.add(path);
|
|
442
|
+
const previous = this.fileSizeAt(canvasId, canvas.preview_revision == null ? null : Number(canvas.preview_revision), path);
|
|
443
|
+
const short = shorterThanCurrent({ path, type: file.type, bytes: file.bytes }, previous);
|
|
444
|
+
if (short)
|
|
445
|
+
return toolError('VALIDATION', short);
|
|
360
446
|
staged.push({ path, type: file.type.toLowerCase(), bytes: file.bytes });
|
|
361
447
|
}
|
|
362
448
|
const dropping = [];
|
|
@@ -398,7 +484,7 @@ export class CanvasStore {
|
|
|
398
484
|
return { ok: true, files: [...next.keys()].sort() };
|
|
399
485
|
});
|
|
400
486
|
}
|
|
401
|
-
publishDraft(canvasId) {
|
|
487
|
+
publishDraft(canvasId, ticketId) {
|
|
402
488
|
if (!CANVAS_ID.test(canvasId))
|
|
403
489
|
return Promise.resolve(toolError('NOT_FOUND', 'No such canvas.'));
|
|
404
490
|
return this.locked(canvasId, () => {
|
|
@@ -406,6 +492,15 @@ export class CanvasStore {
|
|
|
406
492
|
const canvas = this.rawCanvas(canvasId);
|
|
407
493
|
if (!canvas)
|
|
408
494
|
return toolError('NOT_FOUND', 'No such canvas.');
|
|
495
|
+
if (typeof ticketId === 'string' && ticketId !== '') {
|
|
496
|
+
if (ticketId.length > 80)
|
|
497
|
+
return toolError('VALIDATION', 'ticket id is not allowed.');
|
|
498
|
+
const prior = this.db
|
|
499
|
+
.prepare('SELECT ticket_id FROM canvas_ticket_publishes WHERE ticket_id = ?')
|
|
500
|
+
.get(ticketId);
|
|
501
|
+
if (prior)
|
|
502
|
+
return toolError('VALIDATION', 'This ticket already published. Call canvas_publish once, after every file is staged.');
|
|
503
|
+
}
|
|
409
504
|
const rows = this.db
|
|
410
505
|
.prepare('SELECT path, media_type, bytes FROM canvas_drafts WHERE canvas_id = ? ORDER BY path ASC')
|
|
411
506
|
.all(canvasId);
|
|
@@ -415,7 +510,7 @@ export class CanvasStore {
|
|
|
415
510
|
bytes: Buffer.from(row.bytes),
|
|
416
511
|
}));
|
|
417
512
|
const preview = canvas.preview_revision == null ? null : Number(canvas.preview_revision);
|
|
418
|
-
|
|
513
|
+
const published = this.publishLocked({
|
|
419
514
|
canvasId,
|
|
420
515
|
kind: preview == null ? 'snapshot' : 'delta',
|
|
421
516
|
baseRevision: preview,
|
|
@@ -423,6 +518,13 @@ export class CanvasStore {
|
|
|
423
518
|
expectedFence: Number(canvas.fence_generation),
|
|
424
519
|
files,
|
|
425
520
|
});
|
|
521
|
+
if (published.ok === true && typeof ticketId === 'string' && ticketId !== '') {
|
|
522
|
+
this.db
|
|
523
|
+
.prepare('INSERT INTO canvas_ticket_publishes (ticket_id, canvas_id, revision) VALUES (?, ?, ?)')
|
|
524
|
+
.run(ticketId, canvasId, published.revision);
|
|
525
|
+
}
|
|
526
|
+
const kind = preview == null ? 'snapshot' : 'delta';
|
|
527
|
+
return this.emitPublished(published, kind);
|
|
426
528
|
}
|
|
427
529
|
catch (error) {
|
|
428
530
|
return asStoreFailure(error);
|
|
@@ -475,7 +577,7 @@ export class CanvasStore {
|
|
|
475
577
|
publish(input) {
|
|
476
578
|
return this.locked(input.canvasId, () => {
|
|
477
579
|
try {
|
|
478
|
-
return this.publishLocked(input);
|
|
580
|
+
return this.emitPublished(this.publishLocked(input), input.kind);
|
|
479
581
|
}
|
|
480
582
|
catch (error) {
|
|
481
583
|
return asStoreFailure(error);
|
|
@@ -626,6 +728,15 @@ export class CanvasStore {
|
|
|
626
728
|
return toolError('CAS_CONFLICT', 'baseRevision is not the current preview.');
|
|
627
729
|
parent = Number(canvas.preview_revision);
|
|
628
730
|
}
|
|
731
|
+
for (const file of input.files) {
|
|
732
|
+
const path = normalizeCanvasPath(file.path);
|
|
733
|
+
if (!path)
|
|
734
|
+
continue;
|
|
735
|
+
const previous = parent == null ? null : this.fileSizeAt(input.canvasId, parent, path);
|
|
736
|
+
const short = shorterThanCurrent(file, previous);
|
|
737
|
+
if (short)
|
|
738
|
+
return toolError('VALIDATION', short);
|
|
739
|
+
}
|
|
629
740
|
const revision = this.nextRevision(input.canvasId);
|
|
630
741
|
const createdAt = nowIso();
|
|
631
742
|
const hash = manifestHash(checked);
|
|
@@ -682,6 +793,71 @@ export class CanvasStore {
|
|
|
682
793
|
},
|
|
683
794
|
});
|
|
684
795
|
}
|
|
796
|
+
fileSizeAt(canvasId, revision, path) {
|
|
797
|
+
if (revision == null)
|
|
798
|
+
return null;
|
|
799
|
+
const row = this.db
|
|
800
|
+
.prepare('SELECT size FROM canvas_files WHERE canvas_id = ? AND revision = ? AND path = ?')
|
|
801
|
+
.get(canvasId, revision, path);
|
|
802
|
+
return row ? Number(row.size) : null;
|
|
803
|
+
}
|
|
804
|
+
previewPaths(canvasId) {
|
|
805
|
+
const canvas = this.rawCanvas(canvasId);
|
|
806
|
+
if (!canvas || canvas.preview_revision == null)
|
|
807
|
+
return [];
|
|
808
|
+
return this.db
|
|
809
|
+
.prepare('SELECT path FROM canvas_files WHERE canvas_id = ? AND revision = ? ORDER BY path ASC')
|
|
810
|
+
.all(canvasId, Number(canvas.preview_revision))
|
|
811
|
+
.map(row => String(row.path));
|
|
812
|
+
}
|
|
813
|
+
/** Current preview. HTML and CSS include text. Other files are path, type, and size only. */
|
|
814
|
+
previewListing(canvasId) {
|
|
815
|
+
const canvas = this.rawCanvas(canvasId);
|
|
816
|
+
if (!canvas)
|
|
817
|
+
return toolError('NOT_FOUND', 'No such canvas.');
|
|
818
|
+
const revision = canvas.preview_revision == null ? null : Number(canvas.preview_revision);
|
|
819
|
+
if (revision == null)
|
|
820
|
+
return { ok: true, revision: null, files: [] };
|
|
821
|
+
const rows = this.db
|
|
822
|
+
.prepare(`SELECT f.path, f.media_type, f.size, b.bytes
|
|
823
|
+
FROM canvas_files f
|
|
824
|
+
JOIN canvas_blobs b ON b.hash = f.hash
|
|
825
|
+
WHERE f.canvas_id = ? AND f.revision = ?
|
|
826
|
+
ORDER BY f.path ASC`)
|
|
827
|
+
.all(canvasId, revision);
|
|
828
|
+
return {
|
|
829
|
+
ok: true,
|
|
830
|
+
revision,
|
|
831
|
+
files: rows.map(row => {
|
|
832
|
+
const type = String(row.media_type);
|
|
833
|
+
const item = { path: String(row.path), type, bytes: Number(row.size) };
|
|
834
|
+
if (type === 'text/html' || type === 'text/css')
|
|
835
|
+
item.text = Buffer.from(row.bytes).toString('utf8');
|
|
836
|
+
return item;
|
|
837
|
+
}),
|
|
838
|
+
};
|
|
839
|
+
}
|
|
840
|
+
/** Staged bytes win. Otherwise the current preview file. */
|
|
841
|
+
currentFile(canvasId, path) {
|
|
842
|
+
const draft = this.db
|
|
843
|
+
.prepare('SELECT media_type, bytes FROM canvas_drafts WHERE canvas_id = ? AND path = ?')
|
|
844
|
+
.get(canvasId, path);
|
|
845
|
+
if (draft)
|
|
846
|
+
return { type: String(draft.media_type), bytes: Buffer.from(draft.bytes) };
|
|
847
|
+
const canvas = this.rawCanvas(canvasId);
|
|
848
|
+
const revision = canvas?.preview_revision == null ? null : Number(canvas.preview_revision);
|
|
849
|
+
if (revision == null)
|
|
850
|
+
return null;
|
|
851
|
+
const row = this.db
|
|
852
|
+
.prepare(`SELECT f.media_type, b.bytes
|
|
853
|
+
FROM canvas_files f
|
|
854
|
+
JOIN canvas_blobs b ON b.hash = f.hash
|
|
855
|
+
WHERE f.canvas_id = ? AND f.revision = ? AND f.path = ?`)
|
|
856
|
+
.get(canvasId, revision, path);
|
|
857
|
+
if (!row)
|
|
858
|
+
return null;
|
|
859
|
+
return { type: String(row.media_type), bytes: Buffer.from(row.bytes) };
|
|
860
|
+
}
|
|
685
861
|
revisionFiles(canvasId, revision) {
|
|
686
862
|
if (!CANVAS_ID.test(canvasId) || !Number.isInteger(revision) || revision < 1)
|
|
687
863
|
return toolError('NOT_FOUND', 'No such revision.');
|
|
@@ -754,6 +930,51 @@ export class CanvasStore {
|
|
|
754
930
|
FROM canvases WHERE id = ?`)
|
|
755
931
|
.get(canvasId);
|
|
756
932
|
}
|
|
933
|
+
/** Current preview of each canvas. Older revisions are not replayed as extra cards. */
|
|
934
|
+
publishedPreviews() {
|
|
935
|
+
const rows = this.db
|
|
936
|
+
.prepare(`SELECT c.id, c.session_id, c.title, c.ticket_id, r.revision, r.kind
|
|
937
|
+
FROM canvases c
|
|
938
|
+
JOIN canvas_revisions r ON r.canvas_id = c.id AND r.revision = c.preview_revision
|
|
939
|
+
WHERE c.preview_revision IS NOT NULL AND r.status != 'failed' AND r.kind IN ('snapshot', 'delta')`)
|
|
940
|
+
.all();
|
|
941
|
+
return rows.flatMap(row => {
|
|
942
|
+
const kind = row.kind === 'delta' ? 'delta' : row.kind === 'snapshot' ? 'snapshot' : null;
|
|
943
|
+
if (!kind || typeof row.id !== 'string' || typeof row.revision !== 'number')
|
|
944
|
+
return [];
|
|
945
|
+
return [
|
|
946
|
+
{
|
|
947
|
+
canvasId: row.id,
|
|
948
|
+
sessionId: typeof row.session_id === 'string' ? row.session_id : '',
|
|
949
|
+
revision: row.revision,
|
|
950
|
+
kind,
|
|
951
|
+
title: typeof row.title === 'string' ? row.title : '',
|
|
952
|
+
ticketId: typeof row.ticket_id === 'string' ? row.ticket_id : null,
|
|
953
|
+
},
|
|
954
|
+
];
|
|
955
|
+
});
|
|
956
|
+
}
|
|
957
|
+
emitPublished(result, kind) {
|
|
958
|
+
if (result?.ok !== true || typeof result.revision !== 'number' || !result.canvas)
|
|
959
|
+
return result;
|
|
960
|
+
const event = {
|
|
961
|
+
canvasId: result.canvas.id,
|
|
962
|
+
sessionId: result.canvas.sessionId,
|
|
963
|
+
revision: result.revision,
|
|
964
|
+
kind,
|
|
965
|
+
title: result.canvas.title,
|
|
966
|
+
ticketId: result.canvas.ticketId,
|
|
967
|
+
};
|
|
968
|
+
for (const listener of this.publishListeners) {
|
|
969
|
+
try {
|
|
970
|
+
listener(event);
|
|
971
|
+
}
|
|
972
|
+
catch {
|
|
973
|
+
/* the revision is already committed */
|
|
974
|
+
}
|
|
975
|
+
}
|
|
976
|
+
return result;
|
|
977
|
+
}
|
|
757
978
|
canvasRow(canvasId) {
|
|
758
979
|
const row = this.rawCanvas(canvasId);
|
|
759
980
|
if (!row)
|