@firstpick/pi-package-webui 0.11.1 → 0.11.3
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 +151 -12
- package/README.md +34 -6
- package/TECHNICAL.md +75 -8
- package/bin/pi-webui.mjs +5390 -11423
- package/index.ts +10 -3
- 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 +4 -4
- 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 -3886
- 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 +322 -169
- package/public/subagent-launch-slot-state.mjs +2 -2
- 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/action-feedback-gutter.spec.mjs +251 -0
- 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-fast-mode-static.test.mjs +16 -11
- package/tests/codex-speed-mode-browser-state.test.mjs +6 -3
- package/tests/codex-usage-source.test.mjs +4 -3
- 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-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 +21 -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-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,318 @@
|
|
|
1
|
+
import { readFile, readdir, stat } from "node:fs/promises";
|
|
2
|
+
import { homedir } from "node:os";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { NATURAL_CONVERSATION_FEATURE_ID } from "./conversation-mode.mjs";
|
|
5
|
+
|
|
6
|
+
const VOICE_AUDIO_BODY_LIMIT_BYTES = 24 * 1024 * 1024;
|
|
7
|
+
export const VOICE_AUDIO_JSON_BODY_LIMIT_BYTES = Math.ceil(VOICE_AUDIO_BODY_LIMIT_BYTES * 1.4) + 1024 * 1024;
|
|
8
|
+
const VOICE_PROVIDER_TIMEOUT_MS = Math.max(1000, Number.parseInt(process.env.PI_VOICE_PROVIDER_TIMEOUT_MS || "120000", 10) || 120000);
|
|
9
|
+
const VOICE_TTS_TEXT_MAX_CHARS = 12_000;
|
|
10
|
+
|
|
11
|
+
export function createConversationVoiceService({
|
|
12
|
+
naturalConversationFeatureData,
|
|
13
|
+
naturalConversationModeSnapshot,
|
|
14
|
+
isLocalRequest,
|
|
15
|
+
makeHttpError,
|
|
16
|
+
formatBytes,
|
|
17
|
+
truncateStatusText,
|
|
18
|
+
}) {
|
|
19
|
+
// Piper voice switching for the native /talk audio loop. The WebUI never
|
|
20
|
+
// imports the natural-conversation package; it mirrors the package's small
|
|
21
|
+
// voice catalog for display, reads voice.json / the piper voice dir for
|
|
22
|
+
// state, and performs the actual switch (incl. download) by sending
|
|
23
|
+
// `/talk voice <id>` over RPC so the package stays the single writer.
|
|
24
|
+
const CONVERSATION_VOICE_CATALOG = [
|
|
25
|
+
{ id: "en_US-lessac-medium", file: "en_US-lessac-medium.onnx", sizeMb: 63, note: "natural US English" },
|
|
26
|
+
{ id: "en_GB-alba-medium", file: "en_GB-alba-medium.onnx", sizeMb: 63, note: "natural British English" },
|
|
27
|
+
{ id: "de_DE-thorsten-medium", file: "de_DE-thorsten-medium.onnx", sizeMb: 63, note: "natural German" },
|
|
28
|
+
{ id: "de_DE-thorsten-high", file: "de_DE-thorsten-high.onnx", sizeMb: 110, note: "German, best quality" },
|
|
29
|
+
];
|
|
30
|
+
const CONVERSATION_VOICE_SWITCH_TIMEOUT_MS = 10 * 60 * 1000; // downloads can take a while
|
|
31
|
+
|
|
32
|
+
function conversationVoiceConfigPath() {
|
|
33
|
+
const override = String(process.env.PI_VOICE_CONFIG_PATH || "").trim();
|
|
34
|
+
return override || path.join(homedir(), ".pi", "agent", "voice.json");
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function conversationVoiceDirs() {
|
|
38
|
+
const data = String(process.env.XDG_DATA_HOME || "").trim() || path.join(homedir(), ".local", "share");
|
|
39
|
+
return [path.join(data, "piper"), path.join(data, "piper", "voices"), path.join(data, "piper-voices")];
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
async function conversationVoicesData() {
|
|
43
|
+
let ttsProvider = null;
|
|
44
|
+
let currentModelPath = null;
|
|
45
|
+
try {
|
|
46
|
+
const config = JSON.parse(await readFile(conversationVoiceConfigPath(), "utf8"));
|
|
47
|
+
ttsProvider = config?.native?.tts?.provider ?? null;
|
|
48
|
+
if (ttsProvider === "piper") currentModelPath = config?.native?.tts?.modelPath ?? null;
|
|
49
|
+
} catch {
|
|
50
|
+
// no voice.json yet — the dropdown still lists the catalog
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const onDisk = new Map();
|
|
54
|
+
for (const dir of conversationVoiceDirs()) {
|
|
55
|
+
let entries = [];
|
|
56
|
+
try {
|
|
57
|
+
entries = await readdir(dir);
|
|
58
|
+
} catch {
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
for (const entry of entries) {
|
|
62
|
+
if (!entry.endsWith(".onnx") || onDisk.has(entry)) continue;
|
|
63
|
+
const modelPath = path.join(dir, entry);
|
|
64
|
+
const sidecar = await stat(`${modelPath}.json`).catch(() => null);
|
|
65
|
+
if (!sidecar) continue;
|
|
66
|
+
const size = await stat(modelPath).catch(() => null);
|
|
67
|
+
onDisk.set(entry, { path: modelPath, sizeMb: size ? Math.round(size.size / (1024 * 1024)) : 0 });
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const voices = CONVERSATION_VOICE_CATALOG.map((entry) => ({
|
|
72
|
+
id: entry.id,
|
|
73
|
+
sizeMb: entry.sizeMb,
|
|
74
|
+
note: entry.note,
|
|
75
|
+
downloaded: onDisk.has(entry.file),
|
|
76
|
+
current: Boolean(currentModelPath && currentModelPath.endsWith(`/${entry.file}`)),
|
|
77
|
+
}));
|
|
78
|
+
for (const [file, info] of onDisk) {
|
|
79
|
+
if (CONVERSATION_VOICE_CATALOG.some((entry) => entry.file === file)) continue;
|
|
80
|
+
voices.push({
|
|
81
|
+
id: file.replace(/\.onnx$/, ""),
|
|
82
|
+
sizeMb: info.sizeMb,
|
|
83
|
+
note: "found on disk",
|
|
84
|
+
downloaded: true,
|
|
85
|
+
current: info.path === currentModelPath,
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
return { voices, current: voices.find((voice) => voice.current)?.id ?? null, ttsProvider };
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
async function setConversationVoice(tab, body = {}) {
|
|
92
|
+
const voiceId = String(body.voice || "").trim();
|
|
93
|
+
if (!/^[\w.-]{1,120}$/.test(voiceId)) throw makeHttpError(400, "voice must be a Piper voice id (letters, digits, dot, dash, underscore)");
|
|
94
|
+
const feature = await naturalConversationFeatureData(tab);
|
|
95
|
+
if (!feature.available) throw makeHttpError(404, feature.unavailableReason);
|
|
96
|
+
const commandName = feature.commands.find((name) => name === "talk") || feature.commands[0] || "talk";
|
|
97
|
+
const response = await tab.rpc.send({ type: "prompt", message: `/${commandName} voice ${voiceId}` }, CONVERSATION_VOICE_SWITCH_TIMEOUT_MS);
|
|
98
|
+
if (response.success === false) throw makeHttpError(400, response.error || `Failed to switch the conversation voice to ${voiceId}`);
|
|
99
|
+
return { ...(await conversationVoicesData()), response };
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const VOICE_AUDIO_MIME_TYPES = new Set(["audio/webm", "audio/wav", "audio/x-wav", "audio/mpeg", "audio/mp3", "audio/mp4", "audio/ogg", "audio/flac", "application/octet-stream"]);
|
|
103
|
+
const VOICE_STT_PROVIDER_IDS = ["local", "groq", "openai", "cloudflare"];
|
|
104
|
+
const VOICE_TTS_PROVIDER_IDS = ["local", "openai"];
|
|
105
|
+
|
|
106
|
+
function safeVoiceEndpointLabel(value) {
|
|
107
|
+
try {
|
|
108
|
+
const parsed = new URL(String(value || ""));
|
|
109
|
+
return `${parsed.origin}${parsed.pathname}`;
|
|
110
|
+
} catch {
|
|
111
|
+
return "";
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function naturalConversationVoiceProviderStatus(kind = "stt") {
|
|
116
|
+
const stt = kind === "stt";
|
|
117
|
+
const localUrl = stt ? process.env.PI_VOICE_STT_URL : process.env.PI_VOICE_TTS_URL;
|
|
118
|
+
const providers = [
|
|
119
|
+
{
|
|
120
|
+
id: "local",
|
|
121
|
+
label: stt ? "Local STT endpoint" : "Local TTS endpoint",
|
|
122
|
+
configured: !!localUrl,
|
|
123
|
+
env: stt ? ["PI_VOICE_STT_URL"] : ["PI_VOICE_TTS_URL"],
|
|
124
|
+
endpoint: safeVoiceEndpointLabel(localUrl),
|
|
125
|
+
},
|
|
126
|
+
];
|
|
127
|
+
if (stt) {
|
|
128
|
+
providers.push(
|
|
129
|
+
{ id: "groq", label: "Groq Whisper", configured: !!process.env.GROQ_API_KEY, env: ["GROQ_API_KEY"], defaultModel: process.env.PI_VOICE_GROQ_STT_MODEL || "whisper-large-v3-turbo" },
|
|
130
|
+
{ id: "openai", label: "OpenAI transcription", configured: !!process.env.OPENAI_API_KEY, env: ["OPENAI_API_KEY"], defaultModel: process.env.PI_VOICE_OPENAI_STT_MODEL || "gpt-4o-mini-transcribe" },
|
|
131
|
+
{ id: "cloudflare", label: "Cloudflare Workers AI Whisper", configured: !!(process.env.CLOUDFLARE_API_TOKEN && process.env.CLOUDFLARE_ACCOUNT_ID), env: ["CLOUDFLARE_API_TOKEN", "CLOUDFLARE_ACCOUNT_ID"], deferred: true },
|
|
132
|
+
);
|
|
133
|
+
} else {
|
|
134
|
+
providers.push({ id: "openai", label: "OpenAI speech", configured: !!process.env.OPENAI_API_KEY, env: ["OPENAI_API_KEY"], defaultModel: process.env.PI_VOICE_OPENAI_TTS_MODEL || "gpt-4o-mini-tts" });
|
|
135
|
+
}
|
|
136
|
+
return {
|
|
137
|
+
kind: stt ? "stt" : "tts",
|
|
138
|
+
providers,
|
|
139
|
+
defaultProvider: (stt ? process.env.PI_VOICE_STT_PROVIDER : process.env.PI_VOICE_TTS_PROVIDER) || (localUrl ? "local" : ""),
|
|
140
|
+
remoteConsent: stt ? "Remote WebUI raw-audio uploads require per-request remoteMicStreamingConsentAccepted=true; browser Web Speech does not use this route." : "Server-side TTS sends answer text to the selected provider only when this route is explicitly used.",
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function naturalConversationUnavailableResponse(tab, kind = "stt") {
|
|
145
|
+
return {
|
|
146
|
+
featureId: NATURAL_CONVERSATION_FEATURE_ID,
|
|
147
|
+
available: false,
|
|
148
|
+
mode: naturalConversationModeSnapshot(tab, { available: false, enabled: false, uiState: "off" }),
|
|
149
|
+
voice: naturalConversationVoiceProviderStatus(kind),
|
|
150
|
+
message: "Natural Conversation server-side voice fallbacks are opt-in. Configure a local endpoint or explicit hosted provider env vars; browser-native audio remains the default Web UI path.",
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function naturalConversationProviderNotConfiguredError(kind, provider, message) {
|
|
155
|
+
const error = makeHttpError(provider ? 424 : 501, message);
|
|
156
|
+
error.voice = naturalConversationVoiceProviderStatus(kind);
|
|
157
|
+
return error;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function requestedNaturalConversationVoiceProvider(kind, body = {}) {
|
|
161
|
+
const requested = String(body.provider || body.providerId || (kind === "stt" ? process.env.PI_VOICE_STT_PROVIDER : process.env.PI_VOICE_TTS_PROVIDER) || "").trim().toLowerCase();
|
|
162
|
+
const ids = kind === "stt" ? VOICE_STT_PROVIDER_IDS : VOICE_TTS_PROVIDER_IDS;
|
|
163
|
+
const localUrl = kind === "stt" ? process.env.PI_VOICE_STT_URL : process.env.PI_VOICE_TTS_URL;
|
|
164
|
+
const provider = requested || (localUrl ? "local" : "");
|
|
165
|
+
if (!provider) throw naturalConversationProviderNotConfiguredError(kind, "", `No Natural Conversation ${kind.toUpperCase()} provider is configured. Set ${kind === "stt" ? "PI_VOICE_STT_URL or PI_VOICE_STT_PROVIDER" : "PI_VOICE_TTS_URL or PI_VOICE_TTS_PROVIDER"}.`);
|
|
166
|
+
if (!ids.includes(provider)) throw makeHttpError(400, `Unsupported Natural Conversation ${kind.toUpperCase()} provider: ${provider}`);
|
|
167
|
+
if (provider === "local" && !localUrl) throw naturalConversationProviderNotConfiguredError(kind, provider, `Local Natural Conversation ${kind.toUpperCase()} endpoint is not configured. Set ${kind === "stt" ? "PI_VOICE_STT_URL" : "PI_VOICE_TTS_URL"}.`);
|
|
168
|
+
if (provider === "groq" && !process.env.GROQ_API_KEY) throw naturalConversationProviderNotConfiguredError(kind, provider, "Groq STT is not configured. Set GROQ_API_KEY server-side; never send it from the browser.");
|
|
169
|
+
if (provider === "openai" && !process.env.OPENAI_API_KEY) throw naturalConversationProviderNotConfiguredError(kind, provider, `OpenAI ${kind.toUpperCase()} is not configured. Set OPENAI_API_KEY server-side; never send it from the browser.`);
|
|
170
|
+
if (provider === "cloudflare") throw makeHttpError(501, "Cloudflare Workers AI STT is a selected Phase 4 provider but its adapter is deferred until the exact upload contract is validated; use local, Groq, or OpenAI for this slice.");
|
|
171
|
+
return provider;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function requireRemoteMicConsentForStt(req, body = {}) {
|
|
175
|
+
if (isLocalRequest(req)) return;
|
|
176
|
+
if (body.remoteMicStreamingConsentAccepted === true || body.remoteMicStreamingConsent === true) return;
|
|
177
|
+
throw makeHttpError(403, "Remote WebUI STT uploads require explicit per-tab remote microphone streaming consent before raw audio is sent to the Pi host or an STT provider.");
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function decodeVoiceAudioBody(body = {}) {
|
|
181
|
+
const value = String(body.audioBase64 || body.audio || body.data || "").trim();
|
|
182
|
+
if (!value) throw makeHttpError(400, "audioBase64 is required for Natural Conversation STT fallback uploads");
|
|
183
|
+
const dataUrlMatch = value.match(/^data:([^;,]+);base64,(.*)$/s);
|
|
184
|
+
const mimeType = String(body.mimeType || body.contentType || dataUrlMatch?.[1] || "audio/webm").toLowerCase();
|
|
185
|
+
if (!VOICE_AUDIO_MIME_TYPES.has(mimeType)) throw makeHttpError(415, `Unsupported voice audio mime type: ${mimeType}`);
|
|
186
|
+
const base64 = (dataUrlMatch?.[2] || value).replace(/\s+/g, "");
|
|
187
|
+
if (!/^[A-Za-z0-9+/]*={0,2}$/.test(base64)) throw makeHttpError(400, "audioBase64 must be valid base64");
|
|
188
|
+
const buffer = Buffer.from(base64, "base64");
|
|
189
|
+
if (!buffer.length) throw makeHttpError(400, "audioBase64 decoded to an empty audio payload");
|
|
190
|
+
if (buffer.length > VOICE_AUDIO_BODY_LIMIT_BYTES) throw makeHttpError(413, `Audio payload is too large (limit ${formatBytes(VOICE_AUDIO_BODY_LIMIT_BYTES)})`);
|
|
191
|
+
return {
|
|
192
|
+
buffer,
|
|
193
|
+
mimeType,
|
|
194
|
+
fileName: String(body.fileName || `speech.${mimeType.includes("wav") ? "wav" : mimeType.includes("mpeg") || mimeType.includes("mp3") ? "mp3" : "webm"}`).replace(/[\\/\0]/g, "_").slice(0, 120),
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function voiceFetchError(provider, response, text) {
|
|
199
|
+
const status = response.status >= 500 ? 502 : 400;
|
|
200
|
+
return makeHttpError(status, `${provider} voice provider returned HTTP ${response.status}: ${truncateStatusText(text || response.statusText || "request failed", 500)}`);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
async function fetchVoiceProvider(provider, url, init = {}) {
|
|
204
|
+
const response = await fetch(url, { ...init, signal: AbortSignal.timeout(VOICE_PROVIDER_TIMEOUT_MS) });
|
|
205
|
+
if (!response.ok) throw voiceFetchError(provider, response, await response.text().catch(() => ""));
|
|
206
|
+
return response;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
function voiceFormData(audio, body = {}, model = "") {
|
|
210
|
+
const form = new FormData();
|
|
211
|
+
form.set("file", new Blob([audio.buffer], { type: audio.mimeType }), audio.fileName);
|
|
212
|
+
if (model) form.set("model", model);
|
|
213
|
+
if (body.language) form.set("language", String(body.language));
|
|
214
|
+
if (body.prompt) form.set("prompt", String(body.prompt));
|
|
215
|
+
return form;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
function transcriptTextFromProviderJson(json) {
|
|
219
|
+
return String(json?.text || json?.transcript || json?.data?.text || json?.result?.text || "").trim();
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
async function parseTranscriptResponse(provider, response) {
|
|
223
|
+
const contentType = String(response.headers.get("content-type") || "").toLowerCase();
|
|
224
|
+
if (contentType.includes("application/json")) {
|
|
225
|
+
const json = await response.json();
|
|
226
|
+
const text = transcriptTextFromProviderJson(json);
|
|
227
|
+
if (!text) throw makeHttpError(502, `${provider} STT provider returned JSON without a transcript text field`);
|
|
228
|
+
return text;
|
|
229
|
+
}
|
|
230
|
+
const text = (await response.text()).trim();
|
|
231
|
+
if (!text) throw makeHttpError(502, `${provider} STT provider returned an empty transcript`);
|
|
232
|
+
return text;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
async function transcribeWithLocalProvider(audio, body = {}) {
|
|
236
|
+
const form = voiceFormData(audio, body, body.model ? String(body.model) : "");
|
|
237
|
+
const response = await fetchVoiceProvider("local STT", process.env.PI_VOICE_STT_URL, { method: "POST", body: form });
|
|
238
|
+
return parseTranscriptResponse("local", response);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
async function transcribeWithOpenAiCompatibleProvider(provider, audio, body = {}) {
|
|
242
|
+
const openai = provider === "openai";
|
|
243
|
+
const url = openai ? "https://api.openai.com/v1/audio/transcriptions" : "https://api.groq.com/openai/v1/audio/transcriptions";
|
|
244
|
+
const model = String(body.model || (openai ? process.env.PI_VOICE_OPENAI_STT_MODEL || "gpt-4o-mini-transcribe" : process.env.PI_VOICE_GROQ_STT_MODEL || "whisper-large-v3-turbo"));
|
|
245
|
+
const form = voiceFormData(audio, body, model);
|
|
246
|
+
form.set("response_format", "json");
|
|
247
|
+
const response = await fetchVoiceProvider(`${provider} STT`, url, {
|
|
248
|
+
method: "POST",
|
|
249
|
+
headers: { authorization: `Bearer ${openai ? process.env.OPENAI_API_KEY : process.env.GROQ_API_KEY}` },
|
|
250
|
+
body: form,
|
|
251
|
+
});
|
|
252
|
+
return parseTranscriptResponse(provider, response);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
async function handleNaturalConversationSttTranscribe(req, tab, body = {}) {
|
|
256
|
+
requireRemoteMicConsentForStt(req, body);
|
|
257
|
+
const provider = requestedNaturalConversationVoiceProvider("stt", body);
|
|
258
|
+
const audio = decodeVoiceAudioBody(body);
|
|
259
|
+
const text = provider === "local" ? await transcribeWithLocalProvider(audio, body) : await transcribeWithOpenAiCompatibleProvider(provider, audio, body);
|
|
260
|
+
return { provider, text, mimeType: audio.mimeType, byteLength: audio.buffer.length, mode: naturalConversationModeSnapshot(tab) };
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
function normalizeVoiceText(body = {}) {
|
|
264
|
+
const text = String(body.text || body.input || "").trim();
|
|
265
|
+
if (!text) throw makeHttpError(400, "text is required for Natural Conversation TTS fallback synthesis");
|
|
266
|
+
if (text.length > VOICE_TTS_TEXT_MAX_CHARS) throw makeHttpError(413, `TTS text is too long (limit ${VOICE_TTS_TEXT_MAX_CHARS} characters)`);
|
|
267
|
+
return text;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
async function audioPayloadFromResponse(provider, response, preferredFormat = "") {
|
|
271
|
+
const contentType = String(response.headers.get("content-type") || "").toLowerCase();
|
|
272
|
+
if (contentType.includes("application/json")) {
|
|
273
|
+
const json = await response.json();
|
|
274
|
+
const audioBase64 = String(json.audioBase64 || json.audio || json.data?.audioBase64 || "").trim();
|
|
275
|
+
if (!audioBase64) throw makeHttpError(502, `${provider} TTS provider returned JSON without audioBase64`);
|
|
276
|
+
const buffer = Buffer.from(audioBase64, "base64");
|
|
277
|
+
return { audioBase64, contentType: json.mimeType || json.contentType || "audio/mpeg", format: json.format || preferredFormat || "mp3", byteLength: buffer.length };
|
|
278
|
+
}
|
|
279
|
+
const buffer = Buffer.from(await response.arrayBuffer());
|
|
280
|
+
if (!buffer.length) throw makeHttpError(502, `${provider} TTS provider returned empty audio`);
|
|
281
|
+
return { audioBase64: buffer.toString("base64"), contentType: contentType || "audio/mpeg", format: preferredFormat || (contentType.includes("wav") ? "wav" : "mp3"), byteLength: buffer.length };
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
async function synthesizeWithLocalProvider(text, body = {}) {
|
|
285
|
+
const response = await fetchVoiceProvider("local TTS", process.env.PI_VOICE_TTS_URL, {
|
|
286
|
+
method: "POST",
|
|
287
|
+
headers: { "content-type": "application/json" },
|
|
288
|
+
body: JSON.stringify({ text, voice: body.voice || process.env.PI_VOICE_TTS_VOICE || undefined, format: body.format || process.env.PI_VOICE_TTS_FORMAT || "mp3" }),
|
|
289
|
+
});
|
|
290
|
+
return audioPayloadFromResponse("local", response, body.format || process.env.PI_VOICE_TTS_FORMAT || "mp3");
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
async function synthesizeWithOpenAiProvider(text, body = {}) {
|
|
294
|
+
const format = String(body.format || process.env.PI_VOICE_OPENAI_TTS_FORMAT || "mp3");
|
|
295
|
+
const response = await fetchVoiceProvider("openai TTS", "https://api.openai.com/v1/audio/speech", {
|
|
296
|
+
method: "POST",
|
|
297
|
+
headers: { authorization: `Bearer ${process.env.OPENAI_API_KEY}`, "content-type": "application/json" },
|
|
298
|
+
body: JSON.stringify({ model: body.model || process.env.PI_VOICE_OPENAI_TTS_MODEL || "gpt-4o-mini-tts", voice: body.voice || process.env.PI_VOICE_OPENAI_TTS_VOICE || "alloy", input: text, response_format: format }),
|
|
299
|
+
});
|
|
300
|
+
return audioPayloadFromResponse("openai", response, format);
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
async function handleNaturalConversationTtsSpeech(_req, tab, body = {}) {
|
|
304
|
+
const provider = requestedNaturalConversationVoiceProvider("tts", body);
|
|
305
|
+
const text = normalizeVoiceText(body);
|
|
306
|
+
const audio = provider === "local" ? await synthesizeWithLocalProvider(text, body) : await synthesizeWithOpenAiProvider(text, body);
|
|
307
|
+
return { provider, ...audio, mode: naturalConversationModeSnapshot(tab) };
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
return {
|
|
311
|
+
conversationVoicesData,
|
|
312
|
+
setConversationVoice,
|
|
313
|
+
naturalConversationVoiceProviderStatus,
|
|
314
|
+
naturalConversationUnavailableResponse,
|
|
315
|
+
handleNaturalConversationSttTranscribe,
|
|
316
|
+
handleNaturalConversationTtsSpeech,
|
|
317
|
+
};
|
|
318
|
+
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
const EXTENSION_UI_BLOCKING_METHODS = new Set(["select", "confirm", "input", "editor"]);
|
|
2
|
+
|
|
3
|
+
/** Create the approval service; the caller owns tab state and lifecycle wiring. */
|
|
4
|
+
export function createExtensionUiRequestService({
|
|
5
|
+
truncateStatusText,
|
|
6
|
+
markTabWorking,
|
|
7
|
+
sendSseToClient,
|
|
8
|
+
tabActivitySnapshot,
|
|
9
|
+
broadcastTabEvent,
|
|
10
|
+
}) {
|
|
11
|
+
function pendingExtensionUiMap(tab) {
|
|
12
|
+
if (!tab.pendingExtensionUiRequests) tab.pendingExtensionUiRequests = new Map();
|
|
13
|
+
return tab.pendingExtensionUiRequests;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function isPendingExtensionUiRequest(event) {
|
|
17
|
+
return event?.type === "extension_ui_request" && EXTENSION_UI_BLOCKING_METHODS.has(event.method) && event.id;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function pruneExpiredPendingExtensionUiRequests(tab, nowMs = Date.now()) {
|
|
21
|
+
const pending = tab?.pendingExtensionUiRequests;
|
|
22
|
+
if (!pending) return;
|
|
23
|
+
for (const [id, request] of pending) {
|
|
24
|
+
const expiresAtMs = Date.parse(request.expiresAt || "");
|
|
25
|
+
if (Number.isFinite(expiresAtMs) && expiresAtMs <= nowMs) pending.delete(id);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function pendingExtensionUiRequests(tab) {
|
|
30
|
+
pruneExpiredPendingExtensionUiRequests(tab);
|
|
31
|
+
return [...(tab?.pendingExtensionUiRequests?.values() || [])];
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function pendingExtensionUiRequestSummaries(tab) {
|
|
35
|
+
return pendingExtensionUiRequests(tab).map((request) => ({
|
|
36
|
+
id: request.id,
|
|
37
|
+
method: request.method,
|
|
38
|
+
title: truncateStatusText(request.title || request.placeholder || "", 120),
|
|
39
|
+
message: request.message ? truncateStatusText(request.message, 180) : undefined,
|
|
40
|
+
receivedAt: request.receivedAt,
|
|
41
|
+
expiresAt: request.expiresAt,
|
|
42
|
+
}));
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function trackPendingExtensionUiRequest(tab, event) {
|
|
46
|
+
if (!isPendingExtensionUiRequest(event)) return;
|
|
47
|
+
const receivedAt = new Date().toISOString();
|
|
48
|
+
const timeoutMs = Number(event.timeout);
|
|
49
|
+
const expiresAt = Number.isFinite(timeoutMs) && timeoutMs > 0 ? new Date(Date.parse(receivedAt) + timeoutMs + 1000).toISOString() : undefined;
|
|
50
|
+
pendingExtensionUiMap(tab).set(String(event.id), { ...event, receivedAt, expiresAt });
|
|
51
|
+
if (!tab.activity?.isWorking) markTabWorking(tab, receivedAt);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function resolvePendingExtensionUiRequest(tab, id) {
|
|
55
|
+
if (!id) return false;
|
|
56
|
+
return !!tab?.pendingExtensionUiRequests?.delete(String(id));
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function clearPendingExtensionUiRequests(tab) {
|
|
60
|
+
tab?.pendingExtensionUiRequests?.clear();
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function replayPendingExtensionUiRequests(tab, client) {
|
|
64
|
+
const pending = pendingExtensionUiRequests(tab);
|
|
65
|
+
for (const request of pending) {
|
|
66
|
+
sendSseToClient(client, {
|
|
67
|
+
...request,
|
|
68
|
+
type: "extension_ui_request",
|
|
69
|
+
replayed: true,
|
|
70
|
+
tabId: tab.id,
|
|
71
|
+
tabTitle: tab.title,
|
|
72
|
+
pendingExtensionUiRequestCount: pending.length,
|
|
73
|
+
tabActivity: tabActivitySnapshot(tab),
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
async function cancelPendingExtensionUiRequests(tab) {
|
|
79
|
+
const pending = pendingExtensionUiRequests(tab);
|
|
80
|
+
if (!pending.length) return 0;
|
|
81
|
+
const ids = [];
|
|
82
|
+
for (const request of pending) {
|
|
83
|
+
ids.push(String(request.id));
|
|
84
|
+
try {
|
|
85
|
+
await tab.rpc.writeRaw({ type: "extension_ui_response", id: request.id, cancelled: true });
|
|
86
|
+
} catch {
|
|
87
|
+
// Abort should remain best-effort even if the RPC process already exited.
|
|
88
|
+
}
|
|
89
|
+
resolvePendingExtensionUiRequest(tab, request.id);
|
|
90
|
+
}
|
|
91
|
+
broadcastTabEvent(tab, {
|
|
92
|
+
type: "webui_extension_ui_cancelled",
|
|
93
|
+
tabId: tab.id,
|
|
94
|
+
tabTitle: tab.title,
|
|
95
|
+
ids,
|
|
96
|
+
pendingExtensionUiRequestCount: pendingExtensionUiRequests(tab).length,
|
|
97
|
+
tabActivity: tabActivitySnapshot(tab),
|
|
98
|
+
});
|
|
99
|
+
return ids.length;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return {
|
|
103
|
+
pendingExtensionUiMap,
|
|
104
|
+
isPendingExtensionUiRequest,
|
|
105
|
+
pruneExpiredPendingExtensionUiRequests,
|
|
106
|
+
pendingExtensionUiRequests,
|
|
107
|
+
pendingExtensionUiRequestSummaries,
|
|
108
|
+
trackPendingExtensionUiRequest,
|
|
109
|
+
resolvePendingExtensionUiRequest,
|
|
110
|
+
clearPendingExtensionUiRequests,
|
|
111
|
+
replayPendingExtensionUiRequests,
|
|
112
|
+
cancelPendingExtensionUiRequests,
|
|
113
|
+
};
|
|
114
|
+
}
|