context_drop 0.2.0 → 0.3.1

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 (40) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +149 -12
  3. data/Rakefile +110 -1
  4. data/app/assets/javascripts/context_drop/context_drop.js +1759 -105
  5. data/app/controllers/context_drop/application_controller.rb +52 -3
  6. data/app/controllers/context_drop/configs_controller.rb +10 -1
  7. data/app/controllers/context_drop/dashboard/feedbacks_controller.rb +99 -14
  8. data/app/controllers/context_drop/feedbacks_controller.rb +536 -17
  9. data/app/helpers/context_drop/dashboard_helper.rb +4 -0
  10. data/app/javascript/context_drop/__tests__/api-client.test.ts +255 -0
  11. data/app/javascript/context_drop/__tests__/widget.test.ts +355 -1
  12. data/app/javascript/context_drop/api-client.ts +343 -22
  13. data/app/javascript/context_drop/element-picker.ts +1 -1
  14. data/app/javascript/context_drop/icons.ts +6 -0
  15. data/app/javascript/context_drop/screenshot.ts +1 -1
  16. data/app/javascript/context_drop/styles.ts +311 -0
  17. data/app/javascript/context_drop/types.ts +141 -6
  18. data/app/javascript/context_drop/widget.ts +1072 -35
  19. data/app/models/context_drop/attachment.rb +5 -0
  20. data/app/models/context_drop/comment.rb +50 -0
  21. data/app/models/context_drop/feedback.rb +81 -11
  22. data/app/services/context_drop/ownership_token.rb +114 -0
  23. data/app/views/context_drop/dashboard/feedbacks/index.html.erb +14 -0
  24. data/app/views/context_drop/dashboard/feedbacks/show.html.erb +169 -2
  25. data/app/views/layouts/context_drop/application.html.erb +131 -7
  26. data/config/routes.rb +16 -1
  27. data/db/migrate/20261113000005_add_deleted_at_to_context_drop_feedbacks.rb +11 -0
  28. data/db/migrate/20261116000001_create_context_drop_comments_and_update_statuses.rb +44 -0
  29. data/lib/context_drop/agent/orchestrator.rb +1 -1
  30. data/lib/context_drop/configuration.rb +26 -0
  31. data/lib/context_drop/mcp/dispatcher.rb +2 -1
  32. data/lib/context_drop/mcp/prompts/investigate_feedback.rb +5 -3
  33. data/lib/context_drop/mcp/server.rb +1 -0
  34. data/lib/context_drop/mcp/tools/add_feedback_comment.rb +139 -0
  35. data/lib/context_drop/mcp/tools/get_feedback.rb +45 -6
  36. data/lib/context_drop/mcp/tools/get_feedback_attachment.rb +3 -0
  37. data/lib/context_drop/mcp/tools/list_feedbacks.rb +23 -3
  38. data/lib/context_drop/mcp/tools/update_feedback_status.rb +5 -3
  39. data/lib/context_drop/version.rb +1 -1
  40. metadata +7 -2
@@ -1,11 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "digest"
4
+ require "securerandom"
4
5
 
5
6
  module ContextDrop
6
7
  class ApplicationController < ActionController::Base
7
- protect_from_forgery with: :exception
8
+ protect_from_forgery with: :exception, unless: :verified_cross_origin_widget_request?
8
9
 
10
+ before_action :apply_widget_cors_headers!
9
11
  before_action :ensure_context_drop_enabled!
10
12
 
11
13
  rescue_from ActionController::InvalidAuthenticityToken, with: :handle_invalid_csrf_token
@@ -90,8 +92,19 @@ module ContextDrop
90
92
  end
91
93
  end
92
94
 
93
- def hashed_session_identifier
94
- raw_id = session.id.to_s if respond_to?(:session) && session.respond_to?(:id) && session.id
95
+ def hashed_session_identifier(initialize_if_missing: false)
96
+ raw_id = nil
97
+ if respond_to?(:session) && session
98
+ raw_id = session[:cd_sid].to_s if session.respond_to?(:[])
99
+ if raw_id.to_s.empty? && initialize_if_missing && session.respond_to?(:[]=)
100
+ generated = SecureRandom.hex(16)
101
+ session[:cd_sid] = generated
102
+ raw_id = generated
103
+ end
104
+ if raw_id.to_s.empty? && session.respond_to?(:id) && session.id
105
+ raw_id = session.id.to_s
106
+ end
107
+ end
95
108
  return nil if raw_id.nil? || raw_id.empty?
96
109
 
97
110
  Digest::SHA256.hexdigest(raw_id)[0, 32]
@@ -110,5 +123,41 @@ module ContextDrop
110
123
  def handle_sensitive_element_error(exception)
111
124
  render json: { error: exception.message }, status: :unprocessable_entity
112
125
  end
