context_drop 0.3.4 → 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 (30) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +74 -1
  3. data/app/controllers/context_drop/application_controller.rb +12 -0
  4. data/app/controllers/context_drop/dashboard/feedbacks_controller.rb +38 -0
  5. data/app/controllers/context_drop/dashboard/integrations_controller.rb +140 -0
  6. data/app/controllers/context_drop/feedbacks_controller.rb +38 -14
  7. data/app/controllers/context_drop/mcp_events_controller.rb +66 -0
  8. data/app/helpers/context_drop/dashboard_helper.rb +6 -0
  9. data/app/models/context_drop/event_occurrence.rb +34 -0
  10. data/app/models/context_drop/mcp_event_subscription.rb +25 -0
  11. data/app/models/context_drop/notification_destination.rb +84 -0
  12. data/app/views/context_drop/dashboard/feedbacks/show.html.erb +8 -10
  13. data/app/views/context_drop/dashboard/integrations/_form.html.erb +92 -0
  14. data/app/views/context_drop/dashboard/integrations/edit.html.erb +13 -0
  15. data/app/views/context_drop/dashboard/integrations/index.html.erb +101 -0
  16. data/app/views/context_drop/dashboard/integrations/new.html.erb +13 -0
  17. data/app/views/layouts/context_drop/application.html.erb +78 -0
  18. data/config/routes.rb +5 -0
  19. data/db/migrate/20261124000001_create_context_drop_events_and_notifications.rb +47 -0
  20. data/lib/context_drop/feedback_events/publisher.rb +100 -0
  21. data/lib/context_drop/mcp/dispatcher.rb +20 -5
  22. data/lib/context_drop/mcp/event_stream_registry.rb +34 -0
  23. data/lib/context_drop/mcp/events.rb +178 -0
  24. data/lib/context_drop/notification_secrets.rb +85 -0
  25. data/lib/context_drop/notifications/deliverer.rb +49 -0
  26. data/lib/context_drop/notifications/lark_client.rb +140 -0
  27. data/lib/context_drop/notifications/webhook_client.rb +103 -0
  28. data/lib/context_drop/version.rb +1 -1
  29. data/lib/context_drop.rb +5 -0
  30. metadata +21 -4
@@ -208,13 +208,11 @@
208
208
 
209
209
  <% if att.respond_to?(:file) && att.file.attached? %>
210
210
  <div style="margin: 0.5rem 0;">
211
- <% if main_app.respond_to?(:url_for) %>
212
- <a href="<%= main_app.url_for(att.file) %>" target="_blank" rel="noopener noreferrer">
213
- <img src="<%= main_app.url_for(att.file) %>"
214
- alt="Attachment #<%= att.id %>"
215
- style="max-width: 100%; max-height: 280px; object-fit: contain; border-radius: 6px; border: 1px solid var(--cd-border-subtle); display: block;" />
216
- </a>
217
- <% end %>
211
+ <a href="<%= cd_dashboard_attachment_path(att) %>" target="_blank" rel="noopener noreferrer">
212
+ <img src="<%= cd_dashboard_attachment_path(att) %>"
213
+ alt="Attachment #<%= att.id %>"
214
+ style="max-width: 100%; max-height: 280px; object-fit: contain; border-radius: 6px; border: 1px solid var(--cd-border-subtle); display: block;" />
215
+ </a>
218
216
  </div>
219
217
  <div style="font-size: 0.75rem; color: var(--cd-muted); font-family: var(--cd-font-mono);">
220
218
  <%= att.file.filename %> (<%= att.file.content_type %>, <%= att.file.byte_size %> bytes)
@@ -261,9 +259,9 @@
261
259
  <% if comment.attachments.any? %>
262
260
  <div class="cd-comment-images">
263
261
  <% comment.attachments.each do |att| %>
264
- <% if att.respond_to?(:file) && att.file.attached? && main_app.respond_to?(:url_for) %>
265
- <a href="<%= main_app.url_for(att.file) %>" target="_blank" rel="noopener noreferrer" title="<%= att.file.filename %>">
266
- <img src="<%= main_app.url_for(att.file) %>"
262
+ <% if att.respond_to?(:file) && att.file.attached? %>
263
+ <a href="<%= cd_dashboard_attachment_path(att) %>" target="_blank" rel="noopener noreferrer" title="<%= att.file.filename %>">
264
+ <img src="<%= cd_dashboard_attachment_path(att) %>"
267
265
  alt="Comment attachment #<%= att.id %>"
