context-drop 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -5
- package/app/assets/javascripts/context_drop/context_drop.js +1039 -26
- package/dist/app/javascript/context_drop/api-client.d.ts +13 -1
- package/dist/app/javascript/context_drop/api-client.js +192 -1
- package/dist/app/javascript/context_drop/icons.d.ts +2 -0
- package/dist/app/javascript/context_drop/icons.js +2 -0
- package/dist/app/javascript/context_drop/styles.d.ts +1 -1
- package/dist/app/javascript/context_drop/styles.js +181 -0
- package/dist/app/javascript/context_drop/types.d.ts +83 -6
- package/dist/app/javascript/context_drop/widget.d.ts +18 -0
- package/dist/app/javascript/context_drop/widget.js +550 -25
- package/dist/src/cli.js +1 -1
- package/dist/src/mcp/dispatcher.js +36 -10
- package/dist/src/next/index.d.ts +1 -1
- package/dist/src/react/index.d.ts +16 -2
- package/dist/src/react/index.js +21 -1
- package/dist/src/server/handler.d.ts +7 -1
- package/dist/src/server/handler.js +584 -81
- package/dist/src/server/storage/local.d.ts +1 -1
- package/dist/src/server/storage/local.js +261 -8
- package/dist/src/server/storage/memory.d.ts +4 -1
- package/dist/src/server/storage/memory.js +169 -3
- package/dist/src/server/storage/postgres.d.ts +1 -1
- package/dist/src/server/storage/postgres.js +189 -6
- package/dist/src/server/types.d.ts +38 -1
- package/dist/src/server/types.js +1 -1
- package/package.json +1 -1
|
@@ -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
|
|
@@ -152,6 +152,10 @@ export function createLocalStorageAdapter(options = {}) {
|
|
|
152
152
|
created_at TEXT NOT NULL
|
|
153
153
|
);
|
|
154
154
|
`);
|
|
155
|
+
const cols = sqliteDb.prepare("PRAGMA table_info(context_drop_feedbacks)").all();
|
|
156
|
+
if (!cols.some((c) => String(c["name"]) === "deleted_at")) {
|
|
157
|
+
sqliteDb.exec("ALTER TABLE context_drop_feedbacks ADD COLUMN deleted_at TEXT;");
|
|
158
|
+
}
|
|
155
159
|
const hydrateFeedbackRow = (row) => {
|
|
156
160
|
const fbId = Number(row["id"]);
|
|
157
161
|
const elRows = sqliteDb
|
|
@@ -180,7 +184,7 @@ export function createLocalStorageAdapter(options = {}) {
|
|
|
180
184
|
element_reference_id: att["element_reference_id"] !== null && att["element_reference_id"] !== undefined
|
|
181
185
|
? Number(att["element_reference_id"])
|
|
182
186
|
: null,
|
|
183
|
-
attachment_type:
|
|
187
|
+
attachment_type: att["attachment_type"] === "screenshot" ? "screenshot" : "image",
|
|
184
188
|
filename: att["filename"] ?? null,
|
|
185
189
|
content_type: att["content_type"] ?? null,
|
|
186
190
|
byte_size: att["byte_size"] !== null && att["byte_size"] !== undefined
|
|
@@ -203,10 +207,14 @@ export function createLocalStorageAdapter(options = {}) {
|
|
|
203
207
|
element_references: elements,
|
|
204
208
|
attachments,
|
|
205
209
|
created_at: String(row["created_at"]),
|
|
206
|
-
updated_at: String(row["updated_at"])
|
|
210
|
+
updated_at: String(row["updated_at"]),
|
|
211
|
+
deleted_at: row["deleted_at"] ?? null
|
|
207
212
|
};
|
|
208
213
|
};
|
|
209
214
|
return {
|
|
215
|
+
getOwnershipSecret() {
|
|
216
|
+
return secretKey;
|
|
217
|
+
},
|
|
210
218
|
async savePendingBlob(blob) {
|
|
211
219
|
const rawId = crypto.randomUUID();
|
|
212
220
|
const stored = await blobStore.putBlob({
|
|
@@ -304,6 +312,17 @@ export function createLocalStorageAdapter(options = {}) {
|
|
|
304
312
|
records: hydrated.slice(offset, offset + limit).map(toFeedbackSummary)
|
|
305
313
|
};
|
|
306
314
|
},
|
|
315
|
+
async listOwnFeedbacks(query) {
|
|
316
|
+
const limit = Math.max(1, Math.min(50, query.limit ?? 20));
|
|
317
|
+
const rows = sqliteDb
|
|
318
|
+
.prepare("SELECT * FROM context_drop_feedbacks WHERE deleted_at IS NULL ORDER BY created_at DESC, id DESC")
|
|
319
|
+
.all();
|
|
320
|
+
const matching = rows.map(hydrateFeedbackRow).filter((fb) => matchesOwnFeedback(fb, query));
|
|
321
|
+
return {
|
|
322
|
+
total: matching.length,
|
|
323
|
+
records: matching.slice(0, limit)
|
|
324
|
+
};
|
|
325
|
+
},
|
|
307
326
|
async getFeedback(id) {
|
|
308
327
|
const row = sqliteDb
|
|
309
328
|
.prepare("SELECT * FROM context_drop_feedbacks WHERE id = ?")
|
|
@@ -312,6 +331,105 @@ export function createLocalStorageAdapter(options = {}) {
|
|
|
312
331
|
return null;
|
|
313
332
|
return hydrateFeedbackRow(row);
|
|
314
333
|
},
|
|
334
|
+
async updateFeedback(id, input) {
|
|
335
|
+
const fbId = Number(id);
|
|
336
|
+
const existing = await this.getFeedback(fbId);
|
|
337
|
+
if (!existing || existing.deleted_at)
|
|
338
|
+
return null;
|
|
339
|
+
const now = new Date().toISOString();
|
|
340
|
+
const nextMessage = input.message !== undefined ? input.message : existing.message;
|
|
341
|
+
const nextMetadata = input.metadata !== undefined ? input.metadata : existing.metadata;
|
|
342
|
+
sqliteDb
|
|
343
|
+
.prepare("UPDATE context_drop_feedbacks SET message = ?, metadata_json = ?, updated_at = ? WHERE id = ?")
|
|
344
|
+
.run(nextMessage, JSON.stringify(nextMetadata || {}), now, fbId);
|
|
345
|
+
const elIdByRef = new Map();
|
|
346
|
+
if (input.element_references !== undefined) {
|
|
347
|
+
sqliteDb
|
|
348
|
+
.prepare("DELETE FROM context_drop_element_references WHERE feedback_id = ?")
|
|
349
|
+
.run(fbId);
|
|
350
|
+
const insertElStmt = sqliteDb.prepare(`INSERT INTO context_drop_element_references
|
|
351
|
+
(feedback_id, client_ref, tag_name, selector, dom_path, text_content, attributes_json, bounding_rect_json, viewport_json, metadata_json, captured_at)
|
|
352
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`);
|
|
353
|
+
input.element_references.forEach((el, idx) => {
|
|
354
|
+
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);
|
|
355
|
+
elIdByRef.set(el.client_ref || String(idx), Number(elRes.lastInsertRowid));
|
|
356
|
+
});
|
|
357
|
+
}
|
|
358
|
+
else {
|
|
359
|
+
existing.element_references.forEach((el, idx) => {
|
|
360
|
+
elIdByRef.set(el.client_ref || String(idx), Number(el.id));
|
|
361
|
+
});
|
|
362
|
+
}
|
|
363
|
+
if (input.attachments !== undefined) {
|
|
364
|
+
const retainedIds = new Set();
|
|
365
|
+
const prevById = new Map();
|
|
366
|
+
for (const a of existing.attachments) {
|
|
367
|
+
prevById.set(String(a.id), a);
|
|
368
|
+
}
|
|
369
|
+
for (const spec of input.attachments) {
|
|
370
|
+
if (spec.kind === "retain") {
|
|
371
|
+
const prev = prevById.get(String(spec.existing_id));
|
|
372
|
+
if (!prev)
|
|
373
|
+
continue;
|
|
374
|
+
retainedIds.add(Number(prev.id));
|
|
375
|
+
const linkedElId = spec.element_client_ref !== undefined
|
|
376
|
+
? spec.element_client_ref
|
|
377
|
+
? elIdByRef.get(spec.element_client_ref) ?? null
|
|
378
|
+
: null
|
|
379
|
+
: input.element_references === undefined && prev.element_reference_id !== null
|
|
380
|
+
? Number(prev.element_reference_id)
|
|
381
|
+
: null;
|
|
382
|
+
const nextAttMeta = spec.update_metadata && spec.metadata ? spec.metadata : prev.metadata;
|
|
383
|
+
sqliteDb
|
|
384
|
+
.prepare("UPDATE context_drop_attachments SET element_reference_id = ?, metadata_json = ? WHERE id = ?")
|
|
385
|
+
.run(linkedElId, JSON.stringify(nextAttMeta || {}), Number(prev.id));
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
for (const prev of existing.attachments) {
|
|
389
|
+
if (!retainedIds.has(Number(prev.id))) {
|
|
390
|
+
sqliteDb
|
|
391
|
+
.prepare("DELETE FROM context_drop_attachments WHERE id = ?")
|
|
392
|
+
.run(Number(prev.id));
|
|
393
|
+
if (prev.blob_key && blobStore.deleteBlob) {
|
|
394
|
+
await blobStore.deleteBlob(prev.blob_key);
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
const insertAttStmt = sqliteDb.prepare(`INSERT INTO context_drop_attachments
|
|
399
|
+
(feedback_id, element_reference_id, attachment_type, filename, content_type, byte_size, blob_key, metadata_json, created_at)
|
|
400
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`);
|
|
401
|
+
for (const spec of input.attachments) {
|
|
402
|
+
if (spec.kind !== "create")
|
|
403
|
+
continue;
|
|
404
|
+
const linkedElId = spec.element_client_ref
|
|
405
|
+
? elIdByRef.get(spec.element_client_ref) ?? null
|
|
406
|
+
: null;
|
|
407
|
+
let blobKey = null;
|
|
408
|
+
if (spec.data) {
|
|
409
|
+
const saved = await blobStore.putBlob({
|
|
410
|
+
key: `att_${fbId}_${crypto.randomUUID()}`,
|
|
411
|
+
data: spec.data,
|
|
412
|
+
contentType: spec.content_type || "image/png",
|
|
413
|
+
filename: spec.filename || "attachment.png"
|
|
414
|
+
});
|
|
415
|
+
blobKey = saved.key;
|
|
416
|
+
}
|
|
417
|
+
insertAttStmt.run(fbId, linkedElId, spec.attachment_type, spec.filename, spec.content_type, spec.byte_size, blobKey, JSON.stringify(spec.metadata || {}), now);
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
return this.getFeedback(fbId);
|
|
421
|
+
},
|
|
422
|
+
async softDeleteFeedback(id) {
|
|
423
|
+
const fbId = Number(id);
|
|
424
|
+
const existing = await this.getFeedback(fbId);
|
|
425
|
+
if (!existing || existing.deleted_at)
|
|
426
|
+
return null;
|
|
427
|
+
const now = new Date().toISOString();
|
|
428
|
+
sqliteDb
|
|
429
|
+
.prepare("UPDATE context_drop_feedbacks SET deleted_at = ?, updated_at = ? WHERE id = ? AND deleted_at IS NULL")
|
|
430
|
+
.run(now, now, fbId);
|
|
431
|
+
return this.getFeedback(fbId);
|
|
432
|
+
},
|
|
315
433
|
async getAttachment(attachmentId) {
|
|
316
434
|
const row = sqliteDb
|
|
317
435
|
.prepare(`SELECT a.*, e.selector AS element_selector
|
|
@@ -351,7 +469,7 @@ export function createLocalStorageAdapter(options = {}) {
|
|
|
351
469
|
},
|
|
352
470
|
async updateFeedbackStatus(id, status) {
|
|
353
471
|
const existing = await this.getFeedback(id);
|
|
354
|
-
if (!existing)
|
|
472
|
+
if (!existing || existing.deleted_at)
|
|
355
473
|
return null;
|
|
356
474
|
const now = new Date().toISOString();
|
|
357
475
|
sqliteDb
|
|
@@ -399,6 +517,9 @@ export function createLocalStorageAdapter(options = {}) {
|
|
|
399
517
|
fs.renameSync(tmpPath, jsonFilePath);
|
|
400
518
|
}
|
|
401
519
|
return {
|
|
520
|
+
getOwnershipSecret() {
|
|
521
|
+
return secretKey;
|
|
522
|
+
},
|
|
402
523
|
async savePendingBlob(blob) {
|
|
403
524
|
const rawId = crypto.randomUUID();
|
|
404
525
|
const stored = await blobStore.putBlob({
|
|
@@ -508,7 +629,8 @@ export function createLocalStorageAdapter(options = {}) {
|
|
|
508
629
|
element_references: storedElements,
|
|
509
630
|
attachments: storedAttachments,
|
|
510
631
|
created_at: now,
|
|
511
|
-
updated_at: now
|
|
632
|
+
updated_at: now,
|
|
633
|
+
deleted_at: null
|
|
512
634
|
};
|
|
513
635
|
state.feedbacks.push(record);
|
|
514
636
|
writeJsonStore(state);
|
|
@@ -529,11 +651,142 @@ export function createLocalStorageAdapter(options = {}) {
|
|
|
529
651
|
records: matching.slice(offset, offset + limit).map(toFeedbackSummary)
|
|
530
652
|
};
|
|
531
653
|
},
|
|
654
|
+
async listOwnFeedbacks(query) {
|
|
655
|
+
const state = readJsonStore();
|
|
656
|
+
const limit = Math.max(1, Math.min(50, query.limit ?? 20));
|
|
657
|
+
const matching = state.feedbacks
|
|
658
|
+
.filter((fb) => matchesOwnFeedback(fb, query))
|
|
659
|
+
.sort((a, b) => {
|
|
660
|
+
const dt = Date.parse(b.created_at) - Date.parse(a.created_at);
|
|
661
|
+
return dt !== 0 ? dt : Number(b.id) - Number(a.id);
|
|
662
|
+
});
|
|
663
|
+
return {
|
|
664
|
+
total: matching.length,
|
|
665
|
+
records: matching.slice(0, limit).map((fb) => structuredClone(fb))
|
|
666
|
+
};
|
|
667
|
+
},
|
|
532
668
|
async getFeedback(id) {
|
|
533
669
|
const state = readJsonStore();
|
|
534
670
|
const found = state.feedbacks.find((fb) => String(fb.id) === String(id));
|
|
535
671
|
return found ? structuredClone(found) : null;
|
|
536
672
|
},
|
|
673
|
+
async updateFeedback(id, input) {
|
|
674
|
+
const state = readJsonStore();
|
|
675
|
+
const found = state.feedbacks.find((fb) => String(fb.id) === String(id));
|
|
676
|
+
if (!found || found.deleted_at)
|
|
677
|
+
return null;
|
|
678
|
+
const now = new Date().toISOString();
|
|
679
|
+
if (input.message !== undefined) {
|
|
680
|
+
found.message = input.message;
|
|
681
|
+
}
|
|
682
|
+
if (input.metadata !== undefined) {
|
|
683
|
+
found.metadata = { ...input.metadata };
|
|
684
|
+
}
|
|
685
|
+
const elementsByRef = new Map();
|
|
686
|
+
if (input.element_references !== undefined) {
|
|
687
|
+
found.element_references = input.element_references.map((el, idx) => {
|
|
688
|
+
const elRec = {
|
|
689
|
+
id: state.nextElementId++,
|
|
690
|
+
feedback_id: found.id,
|
|
691
|
+
client_ref: el.client_ref,
|
|
692
|
+
tag_name: el.tag_name,
|
|
693
|
+
selector: el.selector,
|
|
694
|
+
dom_path: el.dom_path,
|
|
695
|
+
text_content: el.text_content,
|
|
696
|
+
attributes_snapshot: { ...el.attributes_snapshot },
|
|
697
|
+
bounding_rect: { ...el.bounding_rect },
|
|
698
|
+
viewport: { ...el.viewport },
|
|
699
|
+
metadata: { ...el.metadata },
|
|
700
|
+
captured_at: el.captured_at
|
|
701
|
+
};
|
|
702
|
+
elementsByRef.set(el.client_ref || String(idx), elRec);
|
|
703
|
+
return elRec;
|
|
704
|
+
});
|
|
705
|
+
}
|
|
706
|
+
else {
|
|
707
|
+
found.element_references.forEach((el, idx) => {
|
|
708
|
+
elementsByRef.set(el.client_ref || String(idx), el);
|
|
709
|
+
});
|
|
710
|
+
}
|
|
711
|
+
if (input.attachments !== undefined) {
|
|
712
|
+
const prevById = new Map();
|
|
713
|
+
for (const a of found.attachments) {
|
|
714
|
+
prevById.set(String(a.id), a);
|
|
715
|
+
}
|
|
716
|
+
const nextAttachments = [];
|
|
717
|
+
const retainedBlobKeys = new Set();
|
|
718
|
+
for (const spec of input.attachments) {
|
|
719
|
+
if (spec.kind === "retain") {
|
|
720
|
+
const prev = prevById.get(String(spec.existing_id));
|
|
721
|
+
if (!prev)
|
|
722
|
+
continue;
|
|
723
|
+
if (prev.blob_key)
|
|
724
|
+
retainedBlobKeys.add(prev.blob_key);
|
|
725
|
+
const linkedEl = spec.element_client_ref !== undefined
|
|
726
|
+
? spec.element_client_ref
|
|
727
|
+
? elementsByRef.get(spec.element_client_ref) ?? null
|
|
728
|
+
: null
|
|
729
|
+
: input.element_references === undefined
|
|
730
|
+
? found.element_references.find((e) => e.id === prev.element_reference_id) ?? null
|
|
731
|
+
: null;
|
|
732
|
+
nextAttachments.push({
|
|
733
|
+
...prev,
|
|
734
|
+
element_reference_id: linkedEl ? linkedEl.id : null,
|
|
735
|
+
metadata: spec.update_metadata && spec.metadata ? { ...spec.metadata } : { ...prev.metadata }
|
|
736
|
+
});
|
|
737
|
+
}
|
|
738
|
+
else {
|
|
739
|
+
const attId = state.nextAttachmentId++;
|
|
740
|
+
const linkedEl = spec.element_client_ref
|
|
741
|
+
? elementsByRef.get(spec.element_client_ref) ?? null
|
|
742
|
+
: null;
|
|
743
|
+
let blobKey = null;
|
|
744
|
+
if (spec.data) {
|
|
745
|
+
const saved = await blobStore.putBlob({
|
|
746
|
+
key: `att_${found.id}_${attId}_${crypto.randomUUID()}`,
|
|
747
|
+
data: spec.data,
|
|
748
|
+
contentType: spec.content_type || "image/png",
|
|
749
|
+
filename: spec.filename || "attachment.png"
|
|
750
|
+
});
|
|
751
|
+
blobKey = saved.key;
|
|
752
|
+
retainedBlobKeys.add(blobKey);
|
|
753
|
+
}
|
|
754
|
+
nextAttachments.push({
|
|
755
|
+
id: attId,
|
|
756
|
+
feedback_id: found.id,
|
|
757
|
+
element_reference_id: linkedEl ? linkedEl.id : null,
|
|
758
|
+
attachment_type: spec.attachment_type,
|
|
759
|
+
filename: spec.filename,
|
|
760
|
+
content_type: spec.content_type,
|
|
761
|
+
byte_size: spec.byte_size,
|
|
762
|
+
blob_key: blobKey,
|
|
763
|
+
metadata: { ...spec.metadata },
|
|
764
|
+
created_at: now
|
|
765
|
+
});
|
|
766
|
+
}
|
|
767
|
+
}
|
|
768
|
+
for (const prev of found.attachments) {
|
|
769
|
+
if (prev.blob_key && !retainedBlobKeys.has(prev.blob_key) && blobStore.deleteBlob) {
|
|
770
|
+
await blobStore.deleteBlob(prev.blob_key);
|
|
771
|
+
}
|
|
772
|
+
}
|
|
773
|
+
found.attachments = nextAttachments;
|
|
774
|
+
}
|
|
775
|
+
found.updated_at = now;
|
|
776
|
+
writeJsonStore(state);
|
|
777
|
+
return structuredClone(found);
|
|
778
|
+
},
|
|
779
|
+
async softDeleteFeedback(id) {
|
|
780
|
+
const state = readJsonStore();
|
|
781
|
+
const found = state.feedbacks.find((fb) => String(fb.id) === String(id));
|
|
782
|
+
if (!found || found.deleted_at)
|
|
783
|
+
return null;
|
|
784
|
+
const now = new Date().toISOString();
|
|
785
|
+
found.deleted_at = now;
|
|
786
|
+
found.updated_at = now;
|
|
787
|
+
writeJsonStore(state);
|
|
788
|
+
return structuredClone(found);
|
|
789
|
+
},
|
|
537
790
|
async getAttachment(attachmentId) {
|
|
538
791
|
const state = readJsonStore();
|
|
539
792
|
for (const fb of state.feedbacks) {
|
|
@@ -559,7 +812,7 @@ export function createLocalStorageAdapter(options = {}) {
|
|
|
559
812
|
async updateFeedbackStatus(id, status) {
|
|
560
813
|
const state = readJsonStore();
|
|
561
814
|
const found = state.feedbacks.find((fb) => String(fb.id) === String(id));
|
|
562
|
-
if (!found)
|
|
815
|
+
if (!found || found.deleted_at)
|
|
563
816
|
return null;
|
|
564
817
|
const previous_status = found.status;
|
|
565
818
|
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;
|
|
@@ -13,8 +13,40 @@ export function formatSubmitterLabel(submitter) {
|
|
|
13
13
|
}
|
|
14
14
|
return "Anonymous";
|
|
15
15
|
}
|
|
16
|
+
export function hasAuthenticatedSubmitter(submitter) {
|
|
17
|
+
if (!submitter)
|
|
18
|
+
return false;
|
|
19
|
+
return submitter.id !== undefined && submitter.id !== null && String(submitter.id).trim() !== "";
|
|
20
|
+
}
|
|
21
|
+
export function submitterMatches(recordSubmitter, callerSubmitter) {
|
|
22
|
+
if (!hasAuthenticatedSubmitter(recordSubmitter) || !hasAuthenticatedSubmitter(callerSubmitter)) {
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
const recType = String(recordSubmitter?.type || "User").trim();
|
|
26
|
+
const callerType = String(callerSubmitter?.type || "User").trim();
|
|
27
|
+
return (recType === callerType &&
|
|
28
|
+
String(recordSubmitter?.id).trim() === String(callerSubmitter?.id).trim());
|
|
29
|
+
}
|
|
30
|
+
export function matchesOwnFeedback(record, query) {
|
|
31
|
+
if (record.deleted_at)
|
|
32
|
+
return false;
|
|
33
|
+
if (hasAuthenticatedSubmitter(record.submitter)) {
|
|
34
|
+
return submitterMatches(record.submitter, query.submitter);
|
|
35
|
+
}
|
|
36
|
+
if (query.token_ids && query.token_ids.some((tid) => String(tid) === String(record.id))) {
|
|
37
|
+
return true;
|
|
38
|
+
}
|
|
39
|
+
if (query.session_id &&
|
|
40
|
+
query.session_id.trim().length > 0 &&
|
|
41
|
+
record.session_id &&
|
|
42
|
+
record.session_id.trim().length > 0 &&
|
|
43
|
+
query.session_id === record.session_id) {
|
|
44
|
+
return true;
|
|
45
|
+
}
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
16
48
|
export function toFeedbackSummary(record) {
|
|
17
|
-
|
|
49
|
+
const summary = {
|
|
18
50
|
id: record.id,
|
|
19
51
|
qualified_id: `${record.environment}:${record.id}`,
|
|
20
52
|
environment: record.environment,
|
|
@@ -27,8 +59,16 @@ export function toFeedbackSummary(record) {
|
|
|
27
59
|
element_selectors: record.element_references.map((el) => el.selector),
|
|
28
60
|
created_at: record.created_at
|
|
29
61
|
};
|
|
62
|
+
if (record.deleted_at) {
|
|
63
|
+
summary.deleted = true;
|
|
64
|
+
summary.deleted_at = record.deleted_at;
|
|
65
|
+
}
|
|
66
|
+
return summary;
|
|
30
67
|
}
|
|
31
68
|
export function matchesFeedbackFilter(record, filter) {
|
|
69
|
+
if (!filter.include_deleted && record.deleted_at) {
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
32
72
|
if (filter.environment && filter.environment.trim().length > 0) {
|
|
33
73
|
if (record.environment.toLowerCase() !== filter.environment.trim().toLowerCase()) {
|
|
34
74
|
return false;
|
|
@@ -82,6 +122,9 @@ export function createMemoryStorageAdapter() {
|
|
|
82
122
|
return rawId;
|
|
83
123
|
}
|
|
84
124
|
return {
|
|
125
|
+
getOwnershipSecret() {
|
|
126
|
+
return secret;
|
|
127
|
+
},
|
|
85
128
|
async savePendingBlob(blob) {
|
|
86
129
|
const rawId = crypto.randomUUID();
|
|
87
130
|
pendingBlobs.set(rawId, {
|
|
@@ -158,7 +201,8 @@ export function createMemoryStorageAdapter() {
|
|
|
158
201
|
element_references: storedElements,
|
|
159
202
|
attachments: storedAttachments,
|
|
160
203
|
created_at: now,
|
|
161
|
-
updated_at: now
|
|
204
|
+
updated_at: now,
|
|
205
|
+
deleted_at: null
|
|
162
206
|
};
|
|
163
207
|
feedbacks.set(id, record);
|
|
164
208
|
return structuredClone(record);
|
|
@@ -177,11 +221,133 @@ export function createMemoryStorageAdapter() {
|
|
|
177
221
|
records: matching.slice(offset, offset + limit).map(toFeedbackSummary)
|
|
178
222
|
};
|
|
179
223
|
},
|
|
224
|
+
async listOwnFeedbacks(query) {
|
|
225
|
+
const limit = Math.max(1, Math.min(50, query.limit ?? 20));
|
|
226
|
+
const matching = Array.from(feedbacks.values())
|
|
227
|
+
.filter((fb) => matchesOwnFeedback(fb, query))
|
|
228
|
+
.sort((a, b) => {
|
|
229
|
+
const dt = Date.parse(b.created_at) - Date.parse(a.created_at);
|
|
230
|
+
return dt !== 0 ? dt : Number(b.id) - Number(a.id);
|
|
231
|
+
});
|
|
232
|
+
return {
|
|
233
|
+
total: matching.length,
|
|
234
|
+
records: matching.slice(0, limit).map((fb) => structuredClone(fb))
|
|
235
|
+
};
|
|
236
|
+
},
|
|
180
237
|
async getFeedback(id) {
|
|
181
238
|
const numId = Number(id);
|
|
182
239
|
const found = feedbacks.get(numId);
|
|
183
240
|
return found ? structuredClone(found) : null;
|
|
184
241
|
},
|
|
242
|
+
async updateFeedback(id, input) {
|
|
243
|
+
const numId = Number(id);
|
|
244
|
+
const existing = feedbacks.get(numId);
|
|
245
|
+
if (!existing || existing.deleted_at)
|
|
246
|
+
return null;
|
|
247
|
+
const now = new Date().toISOString();
|
|
248
|
+
if (input.message !== undefined) {
|
|
249
|
+
existing.message = input.message;
|
|
250
|
+
}
|
|
251
|
+
if (input.metadata !== undefined) {
|
|
252
|
+
existing.metadata = { ...input.metadata };
|
|
253
|
+
}
|
|
254
|
+
const elementsByRef = new Map();
|
|
255
|
+
if (input.element_references !== undefined) {
|
|
256
|
+
existing.element_references = input.element_references.map((el, idx) => {
|
|
257
|
+
const elRec = {
|
|
258
|
+
id: nextElementId++,
|
|
259
|
+
feedback_id: existing.id,
|
|
260
|
+
client_ref: el.client_ref,
|
|
261
|
+
tag_name: el.tag_name,
|
|
262
|
+
selector: el.selector,
|
|
263
|
+
dom_path: el.dom_path,
|
|
264
|
+
text_content: el.text_content,
|
|
265
|
+
attributes_snapshot: { ...el.attributes_snapshot },
|
|
266
|
+
bounding_rect: { ...el.bounding_rect },
|
|
267
|
+
viewport: { ...el.viewport },
|
|
268
|
+
metadata: { ...el.metadata },
|
|
269
|
+
captured_at: el.captured_at
|
|
270
|
+
};
|
|
271
|
+
elementsByRef.set(el.client_ref || String(idx), elRec);
|
|
272
|
+
return elRec;
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
else {
|
|
276
|
+
existing.element_references.forEach((el, idx) => {
|
|
277
|
+
elementsByRef.set(el.client_ref || String(idx), el);
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
if (input.attachments !== undefined) {
|
|
281
|
+
const prevById = new Map();
|
|
282
|
+
for (const a of existing.attachments) {
|
|
283
|
+
prevById.set(String(a.id), a);
|
|
284
|
+
}
|
|
285
|
+
const nextAttachments = [];
|
|
286
|
+
const retainedBlobKeys = new Set();
|
|
287
|
+
for (const spec of input.attachments) {
|
|
288
|
+
if (spec.kind === "retain") {
|
|
289
|
+
const prev = prevById.get(String(spec.existing_id));
|
|
290
|
+
if (!prev)
|
|
291
|
+
continue;
|
|
292
|
+
if (prev.blob_key)
|
|
293
|
+
retainedBlobKeys.add(prev.blob_key);
|
|
294
|
+
const linkedEl = spec.element_client_ref !== undefined
|
|
295
|
+
? spec.element_client_ref
|
|
296
|
+
? elementsByRef.get(spec.element_client_ref) ?? null
|
|
297
|
+
: null
|
|
298
|
+
: input.element_references === undefined
|
|
299
|
+
? existing.element_references.find((e) => e.id === prev.element_reference_id) ?? null
|
|
300
|
+
: null;
|
|
301
|
+
nextAttachments.push({
|
|
302
|
+
...prev,
|
|
303
|
+
element_reference_id: linkedEl ? linkedEl.id : null,
|
|
304
|
+
metadata: spec.update_metadata && spec.metadata ? { ...spec.metadata } : { ...prev.metadata }
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
else {
|
|
308
|
+
const attId = nextAttachmentId++;
|
|
309
|
+
const linkedEl = spec.element_client_ref
|
|
310
|
+
? elementsByRef.get(spec.element_client_ref) ?? null
|
|
311
|
+
: null;
|
|
312
|
+
const blobKey = spec.data ? `att_${attId}` : null;
|
|
313
|
+
if (blobKey && spec.data) {
|
|
314
|
+
attachmentBlobs.set(blobKey, new Uint8Array(spec.data));
|
|
315
|
+
retainedBlobKeys.add(blobKey);
|
|
316
|
+
}
|
|
317
|
+
nextAttachments.push({
|
|
318
|
+
id: attId,
|
|
319
|
+
feedback_id: existing.id,
|
|
320
|
+
element_reference_id: linkedEl ? linkedEl.id : null,
|
|
321
|
+
attachment_type: spec.attachment_type,
|
|
322
|
+
filename: spec.filename,
|
|
323
|
+
content_type: spec.content_type,
|
|
324
|
+
byte_size: spec.byte_size,
|
|
325
|
+
blob_key: blobKey,
|
|
326
|
+
metadata: { ...spec.metadata },
|
|
327
|
+
created_at: now
|
|
328
|
+
});
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
for (const prev of existing.attachments) {
|
|
332
|
+
if (prev.blob_key && !retainedBlobKeys.has(prev.blob_key)) {
|
|
333
|
+
attachmentBlobs.delete(prev.blob_key);
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
existing.attachments = nextAttachments;
|
|
337
|
+
}
|
|
338
|
+
existing.updated_at = now;
|
|
339
|
+
return structuredClone(existing);
|
|
340
|
+
},
|
|
341
|
+
async softDeleteFeedback(id) {
|
|
342
|
+
const numId = Number(id);
|
|
343
|
+
const existing = feedbacks.get(numId);
|
|
344
|
+
if (!existing || existing.deleted_at)
|
|
345
|
+
return null;
|
|
346
|
+
const now = new Date().toISOString();
|
|
347
|
+
existing.deleted_at = now;
|
|
348
|
+
existing.updated_at = now;
|
|
349
|
+
return structuredClone(existing);
|
|
350
|
+
},
|
|
185
351
|
async getAttachment(attachmentId) {
|
|
186
352
|
const numAttId = Number(attachmentId);
|
|
187
353
|
for (const fb of feedbacks.values()) {
|
|
@@ -207,7 +373,7 @@ export function createMemoryStorageAdapter() {
|
|
|
207
373
|
async updateFeedbackStatus(id, status) {
|
|
208
374
|
const numId = Number(id);
|
|
209
375
|
const existing = feedbacks.get(numId);
|
|
210
|
-
if (!existing)
|
|
376
|
+
if (!existing || existing.deleted_at)
|
|
211
377
|
return null;
|
|
212
378
|
const previous_status = existing.status;
|
|
213
379
|
existing.status = status;
|
|
@@ -15,7 +15,7 @@ export interface PostgresStorageAdapterOptions {
|
|
|
15
15
|
*/
|
|
16
16
|
autoMigrate?: boolean;
|
|
17
17
|
/**
|
|
18
|
-
* Optional HMAC secret for signing pending upload blob IDs (`process.env.CONTEXT_DROP_SECRET` used if omitted).
|
|
18
|
+
* Optional HMAC secret for signing pending upload blob IDs and ownership tokens (`process.env.CONTEXT_DROP_SECRET` used if omitted).
|
|
19
19
|
*/
|
|
20
20
|
secret?: string;
|
|
21
21
|
/**
|