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.
- checksums.yaml +4 -4
- data/README.md +74 -1
- data/app/controllers/context_drop/application_controller.rb +12 -0
- data/app/controllers/context_drop/dashboard/feedbacks_controller.rb +38 -0
- data/app/controllers/context_drop/dashboard/integrations_controller.rb +140 -0
- data/app/controllers/context_drop/feedbacks_controller.rb +38 -14
- data/app/controllers/context_drop/mcp_events_controller.rb +66 -0
- data/app/helpers/context_drop/dashboard_helper.rb +6 -0
- data/app/models/context_drop/event_occurrence.rb +34 -0
- data/app/models/context_drop/mcp_event_subscription.rb +25 -0
- data/app/models/context_drop/notification_destination.rb +84 -0
- data/app/views/context_drop/dashboard/feedbacks/show.html.erb +8 -10
- data/app/views/context_drop/dashboard/integrations/_form.html.erb +92 -0
- data/app/views/context_drop/dashboard/integrations/edit.html.erb +13 -0
- data/app/views/context_drop/dashboard/integrations/index.html.erb +101 -0
- data/app/views/context_drop/dashboard/integrations/new.html.erb +13 -0
- data/app/views/layouts/context_drop/application.html.erb +78 -0
- data/config/routes.rb +5 -0
- data/db/migrate/20261124000001_create_context_drop_events_and_notifications.rb +47 -0
- data/lib/context_drop/feedback_events/publisher.rb +100 -0
- data/lib/context_drop/mcp/dispatcher.rb +20 -5
- data/lib/context_drop/mcp/event_stream_registry.rb +34 -0
- data/lib/context_drop/mcp/events.rb +178 -0
- data/lib/context_drop/notification_secrets.rb +85 -0
- data/lib/context_drop/notifications/deliverer.rb +49 -0
- data/lib/context_drop/notifications/lark_client.rb +140 -0
- data/lib/context_drop/notifications/webhook_client.rb +103 -0
- data/lib/context_drop/version.rb +1 -1
- data/lib/context_drop.rb +5 -0
- metadata +21 -4
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require "securerandom"
|
|
5
|
+
|
|
6
|
+
module ContextDrop
|
|
7
|
+
module MCP
|
|
8
|
+
module Events
|
|
9
|
+
EXTENSION_ID = "io.modelcontextprotocol/events"
|
|
10
|
+
POLL_DEFAULT_MS = 3_000
|
|
11
|
+
SUBSCRIPTION_TTL_HOURS = 24
|
|
12
|
+
|
|
13
|
+
class << self
|
|
14
|
+
def event_types
|
|
15
|
+
@event_types ||= [
|
|
16
|
+
event_type_entry("feedback.created", "A new ContextDrop feedback submission was persisted."),
|
|
17
|
+
event_type_entry("feedback.updated", "Feedback content or attachments were updated without a status transition."),
|
|
18
|
+
event_type_entry("feedback.status_changed", "Feedback status changed (triage workflow)."),
|
|
19
|
+
event_type_entry("feedback.deleted", "Feedback was soft-deleted.")
|
|
20
|
+
].freeze
|
|
21
|
+
end
|
|
22
|
+
def capability_payload
|
|
23
|
+
{ listChanged: false }
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def list(_params = {})
|
|
27
|
+
{ events: event_types }
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def poll(params)
|
|
31
|
+
name = params["name"].to_s
|
|
32
|
+
arguments = params["arguments"].is_a?(Hash) ? params["arguments"] : {}
|
|
33
|
+
cursor = params["cursor"]
|
|
34
|
+
env = arguments["environment"].to_s.strip
|
|
35
|
+
|
|
36
|
+
scope = occurrence_scope(name)
|
|
37
|
+
scope = scope.where("context_drop_event_occurrences.id > ?", cursor.to_i) if cursor.present?
|
|
38
|
+
|
|
39
|
+
if env.present? && scope.klass.column_names.include?("data")
|
|
40
|
+
# SQLite JSON filtering via LIKE for portability in tests; AR records filter in Ruby below.
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
records = scope.limit(50).to_a
|
|
44
|
+
if env.present?
|
|
45
|
+
records = records.select do |row|
|
|
46
|
+
row.data.is_a?(Hash) && row.data["environment"].to_s.casecmp?(env)
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
events = records.map(&:occurrence_payload)
|
|
51
|
+
next_cursor = records.last&.cursor
|
|
52
|
+
{
|
|
53
|
+
events: events,
|
|
54
|
+
cursor: next_cursor,
|
|
55
|
+
nextPollMs: POLL_DEFAULT_MS,
|
|
56
|
+
truncated: false
|
|
57
|
+
}
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def subscribe(params, request: nil)
|
|
61
|
+
ensure_subscription_table!
|
|
62
|
+
name = params["name"].to_s
|
|
63
|
+
arguments = params["arguments"].is_a?(Hash) ? params["arguments"] : {}
|
|
64
|
+
callback_url = params["callbackUrl"].to_s.strip
|
|
65
|
+
ttl_hours = (params["ttlHours"] || SUBSCRIPTION_TTL_HOURS).to_i.clamp(1, 168)
|
|
66
|
+
|
|
67
|
+
raise ContextDrop::Error, "callbackUrl is required" if callback_url.empty?
|
|
68
|
+
|
|
69
|
+
ContextDrop::Notifications::WebhookClient.validate_https_uri!(callback_url)
|
|
70
|
+
signing_secret = SecureRandom.hex(32)
|
|
71
|
+
subscription = ContextDrop::McpEventSubscription.create!(
|
|
72
|
+
subscription_id: SecureRandom.uuid,
|
|
73
|
+
event_name: name,
|
|
74
|
+
arguments: arguments,
|
|
75
|
+
callback_url: callback_url,
|
|
76
|
+
signing_secret: signing_secret,
|
|
77
|
+
delivery_mode: "webhook",
|
|
78
|
+
expires_at: ttl_hours.hours.from_now
|
|
79
|
+
)
|
|
80
|
+
|
|
81
|
+
verification_token = nil
|
|
82
|
+
if request
|
|
83
|
+
verification_token = perform_callback_verification(request, subscription, signing_secret)
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
{
|
|
87
|
+
subscriptionId: subscription.subscription_id,
|
|
88
|
+
expiresAt: subscription.expires_at.iso8601,
|
|
89
|
+
signingSecret: signing_secret,
|
|
90
|
+
verificationChallenge: verification_token
|
|
91
|
+
}
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def unsubscribe(params)
|
|
95
|
+
ensure_subscription_table!
|
|
96
|
+
sub_id = params["subscriptionId"].to_s.strip
|
|
97
|
+
sub = ContextDrop::McpEventSubscription.find_by(subscription_id: sub_id)
|
|
98
|
+
sub&.destroy
|
|
99
|
+
{ removed: !sub.nil? }
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
private
|
|
103
|
+
|
|
104
|
+
def occurrence_scope(name)
|
|
105
|
+
ensure_occurrence_table!
|
|
106
|
+
scope = ContextDrop::EventOccurrence.chronological
|
|
107
|
+
scope = scope.named(name) if name.present?
|
|
108
|
+
scope
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def ensure_occurrence_table!
|
|
112
|
+
return if ContextDrop::EventOccurrence.connection.data_source_exists?("context_drop_event_occurrences")
|
|
113
|
+
|
|
114
|
+
raise ContextDrop::Error, "ContextDrop events persistence is not installed (run engine migrations)"
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
def ensure_subscription_table!
|
|
118
|
+
return if ContextDrop::McpEventSubscription.connection.data_source_exists?("context_drop_mcp_event_subscriptions")
|
|
119
|
+
|
|
120
|
+
raise ContextDrop::Error, "ContextDrop MCP webhook subscriptions are not installed (run engine migrations)"
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
def perform_callback_verification(_request, subscription, signing_secret)
|
|
124
|
+
challenge = SecureRandom.hex(16)
|
|
125
|
+
occurrence = ContextDrop::EventOccurrence.new(
|
|
126
|
+
event_id: "verification:#{subscription.subscription_id}",
|
|
127
|
+
name: subscription.event_name,
|
|
128
|
+
data: { "verification" => true, "challenge" => challenge },
|
|
129
|
+
occurred_at: Time.current
|
|
130
|
+
)
|
|
131
|
+
ContextDrop::Notifications::WebhookClient.deliver_mcp_subscription(subscription, occurrence)
|
|
132
|
+
challenge
|
|
133
|
+
rescue StandardError
|
|
134
|
+
nil
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
def event_type_entry(name, description)
|
|
138
|
+
{
|
|
139
|
+
name: name,
|
|
140
|
+
description: description,
|
|
141
|
+
inputSchema: environment_filter_schema,
|
|
142
|
+
payloadSchema: feedback_payload_schema,
|
|
143
|
+
delivery: %w[poll push webhook]
|
|
144
|
+
}
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
def environment_filter_schema
|
|
148
|
+
{
|
|
149
|
+
type: "object",
|
|
150
|
+
properties: {
|
|
151
|
+
environment: {
|
|
152
|
+
type: "string",
|
|
153
|
+
description: "Optional ContextDrop environment filter (e.g. staging, production)."
|
|
154
|
+
}
|
|
155
|
+
},
|
|
156
|
+
additionalProperties: false
|
|
157
|
+
}
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
def feedback_payload_schema
|
|
161
|
+
{
|
|
162
|
+
type: "object",
|
|
163
|
+
properties: {
|
|
164
|
+
feedback_id: { type: "integer" },
|
|
165
|
+
environment: { type: "string" },
|
|
166
|
+
status: { type: "string" },
|
|
167
|
+
previous_status: { type: %w[string null] },
|
|
168
|
+
message: { type: "string" },
|
|
169
|
+
page_url: { type: "string" },
|
|
170
|
+
dashboard_url: { type: "string" }
|
|
171
|
+
},
|
|
172
|
+
required: %w[feedback_id environment status page_url]
|
|
173
|
+
}
|
|
174
|
+
end
|
|
175
|
+
end
|
|
176
|
+
end
|
|
177
|
+
end
|
|
178
|
+
end
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require "openssl"
|
|
5
|
+
require "securerandom"
|
|
6
|
+
require "fileutils"
|
|
7
|
+
require "digest"
|
|
8
|
+
require_relative "../context_drop/base64_util"
|
|
9
|
+
|
|
10
|
+
module ContextDrop
|
|
11
|
+
class NotificationSecrets
|
|
12
|
+
CIPHER_NAME = "aes-256-gcm"
|
|
13
|
+
|
|
14
|
+
class << self
|
|
15
|
+
def encrypt(hash)
|
|
16
|
+
return nil if hash.nil? || hash.empty?
|
|
17
|
+
|
|
18
|
+
cipher = OpenSSL::Cipher.new(CIPHER_NAME)
|
|
19
|
+
cipher.encrypt
|
|
20
|
+
cipher.key = master_key
|
|
21
|
+
iv = cipher.random_iv
|
|
22
|
+
cipher.auth_data = ""
|
|
23
|
+
ciphertext = cipher.update(JSON.generate(hash)) + cipher.final
|
|
24
|
+
tag = cipher.auth_tag
|
|
25
|
+
{
|
|
26
|
+
"v" => 1,
|
|
27
|
+
"iv" => ContextDrop::Base64Util.strict_encode64(iv),
|
|
28
|
+
"tag" => ContextDrop::Base64Util.strict_encode64(tag),
|
|
29
|
+
"ciphertext" => ContextDrop::Base64Util.strict_encode64(ciphertext)
|
|
30
|
+
}
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def decrypt(blob)
|
|
34
|
+
return {} if blob.nil? || blob.empty?
|
|
35
|
+
return blob if blob.is_a?(Hash) && blob["ciphertext"].nil?
|
|
36
|
+
|
|
37
|
+
payload = blob.is_a?(Hash) ? blob : JSON.parse(blob.to_s)
|
|
38
|
+
cipher = OpenSSL::Cipher.new(CIPHER_NAME)
|
|
39
|
+
cipher.decrypt
|
|
40
|
+
cipher.key = master_key
|
|
41
|
+
cipher.iv = ContextDrop::Base64Util.strict_decode64(payload["iv"].to_s)
|
|
42
|
+
cipher.auth_tag = ContextDrop::Base64Util.strict_decode64(payload["tag"].to_s)
|
|
43
|
+
cipher.auth_data = ""
|
|
44
|
+
JSON.parse(cipher.update(ContextDrop::Base64Util.strict_decode64(payload["ciphertext"].to_s)) + cipher.final)
|
|
45
|
+
rescue StandardError
|
|
46
|
+
{}
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def mask_secret(raw)
|
|
50
|
+
str = raw.to_s.strip
|
|
51
|
+
return nil if str.empty?
|
|
52
|
+
return "••••••••" if str.length <= 6
|
|
53
|
+
|
|
54
|
+
"••••#{str[-4, 4]}"
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
private
|
|
58
|
+
|
|
59
|
+
def master_key
|
|
60
|
+
explicit = ContextDrop.config.ownership_secret.to_s.strip
|
|
61
|
+
return Digest::SHA256.digest(explicit) if explicit.bytesize >= 16
|
|
62
|
+
|
|
63
|
+
path = resolved_master_key_path
|
|
64
|
+
if File.exist?(path)
|
|
65
|
+
raw = File.binread(path)
|
|
66
|
+
return raw if raw.bytesize == 32
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
FileUtils.mkdir_p(File.dirname(path))
|
|
70
|
+
raw = SecureRandom.random_bytes(32)
|
|
71
|
+
File.binwrite(path, raw)
|
|
72
|
+
raw
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def resolved_master_key_path
|
|
76
|
+
root = if defined?(Rails) && Rails.respond_to?(:root) && Rails.root
|
|
77
|
+
Rails.root.to_s
|
|
78
|
+
else
|
|
79
|
+
Dir.pwd
|
|
80
|
+
end
|
|
81
|
+
File.join(root, "tmp", "context_drop_notification.key")
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
end
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ContextDrop
|
|
4
|
+
module Notifications
|
|
5
|
+
class Deliverer
|
|
6
|
+
class << self
|
|
7
|
+
def deliver_for_occurrence(occurrence)
|
|
8
|
+
return unless destinations_ready?
|
|
9
|
+
|
|
10
|
+
ContextDrop::NotificationDestination.enabled.find_each do |destination|
|
|
11
|
+
next unless destination.subscribed_to?(occurrence.name)
|
|
12
|
+
|
|
13
|
+
deliver_destination(destination, occurrence)
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def deliver_destination(destination, occurrence)
|
|
18
|
+
case destination.destination_type
|
|
19
|
+
when "lark"
|
|
20
|
+
LarkClient.deliver(destination, occurrence)
|
|
21
|
+
when "webhook"
|
|
22
|
+
WebhookClient.deliver_destination(destination, occurrence)
|
|
23
|
+
else
|
|
24
|
+
raise LarkClient::DeliveryError, "Unknown destination type #{destination.destination_type}"
|
|
25
|
+
end
|
|
26
|
+
destination.record_delivery!(status: "delivered")
|
|
27
|
+
rescue StandardError => e
|
|
28
|
+
destination.record_delivery!(status: "failed", error: e.message)
|
|
29
|
+
log_error(destination, e)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
private
|
|
33
|
+
|
|
34
|
+
def destinations_ready?
|
|
35
|
+
defined?(ContextDrop::NotificationDestination) &&
|
|
36
|
+
ContextDrop::NotificationDestination.connection.data_source_exists?("context_drop_notification_destinations")
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def log_error(destination, err)
|
|
40
|
+
return unless defined?(Rails) && Rails.respond_to?(:logger) && Rails.logger
|
|
41
|
+
|
|
42
|
+
Rails.logger.warn(
|
|
43
|
+
"[ContextDrop::Notifications] destination ##{destination.id} #{destination.name}: #{err.class}: #{err.message}"
|
|
44
|
+
)
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require "net/http"
|
|
5
|
+
require "uri"
|
|
6
|
+
|
|
7
|
+
module ContextDrop
|
|
8
|
+
module Notifications
|
|
9
|
+
class LarkClient
|
|
10
|
+
class DeliveryError < StandardError; end
|
|
11
|
+
|
|
12
|
+
class << self
|
|
13
|
+
def deliver(destination, occurrence)
|
|
14
|
+
settings = destination.settings.is_a?(Hash) ? destination.settings : {}
|
|
15
|
+
secrets = destination.secrets
|
|
16
|
+
app_id = secrets["app_id"].to_s.strip
|
|
17
|
+
app_secret = secrets["app_secret"].to_s.strip
|
|
18
|
+
receive_id = settings["receive_id"].to_s.strip
|
|
19
|
+
receive_id_type = settings["receive_id_type"].to_s.strip.presence || "chat_id"
|
|
20
|
+
api_host = settings["api_host"].to_s.strip.presence || "https://open.larksuite.com"
|
|
21
|
+
|
|
22
|
+
raise DeliveryError, "Lark app_id missing" if app_id.empty?
|
|
23
|
+
raise DeliveryError, "Lark app_secret missing" if app_secret.empty?
|
|
24
|
+
raise DeliveryError, "Lark receive_id missing" if receive_id.empty?
|
|
25
|
+
|
|
26
|
+
token = fetch_tenant_access_token(api_host, app_id, app_secret)
|
|
27
|
+
card = build_message_card(occurrence)
|
|
28
|
+
post_message(api_host, token, receive_id_type, receive_id, card)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
private
|
|
32
|
+
|
|
33
|
+
def fetch_tenant_access_token(api_host, app_id, app_secret)
|
|
34
|
+
uri = URI.join("#{api_host}/", "open-apis/auth/v3/tenant_access_token/internal")
|
|
35
|
+
body = JSON.generate(app_id: app_id, app_secret: app_secret)
|
|
36
|
+
res = post_json(uri, body, "Content-Type" => "application/json")
|
|
37
|
+
parsed = JSON.parse(res.body)
|
|
38
|
+
raise DeliveryError, parsed["msg"].to_s unless parsed["code"].to_i.zero?
|
|
39
|
+
|
|
40
|
+
parsed["tenant_access_token"].to_s
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def post_message(api_host, token, receive_id_type, receive_id, card)
|
|
44
|
+
uri = URI.join("#{api_host}/", "open-apis/im/v1/messages")
|
|
45
|
+
uri.query = URI.encode_www_form(receive_id_type: receive_id_type)
|
|
46
|
+
payload = {
|
|
47
|
+
receive_id: receive_id,
|
|
48
|
+
msg_type: "interactive",
|
|
49
|
+
content: card
|
|
50
|
+
}
|
|
51
|
+
res = post_json(uri, JSON.generate(payload), "Authorization" => "Bearer #{token}", "Content-Type" => "application/json")
|
|
52
|
+
parsed = JSON.parse(res.body)
|
|
53
|
+
raise DeliveryError, parsed["msg"].to_s unless parsed["code"].to_i.zero?
|
|
54
|
+
|
|
55
|
+
true
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def build_message_card(occurrence)
|
|
59
|
+
data = occurrence.data.is_a?(Hash) ? occurrence.data : {}
|
|
60
|
+
title = event_title(occurrence.name)
|
|
61
|
+
env = data["environment"].to_s
|
|
62
|
+
status = data["status"].to_s
|
|
63
|
+
prev = data["previous_status"].to_s
|
|
64
|
+
status_line = prev.present? ? "#{prev} → #{status}" : status
|
|
65
|
+
msg = data["message"].to_s
|
|
66
|
+
page = data["page_url"].to_s
|
|
67
|
+
dash = data["dashboard_url"].to_s
|
|
68
|
+
|
|
69
|
+
lines = [
|
|
70
|
+
"**Feedback ##{data['feedback_id']}** · `#{env}`",
|
|
71
|
+
"**Status:** #{status_line}",
|
|
72
|
+
msg.present? ? "> #{msg}" : nil,
|
|
73
|
+
page.present? ? "**Page:** #{page}" : nil
|
|
74
|
+
].compact.join("\n")
|
|
75
|
+
|
|
76
|
+
card = {
|
|
77
|
+
config: { wide_screen_mode: true },
|
|
78
|
+
header: {
|
|
79
|
+
template: header_color(occurrence.name),
|
|
80
|
+
title: { tag: "plain_text", content: title }
|
|
81
|
+
},
|
|
82
|
+
elements: [
|
|
83
|
+
{ tag: "div", text: { tag: "lark_md", content: lines } }
|
|
84
|
+
]
|
|
85
|
+
}
|
|
86
|
+
if dash.present?
|
|
87
|
+
card[:elements] << {
|
|
88
|
+
tag: "action",
|
|
89
|
+
actions: [
|
|
90
|
+
{
|
|
91
|
+
tag: "button",
|
|
92
|
+
text: { tag: "plain_text", content: "Open in Dashboard" },
|
|
93
|
+
type: "primary",
|
|
94
|
+
multi_url: {
|
|
95
|
+
url: dash,
|
|
96
|
+
pc_url: dash,
|
|
97
|
+
android_url: dash,
|
|
98
|
+
ios_url: dash
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
]
|
|
102
|
+
}
|
|
103
|
+
end
|
|
104
|
+
JSON.generate(card)
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
def event_title(name)
|
|
108
|
+
{
|
|
109
|
+
"feedback.created" => "New ContextDrop feedback",
|
|
110
|
+
"feedback.updated" => "Feedback updated",
|
|
111
|
+
"feedback.status_changed" => "Feedback status changed",
|
|
112
|
+
"feedback.deleted" => "Feedback deleted"
|
|
113
|
+
}[name] || "ContextDrop feedback event"
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
def header_color(name)
|
|
117
|
+
case name
|
|
118
|
+
when "feedback.created" then "blue"
|
|
119
|
+
when "feedback.status_changed" then "orange"
|
|
120
|
+
when "feedback.deleted" then "red"
|
|
121
|
+
else "wathet"
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
def post_json(uri, body, headers)
|
|
126
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
|
127
|
+
http.use_ssl = uri.scheme == "https"
|
|
128
|
+
http.open_timeout = 8
|
|
129
|
+
http.read_timeout = 15
|
|
130
|
+
req = Net::HTTP::Post.new(uri.request_uri, headers)
|
|
131
|
+
req.body = body
|
|
132
|
+
res = http.request(req)
|
|
133
|
+
return res if res.is_a?(Net::HTTPSuccess)
|
|
134
|
+
|
|
135
|
+
raise DeliveryError, "HTTP #{res.code}: #{res.body.to_s.truncate(300)}"
|
|
136
|
+
end
|
|
137
|
+
end
|
|
138
|
+
end
|
|
139
|
+
end
|
|
140
|
+
end
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require "net/http"
|
|
5
|
+
require "openssl"
|
|
6
|
+
require "uri"
|
|
7
|
+
require_relative "../../context_drop/base64_util"
|
|
8
|
+
|
|
9
|
+
module ContextDrop
|
|
10
|
+
module Notifications
|
|
11
|
+
class WebhookClient
|
|
12
|
+
class DeliveryError < StandardError; end
|
|
13
|
+
|
|
14
|
+
USER_AGENT = "ContextDrop-Webhook/1.0"
|
|
15
|
+
|
|
16
|
+
class << self
|
|
17
|
+
def deliver_destination(destination, occurrence)
|
|
18
|
+
url = destination.settings["url"].to_s.strip
|
|
19
|
+
raise DeliveryError, "Webhook URL missing" if url.empty?
|
|
20
|
+
|
|
21
|
+
uri = validate_https_uri!(url)
|
|
22
|
+
body = JSON.generate(occurrence.occurrence_payload)
|
|
23
|
+
headers = signed_headers(secret: destination.secrets["signing_secret"].to_s, body: body, msg_id: occurrence.event_id)
|
|
24
|
+
headers["Content-Type"] = "application/json"
|
|
25
|
+
headers["User-Agent"] = USER_AGENT
|
|
26
|
+
post_json(uri, body, headers)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def deliver_mcp_subscription(subscription, occurrence)
|
|
30
|
+
uri = validate_https_uri!(subscription.callback_url)
|
|
31
|
+
body = JSON.generate(occurrence.occurrence_payload)
|
|
32
|
+
headers = signed_headers(
|
|
33
|
+
secret: subscription.signing_secret,
|
|
34
|
+
body: body,
|
|
35
|
+
msg_id: occurrence.event_id
|
|
36
|
+
)
|
|
37
|
+
headers["Content-Type"] = "application/json"
|
|
38
|
+
headers["User-Agent"] = USER_AGENT
|
|
39
|
+
post_json(uri, body, headers)
|
|
40
|
+
subscription.update_columns(last_cursor: occurrence.cursor, updated_at: Time.current)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def validate_https_uri!(url)
|
|
44
|
+
uri = URI.parse(url)
|
|
45
|
+
raise DeliveryError, "Webhook URL must use HTTPS" unless uri.is_a?(URI::HTTPS)
|
|
46
|
+
raise DeliveryError, "Webhook URL host missing" if uri.host.to_s.strip.empty?
|
|
47
|
+
raise DeliveryError, "Webhook URL blocked (private network)" if private_or_local_host?(uri.host)
|
|
48
|
+
|
|
49
|
+
uri
|
|
50
|
+
rescue URI::InvalidURIError => e
|
|
51
|
+
raise DeliveryError, "Invalid webhook URL: #{e.message}"
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def signed_headers(secret:, body:, msg_id:)
|
|
55
|
+
timestamp = Time.now.to_i.to_s
|
|
56
|
+
base = "#{msg_id}.#{timestamp}.#{body}"
|
|
57
|
+
sig = if secret.to_s.strip.empty?
|
|
58
|
+
""
|
|
59
|
+
else
|
|
60
|
+
"v1,#{ContextDrop::Base64Util.strict_encode64(OpenSSL::HMAC.digest("SHA256", secret, base))}"
|
|
61
|
+
end
|
|
62
|
+
{
|
|
63
|
+
"webhook-id" => msg_id,
|
|
64
|
+
"webhook-timestamp" => timestamp,
|
|
65
|
+
"webhook-signature" => sig
|
|
66
|
+
}
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
private
|
|
70
|
+
|
|
71
|
+
def post_json(uri, body, headers)
|
|
72
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
|
73
|
+
http.use_ssl = uri.scheme == "https"
|
|
74
|
+
http.open_timeout = 8
|
|
75
|
+
http.read_timeout = 12
|
|
76
|
+
req = Net::HTTP::Post.new(uri.request_uri, headers)
|
|
77
|
+
req.body = body
|
|
78
|
+
res = http.request(req)
|
|
79
|
+
return res if res.is_a?(Net::HTTPSuccess)
|
|
80
|
+
|
|
81
|
+
raise DeliveryError, "HTTP #{res.code}: #{res.body.to_s.truncate(300)}"
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def private_or_local_host?(host)
|
|
85
|
+
down = host.to_s.strip.downcase
|
|
86
|
+
return true if %w[localhost metadata.google.internal].include?(down)
|
|
87
|
+
|
|
88
|
+
if down.match?(/\A\d{1,3}(\.\d{1,3}){3}\z/)
|
|
89
|
+
octets = down.split(".").map(&:to_i)
|
|
90
|
+
return true if octets[0] == 10
|
|
91
|
+
return true if octets[0] == 127
|
|
92
|
+
return true if octets[0] == 0
|
|
93
|
+
return true if octets[0] == 169 && octets[1] == 254
|
|
94
|
+
return true if octets[0] == 192 && octets[1] == 168
|
|
95
|
+
return true if octets[0] == 172 && octets[1] >= 16 && octets[1] <= 31
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
false
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
end
|
data/lib/context_drop/version.rb
CHANGED
data/lib/context_drop.rb
CHANGED
|
@@ -7,6 +7,11 @@ end
|
|
|
7
7
|
require_relative "context_drop/version"
|
|
8
8
|
require_relative "context_drop/base64_util"
|
|
9
9
|
require_relative "context_drop/configuration"
|
|
10
|
+
require_relative "context_drop/notification_secrets"
|
|
11
|
+
require_relative "context_drop/feedback_events/publisher"
|
|
12
|
+
require_relative "context_drop/notifications/webhook_client"
|
|
13
|
+
require_relative "context_drop/notifications/lark_client"
|
|
14
|
+
require_relative "context_drop/notifications/deliverer"
|
|
10
15
|
require_relative "context_drop/mcp/server"
|
|
11
16
|
require_relative "context_drop/agent/key_store"
|
|
12
17
|
require_relative "context_drop/agent/workspace_sandbox"
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: context_drop
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.3.
|
|
4
|
+
version: 0.3.5
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Kemal Elmizan
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-11-
|
|
11
|
+
date: 2026-11-24 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: actionpack
|
|
@@ -173,9 +173,11 @@ files:
|
|
|
173
173
|
- app/controllers/context_drop/attachments_controller.rb
|
|
174
174
|
- app/controllers/context_drop/configs_controller.rb
|
|
175
175
|
- app/controllers/context_drop/dashboard/feedbacks_controller.rb
|
|
176
|
+
- app/controllers/context_drop/dashboard/integrations_controller.rb
|
|
176
177
|
- app/controllers/context_drop/feedbacks_controller.rb
|
|
177
178
|
- app/controllers/context_drop/health_controller.rb
|
|
178
179
|
- app/controllers/context_drop/mcp_controller.rb
|
|
180
|
+
- app/controllers/context_drop/mcp_events_controller.rb
|
|
179
181
|
- app/helpers/context_drop/dashboard_helper.rb
|
|
180
182
|
- app/helpers/context_drop/widget_helper.rb
|
|
181
183
|
- app/javascript/context_drop/__tests__/api-client.test.ts
|
|
@@ -194,12 +196,19 @@ files:
|
|
|
194
196
|
- app/models/context_drop/attachment.rb
|
|
195
197
|
- app/models/context_drop/comment.rb
|
|
196
198
|
- app/models/context_drop/element_reference.rb
|
|
199
|
+
- app/models/context_drop/event_occurrence.rb
|
|
197
200
|
- app/models/context_drop/feedback.rb
|
|
201
|
+
- app/models/context_drop/mcp_event_subscription.rb
|
|
202
|
+
- app/models/context_drop/notification_destination.rb
|
|
198
203
|
- app/services/context_drop/metadata_sanitizer.rb
|
|
199
204
|
- app/services/context_drop/ownership_token.rb
|
|
200
205
|
- app/services/context_drop/upload_validator.rb
|
|
201
206
|
- app/views/context_drop/dashboard/feedbacks/index.html.erb
|
|
202
207
|
- app/views/context_drop/dashboard/feedbacks/show.html.erb
|
|
208
|
+
- app/views/context_drop/dashboard/integrations/_form.html.erb
|
|
209
|
+
- app/views/context_drop/dashboard/integrations/edit.html.erb
|
|
210
|
+
- app/views/context_drop/dashboard/integrations/index.html.erb
|
|
211
|
+
- app/views/context_drop/dashboard/integrations/new.html.erb
|
|
203
212
|
- app/views/layouts/context_drop/application.html.erb
|
|
204
213
|
- config/routes.rb
|
|
205
214
|
- db/migrate/20261109000001_create_context_drop_feedbacks.rb
|
|
@@ -208,6 +217,7 @@ files:
|
|
|
208
217
|
- db/migrate/20261112000004_add_environment_to_context_drop_feedbacks.rb
|
|
209
218
|
- db/migrate/20261113000005_add_deleted_at_to_context_drop_feedbacks.rb
|
|
210
219
|
- db/migrate/20261116000001_create_context_drop_comments_and_update_statuses.rb
|
|
220
|
+
- db/migrate/20261124000001_create_context_drop_events_and_notifications.rb
|
|
211
221
|
- lib/context_drop.rb
|
|
212
222
|
- lib/context_drop/agent/key_store.rb
|
|
213
223
|
- lib/context_drop/agent/llm_client.rb
|
|
@@ -216,8 +226,11 @@ files:
|
|
|
216
226
|
- lib/context_drop/base64_util.rb
|
|
217
227
|
- lib/context_drop/configuration.rb
|
|
218
228
|
- lib/context_drop/engine.rb
|
|
229
|
+
- lib/context_drop/feedback_events/publisher.rb
|
|
219
230
|
- lib/context_drop/mcp/dispatcher.rb
|
|
220
231
|
- lib/context_drop/mcp/environment_router.rb
|
|
232
|
+
- lib/context_drop/mcp/event_stream_registry.rb
|
|
233
|
+
- lib/context_drop/mcp/events.rb
|
|
221
234
|
- lib/context_drop/mcp/prompts/investigate_feedback.rb
|
|
222
235
|
- lib/context_drop/mcp/remote_client.rb
|
|
223
236
|
- lib/context_drop/mcp/server.rb
|
|
@@ -228,6 +241,10 @@ files:
|
|
|
228
241
|
- lib/context_drop/mcp/tools/list_environments.rb
|
|
229
242
|
- lib/context_drop/mcp/tools/list_feedbacks.rb
|
|
230
243
|
- lib/context_drop/mcp/tools/update_feedback_status.rb
|
|
244
|
+
- lib/context_drop/notification_secrets.rb
|
|
245
|
+
- lib/context_drop/notifications/deliverer.rb
|
|
246
|
+
- lib/context_drop/notifications/lark_client.rb
|
|
247
|
+
- lib/context_drop/notifications/webhook_client.rb
|
|
231
248
|
- lib/context_drop/version.rb
|
|
232
249
|
- lib/generators/context_drop/install/install_generator.rb
|
|
233
250
|
- lib/generators/context_drop/install/templates/initializer.rb.tt
|
|
@@ -248,14 +265,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
248
265
|
requirements:
|
|
249
266
|
- - ">="
|
|
250
267
|
- !ruby/object:Gem::Version
|
|
251
|
-
version: 3.
|
|
268
|
+
version: 3.3.14
|
|
252
269
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
253
270
|
requirements:
|
|
254
271
|
- - ">="
|
|
255
272
|
- !ruby/object:Gem::Version
|
|
256
273
|
version: '0'
|
|
257
274
|
requirements: []
|
|
258
|
-
rubygems_version: 3.
|
|
275
|
+
rubygems_version: 3.5.22
|
|
259
276
|
signing_key:
|
|
260
277
|
specification_version: 4
|
|
261
278
|
summary: Self-hosted contextual feedback engine, Shadow DOM widget, and MCP server
|