context-drop 0.2.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -11,7 +11,7 @@ export interface LocalStorageAdapterOptions {
11
11
  */
12
12
  backend?: "auto" | "sqlite" | "json";
13
13
  /**
14
- * Optional HMAC secret for signing pending upload blob IDs.
14
+ * Optional HMAC secret for signing pending upload blob IDs and ownership tokens.
15
15
  */
16
16
  secret?: string;
17
17
  /**
@@ -2,11 +2,10 @@ import crypto from "node:crypto";
2
2
  import fs from "node:fs";
3
3
  import { createRequire } from "node:module";
4
4
  import path from "node:path";
5
- import { matchesFeedbackFilter, toFeedbackSummary } from "./memory.js";
5
+ import { matchesFeedbackFilter, matchesOwnFeedback, toFeedbackSummary } from "./memory.js";
6
6
  function tryOpenNodeSqlite(dbPath) {
7
7
  try {
8
8
  const req = createRequire(import.meta.url);
9
- // Suppress Node experimental SQLite warning when probing built-in module
10
9
  const sqliteMod = req("node:sqlite");
11
10
  if (sqliteMod && typeof sqliteMod.DatabaseSync === "function") {
12
11
  const db = new sqliteMod.DatabaseSync(dbPath);
@@ -118,7 +117,8 @@ export function createLocalStorageAdapter(options = {}) {
118
117
  submitter_json TEXT NOT NULL,
119
118
  metadata_json TEXT NOT NULL,
120
119
  created_at TEXT NOT NULL,
121
- updated_at TEXT NOT NULL
120
+ updated_at TEXT NOT NULL,
121
+ deleted_at TEXT
122
122
  );
123
123
 
124
124
  CREATE INDEX IF NOT EXISTS idx_cd_feedbacks_env_status
@@ -139,9 +139,20 @@ export function createLocalStorageAdapter(options = {}) {
139
139
  captured_at TEXT NOT NULL
140
140
  );
141
141
 
142
+ CREATE TABLE IF NOT EXISTS context_drop_comments (
143
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
144
+ feedback_id INTEGER NOT NULL REFERENCES context_drop_feedbacks(id) ON DELETE CASCADE,
145
+ author_type TEXT,
146
+ author_label TEXT,
147
+ body TEXT,
148
+ created_at TEXT NOT NULL,
149
+ updated_at TEXT NOT NULL
150
+ );
151
+
142
152
  CREATE TABLE IF NOT EXISTS context_drop_attachments (
143
153
  id INTEGER PRIMARY KEY AUTOINCREMENT,
144
154
  feedback_id INTEGER NOT NULL REFERENCES context_drop_feedbacks(id) ON DELETE CASCADE,
155
+ comment_id INTEGER REFERENCES context_drop_comments(id) ON DELETE CASCADE,
145
156
  element_reference_id INTEGER REFERENCES context_drop_element_references(id) ON DELETE SET NULL,
146
157
  attachment_type TEXT NOT NULL,
147
158
  filename TEXT,
@@ -152,6 +163,15 @@ export function createLocalStorageAdapter(options = {}) {
152
163
  created_at TEXT NOT NULL
153
164
  );
154
165
  `);
166
+ const cols = sqliteDb.prepare("PRAGMA table_info(context_drop_feedbacks)").all();
167
+ if (!cols.some((c) => String(c["name"]) === "deleted_at")) {
168
+ sqliteDb.exec("ALTER TABLE context_drop_feedbacks ADD COLUMN deleted_at TEXT;");
169
+ }
170
+ const attCols = sqliteDb.prepare("PRAGMA table_info(context_drop_attachments)").all();
171
+ if (!attCols.some((c) => String(c["name"]) === "comment_id")) {
172
+ sqliteDb.exec("ALTER TABLE context_drop_attachments ADD COLUMN comment_id INTEGER REFERENCES context_drop_comments(id) ON DELETE CASCADE;");
173
+ }
174
+ sqliteDb.exec("UPDATE context_drop_feedbacks SET status = 'fixed' WHERE status = 'resolved';");
155
175
  const hydrateFeedbackRow = (row) => {
156
176
  const fbId = Number(row["id"]);
157
177
  const elRows = sqliteDb
@@ -160,6 +180,9 @@ export function createLocalStorageAdapter(options = {}) {
160
180
  const attRows = sqliteDb
161
181
  .prepare("SELECT * FROM context_drop_attachments WHERE feedback_id = ? ORDER BY id ASC")
162
182
  .all(fbId);
183
+ const commentRows = sqliteDb
184
+ .prepare("SELECT * FROM context_drop_comments WHERE feedback_id = ? ORDER BY id ASC")
185
+ .all(fbId);
163
186
  const elements = elRows.map((el) => ({
164
187
  id: Number(el["id"]),
165
188
  feedback_id: fbId,
@@ -174,13 +197,16 @@ export function createLocalStorageAdapter(options = {}) {
174
197
  metadata: JSON.parse(String(el["metadata_json"] || "{}")),
175
198
  captured_at: String(el["captured_at"])
176
199
  }));
177
- const attachments = attRows.map((att) => ({
200
+ const allAttachments = attRows.map((att) => ({
178
201
  id: Number(att["id"]),
179
202
  feedback_id: fbId,
203
+ comment_id: att["comment_id"] !== null && att["comment_id"] !== undefined
204
+ ? Number(att["comment_id"])
205
+ : null,
180
206
  element_reference_id: att["element_reference_id"] !== null && att["element_reference_id"] !== undefined
181
207
  ? Number(att["element_reference_id"])
182
208
  : null,
183
- attachment_type: (att["attachment_type"] === "screenshot" ? "screenshot" : "image"),
209
+ attachment_type: att["attachment_type"] === "screenshot" ? "screenshot" : "image",
184
210
  filename: att["filename"] ?? null,
185
211
  content_type: att["content_type"] ?? null,
186
212
  byte_size: att["byte_size"] !== null && att["byte_size"] !== undefined
@@ -190,10 +216,34 @@ export function createLocalStorageAdapter(options = {}) {
190
216
  metadata: JSON.parse(String(att["metadata_json"] || "{}")),
191
217
  created_at: String(att["created_at"])
192
218
  }));
219
+ const attachments = allAttachments.filter((a) => a.comment_id === null || a.comment_id === undefined);
220
+ const attachmentsByComment = new Map();
221
+ for (const a of allAttachments) {
222
+ if (a.comment_id !== null && a.comment_id !== undefined) {
223
+ const list = attachmentsByComment.get(Number(a.comment_id)) ?? [];
224
+ list.push(a);
225
+ attachmentsByComment.set(Number(a.comment_id), list);
226
+ }
227
+ }
228
+ const comments = commentRows.map((c) => {
229
+ const cid = Number(c["id"]);
230
+ return {
231
+ id: cid,
232
+ feedback_id: fbId,
233
+ author_type: c["author_type"] ?? null,
234
+ author_label: String(c["author_label"] || "You"),
235
+ body: c["body"] ?? null,
236
+ attachments: attachmentsByComment.get(cid) ?? [],
237
+ created_at: String(c["created_at"]),
238
+ updated_at: String(c["updated_at"])
239
+ };
240
+ });
241
+ const rawStatus = String(row["status"]);
242
+ const normalizedStatus = (rawStatus === "resolved" ? "fixed" : rawStatus);
193
243
  return {
194
244
  id: fbId,
195
245
  environment: String(row["environment"]),
196
- status: String(row["status"]),
246
+ status: normalizedStatus,
197
247
  message: row["message"] ?? null,
198
248
  page_url: String(row["page_url"]),
199
249
  user_agent: row["user_agent"] ?? null,
@@ -202,11 +252,16 @@ export function createLocalStorageAdapter(options = {}) {
202
252
  metadata: JSON.parse(String(row["metadata_json"] || "{}")),
203
253
  element_references: elements,
204
254
  attachments,
255
+ comments,
205
256
  created_at: String(row["created_at"]),
206
- updated_at: String(row["updated_at"])
257
+ updated_at: String(row["updated_at"]),
258
+ deleted_at: row["deleted_at"] ?? null
207
259
  };
208
260
  };
209
261
  return {
262
+ getOwnershipSecret() {
263
+ return secretKey;
264
+ },
210
265
  async savePendingBlob(blob) {
211
266
  const rawId = crypto.randomUUID();
212
267
  const stored = await blobStore.putBlob({
@@ -304,6 +359,17 @@ export function createLocalStorageAdapter(options = {}) {
304
359
  records: hydrated.slice(offset, offset + limit).map(toFeedbackSummary)
305
360
  };
306
361
  },
362
+ async listOwnFeedbacks(query) {
363
+ const limit = Math.max(1, Math.min(50, query.limit ?? 20));
364
+ const rows = sqliteDb
365
+ .prepare("SELECT * FROM context_drop_feedbacks WHERE deleted_at IS NULL ORDER BY created_at DESC, id DESC")
366
+ .all();
367
+ const matching = rows.map(hydrateFeedbackRow).filter((fb) => matchesOwnFeedback(fb, query));
368
+ return {
369
+ total: matching.length,
370
+ records: matching.slice(0, limit)
371
+ };
372
+ },
307
373
  async getFeedback(id) {
308
374
  const row = sqliteDb
309
375
  .prepare("SELECT * FROM context_drop_feedbacks WHERE id = ?")
@@ -312,6 +378,110 @@ export function createLocalStorageAdapter(options = {}) {
312
378
  return null;
313
379
  return hydrateFeedbackRow(row);
314
380
  },
381
+ async updateFeedback(id, input) {
382
+ const fbId = Number(id);
383
+ const existing = await this.getFeedback(fbId);
384
+ if (!existing || existing.deleted_at)
385
+ return null;
386
+ const now = new Date().toISOString();
387
+ const nextMessage = input.message !== undefined ? input.message : existing.message;
388
+ const nextMetadata = input.metadata !== undefined ? input.metadata : existing.metadata;
389
+ const contentEdited = input.message !== undefined ||
390
+ input.metadata !== undefined ||
391
+ input.element_references !== undefined ||
392
+ input.attachments !== undefined;
393
+ const nextStatus = input.status ?? (contentEdited ? "updated" : existing.status);
394
+ sqliteDb
395
+ .prepare("UPDATE context_drop_feedbacks SET message = ?, metadata_json = ?, status = ?, updated_at = ? WHERE id = ?")
396
+ .run(nextMessage, JSON.stringify(nextMetadata || {}), nextStatus, now, fbId);
397
+ const elIdByRef = new Map();
398
+ if (input.element_references !== undefined) {
399
+ sqliteDb
400
+ .prepare("DELETE FROM context_drop_element_references WHERE feedback_id = ?")
401
+ .run(fbId);
402
+ const insertElStmt = sqliteDb.prepare(`INSERT INTO context_drop_element_references
403
+ (feedback_id, client_ref, tag_name, selector, dom_path, text_content, attributes_json, bounding_rect_json, viewport_json, metadata_json, captured_at)
404
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`);
405
+ input.element_references.forEach((el, idx) => {
406
+ const elRes = insertElStmt.run(fbId, el.client_ref, el.tag_name, el.selector, el.dom_path, el.text_content, JSON.stringify(el.attributes_snapshot || {}), JSON.stringify(el.bounding_rect || {}), JSON.stringify(el.viewport || {}), JSON.stringify(el.metadata || {}), el.captured_at);
407
+ elIdByRef.set(el.client_ref || String(idx), Number(elRes.lastInsertRowid));
408
+ });
409
+ }
410
+ else {
411
+ existing.element_references.forEach((el, idx) => {
412
+ elIdByRef.set(el.client_ref || String(idx), Number(el.id));
413
+ });
414
+ }
415
+ if (input.attachments !== undefined) {
416
+ const retainedIds = new Set();
417
+ const prevById = new Map();
418
+ for (const a of existing.attachments) {
419
+ prevById.set(String(a.id), a);
420
+ }
421
+ for (const spec of input.attachments) {
422
+ if (spec.kind === "retain") {
423
+ const prev = prevById.get(String(spec.existing_id));
424
+ if (!prev)
425
+ continue;
426
+ retainedIds.add(Number(prev.id));
427
+ const linkedElId = spec.element_client_ref !== undefined
428
+ ? spec.element_client_ref
429
+ ? elIdByRef.get(spec.element_client_ref) ?? null
430
+ : null
431
+ : input.element_references === undefined && prev.element_reference_id !== null
432
+ ? Number(prev.element_reference_id)
433
+ : null;
434
+ const nextAttMeta = spec.update_metadata && spec.metadata ? spec.metadata : prev.metadata;
435
+ sqliteDb
436
+ .prepare("UPDATE context_drop_attachments SET element_reference_id = ?, metadata_json = ? WHERE id = ?")
437
+ .run(linkedElId, JSON.stringify(nextAttMeta || {}), Number(prev.id));
438
+ }
439
+ }
440
+ for (const prev of existing.attachments) {
441
+ if (!retainedIds.has(Number(prev.id))) {
442
+ sqliteDb
443
+ .prepare("DELETE FROM context_drop_attachments WHERE id = ?")
444
+ .run(Number(prev.id));
445
+ if (prev.blob_key && blobStore.deleteBlob) {
446
+ await blobStore.deleteBlob(prev.blob_key);
447
+ }
448
+ }
449
+ }
450
+ const insertAttStmt = sqliteDb.prepare(`INSERT INTO context_drop_attachments
451
+ (feedback_id, element_reference_id, attachment_type, filename, content_type, byte_size, blob_key, metadata_json, created_at)
452
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`);
453
+ for (const spec of input.attachments) {
454
+ if (spec.kind !== "create")
455
+ continue;
456
+ const linkedElId = spec.element_client_ref
457
+ ? elIdByRef.get(spec.element_client_ref) ?? null
458
+ : null;
459
+ let blobKey = null;
460
+ if (spec.data) {
461
+ const saved = await blobStore.putBlob({
462
+ key: `att_${fbId}_${crypto.randomUUID()}`,
463
+ data: spec.data,
464
+ contentType: spec.content_type || "image/png",
465
+ filename: spec.filename || "attachment.png"
466
+ });
467
+ blobKey = saved.key;
468
+ }
469
+ insertAttStmt.run(fbId, linkedElId, spec.attachment_type, spec.filename, spec.content_type, spec.byte_size, blobKey, JSON.stringify(spec.metadata || {}), now);
470
+ }
471
+ }
472
+ return this.getFeedback(fbId);
473
+ },
474
+ async softDeleteFeedback(id) {
475
+ const fbId = Number(id);
476
+ const existing = await this.getFeedback(fbId);
477
+ if (!existing || existing.deleted_at)
478
+ return null;
479
+ const now = new Date().toISOString();
480
+ sqliteDb
481
+ .prepare("UPDATE context_drop_feedbacks SET deleted_at = ?, updated_at = ? WHERE id = ? AND deleted_at IS NULL")
482
+ .run(now, now, fbId);
483
+ return this.getFeedback(fbId);
484
+ },
315
485
  async getAttachment(attachmentId) {
316
486
  const row = sqliteDb
317
487
  .prepare(`SELECT a.*, e.selector AS element_selector
@@ -351,7 +521,7 @@ export function createLocalStorageAdapter(options = {}) {
351
521
  },
352
522
  async updateFeedbackStatus(id, status) {
353
523
  const existing = await this.getFeedback(id);
354
- if (!existing)
524
+ if (!existing || existing.deleted_at)
355
525
  return null;
356
526
  const now = new Date().toISOString();
357
527
  sqliteDb
@@ -364,6 +534,48 @@ export function createLocalStorageAdapter(options = {}) {
364
534
  previous_status: existing.status,
365
535
  feedback: updated
366
536
  };
537
+ },
538
+ async addComment(feedbackId, input) {
539
+ const fbId = Number(feedbackId);
540
+ const existing = await this.getFeedback(fbId);
541
+ if (!existing || existing.deleted_at)
542
+ return null;
543
+ const now = new Date().toISOString();
544
+ const commentRes = sqliteDb
545
+ .prepare(`INSERT INTO context_drop_comments (feedback_id, author_type, author_label, body, created_at, updated_at)
546
+ VALUES (?, ?, ?, ?, ?, ?)`)
547
+ .run(fbId, input.author_type ?? "User", input.author_label || "You", input.body, now, now);
548
+ const commentId = Number(commentRes.lastInsertRowid);
549
+ const insertAttStmt = sqliteDb.prepare(`INSERT INTO context_drop_attachments
550
+ (feedback_id, comment_id, element_reference_id, attachment_type, filename, content_type, byte_size, blob_key, metadata_json, created_at)
551
+ VALUES (?, ?, NULL, ?, ?, ?, ?, ?, ?, ?)`);
552
+ for (const att of input.attachments ?? []) {
553
+ let blobKey = null;
554
+ if (att.data) {
555
+ const saved = await blobStore.putBlob({
556
+ key: `fb_${fbId}_comment_${commentId}_${crypto.randomUUID()}`,
557
+ data: att.data,
558
+ contentType: att.content_type || "image/png",
559
+ filename: att.filename || "image.png"
560
+ });
561
+ blobKey = saved.key;
562
+ }
563
+ insertAttStmt.run(fbId, commentId, att.attachment_type, att.filename, att.content_type, att.byte_size, blobKey, JSON.stringify(att.metadata || {}), now);
564
+ }
565
+ const nextStatus = input.status ?? "updated";
566
+ sqliteDb
567
+ .prepare("UPDATE context_drop_feedbacks SET status = ?, updated_at = ? WHERE id = ?")
568
+ .run(nextStatus, now, fbId);
569
+ const updated = await this.getFeedback(fbId);
570
+ if (!updated)
571
+ return null;
572
+ const createdComment = (updated.comments ?? []).find((c) => Number(c.id) === commentId);
573
+ if (!createdComment)
574
+ return null;
575
+ return {
576
+ comment: createdComment,
577
+ feedback: updated
578
+ };
367
579
  }
368
580
  };
369
581
  }
@@ -399,6 +611,9 @@ export function createLocalStorageAdapter(options = {}) {
399
611
  fs.renameSync(tmpPath, jsonFilePath);
400
612
  }
401
613
  return {
614
+ getOwnershipSecret() {
615
+ return secretKey;
616
+ },
402
617
  async savePendingBlob(blob) {
403
618
  const rawId = crypto.randomUUID();
404
619
  const stored = await blobStore.putBlob({
@@ -508,7 +723,8 @@ export function createLocalStorageAdapter(options = {}) {
508
723
  element_references: storedElements,
509
724
  attachments: storedAttachments,
510
725
  created_at: now,
511
- updated_at: now
726
+ updated_at: now,
727
+ deleted_at: null
512
728
  };
513
729
  state.feedbacks.push(record);
514
730
  writeJsonStore(state);
@@ -529,11 +745,152 @@ export function createLocalStorageAdapter(options = {}) {
529
745
  records: matching.slice(offset, offset + limit).map(toFeedbackSummary)
530
746
  };
531
747
  },
748
+ async listOwnFeedbacks(query) {
749
+ const state = readJsonStore();
750
+ const limit = Math.max(1, Math.min(50, query.limit ?? 20));
751
+ const matching = state.feedbacks
752
+ .filter((fb) => matchesOwnFeedback(fb, query))
753
+ .sort((a, b) => {
754
+ const dt = Date.parse(b.created_at) - Date.parse(a.created_at);
755
+ return dt !== 0 ? dt : Number(b.id) - Number(a.id);
756
+ });
757
+ return {
758
+ total: matching.length,
759
+ records: matching.slice(0, limit).map((fb) => structuredClone(fb))
760
+ };
761
+ },
532
762
  async getFeedback(id) {
533
763
  const state = readJsonStore();
534
764
  const found = state.feedbacks.find((fb) => String(fb.id) === String(id));
535
765
  return found ? structuredClone(found) : null;
536
766
  },
767
+ async updateFeedback(id, input) {
768
+ const state = readJsonStore();
769
+ const found = state.feedbacks.find((fb) => String(fb.id) === String(id));
770
+ if (!found || found.deleted_at)
771
+ return null;
772
+ const now = new Date().toISOString();
773
+ const contentEdited = input.message !== undefined ||
774
+ input.metadata !== undefined ||
775
+ input.element_references !== undefined ||
776
+ input.attachments !== undefined;
777
+ if (input.message !== undefined) {
778
+ found.message = input.message;
779
+ }
780
+ if (input.metadata !== undefined) {
781
+ found.metadata = { ...input.metadata };
782
+ }
783
+ if (input.status !== undefined) {
784
+ found.status = input.status;
785
+ }
786
+ else if (contentEdited) {
787
+ found.status = "updated";
788
+ }
789
+ const elementsByRef = new Map();
790
+ if (input.element_references !== undefined) {
791
+ found.element_references = input.element_references.map((el, idx) => {
792
+ const elRec = {
793
+ id: state.nextElementId++,
794
+ feedback_id: found.id,
795
+ client_ref: el.client_ref,
796
+ tag_name: el.tag_name,
797
+ selector: el.selector,
798
+ dom_path: el.dom_path,
799
+ text_content: el.text_content,
800
+ attributes_snapshot: { ...el.attributes_snapshot },
801
+ bounding_rect: { ...el.bounding_rect },
802
+ viewport: { ...el.viewport },
803
+ metadata: { ...el.metadata },
804
+ captured_at: el.captured_at
805
+ };
806
+ elementsByRef.set(el.client_ref || String(idx), elRec);
807
+ return elRec;
808
+ });
809
+ }
810
+ else {
811
+ found.element_references.forEach((el, idx) => {
812
+ elementsByRef.set(el.client_ref || String(idx), el);
813
+ });
814
+ }
815
+ if (input.attachments !== undefined) {
816
+ const prevById = new Map();
817
+ for (const a of found.attachments) {
818
+ prevById.set(String(a.id), a);
819
+ }
820
+ const nextAttachments = [];
821
+ const retainedBlobKeys = new Set();
822
+ for (const spec of input.attachments) {
823
+ if (spec.kind === "retain") {
824
+ const prev = prevById.get(String(spec.existing_id));
825
+ if (!prev)
826
+ continue;
827
+ if (prev.blob_key)
828
+ retainedBlobKeys.add(prev.blob_key);
829
+ const linkedEl = spec.element_client_ref !== undefined
830
+ ? spec.element_client_ref
831
+ ? elementsByRef.get(spec.element_client_ref) ?? null
832
+ : null
833
+ : input.element_references === undefined
834
+ ? found.element_references.find((e) => e.id === prev.element_reference_id) ?? null
835
+ : null;
836
+ nextAttachments.push({
837
+ ...prev,
838
+ element_reference_id: linkedEl ? linkedEl.id : null,
839
+ metadata: spec.update_metadata && spec.metadata ? { ...spec.metadata } : { ...prev.metadata }
840
+ });
841
+ }
842
+ else {
843
+ const attId = state.nextAttachmentId++;
844
+ const linkedEl = spec.element_client_ref
845
+ ? elementsByRef.get(spec.element_client_ref) ?? null
846
+ : null;
847
+ let blobKey = null;
848
+ if (spec.data) {
849
+ const saved = await blobStore.putBlob({
850
+ key: `att_${found.id}_${attId}_${crypto.randomUUID()}`,
851
+ data: spec.data,
852
+ contentType: spec.content_type || "image/png",
853
+ filename: spec.filename || "attachment.png"
854
+ });
855
+ blobKey = saved.key;
856
+ retainedBlobKeys.add(blobKey);
857
+ }
858
+ nextAttachments.push({
859
+ id: attId,
860
+ feedback_id: found.id,
861
+ element_reference_id: linkedEl ? linkedEl.id : null,
862
+ attachment_type: spec.attachment_type,
863
+ filename: spec.filename,
864
+ content_type: spec.content_type,
865
+ byte_size: spec.byte_size,
866
+ blob_key: blobKey,
867
+ metadata: { ...spec.metadata },
868
+ created_at: now
869
+ });
870
+ }
871
+ }
872
+ for (const prev of found.attachments) {
873
+ if (prev.blob_key && !retainedBlobKeys.has(prev.blob_key) && blobStore.deleteBlob) {
874
+ await blobStore.deleteBlob(prev.blob_key);
875
+ }
876
+ }
877
+ found.attachments = nextAttachments;
878
+ }
879
+ found.updated_at = now;
880
+ writeJsonStore(state);
881
+ return structuredClone(found);
882
+ },
883
+ async softDeleteFeedback(id) {
884
+ const state = readJsonStore();
885
+ const found = state.feedbacks.find((fb) => String(fb.id) === String(id));
886
+ if (!found || found.deleted_at)
887
+ return null;
888
+ const now = new Date().toISOString();
889
+ found.deleted_at = now;
890
+ found.updated_at = now;
891
+ writeJsonStore(state);
892
+ return structuredClone(found);
893
+ },
537
894
  async getAttachment(attachmentId) {
538
895
  const state = readJsonStore();
539
896
  for (const fb of state.feedbacks) {
@@ -549,6 +906,17 @@ export function createLocalStorageAdapter(options = {}) {
549
906
  data: bytes
550
907
  };
551
908
  }
909
+ for (const comment of fb.comments ?? []) {
910
+ const commentAtt = comment.attachments.find((a) => String(a.id) === String(attachmentId));
911
+ if (commentAtt) {
912
+ const bytes = commentAtt.blob_key ? await blobStore.getBlob(commentAtt.blob_key) : null;
913
+ return {
914
+ attachment: structuredClone(commentAtt),
915
+ element_selector: null,
916
+ data: bytes
917
+ };
918
+ }
919
+ }
552
920
  }
553
921
  return null;
554
922
  },
@@ -556,10 +924,66 @@ export function createLocalStorageAdapter(options = {}) {
556
924
  const res = await this.getAttachment(attachmentId);
557
925
  return res?.data ?? null;
558
926
  },
927
+ async addComment(feedbackId, input) {
928
+ const state = readJsonStore();
929
+ const found = state.feedbacks.find((fb) => String(fb.id) === String(feedbackId));
930
+ if (!found || found.deleted_at)
931
+ return null;
932
+ const now = new Date().toISOString();
933
+ const commentId = state.nextCommentId ?? 1;
934
+ state.nextCommentId = commentId + 1;
935
+ const commentAttachments = [];
936
+ for (const att of input.attachments ?? []) {
937
+ const attId = state.nextAttachmentId++;
938
+ let blobKey = null;
939
+ if (att.data) {
940
+ const saved = await blobStore.putBlob({
941
+ key: `fb_${found.id}_comment_${commentId}_${attId}_${crypto.randomUUID()}`,
942
+ data: att.data,
943
+ contentType: att.content_type || "image/png",
944
+ filename: att.filename || "image.png"
945
+ });
946
+ blobKey = saved.key;
947
+ }
948
+ commentAttachments.push({
949
+ id: attId,
950
+ feedback_id: found.id,
951
+ comment_id: commentId,
952
+ element_reference_id: null,
953
+ attachment_type: att.attachment_type,
954
+ filename: att.filename,
955
+ content_type: att.content_type,
956
+ byte_size: att.byte_size,
957
+ blob_key: blobKey,
958
+ metadata: { ...att.metadata },
959
+ created_at: now
960
+ });
961
+ }
962
+ const comment = {
963
+ id: commentId,
964
+ feedback_id: found.id,
965
+ author_type: input.author_type ?? "User",
966
+ author_label: input.author_label || "You",
967
+ body: input.body,
968
+ attachments: commentAttachments,
969
+ created_at: now,
970
+ updated_at: now
971
+ };
972
+ if (!found.comments)
973
+ found.comments = [];
974
+ found.comments.push(comment);
975
+ found.status = input.status ?? "updated";
976
+ found.updated_at = now;
977
+ writeJsonStore(state);
978
+ return {
979
+ comment: structuredClone(comment),
980
+ feedback: structuredClone(found)
981
+ };
982
+ },
559
983
  async updateFeedbackStatus(id, status) {
560
984
  const state = readJsonStore();
561
985
  const found = state.feedbacks.find((fb) => String(fb.id) === String(id));
562
- if (!found)
986
+ if (!found || found.deleted_at)
563
987
  return null;
564
988
  const previous_status = found.status;
565
989
  found.status = status;
@@ -1,5 +1,8 @@
1
- import type { FeedbackQueryFilter, FeedbackSummaryItem, StorageAdapter, StoredFeedbackRecord } from "../types.js";
1
+ import type { FeedbackQueryFilter, FeedbackSummaryItem, OwnFeedbackLookupQuery, StorageAdapter, StoredFeedbackRecord, SubmitterIdentity } from "../types.js";
2
2
  export declare function formatSubmitterLabel(submitter: StoredFeedbackRecord["submitter"]): string;
3
+ export declare function hasAuthenticatedSubmitter(submitter?: SubmitterIdentity | null): boolean;
4
+ export declare function submitterMatches(recordSubmitter?: SubmitterIdentity | null, callerSubmitter?: SubmitterIdentity | null): boolean;
5
+ export declare function matchesOwnFeedback(record: StoredFeedbackRecord, query: OwnFeedbackLookupQuery): boolean;
3
6
  export declare function toFeedbackSummary(record: StoredFeedbackRecord): FeedbackSummaryItem;
4
7
  export declare function matchesFeedbackFilter(record: StoredFeedbackRecord, filter: FeedbackQueryFilter): boolean;
5
8
  export declare function createMemoryStorageAdapter(): StorageAdapter;