@thready-web/dsh-bot-plugin 0.1.0-beta.11 → 0.1.0-beta.13

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 (55) hide show
  1. package/README.md +20 -3
  2. package/dist/approvals.js +19 -1
  3. package/dist/canvas-compose.js +73 -0
  4. package/dist/canvas-http.js +124 -0
  5. package/dist/canvas-inspector.js +350 -0
  6. package/dist/canvas-store.js +227 -6
  7. package/dist/canvas-tools.js +199 -30
  8. package/dist/card-log.js +216 -0
  9. package/dist/context.js +5 -3
  10. package/dist/deleting.js +54 -0
  11. package/dist/desk-ledger.js +18 -0
  12. package/dist/desk.js +30 -5
  13. package/dist/http-plugin.js +2 -1
  14. package/dist/http.js +200 -4
  15. package/dist/inbox.js +24 -2
  16. package/dist/index.js +65 -13
  17. package/dist/questions.js +10 -0
  18. package/dist/runtime-registry.js +20 -0
  19. package/dist/session-delete-job.js +77 -0
  20. package/dist/session-files.js +33 -1
  21. package/dist/session-wipe.js +204 -0
  22. package/dist/store.js +46 -3
  23. package/dist/triggers.js +8 -0
  24. package/dist/work-admit.js +9 -20
  25. package/dist/work-child.js +27 -10
  26. package/dist/work-ledger.js +5 -1
  27. package/dist/work-mock.js +10 -0
  28. package/dist/work-tickets.js +5 -3
  29. package/dist/work-tools.js +11 -9
  30. package/dist/work-worker.js +240 -6
  31. package/dist/worker-handles.js +33 -0
  32. package/dist/workspace-files.js +98 -17
  33. package/package.json +6 -3
  34. package/public/assets/{SourceEditor-uu0G7jsM.js → SourceEditor-BjtoMvcX.js} +3 -3
  35. package/public/assets/{cssMode-C5DAFF5H.js → cssMode--96PQtWh.js} +1 -1
  36. package/public/assets/{freemarker2-DExNwSiZ.js → freemarker2-Bz-HlISL.js} +1 -1
  37. package/public/assets/{handlebars-BK7YAvpQ.js → handlebars-D9ScJ8AM.js} +1 -1
  38. package/public/assets/{html-CdDm_zae.js → html-Czc0jojJ.js} +1 -1
  39. package/public/assets/{htmlMode-CLdn9oa1.js → htmlMode-CCww5Z1w.js} +1 -1
  40. package/public/assets/index-DRCgKxWk.css +1 -0
  41. package/public/assets/index-ztUdW074.js +72 -0
  42. package/public/assets/{javascript-mLl83_3W.js → javascript-B-yRZSiz.js} +1 -1
  43. package/public/assets/{jsonMode-B1-bihFd.js → jsonMode-ASWsY833.js} +1 -1
  44. package/public/assets/{liquid-Cv8m0HUb.js → liquid-BGoox2vJ.js} +1 -1
  45. package/public/assets/{mdx-Q12b91rL.js → mdx-DHgvnl_U.js} +1 -1
  46. package/public/assets/{python-Kda811W0.js → python-BJvVD30_.js} +1 -1
  47. package/public/assets/{razor-CRuCpdOW.js → razor-0PwR35mu.js} +1 -1
  48. package/public/assets/{tsMode-BGHcz-M3.js → tsMode-3ETp8Gdh.js} +1 -1
  49. package/public/assets/{typescript-Db1e0DCT.js → typescript-BT0iTK6K.js} +1 -1
  50. package/public/assets/{xml-H3zzFmnd.js → xml-BTaNc_uT.js} +1 -1
  51. package/public/assets/{yaml-BQrKvmTF.js → yaml-Dr7LW3tK.js} +1 -1
  52. package/public/index.html +2 -2
  53. package/skills/ui/ui-design/SKILL.md +11 -5
  54. package/public/assets/index-BB-QDSf2.css +0 -1
  55. package/public/assets/index-CsX-_qbo.js +0 -62
@@ -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
- return activeContent(type, file.bytes);
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.prepare('SELECT id FROM canvases WHERE ticket_id = ?').get(ticketId);
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
- if (!this.rawCanvas(canvasId))
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
- return this.publishLocked({
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)