context-drop 0.0.0-stage → 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.
Files changed (60) hide show
  1. package/MIT-LICENSE +21 -0
  2. package/README.md +710 -2
  3. package/app/assets/javascripts/context_drop/context_drop.js +9949 -0
  4. package/dist/app/javascript/context_drop/api-client.d.ts +53 -0
  5. package/dist/app/javascript/context_drop/api-client.js +470 -0
  6. package/dist/app/javascript/context_drop/element-picker.d.ts +111 -0
  7. package/dist/app/javascript/context_drop/element-picker.js +1428 -0
  8. package/dist/app/javascript/context_drop/icons.d.ts +34 -0
  9. package/dist/app/javascript/context_drop/icons.js +34 -0
  10. package/dist/app/javascript/context_drop/index.d.ts +7 -0
  11. package/dist/app/javascript/context_drop/index.js +9 -0
  12. package/dist/app/javascript/context_drop/markdown.d.ts +7 -0
  13. package/dist/app/javascript/context_drop/markdown.js +129 -0
  14. package/dist/app/javascript/context_drop/sanitizer.d.ts +38 -0
  15. package/dist/app/javascript/context_drop/sanitizer.js +769 -0
  16. package/dist/app/javascript/context_drop/screenshot.d.ts +21 -0
  17. package/dist/app/javascript/context_drop/screenshot.js +245 -0
  18. package/dist/app/javascript/context_drop/styles.d.ts +1 -0
  19. package/dist/app/javascript/context_drop/styles.js +2196 -0
  20. package/dist/app/javascript/context_drop/types.d.ts +316 -0
  21. package/dist/app/javascript/context_drop/types.js +1 -0
  22. package/dist/app/javascript/context_drop/widget.d.ts +206 -0
  23. package/dist/app/javascript/context_drop/widget.js +3752 -0
  24. package/dist/src/cli.d.ts +3 -0
  25. package/dist/src/cli.js +207 -0
  26. package/dist/src/index.d.ts +5 -0
  27. package/dist/src/index.js +5 -0
  28. package/dist/src/mcp/dispatcher.d.ts +42 -0
  29. package/dist/src/mcp/dispatcher.js +895 -0
  30. package/dist/src/mcp/environment-router.d.ts +38 -0
  31. package/dist/src/mcp/environment-router.js +174 -0
  32. package/dist/src/mcp/index.d.ts +24 -0
  33. package/dist/src/mcp/index.js +75 -0
  34. package/dist/src/mcp/remote-client.d.ts +25 -0
  35. package/dist/src/mcp/remote-client.js +98 -0
  36. package/dist/src/next/index.d.ts +13 -0
  37. package/dist/src/next/index.js +25 -0
  38. package/dist/src/next/server.d.ts +1 -0
  39. package/dist/src/next/server.js +1 -0
  40. package/dist/src/react/index.d.ts +104 -0
  41. package/dist/src/react/index.js +144 -0
  42. package/dist/src/server/handler.d.ts +48 -0
  43. package/dist/src/server/handler.js +1180 -0
  44. package/dist/src/server/index.d.ts +8 -0
  45. package/dist/src/server/index.js +8 -0
  46. package/dist/src/server/sanitizer.d.ts +15 -0
  47. package/dist/src/server/sanitizer.js +201 -0
  48. package/dist/src/server/storage/local.d.ts +22 -0
  49. package/dist/src/server/storage/local.js +827 -0
  50. package/dist/src/server/storage/memory.d.ts +8 -0
  51. package/dist/src/server/storage/memory.js +387 -0
  52. package/dist/src/server/storage/postgres.d.ts +27 -0
  53. package/dist/src/server/storage/postgres.js +629 -0
  54. package/dist/src/server/types.d.ts +227 -0
  55. package/dist/src/server/types.js +7 -0
  56. package/dist/src/server/upload-validator.d.ts +16 -0
  57. package/dist/src/server/upload-validator.js +196 -0
  58. package/dist/src/widget/index.d.ts +1 -0
  59. package/dist/src/widget/index.js +1 -0
  60. package/package.json +99 -4
