context_drop 0.3.3 → 0.3.5

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 (34) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +74 -1
  3. data/app/assets/javascripts/context_drop/context_drop.js +152 -11
  4. data/app/controllers/context_drop/application_controller.rb +12 -0
  5. data/app/controllers/context_drop/dashboard/feedbacks_controller.rb +38 -0
  6. data/app/controllers/context_drop/dashboard/integrations_controller.rb +140 -0
  7. data/app/controllers/context_drop/feedbacks_controller.rb +38 -14
  8. data/app/controllers/context_drop/mcp_events_controller.rb +66 -0
  9. data/app/helpers/context_drop/dashboard_helper.rb +6 -0
  10. data/app/javascript/context_drop/__tests__/widget.test.ts +229 -18
  11. data/app/javascript/context_drop/styles.ts +16 -0
  12. data/app/javascript/context_drop/widget.ts +136 -11
  13. data/app/models/context_drop/event_occurrence.rb +34 -0
  14. data/app/models/context_drop/mcp_event_subscription.rb +25 -0
  15. data/app/models/context_drop/notification_destination.rb +84 -0
  16. data/app/views/context_drop/dashboard/feedbacks/show.html.erb +8 -10
  17. data/app/views/context_drop/dashboard/integrations/_form.html.erb +92 -0
  18. data/app/views/context_drop/dashboard/integrations/edit.html.erb +13 -0
  19. data/app/views/context_drop/dashboard/integrations/index.html.erb +101 -0
  20. data/app/views/context_drop/dashboard/integrations/new.html.erb +13 -0
  21. data/app/views/layouts/context_drop/application.html.erb +78 -0
  22. data/config/routes.rb +5 -0
  23. data/db/migrate/20261124000001_create_context_drop_events_and_notifications.rb +47 -0
  24. data/lib/context_drop/feedback_events/publisher.rb +100 -0
  25. data/lib/context_drop/mcp/dispatcher.rb +20 -5
  26. data/lib/context_drop/mcp/event_stream_registry.rb +34 -0
  27. data/lib/context_drop/mcp/events.rb +178 -0
  28. data/lib/context_drop/notification_secrets.rb +85 -0
  29. data/lib/context_drop/notifications/deliverer.rb +49 -0
  30. data/lib/context_drop/notifications/lark_client.rb +140 -0
  31. data/lib/context_drop/notifications/webhook_client.rb +103 -0
  32. data/lib/context_drop/version.rb +1 -1
  33. data/lib/context_drop.rb +5 -0
  34. metadata +21 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a7a997284b7383ca914c11ff5d92264e3270f559d86b353fb25df0430fcf1e0b
4
- data.tar.gz: e7dfea52eb1646010973caf533f3fde3c152e040b9a78ec6bc234f72bd9a76dc
3
+ metadata.gz: 2e667b7c0a99e4f141d1f5b1a4418570ca0ff9879725d067d57ae775e972faae
4
+ data.tar.gz: a24283e1c5812b17eb46f4a5c65ced33d815cf21cd73cf1fde438bfb318e36d8
5
5
  SHA512:
6
- metadata.gz: a818222b5c1b027a9d0450a087123fa6eda55297618ca9e0719cf3effd389522db81307fbe9b639d5de0f226ca7134bd6f26532f275bb35709361bd17e366ec8
7
- data.tar.gz: 3a9e46486927a790a525e0c209447bb6443ce0e225d9a78edee5d3770fee02b119c0244449a5adbf27de9f9ba5e0710bd25f5a5e332a59f9070b3ac665590bfb
6
+ metadata.gz: 1dd43b47aa0367f25767e149f9028ce81986597ae30f41bae280d8bd9b98328bc6dc3c1b53197c03d969ec82dc47bdb740a9b83579331e5a3bea605c965c6374
7
+ data.tar.gz: 88d3a00c38b2e988376a5a7231b0e59fdaf57459c87f3ceb9d7f8a0515f69e1ef0d63c42afb8e24703ae6f9b8d27b57aa8addb82342b5da42babe2749852b0b7
data/README.md CHANGED
@@ -266,7 +266,7 @@ Already running `ContextDrop::Engine` on Rails? Point `<ContextDrop />` at your
266
266
 
267
267
  ### Requirements
268
268
 
