context_drop 0.1.3 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +218 -49
  3. data/SPEC.md +74 -26
  4. data/app/assets/javascripts/context_drop/context_drop.js +5227 -652
  5. data/app/controllers/context_drop/agent_controller.rb +170 -0
  6. data/app/controllers/context_drop/configs_controller.rb +2 -1
  7. data/app/controllers/context_drop/dashboard/feedbacks_controller.rb +62 -2
  8. data/app/controllers/context_drop/feedbacks_controller.rb +13 -3
  9. data/app/controllers/context_drop/mcp_controller.rb +2 -1
  10. data/app/helpers/context_drop/dashboard_helper.rb +52 -0
  11. data/app/helpers/context_drop/widget_helper.rb +11 -1
  12. data/app/javascript/context_drop/__tests__/widget.test.ts +731 -92
  13. data/app/javascript/context_drop/api-client.ts +130 -11
  14. data/app/javascript/context_drop/element-picker.ts +247 -30
  15. data/app/javascript/context_drop/icons.ts +24 -0
  16. data/app/javascript/context_drop/markdown.ts +146 -0
  17. data/app/javascript/context_drop/sanitizer.ts +413 -2
  18. data/app/javascript/context_drop/screenshot.ts +102 -8
  19. data/app/javascript/context_drop/styles.ts +1528 -252
  20. data/app/javascript/context_drop/types.ts +147 -0
  21. data/app/javascript/context_drop/widget.ts +2629 -288
  22. data/app/models/context_drop/feedback.rb +34 -5
  23. data/app/views/context_drop/dashboard/feedbacks/index.html.erb +166 -60
  24. data/app/views/context_drop/dashboard/feedbacks/show.html.erb +178 -47
  25. data/app/views/layouts/context_drop/application.html.erb +416 -70
  26. data/config/routes.rb +11 -0
  27. data/db/migrate/20261112000004_add_environment_to_context_drop_feedbacks.rb +10 -0
  28. data/lib/context_drop/agent/key_store.rb +290 -0
  29. data/lib/context_drop/agent/llm_client.rb +413 -0
  30. data/lib/context_drop/agent/orchestrator.rb +314 -0
  31. data/lib/context_drop/agent/workspace_sandbox.rb +499 -0
  32. data/lib/context_drop/configuration.rb +216 -3
  33. data/lib/context_drop/mcp/dispatcher.rb +71 -29
  34. data/lib/context_drop/mcp/environment_router.rb +115 -0
  35. data/lib/context_drop/mcp/prompts/investigate_feedback.rb +87 -8
  36. data/lib/context_drop/mcp/remote_client.rb +120 -0
  37. data/lib/context_drop/mcp/server.rb +9 -2
  38. data/lib/context_drop/mcp/tools/get_feedback.rb +50 -4
  39. data/lib/context_drop/mcp/tools/get_feedback_attachment.rb +26 -4
  40. data/lib/context_drop/mcp/tools/list_environments.rb +60 -0
  41. data/lib/context_drop/mcp/tools/list_feedbacks.rb +174 -22
  42. data/lib/context_drop/mcp/tools/update_feedback_status.rb +45 -4
  43. data/lib/context_drop/version.rb +1 -1
  44. data/lib/context_drop.rb +8 -2
  45. data/lib/generators/context_drop/install/templates/initializer.rb.tt +29 -2
  46. metadata +13 -2
