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.
@@ -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
- return {
49
+ const summary = {
18
50
  id: record.id,
19
51
  qualified_id: `${record.environment}:${record.id}`,
20
52
  environment: record.environment,
@@ -24,11 +56,20 @@ export function toFeedbackSummary(record) {
24
56
  submitter: formatSubmitterLabel(record.submitter),
25
57
  attachments_count: record.attachments.length,
26
58
  element_references_count: record.element_references.length,
59
+ comments_count: record.comments?.length ?? 0,
27
60
  element_selectors: record.element_references.map((el) => el.selector),
28
61
  created_at: record.created_at
29
62
  };
63
+ if (record.deleted_at) {
64
+ summary.deleted = true;
65
+ summary.deleted_at = record.deleted_at;
66
+ }
67
+ return summary;
30
68
  }
31
69
  export function matchesFeedbackFilter(record, filter) {
70
+ if (!filter.include_deleted && record.deleted_at) {
71
+ return false;
72
+ }
32
73
  if (filter.environment && filter.environment.trim().length > 0) {
33
74
  if (record.environment.toLowerCase() !== filter.environment.trim().toLowerCase()) {
34
75
  return false;
@@ -59,6 +100,7 @@ export function createMemoryStorageAdapter() {
59
100
  let nextFeedbackId = 1;
60
101
  let nextElementId = 1;
61
102
  let nextAttachmentId = 1;
103
+ let nextCommentId = 1;
62
104
  const secret = crypto.randomBytes(32);
63
105
  const pendingBlobs = new Map();
64
106
  const attachmentBlobs = new Map();
@@ -82,6 +124,9 @@ export function createMemoryStorageAdapter() {
82
124
  return rawId;
83
125
  }
84
126
  return {
127
+ getOwnershipSecret() {
128
+ return secret;
129
+ },
85
130
  async savePendingBlob(blob) {
86
131
  const rawId = crypto.randomUUID();
87
132
  pendingBlobs.set(rawId, {
@@ -157,8 +202,10 @@ export function createMemoryStorageAdapter() {
157
202
  metadata: { ...input.metadata },
158
203
  element_references: storedElements,
159
204
  attachments: storedAttachments,
205
+ comments: [],
160
206
  created_at: now,
161
- updated_at: now
207
+ updated_at: now,
208
+ deleted_at: null
162
209
  };
163
210
  feedbacks.set(id, record);
164
211
  return structuredClone(record);
@@ -177,11 +224,143 @@ export function createMemoryStorageAdapter() {
177
224
  records: matching.slice(offset, offset + limit).map(toFeedbackSummary)
178
225
  };
179
226
  },
227
+ async listOwnFeedbacks(query) {
228
+ const limit = Math.max(1, Math.min(50, query.limit ?? 20));
229
+ const matching = Array.from(feedbacks.values())
230
+ .filter((fb) => matchesOwnFeedback(fb, query))
231
+ .sort((a, b) => {
232
+ const dt = Date.parse(b.created_at) - Date.parse(a.created_at);
233
+ return dt !== 0 ? dt : Number(b.id) - Number(a.id);
234
+ });
235
+ return {
236
+ total: matching.length,
237
+ records: matching.slice(0, limit).map((fb) => structuredClone(fb))
238
+ };
239
+ },
180
240
  async getFeedback(id) {
181
241
  const numId = Number(id);
182
242
  const found = feedbacks.get(numId);
183
243
  return found ? structuredClone(found) : null;
184
244
  },
245
+ async updateFeedback(id, input) {
246
+ const numId = Number(id);
247
+ const existing = feedbacks.get(numId);
248
+ if (!existing || existing.deleted_at)
249
+ return null;
250
+ const now = new Date().toISOString();
251
+ const contentEdited = input.message !== undefined ||
252
+ input.metadata !== undefined ||
253
+ input.element_references !== undefined ||
254
+ input.attachments !== undefined;
255
+ if (input.message !== undefined) {
256
+ existing.message = input.message;
257
+ }
258
+ if (input.metadata !== undefined) {
259
+ existing.metadata = { ...input.metadata };
260
+ }
261
+ if (input.status !== undefined) {
262
+ existing.status = input.status;
263
+ }
264
+ else if (contentEdited) {
265
+ existing.status = "updated";
266
+ }
267
+ const elementsByRef = new Map();
268
+ if (input.element_references !== undefined) {
269
+ existing.element_references = input.element_references.map((el, idx) => {
270
+ const elRec = {
271
+ id: nextElementId++,
272
+ feedback_id: existing.id,
273
+ client_ref: el.client_ref,
274
+ tag_name: el.tag_name,
275
+ selector: el.selector,
276
+ dom_path: el.dom_path,
277
+ text_content: el.text_content,
278
+ attributes_snapshot: { ...el.attributes_snapshot },
279
+ bounding_rect: { ...el.bounding_rect },
280
+ viewport: { ...el.viewport },
281
+ metadata: { ...el.metadata },
282
+ captured_at: el.captured_at
283
+ };
284
+ elementsByRef.set(el.client_ref || String(idx), elRec);
285
+ return elRec;
286
+ });
287
+ }
288
+ else {
289
+ existing.element_references.forEach((el, idx) => {
290
+ elementsByRef.set(el.client_ref || String(idx), el);
291
+ });
292
+ }
293
+ if (input.attachments !== undefined) {
294
+ const prevById = new Map();
295
+ for (const a of existing.attachments) {
296
+ prevById.set(String(a.id), a);
297
+ }
298
+ const nextAttachments = [];
299
+ const retainedBlobKeys = new Set();
300
+ for (const spec of input.attachments) {
301
+ if (spec.kind === "retain") {
302
+ const prev = prevById.get(String(spec.existing_id));
303
+ if (!prev)
304
+ continue;
305
+ if (prev.blob_key)
306
+ retainedBlobKeys.add(prev.blob_key);
307
+ const linkedEl = spec.element_client_ref !== undefined
308
+ ? spec.element_client_ref
309
+ ? elementsByRef.get(spec.element_client_ref) ?? null
310
+ : null
311
+ : input.element_references === undefined
312
+ ? existing.element_references.find((e) => e.id === prev.element_reference_id) ?? null
313
+ : null;
314
+ nextAttachments.push({
315
+ ...prev,
316
+ element_reference_id: linkedEl ? linkedEl.id : null,
317
+ metadata: spec.update_metadata && spec.metadata ? { ...spec.metadata } : { ...prev.metadata }
318
+ });
319
+ }
320
+ else {
321
+ const attId = nextAttachmentId++;
322
+ const linkedEl = spec.element_client_ref
323
+ ? elementsByRef.get(spec.element_client_ref) ?? null
324
+ : null;
325
+ const blobKey = spec.data ? `att_${attId}` : null;
326
+ if (blobKey && spec.data) {
327
+ attachmentBlobs.set(blobKey, new Uint8Array(spec.data));
328
+ retainedBlobKeys.add(blobKey);
329
+ }
330
+ nextAttachments.push({
331
+ id: attId,
332
+ feedback_id: existing.id,
333
+ element_reference_id: linkedEl ? linkedEl.id : null,
334
+ attachment_type: spec.attachment_type,
335
+ filename: spec.filename,
336
+ content_type: spec.content_type,
337
+ byte_size: spec.byte_size,
338
+ blob_key: blobKey,
339
+ metadata: { ...spec.metadata },
340
+ created_at: now
341
+ });
342
+ }
343
+ }
344
+ for (const prev of existing.attachments) {
345
+ if (prev.blob_key && !retainedBlobKeys.has(prev.blob_key)) {
346
+ attachmentBlobs.delete(prev.blob_key);
347
+ }
348
+ }
349
+ existing.attachments = nextAttachments;
350
+ }
351
+ existing.updated_at = now;
352
+ return structuredClone(existing);
353
+ },
354
+ async softDeleteFeedback(id) {
355
+ const numId = Number(id);
356
+ const existing = feedbacks.get(numId);
357
+ if (!existing || existing.deleted_at)
358
+ return null;
359
+ const now = new Date().toISOString();
360
+ existing.deleted_at = now;
361
+ existing.updated_at = now;
362
+ return structuredClone(existing);
363
+ },
185
364
  async getAttachment(attachmentId) {
186
365
  const numAttId = Number(attachmentId);
187
366
  for (const fb of feedbacks.values()) {
@@ -197,6 +376,17 @@ export function createMemoryStorageAdapter() {
197
376
  data: bytes ? new Uint8Array(bytes) : null
198
377
  };
199
378
  }
379
+ for (const comment of fb.comments ?? []) {
380
+ const commentAtt = comment.attachments.find((a) => Number(a.id) === numAttId);
381
+ if (commentAtt) {
382
+ const bytes = commentAtt.blob_key ? attachmentBlobs.get(commentAtt.blob_key) ?? null : null;
383
+ return {
384
+ attachment: structuredClone(commentAtt),
385
+ element_selector: null,
386
+ data: bytes ? new Uint8Array(bytes) : null
387
+ };
388
+ }
389
+ }
200
390
  }
201
391
  return null;
202
392
  },
@@ -204,10 +394,58 @@ export function createMemoryStorageAdapter() {
204
394
  const res = await this.getAttachment(attachmentId);
205
395
  return res?.data ?? null;
206
396
  },
397
+ async addComment(feedbackId, input) {
398
+ const numId = Number(feedbackId);
399
+ const existing = feedbacks.get(numId);
400
+ if (!existing || existing.deleted_at)
401
+ return null;
402
+ const now = new Date().toISOString();
403
+ const commentId = nextCommentId++;
404
+ const commentAttachments = [];
405
+ for (const att of input.attachments ?? []) {
406
+ const attId = nextAttachmentId++;
407
+ const blobKey = att.data ? `att_${attId}` : null;
408
+ if (blobKey && att.data) {
409
+ attachmentBlobs.set(blobKey, new Uint8Array(att.data));
410
+ }
411
+ commentAttachments.push({
412
+ id: attId,
413
+ feedback_id: existing.id,
414
+ comment_id: commentId,
415
+ element_reference_id: null,
416
+ attachment_type: att.attachment_type,
417
+ filename: att.filename,
418
+ content_type: att.content_type,
419
+ byte_size: att.byte_size,
420
+ blob_key: blobKey,
421
+ metadata: { ...att.metadata },
422
+ created_at: now
423
+ });
424
+ }
425
+ const comment = {
426
+ id: commentId,
427
+ feedback_id: existing.id,
428
+ author_type: input.author_type ?? "User",
429
+ author_label: input.author_label || "You",
430
+ body: input.body,
431
+ attachments: commentAttachments,
432
+ created_at: now,
433
+ updated_at: now
434
+ };
435
+ if (!existing.comments)
436
+ existing.comments = [];
437
+ existing.comments.push(comment);
438
+ existing.status = input.status ?? "updated";
439
+ existing.updated_at = now;
440
+ return {
441
+ comment: structuredClone(comment),
442
+ feedback: structuredClone(existing)
443
+ };
444
+ },
207
445
  async updateFeedbackStatus(id, status) {
208
446
  const numId = Number(id);
209
447
  const existing = feedbacks.get(numId);
210
- if (!existing)
448
+ if (!existing || existing.deleted_at)
211
449
  return null;
212
450
  const previous_status = existing.status;
213
451
  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
  /**