@firstpick/pi-package-webui 0.5.9 → 0.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/bin/pi-webui.mjs CHANGED
@@ -81,7 +81,7 @@ const UPDATE_STATUS_CACHE_MS = 10 * 60 * 1000;
81
81
  const UPDATE_STATUS_TIMEOUT_MS = 10 * 1000;
82
82
  const PI_UPDATE_TIMEOUT_MS = 15 * 60 * 1000;
83
83
  const PI_UPDATE_OUTPUT_MAX_CHARS = 120_000;
84
- const UPDATE_PACKAGE_NAMES = [PI_CODING_AGENT_PACKAGE, WEBUI_PACKAGE];
84
+ const CORE_UPDATE_PACKAGE_NAMES = [PI_CODING_AGENT_PACKAGE, WEBUI_PACKAGE];
85
85
  const PACKAGE_UPDATE_TIMEOUT_MS = 15 * 60 * 1000;
86
86
  const PACKAGE_UPDATE_OUTPUT_MAX_CHARS = 120_000;
87
87
  const CODEX_USAGE_TIMEOUT_MS = 15 * 1000;
@@ -91,6 +91,10 @@ const OPENAI_CODEX_USAGE_ENDPOINT = process.env.PI_WEBUI_CODEX_USAGE_URL || "htt
91
91
  const BODY_LIMIT_BYTES = 1024 * 1024;
92
92
  const SKILL_FILE_BODY_LIMIT_BYTES = 2 * 1024 * 1024;
93
93
  const PROMPT_BODY_LIMIT_BYTES = 24 * 1024 * 1024;
94
+ const VOICE_AUDIO_BODY_LIMIT_BYTES = 24 * 1024 * 1024;
95
+ const VOICE_AUDIO_JSON_BODY_LIMIT_BYTES = Math.ceil(VOICE_AUDIO_BODY_LIMIT_BYTES * 1.4) + 1024 * 1024;
96
+ const VOICE_PROVIDER_TIMEOUT_MS = Math.max(1000, Number.parseInt(process.env.PI_VOICE_PROVIDER_TIMEOUT_MS || "120000", 10) || 120000);
97
+ const VOICE_TTS_TEXT_MAX_CHARS = 12_000;
94
98
  const UPLOAD_BODY_LIMIT_BYTES = 96 * 1024 * 1024;
95
99
  const ATTACHMENT_UPLOAD_MAX_FILES = 12;
96
100
  const ATTACHMENT_UPLOAD_MAX_FILE_BYTES = 64 * 1024 * 1024;
@@ -261,6 +265,11 @@ const WEBUI_CONTROLLED_PACKAGES = new Set([
261
265
  .filter(([featureId]) => featureId !== NATURAL_CONVERSATION_FEATURE_ID)
262
266
  .map(([, packageName]) => packageName),
263
267
  ]);
268
+ const UPDATE_PACKAGE_NAMES = [...new Set([
269
+ ...CORE_UPDATE_PACKAGE_NAMES,
270
+ ...WEBUI_CONTROLLED_PACKAGES,
271
+ ...OPTIONAL_FEATURE_PACKAGES.values(),
272
+ ])].sort();
264
273
  const NATURAL_CONVERSATION_STATUS_KEY = "natural-conversation";
265
274
  const NATURAL_CONVERSATION_COMMAND_NAMES = ["talk", "voice", "conversation"];
266
275
  const PACKAGE_NAME_CACHE = new Map();
@@ -671,7 +680,9 @@ function makeHttpError(statusCode, message) {
671
680
 
672
681
  function sendError(res, statusCode, error) {
673
682
  const message = statusCode >= 500 ? sanitizeError(error) : formatCliError(error);
674
- sendJson(res, statusCode, { ok: false, error: message });
683
+ const payload = { ok: false, error: message };
684
+ if (error?.optionalFeatureInstall) payload.optionalFeatureInstall = error.optionalFeatureInstall;
685
+ sendJson(res, statusCode, payload);
675
686
  }
676
687
 
677
688
  function formatBytes(bytes) {
@@ -1114,7 +1125,10 @@ function runCommand(command, args, { cwd, timeoutMs = 2000, maxOutputLength = 20
1114
1125
  if (stderr.length > maxOutputLength) stderr = stderr.slice(-maxOutputLength);
1115
1126
  });
1116
1127
  child.on("error", (error) => finish({ exitCode: undefined, stdout, stderr: sanitizeError(error), error: sanitizeError(error) }));
1117
- child.on("exit", (exitCode) => finish({ exitCode, stdout, stderr, timedOut: false }));
1128
+ // "close", not "exit": exit can fire before the stdio pipes flush, which
1129
+ // intermittently truncates stdout (empty `git rev-parse --show-toplevel`
1130
+ // output resolves to process.cwd() and reads the wrong repository).
1131
+ child.on("close", (exitCode) => finish({ exitCode, stdout, stderr, timedOut: false }));
1118
1132
  });
1119
1133
  }
1120
1134
 
@@ -1287,25 +1301,115 @@ async function optionalFeaturePackageStatuses() {
1287
1301
  return { features };
1288
1302
  }
1289
1303
 