@@ -0,0 +1,170 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "uri"
4
+
5
+ module ContextDrop
6
+ class AgentController < ApplicationController
7
+ before_action :enforce_local_agent_security_wall!
8
+
9
+ def status
10
+ key_store = ContextDrop::Agent::KeyStore.new
11
+ provider_info = key_store.providers_status
12
+
13
+ render json: {
14
+ enabled: true,
15
+ environment: ContextDrop.config.resolved_environment,
16
+ workspace_name: File.basename(ContextDrop.config.resolved_workspace_root),
17
+ mcp_environments: ContextDrop::MCP::EnvironmentRouter.available_environments,
18
+ active_provider: provider_info[:active_provider],
19
+ providers: provider_info[:providers]
20
+ }, status: :ok
21
+ end
22
+
23
+ def save_keys
24
+ provider = params[:provider].to_s.strip.downcase
25
+ api_key = params.key?(:api_key) ? params[:api_key].to_s : nil
26
+ model = params.key?(:model) ? params[:model].to_s : nil
27
+ base_url = params.key?(:base_url) ? params[:base_url].to_s : nil
28
+ make_active = params.key?(:make_active) ? ActiveModel::Type::Boolean.new.cast(params[:make_active]) : true
29
+
30
+ if provider == "custom" && base_url && !base_url.strip.empty?
31
+ ContextDrop::Agent::LlmClient.validate_custom_base_url!(base_url, resolve_dns: false)
32
+ end
33
+
34
+ key_store = ContextDrop::Agent::KeyStore.new
35
+ updated = key_store.save_provider_config!(
36
+ provider: provider,
37
+ api_key: api_key,
38
+ model: model,
39
+ base_url: base_url,
40
+ make_active: make_active
41
+ )
42
+
43
+ render json: updated, status: :ok
44
+ rescue ContextDrop::Error => e
45
+ render json: { error: e.message }, status: :unprocessable_entity
46
+ end
47
+
48
+ def destroy_keys
49
+ provider = (params[:provider] || "all").to_s
50
+ key_store = ContextDrop::Agent::KeyStore.new
51
+ updated = key_store.clear_provider_key!(provider: provider)
52
+
53
+ render json: updated, status: :ok
54
+ rescue ContextDrop::Error => e
55
+ render json: { error: e.message }, status: :unprocessable_entity
56
+ end
57
+
58
+ def feedbacks
59
+ env_filter = (params[:environment] || "all").to_s
60
+ status_filter = (params[:status] || "new").to_s
61
+ res = ContextDrop::MCP::Tools::ListFeedbacks.call(
62
+ "environment" => env_filter,
63
+ "status" => status_filter == "all" ? "" : status_filter,
64
+ "limit" => (params[:limit] || 20).to_i
65
+ )
66
+
67
+ if res[:isError]
68
+ err = JSON.parse(res.dig(:content, 0, :text).to_s)["error"] rescue "Failed to list feedbacks"
69
+ render json: { error: err }, status: :unprocessable_entity
70
+ else
71
+ render json: JSON.parse(res.dig(:content, 0, :text).to_s), status: :ok
72
+ end
73
+ end
74
+
75
+ def chat
76
+ raw_messages = params[:messages].is_a?(Array) ? params[:messages].map { |m| to_plain_hash(m) } : []
77
+ raw_elements = params[:element_references].is_a?(Array) ? params[:element_references].map { |e| to_plain_hash(e) } : []
78
+ raw_attachments = params[:attachments].is_a?(Array) ? params[:attachments].map { |a| to_plain_hash(a) } : []
79
+
80
+ orchestrator = ContextDrop::Agent::Orchestrator.new
81
+ result = orchestrator.run_turn(
82
+ messages: raw_messages,
83
+ page_url: params[:page_url].to_s.strip.presence || request.referer,
84
+ element_references: raw_elements,
85
+ attachments: raw_attachments,
86
+ provider: params[:provider].to_s.strip.presence,
87
+ model: params[:model].to_s.strip.presence
88
+ )
89
+
90
+ render json: result, status: :ok
91
+ rescue ContextDrop::Error => e
92
+ render json: { error: e.message }, status: :unprocessable_entity
93
+ end
94
+
95
+ def apply_edit
96
+ sandbox = ContextDrop::Agent::WorkspaceSandbox.new
97
+ updated = sandbox.apply_edit!(params[:id].to_s)
98
+ render json: { edit: updated }, status: :ok
99
+ rescue ContextDrop::Error => e
100
+ render json: { error: e.message }, status: :unprocessable_entity
101
+ end
102
+
103
+ def reject_edit
104
+ sandbox = ContextDrop::Agent::WorkspaceSandbox.new
105
+ updated = sandbox.reject_edit!(params[:id].to_s)
106
+ render json: { edit: updated }, status: :ok
107
+ rescue ContextDrop::Error => e
108
+ render json: { error: e.message }, status: :unprocessable_entity
109
+ end
110
+
111
+ def revert_edit
112
+ sandbox = ContextDrop::Agent::WorkspaceSandbox.new
113
+ updated = sandbox.revert_edit!(params[:id].to_s)
114
+ render json: { edit: updated }, status: :ok
115
+ rescue ContextDrop::Error => e
116
+ render json: { error: e.message }, status: :unprocessable_entity
117
+ end
118
+
119
+ private
120
+
121
+ # Defense-in-depth gate:
122
+ # 1. Hard-disabled in production or non-development environments (`404 Not Found`).
123
+ # 2. Loopback IP + loopback Host header required (blocks LAN/tunnel exposure & DNS rebinding).
124
+ # 3. Same-Origin header verification on mutating requests (blocks cross-origin browser attacks).
125
+ # 4. Dashboard / developer authorization policy check.
126
+ def enforce_local_agent_security_wall!
127
+ unless ContextDrop.config.local_agent_allowed?
128
+ render json: { error: "Not Found" }, status: :not_found
129
+ return
130
+ end
131
+
132
+ unless ContextDrop.config.loopback_request?(request)
133
+ render json: { error: "Forbidden: ContextDrop Agent is restricted to local loopback (127.0.0.1 / ::1)" },
134
+ status: :forbidden
135
+ return
136
+ end
137
+
138
+ if !request.get? && !request.head? && !valid_same_origin_request?
139
+ render json: { error: "Forbidden: Cross-origin request blocked" }, status: :forbidden
140
+ return
141
+ end
142
+
143
+ return if ContextDrop.config.dashboard_authorized?(self)
144
+
145
+ render json: { error: "Forbidden: Unauthorized for ContextDrop Local Agent" }, status: :forbidden
146
+ end
147
+
148
+ def valid_same_origin_request?
149
+ origin = request.headers["Origin"].to_s.strip
150
+ return true if origin.empty? # Same-origin non-CORS or test harness without Origin; CSRF still protects POST/DELETE
151
+
152
+ origin_uri = URI.parse(origin)
153
+ expected_host = request.host.to_s.downcase
154
+ expected_port = request.port
155
+ origin_uri.host.to_s.downcase == expected_host && origin_uri.port == expected_port
156
+ rescue URI::InvalidURIError
157
+ false
158
+ end
159
+
160
+ def to_plain_hash(obj)
161
+ if obj.respond_to?(:to_unsafe_h)
162
+ obj.to_unsafe_h.transform_keys(&:to_s)
163
+ elsif obj.is_a?(Hash)
164
+ obj.transform_keys(&:to_s)
165
+ else
166
+ {}
167
+ end
168
+ end
169
+ end
170
+ end
@@ -7,7 +7,8 @@ module ContextDrop
7
7
 
