@firstpick/pi-package-webui 0.11.0 → 0.11.2
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/DEVELOPMENT.md +181 -12
- package/README.md +41 -9
- package/TECHNICAL.md +91 -8
- package/bin/pi-webui.mjs +5432 -11428
- package/index.ts +10 -3
- package/lib/codex-cli-usage.mjs +232 -0
- package/lib/codex-usage-auth.mjs +39 -3
- package/lib/codex-usage-source.mjs +36 -0
- package/lib/component-update-state.mjs +2 -2
- package/lib/git-workflow-preference-values.mjs +122 -0
- package/lib/git-workflow-preferences.mjs +35 -563
- package/lib/package-updates.mjs +260 -0
- package/lib/rpc-supervisor-client.mjs +8 -4
- package/lib/server/app-runners.mjs +1667 -0
- package/lib/server/codex-fast-mode.mjs +161 -0
- package/lib/server/conversation-mode.mjs +218 -0
- package/lib/server/conversation-voice.mjs +318 -0
- package/lib/server/extension-ui-requests.mjs +114 -0
- package/lib/server/git-generation.mjs +459 -0
- package/lib/server/git-mutations.mjs +1827 -0
- package/lib/server/git-reads.mjs +608 -0
- package/lib/server/git-worktree-sessions.mjs +408 -0
- package/lib/server/helper-requests.mjs +82 -0
- package/lib/server/no-project-workspaces.mjs +103 -0
- package/lib/server/rpc-transports.mjs +394 -0
- package/lib/server/settings-store.mjs +544 -0
- package/lib/server/sse-delivery.mjs +138 -0
- package/lib/server/workspace-files.mjs +563 -0
- package/lib/update/supervisor.mjs +4 -0
- package/lib/webui-workspaces.mjs +2 -0
- package/package.json +5 -5
- package/public/activity-agents-controller.mjs +269 -0
- package/public/activity-events-controller.mjs +236 -0
- package/public/activity-intercom-controller.mjs +627 -0
- package/public/activity-queue-controller.mjs +534 -0
- package/public/activity-todo-view.mjs +89 -0
- package/public/activity-usage-controller.mjs +589 -0
- package/public/app.js +3761 -3878
- package/public/conversation-overview.css +84 -0
- package/public/conversation-overview.mjs +193 -0
- package/public/draft-conversations.mjs +135 -0
- package/public/focused-composer-controls.css +111 -0
- package/public/focused-composer-controls.mjs +143 -0
- package/public/focused-composer-disclosure.mjs +223 -0
- package/public/focused-shell-adapter.css +264 -0
- package/public/focused-shell-adapter.mjs +257 -0
- package/public/index.html +137 -80
- package/public/model-catalog-order.mjs +63 -0
- package/public/platform-event-stream.mjs +64 -0
- package/public/project-artifact-controller.mjs +218 -0
- package/public/project-file-viewer-controller.mjs +383 -0
- package/public/project-git-tools-controller.mjs +181 -0
- package/public/project-runners-controller.mjs +220 -0
- package/public/service-worker.js +52 -1
- package/public/session-attention-scheduler.mjs +182 -0
- package/public/session-composer-store.mjs +56 -0
- package/public/session-context.mjs +51 -0
- package/public/session-extension-requests.mjs +63 -0
- package/public/session-shelf-state.mjs +151 -0
- package/public/session-shelves.css +171 -0
- package/public/session-shelves.mjs +95 -0
- package/public/settings-navigation-controller.mjs +249 -0
- package/public/settings-navigation.css +61 -0
- package/public/settings-resource-controller.mjs +232 -0
- package/public/settings-workspace.css +19 -0
- package/public/settings-workspace.mjs +175 -0
- package/public/shell-actions.mjs +87 -0
- package/public/shell-attention-inbox.css +72 -0
- package/public/shell-attention-inbox.mjs +198 -0
- package/public/shell-context-actions.css +61 -0
- package/public/shell-context-actions.mjs +168 -0
- package/public/shell-inspector-resize.mjs +124 -0
- package/public/shell-navigation-resize.mjs +107 -0
- package/public/shell-navigation.mjs +127 -0
- package/public/shell-preferences.mjs +108 -0
- package/public/shell-surfaces.mjs +99 -0
- package/public/shell-tools-launcher.css +97 -0
- package/public/shell-tools-launcher.mjs +222 -0
- package/public/shell-view-host.mjs +115 -0
- package/public/shell-workspace.css +266 -0
- package/public/shell-workspace.mjs +402 -0
- package/public/sidebar-update-menu.css +190 -0
- package/public/sidebar-update-menu.mjs +512 -0
- package/public/styles.css +320 -169
- package/public/subagent-launch-slot-state.mjs +2 -2
- package/public/theme-contract.mjs +1 -1
- package/public/themed-selects.css +156 -0
- package/public/workspace-choices.mjs +104 -0
- package/tests/activity-agents-controller.test.mjs +652 -0
- package/tests/activity-events-controller.test.mjs +511 -0
- package/tests/app-runner-terminal-static.test.mjs +3 -3
- package/tests/append-system-http.test.mjs +7 -6
- package/tests/append-system-selector-static.test.mjs +6 -6
- package/tests/aur-review-widget-static.test.mjs +11 -3
- package/tests/boot-failure-diagnostics.test.mjs +8 -3
- package/tests/browser/activity-intercom-controller.spec.mjs +457 -0
- package/tests/browser/activity-navigation-integration.spec.mjs +45 -0
- package/tests/browser/activity-owner-integration.spec.mjs +201 -0
- package/tests/browser/activity-queue-controller.spec.mjs +405 -0
- package/tests/browser/activity-section-picker.spec.mjs +294 -0
- package/tests/browser/activity-todo-view.spec.mjs +60 -0
- package/tests/browser/activity-usage-controller.spec.mjs +416 -0
- package/tests/browser/compact-layout-integration.spec.mjs +58 -0
- package/tests/browser/composer-action-grid-reorder.spec.mjs +16 -0
- package/tests/browser/composer-disclosure.spec.mjs +389 -0
- package/tests/browser/composer-toolbar-polish.spec.mjs +343 -0
- package/tests/browser/control-deck-component-updates.spec.mjs +5 -14
- package/tests/browser/control-deck-side-panels.spec.mjs +27 -12
- package/tests/browser/control-visibility.spec.mjs +5 -0
- package/tests/browser/controls-layout.spec.mjs +21 -2
- package/tests/browser/conversation-overview.spec.mjs +251 -0
- package/tests/browser/draft-conversations.spec.mjs +276 -0
- package/tests/browser/empty-startup-fixture.mjs +135 -0
- package/tests/browser/empty-startup.spec.mjs +282 -0
- package/tests/browser/events-tree-context-menu.spec.mjs +1 -0
- package/tests/browser/focused-composer-more.spec.mjs +127 -0
- package/tests/browser/focused-model-shortcuts.spec.mjs +120 -0
- package/tests/browser/focused-navigation-fixture.mjs +32 -0
- package/tests/browser/focused-project-activity.spec.mjs +111 -0
- package/tests/browser/focused-shell-integration.spec.mjs +208 -0
- package/tests/browser/folder-picker-layout-fixture.mjs +13 -0
- package/tests/browser/folder-picker-layout.spec.mjs +302 -0
- package/tests/browser/footer-model-advanced-layout.spec.mjs +27 -14
- package/tests/browser/git-setup-cancel.spec.mjs +251 -0
- package/tests/browser/guided-git-fallback.spec.mjs +40 -21
- package/tests/browser/inspector-header.spec.mjs +221 -0
- package/tests/browser/inspector-overview-integration.spec.mjs +104 -0
- package/tests/browser/inspector-resize.spec.mjs +227 -0
- package/tests/browser/inspector-topic-overflow.spec.mjs +56 -0
- package/tests/browser/interaction-action-discovery.spec.mjs +332 -0
- package/tests/browser/interaction-attention-inbox.spec.mjs +364 -0
- package/tests/browser/interaction-attention-integration.spec.mjs +53 -0
- package/tests/browser/interaction-composer-controls.spec.mjs +267 -0
- package/tests/browser/interaction-composer-integration.spec.mjs +175 -0
- package/tests/browser/interaction-continuity.spec.mjs +50 -26
- package/tests/browser/interaction-model-picker.spec.mjs +86 -0
- package/tests/browser/last-terminal-close.spec.mjs +280 -0
- package/tests/browser/mobile-foundation.spec.mjs +172 -20
- package/tests/browser/model-catalog-groups.spec.mjs +104 -0
- package/tests/browser/more-row-menus.spec.mjs +258 -0
- package/tests/browser/new-tab-hit-target.spec.mjs +228 -0
- package/tests/browser/no-project-conversation.spec.mjs +113 -0
- package/tests/browser/optimistic-prompt.spec.mjs +3 -0
- package/tests/browser/overhaul-w7-accessibility.spec.mjs +227 -0
- package/tests/browser/overhaul-w7-capabilities.spec.mjs +298 -0
- package/tests/browser/overhaul-w7-companions.spec.mjs +290 -0
- package/tests/browser/overhaul-w7-focus-repairs.spec.mjs +210 -0
- package/tests/browser/overhaul-w7-layout-repairs.spec.mjs +183 -0
- package/tests/browser/overhaul-w7-parent-repairs.spec.mjs +97 -0
- package/tests/browser/overhaul-w7-pwa.spec.mjs +247 -0
- package/tests/browser/overhaul-w7-responsive.spec.mjs +450 -0
- package/tests/browser/overhaul-w7-update.spec.mjs +215 -0
- package/tests/browser/overhaul-w7-zoom.spec.mjs +243 -0
- package/tests/browser/overview-agent-ownership.spec.mjs +45 -0
- package/tests/browser/persistent-ui-layout.spec.mjs +3 -0
- package/tests/browser/project-artifact-controller.spec.mjs +324 -0
- package/tests/browser/project-artifact-integration.spec.mjs +103 -0
- package/tests/browser/project-file-buffer-integration.spec.mjs +230 -0
- package/tests/browser/project-git-tools-integration.spec.mjs +369 -0
- package/tests/browser/session-fixture.mjs +118 -0
- package/tests/browser/session-ownership.spec.mjs +175 -0
- package/tests/browser/session-shelves-integration.spec.mjs +219 -0
- package/tests/browser/session-shelves.spec.mjs +340 -0
- package/tests/browser/session-sidebar-resize.spec.mjs +138 -0
- package/tests/browser/session-summary.spec.mjs +17 -12
- package/tests/browser/settings-complete-home.spec.mjs +463 -0
- package/tests/browser/settings-entrypoint.spec.mjs +104 -0
- package/tests/browser/settings-navigation-controller.spec.mjs +383 -0
- package/tests/browser/settings-navigation-integration.spec.mjs +348 -0
- package/tests/browser/settings-resource-lifetime.spec.mjs +349 -0
- package/tests/browser/settings-workspace-integration.spec.mjs +132 -0
- package/tests/browser/shell-ownership.spec.mjs +398 -0
- package/tests/browser/shell-rollout-fixture.mjs +259 -0
- package/tests/browser/shell-rollout.spec.mjs +357 -0
- package/tests/browser/shell-tools-launcher.spec.mjs +260 -0
- package/tests/browser/shell-workspace.spec.mjs +433 -0
- package/tests/browser/side-panel-section-reorder.spec.mjs +3 -1
- package/tests/browser/sidebar-floating-tooltips.spec.mjs +37 -0
- package/tests/browser/sidebar-update-integration.spec.mjs +277 -0
- package/tests/browser/sidebar-update-menu.spec.mjs +402 -0
- package/tests/browser/sidebar-update-progress.spec.mjs +472 -0
- package/tests/browser/stream-output-isolation.spec.mjs +6 -3
- package/tests/browser/themed-selects.spec.mjs +297 -0
- package/tests/browser/thread-start-flow.spec.mjs +481 -0
- package/tests/browser/ui-polish-activity-controls.spec.mjs +177 -0
- package/tests/browser/ui-polish-features.spec.mjs +85 -0
- package/tests/browser/ui-polish-file-header.spec.mjs +43 -0
- package/tests/browser/ui-polish-local-git.spec.mjs +183 -0
- package/tests/browser/ui-polish-mobile-recovery.spec.mjs +36 -0
- package/tests/browser/ui-polish-navigation.spec.mjs +127 -0
- package/tests/browser/ui-polish-session-actions.spec.mjs +282 -0
- package/tests/browser/user-noticeable-fixes.spec.mjs +10 -3
- package/tests/browser/windows-drive-parent.spec.mjs +4 -2
- package/tests/browser/workspace-draft-selection.spec.mjs +266 -0
- package/tests/codex-cli-usage.test.mjs +189 -0
- package/tests/codex-fast-mode-static.test.mjs +16 -11
- package/tests/codex-speed-mode-browser-state.test.mjs +6 -3
- package/tests/codex-usage-auth.test.mjs +168 -9
- package/tests/codex-usage-http.test.mjs +124 -0
- package/tests/codex-usage-source.test.mjs +146 -0
- package/tests/component-update-api-static.test.mjs +2 -2
- package/tests/composer-action-grid-reorder-static.test.mjs +3 -3
- package/tests/control-deck-component-updates-static.test.mjs +9 -4
- package/tests/control-visibility-static.test.mjs +3 -3
- package/tests/controls-layout-static.test.mjs +5 -5
- package/tests/conversation-overview-adapter.test.mjs +100 -0
- package/tests/document-artifact-static.test.mjs +2 -1
- package/tests/draft-conversations.test.mjs +205 -0
- package/tests/empty-startup-launcher.test.mjs +197 -0
- package/tests/events-tree-context-menu-static.test.mjs +22 -18
- package/tests/fake-pi-session-ownership.test.mjs +302 -0
- package/tests/fast-mode-client-static.test.mjs +1 -1
- package/tests/feature-decision-output-popup-static.test.mjs +2 -2
- package/tests/feature-system-prompt-optional-feature-static.test.mjs +1 -1
- package/tests/file-viewer-image-static.test.mjs +15 -11
- package/tests/file-viewer-search-static.test.mjs +5 -5
- package/tests/fixtures/fake-codex-app-server.mjs +61 -0
- package/tests/fixtures/fake-pi.mjs +41 -9
- package/tests/footer-provider-usage-visual-static.test.mjs +7 -6
- package/tests/foreground-transcript-catch-up-static.test.mjs +7 -4
- package/tests/git-footer-context-menu-static.test.mjs +1 -1
- package/tests/git-panel-file-preview-static.test.mjs +41 -48
- package/tests/git-panel-live-updates-static.test.mjs +2 -1
- package/tests/git-workflow-dispatch.test.mjs +66 -0
- package/tests/git-workflow-preferences.test.mjs +9 -1
- package/tests/guided-git-activation-static.test.mjs +14 -11
- package/tests/guided-git-fallback-ui-static.test.mjs +8 -5
- package/tests/guided-git-message-reliability-static.test.mjs +10 -6
- package/tests/guided-git-publication-backend-static.test.mjs +16 -11
- package/tests/http-endpoints-harness.test.mjs +29 -9
- package/tests/interaction-state-stability-static.test.mjs +4 -3
- package/tests/intercom-conversation-viewer-static.test.mjs +29 -17
- package/tests/intercom-tag-layout-static.test.mjs +13 -12
- package/tests/intercom-tag-overflow-layout.test.mjs +11 -10
- package/tests/main-output-loading-static.test.mjs +4 -4
- package/tests/middle-button-drag-scroll-static.test.mjs +3 -3
- package/tests/mobile-compact-layout.test.mjs +2 -2
- package/tests/mobile-continuity-tablet-static.test.mjs +3 -3
- package/tests/mobile-foundation-static.test.mjs +10 -7
- package/tests/mobile-phone-experience-static.test.mjs +3 -2
- package/tests/mobile-static.test.mjs +173 -135
- package/tests/model-catalog-order.test.mjs +103 -0
- package/tests/native-parity.test.mjs +6 -4
- package/tests/native-session-choice-owner.test.mjs +102 -0
- package/tests/native-update-lifecycle.test.mjs +10 -5
- package/tests/no-project-http-harness.test.mjs +328 -0
- package/tests/no-project-navigation.test.mjs +54 -0
- package/tests/no-project-workspaces.test.mjs +77 -0
- package/tests/open-issue-wizard-static.test.mjs +1 -1
- package/tests/optional-feature-migration-frontend.test.mjs +4 -4
- package/tests/optional-feature-prompt-impact-static.test.mjs +3 -3
- package/tests/package-updates.test.mjs +630 -0
- package/tests/persistent-ui-layout-static.test.mjs +3 -3
- package/tests/platform-event-stream.test.mjs +253 -0
- package/tests/project-file-viewer-controller.test.mjs +652 -0
- package/tests/project-git-tools-adapter.test.mjs +103 -0
- package/tests/project-git-tools-controller.test.mjs +577 -0
- package/tests/project-runners-controller.test.mjs +453 -0
- package/tests/prompt-input-vertical-scroll-static.test.mjs +3 -3
- package/tests/prompt-routing-input-clear-static.test.mjs +2 -2
- package/tests/questionnaire-dialog.test.mjs +6 -6
- package/tests/queue-edit-reorder-static.test.mjs +48 -51
- package/tests/rpc-supervisor-host.test.mjs +4 -1
- package/tests/safety-guard-auto-review-static.test.mjs +1 -1
- package/tests/sampling-native-controls-user-flow.test.mjs +7 -1
- package/tests/server-app-runners.test.mjs +536 -0
- package/tests/server-codex-fast-mode.test.mjs +346 -0
- package/tests/server-conversation-mode.test.mjs +386 -0
- package/tests/server-conversation-voice.test.mjs +426 -0
- package/tests/server-extension-ui-requests.test.mjs +357 -0
- package/tests/server-git-generation.test.mjs +558 -0
- package/tests/server-git-mutations.test.mjs +729 -0
- package/tests/server-git-reads.test.mjs +381 -0
- package/tests/server-git-worktree-sessions.test.mjs +502 -0
- package/tests/server-helper-requests.test.mjs +364 -0
- package/tests/server-rpc-transports.test.mjs +553 -0
- package/tests/server-settings-store.test.mjs +132 -0
- package/tests/server-shell-rollout.test.mjs +478 -0
- package/tests/server-sse-delivery.test.mjs +410 -0
- package/tests/server-workspace-files.test.mjs +546 -0
- package/tests/session-attention-scheduler.test.mjs +514 -0
- package/tests/session-composer-store.test.mjs +153 -0
- package/tests/session-context.test.mjs +134 -0
- package/tests/session-extension-requests.test.mjs +193 -0
- package/tests/session-ownership-adapter.test.mjs +130 -0
- package/tests/session-sampling-ui-static.test.mjs +10 -4
- package/tests/session-send-lifetime.test.mjs +140 -0
- package/tests/session-shelf-state.test.mjs +348 -0
- package/tests/session-shelves-adapter.test.mjs +79 -0
- package/tests/session-summary-lifetime.test.mjs +54 -0
- package/tests/settings-resource-controller.test.mjs +548 -0
- package/tests/shell-action-adapter.test.mjs +169 -0
- package/tests/shell-actions.test.mjs +182 -0
- package/tests/shell-navigation.test.mjs +348 -0
- package/tests/shell-preferences.test.mjs +367 -0
- package/tests/side-panel-section-height-removal-static.test.mjs +3 -3
- package/tests/stream-output-isolation-static.test.mjs +1 -1
- package/tests/subagent-gate-visibility.test.mjs +1 -1
- package/tests/subagent-launch-slot-state.test.mjs +2 -2
- package/tests/subagent-launch-slots-static.test.mjs +1 -1
- package/tests/subagent-observability-static.test.mjs +2 -2
- package/tests/subagent-reliability-integration.test.mjs +13 -30
- package/tests/syntax-highlighting-static.test.mjs +3 -3
- package/tests/tab-close-continuity-static.test.mjs +1 -1
- package/tests/tab-metadata-ordering.test.mjs +99 -0
- package/tests/terminal-cwd-grouping-static.test.mjs +2 -2
- package/tests/terminal-tab-prompt-tooltip-static.test.mjs +2 -0
- package/tests/terminal-tab-workspace-static.test.mjs +5 -4
- package/tests/terminal-tabs-resize-static.test.mjs +1 -1
- package/tests/theme-contract.test.mjs +10 -3
- package/tests/theme-customizer-static.test.mjs +1 -1
- package/tests/thread-display-title.test.mjs +44 -0
- package/tests/update-api-harness.test.mjs +15 -14
- package/tests/update-tab-restore-harness.test.mjs +3 -0
- package/tests/webui-settings-locking.test.mjs +2 -2
- package/tests/windows-drive-cwd-picker-static.test.mjs +7 -9
- package/tests/workspace-choices.test.mjs +114 -0
- package/tests/workspace-save-load-static.test.mjs +7 -4
|
@@ -0,0 +1,627 @@
|
|
|
1
|
+
const INTERCOM_CONVERSATION_ID_PATTERN = /^conv_[A-Za-z0-9_-]{24}$/;
|
|
2
|
+
const INTERCOM_MESSAGE_ID_PATTERN = /^msg_[A-Za-z0-9_-]{24}$/;
|
|
3
|
+
const INTERCOM_DETAIL_REFRESH_MS = 5_000;
|
|
4
|
+
|
|
5
|
+
function intercomBoundedString(value, maxLength = 240) {
|
|
6
|
+
if (typeof value !== "string") return "";
|
|
7
|
+
return value.trim().slice(0, maxLength);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function normalizeIntercomParticipant(value, fallback) {
|
|
11
|
+
const participant = value && typeof value === "object" && !Array.isArray(value) ? value : {};
|
|
12
|
+
const id = intercomBoundedString(participant.id, 240);
|
|
13
|
+
const name = intercomBoundedString(participant.name, 160);
|
|
14
|
+
return { id: id || null, name: name || id || fallback };
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function intercomParticipantLabel(participant, fallback = "Agent") {
|
|
18
|
+
const normalized = normalizeIntercomParticipant(participant, fallback);
|
|
19
|
+
return normalized.id && normalized.id !== normalized.name ? `${normalized.name} (${normalized.id})` : normalized.name;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function normalizeIntercomConversationSummary(value) {
|
|
23
|
+
if (!value || typeof value !== "object" || !INTERCOM_CONVERSATION_ID_PATTERN.test(String(value.id || ""))) return null;
|
|
24
|
+
const participants = value.participants && typeof value.participants === "object" ? value.participants : {};
|
|
25
|
+
const messageCount = Number(value.messageCount);
|
|
26
|
+
return Object.freeze({
|
|
27
|
+
id: String(value.id),
|
|
28
|
+
participants: Object.freeze({
|
|
29
|
+
local: Object.freeze(normalizeIntercomParticipant(participants.local, "Local agent")),
|
|
30
|
+
peer: Object.freeze(normalizeIntercomParticipant(participants.peer, "Peer agent")),
|
|
31
|
+
}),
|
|
32
|
+
messageCount: Number.isSafeInteger(messageCount) && messageCount >= 0 ? messageCount : 0,
|
|
33
|
+
lastMessageAt: typeof value.lastMessageAt === "string" ? value.lastMessageAt : null,
|
|
34
|
+
truncatedBefore: value.truncatedBefore === true,
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function normalizeIntercomConversationDetail(value) {
|
|
39
|
+
const summary = normalizeIntercomConversationSummary(value);
|
|
40
|
+
if (!summary) return null;
|
|
41
|
+
const messages = [];
|
|
42
|
+
const seenMessageIds = new Set();
|
|
43
|
+
for (const candidate of Array.isArray(value.messages) ? value.messages.slice(-200) : []) {
|
|
44
|
+
const id = String(candidate?.id || "");
|
|
45
|
+
if (!candidate || typeof candidate !== "object" || !INTERCOM_MESSAGE_ID_PATTERN.test(id) || seenMessageIds.has(id)
|
|
46
|
+
|| !["local", "peer"].includes(candidate.direction) || typeof candidate.text !== "string") continue;
|
|
47
|
+
seenMessageIds.add(id);
|
|
48
|
+
const timestamp = typeof candidate.timestamp === "string" && Number.isFinite(Date.parse(candidate.timestamp)) ? candidate.timestamp : null;
|
|
49
|
+
messages.push({
|
|
50
|
+
id,
|
|
51
|
+
direction: candidate.direction,
|
|
52
|
+
text: candidate.text.slice(0, 65_536),
|
|
53
|
+
timestamp,
|
|
54
|
+
truncatedText: candidate.truncatedText === true,
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
return { ...summary, messages };
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function intercomConversationLabel(conversation) {
|
|
61
|
+
const local = intercomParticipantLabel(conversation?.participants?.local, "Local agent");
|
|
62
|
+
const peer = intercomParticipantLabel(conversation?.participants?.peer, "Peer agent");
|
|
63
|
+
return `${local} ↔ ${peer}`;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function intercomConversationSummarySignature(conversations) {
|
|
67
|
+
return JSON.stringify((conversations || []).map((conversation) => [
|
|
68
|
+
conversation.id,
|
|
69
|
+
intercomConversationLabel(conversation),
|
|
70
|
+
conversation.messageCount,
|
|
71
|
+
conversation.lastMessageAt,
|
|
72
|
+
conversation.truncatedBefore,
|
|
73
|
+
]));
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** Owns read-only Intercom summaries, detail rendering and polling for one foreground context. */
|
|
77
|
+
export function createActivityIntercomController({
|
|
78
|
+
document, nodes, api, getContext, isCurrent,
|
|
79
|
+
onEvent = () => {}, onChange = () => {},
|
|
80
|
+
onDialogOpen, onDialogClose,
|
|
81
|
+
setTimeout = document.defaultView.setTimeout.bind(document.defaultView),
|
|
82
|
+
clearTimeout = document.defaultView.clearTimeout.bind(document.defaultView),
|
|
83
|
+
requestAnimationFrame = document.defaultView.requestAnimationFrame.bind(document.defaultView),
|
|
84
|
+
cancelAnimationFrame = document.defaultView.cancelAnimationFrame.bind(document.defaultView),
|
|
85
|
+
ResizeObserver = document.defaultView.ResizeObserver,
|
|
86
|
+
}) {
|
|
87
|
+
const window = document.defaultView;
|
|
88
|
+
const intercomConversationSummariesByTab = new Map();
|
|
89
|
+
const intercomMessageRenderSignatures = new WeakMap();
|
|
90
|
+
const emptySummaries = Object.freeze([]);
|
|
91
|
+
const listeners = [];
|
|
92
|
+
let disposed = false;
|
|
93
|
+
let installed = false;
|
|
94
|
+
let intercomSummaryRequestSerial = 0;
|
|
95
|
+
let intercomSummaryRefreshTimer = null;
|
|
96
|
+
let intercomDetailRequestSerial = 0;
|
|
97
|
+
let intercomDetailRefreshTimer = null;
|
|
98
|
+
let intercomConversationDialogState = null;
|
|
99
|
+
let intercomConversationTagLayoutFrame = null;
|
|
100
|
+
let intercomConversationTagResizeObserver = null;
|
|
101
|
+
let intercomConversationOverflowOpen = false;
|
|
102
|
+
let detailFrame = null;
|
|
103
|
+
|
|
104
|
+
function make(tag, className, text) {
|
|
105
|
+
const element = document.createElement(tag);
|
|
106
|
+
element.className = className;
|
|
107
|
+
if (text !== undefined) element.textContent = text;
|
|
108
|
+
return element;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function listen(target, type, listener, options) {
|
|
112
|
+
target.addEventListener(type, listener, options);
|
|
113
|
+
listeners.push(() => target.removeEventListener(type, listener, options));
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function current(context) {
|
|
117
|
+
return !disposed && Boolean(context?.tabId) && isCurrent(context);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function detailCurrent(state) {
|
|
121
|
+
return Boolean(state && state === intercomConversationDialogState && nodes.intercomConversationDialog?.open && current(state));
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function setOverflowOpen(open, { focusMenu = false, restoreFocus = false } = {}) {
|
|
125
|
+
if (disposed) return;
|
|
126
|
+
const container = nodes.intercomConversationTags;
|
|
127
|
+
const overflow = container?.querySelector("button.composer-intercom-tag.overflow");
|
|
128
|
+
const menu = container?.querySelector(".composer-intercom-overflow-menu");
|
|
129
|
+
if (!container || !overflow || !menu) {
|
|
130
|
+
intercomConversationOverflowOpen = false;
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const availableItems = [...menu.querySelectorAll("button:not([hidden])")];
|
|
135
|
+
const expanded = Boolean(open && !overflow.hidden && availableItems.length);
|
|
136
|
+
intercomConversationOverflowOpen = expanded;
|
|
137
|
+
container.classList.toggle("overflow-open", expanded);
|
|
138
|
+
overflow.setAttribute("aria-expanded", expanded ? "true" : "false");
|
|
139
|
+
menu.hidden = !expanded;
|
|
140
|
+
|
|
141
|
+
if (expanded && focusMenu) availableItems[0]?.focus();
|
|
142
|
+
else if (!expanded && restoreFocus) overflow.focus();
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function fitTags() {
|
|
146
|
+
const container = nodes.intercomConversationTags;
|
|
147
|
+
if (disposed || !container || container.hidden) return;
|
|
148
|
+
const tags = [...container.querySelectorAll(":scope > button.composer-intercom-tag:not(.overflow)")];
|
|
149
|
+
const overflow = container.querySelector("button.composer-intercom-tag.overflow");
|
|
150
|
+
const menuItems = [...container.querySelectorAll(".composer-intercom-overflow-menu-item")];
|
|
151
|
+
if (!tags.length || !overflow || menuItems.length !== tags.length) return;
|
|
152
|
+
|
|
153
|
+
const availableWidth = container.clientWidth;
|
|
154
|
+
if (availableWidth <= 0) return;
|
|
155
|
+
|
|
156
|
+
for (const tag of tags) tag.hidden = false;
|
|
157
|
+
overflow.hidden = false;
|
|
158
|
+
|
|
159
|
+
const containerStyle = window.getComputedStyle(container);
|
|
160
|
+
const gap = Number.parseFloat(containerStyle.columnGap || containerStyle.gap) || 0;
|
|
161
|
+
const tagWidths = tags.map((tag) => tag.getBoundingClientRect().width);
|
|
162
|
+
const overflowWidths = new Map();
|
|
163
|
+
const maxOverflowDigits = String(tags.length).length;
|
|
164
|
+
for (let digits = 1; digits <= maxOverflowDigits; digits += 1) {
|
|
165
|
+
overflow.textContent = `+${"0".repeat(digits)}`;
|
|
166
|
+
overflowWidths.set(digits, overflow.getBoundingClientRect().width);
|
|
167
|
+
}
|
|
168
|
+
let visibleCount = 0;
|
|
169
|
+
|
|
170
|
+
for (let count = tags.length; count >= 0; count -= 1) {
|
|
171
|
+
const hiddenCount = tags.length - count;
|
|
172
|
+
const itemCount = count + (hiddenCount ? 1 : 0);
|
|
173
|
+
const tagsWidth = tagWidths.slice(0, count).reduce((total, width) => total + width, 0);
|
|
174
|
+
const overflowWidth = hiddenCount ? overflowWidths.get(String(hiddenCount).length) || 0 : 0;
|
|
175
|
+
const requiredWidth = tagsWidth + overflowWidth + Math.max(0, itemCount - 1) * gap;
|
|
176
|
+
if (requiredWidth <= availableWidth + 0.5) {
|
|
177
|
+
visibleCount = count;
|
|
178
|
+
break;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
tags.forEach((tag, index) => { tag.hidden = index >= visibleCount; });
|
|
183
|
+
menuItems.forEach((item, index) => { item.hidden = index < visibleCount; });
|
|
184
|
+
const hiddenCount = tags.length - visibleCount;
|
|
185
|
+
overflow.hidden = hiddenCount === 0;
|
|
186
|
+
if (hiddenCount) {
|
|
187
|
+
const label = `Show ${hiddenCount} more agent conversation${hiddenCount === 1 ? "" : "s"}`;
|
|
188
|
+
overflow.textContent = `+${hiddenCount}`;
|
|
189
|
+
overflow.title = `${label}.`;
|
|
190
|
+
overflow.setAttribute("aria-label", label);
|
|
191
|
+
if (intercomConversationOverflowOpen) setOverflowOpen(true);
|
|
192
|
+
} else {
|
|
193
|
+
setOverflowOpen(false);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
function scheduleTagLayout() {
|
|
198
|
+
if (disposed || intercomConversationTagLayoutFrame !== null) return;
|
|
199
|
+
intercomConversationTagLayoutFrame = requestAnimationFrame(() => {
|
|
200
|
+
intercomConversationTagLayoutFrame = null;
|
|
201
|
+
fitTags();
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
function install() {
|
|
206
|
+
const container = nodes.intercomConversationTags;
|
|
207
|
+
if (disposed || installed || !container) return;
|
|
208
|
+
installed = true;
|
|
209
|
+
if (typeof ResizeObserver === "function") {
|
|
210
|
+
intercomConversationTagResizeObserver = new ResizeObserver(scheduleTagLayout);
|
|
211
|
+
intercomConversationTagResizeObserver.observe(container);
|
|
212
|
+
if (container.parentElement) intercomConversationTagResizeObserver.observe(container.parentElement);
|
|
213
|
+
} else {
|
|
214
|
+
listen(window, "resize", scheduleTagLayout, { passive: true });
|
|
215
|
+
}
|
|
216
|
+
listen(container, "click", (event) => {
|
|
217
|
+
const button = event.target.closest?.("button.composer-intercom-tag");
|
|
218
|
+
if (!button || !container.contains(button)) return;
|
|
219
|
+
if (button.classList.contains("overflow")) {
|
|
220
|
+
const opening = !intercomConversationOverflowOpen;
|
|
221
|
+
setOverflowOpen(opening, { focusMenu: opening });
|
|
222
|
+
} else {
|
|
223
|
+
if (button.classList.contains("composer-intercom-overflow-menu-item")) {
|
|
224
|
+
setOverflowOpen(false, { restoreFocus: true });
|
|
225
|
+
}
|
|
226
|
+
open(button.dataset.intercomConversationId);
|
|
227
|
+
}
|
|
228
|
+
});
|
|
229
|
+
listen(container, "keydown", (event) => {
|
|
230
|
+
if (event.key !== "Escape" || !intercomConversationOverflowOpen) return;
|
|
231
|
+
event.preventDefault();
|
|
232
|
+
event.stopPropagation();
|
|
233
|
+
setOverflowOpen(false, { restoreFocus: true });
|
|
234
|
+
});
|
|
235
|
+
listen(container, "focusout", () => {
|
|
236
|
+
window.queueMicrotask(() => {
|
|
237
|
+
if (!disposed && intercomConversationOverflowOpen && !container.contains(document.activeElement)) setOverflowOpen(false);
|
|
238
|
+
});
|
|
239
|
+
});
|
|
240
|
+
listen(document, "pointerdown", (event) => {
|
|
241
|
+
if (intercomConversationOverflowOpen && !container.contains(event.target)) setOverflowOpen(false);
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
function updateIntercomConversationTag(tag, conversation) {
|
|
246
|
+
const label = intercomConversationLabel(conversation);
|
|
247
|
+
tag.setAttribute("aria-label", `Open agent conversation ${label}`);
|
|
248
|
+
tag.title = `${label} · ${conversation.messageCount} message${conversation.messageCount === 1 ? "" : "s"}`;
|
|
249
|
+
const visualSignature = JSON.stringify([label, conversation.messageCount]);
|
|
250
|
+
if (tag.dataset.intercomVisualSignature === visualSignature) return;
|
|
251
|
+
const children = [make("span", "composer-intercom-tag-icon", "↔"), make("span", "composer-intercom-tag-label", label)];
|
|
252
|
+
if (conversation.messageCount > 0) children.push(make("span", "composer-intercom-tag-count", String(conversation.messageCount)));
|
|
253
|
+
tag.replaceChildren(...children);
|
|
254
|
+
tag.dataset.intercomVisualSignature = visualSignature;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
function renderTags(tabId = getContext().tabId) {
|
|
258
|
+
const container = nodes.intercomConversationTags;
|
|
259
|
+
if (disposed || !container) return;
|
|
260
|
+
const conversations = intercomConversationSummariesByTab.get(tabId) || [];
|
|
261
|
+
if (!conversations.length) {
|
|
262
|
+
intercomConversationOverflowOpen = false;
|
|
263
|
+
container.classList.remove("overflow-open");
|
|
264
|
+
container.replaceChildren();
|
|
265
|
+
container.hidden = true;
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
let overflow = container.querySelector("button.composer-intercom-tag.overflow");
|
|
270
|
+
if (!overflow) {
|
|
271
|
+
overflow = make("button", "composer-intercom-tag overflow", `+${conversations.length}`);
|
|
272
|
+
overflow.type = "button";
|
|
273
|
+
overflow.hidden = true;
|
|
274
|
+
overflow.setAttribute("aria-haspopup", "dialog");
|
|
275
|
+
overflow.setAttribute("aria-expanded", "false");
|
|
276
|
+
overflow.setAttribute("aria-controls", "intercomConversationOverflowMenu");
|
|
277
|
+
}
|
|
278
|
+
let menu = container.querySelector(".composer-intercom-overflow-menu");
|
|
279
|
+
if (!menu) {
|
|
280
|
+
menu = make("div", "composer-intercom-overflow-menu");
|
|
281
|
+
menu.id = "intercomConversationOverflowMenu";
|
|
282
|
+
menu.hidden = true;
|
|
283
|
+
menu.setAttribute("role", "dialog");
|
|
284
|
+
menu.setAttribute("aria-label", "More agent conversations");
|
|
285
|
+
}
|
|
286
|
+
if (overflow.parentElement !== container) container.append(overflow);
|
|
287
|
+
if (menu.parentElement !== container) container.append(menu);
|
|
288
|
+
|
|
289
|
+
const existingTags = new Map([...container.querySelectorAll(":scope > .composer-intercom-tag[data-intercom-conversation-id]")]
|
|
290
|
+
.map((tag) => [tag.dataset.intercomConversationId, tag]));
|
|
291
|
+
const existingMenuItems = new Map([...menu.querySelectorAll(".composer-intercom-overflow-menu-item[data-intercom-conversation-id]")]
|
|
292
|
+
.map((tag) => [tag.dataset.intercomConversationId, tag]));
|
|
293
|
+
const orderedTags = [];
|
|
294
|
+
const orderedMenuItems = [];
|
|
295
|
+
const seenConversationIds = new Set();
|
|
296
|
+
for (const conversation of conversations) {
|
|
297
|
+
if (seenConversationIds.has(conversation.id)) continue;
|
|
298
|
+
seenConversationIds.add(conversation.id);
|
|
299
|
+
let tag = existingTags.get(conversation.id);
|
|
300
|
+
if (tag) existingTags.delete(conversation.id);
|
|
301
|
+
else {
|
|
302
|
+
tag = make("button", "composer-intercom-tag");
|
|
303
|
+
tag.type = "button";
|
|
304
|
+
tag.dataset.intercomConversationId = conversation.id;
|
|
305
|
+
tag.setAttribute("aria-haspopup", "dialog");
|
|
306
|
+
tag.setAttribute("aria-controls", "intercomConversationDialog");
|
|
307
|
+
}
|
|
308
|
+
updateIntercomConversationTag(tag, conversation);
|
|
309
|
+
orderedTags.push(tag);
|
|
310
|
+
|
|
311
|
+
let menuItem = existingMenuItems.get(conversation.id);
|
|
312
|
+
if (menuItem) existingMenuItems.delete(conversation.id);
|
|
313
|
+
else {
|
|
314
|
+
menuItem = make("button", "composer-intercom-tag composer-intercom-overflow-menu-item");
|
|
315
|
+
menuItem.type = "button";
|
|
316
|
+
menuItem.dataset.intercomConversationId = conversation.id;
|
|
317
|
+
menuItem.setAttribute("aria-haspopup", "dialog");
|
|
318
|
+
menuItem.setAttribute("aria-controls", "intercomConversationDialog");
|
|
319
|
+
}
|
|
320
|
+
updateIntercomConversationTag(menuItem, conversation);
|
|
321
|
+
orderedMenuItems.push(menuItem);
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
for (const staleTag of existingTags.values()) staleTag.remove();
|
|
325
|
+
for (const staleItem of existingMenuItems.values()) staleItem.remove();
|
|
326
|
+
let tagCursor = container.firstElementChild;
|
|
327
|
+
for (const tag of orderedTags) {
|
|
328
|
+
if (tag === tagCursor) tagCursor = tagCursor.nextElementSibling;
|
|
329
|
+
else container.insertBefore(tag, tagCursor);
|
|
330
|
+
}
|
|
331
|
+
let menuCursor = menu.firstElementChild;
|
|
332
|
+
for (const menuItem of orderedMenuItems) {
|
|
333
|
+
if (menuItem === menuCursor) menuCursor = menuCursor.nextElementSibling;
|
|
334
|
+
else menu.insertBefore(menuItem, menuCursor);
|
|
335
|
+
}
|
|
336
|
+
container.hidden = false;
|
|
337
|
+
scheduleTagLayout();
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
function setStatus(message = "", level = "") {
|
|
341
|
+
const status = nodes.intercomConversationStatus;
|
|
342
|
+
if (!status) return;
|
|
343
|
+
status.textContent = message;
|
|
344
|
+
status.classList.toggle("error", level === "error");
|
|
345
|
+
status.classList.toggle("warn", level === "warn");
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
function formatIntercomMessageTime(value) {
|
|
349
|
+
const timestamp = Date.parse(value || "");
|
|
350
|
+
if (!Number.isFinite(timestamp)) return "";
|
|
351
|
+
return new Date(timestamp).toLocaleString([], { dateStyle: "medium", timeStyle: "short" });
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
function renderIntercomConversationMessage(article, message, conversation) {
|
|
355
|
+
const participant = message.direction === "local" ? conversation.participants.local : conversation.participants.peer;
|
|
356
|
+
const senderLabel = intercomParticipantLabel(participant, message.direction === "local" ? "Local agent" : "Peer agent");
|
|
357
|
+
const formattedTime = formatIntercomMessageTime(message.timestamp);
|
|
358
|
+
const signature = JSON.stringify([message.direction, senderLabel, message.text, message.timestamp, message.truncatedText]);
|
|
359
|
+
if (intercomMessageRenderSignatures.get(article) === signature) return;
|
|
360
|
+
|
|
361
|
+
article.className = `intercom-conversation-message ${message.direction}`;
|
|
362
|
+
article.dataset.intercomMessageId = message.id;
|
|
363
|
+
article.setAttribute("aria-label", `${senderLabel} message`);
|
|
364
|
+
const header = make("header", "intercom-conversation-message-header");
|
|
365
|
+
header.append(make("strong", "intercom-conversation-message-sender", senderLabel));
|
|
366
|
+
if (formattedTime) {
|
|
367
|
+
const time = make("time", "intercom-conversation-message-time", formattedTime);
|
|
368
|
+
time.dateTime = message.timestamp;
|
|
369
|
+
header.append(time);
|
|
370
|
+
}
|
|
371
|
+
const text = make("p", "intercom-conversation-message-text");
|
|
372
|
+
text.textContent = message.text;
|
|
373
|
+
const children = [header, text];
|
|
374
|
+
if (message.truncatedText) children.push(make("span", "intercom-conversation-truncated", "Message truncated"));
|
|
375
|
+
article.replaceChildren(...children);
|
|
376
|
+
intercomMessageRenderSignatures.set(article, signature);
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
function renderIntercomConversationDetail(conversation) {
|
|
380
|
+
const transcript = nodes.intercomConversationTranscript;
|
|
381
|
+
if (!transcript || !conversation) return;
|
|
382
|
+
const state = intercomConversationDialogState;
|
|
383
|
+
const signature = JSON.stringify([
|
|
384
|
+
conversation.id,
|
|
385
|
+
conversation.truncatedBefore,
|
|
386
|
+
intercomConversationLabel(conversation),
|
|
387
|
+
conversation.messages.map((message) => [message.id, message.direction, message.text, message.timestamp, message.truncatedText]),
|
|
388
|
+
]);
|
|
389
|
+
if (state?.detailSignature === signature) return false;
|
|
390
|
+
if (state) state.detailSignature = signature;
|
|
391
|
+
|
|
392
|
+
const existingArticles = new Map([...transcript.querySelectorAll(":scope > .intercom-conversation-message[data-intercom-message-id]")]
|
|
393
|
+
.map((article) => [article.dataset.intercomMessageId, article]));
|
|
394
|
+
const shouldFollow = !existingArticles.size || transcript.scrollHeight - transcript.scrollTop - transcript.clientHeight < 80;
|
|
395
|
+
const previousScrollTop = transcript.scrollTop;
|
|
396
|
+
const anchor = shouldFollow ? null : [...existingArticles.values()]
|
|
397
|
+
.find((article) => article.offsetTop + article.offsetHeight > transcript.scrollTop);
|
|
398
|
+
const anchorOffset = anchor ? anchor.getBoundingClientRect().top - transcript.getBoundingClientRect().top : 0;
|
|
399
|
+
const orderedArticles = [];
|
|
400
|
+
|
|
401
|
+
for (const message of conversation.messages) {
|
|
402
|
+
let article = existingArticles.get(message.id);
|
|
403
|
+
if (article) existingArticles.delete(message.id);
|
|
404
|
+
else article = make("article", `intercom-conversation-message ${message.direction}`);
|
|
405
|
+
renderIntercomConversationMessage(article, message, conversation);
|
|
406
|
+
orderedArticles.push(article);
|
|
407
|
+
}
|
|
408
|
+
for (const staleArticle of existingArticles.values()) staleArticle.remove();
|
|
409
|
+
transcript.querySelector(":scope > .intercom-conversation-empty")?.remove();
|
|
410
|
+
|
|
411
|
+
let cursor = transcript.firstElementChild;
|
|
412
|
+
for (const article of orderedArticles) {
|
|
413
|
+
if (article === cursor) cursor = cursor.nextElementSibling;
|
|
414
|
+
else transcript.insertBefore(article, cursor);
|
|
415
|
+
}
|
|
416
|
+
if (!orderedArticles.length) transcript.append(make("p", "intercom-conversation-empty", "No messages are available in this conversation."));
|
|
417
|
+
|
|
418
|
+
if (detailFrame !== null) cancelAnimationFrame(detailFrame);
|
|
419
|
+
detailFrame = requestAnimationFrame(() => {
|
|
420
|
+
detailFrame = null;
|
|
421
|
+
if (!detailCurrent(state)) return;
|
|
422
|
+
if (shouldFollow) transcript.scrollTop = transcript.scrollHeight;
|
|
423
|
+
else if (anchor?.isConnected) {
|
|
424
|
+
const currentAnchorOffset = anchor.getBoundingClientRect().top - transcript.getBoundingClientRect().top;
|
|
425
|
+
transcript.scrollTop += currentAnchorOffset - anchorOffset;
|
|
426
|
+
}
|
|
427
|
+
else transcript.scrollTop = previousScrollTop;
|
|
428
|
+
});
|
|
429
|
+
return true;
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
function updateIntercomSummaryFromDetail(conversation) {
|
|
433
|
+
const state = intercomConversationDialogState;
|
|
434
|
+
if (!state || !conversation) return;
|
|
435
|
+
const summaries = intercomConversationSummariesByTab.get(state.tabId) || [];
|
|
436
|
+
const index = summaries.findIndex((summary) => summary.id === conversation.id);
|
|
437
|
+
if (index < 0) return;
|
|
438
|
+
const next = [...summaries];
|
|
439
|
+
next[index] = normalizeIntercomConversationSummary(conversation);
|
|
440
|
+
if (intercomConversationSummarySignature(summaries) === intercomConversationSummarySignature(next)) return false;
|
|
441
|
+
intercomConversationSummariesByTab.set(state.tabId, Object.freeze(next));
|
|
442
|
+
return true;
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
async function refreshSummaries(context = getContext()) {
|
|
446
|
+
if (!current(context)) return false;
|
|
447
|
+
const target = { ...context };
|
|
448
|
+
const serial = ++intercomSummaryRequestSerial;
|
|
449
|
+
const response = await api("/api/intercom/conversations", { tabId: target.tabId });
|
|
450
|
+
if (serial !== intercomSummaryRequestSerial || !current(target)) return false;
|
|
451
|
+
const seen = new Set();
|
|
452
|
+
const conversations = [];
|
|
453
|
+
for (const value of Array.isArray(response.data?.conversations) ? response.data.conversations : []) {
|
|
454
|
+
const conversation = normalizeIntercomConversationSummary(value);
|
|
455
|
+
if (!conversation || seen.has(conversation.id)) continue;
|
|
456
|
+
seen.add(conversation.id);
|
|
457
|
+
conversations.push(conversation);
|
|
458
|
+
}
|
|
459
|
+
const previous = getSummaries(target.tabId);
|
|
460
|
+
const changed = intercomConversationSummarySignature(previous) !== intercomConversationSummarySignature(conversations);
|
|
461
|
+
intercomConversationSummariesByTab.set(target.tabId, Object.freeze(conversations));
|
|
462
|
+
if (changed && !nodes.intercomConversationDialog?.open) renderTags(target.tabId);
|
|
463
|
+
if (intercomConversationDialogState?.tabId === target.tabId && !seen.has(intercomConversationDialogState.conversationId)) {
|
|
464
|
+
close({ restoreFocus: false });
|
|
465
|
+
}
|
|
466
|
+
if (changed) onChange();
|
|
467
|
+
return true;
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
function scheduleRefresh(context = getContext(), delay = 180) {
|
|
471
|
+
if (disposed) return;
|
|
472
|
+
clearTimeout(intercomSummaryRefreshTimer);
|
|
473
|
+
intercomSummaryRefreshTimer = null;
|
|
474
|
+
if (!current(context)) return;
|
|
475
|
+
const target = { ...context };
|
|
476
|
+
intercomSummaryRefreshTimer = setTimeout(() => {
|
|
477
|
+
intercomSummaryRefreshTimer = null;
|
|
478
|
+
if (!current(target)) return;
|
|
479
|
+
const completion = refreshSummaries(target);
|
|
480
|
+
const serial = intercomSummaryRequestSerial;
|
|
481
|
+
completion.catch((error) => {
|
|
482
|
+
if (serial === intercomSummaryRequestSerial && current(target)) {
|
|
483
|
+
onEvent(`agent conversation refresh failed: ${error.message || String(error)}`, "warn");
|
|
484
|
+
}
|
|
485
|
+
});
|
|
486
|
+
}, delay);
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
function scheduleDetailRefresh() {
|
|
490
|
+
clearTimeout(intercomDetailRefreshTimer);
|
|
491
|
+
intercomDetailRefreshTimer = null;
|
|
492
|
+
const state = intercomConversationDialogState;
|
|
493
|
+
if (!detailCurrent(state)) return;
|
|
494
|
+
intercomDetailRefreshTimer = setTimeout(() => {
|
|
495
|
+
intercomDetailRefreshTimer = null;
|
|
496
|
+
if (detailCurrent(state)) refreshDetail();
|
|
497
|
+
}, INTERCOM_DETAIL_REFRESH_MS);
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
async function refreshDetail({ announceLoading = false } = {}) {
|
|
501
|
+
const state = intercomConversationDialogState;
|
|
502
|
+
if (!detailCurrent(state)) return false;
|
|
503
|
+
clearTimeout(intercomDetailRefreshTimer);
|
|
504
|
+
intercomDetailRefreshTimer = null;
|
|
505
|
+
const serial = ++intercomDetailRequestSerial;
|
|
506
|
+
if (announceLoading) setStatus("Loading agent messages…");
|
|
507
|
+
try {
|
|
508
|
+
const path = `/api/intercom/conversations?conversation=${encodeURIComponent(state.conversationId)}`;
|
|
509
|
+
const response = await api(path, { tabId: state.tabId });
|
|
510
|
+
if (serial !== intercomDetailRequestSerial || !detailCurrent(state)) return false;
|
|
511
|
+
const conversation = normalizeIntercomConversationDetail(response.data?.conversation);
|
|
512
|
+
if (!conversation || conversation.id !== state.conversationId) throw new Error("Agent conversation is unavailable.");
|
|
513
|
+
nodes.intercomConversationTitle.textContent = intercomParticipantLabel(conversation.participants.peer, "Agent conversation");
|
|
514
|
+
nodes.intercomConversationParticipants.textContent = intercomConversationLabel(conversation);
|
|
515
|
+
const detailChanged = renderIntercomConversationDetail(conversation);
|
|
516
|
+
const summaryChanged = updateIntercomSummaryFromDetail(conversation);
|
|
517
|
+
const notices = [];
|
|
518
|
+
if (conversation.truncatedBefore) notices.push("Earlier messages are unavailable.");
|
|
519
|
+
if (conversation.messages.some((message) => message.truncatedText)) notices.push("One or more messages were truncated.");
|
|
520
|
+
setStatus(notices.join(" "), notices.length ? "warn" : "");
|
|
521
|
+
if (detailChanged || summaryChanged) onChange();
|
|
522
|
+
return true;
|
|
523
|
+
} catch (error) {
|
|
524
|
+
if (serial === intercomDetailRequestSerial && detailCurrent(state)) setStatus(error.message || String(error), "error");
|
|
525
|
+
return false;
|
|
526
|
+
} finally {
|
|
527
|
+
if (serial === intercomDetailRequestSerial && detailCurrent(state)) scheduleDetailRefresh();
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
function open(conversationId) {
|
|
532
|
+
const context = getContext();
|
|
533
|
+
const dialog = nodes.intercomConversationDialog;
|
|
534
|
+
if (!current(context) || !INTERCOM_CONVERSATION_ID_PATTERN.test(String(conversationId || "")) || !dialog) return false;
|
|
535
|
+
const summary = getSummaries(context.tabId).find((conversation) => conversation.id === conversationId);
|
|
536
|
+
if (!summary) return false;
|
|
537
|
+
resetDetail();
|
|
538
|
+
const state = { ...context, conversationId, detailSignature: "" };
|
|
539
|
+
intercomConversationDialogState = state;
|
|
540
|
+
nodes.intercomConversationTitle.textContent = intercomParticipantLabel(summary.participants.peer, "Agent conversation");
|
|
541
|
+
nodes.intercomConversationParticipants.textContent = intercomConversationLabel(summary);
|
|
542
|
+
setStatus("Loading agent messages…");
|
|
543
|
+
onDialogOpen(dialog);
|
|
544
|
+
if (!detailCurrent(state)) return false;
|
|
545
|
+
window.queueMicrotask(() => {
|
|
546
|
+
if (detailCurrent(state)) nodes.intercomConversationCloseButton?.focus({ preventScroll: true });
|
|
547
|
+
});
|
|
548
|
+
refreshDetail();
|
|
549
|
+
return true;
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
function resetDetail() {
|
|
553
|
+
intercomDetailRequestSerial += 1;
|
|
554
|
+
clearTimeout(intercomDetailRefreshTimer);
|
|
555
|
+
intercomDetailRefreshTimer = null;
|
|
556
|
+
if (detailFrame !== null) cancelAnimationFrame(detailFrame);
|
|
557
|
+
detailFrame = null;
|
|
558
|
+
intercomConversationDialogState = null;
|
|
559
|
+
nodes.intercomConversationTranscript?.replaceChildren();
|
|
560
|
+
if (nodes.intercomConversationParticipants) nodes.intercomConversationParticipants.textContent = "";
|
|
561
|
+
setStatus("");
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
function reset() {
|
|
565
|
+
if (disposed) return;
|
|
566
|
+
resetDetail();
|
|
567
|
+
renderTags();
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
function close({ restoreFocus = true } = {}) {
|
|
571
|
+
if (disposed) return;
|
|
572
|
+
const dialog = nodes.intercomConversationDialog;
|
|
573
|
+
reset();
|
|
574
|
+
if (dialog?.open) onDialogClose(dialog, { restoreFocus });
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
function activate(context = getContext()) {
|
|
578
|
+
if (disposed) return;
|
|
579
|
+
intercomSummaryRequestSerial += 1;
|
|
580
|
+
clearTimeout(intercomSummaryRefreshTimer);
|
|
581
|
+
intercomSummaryRefreshTimer = null;
|
|
582
|
+
setOverflowOpen(false);
|
|
583
|
+
close({ restoreFocus: false });
|
|
584
|
+
renderTags(context?.tabId);
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
function getSummaries(tabId = getContext()?.tabId) {
|
|
588
|
+
return intercomConversationSummariesByTab.get(tabId) || emptySummaries;
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
function prune(liveIds) {
|
|
592
|
+
if (disposed) return;
|
|
593
|
+
const live = new Set(liveIds);
|
|
594
|
+
for (const tabId of intercomConversationSummariesByTab.keys()) {
|
|
595
|
+
if (!live.has(tabId)) intercomConversationSummariesByTab.delete(tabId);
|
|
596
|
+
}
|
|
597
|
+
if (!live.has(getContext()?.tabId)) {
|
|
598
|
+
intercomSummaryRequestSerial += 1;
|
|
599
|
+
clearTimeout(intercomSummaryRefreshTimer);
|
|
600
|
+
intercomSummaryRefreshTimer = null;
|
|
601
|
+
renderTags();
|
|
602
|
+
}
|
|
603
|
+
if (intercomConversationDialogState && !live.has(intercomConversationDialogState.tabId)) close({ restoreFocus: false });
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
function dispose() {
|
|
607
|
+
if (disposed) return;
|
|
608
|
+
intercomSummaryRequestSerial += 1;
|
|
609
|
+
clearTimeout(intercomSummaryRefreshTimer);
|
|
610
|
+
intercomSummaryRefreshTimer = null;
|
|
611
|
+
resetDetail();
|
|
612
|
+
setOverflowOpen(false);
|
|
613
|
+
if (intercomConversationTagLayoutFrame !== null) cancelAnimationFrame(intercomConversationTagLayoutFrame);
|
|
614
|
+
intercomConversationTagLayoutFrame = null;
|
|
615
|
+
intercomConversationTagResizeObserver?.disconnect();
|
|
616
|
+
intercomConversationTagResizeObserver = null;
|
|
617
|
+
for (const remove of listeners) remove();
|
|
618
|
+
listeners.length = 0;
|
|
619
|
+
intercomConversationSummariesByTab.clear();
|
|
620
|
+
nodes.intercomConversationTags?.replaceChildren();
|
|
621
|
+
if (nodes.intercomConversationTags) nodes.intercomConversationTags.hidden = true;
|
|
622
|
+
disposed = true;
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
return { install, activate, refreshSummaries, scheduleRefresh, renderTags, fitTags, scheduleTagLayout, setOverflowOpen,
|
|
626
|
+
open, refreshDetail, reset, close, prune, getSummaries, dispose };
|
|
627
|
+
}
|