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