268
266
  class="cd-comment-thumb" />
269
267
  </a>
@@ -0,0 +1,92 @@
1
+ <%= form_with model: @destination, url: @destination.persisted? ? dashboard_integration_path(@destination) : dashboard_integrations_path, scope: :notification_destination, class: "cd-form-stack" do |f| %>
2
+ <% if @destination.errors.any? %>
3
+ <div class="cd-flash cd-flash-alert" role="alert">
4
+ <%= @destination.errors.full_messages.join(", ") %>
5
+ </div>
6
+ <% end %>
7
+
8
+ <div class="cd-form-grid">
9
+ <div class="cd-field">
10
+ <%= f.label :name, "Display name" %>
11
+ <%= f.text_field :name, class: "cd-input", required: true, placeholder: "Triage · Lark" %>
12
+ </div>
13
+
14
+ <div class="cd-field">
15
+ <%= f.label :destination_type, "Destination type" %>
16
+ <%= f.select :destination_type, ContextDrop::NotificationDestination::DESTINATION_TYPES, {}, class: "cd-input" %>
17
+ </div>
18
+
19
+ <div class="cd-field cd-field-span">
20
+ <span class="cd-label">Lifecycle events</span>
21
+ <div class="cd-check-grid">
22
+ <% ContextDrop::EventOccurrence::EVENT_NAMES.each do |ev| %>
23
+ <label class="cd-check-row">
24
+ <%= check_box_tag "notification_destination[event_types][]", ev, @destination.subscribed_to?(ev) %>
25
+ <span class="cd-chip cd-chip-mono cd-chip-compact"><%= ev %></span>
26
+ </label>
27
+ <% end %>
28
+ </div>
29
+ </div>
30
+
31
+ <div class="cd-field">
32
+ <label class="cd-check-row">
33
+ <%= f.check_box :enabled %>
34
+ <span>Enabled</span>
35
+ </label>
36
+ </div>
37
+ </div>
38
+
39
+ <% if @destination.destination_type == "lark" %>
40
+ <div class="cd-card cd-card-inset" style="margin-top: 1rem;">
41
+ <h3 class="cd-section-title">Lark app bot</h3>
42
+ <p class="cd-section-hint">Credentials are encrypted at rest. Use a custom app with Bot capability and <code class="cd-inline-code">im:message</code> scope in your Lark developer console.</p>
43
+ <div class="cd-form-grid">
44
+ <div class="cd-field">
45
+ <label for="notification_destination_app_id">App ID</label>
46
+ <input type="text" name="notification_destination[app_id]" id="notification_destination_app_id" class="cd-input" autocomplete="off"
47
+ placeholder="<%= @destination.masked_secrets['app_id'] || 'cli_xxxxxxxx' %>" />
48
+ </div>
49
+ <div class="cd-field">
50
+ <label for="notification_destination_app_secret">App Secret</label>
51
+ <input type="password" name="notification_destination[app_secret]" id="notification_destination_app_secret" class="cd-input" autocomplete="new-password"
52
+ placeholder="<%= @destination.persisted? ? 'Leave blank to keep current secret' : 'Required' %>" />
53
+ </div>
54
+ <div class="cd-field">
55
+ <%= f.label :receive_id, "Receive ID (chat_id)", value: nil %>
56
+ <%= f.text_field :receive_id, name: "notification_destination[settings][receive_id]", value: @destination.settings["receive_id"], class: "cd-input", placeholder: "oc_xxxxxxxx" %>
57
+ </div>
58
+ <div class="cd-field">
59
+ <%= f.label :receive_id_type, "Receive ID type" %>
60
+ <%= select_tag "notification_destination[settings][receive_id_type]",
61
+ options_for_select([%w[chat_id chat_id], %w[open_id open_id], %w[user_id user_id]], @destination.settings["receive_id_type"].presence || "chat_id"),
62
+ class: "cd-input" %>
63
+ </div>
64
+ <div class="cd-field cd-field-span">
65
+ <%= f.label :api_host, "API host (optional)" %>
66
+ <%= f.text_field :api_host, name: "notification_destination[settings][api_host]", value: @destination.settings["api_host"], class: "cd-input", placeholder: "https://open.larksuite.com" %>
67
+ </div>
68
+ </div>
69
+ </div>
70
+ <% else %>
71
+ <div class="cd-card cd-card-inset" style="margin-top: 1rem;">
72
+ <h3 class="cd-section-title">HTTPS webhook</h3>
73
+ <p class="cd-section-hint">Deliveries use Standard Webhooks signatures (<code class="cd-inline-code">webhook-signature</code> header).</p>
74
+ <div class="cd-form-grid">
75
+ <div class="cd-field cd-field-span">
76
+ <%= f.label :url, "Callback URL" %>
77
+ <%= f.text_field :url, name: "notification_destination[settings][url]", value: @destination.settings["url"], class: "cd-input", placeholder: "https://example.com/hooks/contextdrop" %>
78
+ </div>
79
+ <div class="cd-field cd-field-span">
80
+ <label for="notification_destination_signing_secret">Signing secret</label>
81
+ <input type="password" name="notification_destination[signing_secret]" id="notification_destination_signing_secret" class="cd-input" autocomplete="new-password"
82
+ placeholder="<%= @destination.persisted? ? 'Leave blank to keep current secret' : 'Optional HMAC secret' %>" />
83
+ </div>
84
+ </div>
85
+ </div>
86
+ <% end %>
87
+
88
+ <div class="cd-form-actions">
89
+ <%= f.submit @destination.persisted? ? "Save destination" : "Create destination", class: "cd-btn cd-btn-primary" %>
90
+ <%= link_to "Cancel", dashboard_integrations_path, class: "cd-btn" %>
91
+ </div>
92
+ <% end %>
@@ -0,0 +1,13 @@
1
+ <% content_for :cd_breadcrumbs do %>
2
+ <%= link_to "Integrations", dashboard_integrations_path, class: "cd-crumb-link" %>
3
+ <span class="cd-crumb-sep">/</span>
4
+ <span class="cd-crumb-current"><%= @destination.name %></span>
5
+ <% end %>
6
+
7
+ <div class="cd-page-head">
8
+ <h1 class="cd-page-title">Edit destination</h1>
9
+ </div>
10
+
11
+ <div class="cd-card" style="padding: 1.25rem 1.5rem 1.5rem;">
12
+ <%= render "form" %>
13
+ </div>
@@ -0,0 +1,101 @@
1
+ <% content_for :cd_breadcrumbs do %>
2
+ <span class="cd-crumb-current">Integrations</span>
3
+ <% end %>
4
+
5
+ <div class="cd-page-head">
6
+ <div>
7
+ <h1 class="cd-page-title">Notifications &amp; webhooks</h1>
8
+ <p class="cd-page-lede">Route ContextDrop lifecycle events to Lark message cards or signed HTTPS webhooks. MCP clients can also subscribe via <code class="cd-inline-code">events/poll</code>, <code class="cd-inline-code">events/stream</code>, and <code class="cd-inline-code">events/subscribe</code>.</p>
9
+ </div>
10
+ <div class="cd-page-actions">
11
+ <%= link_to "Add Lark bot", new_dashboard_integration_path(destination_type: "lark"), class: "cd-btn cd-btn-primary" %>
12
+ <%= link_to "Add webhook", new_dashboard_integration_path(destination_type: "webhook"), class: "cd-btn" %>
13
+ </div>
14
+ </div>
15
+
16
+ <div class="cd-card cd-card-tight" style="margin-bottom: 1rem;">
17
+ <h2 class="cd-section-title">MCP event types</h2>
18
+ <ul class="cd-chip-list" aria-label="Supported MCP events">
19
+ <% ContextDrop::EventOccurrence::EVENT_NAMES.each do |ev| %>
20
+ <li><span class="cd-chip cd-chip-mono"><%= ev %></span></li>
21
+ <% end %>
22
+ </ul>
23
+ </div>
24
+
25
+ <% if @destinations.empty? %>
26
+ <div class="cd-card" style="padding: 2.5rem 1.5rem; text-align: center;">
27
+ <p style="margin: 0 0 0.35rem; font-weight: 600;">No destinations configured</p>
28
+ <p style="margin: 0 0 1rem; color: var(--cd-muted); font-size: 0.875rem;">Add a Lark bot to notify your triage channel when new feedback arrives.</p>
29
+ <%= link_to "Configure Lark", new_dashboard_integration_path(destination_type: "lark"), class: "cd-btn cd-btn-primary" %>
30
+ </div>
31
+ <% else %>
32
+ <div class="cd-table-wrap cd-card cd-card-tight">
33
+ <table class="cd-table">
34
+ <thead>
35
+ <tr>
36
+ <th>Name</th>
37
+ <th>Type</th>
38
+ <th>Events</th>
39
+ <th>Status</th>
40
+ <th>Last delivery</th>
41
+ <th class="cd-cell-actions">Actions</th>
42
+ </tr>
43
+ </thead>
44
+ <tbody>
45
+ <% @destinations.each do |dest| %>
46
+ <tr>
47
+ <td style="font-weight: 600;"><%= dest.name %></td>
48
+ <td><span class="cd-chip"><%= dest.destination_type %></span></td>
49
+ <td>
50
+ <div class="cd-chip-row">
51
+ <% dest.event_types.each do |ev| %>
52
+ <span class="cd-chip cd-chip-mono cd-chip-compact"><%= ev %></span>
53
+ <% end %>
54
+ </div>
55
+ </td>
56
+ <td>
57
+ <% if dest.enabled? %>
58
+ <span class="cd-status-badge cd-status-reviewed">Enabled</span>
59
+ <% else %>
60
+ <span class="cd-status-badge cd-status-dismissed">Paused</span>
61
+ <% end %>
62
+ </td>
63
+ <td style="font-size: 0.8125rem; color: var(--cd-muted); max-width: 220px;">
64
+ <% if dest.last_delivered_at %>
65
+ <div><%= dest.last_delivery_status %> · <%= time_ago_in_words(dest.last_delivered_at) %> ago</div>
66
+ <% if dest.last_delivery_error.present? %>
67
+ <div style="color: var(--cd-deleted-text); margin-top: 0.25rem;"><%= dest.last_delivery_error.truncate(120) %></div>
68
+ <% end %>
69
+ <% else %>
70
+ <span>Not yet delivered</span>
71
+ <% end %>
72
+ </td>
73
+ <td class="cd-cell-actions">
74
+ <div class="cd-btn-row">
75
+ <%= button_to "Test", test_delivery_dashboard_integration_path(dest), method: :post, class: "cd-btn cd-btn-compact", form: { data: { turbo: false } } %>
76
+ <%= link_to "Edit", edit_dashboard_integration_path(dest), class: "cd-btn cd-btn-compact" %>
77
+ <%= button_to "Remove", dashboard_integration_path(dest), method: :delete, class: "cd-btn cd-btn-danger cd-btn-compact", form: { data: { turbo_confirm: "Remove this destination?" } } %>
78
+ </div>
79
+ </td>
80
+ </tr>
81
+ <% end %>
82
+ </tbody>
83
+ </table>
84
+ </div>
85
+ <% end %>
86
+
87
+ <% if @mcp_subscriptions.present? %>
88
+ <div class="cd-card cd-card-tight" style="margin-top: 1.25rem;">
89
+ <h2 class="cd-section-title">Active MCP webhook subscriptions</h2>
90
+ <p class="cd-section-hint">Registered by MCP clients via <code class="cd-inline-code">events/subscribe</code> (expires automatically).</p>
91
+ <ul class="cd-meta-list" style="margin-top: 0.75rem;">
92
+ <% @mcp_subscriptions.each do |sub| %>
93
+ <dt><span class="cd-chip cd-chip-mono"><%= sub.event_name %></span></dt>
94
+ <dd>
95
+ <span class="cd-break-all"><%= sub.callback_url %></span>
96
+ <span style="color: var(--cd-muted);"> · expires <%= sub.expires_at.iso8601 %></span>
97
+ </dd>
98
+ <% end %>
99
+ </ul>
100
+ </div>
101
+ <% end %>
@@ -0,0 +1,13 @@
1
+ <% content_for :cd_breadcrumbs do %>
2
+ <%= link_to "Integrations", dashboard_integrations_path, class: "cd-crumb-link" %>
3
+ <span class="cd-crumb-sep">/</span>
4
+ <span class="cd-crumb-current">New destination</span>
5
+ <% end %>
6
+
7
+ <div class="cd-page-head">
8
+ <h1 class="cd-page-title">Add notification destination</h1>
9
+ </div>
10
+
11
+ <div class="cd-card" style="padding: 1.25rem 1.5rem 1.5rem;">
12
+ <%= render "form" %>
13
+ </div>
@@ -828,6 +828,79 @@
828
828
  }
