@firstpick/pi-package-webui 0.5.8 → 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/bin/pi-webui.mjs CHANGED
@@ -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;
@@ -238,6 +242,7 @@ const unavailableNative = (command, details = {}) => nativeCommandUnavailable(co
238
242
  function parseSlashCommand(message) {
239
243
  return parseNativeSlashCommand(message, NATIVE_SLASH_COMMAND_NAMES);
240
244
  }
245
+ const NATURAL_CONVERSATION_FEATURE_ID = "naturalConversation";
241
246
  const OPTIONAL_FEATURE_PACKAGES = new Map([
242
247
  ["btwCommand", "@firstpick/pi-extension-btw"],
243
248
  ["gitWorkflow", "@firstpick/pi-prompts-git-pr"],
@@ -249,11 +254,19 @@ const OPTIONAL_FEATURE_PACKAGES = new Map([
249
254
  ["todoProgressWidget", "@firstpick/pi-extension-todo-progress"],
250
255
  ["tuiToolsCommand", "@firstpick/pi-extension-tools"],
251
256
  ["remoteWebui", "@firstpick/pi-package-remote-webui"],
257
+ ["naturalConversation", "@firstpick/pi-package-natural-conversation"],
252
258
  ["gitFooterStatus", "@firstpick/pi-extension-git-footer-status"],
253
259
  ["statsCommand", "@firstpick/pi-extension-stats"],
254
260
  ["themeBundle", "@firstpick/pi-themes-bundle"],
255
261
  ]);
256
- const WEBUI_CONTROLLED_PACKAGES = new Set([WEBUI_PACKAGE, ...OPTIONAL_FEATURE_PACKAGES.values()]);
262
+ const WEBUI_CONTROLLED_PACKAGES = new Set([
263
+ WEBUI_PACKAGE,
264
+ ...[...OPTIONAL_FEATURE_PACKAGES.entries()]
265
+ .filter(([featureId]) => featureId !== NATURAL_CONVERSATION_FEATURE_ID)
266
+ .map(([, packageName]) => packageName),
267
+ ]);
268
+ const NATURAL_CONVERSATION_STATUS_KEY = "natural-conversation";
269
+ const NATURAL_CONVERSATION_COMMAND_NAMES = ["talk", "voice", "conversation"];
257
270
  const PACKAGE_NAME_CACHE = new Map();
258
271
 
259
272
  function usage() {
@@ -1105,7 +1118,10 @@ function runCommand(command, args, { cwd, timeoutMs = 2000, maxOutputLength = 20
1105
1118
  if (stderr.length > maxOutputLength) stderr = stderr.slice(-maxOutputLength);
1106
1119
  });
1107
1120
  child.on("error", (error) => finish({ exitCode: undefined, stdout, stderr: sanitizeError(error), error: sanitizeError(error) }));
1108
- child.on("exit", (exitCode) => finish({ exitCode, stdout, stderr, timedOut: false }));
1121
+ // "close", not "exit": exit can fire before the stdio pipes flush, which
1122
+ // intermittently truncates stdout (empty `git rev-parse --show-toplevel`
1123
+ // output resolves to process.cwd() and reads the wrong repository).
1124
+ child.on("close", (exitCode) => finish({ exitCode, stdout, stderr, timedOut: false }));
1109
1125
  });
1110
1126
  }
1111
1127
 
@@ -1119,6 +1135,31 @@ function nodeModulesParentForPackageRoot(root = packageRoot) {
1119
1135
  return root;
1120
1136
  }
1121
1137
 
1138
+ function prependNodePathEntries(entries) {
1139
+ const existing = String(process.env.NODE_PATH || "").split(path.delimiter).filter(Boolean);
1140
+ const seen = new Set();
1141
+ const next = [];
1142
+ for (const entry of [...entries, ...existing]) {
1143
+ if (!entry) continue;
1144
+ const normalized = path.resolve(entry);
1145
+ const key = process.platform === "win32" ? normalized.toLowerCase() : normalized;
1146
+ if (seen.has(key)) continue;
1147
+ seen.add(key);
1148
+ next.push(normalized);
1149
+ }
1150
+ if (next.length) process.env.NODE_PATH = next.join(path.delimiter);
1151
+ }
1152
+
1153
+ async function configureDevDependencyResolution() {
1154
+ if (!webuiDevServer) return;
1155
+ const workspaceRoot = await devWorkspaceRoot();
1156
+ prependNodePathEntries([
1157
+ workspaceRoot ? path.join(workspaceRoot, "node_modules") : "",
1158
+ path.join(packageRoot, "node_modules"),
1159
+ path.join(configuredAgentNpmRoot(), "node_modules"),
1160
+ ]);
1161
+ }
1162
+
1122
1163
  function declaredDependencySpec(pkg, packageName) {
1123
1164
  return firstDefined(
1124
1165
  pkg?.dependencies?.[packageName],
@@ -2470,7 +2511,7 @@ function scheduleAppRunnerBroadcast(tab) {
2470
2511
  }
2471
2512
 
2472
2513
  function terminateAppRunnerChild(run, signal = "SIGTERM") {
2473
- if (!run?.child || run.child.killed) return false;
2514
+ if (!run?.child || run.child.exitCode !== null || run.child.signalCode !== null) return false;
2474
2515
  try {
2475
2516
  if (process.platform !== "win32" && run.pid) process.kill(-run.pid, signal);
2476
2517
  else run.child.kill(signal);
@@ -2485,6 +2526,24 @@ function terminateAppRunnerChild(run, signal = "SIGTERM") {
2485
2526
  }
2486
2527
  }
2487
2528
 
2529
+ function appRunnerStdinWritable(run) {
2530
+ const stdin = run?.child?.stdin;
2531
+ return !!stdin && !stdin.destroyed && !stdin.writableEnded && run.stdinClosed !== true;
2532
+ }
2533
+
2534
+ function interruptAppRunnerChild(run) {
2535
+ if (!run?.child || run.child.exitCode !== null || run.child.signalCode !== null) return false;
2536
+ if (appRunnerStdinWritable(run) && (run.executionMode === "pty" || process.platform === "win32")) {
2537
+ try {
2538
+ run.child.stdin.write("\x03", "utf8");
2539
+ return true;
2540
+ } catch {
2541
+ // Fall back to process signals below.
2542
+ }
2543
+ }
2544
+ return terminateAppRunnerChild(run, "SIGINT");
2545
+ }
2546
+
2488
2547
  function finishAppRunner(tab, run, patch = {}) {
2489
2548
  if (!run || run.settled) return;
2490
2549
  run.settled = true;
@@ -2600,13 +2659,14 @@ function stopAppRunnerForTab(tab, reason = "stop requested", { force = false } =
2600
2659
  const run = tab?.appRunner;
2601
2660
  if (!run || run.status !== "running") return false;
2602
2661
  run.stopping = true;
2603
- appendAppRunnerLine(run, `# ${reason}; sending ${force ? "SIGKILL" : "SIGTERM"}`);
2604
- terminateAppRunnerChild(run, force ? "SIGKILL" : "SIGTERM");
2662
+ appendAppRunnerLine(run, `# ${reason}; sending ${force ? "SIGKILL" : "Ctrl+C"}`);
2663
+ if (force) terminateAppRunnerChild(run, "SIGKILL");
2664
+ else interruptAppRunnerChild(run);
2605
2665
  if (!force) {
2606
2666
  clearTimeout(run.stopTimer);
2607
2667
  run.stopTimer = setTimeout(() => {
2608
2668
  if (run.status === "running") {
2609
- appendAppRunnerLine(run, "# app runner did not stop; sending SIGKILL");
2669
+ appendAppRunnerLine(run, "# app runner did not stop after Ctrl+C; sending SIGKILL");
2610
2670
  terminateAppRunnerChild(run, "SIGKILL");
2611
2671
  scheduleAppRunnerBroadcast(tab);
2612
2672
  }
@@ -4268,7 +4328,9 @@ function runWorkflowCommand(command, args, { cwd, label = formatWorkflowCommand(
4268
4328
  if (stderr.length > 100000) stderr = stderr.slice(-100000);
4269
4329
  });
4270
4330
  child.on("error", (error) => finish({ exitCode: undefined, stderr: stderr || sanitizeError(error), error: sanitizeError(error) }));
4271
- child.on("exit", (exitCode, signal) => finish({ exitCode, signal }));
4331
+ // "close", not "exit": exit can fire before the stdio pipes flush,
4332
+ // intermittently truncating collected stdout/stderr.
4333
+ child.on("close", (exitCode, signal) => finish({ exitCode, signal }));
4272
4334
  });
4273
4335
  }
4274
4336
 
@@ -4485,7 +4547,7 @@ async function readBundledThemes() {
4485
4547
  function normalizeStaticPath(urlPath) {
4486
4548
  if (urlPath === "/") return "index.html";
4487
4549
  const name = urlPath.startsWith("/") ? urlPath.slice(1) : urlPath;
4488
- 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;
4550
+ 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;
4489
4551
  return name;
4490
4552
  }
4491
4553
 
@@ -4801,6 +4863,7 @@ try {
4801
4863
 
4802
4864
  process.env.PI_WEBUI_HOST = options.host;
4803
4865
  process.env.PI_WEBUI_PORT = String(options.port);
4866
+ await configureDevDependencyResolution();
4804
4867
 
4805
4868
  const startupDelayMs = Number.parseInt(process.env.PI_WEBUI_START_DELAY_MS || "", 10);
4806
4869
  delete process.env.PI_WEBUI_START_DELAY_MS;
@@ -5071,9 +5134,19 @@ function rememberTabState(tab, state) {
5071
5134
  if (!options.noSession && Object.prototype.hasOwnProperty.call(state, "sessionFile")) tab.sessionFile = sessionFileFromState(state);
5072
5135
  }
5073
5136
 
5137
+ function patchTabState(tab, patch) {
5138
+ if (!tab || !patch || typeof patch !== "object") return;
5139
+ tab.lastState = { ...(tab.lastState || {}), ...patch };
5140
+ }
5141
+
5074
5142
  function stateWithPendingThinking(tab, state) {
5075
- if (!state || typeof state !== "object" || !tab?.pendingThinkingLevel) return state;
5076
- return { ...state, pendingThinkingLevel: tab.pendingThinkingLevel };
5143
+ if (!state || typeof state !== "object") return state;
5144
+ const queueLength = tab ? compactionQueueForTab(tab).length : 0;
5145
+ if (!tab?.pendingThinkingLevel && queueLength === 0) return state;
5146
+ const patch = {};
5147
+ if (tab?.pendingThinkingLevel) patch.pendingThinkingLevel = tab.pendingThinkingLevel;
5148
+ if (queueLength > 0) patch.pendingMessageCount = Number(state.pendingMessageCount || 0) + queueLength;
5149
+ return { ...state, ...patch };
5077
5150
  }
5078
5151
 
5079
5152
  function responseWithPendingThinking(tab, response) {
@@ -5167,6 +5240,96 @@ function clearExtensionWidgets(tab) {
5167
5240
  tab?.extensionWidgets?.clear();
5168
5241
  }
5169
5242
 
5243
+ function naturalConversationStatusState(statusText) {
5244
+ const text = stripAnsi(statusText).replace(/\s+/g, " ").trim();
5245
+ if (!text) return { enabled: false, uiState: "off", statusText: "" };
5246
+ const match = text.match(/voice\s*:\s*([a-z0-9_-]+)/i);
5247
+ return { enabled: true, uiState: (match?.[1] || "listening").toLowerCase(), statusText: text };
5248
+ }
5249
+
5250
+ function naturalConversationModeSnapshot(tab, patch = {}) {
5251
+ const previous = tab?.conversationMode && typeof tab.conversationMode === "object" ? tab.conversationMode : {};
5252
+ const status = naturalConversationStatusState(extensionStatusMap(tab).get(NATURAL_CONVERSATION_STATUS_KEY) || previous.statusText || "");
5253
+ const enabled = patch.enabled ?? status.enabled ?? previous.enabled ?? false;
5254
+ const uiState = patch.uiState || (enabled ? status.uiState || previous.uiState || "listening" : "off");
5255
+ return {
5256
+ featureId: NATURAL_CONVERSATION_FEATURE_ID,
5257
+ available: patch.available ?? previous.available ?? false,
5258
+ enabled,
5259
+ uiState,
5260
+ statusText: patch.statusText ?? status.statusText ?? previous.statusText ?? "",
5261
+ allowedTools: Array.isArray(patch.allowedTools) ? patch.allowedTools : Array.isArray(previous.allowedTools) ? previous.allowedTools : ["read", "grep", "find", "ls"],
5262
+ provider: patch.provider || previous.provider || "browser-shell",
5263
+ startedAt: patch.startedAt ?? previous.startedAt ?? null,
5264
+ packageInstalled: patch.packageInstalled ?? previous.packageInstalled ?? false,
5265
+ loadedCommands: Array.isArray(patch.loadedCommands) ? patch.loadedCommands : Array.isArray(previous.loadedCommands) ? previous.loadedCommands : [],
5266
+ updatedAt: patch.updatedAt || previous.updatedAt || new Date().toISOString(),
5267
+ };
5268
+ }
5269
+
5270
+ function resetNaturalConversationMode(tab) {
5271
+ if (!tab) return;
5272
+ tab.conversationMode = naturalConversationModeSnapshot(tab, { available: false, enabled: false, uiState: "off", statusText: "", loadedCommands: [] });
5273
+ }
5274
+
5275
+ function rememberNaturalConversationStatusEvent(tab, event) {
5276
+ if (event?.type !== "extension_ui_request" || event.method !== "setStatus" || event.statusKey !== NATURAL_CONVERSATION_STATUS_KEY) return;
5277
+ const status = naturalConversationStatusState(event.statusText || "");
5278
+ tab.conversationMode = naturalConversationModeSnapshot(tab, {
5279
+ ...status,
5280
+ available: true,
5281
+ loadedCommands: tab?.conversationMode?.loadedCommands?.length ? tab.conversationMode.loadedCommands : ["talk"],
5282
+ });
5283
+ }
5284
+
5285
+ function isNaturalConversationActive(tab) {
5286
+ return naturalConversationModeSnapshot(tab).enabled === true;
5287
+ }
5288
+
5289
+ function naturalConversationCommandBaseName(name) {
5290
+ return String(name || "").trim().toLowerCase().replace(/:\d+$/, "");
5291
+ }
5292
+
5293
+ function naturalConversationSlashCommandName(message) {
5294
+ const match = String(message || "").trim().match(/^\/([^\s]+)/);
5295
+ return match ? naturalConversationCommandBaseName(match[1]) : "";
5296
+ }
5297
+
5298
+ function isNaturalConversationSlashCommand(message) {
5299
+ return NATURAL_CONVERSATION_COMMAND_NAMES.includes(naturalConversationSlashCommandName(message));
5300
+ }
5301
+
5302
+ function blockNaturalConversationAction(action) {
5303
+ throw makeHttpError(409, `Natural Conversation Mode is active; ${action}. Leave the mode first with /talk off.`);
5304
+ }
5305
+
5306
+ function ensureNaturalConversationRouteAllowed(tab, action) {
5307
+ if (isNaturalConversationActive(tab)) blockNaturalConversationAction(action);
5308
+ }
5309
+
5310
+ async function ensureNaturalConversationPromptSafety(tab, command) {
5311
+ if (!isNaturalConversationActive(tab) || !["prompt", "steer", "follow_up"].includes(command?.type)) return null;
5312
+ tab.pendingThinkingLevel = undefined;
5313
+ const stateResult = await safeRpcData(tab, { type: "get_state" }, STATUS_RPC_TIMEOUT_MS);
5314
+ if (stateResult.ok && stateIsBusyForSettings(stateResult.data)) return null;
5315
+ const response = await setThinkingLevelForTab(tab, "off", { allowPending: false });
5316
+ return response?.success === false ? response : null;
5317
+ }
5318
+
5319
+ function enforceNaturalConversationCommandAllowed(tab, command) {
5320
+ if (!isNaturalConversationActive(tab)) return;
5321
+ if (command?.type === "set_thinking_level") {
5322
+ if (command.level !== "off") blockNaturalConversationAction("thinking is forced off");
5323
+ return;
5324
+ }
5325
+ if (!["prompt", "steer", "follow_up", "abort", "abort_bash"].includes(command?.type)) {
5326
+ blockNaturalConversationAction(`${command?.type || "this action"} is blocked`);
5327
+ }
5328
+ if (command?.type === "prompt" && naturalConversationSlashCommandName(command.message) && !isNaturalConversationSlashCommand(command.message)) {
5329
+ blockNaturalConversationAction("slash commands are blocked from the Web UI shell");
5330
+ }
5331
+ }
5332
+
5170
5333
  function replayExtensionStatuses(tab, res) {
5171
5334
  for (const [statusKey, statusText] of extensionStatusMap(tab)) {
5172
5335
  sendSse(res, {
@@ -5207,6 +5370,160 @@ function bashQueueForTab(tab) {
5207
5370
  return tab.bashQueue;
5208
5371
  }
5209
5372
 
5373
+ function compactionQueueForTab(tab) {
5374
+ if (!tab.compactionQueue) tab.compactionQueue = [];
5375
+ return tab.compactionQueue;
5376
+ }
5377
+
5378
+ function queueableCompactionCommand(command) {
5379
+ return ["prompt", "steer", "follow_up"].includes(command?.type) && !!String(command.message || "").trim();
5380
+ }
5381
+
5382
+ function compactionQueueMode(command) {
5383
+ if (command?.type === "follow_up" || command?.streamingBehavior === "followUp") return "followUp";
5384
+ return "steer";
5385
+ }
5386
+
5387
+ function compactQueuedCommand(command) {
5388
+ const queued = {
5389
+ type: command.type,
5390
+ message: String(command.message || ""),
5391
+ mode: compactionQueueMode(command),
5392
+ };
5393
+ if (Array.isArray(command.images) && command.images.length) queued.images = command.images;
5394
+ return queued;
5395
+ }
5396
+
5397
+ function compactionQueueEvent(tab, extra = {}) {
5398
+ const queue = compactionQueueForTab(tab);
5399
+ const steering = [];
5400
+ const followUp = [];
5401
+ for (const item of queue) {
5402
+ const message = String(item?.command?.message || "").trim();
5403
+ if (!message) continue;
5404
+ if (item.command.mode === "followUp") followUp.push(message);
5405
+ else steering.push(message);
5406
+ }
5407
+ return {
5408
+ type: "webui_compaction_queue_update",
5409
+ tabId: tab.id,
5410
+ tabTitle: tab.title,
5411
+ queueLength: queue.length,
5412
+ pendingMessageCount: queue.length,
5413
+ steering,
5414
+ followUp,
5415
+ draining: tab.compactionQueueDraining === true,
5416
+ tabActivity: tabActivitySnapshot(tab),
5417
+ ...extra,
5418
+ };
5419
+ }
5420
+
5421
+ function enqueueCommandUntilCompactionEnds(tab, command) {
5422
+ const queue = compactionQueueForTab(tab);
5423
+ const item = {
5424
+ id: randomUUID(),
5425
+ command: compactQueuedCommand(command),
5426
+ enqueuedAt: new Date().toISOString(),
5427
+ };
5428
+ queue.push(item);
5429
+ broadcastTabEvent(tab, compactionQueueEvent(tab, { queuedId: item.id }));
5430
+ return rpcSuccess(command.type, {
5431
+ queued: true,
5432
+ queuedFor: "compaction",
5433
+ queueLength: queue.length,
5434
+ message: "Prompt queued; Pi will resume automatically after compaction finishes.",
5435
+ });
5436
+ }
5437
+
5438
+ function maybeQueueCommandDuringCompaction(tab, command) {
5439
+ if (!queueableCompactionCommand(command)) return null;
5440
+ if (!tab?.lastState?.isCompacting) return null;
5441
+ return enqueueCommandUntilCompactionEnds(tab, command);
5442
+ }
5443
+
5444
+ function queuedPromptCommand(item) {
5445
+ const command = { type: "prompt", message: item.command.message };
5446
+ if (Array.isArray(item.command.images) && item.command.images.length) command.images = item.command.images;
5447
+ return command;
5448
+ }
5449
+
5450
+ function queuedStreamingCommand(item) {
5451
+ const command = {
5452
+ type: item.command.mode === "followUp" ? "follow_up" : "steer",
5453
+ message: item.command.message,
5454
+ };
5455
+ if (Array.isArray(item.command.images) && item.command.images.length) command.images = item.command.images;
5456
+ return command;
5457
+ }
5458
+
5459
+ function queuedRetryCommand(item) {
5460
+ const message = String(item.command.message || "").trim();
5461
+ if (item.command.type === "prompt" && message.startsWith("/")) {
5462
+ const command = queuedPromptCommand(item);
5463
+ command.streamingBehavior = item.command.mode === "followUp" ? "followUp" : "steer";
5464
+ return command;
5465
+ }
5466
+ return queuedStreamingCommand(item);
5467
+ }
5468
+
5469
+ function requeueCompactionItems(tab, items) {
5470
+ if (!items?.length) return;
5471
+ const queue = compactionQueueForTab(tab);
5472
+ queue.unshift(...items);
5473
+ broadcastTabEvent(tab, compactionQueueEvent(tab));
5474
+ }
5475
+
5476
+ async function compactionFlushShouldJoinActiveRun(tab, event = {}) {
5477
+ if (event.willRetry === true) return true;
5478
+ await delay(80);
5479
+ const state = await safeRpcData(tab, { type: "get_state" }, STATUS_RPC_TIMEOUT_MS).catch(() => ({ ok: false }));
5480
+ if (state.ok) return !!state.data?.isStreaming;
5481
+ return !!tab.lastState?.isStreaming;
5482
+ }
5483
+
5484
+ async function flushCompactionQueue(tab, event = {}) {
5485
+ const queue = compactionQueueForTab(tab);
5486
+ if (!queue.length || tab.compactionQueueDraining) return;
5487
+ const items = queue.splice(0);
5488
+ tab.compactionQueueDraining = true;
5489
+ broadcastTabEvent(tab, compactionQueueEvent(tab));
5490
+ const remaining = [...items];
5491
+ try {
5492
+ const joinActiveRun = await compactionFlushShouldJoinActiveRun(tab, event);
5493
+ if (joinActiveRun) {
5494
+ while (remaining.length) {
5495
+ const item = remaining[0];
5496
+ const response = await tab.rpc.send(queuedRetryCommand(item));
5497
+ if (response.success === false) throw new Error(response.error || "failed to queue prompt after compaction");
5498
+ remaining.shift();
5499
+ }
5500
+ } else {
5501
+ let startedPrompt = false;
5502
+ while (remaining.length) {
5503
+ const item = remaining[0];
5504
+ const command = startedPrompt ? queuedStreamingCommand(item) : queuedPromptCommand(item);
5505
+ if (!startedPrompt) {
5506
+ const pendingThinkingResponse = await applyPendingThinkingBeforePrompt(tab);
5507
+ if (pendingThinkingResponse?.success === false) throw new Error(pendingThinkingResponse.error || "failed to apply queued thinking level");
5508
+ maybeNameTabForConversation(tab, command);
5509
+ markTabWorking(tab);
5510
+ }
5511
+ const response = await tab.rpc.send(command, command.type === "prompt" ? PROMPT_REQUEST_TIMEOUT_MS : REQUEST_TIMEOUT_MS);
5512
+ if (response.success === false) throw new Error(response.error || "failed to resume after compaction");
5513
+ remaining.shift();
5514
+ if (command.type === "prompt") startedPrompt = true;
5515
+ }
5516
+ }
5517
+ broadcastTabEvent(tab, compactionQueueEvent(tab, { drained: true }));
5518
+ } catch (error) {
5519
+ requeueCompactionItems(tab, remaining);
5520
+ broadcastTabEvent(tab, compactionQueueEvent(tab, { error: sanitizeError(error) }));
5521
+ } finally {
5522
+ tab.compactionQueueDraining = false;
5523
+ broadcastTabEvent(tab, compactionQueueEvent(tab));
5524
+ }
5525
+ }
5526
+
5210
5527
  function settleBashQueueItem(item, kind, value) {
5211
5528
  if (!item || item.settled) return;
5212
5529
  item.settled = true;
@@ -5446,13 +5763,24 @@ function updateTabActivityFromEvent(tab, event) {
5446
5763
  const timestamp = new Date().toISOString();
5447
5764
  switch (event?.type) {
5448
5765
  case "agent_start":
5766
+ patchTabState(tab, { isStreaming: true });
5767
+ markTabWorking(tab, timestamp);
5768
+ break;
5449
5769
  case "compaction_start":
5770
+ patchTabState(tab, { isCompacting: true });
5450
5771
  markTabWorking(tab, timestamp);
5451
5772
  break;
5452
5773
  case "agent_end":
5774
+ patchTabState(tab, { isStreaming: false });
5775
+ markTabDone(tab, timestamp);
5776
+ break;
5453
5777
  case "compaction_end":
5778
+ patchTabState(tab, { isCompacting: false });
5454
5779
  markTabDone(tab, timestamp);
5455
5780
  break;
5781
+ case "queue_update":
5782
+ patchTabState(tab, { pendingMessageCount: (event.steering?.length || 0) + (event.followUp?.length || 0) });
5783
+ break;
5456
5784
  case "pi_process_exit":
5457
5785
  case "pi_process_error":
5458
5786
  if (tab.activity?.isWorking) markTabDone(tab, timestamp);
@@ -5499,14 +5827,17 @@ function attachRpcToTab(tab, rpc) {
5499
5827
  clearPendingExtensionUiRequests(tab);
5500
5828
  clearExtensionStatuses(tab);
5501
5829
  clearExtensionWidgets(tab);
5830
+ resetNaturalConversationMode(tab);
5502
5831
  } else {
5503
5832
  rememberExtensionStatusEvent(tab, scopedEvent);
5833
+ rememberNaturalConversationStatusEvent(tab, scopedEvent);
5504
5834
  rememberExtensionWidgetEvent(tab, scopedEvent);
5505
5835
  trackPendingExtensionUiRequest(tab, scopedEvent);
5506
5836
  }
5507
5837
  scopedEvent = { ...scopedEvent, tabActivity: tabActivitySnapshot(tab), pendingExtensionUiRequestCount: pendingExtensionUiRequests(tab).length };
5508
5838
  recordEvent(scopedEvent);
5509
5839
  for (const client of tab.sseClients) sendSse(client, scopedEvent);
5840
+ if (event?.type === "compaction_end" && event.aborted !== true) void flushCompactionQueue(tab, event);
5510
5841
  });
5511
5842
  }
5512
5843
 
@@ -5544,10 +5875,13 @@ async function createTab({ id: requestedId, index, title, titleSource, conversat
5544
5875
  webuiHelperResponseIds: new Set(),
5545
5876
  bashQueue: [],
5546
5877
  bashQueueDraining: false,
5878
+ compactionQueue: [],
5879
+ compactionQueueDraining: false,
5547
5880
  rpc: undefined,
5548
5881
  rpcUnsubscribe: undefined,
5549
5882
  sseClients: new Set(),
5550
5883
  };
5884
+ resetNaturalConversationMode(tab);
5551
5885
 
5552
5886
  attachRpcToTab(tab, rpc);
5553
5887
  tabs.set(id, tab);
@@ -5591,6 +5925,7 @@ function tabMeta(tab) {
5591
5925
  pendingExtensionUiRequestCount: pendingExtensionUiRequests(tab).length,
5592
5926
  activity: tabActivitySnapshot(tab),
5593
5927
  appRunner: publicAppRunnerState(tab.appRunner),
5928
+ conversationMode: naturalConversationModeSnapshot(tab),
5594
5929
  };
5595
5930
  }
5596
5931
 
@@ -6132,6 +6467,7 @@ async function updateTabCwd(id, cwd) {
6132
6467
  clearPendingExtensionUiRequests(tab);
6133
6468
  clearExtensionStatuses(tab);
6134
6469
  clearExtensionWidgets(tab);
6470
+ resetNaturalConversationMode(tab);
6135
6471
  const rpc = new PiRpcProcess({ ...piCommand, cwd: tab.cwd });
6136
6472
  attachRpcToTab(tab, rpc);
6137
6473
  rpc.start();
@@ -6170,6 +6506,7 @@ async function restartTabRpc(tab, reason = "reload") {
6170
6506
  clearPendingExtensionUiRequests(tab);
6171
6507
  clearExtensionStatuses(tab);
6172
6508
  clearExtensionWidgets(tab);
6509
+ resetNaturalConversationMode(tab);
6173
6510
  const rpc = new PiRpcProcess({ ...piCommand, cwd: tab.cwd });
6174
6511
  attachRpcToTab(tab, rpc);
6175
6512
  rpc.start();
@@ -6754,19 +7091,402 @@ async function annotateSkillCommandState(tab, commands) {
6754
7091
  });
6755
7092
  }
6756
7093
 
6757
- async function getCommandData(tab) {
7094
+ function naturalConversationLoadedCommandNames(commands = []) {
7095
+ const names = [];
7096
+ const seen = new Set();
7097
+ for (const command of commands || []) {
7098
+ const baseName = naturalConversationCommandBaseName(command?.name || command?.invokeName || "");
7099
+ if (!NATURAL_CONVERSATION_COMMAND_NAMES.includes(baseName) || seen.has(baseName)) continue;
7100
+ seen.add(baseName);
7101
+ names.push(baseName);
7102
+ }
7103
+ return names;
7104
+ }
7105
+
7106
+ function rememberNaturalConversationCommands(tab, commands = []) {
7107
+ const loadedCommands = naturalConversationLoadedCommandNames(commands);
7108
+ const available = loadedCommands.length > 0;
7109
+ tab.conversationMode = naturalConversationModeSnapshot(tab, {
7110
+ available,
7111
+ enabled: available ? undefined : false,
7112
+ uiState: available ? undefined : "off",
7113
+ statusText: available ? undefined : "",
7114
+ loadedCommands,
7115
+ });
7116
+ return tab.conversationMode;
7117
+ }
7118
+
7119
+ async function getCommandData(tab, { annotateSkills = true } = {}) {
6758
7120
  try {
6759
7121
  const response = await tab.rpc.send({ type: "get_commands" });
6760
7122
  if (response.success === false) throw makeHttpError(400, response.error || "failed to load commands");
6761
- const rpcCommands = await annotateSkillCommandState(tab, response.data?.commands || []);
7123
+ const rawCommands = (response.data?.commands || []).filter((command) => command?.name !== WEBUI_HELPER_COMMAND);
7124
+ const rpcCommands = annotateSkills ? await annotateSkillCommandState(tab, rawCommands) : rawCommands;
7125
+ rememberNaturalConversationCommands(tab, rpcCommands);
6762
7126
  return { commands: [...NATIVE_SLASH_COMMANDS, ...rpcCommands], rpcRunning: true };
6763
7127
  } catch (error) {
6764
7128
  const message = sanitizeError(error);
6765
7129
  if (!/Pi RPC process is not running/i.test(message)) throw error;
7130
+ rememberNaturalConversationCommands(tab, []);
6766
7131
  return { commands: [...NATIVE_SLASH_COMMANDS], rpcRunning: false, error: message };
6767
7132
  }
6768
7133
  }
6769
7134
 
7135
+ async function naturalConversationPackageStatus() {
7136
+ try {
7137
+ return await optionalFeaturePackageStatus(NATURAL_CONVERSATION_FEATURE_ID);
7138
+ } catch (error) {
7139
+ return { featureId: NATURAL_CONVERSATION_FEATURE_ID, packageName: OPTIONAL_FEATURE_PACKAGES.get(NATURAL_CONVERSATION_FEATURE_ID), installed: false, error: sanitizeError(error) };
7140
+ }
7141
+ }
7142
+
7143
+ async function naturalConversationFeatureData(tab, { refreshCommands = true } = {}) {
7144
+ let commandData = null;
7145
+ let commandError = "";
7146
+ if (refreshCommands) {
7147
+ try {
7148
+ commandData = await getCommandData(tab, { annotateSkills: false });
7149
+ } catch (error) {
7150
+ commandError = sanitizeError(error);
7151
+ rememberNaturalConversationCommands(tab, []);
7152
+ }
7153
+ }
7154
+ const packageStatus = await naturalConversationPackageStatus();
7155
+ tab.conversationMode = naturalConversationModeSnapshot(tab, { packageInstalled: packageStatus.installed === true });
7156
+ const mode = naturalConversationModeSnapshot(tab);
7157
+ const available = mode.available === true;
7158
+ return {
7159
+ featureId: NATURAL_CONVERSATION_FEATURE_ID,
7160
+ packageName: OPTIONAL_FEATURE_PACKAGES.get(NATURAL_CONVERSATION_FEATURE_ID),
7161
+ available,
7162
+ packageInstalled: packageStatus.installed === true,
7163
+ packageStatus,
7164
+ commands: mode.loadedCommands,
7165
+ mode,
7166
+ rpcRunning: commandData?.rpcRunning !== false && !commandError,
7167
+ unavailableReason: available
7168
+ ? ""
7169
+ : packageStatus.installed
7170
+ ? "Natural Conversation package is installed, but /talk is not loaded in the active Pi tab. Reload the tab or enable the package extension."
7171
+ : "Natural Conversation package is not installed or not visible from the Web UI package root.",
7172
+ error: commandError || undefined,
7173
+ };
7174
+ }
7175
+
7176
+ async function setNaturalConversationMode(tab, body = {}) {
7177
+ const desired = body.enabled === true;
7178
+ const feature = await naturalConversationFeatureData(tab);
7179
+ if (!feature.available) throw makeHttpError(404, feature.unavailableReason);
7180
+ const commandName = feature.commands.find((name) => name === "talk") || feature.commands[0] || "talk";
7181
+ const response = await tab.rpc.send({ type: "prompt", message: `/${commandName} ${desired ? "on" : "off"}` }, REQUEST_TIMEOUT_MS);
7182
+ if (response.success === false) throw makeHttpError(400, response.error || `Failed to ${desired ? "enable" : "disable"} Natural Conversation Mode`);
7183
+ tab.conversationMode = naturalConversationModeSnapshot(tab, {
7184
+ available: true,
7185
+ enabled: desired,
7186
+ uiState: desired ? "listening" : "off",
7187
+ statusText: desired ? `Voice: listening` : "",
7188
+ loadedCommands: feature.commands,
7189
+ packageInstalled: feature.packageInstalled,
7190
+ startedAt: desired ? naturalConversationModeSnapshot(tab).startedAt || new Date().toISOString() : null,
7191
+ });
7192
+ if (desired) {
7193
+ tab.pendingThinkingLevel = undefined;
7194
+ await setThinkingLevelForTab(tab, "off", { allowPending: false }).catch(() => null);
7195
+ }
7196
+ return { ...(await naturalConversationFeatureData(tab, { refreshCommands: false })), response };
7197
+ }
7198
+
7199
+ // Piper voice switching for the native /talk audio loop. The WebUI never
7200
+ // imports the natural-conversation package; it mirrors the package's small
7201
+ // voice catalog for display, reads voice.json / the piper voice dir for
7202
+ // state, and performs the actual switch (incl. download) by sending
7203
+ // `/talk voice <id>` over RPC so the package stays the single writer.
7204
+ const CONVERSATION_VOICE_CATALOG = [
7205
+ { id: "en_US-lessac-medium", file: "en_US-lessac-medium.onnx", sizeMb: 63, note: "natural US English" },
7206
+ { id: "en_GB-alba-medium", file: "en_GB-alba-medium.onnx", sizeMb: 63, note: "natural British English" },
7207
+ { id: "de_DE-thorsten-medium", file: "de_DE-thorsten-medium.onnx", sizeMb: 63, note: "natural German" },
7208
+ { id: "de_DE-thorsten-high", file: "de_DE-thorsten-high.onnx", sizeMb: 110, note: "German, best quality" },
7209
+ ];
7210
+ const CONVERSATION_VOICE_SWITCH_TIMEOUT_MS = 10 * 60 * 1000; // downloads can take a while
7211
+
7212
+ function conversationVoiceConfigPath() {
7213
+ const override = String(process.env.PI_VOICE_CONFIG_PATH || "").trim();
7214
+ return override || path.join(homedir(), ".pi", "agent", "voice.json");
7215
+ }
7216
+
7217
+ function conversationVoiceDirs() {
7218
+ const data = String(process.env.XDG_DATA_HOME || "").trim() || path.join(homedir(), ".local", "share");
7219
+ return [path.join(data, "piper"), path.join(data, "piper", "voices"), path.join(data, "piper-voices")];
7220
+ }
7221
+
7222
+ async function conversationVoicesData() {
7223
+ let ttsProvider = null;
7224
+ let currentModelPath = null;
7225
+ try {
7226
+ const config = JSON.parse(await readFile(conversationVoiceConfigPath(), "utf8"));
7227
+ ttsProvider = config?.native?.tts?.provider ?? null;
7228
+ if (ttsProvider === "piper") currentModelPath = config?.native?.tts?.modelPath ?? null;
7229
+ } catch {
7230
+ // no voice.json yet — the dropdown still lists the catalog
7231
+ }
7232
+
7233
+ const onDisk = new Map();
7234
+ for (const dir of conversationVoiceDirs()) {
7235
+ let entries = [];
7236
+ try {
7237
+ entries = await readdir(dir);
7238
+ } catch {
7239
+ continue;
7240
+ }
7241
+ for (const entry of entries) {
7242
+ if (!entry.endsWith(".onnx") || onDisk.has(entry)) continue;
7243
+ const modelPath = path.join(dir, entry);
7244
+ const sidecar = await stat(`${modelPath}.json`).catch(() => null);
7245
+ if (!sidecar) continue;
7246
+ const size = await stat(modelPath).catch(() => null);
7247
+ onDisk.set(entry, { path: modelPath, sizeMb: size ? Math.round(size.size / (1024 * 1024)) : 0 });
7248
+ }
7249
+ }
7250
+
7251
+ const voices = CONVERSATION_VOICE_CATALOG.map((entry) => ({
7252
+ id: entry.id,
7253
+ sizeMb: entry.sizeMb,
7254
+ note: entry.note,
7255
+ downloaded: onDisk.has(entry.file),
7256
+ current: Boolean(currentModelPath && currentModelPath.endsWith(`/${entry.file}`)),
7257
+ }));
7258
+ for (const [file, info] of onDisk) {
7259
+ if (CONVERSATION_VOICE_CATALOG.some((entry) => entry.file === file)) continue;
7260
+ voices.push({
7261
+ id: file.replace(/\.onnx$/, ""),
7262
+ sizeMb: info.sizeMb,
7263
+ note: "found on disk",
7264
+ downloaded: true,
7265
+ current: info.path === currentModelPath,
7266
+ });
7267
+ }
7268
+ return { voices, current: voices.find((voice) => voice.current)?.id ?? null, ttsProvider };
7269
+ }
7270
+
7271
+ async function setConversationVoice(tab, body = {}) {
7272
+ const voiceId = String(body.voice || "").trim();
7273
+ if (!/^[\w.-]{1,120}$/.test(voiceId)) throw makeHttpError(400, "voice must be a Piper voice id (letters, digits, dot, dash, underscore)");
7274
+ const feature = await naturalConversationFeatureData(tab);
7275
+ if (!feature.available) throw makeHttpError(404, feature.unavailableReason);
7276
+ const commandName = feature.commands.find((name) => name === "talk") || feature.commands[0] || "talk";
7277
+ const response = await tab.rpc.send({ type: "prompt", message: `/${commandName} voice ${voiceId}` }, CONVERSATION_VOICE_SWITCH_TIMEOUT_MS);
7278
+ if (response.success === false) throw makeHttpError(400, response.error || `Failed to switch the conversation voice to ${voiceId}`);
7279
+ return { ...(await conversationVoicesData()), response };
7280
+ }
7281
+
7282
+ 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"]);
7283
+ const VOICE_STT_PROVIDER_IDS = ["local", "groq", "openai", "cloudflare"];
7284
+ const VOICE_TTS_PROVIDER_IDS = ["local", "openai"];
7285
+
7286
+ function safeVoiceEndpointLabel(value) {
7287
+ try {
7288
+ const parsed = new URL(String(value || ""));
7289
+ return `${parsed.origin}${parsed.pathname}`;
7290
+ } catch {
7291
+ return "";
7292
+ }
7293
+ }
7294
+
7295
+ function naturalConversationVoiceProviderStatus(kind = "stt") {
7296
+ const stt = kind === "stt";
7297
+ const localUrl = stt ? process.env.PI_VOICE_STT_URL : process.env.PI_VOICE_TTS_URL;
7298
+ const providers = [
7299
+ {
7300
+ id: "local",
7301
+ label: stt ? "Local STT endpoint" : "Local TTS endpoint",
7302
+ configured: !!localUrl,
7303
+ env: stt ? ["PI_VOICE_STT_URL"] : ["PI_VOICE_TTS_URL"],
7304
+ endpoint: safeVoiceEndpointLabel(localUrl),
7305
+ },
7306
+ ];
7307
+ if (stt) {
7308
+ providers.push(
7309
+ { 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" },
7310
+ { 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" },
7311
+ { 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 },
7312
+ );
7313
+ } else {
7314
+ 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" });
7315
+ }
7316
+ return {
7317
+ kind: stt ? "stt" : "tts",
7318
+ providers,
7319
+ defaultProvider: (stt ? process.env.PI_VOICE_STT_PROVIDER : process.env.PI_VOICE_TTS_PROVIDER) || (localUrl ? "local" : ""),
7320
+ 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.",
7321
+ };
7322
+ }
7323
+
7324
+ function naturalConversationUnavailableResponse(tab, kind = "stt") {
7325
+ return {
7326
+ featureId: NATURAL_CONVERSATION_FEATURE_ID,
7327
+ available: false,
7328
+ mode: naturalConversationModeSnapshot(tab, { available: false, enabled: false, uiState: "off" }),
7329
+ voice: naturalConversationVoiceProviderStatus(kind),
7330
+ 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.",
7331
+ };
7332
+ }
7333
+
7334
+ function naturalConversationProviderNotConfiguredError(kind, provider, message) {
7335
+ const error = makeHttpError(provider ? 424 : 501, message);
7336
+ error.voice = naturalConversationVoiceProviderStatus(kind);
7337
+ return error;
7338
+ }
7339
+
7340
+ function requestedNaturalConversationVoiceProvider(kind, body = {}) {
7341
+ const requested = String(body.provider || body.providerId || (kind === "stt" ? process.env.PI_VOICE_STT_PROVIDER : process.env.PI_VOICE_TTS_PROVIDER) || "").trim().toLowerCase();
7342
+ const ids = kind === "stt" ? VOICE_STT_PROVIDER_IDS : VOICE_TTS_PROVIDER_IDS;
7343
+ const localUrl = kind === "stt" ? process.env.PI_VOICE_STT_URL : process.env.PI_VOICE_TTS_URL;
7344
+ const provider = requested || (localUrl ? "local" : "");
7345
+ 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"}.`);
7346
+ if (!ids.includes(provider)) throw makeHttpError(400, `Unsupported Natural Conversation ${kind.toUpperCase()} provider: ${provider}`);
7347
+ 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"}.`);
7348
+ 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.");
7349
+ 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.`);
7350
+ 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.");
7351
+ return provider;
7352
+ }
7353
+
7354
+ function requireRemoteMicConsentForStt(req, body = {}) {
7355
+ if (isLocalRequest(req)) return;
7356
+ if (body.remoteMicStreamingConsentAccepted === true || body.remoteMicStreamingConsent === true) return;
7357
+ 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.");
7358
+ }
7359
+
7360
+ function decodeVoiceAudioBody(body = {}) {
7361
+ const value = String(body.audioBase64 || body.audio || body.data || "").trim();
7362
+ if (!value) throw makeHttpError(400, "audioBase64 is required for Natural Conversation STT fallback uploads");
7363
+ const dataUrlMatch = value.match(/^data:([^;,]+);base64,(.*)$/s);
7364
+ const mimeType = String(body.mimeType || body.contentType || dataUrlMatch?.[1] || "audio/webm").toLowerCase();
7365
+ if (!VOICE_AUDIO_MIME_TYPES.has(mimeType)) throw makeHttpError(415, `Unsupported voice audio mime type: ${mimeType}`);
7366
+ const base64 = (dataUrlMatch?.[2] || value).replace(/\s+/g, "");
7367
+ if (!/^[A-Za-z0-9+/]*={0,2}$/.test(base64)) throw makeHttpError(400, "audioBase64 must be valid base64");
7368
+ const buffer = Buffer.from(base64, "base64");
7369
+ if (!buffer.length) throw makeHttpError(400, "audioBase64 decoded to an empty audio payload");
7370
+ if (buffer.length > VOICE_AUDIO_BODY_LIMIT_BYTES) throw makeHttpError(413, `Audio payload is too large (limit ${formatBytes(VOICE_AUDIO_BODY_LIMIT_BYTES)})`);
7371
+ return {
7372
+ buffer,
7373
+ mimeType,
7374
+ fileName: String(body.fileName || `speech.${mimeType.includes("wav") ? "wav" : mimeType.includes("mpeg") || mimeType.includes("mp3") ? "mp3" : "webm"}`).replace(/[\\/\0]/g, "_").slice(0, 120),
7375
+ };
7376
+ }
7377
+
7378
+ function voiceFetchError(provider, response, text) {
7379
+ const status = response.status >= 500 ? 502 : 400;
7380
+ return makeHttpError(status, `${provider} voice provider returned HTTP ${response.status}: ${truncateStatusText(text || response.statusText || "request failed", 500)}`);
7381
+ }
7382
+
7383
+ async function fetchVoiceProvider(provider, url, init = {}) {
7384
+ const response = await fetch(url, { ...init, signal: AbortSignal.timeout(VOICE_PROVIDER_TIMEOUT_MS) });
7385
+ if (!response.ok) throw voiceFetchError(provider, response, await response.text().catch(() => ""));
7386
+ return response;
7387
+ }
7388
+
7389
+ function voiceFormData(audio, body = {}, model = "") {
7390
+ const form = new FormData();
7391
+ form.set("file", new Blob([audio.buffer], { type: audio.mimeType }), audio.fileName);
7392
+ if (model) form.set("model", model);
7393
+ if (body.language) form.set("language", String(body.language));
7394
+ if (body.prompt) form.set("prompt", String(body.prompt));
7395
+ return form;
7396
+ }
7397
+
7398
+ function transcriptTextFromProviderJson(json) {
7399
+ return String(json?.text || json?.transcript || json?.data?.text || json?.result?.text || "").trim();
7400
+ }
7401
+
7402
+ async function parseTranscriptResponse(provider, response) {
7403
+ const contentType = String(response.headers.get("content-type") || "").toLowerCase();
7404
+ if (contentType.includes("application/json")) {
7405
+ const json = await response.json();
7406
+ const text = transcriptTextFromProviderJson(json);
7407
+ if (!text) throw makeHttpError(502, `${provider} STT provider returned JSON without a transcript text field`);
7408
+ return text;
7409
+ }
7410
+ const text = (await response.text()).trim();
7411
+ if (!text) throw makeHttpError(502, `${provider} STT provider returned an empty transcript`);
7412
+ return text;
7413
+ }
7414
+
7415
+ async function transcribeWithLocalProvider(audio, body = {}) {
7416
+ const form = voiceFormData(audio, body, body.model ? String(body.model) : "");
7417
+ const response = await fetchVoiceProvider("local STT", process.env.PI_VOICE_STT_URL, { method: "POST", body: form });
7418
+ return parseTranscriptResponse("local", response);
7419
+ }
7420
+
7421
+ async function transcribeWithOpenAiCompatibleProvider(provider, audio, body = {}) {
7422
+ const openai = provider === "openai";
7423
+ const url = openai ? "https://api.openai.com/v1/audio/transcriptions" : "https://api.groq.com/openai/v1/audio/transcriptions";
7424
+ 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"));
7425
+ const form = voiceFormData(audio, body, model);
7426
+ form.set("response_format", "json");
7427
+ const response = await fetchVoiceProvider(`${provider} STT`, url, {
7428
+ method: "POST",
7429
+ headers: { authorization: `Bearer ${openai ? process.env.OPENAI_API_KEY : process.env.GROQ_API_KEY}` },
7430
+ body: form,
7431
+ });
7432
+ return parseTranscriptResponse(provider, response);
7433
+ }
7434
+
7435
+ async function handleNaturalConversationSttTranscribe(req, tab, body = {}) {
7436
+ requireRemoteMicConsentForStt(req, body);
7437
+ const provider = requestedNaturalConversationVoiceProvider("stt", body);
7438
+ const audio = decodeVoiceAudioBody(body);
7439
+ const text = provider === "local" ? await transcribeWithLocalProvider(audio, body) : await transcribeWithOpenAiCompatibleProvider(provider, audio, body);
7440
+ return { provider, text, mimeType: audio.mimeType, byteLength: audio.buffer.length, mode: naturalConversationModeSnapshot(tab) };
7441
+ }
7442
+
7443
+ function normalizeVoiceText(body = {}) {
7444
+ const text = String(body.text || body.input || "").trim();
7445
+ if (!text) throw makeHttpError(400, "text is required for Natural Conversation TTS fallback synthesis");
7446
+ if (text.length > VOICE_TTS_TEXT_MAX_CHARS) throw makeHttpError(413, `TTS text is too long (limit ${VOICE_TTS_TEXT_MAX_CHARS} characters)`);
7447
+ return text;
7448
+ }
7449
+
7450
+ async function audioPayloadFromResponse(provider, response, preferredFormat = "") {
7451
+ const contentType = String(response.headers.get("content-type") || "").toLowerCase();
7452
+ if (contentType.includes("application/json")) {
7453
+ const json = await response.json();
7454
+ const audioBase64 = String(json.audioBase64 || json.audio || json.data?.audioBase64 || "").trim();
7455
+ if (!audioBase64) throw makeHttpError(502, `${provider} TTS provider returned JSON without audioBase64`);
7456
+ const buffer = Buffer.from(audioBase64, "base64");
7457
+ return { audioBase64, contentType: json.mimeType || json.contentType || "audio/mpeg", format: json.format || preferredFormat || "mp3", byteLength: buffer.length };
7458
+ }
7459
+ const buffer = Buffer.from(await response.arrayBuffer());
7460
+ if (!buffer.length) throw makeHttpError(502, `${provider} TTS provider returned empty audio`);
7461
+ return { audioBase64: buffer.toString("base64"), contentType: contentType || "audio/mpeg", format: preferredFormat || (contentType.includes("wav") ? "wav" : "mp3"), byteLength: buffer.length };
7462
+ }
7463
+
7464
+ async function synthesizeWithLocalProvider(text, body = {}) {
7465
+ const response = await fetchVoiceProvider("local TTS", process.env.PI_VOICE_TTS_URL, {
7466
+ method: "POST",
7467
+ headers: { "content-type": "application/json" },
7468
+ body: JSON.stringify({ text, voice: body.voice || process.env.PI_VOICE_TTS_VOICE || undefined, format: body.format || process.env.PI_VOICE_TTS_FORMAT || "mp3" }),
7469
+ });
7470
+ return audioPayloadFromResponse("local", response, body.format || process.env.PI_VOICE_TTS_FORMAT || "mp3");
7471
+ }
7472
+
7473
+ async function synthesizeWithOpenAiProvider(text, body = {}) {
7474
+ const format = String(body.format || process.env.PI_VOICE_OPENAI_TTS_FORMAT || "mp3");
7475
+ const response = await fetchVoiceProvider("openai TTS", "https://api.openai.com/v1/audio/speech", {
7476
+ method: "POST",
7477
+ headers: { authorization: `Bearer ${process.env.OPENAI_API_KEY}`, "content-type": "application/json" },
7478
+ 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 }),
7479
+ });
7480
+ return audioPayloadFromResponse("openai", response, format);
7481
+ }
7482
+
7483
+ async function handleNaturalConversationTtsSpeech(_req, tab, body = {}) {
7484
+ const provider = requestedNaturalConversationVoiceProvider("tts", body);
7485
+ const text = normalizeVoiceText(body);
7486
+ const audio = provider === "local" ? await synthesizeWithLocalProvider(text, body) : await synthesizeWithOpenAiProvider(text, body);
7487
+ return { provider, ...audio, mode: naturalConversationModeSnapshot(tab) };
7488
+ }
7489
+
6770
7490
  function resolveCliPath(value) {
6771
7491
  const text = String(value || "").trim();
6772
7492
  if (!text) return "";
@@ -7215,6 +7935,12 @@ async function handleNativeSlashCommand(tab, body, req) {
7215
7935
  const response = await tab.rpc.send({ type: "new_session" });
7216
7936
  if (response.success === false) return response;
7217
7937
  tab.conversationStarted = false;
7938
+ forgetTabState(tab);
7939
+ rememberTabState(tab, response.data);
7940
+ clearPendingExtensionUiRequests(tab);
7941
+ clearExtensionStatuses(tab);
7942
+ clearExtensionWidgets(tab);
7943
+ resetNaturalConversationMode(tab);
7218
7944
  return respondNative("new", {
7219
7945
  status: "succeeded",
7220
7946
  message: "Started a new session.",
@@ -7678,6 +8404,10 @@ async function setThinkingLevelForTab(tab, level, { allowPending = true } = {})
7678
8404
  }
7679
8405
 
7680
8406
  async function applyPendingThinkingBeforePrompt(tab) {
8407
+ if (isNaturalConversationActive(tab)) {
8408
+ tab.pendingThinkingLevel = undefined;
8409
+ return null;
8410
+ }
7681
8411
  const level = tab?.pendingThinkingLevel;
7682
8412
  if (!level) return null;
7683
8413
  const stateResult = await safeRpcData(tab, { type: "get_state" }, STATUS_RPC_TIMEOUT_MS);
@@ -8006,6 +8736,7 @@ const server = createServer(async (req, res) => {
8006
8736
  if (url.pathname === "/api/app-runner" && req.method === "POST") {
8007
8737
  const body = await readJsonBody(req);
8008
8738
  const tab = getRequestedTab(req, url, body);
8739
+ ensureNaturalConversationRouteAllowed(tab, "app runner actions are blocked");
8009
8740
  sendJson(res, 200, { ok: true, data: await startAppRunner(tab, String(body.runnerId || body.id || "")) });
8010
8741
  return;
8011
8742
  }
@@ -8013,6 +8744,7 @@ const server = createServer(async (req, res) => {
8013
8744
  if (url.pathname === "/api/app-runner/input" && req.method === "POST") {
8014
8745
  const body = await readJsonBody(req);
8015
8746
  const tab = getRequestedTab(req, url, body);
8747
+ ensureNaturalConversationRouteAllowed(tab, "app runner actions are blocked");
8016
8748
  const text = Object.prototype.hasOwnProperty.call(body, "text") ? body.text : body.input;
8017
8749
  sendJson(res, 200, { ok: true, data: sendAppRunnerInput(tab, text, { appendNewline: body.newline !== false, closeStdin: body.closeStdin === true || body.close === true }) });
8018
8750
  return;
@@ -8021,6 +8753,7 @@ const server = createServer(async (req, res) => {
8021
8753
  if (url.pathname === "/api/app-runner/context" && req.method === "POST") {
8022
8754
  const body = await readJsonBody(req);
8023
8755
  const tab = getRequestedTab(req, url, body);
8756
+ ensureNaturalConversationRouteAllowed(tab, "app runner actions are blocked");
8024
8757
  sendJson(res, 200, { ok: true, data: await transferAppRunnerContext(tab, body) });
8025
8758
  return;
8026
8759
  }
@@ -8028,6 +8761,7 @@ const server = createServer(async (req, res) => {
8028
8761
  if (url.pathname === "/api/app-runner/stop" && req.method === "POST") {
8029
8762
  const body = await readJsonBody(req);
8030
8763
  const tab = getRequestedTab(req, url, body);
8764
+ ensureNaturalConversationRouteAllowed(tab, "app runner actions are blocked");
8031
8765
  stopAppRunnerForTab(tab, "stop requested from Web UI");
8032
8766
  sendJson(res, 200, { ok: true, data: await getAppRunnerData(tab) });
8033
8767
  return;
@@ -8036,6 +8770,7 @@ const server = createServer(async (req, res) => {
8036
8770
  if (url.pathname === "/api/app-runner/clear" && req.method === "POST") {
8037
8771
  const body = await readJsonBody(req);
8038
8772
  const tab = getRequestedTab(req, url, body);
8773
+ ensureNaturalConversationRouteAllowed(tab, "app runner actions are blocked");
8039
8774
  clearAppRunnerForTab(tab);
8040
8775
  sendJson(res, 200, { ok: true, data: await getAppRunnerData(tab) });
8041
8776
  return;
@@ -8050,6 +8785,7 @@ const server = createServer(async (req, res) => {
8050
8785
  if (url.pathname === "/api/app-runner-config" && req.method === "POST") {
8051
8786
  const body = await readJsonBody(req);
8052
8787
  const tab = getRequestedTab(req, url, body);
8788
+ ensureNaturalConversationRouteAllowed(tab, "app runner configuration changes are blocked");
8053
8789
  sendJson(res, 200, { ok: true, data: await saveCustomAppRunner(tab, body.runner || body) });
8054
8790
  return;
8055
8791
  }
@@ -8057,6 +8793,7 @@ const server = createServer(async (req, res) => {
8057
8793
  if (url.pathname === "/api/app-runner-config" && req.method === "DELETE") {
8058
8794
  const body = await readJsonBody(req);
8059
8795
  const tab = getRequestedTab(req, url, body);
8796
+ ensureNaturalConversationRouteAllowed(tab, "app runner configuration changes are blocked");
8060
8797
  sendJson(res, 200, { ok: true, data: await deleteCustomAppRunner(tab, body.id || body.runnerId) });
8061
8798
  return;
8062
8799
  }
@@ -8078,6 +8815,8 @@ const server = createServer(async (req, res) => {
8078
8815
 
8079
8816
  if (url.pathname === "/api/directories" && req.method === "POST") {
8080
8817
  const body = await readJsonBody(req);
8818
+ const tab = getRequestedTab(req, url, body);
8819
+ ensureNaturalConversationRouteAllowed(tab, "directory creation is blocked");
8081
8820
  const activeCwd = directoryPickerActiveCwd(req, url, body);
8082
8821
  sendJson(res, 201, {
8083
8822
  ok: true,
@@ -8099,6 +8838,8 @@ const server = createServer(async (req, res) => {
8099
8838
 
8100
8839
  if (url.pathname === "/api/path-fast-picks" && req.method === "POST") {
8101
8840
  const body = await readJsonBody(req);
8841
+ const tab = getRequestedTab(req, url, body);
8842
+ ensureNaturalConversationRouteAllowed(tab, "path fast-pick changes are blocked");
8102
8843
  const picks = await writePathFastPicks(body.picks ?? body);
8103
8844
  sendJson(res, 200, { ok: true, data: { picks } });
8104
8845
  return;
@@ -8106,6 +8847,8 @@ const server = createServer(async (req, res) => {
8106
8847
 
8107
8848
  if (url.pathname === "/api/attachments" && req.method === "POST") {
8108
8849
  const body = await readJsonBody(req, { limitBytes: requestBodyLimitForPath(url.pathname) });
8850
+ const tab = getRequestedTab(req, url, body);
8851
+ ensureNaturalConversationRouteAllowed(tab, "attachment uploads are blocked");
8109
8852
  sendJson(res, 201, { ok: true, data: await saveUploadedAttachments(body) });
8110
8853
  return;
8111
8854
  }
@@ -8119,6 +8862,7 @@ const server = createServer(async (req, res) => {
8119
8862
  if (url.pathname === "/api/model-cycle" && req.method === "POST") {
8120
8863
  const body = await readJsonBody(req);
8121
8864
  const tab = getRequestedTab(req, url, body);
8865
+ ensureNaturalConversationRouteAllowed(tab, "model changes are blocked");
8122
8866
  const response = await cycleTabModel(tab, body.direction || body.mode);
8123
8867
  sendJson(res, response.success === false ? 400 : 200, responseWithTab(response, tab));
8124
8868
  return;
@@ -8145,6 +8889,7 @@ const server = createServer(async (req, res) => {
8145
8889
  if (url.pathname === "/api/fork" && req.method === "POST") {
8146
8890
  const body = await readJsonBody(req);
8147
8891
  const tab = getRequestedTab(req, url, body);
8892
+ ensureNaturalConversationRouteAllowed(tab, "session fork actions are blocked");
8148
8893
  const response = await runForkCommand(tab, body.entryId);
8149
8894
  sendJson(res, response.success === false ? 400 : 200, responseWithTab(response, tab));
8150
8895
  return;
@@ -8153,6 +8898,7 @@ const server = createServer(async (req, res) => {
8153
8898
  if (url.pathname === "/api/clone" && req.method === "POST") {
8154
8899
  const body = await readJsonBody(req);
8155
8900
  const tab = getRequestedTab(req, url, body);
8901
+ ensureNaturalConversationRouteAllowed(tab, "session clone actions are blocked");
8156
8902
  const response = await runCloneCommand(tab);
8157
8903
  sendJson(res, response.success === false ? 400 : 200, responseWithTab(response, tab));
8158
8904
  return;
@@ -8161,6 +8907,7 @@ const server = createServer(async (req, res) => {
8161
8907
  if (url.pathname === "/api/switch-session" && req.method === "POST") {
8162
8908
  const body = await readJsonBody(req);
8163
8909
  const tab = getRequestedTab(req, url, body);
8910
+ ensureNaturalConversationRouteAllowed(tab, "session switching is blocked");
8164
8911
  const response = await switchTabSession(tab, body.sessionPath || body.path);
8165
8912
  sendJson(res, response.success === false ? 400 : 200, responseWithTab(response, tab));
8166
8913
  return;
@@ -8169,6 +8916,7 @@ const server = createServer(async (req, res) => {
8169
8916
  if (url.pathname === "/api/session-rename" && req.method === "POST") {
8170
8917
  const body = await readJsonBody(req);
8171
8918
  const tab = getRequestedTab(req, url, body);
8919
+ ensureNaturalConversationRouteAllowed(tab, "session renaming is blocked");
8172
8920
  sendJson(res, 200, { ok: true, data: await renameSessionData(tab, body), tab: tabMeta(tab) });
8173
8921
  return;
8174
8922
  }
@@ -8177,6 +8925,7 @@ const server = createServer(async (req, res) => {
8177
8925
  requireLocalhostRoute(req, url.pathname);
8178
8926
  const body = await readJsonBody(req);
8179
8927
  const tab = getRequestedTab(req, url, body);
8928
+ ensureNaturalConversationRouteAllowed(tab, "session deletion is blocked");
8180
8929
  sendJson(res, 200, { ok: true, data: await deleteSessionData(tab, body), tab: tabMeta(tab) });
8181
8930
  return;
8182
8931
  }
@@ -8196,11 +8945,68 @@ const server = createServer(async (req, res) => {
8196
8945
  if (url.pathname === "/api/tree-navigate" && req.method === "POST") {
8197
8946
  const body = await readJsonBody(req);
8198
8947
  const tab = getRequestedTab(req, url, body);
8948
+ ensureNaturalConversationRouteAllowed(tab, "session tree navigation is blocked");
8199
8949
  const response = await navigateSessionTree(tab, body);
8200
8950
  sendJson(res, response.success === false ? 400 : 200, responseWithTab(response, tab));
8201
8951
  return;
8202
8952
  }
8203
8953
 
8954
+ if (url.pathname === "/api/features/natural-conversation" && req.method === "GET") {
8955
+ const tab = getRequestedTab(req, url);
8956
+ sendJson(res, 200, { ok: true, data: await naturalConversationFeatureData(tab) });
8957
+ return;
8958
+ }
8959
+
8960
+ if (url.pathname === "/api/conversation-mode" && req.method === "GET") {
8961
+ const tab = getRequestedTab(req, url);
8962
+ sendJson(res, 200, { ok: true, data: await naturalConversationFeatureData(tab) });
8963
+ return;
8964
+ }
8965
+
8966
+ if (url.pathname === "/api/conversation-mode" && req.method === "POST") {
8967
+ const body = await readJsonBody(req);
8968
+ const tab = getRequestedTab(req, url, body);
8969
+ sendJson(res, 200, { ok: true, data: await setNaturalConversationMode(tab, body), tab: tabMeta(tab) });
8970
+ return;
8971
+ }
8972
+
8973
+ if (url.pathname === "/api/conversation-voices" && req.method === "GET") {
8974
+ const tab = getRequestedTab(req, url);
8975
+ sendJson(res, 200, { ok: true, data: await conversationVoicesData(), tab: tabMeta(tab) });
8976
+ return;
8977
+ }
8978
+
8979
+ if (url.pathname === "/api/conversation-voice" && req.method === "POST") {
8980
+ const body = await readJsonBody(req);
8981
+ const tab = getRequestedTab(req, url, body);
8982
+ sendJson(res, 200, { ok: true, data: await setConversationVoice(tab, body), tab: tabMeta(tab) });
8983
+ return;
8984
+ }
8985
+
8986
+ if (url.pathname === "/api/stt/transcribe" && req.method === "POST") {
8987
+ const body = await readJsonBody(req, { limitBytes: VOICE_AUDIO_JSON_BODY_LIMIT_BYTES });
8988
+ const tab = getRequestedTab(req, url, body);
8989
+ try {
8990
+ sendJson(res, 200, { ok: true, data: await handleNaturalConversationSttTranscribe(req, tab, body) });
8991
+ } catch (error) {
8992
+ const statusCode = error?.statusCode || 500;
8993
+ 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) });
8994
+ }
8995
+ return;
8996
+ }
8997
+
8998
+ if (url.pathname === "/api/tts/speech" && req.method === "POST") {
8999
+ const body = await readJsonBody(req);
9000
+ const tab = getRequestedTab(req, url, body);
9001
+ try {
9002
+ sendJson(res, 200, { ok: true, data: await handleNaturalConversationTtsSpeech(req, tab, body) });
9003
+ } catch (error) {
9004
+ const statusCode = error?.statusCode || 500;
9005
+ 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) });
9006
+ }
9007
+ return;
9008
+ }
9009
+
8204
9010
  if (url.pathname === "/api/optional-features" && req.method === "GET") {
8205
9011
  sendJson(res, 200, { ok: true, data: await optionalFeaturePackageStatuses() });
8206
9012
  return;
@@ -8209,6 +9015,8 @@ const server = createServer(async (req, res) => {
8209
9015
  if (url.pathname === "/api/optional-feature-install" && req.method === "POST") {
8210
9016
  requireLocalhostRoute(req, url.pathname);
8211
9017
  const body = await readJsonBody(req);
9018
+ const tab = getRequestedTab(req, url, body);
9019
+ ensureNaturalConversationRouteAllowed(tab, "optional feature installs are blocked");
8212
9020
  const data = await installOptionalFeaturePackage(String(body.featureId || ""));
8213
9021
  sendJson(res, 200, { ok: true, data });
8214
9022
  return;
@@ -8223,6 +9031,7 @@ const server = createServer(async (req, res) => {
8223
9031
  if (url.pathname === "/api/tools" && req.method === "POST") {
8224
9032
  const body = await readJsonBody(req);
8225
9033
  const tab = getRequestedTab(req, url, body);
9034
+ if (isNaturalConversationActive(tab)) blockNaturalConversationAction("tool configuration changes are blocked");
8226
9035
  sendJson(res, 200, { ok: true, data: await setToolConfigData(tab, body) });
8227
9036
  return;
8228
9037
  }
@@ -8236,6 +9045,7 @@ const server = createServer(async (req, res) => {
8236
9045
  if (url.pathname === "/api/skills" && req.method === "POST") {
8237
9046
  const body = await readJsonBody(req);
8238
9047
  const tab = getRequestedTab(req, url, body);
9048
+ ensureNaturalConversationRouteAllowed(tab, "skill configuration changes are blocked");
8239
9049
  sendJson(res, 200, { ok: true, data: await setSkillConfigData(tab, body) });
8240
9050
  return;
8241
9051
  }
@@ -8250,6 +9060,7 @@ const server = createServer(async (req, res) => {
8250
9060
  requireLocalhostRoute(req, url.pathname);
8251
9061
  const body = await readJsonBody(req, { limitBytes: SKILL_FILE_BODY_LIMIT_BYTES });
8252
9062
  const tab = getRequestedTab(req, url, body);
9063
+ ensureNaturalConversationRouteAllowed(tab, "skill file edits are blocked");
8253
9064
  sendJson(res, 200, { ok: true, data: await saveSkillFileData(tab, body) });
8254
9065
  return;
8255
9066
  }
@@ -8263,6 +9074,7 @@ const server = createServer(async (req, res) => {
8263
9074
  if (url.pathname === "/api/settings" && req.method === "POST") {
8264
9075
  const body = await readJsonBody(req);
8265
9076
  const tab = getRequestedTab(req, url, body);
9077
+ ensureNaturalConversationRouteAllowed(tab, "settings changes are blocked");
8266
9078
  sendJson(res, 200, { ok: true, data: await setNativeSettingsData(tab, body) });
8267
9079
  return;
8268
9080
  }
@@ -8276,6 +9088,7 @@ const server = createServer(async (req, res) => {
8276
9088
  if (url.pathname === "/api/action-feedback" && req.method === "POST") {
8277
9089
  const body = await readJsonBody(req);
8278
9090
  const tab = getRequestedTab(req, url, body);
9091
+ ensureNaturalConversationRouteAllowed(tab, "feedback-learning prompts are blocked");
8279
9092
  const response = await handleActionFeedback(tab, body);
8280
9093
  sendJson(res, response.success === false ? 400 : 200, response);
8281
9094
  return;
@@ -8296,12 +9109,26 @@ const server = createServer(async (req, res) => {
8296
9109
  if (url.pathname === "/api/prompt" && req.method === "POST") {
8297
9110
  const body = await readJsonBody(req, { limitBytes: requestBodyLimitForPath(url.pathname) });
8298
9111
  const tab = getRequestedTab(req, url, body);
9112
+ if (isNaturalConversationActive(tab) && naturalConversationSlashCommandName(body.message) && !isNaturalConversationSlashCommand(body.message)) {
9113
+ blockNaturalConversationAction("slash commands are blocked from the Web UI shell");
9114
+ }
8299
9115
  const nativeResponse = await handleNativeSlashCommand(tab, body, req);
8300
9116
  if (nativeResponse) {
8301
9117
  sendJson(res, nativeResponse.success === false ? 400 : 200, responseWithTab(nativeResponse, tab));
8302
9118
  return;
8303
9119
  }
8304
9120
  const command = commandFromPost(url.pathname, body);
9121
+ enforceNaturalConversationCommandAllowed(tab, command);
9122
+ const queuedForCompaction = maybeQueueCommandDuringCompaction(tab, command);
9123
+ if (queuedForCompaction) {
9124
+ sendJson(res, 202, responseWithTab(queuedForCompaction, tab));
9125
+ return;
9126
+ }
9127
+ const naturalConversationSafetyResponse = await ensureNaturalConversationPromptSafety(tab, command);
9128
+ if (naturalConversationSafetyResponse?.success === false) {
9129
+ sendJson(res, 400, responseWithTab(naturalConversationSafetyResponse, tab));
9130
+ return;
9131
+ }
8305
9132
  const pendingThinkingResponse = await applyPendingThinkingBeforePrompt(tab);
8306
9133
  if (pendingThinkingResponse?.success === false) {
8307
9134
  sendJson(res, 400, responseWithTab(pendingThinkingResponse, tab));
@@ -8341,6 +9168,7 @@ const server = createServer(async (req, res) => {
8341
9168
  if (url.pathname === "/api/git-changes/pull" && req.method === "POST") {
8342
9169
  const body = await readJsonBody(req);
8343
9170
  const tab = getRequestedTab(req, url, body);
9171
+ ensureNaturalConversationRouteAllowed(tab, "git pull is blocked");
8344
9172
  try {
8345
9173
  sendJson(res, 200, await pullGitChanges(tab.cwd));
8346
9174
  } catch (error) {
@@ -8362,6 +9190,7 @@ const server = createServer(async (req, res) => {
8362
9190
  if (url.pathname === "/api/git-worktrees" && req.method === "POST") {
8363
9191
  const body = await readJsonBody(req);
8364
9192
  const tab = getRequestedTab(req, url, body);
9193
+ ensureNaturalConversationRouteAllowed(tab, "git worktree changes are blocked");
8365
9194
  try {
8366
9195
  sendJson(res, 200, { ok: true, data: await createGitWorktreeTab(tab, body) });
8367
9196
  } catch (error) {
@@ -8373,6 +9202,7 @@ const server = createServer(async (req, res) => {
8373
9202
  if (url.pathname === "/api/git-worktrees/open" && req.method === "POST") {
8374
9203
  const body = await readJsonBody(req);
8375
9204
  const tab = getRequestedTab(req, url, body);
9205
+ ensureNaturalConversationRouteAllowed(tab, "git worktree changes are blocked");
8376
9206
  try {
8377
9207
  sendJson(res, 200, { ok: true, data: await openExistingGitWorktreeTab(tab, body) });
8378
9208
  } catch (error) {
@@ -8385,6 +9215,7 @@ const server = createServer(async (req, res) => {
8385
9215
  requireLocalhost(req, "Removing Git worktrees is only allowed from localhost");
8386
9216
  const body = await readJsonBody(req);
8387
9217
  const tab = getRequestedTab(req, url, body);
9218
+ ensureNaturalConversationRouteAllowed(tab, "git worktree removal is blocked");
8388
9219
  try {
8389
9220
  sendJson(res, 200, { ok: true, data: await removeGitWorktreeForTab(tab, body) });
8390
9221
  } catch (error) {
@@ -8406,6 +9237,7 @@ const server = createServer(async (req, res) => {
8406
9237
  if (url.pathname === "/api/git-branch" && req.method === "POST") {
8407
9238
  const body = await readJsonBody(req);
8408
9239
  const tab = getRequestedTab(req, url, body);
9240
+ ensureNaturalConversationRouteAllowed(tab, "git branch changes are blocked");
8409
9241
  try {
8410
9242
  sendJson(res, 200, await switchGitBranch(tab.cwd, body.branch, { create: body.create === true }));
8411
9243
  } catch (error) {
@@ -8417,6 +9249,7 @@ const server = createServer(async (req, res) => {
8417
9249
  if (url.pathname.startsWith("/api/git-workflow/")) {
8418
9250
  const body = req.method === "POST" ? await readJsonBody(req) : {};
8419
9251
  const tab = getRequestedTab(req, url, body);
9252
+ if (req.method !== "GET") ensureNaturalConversationRouteAllowed(tab, "git workflow actions are blocked");
8420
9253
  const response = await handleGitWorkflowRequest(url.pathname, body, tab);
8421
9254
  if (response) {
8422
9255
  sendJson(res, 200, response);
@@ -8460,7 +9293,18 @@ const server = createServer(async (req, res) => {
8460
9293
  const command = commandFromPost(url.pathname, body);
8461
9294
  if (command) {
8462
9295
  const tab = getRequestedTab(req, url, body);
9296
+ enforceNaturalConversationCommandAllowed(tab, command);
8463
9297
  if (command.type === "abort") await cancelPendingExtensionUiRequests(tab);
9298
+ const queuedForCompaction = maybeQueueCommandDuringCompaction(tab, command);
9299
+ if (queuedForCompaction) {
9300
+ sendJson(res, 202, responseWithTab(queuedForCompaction, tab));
9301
+ return;
9302
+ }
9303
+ const naturalConversationSafetyResponse = await ensureNaturalConversationPromptSafety(tab, command);
9304
+ if (naturalConversationSafetyResponse?.success === false) {
9305
+ sendJson(res, 400, responseWithTab(naturalConversationSafetyResponse, tab));
9306
+ return;
9307
+ }
8464
9308
  const startsVisibleWork = commandStartsVisibleWork(command);
8465
9309
  if (startsVisibleWork) {
8466
9310
  maybeNameTabForConversation(tab, command);
@@ -8483,6 +9327,7 @@ const server = createServer(async (req, res) => {
8483
9327
  clearPendingExtensionUiRequests(tab);
8484
9328
  clearExtensionStatuses(tab);
8485
9329
  clearExtensionWidgets(tab);
9330
+ resetNaturalConversationMode(tab);
8486
9331
  }
8487
9332
  sendJson(res, response.success === false ? 400 : 200, responseWithTab(response, tab));
8488
9333
  return;