context-drop 0.0.0-stage → 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.
- package/MIT-LICENSE +21 -0
- package/README.md +709 -2
- package/app/assets/javascripts/context_drop/context_drop.js +8936 -0
- package/dist/app/javascript/context_drop/api-client.d.ts +41 -0
- package/dist/app/javascript/context_drop/api-client.js +279 -0
- package/dist/app/javascript/context_drop/element-picker.d.ts +111 -0
- package/dist/app/javascript/context_drop/element-picker.js +1428 -0
- package/dist/app/javascript/context_drop/icons.d.ts +32 -0
- package/dist/app/javascript/context_drop/icons.js +32 -0
- package/dist/app/javascript/context_drop/index.d.ts +7 -0
- package/dist/app/javascript/context_drop/index.js +9 -0
- package/dist/app/javascript/context_drop/markdown.d.ts +7 -0
- package/dist/app/javascript/context_drop/markdown.js +129 -0
- package/dist/app/javascript/context_drop/sanitizer.d.ts +38 -0
- package/dist/app/javascript/context_drop/sanitizer.js +769 -0
- package/dist/app/javascript/context_drop/screenshot.d.ts +21 -0
- package/dist/app/javascript/context_drop/screenshot.js +245 -0
- package/dist/app/javascript/context_drop/styles.d.ts +1 -0
- package/dist/app/javascript/context_drop/styles.js +2015 -0
- package/dist/app/javascript/context_drop/types.d.ts +239 -0
- package/dist/app/javascript/context_drop/types.js +1 -0
- package/dist/app/javascript/context_drop/widget.d.ts +188 -0
- package/dist/app/javascript/context_drop/widget.js +3227 -0
- package/dist/src/cli.d.ts +3 -0
- package/dist/src/cli.js +207 -0
- package/dist/src/index.d.ts +5 -0
- package/dist/src/index.js +5 -0
- package/dist/src/mcp/dispatcher.d.ts +42 -0
- package/dist/src/mcp/dispatcher.js +869 -0
- package/dist/src/mcp/environment-router.d.ts +38 -0
- package/dist/src/mcp/environment-router.js +174 -0
- package/dist/src/mcp/index.d.ts +24 -0
- package/dist/src/mcp/index.js +75 -0
- package/dist/src/mcp/remote-client.d.ts +25 -0
- package/dist/src/mcp/remote-client.js +98 -0
- package/dist/src/next/index.d.ts +13 -0
- package/dist/src/next/index.js +25 -0
- package/dist/src/next/server.d.ts +1 -0
- package/dist/src/next/server.js +1 -0
- package/dist/src/react/index.d.ts +90 -0
- package/dist/src/react/index.js +124 -0
- package/dist/src/server/handler.d.ts +42 -0
- package/dist/src/server/handler.js +677 -0
- package/dist/src/server/index.d.ts +8 -0
- package/dist/src/server/index.js +8 -0
- package/dist/src/server/sanitizer.d.ts +15 -0
- package/dist/src/server/sanitizer.js +201 -0
- package/dist/src/server/storage/local.d.ts +22 -0
- package/dist/src/server/storage/local.js +574 -0
- package/dist/src/server/storage/memory.d.ts +5 -0
- package/dist/src/server/storage/memory.js +221 -0
- package/dist/src/server/storage/postgres.d.ts +27 -0
- package/dist/src/server/storage/postgres.js +446 -0
- package/dist/src/server/types.d.ts +190 -0
- package/dist/src/server/types.js +7 -0
- package/dist/src/server/upload-validator.d.ts +16 -0
- package/dist/src/server/upload-validator.js +196 -0
- package/dist/src/widget/index.d.ts +1 -0
- package/dist/src/widget/index.js +1 -0
- package/package.json +99 -4
|
@@ -0,0 +1,3227 @@
|
|
|
1
|
+
import { MANDATORY_EXCLUDED_SELECTORS, describeSmartComponent } from "./sanitizer.js";
|
|
2
|
+
import { ICON_CAMERA, ICON_CHECK, ICON_CHEVRON_DOWN, ICON_CHEVRON_RIGHT, ICON_CHEVRON_UP, ICON_CLOSE, ICON_CODE_PROMPT, ICON_EXPAND, ICON_FILE_CODE, ICON_GRIP, ICON_IMAGE, ICON_INBOX, ICON_KEY, ICON_MINIMIZE, ICON_PIN, ICON_PLUS, ICON_PUSH_LAYOUT, ICON_SCROLL_VERTICAL, ICON_SEND, ICON_SHIELD, ICON_SIDEBAR, ICON_SUN, ICON_MOON, ICON_UNDO } from "./icons.js";
|
|
3
|
+
import { WIDGET_SHADOW_STYLES } from "./styles.js";
|
|
4
|
+
import { ContextDropApiClient } from "./api-client.js";
|
|
5
|
+
import { ElementPicker } from "./element-picker.js";
|
|
6
|
+
import { captureElementScreenshot, captureSurroundingContextScreenshot, captureViewportScreenshot } from "./screenshot.js";
|
|
7
|
+
import { buildUnifiedDiffElement, renderSafeMarkdown } from "./markdown.js";
|
|
8
|
+
const CORNER_SNAP_ZONE_PX = 68;
|
|
9
|
+
const PEEL_DRAG_THRESHOLD_PX = 6;
|
|
10
|
+
const DEFAULT_PROVIDERS_FALLBACK = [
|
|
11
|
+
{
|
|
12
|
+
id: "anthropic",
|
|
13
|
+
label: "Anthropic Claude",
|
|
14
|
+
configured: false,
|
|
15
|
+
source: null,
|
|
16
|
+
env_var_hint: "ANTHROPIC_API_KEY",
|
|
17
|
+
masked_key: null,
|
|
18
|
+
model: "claude-sonnet-4-20250514",
|
|
19
|
+
default_model: "claude-sonnet-4-20250514",
|
|
20
|
+
models: ["claude-sonnet-4-20250514", "claude-3-7-sonnet-latest", "claude-3-5-haiku-latest"],
|
|
21
|
+
supports_base_url: false
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
id: "gemini",
|
|
25
|
+
label: "Google Gemini",
|
|
26
|
+
configured: false,
|
|
27
|
+
source: null,
|
|
28
|
+
env_var_hint: "GEMINI_API_KEY",
|
|
29
|
+
masked_key: null,
|
|
30
|
+
model: "gemini-2.5-pro",
|
|
31
|
+
default_model: "gemini-2.5-pro",
|
|
32
|
+
models: ["gemini-2.5-pro", "gemini-2.5-flash"],
|
|
33
|
+
supports_base_url: false
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
id: "openai",
|
|
37
|
+
label: "OpenAI",
|
|
38
|
+
configured: false,
|
|
39
|
+
source: null,
|
|
40
|
+
env_var_hint: "OPENAI_API_KEY",
|
|
41
|
+
masked_key: null,
|
|
42
|
+
model: "gpt-4.1",
|
|
43
|
+
default_model: "gpt-4.1",
|
|
44
|
+
models: ["gpt-4.1", "gpt-4.1-mini", "o4-mini"],
|
|
45
|
+
supports_base_url: false
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
id: "custom",
|
|
49
|
+
label: "OpenAI-Compatible (OpenRouter / Ollama)",
|
|
50
|
+
configured: false,
|
|
51
|
+
source: null,
|
|
52
|
+
env_var_hint: "CONTEXT_DROP_LLM_API_KEY",
|
|
53
|
+
masked_key: null,
|
|
54
|
+
model: "anthropic/claude-sonnet-4",
|
|
55
|
+
default_model: "anthropic/claude-sonnet-4",
|
|
56
|
+
models: ["anthropic/claude-sonnet-4", "google/gemini-2.5-pro", "llama3.3"],
|
|
57
|
+
supports_base_url: true,
|
|
58
|
+
base_url: "https://openrouter.ai/api/v1"
|
|
59
|
+
}
|
|
60
|
+
];
|
|
61
|
+
const BOOK_CORNER_SVG = `<svg class="cd-book-corner-svg" viewBox="0 0 40 40" fill="none" aria-hidden="true"><polygon points="0,0 36,0 0,36" fill="#c2410c"/><polygon points="0,0 36,0 0,36" fill="rgba(17, 19, 24, 0.08)"/><path d="M12.2 6.2l4.1 4.1-1.7 1.7-1.4-.5-2.5 2.5.5 2-1.3 1.3-2.4-2.4-2.3 2.3 2.3-2.3-2.4-2.4 1.3-1.3 2 .5 2.5-2.5-.5-1.4 1.8-1.6z" stroke="#faf9f6" stroke-width="1.35" stroke-linecap="round" stroke-linejoin="round"/><path d="M36 0Q35.5 21 33.5 33.5Q21 35.5 0 36Z" fill="rgba(17, 19, 24, 0.16)"/><path class="cd-corner-flap" d="M36 0Q34.5 18.5 31 31Q18.5 34.5 0 36L36 0Z" fill="#faf9f6" stroke="#d6d3cd" stroke-width="1.2" stroke-linejoin="round"/></svg>`;
|
|
62
|
+
const DEFAULT_VERBOSE_PLACEHOLDER = "Describe the issue or drop context from the page...";
|
|
63
|
+
const DEFAULT_SMART_PLACEHOLDER = "Tell us what's happening...";
|
|
64
|
+
export const DEFAULT_SIDEBAR_WIDTH = 560;
|
|
65
|
+
const DEFAULT_CONFIG = {
|
|
66
|
+
enabled: true,
|
|
67
|
+
environment: "production",
|
|
68
|
+
position: "bottom_right",
|
|
69
|
+
placeholder: DEFAULT_VERBOSE_PLACEHOLDER,
|
|
70
|
+
agent: {
|
|
71
|
+
enabled: false
|
|
72
|
+
},
|
|
73
|
+
features: {
|
|
74
|
+
element_selection: true,
|
|
75
|
+
screenshot_capture: true,
|
|
76
|
+
element_capture_padding: 16,
|
|
77
|
+
smart_capture_padding: 84
|
|
78
|
+
},
|
|
79
|
+
limits: {
|
|
80
|
+
max_attachments: 10,
|
|
81
|
+
max_element_references: 10,
|
|
82
|
+
max_attachment_size: 10 * 1024 * 1024,
|
|
83
|
+
allowed_content_types: ["image/png", "image/jpeg", "image/webp", "image/gif"],
|
|
84
|
+
max_image_dimensions: [4096, 4096]
|
|
85
|
+
},
|
|
86
|
+
privacy: {
|
|
87
|
+
excluded_selectors: Array.from(MANDATORY_EXCLUDED_SELECTORS)
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
const BaseHTMLElement = typeof HTMLElement !== "undefined"
|
|
91
|
+
? HTMLElement
|
|
92
|
+
: class {
|
|
93
|
+
};
|
|
94
|
+
export class ContextDropWidgetElement extends BaseHTMLElement {
|
|
95
|
+
static get observedAttributes() {
|
|
96
|
+
return [
|
|
97
|
+
"data-endpoint",
|
|
98
|
+
"data-position",
|
|
99
|
+
"data-placeholder",
|
|
100
|
+
"data-config",
|
|
101
|
+
"data-metadata",
|
|
102
|
+
"data-credentials",
|
|
103
|
+
"data-selection-mode",
|
|
104
|
+
"data-environment",
|
|
105
|
+
"data-theme",
|
|
106
|
+
"data-cd-theme"
|
|
107
|
+
];
|
|
108
|
+
}
|
|
109
|
+
shadow = null;
|
|
110
|
+
config = structuredClone(DEFAULT_CONFIG);
|
|
111
|
+
customMetadata = {};
|
|
112
|
+
customHeaders = {};
|
|
113
|
+
apiClient = new ContextDropApiClient("/context_drop");
|
|
114
|
+
picker = null;
|
|
115
|
+
isOpen = false;
|
|
116
|
+
isMinimized = false;
|
|
117
|
+
isSubmitting = false;
|
|
118
|
+
isAgentBusy = false;
|
|
119
|
+
surfaceMode = "feedback";
|
|
120
|
+
sidebarSide = "right";
|
|
121
|
+
sidebarWidth = DEFAULT_SIDEBAR_WIDTH;
|
|
122
|
+
currentTheme = "light";
|
|
123
|
+
composerThemeToggleBtn = null;
|
|
124
|
+
sidebarThemeToggleBtn = null;
|
|
125
|
+
pushPageLayout = false;
|
|
126
|
+
freePos = null;
|
|
127
|
+
isPeelDragging = false;
|
|
128
|
+
customPlaceholderSet = false;
|
|
129
|
+
pendingSmartCaptures = new Set();
|
|
130
|
+
elementReferences = [];
|
|
131
|
+
elementDomNodes = new Map();
|
|
132
|
+
attachments = [];
|
|
133
|
+
sessionHistory = [];
|
|
134
|
+
// Local Agent state
|
|
135
|
+
agentStatusLoaded = false;
|
|
136
|
+
agentWorkspaceName = "rails-app";
|
|
137
|
+
agentMcpEnvironments = ["local"];
|
|
138
|
+
agentProviders = structuredClone(DEFAULT_PROVIDERS_FALLBACK);
|
|
139
|
+
activeProviderId = "anthropic";
|
|
140
|
+
selectedVaultProviderId = "anthropic";
|
|
141
|
+
isByokDrawerOpen = false;
|
|
142
|
+
isFeedbacksDrawerOpen = false;
|
|
143
|
+
feedbacksEnvFilter = "all";
|
|
144
|
+
crossEnvFeedbacks = [];
|
|
145
|
+
isLoadingFeedbacks = false;
|
|
146
|
+
agentMessages = [];
|
|
147
|
+
// Shadow DOM references
|
|
148
|
+
rootContainer = null;
|
|
149
|
+
bookCornerBtn = null;
|
|
150
|
+
bookCornerCountEl = null;
|
|
151
|
+
bookCornerAgentDotEl = null;
|
|
152
|
+
bookCornerHintEl = null;
|
|
153
|
+
dragPinEl = null;
|
|
154
|
+
cornerSnapGhostEl = null;
|
|
155
|
+
compactBarEl = null;
|
|
156
|
+
compactStatusEl = null;
|
|
157
|
+
compactStepGroupEl = null;
|
|
158
|
+
compactLayerUpBtn = null;
|
|
159
|
+
compactLayerDownBtn = null;
|
|
160
|
+
compactDoneBtn = null;
|
|
161
|
+
compactPinBtn = null;
|
|
162
|
+
// Floating Feedback Composer elements
|
|
163
|
+
composerEl = null;
|
|
164
|
+
composerTitleEl = null;
|
|
165
|
+
composerMinBtn = null;
|
|
166
|
+
cornerPinBtn = null;
|
|
167
|
+
composerModeSwitchWrap = null;
|
|
168
|
+
composerToolbarPickBtn = null;
|
|
169
|
+
composerToolbarScreenshotBtn = null;
|
|
170
|
+
composerToolbarUploadBtn = null;
|
|
171
|
+
chatThreadEl = null;
|
|
172
|
+
textareaEl = null;
|
|
173
|
+
trayEl = null;
|
|
174
|
+
statusEl = null;
|
|
175
|
+
fileInputEl = null;
|
|
176
|
+
submitBtn = null;
|
|
177
|
+
// Docked Local Agent Sidebar elements
|
|
178
|
+
agentSidebarEl = null;
|
|
179
|
+
sidebarPinBtn = null;
|
|
180
|
+
sidebarEnvPillEl = null;
|
|
181
|
+
sidebarPushBtn = null;
|
|
182
|
+
keyDotEl = null;
|
|
183
|
+
providerPillLabelEl = null;
|
|
184
|
+
keysToggleBtn = null;
|
|
185
|
+
feedbacksToggleBtn = null;
|
|
186
|
+
byokDrawerEl = null;
|
|
187
|
+
byokProviderSelect = null;
|
|
188
|
+
byokModelInput = null;
|
|
189
|
+
byokModelPresetsSelect = null;
|
|
190
|
+
byokBaseUrlField = null;
|
|
191
|
+
byokBaseUrlInput = null;
|
|
192
|
+
byokKeySourceBadge = null;
|
|
193
|
+
byokKeyInput = null;
|
|
194
|
+
feedbacksDrawerEl = null;
|
|
195
|
+
feedbacksTabsEl = null;
|
|
196
|
+
feedbacksListEl = null;
|
|
197
|
+
agentStreamEl = null;
|
|
198
|
+
agentTrayEl = null;
|
|
199
|
+
agentStatusEl = null;
|
|
200
|
+
agentTextareaEl = null;
|
|
201
|
+
agentSendBtn = null;
|
|
202
|
+
lastPointerX = 0;
|
|
203
|
+
lastPointerY = 0;
|
|
204
|
+
boundTrackPointer = () => { };
|
|
205
|
+
boundGlobalKeyDown = () => { };
|
|
206
|
+
cancelActivePeelDrag = null;
|
|
207
|
+
attributeChangedCallback(name, oldValue, newValue) {
|
|
208
|
+
if (oldValue === newValue)
|
|
209
|
+
return;
|
|
210
|
+
if ((name === "data-theme" || name === "data-cd-theme") && (newValue === "dark" || newValue === "light")) {
|
|
211
|
+
this.applyTheme(newValue, false);
|
|
212
|
+
}
|
|
213
|
+
else if (name === "data-position" && newValue) {
|
|
214
|
+
this.setCornerPosition(newValue);
|
|
215
|
+
}
|
|
216
|
+
else if (name === "data-endpoint" && newValue && this.shadow) {
|
|
217
|
+
this.setEndpoint(newValue);
|
|
218
|
+
}
|
|
219
|
+
else if (name === "data-credentials" && newValue && this.shadow) {
|
|
220
|
+
if (newValue === "same-origin" || newValue === "include" || newValue === "omit") {
|
|
221
|
+
this.apiClient.setCredentials(newValue);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
else if (name === "data-placeholder" && newValue && this.shadow) {
|
|
225
|
+
this.setWidgetConfig({ placeholder: newValue });
|
|
226
|
+
}
|
|
227
|
+
else if (name === "data-metadata" && this.shadow) {
|
|
228
|
+
if (newValue) {
|
|
229
|
+
try {
|
|
230
|
+
this.customMetadata = JSON.parse(newValue);
|
|
231
|
+
}
|
|
232
|
+
catch {
|
|
233
|
+
this.customMetadata = {};
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
else {
|
|
237
|
+
this.customMetadata = {};
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
else if (name === "data-selection-mode" && newValue && this.shadow) {
|
|
241
|
+
if (newValue === "smart" || newValue === "verbose") {
|
|
242
|
+
this.setWidgetConfig({ features: { ...this.config.features, selection_mode: newValue } });
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
else if (name === "data-environment" && newValue && this.shadow) {
|
|
246
|
+
this.setWidgetConfig({ environment: newValue });
|
|
247
|
+
}
|
|
248
|
+
else if (name === "data-config" && newValue && this.shadow) {
|
|
249
|
+
try {
|
|
250
|
+
const parsed = JSON.parse(newValue);
|
|
251
|
+
this.setWidgetConfig(parsed);
|
|
252
|
+
}
|
|
253
|
+
catch {
|
|
254
|
+
// Ignore malformed JSON
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
connectedCallback() {
|
|
259
|
+
if (!this.shadow) {
|
|
260
|
+
this.shadow = this.attachShadow({ mode: "open" });
|
|
261
|
+
}
|
|
262
|
+
this.readDataAttributes();
|
|
263
|
+
this.initTheme();
|
|
264
|
+
try {
|
|
265
|
+
const storedWidth = typeof localStorage !== "undefined" ? localStorage.getItem("context_drop_sidebar_width") : null;
|
|
266
|
+
if (storedWidth) {
|
|
267
|
+
const parsed = parseInt(storedWidth, 10);
|
|
268
|
+
if (!Number.isNaN(parsed) && parsed >= 360 && parsed <= 920) {
|
|
269
|
+
// If stored width was from the old cramped v0.2-alpha default (<= 440px), upgrade to DEFAULT_SIDEBAR_WIDTH
|
|
270
|
+
this.sidebarWidth = parsed <= 440 ? DEFAULT_SIDEBAR_WIDTH : parsed;
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
catch {
|
|
275
|
+
// Non-fatal
|
|
276
|
+
}
|
|
277
|
+
this.surfaceMode = this.config.agent?.enabled ? "agent" : "feedback";
|
|
278
|
+
this.renderShadowDom();
|
|
279
|
+
this.boundTrackPointer = (ev) => {
|
|
280
|
+
this.lastPointerX = ev.clientX;
|
|
281
|
+
this.lastPointerY = ev.clientY;
|
|
282
|
+
};
|
|
283
|
+
this.boundGlobalKeyDown = (ev) => {
|
|
284
|
+
if (ev.key === "Escape" && this.isPeelDragging && this.cancelActivePeelDrag) {
|
|
285
|
+
ev.preventDefault();
|
|
286
|
+
ev.stopPropagation();
|
|
287
|
+
this.cancelActivePeelDrag();
|
|
288
|
+
return;
|
|
289
|
+
}
|
|
290
|
+
// Alt+Shift+P / Option+Shift+P toggles Freeze & Pin mode in technical/verbose environments
|
|
291
|
+
if (!this.isSmartSelectionMode() &&
|
|
292
|
+
ev.altKey &&
|
|
293
|
+
ev.shiftKey &&
|
|
294
|
+
(ev.code === "KeyP" || (typeof ev.key === "string" && ev.key.toLowerCase() === "p"))) {
|
|
295
|
+
ev.preventDefault();
|
|
296
|
+
ev.stopPropagation();
|
|
297
|
+
if (typeof ev.stopImmediatePropagation === "function") {
|
|
298
|
+
ev.stopImmediatePropagation();
|
|
299
|
+
}
|
|
300
|
+
this.togglePinSelection();
|
|
301
|
+
}
|
|
302
|
+
};
|
|
303
|
+
if (typeof window !== "undefined") {
|
|
304
|
+
window.addEventListener("mousemove", this.boundTrackPointer, { capture: true, passive: true });
|
|
305
|
+
window.addEventListener("keydown", this.boundGlobalKeyDown, { capture: true });
|
|
306
|
+
}
|
|
307
|
+
this.picker = new ElementPicker({
|
|
308
|
+
onSelect: (snapshot, el) => {
|
|
309
|
+
const added = this.addElementReference(snapshot, el);
|
|
310
|
+
if (added) {
|
|
311
|
+
if (this.isSmartSelectionMode()) {
|
|
312
|
+
this.hideStatus();
|
|
313
|
+
if (this.config.features.screenshot_capture) {
|
|
314
|
+
const friendlyLabel = this.getElementFriendlyLabel(snapshot, el);
|
|
315
|
+
void this.captureSmartSurroundingContext(el, snapshot.client_ref, friendlyLabel);
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
else {
|
|
319
|
+
const count = this.elementReferences.length;
|
|
320
|
+
this.showStatus(count === 1
|
|
321
|
+
? `Pinned ${snapshot.selector}`
|
|
322
|
+
: `Pinned ${count} elements (latest: ${snapshot.selector})`, "success");
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
this.syncUiStates();
|
|
326
|
+
},
|
|
327
|
+
onUnselect: (clientRef) => {
|
|
328
|
+
this.removeElementReference(clientRef);
|
|
329
|
+
},
|
|
330
|
+
onExit: () => {
|
|
331
|
+
if (this.isPeelDragging)
|
|
332
|
+
return;
|
|
333
|
+
this.isMinimized = false;
|
|
334
|
+
this.openComposer();
|
|
335
|
+
this.syncUiStates();
|
|
336
|
+
},
|
|
337
|
+
canSelectMore: () => this.isPeelDragging ||
|
|
338
|
+
this.elementReferences.length < this.config.limits.max_element_references,
|
|
339
|
+
getExcludedSelectors: () => this.config.privacy.excluded_selectors,
|
|
340
|
+
getSelectionMode: () => (this.isSmartSelectionMode() ? "smart" : "verbose"),
|
|
341
|
+
getPinnedElements: () => this.elementReferences
|
|
342
|
+
.map((ref) => {
|
|
343
|
+
const node = this.elementDomNodes.get(ref.client_ref);
|
|
344
|
+
const friendlyLabel = typeof ref.metadata?.friendly_label === "string"
|
|
345
|
+
? ref.metadata.friendly_label
|
|
346
|
+
: undefined;
|
|
347
|
+
return node
|
|
348
|
+
? { clientRef: ref.client_ref, selector: ref.selector, friendlyLabel, element: node }
|
|
349
|
+
: null;
|
|
350
|
+
})
|
|
351
|
+
.filter((item) => item !== null),
|
|
352
|
+
onStateChange: (state) => {
|
|
353
|
+
this.syncPinActiveIndicators(state.active);
|
|
354
|
+
if (!this.isPeelDragging) {
|
|
355
|
+
this.renderCompactCoachBar(state.active, state.candidateStack.length);
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
});
|
|
359
|
+
const hasInlineConfig = typeof this.hasAttribute === "function" && this.hasAttribute("data-config");
|
|
360
|
+
if (!hasInlineConfig && typeof fetch === "function") {
|
|
361
|
+
void this.hydrateConfigFromServer();
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
disconnectedCallback() {
|
|
365
|
+
if (this.cancelActivePeelDrag) {
|
|
366
|
+
this.cancelActivePeelDrag();
|
|
367
|
+
}
|
|
368
|
+
if (this.picker && this.picker.isActive()) {
|
|
369
|
+
this.picker.stop({ silent: true });
|
|
370
|
+
}
|
|
371
|
+
this.picker?.hideExternalPreview();
|
|
372
|
+
this.syncHostPagePushMargin(false);
|
|
373
|
+
if (typeof window !== "undefined") {
|
|
374
|
+
window.removeEventListener("mousemove", this.boundTrackPointer, { capture: true });
|
|
375
|
+
window.removeEventListener("keydown", this.boundGlobalKeyDown, { capture: true });
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
getConfig() {
|
|
379
|
+
return this.config;
|
|
380
|
+
}
|
|
381
|
+
setEndpoint(endpoint, options = {}) {
|
|
382
|
+
const resolvedCredentials = options.credentials ||
|
|
383
|
+
this.getAttribute("data-credentials") ||
|
|
384
|
+
"same-origin";
|
|
385
|
+
const resolvedHeaders = options.headers || this.customHeaders;
|
|
386
|
+
this.customHeaders = { ...resolvedHeaders };
|
|
387
|
+
this.apiClient = new ContextDropApiClient(endpoint || "/context_drop", {
|
|
388
|
+
credentials: resolvedCredentials,
|
|
389
|
+
headers: this.customHeaders
|
|
390
|
+
});
|
|
391
|
+
}
|
|
392
|
+
setHeaders(headers) {
|
|
393
|
+
this.customHeaders = { ...headers };
|
|
394
|
+
this.apiClient.setHeaders(this.customHeaders);
|
|
395
|
+
}
|
|
396
|
+
setCredentials(credentials) {
|
|
397
|
+
this.apiClient.setCredentials(credentials);
|
|
398
|
+
}
|
|
399
|
+
setCustomMetadata(metadata) {
|
|
400
|
+
this.customMetadata = { ...metadata };
|
|
401
|
+
}
|
|
402
|
+
emitWidgetEvent(name, detail) {
|
|
403
|
+
if (typeof CustomEvent !== "function" || typeof this.dispatchEvent !== "function") {
|
|
404
|
+
return;
|
|
405
|
+
}
|
|
406
|
+
try {
|
|
407
|
+
this.dispatchEvent(new CustomEvent(name, {
|
|
408
|
+
bubbles: true,
|
|
409
|
+
composed: true,
|
|
410
|
+
detail
|
|
411
|
+
}));
|
|
412
|
+
}
|
|
413
|
+
catch {
|
|
414
|
+
// Non-fatal in synthetic DOM environments
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
setWidgetConfig(partial) {
|
|
418
|
+
const wasAgentEnabled = Boolean(this.config.agent?.enabled);
|
|
419
|
+
if (typeof partial.placeholder === "string" &&
|
|
420
|
+
partial.placeholder !== DEFAULT_VERBOSE_PLACEHOLDER &&
|
|
421
|
+
partial.placeholder !== DEFAULT_SMART_PLACEHOLDER) {
|
|
422
|
+
this.customPlaceholderSet = true;
|
|
423
|
+
}
|
|
424
|
+
this.config = {
|
|
425
|
+
...this.config,
|
|
426
|
+
...partial,
|
|
427
|
+
agent: { ...(this.config.agent || { enabled: false }), ...(partial.agent || {}) },
|
|
428
|
+
features: { ...this.config.features, ...(partial.features || {}) },
|
|
429
|
+
limits: { ...this.config.limits, ...(partial.limits || {}) },
|
|
430
|
+
privacy: { ...this.config.privacy, ...(partial.privacy || {}) }
|
|
431
|
+
};
|
|
432
|
+
if (partial.csrf_token) {
|
|
433
|
+
this.apiClient.setCsrfToken(partial.csrf_token);
|
|
434
|
+
}
|
|
435
|
+
if (partial.position) {
|
|
436
|
+
this.setCornerPosition(partial.position);
|
|
437
|
+
}
|
|
438
|
+
const nowAgentEnabled = Boolean(this.config.agent?.enabled);
|
|
439
|
+
if (nowAgentEnabled && !wasAgentEnabled) {
|
|
440
|
+
this.surfaceMode = "agent";
|
|
441
|
+
if (this.isOpen) {
|
|
442
|
+
void this.ensureAgentStatusLoaded();
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
else if (!nowAgentEnabled) {
|
|
446
|
+
this.surfaceMode = "feedback";
|
|
447
|
+
}
|
|
448
|
+
this.renderTray();
|
|
449
|
+
this.renderChatThread();
|
|
450
|
+
this.syncUiStates();
|
|
451
|
+
}
|
|
452
|
+
getSurfaceMode() {
|
|
453
|
+
return this.isAgentModeActive() ? "agent" : "feedback";
|
|
454
|
+
}
|
|
455
|
+
setSurfaceMode(mode) {
|
|
456
|
+
if (mode === "agent" && !this.config.agent?.enabled) {
|
|
457
|
+
return;
|
|
458
|
+
}
|
|
459
|
+
this.surfaceMode = mode;
|
|
460
|
+
if (mode === "agent" && this.isOpen) {
|
|
461
|
+
void this.ensureAgentStatusLoaded();
|
|
462
|
+
}
|
|
463
|
+
this.renderTray();
|
|
464
|
+
this.renderChatThread();
|
|
465
|
+
this.syncUiStates();
|
|
466
|
+
this.focusActiveInput();
|
|
467
|
+
}
|
|
468
|
+
getElementReferences() {
|
|
469
|
+
return [...this.elementReferences];
|
|
470
|
+
}
|
|
471
|
+
getAttachments() {
|
|
472
|
+
return [...this.attachments];
|
|
473
|
+
}
|
|
474
|
+
getSessionHistory() {
|
|
475
|
+
return [...this.sessionHistory];
|
|
476
|
+
}
|
|
477
|
+
getAgentMessages() {
|
|
478
|
+
return [...this.agentMessages];
|
|
479
|
+
}
|
|
480
|
+
isAgentModeActive() {
|
|
481
|
+
return Boolean(this.config.agent?.enabled) && this.surfaceMode === "agent";
|
|
482
|
+
}
|
|
483
|
+
isSmartSelectionMode() {
|
|
484
|
+
if (this.isAgentModeActive()) {
|
|
485
|
+
return false;
|
|
486
|
+
}
|
|
487
|
+
if (this.config.features.selection_mode === "smart") {
|
|
488
|
+
return true;
|
|
489
|
+
}
|
|
490
|
+
if (this.config.features.selection_mode === "verbose") {
|
|
491
|
+
return false;
|
|
492
|
+
}
|
|
493
|
+
return (this.config.environment || "").toLowerCase() === "production";
|
|
494
|
+
}
|
|
495
|
+
getElementFriendlyLabel(ref, domNode) {
|
|
496
|
+
const metaLabel = ref.metadata?.friendly_label;
|
|
497
|
+
if (typeof metaLabel === "string" && metaLabel.trim().length > 0) {
|
|
498
|
+
return metaLabel.trim();
|
|
499
|
+
}
|
|
500
|
+
if (domNode) {
|
|
501
|
+
return describeSmartComponent(domNode).label;
|
|
502
|
+
}
|
|
503
|
+
return ref.tag_name ? `Selected ${ref.tag_name} area` : "Selected page area";
|
|
504
|
+
}
|
|
505
|
+
openComposer() {
|
|
506
|
+
const wasOpen = this.isOpen && !this.isMinimized;
|
|
507
|
+
this.isOpen = true;
|
|
508
|
+
this.isMinimized = false;
|
|
509
|
+
if (this.isAgentModeActive()) {
|
|
510
|
+
void this.ensureAgentStatusLoaded();
|
|
511
|
+
}
|
|
512
|
+
this.syncUiStates();
|
|
513
|
+
this.focusActiveInput();
|
|
514
|
+
if (!wasOpen) {
|
|
515
|
+
this.emitWidgetEvent("context-drop:open", { open: true });
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
closeComposer() {
|
|
519
|
+
const wasOpen = this.isOpen;
|
|
520
|
+
this.isOpen = false;
|
|
521
|
+
this.isMinimized = false;
|
|
522
|
+
if (this.picker && this.picker.isActive()) {
|
|
523
|
+
this.picker.stop({ silent: true });
|
|
524
|
+
}
|
|
525
|
+
this.picker?.hideExternalPreview();
|
|
526
|
+
this.syncUiStates();
|
|
527
|
+
if (wasOpen) {
|
|
528
|
+
this.emitWidgetEvent("context-drop:close", { open: false });
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
toggleComposer() {
|
|
532
|
+
if (this.isOpen && !this.isMinimized) {
|
|
533
|
+
this.closeComposer();
|
|
534
|
+
}
|
|
535
|
+
else {
|
|
536
|
+
this.openComposer();
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
minimizeToCompactBar() {
|
|
540
|
+
this.isOpen = true;
|
|
541
|
+
this.isMinimized = true;
|
|
542
|
+
this.syncUiStates();
|
|
543
|
+
}
|
|
544
|
+
expandFromCompactBar() {
|
|
545
|
+
if (this.picker && this.picker.isActive()) {
|
|
546
|
+
this.picker.stop({ silent: true });
|
|
547
|
+
}
|
|
548
|
+
this.isMinimized = false;
|
|
549
|
+
this.openComposer();
|
|
550
|
+
}
|
|
551
|
+
startPinSelection() {
|
|
552
|
+
if (!this.config.features.element_selection || !this.picker)
|
|
553
|
+
return;
|
|
554
|
+
if (this.elementReferences.length >= this.config.limits.max_element_references) {
|
|
555
|
+
this.openComposer();
|
|
556
|
+
this.showStatus(`Maximum of ${this.config.limits.max_element_references} pinned elements reached.`, "error");
|
|
557
|
+
return;
|
|
558
|
+
}
|
|
559
|
+
this.minimizeToCompactBar();
|
|
560
|
+
this.picker.start({ suppressCursorPin: false });
|
|
561
|
+
if (this.lastPointerX > 0 && this.lastPointerY > 0) {
|
|
562
|
+
this.picker.highlightAtPoint(this.lastPointerX, this.lastPointerY);
|
|
563
|
+
}
|
|
564
|
+
this.syncUiStates();
|
|
565
|
+
}
|
|
566
|
+
togglePinSelection() {
|
|
567
|
+
if (this.picker && this.picker.isActive()) {
|
|
568
|
+
this.picker.stop();
|
|
569
|
+
}
|
|
570
|
+
else {
|
|
571
|
+
this.startPinSelection();
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
getTheme() {
|
|
575
|
+
return this.currentTheme;
|
|
576
|
+
}
|
|
577
|
+
setTheme(theme) {
|
|
578
|
+
this.applyTheme(theme, true);
|
|
579
|
+
}
|
|
580
|
+
toggleTheme() {
|
|
581
|
+
const next = this.currentTheme === "dark" ? "light" : "dark";
|
|
582
|
+
this.applyTheme(next, true);
|
|
583
|
+
}
|
|
584
|
+
initTheme() {
|
|
585
|
+
let theme = "light";
|
|
586
|
+
try {
|
|
587
|
+
const stored = typeof localStorage !== "undefined" ? localStorage.getItem("context_drop_theme") : null;
|
|
588
|
+
if (stored === "dark" || stored === "light") {
|
|
589
|
+
theme = stored;
|
|
590
|
+
}
|
|
591
|
+
else {
|
|
592
|
+
const attr = this.getAttribute("data-cd-theme") || this.getAttribute("data-theme");
|
|
593
|
+
if (attr === "dark" || attr === "light") {
|
|
594
|
+
theme = attr;
|
|
595
|
+
}
|
|
596
|
+
else if (typeof window !== "undefined" &&
|
|
597
|
+
window.matchMedia &&
|
|
598
|
+
window.matchMedia("(prefers-color-scheme: dark)").matches) {
|
|
599
|
+
theme = "dark";
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
catch {
|
|
604
|
+
// Non-fatal if localStorage is restricted
|
|
605
|
+
}
|
|
606
|
+
this.applyTheme(theme, false);
|
|
607
|
+
if (typeof window !== "undefined" && window.matchMedia) {
|
|
608
|
+
try {
|
|
609
|
+
const mediaQuery = window.matchMedia("(prefers-color-scheme: dark)");
|
|
610
|
+
mediaQuery.addEventListener?.("change", (e) => {
|
|
611
|
+
try {
|
|
612
|
+
if (!localStorage.getItem("context_drop_theme")) {
|
|
613
|
+
this.applyTheme(e.matches ? "dark" : "light", false);
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
catch {
|
|
617
|
+
// ignore
|
|
618
|
+
}
|
|
619
|
+
});
|
|
620
|
+
}
|
|
621
|
+
catch {
|
|
622
|
+
// ignore
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
applyTheme(theme, persist = true) {
|
|
627
|
+
this.currentTheme = theme;
|
|
628
|
+
if (persist) {
|
|
629
|
+
try {
|
|
630
|
+
localStorage.setItem("context_drop_theme", theme);
|
|
631
|
+
}
|
|
632
|
+
catch {
|
|
633
|
+
// Non-fatal
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
// Use namespaced `data-cd-theme` on the custom element host so host CSS frameworks
|
|
637
|
+
// (e.g. DaisyUI / Bootstrap `[data-theme="dark"]` or `body:has([data-theme="dark"])`)
|
|
638
|
+
// never alter the host page theme when toggling the widget theme.
|
|
639
|
+
if (typeof this.setAttribute === "function") {
|
|
640
|
+
this.setAttribute("data-cd-theme", theme);
|
|
641
|
+
}
|
|
642
|
+
this.rootContainer?.setAttribute("data-theme", theme);
|
|
643
|
+
this.agentSidebarEl?.setAttribute("data-theme", theme);
|
|
644
|
+
this.composerEl?.setAttribute("data-theme", theme);
|
|
645
|
+
this.compactBarEl?.setAttribute("data-theme", theme);
|
|
646
|
+
this.bookCornerBtn?.setAttribute("data-theme", theme);
|
|
647
|
+
this.dragPinEl?.setAttribute("data-theme", theme);
|
|
648
|
+
this.cornerSnapGhostEl?.setAttribute("data-theme", theme);
|
|
649
|
+
if (this.composerThemeToggleBtn) {
|
|
650
|
+
this.updateThemeToggleBtn(this.composerThemeToggleBtn);
|
|
651
|
+
}
|
|
652
|
+
if (this.sidebarThemeToggleBtn) {
|
|
653
|
+
this.updateThemeToggleBtn(this.sidebarThemeToggleBtn);
|
|
654
|
+
}
|
|
655
|
+
if (typeof this.dispatchEvent === "function") {
|
|
656
|
+
try {
|
|
657
|
+
this.dispatchEvent(new CustomEvent("contextdrop:themechange", {
|
|
658
|
+
bubbles: false,
|
|
659
|
+
composed: false,
|
|
660
|
+
detail: { theme }
|
|
661
|
+
}));
|
|
662
|
+
}
|
|
663
|
+
catch {
|
|
664
|
+
// ignore
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
buildThemeToggleBtn() {
|
|
669
|
+
const btn = document.createElement("button");
|
|
670
|
+
btn.type = "button";
|
|
671
|
+
btn.className = "cd-icon-btn cd-theme-toggle";
|
|
672
|
+
this.updateThemeToggleBtn(btn);
|
|
673
|
+
btn.addEventListener("click", () => {
|
|
674
|
+
this.toggleTheme();
|
|
675
|
+
});
|
|
676
|
+
return btn;
|
|
677
|
+
}
|
|
678
|
+
updateThemeToggleBtn(btn) {
|
|
679
|
+
const isDark = this.currentTheme === "dark";
|
|
680
|
+
btn.title = isDark ? "Switch to light theme" : "Switch to dark theme";
|
|
681
|
+
btn.setAttribute("aria-label", isDark ? "Switch to light theme" : "Switch to dark theme");
|
|
682
|
+
btn.innerHTML = isDark ? ICON_SUN : ICON_MOON;
|
|
683
|
+
}
|
|
684
|
+
setCornerPosition(position) {
|
|
685
|
+
if (!["top_left", "top_right", "bottom_left", "bottom_right"].includes(position)) {
|
|
686
|
+
return;
|
|
687
|
+
}
|
|
688
|
+
this.config.position = position;
|
|
689
|
+
this.freePos = null;
|
|
690
|
+
if (this.rootContainer) {
|
|
691
|
+
this.rootContainer.className = `cd-root pos-${position}`;
|
|
692
|
+
this.rootContainer.style.left = "";
|
|
693
|
+
this.rootContainer.style.top = "";
|
|
694
|
+
}
|
|
695
|
+
if (this.bookCornerBtn) {
|
|
696
|
+
this.bookCornerBtn.className = `cd-book-corner corner-${position}`;
|
|
697
|
+
}
|
|
698
|
+
}
|
|
699
|
+
setFreePosition(left, top) {
|
|
700
|
+
if (!this.rootContainer)
|
|
701
|
+
return;
|
|
702
|
+
const vw = typeof window !== "undefined" ? window.innerWidth || 1024 : 1024;
|
|
703
|
+
const vh = typeof window !== "undefined" ? window.innerHeight || 768 : 768;
|
|
704
|
+
const clampedLeft = Math.max(8, Math.min(left, vw - 72));
|
|
705
|
+
const clampedTop = Math.max(8, Math.min(top, vh - 48));
|
|
706
|
+
this.freePos = { left: clampedLeft, top: clampedTop };
|
|
707
|
+
this.rootContainer.classList.add("is-free-positioned");
|
|
708
|
+
this.rootContainer.style.left = `${clampedLeft}px`;
|
|
709
|
+
this.rootContainer.style.top = `${clampedTop}px`;
|
|
710
|
+
}
|
|
711
|
+
addElementReference(snapshot, elementNode) {
|
|
712
|
+
if (this.elementReferences.length >= this.config.limits.max_element_references) {
|
|
713
|
+
this.showStatus(`Maximum of ${this.config.limits.max_element_references} DOM elements reached.`, "error");
|
|
714
|
+
return false;
|
|
715
|
+
}
|
|
716
|
+
this.elementReferences.push(snapshot);
|
|
717
|
+
if (elementNode) {
|
|
718
|
+
this.elementDomNodes.set(snapshot.client_ref, elementNode);
|
|
719
|
+
}
|
|
720
|
+
this.picker?.refreshPinnedMarkers();
|
|
721
|
+
this.renderTray();
|
|
722
|
+
this.syncUiStates();
|
|
723
|
+
return true;
|
|
724
|
+
}
|
|
725
|
+
removeElementReference(clientRef) {
|
|
726
|
+
this.elementReferences = this.elementReferences.filter((el) => el.client_ref !== clientRef);
|
|
727
|
+
this.elementDomNodes.delete(clientRef);
|
|
728
|
+
this.attachments = this.attachments.filter((att) => !(att.element_reference_ref === clientRef && att.metadata?.auto_smart_context === true));
|
|
729
|
+
this.picker?.hideExternalPreview();
|
|
730
|
+
this.picker?.refreshPinnedMarkers();
|
|
731
|
+
this.renderTray();
|
|
732
|
+
this.syncUiStates();
|
|
733
|
+
}
|
|
734
|
+
removeAttachment(attachmentId) {
|
|
735
|
+
this.attachments = this.attachments.filter((att) => att.id !== attachmentId);
|
|
736
|
+
this.renderTray();
|
|
737
|
+
this.syncUiStates();
|
|
738
|
+
}
|
|
739
|
+
focusActiveInput() {
|
|
740
|
+
if (this.isAgentModeActive()) {
|
|
741
|
+
this.agentTextareaEl?.focus();
|
|
742
|
+
}
|
|
743
|
+
else {
|
|
744
|
+
this.textareaEl?.focus();
|
|
745
|
+
}
|
|
746
|
+
}
|
|
747
|
+
async hydrateConfigFromServer() {
|
|
748
|
+
try {
|
|
749
|
+
const remoteConfig = await this.apiClient.fetchConfig();
|
|
750
|
+
if (remoteConfig && typeof remoteConfig === "object") {
|
|
751
|
+
const posOverride = this.getAttribute("data-position");
|
|
752
|
+
this.setWidgetConfig({
|
|
753
|
+
...remoteConfig,
|
|
754
|
+
position: posOverride || remoteConfig.position || this.config.position
|
|
755
|
+
});
|
|
756
|
+
}
|
|
757
|
+
}
|
|
758
|
+
catch {
|
|
759
|
+
// Non-fatal in static/offline environments
|
|
760
|
+
}
|
|
761
|
+
}
|
|
762
|
+
readDataAttributes() {
|
|
763
|
+
const endpoint = this.getAttribute("data-endpoint") || "/context_drop";
|
|
764
|
+
const rawCredentials = this.getAttribute("data-credentials");
|
|
765
|
+
const credentials = rawCredentials === "include" || rawCredentials === "omit" || rawCredentials === "same-origin"
|
|
766
|
+
? rawCredentials
|
|
767
|
+
: "same-origin";
|
|
768
|
+
this.apiClient = new ContextDropApiClient(endpoint, {
|
|
769
|
+
credentials,
|
|
770
|
+
headers: this.customHeaders
|
|
771
|
+
});
|
|
772
|
+
const rawConfig = this.getAttribute("data-config");
|
|
773
|
+
if (rawConfig) {
|
|
774
|
+
try {
|
|
775
|
+
const parsed = JSON.parse(rawConfig);
|
|
776
|
+
if (typeof parsed.placeholder === "string" &&
|
|
777
|
+
parsed.placeholder !== DEFAULT_VERBOSE_PLACEHOLDER &&
|
|
778
|
+
parsed.placeholder !== DEFAULT_SMART_PLACEHOLDER) {
|
|
779
|
+
this.customPlaceholderSet = true;
|
|
780
|
+
}
|
|
781
|
+
this.config = {
|
|
782
|
+
...this.config,
|
|
783
|
+
...parsed,
|
|
784
|
+
agent: { ...(this.config.agent || { enabled: false }), ...(parsed.agent || {}) },
|
|
785
|
+
features: { ...this.config.features, ...(parsed.features || {}) },
|
|
786
|
+
limits: { ...this.config.limits, ...(parsed.limits || {}) },
|
|
787
|
+
privacy: { ...this.config.privacy, ...(parsed.privacy || {}) }
|
|
788
|
+
};
|
|
789
|
+
if (parsed.csrf_token) {
|
|
790
|
+
this.apiClient.setCsrfToken(parsed.csrf_token);
|
|
791
|
+
}
|
|
792
|
+
}
|
|
793
|
+
catch {
|
|
794
|
+
// Ignore malformed JSON attribute
|
|
795
|
+
}
|
|
796
|
+
}
|
|
797
|
+
const posOverride = this.getAttribute("data-position");
|
|
798
|
+
if (posOverride &&
|
|
799
|
+
["bottom_right", "bottom_left", "top_right", "top_left"].includes(posOverride)) {
|
|
800
|
+
this.config.position = posOverride;
|
|
801
|
+
}
|
|
802
|
+
const placeholderOverride = this.getAttribute("data-placeholder");
|
|
803
|
+
if (placeholderOverride) {
|
|
804
|
+
this.config.placeholder = placeholderOverride;
|
|
805
|
+
this.customPlaceholderSet = true;
|
|
806
|
+
}
|
|
807
|
+
const rawMetadata = this.getAttribute("data-metadata");
|
|
808
|
+
if (rawMetadata) {
|
|
809
|
+
try {
|
|
810
|
+
this.customMetadata = JSON.parse(rawMetadata);
|
|
811
|
+
}
|
|
812
|
+
catch {
|
|
813
|
+
this.customMetadata = {};
|
|
814
|
+
}
|
|
815
|
+
}
|
|
816
|
+
const themeOverride = this.getAttribute("data-cd-theme") || this.getAttribute("data-theme");
|
|
817
|
+
if (themeOverride === "dark" || themeOverride === "light") {
|
|
818
|
+
this.currentTheme = themeOverride;
|
|
819
|
+
}
|
|
820
|
+
const selectionModeOverride = this.getAttribute("data-selection-mode");
|
|
821
|
+
if (selectionModeOverride === "smart" || selectionModeOverride === "verbose") {
|
|
822
|
+
this.config.features.selection_mode = selectionModeOverride;
|
|
823
|
+
}
|
|
824
|
+
const envOverride = this.getAttribute("data-environment");
|
|
825
|
+
if (envOverride && envOverride.trim().length > 0) {
|
|
826
|
+
this.config.environment = envOverride.trim();
|
|
827
|
+
}
|
|
828
|
+
}
|
|
829
|
+
detectViewportSnapCorner(clientX, clientY, originCorner, leftOriginZone) {
|
|
830
|
+
if (typeof window === "undefined")
|
|
831
|
+
return null;
|
|
832
|
+
const vw = window.innerWidth || 1024;
|
|
833
|
+
const vh = window.innerHeight || 768;
|
|
834
|
+
let candidate = null;
|
|
835
|
+
if (clientX <= CORNER_SNAP_ZONE_PX && clientY <= CORNER_SNAP_ZONE_PX) {
|
|
836
|
+
candidate = "top_left";
|
|
837
|
+
}
|
|
838
|
+
else if (clientX >= vw - CORNER_SNAP_ZONE_PX && clientY <= CORNER_SNAP_ZONE_PX) {
|
|
839
|
+
candidate = "top_right";
|
|
840
|
+
}
|
|
841
|
+
else if (clientX <= CORNER_SNAP_ZONE_PX && clientY >= vh - CORNER_SNAP_ZONE_PX) {
|
|
842
|
+
candidate = "bottom_left";
|
|
843
|
+
}
|
|
844
|
+
else if (clientX >= vw - CORNER_SNAP_ZONE_PX && clientY >= vh - CORNER_SNAP_ZONE_PX) {
|
|
845
|
+
candidate = "bottom_right";
|
|
846
|
+
}
|
|
847
|
+
if (!candidate)
|
|
848
|
+
return null;
|
|
849
|
+
if (candidate === originCorner && !leftOriginZone)
|
|
850
|
+
return null;
|
|
851
|
+
return candidate;
|
|
852
|
+
}
|
|
853
|
+
isOutsideCornerZone(clientX, clientY, corner) {
|
|
854
|
+
if (typeof window === "undefined")
|
|
855
|
+
return true;
|
|
856
|
+
const vw = window.innerWidth || 1024;
|
|
857
|
+
const vh = window.innerHeight || 768;
|
|
858
|
+
const exitRadius = CORNER_SNAP_ZONE_PX + 10;
|
|
859
|
+
switch (corner) {
|
|
860
|
+
case "top_left":
|
|
861
|
+
return clientX > exitRadius || clientY > exitRadius;
|
|
862
|
+
case "top_right":
|
|
863
|
+
return clientX < vw - exitRadius || clientY > exitRadius;
|
|
864
|
+
case "bottom_left":
|
|
865
|
+
return clientX > exitRadius || clientY < vh - exitRadius;
|
|
866
|
+
case "bottom_right":
|
|
867
|
+
return clientX < vw - exitRadius || clientY < vh - exitRadius;
|
|
868
|
+
}
|
|
869
|
+
}
|
|
870
|
+
attachBookCornerLauncher(cornerBtn) {
|
|
871
|
+
let startX = 0;
|
|
872
|
+
let startY = 0;
|
|
873
|
+
let pointerActive = false;
|
|
874
|
+
let peeled = false;
|
|
875
|
+
let leftOriginZone = false;
|
|
876
|
+
let activeSnapCorner = null;
|
|
877
|
+
let suppressNextClick = false;
|
|
878
|
+
let suppressTimer = null;
|
|
879
|
+
const clearSuppress = () => {
|
|
880
|
+
suppressNextClick = false;
|
|
881
|
+
if (suppressTimer !== null) {
|
|
882
|
+
clearTimeout(suppressTimer);
|
|
883
|
+
suppressTimer = null;
|
|
884
|
+
}
|
|
885
|
+
};
|
|
886
|
+
const updateDragPinPosition = (x, y) => {
|
|
887
|
+
if (!this.dragPinEl)
|
|
888
|
+
return;
|
|
889
|
+
this.dragPinEl.style.transform = `translate3d(${Math.round(x)}px, ${Math.round(y)}px, 0)`;
|
|
890
|
+
};
|
|
891
|
+
const finishPeelVisuals = () => {
|
|
892
|
+
pointerActive = false;
|
|
893
|
+
this.isPeelDragging = false;
|
|
894
|
+
this.cancelActivePeelDrag = null;
|
|
895
|
+
cornerBtn.classList.remove("is-peeled");
|
|
896
|
+
if (this.dragPinEl) {
|
|
897
|
+
this.dragPinEl.hidden = true;
|
|
898
|
+
this.dragPinEl.classList.remove("has-target", "is-snapping-corner");
|
|
899
|
+
}
|
|
900
|
+
if (this.cornerSnapGhostEl) {
|
|
901
|
+
this.cornerSnapGhostEl.hidden = true;
|
|
902
|
+
}
|
|
903
|
+
if (typeof window !== "undefined") {
|
|
904
|
+
window.removeEventListener("pointermove", onPointerMove);
|
|
905
|
+
window.removeEventListener("pointerup", onPointerUp);
|
|
906
|
+
window.removeEventListener("pointercancel", onPointerCancel);
|
|
907
|
+
}
|
|
908
|
+
};
|
|
909
|
+
cornerBtn.addEventListener("click", (ev) => {
|
|
910
|
+
if (suppressNextClick) {
|
|
911
|
+
clearSuppress();
|
|
912
|
+
ev.preventDefault();
|
|
913
|
+
ev.stopPropagation();
|
|
914
|
+
return;
|
|
915
|
+
}
|
|
916
|
+
this.openComposer();
|
|
917
|
+
});
|
|
918
|
+
const onPointerMove = (ev) => {
|
|
919
|
+
if (!pointerActive)
|
|
920
|
+
return;
|
|
921
|
+
this.lastPointerX = ev.clientX;
|
|
922
|
+
this.lastPointerY = ev.clientY;
|
|
923
|
+
const travel = Math.hypot(ev.clientX - startX, ev.clientY - startY);
|
|
924
|
+
if (!peeled && travel > PEEL_DRAG_THRESHOLD_PX) {
|
|
925
|
+
peeled = true;
|
|
926
|
+
this.isPeelDragging = true;
|
|
927
|
+
cornerBtn.classList.add("is-peeled");
|
|
928
|
+
if (this.dragPinEl) {
|
|
929
|
+
this.dragPinEl.hidden = false;
|
|
930
|
+
updateDragPinPosition(ev.clientX, ev.clientY);
|
|
931
|
+
}
|
|
932
|
+
if (this.config.features.element_selection &&
|
|
933
|
+
this.picker &&
|
|
934
|
+
this.elementReferences.length < this.config.limits.max_element_references) {
|
|
935
|
+
this.picker.start({ suppressCursorPin: true });
|
|
936
|
+
}
|
|
937
|
+
}
|
|
938
|
+
if (!peeled)
|
|
939
|
+
return;
|
|
940
|
+
updateDragPinPosition(ev.clientX, ev.clientY);
|
|
941
|
+
if (!leftOriginZone && this.isOutsideCornerZone(ev.clientX, ev.clientY, this.config.position)) {
|
|
942
|
+
leftOriginZone = true;
|
|
943
|
+
}
|
|
944
|
+
activeSnapCorner = this.detectViewportSnapCorner(ev.clientX, ev.clientY, this.config.position, leftOriginZone);
|
|
945
|
+
if (activeSnapCorner) {
|
|
946
|
+
this.picker?.clearCandidateHighlight();
|
|
947
|
+
if (this.cornerSnapGhostEl) {
|
|
948
|
+
this.cornerSnapGhostEl.className = `cd-corner-snap-ghost corner-${activeSnapCorner}`;
|
|
949
|
+
this.cornerSnapGhostEl.hidden = false;
|
|
950
|
+
}
|
|
951
|
+
if (this.dragPinEl) {
|
|
952
|
+
this.dragPinEl.classList.add("is-snapping-corner");
|
|
953
|
+
this.dragPinEl.classList.remove("has-target");
|
|
954
|
+
}
|
|
955
|
+
}
|
|
956
|
+
else {
|
|
957
|
+
if (this.cornerSnapGhostEl) {
|
|
958
|
+
this.cornerSnapGhostEl.hidden = true;
|
|
959
|
+
}
|
|
960
|
+
if (this.dragPinEl) {
|
|
961
|
+
this.dragPinEl.classList.remove("is-snapping-corner");
|
|
962
|
+
}
|
|
963
|
+
this.picker?.highlightAtPoint(ev.clientX, ev.clientY, ev);
|
|
964
|
+
const hasTarget = Boolean(this.picker?.getActiveCandidate());
|
|
965
|
+
this.dragPinEl?.classList.toggle("has-target", hasTarget);
|
|
966
|
+
}
|
|
967
|
+
};
|
|
968
|
+
const armClickSuppression = () => {
|
|
969
|
+
suppressNextClick = true;
|
|
970
|
+
if (suppressTimer !== null) {
|
|
971
|
+
clearTimeout(suppressTimer);
|
|
972
|
+
}
|
|
973
|
+
suppressTimer = setTimeout(() => {
|
|
974
|
+
suppressNextClick = false;
|
|
975
|
+
suppressTimer = null;
|
|
976
|
+
}, 40);
|
|
977
|
+
};
|
|
978
|
+
const onPointerUp = (ev) => {
|
|
979
|
+
if (!pointerActive)
|
|
980
|
+
return;
|
|
981
|
+
const wasPeeled = peeled;
|
|
982
|
+
const snapTarget = activeSnapCorner;
|
|
983
|
+
peeled = false;
|
|
984
|
+
activeSnapCorner = null;
|
|
985
|
+
if (!wasPeeled) {
|
|
986
|
+
finishPeelVisuals();
|
|
987
|
+
return;
|
|
988
|
+
}
|
|
989
|
+
armClickSuppression();
|
|
990
|
+
if (snapTarget) {
|
|
991
|
+
this.picker?.stop({ silent: true });
|
|
992
|
+
finishPeelVisuals();
|
|
993
|
+
this.setCornerPosition(snapTarget);
|
|
994
|
+
this.syncUiStates();
|
|
995
|
+
return;
|
|
996
|
+
}
|
|
997
|
+
this.picker?.highlightAtPoint(ev.clientX, ev.clientY, ev);
|
|
998
|
+
const committed = this.picker ? this.picker.commitActiveCandidate(ev.clientX, ev.clientY) : false;
|
|
999
|
+
this.picker?.stop({ silent: true });
|
|
1000
|
+
finishPeelVisuals();
|
|
1001
|
+
if (committed) {
|
|
1002
|
+
this.openComposer();
|
|
1003
|
+
}
|
|
1004
|
+
else {
|
|
1005
|
+
this.syncUiStates();
|
|
1006
|
+
}
|
|
1007
|
+
};
|
|
1008
|
+
const onPointerCancel = () => {
|
|
1009
|
+
if (!pointerActive)
|
|
1010
|
+
return;
|
|
1011
|
+
peeled = false;
|
|
1012
|
+
activeSnapCorner = null;
|
|
1013
|
+
this.picker?.stop({ silent: true });
|
|
1014
|
+
finishPeelVisuals();
|
|
1015
|
+
clearSuppress();
|
|
1016
|
+
this.syncUiStates();
|
|
1017
|
+
};
|
|
1018
|
+
cornerBtn.addEventListener("pointerdown", (ev) => {
|
|
1019
|
+
if (ev.button !== 0 || typeof window === "undefined")
|
|
1020
|
+
return;
|
|
1021
|
+
startX = ev.clientX;
|
|
1022
|
+
startY = ev.clientY;
|
|
1023
|
+
pointerActive = true;
|
|
1024
|
+
peeled = false;
|
|
1025
|
+
leftOriginZone = false;
|
|
1026
|
+
activeSnapCorner = null;
|
|
1027
|
+
clearSuppress();
|
|
1028
|
+
this.cancelActivePeelDrag = () => {
|
|
1029
|
+
armClickSuppression();
|
|
1030
|
+
onPointerCancel();
|
|
1031
|
+
};
|
|
1032
|
+
window.addEventListener("pointermove", onPointerMove);
|
|
1033
|
+
window.addEventListener("pointerup", onPointerUp);
|
|
1034
|
+
window.addEventListener("pointercancel", onPointerCancel);
|
|
1035
|
+
});
|
|
1036
|
+
}
|
|
1037
|
+
attachDragHandle(handleEl) {
|
|
1038
|
+
let startX = 0;
|
|
1039
|
+
let startY = 0;
|
|
1040
|
+
let initialLeft = 0;
|
|
1041
|
+
let initialTop = 0;
|
|
1042
|
+
let dragging = false;
|
|
1043
|
+
let moved = false;
|
|
1044
|
+
const onPointerMove = (ev) => {
|
|
1045
|
+
if (!dragging)
|
|
1046
|
+
return;
|
|
1047
|
+
const dx = ev.clientX - startX;
|
|
1048
|
+
const dy = ev.clientY - startY;
|
|
1049
|
+
if (!moved && Math.hypot(dx, dy) > 5) {
|
|
1050
|
+
moved = true;
|
|
1051
|
+
}
|
|
1052
|
+
if (moved) {
|
|
1053
|
+
this.setFreePosition(initialLeft + dx, initialTop + dy);
|
|
1054
|
+
}
|
|
1055
|
+
};
|
|
1056
|
+
const cleanupDrag = () => {
|
|
1057
|
+
dragging = false;
|
|
1058
|
+
window.removeEventListener("pointermove", onPointerMove);
|
|
1059
|
+
window.removeEventListener("pointerup", cleanupDrag);
|
|
1060
|
+
window.removeEventListener("pointercancel", cleanupDrag);
|
|
1061
|
+
};
|
|
1062
|
+
handleEl.addEventListener("pointerdown", (ev) => {
|
|
1063
|
+
if (ev.button !== 0)
|
|
1064
|
+
return;
|
|
1065
|
+
const targetEl = ev.target && typeof ev.target.closest === "function"
|
|
1066
|
+
? ev.target
|
|
1067
|
+
: null;
|
|
1068
|
+
const closestBtn = targetEl?.closest("button");
|
|
1069
|
+
if (closestBtn && closestBtn !== handleEl && handleEl.contains(closestBtn)) {
|
|
1070
|
+
return;
|
|
1071
|
+
}
|
|
1072
|
+
if (!this.rootContainer || typeof window === "undefined")
|
|
1073
|
+
return;
|
|
1074
|
+
const rect = this.rootContainer.getBoundingClientRect();
|
|
1075
|
+
startX = ev.clientX;
|
|
1076
|
+
startY = ev.clientY;
|
|
1077
|
+
initialLeft = this.freePos ? this.freePos.left : rect.left;
|
|
1078
|
+
initialTop = this.freePos ? this.freePos.top : rect.top;
|
|
1079
|
+
dragging = true;
|
|
1080
|
+
moved = false;
|
|
1081
|
+
window.addEventListener("pointermove", onPointerMove);
|
|
1082
|
+
window.addEventListener("pointerup", cleanupDrag);
|
|
1083
|
+
window.addEventListener("pointercancel", cleanupDrag);
|
|
1084
|
+
});
|
|
1085
|
+
}
|
|
1086
|
+
attachSidebarResizer(resizerEl) {
|
|
1087
|
+
let startX = 0;
|
|
1088
|
+
let startWidth = this.sidebarWidth;
|
|
1089
|
+
let active = false;
|
|
1090
|
+
const onPointerMove = (ev) => {
|
|
1091
|
+
if (!active || typeof window === "undefined")
|
|
1092
|
+
return;
|
|
1093
|
+
const delta = this.sidebarSide === "right" ? startX - ev.clientX : ev.clientX - startX;
|
|
1094
|
+
const maxAllowed = Math.max(360, (window.innerWidth || 1024) - 80);
|
|
1095
|
+
this.sidebarWidth = Math.max(360, Math.min(Math.min(920, maxAllowed), startWidth + delta));
|
|
1096
|
+
if (this.agentSidebarEl) {
|
|
1097
|
+
this.agentSidebarEl.style.width = `${Math.round(this.sidebarWidth)}px`;
|
|
1098
|
+
}
|
|
1099
|
+
if (this.pushPageLayout && this.isOpen && !this.isMinimized && this.isAgentModeActive()) {
|
|
1100
|
+
this.syncHostPagePushMargin(true);
|
|
1101
|
+
}
|
|
1102
|
+
};
|
|
1103
|
+
const stopResize = () => {
|
|
1104
|
+
active = false;
|
|
1105
|
+
resizerEl.classList.remove("is-resizing");
|
|
1106
|
+
try {
|
|
1107
|
+
localStorage.setItem("context_drop_sidebar_width", String(Math.round(this.sidebarWidth)));
|
|
1108
|
+
}
|
|
1109
|
+
catch {
|
|
1110
|
+
// Non-fatal
|
|
1111
|
+
}
|
|
1112
|
+
window.removeEventListener("pointermove", onPointerMove);
|
|
1113
|
+
window.removeEventListener("pointerup", stopResize);
|
|
1114
|
+
window.removeEventListener("pointercancel", stopResize);
|
|
1115
|
+
};
|
|
1116
|
+
resizerEl.addEventListener("pointerdown", (ev) => {
|
|
1117
|
+
if (ev.button !== 0 || typeof window === "undefined")
|
|
1118
|
+
return;
|
|
1119
|
+
ev.preventDefault();
|
|
1120
|
+
active = true;
|
|
1121
|
+
startX = ev.clientX;
|
|
1122
|
+
startWidth = this.sidebarWidth;
|
|
1123
|
+
resizerEl.classList.add("is-resizing");
|
|
1124
|
+
window.addEventListener("pointermove", onPointerMove);
|
|
1125
|
+
window.addEventListener("pointerup", stopResize);
|
|
1126
|
+
window.addEventListener("pointercancel", stopResize);
|
|
1127
|
+
});
|
|
1128
|
+
}
|
|
1129
|
+
syncHostPagePushMargin(shouldPush) {
|
|
1130
|
+
if (typeof document === "undefined" || !document.documentElement)
|
|
1131
|
+
return;
|
|
1132
|
+
const htmlEl = document.documentElement;
|
|
1133
|
+
if (!shouldPush) {
|
|
1134
|
+
if (htmlEl.getAttribute("data-context-drop-pushed") === "true") {
|
|
1135
|
+
htmlEl.style.marginRight = "";
|
|
1136
|
+
htmlEl.style.marginLeft = "";
|
|
1137
|
+
if (typeof htmlEl.removeAttribute === "function") {
|
|
1138
|
+
htmlEl.removeAttribute("data-context-drop-pushed");
|
|
1139
|
+
}
|
|
1140
|
+
else {
|
|
1141
|
+
htmlEl.setAttribute("data-context-drop-pushed", "");
|
|
1142
|
+
}
|
|
1143
|
+
}
|
|
1144
|
+
return;
|
|
1145
|
+
}
|
|
1146
|
+
htmlEl.setAttribute("data-context-drop-pushed", "true");
|
|
1147
|
+
if (this.sidebarSide === "right") {
|
|
1148
|
+
htmlEl.style.marginLeft = "";
|
|
1149
|
+
htmlEl.style.marginRight = `${Math.round(this.sidebarWidth)}px`;
|
|
1150
|
+
}
|
|
1151
|
+
else {
|
|
1152
|
+
htmlEl.style.marginRight = "";
|
|
1153
|
+
htmlEl.style.marginLeft = `${Math.round(this.sidebarWidth)}px`;
|
|
1154
|
+
}
|
|
1155
|
+
}
|
|
1156
|
+
renderShadowDom() {
|
|
1157
|
+
if (!this.shadow)
|
|
1158
|
+
return;
|
|
1159
|
+
this.shadow.innerHTML = "";
|
|
1160
|
+
const styleEl = document.createElement("style");
|
|
1161
|
+
styleEl.textContent = WIDGET_SHADOW_STYLES;
|
|
1162
|
+
this.shadow.appendChild(styleEl);
|
|
1163
|
+
const root = document.createElement("div");
|
|
1164
|
+
root.className = `cd-root pos-${this.config.position}`;
|
|
1165
|
+
this.rootContainer = root;
|
|
1166
|
+
// 1. Compact Minimized Pinning Bar (visible while pinning elements on the page or minimized)
|
|
1167
|
+
const compactBar = this.buildCompactBarDom();
|
|
1168
|
+
root.appendChild(compactBar);
|
|
1169
|
+
// 2. Standard Floating Feedback Chatbox Composer
|
|
1170
|
+
const composer = this.buildFeedbackComposerDom();
|
|
1171
|
+
root.appendChild(composer);
|
|
1172
|
+
this.shadow.appendChild(root);
|
|
1173
|
+
// 3. Docked Local BYOK Agentic Coding Sidebar (active in local development mode)
|
|
1174
|
+
const sidebar = this.buildAgentSidebarDom();
|
|
1175
|
+
this.shadow.appendChild(sidebar);
|
|
1176
|
+
// 4. Book-Corner Fold Launcher (flush at viewport corner, peels into pushpin on drag)
|
|
1177
|
+
const bookCorner = document.createElement("button");
|
|
1178
|
+
bookCorner.type = "button";
|
|
1179
|
+
bookCorner.className = `cd-book-corner corner-${this.config.position}`;
|
|
1180
|
+
bookCorner.setAttribute("aria-label", "Open ContextDrop chatbox, or drag corner to pin an element on the page");
|
|
1181
|
+
bookCorner.setAttribute("aria-expanded", String(this.isOpen));
|
|
1182
|
+
const cornerArt = document.createElement("span");
|
|
1183
|
+
cornerArt.className = "cd-book-corner-art";
|
|
1184
|
+
cornerArt.innerHTML = BOOK_CORNER_SVG;
|
|
1185
|
+
bookCorner.appendChild(cornerArt);
|
|
1186
|
+
const agentDot = document.createElement("span");
|
|
1187
|
+
agentDot.className = "cd-corner-agent-dot";
|
|
1188
|
+
agentDot.hidden = true;
|
|
1189
|
+
this.bookCornerAgentDotEl = agentDot;
|
|
1190
|
+
bookCorner.appendChild(agentDot);
|
|
1191
|
+
const countBadge = document.createElement("span");
|
|
1192
|
+
countBadge.className = "cd-corner-count";
|
|
1193
|
+
countBadge.hidden = true;
|
|
1194
|
+
this.bookCornerCountEl = countBadge;
|
|
1195
|
+
bookCorner.appendChild(countBadge);
|
|
1196
|
+
const hoverHint = document.createElement("span");
|
|
1197
|
+
hoverHint.className = "cd-corner-hint";
|
|
1198
|
+
hoverHint.setAttribute("aria-hidden", "true");
|
|
1199
|
+
hoverHint.textContent = "Click to chat · Drag to pin";
|
|
1200
|
+
this.bookCornerHintEl = hoverHint;
|
|
1201
|
+
bookCorner.appendChild(hoverHint);
|
|
1202
|
+
this.attachBookCornerLauncher(bookCorner);
|
|
1203
|
+
this.bookCornerBtn = bookCorner;
|
|
1204
|
+
this.shadow.appendChild(bookCorner);
|
|
1205
|
+
// 5. Morphing Drag Pushpin + Viewport Corner Snap Ghost
|
|
1206
|
+
const snapGhost = document.createElement("div");
|
|
1207
|
+
snapGhost.className = `cd-corner-snap-ghost corner-${this.config.position}`;
|
|
1208
|
+
snapGhost.hidden = true;
|
|
1209
|
+
this.cornerSnapGhostEl = snapGhost;
|
|
1210
|
+
this.shadow.appendChild(snapGhost);
|
|
1211
|
+
const dragPin = document.createElement("div");
|
|
1212
|
+
dragPin.className = "cd-drag-pin";
|
|
1213
|
+
dragPin.hidden = true;
|
|
1214
|
+
const dragPinHead = document.createElement("div");
|
|
1215
|
+
dragPinHead.className = "cd-drag-pin-head";
|
|
1216
|
+
dragPinHead.innerHTML = ICON_PIN;
|
|
1217
|
+
dragPin.appendChild(dragPinHead);
|
|
1218
|
+
this.dragPinEl = dragPin;
|
|
1219
|
+
this.shadow.appendChild(dragPin);
|
|
1220
|
+
this.applyTheme(this.currentTheme, false);
|
|
1221
|
+
this.syncUiStates();
|
|
1222
|
+
}
|
|
1223
|
+
buildCompactBarDom() {
|
|
1224
|
+
const compactBar = document.createElement("div");
|
|
1225
|
+
compactBar.className = "cd-compact-bar";
|
|
1226
|
+
compactBar.setAttribute("role", "region");
|
|
1227
|
+
compactBar.setAttribute("aria-label", "ContextDrop Pinning Bar");
|
|
1228
|
+
compactBar.hidden = true;
|
|
1229
|
+
this.compactBarEl = compactBar;
|
|
1230
|
+
const compactGrip = document.createElement("span");
|
|
1231
|
+
compactGrip.className = "cd-pin-dock-grip";
|
|
1232
|
+
compactGrip.title = "Drag to move pinning bar";
|
|
1233
|
+
compactGrip.innerHTML = ICON_GRIP;
|
|
1234
|
+
this.attachDragHandle(compactBar);
|
|
1235
|
+
compactBar.appendChild(compactGrip);
|
|
1236
|
+
const compactPinBtn = document.createElement("button");
|
|
1237
|
+
compactPinBtn.type = "button";
|
|
1238
|
+
compactPinBtn.className = "cd-pin-corner-btn is-active";
|
|
1239
|
+
compactPinBtn.title = "Toggle Freeze & Pin (⌥⇧P)";
|
|
1240
|
+
compactPinBtn.setAttribute("aria-label", "Toggle element pin mode (Option+Shift+P)");
|
|
1241
|
+
compactPinBtn.innerHTML = `${ICON_PIN}<span>Pinning</span>`;
|
|
1242
|
+
compactPinBtn.addEventListener("click", () => this.togglePinSelection());
|
|
1243
|
+
this.compactPinBtn = compactPinBtn;
|
|
1244
|
+
compactBar.appendChild(compactPinBtn);
|
|
1245
|
+
const compactStatus = document.createElement("div");
|
|
1246
|
+
compactStatus.className = "cd-compact-status";
|
|
1247
|
+
this.compactStatusEl = compactStatus;
|
|
1248
|
+
compactBar.appendChild(compactStatus);
|
|
1249
|
+
const compactStepGroup = document.createElement("div");
|
|
1250
|
+
compactStepGroup.className = "cd-compact-step-group";
|
|
1251
|
+
this.compactStepGroupEl = compactStepGroup;
|
|
1252
|
+
const compactLayerDown = document.createElement("button");
|
|
1253
|
+
compactLayerDown.type = "button";
|
|
1254
|
+
compactLayerDown.className = "cd-icon-btn";
|
|
1255
|
+
compactLayerDown.title = "Step to inner/child element (Down arrow)";
|
|
1256
|
+
compactLayerDown.setAttribute("aria-label", "Step to child element");
|
|
1257
|
+
compactLayerDown.innerHTML = ICON_CHEVRON_DOWN;
|
|
1258
|
+
compactLayerDown.addEventListener("click", () => this.picker?.cycleDepth(-1, true));
|
|
1259
|
+
this.compactLayerDownBtn = compactLayerDown;
|
|
1260
|
+
compactStepGroup.appendChild(compactLayerDown);
|
|
1261
|
+
const compactLayerUp = document.createElement("button");
|
|
1262
|
+
compactLayerUp.type = "button";
|
|
1263
|
+
compactLayerUp.className = "cd-icon-btn";
|
|
1264
|
+
compactLayerUp.title = "Step to outer/parent element (Up arrow)";
|
|
1265
|
+
compactLayerUp.setAttribute("aria-label", "Step to parent element");
|
|
1266
|
+
compactLayerUp.innerHTML = ICON_CHEVRON_UP;
|
|
1267
|
+
compactLayerUp.addEventListener("click", () => this.picker?.cycleDepth(1, true));
|
|
1268
|
+
this.compactLayerUpBtn = compactLayerUp;
|
|
1269
|
+
compactStepGroup.appendChild(compactLayerUp);
|
|
1270
|
+
compactBar.appendChild(compactStepGroup);
|
|
1271
|
+
const expandBtn = document.createElement("button");
|
|
1272
|
+
expandBtn.type = "button";
|
|
1273
|
+
expandBtn.className = "cd-btn";
|
|
1274
|
+
expandBtn.setAttribute("aria-label", "Cancel pinning and return to chatbox");
|
|
1275
|
+
expandBtn.innerHTML = `<span>Cancel</span><kbd class="cd-kbd">Esc</kbd>`;
|
|
1276
|
+
expandBtn.addEventListener("click", () => this.expandFromCompactBar());
|
|
1277
|
+
this.compactDoneBtn = expandBtn;
|
|
1278
|
+
compactBar.appendChild(expandBtn);
|
|
1279
|
+
return compactBar;
|
|
1280
|
+
}
|
|
1281
|
+
buildModeSwitchDom(activeMode) {
|
|
1282
|
+
const wrap = document.createElement("div");
|
|
1283
|
+
wrap.className = "cd-mode-switch";
|
|
1284
|
+
wrap.setAttribute("role", "tablist");
|
|
1285
|
+
wrap.setAttribute("aria-label", "ContextDrop mode");
|
|
1286
|
+
const agentBtn = document.createElement("button");
|
|
1287
|
+
agentBtn.type = "button";
|
|
1288
|
+
agentBtn.className = `cd-mode-btn${activeMode === "agent" ? " is-active" : ""}`;
|
|
1289
|
+
agentBtn.setAttribute("aria-selected", String(activeMode === "agent"));
|
|
1290
|
+
agentBtn.innerHTML = `${ICON_CODE_PROMPT}<span>Agent</span>`;
|
|
1291
|
+
agentBtn.addEventListener("click", () => this.setSurfaceMode("agent"));
|
|
1292
|
+
const feedbackBtn = document.createElement("button");
|
|
1293
|
+
feedbackBtn.type = "button";
|
|
1294
|
+
feedbackBtn.className = `cd-mode-btn${activeMode === "feedback" ? " is-active" : ""}`;
|
|
1295
|
+
feedbackBtn.setAttribute("aria-selected", String(activeMode === "feedback"));
|
|
1296
|
+
feedbackBtn.textContent = "Feedback";
|
|
1297
|
+
feedbackBtn.addEventListener("click", () => this.setSurfaceMode("feedback"));
|
|
1298
|
+
wrap.appendChild(agentBtn);
|
|
1299
|
+
wrap.appendChild(feedbackBtn);
|
|
1300
|
+
return wrap;
|
|
1301
|
+
}
|
|
1302
|
+
buildFeedbackComposerDom() {
|
|
1303
|
+
const composer = document.createElement("div");
|
|
1304
|
+
composer.className = "cd-composer";
|
|
1305
|
+
composer.setAttribute("role", "dialog");
|
|
1306
|
+
composer.setAttribute("aria-label", "ContextDrop Feedback Chatbox");
|
|
1307
|
+
composer.hidden = true;
|
|
1308
|
+
this.composerEl = composer;
|
|
1309
|
+
composer.addEventListener("dragover", (ev) => {
|
|
1310
|
+
ev.preventDefault();
|
|
1311
|
+
composer.classList.add("is-dragging-over");
|
|
1312
|
+
});
|
|
1313
|
+
composer.addEventListener("dragleave", () => {
|
|
1314
|
+
composer.classList.remove("is-dragging-over");
|
|
1315
|
+
});
|
|
1316
|
+
composer.addEventListener("drop", (ev) => {
|
|
1317
|
+
ev.preventDefault();
|
|
1318
|
+
composer.classList.remove("is-dragging-over");
|
|
1319
|
+
const files = ev.dataTransfer?.files;
|
|
1320
|
+
if (files && files.length > 0) {
|
|
1321
|
+
void this.handleFilesSelected(Array.from(files));
|
|
1322
|
+
}
|
|
1323
|
+
});
|
|
1324
|
+
const header = document.createElement("div");
|
|
1325
|
+
header.className = "cd-composer-header";
|
|
1326
|
+
this.attachDragHandle(header);
|
|
1327
|
+
const headerLeft = document.createElement("div");
|
|
1328
|
+
headerLeft.className = "cd-header-left";
|
|
1329
|
+
if (this.config.features.element_selection) {
|
|
1330
|
+
const cornerPinBtn = document.createElement("button");
|
|
1331
|
+
cornerPinBtn.type = "button";
|
|
1332
|
+
cornerPinBtn.className = "cd-pin-corner-btn";
|
|
1333
|
+
cornerPinBtn.title = "Freeze & Pin element (⌥⇧P)";
|
|
1334
|
+
cornerPinBtn.setAttribute("aria-label", "Select DOM element on page (Option+Shift+P)");
|
|
1335
|
+
cornerPinBtn.innerHTML = `${ICON_PIN}<span>Pin Element</span>`;
|
|
1336
|
+
cornerPinBtn.addEventListener("click", () => this.startPinSelection());
|
|
1337
|
+
this.cornerPinBtn = cornerPinBtn;
|
|
1338
|
+
headerLeft.appendChild(cornerPinBtn);
|
|
1339
|
+
}
|
|
1340
|
+
const title = document.createElement("h2");
|
|
1341
|
+
title.className = "cd-composer-title";
|
|
1342
|
+
title.textContent = "ContextDrop";
|
|
1343
|
+
this.composerTitleEl = title;
|
|
1344
|
+
headerLeft.appendChild(title);
|
|
1345
|
+
const headerActions = document.createElement("div");
|
|
1346
|
+
headerActions.className = "cd-header-actions";
|
|
1347
|
+
const modeSwitchHolder = document.createElement("div");
|
|
1348
|
+
modeSwitchHolder.className = "cd-composer-mode-switch-wrap";
|
|
1349
|
+
modeSwitchHolder.hidden = !this.config.agent?.enabled;
|
|
1350
|
+
modeSwitchHolder.appendChild(this.buildModeSwitchDom("feedback"));
|
|
1351
|
+
this.composerModeSwitchWrap = modeSwitchHolder;
|
|
1352
|
+
headerActions.appendChild(modeSwitchHolder);
|
|
1353
|
+
const themeBtn = this.buildThemeToggleBtn();
|
|
1354
|
+
this.composerThemeToggleBtn = themeBtn;
|
|
1355
|
+
headerActions.appendChild(themeBtn);
|
|
1356
|
+
const minBtn = document.createElement("button");
|
|
1357
|
+
minBtn.type = "button";
|
|
1358
|
+
minBtn.className = "cd-icon-btn";
|
|
1359
|
+
minBtn.title = "Minimize to compact bar";
|
|
1360
|
+
minBtn.setAttribute("aria-label", "Minimize chatbox to compact bar");
|
|
1361
|
+
minBtn.innerHTML = ICON_MINIMIZE;
|
|
1362
|
+
minBtn.addEventListener("click", () => this.minimizeToCompactBar());
|
|
1363
|
+
this.composerMinBtn = minBtn;
|
|
1364
|
+
const closeBtn = document.createElement("button");
|
|
1365
|
+
closeBtn.type = "button";
|
|
1366
|
+
closeBtn.className = "cd-icon-btn";
|
|
1367
|
+
closeBtn.title = "Close chatbox (Esc)";
|
|
1368
|
+
closeBtn.setAttribute("aria-label", "Close feedback composer");
|
|
1369
|
+
closeBtn.innerHTML = ICON_CLOSE;
|
|
1370
|
+
closeBtn.addEventListener("click", () => this.closeComposer());
|
|
1371
|
+
headerActions.appendChild(minBtn);
|
|
1372
|
+
headerActions.appendChild(closeBtn);
|
|
1373
|
+
header.appendChild(headerLeft);
|
|
1374
|
+
header.appendChild(headerActions);
|
|
1375
|
+
composer.appendChild(header);
|
|
1376
|
+
const chatThread = document.createElement("div");
|
|
1377
|
+
chatThread.className = "cd-chat-thread";
|
|
1378
|
+
chatThread.setAttribute("aria-live", "polite");
|
|
1379
|
+
this.chatThreadEl = chatThread;
|
|
1380
|
+
composer.appendChild(chatThread);
|
|
1381
|
+
this.renderChatThread();
|
|
1382
|
+
const tray = document.createElement("div");
|
|
1383
|
+
tray.className = "cd-tray";
|
|
1384
|
+
tray.setAttribute("aria-label", "Attached context items");
|
|
1385
|
+
this.trayEl = tray;
|
|
1386
|
+
composer.appendChild(tray);
|
|
1387
|
+
const status = document.createElement("div");
|
|
1388
|
+
status.className = "cd-status";
|
|
1389
|
+
status.setAttribute("role", "status");
|
|
1390
|
+
status.hidden = true;
|
|
1391
|
+
this.statusEl = status;
|
|
1392
|
+
composer.appendChild(status);
|
|
1393
|
+
const fileInput = document.createElement("input");
|
|
1394
|
+
fileInput.type = "file";
|
|
1395
|
+
fileInput.multiple = true;
|
|
1396
|
+
fileInput.accept = this.config.limits.allowed_content_types.join(",");
|
|
1397
|
+
fileInput.hidden = true;
|
|
1398
|
+
fileInput.addEventListener("change", () => {
|
|
1399
|
+
if (fileInput.files) {
|
|
1400
|
+
void this.handleFilesSelected(Array.from(fileInput.files));
|
|
1401
|
+
fileInput.value = "";
|
|
1402
|
+
}
|
|
1403
|
+
});
|
|
1404
|
+
this.fileInputEl = fileInput;
|
|
1405
|
+
composer.appendChild(fileInput);
|
|
1406
|
+
const promptShell = document.createElement("div");
|
|
1407
|
+
promptShell.className = "cd-prompt-shell";
|
|
1408
|
+
const textarea = document.createElement("textarea");
|
|
1409
|
+
textarea.className = "cd-textarea";
|
|
1410
|
+
textarea.placeholder = this.config.placeholder;
|
|
1411
|
+
textarea.setAttribute("aria-label", "Feedback description");
|
|
1412
|
+
textarea.addEventListener("keydown", (ev) => {
|
|
1413
|
+
if ((ev.metaKey || ev.ctrlKey) && ev.key === "Enter") {
|
|
1414
|
+
ev.preventDefault();
|
|
1415
|
+
void this.handleSubmit();
|
|
1416
|
+
}
|
|
1417
|
+
else if (ev.key === "Escape") {
|
|
1418
|
+
ev.preventDefault();
|
|
1419
|
+
this.closeComposer();
|
|
1420
|
+
}
|
|
1421
|
+
});
|
|
1422
|
+
textarea.addEventListener("paste", (ev) => this.handlePaste(ev));
|
|
1423
|
+
this.textareaEl = textarea;
|
|
1424
|
+
promptShell.appendChild(textarea);
|
|
1425
|
+
const toolbar = document.createElement("div");
|
|
1426
|
+
toolbar.className = "cd-toolbar";
|
|
1427
|
+
const actions = this.buildPromptToolbarActions(false);
|
|
1428
|
+
const submitBtn = document.createElement("button");
|
|
1429
|
+
submitBtn.type = "button";
|
|
1430
|
+
submitBtn.className = "cd-btn cd-btn-primary";
|
|
1431
|
+
submitBtn.setAttribute("aria-label", "Submit feedback");
|
|
1432
|
+
submitBtn.innerHTML = `${ICON_SEND}<span>Send</span>`;
|
|
1433
|
+
submitBtn.addEventListener("click", () => {
|
|
1434
|
+
void this.handleSubmit();
|
|
1435
|
+
});
|
|
1436
|
+
this.submitBtn = submitBtn;
|
|
1437
|
+
toolbar.appendChild(actions);
|
|
1438
|
+
toolbar.appendChild(submitBtn);
|
|
1439
|
+
promptShell.appendChild(toolbar);
|
|
1440
|
+
composer.appendChild(promptShell);
|
|
1441
|
+
return composer;
|
|
1442
|
+
}
|
|
1443
|
+
buildPromptToolbarActions(forAgentSidebar = false) {
|
|
1444
|
+
const actions = document.createElement("div");
|
|
1445
|
+
actions.className = "cd-toolbar-actions";
|
|
1446
|
+
if (this.config.features.element_selection) {
|
|
1447
|
+
const pickBtn = document.createElement("button");
|
|
1448
|
+
pickBtn.type = "button";
|
|
1449
|
+
pickBtn.className = "cd-btn";
|
|
1450
|
+
pickBtn.title = "Freeze & Pin element (⌥⇧P)";
|
|
1451
|
+
pickBtn.setAttribute("aria-label", "Pin HTML element as context (Option+Shift+P)");
|
|
1452
|
+
pickBtn.innerHTML = `${ICON_PIN}<span>Pin</span><kbd class="cd-kbd">⌥⇧P</kbd>`;
|
|
1453
|
+
pickBtn.addEventListener("click", () => this.startPinSelection());
|
|
1454
|
+
if (!forAgentSidebar) {
|
|
1455
|
+
this.composerToolbarPickBtn = pickBtn;
|
|
1456
|
+
}
|
|
1457
|
+
actions.appendChild(pickBtn);
|
|
1458
|
+
}
|
|
1459
|
+
if (this.config.features.screenshot_capture) {
|
|
1460
|
+
const screenshotBtn = document.createElement("button");
|
|
1461
|
+
screenshotBtn.type = "button";
|
|
1462
|
+
screenshotBtn.className = "cd-btn";
|
|
1463
|
+
screenshotBtn.setAttribute("aria-label", "Capture viewport screenshot");
|
|
1464
|
+
screenshotBtn.innerHTML = `${ICON_CAMERA}<span>Screenshot</span>`;
|
|
1465
|
+
screenshotBtn.addEventListener("click", () => {
|
|
1466
|
+
void this.handleViewportScreenshot();
|
|
1467
|
+
});
|
|
1468
|
+
if (!forAgentSidebar) {
|
|
1469
|
+
this.composerToolbarScreenshotBtn = screenshotBtn;
|
|
1470
|
+
}
|
|
1471
|
+
actions.appendChild(screenshotBtn);
|
|
1472
|
+
}
|
|
1473
|
+
const uploadBtn = document.createElement("button");
|
|
1474
|
+
uploadBtn.type = "button";
|
|
1475
|
+
uploadBtn.className = "cd-btn";
|
|
1476
|
+
uploadBtn.setAttribute("aria-label", "Upload image attachment");
|
|
1477
|
+
uploadBtn.innerHTML = `${ICON_IMAGE}<span>Image</span>`;
|
|
1478
|
+
uploadBtn.addEventListener("click", () => this.fileInputEl?.click());
|
|
1479
|
+
if (!forAgentSidebar) {
|
|
1480
|
+
this.composerToolbarUploadBtn = uploadBtn;
|
|
1481
|
+
}
|
|
1482
|
+
actions.appendChild(uploadBtn);
|
|
1483
|
+
return actions;
|
|
1484
|
+
}
|
|
1485
|
+
buildAgentSidebarDom() {
|
|
1486
|
+
const sidebar = document.createElement("aside");
|
|
1487
|
+
sidebar.className = `cd-agent-sidebar side-${this.sidebarSide}`;
|
|
1488
|
+
sidebar.setAttribute("role", "dialog");
|
|
1489
|
+
sidebar.setAttribute("aria-label", "ContextDrop Local Coding Agent Sidebar");
|
|
1490
|
+
sidebar.style.width = `${this.sidebarWidth}px`;
|
|
1491
|
+
sidebar.hidden = true;
|
|
1492
|
+
this.agentSidebarEl = sidebar;
|
|
1493
|
+
sidebar.addEventListener("dragover", (ev) => {
|
|
1494
|
+
ev.preventDefault();
|
|
1495
|
+
sidebar.classList.add("is-dragging-over");
|
|
1496
|
+
});
|
|
1497
|
+
sidebar.addEventListener("dragleave", () => {
|
|
1498
|
+
sidebar.classList.remove("is-dragging-over");
|
|
1499
|
+
});
|
|
1500
|
+
sidebar.addEventListener("drop", (ev) => {
|
|
1501
|
+
ev.preventDefault();
|
|
1502
|
+
sidebar.classList.remove("is-dragging-over");
|
|
1503
|
+
const files = ev.dataTransfer?.files;
|
|
1504
|
+
if (files && files.length > 0) {
|
|
1505
|
+
void this.handleFilesSelected(Array.from(files));
|
|
1506
|
+
}
|
|
1507
|
+
});
|
|
1508
|
+
const resizer = document.createElement("div");
|
|
1509
|
+
resizer.className = "cd-sidebar-resizer";
|
|
1510
|
+
resizer.title = "Drag edge to resize sidebar";
|
|
1511
|
+
this.attachSidebarResizer(resizer);
|
|
1512
|
+
sidebar.appendChild(resizer);
|
|
1513
|
+
// 1. Sidebar Header
|
|
1514
|
+
const header = document.createElement("div");
|
|
1515
|
+
header.className = "cd-sidebar-header";
|
|
1516
|
+
const headerLeft = document.createElement("div");
|
|
1517
|
+
headerLeft.className = "cd-header-left";
|
|
1518
|
+
if (this.config.features.element_selection) {
|
|
1519
|
+
const pinBtn = document.createElement("button");
|
|
1520
|
+
pinBtn.type = "button";
|
|
1521
|
+
pinBtn.className = "cd-pin-corner-btn";
|
|
1522
|
+
pinBtn.title = "Freeze & Pin element (⌥⇧P)";
|
|
1523
|
+
pinBtn.setAttribute("aria-label", "Pin DOM element for agent (Option+Shift+P)");
|
|
1524
|
+
pinBtn.innerHTML = `${ICON_PIN}<span>Pin</span>`;
|
|
1525
|
+
pinBtn.addEventListener("click", () => this.startPinSelection());
|
|
1526
|
+
this.sidebarPinBtn = pinBtn;
|
|
1527
|
+
headerLeft.appendChild(pinBtn);
|
|
1528
|
+
}
|
|
1529
|
+
const title = document.createElement("h2");
|
|
1530
|
+
title.className = "cd-composer-title";
|
|
1531
|
+
title.textContent = "ContextDrop";
|
|
1532
|
+
headerLeft.appendChild(title);
|
|
1533
|
+
const envPill = document.createElement("span");
|
|
1534
|
+
envPill.className = "cd-env-pill is-local";
|
|
1535
|
+
envPill.textContent = this.getShortEnvironmentSlug();
|
|
1536
|
+
this.sidebarEnvPillEl = envPill;
|
|
1537
|
+
headerLeft.appendChild(envPill);
|
|
1538
|
+
const headerActions = document.createElement("div");
|
|
1539
|
+
headerActions.className = "cd-header-actions";
|
|
1540
|
+
headerActions.appendChild(this.buildModeSwitchDom("agent"));
|
|
1541
|
+
const themeBtn = this.buildThemeToggleBtn();
|
|
1542
|
+
this.sidebarThemeToggleBtn = themeBtn;
|
|
1543
|
+
headerActions.appendChild(themeBtn);
|
|
1544
|
+
const newThreadBtn = document.createElement("button");
|
|
1545
|
+
newThreadBtn.type = "button";
|
|
1546
|
+
newThreadBtn.className = "cd-icon-btn";
|
|
1547
|
+
newThreadBtn.title = "New agent conversation";
|
|
1548
|
+
newThreadBtn.setAttribute("aria-label", "Start new agent conversation");
|
|
1549
|
+
newThreadBtn.innerHTML = ICON_PLUS;
|
|
1550
|
+
newThreadBtn.addEventListener("click", () => {
|
|
1551
|
+
this.agentMessages = [];
|
|
1552
|
+
this.renderAgentStream();
|
|
1553
|
+
this.agentTextareaEl?.focus();
|
|
1554
|
+
});
|
|
1555
|
+
const pushLayoutBtn = document.createElement("button");
|
|
1556
|
+
pushLayoutBtn.type = "button";
|
|
1557
|
+
pushLayoutBtn.className = "cd-icon-btn";
|
|
1558
|
+
pushLayoutBtn.title = "Toggle pushing page layout alongside sidebar";
|
|
1559
|
+
pushLayoutBtn.setAttribute("aria-label", "Toggle push page layout");
|
|
1560
|
+
pushLayoutBtn.innerHTML = ICON_PUSH_LAYOUT;
|
|
1561
|
+
pushLayoutBtn.addEventListener("click", () => {
|
|
1562
|
+
this.pushPageLayout = !this.pushPageLayout;
|
|
1563
|
+
this.syncUiStates();
|
|
1564
|
+
});
|
|
1565
|
+
this.sidebarPushBtn = pushLayoutBtn;
|
|
1566
|
+
const flipSideBtn = document.createElement("button");
|
|
1567
|
+
flipSideBtn.type = "button";
|
|
1568
|
+
flipSideBtn.className = "cd-icon-btn";
|
|
1569
|
+
flipSideBtn.title = "Dock sidebar on left or right";
|
|
1570
|
+
flipSideBtn.setAttribute("aria-label", "Switch sidebar dock side");
|
|
1571
|
+
flipSideBtn.innerHTML = ICON_SIDEBAR;
|
|
1572
|
+
flipSideBtn.addEventListener("click", () => {
|
|
1573
|
+
this.sidebarSide = this.sidebarSide === "right" ? "left" : "right";
|
|
1574
|
+
sidebar.className = `cd-agent-sidebar side-${this.sidebarSide}`;
|
|
1575
|
+
this.syncUiStates();
|
|
1576
|
+
});
|
|
1577
|
+
const minBtn = document.createElement("button");
|
|
1578
|
+
minBtn.type = "button";
|
|
1579
|
+
minBtn.className = "cd-icon-btn";
|
|
1580
|
+
minBtn.title = "Minimize sidebar";
|
|
1581
|
+
minBtn.setAttribute("aria-label", "Minimize agent sidebar");
|
|
1582
|
+
minBtn.innerHTML = ICON_MINIMIZE;
|
|
1583
|
+
minBtn.addEventListener("click", () => this.minimizeToCompactBar());
|
|
1584
|
+
const closeBtn = document.createElement("button");
|
|
1585
|
+
closeBtn.type = "button";
|
|
1586
|
+
closeBtn.className = "cd-icon-btn";
|
|
1587
|
+
closeBtn.title = "Close sidebar (Esc)";
|
|
1588
|
+
closeBtn.setAttribute("aria-label", "Close agent sidebar");
|
|
1589
|
+
closeBtn.innerHTML = ICON_CLOSE;
|
|
1590
|
+
closeBtn.addEventListener("click", () => this.closeComposer());
|
|
1591
|
+
headerActions.appendChild(newThreadBtn);
|
|
1592
|
+
headerActions.appendChild(pushLayoutBtn);
|
|
1593
|
+
headerActions.appendChild(flipSideBtn);
|
|
1594
|
+
headerActions.appendChild(minBtn);
|
|
1595
|
+
headerActions.appendChild(closeBtn);
|
|
1596
|
+
header.appendChild(headerLeft);
|
|
1597
|
+
header.appendChild(headerActions);
|
|
1598
|
+
sidebar.appendChild(header);
|
|
1599
|
+
// 2. Provider + BYOK Vault + Cross-Environment Feedbacks Subbar
|
|
1600
|
+
const subbar = document.createElement("div");
|
|
1601
|
+
subbar.className = "cd-agent-subbar";
|
|
1602
|
+
const providerPill = document.createElement("button");
|
|
1603
|
+
providerPill.type = "button";
|
|
1604
|
+
providerPill.className = "cd-provider-pill-btn";
|
|
1605
|
+
providerPill.title = "Configure BYOK provider & model";
|
|
1606
|
+
providerPill.setAttribute("aria-label", "Configure BYOK provider and model");
|
|
1607
|
+
const keyDot = document.createElement("span");
|
|
1608
|
+
keyDot.className = "cd-key-dot";
|
|
1609
|
+
this.keyDotEl = keyDot;
|
|
1610
|
+
const providerLabel = document.createElement("span");
|
|
1611
|
+
providerLabel.className = "cd-provider-pill-label";
|
|
1612
|
+
providerLabel.textContent = "claude-sonnet-4 · BYOK";
|
|
1613
|
+
this.providerPillLabelEl = providerLabel;
|
|
1614
|
+
providerPill.appendChild(keyDot);
|
|
1615
|
+
providerPill.appendChild(providerLabel);
|
|
1616
|
+
providerPill.addEventListener("click", () => this.toggleByokDrawer());
|
|
1617
|
+
const subbarRight = document.createElement("div");
|
|
1618
|
+
subbarRight.className = "cd-subbar-right";
|
|
1619
|
+
const keysBtn = document.createElement("button");
|
|
1620
|
+
keysBtn.type = "button";
|
|
1621
|
+
keysBtn.className = "cd-btn";
|
|
1622
|
+
keysBtn.setAttribute("aria-label", "Open BYOK API key settings");
|
|
1623
|
+
keysBtn.innerHTML = `${ICON_KEY}<span>BYOK</span>`;
|
|
1624
|
+
keysBtn.addEventListener("click", () => this.toggleByokDrawer());
|
|
1625
|
+
this.keysToggleBtn = keysBtn;
|
|
1626
|
+
const feedbacksBtn = document.createElement("button");
|
|
1627
|
+
feedbacksBtn.type = "button";
|
|
1628
|
+
feedbacksBtn.className = "cd-btn";
|
|
1629
|
+
feedbacksBtn.setAttribute("aria-label", "Browse feedbacks across environments");
|
|
1630
|
+
feedbacksBtn.innerHTML = `${ICON_INBOX}<span>Feedbacks</span>`;
|
|
1631
|
+
feedbacksBtn.addEventListener("click", () => this.toggleFeedbacksDrawer());
|
|
1632
|
+
this.feedbacksToggleBtn = feedbacksBtn;
|
|
1633
|
+
subbarRight.appendChild(keysBtn);
|
|
1634
|
+
subbarRight.appendChild(feedbacksBtn);
|
|
1635
|
+
subbar.appendChild(providerPill);
|
|
1636
|
+
subbar.appendChild(subbarRight);
|
|
1637
|
+
sidebar.appendChild(subbar);
|
|
1638
|
+
// 3. Inline BYOK Vault Drawer
|
|
1639
|
+
sidebar.appendChild(this.buildByokDrawerDom());
|
|
1640
|
+
// 4. Inline Cross-Environment Feedbacks Drawer
|
|
1641
|
+
sidebar.appendChild(this.buildFeedbacksDrawerDom());
|
|
1642
|
+
// 5. Agent Conversation Stream
|
|
1643
|
+
const stream = document.createElement("div");
|
|
1644
|
+
stream.className = "cd-agent-stream";
|
|
1645
|
+
stream.setAttribute("aria-live", "polite");
|
|
1646
|
+
this.agentStreamEl = stream;
|
|
1647
|
+
sidebar.appendChild(stream);
|
|
1648
|
+
this.renderAgentStream();
|
|
1649
|
+
// 6. Shared Context Tray + Status + Prompt Input Shell
|
|
1650
|
+
const agentTray = document.createElement("div");
|
|
1651
|
+
agentTray.className = "cd-tray";
|
|
1652
|
+
agentTray.setAttribute("aria-label", "Pinned DOM context for agent");
|
|
1653
|
+
this.agentTrayEl = agentTray;
|
|
1654
|
+
sidebar.appendChild(agentTray);
|
|
1655
|
+
const agentStatus = document.createElement("div");
|
|
1656
|
+
agentStatus.className = "cd-status";
|
|
1657
|
+
agentStatus.setAttribute("role", "status");
|
|
1658
|
+
agentStatus.hidden = true;
|
|
1659
|
+
this.agentStatusEl = agentStatus;
|
|
1660
|
+
sidebar.appendChild(agentStatus);
|
|
1661
|
+
const promptShell = document.createElement("div");
|
|
1662
|
+
promptShell.className = "cd-prompt-shell";
|
|
1663
|
+
const textarea = document.createElement("textarea");
|
|
1664
|
+
textarea.className = "cd-textarea";
|
|
1665
|
+
textarea.placeholder =
|
|
1666
|
+
"Ask the local agent to inspect pinned DOM elements, read Rails views, or propose a fix...";
|
|
1667
|
+
textarea.setAttribute("aria-label", "Agent prompt");
|
|
1668
|
+
textarea.addEventListener("keydown", (ev) => {
|
|
1669
|
+
if ((ev.metaKey || ev.ctrlKey) && ev.key === "Enter") {
|
|
1670
|
+
ev.preventDefault();
|
|
1671
|
+
void this.handleAgentSubmit();
|
|
1672
|
+
}
|
|
1673
|
+
else if (ev.key === "Escape") {
|
|
1674
|
+
ev.preventDefault();
|
|
1675
|
+
this.closeComposer();
|
|
1676
|
+
}
|
|
1677
|
+
});
|
|
1678
|
+
textarea.addEventListener("paste", (ev) => this.handlePaste(ev));
|
|
1679
|
+
this.agentTextareaEl = textarea;
|
|
1680
|
+
promptShell.appendChild(textarea);
|
|
1681
|
+
const toolbar = document.createElement("div");
|
|
1682
|
+
toolbar.className = "cd-toolbar";
|
|
1683
|
+
toolbar.appendChild(this.buildPromptToolbarActions(true));
|
|
1684
|
+
const sendBtn = document.createElement("button");
|
|
1685
|
+
sendBtn.type = "button";
|
|
1686
|
+
sendBtn.className = "cd-btn cd-btn-primary";
|
|
1687
|
+
sendBtn.setAttribute("aria-label", "Send prompt to local agent");
|
|
1688
|
+
sendBtn.innerHTML = `${ICON_SEND}<span>Ask Agent</span>`;
|
|
1689
|
+
sendBtn.addEventListener("click", () => {
|
|
1690
|
+
void this.handleAgentSubmit();
|
|
1691
|
+
});
|
|
1692
|
+
this.agentSendBtn = sendBtn;
|
|
1693
|
+
toolbar.appendChild(sendBtn);
|
|
1694
|
+
promptShell.appendChild(toolbar);
|
|
1695
|
+
sidebar.appendChild(promptShell);
|
|
1696
|
+
this.syncProviderControls();
|
|
1697
|
+
return sidebar;
|
|
1698
|
+
}
|
|
1699
|
+
buildByokDrawerDom() {
|
|
1700
|
+
const drawer = document.createElement("div");
|
|
1701
|
+
drawer.className = "cd-inline-drawer cd-byok-drawer";
|
|
1702
|
+
drawer.hidden = true;
|
|
1703
|
+
this.byokDrawerEl = drawer;
|
|
1704
|
+
const securityNote = document.createElement("div");
|
|
1705
|
+
securityNote.className = "cd-vault-security-note";
|
|
1706
|
+
securityNote.innerHTML = `${ICON_SHIELD}<span>Keys stay encrypted on disk in <code>tmp/context_drop_byok.enc</code> (AES-256-GCM) or read from server ENV—never stored in browser storage.</span>`;
|
|
1707
|
+
drawer.appendChild(securityNote);
|
|
1708
|
+
const grid = document.createElement("div");
|
|
1709
|
+
grid.className = "cd-form-grid";
|
|
1710
|
+
// Provider field
|
|
1711
|
+
const provField = document.createElement("div");
|
|
1712
|
+
provField.className = "cd-field";
|
|
1713
|
+
const provLabel = document.createElement("label");
|
|
1714
|
+
provLabel.className = "cd-field-label";
|
|
1715
|
+
provLabel.textContent = "Provider";
|
|
1716
|
+
const provSelect = document.createElement("select");
|
|
1717
|
+
provSelect.className = "cd-select";
|
|
1718
|
+
provSelect.setAttribute("aria-label", "LLM Provider");
|
|
1719
|
+
provSelect.addEventListener("change", () => {
|
|
1720
|
+
this.selectedVaultProviderId = provSelect.value;
|
|
1721
|
+
this.syncByokDrawerFields();
|
|
1722
|
+
});
|
|
1723
|
+
this.byokProviderSelect = provSelect;
|
|
1724
|
+
provField.appendChild(provLabel);
|
|
1725
|
+
provField.appendChild(provSelect);
|
|
1726
|
+
grid.appendChild(provField);
|
|
1727
|
+
// Preset model picker
|
|
1728
|
+
const presetField = document.createElement("div");
|
|
1729
|
+
presetField.className = "cd-field";
|
|
1730
|
+
const presetLabel = document.createElement("label");
|
|
1731
|
+
presetLabel.className = "cd-field-label";
|
|
1732
|
+
presetLabel.textContent = "Preset";
|
|
1733
|
+
const presetSelect = document.createElement("select");
|
|
1734
|
+
presetSelect.className = "cd-select";
|
|
1735
|
+
presetSelect.setAttribute("aria-label", "Model preset");
|
|
1736
|
+
presetSelect.addEventListener("change", () => {
|
|
1737
|
+
if (this.byokModelInput && presetSelect.value) {
|
|
1738
|
+
this.byokModelInput.value = presetSelect.value;
|
|
1739
|
+
}
|
|
1740
|
+
});
|
|
1741
|
+
this.byokModelPresetsSelect = presetSelect;
|
|
1742
|
+
presetField.appendChild(presetLabel);
|
|
1743
|
+
presetField.appendChild(presetSelect);
|
|
1744
|
+
grid.appendChild(presetField);
|
|
1745
|
+
// Editable model ID
|
|
1746
|
+
const modelField = document.createElement("div");
|
|
1747
|
+
modelField.className = "cd-field is-full";
|
|
1748
|
+
const modelLabel = document.createElement("label");
|
|
1749
|
+
modelLabel.className = "cd-field-label";
|
|
1750
|
+
modelLabel.textContent = "Model ID";
|
|
1751
|
+
const modelInput = document.createElement("input");
|
|
1752
|
+
modelInput.type = "text";
|
|
1753
|
+
modelInput.className = "cd-input is-mono";
|
|
1754
|
+
modelInput.setAttribute("aria-label", "Model identifier");
|
|
1755
|
+
this.byokModelInput = modelInput;
|
|
1756
|
+
modelField.appendChild(modelLabel);
|
|
1757
|
+
modelField.appendChild(modelInput);
|
|
1758
|
+
grid.appendChild(modelField);
|
|
1759
|
+
// Optional Base URL field (for custom OpenAI-compatible provider)
|
|
1760
|
+
const baseUrlField = document.createElement("div");
|
|
1761
|
+
baseUrlField.className = "cd-field is-full";
|
|
1762
|
+
baseUrlField.hidden = true;
|
|
1763
|
+
const baseUrlLabel = document.createElement("label");
|
|
1764
|
+
baseUrlLabel.className = "cd-field-label";
|
|
1765
|
+
baseUrlLabel.textContent = "OpenAI-Compatible Base URL";
|
|
1766
|
+
const baseUrlInput = document.createElement("input");
|
|
1767
|
+
baseUrlInput.type = "url";
|
|
1768
|
+
baseUrlInput.className = "cd-input is-mono";
|
|
1769
|
+
baseUrlInput.placeholder = "https://openrouter.ai/api/v1 or http://localhost:11434/v1";
|
|
1770
|
+
baseUrlInput.setAttribute("aria-label", "Custom OpenAI-compatible Base URL");
|
|
1771
|
+
this.byokBaseUrlInput = baseUrlInput;
|
|
1772
|
+
baseUrlField.appendChild(baseUrlLabel);
|
|
1773
|
+
baseUrlField.appendChild(baseUrlInput);
|
|
1774
|
+
this.byokBaseUrlField = baseUrlField;
|
|
1775
|
+
grid.appendChild(baseUrlField);
|
|
1776
|
+
// API Key field
|
|
1777
|
+
const keyField = document.createElement("div");
|
|
1778
|
+
keyField.className = "cd-field is-full";
|
|
1779
|
+
const keyLabel = document.createElement("label");
|
|
1780
|
+
keyLabel.className = "cd-field-label";
|
|
1781
|
+
const keyLabelText = document.createElement("span");
|
|
1782
|
+
keyLabelText.textContent = "API Key";
|
|
1783
|
+
const keyBadge = document.createElement("span");
|
|
1784
|
+
keyBadge.className = "cd-key-source-badge";
|
|
1785
|
+
keyBadge.hidden = true;
|
|
1786
|
+
this.byokKeySourceBadge = keyBadge;
|
|
1787
|
+
keyLabel.appendChild(keyLabelText);
|
|
1788
|
+
keyLabel.appendChild(keyBadge);
|
|
1789
|
+
const keyInput = document.createElement("input");
|
|
1790
|
+
keyInput.type = "password";
|
|
1791
|
+
keyInput.autocomplete = "off";
|
|
1792
|
+
keyInput.className = "cd-input is-mono";
|
|
1793
|
+
keyInput.placeholder = "Paste API key (leave blank to keep existing key)";
|
|
1794
|
+
keyInput.setAttribute("aria-label", "Provider API Key");
|
|
1795
|
+
this.byokKeyInput = keyInput;
|
|
1796
|
+
keyField.appendChild(keyLabel);
|
|
1797
|
+
keyField.appendChild(keyInput);
|
|
1798
|
+
grid.appendChild(keyField);
|
|
1799
|
+
drawer.appendChild(grid);
|
|
1800
|
+
const actions = document.createElement("div");
|
|
1801
|
+
actions.className = "cd-drawer-actions";
|
|
1802
|
+
const clearBtn = document.createElement("button");
|
|
1803
|
+
clearBtn.type = "button";
|
|
1804
|
+
clearBtn.className = "cd-btn cd-btn-danger-outline";
|
|
1805
|
+
clearBtn.textContent = "Clear Vault Key";
|
|
1806
|
+
clearBtn.addEventListener("click", () => {
|
|
1807
|
+
void this.handleClearByokKey();
|
|
1808
|
+
});
|
|
1809
|
+
const rightGroup = document.createElement("div");
|
|
1810
|
+
rightGroup.className = "cd-diff-btn-group";
|
|
1811
|
+
const closeBtn = document.createElement("button");
|
|
1812
|
+
closeBtn.type = "button";
|
|
1813
|
+
closeBtn.className = "cd-btn";
|
|
1814
|
+
closeBtn.textContent = "Close";
|
|
1815
|
+
closeBtn.addEventListener("click", () => {
|
|
1816
|
+
this.isByokDrawerOpen = false;
|
|
1817
|
+
this.syncUiStates();
|
|
1818
|
+
});
|
|
1819
|
+
const saveBtn = document.createElement("button");
|
|
1820
|
+
saveBtn.type = "button";
|
|
1821
|
+
saveBtn.className = "cd-btn cd-btn-primary";
|
|
1822
|
+
saveBtn.textContent = "Use & Save";
|
|
1823
|
+
saveBtn.addEventListener("click", () => {
|
|
1824
|
+
void this.handleSaveByokSettings();
|
|
1825
|
+
});
|
|
1826
|
+
rightGroup.appendChild(closeBtn);
|
|
1827
|
+
rightGroup.appendChild(saveBtn);
|
|
1828
|
+
actions.appendChild(clearBtn);
|
|
1829
|
+
actions.appendChild(rightGroup);
|
|
1830
|
+
drawer.appendChild(actions);
|
|
1831
|
+
return drawer;
|
|
1832
|
+
}
|
|
1833
|
+
buildFeedbacksDrawerDom() {
|
|
1834
|
+
const drawer = document.createElement("div");
|
|
1835
|
+
drawer.className = "cd-inline-drawer cd-feedbacks-drawer";
|
|
1836
|
+
drawer.hidden = true;
|
|
1837
|
+
this.feedbacksDrawerEl = drawer;
|
|
1838
|
+
const filterRow = document.createElement("div");
|
|
1839
|
+
filterRow.className = "cd-env-filter-row";
|
|
1840
|
+
const tabs = document.createElement("div");
|
|
1841
|
+
tabs.className = "cd-env-tabs";
|
|
1842
|
+
this.feedbacksTabsEl = tabs;
|
|
1843
|
+
filterRow.appendChild(tabs);
|
|
1844
|
+
const refreshBtn = document.createElement("button");
|
|
1845
|
+
refreshBtn.type = "button";
|
|
1846
|
+
refreshBtn.className = "cd-btn";
|
|
1847
|
+
refreshBtn.textContent = "Refresh";
|
|
1848
|
+
refreshBtn.addEventListener("click", () => {
|
|
1849
|
+
void this.loadCrossEnvFeedbacks(this.feedbacksEnvFilter);
|
|
1850
|
+
});
|
|
1851
|
+
filterRow.appendChild(refreshBtn);
|
|
1852
|
+
drawer.appendChild(filterRow);
|
|
1853
|
+
const list = document.createElement("div");
|
|
1854
|
+
list.className = "cd-feedbacks-list";
|
|
1855
|
+
this.feedbacksListEl = list;
|
|
1856
|
+
drawer.appendChild(list);
|
|
1857
|
+
return drawer;
|
|
1858
|
+
}
|
|
1859
|
+
getShortEnvironmentSlug() {
|
|
1860
|
+
const raw = (this.config.environment || "development").toLowerCase();
|
|
1861
|
+
if (raw === "development")
|
|
1862
|
+
return "dev";
|
|
1863
|
+
if (raw === "production")
|
|
1864
|
+
return "prod";
|
|
1865
|
+
return raw;
|
|
1866
|
+
}
|
|
1867
|
+
toggleByokDrawer() {
|
|
1868
|
+
this.isByokDrawerOpen = !this.isByokDrawerOpen;
|
|
1869
|
+
if (this.isByokDrawerOpen) {
|
|
1870
|
+
this.isFeedbacksDrawerOpen = false;
|
|
1871
|
+
this.selectedVaultProviderId = this.activeProviderId;
|
|
1872
|
+
this.syncProviderControls();
|
|
1873
|
+
}
|
|
1874
|
+
this.syncUiStates();
|
|
1875
|
+
}
|
|
1876
|
+
toggleFeedbacksDrawer() {
|
|
1877
|
+
this.isFeedbacksDrawerOpen = !this.isFeedbacksDrawerOpen;
|
|
1878
|
+
if (this.isFeedbacksDrawerOpen) {
|
|
1879
|
+
this.isByokDrawerOpen = false;
|
|
1880
|
+
void this.loadCrossEnvFeedbacks(this.feedbacksEnvFilter);
|
|
1881
|
+
}
|
|
1882
|
+
this.syncUiStates();
|
|
1883
|
+
}
|
|
1884
|
+
async ensureAgentStatusLoaded() {
|
|
1885
|
+
if (this.agentStatusLoaded)
|
|
1886
|
+
return;
|
|
1887
|
+
try {
|
|
1888
|
+
const status = await this.apiClient.fetchAgentStatus();
|
|
1889
|
+
this.agentStatusLoaded = true;
|
|
1890
|
+
this.agentWorkspaceName = status.workspace_name || this.agentWorkspaceName;
|
|
1891
|
+
if (Array.isArray(status.mcp_environments) && status.mcp_environments.length > 0) {
|
|
1892
|
+
this.agentMcpEnvironments = status.mcp_environments.map((e) => e.name);
|
|
1893
|
+
}
|
|
1894
|
+
if (Array.isArray(status.providers) && status.providers.length > 0) {
|
|
1895
|
+
this.agentProviders = status.providers;
|
|
1896
|
+
}
|
|
1897
|
+
if (status.active_provider) {
|
|
1898
|
+
this.activeProviderId = status.active_provider;
|
|
1899
|
+
this.selectedVaultProviderId = status.active_provider;
|
|
1900
|
+
}
|
|
1901
|
+
this.syncProviderControls();
|
|
1902
|
+
this.renderAgentStream();
|
|
1903
|
+
}
|
|
1904
|
+
catch {
|
|
1905
|
+
// Keep fallback provider definitions if offline
|
|
1906
|
+
}
|
|
1907
|
+
}
|
|
1908
|
+
getActiveProviderInfo() {
|
|
1909
|
+
return (this.agentProviders.find((p) => p.id === this.activeProviderId) ||
|
|
1910
|
+
this.agentProviders[0] ||
|
|
1911
|
+
DEFAULT_PROVIDERS_FALLBACK[0]);
|
|
1912
|
+
}
|
|
1913
|
+
syncProviderControls() {
|
|
1914
|
+
const active = this.getActiveProviderInfo();
|
|
1915
|
+
if (this.keyDotEl) {
|
|
1916
|
+
this.keyDotEl.classList.toggle("is-ready", Boolean(active.configured));
|
|
1917
|
+
}
|
|
1918
|
+
if (this.providerPillLabelEl) {
|
|
1919
|
+
const sourceTag = active.source === "env"
|
|
1920
|
+
? "ENV"
|
|
1921
|
+
: active.source === "vault"
|
|
1922
|
+
? "Vault"
|
|
1923
|
+
: active.source === "local_no_key"
|
|
1924
|
+
? "Local"
|
|
1925
|
+
: "Set BYOK";
|
|
1926
|
+
this.providerPillLabelEl.textContent = `${active.model} · ${sourceTag}`;
|
|
1927
|
+
}
|
|
1928
|
+
if (this.byokProviderSelect) {
|
|
1929
|
+
this.byokProviderSelect.innerHTML = "";
|
|
1930
|
+
for (const prov of this.agentProviders) {
|
|
1931
|
+
const opt = document.createElement("option");
|
|
1932
|
+
opt.value = prov.id;
|
|
1933
|
+
opt.textContent = prov.configured ? `${prov.label} (Ready)` : prov.label;
|
|
1934
|
+
if (prov.id === this.selectedVaultProviderId) {
|
|
1935
|
+
opt.selected = true;
|
|
1936
|
+
}
|
|
1937
|
+
this.byokProviderSelect.appendChild(opt);
|
|
1938
|
+
}
|
|
1939
|
+
this.byokProviderSelect.value = this.selectedVaultProviderId;
|
|
1940
|
+
}
|
|
1941
|
+
this.syncByokDrawerFields();
|
|
1942
|
+
}
|
|
1943
|
+
syncByokDrawerFields() {
|
|
1944
|
+
const selected = this.agentProviders.find((p) => p.id === this.selectedVaultProviderId) ||
|
|
1945
|
+
this.getActiveProviderInfo();
|
|
1946
|
+
if (this.byokModelPresetsSelect) {
|
|
1947
|
+
this.byokModelPresetsSelect.innerHTML = "";
|
|
1948
|
+
for (const modelName of selected.models || [selected.default_model]) {
|
|
1949
|
+
const opt = document.createElement("option");
|
|
1950
|
+
opt.value = modelName;
|
|
1951
|
+
opt.textContent = modelName;
|
|
1952
|
+
if (modelName === selected.model) {
|
|
1953
|
+
opt.selected = true;
|
|
1954
|
+
}
|
|
1955
|
+
this.byokModelPresetsSelect.appendChild(opt);
|
|
1956
|
+
}
|
|
1957
|
+
}
|
|
1958
|
+
if (this.byokModelInput) {
|
|
1959
|
+
this.byokModelInput.value = selected.model || selected.default_model;
|
|
1960
|
+
}
|
|
1961
|
+
if (this.byokBaseUrlField && this.byokBaseUrlInput) {
|
|
1962
|
+
this.byokBaseUrlField.hidden = !selected.supports_base_url;
|
|
1963
|
+
this.byokBaseUrlInput.value = selected.base_url || "";
|
|
1964
|
+
}
|
|
1965
|
+
if (this.byokKeySourceBadge) {
|
|
1966
|
+
if (selected.configured) {
|
|
1967
|
+
this.byokKeySourceBadge.hidden = false;
|
|
1968
|
+
const src = selected.source === "env"
|
|
1969
|
+
? `ENV ${selected.env_var_hint}`
|
|
1970
|
+
: selected.source === "vault"
|
|
1971
|
+
? "Vault"
|
|
1972
|
+
: "Local URL";
|
|
1973
|
+
this.byokKeySourceBadge.textContent = selected.masked_key
|
|
1974
|
+
? `${src} (${selected.masked_key})`
|
|
1975
|
+
: src;
|
|
1976
|
+
}
|
|
1977
|
+
else {
|
|
1978
|
+
this.byokKeySourceBadge.hidden = true;
|
|
1979
|
+
}
|
|
1980
|
+
}
|
|
1981
|
+
if (this.byokKeyInput) {
|
|
1982
|
+
this.byokKeyInput.value = "";
|
|
1983
|
+
this.byokKeyInput.placeholder = selected.configured
|
|
1984
|
+
? "Leave blank to keep configured key, or paste to override"
|
|
1985
|
+
: `Paste key or export ${selected.env_var_hint}`;
|
|
1986
|
+
}
|
|
1987
|
+
}
|
|
1988
|
+
async handleSaveByokSettings() {
|
|
1989
|
+
const provider = this.byokProviderSelect?.value || this.selectedVaultProviderId;
|
|
1990
|
+
const model = (this.byokModelInput?.value || "").trim();
|
|
1991
|
+
const rawKey = (this.byokKeyInput?.value || "").trim();
|
|
1992
|
+
const baseUrl = (this.byokBaseUrlInput?.value || "").trim();
|
|
1993
|
+
try {
|
|
1994
|
+
const payload = {
|
|
1995
|
+
provider,
|
|
1996
|
+
model: model || undefined,
|
|
1997
|
+
make_active: true
|
|
1998
|
+
};
|
|
1999
|
+
if (rawKey.length > 0) {
|
|
2000
|
+
payload.api_key = rawKey;
|
|
2001
|
+
}
|
|
2002
|
+
if (provider === "custom") {
|
|
2003
|
+
payload.base_url = baseUrl;
|
|
2004
|
+
}
|
|
2005
|
+
const updated = await this.apiClient.saveAgentKeys(payload);
|
|
2006
|
+
if (this.byokKeyInput) {
|
|
2007
|
+
this.byokKeyInput.value = "";
|
|
2008
|
+
}
|
|
2009
|
+
this.activeProviderId = updated.active_provider || provider;
|
|
2010
|
+
this.selectedVaultProviderId = this.activeProviderId;
|
|
2011
|
+
if (Array.isArray(updated.providers)) {
|
|
2012
|
+
this.agentProviders = updated.providers;
|
|
2013
|
+
}
|
|
2014
|
+
this.isByokDrawerOpen = false;
|
|
2015
|
+
this.syncProviderControls();
|
|
2016
|
+
this.syncUiStates();
|
|
2017
|
+
this.showStatus("Saved provider settings in local encrypted vault.", "success");
|
|
2018
|
+
}
|
|
2019
|
+
catch (err) {
|
|
2020
|
+
const msg = err instanceof Error ? err.message : "Failed to save BYOK settings.";
|
|
2021
|
+
this.showStatus(msg, "error");
|
|
2022
|
+
}
|
|
2023
|
+
}
|
|
2024
|
+
async handleClearByokKey() {
|
|
2025
|
+
const provider = this.byokProviderSelect?.value || this.selectedVaultProviderId;
|
|
2026
|
+
try {
|
|
2027
|
+
const updated = await this.apiClient.deleteAgentKeys(provider);
|
|
2028
|
+
if (this.byokKeyInput) {
|
|
2029
|
+
this.byokKeyInput.value = "";
|
|
2030
|
+
}
|
|
2031
|
+
if (Array.isArray(updated.providers)) {
|
|
2032
|
+
this.agentProviders = updated.providers;
|
|
2033
|
+
}
|
|
2034
|
+
this.activeProviderId = updated.active_provider || this.activeProviderId;
|
|
2035
|
+
this.syncProviderControls();
|
|
2036
|
+
this.showStatus(`Cleared stored vault key for ${provider}.`, "success");
|
|
2037
|
+
}
|
|
2038
|
+
catch (err) {
|
|
2039
|
+
const msg = err instanceof Error ? err.message : "Failed to clear key.";
|
|
2040
|
+
this.showStatus(msg, "error");
|
|
2041
|
+
}
|
|
2042
|
+
}
|
|
2043
|
+
async loadCrossEnvFeedbacks(environment) {
|
|
2044
|
+
this.feedbacksEnvFilter = environment;
|
|
2045
|
+
this.isLoadingFeedbacks = true;
|
|
2046
|
+
this.renderFeedbacksDrawerContent();
|
|
2047
|
+
try {
|
|
2048
|
+
const res = await this.apiClient.fetchAgentFeedbacks(environment, "new");
|
|
2049
|
+
this.crossEnvFeedbacks = Array.isArray(res.feedbacks) ? res.feedbacks : [];
|
|
2050
|
+
if (Array.isArray(res.environments_queried) && res.environments_queried.length > 0) {
|
|
2051
|
+
for (const envName of res.environments_queried) {
|
|
2052
|
+
if (!this.agentMcpEnvironments.includes(envName)) {
|
|
2053
|
+
this.agentMcpEnvironments.push(envName);
|
|
2054
|
+
}
|
|
2055
|
+
}
|
|
2056
|
+
}
|
|
2057
|
+
}
|
|
2058
|
+
catch (err) {
|
|
2059
|
+
const msg = err instanceof Error ? err.message : "Failed to query feedbacks.";
|
|
2060
|
+
this.showStatus(msg, "error");
|
|
2061
|
+
}
|
|
2062
|
+
finally {
|
|
2063
|
+
this.isLoadingFeedbacks = false;
|
|
2064
|
+
this.renderFeedbacksDrawerContent();
|
|
2065
|
+
}
|
|
2066
|
+
}
|
|
2067
|
+
renderFeedbacksDrawerContent() {
|
|
2068
|
+
if (this.feedbacksTabsEl) {
|
|
2069
|
+
this.feedbacksTabsEl.innerHTML = "";
|
|
2070
|
+
const envOptions = ["all", ...this.agentMcpEnvironments];
|
|
2071
|
+
for (const envName of envOptions) {
|
|
2072
|
+
const tab = document.createElement("button");
|
|
2073
|
+
tab.type = "button";
|
|
2074
|
+
tab.className = `cd-env-tab${this.feedbacksEnvFilter === envName ? " is-active" : ""}`;
|
|
2075
|
+
tab.textContent = envName;
|
|
2076
|
+
tab.addEventListener("click", () => {
|
|
2077
|
+
void this.loadCrossEnvFeedbacks(envName);
|
|
2078
|
+
});
|
|
2079
|
+
this.feedbacksTabsEl.appendChild(tab);
|
|
2080
|
+
}
|
|
2081
|
+
}
|
|
2082
|
+
if (!this.feedbacksListEl)
|
|
2083
|
+
return;
|
|
2084
|
+
this.feedbacksListEl.innerHTML = "";
|
|
2085
|
+
if (this.isLoadingFeedbacks) {
|
|
2086
|
+
const loading = document.createElement("div");
|
|
2087
|
+
loading.className = "cd-chat-empty-hint";
|
|
2088
|
+
loading.textContent = "Querying connected MCP environments...";
|
|
2089
|
+
this.feedbacksListEl.appendChild(loading);
|
|
2090
|
+
return;
|
|
2091
|
+
}
|
|
2092
|
+
if (this.crossEnvFeedbacks.length === 0) {
|
|
2093
|
+
const empty = document.createElement("div");
|
|
2094
|
+
empty.className = "cd-chat-empty-hint";
|
|
2095
|
+
empty.textContent = `No open feedbacks found in ${this.feedbacksEnvFilter}.`;
|
|
2096
|
+
this.feedbacksListEl.appendChild(empty);
|
|
2097
|
+
return;
|
|
2098
|
+
}
|
|
2099
|
+
for (const item of this.crossEnvFeedbacks) {
|
|
2100
|
+
const row = document.createElement("div");
|
|
2101
|
+
row.className = "cd-feedback-row";
|
|
2102
|
+
const top = document.createElement("div");
|
|
2103
|
+
top.className = "cd-feedback-row-top";
|
|
2104
|
+
const idBadge = document.createElement("span");
|
|
2105
|
+
idBadge.className = "cd-feedback-row-id";
|
|
2106
|
+
const qid = item.qualified_id || `${item.environment}:${item.id}`;
|
|
2107
|
+
idBadge.textContent = item.primary_selector ? `${qid} · ${item.primary_selector}` : qid;
|
|
2108
|
+
const investigateBtn = document.createElement("button");
|
|
2109
|
+
investigateBtn.type = "button";
|
|
2110
|
+
investigateBtn.className = "cd-btn";
|
|
2111
|
+
investigateBtn.textContent = "Investigate";
|
|
2112
|
+
investigateBtn.addEventListener("click", () => {
|
|
2113
|
+
const prompt = `Investigate feedback ${qid} ("${item.message}")${item.primary_selector ? ` targeting \`${item.primary_selector}\`` : ""} and propose a fix if needed.`;
|
|
2114
|
+
if (this.agentTextareaEl) {
|
|
2115
|
+
this.agentTextareaEl.value = prompt;
|
|
2116
|
+
this.agentTextareaEl.focus();
|
|
2117
|
+
}
|
|
2118
|
+
this.isFeedbacksDrawerOpen = false;
|
|
2119
|
+
this.syncUiStates();
|
|
2120
|
+
});
|
|
2121
|
+
top.appendChild(idBadge);
|
|
2122
|
+
top.appendChild(investigateBtn);
|
|
2123
|
+
const msgP = document.createElement("p");
|
|
2124
|
+
msgP.className = "cd-feedback-row-msg";
|
|
2125
|
+
msgP.textContent = item.message || "(Context drop without text)";
|
|
2126
|
+
row.appendChild(top);
|
|
2127
|
+
row.appendChild(msgP);
|
|
2128
|
+
this.feedbacksListEl.appendChild(row);
|
|
2129
|
+
}
|
|
2130
|
+
}
|
|
2131
|
+
renderAgentStream() {
|
|
2132
|
+
if (!this.agentStreamEl)
|
|
2133
|
+
return;
|
|
2134
|
+
this.agentStreamEl.innerHTML = "";
|
|
2135
|
+
if (this.agentMessages.length === 0 && !this.isAgentBusy) {
|
|
2136
|
+
const empty = document.createElement("div");
|
|
2137
|
+
empty.className = "cd-agent-empty";
|
|
2138
|
+
const title = document.createElement("p");
|
|
2139
|
+
title.className = "cd-agent-empty-title";
|
|
2140
|
+
title.textContent = `Inspect & edit ${this.agentWorkspaceName} from the live DOM.`;
|
|
2141
|
+
const desc = document.createElement("p");
|
|
2142
|
+
desc.className = "cd-agent-empty-desc";
|
|
2143
|
+
desc.textContent = `Connected MCP environments: ${this.agentMcpEnvironments.join(", ")}. File edits always stage an inline diff for your approval.`;
|
|
2144
|
+
const starters = document.createElement("div");
|
|
2145
|
+
starters.className = "cd-starter-list";
|
|
2146
|
+
const starterItems = [
|
|
2147
|
+
{
|
|
2148
|
+
label: "Pin an element on this page & find its Rails template",
|
|
2149
|
+
action: () => {
|
|
2150
|
+
if (this.elementReferences.length === 0) {
|
|
2151
|
+
this.startPinSelection();
|
|
2152
|
+
}
|
|
2153
|
+
else if (this.agentTextareaEl) {
|
|
2154
|
+
this.agentTextareaEl.value =
|
|
2155
|
+
"Locate the Rails view/partial rendering the pinned element and explain how it is styled.";
|
|
2156
|
+
void this.handleAgentSubmit();
|
|
2157
|
+
}
|
|
2158
|
+
}
|
|
2159
|
+
},
|
|
2160
|
+
{
|
|
2161
|
+
label: "Triage open feedbacks across all environments",
|
|
2162
|
+
action: () => {
|
|
2163
|
+
if (this.agentTextareaEl) {
|
|
2164
|
+
this.agentTextareaEl.value =
|
|
2165
|
+
"List open ContextDrop feedbacks across all environments and summarize the highest-priority UI issues.";
|
|
2166
|
+
void this.handleAgentSubmit();
|
|
2167
|
+
}
|
|
2168
|
+
}
|
|
2169
|
+
},
|
|
2170
|
+
{
|
|
2171
|
+
label: "Inspect pinned element spacing & propose a clean CSS/ERB fix",
|
|
2172
|
+
action: () => {
|
|
2173
|
+
if (this.agentTextareaEl) {
|
|
2174
|
+
this.agentTextareaEl.value =
|
|
2175
|
+
"Inspect the pinned element in the workspace and stage a proposed file edit to polish its layout and accessibility.";
|
|
2176
|
+
this.agentTextareaEl.focus();
|
|
2177
|
+
}
|
|
2178
|
+
}
|
|
2179
|
+
}
|
|
2180
|
+
];
|
|
2181
|
+
for (const item of starterItems) {
|
|
2182
|
+
const btn = document.createElement("button");
|
|
2183
|
+
btn.type = "button";
|
|
2184
|
+
btn.className = "cd-starter-btn";
|
|
2185
|
+
const span = document.createElement("span");
|
|
2186
|
+
span.textContent = item.label;
|
|
2187
|
+
const arrow = document.createElement("span");
|
|
2188
|
+
arrow.innerHTML = ICON_CHEVRON_RIGHT;
|
|
2189
|
+
btn.appendChild(span);
|
|
2190
|
+
btn.appendChild(arrow);
|
|
2191
|
+
btn.addEventListener("click", item.action);
|
|
2192
|
+
starters.appendChild(btn);
|
|
2193
|
+
}
|
|
2194
|
+
empty.appendChild(title);
|
|
2195
|
+
empty.appendChild(desc);
|
|
2196
|
+
empty.appendChild(starters);
|
|
2197
|
+
this.agentStreamEl.appendChild(empty);
|
|
2198
|
+
return;
|
|
2199
|
+
}
|
|
2200
|
+
for (const msg of this.agentMessages) {
|
|
2201
|
+
const card = document.createElement("div");
|
|
2202
|
+
card.className = `cd-agent-msg is-${msg.role}`;
|
|
2203
|
+
if (msg.role === "user") {
|
|
2204
|
+
if ((msg.elements && msg.elements.length > 0) || (msg.attachments && msg.attachments.length > 0)) {
|
|
2205
|
+
const pills = document.createElement("div");
|
|
2206
|
+
pills.className = "cd-chat-msg-pills";
|
|
2207
|
+
for (const el of msg.elements || []) {
|
|
2208
|
+
const pill = document.createElement("span");
|
|
2209
|
+
pill.className = "cd-chat-msg-pill";
|
|
2210
|
+
pill.textContent = el.selector;
|
|
2211
|
+
pills.appendChild(pill);
|
|
2212
|
+
}
|
|
2213
|
+
for (const att of msg.attachments || []) {
|
|
2214
|
+
const pill = document.createElement("span");
|
|
2215
|
+
pill.className = "cd-chat-msg-pill";
|
|
2216
|
+
pill.textContent = att.label;
|
|
2217
|
+
pills.appendChild(pill);
|
|
2218
|
+
}
|
|
2219
|
+
card.appendChild(pills);
|
|
2220
|
+
}
|
|
2221
|
+
const textP = document.createElement("p");
|
|
2222
|
+
textP.className = "cd-chat-msg-text";
|
|
2223
|
+
textP.textContent = msg.content;
|
|
2224
|
+
card.appendChild(textP);
|
|
2225
|
+
}
|
|
2226
|
+
else {
|
|
2227
|
+
if (msg.toolCalls && msg.toolCalls.length > 0) {
|
|
2228
|
+
const traces = document.createElement("div");
|
|
2229
|
+
traces.className = "cd-tool-traces";
|
|
2230
|
+
for (const trace of msg.toolCalls) {
|
|
2231
|
+
const row = document.createElement("div");
|
|
2232
|
+
row.className = `cd-tool-trace${trace.status === "error" ? " is-error" : ""}`;
|
|
2233
|
+
const badge = document.createElement("span");
|
|
2234
|
+
badge.className = "cd-tool-trace-badge";
|
|
2235
|
+
badge.textContent = trace.name;
|
|
2236
|
+
const summary = document.createElement("span");
|
|
2237
|
+
summary.className = "cd-tool-trace-summary";
|
|
2238
|
+
summary.textContent = trace.summary;
|
|
2239
|
+
row.appendChild(badge);
|
|
2240
|
+
row.appendChild(summary);
|
|
2241
|
+
traces.appendChild(row);
|
|
2242
|
+
}
|
|
2243
|
+
card.appendChild(traces);
|
|
2244
|
+
}
|
|
2245
|
+
if (msg.content) {
|
|
2246
|
+
const mdBody = document.createElement("div");
|
|
2247
|
+
mdBody.className = "cd-md-body";
|
|
2248
|
+
mdBody.innerHTML = renderSafeMarkdown(msg.content);
|
|
2249
|
+
card.appendChild(mdBody);
|
|
2250
|
+
}
|
|
2251
|
+
if (msg.proposedEdits && msg.proposedEdits.length > 0) {
|
|
2252
|
+
for (const edit of msg.proposedEdits) {
|
|
2253
|
+
card.appendChild(this.buildDiffCardDom(edit));
|
|
2254
|
+
}
|
|
2255
|
+
}
|
|
2256
|
+
}
|
|
2257
|
+
this.agentStreamEl.appendChild(card);
|
|
2258
|
+
}
|
|
2259
|
+
if (this.isAgentBusy) {
|
|
2260
|
+
const thinking = document.createElement("div");
|
|
2261
|
+
thinking.className = "cd-agent-thinking";
|
|
2262
|
+
const lbl = document.createElement("span");
|
|
2263
|
+
lbl.className = "cd-thinking-label";
|
|
2264
|
+
lbl.textContent = "Inspecting workspace & DOM context...";
|
|
2265
|
+
const b1 = document.createElement("div");
|
|
2266
|
+
b1.className = "cd-skeleton-bar";
|
|
2267
|
+
const b2 = document.createElement("div");
|
|
2268
|
+
b2.className = "cd-skeleton-bar is-short";
|
|
2269
|
+
thinking.appendChild(lbl);
|
|
2270
|
+
thinking.appendChild(b1);
|
|
2271
|
+
thinking.appendChild(b2);
|
|
2272
|
+
this.agentStreamEl.appendChild(thinking);
|
|
2273
|
+
}
|
|
2274
|
+
this.agentStreamEl.scrollTop = this.agentStreamEl.scrollHeight;
|
|
2275
|
+
}
|
|
2276
|
+
buildDiffCardDom(edit) {
|
|
2277
|
+
const card = document.createElement("div");
|
|
2278
|
+
card.className = "cd-diff-card";
|
|
2279
|
+
card.setAttribute("data-edit-id", edit.id);
|
|
2280
|
+
const header = document.createElement("div");
|
|
2281
|
+
header.className = "cd-diff-card-header";
|
|
2282
|
+
const pathGroup = document.createElement("div");
|
|
2283
|
+
pathGroup.className = "cd-diff-path-group";
|
|
2284
|
+
const iconSpan = document.createElement("span");
|
|
2285
|
+
iconSpan.innerHTML = ICON_FILE_CODE;
|
|
2286
|
+
const pathSpan = document.createElement("span");
|
|
2287
|
+
pathSpan.className = "cd-diff-path";
|
|
2288
|
+
pathSpan.textContent = edit.path;
|
|
2289
|
+
pathSpan.title = edit.path;
|
|
2290
|
+
pathGroup.appendChild(iconSpan);
|
|
2291
|
+
pathGroup.appendChild(pathSpan);
|
|
2292
|
+
const stats = document.createElement("div");
|
|
2293
|
+
stats.className = "cd-diff-stats";
|
|
2294
|
+
const addSpan = document.createElement("span");
|
|
2295
|
+
addSpan.className = "cd-diff-stat-add";
|
|
2296
|
+
addSpan.textContent = `+${edit.added_lines}`;
|
|
2297
|
+
const delSpan = document.createElement("span");
|
|
2298
|
+
delSpan.className = "cd-diff-stat-del";
|
|
2299
|
+
delSpan.textContent = `-${edit.removed_lines}`;
|
|
2300
|
+
stats.appendChild(addSpan);
|
|
2301
|
+
stats.appendChild(delSpan);
|
|
2302
|
+
header.appendChild(pathGroup);
|
|
2303
|
+
header.appendChild(stats);
|
|
2304
|
+
card.appendChild(header);
|
|
2305
|
+
if (edit.summary) {
|
|
2306
|
+
const summary = document.createElement("div");
|
|
2307
|
+
summary.className = "cd-diff-summary";
|
|
2308
|
+
summary.textContent = edit.summary;
|
|
2309
|
+
card.appendChild(summary);
|
|
2310
|
+
}
|
|
2311
|
+
card.appendChild(buildUnifiedDiffElement(document, edit.diff));
|
|
2312
|
+
const footer = document.createElement("div");
|
|
2313
|
+
footer.className = "cd-diff-footer";
|
|
2314
|
+
const statusPill = document.createElement("span");
|
|
2315
|
+
statusPill.className = `cd-diff-status-pill${edit.status === "applied" ? " is-applied" : edit.status === "rejected" ? " is-rejected" : ""}`;
|
|
2316
|
+
if (edit.status === "applied") {
|
|
2317
|
+
statusPill.innerHTML = `${ICON_CHECK}<span>Applied to workspace</span>`;
|
|
2318
|
+
}
|
|
2319
|
+
else if (edit.status === "rejected") {
|
|
2320
|
+
statusPill.textContent = "Rejected";
|
|
2321
|
+
}
|
|
2322
|
+
else if (edit.status === "reverted") {
|
|
2323
|
+
statusPill.textContent = "Reverted";
|
|
2324
|
+
}
|
|
2325
|
+
else {
|
|
2326
|
+
statusPill.textContent = "Staged · Awaiting approval";
|
|
2327
|
+
}
|
|
2328
|
+
footer.appendChild(statusPill);
|
|
2329
|
+
const btnGroup = document.createElement("div");
|
|
2330
|
+
btnGroup.className = "cd-diff-btn-group";
|
|
2331
|
+
if (edit.status === "pending") {
|
|
2332
|
+
const rejectBtn = document.createElement("button");
|
|
2333
|
+
rejectBtn.type = "button";
|
|
2334
|
+
rejectBtn.className = "cd-btn";
|
|
2335
|
+
rejectBtn.textContent = "Reject";
|
|
2336
|
+
rejectBtn.addEventListener("click", () => {
|
|
2337
|
+
void this.handleEditDecision(edit, "reject");
|
|
2338
|
+
});
|
|
2339
|
+
const applyBtn = document.createElement("button");
|
|
2340
|
+
applyBtn.type = "button";
|
|
2341
|
+
applyBtn.className = "cd-btn cd-btn-apply";
|
|
2342
|
+
applyBtn.innerHTML = `${ICON_CHECK}<span>Apply Change</span>`;
|
|
2343
|
+
applyBtn.addEventListener("click", () => {
|
|
2344
|
+
void this.handleEditDecision(edit, "apply");
|
|
2345
|
+
});
|
|
2346
|
+
btnGroup.appendChild(rejectBtn);
|
|
2347
|
+
btnGroup.appendChild(applyBtn);
|
|
2348
|
+
}
|
|
2349
|
+
else if (edit.status === "applied") {
|
|
2350
|
+
const revertBtn = document.createElement("button");
|
|
2351
|
+
revertBtn.type = "button";
|
|
2352
|
+
revertBtn.className = "cd-btn";
|
|
2353
|
+
revertBtn.innerHTML = `${ICON_UNDO}<span>Revert</span>`;
|
|
2354
|
+
revertBtn.addEventListener("click", () => {
|
|
2355
|
+
void this.handleEditDecision(edit, "revert");
|
|
2356
|
+
});
|
|
2357
|
+
btnGroup.appendChild(revertBtn);
|
|
2358
|
+
}
|
|
2359
|
+
footer.appendChild(btnGroup);
|
|
2360
|
+
card.appendChild(footer);
|
|
2361
|
+
return card;
|
|
2362
|
+
}
|
|
2363
|
+
async handleEditDecision(edit, action) {
|
|
2364
|
+
try {
|
|
2365
|
+
const res = action === "apply"
|
|
2366
|
+
? await this.apiClient.applyEdit(edit.id)
|
|
2367
|
+
: action === "reject"
|
|
2368
|
+
? await this.apiClient.rejectEdit(edit.id)
|
|
2369
|
+
: await this.apiClient.revertEdit(edit.id);
|
|
2370
|
+
if (res?.edit) {
|
|
2371
|
+
edit.status = res.edit.status;
|
|
2372
|
+
}
|
|
2373
|
+
this.renderAgentStream();
|
|
2374
|
+
if (action === "apply") {
|
|
2375
|
+
this.showStatus(`Applied change to ${edit.path}. Reload page if needed.`, "success");
|
|
2376
|
+
}
|
|
2377
|
+
else if (action === "revert") {
|
|
2378
|
+
this.showStatus(`Reverted ${edit.path} to original contents.`, "success");
|
|
2379
|
+
}
|
|
2380
|
+
}
|
|
2381
|
+
catch (err) {
|
|
2382
|
+
const msg = err instanceof Error ? err.message : `Failed to ${action} edit.`;
|
|
2383
|
+
this.showStatus(msg, "error");
|
|
2384
|
+
}
|
|
2385
|
+
}
|
|
2386
|
+
async handleAgentSubmit() {
|
|
2387
|
+
if (this.isAgentBusy)
|
|
2388
|
+
return;
|
|
2389
|
+
const rawPrompt = (this.agentTextareaEl?.value || "").trim();
|
|
2390
|
+
if (rawPrompt.length === 0 &&
|
|
2391
|
+
this.elementReferences.length === 0 &&
|
|
2392
|
+
this.attachments.length === 0) {
|
|
2393
|
+
this.showStatus("Enter a prompt or pin an element on the page first.", "error");
|
|
2394
|
+
return;
|
|
2395
|
+
}
|
|
2396
|
+
const promptText = rawPrompt || "Inspect the pinned DOM element(s) in this Rails workspace and explain how they work.";
|
|
2397
|
+
const attachedElements = [...this.elementReferences];
|
|
2398
|
+
const attachedMedia = this.attachments.map((att) => ({
|
|
2399
|
+
id: att.id,
|
|
2400
|
+
label: att.label,
|
|
2401
|
+
preview_url: att.preview_url,
|
|
2402
|
+
attachment_type: att.attachment_type
|
|
2403
|
+
}));
|
|
2404
|
+
const requestAttachments = this.attachments.map((att) => ({
|
|
2405
|
+
attachment_type: att.attachment_type,
|
|
2406
|
+
filename: att.filename,
|
|
2407
|
+
data_url: att.data_url
|
|
2408
|
+
}));
|
|
2409
|
+
const now = new Date();
|
|
2410
|
+
const timeLabel = `${String(now.getHours()).padStart(2, "0")}:${String(now.getMinutes()).padStart(2, "0")}`;
|
|
2411
|
+
const userMsg = {
|
|
2412
|
+
id: `msg_${Date.now()}_u`,
|
|
2413
|
+
role: "user",
|
|
2414
|
+
content: promptText,
|
|
2415
|
+
createdAt: timeLabel,
|
|
2416
|
+
elements: attachedElements,
|
|
2417
|
+
attachments: attachedMedia
|
|
2418
|
+
};
|
|
2419
|
+
this.agentMessages.push(userMsg);
|
|
2420
|
+
if (this.agentTextareaEl) {
|
|
2421
|
+
this.agentTextareaEl.value = "";
|
|
2422
|
+
}
|
|
2423
|
+
this.elementReferences = [];
|
|
2424
|
+
this.elementDomNodes.clear();
|
|
2425
|
+
this.attachments = [];
|
|
2426
|
+
this.picker?.hideExternalPreview();
|
|
2427
|
+
this.renderTray();
|
|
2428
|
+
this.isAgentBusy = true;
|
|
2429
|
+
if (this.agentSendBtn) {
|
|
2430
|
+
this.agentSendBtn.disabled = true;
|
|
2431
|
+
this.agentSendBtn.innerHTML = `<span>Working...</span>`;
|
|
2432
|
+
}
|
|
2433
|
+
this.renderAgentStream();
|
|
2434
|
+
this.syncUiStates();
|
|
2435
|
+
try {
|
|
2436
|
+
const activeProv = this.getActiveProviderInfo();
|
|
2437
|
+
const res = await this.apiClient.sendAgentChat({
|
|
2438
|
+
messages: this.agentMessages.map((m) => ({ role: m.role, content: m.content })),
|
|
2439
|
+
page_url: typeof window !== "undefined" ? window.location.href : "/",
|
|
2440
|
+
element_references: attachedElements,
|
|
2441
|
+
attachments: requestAttachments,
|
|
2442
|
+
provider: activeProv.id,
|
|
2443
|
+
model: activeProv.model
|
|
2444
|
+
});
|
|
2445
|
+
this.agentMessages.push({
|
|
2446
|
+
id: `msg_${Date.now()}_a`,
|
|
2447
|
+
role: "assistant",
|
|
2448
|
+
content: res.reply || "",
|
|
2449
|
+
createdAt: timeLabel,
|
|
2450
|
+
toolCalls: res.tool_calls || [],
|
|
2451
|
+
proposedEdits: res.proposed_edits || [],
|
|
2452
|
+
provider: res.provider,
|
|
2453
|
+
model: res.model
|
|
2454
|
+
});
|
|
2455
|
+
this.hideStatus();
|
|
2456
|
+
}
|
|
2457
|
+
catch (err) {
|
|
2458
|
+
const msg = err instanceof Error ? err.message : "Agent request failed.";
|
|
2459
|
+
if (/no api key|byok|api_key/i.test(msg)) {
|
|
2460
|
+
this.isByokDrawerOpen = true;
|
|
2461
|
+
}
|
|
2462
|
+
this.showStatus(msg, "error");
|
|
2463
|
+
}
|
|
2464
|
+
finally {
|
|
2465
|
+
this.isAgentBusy = false;
|
|
2466
|
+
if (this.agentSendBtn) {
|
|
2467
|
+
this.agentSendBtn.disabled = false;
|
|
2468
|
+
this.agentSendBtn.innerHTML = `${ICON_SEND}<span>Ask Agent</span>`;
|
|
2469
|
+
}
|
|
2470
|
+
this.renderAgentStream();
|
|
2471
|
+
this.syncUiStates();
|
|
2472
|
+
}
|
|
2473
|
+
}
|
|
2474
|
+
syncPinActiveIndicators(pickerActive) {
|
|
2475
|
+
for (const btn of [this.cornerPinBtn, this.sidebarPinBtn]) {
|
|
2476
|
+
if (btn) {
|
|
2477
|
+
btn.classList.toggle("is-active", pickerActive);
|
|
2478
|
+
btn.setAttribute("aria-pressed", String(pickerActive));
|
|
2479
|
+
}
|
|
2480
|
+
}
|
|
2481
|
+
if (this.compactPinBtn) {
|
|
2482
|
+
const smartMode = this.isSmartSelectionMode();
|
|
2483
|
+
this.compactPinBtn.classList.toggle("is-active", pickerActive);
|
|
2484
|
+
if (smartMode) {
|
|
2485
|
+
this.compactPinBtn.title = "Highlight an area on the page";
|
|
2486
|
+
this.compactPinBtn.setAttribute("aria-label", "Toggle page highlight mode");
|
|
2487
|
+
this.compactPinBtn.innerHTML = pickerActive
|
|
2488
|
+
? `${ICON_PIN}<span>Selecting</span>`
|
|
2489
|
+
: `${ICON_PIN}<span>Select area</span>`;
|
|
2490
|
+
}
|
|
2491
|
+
else {
|
|
2492
|
+
this.compactPinBtn.title = "Toggle Freeze & Pin (⌥⇧P)";
|
|
2493
|
+
this.compactPinBtn.setAttribute("aria-label", "Toggle element pin mode (Option+Shift+P)");
|
|
2494
|
+
this.compactPinBtn.innerHTML = pickerActive
|
|
2495
|
+
? `${ICON_PIN}<span>Pinning</span>`
|
|
2496
|
+
: `${ICON_PIN}<span>Pin Element</span>`;
|
|
2497
|
+
}
|
|
2498
|
+
}
|
|
2499
|
+
}
|
|
2500
|
+
getVisibleContextCount() {
|
|
2501
|
+
if (!this.isSmartSelectionMode()) {
|
|
2502
|
+
return this.elementReferences.length + this.attachments.length;
|
|
2503
|
+
}
|
|
2504
|
+
const unlinkedAttachments = this.attachments.filter((att) => !(att.metadata?.auto_smart_context === true &&
|
|
2505
|
+
att.element_reference_ref &&
|
|
2506
|
+
this.elementReferences.some((el) => el.client_ref === att.element_reference_ref)));
|
|
2507
|
+
return this.elementReferences.length + unlinkedAttachments.length;
|
|
2508
|
+
}
|
|
2509
|
+
syncUiStates() {
|
|
2510
|
+
const pickerActive = this.picker ? this.picker.isActive() : false;
|
|
2511
|
+
const totalContextCount = this.getVisibleContextCount();
|
|
2512
|
+
const agentMode = this.isAgentModeActive();
|
|
2513
|
+
const smartMode = this.isSmartSelectionMode();
|
|
2514
|
+
const showAgentSidebar = this.isOpen && !this.isMinimized && agentMode;
|
|
2515
|
+
const showFeedbackComposer = this.isOpen && !this.isMinimized && !agentMode;
|
|
2516
|
+
if (this.composerEl) {
|
|
2517
|
+
this.composerEl.hidden = !showFeedbackComposer;
|
|
2518
|
+
this.composerEl.classList.toggle("is-smart-mode", smartMode);
|
|
2519
|
+
}
|
|
2520
|
+
if (this.composerTitleEl) {
|
|
2521
|
+
this.composerTitleEl.textContent = smartMode ? "Send Feedback" : "ContextDrop";
|
|
2522
|
+
}
|
|
2523
|
+
if (this.composerMinBtn) {
|
|
2524
|
+
this.composerMinBtn.hidden = smartMode;
|
|
2525
|
+
}
|
|
2526
|
+
if (this.composerModeSwitchWrap) {
|
|
2527
|
+
this.composerModeSwitchWrap.hidden = !this.config.agent?.enabled;
|
|
2528
|
+
}
|
|
2529
|
+
if (this.textareaEl && !this.customPlaceholderSet) {
|
|
2530
|
+
this.textareaEl.placeholder = smartMode
|
|
2531
|
+
? DEFAULT_SMART_PLACEHOLDER
|
|
2532
|
+
: DEFAULT_VERBOSE_PLACEHOLDER;
|
|
2533
|
+
}
|
|
2534
|
+
if (this.composerToolbarPickBtn) {
|
|
2535
|
+
if (smartMode) {
|
|
2536
|
+
this.composerToolbarPickBtn.title = "Highlight an area on the page";
|
|
2537
|
+
this.composerToolbarPickBtn.setAttribute("aria-label", "Highlight an area on the page");
|
|
2538
|
+
this.composerToolbarPickBtn.innerHTML = `${ICON_PIN}<span>Select area</span>`;
|
|
2539
|
+
}
|
|
2540
|
+
else {
|
|
2541
|
+
this.composerToolbarPickBtn.title = "Freeze & Pin element (⌥⇧P)";
|
|
2542
|
+
this.composerToolbarPickBtn.setAttribute("aria-label", "Pin HTML element as context (Option+Shift+P)");
|
|
2543
|
+
this.composerToolbarPickBtn.innerHTML = `${ICON_PIN}<span>Pin</span><kbd class="cd-kbd">⌥⇧P</kbd>`;
|
|
2544
|
+
}
|
|
2545
|
+
}
|
|
2546
|
+
if (this.composerToolbarScreenshotBtn) {
|
|
2547
|
+
this.composerToolbarScreenshotBtn.hidden = smartMode;
|
|
2548
|
+
}
|
|
2549
|
+
if (this.composerToolbarUploadBtn) {
|
|
2550
|
+
this.composerToolbarUploadBtn.innerHTML = smartMode
|
|
2551
|
+
? `${ICON_IMAGE}<span>Add image</span>`
|
|
2552
|
+
: `${ICON_IMAGE}<span>Image</span>`;
|
|
2553
|
+
}
|
|
2554
|
+
if (this.agentSidebarEl) {
|
|
2555
|
+
this.agentSidebarEl.hidden = !showAgentSidebar;
|
|
2556
|
+
}
|
|
2557
|
+
if (this.sidebarEnvPillEl) {
|
|
2558
|
+
this.sidebarEnvPillEl.textContent = this.getShortEnvironmentSlug();
|
|
2559
|
+
}
|
|
2560
|
+
if (this.sidebarPushBtn) {
|
|
2561
|
+
this.sidebarPushBtn.classList.toggle("is-active", this.pushPageLayout);
|
|
2562
|
+
}
|
|
2563
|
+
if (this.byokDrawerEl) {
|
|
2564
|
+
this.byokDrawerEl.hidden = !this.isByokDrawerOpen;
|
|
2565
|
+
}
|
|
2566
|
+
if (this.keysToggleBtn) {
|
|
2567
|
+
this.keysToggleBtn.classList.toggle("is-active", this.isByokDrawerOpen);
|
|
2568
|
+
}
|
|
2569
|
+
if (this.feedbacksDrawerEl) {
|
|
2570
|
+
this.feedbacksDrawerEl.hidden = !this.isFeedbacksDrawerOpen;
|
|
2571
|
+
}
|
|
2572
|
+
if (this.feedbacksToggleBtn) {
|
|
2573
|
+
this.feedbacksToggleBtn.classList.toggle("is-active", this.isFeedbacksDrawerOpen);
|
|
2574
|
+
}
|
|
2575
|
+
this.syncHostPagePushMargin(showAgentSidebar && this.pushPageLayout);
|
|
2576
|
+
if (this.compactBarEl) {
|
|
2577
|
+
this.compactBarEl.hidden = !(this.isOpen && this.isMinimized);
|
|
2578
|
+
this.compactBarEl.classList.toggle("is-smart-mode", smartMode);
|
|
2579
|
+
}
|
|
2580
|
+
if (this.bookCornerBtn) {
|
|
2581
|
+
this.bookCornerBtn.hidden = this.isOpen;
|
|
2582
|
+
this.bookCornerBtn.setAttribute("aria-expanded", String(this.isOpen && !this.isMinimized));
|
|
2583
|
+
this.bookCornerBtn.setAttribute("aria-label", smartMode
|
|
2584
|
+
? "Send feedback or drag corner to highlight part of the page"
|
|
2585
|
+
: "Open ContextDrop chatbox, or drag corner to pin an element on the page");
|
|
2586
|
+
}
|
|
2587
|
+
if (this.bookCornerHintEl) {
|
|
2588
|
+
this.bookCornerHintEl.textContent = this.config.agent?.enabled
|
|
2589
|
+
? "Open Agent · Drag to pin"
|
|
2590
|
+
: smartMode
|
|
2591
|
+
? "Send feedback · Drag to highlight"
|
|
2592
|
+
: "Click to chat · Drag to pin";
|
|
2593
|
+
}
|
|
2594
|
+
if (this.bookCornerAgentDotEl) {
|
|
2595
|
+
this.bookCornerAgentDotEl.hidden = !this.config.agent?.enabled;
|
|
2596
|
+
}
|
|
2597
|
+
if (this.bookCornerCountEl) {
|
|
2598
|
+
this.bookCornerCountEl.hidden = totalContextCount === 0;
|
|
2599
|
+
this.bookCornerCountEl.textContent = String(totalContextCount);
|
|
2600
|
+
}
|
|
2601
|
+
const pinCountHtml = this.elementReferences.length > 0
|
|
2602
|
+
? `<span class="cd-count-pill">${this.elementReferences.length}</span>`
|
|
2603
|
+
: "";
|
|
2604
|
+
if (this.cornerPinBtn) {
|
|
2605
|
+
if (smartMode) {
|
|
2606
|
+
this.cornerPinBtn.title = "Highlight an area on the page";
|
|
2607
|
+
this.cornerPinBtn.setAttribute("aria-label", "Highlight an area on the page");
|
|
2608
|
+
this.cornerPinBtn.innerHTML = `${ICON_PIN}<span>Select area</span>${pinCountHtml}`;
|
|
2609
|
+
}
|
|
2610
|
+
else {
|
|
2611
|
+
this.cornerPinBtn.title = "Freeze & Pin element (⌥⇧P)";
|
|
2612
|
+
this.cornerPinBtn.setAttribute("aria-label", "Select DOM element on page (Option+Shift+P)");
|
|
2613
|
+
this.cornerPinBtn.innerHTML = `${ICON_PIN}<span>Pin Element</span>${pinCountHtml}`;
|
|
2614
|
+
}
|
|
2615
|
+
}
|
|
2616
|
+
if (this.sidebarPinBtn) {
|
|
2617
|
+
this.sidebarPinBtn.innerHTML = `${ICON_PIN}<span>Pin</span>${pinCountHtml}`;
|
|
2618
|
+
}
|
|
2619
|
+
this.syncPinActiveIndicators(pickerActive);
|
|
2620
|
+
if (!this.isPeelDragging) {
|
|
2621
|
+
this.renderCompactCoachBar(pickerActive, this.picker ? this.picker.getCandidateStack().length : 0);
|
|
2622
|
+
}
|
|
2623
|
+
}
|
|
2624
|
+
renderCompactCoachBar(pickerActive, candidateCount = 0) {
|
|
2625
|
+
if (!this.compactStatusEl)
|
|
2626
|
+
return;
|
|
2627
|
+
this.compactStatusEl.innerHTML = "";
|
|
2628
|
+
const smartMode = this.isSmartSelectionMode();
|
|
2629
|
+
const pinnedCount = this.elementReferences.length;
|
|
2630
|
+
const totalContextCount = this.getVisibleContextCount();
|
|
2631
|
+
if (this.compactStepGroupEl) {
|
|
2632
|
+
this.compactStepGroupEl.hidden = !pickerActive || smartMode;
|
|
2633
|
+
}
|
|
2634
|
+
if (this.compactLayerUpBtn) {
|
|
2635
|
+
this.compactLayerUpBtn.disabled = candidateCount <= 1;
|
|
2636
|
+
}
|
|
2637
|
+
if (this.compactLayerDownBtn) {
|
|
2638
|
+
this.compactLayerDownBtn.disabled = candidateCount <= 1;
|
|
2639
|
+
}
|
|
2640
|
+
if (pickerActive && smartMode) {
|
|
2641
|
+
const coach = document.createElement("span");
|
|
2642
|
+
coach.className = "cd-compact-coach-text";
|
|
2643
|
+
coach.textContent = "Click any part of the page to highlight it";
|
|
2644
|
+
this.compactStatusEl.appendChild(coach);
|
|
2645
|
+
if (this.compactDoneBtn) {
|
|
2646
|
+
this.compactDoneBtn.className = "cd-btn";
|
|
2647
|
+
this.compactDoneBtn.setAttribute("aria-label", "Cancel selection and return to feedback");
|
|
2648
|
+
this.compactDoneBtn.innerHTML = `<span>Cancel</span>`;
|
|
2649
|
+
}
|
|
2650
|
+
return;
|
|
2651
|
+
}
|
|
2652
|
+
if (pickerActive) {
|
|
2653
|
+
if (pinnedCount === 0) {
|
|
2654
|
+
const coach = document.createElement("span");
|
|
2655
|
+
coach.className = "cd-compact-coach-text";
|
|
2656
|
+
coach.innerHTML = `<strong>Space</strong><span>or click to pin</span><span>·</span><span class="cd-compact-wheel-icon">${ICON_SCROLL_VERTICAL}</span><strong>Scroll</strong><span>layers</span><span>·</span><kbd class="cd-kbd">⌥⇧P</kbd><span>freeze</span>`;
|
|
2657
|
+
this.compactStatusEl.appendChild(coach);
|
|
2658
|
+
}
|
|
2659
|
+
else {
|
|
2660
|
+
const latest = this.elementReferences[pinnedCount - 1];
|
|
2661
|
+
const pill = document.createElement("div");
|
|
2662
|
+
pill.className = "cd-compact-pinned-pill";
|
|
2663
|
+
const countSpan = document.createElement("span");
|
|
2664
|
+
countSpan.className = "cd-compact-pinned-count";
|
|
2665
|
+
countSpan.textContent = `${pinnedCount} pinned`;
|
|
2666
|
+
const selSpan = document.createElement("span");
|
|
2667
|
+
selSpan.className = "cd-compact-pinned-selector";
|
|
2668
|
+
selSpan.textContent = latest.selector;
|
|
2669
|
+
selSpan.title = latest.dom_path || latest.selector;
|
|
2670
|
+
const undoBtn = document.createElement("button");
|
|
2671
|
+
undoBtn.type = "button";
|
|
2672
|
+
undoBtn.className = "cd-chip-remove";
|
|
2673
|
+
undoBtn.title = `Remove ${latest.selector}`;
|
|
2674
|
+
undoBtn.setAttribute("aria-label", `Undo pin ${latest.selector}`);
|
|
2675
|
+
undoBtn.innerHTML = ICON_CLOSE;
|
|
2676
|
+
undoBtn.addEventListener("click", (ev) => {
|
|
2677
|
+
ev.stopPropagation();
|
|
2678
|
+
this.removeElementReference(latest.client_ref);
|
|
2679
|
+
});
|
|
2680
|
+
pill.appendChild(countSpan);
|
|
2681
|
+
pill.appendChild(selSpan);
|
|
2682
|
+
pill.appendChild(undoBtn);
|
|
2683
|
+
this.compactStatusEl.appendChild(pill);
|
|
2684
|
+
const addMore = document.createElement("span");
|
|
2685
|
+
addMore.className = "cd-compact-coach-text";
|
|
2686
|
+
addMore.innerHTML = `<strong>Space</strong><span>or click to add more</span>`;
|
|
2687
|
+
this.compactStatusEl.appendChild(addMore);
|
|
2688
|
+
}
|
|
2689
|
+
if (this.compactDoneBtn) {
|
|
2690
|
+
if (pinnedCount > 0) {
|
|
2691
|
+
this.compactDoneBtn.className = "cd-btn cd-btn-primary";
|
|
2692
|
+
this.compactDoneBtn.setAttribute("aria-label", "Finish pinning elements and return to chatbox");
|
|
2693
|
+
this.compactDoneBtn.innerHTML = `${ICON_CHECK}<span>Done (${pinnedCount})</span><kbd class="cd-kbd">Esc</kbd>`;
|
|
2694
|
+
}
|
|
2695
|
+
else {
|
|
2696
|
+
this.compactDoneBtn.className = "cd-btn";
|
|
2697
|
+
this.compactDoneBtn.setAttribute("aria-label", "Cancel pinning and return to chatbox");
|
|
2698
|
+
this.compactDoneBtn.innerHTML = `<span>Cancel</span><kbd class="cd-kbd">Esc</kbd>`;
|
|
2699
|
+
}
|
|
2700
|
+
}
|
|
2701
|
+
return;
|
|
2702
|
+
}
|
|
2703
|
+
const summary = document.createElement("span");
|
|
2704
|
+
summary.className = "cd-compact-coach-text";
|
|
2705
|
+
summary.textContent =
|
|
2706
|
+
totalContextCount > 0
|
|
2707
|
+
? `${totalContextCount} context item${totalContextCount > 1 ? "s" : ""} attached`
|
|
2708
|
+
: this.isAgentModeActive()
|
|
2709
|
+
? "Agent sidebar minimized"
|
|
2710
|
+
: "Chatbox minimized";
|
|
2711
|
+
this.compactStatusEl.appendChild(summary);
|
|
2712
|
+
if (this.compactDoneBtn) {
|
|
2713
|
+
this.compactDoneBtn.className = "cd-btn cd-btn-primary";
|
|
2714
|
+
this.compactDoneBtn.setAttribute("aria-label", "Return to chatbox");
|
|
2715
|
+
this.compactDoneBtn.innerHTML = `${ICON_EXPAND}<span>${this.isAgentModeActive() ? "Agent" : "Chat"}</span>`;
|
|
2716
|
+
}
|
|
2717
|
+
}
|
|
2718
|
+
renderChatThread() {
|
|
2719
|
+
if (!this.chatThreadEl)
|
|
2720
|
+
return;
|
|
2721
|
+
this.chatThreadEl.innerHTML = "";
|
|
2722
|
+
const smartMode = this.isSmartSelectionMode();
|
|
2723
|
+
if (this.sessionHistory.length === 0) {
|
|
2724
|
+
if (smartMode && (this.elementReferences.length > 0 || this.attachments.length > 0)) {
|
|
2725
|
+
this.chatThreadEl.hidden = true;
|
|
2726
|
+
return;
|
|
2727
|
+
}
|
|
2728
|
+
this.chatThreadEl.hidden = false;
|
|
2729
|
+
const emptyBox = document.createElement("div");
|
|
2730
|
+
emptyBox.className = smartMode ? "cd-chat-empty is-smart" : "cd-chat-empty";
|
|
2731
|
+
if (smartMode) {
|
|
2732
|
+
const hint = document.createElement("p");
|
|
2733
|
+
hint.className = "cd-chat-empty-hint";
|
|
2734
|
+
hint.textContent =
|
|
2735
|
+
"Drag the corner or select an area on the page, or write a quick note below.";
|
|
2736
|
+
emptyBox.appendChild(hint);
|
|
2737
|
+
}
|
|
2738
|
+
else {
|
|
2739
|
+
const p1 = document.createElement("p");
|
|
2740
|
+
p1.className = "cd-chat-empty-text";
|
|
2741
|
+
p1.textContent = "Drop context directly from this page.";
|
|
2742
|
+
const p2 = document.createElement("p");
|
|
2743
|
+
p2.className = "cd-chat-empty-hint";
|
|
2744
|
+
p2.innerHTML =
|
|
2745
|
+
'Drag the folded corner onto any element, or press <kbd class="cd-kbd">⌥⇧P</kbd> to freeze & pin, scroll to cycle layers, and press <kbd class="cd-kbd">Space</kbd> to pin.';
|
|
2746
|
+
emptyBox.appendChild(p1);
|
|
2747
|
+
emptyBox.appendChild(p2);
|
|
2748
|
+
}
|
|
2749
|
+
this.chatThreadEl.appendChild(emptyBox);
|
|
2750
|
+
return;
|
|
2751
|
+
}
|
|
2752
|
+
this.chatThreadEl.hidden = false;
|
|
2753
|
+
for (const entry of this.sessionHistory) {
|
|
2754
|
+
const msgCard = document.createElement("div");
|
|
2755
|
+
msgCard.className = "cd-chat-msg";
|
|
2756
|
+
if (entry.elements.length > 0 || entry.attachments.length > 0) {
|
|
2757
|
+
const pills = document.createElement("div");
|
|
2758
|
+
pills.className = "cd-chat-msg-pills";
|
|
2759
|
+
for (const el of entry.elements) {
|
|
2760
|
+
const pill = document.createElement("span");
|
|
2761
|
+
pill.className = smartMode ? "cd-chat-msg-pill is-friendly" : "cd-chat-msg-pill";
|
|
2762
|
+
pill.textContent = smartMode ? this.getElementFriendlyLabel(el) : el.selector;
|
|
2763
|
+
pills.appendChild(pill);
|
|
2764
|
+
}
|
|
2765
|
+
for (const att of entry.attachments) {
|
|
2766
|
+
if (smartMode && att.attachment_type === "screenshot" && entry.elements.length > 0) {
|
|
2767
|
+
continue;
|
|
2768
|
+
}
|
|
2769
|
+
const pill = document.createElement("span");
|
|
2770
|
+
pill.className = smartMode ? "cd-chat-msg-pill is-friendly" : "cd-chat-msg-pill";
|
|
2771
|
+
pill.textContent = att.label;
|
|
2772
|
+
pills.appendChild(pill);
|
|
2773
|
+
}
|
|
2774
|
+
if (pills.childNodes.length > 0) {
|
|
2775
|
+
msgCard.appendChild(pills);
|
|
2776
|
+
}
|
|
2777
|
+
}
|
|
2778
|
+
if (entry.message) {
|
|
2779
|
+
const textP = document.createElement("p");
|
|
2780
|
+
textP.className = "cd-chat-msg-text";
|
|
2781
|
+
textP.textContent = entry.message;
|
|
2782
|
+
msgCard.appendChild(textP);
|
|
2783
|
+
}
|
|
2784
|
+
const meta = document.createElement("div");
|
|
2785
|
+
meta.className = "cd-chat-msg-meta";
|
|
2786
|
+
const idSpan = document.createElement("span");
|
|
2787
|
+
idSpan.textContent = smartMode ? "Feedback sent" : `Dropped #${entry.id}`;
|
|
2788
|
+
const timeSpan = document.createElement("span");
|
|
2789
|
+
timeSpan.textContent = entry.createdAt;
|
|
2790
|
+
meta.appendChild(idSpan);
|
|
2791
|
+
meta.appendChild(timeSpan);
|
|
2792
|
+
msgCard.appendChild(meta);
|
|
2793
|
+
this.chatThreadEl.appendChild(msgCard);
|
|
2794
|
+
}
|
|
2795
|
+
this.chatThreadEl.scrollTop = this.chatThreadEl.scrollHeight;
|
|
2796
|
+
}
|
|
2797
|
+
renderTray() {
|
|
2798
|
+
for (const targetTray of [this.trayEl, this.agentTrayEl]) {
|
|
2799
|
+
if (!targetTray)
|
|
2800
|
+
continue;
|
|
2801
|
+
this.populateTrayElement(targetTray);
|
|
2802
|
+
}
|
|
2803
|
+
if (this.sessionHistory.length === 0) {
|
|
2804
|
+
this.renderChatThread();
|
|
2805
|
+
}
|
|
2806
|
+
}
|
|
2807
|
+
populateTrayElement(trayContainer) {
|
|
2808
|
+
trayContainer.innerHTML = "";
|
|
2809
|
+
const smartTray = trayContainer === this.trayEl && this.isSmartSelectionMode();
|
|
2810
|
+
if (smartTray) {
|
|
2811
|
+
const linkedShotIds = new Set();
|
|
2812
|
+
for (const elRef of this.elementReferences) {
|
|
2813
|
+
const domNode = this.elementDomNodes.get(elRef.client_ref);
|
|
2814
|
+
const friendlyLabel = this.getElementFriendlyLabel(elRef, domNode);
|
|
2815
|
+
const linkedShot = this.attachments.find((att) => att.element_reference_ref === elRef.client_ref);
|
|
2816
|
+
if (linkedShot) {
|
|
2817
|
+
linkedShotIds.add(linkedShot.id);
|
|
2818
|
+
}
|
|
2819
|
+
const card = document.createElement("div");
|
|
2820
|
+
card.className = "cd-smart-card";
|
|
2821
|
+
card.setAttribute("data-chip-type", "smart-context");
|
|
2822
|
+
if (domNode) {
|
|
2823
|
+
card.addEventListener("mouseenter", () => this.picker?.showExternalPreview(domNode));
|
|
2824
|
+
card.addEventListener("mouseleave", () => this.picker?.hideExternalPreview());
|
|
2825
|
+
}
|
|
2826
|
+
if (linkedShot?.preview_url) {
|
|
2827
|
+
const thumb = document.createElement("img");
|
|
2828
|
+
thumb.className = "cd-smart-card-thumb";
|
|
2829
|
+
thumb.src = linkedShot.preview_url;
|
|
2830
|
+
thumb.alt = friendlyLabel;
|
|
2831
|
+
card.appendChild(thumb);
|
|
2832
|
+
}
|
|
2833
|
+
else {
|
|
2834
|
+
const iconBadge = document.createElement("span");
|
|
2835
|
+
iconBadge.className = "cd-smart-card-icon";
|
|
2836
|
+
iconBadge.innerHTML = ICON_PIN;
|
|
2837
|
+
card.appendChild(iconBadge);
|
|
2838
|
+
}
|
|
2839
|
+
const body = document.createElement("div");
|
|
2840
|
+
body.className = "cd-smart-card-body";
|
|
2841
|
+
const title = document.createElement("span");
|
|
2842
|
+
title.className = "cd-smart-card-title";
|
|
2843
|
+
title.textContent = friendlyLabel;
|
|
2844
|
+
const sub = document.createElement("span");
|
|
2845
|
+
sub.className = "cd-smart-card-sub";
|
|
2846
|
+
sub.textContent = linkedShot
|
|
2847
|
+
? "Area & surrounding context captured"
|
|
2848
|
+
: "Highlighted on page";
|
|
2849
|
+
body.appendChild(title);
|
|
2850
|
+
body.appendChild(sub);
|
|
2851
|
+
card.appendChild(body);
|
|
2852
|
+
const removeBtn = document.createElement("button");
|
|
2853
|
+
removeBtn.type = "button";
|
|
2854
|
+
removeBtn.className = "cd-chip-remove";
|
|
2855
|
+
removeBtn.setAttribute("aria-label", `Remove ${friendlyLabel}`);
|
|
2856
|
+
removeBtn.innerHTML = ICON_CLOSE;
|
|
2857
|
+
removeBtn.addEventListener("click", () => this.removeElementReference(elRef.client_ref));
|
|
2858
|
+
card.appendChild(removeBtn);
|
|
2859
|
+
trayContainer.appendChild(card);
|
|
2860
|
+
}
|
|
2861
|
+
for (const att of this.attachments) {
|
|
2862
|
+
if (linkedShotIds.has(att.id))
|
|
2863
|
+
continue;
|
|
2864
|
+
const card = document.createElement("div");
|
|
2865
|
+
card.className = "cd-smart-card";
|
|
2866
|
+
card.setAttribute("data-chip-type", att.attachment_type);
|
|
2867
|
+
if (att.preview_url) {
|
|
2868
|
+
const thumb = document.createElement("img");
|
|
2869
|
+
thumb.className = "cd-smart-card-thumb";
|
|
2870
|
+
thumb.src = att.preview_url;
|
|
2871
|
+
thumb.alt = att.label;
|
|
2872
|
+
card.appendChild(thumb);
|
|
2873
|
+
}
|
|
2874
|
+
const body = document.createElement("div");
|
|
2875
|
+
body.className = "cd-smart-card-body";
|
|
2876
|
+
const title = document.createElement("span");
|
|
2877
|
+
title.className = "cd-smart-card-title";
|
|
2878
|
+
title.textContent = att.label;
|
|
2879
|
+
const sub = document.createElement("span");
|
|
2880
|
+
sub.className = "cd-smart-card-sub";
|
|
2881
|
+
sub.textContent = "Attached image";
|
|
2882
|
+
body.appendChild(title);
|
|
2883
|
+
body.appendChild(sub);
|
|
2884
|
+
card.appendChild(body);
|
|
2885
|
+
const removeBtn = document.createElement("button");
|
|
2886
|
+
removeBtn.type = "button";
|
|
2887
|
+
removeBtn.className = "cd-chip-remove";
|
|
2888
|
+
removeBtn.setAttribute("aria-label", `Remove ${att.label}`);
|
|
2889
|
+
removeBtn.innerHTML = ICON_CLOSE;
|
|
2890
|
+
removeBtn.addEventListener("click", () => this.removeAttachment(att.id));
|
|
2891
|
+
card.appendChild(removeBtn);
|
|
2892
|
+
trayContainer.appendChild(card);
|
|
2893
|
+
}
|
|
2894
|
+
return;
|
|
2895
|
+
}
|
|
2896
|
+
for (const elRef of this.elementReferences) {
|
|
2897
|
+
const chip = document.createElement("div");
|
|
2898
|
+
chip.className = "cd-chip";
|
|
2899
|
+
chip.setAttribute("data-chip-type", "element");
|
|
2900
|
+
const domNode = this.elementDomNodes.get(elRef.client_ref);
|
|
2901
|
+
if (domNode) {
|
|
2902
|
+
chip.addEventListener("mouseenter", () => this.picker?.showExternalPreview(domNode));
|
|
2903
|
+
chip.addEventListener("mouseleave", () => this.picker?.hideExternalPreview());
|
|
2904
|
+
}
|
|
2905
|
+
const badge = document.createElement("span");
|
|
2906
|
+
badge.className = "cd-chip-badge";
|
|
2907
|
+
badge.innerHTML = `${ICON_PIN}<span>Pin</span>`;
|
|
2908
|
+
const label = document.createElement("span");
|
|
2909
|
+
label.className = "cd-chip-label";
|
|
2910
|
+
label.textContent = elRef.selector;
|
|
2911
|
+
label.title = elRef.dom_path || elRef.selector;
|
|
2912
|
+
chip.appendChild(badge);
|
|
2913
|
+
chip.appendChild(label);
|
|
2914
|
+
if (this.config.features.screenshot_capture && domNode) {
|
|
2915
|
+
const snapBtn = document.createElement("button");
|
|
2916
|
+
snapBtn.type = "button";
|
|
2917
|
+
snapBtn.className = "cd-chip-action";
|
|
2918
|
+
snapBtn.title = "Capture element screenshot";
|
|
2919
|
+
snapBtn.setAttribute("aria-label", `Capture screenshot of ${elRef.selector}`);
|
|
2920
|
+
snapBtn.innerHTML = ICON_CAMERA;
|
|
2921
|
+
snapBtn.addEventListener("click", () => {
|
|
2922
|
+
void this.handleElementScreenshot(domNode, elRef.client_ref, elRef.selector);
|
|
2923
|
+
});
|
|
2924
|
+
chip.appendChild(snapBtn);
|
|
2925
|
+
}
|
|
2926
|
+
const removeBtn = document.createElement("button");
|
|
2927
|
+
removeBtn.type = "button";
|
|
2928
|
+
removeBtn.className = "cd-chip-remove";
|
|
2929
|
+
removeBtn.setAttribute("aria-label", `Remove ${elRef.selector}`);
|
|
2930
|
+
removeBtn.innerHTML = ICON_CLOSE;
|
|
2931
|
+
removeBtn.addEventListener("click", () => this.removeElementReference(elRef.client_ref));
|
|
2932
|
+
chip.appendChild(removeBtn);
|
|
2933
|
+
trayContainer.appendChild(chip);
|
|
2934
|
+
}
|
|
2935
|
+
for (const att of this.attachments) {
|
|
2936
|
+
const chip = document.createElement("div");
|
|
2937
|
+
chip.className = "cd-chip";
|
|
2938
|
+
chip.setAttribute("data-chip-type", att.attachment_type);
|
|
2939
|
+
if (att.preview_url) {
|
|
2940
|
+
const img = document.createElement("img");
|
|
2941
|
+
img.className = "cd-chip-thumb";
|
|
2942
|
+
img.src = att.preview_url;
|
|
2943
|
+
img.alt = att.label;
|
|
2944
|
+
chip.appendChild(img);
|
|
2945
|
+
}
|
|
2946
|
+
const badge = document.createElement("span");
|
|
2947
|
+
badge.className = "cd-chip-badge";
|
|
2948
|
+
badge.textContent = att.attachment_type === "screenshot" ? "Shot" : "Image";
|
|
2949
|
+
const label = document.createElement("span");
|
|
2950
|
+
label.className = "cd-chip-label";
|
|
2951
|
+
label.textContent = att.label;
|
|
2952
|
+
const removeBtn = document.createElement("button");
|
|
2953
|
+
removeBtn.type = "button";
|
|
2954
|
+
removeBtn.className = "cd-chip-remove";
|
|
2955
|
+
removeBtn.setAttribute("aria-label", `Remove ${att.label}`);
|
|
2956
|
+
removeBtn.innerHTML = ICON_CLOSE;
|
|
2957
|
+
removeBtn.addEventListener("click", () => this.removeAttachment(att.id));
|
|
2958
|
+
chip.appendChild(badge);
|
|
2959
|
+
chip.appendChild(label);
|
|
2960
|
+
chip.appendChild(removeBtn);
|
|
2961
|
+
trayContainer.appendChild(chip);
|
|
2962
|
+
}
|
|
2963
|
+
}
|
|
2964
|
+
async handleFilesSelected(files) {
|
|
2965
|
+
for (const file of files) {
|
|
2966
|
+
if (this.attachments.length >= this.config.limits.max_attachments) {
|
|
2967
|
+
this.showStatus(`Maximum of ${this.config.limits.max_attachments} attachments reached.`, "error");
|
|
2968
|
+
break;
|
|
2969
|
+
}
|
|
2970
|
+
if (!this.config.limits.allowed_content_types.includes(file.type)) {
|
|
2971
|
+
this.showStatus(`Unsupported file type: ${file.type || file.name}`, "error");
|
|
2972
|
+
continue;
|
|
2973
|
+
}
|
|
2974
|
+
if (file.size > this.config.limits.max_attachment_size) {
|
|
2975
|
+
this.showStatus(`File ${file.name} exceeds size limit.`, "error");
|
|
2976
|
+
continue;
|
|
2977
|
+
}
|
|
2978
|
+
const dataUrl = await this.readFileAsDataUrl(file);
|
|
2979
|
+
this.attachments.push({
|
|
2980
|
+
id: `att_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`,
|
|
2981
|
+
attachment_type: "image",
|
|
2982
|
+
label: file.name,
|
|
2983
|
+
preview_url: dataUrl,
|
|
2984
|
+
blob: file,
|
|
2985
|
+
data_url: dataUrl,
|
|
2986
|
+
filename: file.name,
|
|
2987
|
+
metadata: {
|
|
2988
|
+
original_filename: file.name,
|
|
2989
|
+
byte_size: file.size
|
|
2990
|
+
}
|
|
2991
|
+
});
|
|
2992
|
+
}
|
|
2993
|
+
this.renderTray();
|
|
2994
|
+
this.syncUiStates();
|
|
2995
|
+
}
|
|
2996
|
+
handlePaste(event) {
|
|
2997
|
+
const items = event.clipboardData?.items;
|
|
2998
|
+
if (!items)
|
|
2999
|
+
return;
|
|
3000
|
+
const imageFiles = [];
|
|
3001
|
+
for (let i = 0; i < items.length; i += 1) {
|
|
3002
|
+
const item = items[i];
|
|
3003
|
+
if (item.type.startsWith("image/")) {
|
|
3004
|
+
const file = item.getAsFile();
|
|
3005
|
+
if (file)
|
|
3006
|
+
imageFiles.push(file);
|
|
3007
|
+
}
|
|
3008
|
+
}
|
|
3009
|
+
if (imageFiles.length > 0) {
|
|
3010
|
+
void this.handleFilesSelected(imageFiles);
|
|
3011
|
+
}
|
|
3012
|
+
}
|
|
3013
|
+
readFileAsDataUrl(file) {
|
|
3014
|
+
return new Promise((resolve, reject) => {
|
|
3015
|
+
const reader = new FileReader();
|
|
3016
|
+
reader.onload = () => resolve(String(reader.result || ""));
|
|
3017
|
+
reader.onerror = () => reject(new Error("Failed to read image file"));
|
|
3018
|
+
reader.readAsDataURL(file);
|
|
3019
|
+
});
|
|
3020
|
+
}
|
|
3021
|
+
async handleViewportScreenshot() {
|
|
3022
|
+
if (this.attachments.length >= this.config.limits.max_attachments) {
|
|
3023
|
+
this.showStatus(`Maximum of ${this.config.limits.max_attachments} attachments reached.`, "error");
|
|
3024
|
+
return;
|
|
3025
|
+
}
|
|
3026
|
+
try {
|
|
3027
|
+
const result = await captureViewportScreenshot(this.config.privacy.excluded_selectors);
|
|
3028
|
+
this.attachments.push({
|
|
3029
|
+
id: `shot_${Date.now()}_${Math.random().toString(36).slice(2, 7)}`,
|
|
3030
|
+
attachment_type: "screenshot",
|
|
3031
|
+
label: `Viewport (${result.width}×${result.height})`,
|
|
3032
|
+
preview_url: result.dataUrl,
|
|
3033
|
+
blob: result.blob,
|
|
3034
|
+
data_url: result.dataUrl,
|
|
3035
|
+
filename: `viewport-${Date.now()}.png`,
|
|
3036
|
+
metadata: result.metadata
|
|
3037
|
+
});
|
|
3038
|
+
this.renderTray();
|
|
3039
|
+
this.syncUiStates();
|
|
3040
|
+
}
|
|
3041
|
+
catch (err) {
|
|
3042
|
+
const message = err instanceof Error ? err.message : "Screenshot capture unavailable.";
|
|
3043
|
+
this.showStatus(message, "error");
|
|
3044
|
+
}
|
|
3045
|
+
}
|
|
3046
|
+
async handleElementScreenshot(element, elementClientRef, selectorLabel) {
|
|
3047
|
+
if (this.attachments.length >= this.config.limits.max_attachments) {
|
|
3048
|
+
this.showStatus(`Maximum of ${this.config.limits.max_attachments} attachments reached.`, "error");
|
|
3049
|
+
return;
|
|
3050
|
+
}
|
|
3051
|
+
try {
|
|
3052
|
+
const result = await captureElementScreenshot(element, this.config.features.element_capture_padding, this.config.privacy.excluded_selectors, elementClientRef);
|
|
3053
|
+
this.attachments.push({
|
|
3054
|
+
id: `elshot_${Date.now()}_${Math.random().toString(36).slice(2, 7)}`,
|
|
3055
|
+
attachment_type: "screenshot",
|
|
3056
|
+
label: `Element ${selectorLabel}`,
|
|
3057
|
+
preview_url: result.dataUrl,
|
|
3058
|
+
blob: result.blob,
|
|
3059
|
+
data_url: result.dataUrl,
|
|
3060
|
+
filename: `element-${Date.now()}.png`,
|
|
3061
|
+
element_reference_ref: elementClientRef,
|
|
3062
|
+
metadata: result.metadata
|
|
3063
|
+
});
|
|
3064
|
+
this.renderTray();
|
|
3065
|
+
this.syncUiStates();
|
|
3066
|
+
}
|
|
3067
|
+
catch (err) {
|
|
3068
|
+
const message = err instanceof Error ? err.message : "Element screenshot failed.";
|
|
3069
|
+
this.showStatus(message, "error");
|
|
3070
|
+
}
|
|
3071
|
+
}
|
|
3072
|
+
captureSmartSurroundingContext(element, elementClientRef, friendlyLabel) {
|
|
3073
|
+
const task = (async () => {
|
|
3074
|
+
if (this.attachments.length >= this.config.limits.max_attachments) {
|
|
3075
|
+
return;
|
|
3076
|
+
}
|
|
3077
|
+
try {
|
|
3078
|
+
const result = await captureSurroundingContextScreenshot(element, this.config.features.smart_capture_padding ?? 84, this.config.privacy.excluded_selectors, elementClientRef);
|
|
3079
|
+
if (!this.elementReferences.some((ref) => ref.client_ref === elementClientRef)) {
|
|
3080
|
+
return;
|
|
3081
|
+
}
|
|
3082
|
+
this.attachments = this.attachments.filter((att) => !(att.element_reference_ref === elementClientRef &&
|
|
3083
|
+
att.metadata?.auto_smart_context === true));
|
|
3084
|
+
this.attachments.push({
|
|
3085
|
+
id: `smartshot_${Date.now()}_${Math.random().toString(36).slice(2, 7)}`,
|
|
3086
|
+
attachment_type: "screenshot",
|
|
3087
|
+
label: `Context around ${friendlyLabel}`,
|
|
3088
|
+
preview_url: result.dataUrl,
|
|
3089
|
+
blob: result.blob,
|
|
3090
|
+
data_url: result.dataUrl,
|
|
3091
|
+
filename: `context-${Date.now()}.png`,
|
|
3092
|
+
element_reference_ref: elementClientRef,
|
|
3093
|
+
metadata: {
|
|
3094
|
+
...result.metadata,
|
|
3095
|
+
auto_smart_context: true,
|
|
3096
|
+
friendly_label: friendlyLabel
|
|
3097
|
+
}
|
|
3098
|
+
});
|
|
3099
|
+
this.renderTray();
|
|
3100
|
+
this.syncUiStates();
|
|
3101
|
+
}
|
|
3102
|
+
catch {
|
|
3103
|
+
// Non-fatal in headless or non-canvas environments; smart element card still renders
|
|
3104
|
+
}
|
|
3105
|
+
})();
|
|
3106
|
+
this.pendingSmartCaptures.add(task);
|
|
3107
|
+
void task.finally(() => {
|
|
3108
|
+
this.pendingSmartCaptures.delete(task);
|
|
3109
|
+
});
|
|
3110
|
+
return task;
|
|
3111
|
+
}
|
|
3112
|
+
async handleSubmit() {
|
|
3113
|
+
if (this.isSubmitting)
|
|
3114
|
+
return;
|
|
3115
|
+
const smartMode = this.isSmartSelectionMode();
|
|
3116
|
+
if (this.pendingSmartCaptures.size > 0) {
|
|
3117
|
+
await Promise.allSettled(Array.from(this.pendingSmartCaptures));
|
|
3118
|
+
}
|
|
3119
|
+
const message = (this.textareaEl?.value || "").trim();
|
|
3120
|
+
if (message.length === 0 &&
|
|
3121
|
+
this.attachments.length === 0 &&
|
|
3122
|
+
this.elementReferences.length === 0) {
|
|
3123
|
+
this.showStatus(smartMode
|
|
3124
|
+
? "Write a short note or highlight part of the page first."
|
|
3125
|
+
: "Enter a note, pin an element, or attach a screenshot first.", "error");
|
|
3126
|
+
return;
|
|
3127
|
+
}
|
|
3128
|
+
this.isSubmitting = true;
|
|
3129
|
+
if (this.submitBtn) {
|
|
3130
|
+
this.submitBtn.disabled = true;
|
|
3131
|
+
this.submitBtn.innerHTML = `<span>Sending...</span>`;
|
|
3132
|
+
}
|
|
3133
|
+
try {
|
|
3134
|
+
const submittedElements = [...this.elementReferences];
|
|
3135
|
+
const submittedAttachments = this.attachments.map((att) => ({
|
|
3136
|
+
id: att.id,
|
|
3137
|
+
label: att.label,
|
|
3138
|
+
preview_url: att.preview_url,
|
|
3139
|
+
attachment_type: att.attachment_type
|
|
3140
|
+
}));
|
|
3141
|
+
const payload = {
|
|
3142
|
+
message,
|
|
3143
|
+
page_url: typeof window !== "undefined" ? window.location.href : "/",
|
|
3144
|
+
user_agent: typeof navigator !== "undefined" ? navigator.userAgent : "",
|
|
3145
|
+
metadata: {
|
|
3146
|
+
...this.customMetadata,
|
|
3147
|
+
environment: this.config.environment,
|
|
3148
|
+
selection_mode: smartMode ? "smart" : "verbose",
|
|
3149
|
+
viewport: {
|
|
3150
|
+
width: typeof window !== "undefined" ? window.innerWidth : 0,
|
|
3151
|
+
height: typeof window !== "undefined" ? window.innerHeight : 0
|
|
3152
|
+
},
|
|
3153
|
+
locale: typeof navigator !== "undefined" ? navigator.language : "en"
|
|
3154
|
+
},
|
|
3155
|
+
element_references: this.elementReferences,
|
|
3156
|
+
attachments: this.attachments.map((att) => ({
|
|
3157
|
+
attachment_type: att.attachment_type,
|
|
3158
|
+
filename: att.filename,
|
|
3159
|
+
signed_blob_id: att.signed_blob_id,
|
|
3160
|
+
data_url: att.signed_blob_id ? undefined : att.data_url,
|
|
3161
|
+
element_reference_ref: att.element_reference_ref,
|
|
3162
|
+
metadata: att.metadata
|
|
3163
|
+
}))
|
|
3164
|
+
};
|
|
3165
|
+
const res = await this.apiClient.submitFeedback(payload);
|
|
3166
|
+
const now = new Date();
|
|
3167
|
+
const timeLabel = `${String(now.getHours()).padStart(2, "0")}:${String(now.getMinutes()).padStart(2, "0")}`;
|
|
3168
|
+
this.sessionHistory.push({
|
|
3169
|
+
id: res?.id ?? this.sessionHistory.length + 1,
|
|
3170
|
+
message: message || (smartMode ? "(Highlighted page area)" : "(Context drop without text)"),
|
|
3171
|
+
createdAt: timeLabel,
|
|
3172
|
+
elements: submittedElements,
|
|
3173
|
+
attachments: submittedAttachments
|
|
3174
|
+
});
|
|
3175
|
+
if (this.textareaEl) {
|
|
3176
|
+
this.textareaEl.value = "";
|
|
3177
|
+
}
|
|
3178
|
+
this.elementReferences = [];
|
|
3179
|
+
this.elementDomNodes.clear();
|
|
3180
|
+
this.attachments = [];
|
|
3181
|
+
this.picker?.hideExternalPreview();
|
|
3182
|
+
this.renderTray();
|
|
3183
|
+
this.renderChatThread();
|
|
3184
|
+
this.syncUiStates();
|
|
3185
|
+
this.showStatus(smartMode ? "Thanks! Your feedback was sent." : "Context drop saved.", "success");
|
|
3186
|
+
this.emitWidgetEvent("context-drop:submit", {
|
|
3187
|
+
response: res,
|
|
3188
|
+
payload
|
|
3189
|
+
});
|
|
3190
|
+
}
|
|
3191
|
+
catch (err) {
|
|
3192
|
+
const msg = err instanceof Error ? err.message : "Failed to submit feedback.";
|
|
3193
|
+
this.showStatus(msg, "error");
|
|
3194
|
+
this.emitWidgetEvent("context-drop:error", {
|
|
3195
|
+
error: msg
|
|
3196
|
+
});
|
|
3197
|
+
}
|
|
3198
|
+
finally {
|
|
3199
|
+
this.isSubmitting = false;
|
|
3200
|
+
if (this.submitBtn) {
|
|
3201
|
+
this.submitBtn.disabled = false;
|
|
3202
|
+
this.submitBtn.innerHTML = `${ICON_SEND}<span>Send</span>`;
|
|
3203
|
+
}
|
|
3204
|
+
}
|
|
3205
|
+
}
|
|
3206
|
+
hideStatus() {
|
|
3207
|
+
for (const bar of [this.statusEl, this.agentStatusEl]) {
|
|
3208
|
+
if (bar) {
|
|
3209
|
+
bar.hidden = true;
|
|
3210
|
+
}
|
|
3211
|
+
}
|
|
3212
|
+
}
|
|
3213
|
+
showStatus(text, kind) {
|
|
3214
|
+
for (const bar of [this.statusEl, this.agentStatusEl]) {
|
|
3215
|
+
if (!bar)
|
|
3216
|
+
continue;
|
|
3217
|
+
bar.textContent = text;
|
|
3218
|
+
bar.className = `cd-status ${kind}`;
|
|
3219
|
+
bar.hidden = false;
|
|
3220
|
+
}
|
|
3221
|
+
}
|
|
3222
|
+
}
|
|
3223
|
+
export function registerContextDropWidget() {
|
|
3224
|
+
if (typeof customElements !== "undefined" && !customElements.get("context-drop-widget")) {
|
|
3225
|
+
customElements.define("context-drop-widget", ContextDropWidgetElement);
|
|
3226
|
+
}
|
|
3227
|
+
}
|