829
829
  .cd-meta-list dt { color: var(--cd-muted); font-weight: 500; }
830
830
  .cd-meta-list dd { margin: 0; overflow-wrap: anywhere; }
831
+ .cd-page-head {
832
+ display: flex;
833
+ flex-wrap: wrap;
834
+ align-items: flex-start;
835
+ justify-content: space-between;
836
+ gap: 1rem;
837
+ margin-bottom: 1rem;
838
+ }
839
+ .cd-page-title { margin: 0; font-size: 1.35rem; font-weight: 650; letter-spacing: -0.02em; }
840
+ .cd-page-lede { margin: 0.35rem 0 0; color: var(--cd-muted); font-size: 0.875rem; max-width: 52rem; line-height: 1.45; }
841
+ .cd-page-actions { display: flex; flex-wrap: wrap; gap: 0.5rem; align-items: center; }
842
+ .cd-section-title { margin: 0 0 0.35rem; font-size: 0.9375rem; font-weight: 650; }
843
+ .cd-section-hint { margin: 0; color: var(--cd-muted); font-size: 0.8125rem; line-height: 1.45; }
844
+ .cd-inline-code {
845
+ font-family: var(--cd-font-mono);
846
+ font-size: 0.8125rem;
847
+ background: var(--cd-surface-recessed);
848
+ border: 1px solid var(--cd-border-subtle);
849
+ border-radius: 4px;
850
+ padding: 0.1rem 0.35rem;
851
+ }
852
+ .cd-chip-list { display: flex; flex-wrap: wrap; gap: 0.4rem; list-style: none; margin: 0.5rem 0 0; padding: 0; }
853
+ .cd-chip-row { display: flex; flex-wrap: wrap; gap: 0.35rem; }
854
+ .cd-chip {
855
+ display: inline-flex;
856
+ align-items: center;
857
+ font-size: 0.75rem;
858
+ padding: 0.15rem 0.45rem;
859
+ border-radius: 5px;
860
+ border: 1px solid var(--cd-border-subtle);
861
+ background: var(--cd-surface-recessed);
862
+ color: var(--cd-text-secondary);
863
+ }
864
+ .cd-chip-mono { font-family: var(--cd-font-mono); }
865
+ .cd-chip-compact { padding: 0.08rem 0.35rem; }
866
+ .cd-card-inset {
867
+ background: var(--cd-surface-subtle);
868
+ border-style: dashed;
869
+ }
870
+ .cd-form-stack { display: grid; gap: 0.75rem; }
871
+ .cd-form-grid {
872
+ display: grid;
873
+ grid-template-columns: repeat(2, minmax(0, 1fr));
874
+ gap: 0.75rem 1rem;
875
+ }
876
+ .cd-field { display: grid; gap: 0.3rem; font-size: 0.875rem; }
877
+ .cd-field-span { grid-column: 1 / -1; }
878
+ .cd-label { color: var(--cd-muted); font-weight: 500; }
879
+ .cd-check-grid { display: grid; gap: 0.35rem; margin-top: 0.35rem; }
880
+ .cd-check-row { display: inline-flex; align-items: center; gap: 0.45rem; cursor: pointer; }
881
+ .cd-form-actions { display: flex; flex-wrap: wrap; gap: 0.5rem; margin-top: 1.25rem; }
882
+ .cd-btn-compact { padding: 0.25rem 0.55rem; font-size: 0.8125rem; }
883
+ .cd-btn-danger { color: var(--cd-deleted-text); border-color: var(--cd-deleted-border); background: var(--cd-deleted-bg); }
884
+ .cd-btn-row { display: flex; flex-wrap: wrap; gap: 0.35rem; justify-content: flex-end; }
885
+ .cd-break-all { overflow-wrap: anywhere; font-family: var(--cd-font-mono); font-size: 0.75rem; }
886
+ .cd-crumb-link { color: var(--cd-primary); text-decoration: none; font-weight: 500; }
887
+ .cd-crumb-link:hover { text-decoration: underline; }
888
+ .cd-crumb-current { color: var(--cd-muted); font-weight: 500; }
889
+ .cd-header-nav {
890
+ display: inline-flex;
891
+ align-items: center;
892
+ gap: 0.35rem;
893
+ margin-left: 0.75rem;
894
+ font-size: 0.8125rem;
895
+ }
896
+ .cd-header-nav a {
897
+ color: var(--cd-text-secondary);
898
+ text-decoration: none;
899
+ padding: 0.2rem 0.45rem;
900
+ border-radius: 5px;
901
+ }
902
+ .cd-header-nav a:hover { background: var(--cd-surface-recessed); color: var(--cd-text); }
903
+ .cd-header-nav a.active { background: var(--cd-primary-soft); color: var(--cd-primary-hover); font-weight: 600; }
831
904
  </style>