@@ -0,0 +1,8 @@
1
+ import type { FeedbackQueryFilter, FeedbackSummaryItem, OwnFeedbackLookupQuery, StorageAdapter, StoredFeedbackRecord, SubmitterIdentity } from "../types.js";
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;
6
+ export declare function toFeedbackSummary(record: StoredFeedbackRecord): FeedbackSummaryItem;
7
+ export declare function matchesFeedbackFilter(record: StoredFeedbackRecord, filter: FeedbackQueryFilter): boolean;
8
+ export declare function createMemoryStorageAdapter(): StorageAdapter;
@@ -0,0 +1,387 @@
1
+ import crypto from "node:crypto";
2
+ export function formatSubmitterLabel(submitter) {
3
+ if (!submitter)
4
+ return "Anonymous";
5
+ if (submitter.label && String(submitter.label).trim().length > 0) {
6
+ return String(submitter.label).trim();
7
+ }
8
+ if (submitter.type && submitter.id !== undefined && submitter.id !== null) {
9
+ return `${submitter.type}#${submitter.id}`;
10
+ }
11
+ if (submitter.id !== undefined && submitter.id !== null) {
12
+ return `User#${submitter.id}`;
13
+ }
14
+ return "Anonymous";
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
+ }
48
+ export function toFeedbackSummary(record) {
49
+ const summary = {
50
+ id: record.id,
51
+ qualified_id: `${record.environment}:${record.id}`,
52
+ environment: record.environment,
53
+ status: record.status,
54
+ message: record.message,
55
+ page_url: record.page_url,
56
+ submitter: formatSubmitterLabel(record.submitter),
57
+ attachments_count: record.attachments.length,
58
+ element_references_count: record.element_references.length,
59
+ element_selectors: record.element_references.map((el) => el.selector),
60
+ created_at: record.created_at
61
+ };
62
+ if (record.deleted_at) {
63
+ summary.deleted = true;
64
+ summary.deleted_at = record.deleted_at;
65
+ }
66
+ return summary;
67
+ }
68
+ export function matchesFeedbackFilter(record, filter) {
69
+ if (!filter.include_deleted && record.deleted_at) {
70
+ return false;
71
+ }
72
+ if (filter.environment && filter.environment.trim().length > 0) {
73
+ if (record.environment.toLowerCase() !== filter.environment.trim().toLowerCase()) {
74
+ return false;
75
+ }
76
+ }
77
+ if (filter.status && record.status !== filter.status) {
78
+ return false;
79
+ }
80
+ if (filter.page_url && filter.page_url.trim().length > 0) {
81
+ const needle = filter.page_url.trim().toLowerCase();
82
+ if (!record.page_url.toLowerCase().includes(needle)) {
83
+ return false;
84
+ }
85
+ }
86
+ if (filter.query && filter.query.trim().length > 0) {
87
+ const q = filter.query.trim().toLowerCase();
88
+ const inMessage = (record.message ?? "").toLowerCase().includes(q);
89
+ const inElements = record.element_references.some((el) => el.selector.toLowerCase().includes(q) ||
90
+ (el.dom_path ?? "").toLowerCase().includes(q) ||
91
+ (el.text_content ?? "").toLowerCase().includes(q));
92
+ if (!inMessage && !inElements) {
93
+ return false;
94
+ }
95
+ }
96
+ return true;
97
+ }
98
+ export function createMemoryStorageAdapter() {
99
+ let nextFeedbackId = 1;
100
+ let nextElementId = 1;
101
+ let nextAttachmentId = 1;
102
+ const secret = crypto.randomBytes(32);
103
+ const pendingBlobs = new Map();
104
+ const attachmentBlobs = new Map();
105
+ const feedbacks = new Map();
106
+ function signId(rawId) {
107
+ const sig = crypto.createHmac("sha256", secret).update(rawId).digest("hex");
108
+ return `${rawId}.${sig}`;
109
+ }
110
+ function verifySignedId(token) {
111
+ const dotIdx = token.lastIndexOf(".");
112
+ if (dotIdx <= 0)
113
+ return null;
114
+ const rawId = token.slice(0, dotIdx);
115
+ const provided = token.slice(dotIdx + 1);
116
+ const expected = crypto.createHmac("sha256", secret).update(rawId).digest("hex");
117
+ if (provided.length !== expected.length)
118
+ return null;
119
+ if (!crypto.timingSafeEqual(Buffer.from(provided), Buffer.from(expected))) {
120
+ return null;
121
+ }
122
+ return rawId;
123
+ }
124
+ return {
125
+ getOwnershipSecret() {
126
+ return secret;
127
+ },
128
+ async savePendingBlob(blob) {
129
+ const rawId = crypto.randomUUID();
130
+ pendingBlobs.set(rawId, {
131
+ ...blob,
132
+ data: new Uint8Array(blob.data)
133
+ });
134
+ return { signed_blob_id: signId(rawId) };
135
+ },
136
+ async consumePendingBlob(signedBlobId) {
137
+ const rawId = verifySignedId(signedBlobId);
138
+ if (!rawId)
139
+ return null;
140
+ const blob = pendingBlobs.get(rawId) ?? null;
141
+ if (blob) {
142
+ pendingBlobs.delete(rawId);
143
+ }
144
+ return blob;
145
+ },
146
+ async createFeedback(input) {
147
+ const id = nextFeedbackId++;
148
+ const now = new Date().toISOString();
149
+ const elementsByClientRef = new Map();
150
+ const storedElements = input.element_references.map((el, idx) => {
151
+ const elRecord = {
152
+ id: nextElementId++,
153
+ feedback_id: id,
154
+ client_ref: el.client_ref,
155
+ tag_name: el.tag_name,
156
+ selector: el.selector,
157
+ dom_path: el.dom_path,
158
+ text_content: el.text_content,
159
+ attributes_snapshot: { ...el.attributes_snapshot },
160
+ bounding_rect: { ...el.bounding_rect },
161
+ viewport: { ...el.viewport },
162
+ metadata: { ...el.metadata },
163
+ captured_at: el.captured_at
164
+ };
165
+ const refKey = el.client_ref || String(idx);
166
+ elementsByClientRef.set(refKey, elRecord);
167
+ return elRecord;
168
+ });
169
+ const storedAttachments = input.attachments.map((att) => {
170
+ const attId = nextAttachmentId++;
171
+ const linkedEl = att.element_client_ref
172
+ ? elementsByClientRef.get(att.element_client_ref) ?? null
173
+ : null;
174
+ const blobKey = att.data ? `att_${attId}` : null;
175
+ if (blobKey && att.data) {
176
+ attachmentBlobs.set(blobKey, new Uint8Array(att.data));
177
+ }
178
+ return {
179
+ id: attId,
180
+ feedback_id: id,
181
+ element_reference_id: linkedEl ? linkedEl.id : null,
182
+ attachment_type: att.attachment_type,
183
+ filename: att.filename,
184
+ content_type: att.content_type,
185
+ byte_size: att.byte_size,
186
+ blob_key: blobKey,
187
+ metadata: { ...att.metadata },
188
+ created_at: now
189
+ };
190
+ });
191
+ const record = {
192
+ id,
193
+ environment: input.environment,
194
+ status: "new",
195
+ message: input.message,
196
+ page_url: input.page_url,
197
+ user_agent: input.user_agent,
198
+ session_id: input.session_id,
199
+ submitter: { ...input.submitter },
200
+ metadata: { ...input.metadata },
201
+ element_references: storedElements,
202
+ attachments: storedAttachments,
203
+ created_at: now,
204
+ updated_at: now,
205
+ deleted_at: null
206
+ };
207
+ feedbacks.set(id, record);
208
+ return structuredClone(record);
209
+ },
210
+ async listFeedbacks(filter) {
211
+ const limit = Math.max(1, Math.min(100, filter.limit ?? 20));
212
+ const offset = Math.max(0, filter.offset ?? 0);
213
+ const matching = Array.from(feedbacks.values())
214
+ .filter((fb) => matchesFeedbackFilter(fb, filter))
215
+ .sort((a, b) => {
216
+ const dt = Date.parse(b.created_at) - Date.parse(a.created_at);
217
+ return dt !== 0 ? dt : Number(b.id) - Number(a.id);
218
+ });
219
+ return {
220
+ total: matching.length,
221
+ records: matching.slice(offset, offset + limit).map(toFeedbackSummary)
222
+ };
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
+ },
237
+ async getFeedback(id) {
238
+ const numId = Number(id);
239
+ const found = feedbacks.get(numId);
240
+ return found ? structuredClone(found) : null;
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
+ },
351
+ async getAttachment(attachmentId) {
352
+ const numAttId = Number(attachmentId);
353
+ for (const fb of feedbacks.values()) {
354
+ const att = fb.attachments.find((a) => Number(a.id) === numAttId);
355
+ if (att) {
356
+ const linkedEl = att.element_reference_id !== null
357
+ ? fb.element_references.find((e) => e.id === att.element_reference_id)
358
+ : undefined;
359
+ const bytes = att.blob_key ? attachmentBlobs.get(att.blob_key) ?? null : null;
360
+ return {
361
+ attachment: structuredClone(att),
362
+ element_selector: linkedEl?.selector ?? null,
363
+ data: bytes ? new Uint8Array(bytes) : null
364
+ };
365
+ }
366
+ }
367
+ return null;
368
+ },
369
+ async getAttachmentBytes(attachmentId) {
370
+ const res = await this.getAttachment(attachmentId);
371
+ return res?.data ?? null;
372
+ },
373
+ async updateFeedbackStatus(id, status) {
374
+ const numId = Number(id);
375
+ const existing = feedbacks.get(numId);
376
+ if (!existing || existing.deleted_at)
377
+ return null;
378
+ const previous_status = existing.status;
379
+ existing.status = status;
380
+ existing.updated_at = new Date().toISOString();
381
+ return {
382
+ previous_status,
383
+ feedback: structuredClone(existing)
384
+ };
385
+ }
386
+ };
387
+ }
@@ -0,0 +1,27 @@
1
+ import type { BlobStorageAdapter, StorageAdapter } from "../types.js";
2
+ export interface PostgresQueryResult<Row = Record<string, unknown>> {
3
+ rows: Row[];
4
+ }
5
+ export interface PostgresQueryable {
6
+ query(text: string, values?: unknown[]): Promise<PostgresQueryResult>;
7
+ }
8
+ export interface PostgresStorageAdapterOptions {
9
+ /**
10
+ * Any PostgreSQL client or pool exposing `query(sql, values)` (`pg.Pool`, `@vercel/postgres`, `@neondatabase/serverless`, etc.).
11
+ */
12
+ pool: PostgresQueryable;
13
+ /**
14
+ * Automatically create `context_drop_*` tables on first query if they don't exist (default `true`).
15
+ */
16
+ autoMigrate?: boolean;
17
+ /**
18
+ * Optional HMAC secret for signing pending upload blob IDs and ownership tokens (`process.env.CONTEXT_DROP_SECRET` used if omitted).
19
+ */
20
+ secret?: string;
21
+ /**
22
+ * Optional external blob store (e.g. S3, R2, Vercel Blob, Supabase Storage).
23
+ * When omitted, binary attachments are stored directly in PostgreSQL `BYTEA`.
24
+ */
25
+ blobStore?: BlobStorageAdapter;
26
+ }
27
+ export declare function createPostgresStorageAdapter(options: PostgresStorageAdapterOptions): StorageAdapter;