1304
+ function optionalFeatureInstallOutputTail(result, maxLength = 4000) {
1305
+ const text = stripAnsi([result?.stderr, result?.stdout].filter(Boolean).join("\n").trim());
1306
+ if (text.length <= maxLength) return text;
1307
+ return `…${text.slice(-Math.max(0, maxLength - 1))}`;
1308
+ }
1309
+
1310
+ function optionalFeatureInstallFailureKind(result, message = "") {
1311
+ const combined = `${message}\n${result?.error || ""}\n${result?.stderr || ""}\n${result?.stdout || ""}`;
1312
+ if (result?.timedOut) return "timeout";
1313
+ if (/\b(?:ENOENT|command not found|not recognized|spawn\s+\S+\s+ENOENT)\b/i.test(combined)) return "npm-not-found";
1314
+ if (/\b(?:EACCES|EPERM|permission denied|access denied)\b/i.test(combined)) return "permission";
1315
+ if (/\b(?:EAI_AGAIN|ENOTFOUND|ECONNRESET|ETIMEDOUT|network timeout|registry\.npmjs\.org|fetch failed)\b/i.test(combined)) return "network";
1316
+ return "npm-exit";
1317
+ }
1318
+
1319
+ function optionalFeatureInstallFailureHint(kind, { command, installRoot } = {}) {
1320
+ switch (kind) {
1321
+ case "install-root":
1322
+ return `Set ${OPTIONAL_FEATURE_INSTALL_ROOT_ENV} to the Pi/Web UI npm package root, then retry.`;
1323
+ case "npm-not-found":
1324
+ return "npm could not be started. Install npm or set PI_WEBUI_NPM_BIN to an absolute npm-compatible executable path.";
1325
+ case "permission":
1326
+ return `The Web UI process cannot write to ${installRoot || "the selected npm prefix"}. Retry from the owning user or use ${OPTIONAL_FEATURE_INSTALL_ROOT_ENV} with a writable package root.`;
1327
+ case "network":
1328
+ return "npm could not reach the registry reliably. Check network/proxy/registry settings, then retry or run the copied command manually.";
1329
+ case "timeout":
1330
+ return "npm did not finish within 5 minutes. Check for a stuck package manager, lock contention, or slow network, then retry manually.";
1331
+ case "status-check":
1332
+ return "npm finished, but Web UI could not verify the package status. Reload the Web UI and recheck Optional features.";
1333
+ default:
1334
+ return command ? "Run the copied npm command manually on the Web UI host to see full package-manager diagnostics." : "Check the activity log and npm output, then retry.";
1335
+ }
1336
+ }
1337
+
1338
+ function makeOptionalFeatureInstallError(statusCode, message, details = {}) {
1339
+ const error = makeHttpError(statusCode, message);
1340
+ error.optionalFeatureInstall = {
1341
+ kind: details.kind || "unknown",
1342
+ featureId: details.featureId || "",
1343
+ packageName: details.packageName || "",
1344
+ installRoot: details.installRoot || "",
1345
+ command: details.command || "",
1346
+ exitCode: details.exitCode,
1347
+ timedOut: details.timedOut === true,
1348
+ message,
1349
+ hint: details.hint || optionalFeatureInstallFailureHint(details.kind, details),
1350
+ outputTail: details.outputTail || "",
1351
+ };
1352
+ return error;
1353
+ }
1354
+
1290
1355
  async function installOptionalFeaturePackage(featureId) {
1291
1356
  const beforeStatus = await optionalFeaturePackageStatus(featureId);
1292
1357
  const packageName = beforeStatus.packageName;
1293
1358
 
1294
- const installRoot = await optionalDependencyInstallRoot();
1359
+ let installRoot;
1360
+ try {
1361
+ installRoot = await optionalDependencyInstallRoot();
1362
+ } catch (error) {
1363
+ const message = formatCliError(error);
1364
+ throw makeOptionalFeatureInstallError(error?.statusCode || 500, message, {
1365
+ kind: "install-root",
1366
+ featureId,
1367
+ packageName,
1368
+ hint: optionalFeatureInstallFailureHint("install-root"),
1369
+ });
1370
+ }
1371
+
1295
1372
  const npmCommand = process.env.PI_WEBUI_NPM_BIN || "npm";
1296
1373
  const args = ["install", "--prefix", installRoot, packageName];
1374
+ const command = formatCommandForDisplay(npmCommand, args);
1297
1375
  const result = await runCommand(npmCommand, args, {
1298
1376
  cwd: installRoot,
1299
1377
  timeoutMs: 5 * 60 * 1000,
1300
1378
  maxOutputLength: 80000,
1301
1379
  });
1302
- const command = formatCommandForDisplay(npmCommand, args);
1303
1380
  const ok = result.exitCode === 0 && !result.timedOut && !result.error;
1304
1381
  if (!ok) {
1305
- const details = [result.error, result.timedOut ? "timed out" : undefined, result.stderr?.trim(), result.stdout?.trim()].filter(Boolean).join("\n");
1306
- throw makeHttpError(500, `Optional feature install failed: ${command}${details ? `\n${details}` : ""}`);
1382
+ const kind = optionalFeatureInstallFailureKind(result);
1383
+ const message = result.timedOut
1384
+ ? `Optional feature install timed out after 5 minutes: ${command}`
1385
+ : result.error
1386
+ ? `Optional feature install could not start: ${command}`
1387
+ : `Optional feature install failed with exit code ${result.exitCode ?? "unknown"}: ${command}`;
1388
+ throw makeOptionalFeatureInstallError(result.timedOut ? 504 : 500, message, {
1389
+ kind,
1390
+ featureId,
1391
+ packageName,
1392
+ installRoot,
1393
+ command,
1394
+ exitCode: result.exitCode,
1395
+ timedOut: result.timedOut,
1396
+ outputTail: optionalFeatureInstallOutputTail(result),
1397
+ });
1398
+ }
1399
+ let afterStatus;
1400
+ try {
1401
+ afterStatus = await optionalFeaturePackageStatus(featureId);
1402
+ } catch (error) {
1403
+ const message = `Optional feature install finished, but status verification failed: ${formatCliError(error)}`;
1404
+ throw makeOptionalFeatureInstallError(error?.statusCode || 500, message, {
1405
+ kind: "status-check",
1406
+ featureId,
1407
+ packageName,
1408
+ installRoot,
1409
+ command,
1410
+ outputTail: optionalFeatureInstallOutputTail(result),
1411
+ });
1307
1412
  }
1308
- const afterStatus = await optionalFeaturePackageStatus(featureId);
1309
1413
  const operation = beforeStatus.installed ? "Updated" : "Installed";
1310
1414
  return {
1311
1415
  featureId,
@@ -4321,7 +4425,9 @@ function runWorkflowCommand(command, args, { cwd, label = formatWorkflowCommand(
4321
4425
  if (stderr.length > 100000) stderr = stderr.slice(-100000);
4322
4426
  });
4323
4427
  child.on("error", (error) => finish({ exitCode: undefined, stderr: stderr || sanitizeError(error), error: sanitizeError(error) }));
4324
- child.on("exit", (exitCode, signal) => finish({ exitCode, signal }));
4428
+ // "close", not "exit": exit can fire before the stdio pipes flush,
4429
+ // intermittently truncating collected stdout/stderr.
4430
+ child.on("close", (exitCode, signal) => finish({ exitCode, signal }));
4325
4431
  });
4326
4432
  }