832
905
  </head>
833
906
  <body>
@@ -848,6 +921,11 @@
848
921
  <span class="cd-context-chip" style="font-family: var(--cd-font-mono);"><%= ContextDrop.configuration.resolved_environment %></span>
849
922
  </a>
850
923
 
924
+ <nav class="cd-header-nav" aria-label="Dashboard sections">
925
+ <%= link_to "Feedback", dashboard_feedbacks_path, class: (controller_path == "context_drop/dashboard/feedbacks" ? "active" : nil) %>
926
+ <%= link_to "Integrations", dashboard_integrations_path, class: (controller_path == "context_drop/dashboard/integrations" ? "active" : nil) %>
927
+ </nav>
928
+
851
929
  <% if content_for?(:cd_breadcrumbs) %>
852
930
  <span class="cd-crumb-sep">/</span>
853
931
  <%= yield :cd_breadcrumbs %>
data/config/routes.rb CHANGED
@@ -20,6 +20,7 @@ ContextDrop::Engine.routes.draw do
20
20
  get "feedback/:feedback_id/attachments/:id", to: "feedbacks#attachment"
21
21
  get "health", to: "health#show"
22
22
  post "mcp", to: "mcp#create"
23
+ get "mcp/events/stream", to: "mcp_events#stream"
23
24
 
