context-drop 0.2.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,5 @@
1
1
  import { MANDATORY_EXCLUDED_SELECTORS, describeSmartComponent } from "./sanitizer.js";
2
- import { ICON_CAMERA, ICON_CHECK, ICON_CHEVRON_DOWN, ICON_CHEVRON_RIGHT, ICON_CHEVRON_UP, ICON_CLOSE, ICON_CODE_PROMPT, ICON_EXPAND, ICON_FILE_CODE, ICON_GRIP, ICON_IMAGE, ICON_INBOX, ICON_KEY, ICON_MINIMIZE, ICON_PIN, ICON_PLUS, ICON_PUSH_LAYOUT, ICON_SCROLL_VERTICAL, ICON_SEND, ICON_SHIELD, ICON_SIDEBAR, ICON_SUN, ICON_MOON, ICON_UNDO } from "./icons.js";
2
+ import { ICON_CAMERA, ICON_CHECK, ICON_CHEVRON_DOWN, ICON_CHEVRON_RIGHT, ICON_CHEVRON_UP, ICON_CLOSE, ICON_CODE_PROMPT, ICON_COMMENT, 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
3
  import { WIDGET_SHADOW_STYLES } from "./styles.js";
4
4
  import { ContextDropApiClient } from "./api-client.js";
5
5
  import { ElementPicker } from "./element-picker.js";
@@ -131,6 +131,14 @@ export class ContextDropWidgetElement extends BaseHTMLElement {
131
131
  elementDomNodes = new Map();
132
132
  attachments = [];
133
133
  sessionHistory = [];
134
+ ownFeedbacksLoaded = false;
135
+ inFlightOwnFeedbacksPromise = null;
136
+ editingFeedbackId = null;
137
+ commentingFeedbackId = null;
138
+ reviewingFeedbackId = null;
139
+ confirmingDeleteId = null;
140
+ deletingFeedbackId = null;
141
+ stashedDraftBeforeEdit = null;
134
142
  // Local Agent state
135
143
  agentStatusLoaded = false;
136
144
  agentWorkspaceName = "rails-app";
@@ -169,6 +177,8 @@ export class ContextDropWidgetElement extends BaseHTMLElement {
169
177
  composerToolbarScreenshotBtn = null;
170
178
  composerToolbarUploadBtn = null;
171
179
  chatThreadEl = null;
180
+ editBannerEl = null;
181
+ editBannerInfoEl = null;
172
182
  textareaEl = null;
173
183
  trayEl = null;
174
184
  statusEl = null;
@@ -287,6 +297,22 @@ export class ContextDropWidgetElement extends BaseHTMLElement {
287
297
  this.cancelActivePeelDrag();
288
298
  return;
289
299
  }
300
+ if (ev.key === "Escape" &&
301
+ !(this.picker && this.picker.isActive()) &&
302
+ this.isOpen &&
303
+ !this.isMinimized &&
304
+ !this.isAgentModeActive()) {
305
+ if (this.confirmingDeleteId !== null) {
306
+ ev.preventDefault();
307
+ this.cancelDeleteFeedback();
308
+ return;
309
+ }
310
+ if (this.editingFeedbackId !== null) {
311
+ ev.preventDefault();
312
+ this.cancelEditingFeedback();
313
+ return;
314
+ }
315
+ }
290
316
  // Alt+Shift+P / Option+Shift+P toggles Freeze & Pin mode in technical/verbose environments
291
317
  if (!this.isSmartSelectionMode() &&
292
318
  ev.altKey &&
@@ -384,6 +410,8 @@ export class ContextDropWidgetElement extends BaseHTMLElement {
384
410
  "same-origin";
385
411
  const resolvedHeaders = options.headers || this.customHeaders;
386
412
  this.customHeaders = { ...resolvedHeaders };
413
+ this.ownFeedbacksLoaded = false;
414
+ this.inFlightOwnFeedbacksPromise = null;
387
415
  this.apiClient = new ContextDropApiClient(endpoint || "/context_drop", {
388
416
  credentials: resolvedCredentials,
389
417
  headers: this.customHeaders
@@ -457,8 +485,13 @@ export class ContextDropWidgetElement extends BaseHTMLElement {
457
485
  return;
458
486
  }
459
487
  this.surfaceMode = mode;
460
- if (mode === "agent" && this.isOpen) {
461
- void this.ensureAgentStatusLoaded();
488
+ if (this.isOpen) {
489
+ if (mode === "agent") {
490
+ void this.ensureAgentStatusLoaded();
491
+ }
492
+ else if (!this.isMinimized) {
493
+ void this.ensureOwnFeedbacksLoaded();
494
+ }
462
495
  }
463
496
  this.renderTray();
464
497
  this.renderChatThread();
@@ -474,6 +507,379 @@ export class ContextDropWidgetElement extends BaseHTMLElement {
474
507
  getSessionHistory() {
475
508
  return [...this.sessionHistory];
476
509
  }
510
+ getEditingFeedbackId() {
511
+ return this.editingFeedbackId;
512
+ }
513
+ formatTimestampLabel(isoOrLabel) {
514
+ if (isoOrLabel && /^\d{2}:\d{2}$/.test(isoOrLabel.trim())) {
515
+ return isoOrLabel.trim();
516
+ }
517
+ const parsed = isoOrLabel ? new Date(isoOrLabel) : new Date();
518
+ const valid = Number.isNaN(parsed.getTime()) ? new Date() : parsed;
519
+ return `${String(valid.getHours()).padStart(2, "0")}:${String(valid.getMinutes()).padStart(2, "0")}`;
520
+ }
521
+ mapOwnRecordToSessionEntry(record, existingEntry) {
522
+ const smartMode = this.isSmartSelectionMode();
523
+ const rawMessage = typeof record.message === "string" ? record.message : "";
524
+ const displayMessage = rawMessage || (smartMode ? "(Highlighted page area)" : "(Context drop without text)");
525
+ const token = record.ownership_token ||
526
+ existingEntry?.ownershipToken ||
527
+ this.apiClient.getOwnershipToken(record.id) ||
528
+ undefined;
529
+ const isEdited = Boolean(record.created_at && record.updated_at && record.updated_at !== record.created_at);
530
+ const serverAttachments = Array.isArray(record.attachments) ? record.attachments : [];
531
+ const mappedAttachments = serverAttachments.map((att, idx) => {
532
+ const existingAtt = existingEntry?.attachments.find((a) => (a.server_id !== undefined && String(a.server_id) === String(att.id)) ||
533
+ (a.server_id === undefined &&
534
+ idx < (existingEntry?.attachments.length || 0) &&
535
+ existingEntry?.attachments[idx]?.filename === att.filename));
536
+ const previewUrl = existingAtt?.preview_url &&
537
+ (existingAtt.preview_url.startsWith("data:") || existingAtt.preview_url.startsWith("blob:"))
538
+ ? existingAtt.preview_url
539
+ : this.apiClient.buildAttachmentPreviewUrl(record.id, att.id, token);
540
+ const meta = att.metadata && typeof att.metadata === "object" && !Array.isArray(att.metadata)
541
+ ? { ...att.metadata }
542
+ : existingAtt?.metadata
543
+ ? { ...existingAtt.metadata }
544
+ : {};
545
+ let label = existingAtt?.label || "";
546
+ if (!label) {
547
+ if (meta.auto_smart_context === true && typeof meta.friendly_label === "string") {
548
+ label = `Context around ${meta.friendly_label}`;
549
+ }
550
+ else if (att.filename) {
551
+ label = att.filename;
552
+ }
553
+ else {
554
+ label = att.attachment_type === "screenshot" ? "Screenshot" : "Attached image";
555
+ }
556
+ }
557
+ return {
558
+ id: existingAtt?.id || `srv_att_${record.id}_${att.id}`,
559
+ server_id: att.id,
560
+ label,
561
+ preview_url: previewUrl,
562
+ attachment_type: att.attachment_type === "screenshot" ? "screenshot" : "image",
563
+ filename: att.filename,
564
+ element_reference_ref: att.element_reference_ref ?? existingAtt?.element_reference_ref ?? null,
565
+ metadata: meta
566
+ };
567
+ });
568
+ const elements = Array.isArray(record.element_references)
569
+ ? structuredClone(record.element_references)
570
+ : existingEntry?.elements
571
+ ? structuredClone(existingEntry.elements)
572
+ : [];
573
+ const mappedComments = Array.isArray(record.comments)
574
+ ? record.comments.map((c) => ({
575
+ id: c.id,
576
+ author_label: c.author_label || "You",
577
+ author_type: c.author_type ?? null,
578
+ body: c.body ?? null,
579
+ createdAt: this.formatTimestampLabel(c.created_at),
580
+ rawCreatedAt: c.created_at,
581
+ attachments: Array.isArray(c.attachments)
582
+ ? c.attachments.map((att) => ({
583
+ id: `srv-catt-${att.id}`,
584
+ server_id: att.id,
585
+ label: att.filename || "image.png",
586
+ preview_url: this.apiClient.buildAttachmentPreviewUrl(record.id, att.id, token),
587
+ attachment_type: att.attachment_type || "image",
588
+ filename: att.filename,
589
+ metadata: att.metadata || {}
590
+ }))
591
+ : []
592
+ }))
593
+ : existingEntry?.comments
594
+ ? structuredClone(existingEntry.comments)
595
+ : [];
596
+ return {
597
+ id: record.id,
598
+ message: displayMessage,
599
+ rawMessage,
600
+ createdAt: this.formatTimestampLabel(record.created_at || existingEntry?.rawCreatedAt),
601
+ rawCreatedAt: record.created_at,
602
+ rawUpdatedAt: record.updated_at,
603
+ isEdited: isEdited || Boolean(existingEntry?.isEdited),
604
+ status: record.status,
605
+ ownershipToken: token,
606
+ elements,
607
+ attachments: mappedAttachments,
608
+ comments: mappedComments
609
+ };
610
+ }
611
+ async refreshOwnFeedbacks() {
612
+ if (typeof fetch !== "function") {
613
+ return [...this.sessionHistory];
614
+ }
615
+ if (this.inFlightOwnFeedbacksPromise) {
616
+ return this.inFlightOwnFeedbacksPromise;
617
+ }
618
+ this.inFlightOwnFeedbacksPromise = (async () => {
619
+ try {
620
+ const res = await this.apiClient.fetchOwnFeedbacks();
621
+ this.ownFeedbacksLoaded = true;
622
+ const records = Array.isArray(res?.feedbacks) ? res.feedbacks : [];
623
+ if (records.length === 0) {
624
+ return [...this.sessionHistory];
625
+ }
626
+ const existingById = new Map();
627
+ for (const entry of this.sessionHistory) {
628
+ existingById.set(String(entry.id), entry);
629
+ }
630
+ const chronologicalServerRecords = [...records].reverse();
631
+ const serverIds = new Set();
632
+ const merged = [];
633
+ for (const record of chronologicalServerRecords) {
634
+ if (!record || record.id === undefined || record.id === null)
635
+ continue;
636
+ const idKey = String(record.id);
637
+ serverIds.add(idKey);
638
+ merged.push(this.mapOwnRecordToSessionEntry(record, existingById.get(idKey)));
639
+ }
640
+ for (const localEntry of this.sessionHistory) {
641
+ if (!serverIds.has(String(localEntry.id))) {
642
+ merged.push(localEntry);
643
+ }
644
+ }
645
+ this.sessionHistory = merged;
646
+ this.renderChatThread();
647
+ this.syncUiStates();
648
+ }
649
+ catch {
650
+ // Non-fatal when offline or using minimal synthetic endpoints
651
+ }
652
+ finally {
653
+ this.inFlightOwnFeedbacksPromise = null;
654
+ }
655
+ return [...this.sessionHistory];
656
+ })();
657
+ return this.inFlightOwnFeedbacksPromise;
658
+ }
659
+ async ensureOwnFeedbacksLoaded() {
660
+ if (this.ownFeedbacksLoaded)
661
+ return;
662
+ await this.refreshOwnFeedbacks();
663
+ }
664
+ startEditingFeedback(feedbackId) {
665
+ const targetKey = String(feedbackId);
666
+ const entry = this.sessionHistory.find((item) => String(item.id) === targetKey);
667
+ if (!entry)
668
+ return false;
669
+ if (this.commentingFeedbackId !== null) {
670
+ this.cancelCommentingFeedback();
671
+ }
672
+ this.confirmingDeleteId = null;
673
+ this.hideStatus();
674
+ if (this.editingFeedbackId === null && this.commentingFeedbackId === null) {
675
+ this.stashedDraftBeforeEdit = {
676
+ message: this.textareaEl?.value || "",
677
+ elementReferences: [...this.elementReferences],
678
+ elementDomNodes: new Map(this.elementDomNodes),
679
+ attachments: [...this.attachments]
680
+ };
681
+ }
682
+ this.editingFeedbackId = targetKey;
683
+ if (this.textareaEl) {
684
+ this.textareaEl.value = entry.rawMessage !== undefined ? entry.rawMessage : entry.message;
685
+ }
686
+ this.elementReferences = structuredClone(entry.elements);
687
+ this.elementDomNodes.clear();
688
+ if (typeof document !== "undefined" && typeof document.querySelector === "function") {
689
+ for (const elRef of this.elementReferences) {
690
+ if (!elRef.selector)
691
+ continue;
692
+ try {
693
+ const liveNode = document.querySelector(elRef.selector);
694
+ if (liveNode) {
695
+ this.elementDomNodes.set(elRef.client_ref, liveNode);
696
+ }
697
+ }
698
+ catch {
699
+ // Ignore invalid or complex selectors not resolvable on current page
700
+ }
701
+ }
702
+ }
703
+ this.attachments = entry.attachments.map((att) => ({
704
+ id: att.id,
705
+ existing_attachment_id: att.server_id,
706
+ attachment_type: att.attachment_type,
707
+ label: att.label,
708
+ preview_url: att.preview_url,
709
+ signed_blob_id: att.signed_blob_id,
710
+ data_url: att.data_url,
711
+ filename: att.filename || att.label || "attachment.png",
712
+ element_reference_ref: att.element_reference_ref || undefined,
713
+ metadata: att.metadata ? structuredClone(att.metadata) : {}
714
+ }));
715
+ this.picker?.refreshPinnedMarkers();
716
+ this.renderTray();
717
+ this.renderChatThread();
718
+ this.syncUiStates();
719
+ this.textareaEl?.focus();
720
+ return true;
721
+ }
722
+ cancelEditingFeedback() {
723
+ if (this.editingFeedbackId === null)
724
+ return;
725
+ this.editingFeedbackId = null;
726
+ if (this.stashedDraftBeforeEdit) {
727
+ if (this.textareaEl) {
728
+ this.textareaEl.value = this.stashedDraftBeforeEdit.message;
729
+ }
730
+ this.elementReferences = this.stashedDraftBeforeEdit.elementReferences;
731
+ this.elementDomNodes = this.stashedDraftBeforeEdit.elementDomNodes;
732
+ this.attachments = this.stashedDraftBeforeEdit.attachments;
733
+ this.stashedDraftBeforeEdit = null;
734
+ }
735
+ else {
736
+ if (this.textareaEl) {
737
+ this.textareaEl.value = "";
738
+ }
739
+ this.elementReferences = [];
740
+ this.elementDomNodes.clear();
741
+ this.attachments = [];
742
+ }
743
+ this.picker?.hideExternalPreview();
744
+ this.picker?.refreshPinnedMarkers();
745
+ this.renderTray();
746
+ this.renderChatThread();
747
+ this.syncUiStates();
748
+ }
749
+ startCommentingFeedback(feedbackId) {
750
+ const targetKey = String(feedbackId);
751
+ const entry = this.sessionHistory.find((item) => String(item.id) === targetKey);
752
+ if (!entry)
753
+ return false;
754
+ if (this.editingFeedbackId !== null) {
755
+ this.cancelEditingFeedback();
756
+ }
757
+ this.confirmingDeleteId = null;
758
+ this.hideStatus();
759
+ if (this.commentingFeedbackId === null && this.editingFeedbackId === null) {
760
+ this.stashedDraftBeforeEdit = {
761
+ message: this.textareaEl?.value || "",
762
+ elementReferences: [...this.elementReferences],
763
+ elementDomNodes: new Map(this.elementDomNodes),
764
+ attachments: [...this.attachments]
765
+ };
766
+ }
767
+ this.commentingFeedbackId = targetKey;
768
+ if (this.textareaEl) {
769
+ this.textareaEl.value = "";
770
+ }
771
+ this.elementReferences = [];
772
+ this.elementDomNodes.clear();
773
+ this.attachments = [];
774
+ this.picker?.hideExternalPreview();
775
+ this.picker?.refreshPinnedMarkers();
776
+ this.renderTray();
777
+ this.renderChatThread();
778
+ this.syncUiStates();
779
+ this.textareaEl?.focus();
780
+ return true;
781
+ }
782
+ cancelCommentingFeedback() {
783
+ if (this.commentingFeedbackId === null)
784
+ return;
785
+ this.commentingFeedbackId = null;
786
+ if (this.stashedDraftBeforeEdit) {
787
+ if (this.textareaEl) {
788
+ this.textareaEl.value = this.stashedDraftBeforeEdit.message;
789
+ }
790
+ this.elementReferences = this.stashedDraftBeforeEdit.elementReferences;
791
+ this.elementDomNodes = this.stashedDraftBeforeEdit.elementDomNodes;
792
+ this.attachments = this.stashedDraftBeforeEdit.attachments;
793
+ this.stashedDraftBeforeEdit = null;
794
+ }
795
+ else {
796
+ if (this.textareaEl) {
797
+ this.textareaEl.value = "";
798
+ }
799
+ this.elementReferences = [];
800
+ this.elementDomNodes.clear();
801
+ this.attachments = [];
802
+ }
803
+ this.renderTray();
804
+ this.renderChatThread();
805
+ this.syncUiStates();
806
+ }
807
+ async markFeedbackReviewed(feedbackId) {
808
+ if (this.reviewingFeedbackId !== null || this.isSubmitting)
809
+ return;
810
+ const targetKey = String(feedbackId);
811
+ const entry = this.sessionHistory.find((item) => String(item.id) === targetKey);
812
+ if (!entry)
813
+ return;
814
+ this.reviewingFeedbackId = targetKey;
815
+ this.renderChatThread();
816
+ try {
817
+ const res = await this.apiClient.updateFeedback(entry.id, { status: "reviewed" }, entry.ownershipToken);
818
+ entry.status = res?.status || "reviewed";
819
+ if (res?.ownership_token) {
820
+ entry.ownershipToken = res.ownership_token;
821
+ }
822
+ this.reviewingFeedbackId = null;
823
+ this.renderChatThread();
824
+ this.showStatus("Fix marked as reviewed.", "success");
825
+ this.emitWidgetEvent("context-drop:update", { response: res, id: entry.id });
826
+ }
827
+ catch (err) {
828
+ this.reviewingFeedbackId = null;
829
+ this.renderChatThread();
830
+ const msg = err instanceof Error ? err.message : "Failed to mark feedback as reviewed.";
831
+ this.showStatus(msg, "error");
832
+ }
833
+ }
834
+ requestDeleteFeedback(feedbackId) {
835
+ const targetKey = String(feedbackId);
836
+ if (!this.sessionHistory.some((item) => String(item.id) === targetKey))
837
+ return;
838
+ this.confirmingDeleteId = this.confirmingDeleteId === targetKey ? null : targetKey;
839
+ this.renderChatThread();
840
+ }
841
+ cancelDeleteFeedback() {
842
+ if (this.confirmingDeleteId === null)
843
+ return;
844
+ this.confirmingDeleteId = null;
845
+ this.renderChatThread();
846
+ }
847
+ async confirmDeleteFeedback(feedbackId) {
848
+ if (this.deletingFeedbackId !== null)
849
+ return;
850
+ const targetKey = String(feedbackId);
851
+ const entry = this.sessionHistory.find((item) => String(item.id) === targetKey);
852
+ if (!entry)
853
+ return;
854
+ this.deletingFeedbackId = targetKey;
855
+ this.renderChatThread();
856
+ try {
857
+ const res = await this.apiClient.deleteFeedback(entry.id, entry.ownershipToken);
858
+ if (this.editingFeedbackId === targetKey) {
859
+ this.cancelEditingFeedback();
860
+ }
861
+ this.sessionHistory = this.sessionHistory.filter((item) => String(item.id) !== targetKey);
862
+ this.confirmingDeleteId = null;
863
+ this.deletingFeedbackId = null;
864
+ this.renderChatThread();
865
+ this.renderTray();
866
+ this.syncUiStates();
867
+ this.showStatus(this.isSmartSelectionMode() ? "Your feedback was deleted." : "Feedback deleted.", "success");
868
+ this.emitWidgetEvent("context-drop:delete", {
869
+ response: res,
870
+ id: entry.id
871
+ });
872
+ }
873
+ catch (err) {
874
+ this.deletingFeedbackId = null;
875
+ this.renderChatThread();
876
+ const msg = err instanceof Error ? err.message : "Failed to delete feedback.";
877
+ this.showStatus(msg, "error");
878
+ this.emitWidgetEvent("context-drop:error", {
879
+ error: msg
880
+ });
881
+ }
882
+ }
477
883
  getAgentMessages() {
478
884
  return [...this.agentMessages];
479
885
  }
@@ -509,6 +915,9 @@ export class ContextDropWidgetElement extends BaseHTMLElement {
509
915
  if (this.isAgentModeActive()) {
510
916
  void this.ensureAgentStatusLoaded();
511
917
  }
918
+ else {
919
+ void this.ensureOwnFeedbacksLoaded();
920
+ }
512
921
  this.syncUiStates();
513
922
  this.focusActiveInput();
514
923
  if (!wasOpen) {
@@ -1405,6 +1814,29 @@ export class ContextDropWidgetElement extends BaseHTMLElement {
1405
1814
  composer.appendChild(fileInput);
1406
1815
  const promptShell = document.createElement("div");
1407
1816
  promptShell.className = "cd-prompt-shell";
1817
+ const editBanner = document.createElement("div");
1818
+ editBanner.className = "cd-edit-banner";
1819
+ editBanner.hidden = true;
1820
+ const editBannerInfo = document.createElement("span");
1821
+ editBannerInfo.className = "cd-edit-banner-info";
1822
+ this.editBannerInfoEl = editBannerInfo;
1823
+ editBanner.appendChild(editBannerInfo);
1824
+ const cancelEditBtn = document.createElement("button");
1825
+ cancelEditBtn.type = "button";
1826
+ cancelEditBtn.className = "cd-btn cd-btn-xs";
1827
+ cancelEditBtn.setAttribute("aria-label", "Cancel editing feedback");
1828
+ cancelEditBtn.textContent = "Cancel";
1829
+ cancelEditBtn.addEventListener("click", () => {
1830
+ if (this.commentingFeedbackId !== null) {
1831
+ this.cancelCommentingFeedback();
1832
+ }
1833
+ else {
1834
+ this.cancelEditingFeedback();
1835
+ }
1836
+ });
1837
+ editBanner.appendChild(cancelEditBtn);
1838
+ this.editBannerEl = editBanner;
1839
+ promptShell.appendChild(editBanner);
1408
1840
  const textarea = document.createElement("textarea");
1409
1841
  textarea.className = "cd-textarea";
1410
1842
  textarea.placeholder = this.config.placeholder;
@@ -1415,8 +1847,21 @@ export class ContextDropWidgetElement extends BaseHTMLElement {
1415
1847
  void this.handleSubmit();
1416
1848
  }
1417
1849
  else if (ev.key === "Escape") {
1850
+ if (ev.defaultPrevented)
1851
+ return;
1418
1852
  ev.preventDefault();
1419
- this.closeComposer();
1853
+ if (this.confirmingDeleteId !== null) {
1854
+ this.cancelDeleteFeedback();
1855
+ }
1856
+ else if (this.commentingFeedbackId !== null) {
1857
+ this.cancelCommentingFeedback();
1858
+ }
1859
+ else if (this.editingFeedbackId !== null) {
1860
+ this.cancelEditingFeedback();
1861
+ }
1862
+ else {
1863
+ this.closeComposer();
1864
+ }
1420
1865
  }
1421
1866
  });
1422
1867
  textarea.addEventListener("paste", (ev) => this.handlePaste(ev));
@@ -2526,12 +2971,22 @@ export class ContextDropWidgetElement extends BaseHTMLElement {
2526
2971
  if (this.composerModeSwitchWrap) {
2527
2972
  this.composerModeSwitchWrap.hidden = !this.config.agent?.enabled;
2528
2973
  }
2529
- if (this.textareaEl && !this.customPlaceholderSet) {
2530
- this.textareaEl.placeholder = smartMode
2531
- ? DEFAULT_SMART_PLACEHOLDER
2532
- : DEFAULT_VERBOSE_PLACEHOLDER;
2974
+ const isCommenting = this.commentingFeedbackId !== null;
2975
+ if (this.textareaEl) {
2976
+ if (isCommenting) {
2977
+ this.textareaEl.placeholder = "Write a comment... (attach or paste images below)";
2978
+ }
2979
+ else if (!this.customPlaceholderSet) {
2980
+ this.textareaEl.placeholder = smartMode
2981
+ ? DEFAULT_SMART_PLACEHOLDER
2982
+ : DEFAULT_VERBOSE_PLACEHOLDER;
2983
+ }
2984
+ }
2985
+ if (this.cornerPinBtn) {
2986
+ this.cornerPinBtn.hidden = isCommenting;
2533
2987
  }
2534
2988
  if (this.composerToolbarPickBtn) {
2989
+ this.composerToolbarPickBtn.hidden = isCommenting;
2535
2990
  if (smartMode) {
2536
2991
  this.composerToolbarPickBtn.title = "Highlight an area on the page";
2537
2992
  this.composerToolbarPickBtn.setAttribute("aria-label", "Highlight an area on the page");
@@ -2544,13 +2999,64 @@ export class ContextDropWidgetElement extends BaseHTMLElement {
2544
2999
  }
2545
3000
  }
2546
3001
  if (this.composerToolbarScreenshotBtn) {
2547
- this.composerToolbarScreenshotBtn.hidden = smartMode;
3002
+ this.composerToolbarScreenshotBtn.hidden = smartMode && !isCommenting;
2548
3003
  }
2549
3004
  if (this.composerToolbarUploadBtn) {
2550
3005
  this.composerToolbarUploadBtn.innerHTML = smartMode
2551
3006
  ? `${ICON_IMAGE}<span>Add image</span>`
2552
3007
  : `${ICON_IMAGE}<span>Image</span>`;
2553
3008
  }
3009
+ if (this.editBannerEl && this.editBannerInfoEl) {
3010
+ if (this.editingFeedbackId !== null) {
3011
+ this.editBannerEl.hidden = false;
3012
+ this.editBannerInfoEl.innerHTML = "";
3013
+ const iconSpan = document.createElement("span");
3014
+ iconSpan.innerHTML = ICON_EDIT;
3015
+ const labelSpan = document.createElement("span");
3016
+ labelSpan.textContent = smartMode ? "Editing your feedback" : "Editing feedback";
3017
+ this.editBannerInfoEl.appendChild(iconSpan);
3018
+ this.editBannerInfoEl.appendChild(labelSpan);
3019
+ if (!smartMode) {
3020
+ const idSpan = document.createElement("span");
3021
+ idSpan.className = "cd-edit-banner-id";
3022
+ idSpan.textContent = `#${this.editingFeedbackId}`;
3023
+ this.editBannerInfoEl.appendChild(idSpan);
3024
+ }
3025
+ }
3026
+ else if (this.commentingFeedbackId !== null) {
3027
+ this.editBannerEl.hidden = false;
3028
+ this.editBannerInfoEl.innerHTML = "";
3029
+ const iconSpan = document.createElement("span");
3030
+ iconSpan.innerHTML = ICON_COMMENT;
3031
+ const labelSpan = document.createElement("span");
3032
+ labelSpan.textContent = smartMode ? "Commenting on feedback" : "Commenting on";
3033
+ this.editBannerInfoEl.appendChild(iconSpan);
3034
+ this.editBannerInfoEl.appendChild(labelSpan);
3035
+ if (!smartMode) {
3036
+ const idSpan = document.createElement("span");
3037
+ idSpan.className = "cd-edit-banner-id";
3038
+ idSpan.textContent = `#${this.commentingFeedbackId}`;
3039
+ this.editBannerInfoEl.appendChild(idSpan);
3040
+ }
3041
+ }
3042
+ else {
3043
+ this.editBannerEl.hidden = true;
3044
+ }
3045
+ }
3046
+ if (this.submitBtn && !this.isSubmitting) {
3047
+ if (this.editingFeedbackId !== null) {
3048
+ this.submitBtn.setAttribute("aria-label", "Save feedback changes");
3049
+ this.submitBtn.innerHTML = `${ICON_CHECK}<span>Save</span>`;
3050
+ }
3051
+ else if (this.commentingFeedbackId !== null) {
3052
+ this.submitBtn.setAttribute("aria-label", "Submit comment");
3053
+ this.submitBtn.innerHTML = `${ICON_SEND}<span>Comment</span>`;
3054
+ }
3055
+ else {
3056
+ this.submitBtn.setAttribute("aria-label", "Submit feedback");
3057
+ this.submitBtn.innerHTML = `${ICON_SEND}<span>Send</span>`;
3058
+ }
3059
+ }
2554
3060
  if (this.agentSidebarEl) {
2555
3061
  this.agentSidebarEl.hidden = !showAgentSidebar;
2556
3062
  }
@@ -2751,8 +3257,20 @@ export class ContextDropWidgetElement extends BaseHTMLElement {
2751
3257
  }
2752
3258
  this.chatThreadEl.hidden = false;
2753
3259
  for (const entry of this.sessionHistory) {
3260
+ const entryIdStr = String(entry.id);
3261
+ const isEditingThis = this.editingFeedbackId === entryIdStr;
3262
+ const isCommentingThis = this.commentingFeedbackId === entryIdStr;
3263
+ const isReviewingThis = this.reviewingFeedbackId === entryIdStr;
3264
+ const isConfirmingDelete = this.confirmingDeleteId === entryIdStr;
3265
+ const isDeletingThis = this.deletingFeedbackId === entryIdStr;
3266
+ const statusName = entry.status || "new";
2754
3267
  const msgCard = document.createElement("div");
2755
- msgCard.className = "cd-chat-msg";
3268
+ msgCard.className = isEditingThis
3269
+ ? "cd-chat-msg is-editing"
3270
+ : isCommentingThis
3271
+ ? "cd-chat-msg is-commenting"
3272
+ : "cd-chat-msg";
3273
+ msgCard.setAttribute("data-feedback-id", entryIdStr);
2756
3274
  if (entry.elements.length > 0 || entry.attachments.length > 0) {
2757
3275
  const pills = document.createElement("div");
2758
3276
  pills.className = "cd-chat-msg-pills";
@@ -2781,15 +3299,169 @@ export class ContextDropWidgetElement extends BaseHTMLElement {
2781
3299
  textP.textContent = entry.message;
2782
3300
  msgCard.appendChild(textP);
2783
3301
  }
3302
+ if (entry.comments && entry.comments.length > 0) {
3303
+ const commentsList = document.createElement("div");
3304
+ commentsList.className = "cd-comments-list";
3305
+ for (const comment of entry.comments) {
3306
+ const cItem = document.createElement("div");
3307
+ cItem.className = "cd-comment-item";
3308
+ const cHeader = document.createElement("div");
3309
+ cHeader.className = "cd-comment-header";
3310
+ const cAuthor = document.createElement("span");
3311
+ cAuthor.className = "cd-comment-author";
3312
+ cAuthor.textContent = comment.author_label || "You";
3313
+ const cTime = document.createElement("span");
3314
+ cTime.textContent = comment.createdAt;
3315
+ cHeader.appendChild(cAuthor);
3316
+ cHeader.appendChild(cTime);
3317
+ cItem.appendChild(cHeader);
3318
+ if (comment.body) {
3319
+ const cBody = document.createElement("p");
3320
+ cBody.className = "cd-comment-body";
3321
+ cBody.textContent = comment.body;
3322
+ cItem.appendChild(cBody);
3323
+ }
3324
+ if (comment.attachments && comment.attachments.length > 0) {
3325
+ const cImgs = document.createElement("div");
3326
+ cImgs.className = "cd-comment-images";
3327
+ for (const att of comment.attachments) {
3328
+ if (att.preview_url) {
3329
+ const imgLink = document.createElement("a");
3330
+ imgLink.href = att.preview_url;
3331
+ imgLink.target = "_blank";
3332
+ imgLink.rel = "noopener noreferrer";
3333
+ const img = document.createElement("img");
3334
+ img.src = att.preview_url;
3335
+ img.alt = att.label || "Comment image";
3336
+ img.className = "cd-comment-thumb";
3337
+ imgLink.appendChild(img);
3338
+ cImgs.appendChild(imgLink);
3339
+ }
3340
+ }
3341
+ if (cImgs.childNodes.length > 0) {
3342
+ cItem.appendChild(cImgs);
3343
+ }
3344
+ }
3345
+ commentsList.appendChild(cItem);
3346
+ }
3347
+ msgCard.appendChild(commentsList);
3348
+ }
2784
3349
  const meta = document.createElement("div");
2785
3350
  meta.className = "cd-chat-msg-meta";
3351
+ const metaInfo = document.createElement("div");
3352
+ metaInfo.className = "cd-chat-msg-meta-info";
3353
+ const statusBadge = document.createElement("span");
3354
+ statusBadge.className = `cd-msg-status-badge is-${statusName}`;
3355
+ statusBadge.textContent = statusName;
2786
3356
  const idSpan = document.createElement("span");
2787
- idSpan.textContent = smartMode ? "Feedback sent" : `Dropped #${entry.id}`;
3357
+ const baseLabel = smartMode ? "Feedback sent" : `Dropped #${entry.id}`;
3358
+ idSpan.textContent = entry.isEdited ? `${baseLabel} · edited` : baseLabel;
3359
+ const sepSpan = document.createElement("span");
3360
+ sepSpan.setAttribute("aria-hidden", "true");
3361
+ sepSpan.textContent = "·";
2788
3362
  const timeSpan = document.createElement("span");
2789
3363
  timeSpan.textContent = entry.createdAt;
2790
- meta.appendChild(idSpan);
2791
- meta.appendChild(timeSpan);
3364
+ metaInfo.appendChild(statusBadge);
3365
+ metaInfo.appendChild(idSpan);
3366
+ metaInfo.appendChild(sepSpan);
3367
+ metaInfo.appendChild(timeSpan);
3368
+ const actions = document.createElement("div");
3369
+ actions.className = "cd-chat-msg-actions";
3370
+ if (statusName === "fixed") {
3371
+ const reviewBtn = document.createElement("button");
3372
+ reviewBtn.type = "button";
3373
+ reviewBtn.className = "cd-review-btn";
3374
+ reviewBtn.title = "Confirm fix was tested and reviewed";
3375
+ reviewBtn.setAttribute("aria-label", "Mark fix as reviewed");
3376
+ reviewBtn.disabled = isReviewingThis || this.isSubmitting || this.deletingFeedbackId !== null;
3377
+ reviewBtn.innerHTML = `${ICON_CHECK}<span>${isReviewingThis ? "Saving..." : "Mark Reviewed"}</span>`;
3378
+ reviewBtn.addEventListener("click", () => {
3379
+ void this.markFeedbackReviewed(entry.id);
3380
+ });
3381
+ actions.appendChild(reviewBtn);
3382
+ }
3383
+ const commentBtn = document.createElement("button");
3384
+ commentBtn.type = "button";
3385
+ commentBtn.className = `cd-msg-action-btn${isCommentingThis ? " is-active" : ""}`;
3386
+ commentBtn.title = isCommentingThis ? "Cancel comment" : "Add comment";
3387
+ commentBtn.setAttribute("aria-label", "Add comment");
3388
+ commentBtn.setAttribute("aria-pressed", String(isCommentingThis));
3389
+ commentBtn.disabled = this.isSubmitting || this.deletingFeedbackId !== null;
3390
+ commentBtn.innerHTML = ICON_COMMENT;
3391
+ commentBtn.addEventListener("click", () => {
3392
+ if (this.commentingFeedbackId === entryIdStr) {
3393
+ this.cancelCommentingFeedback();
3394
+ }
3395
+ else {
3396
+ this.startCommentingFeedback(entry.id);
3397
+ }
3398
+ });
3399
+ const editBtn = document.createElement("button");
3400
+ editBtn.type = "button";
3401
+ editBtn.className = `cd-msg-action-btn${isEditingThis ? " is-active" : ""}`;
3402
+ editBtn.title = isEditingThis ? "Cancel editing" : "Edit feedback";
3403
+ editBtn.setAttribute("aria-label", "Edit feedback");
3404
+ editBtn.setAttribute("aria-pressed", String(isEditingThis));
3405
+ editBtn.disabled = this.isSubmitting || this.deletingFeedbackId !== null;
3406
+ editBtn.innerHTML = ICON_EDIT;
3407
+ editBtn.addEventListener("click", () => {
3408
+ if (this.editingFeedbackId === entryIdStr) {
3409
+ this.cancelEditingFeedback();
3410
+ }
3411
+ else {
3412
+ this.startEditingFeedback(entry.id);
3413
+ }
3414
+ });
3415
+ const deleteBtn = document.createElement("button");
3416
+ deleteBtn.type = "button";
3417
+ deleteBtn.className = "cd-msg-action-btn is-danger";
3418
+ deleteBtn.title = "Delete feedback";
3419
+ deleteBtn.setAttribute("aria-label", "Delete feedback");
3420
+ deleteBtn.disabled = this.isSubmitting || this.deletingFeedbackId !== null;
3421
+ deleteBtn.innerHTML = ICON_TRASH;
3422
+ deleteBtn.addEventListener("click", () => {
3423
+ this.requestDeleteFeedback(entry.id);
3424
+ });
3425
+ actions.appendChild(commentBtn);
3426
+ actions.appendChild(editBtn);
3427
+ actions.appendChild(deleteBtn);
3428
+ meta.appendChild(metaInfo);
3429
+ meta.appendChild(actions);
2792
3430
  msgCard.appendChild(meta);
3431
+ if (isConfirmingDelete) {
3432
+ const confirmBar = document.createElement("div");
3433
+ confirmBar.className = "cd-msg-delete-confirm";
3434
+ confirmBar.setAttribute("role", "group");
3435
+ confirmBar.setAttribute("aria-label", "Confirm feedback deletion");
3436
+ const confirmLabel = document.createElement("span");
3437
+ confirmLabel.className = "cd-msg-delete-label";
3438
+ confirmLabel.textContent = "Delete this feedback?";
3439
+ const btnGroup = document.createElement("div");
3440
+ btnGroup.className = "cd-msg-delete-btns";
3441
+ const cancelDeleteBtn = document.createElement("button");
3442
+ cancelDeleteBtn.type = "button";
3443
+ cancelDeleteBtn.className = "cd-btn cd-btn-xs";
3444
+ cancelDeleteBtn.setAttribute("aria-label", "Cancel delete");
3445
+ cancelDeleteBtn.disabled = isDeletingThis;
3446
+ cancelDeleteBtn.textContent = "Cancel";
3447
+ cancelDeleteBtn.addEventListener("click", () => {
3448
+ this.cancelDeleteFeedback();
3449
+ });
3450
+ const confirmDeleteBtn = document.createElement("button");
3451
+ confirmDeleteBtn.type = "button";
3452
+ confirmDeleteBtn.className = "cd-btn cd-btn-xs cd-btn-danger";
3453
+ confirmDeleteBtn.setAttribute("aria-label", "Confirm delete feedback");
3454
+ confirmDeleteBtn.disabled = isDeletingThis;
3455
+ confirmDeleteBtn.textContent = isDeletingThis ? "Deleting..." : "Delete";
3456
+ confirmDeleteBtn.addEventListener("click", () => {
3457
+ void this.confirmDeleteFeedback(entry.id);
3458
+ });
3459
+ btnGroup.appendChild(cancelDeleteBtn);
3460
+ btnGroup.appendChild(confirmDeleteBtn);
3461
+ confirmBar.appendChild(confirmLabel);
3462
+ confirmBar.appendChild(btnGroup);
3463
+ msgCard.appendChild(confirmBar);
3464
+ }
2793
3465
  this.chatThreadEl.appendChild(msgCard);
2794
3466
  }
2795
3467
  this.chatThreadEl.scrollTop = this.chatThreadEl.scrollHeight;
@@ -3113,11 +3785,19 @@ export class ContextDropWidgetElement extends BaseHTMLElement {
3113
3785
  if (this.isSubmitting)
3114
3786
  return;
3115
3787
  const smartMode = this.isSmartSelectionMode();
3788
+ const editingId = this.editingFeedbackId;
3789
+ const commentingId = this.commentingFeedbackId;
3116
3790
  if (this.pendingSmartCaptures.size > 0) {
3117
3791
  await Promise.allSettled(Array.from(this.pendingSmartCaptures));
3118
3792
  }
3119
3793
  const message = (this.textareaEl?.value || "").trim();
3120
- if (message.length === 0 &&
3794
+ if (commentingId !== null) {
3795
+ if (message.length === 0 && this.attachments.length === 0) {
3796
+ this.showStatus("Write a comment or attach an image first.", "error");
3797
+ return;
3798
+ }
3799
+ }
3800
+ else if (message.length === 0 &&
3121
3801
  this.attachments.length === 0 &&
3122
3802
  this.elementReferences.length === 0) {
3123
3803
  this.showStatus(smartMode
@@ -3128,30 +3808,144 @@ export class ContextDropWidgetElement extends BaseHTMLElement {
3128
3808
  this.isSubmitting = true;
3129
3809
  if (this.submitBtn) {
3130
3810
  this.submitBtn.disabled = true;
3131
- this.submitBtn.innerHTML = `<span>Sending...</span>`;
3811
+ this.submitBtn.innerHTML =
3812
+ editingId !== null
3813
+ ? `<span>Saving...</span>`
3814
+ : commentingId !== null
3815
+ ? `<span>Posting...</span>`
3816
+ : `<span>Sending...</span>`;
3132
3817
  }
3133
3818
  try {
3134
- const submittedElements = [...this.elementReferences];
3819
+ const submittedElements = structuredClone(this.elementReferences);
3135
3820
  const submittedAttachments = this.attachments.map((att) => ({
3136
3821
  id: att.id,
3822
+ server_id: att.existing_attachment_id,
3137
3823
  label: att.label,
3138
3824
  preview_url: att.preview_url,
3139
- attachment_type: att.attachment_type
3825
+ attachment_type: att.attachment_type,
3826
+ filename: att.filename,
3827
+ signed_blob_id: att.signed_blob_id,
3828
+ data_url: att.data_url,
3829
+ element_reference_ref: att.element_reference_ref || null,
3830
+ metadata: att.metadata ? structuredClone(att.metadata) : {}
3140
3831
  }));
3832
+ const commonMetadata = {
3833
+ ...this.customMetadata,
3834
+ environment: this.config.environment,
3835
+ selection_mode: smartMode ? "smart" : "verbose",
3836
+ viewport: {
3837
+ width: typeof window !== "undefined" ? window.innerWidth : 0,
3838
+ height: typeof window !== "undefined" ? window.innerHeight : 0
3839
+ },
3840
+ locale: typeof navigator !== "undefined" ? navigator.language : "en"
3841
+ };
3842
+ if (commentingId !== null) {
3843
+ const existingIdx = this.sessionHistory.findIndex((item) => String(item.id) === commentingId);
3844
+ const existingEntry = existingIdx >= 0 ? this.sessionHistory[existingIdx] : undefined;
3845
+ const commentPayload = {
3846
+ body: message,
3847
+ attachments: this.attachments.map((att) => ({
3848
+ attachment_type: att.attachment_type,
3849
+ filename: att.filename,
3850
+ signed_blob_id: att.signed_blob_id,
3851
+ data_url: att.signed_blob_id ? undefined : att.data_url,
3852
+ metadata: att.metadata
3853
+ }))
3854
+ };
3855
+ const commentRes = await this.apiClient.createComment(existingEntry?.id ?? commentingId, commentPayload, existingEntry?.ownershipToken);
3856
+ if (commentRes?.feedback) {
3857
+ const updatedEntry = this.mapOwnRecordToSessionEntry(commentRes.feedback, existingEntry);
3858
+ if (existingIdx >= 0) {
3859
+ this.sessionHistory[existingIdx] = updatedEntry;
3860
+ }
3861
+ }
3862
+ else if (existingEntry) {
3863
+ existingEntry.status = commentRes?.status || "updated";
3864
+ if (!existingEntry.comments)
3865
+ existingEntry.comments = [];
3866
+ existingEntry.comments.push({
3867
+ id: commentRes?.id ?? Date.now(),
3868
+ author_label: commentRes?.comment?.author_label || "You",
3869
+ body: message || null,
3870
+ createdAt: this.formatTimestampLabel(commentRes?.created_at),
3871
+ attachments: submittedAttachments
3872
+ });
3873
+ }
3874
+ this.cancelCommentingFeedback();
3875
+ this.showStatus("Comment added.", "success");
3876
+ this.emitWidgetEvent("context-drop:comment", {
3877
+ response: commentRes,
3878
+ payload: commentPayload
3879
+ });
3880
+ return;
3881
+ }
3882
+ if (editingId !== null) {
3883
+ const existingIdx = this.sessionHistory.findIndex((item) => String(item.id) === editingId);
3884
+ const existingEntry = existingIdx >= 0 ? this.sessionHistory[existingIdx] : undefined;
3885
+ const updatePayload = {
3886
+ message,
3887
+ metadata: commonMetadata,
3888
+ element_references: this.elementReferences,
3889
+ attachments: this.attachments.map((att) => {
3890
+ if (att.existing_attachment_id !== undefined && att.existing_attachment_id !== null) {
3891
+ return {
3892
+ existing_id: att.existing_attachment_id,
3893
+ attachment_type: att.attachment_type,
3894
+ element_reference_ref: att.element_reference_ref,
3895
+ metadata: att.metadata
3896
+ };
3897
+ }
3898
+ return {
3899
+ attachment_type: att.attachment_type,
3900
+ filename: att.filename,
3901
+ signed_blob_id: att.signed_blob_id,
3902
+ data_url: att.signed_blob_id ? undefined : att.data_url,
3903
+ element_reference_ref: att.element_reference_ref,
3904
+ metadata: att.metadata
3905
+ };
3906
+ })
3907
+ };
3908
+ const updateRes = await this.apiClient.updateFeedback(existingEntry?.id ?? editingId, updatePayload, existingEntry?.ownershipToken);
3909
+ const mergedPreviewStub = {
3910
+ id: existingEntry?.id ?? updateRes?.id ?? editingId,
3911
+ message: message || (smartMode ? "(Highlighted page area)" : "(Context drop without text)"),
3912
+ rawMessage: message,
3913
+ createdAt: existingEntry?.createdAt || this.formatTimestampLabel(updateRes?.created_at),
3914
+ rawCreatedAt: existingEntry?.rawCreatedAt || updateRes?.created_at,
3915
+ rawUpdatedAt: updateRes?.updated_at || new Date().toISOString(),
3916
+ isEdited: true,
3917
+ status: updateRes?.status || existingEntry?.status,
3918
+ ownershipToken: updateRes?.ownership_token ||
3919
+ updateRes?.feedback?.ownership_token ||
3920
+ existingEntry?.ownershipToken,
3921
+ elements: submittedElements,
3922
+ attachments: submittedAttachments
3923
+ };
3924
+ const updatedEntry = updateRes?.feedback
3925
+ ? {
3926
+ ...this.mapOwnRecordToSessionEntry(updateRes.feedback, mergedPreviewStub),
3927
+ isEdited: true
3928
+ }
3929
+ : mergedPreviewStub;
3930
+ if (existingIdx >= 0) {
3931
+ this.sessionHistory[existingIdx] = updatedEntry;
3932
+ }
3933
+ else {
3934
+ this.sessionHistory.push(updatedEntry);
3935
+ }
3936
+ this.cancelEditingFeedback();
3937
+ this.showStatus(smartMode ? "Your feedback was updated." : "Feedback updated.", "success");
3938
+ this.emitWidgetEvent("context-drop:update", {
3939
+ response: updateRes,
3940
+ payload: updatePayload
3941
+ });
3942
+ return;
3943
+ }
3141
3944
  const payload = {
3142
3945
  message,
3143
3946
  page_url: typeof window !== "undefined" ? window.location.href : "/",
3144
3947
  user_agent: typeof navigator !== "undefined" ? navigator.userAgent : "",
3145
- metadata: {
3146
- ...this.customMetadata,
3147
- environment: this.config.environment,
3148
- selection_mode: smartMode ? "smart" : "verbose",
3149
- viewport: {
3150
- width: typeof window !== "undefined" ? window.innerWidth : 0,
3151
- height: typeof window !== "undefined" ? window.innerHeight : 0
3152
- },
3153
- locale: typeof navigator !== "undefined" ? navigator.language : "en"
3154
- },
3948
+ metadata: commonMetadata,
3155
3949
  element_references: this.elementReferences,
3156
3950
  attachments: this.attachments.map((att) => ({
3157
3951
  attachment_type: att.attachment_type,
@@ -3165,13 +3959,23 @@ export class ContextDropWidgetElement extends BaseHTMLElement {
3165
3959
  const res = await this.apiClient.submitFeedback(payload);
3166
3960
  const now = new Date();
3167
3961
  const timeLabel = `${String(now.getHours()).padStart(2, "0")}:${String(now.getMinutes()).padStart(2, "0")}`;
3168
- this.sessionHistory.push({
3962
+ const localStub = {
3169
3963
  id: res?.id ?? this.sessionHistory.length + 1,
3170
3964
  message: message || (smartMode ? "(Highlighted page area)" : "(Context drop without text)"),
3965
+ rawMessage: message,
3171
3966
  createdAt: timeLabel,
3967
+ rawCreatedAt: res?.created_at,
3968
+ rawUpdatedAt: res?.updated_at || res?.created_at,
3969
+ isEdited: false,
3970
+ status: res?.status,
3971
+ ownershipToken: res?.ownership_token || res?.feedback?.ownership_token,
3172
3972
  elements: submittedElements,
3173
3973
  attachments: submittedAttachments
3174
- });
3974
+ };
3975
+ const createdEntry = res?.feedback
3976
+ ? this.mapOwnRecordToSessionEntry(res.feedback, localStub)
3977
+ : localStub;
3978
+ this.sessionHistory.push(createdEntry);
3175
3979
  if (this.textareaEl) {
3176
3980
  this.textareaEl.value = "";
3177
3981
  }
@@ -3189,7 +3993,11 @@ export class ContextDropWidgetElement extends BaseHTMLElement {
3189
3993
  });
3190
3994
  }
3191
3995
  catch (err) {
3192
- const msg = err instanceof Error ? err.message : "Failed to submit feedback.";
3996
+ const msg = err instanceof Error
3997
+ ? err.message
3998
+ : editingId !== null
3999
+ ? "Failed to update feedback."
4000
+ : "Failed to submit feedback.";
3193
4001
  this.showStatus(msg, "error");
3194
4002
  this.emitWidgetEvent("context-drop:error", {
3195
4003
  error: msg
@@ -3199,8 +4007,16 @@ export class ContextDropWidgetElement extends BaseHTMLElement {
3199
4007
  this.isSubmitting = false;
3200
4008
  if (this.submitBtn) {
3201
4009
  this.submitBtn.disabled = false;
3202
- this.submitBtn.innerHTML = `${ICON_SEND}<span>Send</span>`;
4010
+ if (this.editingFeedbackId !== null) {
4011
+ this.submitBtn.setAttribute("aria-label", "Save feedback changes");
4012
+ this.submitBtn.innerHTML = `${ICON_CHECK}<span>Save</span>`;
4013
+ }
4014
+ else {
4015
+ this.submitBtn.setAttribute("aria-label", "Submit feedback");
4016
+ this.submitBtn.innerHTML = `${ICON_SEND}<span>Send</span>`;
4017
+ }
3203
4018
  }
4019
+ this.renderChatThread();
3204
4020
  }
3205
4021
  }
3206
4022
  hideStatus() {