126
+
127
+ def cors_eligible_widget_endpoint?
128
+ is_a?(ContextDrop::ConfigsController) ||
129
+ is_a?(ContextDrop::AttachmentsController) ||
130
+ is_a?(ContextDrop::FeedbacksController) ||
131
+ is_a?(ContextDrop::HealthController)
132
+ end
133
+
134
+ def apply_widget_cors_headers!
135
+ return unless cors_eligible_widget_endpoint?
136
+
137
+ origin = request.headers["Origin"].to_s.strip
138
+ return if origin.empty? || !ContextDrop.config.origin_allowed?(origin)
139
+
140
+ response.set_header("Access-Control-Allow-Origin", origin)
141
+ response.set_header("Vary", "Origin")
142
+ response.set_header("Access-Control-Allow-Credentials", "true")
143
+ response.set_header("Access-Control-Allow-Methods", "GET, POST, PATCH, PUT, DELETE, OPTIONS")
144
+ response.set_header(
145
+ "Access-Control-Allow-Headers",
146
+ "Content-Type, Accept, X-CSRF-Token, X-Context-Drop-Client, X-Context-Drop-Ownership-Token, X-Context-Drop-Ownership-Tokens, Authorization"
147
+ )
148
+ response.set_header("Access-Control-Max-Age", "86400")
149
+ end
150
+
151
+ def verified_cross_origin_widget_request?
152
+ return false unless cors_eligible_widget_endpoint?
153
+
154
+ origin = request.headers["Origin"].to_s.strip
155
+ return false if origin.empty? || !ContextDrop.config.origin_allowed?(origin)
156
+
157
+ request.options? ||
158
+ request.headers["X-Context-Drop-Client"].to_s.strip == "1" ||
159
+ request.headers["X-Context-Drop-Ownership-Token"].to_s.strip.present? ||
160
+ request.content_type.to_s.downcase.start_with?("application/json")
161
+ end
113
162
  end
114
163
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  module ContextDrop
4
4
  class ConfigsController < ApplicationController
5
- skip_forgery_protection only: [:widget_bundle] if respond_to?(:skip_forgery_protection)
5
+ skip_forgery_protection only: %i[widget_bundle cors_preflight] if respond_to?(:skip_forgery_protection)
6
6
  before_action :enforce_submission_authorization!, only: [:show]
7
7
 
8
8
  def show
@@ -11,6 +11,15 @@ module ContextDrop
11
11
  render json: ContextDrop.config.public_widget_config(agent_enabled: agent_ok).merge(csrf_token: token), status: :ok
12
12
  end
13
13
 
14
+ def cors_preflight
15
+ origin = request.headers["Origin"].to_s.strip
16
+ if !origin.empty? && ContextDrop.config.origin_allowed?(origin)
17
+ head :no_content
18
+ else
19
+ head :forbidden
20
+ end
21
+ end
22
+
14
23
  def widget_bundle
15
24
  bundle_path = ContextDrop::Engine.root.join("app/assets/javascripts/context_drop/context_drop.js")
16
25
  if File.exist?(bundle_path)
@@ -13,21 +13,24 @@ module ContextDrop
13
13
 
14
14
  def index
15
15
  @status_counts = {
16
- "all" => ContextDrop::Feedback.count,
16
+ "all" => ContextDrop::Feedback.active.count,
17
17
  "new" => ContextDrop::Feedback.new_records.count,
18
+ "updated" => ContextDrop::Feedback.updated_records.count,
19
+ "fixed" => ContextDrop::Feedback.fixed.count,
18
20
  "reviewed" => ContextDrop::Feedback.reviewed.count,
19
- "resolved" => ContextDrop::Feedback.resolved.count,
20
- "dismissed" => ContextDrop::Feedback.dismissed.count
21
+ "dismissed" => ContextDrop::Feedback.dismissed.count,
22
+ "deleted" => ContextDrop::Feedback.deleted.count
21
23
  }
22
24
 
23
- scope = ContextDrop::Feedback.includes(:element_references, :attachments).recent
24
-
25
25
  @current_status = params[:status].to_s.strip
26
- if ContextDrop::Feedback::STATUSES.include?(@current_status)
27
- scope = scope.where(status: @current_status)
28
- else
29
- @current_status = ""
30
- end
26
+ scope = if @current_status == "deleted"
27
+ ContextDrop::Feedback.deleted.includes(:element_references, :attachments, :comments).recent
28
+ elsif ContextDrop::Feedback::STATUSES.include?(@current_status)
29
+ ContextDrop::Feedback.active.where(status: @current_status).includes(:element_references, :attachments, :comments).recent
30
+ else
31
+ @current_status = ""
32
+ ContextDrop::Feedback.active.includes(:element_references, :attachments, :comments).recent
33
+ end
31
34
 
32
35
  @page_url_filter = params[:page_url].to_s.strip
33
36
  if @page_url_filter.present?
@@ -37,8 +40,9 @@ module ContextDrop
37
40
  @search_query = (params[:q] || params[:query]).to_s.strip