8
8
  def show
9
9
  token = form_authenticity_token if respond_to?(:form_authenticity_token, true)
10
- render json: ContextDrop.config.public_widget_config.merge(csrf_token: token), status: :ok
10
+ agent_ok = ContextDrop.config.local_agent_allowed? && ContextDrop.config.loopback_request?(request)
11
+ render json: ContextDrop.config.public_widget_config(agent_enabled: agent_ok).merge(csrf_token: token), status: :ok
11
12
  end
12
13
 
13
14
  def widget_bundle
@@ -4,8 +4,10 @@ module ContextDrop
4
4
  module Dashboard
5
5
  class FeedbacksController < ApplicationController
6
6
  layout "context_drop/application"
7
+ helper ContextDrop::DashboardHelper
7
8
 
8
9
  before_action :enforce_dashboard_authorization!
10
+ before_action :resolve_navigation_shortcuts
9
11
 
10
12
  PER_PAGE = 25
11
13
 
@@ -18,7 +20,7 @@ module ContextDrop
18
20
  "dismissed" => ContextDrop::Feedback.dismissed.count
19
21
  }
20
22
 
21
- scope = ContextDrop::Feedback.includes(:submitter, :element_references, :attachments).recent
23
+ scope = ContextDrop::Feedback.includes(:element_references, :attachments).recent
22
24
 
23
25
  @current_status = params[:status].to_s.strip
24
26
  if ContextDrop::Feedback::STATUSES.include?(@current_status)
@@ -48,12 +50,35 @@ module ContextDrop
48
50
  @page = [params[:page].to_i, 1].max
49
51
  @total_pages = [(@total_filtered.to_f / PER_PAGE).ceil, 1].max
50
52
  @feedbacks = scope.limit(PER_PAGE).offset((@page - 1) * PER_PAGE)
53
+
54
+ session[:context_drop_dashboard_index_url] = request.fullpath if respond_to?(:session) && request&.get?
51
55
  end
52
56
 
53
57
  def show
54
58
  @feedback = ContextDrop::Feedback
55
- .includes(:submitter, :element_references, :attachments)
59
+ .includes(:element_references, :attachments)
56
60
  .find(params[:id])