24
25
  scope "agent" do
25
26
  get "status", to: "agent#status"
@@ -34,8 +35,12 @@ ContextDrop::Engine.routes.draw do
34
35
 
35
36
  namespace :dashboard do
36
37
  root to: "feedbacks#index"
38
+ resources :integrations, only: %i[index new create edit update destroy] do
39
+ post :test_delivery, on: :member
40
+ end
37
41
  resources :feedbacks, only: %i[index show update] do
38
42
  post :comments, on: :member, action: :create_comment
43
+ get "attachments/:attachment_id", on: :member, action: :attachment, as: :attachment
39
44
  end
40
45
  end
41
46
 
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ class CreateContextDropEventsAndNotifications < ActiveRecord::Migration[7.1]
4
+ def change
5
+ create_table :context_drop_event_occurrences do |t|
6
+ t.string :event_id, null: false
7
+ t.string :name, null: false
8
+ t.bigint :feedback_id
9
+ t.json :data, null: false, default: {}
10
+ t.datetime :occurred_at, null: false
11
+ t.timestamps
12
+ end
13
+ add_index :context_drop_event_occurrences, :event_id, unique: true
14
+ add_index :context_drop_event_occurrences, %i[name occurred_at id], name: "idx_cd_events_name_time"
15
+ add_index :context_drop_event_occurrences, :feedback_id
16
+
17
+ create_table :context_drop_notification_destinations do |t|
18
+ t.string :name, null: false
19
+ t.boolean :enabled, null: false, default: true
20
+ t.string :destination_type, null: false
21
+ t.json :event_types, null: false, default: []
22
+ t.json :settings, null: false, default: {}
23
+ t.text :secrets_ciphertext
24
+ t.string :last_delivery_status
25
+ t.text :last_delivery_error
26
+ t.datetime :last_delivered_at
27
+ t.timestamps
28
+ end
29
+ add_index :context_drop_notification_destinations, :enabled
30
+ add_index :context_drop_notification_destinations, :destination_type, name: "idx_cd_notif_dest_type"
31
+
32
+ create_table :context_drop_mcp_event_subscriptions do |t|
33
+ t.string :subscription_id, null: false
34
+ t.string :event_name, null: false
35
+ t.json :arguments, null: false, default: {}
36
+ t.string :callback_url, null: false
37
+ t.string :signing_secret, null: false
38
+ t.string :delivery_mode, null: false, default: "webhook"
39
+ t.datetime :expires_at, null: false
40
+ t.string :last_cursor
41
+ t.timestamps
42
+ end
43
+ add_index :context_drop_mcp_event_subscriptions, :subscription_id, unique: true
44
+ add_index :context_drop_mcp_event_subscriptions, :expires_at
45
+ add_index :context_drop_mcp_event_subscriptions, :event_name
46
+ end
47
+ end
@@ -0,0 +1,100 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "securerandom"
4
+
5
+ module ContextDrop
6
+ module FeedbackEvents
7
+ class Publisher
8
+ class << self
9
+ def publish(event_name, feedback, **context)
10
+ return unless persistence_ready?
11
+
12
+ data = build_payload(event_name, feedback, context)
13
+ occurrence = ContextDrop::EventOccurrence.create!(
14
+ event_id: stable_event_id(event_name, feedback, data),
15
+ name: event_name,
16
+ feedback_id: feedback.respond_to?(:id) ? feedback.id : nil,
17
+ data: data,
18
+ occurred_at: Time.current
19
+ )
20
+
21
+ deliver_notifications(occurrence)
22
+ deliver_mcp_webhook_subscriptions(occurrence)
23
+ ContextDrop::MCP::EventStreamRegistry.broadcast(occurrence)
24
+ occurrence
25
+ rescue StandardError => e
26
+ log_error("publish", e)
27
+ nil
28
+ end
29
+
30
+ def map_lifecycle_callback(hook_name, feedback, previous_status: nil, dashboard_url: nil)
31
+ ctx = { previous_status: previous_status, dashboard_url: dashboard_url }
32
+ case hook_name
33
+ when :on_feedback_created
34
+ publish("feedback.created", feedback, **ctx)
35
+ when :on_feedback_updated
36
+ if previous_status.present? && previous_status.to_s != feedback.status.to_s
37
+ publish("feedback.status_changed", feedback, **ctx)
38
+ else
39
+ publish("feedback.updated", feedback, **ctx)
40
+ end
41
+ when :on_feedback_deleted
42
+ publish("feedback.deleted", feedback, **ctx)
43
+ end
44
+ end
45
+
46
+ private
47
+
48
+ def persistence_ready?
49
+ defined?(ContextDrop::EventOccurrence) &&
50
+ ContextDrop::EventOccurrence.connection.data_source_exists?("context_drop_event_occurrences")
51
+ end
52
+
53
+ def stable_event_id(event_name, feedback, _data)
54
+ fb_id = feedback.respond_to?(:id) ? feedback.id : "unknown"
55
+ "#{event_name}:#{fb_id}:#{SecureRandom.uuid}"
56
+ end
57
+
58
+ def build_payload(event_name, feedback, context)
59
+ {
60
+ event: event_name,
61
+ feedback_id: feedback.id,
62
+ environment: feedback.respond_to?(:environment_name) ? feedback.environment_name : ContextDrop.config.resolved_environment,
63
+ status: feedback.status,
64
+ previous_status: context[:previous_status],
65
+ message: feedback.message.to_s.truncate(500),
66
+ page_url: feedback.page_url,
67
+ submitter_label: feedback.respond_to?(:submitter_label) ? feedback.submitter_label : nil,
68
+ element_references_count: feedback.respond_to?(:element_references) ? feedback.element_references.size : 0,
69
+ attachments_count: feedback.respond_to?(:attachments) ? feedback.attachments.size : 0,
70
+ created_at: feedback.created_at&.iso8601,
71
+ updated_at: feedback.updated_at&.iso8601,
72
+ deleted_at: (feedback.deleted_at_value&.iso8601 if feedback.respond_to?(:deleted_at_value)),
73
+ dashboard_url: context[:dashboard_url].presence
74
+ }.compact
75
+ end
76
+
77
+ def deliver_notifications(occurrence)
78
+ ContextDrop::Notifications::Deliverer.deliver_for_occurrence(occurrence)
79
+ end
80
+
81
+ def deliver_mcp_webhook_subscriptions(occurrence)
82
+ return unless defined?(ContextDrop::McpEventSubscription)
83
+ return unless ContextDrop::McpEventSubscription.connection.data_source_exists?("context_drop_mcp_event_subscriptions")
84
+
85
+ ContextDrop::McpEventSubscription.active.find_each do |sub|
86
+ next unless sub.matches_occurrence?(occurrence)
87
+
88
+ ContextDrop::Notifications::WebhookClient.deliver_mcp_subscription(sub, occurrence)
89
+ end
90
+ end
91
+
92
+ def log_error(phase, err)
93
+ return unless defined?(Rails) && Rails.respond_to?(:logger) && Rails.logger
94
+
95
+ Rails.logger.error("[ContextDrop::FeedbackEvents] #{phase}: #{err.class}: #{err.message}")
96
+ end
97
+ end
98
+ end
99
+ end
100
+ end
@@ -1,11 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "json"
4
+ require_relative "events"
5
+ require_relative "event_stream_registry"
4
6
 
