context_drop 0.1.4 → 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.
- checksums.yaml +4 -4
- data/README.md +330 -116
- data/Rakefile +110 -1
- data/SPEC.md +74 -26
- data/app/assets/javascripts/context_drop/context_drop.js +6329 -682
- data/app/controllers/context_drop/agent_controller.rb +170 -0
- data/app/controllers/context_drop/application_controller.rb +52 -3
- data/app/controllers/context_drop/configs_controller.rb +12 -2
- data/app/controllers/context_drop/dashboard/feedbacks_controller.rb +15 -11
- data/app/controllers/context_drop/feedbacks_controller.rb +396 -20
- data/app/controllers/context_drop/mcp_controller.rb +2 -1
- data/app/helpers/context_drop/widget_helper.rb +11 -1
- data/app/javascript/context_drop/__tests__/api-client.test.ts +255 -0
- data/app/javascript/context_drop/__tests__/widget.test.ts +1015 -89
- data/app/javascript/context_drop/api-client.ts +422 -19
- data/app/javascript/context_drop/element-picker.ts +186 -34
- data/app/javascript/context_drop/icons.ts +28 -0
- data/app/javascript/context_drop/markdown.ts +146 -0
- data/app/javascript/context_drop/sanitizer.ts +413 -2
- data/app/javascript/context_drop/screenshot.ts +103 -9
- data/app/javascript/context_drop/styles.ts +1705 -248
- data/app/javascript/context_drop/types.ts +243 -6
- data/app/javascript/context_drop/widget.ts +3318 -294
- data/app/models/context_drop/feedback.rb +88 -8
- data/app/services/context_drop/ownership_token.rb +114 -0
- data/app/views/context_drop/dashboard/feedbacks/index.html.erb +13 -0
- data/app/views/context_drop/dashboard/feedbacks/show.html.erb +19 -0
- data/app/views/layouts/context_drop/application.html.erb +6 -0
- data/config/routes.rb +22 -0
- data/db/migrate/20261112000004_add_environment_to_context_drop_feedbacks.rb +10 -0
- data/db/migrate/20261113000005_add_deleted_at_to_context_drop_feedbacks.rb +11 -0
- data/lib/context_drop/agent/key_store.rb +290 -0
- data/lib/context_drop/agent/llm_client.rb +413 -0
- data/lib/context_drop/agent/orchestrator.rb +314 -0
- data/lib/context_drop/agent/workspace_sandbox.rb +499 -0
- data/lib/context_drop/configuration.rb +242 -3
- data/lib/context_drop/mcp/dispatcher.rb +71 -29
- data/lib/context_drop/mcp/environment_router.rb +115 -0
- data/lib/context_drop/mcp/prompts/investigate_feedback.rb +90 -9
- data/lib/context_drop/mcp/remote_client.rb +120 -0
- data/lib/context_drop/mcp/server.rb +9 -2
- data/lib/context_drop/mcp/tools/get_feedback.rb +62 -5
- data/lib/context_drop/mcp/tools/get_feedback_attachment.rb +29 -4
- data/lib/context_drop/mcp/tools/list_environments.rb +60 -0
- data/lib/context_drop/mcp/tools/list_feedbacks.rb +195 -23
- data/lib/context_drop/mcp/tools/update_feedback_status.rb +48 -5
- data/lib/context_drop/version.rb +1 -1
- data/lib/context_drop.rb +8 -2
- data/lib/generators/context_drop/install/templates/initializer.rb.tt +29 -2
- metadata +14 -2
|
@@ -307,4 +307,259 @@ describe("ContextDropApiClient CSRF handling & lazy bootstrap", () => {
|
|
|
307
307
|
assert.equal(fetchCalls[2].url, "/context_drop/config");
|
|
308
308
|
assert.equal(fetchCalls[3].url, "/context_drop/attachments");
|
|
309
309
|
});
|
|
310
|
+
|
|
311
|
+
it("avoids redundant /config calls when Next.js backend returns csrf_token: null and sends X-Context-Drop-Client cross-origin", async () => {
|
|
312
|
+
mockMetaContent = null;
|
|
313
|
+
mockFetchHandler = async (url: string, _options: RequestInit) => {
|
|
314
|
+
if (url.endsWith("/config")) {
|
|
315
|
+
return new Response(
|
|
316
|
+
JSON.stringify({
|
|
317
|
+
enabled: true,
|
|
318
|
+
position: "bottom_right",
|
|
319
|
+
csrf_token: null
|
|
320
|
+
}),
|
|
321
|
+
{ status: 200, headers: { "Content-Type": "application/json" } }
|
|
322
|
+
);
|
|
323
|
+
}
|
|
324
|
+
return new Response(JSON.stringify({ id: 9, status: "new" }), {
|
|
325
|
+
status: 201,
|
|
326
|
+
headers: { "Content-Type": "application/json" }
|
|
327
|
+
});
|
|
328
|
+
};
|
|
329
|
+
|
|
330
|
+
const client = new ContextDropApiClient("https://api.acme.com/api/context-drop", {
|
|
331
|
+
credentials: "include",
|
|
332
|
+
headers: { "X-Tenant-Id": "acme" }
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
await client.submitFeedback({
|
|
336
|
+
message: "First Next.js feedback",
|
|
337
|
+
page_url: "https://app.acme.com/home",
|
|
338
|
+
user_agent: "test-agent",
|
|
339
|
+
metadata: {},
|
|
340
|
+
element_references: [],
|
|
341
|
+
attachments: []
|
|
342
|
+
});
|
|
343
|
+
|
|
344
|
+
await client.submitFeedback({
|
|
345
|
+
message: "Second Next.js feedback",
|
|
346
|
+
page_url: "https://app.acme.com/home",
|
|
347
|
+
user_agent: "test-agent",
|
|
348
|
+
metadata: {},
|
|
349
|
+
element_references: [],
|
|
350
|
+
attachments: []
|
|
351
|
+
});
|
|
352
|
+
|
|
353
|
+
// Only 1 /config bootstrap call + 2 /feedback calls (no redundant /config before second submit)
|
|
354
|
+
assert.equal(fetchCalls.length, 3);
|
|
355
|
+
assert.equal(fetchCalls[0].url, "https://api.acme.com/api/context-drop/config");
|
|
356
|
+
assert.equal(fetchCalls[1].url, "https://api.acme.com/api/context-drop/feedback");
|
|
357
|
+
assert.equal(fetchCalls[2].url, "https://api.acme.com/api/context-drop/feedback");
|
|
358
|
+
assert.equal(fetchCalls[1].options.credentials, "include");
|
|
359
|
+
const sentHeaders = fetchCalls[1].options.headers as Record<string, string>;
|
|
360
|
+
assert.equal(sentHeaders["X-Context-Drop-Client"], "1");
|
|
361
|
+
assert.equal(sentHeaders["X-Tenant-Id"], "acme");
|
|
362
|
+
});
|
|
363
|
+
});
|
|
364
|
+
|
|
365
|
+
describe("ContextDropApiClient own feedback sync, update, soft-delete & ownership tokens", () => {
|
|
366
|
+
const originalDocument = globalThis.document;
|
|
367
|
+
const originalWindow = (globalThis as Record<string, unknown>).window;
|
|
368
|
+
const originalFetch = globalThis.fetch;
|
|
369
|
+
|
|
370
|
+
let fetchCalls: Array<{ url: string; options: RequestInit }> = [];
|
|
371
|
+
let mockFetchHandler: (url: string, options: RequestInit) => Promise<Response>;
|
|
372
|
+
|
|
373
|
+
beforeEach(() => {
|
|
374
|
+
fetchCalls = [];
|
|
375
|
+
(globalThis as unknown as { document: unknown }).document = {
|
|
376
|
+
querySelector: (selector: string) =>
|
|
377
|
+
selector === 'meta[name="csrf-token"]'
|
|
378
|
+
? {
|
|
379
|
+
getAttribute: (name: string) => (name === "content" ? "csrf-tok-1" : null),
|
|
380
|
+
setAttribute: () => {}
|
|
381
|
+
}
|
|
382
|
+
: null
|
|
383
|
+
};
|
|
384
|
+
|
|
385
|
+
mockFetchHandler = async () =>
|
|
386
|
+
new Response(JSON.stringify({ ok: true }), {
|
|
387
|
+
status: 200,
|
|
388
|
+
headers: { "Content-Type": "application/json" }
|
|
389
|
+
});
|
|
390
|
+
|
|
391
|
+
globalThis.fetch = async (input: RequestInfo | URL, init?: RequestInit) => {
|
|
392
|
+
const url = typeof input === "string" ? input : input.toString();
|
|
393
|
+
const options = init || {};
|
|
394
|
+
fetchCalls.push({ url, options });
|
|
395
|
+
return mockFetchHandler(url, options);
|
|
396
|
+
};
|
|
397
|
+
});
|
|
398
|
+
|
|
399
|
+
afterEach(() => {
|
|
400
|
+
(globalThis as unknown as { document: unknown }).document = originalDocument;
|
|
401
|
+
if (originalWindow === undefined) {
|
|
402
|
+
delete (globalThis as Record<string, unknown>).window;
|
|
403
|
+
} else {
|
|
404
|
+
(globalThis as Record<string, unknown>).window = originalWindow;
|
|
405
|
+
}
|
|
406
|
+
globalThis.fetch = originalFetch;
|
|
407
|
+
});
|
|
408
|
+
|
|
409
|
+
it("persists ownership_token in window.localStorage and falls back cleanly to memory when unavailable", async () => {
|
|
410
|
+
const storeData = new Map<string, string>();
|
|
411
|
+
(globalThis as Record<string, unknown>).window = {
|
|
412
|
+
location: { origin: "http://localhost:3000" },
|
|
413
|
+
localStorage: {
|
|
414
|
+
getItem: (k: string) => storeData.get(k) ?? null,
|
|
415
|
+
setItem: (k: string, v: string) => {
|
|
416
|
+
storeData.set(k, v);
|
|
417
|
+
},
|
|
418
|
+
removeItem: (k: string) => {
|
|
419
|
+
storeData.delete(k);
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
};
|
|
423
|
+
|
|
424
|
+
mockFetchHandler = async () =>
|
|
425
|
+
new Response(
|
|
426
|
+
JSON.stringify({
|
|
427
|
+
id: 77,
|
|
428
|
+
status: "new",
|
|
429
|
+
ownership_token: "77.nonce1.sig1",
|
|
430
|
+
created_at: "2026-11-13T00:00:00Z"
|
|
431
|
+
}),
|
|
432
|
+
{ status: 201, headers: { "Content-Type": "application/json" } }
|
|
433
|
+
);
|
|
434
|
+
|
|
435
|
+
const clientWithStorage = new ContextDropApiClient("/context_drop");
|
|
436
|
+
await clientWithStorage.submitFeedback({
|
|
437
|
+
message: "Stored token test",
|
|
438
|
+
page_url: "http://localhost:3000/app",
|
|
439
|
+
user_agent: "node-test",
|
|
440
|
+
metadata: {},
|
|
441
|
+
element_references: [],
|
|
442
|
+
attachments: []
|
|
443
|
+
});
|
|
444
|
+
|
|
445
|
+
assert.equal(clientWithStorage.getOwnershipToken(77), "77.nonce1.sig1");
|
|
446
|
+
assert.ok(storeData.get("context_drop_ownership_tokens")?.includes("77.nonce1.sig1"));
|
|
447
|
+
|
|
448
|
+
// Second client sharing localStorage reads persisted token across page reloads
|
|
449
|
+
const reloadedClient = new ContextDropApiClient("/context_drop");
|
|
450
|
+
assert.equal(reloadedClient.getOwnershipToken(77), "77.nonce1.sig1");
|
|
451
|
+
assert.equal(
|
|
452
|
+
reloadedClient.buildAttachmentPreviewUrl(77, 9),
|
|
453
|
+
"/context_drop/feedback/77/attachments/9?ownership_token=77.nonce1.sig1"
|
|
454
|
+
);
|
|
455
|
+
|
|
456
|
+
// Without window.localStorage, falls back to in-memory map
|
|
457
|
+
delete (globalThis as Record<string, unknown>).window;
|
|
458
|
+
const memoryClient = new ContextDropApiClient("/context_drop");
|
|
459
|
+
memoryClient.setOwnershipToken(88, "88.nonce2.sig2");
|
|
460
|
+
assert.equal(memoryClient.getOwnershipToken(88), "88.nonce2.sig2");
|
|
461
|
+
assert.equal(
|
|
462
|
+
memoryClient.buildAttachmentPreviewUrl(99, 3),
|
|
463
|
+
"/context_drop/feedback/99/attachments/3"
|
|
464
|
+
);
|
|
465
|
+
});
|
|
466
|
+
|
|
467
|
+
it("sends X-Context-Drop-Ownership-Tokens in fetchOwnFeedbacks, retries CSRF on updateFeedback, and clears token on deleteFeedback", async () => {
|
|
468
|
+
delete (globalThis as Record<string, unknown>).window;
|
|
469
|
+
const client = new ContextDropApiClient("/context_drop");
|
|
470
|
+
client.setOwnershipToken(12, "12.n1.s1");
|
|
471
|
+
|
|
472
|
+
let patchAttempts = 0;
|
|
473
|
+
mockFetchHandler = async (url: string, options: RequestInit) => {
|
|
474
|
+
const method = (options.method || "GET").toUpperCase();
|
|
475
|
+
if (url.startsWith("/context_drop/feedback?") && method === "GET") {
|
|
476
|
+
return new Response(
|
|
477
|
+
JSON.stringify({
|
|
478
|
+
total_count: 1,
|
|
479
|
+
feedbacks: [
|
|
480
|
+
{
|
|
481
|
+
id: 12,
|
|
482
|
+
status: "new",
|
|
483
|
+
message: "Existing feedback",
|
|
484
|
+
page_url: "http://localhost:3000/orders",
|
|
485
|
+
created_at: "2026-11-13T01:00:00Z",
|
|
486
|
+
updated_at: "2026-11-13T01:00:00Z",
|
|
487
|
+
ownership_token: "12.n1.rotated",
|
|
488
|
+
element_references: [],
|
|
489
|
+
attachments: []
|
|
490
|
+
}
|
|
491
|
+
]
|
|
492
|
+
}),
|
|
493
|
+
{ status: 200, headers: { "Content-Type": "application/json" } }
|
|
494
|
+
);
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
if (url.endsWith("/config") && method === "GET") {
|
|
498
|
+
return new Response(
|
|
499
|
+
JSON.stringify({ enabled: true, csrf_token: "fresh-csrf-2" }),
|
|
500
|
+
{ status: 200, headers: { "Content-Type": "application/json" } }
|
|
501
|
+
);
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
if (url === "/context_drop/feedback/12" && method === "PATCH") {
|
|
505
|
+
patchAttempts += 1;
|
|
506
|
+
if (patchAttempts === 1) {
|
|
507
|
+
return new Response(
|
|
508
|
+
JSON.stringify({ error: "Invalid CSRF authenticity token" }),
|
|
509
|
+
{ status: 422, headers: { "Content-Type": "application/json" } }
|
|
510
|
+
);
|
|
511
|
+
}
|
|
512
|
+
return new Response(
|
|
513
|
+
JSON.stringify({
|
|
514
|
+
id: 12,
|
|
515
|
+
status: "new",
|
|
516
|
+
attachments_count: 0,
|
|
517
|
+
element_references_count: 1,
|
|
518
|
+
ownership_token: "12.n1.rotated",
|
|
519
|
+
updated_at: "2026-11-13T02:00:00Z"
|
|
520
|
+
}),
|
|
521
|
+
{ status: 200, headers: { "Content-Type": "application/json" } }
|
|
522
|
+
);
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
if (url === "/context_drop/feedback/12" && method === "DELETE") {
|
|
526
|
+
return new Response(
|
|
527
|
+
JSON.stringify({
|
|
528
|
+
id: 12,
|
|
529
|
+
deleted: true,
|
|
530
|
+
deleted_at: "2026-11-13T03:00:00Z"
|
|
531
|
+
}),
|
|
532
|
+
{ status: 200, headers: { "Content-Type": "application/json" } }
|
|
533
|
+
);
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
throw new Error(`Unexpected request: ${method} ${url}`);
|
|
537
|
+
};
|
|
538
|
+
|
|
539
|
+
const own = await client.fetchOwnFeedbacks(20);
|
|
540
|
+
assert.equal(own.total_count, 1);
|
|
541
|
+
assert.equal(
|
|
542
|
+
(fetchCalls[0].options.headers as Record<string, string>)["X-Context-Drop-Ownership-Tokens"],
|
|
543
|
+
"12.n1.s1"
|
|
544
|
+
);
|
|
545
|
+
assert.equal(client.getOwnershipToken(12), "12.n1.rotated");
|
|
546
|
+
|
|
547
|
+
const updated = await client.updateFeedback(12, {
|
|
548
|
+
message: "Edited feedback text",
|
|
549
|
+
element_references: [],
|
|
550
|
+
attachments: []
|
|
551
|
+
});
|
|
552
|
+
assert.equal(updated.id, 12);
|
|
553
|
+
assert.equal(patchAttempts, 2);
|
|
554
|
+
const patchHeaders = fetchCalls[3].options.headers as Record<string, string>;
|
|
555
|
+
assert.equal(patchHeaders["X-Context-Drop-Ownership-Token"], "12.n1.rotated");
|
|
556
|
+
const patchBody = JSON.parse(String(fetchCalls[3].options.body)) as Record<string, unknown>;
|
|
557
|
+
assert.equal(patchBody.ownership_token, "12.n1.rotated");
|
|
558
|
+
|
|
559
|
+
const deleted = await client.deleteFeedback(12);
|
|
560
|
+
assert.equal(deleted.deleted, true);
|
|
561
|
+
const deleteHeaders = fetchCalls[4].options.headers as Record<string, string>;
|
|
562
|
+
assert.equal(deleteHeaders["X-Context-Drop-Ownership-Token"], "12.n1.rotated");
|
|
563
|
+
assert.equal(client.getOwnershipToken(12), null);
|
|
564
|
+
});
|
|
310
565
|
});
|