@firstpick/pi-package-webui 0.5.9 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/public/app.js CHANGED
@@ -70,6 +70,13 @@ const elements = {
70
70
  followUpButton: $("#followUpButton"),
71
71
  abortButton: $("#abortButton"),
72
72
  conversationModeEndButton: $("#conversationModeEndButton"),
73
+ conversationVoiceMenu: $("#conversationVoiceMenu"),
74
+ conversationVoiceButton: $("#conversationVoiceButton"),
75
+ conversationVoiceButtonLabel: $("#conversationVoiceButtonLabel"),
76
+ conversationVoiceMenuPanel: $("#conversationVoiceMenuPanel"),
77
+ remoteMicStreamingConsent: $("#remoteMicStreamingConsent"),
78
+ remoteMicStreamingConsentText: $("#remoteMicStreamingConsentText"),
79
+ remoteMicStreamingConsentButton: $("#remoteMicStreamingConsentButton"),
73
80
  newSessionButton: $("#newSessionButton"),
74
81
  compactButton: $("#compactButton"),
75
82
  gitWorkflowButton: $("#gitWorkflowButton"),
@@ -1026,6 +1033,7 @@ function isInteractiveDropdownOpen() {
1026
1033
  || nativeCommandMenuOpen
1027
1034
  || appRunnerMenuOpen
1028
1035
  || optionsMenuOpen
1036
+ || conversationVoiceMenuOpen
1029
1037
  || busyPromptBehaviorMenuOpen
1030
1038
  || newTabMenuOpen
1031
1039
  || isFooterPickerOpen()
@@ -1117,6 +1125,7 @@ function mobileDropdownConfigs() {
1117
1125
  { menu: elements.nativeCommandMenuButton?.parentElement, button: elements.nativeCommandMenuButton, panel: elements.nativeCommandMenuButton?.parentElement?.querySelector(".composer-publish-menu-panel") },
1118
1126
  { menu: elements.optionsMenuButton?.parentElement, button: elements.optionsMenuButton, panel: elements.optionsMenu },
1119
1127
  { menu: elements.appRunnerMenu, button: elements.appRunnerMenuButton, panel: elements.appRunnerMenuPanel },
1128
+ { menu: elements.conversationVoiceMenu, button: elements.conversationVoiceButton, panel: elements.conversationVoiceMenuPanel },
1120
1129
  ];
1121
1130
  }
1122
1131
 
@@ -4058,6 +4067,7 @@ function defaultConversationModeState(patch = {}) {
4058
4067
  enabled: false,
4059
4068
  uiState: "off",
4060
4069
  statusText: "",
4070
+ partialTranscript: "",
4061
4071
  allowedTools: ["read", "grep", "find", "ls"],
4062
4072
  provider: "browser-shell",
4063
4073
  startedAt: null,
@@ -4077,16 +4087,25 @@ function normalizeConversationModeState(value = {}) {
4077
4087
  enabled,
4078
4088
  uiState: enabled ? cleanStatusText(source.uiState || "listening").toLowerCase() || "listening" : "off",
4079
4089
  statusText: cleanStatusText(source.statusText || ""),
4090
+ partialTranscript: cleanStatusText(source.partialTranscript || ""),
4080
4091
  allowedTools: Array.isArray(source.allowedTools) && source.allowedTools.length ? source.allowedTools.map((name) => String(name)).filter(Boolean) : ["read", "grep", "find", "ls"],
4081
4092
  packageInstalled: source.packageInstalled === true,
4082
4093
  loadedCommands: Array.isArray(source.loadedCommands) ? source.loadedCommands.map((name) => String(name)).filter(Boolean) : [],
4083
4094
  });
4084
4095
  }
4085
4096
 