5
7
  module ContextDrop
6
8
  module MCP
7
9
  class Dispatcher
8
10
  PROTOCOL_VERSION = "2025-03-26"
11
+ EVENTS_PROTOCOL_VERSION = "2026-07-28"
9
12
 
10
13
  TOOLS = {
11
14
  "list_environments" => ContextDrop::MCP::Tools::ListEnvironments,
@@ -85,13 +88,17 @@ module ContextDrop
85
88
  when "initialize"
86
89
  client_version = params["protocolVersion"].to_s.strip
87
90
  negotiated_version = client_version.empty? ? PROTOCOL_VERSION : client_version
91
+ caps = {
92
+ tools: { listChanged: false },
93
+ resources: { subscribe: false, listChanged: false },
94
+ prompts: { listChanged: false },
95
+ extensions: {
96
+ ContextDrop::MCP::Events::EXTENSION_ID => ContextDrop::MCP::Events.capability_payload
97
+ }
98
+ }
88
99
  jsonrpc_result(id, {
89
100
  protocolVersion: negotiated_version,
90
- capabilities: {
91
- tools: { listChanged: false },
92
- resources: { subscribe: false, listChanged: false },
93
- prompts: { listChanged: false }
94
- },
101
+ capabilities: caps,
95
102
  serverInfo: {
96
103
  name: "context_drop",
97
104
  version: ContextDrop::VERSION,
@@ -119,6 +126,14 @@ module ContextDrop
119
126
  })
120
127
  when "prompts/get"
121
128
  handle_prompt_get(id, params)
129
+ when "events/list"
130
+ jsonrpc_result(id, ContextDrop::MCP::Events.list(params))
131
+ when "events/poll"
132
+ jsonrpc_result(id, ContextDrop::MCP::Events.poll(params))
133
+ when "events/subscribe"
134
+ jsonrpc_result(id, ContextDrop::MCP::Events.subscribe(params, request: params["_context_drop_request"]))
135
+ when "events/unsubscribe"
136
+ jsonrpc_result(id, ContextDrop::MCP::Events.unsubscribe(params))
122
137
  else
123
138
  jsonrpc_error(id, -32_601, "Method not found: #{method}")
124
139
  end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "monitor"
4
+
5
+ module ContextDrop
6
+ module MCP
7
+ class EventStreamRegistry
8
+ @mutex = Monitor.new
9
+ @subscribers = []
10
+
11
+ class << self
12
+ def subscribe(queue)
13
+ @mutex.synchronize { @subscribers << queue }
14
+ queue
15
+ end
16
+
17
+ def unsubscribe(queue)
18
+ @mutex.synchronize { @subscribers.delete(queue) }
19
+ end
20
+
21
+ def broadcast(occurrence)
22
+ payload = occurrence.occurrence_payload
23
+ @mutex.synchronize do
24
+ @subscribers.each do |queue|
25
+ queue << payload
26
+ rescue StandardError
27
+ nil
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end