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
@@ -11,7 +11,7 @@ export interface ScreenshotCaptureResult {
11
11
  }
12
12
 
13
13
  export function collectRedactionRects(customExcludedSelectors: string[] = []): BoundingRectSnapshot[] {
14
- if (typeof document === "undefined") return [];
14
+ if (typeof document === "undefined" || typeof document.querySelectorAll !== "function") return [];
15
15
 
16
16
  const selectors = buildExcludedSelectorList(customExcludedSelectors);
17
17
  const selectorQuery = [
@@ -69,15 +69,23 @@ function dataUrlToBlob(dataUrl: string): Blob {
69
69
  return new Blob([bytes], { type: mime });
70
70
  }
71
71
 
72
+ export interface CaptureViewportOptions {
73
+ highlightRect?: BoundingRectSnapshot | null;
74
+ }
75
+
72
76
  export async function captureViewportScreenshot(
73
77
  customExcludedSelectors: string[] = [],
74
- cropRegion?: BoundingRectSnapshot
78
+ cropRegion?: BoundingRectSnapshot,
79
+ options?: CaptureViewportOptions
75
80
  ): Promise<ScreenshotCaptureResult> {
76
81
  if (typeof document === "undefined" || typeof window === "undefined") {
77
82
  throw new Error("Screenshot capture is only available in a browser environment.");
78
83
  }
79
84
 
80
- const widgets = Array.from(document.querySelectorAll("context-drop-widget, [data-context-drop-ui]")) as HTMLElement[];
85
+ const widgets =
86
+ typeof document.querySelectorAll === "function"
87
+ ? (Array.from(document.querySelectorAll("context-drop-widget, [data-context-drop-ui]")) as HTMLElement[])
88
+ : [];
81
89
  const previousVisibility = widgets.map((el) => el.style.visibility);
82
90
 
83
91
  try {
@@ -102,7 +110,7 @@ export async function captureViewportScreenshot(
102
110
  const canvas = document.createElement("canvas");
103
111
  canvas.width = Math.round(region.width);
104
112
  canvas.height = Math.round(region.height);
105
- const ctx = canvas.getContext("2d");
113
+ const ctx = typeof canvas.getContext === "function" ? canvas.getContext("2d") : null;
106
114
 
107
115
  if (!ctx) {
108
116
  throw new Error("Canvas 2D context is unavailable or restricted by browser policy.");
@@ -117,12 +125,15 @@ export async function captureViewportScreenshot(
117
125
  ctx.fillRect(0, 0, canvas.width, canvas.height);
118
126
 
119
127
  // Paint visible non-excluded DOM elements approximation onto canvas
120
- const visibleNodes = document.body ? Array.from(document.body.querySelectorAll("*")) : [];
128
+ const visibleNodes =
129
+ document.body && typeof document.body.querySelectorAll === "function"
130
+ ? Array.from(document.body.querySelectorAll("*"))
131
+ : [];
121
132
  for (const node of visibleNodes) {
122
133
  if (
123
- node.closest("context-drop-widget") ||
124
- node.hasAttribute("data-context-drop-ui") ||
125
- node.closest("[data-context-drop-ui]")
134
+ (typeof node.closest === "function" &&
135
+ (node.closest("context-drop-widget") || node.closest("[data-context-drop-ui]"))) ||
136
+ node.hasAttribute("data-context-drop-ui")
126
137
  ) {
127
138
  continue;
128
139
  }
@@ -148,9 +159,35 @@ export async function captureViewportScreenshot(
148
159
  ctx.lineWidth = Math.min(parseFloat(style.borderWidth), 4);
149
160
  ctx.strokeRect(rx, ry, r.width, r.height);
150
161
  }
162
+
163
+ // Render leaf text labels so surrounding-context previews are legible
164
+ if (node.children && node.children.length === 0 && typeof ctx.fillText === "function") {
165
+ const text = (node.textContent || "").replace(/\s+/g, " ").trim();
166
+ if (text.length > 0 && r.height >= 10) {
167
+ ctx.fillStyle = style.color || "#111318";
168
+ const fontSize = Math.min(18, Math.max(10, parseFloat(style.fontSize || "12") || 12));
169
+ ctx.font = `${style.fontWeight || "400"} ${Math.round(fontSize)}px sans-serif`;
170
+ ctx.fillText(text.slice(0, 48), rx + 4, ry + Math.min(r.height - 3, fontSize + 3));
171
+ }
172
+ }
151
173
  }
152
174
  }
153
175
 
176
+ // Optional visual callout box highlighting the selected component inside surrounding context
177
+ if (options?.highlightRect) {
178
+ const hr = options.highlightRect;
179
+ const hx = hr.x - region.x;
180
+ const hy = hr.y - region.y;
181
+ ctx.strokeStyle = "#c2410c";
182
+ ctx.lineWidth = 2;
183
+ ctx.strokeRect(
184
+ Math.max(1, hx),
185
+ Math.max(1, hy),
186
+ Math.min(region.width - 2, hr.width),
187
+ Math.min(region.height - 2, hr.height)
188
+ );
189
+ }
190
+
154
191
  // 2. Enforce pre-capture visual redaction of all sensitive/excluded elements
155
192
  for (const red of redactionRects) {
156
193
  const rx = red.x - region.x;
@@ -218,3 +255,60 @@ export async function captureElementScreenshot(
218
255
  }
219
256
  return result;
220
257
  }
258
+
259
+ /**
260
+ * Captures a privacy-redacted screenshot of a selected component together with generous
261
+ * surrounding page context (used automatically in Production "smart" mode) and frames the
262
+ * selected component inside the captured region.
263
+ */
264
+ export async function captureSurroundingContextScreenshot(
265
+ element: Element,
266
+ padding: number = 84,
267
+ customExcludedSelectors: string[] = [],
268
+ elementClientRef?: string
269
+ ): Promise<ScreenshotCaptureResult> {
270
+ if (isElementExcluded(element, customExcludedSelectors)) {
271
+ throw new Error("Cannot capture screenshot of an excluded or sensitive DOM element.");
272
+ }
273
+
274
+ const vw = typeof window !== "undefined" ? Math.max(window.innerWidth || 1024, 1) : 1024;
275
+ const vh = typeof window !== "undefined" ? Math.max(window.innerHeight || 768, 1) : 768;
276
+ const r = element.getBoundingClientRect();
277
+ const baseX = r.x ?? r.left ?? 0;
278
+ const baseY = r.y ?? r.top ?? 0;
279
+ const pad = Math.max(32, padding);
280
+
281
+ // Ensure a comfortable minimum surrounding neighborhood (340x220px) around small controls
282
+ const desiredWidth = Math.min(vw, Math.max(r.width + pad * 2, 340));
283
+ const desiredHeight = Math.min(vh, Math.max(r.height + pad * 2, 220));
284
+ const centerX = baseX + r.width / 2;
285
+ const centerY = baseY + r.height / 2;
286
+
287
+ const cropX = Math.max(0, Math.min(centerX - desiredWidth / 2, vw - desiredWidth));
288
+ const cropY = Math.max(0, Math.min(centerY - desiredHeight / 2, vh - desiredHeight));
289
+
290
+ const cropRegion: BoundingRectSnapshot = {
291
+ x: cropX,
292
+ y: cropY,
293
+ width: desiredWidth,
294
+ height: desiredHeight
295
+ };
296
+
297
+ const targetRect: BoundingRectSnapshot = {
298
+ x: baseX,
299
+ y: baseY,
300
+ width: r.width,
301
+ height: r.height
302
+ };
303
+
304
+ const result = await captureViewportScreenshot(customExcludedSelectors, cropRegion, {
305
+ highlightRect: targetRect
306
+ });
307
+ result.metadata.capture_type = "surrounding_context";
308
+ result.metadata.context_padding = pad;
309
+ result.metadata.highlighted_rect = targetRect;
310
+ if (elementClientRef) {
311
+ result.metadata.element_attachment_ref = elementClientRef;
312
+ }
313
+ return result;
314
+ }