61
+
62
+ # Newer feedback (previous in chronological newest-first list) and older feedback (next in list)
63
+ @newer_feedback = ContextDrop::Feedback
64
+ .where(
65
+ "created_at > :ts OR (created_at = :ts AND id > :id)",
66
+ ts: @feedback.created_at,
67
+ id: @feedback.id
68
+ )
69
+ .order(created_at: :asc, id: :asc)
70
+ .select(:id, :status)
71
+ .first
72
+
73
+ @older_feedback = ContextDrop::Feedback
74
+ .where(
75
+ "created_at < :ts OR (created_at = :ts AND id < :id)",
76
+ ts: @feedback.created_at,
77
+ id: @feedback.id
78
+ )
79
+ .order(created_at: :desc, id: :desc)
80
+ .select(:id, :status)
81
+ .first
57
82
  end
58
83
 
59
84
  def update
@@ -102,6 +127,41 @@ module ContextDrop
102
127
  rescue StandardError
103
128
  str.to_s.gsub(/[%_\\]/) { |m| "\\#{m}" }
104
129
  end
130
+
131
+ def resolve_navigation_shortcuts
132
+ @host_app_path = resolve_host_app_path
133
+ @triage_index_back_path = if respond_to?(:session) && session[:context_drop_dashboard_index_url].present?
134
+ session[:context_drop_dashboard_index_url]
135
+ else
136
+ dashboard_feedbacks_path
137
+ end
138
+ end
139
+
140
+ def resolve_host_app_path
141
+ engine_prefix = (request.respond_to?(:script_name) && request.script_name.presence) || "/context_drop"
142
+ if respond_to?(:session) && request&.referer.present?
143
+ begin
144
+ ref_uri = URI.parse(request.referer)
145
+ same_host = ref_uri.host.nil? || (request.respond_to?(:host) && ref_uri.host == request.host)
146
+ if same_host && ref_uri.path.present? && !ref_uri.path.start_with?(engine_prefix)
147
+ full_ref = [ref_uri.path, ref_uri.query].compact.join("?")
148
+ session[:context_drop_return_to] = full_ref
149
+ end
150
+ rescue URI::InvalidURIError
151
+ # Ignore malformed referer headers
152
+ end
153
+ end
154
+
155
+ return session[:context_drop_return_to] if respond_to?(:session) && session[:context_drop_return_to].present?
156
+
157
+ if respond_to?(:main_app) && main_app.respond_to?(:root_path)
158
+ main_app.root_path
159
+ else
160
+ "/"
161
+ end
162
+ rescue StandardError
163
+ "/"
164
+ end
105
165
  end
106
166
  end
107
167
  end
@@ -27,15 +27,24 @@ module ContextDrop
27
27
  feedback = nil
28
28
 
29
29
  ContextDrop::Feedback.transaction do
30
- feedback = ContextDrop::Feedback.new(
30
+ sanitized_meta = ContextDrop::MetadataSanitizer.sanitize_feedback_metadata(payload["metadata"])
31
+ host_env = ContextDrop.config.resolved_environment
32
+ sanitized_meta["environment"] = host_env
33
+
34
+ feedback_attrs = {
31
35
  submitter: current_context_drop_submitter,
32
36
  session_id: hashed_session_identifier,
33
37
  message: payload["message"].to_s.strip.presence,
34
38
  page_url: (payload["page_url"].presence || request.referer).to_s.strip,
35
39
  user_agent: (payload["user_agent"].presence || request.user_agent).to_s.strip.presence,
36
- metadata: ContextDrop::MetadataSanitizer.sanitize_feedback_metadata(payload["metadata"]),
40
+ metadata: sanitized_meta,
37
41
  status: "new"
38
- )
42
+ }
43
+ if ContextDrop::Feedback.column_names.include?("environment")
44
+ feedback_attrs[:environment] = host_env
45
+ end
46
+
47
+ feedback = ContextDrop::Feedback.new(feedback_attrs)
39
48
 
40
49
  element_refs_by_client_ref = {}
41
50
  raw_elements.each_with_index do |raw_el, idx|
@@ -99,6 +108,7 @@ module ContextDrop
99
108
 