4327
4433
 
@@ -4538,7 +4644,7 @@ async function readBundledThemes() {
4538
4644
  function normalizeStaticPath(urlPath) {
4539
4645
  if (urlPath === "/") return "index.html";
4540
4646
  const name = urlPath.startsWith("/") ? urlPath.slice(1) : urlPath;
4541
- if (!["index.html", "app.js", "styles.css", "favicon.svg", "apple-touch-icon.png", "icon-192.png", "icon-512.png", "catppuccin-mocha-background.png", "matrix-background.webp", "manifest.webmanifest", "service-worker.js"].includes(name)) return undefined;
4647
+ if (!["index.html", "app.js", "voice-conversation.mjs", "styles.css", "favicon.svg", "apple-touch-icon.png", "icon-192.png", "icon-512.png", "catppuccin-mocha-background.png", "matrix-background.webp", "manifest.webmanifest", "service-worker.js"].includes(name)) return undefined;
4542
4648
  return name;
4543
4649
  }
4544
4650
 
@@ -6217,7 +6323,7 @@ async function currentWebuiPackageUpdateTask() {
6217
6323
  const sourceCheckout = webuiDevServer || !String(packageRoot).split(path.sep).includes("node_modules");
6218
6324
  if (sourceCheckout) {
6219
6325
  const manifest = await readJsonFileIfExists(path.join(packageRoot, "package.json"));
6220
- const packages = Object.keys(manifest?.dependencies || {}).filter(isWebuiOrPiPackageName).sort();
6326
+ const packages = declaredWebuiPiPackageNames(manifest);
6221
6327
  return npmPrefixUpdateTask("current Web UI checkout package dependencies", packageRoot, packages);
6222
6328
  }
6223
6329
 
@@ -6232,6 +6338,36 @@ async function agentPackageRootUpdateTask() {
6232
6338
  return npmPrefixUpdateTask("Pi agent npm package root", installRoot, packages);
6233
6339
  }
6234
6340
 
6341
+ async function optionalFeatureInstallRootUpdateTask() {
6342
+ const configuredRoot = process.env[OPTIONAL_FEATURE_INSTALL_ROOT_ENV];
6343
+ if (!configuredRoot) return null;
6344
+ const installRoot = path.resolve(expandUserPath(configuredRoot));
6345
+ const packages = await packagesPresentInInstallPrefix(installRoot);
6346
+ return npmPrefixUpdateTask("configured optional-feature npm root", installRoot, packages);
6347
+ }
6348
+
6349
+ function activeProjectPackageRoots() {
6350
+ const roots = new Set();
6351
+ const add = (cwd) => {
6352
+ if (!cwd) return;
6353
+ roots.add(path.join(path.resolve(cwd), ".pi", "npm"));
6354
+ };
6355
+ add(options.cwd);
6356
+ for (const tab of tabs.values()) add(tab.cwd);
6357
+ for (const tab of closedRestorableTabs) add(tab.cwd);
6358
+ return [...roots].sort();
6359
+ }
6360
+
6361
+ async function projectPackageRootUpdateTasks() {
6362
+ const tasks = [];
6363
+ for (const installRoot of activeProjectPackageRoots()) {
6364
+ const packages = await packagesPresentInInstallPrefix(installRoot);
6365
+ const task = npmPrefixUpdateTask(`project Pi package root (${displayPath(path.dirname(installRoot))})`, installRoot, packages);
6366
+ if (task) tasks.push(task);
6367
+ }
6368
+ return tasks;
6369
+ }
6370
+
6235
6371
  async function npmGlobalNodeModulesRoot() {
6236
6372
  const npmCommand = npmCommandName();
6237
6373
  const result = await runCommand(npmCommand, ["root", "-g"], { timeoutMs: 5000, maxOutputLength: 8000 });
@@ -6296,8 +6432,33 @@ function uniqueUpdateTasks(tasks) {
6296
6432
  }
6297
6433
 
6298
6434
  async function resolveUpdateTasks({ all = false } = {}) {
6435
+ const piTask = await resolvePiUpdateCommand({ all });
6436
+ if (!all) return uniqueUpdateTasks([piTask]);
6437
+
6438
+ const [
6439
+ currentWebuiTask,
6440
+ agentTask,
6441
+ optionalFeatureTask,
6442
+ projectTasks,
6443
+ globalNpmTask,
6444
+ globalBunTask,
6445
+ ] = await Promise.all([
6446
+ currentWebuiPackageUpdateTask(),
6447
+ agentPackageRootUpdateTask(),
6448
+ optionalFeatureInstallRootUpdateTask(),
6449
+ projectPackageRootUpdateTasks(),
6450
+ npmGlobalPackageRootUpdateTask(),
6451
+ bunGlobalPackageRootUpdateTask(),
6452
+ ]);
6453
+
6299
6454
  return uniqueUpdateTasks([
6300
- await resolvePiUpdateCommand({ all }),
6455
+ piTask,
6456
+ currentWebuiTask,
6457
+ agentTask,
6458
+ optionalFeatureTask,
6459
+ ...projectTasks,
6460
+ globalNpmTask,
6461
+ globalBunTask,
6301
6462
  ]);
6302
6463
  }
6303
6464
 
@@ -7107,11 +7268,12 @@ function rememberNaturalConversationCommands(tab, commands = []) {
7107
7268
  return tab.conversationMode;
7108
7269
  }
7109
7270
 
7110
- async function getCommandData(tab) {
7271
+ async function getCommandData(tab, { annotateSkills = true } = {}) {
7111
7272
  try {
7112
7273
  const response = await tab.rpc.send({ type: "get_commands" });
7113
7274
  if (response.success === false) throw makeHttpError(400, response.error || "failed to load commands");
7114
- const rpcCommands = await annotateSkillCommandState(tab, response.data?.commands || []);
7275
+ const rawCommands = (response.data?.commands || []).filter((command) => command?.name !== WEBUI_HELPER_COMMAND);
7276
+ const rpcCommands = annotateSkills ? await annotateSkillCommandState(tab, rawCommands) : rawCommands;
7115
7277
  rememberNaturalConversationCommands(tab, rpcCommands);
7116
7278
  return { commands: [...NATIVE_SLASH_COMMANDS, ...rpcCommands], rpcRunning: true };
7117
7279
  } catch (error) {
@@ -7135,7 +7297,7 @@ async function naturalConversationFeatureData(tab, { refreshCommands = true } =
7135
7297
  let commandError = "";
7136
7298
  if (refreshCommands) {
7137
7299
  try {
7138
- commandData = await getCommandData(tab);
7300
+ commandData = await getCommandData(tab, { annotateSkills: false });
7139
7301
  } catch (error) {
7140
7302
  commandError = sanitizeError(error);
7141
7303
  rememberNaturalConversationCommands(tab, []);
@@ -7186,15 +7348,297 @@ async function setNaturalConversationMode(tab, body = {}) {
7186
7348
  return { ...(await naturalConversationFeatureData(tab, { refreshCommands: false })), response };
7187
7349
  }
7188
7350
 
7189
- function naturalConversationUnavailableResponse(tab) {
7351
+ // Piper voice switching for the native /talk audio loop. The WebUI never
7352
+ // imports the natural-conversation package; it mirrors the package's small
7353
+ // voice catalog for display, reads voice.json / the piper voice dir for
7354
+ // state, and performs the actual switch (incl. download) by sending
7355
+ // `/talk voice <id>` over RPC so the package stays the single writer.
7356
+ const CONVERSATION_VOICE_CATALOG = [
7357
+ { id: "en_US-lessac-medium", file: "en_US-lessac-medium.onnx", sizeMb: 63, note: "natural US English" },
7358
+ { id: "en_GB-alba-medium", file: "en_GB-alba-medium.onnx", sizeMb: 63, note: "natural British English" },
7359
+ { id: "de_DE-thorsten-medium", file: "de_DE-thorsten-medium.onnx", sizeMb: 63, note: "natural German" },
7360
+ { id: "de_DE-thorsten-high", file: "de_DE-thorsten-high.onnx", sizeMb: 110, note: "German, best quality" },
7361
+ ];
7362
+ const CONVERSATION_VOICE_SWITCH_TIMEOUT_MS = 10 * 60 * 1000; // downloads can take a while
7363
+
7364
+ function conversationVoiceConfigPath() {
7365
+ const override = String(process.env.PI_VOICE_CONFIG_PATH || "").trim();
7366
+ return override || path.join(homedir(), ".pi", "agent", "voice.json");
7367
+ }
7368
+
7369
+ function conversationVoiceDirs() {
7370
+ const data = String(process.env.XDG_DATA_HOME || "").trim() || path.join(homedir(), ".local", "share");
7371
+ return [path.join(data, "piper"), path.join(data, "piper", "voices"), path.join(data, "piper-voices")];
7372
+ }
7373
+
7374
+ async function conversationVoicesData() {
7375
+ let ttsProvider = null;
7376
+ let currentModelPath = null;
7377
+ try {
7378
+ const config = JSON.parse(await readFile(conversationVoiceConfigPath(), "utf8"));
7379
+ ttsProvider = config?.native?.tts?.provider ?? null;
7380
+ if (ttsProvider === "piper") currentModelPath = config?.native?.tts?.modelPath ?? null;
7381
+ } catch {
7382
+ // no voice.json yet — the dropdown still lists the catalog
7383
+ }
7384
+
7385
+ const onDisk = new Map();
7386
+ for (const dir of conversationVoiceDirs()) {
7387
+ let entries = [];
7388
+ try {
7389
+ entries = await readdir(dir);
7390
+ } catch {
7391
+ continue;
7392
+ }
7393
+ for (const entry of entries) {
7394
+ if (!entry.endsWith(".onnx") || onDisk.has(entry)) continue;
7395
+ const modelPath = path.join(dir, entry);
7396
+ const sidecar = await stat(`${modelPath}.json`).catch(() => null);
7397
+ if (!sidecar) continue;
7398
+ const size = await stat(modelPath).catch(() => null);
7399
+ onDisk.set(entry, { path: modelPath, sizeMb: size ? Math.round(size.size / (1024 * 1024)) : 0 });
7400
+ }
7401
+ }
7402
+
7403
+ const voices = CONVERSATION_VOICE_CATALOG.map((entry) => ({
7404
+ id: entry.id,
7405
+ sizeMb: entry.sizeMb,
7406
+ note: entry.note,
7407
+ downloaded: onDisk.has(entry.file),
7408
+ current: Boolean(currentModelPath && currentModelPath.endsWith(`/${entry.file}`)),
7409
+ }));
7410
+ for (const [file, info] of onDisk) {
7411
+ if (CONVERSATION_VOICE_CATALOG.some((entry) => entry.file === file)) continue;
7412
+ voices.push({
7413
+ id: file.replace(/\.onnx$/, ""),
7414
+ sizeMb: info.sizeMb,
7415
+ note: "found on disk",
7416
+ downloaded: true,
7417
+ current: info.path === currentModelPath,
7418
+ });
7419
+ }
7420
+ return { voices, current: voices.find((voice) => voice.current)?.id ?? null, ttsProvider };
7421
+ }
7422
+
7423
+ async function setConversationVoice(tab, body = {}) {
7424
+ const voiceId = String(body.voice || "").trim();
7425
+ if (!/^[\w.-]{1,120}$/.test(voiceId)) throw makeHttpError(400, "voice must be a Piper voice id (letters, digits, dot, dash, underscore)");
7426
+ const feature = await naturalConversationFeatureData(tab);
7427
+ if (!feature.available) throw makeHttpError(404, feature.unavailableReason);
7428
+ const commandName = feature.commands.find((name) => name === "talk") || feature.commands[0] || "talk";
7429
+ const response = await tab.rpc.send({ type: "prompt", message: `/${commandName} voice ${voiceId}` }, CONVERSATION_VOICE_SWITCH_TIMEOUT_MS);
7430
+ if (response.success === false) throw makeHttpError(400, response.error || `Failed to switch the conversation voice to ${voiceId}`);
7431
+ return { ...(await conversationVoicesData()), response };
7432
+ }
7433
+
7434
+ 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"]);
7435
+ const VOICE_STT_PROVIDER_IDS = ["local", "groq", "openai", "cloudflare"];
7436
+ const VOICE_TTS_PROVIDER_IDS = ["local", "openai"];
7437
+
7438
+ function safeVoiceEndpointLabel(value) {
7439
+ try {
7440
+ const parsed = new URL(String(value || ""));
7441
+ return `${parsed.origin}${parsed.pathname}`;
7442
+ } catch {
7443
+ return "";
7444
+ }
7445
+ }
7446
+
7447
+ function naturalConversationVoiceProviderStatus(kind = "stt") {
7448
+ const stt = kind === "stt";
7449
+ const localUrl = stt ? process.env.PI_VOICE_STT_URL : process.env.PI_VOICE_TTS_URL;
7450
+ const providers = [
7451
+ {
7452
+ id: "local",
7453
+ label: stt ? "Local STT endpoint" : "Local TTS endpoint",
7454
+ configured: !!localUrl,
7455
+ env: stt ? ["PI_VOICE_STT_URL"] : ["PI_VOICE_TTS_URL"],
7456
+ endpoint: safeVoiceEndpointLabel(localUrl),
7457
+ },
7458
+ ];
7459
+ if (stt) {
7460
+ providers.push(
7461
+ { 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" },
7462
+ { 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" },
7463
+ { 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 },
7464
+ );
7465
+ } else {
7466
+ 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" });
7467
+ }
7468
+ return {
7469
+ kind: stt ? "stt" : "tts",
7470
+ providers,
7471
+ defaultProvider: (stt ? process.env.PI_VOICE_STT_PROVIDER : process.env.PI_VOICE_TTS_PROVIDER) || (localUrl ? "local" : ""),
7472
+ 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.",
7473
+ };
7474
+ }
7475
+
7476
+ function naturalConversationUnavailableResponse(tab, kind = "stt") {
7190
7477
  return {
7191
7478
  featureId: NATURAL_CONVERSATION_FEATURE_ID,
7192
7479
  available: false,
7193
7480
  mode: naturalConversationModeSnapshot(tab, { available: false, enabled: false, uiState: "off" }),
7194
- message: "Natural Conversation STT/TTS fallback endpoints are reserved for a later phase. Install and load @firstpick/pi-package-natural-conversation, then use browser-native audio from the Web UI shell.",
7481
+ voice: naturalConversationVoiceProviderStatus(kind),
7482
+ 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.",
7195
7483
  };
7196
7484
  }
7197
7485
 
7486
+ function naturalConversationProviderNotConfiguredError(kind, provider, message) {
7487
+ const error = makeHttpError(provider ? 424 : 501, message);
7488
+ error.voice = naturalConversationVoiceProviderStatus(kind);
7489
+ return error;
7490
+ }
7491
+
7492
+ function requestedNaturalConversationVoiceProvider(kind, body = {}) {
7493
+ const requested = String(body.provider || body.providerId || (kind === "stt" ? process.env.PI_VOICE_STT_PROVIDER : process.env.PI_VOICE_TTS_PROVIDER) || "").trim().toLowerCase();
7494
+ const ids = kind === "stt" ? VOICE_STT_PROVIDER_IDS : VOICE_TTS_PROVIDER_IDS;
7495
+ const localUrl = kind === "stt" ? process.env.PI_VOICE_STT_URL : process.env.PI_VOICE_TTS_URL;
7496
+ const provider = requested || (localUrl ? "local" : "");
7497
+ 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"}.`);
7498
+ if (!ids.includes(provider)) throw makeHttpError(400, `Unsupported Natural Conversation ${kind.toUpperCase()} provider: ${provider}`);
7499
+ 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"}.`);
7500
+ 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.");
7501
+ 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.`);
7502
+ 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.");
7503
+ return provider;
7504
+ }
7505
+
7506
+ function requireRemoteMicConsentForStt(req, body = {}) {
7507
+ if (isLocalRequest(req)) return;
7508
+ if (body.remoteMicStreamingConsentAccepted === true || body.remoteMicStreamingConsent === true) return;
7509
+ 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.");
7510
+ }
7511
+
7512
+ function decodeVoiceAudioBody(body = {}) {
7513
+ const value = String(body.audioBase64 || body.audio || body.data || "").trim();
7514
+ if (!value) throw makeHttpError(400, "audioBase64 is required for Natural Conversation STT fallback uploads");
7515
+ const dataUrlMatch = value.match(/^data:([^;,]+);base64,(.*)$/s);
7516
+ const mimeType = String(body.mimeType || body.contentType || dataUrlMatch?.[1] || "audio/webm").toLowerCase();
7517
+ if (!VOICE_AUDIO_MIME_TYPES.has(mimeType)) throw makeHttpError(415, `Unsupported voice audio mime type: ${mimeType}`);
7518
+ const base64 = (dataUrlMatch?.[2] || value).replace(/\s+/g, "");
7519
+ if (!/^[A-Za-z0-9+/]*={0,2}$/.test(base64)) throw makeHttpError(400, "audioBase64 must be valid base64");
7520
+ const buffer = Buffer.from(base64, "base64");
7521
+ if (!buffer.length) throw makeHttpError(400, "audioBase64 decoded to an empty audio payload");
7522
+ if (buffer.length > VOICE_AUDIO_BODY_LIMIT_BYTES) throw makeHttpError(413, `Audio payload is too large (limit ${formatBytes(VOICE_AUDIO_BODY_LIMIT_BYTES)})`);
7523
+ return {
7524
+ buffer,
7525
+ mimeType,
7526
+ fileName: String(body.fileName || `speech.${mimeType.includes("wav") ? "wav" : mimeType.includes("mpeg") || mimeType.includes("mp3") ? "mp3" : "webm"}`).replace(/[\\/\0]/g, "_").slice(0, 120),
7527
+ };
7528
+ }
7529
+
7530
+ function voiceFetchError(provider, response, text) {
7531
+ const status = response.status >= 500 ? 502 : 400;
7532
+ return makeHttpError(status, `${provider} voice provider returned HTTP ${response.status}: ${truncateStatusText(text || response.statusText || "request failed", 500)}`);
7533
+ }
7534
+
7535
+ async function fetchVoiceProvider(provider, url, init = {}) {
7536
+ const response = await fetch(url, { ...init, signal: AbortSignal.timeout(VOICE_PROVIDER_TIMEOUT_MS) });
7537
+ if (!response.ok) throw voiceFetchError(provider, response, await response.text().catch(() => ""));
7538
+ return response;
7539
+ }
7540
+
7541
+ function voiceFormData(audio, body = {}, model = "") {
7542
+ const form = new FormData();
7543
+ form.set("file", new Blob([audio.buffer], { type: audio.mimeType }), audio.fileName);
7544
+ if (model) form.set("model", model);
7545
+ if (body.language) form.set("language", String(body.language));
7546
+ if (body.prompt) form.set("prompt", String(body.prompt));
7547
+ return form;
7548
+ }
7549
+
7550
+ function transcriptTextFromProviderJson(json) {
7551
+ return String(json?.text || json?.transcript || json?.data?.text || json?.result?.text || "").trim();
7552
+ }
7553
+
7554
+ async function parseTranscriptResponse(provider, response) {
7555
+ const contentType = String(response.headers.get("content-type") || "").toLowerCase();
7556
+ if (contentType.includes("application/json")) {
7557
+ const json = await response.json();
7558
+ const text = transcriptTextFromProviderJson(json);
7559
+ if (!text) throw makeHttpError(502, `${provider} STT provider returned JSON without a transcript text field`);
7560
+ return text;
7561
+ }
7562
+ const text = (await response.text()).trim();
7563
+ if (!text) throw makeHttpError(502, `${provider} STT provider returned an empty transcript`);
7564
+ return text;
7565
+ }
7566
+
7567
+ async function transcribeWithLocalProvider(audio, body = {}) {
7568
+ const form = voiceFormData(audio, body, body.model ? String(body.model) : "");
7569
+ const response = await fetchVoiceProvider("local STT", process.env.PI_VOICE_STT_URL, { method: "POST", body: form });
7570
+ return parseTranscriptResponse("local", response);
7571
+ }
7572
+
7573
+ async function transcribeWithOpenAiCompatibleProvider(provider, audio, body = {}) {
7574
+ const openai = provider === "openai";
7575
+ const url = openai ? "https://api.openai.com/v1/audio/transcriptions" : "https://api.groq.com/openai/v1/audio/transcriptions";
7576
+ 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"));
7577
+ const form = voiceFormData(audio, body, model);
7578
+ form.set("response_format", "json");
7579
+ const response = await fetchVoiceProvider(`${provider} STT`, url, {
7580
+ method: "POST",
7581
+ headers: { authorization: `Bearer ${openai ? process.env.OPENAI_API_KEY : process.env.GROQ_API_KEY}` },
7582
+ body: form,
7583
+ });
7584
+ return parseTranscriptResponse(provider, response);
7585
+ }
7586
+
7587
+ async function handleNaturalConversationSttTranscribe(req, tab, body = {}) {
7588
+ requireRemoteMicConsentForStt(req, body);
7589
+ const provider = requestedNaturalConversationVoiceProvider("stt", body);
7590
+ const audio = decodeVoiceAudioBody(body);
7591
+ const text = provider === "local" ? await transcribeWithLocalProvider(audio, body) : await transcribeWithOpenAiCompatibleProvider(provider, audio, body);
7592
+ return { provider, text, mimeType: audio.mimeType, byteLength: audio.buffer.length, mode: naturalConversationModeSnapshot(tab) };
7593
+ }
7594
+
7595
+ function normalizeVoiceText(body = {}) {
7596
+ const text = String(body.text || body.input || "").trim();
7597
+ if (!text) throw makeHttpError(400, "text is required for Natural Conversation TTS fallback synthesis");
7598
+ if (text.length > VOICE_TTS_TEXT_MAX_CHARS) throw makeHttpError(413, `TTS text is too long (limit ${VOICE_TTS_TEXT_MAX_CHARS} characters)`);
7599
+ return text;
7600
+ }
7601
+
7602
+ async function audioPayloadFromResponse(provider, response, preferredFormat = "") {
7603
+ const contentType = String(response.headers.get("content-type") || "").toLowerCase();
7604
+ if (contentType.includes("application/json")) {
7605
+ const json = await response.json();
7606
+ const audioBase64 = String(json.audioBase64 || json.audio || json.data?.audioBase64 || "").trim();
7607
+ if (!audioBase64) throw makeHttpError(502, `${provider} TTS provider returned JSON without audioBase64`);
7608
+ const buffer = Buffer.from(audioBase64, "base64");
7609
+ return { audioBase64, contentType: json.mimeType || json.contentType || "audio/mpeg", format: json.format || preferredFormat || "mp3", byteLength: buffer.length };
7610
+ }
7611
+ const buffer = Buffer.from(await response.arrayBuffer());
7612
+ if (!buffer.length) throw makeHttpError(502, `${provider} TTS provider returned empty audio`);
7613
+ return { audioBase64: buffer.toString("base64"), contentType: contentType || "audio/mpeg", format: preferredFormat || (contentType.includes("wav") ? "wav" : "mp3"), byteLength: buffer.length };
7614
+ }
7615
+
7616
+ async function synthesizeWithLocalProvider(text, body = {}) {
7617
+ const response = await fetchVoiceProvider("local TTS", process.env.PI_VOICE_TTS_URL, {
7618
+ method: "POST",
7619
+ headers: { "content-type": "application/json" },
7620
+ body: JSON.stringify({ text, voice: body.voice || process.env.PI_VOICE_TTS_VOICE || undefined, format: body.format || process.env.PI_VOICE_TTS_FORMAT || "mp3" }),
7621
+ });
7622
+ return audioPayloadFromResponse("local", response, body.format || process.env.PI_VOICE_TTS_FORMAT || "mp3");
7623
+ }
7624
+
7625
+ async function synthesizeWithOpenAiProvider(text, body = {}) {
7626
+ const format = String(body.format || process.env.PI_VOICE_OPENAI_TTS_FORMAT || "mp3");
7627
+ const response = await fetchVoiceProvider("openai TTS", "https://api.openai.com/v1/audio/speech", {
7628
+ method: "POST",
7629
+ headers: { authorization: `Bearer ${process.env.OPENAI_API_KEY}`, "content-type": "application/json" },
7630
+ 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 }),
7631
+ });
7632
+ return audioPayloadFromResponse("openai", response, format);
7633
+ }
7634
+
7635
+ async function handleNaturalConversationTtsSpeech(_req, tab, body = {}) {
7636
+ const provider = requestedNaturalConversationVoiceProvider("tts", body);
7637
+ const text = normalizeVoiceText(body);
7638
+ const audio = provider === "local" ? await synthesizeWithLocalProvider(text, body) : await synthesizeWithOpenAiProvider(text, body);
7639
+ return { provider, ...audio, mode: naturalConversationModeSnapshot(tab) };
7640
+ }
7641
+
7198
7642
  function resolveCliPath(value) {
7199
7643
  const text = String(value || "").trim();
7200
7644
  if (!text) return "";
@@ -8678,9 +9122,40 @@ const server = createServer(async (req, res) => {
8678
9122
  return;
8679
9123
  }
8680
9124
 
8681
- if ((url.pathname === "/api/stt/transcribe" || url.pathname === "/api/tts/speech") && req.method === "POST") {
9125
+ if (url.pathname === "/api/conversation-voices" && req.method === "GET") {
8682
9126
  const tab = getRequestedTab(req, url);
8683
- sendJson(res, 501, { ok: false, feature_unavailable: true, data: naturalConversationUnavailableResponse(tab), error: "Natural Conversation hosted/local STT/TTS fallbacks are not implemented in this Web UI shell yet." });
9127
+ sendJson(res, 200, { ok: true, data: await conversationVoicesData(), tab: tabMeta(tab) });
9128
+ return;
9129
+ }
9130
+
9131
+ if (url.pathname === "/api/conversation-voice" && req.method === "POST") {
9132
+ const body = await readJsonBody(req);
9133
+ const tab = getRequestedTab(req, url, body);
9134
+ sendJson(res, 200, { ok: true, data: await setConversationVoice(tab, body), tab: tabMeta(tab) });
9135
+ return;
9136
+ }
9137
+
9138
+ if (url.pathname === "/api/stt/transcribe" && req.method === "POST") {
9139
+ const body = await readJsonBody(req, { limitBytes: VOICE_AUDIO_JSON_BODY_LIMIT_BYTES });
9140
+ const tab = getRequestedTab(req, url, body);
9141
+ try {
9142
+ sendJson(res, 200, { ok: true, data: await handleNaturalConversationSttTranscribe(req, tab, body) });
9143
+ } catch (error) {
9144
+ const statusCode = error?.statusCode || 500;
9145
+ sendJson(res, statusCode, { ok: false, feature_unavailable: statusCode === 501, data: naturalConversationUnavailableResponse(tab, "stt"), voice: error?.voice || naturalConversationVoiceProviderStatus("stt"), error: statusCode >= 500 ? sanitizeError(error) : formatCliError(error) });
9146
+ }
9147
+ return;
9148
+ }
9149
+
9150
+ if (url.pathname === "/api/tts/speech" && req.method === "POST") {
9151
+ const body = await readJsonBody(req);
9152
+ const tab = getRequestedTab(req, url, body);
9153
+ try {
9154
+ sendJson(res, 200, { ok: true, data: await handleNaturalConversationTtsSpeech(req, tab, body) });
9155
+ } catch (error) {
9156
+ const statusCode = error?.statusCode || 500;
9157
+ sendJson(res, statusCode, { ok: false, feature_unavailable: statusCode === 501, data: naturalConversationUnavailableResponse(tab, "tts"), voice: error?.voice || naturalConversationVoiceProviderStatus("tts"), error: statusCode >= 500 ? sanitizeError(error) : formatCliError(error) });
9158
+ }
8684
9159
  return;
8685
9160
  }
8686
9161
 
@@ -71,7 +71,11 @@ function runCommand(command, args, { cwd, timeoutMs = GIT_DEFAULT_TIMEOUT_MS, ma
71
71
  if (stderr.length > maxOutputLength) stderr = stderr.slice(-maxOutputLength);
72
72
  });
73
73
  child.on("error", (error) => finish({ exitCode: undefined, stdout, stderr, error: error?.message || String(error) }));
74
- child.on("exit", (exitCode, signal) => finish({ exitCode, signal, stdout, stderr, timedOut: false }));
74
+ // "close", not "exit": exit can fire before the stdio pipes flush, which
75
+ // intermittently yields empty/truncated stdout for a successful command
76
+ // (e.g. `git rev-parse --show-toplevel` → "" → path.resolve("") →
77
+ // process.cwd() → worktree data read from the wrong repository).
78
+ child.on("close", (exitCode, signal) => finish({ exitCode, signal, stdout, stderr, timedOut: false }));
75
79
  });
76
80
  }
77
81