269
- - **Ruby** `>= 3.1`
269
+ - **Ruby** `>= 3.3.14` (`3.3.14+`, `3.4.13+`, or `4.0.10+` to address [CVE-2026-132825](https://www.ruby-lang.org/en/news/2026/11/18/multiple-vulnerabilities-in-regexp-and-string/))
270
270
  - **Ruby on Rails** `>= 7.1` (`7.1`, `7.2`, `8.0+`)
271
271
  - **Active Storage** installed in the host application (`bin/rails active_storage:install`)
272
272
 
@@ -440,6 +440,79 @@ ContextDrop ships with a built-in **Model Context Protocol (MCP)** server (`Cont
440
440
  | **Resource** | `contextdrop://feedbacks/{id}` | Full JSON snapshot of a specific feedback submission, its pinned elements, and attachment metadata. |
441
441
  | **Prompt** | `investigate_feedback` | Generates a structured root-cause investigation brief (with attached screenshots) for any local or remote `feedback_id` (`"staging:18"`). |
442
442
 
443
+ ### MCP Events, Webhooks & Lark (v0.3+)
444
+
445
+ ContextDrop implements the official MCP **Events** extension ([overview](https://modelcontextprotocol.io/extensions/events/overview)) so agents can react to feedback without constant tool polling.
446
+
447
+ | Delivery | Rails | `@context-drop` (Next.js / Node) |
448
+ |---|---|---|
449
+ | **Poll** | `events/poll` via `POST /context_drop/mcp` | `events/poll` via `POST …/mcp` |
450
+ | **Push (SSE)** | `GET /context_drop/mcp/events/stream` | `GET …/mcp/events/stream` |
451
+ | **Webhook** | `events/subscribe` (signed HTTPS callbacks) | Same |
452
+
453
+ **Lifecycle event types:** `feedback.created`, `feedback.updated`, `feedback.status_changed`, `feedback.deleted`.
454
+
455
+ **Rails — configure destinations in the dashboard**
456
+
457
+ 1. Run `bin/rails db:migrate` (includes event/notification tables).
458
+ 2. Open **`/context_drop/dashboard/integrations`** (header: **Integrations**).
459
+ 3. Add **Lark** (App ID, App Secret, `chat_id` / `receive_id`) or **HTTPS webhook** (callback URL + optional signing secret). Credentials are encrypted at rest. Use **Test** to verify delivery.
460
+
461
+ **Lark:** Create a custom app with Bot + `im:chat` / `im:message`, add the bot to your triage group, copy `chat_id` from the [API Explorer](https://open.larksuite.com/api-explorer) or group settings ([message card guide](https://open.larksuite.com/document/)).
462
+
463
+ **Standalone Node / Next.js — REST integrations API**
464
+
465
+ When you deploy with `createContextDropNextHandler` / `createContextDropPagesHandler`, destinations are stored under `<CONTEXT_DROP_STORAGE_DIR>/events-state.json`:
466
+
467
+ ```bash
468
+ curl -sS -X POST "https://your-app.com/api/context-drop/integrations/destinations" \
469
+ -H "Authorization: Bearer $CONTEXT_DROP_INTEGRATIONS_API_KEY" \
470
+ -H "Content-Type: application/json" \
471
+ -d '{
472
+ "name": "Lark triage",
473
+ "destination_type": "lark",
474
+ "event_types": ["feedback.created"],
475
+ "settings": { "receive_id": "oc_xxx", "receive_id_type": "chat_id" },
476
+ "secrets": { "app_id": "cli_xxx", "app_secret": "..." }
477
+ }'
478
+ ```
479
+
480
+ Defaults: authorize with `CONTEXT_DROP_INTEGRATIONS_API_KEY` (or the MCP API key); disable the pipeline with `notifications: { enabled: false }` in handler config.
481
+
482
+ **Example MCP client calls**
483
+
484
+ ```json
485
+ { "jsonrpc": "2.0", "id": 1, "method": "events/list" }
486
+ ```
487
+
488
+ ```json
489
+ {
490
+ "jsonrpc": "2.0",
491
+ "id": 2,
492
+ "method": "events/poll",
493
+ "params": {
494
+ "name": "feedback.created",
495
+ "arguments": { "environment": "production" },
496
+ "cursor": null
497
+ }
498
+ }
499
+ ```
500
+
501
+ ```json
502
+ {
503
+ "jsonrpc": "2.0",
504
+ "id": 3,
505
+ "method": "events/subscribe",
506
+ "params": {
507
+ "name": "feedback.created",
508
+ "callbackUrl": "https://agent.example/hooks/contextdrop",
509
+ "ttlHours": 24
510
+ }
511
+ }
512
+ ```
513
+
514
+ `npx context-drop mcp` stdio sessions use the same `events/*` methods and persist occurrences in `.context-drop/` alongside feedback storage.
515
+
443
516
  ---
444
517
 
445
518
  ### Recommended Setup: One Unified MCP Server for All Environments (`local` + `staging` + `production`)
@@ -1208,6 +1208,13 @@
1208
1208
  box-sizing: border-box;
1209
1209
  }
1210
1210
 
1211
+ .cd-form-shell {
1212
+ display: contents;
1213
+ margin: 0;
1214
+ padding: 0;
1215
+ border: 0;
1216
+ }
1217
+
1211
1218
  ::selection {
1212
1219
  background: var(--cd-pin-soft);
1213
1220
  color: var(--cd-ink);
@@ -2535,6 +2542,15 @@
2535
2542
  font-size: 11.5px;
2536
2543
  }
2537
2544
 
2545
+ .cd-input.is-masked-secret {
2546
+ -webkit-text-security: disc;
2547
+ }
2548
+
2549
+ .cd-input.is-masked-secret:placeholder-shown,
2550
+ .cd-input.is-masked-secret:disabled {
2551
+ -webkit-text-security: none;
2552
+ }
2553
+
2538
2554
  .cd-drawer-actions {
2539
2555
  display: flex;
2540
2556
  align-items: center;
@@ -6363,6 +6379,7 @@
6363
6379
  this.setCornerPosition(partial.position);
6364
6380
  }
6365
6381
  const nowAgentEnabled = Boolean(this.config.agent?.enabled);
6382
+ this.syncAgentSidebarMount();
6366
6383
  if (nowAgentEnabled && !wasAgentEnabled) {
6367
6384
  this.surfaceMode = "agent";
6368
6385
  if (this.isOpen) {
@@ -7581,9 +7598,95 @@
7581
7598
  }
7582
7599
  }
7583
7600
 
7601
+ clearAgentSidebarRefs() {
7602
+ this.agentSidebarEl = null;
7603
+ this.sidebarPinBtn = null;
7604
+ this.sidebarEnvPillEl = null;
7605
+ this.sidebarThemeToggleBtn = null;
7606
+ this.sidebarPushBtn = null;
7607
+ this.keyDotEl = null;
7608
+ this.providerPillLabelEl = null;
7609
+ this.keysToggleBtn = null;
7610
+ this.feedbacksToggleBtn = null;
7611
+ this.byokDrawerEl = null;
7612
+ this.byokProviderSelect = null;
7613
+ this.byokModelInput = null;
7614
+ this.byokModelPresetsSelect = null;
7615
+ this.byokBaseUrlField = null;
7616
+ this.byokBaseUrlInput = null;
7617
+ this.byokKeySourceBadge = null;
7618
+ this.byokKeyInput = null;
7619
+ this.feedbacksDrawerEl = null;
7620
+ this.feedbacksTabsEl = null;
7621
+ this.feedbacksListEl = null;
7622
+ this.agentStreamEl = null;
7623
+ this.agentTrayEl = null;
7624
+ this.agentStatusEl = null;
7625
+ this.agentTextareaEl = null;
7626
+ this.agentSendBtn = null;
7627
+ }
7628
+
7629
+ syncAgentSidebarMount() {
7630
+ const enabled = Boolean(this.config.agent?.enabled);
7631
+ if (enabled) {
7632
+ if (!this.agentSidebarEl && this.shadow) {
7633
+ const sidebar = this.buildAgentSidebarDom();
7634
+ sidebar.setAttribute("data-theme", this.currentTheme);
7635
+ if (
7636
+ this.bookCornerBtn &&
7637
+ this.bookCornerBtn.parentNode === this.shadow &&
7638
+ typeof this.shadow.insertBefore === "function"
7639
+ ) {
7640
+ this.shadow.insertBefore(sidebar, this.bookCornerBtn);
7641
+ } else {
7642
+ this.shadow.appendChild(sidebar);
7643
+ }
7644
+ }
7645
+ return;
7646
+ }
7647
+
7648
+ this.isByokDrawerOpen = false;
7649
+ this.isFeedbacksDrawerOpen = false;
7650
+ if (this.agentSidebarEl) {
7651
+ this.agentSidebarEl.parentNode?.removeChild(this.agentSidebarEl);
7652
+ }
7653
+ this.clearAgentSidebarRefs();
7654
+ }
7655
+
7656
+ buildFormShell() {
7657
+ const form = document.createElement("form");
7658
+ form.className = "cd-form-shell";
7659
+ form.autocomplete = "off";
7660
+ form.setAttribute("autocomplete", "off");
7661
+ form.noValidate = true;
7662
+ form.setAttribute("novalidate", "");
7663
+ form.setAttribute("data-1p-ignore", "true");
7664
+ form.setAttribute("data-lpignore", "true");
7665
+ form.setAttribute("data-bwignore", "true");
7666
+ form.setAttribute("data-form-type", "other");
7667
+ form.addEventListener("submit", (ev) => {
7668
+ ev.preventDefault();
7669
+ });
7670
+ return form;
7671
+ }
7672
+
7673
+ applyByokAutofillIgnore(control ) {
7674
+ control.autocomplete = "off";
7675
+ control.setAttribute("autocomplete", "off");
7676
+ control.spellcheck = false;
7677
+ control.setAttribute("spellcheck", "false");
7678
+ control.setAttribute("autocorrect", "off");
7679
+ control.setAttribute("autocapitalize", "off");
7680
+ control.setAttribute("data-1p-ignore", "true");
7681
+ control.setAttribute("data-lpignore", "true");
7682
+ control.setAttribute("data-bwignore", "true");
7683
+ control.setAttribute("data-form-type", "other");
7684
+ }
7685
+
7584
7686
  renderShadowDom() {
7585
7687
  if (!this.shadow) return;
7586
7688
  this.shadow.innerHTML = "";
7689
+ this.clearAgentSidebarRefs();
7587
7690
 
7588
7691
  const styleEl = document.createElement("style");
7589
7692
  styleEl.textContent = WIDGET_SHADOW_STYLES;
@@ -7603,9 +7706,11 @@
7603
7706
 
7604
7707
  this.shadow.appendChild(root);
7605
7708
 
7606
- // 3. Docked Local BYOK Agentic Coding Sidebar (active in local development mode)
7607
- const sidebar = this.buildAgentSidebarDom();
7608
- this.shadow.appendChild(sidebar);
7709
+ // 3. Docked Local BYOK Agentic Coding Sidebar (mounted only when local agent is enabled)
7710
+ if (Boolean(this.config.agent?.enabled)) {
7711
+ const sidebar = this.buildAgentSidebarDom();
7712
+ this.shadow.appendChild(sidebar);
7713
+ }
7609
7714
 
7610
7715
  // 4. Book-Corner Fold Launcher (flush at viewport corner, peels into pushpin on drag)
7611
7716
  const bookCorner = document.createElement("button");
@@ -7865,8 +7970,13 @@
7865
7970
  this.statusEl = status;
7866
7971
  composer.appendChild(status);
7867
7972
 
7973
+ const composerForm = this.buildFormShell();
7974
+
7868
7975
  const fileInput = document.createElement("input");
7869
7976
  fileInput.type = "file";
7977
+ fileInput.setAttribute("type", "file");
7978
+ fileInput.autocomplete = "off";
7979
+ fileInput.setAttribute("autocomplete", "off");
7870
7980
  fileInput.multiple = true;
7871
7981
  fileInput.accept = this.config.limits.allowed_content_types.join(",");
7872
7982
  fileInput.hidden = true;
@@ -7877,7 +7987,7 @@
7877
7987
  }
7878
7988
  });
7879
7989
  this.fileInputEl = fileInput;
7880
- composer.appendChild(fileInput);
7990
+ composerForm.appendChild(fileInput);
7881
7991
 
7882
7992
  const promptShell = document.createElement("div");
7883
7993
  promptShell.className = "cd-prompt-shell";
@@ -7909,6 +8019,12 @@
7909
8019
 
7910
8020
  const textarea = document.createElement("textarea");
7911
8021
  textarea.className = "cd-textarea";
8022
+ textarea.autocomplete = "off";
8023
+ textarea.setAttribute("autocomplete", "off");
8024
+ textarea.setAttribute("data-1p-ignore", "true");
8025
+ textarea.setAttribute("data-lpignore", "true");
8026
+ textarea.setAttribute("data-bwignore", "true");
8027
+ textarea.setAttribute("data-form-type", "other");
7912
8028
  textarea.placeholder = this.config.placeholder;
7913
8029
  textarea.setAttribute("aria-label", "Feedback description");
7914
8030
  textarea.addEventListener("keydown", (ev) => {
@@ -7951,7 +8067,8 @@
7951
8067
  toolbar.appendChild(actions);
7952
8068
  toolbar.appendChild(submitBtn);
7953
8069
  promptShell.appendChild(toolbar);
7954
- composer.appendChild(promptShell);
8070
+ composerForm.appendChild(promptShell);
8071
+ composer.appendChild(composerForm);
7955
8072
 
7956
8073
  return composer;
7957
8074
  }
@@ -8208,11 +8325,19 @@
8208
8325
  this.agentStatusEl = agentStatus;
8209
8326
  sidebar.appendChild(agentStatus);
8210
8327
 
8328
+ const agentPromptForm = this.buildFormShell();
8329
+
8211
8330
  const promptShell = document.createElement("div");
8212
8331
  promptShell.className = "cd-prompt-shell";
8213
8332
 
8214
8333
  const textarea = document.createElement("textarea");
8215
8334
  textarea.className = "cd-textarea";
8335
+ textarea.autocomplete = "off";
8336
+ textarea.setAttribute("autocomplete", "off");
8337
+ textarea.setAttribute("data-1p-ignore", "true");
8338
+ textarea.setAttribute("data-lpignore", "true");
8339
+ textarea.setAttribute("data-bwignore", "true");
8340
+ textarea.setAttribute("data-form-type", "other");
8216
8341
  textarea.placeholder =
8217
8342
  "Ask the local agent to inspect pinned DOM elements, read Rails views, or propose a fix...";
8218
8343
  textarea.setAttribute("aria-label", "Agent prompt");
@@ -8245,7 +8370,8 @@
8245
8370
 
8246
8371
  toolbar.appendChild(sendBtn);
8247
8372
  promptShell.appendChild(toolbar);
8248
- sidebar.appendChild(promptShell);
8373
+ agentPromptForm.appendChild(promptShell);
8374
+ sidebar.appendChild(agentPromptForm);
8249
8375
 
8250
8376
  this.syncProviderControls();
8251
8377
  return sidebar;
@@ -8262,6 +8388,8 @@
8262
8388
  securityNote.innerHTML = `${ICON_SHIELD}<span>Keys stay encrypted on disk in <code>tmp/context_drop_byok.enc</code> (AES-256-GCM) or read from server ENV—never stored in browser storage.</span>`;
8263
8389
  drawer.appendChild(securityNote);
8264
8390
 
8391
+ const byokForm = this.buildFormShell();
8392
+
8265
8393
  const grid = document.createElement("div");
8266
8394
  grid.className = "cd-form-grid";
8267
8395
 
@@ -8274,6 +8402,7 @@
8274
8402
  const provSelect = document.createElement("select");
8275
8403
  provSelect.className = "cd-select";
8276
8404
  provSelect.setAttribute("aria-label", "LLM Provider");
8405
+ this.applyByokAutofillIgnore(provSelect);
8277
8406
  provSelect.addEventListener("change", () => {
8278
8407
  this.selectedVaultProviderId = provSelect.value;
8279
8408
  this.syncByokDrawerFields();
@@ -8292,6 +8421,7 @@
8292
8421
  const presetSelect = document.createElement("select");
8293
8422
  presetSelect.className = "cd-select";
8294
8423
  presetSelect.setAttribute("aria-label", "Model preset");
8424
+ this.applyByokAutofillIgnore(presetSelect);
8295
8425
  presetSelect.addEventListener("change", () => {
8296
8426
  if (this.byokModelInput && presetSelect.value) {
8297
8427
  this.byokModelInput.value = presetSelect.value;
@@ -8310,8 +8440,10 @@
8310
8440
  modelLabel.textContent = "Model ID";
8311
8441
  const modelInput = document.createElement("input");
8312
8442
  modelInput.type = "text";
8443
+ modelInput.setAttribute("type", "text");
8313
8444
  modelInput.className = "cd-input is-mono";
8314
8445
  modelInput.setAttribute("aria-label", "Model identifier");
8446
+ this.applyByokAutofillIgnore(modelInput);
8315
8447
  this.byokModelInput = modelInput;
8316
8448
  modelField.appendChild(modelLabel);
8317
8449
  modelField.appendChild(modelInput);
@@ -8326,16 +8458,19 @@
8326
8458
  baseUrlLabel.textContent = "OpenAI-Compatible Base URL";
8327
8459
  const baseUrlInput = document.createElement("input");
8328
8460
  baseUrlInput.type = "url";
8461
+ baseUrlInput.setAttribute("type", "url");
8329
8462
  baseUrlInput.className = "cd-input is-mono";
8330
8463
  baseUrlInput.placeholder = "https://openrouter.ai/api/v1 or http://localhost:11434/v1";
8331
8464
  baseUrlInput.setAttribute("aria-label", "Custom OpenAI-compatible Base URL");
8465
+ this.applyByokAutofillIgnore(baseUrlInput);
8332
8466
  this.byokBaseUrlInput = baseUrlInput;
8333
8467
  baseUrlField.appendChild(baseUrlLabel);
8334
8468
  baseUrlField.appendChild(baseUrlInput);
8335
8469
  this.byokBaseUrlField = baseUrlField;
8336
8470
  grid.appendChild(baseUrlField);
8337
8471
 
8338
- // API Key field
8472
+ // API Key field — uses CSS-masked type="text" rather than type="password" so Chromium/browser
8473
+ // password managers never classify preceding or host-page inputs as login username fields.
8339
8474
  const keyField = document.createElement("div");
8340
8475
  keyField.className = "cd-field is-full";
8341
8476
  const keyLabel = document.createElement("label");
@@ -8350,17 +8485,20 @@
8350
8485
  keyLabel.appendChild(keyBadge);
8351
8486
 
8352
8487
  const keyInput = document.createElement("input");
8353
- keyInput.type = "password";
8354
- keyInput.autocomplete = "off";
8355
- keyInput.className = "cd-input is-mono";
8488
+ keyInput.type = "text";
8489
+ keyInput.setAttribute("type", "text");
8490
+ keyInput.className = "cd-input is-mono is-masked-secret";
8356
8491
  keyInput.placeholder = "Paste API key (leave blank to keep existing key)";
8357
8492
  keyInput.setAttribute("aria-label", "Provider API Key");
8493
+ keyInput.disabled = !this.isByokDrawerOpen;
8494
+ this.applyByokAutofillIgnore(keyInput);
8358
8495
  this.byokKeyInput = keyInput;
8359
8496
  keyField.appendChild(keyLabel);
8360
8497
  keyField.appendChild(keyInput);
8361
8498
  grid.appendChild(keyField);
8362
8499
 
8363
- drawer.appendChild(grid);
8500
+ byokForm.appendChild(grid);
8501
+ drawer.appendChild(byokForm);
8364
8502
 
8365
8503
  const actions = document.createElement("div");
8366
8504
  actions.className = "cd-drawer-actions";
@@ -9277,6 +9415,9 @@
9277
9415
  if (this.byokDrawerEl) {
9278
9416
  this.byokDrawerEl.hidden = !this.isByokDrawerOpen;
9279
9417
  }
9418
+ if (this.byokKeyInput) {
9419
+ this.byokKeyInput.disabled = !this.isByokDrawerOpen;
9420
+ }
9280
9421
  if (this.keysToggleBtn) {
9281
9422
  this.keysToggleBtn.classList.toggle("is-active", this.isByokDrawerOpen);
9282
9423
  }
@@ -159,5 +159,17 @@ module ContextDrop
159
159
  request.headers["X-Context-Drop-Ownership-Token"].to_s.strip.present? ||
160
160
  request.content_type.to_s.downcase.start_with?("application/json")
161
161
  end
162
+
163
+ def stream_context_drop_attachment!(attachment)
164
+ return false unless attachment.respond_to?(:file) && attachment.file.attached?
165
+
166
+ response.set_header("Cache-Control", "private, max-age=3600")
167
+ response.set_header("Content-Security-Policy", "default-src 'none'; sandbox")
168
+ send_data attachment.file.download,
169
+ type: attachment.file.content_type || "image/png",
170
+ disposition: "inline",
171
+ filename: attachment.file.filename.to_s
172
+ true
173
+ end
162
174
  end
163
175
  end
@@ -101,7 +101,14 @@ module ContextDrop
101
101
  return
102
102
  end
103
103
 
104
+ previous_status = @feedback.status
104
105
  @feedback.update!(status: next_status)
106
+ invoke_lifecycle_callback(
107
+ :on_feedback_updated,
108
+ @feedback,
109
+ previous_status: previous_status,
110
+ dashboard_url: dashboard_feedback_url(@feedback)
111
+ )
105
112
 
106
113
  respond_to do |format|
107
114
  format.json do
@@ -117,6 +124,17 @@ module ContextDrop
117
124
  end
118
125
  end
119
126
 
127
+ def attachment
128
+ @feedback = ContextDrop::Feedback.find_by(id: params[:id])
129
+ if @feedback.nil?
130
+ head :not_found
131
+ return
132
+ end
133
+
134
+ att = ContextDrop::Attachment.where(feedback_id: @feedback.id).find_by(id: params[:attachment_id])
135
+ head(:not_found) unless att && stream_context_drop_attachment!(att)
136
+ end
137
+
120
138
  def create_comment
121
139
  @feedback = ContextDrop::Feedback.find(params[:id])
122
140
  body = (params[:body] || params.dig(:comment, :body)).to_s.strip.presence
@@ -198,6 +216,26 @@ module ContextDrop
198
216
 
199
217
  private
200
218
 
219
+ def invoke_lifecycle_callback(hook_name, feedback, **context)
220
+ return unless ContextDrop.config.respond_to?(hook_name)
221
+
222
+ callback = ContextDrop.config.public_send(hook_name)
223
+ callback.call(feedback) if callback.respond_to?(:call)
224
+ ContextDrop::FeedbackEvents::Publisher.map_lifecycle_callback(
225
+ hook_name,
226
+ feedback,
227
+ **context
228
+ )
229
+ rescue StandardError => e
230
+ Rails.logger.error("[ContextDrop] #{hook_name} callback error: #{e.class}: #{e.message}") if defined?(Rails.logger) && Rails.logger
231
+ end
232
+
233
+ def dashboard_feedback_url(feedback)
234
+ url_for(controller: "context_drop/dashboard/feedbacks", action: :show, id: feedback.id, only_path: false)
235
+ rescue StandardError
236
+ "/context_drop/dashboard/feedbacks/#{feedback.id}"
237
+ end
238
+
201
239
  def enforce_dashboard_authorization!
202
240
  return if ContextDrop.config.dashboard_authorized?(self)
203
241
 
@@ -0,0 +1,140 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "securerandom"
4
+
5
+ module ContextDrop
6
+ module Dashboard
7
+ class IntegrationsController < ApplicationController
8
+ layout "context_drop/application"
9
+ helper ContextDrop::DashboardHelper
10
+
11
+ before_action :enforce_dashboard_authorization!
12
+ before_action :ensure_tables!
13
+ before_action :set_destination, only: %i[edit update destroy test_delivery]
14
+
15
+ PERMITTED_EVENTS = ContextDrop::EventOccurrence::EVENT_NAMES
16
+
17
+ def index
18
+ @destinations = ContextDrop::NotificationDestination.order(created_at: :desc)
19
+ @mcp_subscriptions = if subscription_table?
20
+ ContextDrop::McpEventSubscription.active.order(expires_at: :asc).limit(20)
21
+ else
22
+ []
23
+ end
24
+ end
25
+
26
+ def new
27
+ @destination = ContextDrop::NotificationDestination.new(
28
+ enabled: true,
29
+ destination_type: params[:destination_type].presence || "lark",
30
+ event_types: ["feedback.created"]
31
+ )
32
+ end
33
+
34
+ def create
35
+ @destination = ContextDrop::NotificationDestination.new(destination_params)
36
+ apply_secret_params!(@destination)
37
+ if @destination.save
38
+ redirect_to dashboard_integrations_path, notice: "Notification destination \"#{@destination.name}\" created."
39
+ else
40
+ render :new, status: :unprocessable_entity
41
+ end
42
+ end
43
+
44
+ def edit; end
45
+
46
+ def update
47
+ @destination.assign_attributes(destination_params)
48
+ apply_secret_params!(@destination)
49
+ if @destination.save
50
+ redirect_to dashboard_integrations_path, notice: "Saved \"#{@destination.name}\"."
51
+ else
52
+ render :edit, status: :unprocessable_entity
53
+ end
54
+ end
55
+
56
+ def destroy
57
+ name = @destination.name
58
+ @destination.destroy
59
+ redirect_to dashboard_integrations_path, notice: "Removed \"#{name}\"."
60
+ end
61
+
62
+ def test_delivery
63
+ occurrence = ContextDrop::EventOccurrence.new(
64
+ event_id: "test:#{SecureRandom.uuid}",
65
+ name: (@destination.event_types.first || "feedback.created"),
66
+ data: {
67
+ "feedback_id" => 0,
68
+ "environment" => ContextDrop.config.resolved_environment,
69
+ "status" => "new",
70
+ "message" => "Test notification from ContextDrop dashboard.",
71
+ "page_url" => "/",
72
+ "dashboard_url" => dashboard_integrations_url(only_path: false)
73
+ },
74
+ occurred_at: Time.current
75
+ )
76
+ ContextDrop::Notifications::Deliverer.deliver_destination(@destination, occurrence)
77
+ @destination.reload
78
+ if @destination.last_delivery_status == "delivered"
79
+ redirect_to dashboard_integrations_path, notice: "Test notification delivered for \"#{@destination.name}\"."
80
+ else
81
+ redirect_to dashboard_integrations_path,
82
+ alert: "Test failed: #{@destination.last_delivery_error.presence || 'unknown error'}"
83
+ end
84
+ rescue StandardError => e
85
+ redirect_to dashboard_integrations_path, alert: "Test failed: #{e.message}"
86
+ end
87
+
88
+ private
89
+
90
+ def set_destination
91
+ @destination = ContextDrop::NotificationDestination.find(params[:id])
92
+ end
93
+
94
+ def destination_params
95
+ raw = params.require(:notification_destination)
96
+ permitted = raw.permit(
97
+ :name,
98
+ :enabled,
99
+ :destination_type,
100
+ event_types: [],
101
+ settings: %i[url receive_id receive_id_type api_host]
102
+ )
103
+ permitted[:event_types] = Array(permitted[:event_types]).map(&:to_s).select do |ev|
104
+ PERMITTED_EVENTS.include?(ev)
105
+ end
106
+ permitted[:settings] = (permitted[:settings] || {}).to_h.compact_blank
107
+ permitted
108
+ end
109
+
110
+ def apply_secret_params!(destination)
111
+ raw = params[:notification_destination] || {}
112
+ secrets = {}
113
+ case destination.destination_type
114
+ when "lark"
115
+ secrets["app_id"] = raw[:app_id].to_s.strip if raw[:app_id].present?
116
+ secrets["app_secret"] = raw[:app_secret].to_s.strip if raw[:app_secret].present?
117
+ when "webhook"
118
+ secrets["signing_secret"] = raw[:signing_secret].to_s.strip if raw[:signing_secret].present?
119
+ end
120
+ destination.merge_secrets!(secrets) if secrets.any?
121
+ end
122
+
123
+ def enforce_dashboard_authorization!
124
+ return if ContextDrop.config.dashboard_authorized?(self)
125
+
126
+ render plain: "403 Forbidden — Not authorized to configure integrations", status: :forbidden
127
+ end
128
+
129
+ def ensure_tables!
130
+ return if ContextDrop::NotificationDestination.connection.data_source_exists?("context_drop_notification_destinations")
131
+
132
+ render plain: "Run ContextDrop engine migrations to enable integrations.", status: :service_unavailable
133
+ end
134
+
135
+ def subscription_table?
136
+ ContextDrop::McpEventSubscription.connection.data_source_exists?("context_drop_mcp_event_subscriptions")
137
+ end
138
+ end
139
+ end
140
+ end