100
109
  render json: {
101
110
  id: feedback.id,
111
+ environment: feedback.environment_name,
102
112
  status: feedback.status,
103
113
  attachments_count: feedback.attachments.size,
104
114
  element_references_count: feedback.element_references.size,
@@ -13,7 +13,8 @@ module ContextDrop
13
13
  def create
14
14
  raw_body = request.raw_post.to_s
15
15
  payload = JSON.parse(raw_body)
16
- response = ContextDrop::MCP::Dispatcher.call(payload)
16
+ # Remote HTTP endpoints strictly serve their own host environment and never recursively fan out
17
+ response = ContextDrop::MCP::Server.handle_http_payload(payload, local_only: true)
17
18
 
18
19
  if response.nil?
19
20
  head :accepted
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "uri"
4
+
5
+ module ContextDrop
6
+ module DashboardHelper
7
+ def url_path_and_host(raw_url)
8
+ return ["/", nil] if raw_url.blank?
9
+
10
+ uri = URI.parse(raw_url.to_s)
11
+ host = uri.host.presence
12
+ path = uri.path.presence || "/"
13
+ query = uri.query.present? ? "?#{uri.query}" : ""
14
+ [path + query, host]
15
+ rescue URI::InvalidURIError
16
+ [raw_url.to_s, nil]
17
+ end
18
+
19
+ def cd_icon_arrow_left(size: 14)
20
+ %(<svg width="#{size}" height="#{size}" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><line x1="19" y1="12" x2="5" y2="12"></line><polyline points="12 19 5 12 12 5"></polyline></svg>).html_safe
21
+ end
22
+
23
+ def cd_icon_arrow_right(size: 14)
24
+ %(<svg width="#{size}" height="#{size}" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><line x1="5" y1="12" x2="19" y2="12"></line><polyline points="12 5 19 12 12 19"></polyline></svg>).html_safe
25
+ end
26
+
27
+ def cd_icon_search(size: 14)
28
+ %(<svg width="#{size}" height="#{size}" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><circle cx="11" cy="11" r="8"></circle><line x1="21" y1="21" x2="16.65" y2="16.65"></line></svg>).html_safe
29
+ end
30
+
31
+ def cd_icon_link(size: 14)
32
+ %(<svg width="#{size}" height="#{size}" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg>).html_safe
33
+ end
34
+
35
+ def cd_icon_external_link(size: 13)
36
+ %(<svg width="#{size}" height="#{size}" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M18 13v6a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h6"></path><polyline points="15 3 21 3 21 9"></polyline><line x1="10" y1="14" x2="21" y2="3"></line></svg>).html_safe
37
+ end
38
+
39
+ def cd_icon_pin(size: 13)
40
+ %(<svg width="#{size}" height="#{size}" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M12 2a8 8 0 0 0-8 8c0 5.25 8 12 8 12s8-6.75 8-12a8 8 0 0 0-8-8z"></path><circle cx="12" cy="10" r="3"></circle></svg>).html_safe
41
+ end
42
+
43
+ def cd_icon_attachment(size: 13)
44
+ %(<svg width="#{size}" height="#{size}" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M21.44 11.05l-9.19 9.19a6 6 0 0 1-8.49-8.49l9.19-9.19a4 4 0 0 1 5.66 5.66l-9.2 9.19a2 2 0 0 1-2.83-2.83l8.49-8.48"></path></svg>).html_safe
45
+ end
46
+
47
+ def cd_icon_user(size: 13)
48
+ %(<svg width="#{size}" height="#{size}" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M20 21v-2a4 4 0 0 0-4-4H8a4 4 0 0 0-4 4v2"></path><circle cx="12" cy="7" r="4"></circle></svg>).html_safe
49
+ end
50
+ end
51
+ end
52
+
@@ -16,7 +16,17 @@ module ContextDrop
16
16
  controller_instance = respond_to?(:controller) ? controller : nil
17
17
  return nil unless cfg.submission_authorized?(controller_instance)
18
18
 
19
- widget_config = cfg.public_widget_config(options)
19
+ req = if respond_to?(:request)
20
+ request
21
+ elsif controller_instance.respond_to?(:request)
22
+ controller_instance.request
23
+ end
24
+ merged_options = if req
25
+ options.merge(agent_enabled: cfg.local_agent_allowed? && cfg.loopback_request?(req))
26
+ else
27
+ options
28
+ end
29
+ widget_config = cfg.public_widget_config(merged_options)
20
30
  mount_path = resolve_context_drop_mount_path(options[:endpoint])
21
31
  custom_metadata = options[:metadata].is_a?(Hash) ? options[:metadata] : {}
22
32