4086
- function updateConversationModeForTab(tabId = activeTabId, mode = {}, { render = true } = {}) {
4097
+ function updateConversationModeForTab(tabId = activeTabId, mode = {}, { render = true, voiceLoop = false } = {}) {
4087
4098
  if (!tabId) return normalizeConversationModeState(mode);
4088
4099
  const previous = conversationModeByTab.get(tabId) || tabs.find((tab) => tab.id === tabId)?.conversationMode || {};
4089
- const next = normalizeConversationModeState({ ...previous, ...mode });
4100
+ let patch = mode && typeof mode === "object" ? mode : {};
4101
+ if (!voiceLoop && patch.enabled !== false && voiceConversationActiveFor(tabId)) {
4102
+ // While the browser voice loop runs, it owns uiState/statusText; server
4103
+ // snapshots would otherwise reset "speaking"/"transcribing" to "listening".
4104
+ patch = { ...patch };
4105
+ delete patch.uiState;
4106
+ delete patch.statusText;
4107
+ }
4108
+ const next = normalizeConversationModeState({ ...previous, ...patch });
4090
4109
  conversationModeByTab.set(tabId, next);
4091
4110
  if (tabId === activeTabId && render) renderConversationModeControls();
4092
4111
  return next;
@@ -4108,8 +4127,41 @@ function conversationModeButtonLabel(mode = activeConversationMode()) {
4108
4127
  return mode.enabled ? "End Conversation" : "Start Conversation";
4109
4128
  }
4110
4129
 
4130
+ function conversationModePartialPreview(text = "") {
4131
+ const normalized = cleanStatusText(text || "");
4132
+ if (!normalized) return "";
4133
+ return normalized.length > 42 ? `${normalized.slice(0, 39)}…` : normalized;
4134
+ }
4135
+
4136
+ // Declared before renderConversationModeControls ever runs (it is called
4137
+ // during initial render passes) so the voice-menu block never hits a TDZ.
4138
+ let conversationVoiceSwitching = false;
4139
+ let conversationVoicesLoadedForTab = null;
4140
+ let conversationVoiceMenuOpen = false;
4141
+ let conversationVoiceLastData = null;
4142
+ let conversationVoicePending = null;
4143
+
4144
+ // Same open/close contract as the Common-Pi-options menu: click toggles the
4145
+ // panel (hover/focus-within still work on desktop via CSS), so it also works
4146
+ // on touch devices where :hover never fires.
4147
+ function setConversationVoiceMenuOpen(open) {
4148
+ conversationVoiceMenuOpen = !!open;
4149
+ elements.conversationVoiceButton?.setAttribute("aria-expanded", conversationVoiceMenuOpen ? "true" : "false");
4150
+ elements.conversationVoiceButton?.classList.toggle("menu-open", conversationVoiceMenuOpen);
4151
+ elements.conversationVoiceMenu?.classList.toggle("open", conversationVoiceMenuOpen);
4152
+ scheduleMobileDropdownScrollBoundsUpdate();
4153
+ }
4154
+
4111
4155
  function conversationModeDisplayText(mode = activeConversationMode()) {
4112
- return mode.enabled ? `Voice: ${mode.uiState || "listening"}` : "Voice: off";
4156
+ if (!mode.enabled) return "Voice: off";
4157
+ // Voice-switch progress ("downloading de_DE-thorsten-high 42%", "testing …")
4158
+ // arrives via the package status text; show it verbatim instead of the bare state.
4159
+ const progressDetail = /^voice\s*:\s*((?:downloading|testing)\s.+)$/i.exec(mode.statusText || "")?.[1];
4160
+ const uiState = progressDetail || mode.uiState || "listening";
4161
+ const provider = nativeConversationAudioEngaged() ? "native audio" : voiceConversationVisibleProviderLabel();
4162
+ const prefix = provider ? `Voice: ${uiState} · ${provider}` : `Voice: ${uiState}`;
4163
+ const partial = uiState === "transcribing" ? conversationModePartialPreview(mode.partialTranscript) : "";
4164
+ return partial ? `${prefix} — “${partial}”` : prefix;
4113
4165
  }
4114
4166
 
4115
4167
  function conversationModeUnavailableText() {
@@ -4140,7 +4192,10 @@ function renderConversationModeControls() {
4140
4192
  if (elements.conversationModeChip) {
4141
4193
  elements.conversationModeChip.hidden = !active;
4142
4194
  elements.conversationModeChip.textContent = conversationModeDisplayText(mode);
4143
- elements.conversationModeChip.title = `Natural Conversation Mode is active in this tab. Thinking is forced off; tools are limited to ${mode.allowedTools.join(", ")}.`;
4195
+ elements.conversationModeChip.setAttribute("data-voice-state", active ? mode.uiState || "listening" : "off");
4196
+ const chipAction = mode.uiState === "paused" ? "Click to resume browser listening." : "Click to pause browser listening; use End conversation to leave the safe mode.";
4197
+ elements.conversationModeChip.title = `Natural Conversation Mode is active in this tab (${voiceConversationProviderLabel()}). Thinking is forced off; tools are limited to ${mode.allowedTools.join(", ")}. ${chipAction}`;
4198
+ elements.conversationModeChip.setAttribute("aria-label", `Natural Conversation Mode ${mode.uiState || "listening"}. ${chipAction}`);
4144
4199
  }
4145
4200
 
4146
4201
  if (elements.conversationModeEndButton) {
@@ -4148,6 +4203,27 @@ function renderConversationModeControls() {
4148
4203
  elements.conversationModeEndButton.disabled = !commandName;
4149
4204
  elements.conversationModeEndButton.title = commandName ? `End Natural Conversation Mode with /${commandName} off.` : conversationModeUnavailableText();
4150
4205
  }
4206
+
4207
+ if (elements.conversationVoiceMenu) {
4208
+ elements.conversationVoiceMenu.hidden = !active;
4209
+ if (!active && conversationVoiceMenuOpen) setConversationVoiceMenuOpen(false);
4210
+ if (elements.conversationVoiceButton) elements.conversationVoiceButton.disabled = conversationVoiceSwitching || !commandName;
4211
+ if (active && commandName && conversationVoicesLoadedForTab !== activeTabId) void loadConversationVoices();
4212
+ }
4213
+
4214
+ const awaitingRemoteMicConsent = active && !remoteMicConsentGranted(activeTabId);
4215
+ if (elements.remoteMicStreamingConsent) {
4216
+ elements.remoteMicStreamingConsent.hidden = !awaitingRemoteMicConsent;
4217
+ if (awaitingRemoteMicConsent && elements.remoteMicStreamingConsentText) {
4218
+ elements.remoteMicStreamingConsentText.textContent = remoteMicConsentDisclosureText();
4219
+ }
4220
+ }
4221
+ if (elements.remoteMicStreamingConsentButton) {
4222
+ // The consent button stays disabled unless its disclosure is visible.
4223
+ elements.remoteMicStreamingConsentButton.disabled = !awaitingRemoteMicConsent;
4224
+ }
4225
+
4226
+ syncVoiceConversationLoop();
4151
4227
  }
4152
4228
 
4153
4229
  function handleNaturalConversationStatus(statusText, tabId = activeTabId) {
@@ -4185,6 +4261,10 @@ async function setNaturalConversationModeEnabled(enabled) {
4185
4261
  }
4186
4262
  try {
4187
4263
  const response = await api("/api/conversation-mode", { method: "POST", body: { enabled: enabled === true }, tabId: tabContext.tabId });
4264
+ if (enabled !== true) {
4265
+ stopVoiceConversationLoop();
4266
+ remoteMicConsentTabs.delete(tabContext.tabId);
4267
+ }
4188
4268
  applyResponseTab(response);
4189
4269
  if (!isCurrentTabContext(tabContext)) return;
4190
4270
  updateConversationModeForTab(tabContext.tabId, response.data?.mode || {}, { render: true });
@@ -4201,6 +4281,449 @@ function toggleNaturalConversationMode() {
4201
4281
  return setNaturalConversationModeEnabled(!mode.enabled);
4202
4282
  }
4203
4283
 
4284
+ // --- Conversation voice dropdown (native Piper voices via /talk voice) -------
4285
+ // The list comes from GET /api/conversation-voices (server reads voice.json
4286
+ // and the piper voice directory); switching posts to /api/conversation-voice,
4287
+ // which runs `/talk voice <id>` in the package over RPC. Missing voices are
4288
+ // downloaded by the package; progress streams back through the voice chip's
4289
+ // status text ("Voice: downloading <id> <pct>%").
4290
+
4291
+ function conversationVoiceLanguageLabel(voiceId) {
4292
+ const locale = String(voiceId || "").split("-", 1)[0] || "";
4293
+ const language = locale.split("_")[0]?.toUpperCase() || "";
4294
+ return ["DE", "EN"].includes(language) ? language : language;
4295
+ }
4296
+
4297
+ function conversationVoiceGenderLabel(voiceId) {
4298
+ const id = String(voiceId || "").toLowerCase();
4299
+ if (id.includes("thorsten")) return "M";
4300
+ if (id.includes("lessac") || id.includes("alba")) return "F";
4301
+ return "";
4302
+ }
4303
+
4304
+ function conversationVoiceShortLabel(voiceId) {
4305
+ // "de_DE-thorsten-medium" → "DE M Thorsten med" keeps the controls compact.
4306
+ const text = String(voiceId || "").trim();
4307
+ if (!text) return "Voice";
4308
+ const parts = text.split("-").filter(Boolean);
4309
+ const language = conversationVoiceLanguageLabel(text);
4310
+ const gender = conversationVoiceGenderLabel(text);
4311
+ const quality = parts.at(-1) || "";
4312
+ const qualityLabel = quality === "medium" ? "med" : quality === "high" ? "high" : quality;
4313
+ const nameParts = parts.length >= 3 ? parts.slice(1, -1) : parts.slice(1);
4314
+ const name = nameParts.join(" ").replace(/\b\w/g, (char) => char.toUpperCase());
4315
+ return [language, gender, name, qualityLabel].filter(Boolean).join(" ") || text;
4316
+ }
4317
+
4318
+ function conversationVoiceById(data, voiceId) {
4319
+ const wanted = String(voiceId || "").trim();
4320
+ const voices = Array.isArray(data?.voices) ? data.voices : [];
4321
+ return voices.find((voice) => voice?.id === wanted) || null;
4322
+ }
4323
+
4324
+ function conversationVoiceDownloadSizeText(voice) {
4325
+ const sizeMb = Number(voice?.sizeMb);
4326
+ return Number.isFinite(sizeMb) && sizeMb > 0 ? ` (${sizeMb} MB)` : "";
4327
+ }
4328
+
4329
+ function conversationVoiceStartMessage(voice, voiceId) {
4330
+ const wanted = String(voiceId || voice?.id || "voice").trim();
4331
+ if (voice?.downloaded) return `Selecting conversation voice ${wanted}…`;
4332
+ return `Downloading conversation voice ${wanted}${conversationVoiceDownloadSizeText(voice)}, then selecting it…`;
4333
+ }
4334
+
4335
+ function showConversationVoiceFeedback(message, level = "info") {
4336
+ const text = cleanStatusText(message || "");
4337
+ if (!text) return;
4338
+ addEvent(text, level);
4339
+ addTransientMessage({ role: level === "error" ? "error" : "native", title: "/talk voice", content: text, level });
4340
+ }
4341
+
4342
+ function renderConversationVoicePanelNotice(message, level = "info") {
4343
+ const panel = elements.conversationVoiceMenuPanel;
4344
+ if (!panel) return;
4345
+ panel.innerHTML = "";
4346
+ const notice = document.createElement("div");
4347
+ notice.className = "composer-publish-menu-item composer-conversation-voice-item composer-conversation-voice-notice";
4348
+ notice.classList.toggle("error", level === "error");
4349
+ notice.setAttribute("role", level === "error" ? "alert" : "status");
4350
+ notice.textContent = message;
4351
+ panel.appendChild(notice);
4352
+ }
4353
+
4354
+ function renderConversationVoiceOptions(data = conversationVoiceLastData) {
4355
+ const panel = elements.conversationVoiceMenuPanel;
4356
+ if (!panel) return;
4357
+ const source = data || conversationVoiceLastData || {};
4358
+ conversationVoiceLastData = source;
4359
+ const voices = Array.isArray(source?.voices) ? source.voices : [];
4360
+ panel.innerHTML = "";
4361
+ if (elements.conversationVoiceButtonLabel) {
4362
+ elements.conversationVoiceButtonLabel.textContent = source?.current ? conversationVoiceShortLabel(source.current) : "Voice";
4363
+ }
4364
+ if (conversationVoicePending) {
4365
+ const pending = document.createElement("div");
4366
+ pending.className = "composer-publish-menu-item composer-conversation-voice-item composer-conversation-voice-notice pending";
4367
+ pending.setAttribute("role", "status");
4368
+ pending.setAttribute("aria-live", "polite");
4369
+ pending.textContent = conversationVoicePending.message;
4370
+ panel.appendChild(pending);
4371
+ }
4372
+ if (voices.length === 0) {
4373
+ const empty = document.createElement("button");
4374
+ empty.className = "composer-publish-menu-item composer-conversation-voice-item";
4375
+ empty.type = "button";
4376
+ empty.disabled = true;
4377
+ empty.textContent = "No Piper voices — run /talk setup";
4378
+ panel.appendChild(empty);
4379
+ return;
4380
+ }
4381
+ for (const voice of voices) {
4382
+ const item = document.createElement("button");
4383
+ const pendingForItem = conversationVoicePending?.voiceId === voice.id;
4384
+ item.className = "composer-publish-menu-item composer-conversation-voice-item";
4385
+ item.type = "button";
4386
+ item.setAttribute("role", "menuitem");
4387
+ item.dataset.voiceId = voice.id;
4388
+ item.classList.toggle("active", voice.current === true);
4389
+ item.classList.toggle("pending", pendingForItem);
4390
+ const label = conversationVoiceShortLabel(voice.id);
4391
+ item.textContent = pendingForItem
4392
+ ? conversationVoicePending.message
4393
+ : voice.current
4394
+ ? `${label} ✓`
4395
+ : voice.downloaded
4396
+ ? label
4397
+ : `${label} ↓${conversationVoiceDownloadSizeText(voice)}`;
4398
+ item.title = voice.current
4399
+ ? `${voice.note || voice.id} — current voice`
4400
+ : voice.downloaded
4401
+ ? `${voice.note || voice.id} — select this installed Piper voice`
4402
+ : `${voice.note || voice.id} — download and select with /talk voice ${voice.id}`;
4403
+ if (!voice.current) {
4404
+ item.setAttribute("aria-label", voice.downloaded ? `Select conversation voice ${voice.id}` : `Download and select conversation voice ${voice.id}`);
4405
+ }
4406
+ item.disabled = conversationVoiceSwitching || voice.current === true;
4407
+ let pointerActivated = false;
4408
+ const chooseVoice = (event) => {
4409
+ event.preventDefault();
4410
+ event.stopPropagation();
4411
+ void switchConversationVoice(voice.id);
4412
+ };
4413
+ item.addEventListener("pointerdown", (event) => {
4414
+ pointerActivated = true;
4415
+ chooseVoice(event);
4416
+ });
4417
+ item.addEventListener("click", (event) => {
4418
+ if (pointerActivated) {
4419
+ pointerActivated = false;
4420
+ event.preventDefault();
4421
+ event.stopPropagation();
4422
+ return;
4423
+ }
4424
+ chooseVoice(event);
4425
+ });
4426
+ panel.appendChild(item);
4427
+ }
4428
+ }
4429
+
4430
+ async function loadConversationVoices(tabContext = activeTabContext()) {
4431
+ if (!tabContext.tabId) return;
4432
+ try {
4433
+ const response = await api("/api/conversation-voices", { tabId: tabContext.tabId });
4434
+ if (!isCurrentTabContext(tabContext)) return;
4435
+ conversationVoicesLoadedForTab = tabContext.tabId;
4436
+ conversationVoiceLastData = response.data;
4437
+ renderConversationVoiceOptions(response.data);
4438
+ } catch (error) {
4439
+ if (isCurrentTabContext(tabContext)) {
4440
+ conversationVoicesLoadedForTab = null;
4441
+ const message = `conversation voice list failed: ${error.message || String(error)}`;
4442
+ addEvent(message, "warn");
4443
+ renderConversationVoicePanelNotice(message, "error");
4444
+ }
4445
+ }
4446
+ }
4447
+
4448
+ async function switchConversationVoice(voiceId) {
4449
+ const wanted = String(voiceId || "").trim();
4450
+ if (!wanted) return;
4451
+ if (conversationVoiceSwitching) {
4452
+ if (conversationVoicePending?.voiceId === wanted) return;
4453
+ addEvent(`Already switching conversation voice to ${conversationVoicePending?.voiceId || "another voice"}; wait for it to finish.`, "warn");
4454
+ return;
4455
+ }
4456
+ const tabContext = activeTabContext();
4457
+ if (!tabContext.tabId) return;
4458
+ const voice = conversationVoiceById(conversationVoiceLastData, wanted);
4459
+ const downloading = voice?.downloaded !== true;
4460
+ const startMessage = conversationVoiceStartMessage(voice, wanted);
4461
+ conversationVoiceSwitching = true;
4462
+ const label = conversationVoiceShortLabel(wanted);
4463
+ conversationVoicePending = { voiceId: wanted, phase: downloading ? "downloading" : "selecting", message: downloading ? `${label} ↓${conversationVoiceDownloadSizeText(voice)}…` : `${label}…` };
4464
+ setConversationVoiceMenuOpen(true);
4465
+ renderConversationVoiceOptions();
4466
+ if (elements.conversationVoiceButton) elements.conversationVoiceButton.disabled = true;
4467
+ if (elements.conversationVoiceButtonLabel) elements.conversationVoiceButtonLabel.textContent = `→ ${conversationVoiceShortLabel(wanted)}…`;
4468
+ updateConversationModeForTab(tabContext.tabId, {
4469
+ uiState: downloading ? "downloading" : "testing",
4470
+ statusText: downloading ? `Voice: downloading ${wanted} starting` : `Voice: testing ${wanted}`,
4471
+ }, { render: true, voiceLoop: true });
4472
+ showConversationVoiceFeedback(`${startMessage} Progress appears on the Voice chip.`, "info");
4473
+ try {
4474
+ const response = await api("/api/conversation-voice", { method: "POST", body: { voice: wanted }, tabId: tabContext.tabId });
4475
+ if (isCurrentTabContext(tabContext)) {
4476
+ applyResponseTab(response);
4477
+ conversationVoiceLastData = response.data;
4478
+ conversationVoicesLoadedForTab = tabContext.tabId;
4479
+ const current = response.data?.current || wanted;
4480
+ const output = cleanStatusText(response.data?.response?.data?.output || response.data?.response?.data?.message || "");
4481
+ renderConversationVoiceOptions(response.data);
4482
+ showConversationVoiceFeedback(`${downloading ? "Downloaded and selected" : "Selected"} conversation voice ${current}.${output ? ` ${output}` : ""}`, "info");
4483
+ setConversationVoiceMenuOpen(false);
4484
+ }
4485
+ } catch (error) {
4486
+ if (isCurrentTabContext(tabContext)) {
4487
+ updateConversationModeForTab(tabContext.tabId, { uiState: "error", statusText: "Voice: error" }, { render: true, voiceLoop: true });
4488
+ showConversationVoiceFeedback(`Voice switch failed: ${error.message || String(error)}`, "error");
4489
+ await loadConversationVoices(tabContext);
4490
+ }
4491
+ } finally {
4492
+ conversationVoiceSwitching = false;
4493
+ conversationVoicePending = null;
4494
+ if (isCurrentTabContext(tabContext)) {
4495
+ if (elements.conversationVoiceButton) elements.conversationVoiceButton.disabled = false;
4496
+ renderConversationVoiceOptions();
4497
+ renderConversationModeControls();
4498
+ }
4499
+ }
4500
+ }
4501
+
4502
+ // --- Natural Conversation browser voice loop (Phase 3) -----------------------
4503
+ // Browser Web Speech STT/TTS drives the active tab's conversation mode. The
4504
+ // controller logic lives in voice-conversation.mjs (dynamically imported so
4505
+ // app.js stays parseable as a plain script) and is tab-scoped: switching tabs
4506
+ // or disabling the mode tears the loop down.
4507
+
4508
+ const remoteMicConsentTabs = new Set();
4509
+ const VOICE_CONVERSATION_SILENCE_TIMEOUT_MS = 8000;
4510
+ let voiceConversation = null;
4511
+ let voiceConversationTabId = null;
4512
+ let voiceConversationStartToken = 0;
4513
+ let voiceConversationStartingForTab = null;
4514
+ let voiceConversationModulePromise = null;
4515
+
4516
+ function isLocalhostWebui() {
4517
+ const hostname = String(location.hostname || "").toLowerCase();
4518
+ return hostname === "localhost" || hostname === "127.0.0.1" || hostname === "[::1]" || hostname === "::1";
4519
+ }
4520
+
4521
+ function remoteMicConsentGranted(tabId = activeTabId) {
4522
+ if (isLocalhostWebui()) return true;
4523
+ return !!tabId && remoteMicConsentTabs.has(tabId);
4524
+ }
4525
+
4526
+ function remoteMicConsentDisclosureText() {
4527
+ const host = location.host || "this Pi host";
4528
+ const provider = browserSpeechRecognitionCtor() ? "your browser's speech service" : "a browser speech service (unavailable in this browser)";
4529
+ return `Remote session: enabling the microphone captures audio in this browser tab and transcribes it with ${provider}; audio snippets may leave this device to your browser vendor. Only the resulting text transcripts are sent to Pi at ${host} — raw audio is never streamed to the Pi host. Consent applies to this tab only and ends with the conversation. Use End conversation to stop capture at any time.`;
4530
+ }
4531
+
4532
+ function browserSpeechRecognitionCtor() {
4533
+ return window.SpeechRecognition || window.webkitSpeechRecognition || null;
4534
+ }
4535
+
4536
+ function voiceConversationProviderLabel() {
4537
+ const stt = browserSpeechRecognitionCtor() ? "browser mic" : "no browser STT";
4538
+ const tts = window.speechSynthesis ? "browser voice" : "no browser TTS";
4539
+ return `${stt}, ${tts}`;
4540
+ }
4541
+
4542
+ function voiceConversationVisibleProviderLabel() {
4543
+ const hasStt = !!browserSpeechRecognitionCtor();
4544
+ const hasTts = !!window.speechSynthesis;
4545
+ if (hasStt && hasTts) return "browser STT/TTS";
4546
+ if (hasStt) return "browser STT";
4547
+ if (hasTts) return "browser TTS only";
4548
+ return "speech unsupported";
4549
+ }
4550
+
4551
+ function voiceConversationActiveFor(tabId = activeTabId) {
4552
+ return !!voiceConversation && !!tabId && voiceConversationTabId === tabId && voiceConversation.isActive();
4553
+ }
4554
+
4555
+ function updateVoiceConversationModeFromController(tabId = voiceConversationTabId) {
4556
+ if (!tabId || !voiceConversation) return;
4557
+ const state = voiceConversation.getState?.() || {};
4558
+ const uiState = state.uiState || "listening";
4559
+ updateConversationModeForTab(tabId, { uiState, statusText: `Voice: ${uiState}`, partialTranscript: state.partialTranscript || "" }, { render: true, voiceLoop: true });
4560
+ }
4561
+
4562
+ function toggleVoiceConversationPaused() {
4563
+ if (!activeTabId || !activeConversationMode().enabled) return;
4564
+ if (!voiceConversationActiveFor(activeTabId)) {
4565
+ if (!remoteMicConsentGranted(activeTabId)) {
4566
+ addEvent("Remote Web UI session: allow remote microphone streaming before resuming the browser voice loop.", "warn");
4567
+ renderConversationModeControls();
4568
+ return;
4569
+ }
4570
+ void startVoiceConversationLoop(activeTabId);
4571
+ return;
4572
+ }
4573
+ const next = voiceConversation.togglePaused?.();
4574
+ updateVoiceConversationModeFromController(activeTabId);
4575
+ addEvent(next?.userPaused ? "Browser voice loop paused; Natural Conversation safety remains active." : "Browser voice loop resumed.", "info");
4576
+ }
4577
+
4578
+ function loadVoiceConversationModule() {
4579
+ if (!voiceConversationModulePromise) {
4580
+ voiceConversationModulePromise = import("./voice-conversation.mjs?v=1");
4581
+ voiceConversationModulePromise.catch(() => {
4582
+ voiceConversationModulePromise = null;
4583
+ });
4584
+ }
4585
+ return voiceConversationModulePromise;
4586
+ }
4587
+
4588
+ function speakableTextFromMarkdown(text) {
4589
+ return String(text || "")
4590
+ .replace(/```[\s\S]*?```/g, " Code block omitted. ")
4591
+ .replace(/`([^`\n]+)`/g, "$1")
4592
+ .replace(/!\[([^\]]*)\]\([^)]*\)/g, "$1")
4593
+ .replace(/\[([^\]]+)\]\([^)]*\)/g, "$1")
4594
+ .replace(/^#{1,6}\s+/gm, "")
4595
+ .replace(/^\s*[-*+]\s+/gm, "")
4596
+ .replace(/(\*\*|__|\*|_|~~)(\S(?:[^*_~]*\S)?)\1/g, "$2")
4597
+ .replace(/\s+/g, " ")
4598
+ .trim();
4599
+ }
4600
+
4601
+ function latestAssistantSpokenText() {
4602
+ for (let index = latestMessages.length - 1; index >= 0; index -= 1) {
4603
+ const message = latestMessages[index];
4604
+ if (message?.role !== "assistant") continue;
4605
+ const finalMessage = assistantDisplayMessages(message).find((item) => item?.title === "final output");
4606
+ return finalMessage ? textFromContent(finalMessage.content) : "";
4607
+ }
4608
+ return "";
4609
+ }
4610
+
4611
+ function assistantTextAsksQuestion(text) {
4612
+ const lines = String(text || "").trim().split(/\r?\n/).map((line) => line.trim()).filter(Boolean);
4613
+ const lastLine = lines[lines.length - 1] || "";
4614
+ return /\?["')\]]*$/.test(lastLine);
4615
+ }
4616
+
4617
+ async function startVoiceConversationLoop(tabId = activeTabId) {
4618
+ if (!tabId || voiceConversationActiveFor(tabId) || voiceConversationStartingForTab === tabId) return;
4619
+ if (voiceConversation) stopVoiceConversationLoop();
4620
+ if (!remoteMicConsentGranted(tabId)) {
4621
+ const mode = normalizeConversationModeState(conversationModeByTab.get(tabId) || {});
4622
+ if (mode.uiState !== "paused") {
4623
+ updateConversationModeForTab(tabId, { uiState: "paused", statusText: "Voice: paused" }, { render: true, voiceLoop: true });
4624
+ addEvent("Remote Web UI session: the microphone stays off until you allow remote microphone streaming for this tab.", "warn");
4625
+ }
4626
+ return;
4627
+ }
4628
+ const token = ++voiceConversationStartToken;
4629
+ voiceConversationStartingForTab = tabId;
4630
+ let module = null;
4631
+ try {
4632
+ module = await loadVoiceConversationModule();
4633
+ } catch (error) {
4634
+ addEvent(`failed to load the voice conversation module: ${error?.message || String(error)}`, "error");
4635
+ return;
4636
+ } finally {
4637
+ voiceConversationStartingForTab = null;
4638
+ }
4639
+ if (token !== voiceConversationStartToken || tabId !== activeTabId) return;
4640
+ if (!activeConversationMode().enabled) return;
4641
+ const controller = module.createVoiceConversationController({
4642
+ recognitionFactory: () => {
4643
+ const Recognition = browserSpeechRecognitionCtor();
4644
+ if (!Recognition) return null;
4645
+ try {
4646
+ const recognition = new Recognition();
4647
+ recognition.lang = navigator.language || "en-US";
4648
+ return recognition;
4649
+ } catch {
4650
+ return null;
4651
+ }
4652
+ },
4653
+ synthesis: window.speechSynthesis || null,
4654
+ utteranceFactory: (text) => new SpeechSynthesisUtterance(text),
4655
+ sendTranscript: async (text, { reason } = {}) => {
4656
+ if (!voiceConversationActiveFor(tabId) || tabId !== activeTabId) return;
4657
+ if (reason === "interrupt" && currentState?.isStreaming) {
4658
+ await sendPrompt("steer", `[Voice interruption: the user started speaking over the current answer.] ${text}`, { targetTabId: tabId, throwOnError: true });
4659
+ return;
4660
+ }
4661
+ await sendPrompt("prompt", text, { targetTabId: tabId, throwOnError: true });
4662
+ },
4663
+ sendSilenceEvent: async ({ silentSeconds } = {}) => {
4664
+ if (!voiceConversationActiveFor(tabId) || tabId !== activeTabId) return;
4665
+ await sendPrompt("prompt", module.voiceSilenceEventMessage(silentSeconds), { targetTabId: tabId, throwOnError: true });
4666
+ },
4667
+ onPartialTranscript: (partialTranscript) => {
4668
+ if (voiceConversationTabId !== tabId) return;
4669
+ updateConversationModeForTab(tabId, { uiState: partialTranscript ? "transcribing" : undefined, partialTranscript }, { render: true, voiceLoop: true });
4670
+ },
4671
+ onStateChange: (uiState, state = {}) => {
4672
+ if (voiceConversationTabId !== tabId || uiState === "off") return;
4673
+ updateConversationModeForTab(tabId, { uiState, statusText: `Voice: ${uiState}`, partialTranscript: state.partialTranscript || "" }, { render: true, voiceLoop: true });
4674
+ },
4675
+ onEvent: (message, level = "info") => addEvent(message, level),
4676
+ bargeInEnabled: false,
4677
+ silenceTimeoutMs: VOICE_CONVERSATION_SILENCE_TIMEOUT_MS,
4678
+ });
4679
+ voiceConversation = controller;
4680
+ voiceConversationTabId = tabId;
4681
+ controller.start();
4682
+ }
4683
+
4684
+ function stopVoiceConversationLoop() {
4685
+ voiceConversationStartToken += 1;
4686
+ const controller = voiceConversation;
4687
+ voiceConversation = null;
4688
+ voiceConversationTabId = null;
4689
+ if (controller) {
4690
+ try {
4691
+ controller.stop();
4692
+ } catch {
4693
+ // Tearing down a broken speech engine must not break the UI.
4694
+ }
4695
+ }
4696
+ }
4697
+
4698
+ // The package's native audio loop (mic + Piper TTS on the Pi host) announces
4699
+ // itself through its "natural-conversation-audio" widget. While it is armed —
4700
+ // listening or paused — the browser Web Speech loop must stand down entirely,
4701
+ // otherwise both loops transcribe the same speech and speak every answer twice
4702
+ // (native Piper voice + browser voice).
4703
+ function nativeConversationAudioEngaged() {
4704
+ return widgets.has("natural-conversation-audio");
4705
+ }
4706
+
4707
+ function syncVoiceConversationLoop() {
4708
+ const mode = activeConversationMode();
4709
+ const shouldRun = mode.enabled === true && !!activeTabId && !nativeConversationAudioEngaged();
4710
+ if (voiceConversation && (!shouldRun || voiceConversationTabId !== activeTabId)) stopVoiceConversationLoop();
4711
+ if (shouldRun && !voiceConversationActiveFor(activeTabId)) void startVoiceConversationLoop(activeTabId);
4712
+ }
4713
+
4714
+ async function handleVoiceConversationTurnEnd(tabContext = activeTabContext()) {
4715
+ const tabId = tabContext.tabId;
4716
+ if (!voiceConversationActiveFor(tabId)) return;
4717
+ try {
4718
+ await refreshMessages(tabContext);
4719
+ } catch {
4720
+ // Speak whatever transcript state we already have.
4721
+ }
4722
+ if (!isCurrentTabContext(tabContext) || !voiceConversationActiveFor(tabId)) return;
4723
+ const spoken = speakableTextFromMarkdown(latestAssistantSpokenText());
4724
+ voiceConversation.handleAssistantTurnEnd(spoken, { askedQuestion: assistantTextAsksQuestion(spoken) });
4725
+ }
4726
+
4204
4727
  function renderOptionalFeatureDependentDisplays() {
4205
4728
  renderOptionalFeatureControls();
4206
4729
  renderThemeSelect();
@@ -4688,6 +5211,8 @@ function syncTabMetadata(nextTabs = []) {
4688
5211
  widgetsByTab.delete(tabId);
4689
5212
  appRunnerDataByTab.delete(tabId);
4690
5213
  conversationModeByTab.delete(tabId);
5214
+ remoteMicConsentTabs.delete(tabId);
5215
+ if (voiceConversationTabId === tabId) stopVoiceConversationLoop();
4691
5216
  clearGitWorkflowForTab(tabId);
4692
5217
  }
4693
5218
  }
@@ -5031,6 +5556,9 @@ function setWidgetForTab(tabId, widgetKey, request) {
5031
5556
  if (targetTabId === activeTabId) {
5032
5557
  if (hasLines) widgets.set(widgetKey, request);
5033
5558
  else widgets.delete(widgetKey);
5559
+ // Native audio starting/stopping must immediately re-arbitrate which
5560
+ // voice loop (native vs. browser Web Speech) owns this conversation.
5561
+ if (widgetKey === "natural-conversation-audio") queueMicrotask(() => renderConversationModeControls());
5034
5562
  }
5035
5563
 
5036
5564
  return true;
@@ -5530,6 +6058,7 @@ async function switchTab(tabId) {
5530
6058
  cacheMessagesForTab(activeTabId);
5531
6059
  cacheWidgetsForTab(activeTabId);
5532
6060
  const tabContext = setActiveTabId(tabId, { remember: true });
6061
+ if (voiceConversation && voiceConversationTabId !== tabId) stopVoiceConversationLoop();
5533
6062
  resetActiveTabUi();
5534
6063
  renderTabs();
5535
6064
  restoreActiveDraft();
@@ -20553,6 +21082,7 @@ async function sendPrompt(kind = "prompt", explicitMessage, { targetTabId = acti
20553
21082
  const rawMessage = usesPromptInput ? elements.promptInput.value : explicitMessage;
20554
21083
  const originalMessage = String(rawMessage || "").trim();
20555
21084
  if (!targetTabId) return;
21085
+ if (voiceConversationActiveFor(targetTabId)) voiceConversation.handleUserActivity();
20556
21086
  const tabContext = activeTabContext(targetTabId);
20557
21087
  const attachments = usesPromptInput ? [...attachmentsForTab(targetTabId)] : [];
20558
21088
  if (!originalMessage && attachments.length === 0) return;
@@ -20987,6 +21517,7 @@ function handleEvent(event) {
20987
21517
  break;
20988
21518
  case "agent_start":
20989
21519
  if (currentState) currentState = { ...currentState, isStreaming: true };
21520
+ if (voiceConversationActiveFor(event.tabId || activeTabId)) voiceConversation.setAssistantActivity({ streaming: true });
20990
21521
  setRunIndicatorActivity("Agent run started; waiting for first output or action…");
20991
21522
  addEvent("agent started");
20992
21523
  scheduleRefreshState();
@@ -20999,6 +21530,10 @@ function handleEvent(event) {
20999
21530
  notifyAgentDone(event.tabId || activeTabId, { activity: event.tabActivity, tabTitle: event.tabTitle });
21000
21531
  clearContextUsageUnknownAfterCompaction(event.tabId || activeTabId);
21001
21532
  if (currentState) currentState = { ...currentState, isStreaming: false };
21533
+ // handleAssistantTurnEnd resets the controller's streaming/tool flags and
21534
+ // flushes queued interruptions itself; calling setAssistantActivity here
21535
+ // first would flush the queue and then speak the interrupted answer.
21536
+ if (voiceConversationActiveFor(event.tabId || activeTabId)) void handleVoiceConversationTurnEnd(tabContext);
21002
21537
  clearRunIndicatorActivity();
21003
21538
  markTabOutputSeen();
21004
21539
  requestGitFooterWebuiPayload(tabContext, { force: true });
@@ -21038,6 +21573,7 @@ function handleEvent(event) {
21038
21573
  break;
21039
21574
  case "tool_execution_start":
21040
21575
  streamToolCallSeen = true;
21576
+ if (voiceConversationActiveFor(event.tabId || activeTabId)) voiceConversation.setAssistantActivity({ toolRunning: true });
21041
21577
  suppressStreamingAssistantTextBeforeToolCall();
21042
21578
  removeStreamingToolCallCard();
21043
21579
  handleToolExecutionStart(event);
@@ -21049,6 +21585,7 @@ function handleEvent(event) {
21049
21585
  setRunIndicatorActivity(`Running tool: ${runIndicatorToolName(event.toolName)}…`, { scroll: false });
21050
21586
  break;
21051
21587
  case "tool_execution_end":
21588
+ if (voiceConversationActiveFor(event.tabId || activeTabId)) voiceConversation.setAssistantActivity({ toolRunning: false });
21052
21589
  handleToolExecutionEnd(event);
21053
21590
  setRunIndicatorActivity(`Tool ${runIndicatorToolName(event.toolName)} ${event.isError ? "failed" : "finished"}; waiting for the agent's next step…`);
21054
21591
  addEvent(`tool ${event.toolName} ${event.isError ? "failed" : "finished"}`, event.isError ? "error" : "info");
@@ -21442,8 +21979,27 @@ elements.nativeSkillsButton.addEventListener("click", () => runNativeCommandMenu
21442
21979
  elements.nativeToolsButton.addEventListener("click", () => runNativeCommandMenu("/tools"));
21443
21980
  elements.optionsCommandPaletteButton.addEventListener("click", () => openCommandPalette());
21444
21981
  elements.optionsConversationModeButton?.addEventListener("click", () => toggleNaturalConversationMode());
21445
- elements.conversationModeChip?.addEventListener("click", () => toggleNaturalConversationMode());
21982
+ elements.conversationModeChip?.addEventListener("click", () => toggleVoiceConversationPaused());
21446
21983
  elements.conversationModeEndButton?.addEventListener("click", () => setNaturalConversationModeEnabled(false));
21984
+ // The voice menu opens on click (and on hover/focus via CSS on desktop);
21985
+ // refresh the voice list whenever it is opened so download states stay fresh.
21986
+ elements.conversationVoiceButton?.addEventListener("click", () => {
21987
+ setConversationVoiceMenuOpen(!conversationVoiceMenuOpen);
21988
+ if (conversationVoiceMenuOpen && !conversationVoiceSwitching) void loadConversationVoices();
21989
+ });
21990
+ elements.conversationVoiceMenu?.addEventListener("pointerenter", () => {
21991
+ if (!conversationVoiceSwitching) void loadConversationVoices();
21992
+ });
21993
+ elements.conversationVoiceMenu?.addEventListener("pointerleave", () => setConversationVoiceMenuOpen(false));
21994
+ elements.conversationVoiceMenu?.addEventListener("focusin", () => {
21995
+ if (!conversationVoiceSwitching) void loadConversationVoices();
21996
+ });
21997
+ elements.remoteMicStreamingConsentButton?.addEventListener("click", () => {
21998
+ if (!activeTabId) return;
21999
+ remoteMicConsentTabs.add(activeTabId);
22000
+ addEvent("Remote microphone streaming allowed for this tab. Consent ends with the conversation or the tab.", "info");
22001
+ renderConversationModeControls();
22002
+ });
21447
22003
  elements.optionsResumeButton.addEventListener("click", () => runNativeCommandMenu("/resume"));
21448
22004
  elements.optionsReloadButton.addEventListener("click", () => runNativeCommandMenu("/reload"));
21449
22005
  elements.optionsRemoteButton.addEventListener("click", () => runNativeCommandMenu("/remote"));
@@ -21942,6 +22498,9 @@ document.addEventListener("pointerdown", (event) => {
21942
22498
  if (optionsMenuOpen && !event.target?.closest?.(".composer-options-menu")) {
21943
22499
  setOptionsMenuOpen(false);
21944
22500
  }
22501
+ if (conversationVoiceMenuOpen && !event.target?.closest?.(".composer-conversation-voice-menu")) {
22502
+ setConversationVoiceMenuOpen(false);
22503
+ }
21945
22504
  if (busyPromptBehaviorMenuOpen && !event.target?.closest?.(".composer-context-tags, .composer-busy-mode-menu")) {
21946
22505
  setBusyPromptBehaviorMenuOpen(false);
21947
22506
  }
@@ -22168,6 +22727,10 @@ window.addEventListener("keydown", (event) => {
22168
22727
  setOptionsMenuOpen(false);
22169
22728
  return;
22170
22729
  }
22730
+ if (conversationVoiceMenuOpen) {
22731
+ setConversationVoiceMenuOpen(false);
22732
+ return;
22733
+ }
22171
22734
  if (busyPromptBehaviorMenuOpen) {
22172
22735
  setBusyPromptBehaviorMenuOpen(false);
22173
22736
  elements.busyPromptBehaviorTag?.focus({ preventScroll: true });