38
41
  if @search_query.present?
39
42
  pattern = "%#{sanitize_sql_like(@search_query)}%"
40
- scope = scope.left_outer_joins(:element_references).where(
43
+ scope = scope.left_outer_joins(:element_references, :comments).where(
41
44
  "context_drop_feedbacks.message LIKE :q OR " \
45
+ "context_drop_comments.body LIKE :q OR " \
42
46
  "context_drop_element_references.selector LIKE :q OR " \
43
47
  "context_drop_element_references.dom_path LIKE :q OR " \
44
48
  "context_drop_element_references.text_content LIKE :q",
@@ -56,11 +60,13 @@ module ContextDrop
56
60
 
57
61
  def show
58
62
  @feedback = ContextDrop::Feedback
59
- .includes(:element_references, :attachments)
63
+ .includes(:element_references, :attachments, comments: :attachments)
60
64
  .find(params[:id])
61
65
 
66
+ nav_scope = @feedback.deleted? ? ContextDrop::Feedback.deleted : ContextDrop::Feedback.active
67
+
62
68
  # Newer feedback (previous in chronological newest-first list) and older feedback (next in list)
63
- @newer_feedback = ContextDrop::Feedback
69
+ @newer_feedback = nav_scope
64
70
  .where(
65
71
  "created_at > :ts OR (created_at = :ts AND id > :id)",
66
72
  ts: @feedback.created_at,
@@ -70,7 +76,7 @@ module ContextDrop
70
76
  .select(:id, :status)
71
77
  .first
72
78
 
73
- @older_feedback = ContextDrop::Feedback
79
+ @older_feedback = nav_scope
74
80
  .where(
75
81
  "created_at < :ts OR (created_at = :ts AND id < :id)",
76
82
  ts: @feedback.created_at,
@@ -111,6 +117,85 @@ module ContextDrop
111
117
  end
112
118
  end
113
119
 
120
+ def create_comment
121
+ @feedback = ContextDrop::Feedback.find(params[:id])
122
+ body = (params[:body] || params.dig(:comment, :body)).to_s.strip.presence
123
+ raw_files = Array(params[:images] || params.dig(:comment, :images)).reject(&:blank?)
124
+
125
+ if raw_files.size > ContextDrop.config.max_attachments.to_i
126
+ redirect_to dashboard_feedback_path(@feedback),
127
+ alert: "Too many images attached (maximum #{ContextDrop.config.max_attachments})."
128
+ return
129
+ end
130
+
131
+ comment = nil
132
+ ContextDrop::Feedback.transaction do
133
+ comment = @feedback.comments.build(
134
+ author_type: "Dashboard",
135
+ author_label: "Team",
136
+ body: body,
137
+ metadata: {}
138
+ )
139
+
140
+ pending_attachments = raw_files.map do |file|
141
+ validated = ContextDrop::UploadValidator.validate!(file)
142
+ att_meta = {}
143
+ if validated[:dimensions]
144
+ att_meta["image_dimensions"] = {
145
+ "width" => validated[:dimensions][0],
146
+ "height" => validated[:dimensions][1]
147
+ }
148
+ end
149
+ att = comment.attachments.build(
150
+ feedback: @feedback,
151
+ attachment_type: "image",
152
+ metadata: att_meta
153
+ )
154
+ [att, validated]
155
+ end
156
+
157
+ unless comment.valid?
158
+ redirect_to dashboard_feedback_path(@feedback), alert: comment.errors.full_messages.join(", ")
159
+ raise ActiveRecord::Rollback
160
+ end
161
+
162
+ comment.save!
163
+
164
+ pending_attachments.each do |att, validated|
165
+ next unless att.respond_to?(:file)
166
+
167
+ att.file.attach(
168
+ io: validated[:io],
169
+ filename: validated[:filename],
170
+ content_type: validated[:content_type]
171
+ )
172
+ end
173
+
174
+ @feedback.update!(status: "updated")
175
+ end
176
+
177
+ return if performed?
178
+
179
+ respond_to do |format|
180
+ format.json do
181
+ render json: {
182
+ id: comment.id,
183
+ feedback_id: @feedback.id,
184
+ status: @feedback.status,
185
+ created_at: comment.created_at&.iso8601
186
+ }, status: :created
187
+ end
188
+ format.html do
189
+ redirect_to dashboard_feedback_path(@feedback), notice: "Comment added to Feedback ##{@feedback.id}."
190
+ end
191
+ end
192
+ rescue ContextDrop::UploadValidator::ValidationError => e
193
+ respond_to do |format|
194
+ format.json { render json: { error: e.message }, status: :unprocessable_entity }
195
+ format.html { redirect_to dashboard_feedback_path(@feedback), alert: e.message }
196
+ end
197
+ end
198
+
114
199
  private
115
200
 
116
201
  def enforce_dashboard_authorization!