@firstpick/pi-package-webui 0.5.8 → 0.5.9
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/README.md +2 -0
- package/bin/pi-webui.mjs +529 -7
- package/package.json +1 -1
- package/public/app.js +527 -45
- package/public/index.html +12 -4
- package/public/styles.css +211 -2
- package/tests/compaction-resume-harness.test.mjs +114 -0
- package/tests/compaction-static.test.mjs +25 -0
- package/tests/fixtures/compacting-pi.mjs +115 -0
- package/tests/fixtures/fake-pi.mjs +66 -1
- package/tests/http-endpoints-harness.test.mjs +121 -2
- package/tests/mobile-static.test.mjs +30 -5
package/bin/pi-webui.mjs
CHANGED
|
@@ -238,6 +238,7 @@ const unavailableNative = (command, details = {}) => nativeCommandUnavailable(co
|
|
|
238
238
|
function parseSlashCommand(message) {
|
|
239
239
|
return parseNativeSlashCommand(message, NATIVE_SLASH_COMMAND_NAMES);
|
|
240
240
|
}
|
|
241
|
+
const NATURAL_CONVERSATION_FEATURE_ID = "naturalConversation";
|
|
241
242
|
const OPTIONAL_FEATURE_PACKAGES = new Map([
|
|
242
243
|
["btwCommand", "@firstpick/pi-extension-btw"],
|
|
243
244
|
["gitWorkflow", "@firstpick/pi-prompts-git-pr"],
|
|
@@ -249,11 +250,19 @@ const OPTIONAL_FEATURE_PACKAGES = new Map([
|
|
|
249
250
|
["todoProgressWidget", "@firstpick/pi-extension-todo-progress"],
|
|
250
251
|
["tuiToolsCommand", "@firstpick/pi-extension-tools"],
|
|
251
252
|
["remoteWebui", "@firstpick/pi-package-remote-webui"],
|
|
253
|
+
["naturalConversation", "@firstpick/pi-package-natural-conversation"],
|
|
252
254
|
["gitFooterStatus", "@firstpick/pi-extension-git-footer-status"],
|
|
253
255
|
["statsCommand", "@firstpick/pi-extension-stats"],
|
|
254
256
|
["themeBundle", "@firstpick/pi-themes-bundle"],
|
|
255
257
|
]);
|
|
256
|
-
const WEBUI_CONTROLLED_PACKAGES = new Set([
|
|
258
|
+
const WEBUI_CONTROLLED_PACKAGES = new Set([
|
|
259
|
+
WEBUI_PACKAGE,
|
|
260
|
+
...[...OPTIONAL_FEATURE_PACKAGES.entries()]
|
|
261
|
+
.filter(([featureId]) => featureId !== NATURAL_CONVERSATION_FEATURE_ID)
|
|
262
|
+
.map(([, packageName]) => packageName),
|
|
263
|
+
]);
|
|
264
|
+
const NATURAL_CONVERSATION_STATUS_KEY = "natural-conversation";
|
|
265
|
+
const NATURAL_CONVERSATION_COMMAND_NAMES = ["talk", "voice", "conversation"];
|
|
257
266
|
const PACKAGE_NAME_CACHE = new Map();
|
|
258
267
|
|
|
259
268
|
function usage() {
|
|
@@ -1119,6 +1128,31 @@ function nodeModulesParentForPackageRoot(root = packageRoot) {
|
|
|
1119
1128
|
return root;
|
|
1120
1129
|
}
|
|
1121
1130
|
|
|
1131
|
+
function prependNodePathEntries(entries) {
|
|
1132
|
+
const existing = String(process.env.NODE_PATH || "").split(path.delimiter).filter(Boolean);
|
|
1133
|
+
const seen = new Set();
|
|
1134
|
+
const next = [];
|
|
1135
|
+
for (const entry of [...entries, ...existing]) {
|
|
1136
|
+
if (!entry) continue;
|
|
1137
|
+
const normalized = path.resolve(entry);
|
|
1138
|
+
const key = process.platform === "win32" ? normalized.toLowerCase() : normalized;
|
|
1139
|
+
if (seen.has(key)) continue;
|
|
1140
|
+
seen.add(key);
|
|
1141
|
+
next.push(normalized);
|
|
1142
|
+
}
|
|
1143
|
+
if (next.length) process.env.NODE_PATH = next.join(path.delimiter);
|
|
1144
|
+
}
|
|
1145
|
+
|
|
1146
|
+
async function configureDevDependencyResolution() {
|
|
1147
|
+
if (!webuiDevServer) return;
|
|
1148
|
+
const workspaceRoot = await devWorkspaceRoot();
|
|
1149
|
+
prependNodePathEntries([
|
|
1150
|
+
workspaceRoot ? path.join(workspaceRoot, "node_modules") : "",
|
|
1151
|
+
path.join(packageRoot, "node_modules"),
|
|
1152
|
+
path.join(configuredAgentNpmRoot(), "node_modules"),
|
|
1153
|
+
]);
|
|
1154
|
+
}
|
|
1155
|
+
|
|
1122
1156
|
function declaredDependencySpec(pkg, packageName) {
|
|
1123
1157
|
return firstDefined(
|
|
1124
1158
|
pkg?.dependencies?.[packageName],
|
|
@@ -2470,7 +2504,7 @@ function scheduleAppRunnerBroadcast(tab) {
|
|
|
2470
2504
|
}
|
|
2471
2505
|
|
|
2472
2506
|
function terminateAppRunnerChild(run, signal = "SIGTERM") {
|
|
2473
|
-
if (!run?.child || run.child.
|
|
2507
|
+
if (!run?.child || run.child.exitCode !== null || run.child.signalCode !== null) return false;
|
|
2474
2508
|
try {
|
|
2475
2509
|
if (process.platform !== "win32" && run.pid) process.kill(-run.pid, signal);
|
|
2476
2510
|
else run.child.kill(signal);
|
|
@@ -2485,6 +2519,24 @@ function terminateAppRunnerChild(run, signal = "SIGTERM") {
|
|
|
2485
2519
|
}
|
|
2486
2520
|
}
|
|
2487
2521
|
|
|
2522
|
+
function appRunnerStdinWritable(run) {
|
|
2523
|
+
const stdin = run?.child?.stdin;
|
|
2524
|
+
return !!stdin && !stdin.destroyed && !stdin.writableEnded && run.stdinClosed !== true;
|
|
2525
|
+
}
|
|
2526
|
+
|
|
2527
|
+
function interruptAppRunnerChild(run) {
|
|
2528
|
+
if (!run?.child || run.child.exitCode !== null || run.child.signalCode !== null) return false;
|
|
2529
|
+
if (appRunnerStdinWritable(run) && (run.executionMode === "pty" || process.platform === "win32")) {
|
|
2530
|
+
try {
|
|
2531
|
+
run.child.stdin.write("\x03", "utf8");
|
|
2532
|
+
return true;
|
|
2533
|
+
} catch {
|
|
2534
|
+
// Fall back to process signals below.
|
|
2535
|
+
}
|
|
2536
|
+
}
|
|
2537
|
+
return terminateAppRunnerChild(run, "SIGINT");
|
|
2538
|
+
}
|
|
2539
|
+
|
|
2488
2540
|
function finishAppRunner(tab, run, patch = {}) {
|
|
2489
2541
|
if (!run || run.settled) return;
|
|
2490
2542
|
run.settled = true;
|
|
@@ -2600,13 +2652,14 @@ function stopAppRunnerForTab(tab, reason = "stop requested", { force = false } =
|
|
|
2600
2652
|
const run = tab?.appRunner;
|
|
2601
2653
|
if (!run || run.status !== "running") return false;
|
|
2602
2654
|
run.stopping = true;
|
|
2603
|
-
appendAppRunnerLine(run, `# ${reason}; sending ${force ? "SIGKILL" : "
|
|
2604
|
-
terminateAppRunnerChild(run,
|
|
2655
|
+
appendAppRunnerLine(run, `# ${reason}; sending ${force ? "SIGKILL" : "Ctrl+C"}`);
|
|
2656
|
+
if (force) terminateAppRunnerChild(run, "SIGKILL");
|
|
2657
|
+
else interruptAppRunnerChild(run);
|
|
2605
2658
|
if (!force) {
|
|
2606
2659
|
clearTimeout(run.stopTimer);
|
|
2607
2660
|
run.stopTimer = setTimeout(() => {
|
|
2608
2661
|
if (run.status === "running") {
|
|
2609
|
-
appendAppRunnerLine(run, "# app runner did not stop; sending SIGKILL");
|
|
2662
|
+
appendAppRunnerLine(run, "# app runner did not stop after Ctrl+C; sending SIGKILL");
|
|
2610
2663
|
terminateAppRunnerChild(run, "SIGKILL");
|
|
2611
2664
|
scheduleAppRunnerBroadcast(tab);
|
|
2612
2665
|
}
|
|
@@ -4801,6 +4854,7 @@ try {
|
|
|
4801
4854
|
|
|
4802
4855
|
process.env.PI_WEBUI_HOST = options.host;
|
|
4803
4856
|
process.env.PI_WEBUI_PORT = String(options.port);
|
|
4857
|
+
await configureDevDependencyResolution();
|
|
4804
4858
|
|
|
4805
4859
|
const startupDelayMs = Number.parseInt(process.env.PI_WEBUI_START_DELAY_MS || "", 10);
|
|
4806
4860
|
delete process.env.PI_WEBUI_START_DELAY_MS;
|
|
@@ -5071,9 +5125,19 @@ function rememberTabState(tab, state) {
|
|
|
5071
5125
|
if (!options.noSession && Object.prototype.hasOwnProperty.call(state, "sessionFile")) tab.sessionFile = sessionFileFromState(state);
|
|
5072
5126
|
}
|
|
5073
5127
|
|
|
5128
|
+
function patchTabState(tab, patch) {
|
|
5129
|
+
if (!tab || !patch || typeof patch !== "object") return;
|
|
5130
|
+
tab.lastState = { ...(tab.lastState || {}), ...patch };
|
|
5131
|
+
}
|
|
5132
|
+
|
|
5074
5133
|
function stateWithPendingThinking(tab, state) {
|
|
5075
|
-
if (!state || typeof state !== "object"
|
|
5076
|
-
|
|
5134
|
+
if (!state || typeof state !== "object") return state;
|
|
5135
|
+
const queueLength = tab ? compactionQueueForTab(tab).length : 0;
|
|
5136
|
+
if (!tab?.pendingThinkingLevel && queueLength === 0) return state;
|
|
5137
|
+
const patch = {};
|
|
5138
|
+
if (tab?.pendingThinkingLevel) patch.pendingThinkingLevel = tab.pendingThinkingLevel;
|
|
5139
|
+
if (queueLength > 0) patch.pendingMessageCount = Number(state.pendingMessageCount || 0) + queueLength;
|
|
5140
|
+
return { ...state, ...patch };
|
|
5077
5141
|
}
|
|
5078
5142
|
|
|
5079
5143
|
function responseWithPendingThinking(tab, response) {
|
|
@@ -5167,6 +5231,96 @@ function clearExtensionWidgets(tab) {
|
|
|
5167
5231
|
tab?.extensionWidgets?.clear();
|
|
5168
5232
|
}
|
|
5169
5233
|
|
|
5234
|
+
function naturalConversationStatusState(statusText) {
|
|
5235
|
+
const text = stripAnsi(statusText).replace(/\s+/g, " ").trim();
|
|
5236
|
+
if (!text) return { enabled: false, uiState: "off", statusText: "" };
|
|
5237
|
+
const match = text.match(/voice\s*:\s*([a-z0-9_-]+)/i);
|
|
5238
|
+
return { enabled: true, uiState: (match?.[1] || "listening").toLowerCase(), statusText: text };
|
|
5239
|
+
}
|
|
5240
|
+
|
|
5241
|
+
function naturalConversationModeSnapshot(tab, patch = {}) {
|
|
5242
|
+
const previous = tab?.conversationMode && typeof tab.conversationMode === "object" ? tab.conversationMode : {};
|
|
5243
|
+
const status = naturalConversationStatusState(extensionStatusMap(tab).get(NATURAL_CONVERSATION_STATUS_KEY) || previous.statusText || "");
|
|
5244
|
+
const enabled = patch.enabled ?? status.enabled ?? previous.enabled ?? false;
|
|
5245
|
+
const uiState = patch.uiState || (enabled ? status.uiState || previous.uiState || "listening" : "off");
|
|
5246
|
+
return {
|
|
5247
|
+
featureId: NATURAL_CONVERSATION_FEATURE_ID,
|
|
5248
|
+
available: patch.available ?? previous.available ?? false,
|
|
5249
|
+
enabled,
|
|
5250
|
+
uiState,
|
|
5251
|
+
statusText: patch.statusText ?? status.statusText ?? previous.statusText ?? "",
|
|
5252
|
+
allowedTools: Array.isArray(patch.allowedTools) ? patch.allowedTools : Array.isArray(previous.allowedTools) ? previous.allowedTools : ["read", "grep", "find", "ls"],
|
|
5253
|
+
provider: patch.provider || previous.provider || "browser-shell",
|
|
5254
|
+
startedAt: patch.startedAt ?? previous.startedAt ?? null,
|
|
5255
|
+
packageInstalled: patch.packageInstalled ?? previous.packageInstalled ?? false,
|
|
5256
|
+
loadedCommands: Array.isArray(patch.loadedCommands) ? patch.loadedCommands : Array.isArray(previous.loadedCommands) ? previous.loadedCommands : [],
|
|
5257
|
+
updatedAt: patch.updatedAt || previous.updatedAt || new Date().toISOString(),
|
|
5258
|
+
};
|
|
5259
|
+
}
|
|
5260
|
+
|
|
5261
|
+
function resetNaturalConversationMode(tab) {
|
|
5262
|
+
if (!tab) return;
|
|
5263
|
+
tab.conversationMode = naturalConversationModeSnapshot(tab, { available: false, enabled: false, uiState: "off", statusText: "", loadedCommands: [] });
|
|
5264
|
+
}
|
|
5265
|
+
|
|
5266
|
+
function rememberNaturalConversationStatusEvent(tab, event) {
|
|
5267
|
+
if (event?.type !== "extension_ui_request" || event.method !== "setStatus" || event.statusKey !== NATURAL_CONVERSATION_STATUS_KEY) return;
|
|
5268
|
+
const status = naturalConversationStatusState(event.statusText || "");
|
|
5269
|
+
tab.conversationMode = naturalConversationModeSnapshot(tab, {
|
|
5270
|
+
...status,
|
|
5271
|
+
available: true,
|
|
5272
|
+
loadedCommands: tab?.conversationMode?.loadedCommands?.length ? tab.conversationMode.loadedCommands : ["talk"],
|
|
5273
|
+
});
|
|
5274
|
+
}
|
|
5275
|
+
|
|
5276
|
+
function isNaturalConversationActive(tab) {
|
|
5277
|
+
return naturalConversationModeSnapshot(tab).enabled === true;
|
|
5278
|
+
}
|
|
5279
|
+
|
|
5280
|
+
function naturalConversationCommandBaseName(name) {
|
|
5281
|
+
return String(name || "").trim().toLowerCase().replace(/:\d+$/, "");
|
|
5282
|
+
}
|
|
5283
|
+
|
|
5284
|
+
function naturalConversationSlashCommandName(message) {
|
|
5285
|
+
const match = String(message || "").trim().match(/^\/([^\s]+)/);
|
|
5286
|
+
return match ? naturalConversationCommandBaseName(match[1]) : "";
|
|
5287
|
+
}
|
|
5288
|
+
|
|
5289
|
+
function isNaturalConversationSlashCommand(message) {
|
|
5290
|
+
return NATURAL_CONVERSATION_COMMAND_NAMES.includes(naturalConversationSlashCommandName(message));
|
|
5291
|
+
}
|
|
5292
|
+
|
|
5293
|
+
function blockNaturalConversationAction(action) {
|
|
5294
|
+
throw makeHttpError(409, `Natural Conversation Mode is active; ${action}. Leave the mode first with /talk off.`);
|
|
5295
|
+
}
|
|
5296
|
+
|
|
5297
|
+
function ensureNaturalConversationRouteAllowed(tab, action) {
|
|
5298
|
+
if (isNaturalConversationActive(tab)) blockNaturalConversationAction(action);
|
|
5299
|
+
}
|
|
5300
|
+
|
|
5301
|
+
async function ensureNaturalConversationPromptSafety(tab, command) {
|
|
5302
|
+
if (!isNaturalConversationActive(tab) || !["prompt", "steer", "follow_up"].includes(command?.type)) return null;
|
|
5303
|
+
tab.pendingThinkingLevel = undefined;
|
|
5304
|
+
const stateResult = await safeRpcData(tab, { type: "get_state" }, STATUS_RPC_TIMEOUT_MS);
|
|
5305
|
+
if (stateResult.ok && stateIsBusyForSettings(stateResult.data)) return null;
|
|
5306
|
+
const response = await setThinkingLevelForTab(tab, "off", { allowPending: false });
|
|
5307
|
+
return response?.success === false ? response : null;
|
|
5308
|
+
}
|
|
5309
|
+
|
|
5310
|
+
function enforceNaturalConversationCommandAllowed(tab, command) {
|
|
5311
|
+
if (!isNaturalConversationActive(tab)) return;
|
|
5312
|
+
if (command?.type === "set_thinking_level") {
|
|
5313
|
+
if (command.level !== "off") blockNaturalConversationAction("thinking is forced off");
|
|
5314
|
+
return;
|
|
5315
|
+
}
|
|
5316
|
+
if (!["prompt", "steer", "follow_up", "abort", "abort_bash"].includes(command?.type)) {
|
|
5317
|
+
blockNaturalConversationAction(`${command?.type || "this action"} is blocked`);
|
|
5318
|
+
}
|
|
5319
|
+
if (command?.type === "prompt" && naturalConversationSlashCommandName(command.message) && !isNaturalConversationSlashCommand(command.message)) {
|
|
5320
|
+
blockNaturalConversationAction("slash commands are blocked from the Web UI shell");
|
|
5321
|
+
}
|
|
5322
|
+
}
|
|
5323
|
+
|
|
5170
5324
|
function replayExtensionStatuses(tab, res) {
|
|
5171
5325
|
for (const [statusKey, statusText] of extensionStatusMap(tab)) {
|
|
5172
5326
|
sendSse(res, {
|
|
@@ -5207,6 +5361,160 @@ function bashQueueForTab(tab) {
|
|
|
5207
5361
|
return tab.bashQueue;
|
|
5208
5362
|
}
|
|
5209
5363
|
|
|
5364
|
+
function compactionQueueForTab(tab) {
|
|
5365
|
+
if (!tab.compactionQueue) tab.compactionQueue = [];
|
|
5366
|
+
return tab.compactionQueue;
|
|
5367
|
+
}
|
|
5368
|
+
|
|
5369
|
+
function queueableCompactionCommand(command) {
|
|
5370
|
+
return ["prompt", "steer", "follow_up"].includes(command?.type) && !!String(command.message || "").trim();
|
|
5371
|
+
}
|
|
5372
|
+
|
|
5373
|
+
function compactionQueueMode(command) {
|
|
5374
|
+
if (command?.type === "follow_up" || command?.streamingBehavior === "followUp") return "followUp";
|
|
5375
|
+
return "steer";
|
|
5376
|
+
}
|
|
5377
|
+
|
|
5378
|
+
function compactQueuedCommand(command) {
|
|
5379
|
+
const queued = {
|
|
5380
|
+
type: command.type,
|
|
5381
|
+
message: String(command.message || ""),
|
|
5382
|
+
mode: compactionQueueMode(command),
|
|
5383
|
+
};
|
|
5384
|
+
if (Array.isArray(command.images) && command.images.length) queued.images = command.images;
|
|
5385
|
+
return queued;
|
|
5386
|
+
}
|
|
5387
|
+
|
|
5388
|
+
function compactionQueueEvent(tab, extra = {}) {
|
|
5389
|
+
const queue = compactionQueueForTab(tab);
|
|
5390
|
+
const steering = [];
|
|
5391
|
+
const followUp = [];
|
|
5392
|
+
for (const item of queue) {
|
|
5393
|
+
const message = String(item?.command?.message || "").trim();
|
|
5394
|
+
if (!message) continue;
|
|
5395
|
+
if (item.command.mode === "followUp") followUp.push(message);
|
|
5396
|
+
else steering.push(message);
|
|
5397
|
+
}
|
|
5398
|
+
return {
|
|
5399
|
+
type: "webui_compaction_queue_update",
|
|
5400
|
+
tabId: tab.id,
|
|
5401
|
+
tabTitle: tab.title,
|
|
5402
|
+
queueLength: queue.length,
|
|
5403
|
+
pendingMessageCount: queue.length,
|
|
5404
|
+
steering,
|
|
5405
|
+
followUp,
|
|
5406
|
+
draining: tab.compactionQueueDraining === true,
|
|
5407
|
+
tabActivity: tabActivitySnapshot(tab),
|
|
5408
|
+
...extra,
|
|
5409
|
+
};
|
|
5410
|
+
}
|
|
5411
|
+
|
|
5412
|
+
function enqueueCommandUntilCompactionEnds(tab, command) {
|
|
5413
|
+
const queue = compactionQueueForTab(tab);
|
|
5414
|
+
const item = {
|
|
5415
|
+
id: randomUUID(),
|
|
5416
|
+
command: compactQueuedCommand(command),
|
|
5417
|
+
enqueuedAt: new Date().toISOString(),
|
|
5418
|
+
};
|
|
5419
|
+
queue.push(item);
|
|
5420
|
+
broadcastTabEvent(tab, compactionQueueEvent(tab, { queuedId: item.id }));
|
|
5421
|
+
return rpcSuccess(command.type, {
|
|
5422
|
+
queued: true,
|
|
5423
|
+
queuedFor: "compaction",
|
|
5424
|
+
queueLength: queue.length,
|
|
5425
|
+
message: "Prompt queued; Pi will resume automatically after compaction finishes.",
|
|
5426
|
+
});
|
|
5427
|
+
}
|
|
5428
|
+
|
|
5429
|
+
function maybeQueueCommandDuringCompaction(tab, command) {
|
|
5430
|
+
if (!queueableCompactionCommand(command)) return null;
|
|
5431
|
+
if (!tab?.lastState?.isCompacting) return null;
|
|
5432
|
+
return enqueueCommandUntilCompactionEnds(tab, command);
|
|
5433
|
+
}
|
|
5434
|
+
|
|
5435
|
+
function queuedPromptCommand(item) {
|
|
5436
|
+
const command = { type: "prompt", message: item.command.message };
|
|
5437
|
+
if (Array.isArray(item.command.images) && item.command.images.length) command.images = item.command.images;
|
|
5438
|
+
return command;
|
|
5439
|
+
}
|
|
5440
|
+
|
|
5441
|
+
function queuedStreamingCommand(item) {
|
|
5442
|
+
const command = {
|
|
5443
|
+
type: item.command.mode === "followUp" ? "follow_up" : "steer",
|
|
5444
|
+
message: item.command.message,
|
|
5445
|
+
};
|
|
5446
|
+
if (Array.isArray(item.command.images) && item.command.images.length) command.images = item.command.images;
|
|
5447
|
+
return command;
|
|
5448
|
+
}
|
|
5449
|
+
|
|
5450
|
+
function queuedRetryCommand(item) {
|
|
5451
|
+
const message = String(item.command.message || "").trim();
|
|
5452
|
+
if (item.command.type === "prompt" && message.startsWith("/")) {
|
|
5453
|
+
const command = queuedPromptCommand(item);
|
|
5454
|
+
command.streamingBehavior = item.command.mode === "followUp" ? "followUp" : "steer";
|
|
5455
|
+
return command;
|
|
5456
|
+
}
|
|
5457
|
+
return queuedStreamingCommand(item);
|
|
5458
|
+
}
|
|
5459
|
+
|
|
5460
|
+
function requeueCompactionItems(tab, items) {
|
|
5461
|
+
if (!items?.length) return;
|
|
5462
|
+
const queue = compactionQueueForTab(tab);
|
|
5463
|
+
queue.unshift(...items);
|
|
5464
|
+
broadcastTabEvent(tab, compactionQueueEvent(tab));
|
|
5465
|
+
}
|
|
5466
|
+
|
|
5467
|
+
async function compactionFlushShouldJoinActiveRun(tab, event = {}) {
|
|
5468
|
+
if (event.willRetry === true) return true;
|
|
5469
|
+
await delay(80);
|
|
5470
|
+
const state = await safeRpcData(tab, { type: "get_state" }, STATUS_RPC_TIMEOUT_MS).catch(() => ({ ok: false }));
|
|
5471
|
+
if (state.ok) return !!state.data?.isStreaming;
|
|
5472
|
+
return !!tab.lastState?.isStreaming;
|
|
5473
|
+
}
|
|
5474
|
+
|
|
5475
|
+
async function flushCompactionQueue(tab, event = {}) {
|
|
5476
|
+
const queue = compactionQueueForTab(tab);
|
|
5477
|
+
if (!queue.length || tab.compactionQueueDraining) return;
|
|
5478
|
+
const items = queue.splice(0);
|
|
5479
|
+
tab.compactionQueueDraining = true;
|
|
5480
|
+
broadcastTabEvent(tab, compactionQueueEvent(tab));
|
|
5481
|
+
const remaining = [...items];
|
|
5482
|
+
try {
|
|
5483
|
+
const joinActiveRun = await compactionFlushShouldJoinActiveRun(tab, event);
|
|
5484
|
+
if (joinActiveRun) {
|
|
5485
|
+
while (remaining.length) {
|
|
5486
|
+
const item = remaining[0];
|
|
5487
|
+
const response = await tab.rpc.send(queuedRetryCommand(item));
|
|
5488
|
+
if (response.success === false) throw new Error(response.error || "failed to queue prompt after compaction");
|
|
5489
|
+
remaining.shift();
|
|
5490
|
+
}
|
|
5491
|
+
} else {
|
|
5492
|
+
let startedPrompt = false;
|
|
5493
|
+
while (remaining.length) {
|
|
5494
|
+
const item = remaining[0];
|
|
5495
|
+
const command = startedPrompt ? queuedStreamingCommand(item) : queuedPromptCommand(item);
|
|
5496
|
+
if (!startedPrompt) {
|
|
5497
|
+
const pendingThinkingResponse = await applyPendingThinkingBeforePrompt(tab);
|
|
5498
|
+
if (pendingThinkingResponse?.success === false) throw new Error(pendingThinkingResponse.error || "failed to apply queued thinking level");
|
|
5499
|
+
maybeNameTabForConversation(tab, command);
|
|
5500
|
+
markTabWorking(tab);
|
|
5501
|
+
}
|
|
5502
|
+
const response = await tab.rpc.send(command, command.type === "prompt" ? PROMPT_REQUEST_TIMEOUT_MS : REQUEST_TIMEOUT_MS);
|
|
5503
|
+
if (response.success === false) throw new Error(response.error || "failed to resume after compaction");
|
|
5504
|
+
remaining.shift();
|
|
5505
|
+
if (command.type === "prompt") startedPrompt = true;
|
|
5506
|
+
}
|
|
5507
|
+
}
|
|
5508
|
+
broadcastTabEvent(tab, compactionQueueEvent(tab, { drained: true }));
|
|
5509
|
+
} catch (error) {
|
|
5510
|
+
requeueCompactionItems(tab, remaining);
|
|
5511
|
+
broadcastTabEvent(tab, compactionQueueEvent(tab, { error: sanitizeError(error) }));
|
|
5512
|
+
} finally {
|
|
5513
|
+
tab.compactionQueueDraining = false;
|
|
5514
|
+
broadcastTabEvent(tab, compactionQueueEvent(tab));
|
|
5515
|
+
}
|
|
5516
|
+
}
|
|
5517
|
+
|
|
5210
5518
|
function settleBashQueueItem(item, kind, value) {
|
|
5211
5519
|
if (!item || item.settled) return;
|
|
5212
5520
|
item.settled = true;
|
|
@@ -5446,13 +5754,24 @@ function updateTabActivityFromEvent(tab, event) {
|
|
|
5446
5754
|
const timestamp = new Date().toISOString();
|
|
5447
5755
|
switch (event?.type) {
|
|
5448
5756
|
case "agent_start":
|
|
5757
|
+
patchTabState(tab, { isStreaming: true });
|
|
5758
|
+
markTabWorking(tab, timestamp);
|
|
5759
|
+
break;
|
|
5449
5760
|
case "compaction_start":
|
|
5761
|
+
patchTabState(tab, { isCompacting: true });
|
|
5450
5762
|
markTabWorking(tab, timestamp);
|
|
5451
5763
|
break;
|
|
5452
5764
|
case "agent_end":
|
|
5765
|
+
patchTabState(tab, { isStreaming: false });
|
|
5766
|
+
markTabDone(tab, timestamp);
|
|
5767
|
+
break;
|
|
5453
5768
|
case "compaction_end":
|
|
5769
|
+
patchTabState(tab, { isCompacting: false });
|
|
5454
5770
|
markTabDone(tab, timestamp);
|
|
5455
5771
|
break;
|
|
5772
|
+
case "queue_update":
|
|
5773
|
+
patchTabState(tab, { pendingMessageCount: (event.steering?.length || 0) + (event.followUp?.length || 0) });
|
|
5774
|
+
break;
|
|
5456
5775
|
case "pi_process_exit":
|
|
5457
5776
|
case "pi_process_error":
|
|
5458
5777
|
if (tab.activity?.isWorking) markTabDone(tab, timestamp);
|
|
@@ -5499,14 +5818,17 @@ function attachRpcToTab(tab, rpc) {
|
|
|
5499
5818
|
clearPendingExtensionUiRequests(tab);
|
|
5500
5819
|
clearExtensionStatuses(tab);
|
|
5501
5820
|
clearExtensionWidgets(tab);
|
|
5821
|
+
resetNaturalConversationMode(tab);
|
|
5502
5822
|
} else {
|
|
5503
5823
|
rememberExtensionStatusEvent(tab, scopedEvent);
|
|
5824
|
+
rememberNaturalConversationStatusEvent(tab, scopedEvent);
|
|
5504
5825
|
rememberExtensionWidgetEvent(tab, scopedEvent);
|
|
5505
5826
|
trackPendingExtensionUiRequest(tab, scopedEvent);
|
|
5506
5827
|
}
|
|
5507
5828
|
scopedEvent = { ...scopedEvent, tabActivity: tabActivitySnapshot(tab), pendingExtensionUiRequestCount: pendingExtensionUiRequests(tab).length };
|
|
5508
5829
|
recordEvent(scopedEvent);
|
|
5509
5830
|
for (const client of tab.sseClients) sendSse(client, scopedEvent);
|
|
5831
|
+
if (event?.type === "compaction_end" && event.aborted !== true) void flushCompactionQueue(tab, event);
|
|
5510
5832
|
});
|
|
5511
5833
|
}
|
|
5512
5834
|
|
|
@@ -5544,10 +5866,13 @@ async function createTab({ id: requestedId, index, title, titleSource, conversat
|
|
|
5544
5866
|
webuiHelperResponseIds: new Set(),
|
|
5545
5867
|
bashQueue: [],
|
|
5546
5868
|
bashQueueDraining: false,
|
|
5869
|
+
compactionQueue: [],
|
|
5870
|
+
compactionQueueDraining: false,
|
|
5547
5871
|
rpc: undefined,
|
|
5548
5872
|
rpcUnsubscribe: undefined,
|
|
5549
5873
|
sseClients: new Set(),
|
|
5550
5874
|
};
|
|
5875
|
+
resetNaturalConversationMode(tab);
|
|
5551
5876
|
|
|
5552
5877
|
attachRpcToTab(tab, rpc);
|
|
5553
5878
|
tabs.set(id, tab);
|
|
@@ -5591,6 +5916,7 @@ function tabMeta(tab) {
|
|
|
5591
5916
|
pendingExtensionUiRequestCount: pendingExtensionUiRequests(tab).length,
|
|
5592
5917
|
activity: tabActivitySnapshot(tab),
|
|
5593
5918
|
appRunner: publicAppRunnerState(tab.appRunner),
|
|
5919
|
+
conversationMode: naturalConversationModeSnapshot(tab),
|
|
5594
5920
|
};
|
|
5595
5921
|
}
|
|
5596
5922
|
|
|
@@ -6132,6 +6458,7 @@ async function updateTabCwd(id, cwd) {
|
|
|
6132
6458
|
clearPendingExtensionUiRequests(tab);
|
|
6133
6459
|
clearExtensionStatuses(tab);
|
|
6134
6460
|
clearExtensionWidgets(tab);
|
|
6461
|
+
resetNaturalConversationMode(tab);
|
|
6135
6462
|
const rpc = new PiRpcProcess({ ...piCommand, cwd: tab.cwd });
|
|
6136
6463
|
attachRpcToTab(tab, rpc);
|
|
6137
6464
|
rpc.start();
|
|
@@ -6170,6 +6497,7 @@ async function restartTabRpc(tab, reason = "reload") {
|
|
|
6170
6497
|
clearPendingExtensionUiRequests(tab);
|
|
6171
6498
|
clearExtensionStatuses(tab);
|
|
6172
6499
|
clearExtensionWidgets(tab);
|
|
6500
|
+
resetNaturalConversationMode(tab);
|
|
6173
6501
|
const rpc = new PiRpcProcess({ ...piCommand, cwd: tab.cwd });
|
|
6174
6502
|
attachRpcToTab(tab, rpc);
|
|
6175
6503
|
rpc.start();
|
|
@@ -6754,19 +7082,119 @@ async function annotateSkillCommandState(tab, commands) {
|
|
|
6754
7082
|
});
|
|
6755
7083
|
}
|
|
6756
7084
|
|
|
7085
|
+
function naturalConversationLoadedCommandNames(commands = []) {
|
|
7086
|
+
const names = [];
|
|
7087
|
+
const seen = new Set();
|
|
7088
|
+
for (const command of commands || []) {
|
|
7089
|
+
const baseName = naturalConversationCommandBaseName(command?.name || command?.invokeName || "");
|
|
7090
|
+
if (!NATURAL_CONVERSATION_COMMAND_NAMES.includes(baseName) || seen.has(baseName)) continue;
|
|
7091
|
+
seen.add(baseName);
|
|
7092
|
+
names.push(baseName);
|
|
7093
|
+
}
|
|
7094
|
+
return names;
|
|
7095
|
+
}
|
|
7096
|
+
|
|
7097
|
+
function rememberNaturalConversationCommands(tab, commands = []) {
|
|
7098
|
+
const loadedCommands = naturalConversationLoadedCommandNames(commands);
|
|
7099
|
+
const available = loadedCommands.length > 0;
|
|
7100
|
+
tab.conversationMode = naturalConversationModeSnapshot(tab, {
|
|
7101
|
+
available,
|
|
7102
|
+
enabled: available ? undefined : false,
|
|
7103
|
+
uiState: available ? undefined : "off",
|
|
7104
|
+
statusText: available ? undefined : "",
|
|
7105
|
+
loadedCommands,
|
|
7106
|
+
});
|
|
7107
|
+
return tab.conversationMode;
|
|
7108
|
+
}
|
|
7109
|
+
|
|
6757
7110
|
async function getCommandData(tab) {
|
|
6758
7111
|
try {
|
|
6759
7112
|
const response = await tab.rpc.send({ type: "get_commands" });
|
|
6760
7113
|
if (response.success === false) throw makeHttpError(400, response.error || "failed to load commands");
|
|
6761
7114
|
const rpcCommands = await annotateSkillCommandState(tab, response.data?.commands || []);
|
|
7115
|
+
rememberNaturalConversationCommands(tab, rpcCommands);
|
|
6762
7116
|
return { commands: [...NATIVE_SLASH_COMMANDS, ...rpcCommands], rpcRunning: true };
|
|
6763
7117
|
} catch (error) {
|
|
6764
7118
|
const message = sanitizeError(error);
|
|
6765
7119
|
if (!/Pi RPC process is not running/i.test(message)) throw error;
|
|
7120
|
+
rememberNaturalConversationCommands(tab, []);
|
|
6766
7121
|
return { commands: [...NATIVE_SLASH_COMMANDS], rpcRunning: false, error: message };
|
|
6767
7122
|
}
|
|
6768
7123
|
}
|
|
6769
7124
|
|
|
7125
|
+
async function naturalConversationPackageStatus() {
|
|
7126
|
+
try {
|
|
7127
|
+
return await optionalFeaturePackageStatus(NATURAL_CONVERSATION_FEATURE_ID);
|
|
7128
|
+
} catch (error) {
|
|
7129
|
+
return { featureId: NATURAL_CONVERSATION_FEATURE_ID, packageName: OPTIONAL_FEATURE_PACKAGES.get(NATURAL_CONVERSATION_FEATURE_ID), installed: false, error: sanitizeError(error) };
|
|
7130
|
+
}
|
|
7131
|
+
}
|
|
7132
|
+
|
|
7133
|
+
async function naturalConversationFeatureData(tab, { refreshCommands = true } = {}) {
|
|
7134
|
+
let commandData = null;
|
|
7135
|
+
let commandError = "";
|
|
7136
|
+
if (refreshCommands) {
|
|
7137
|
+
try {
|
|
7138
|
+
commandData = await getCommandData(tab);
|
|
7139
|
+
} catch (error) {
|
|
7140
|
+
commandError = sanitizeError(error);
|
|
7141
|
+
rememberNaturalConversationCommands(tab, []);
|
|
7142
|
+
}
|
|
7143
|
+
}
|
|
7144
|
+
const packageStatus = await naturalConversationPackageStatus();
|
|
7145
|
+
tab.conversationMode = naturalConversationModeSnapshot(tab, { packageInstalled: packageStatus.installed === true });
|
|
7146
|
+
const mode = naturalConversationModeSnapshot(tab);
|
|
7147
|
+
const available = mode.available === true;
|
|
7148
|
+
return {
|
|
7149
|
+
featureId: NATURAL_CONVERSATION_FEATURE_ID,
|
|
7150
|
+
packageName: OPTIONAL_FEATURE_PACKAGES.get(NATURAL_CONVERSATION_FEATURE_ID),
|
|
7151
|
+
available,
|
|
7152
|
+
packageInstalled: packageStatus.installed === true,
|
|
7153
|
+
packageStatus,
|
|
7154
|
+
commands: mode.loadedCommands,
|
|
7155
|
+
mode,
|
|
7156
|
+
rpcRunning: commandData?.rpcRunning !== false && !commandError,
|
|
7157
|
+
unavailableReason: available
|
|
7158
|
+
? ""
|
|
7159
|
+
: packageStatus.installed
|
|
7160
|
+
? "Natural Conversation package is installed, but /talk is not loaded in the active Pi tab. Reload the tab or enable the package extension."
|
|
7161
|
+
: "Natural Conversation package is not installed or not visible from the Web UI package root.",
|
|
7162
|
+
error: commandError || undefined,
|
|
7163
|
+
};
|
|
7164
|
+
}
|
|
7165
|
+
|
|
7166
|
+
async function setNaturalConversationMode(tab, body = {}) {
|
|
7167
|
+
const desired = body.enabled === true;
|
|
7168
|
+
const feature = await naturalConversationFeatureData(tab);
|
|
7169
|
+
if (!feature.available) throw makeHttpError(404, feature.unavailableReason);
|
|
7170
|
+
const commandName = feature.commands.find((name) => name === "talk") || feature.commands[0] || "talk";
|
|
7171
|
+
const response = await tab.rpc.send({ type: "prompt", message: `/${commandName} ${desired ? "on" : "off"}` }, REQUEST_TIMEOUT_MS);
|
|
7172
|
+
if (response.success === false) throw makeHttpError(400, response.error || `Failed to ${desired ? "enable" : "disable"} Natural Conversation Mode`);
|
|
7173
|
+
tab.conversationMode = naturalConversationModeSnapshot(tab, {
|
|
7174
|
+
available: true,
|
|
7175
|
+
enabled: desired,
|
|
7176
|
+
uiState: desired ? "listening" : "off",
|
|
7177
|
+
statusText: desired ? `Voice: listening` : "",
|
|
7178
|
+
loadedCommands: feature.commands,
|
|
7179
|
+
packageInstalled: feature.packageInstalled,
|
|
7180
|
+
startedAt: desired ? naturalConversationModeSnapshot(tab).startedAt || new Date().toISOString() : null,
|
|
7181
|
+
});
|
|
7182
|
+
if (desired) {
|
|
7183
|
+
tab.pendingThinkingLevel = undefined;
|
|
7184
|
+
await setThinkingLevelForTab(tab, "off", { allowPending: false }).catch(() => null);
|
|
7185
|
+
}
|
|
7186
|
+
return { ...(await naturalConversationFeatureData(tab, { refreshCommands: false })), response };
|
|
7187
|
+
}
|
|
7188
|
+
|
|
7189
|
+
function naturalConversationUnavailableResponse(tab) {
|
|
7190
|
+
return {
|
|
7191
|
+
featureId: NATURAL_CONVERSATION_FEATURE_ID,
|
|
7192
|
+
available: false,
|
|
7193
|
+
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.",
|
|
7195
|
+
};
|
|
7196
|
+
}
|
|
7197
|
+
|
|
6770
7198
|
function resolveCliPath(value) {
|
|
6771
7199
|
const text = String(value || "").trim();
|
|
6772
7200
|
if (!text) return "";
|
|
@@ -7215,6 +7643,12 @@ async function handleNativeSlashCommand(tab, body, req) {
|
|
|
7215
7643
|
const response = await tab.rpc.send({ type: "new_session" });
|
|
7216
7644
|
if (response.success === false) return response;
|
|
7217
7645
|
tab.conversationStarted = false;
|
|
7646
|
+
forgetTabState(tab);
|
|
7647
|
+
rememberTabState(tab, response.data);
|
|
7648
|
+
clearPendingExtensionUiRequests(tab);
|
|
7649
|
+
clearExtensionStatuses(tab);
|
|
7650
|
+
clearExtensionWidgets(tab);
|
|
7651
|
+
resetNaturalConversationMode(tab);
|
|
7218
7652
|
return respondNative("new", {
|
|
7219
7653
|
status: "succeeded",
|
|
7220
7654
|
message: "Started a new session.",
|
|
@@ -7678,6 +8112,10 @@ async function setThinkingLevelForTab(tab, level, { allowPending = true } = {})
|
|
|
7678
8112
|
}
|
|
7679
8113
|
|
|
7680
8114
|
async function applyPendingThinkingBeforePrompt(tab) {
|
|
8115
|
+
if (isNaturalConversationActive(tab)) {
|
|
8116
|
+
tab.pendingThinkingLevel = undefined;
|
|
8117
|
+
return null;
|
|
8118
|
+
}
|
|
7681
8119
|
const level = tab?.pendingThinkingLevel;
|
|
7682
8120
|
if (!level) return null;
|
|
7683
8121
|
const stateResult = await safeRpcData(tab, { type: "get_state" }, STATUS_RPC_TIMEOUT_MS);
|
|
@@ -8006,6 +8444,7 @@ const server = createServer(async (req, res) => {
|
|
|
8006
8444
|
if (url.pathname === "/api/app-runner" && req.method === "POST") {
|
|
8007
8445
|
const body = await readJsonBody(req);
|
|
8008
8446
|
const tab = getRequestedTab(req, url, body);
|
|
8447
|
+
ensureNaturalConversationRouteAllowed(tab, "app runner actions are blocked");
|
|
8009
8448
|
sendJson(res, 200, { ok: true, data: await startAppRunner(tab, String(body.runnerId || body.id || "")) });
|
|
8010
8449
|
return;
|
|
8011
8450
|
}
|
|
@@ -8013,6 +8452,7 @@ const server = createServer(async (req, res) => {
|
|
|
8013
8452
|
if (url.pathname === "/api/app-runner/input" && req.method === "POST") {
|
|
8014
8453
|
const body = await readJsonBody(req);
|
|
8015
8454
|
const tab = getRequestedTab(req, url, body);
|
|
8455
|
+
ensureNaturalConversationRouteAllowed(tab, "app runner actions are blocked");
|
|
8016
8456
|
const text = Object.prototype.hasOwnProperty.call(body, "text") ? body.text : body.input;
|
|
8017
8457
|
sendJson(res, 200, { ok: true, data: sendAppRunnerInput(tab, text, { appendNewline: body.newline !== false, closeStdin: body.closeStdin === true || body.close === true }) });
|
|
8018
8458
|
return;
|
|
@@ -8021,6 +8461,7 @@ const server = createServer(async (req, res) => {
|
|
|
8021
8461
|
if (url.pathname === "/api/app-runner/context" && req.method === "POST") {
|
|
8022
8462
|
const body = await readJsonBody(req);
|
|
8023
8463
|
const tab = getRequestedTab(req, url, body);
|
|
8464
|
+
ensureNaturalConversationRouteAllowed(tab, "app runner actions are blocked");
|
|
8024
8465
|
sendJson(res, 200, { ok: true, data: await transferAppRunnerContext(tab, body) });
|
|
8025
8466
|
return;
|
|
8026
8467
|
}
|
|
@@ -8028,6 +8469,7 @@ const server = createServer(async (req, res) => {
|
|
|
8028
8469
|
if (url.pathname === "/api/app-runner/stop" && req.method === "POST") {
|
|
8029
8470
|
const body = await readJsonBody(req);
|
|
8030
8471
|
const tab = getRequestedTab(req, url, body);
|
|
8472
|
+
ensureNaturalConversationRouteAllowed(tab, "app runner actions are blocked");
|
|
8031
8473
|
stopAppRunnerForTab(tab, "stop requested from Web UI");
|
|
8032
8474
|
sendJson(res, 200, { ok: true, data: await getAppRunnerData(tab) });
|
|
8033
8475
|
return;
|
|
@@ -8036,6 +8478,7 @@ const server = createServer(async (req, res) => {
|
|
|
8036
8478
|
if (url.pathname === "/api/app-runner/clear" && req.method === "POST") {
|
|
8037
8479
|
const body = await readJsonBody(req);
|
|
8038
8480
|
const tab = getRequestedTab(req, url, body);
|
|
8481
|
+
ensureNaturalConversationRouteAllowed(tab, "app runner actions are blocked");
|
|
8039
8482
|
clearAppRunnerForTab(tab);
|
|
8040
8483
|
sendJson(res, 200, { ok: true, data: await getAppRunnerData(tab) });
|
|
8041
8484
|
return;
|
|
@@ -8050,6 +8493,7 @@ const server = createServer(async (req, res) => {
|
|
|
8050
8493
|
if (url.pathname === "/api/app-runner-config" && req.method === "POST") {
|
|
8051
8494
|
const body = await readJsonBody(req);
|
|
8052
8495
|
const tab = getRequestedTab(req, url, body);
|
|
8496
|
+
ensureNaturalConversationRouteAllowed(tab, "app runner configuration changes are blocked");
|
|
8053
8497
|
sendJson(res, 200, { ok: true, data: await saveCustomAppRunner(tab, body.runner || body) });
|
|
8054
8498
|
return;
|
|
8055
8499
|
}
|
|
@@ -8057,6 +8501,7 @@ const server = createServer(async (req, res) => {
|
|
|
8057
8501
|
if (url.pathname === "/api/app-runner-config" && req.method === "DELETE") {
|
|
8058
8502
|
const body = await readJsonBody(req);
|
|
8059
8503
|
const tab = getRequestedTab(req, url, body);
|
|
8504
|
+
ensureNaturalConversationRouteAllowed(tab, "app runner configuration changes are blocked");
|
|
8060
8505
|
sendJson(res, 200, { ok: true, data: await deleteCustomAppRunner(tab, body.id || body.runnerId) });
|
|
8061
8506
|
return;
|
|
8062
8507
|
}
|
|
@@ -8078,6 +8523,8 @@ const server = createServer(async (req, res) => {
|
|
|
8078
8523
|
|
|
8079
8524
|
if (url.pathname === "/api/directories" && req.method === "POST") {
|
|
8080
8525
|
const body = await readJsonBody(req);
|
|
8526
|
+
const tab = getRequestedTab(req, url, body);
|
|
8527
|
+
ensureNaturalConversationRouteAllowed(tab, "directory creation is blocked");
|
|
8081
8528
|
const activeCwd = directoryPickerActiveCwd(req, url, body);
|
|
8082
8529
|
sendJson(res, 201, {
|
|
8083
8530
|
ok: true,
|
|
@@ -8099,6 +8546,8 @@ const server = createServer(async (req, res) => {
|
|
|
8099
8546
|
|
|
8100
8547
|
if (url.pathname === "/api/path-fast-picks" && req.method === "POST") {
|
|
8101
8548
|
const body = await readJsonBody(req);
|
|
8549
|
+
const tab = getRequestedTab(req, url, body);
|
|
8550
|
+
ensureNaturalConversationRouteAllowed(tab, "path fast-pick changes are blocked");
|
|
8102
8551
|
const picks = await writePathFastPicks(body.picks ?? body);
|
|
8103
8552
|
sendJson(res, 200, { ok: true, data: { picks } });
|
|
8104
8553
|
return;
|
|
@@ -8106,6 +8555,8 @@ const server = createServer(async (req, res) => {
|
|
|
8106
8555
|
|
|
8107
8556
|
if (url.pathname === "/api/attachments" && req.method === "POST") {
|
|
8108
8557
|
const body = await readJsonBody(req, { limitBytes: requestBodyLimitForPath(url.pathname) });
|
|
8558
|
+
const tab = getRequestedTab(req, url, body);
|
|
8559
|
+
ensureNaturalConversationRouteAllowed(tab, "attachment uploads are blocked");
|
|
8109
8560
|
sendJson(res, 201, { ok: true, data: await saveUploadedAttachments(body) });
|
|
8110
8561
|
return;
|
|
8111
8562
|
}
|
|
@@ -8119,6 +8570,7 @@ const server = createServer(async (req, res) => {
|
|
|
8119
8570
|
if (url.pathname === "/api/model-cycle" && req.method === "POST") {
|
|
8120
8571
|
const body = await readJsonBody(req);
|
|
8121
8572
|
const tab = getRequestedTab(req, url, body);
|
|
8573
|
+
ensureNaturalConversationRouteAllowed(tab, "model changes are blocked");
|
|
8122
8574
|
const response = await cycleTabModel(tab, body.direction || body.mode);
|
|
8123
8575
|
sendJson(res, response.success === false ? 400 : 200, responseWithTab(response, tab));
|
|
8124
8576
|
return;
|
|
@@ -8145,6 +8597,7 @@ const server = createServer(async (req, res) => {
|
|
|
8145
8597
|
if (url.pathname === "/api/fork" && req.method === "POST") {
|
|
8146
8598
|
const body = await readJsonBody(req);
|
|
8147
8599
|
const tab = getRequestedTab(req, url, body);
|
|
8600
|
+
ensureNaturalConversationRouteAllowed(tab, "session fork actions are blocked");
|
|
8148
8601
|
const response = await runForkCommand(tab, body.entryId);
|
|
8149
8602
|
sendJson(res, response.success === false ? 400 : 200, responseWithTab(response, tab));
|
|
8150
8603
|
return;
|
|
@@ -8153,6 +8606,7 @@ const server = createServer(async (req, res) => {
|
|
|
8153
8606
|
if (url.pathname === "/api/clone" && req.method === "POST") {
|
|
8154
8607
|
const body = await readJsonBody(req);
|
|
8155
8608
|
const tab = getRequestedTab(req, url, body);
|
|
8609
|
+
ensureNaturalConversationRouteAllowed(tab, "session clone actions are blocked");
|
|
8156
8610
|
const response = await runCloneCommand(tab);
|
|
8157
8611
|
sendJson(res, response.success === false ? 400 : 200, responseWithTab(response, tab));
|
|
8158
8612
|
return;
|
|
@@ -8161,6 +8615,7 @@ const server = createServer(async (req, res) => {
|
|
|
8161
8615
|
if (url.pathname === "/api/switch-session" && req.method === "POST") {
|
|
8162
8616
|
const body = await readJsonBody(req);
|
|
8163
8617
|
const tab = getRequestedTab(req, url, body);
|
|
8618
|
+
ensureNaturalConversationRouteAllowed(tab, "session switching is blocked");
|
|
8164
8619
|
const response = await switchTabSession(tab, body.sessionPath || body.path);
|
|
8165
8620
|
sendJson(res, response.success === false ? 400 : 200, responseWithTab(response, tab));
|
|
8166
8621
|
return;
|
|
@@ -8169,6 +8624,7 @@ const server = createServer(async (req, res) => {
|
|
|
8169
8624
|
if (url.pathname === "/api/session-rename" && req.method === "POST") {
|
|
8170
8625
|
const body = await readJsonBody(req);
|
|
8171
8626
|
const tab = getRequestedTab(req, url, body);
|
|
8627
|
+
ensureNaturalConversationRouteAllowed(tab, "session renaming is blocked");
|
|
8172
8628
|
sendJson(res, 200, { ok: true, data: await renameSessionData(tab, body), tab: tabMeta(tab) });
|
|
8173
8629
|
return;
|
|
8174
8630
|
}
|
|
@@ -8177,6 +8633,7 @@ const server = createServer(async (req, res) => {
|
|
|
8177
8633
|
requireLocalhostRoute(req, url.pathname);
|
|
8178
8634
|
const body = await readJsonBody(req);
|
|
8179
8635
|
const tab = getRequestedTab(req, url, body);
|
|
8636
|
+
ensureNaturalConversationRouteAllowed(tab, "session deletion is blocked");
|
|
8180
8637
|
sendJson(res, 200, { ok: true, data: await deleteSessionData(tab, body), tab: tabMeta(tab) });
|
|
8181
8638
|
return;
|
|
8182
8639
|
}
|
|
@@ -8196,11 +8653,37 @@ const server = createServer(async (req, res) => {
|
|
|
8196
8653
|
if (url.pathname === "/api/tree-navigate" && req.method === "POST") {
|
|
8197
8654
|
const body = await readJsonBody(req);
|
|
8198
8655
|
const tab = getRequestedTab(req, url, body);
|
|
8656
|
+
ensureNaturalConversationRouteAllowed(tab, "session tree navigation is blocked");
|
|
8199
8657
|
const response = await navigateSessionTree(tab, body);
|
|
8200
8658
|
sendJson(res, response.success === false ? 400 : 200, responseWithTab(response, tab));
|
|
8201
8659
|
return;
|
|
8202
8660
|
}
|
|
8203
8661
|
|
|
8662
|
+
if (url.pathname === "/api/features/natural-conversation" && req.method === "GET") {
|
|
8663
|
+
const tab = getRequestedTab(req, url);
|
|
8664
|
+
sendJson(res, 200, { ok: true, data: await naturalConversationFeatureData(tab) });
|
|
8665
|
+
return;
|
|
8666
|
+
}
|
|
8667
|
+
|
|
8668
|
+
if (url.pathname === "/api/conversation-mode" && req.method === "GET") {
|
|
8669
|
+
const tab = getRequestedTab(req, url);
|
|
8670
|
+
sendJson(res, 200, { ok: true, data: await naturalConversationFeatureData(tab) });
|
|
8671
|
+
return;
|
|
8672
|
+
}
|
|
8673
|
+
|
|
8674
|
+
if (url.pathname === "/api/conversation-mode" && req.method === "POST") {
|
|
8675
|
+
const body = await readJsonBody(req);
|
|
8676
|
+
const tab = getRequestedTab(req, url, body);
|
|
8677
|
+
sendJson(res, 200, { ok: true, data: await setNaturalConversationMode(tab, body), tab: tabMeta(tab) });
|
|
8678
|
+
return;
|
|
8679
|
+
}
|
|
8680
|
+
|
|
8681
|
+
if ((url.pathname === "/api/stt/transcribe" || url.pathname === "/api/tts/speech") && req.method === "POST") {
|
|
8682
|
+
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." });
|
|
8684
|
+
return;
|
|
8685
|
+
}
|
|
8686
|
+
|
|
8204
8687
|
if (url.pathname === "/api/optional-features" && req.method === "GET") {
|
|
8205
8688
|
sendJson(res, 200, { ok: true, data: await optionalFeaturePackageStatuses() });
|
|
8206
8689
|
return;
|
|
@@ -8209,6 +8692,8 @@ const server = createServer(async (req, res) => {
|
|
|
8209
8692
|
if (url.pathname === "/api/optional-feature-install" && req.method === "POST") {
|
|
8210
8693
|
requireLocalhostRoute(req, url.pathname);
|
|
8211
8694
|
const body = await readJsonBody(req);
|
|
8695
|
+
const tab = getRequestedTab(req, url, body);
|
|
8696
|
+
ensureNaturalConversationRouteAllowed(tab, "optional feature installs are blocked");
|
|
8212
8697
|
const data = await installOptionalFeaturePackage(String(body.featureId || ""));
|
|
8213
8698
|
sendJson(res, 200, { ok: true, data });
|
|
8214
8699
|
return;
|
|
@@ -8223,6 +8708,7 @@ const server = createServer(async (req, res) => {
|
|
|
8223
8708
|
if (url.pathname === "/api/tools" && req.method === "POST") {
|
|
8224
8709
|
const body = await readJsonBody(req);
|
|
8225
8710
|
const tab = getRequestedTab(req, url, body);
|
|
8711
|
+
if (isNaturalConversationActive(tab)) blockNaturalConversationAction("tool configuration changes are blocked");
|
|
8226
8712
|
sendJson(res, 200, { ok: true, data: await setToolConfigData(tab, body) });
|
|
8227
8713
|
return;
|
|
8228
8714
|
}
|
|
@@ -8236,6 +8722,7 @@ const server = createServer(async (req, res) => {
|
|
|
8236
8722
|
if (url.pathname === "/api/skills" && req.method === "POST") {
|
|
8237
8723
|
const body = await readJsonBody(req);
|
|
8238
8724
|
const tab = getRequestedTab(req, url, body);
|
|
8725
|
+
ensureNaturalConversationRouteAllowed(tab, "skill configuration changes are blocked");
|
|
8239
8726
|
sendJson(res, 200, { ok: true, data: await setSkillConfigData(tab, body) });
|
|
8240
8727
|
return;
|
|
8241
8728
|
}
|
|
@@ -8250,6 +8737,7 @@ const server = createServer(async (req, res) => {
|
|
|
8250
8737
|
requireLocalhostRoute(req, url.pathname);
|
|
8251
8738
|
const body = await readJsonBody(req, { limitBytes: SKILL_FILE_BODY_LIMIT_BYTES });
|
|
8252
8739
|
const tab = getRequestedTab(req, url, body);
|
|
8740
|
+
ensureNaturalConversationRouteAllowed(tab, "skill file edits are blocked");
|
|
8253
8741
|
sendJson(res, 200, { ok: true, data: await saveSkillFileData(tab, body) });
|
|
8254
8742
|
return;
|
|
8255
8743
|
}
|
|
@@ -8263,6 +8751,7 @@ const server = createServer(async (req, res) => {
|
|
|
8263
8751
|
if (url.pathname === "/api/settings" && req.method === "POST") {
|
|
8264
8752
|
const body = await readJsonBody(req);
|
|
8265
8753
|
const tab = getRequestedTab(req, url, body);
|
|
8754
|
+
ensureNaturalConversationRouteAllowed(tab, "settings changes are blocked");
|
|
8266
8755
|
sendJson(res, 200, { ok: true, data: await setNativeSettingsData(tab, body) });
|
|
8267
8756
|
return;
|
|
8268
8757
|
}
|
|
@@ -8276,6 +8765,7 @@ const server = createServer(async (req, res) => {
|
|
|
8276
8765
|
if (url.pathname === "/api/action-feedback" && req.method === "POST") {
|
|
8277
8766
|
const body = await readJsonBody(req);
|
|
8278
8767
|
const tab = getRequestedTab(req, url, body);
|
|
8768
|
+
ensureNaturalConversationRouteAllowed(tab, "feedback-learning prompts are blocked");
|
|
8279
8769
|
const response = await handleActionFeedback(tab, body);
|
|
8280
8770
|
sendJson(res, response.success === false ? 400 : 200, response);
|
|
8281
8771
|
return;
|
|
@@ -8296,12 +8786,26 @@ const server = createServer(async (req, res) => {
|
|
|
8296
8786
|
if (url.pathname === "/api/prompt" && req.method === "POST") {
|
|
8297
8787
|
const body = await readJsonBody(req, { limitBytes: requestBodyLimitForPath(url.pathname) });
|
|
8298
8788
|
const tab = getRequestedTab(req, url, body);
|
|
8789
|
+
if (isNaturalConversationActive(tab) && naturalConversationSlashCommandName(body.message) && !isNaturalConversationSlashCommand(body.message)) {
|
|
8790
|
+
blockNaturalConversationAction("slash commands are blocked from the Web UI shell");
|
|
8791
|
+
}
|
|
8299
8792
|
const nativeResponse = await handleNativeSlashCommand(tab, body, req);
|
|
8300
8793
|
if (nativeResponse) {
|
|
8301
8794
|
sendJson(res, nativeResponse.success === false ? 400 : 200, responseWithTab(nativeResponse, tab));
|
|
8302
8795
|
return;
|
|
8303
8796
|
}
|
|
8304
8797
|
const command = commandFromPost(url.pathname, body);
|
|
8798
|
+
enforceNaturalConversationCommandAllowed(tab, command);
|
|
8799
|
+
const queuedForCompaction = maybeQueueCommandDuringCompaction(tab, command);
|
|
8800
|
+
if (queuedForCompaction) {
|
|
8801
|
+
sendJson(res, 202, responseWithTab(queuedForCompaction, tab));
|
|
8802
|
+
return;
|
|
8803
|
+
}
|
|
8804
|
+
const naturalConversationSafetyResponse = await ensureNaturalConversationPromptSafety(tab, command);
|
|
8805
|
+
if (naturalConversationSafetyResponse?.success === false) {
|
|
8806
|
+
sendJson(res, 400, responseWithTab(naturalConversationSafetyResponse, tab));
|
|
8807
|
+
return;
|
|
8808
|
+
}
|
|
8305
8809
|
const pendingThinkingResponse = await applyPendingThinkingBeforePrompt(tab);
|
|
8306
8810
|
if (pendingThinkingResponse?.success === false) {
|
|
8307
8811
|
sendJson(res, 400, responseWithTab(pendingThinkingResponse, tab));
|
|
@@ -8341,6 +8845,7 @@ const server = createServer(async (req, res) => {
|
|
|
8341
8845
|
if (url.pathname === "/api/git-changes/pull" && req.method === "POST") {
|
|
8342
8846
|
const body = await readJsonBody(req);
|
|
8343
8847
|
const tab = getRequestedTab(req, url, body);
|
|
8848
|
+
ensureNaturalConversationRouteAllowed(tab, "git pull is blocked");
|
|
8344
8849
|
try {
|
|
8345
8850
|
sendJson(res, 200, await pullGitChanges(tab.cwd));
|
|
8346
8851
|
} catch (error) {
|
|
@@ -8362,6 +8867,7 @@ const server = createServer(async (req, res) => {
|
|
|
8362
8867
|
if (url.pathname === "/api/git-worktrees" && req.method === "POST") {
|
|
8363
8868
|
const body = await readJsonBody(req);
|
|
8364
8869
|
const tab = getRequestedTab(req, url, body);
|
|
8870
|
+
ensureNaturalConversationRouteAllowed(tab, "git worktree changes are blocked");
|
|
8365
8871
|
try {
|
|
8366
8872
|
sendJson(res, 200, { ok: true, data: await createGitWorktreeTab(tab, body) });
|
|
8367
8873
|
} catch (error) {
|
|
@@ -8373,6 +8879,7 @@ const server = createServer(async (req, res) => {
|
|
|
8373
8879
|
if (url.pathname === "/api/git-worktrees/open" && req.method === "POST") {
|
|
8374
8880
|
const body = await readJsonBody(req);
|
|
8375
8881
|
const tab = getRequestedTab(req, url, body);
|
|
8882
|
+
ensureNaturalConversationRouteAllowed(tab, "git worktree changes are blocked");
|
|
8376
8883
|
try {
|
|
8377
8884
|
sendJson(res, 200, { ok: true, data: await openExistingGitWorktreeTab(tab, body) });
|
|
8378
8885
|
} catch (error) {
|
|
@@ -8385,6 +8892,7 @@ const server = createServer(async (req, res) => {
|
|
|
8385
8892
|
requireLocalhost(req, "Removing Git worktrees is only allowed from localhost");
|
|
8386
8893
|
const body = await readJsonBody(req);
|
|
8387
8894
|
const tab = getRequestedTab(req, url, body);
|
|
8895
|
+
ensureNaturalConversationRouteAllowed(tab, "git worktree removal is blocked");
|
|
8388
8896
|
try {
|
|
8389
8897
|
sendJson(res, 200, { ok: true, data: await removeGitWorktreeForTab(tab, body) });
|
|
8390
8898
|
} catch (error) {
|
|
@@ -8406,6 +8914,7 @@ const server = createServer(async (req, res) => {
|
|
|
8406
8914
|
if (url.pathname === "/api/git-branch" && req.method === "POST") {
|
|
8407
8915
|
const body = await readJsonBody(req);
|
|
8408
8916
|
const tab = getRequestedTab(req, url, body);
|
|
8917
|
+
ensureNaturalConversationRouteAllowed(tab, "git branch changes are blocked");
|
|
8409
8918
|
try {
|
|
8410
8919
|
sendJson(res, 200, await switchGitBranch(tab.cwd, body.branch, { create: body.create === true }));
|
|
8411
8920
|
} catch (error) {
|
|
@@ -8417,6 +8926,7 @@ const server = createServer(async (req, res) => {
|
|
|
8417
8926
|
if (url.pathname.startsWith("/api/git-workflow/")) {
|
|
8418
8927
|
const body = req.method === "POST" ? await readJsonBody(req) : {};
|
|
8419
8928
|
const tab = getRequestedTab(req, url, body);
|
|
8929
|
+
if (req.method !== "GET") ensureNaturalConversationRouteAllowed(tab, "git workflow actions are blocked");
|
|
8420
8930
|
const response = await handleGitWorkflowRequest(url.pathname, body, tab);
|
|
8421
8931
|
if (response) {
|
|
8422
8932
|
sendJson(res, 200, response);
|
|
@@ -8460,7 +8970,18 @@ const server = createServer(async (req, res) => {
|
|
|
8460
8970
|
const command = commandFromPost(url.pathname, body);
|
|
8461
8971
|
if (command) {
|
|
8462
8972
|
const tab = getRequestedTab(req, url, body);
|
|
8973
|
+
enforceNaturalConversationCommandAllowed(tab, command);
|
|
8463
8974
|
if (command.type === "abort") await cancelPendingExtensionUiRequests(tab);
|
|
8975
|
+
const queuedForCompaction = maybeQueueCommandDuringCompaction(tab, command);
|
|
8976
|
+
if (queuedForCompaction) {
|
|
8977
|
+
sendJson(res, 202, responseWithTab(queuedForCompaction, tab));
|
|
8978
|
+
return;
|
|
8979
|
+
}
|
|
8980
|
+
const naturalConversationSafetyResponse = await ensureNaturalConversationPromptSafety(tab, command);
|
|
8981
|
+
if (naturalConversationSafetyResponse?.success === false) {
|
|
8982
|
+
sendJson(res, 400, responseWithTab(naturalConversationSafetyResponse, tab));
|
|
8983
|
+
return;
|
|
8984
|
+
}
|
|
8464
8985
|
const startsVisibleWork = commandStartsVisibleWork(command);
|
|
8465
8986
|
if (startsVisibleWork) {
|
|
8466
8987
|
maybeNameTabForConversation(tab, command);
|
|
@@ -8483,6 +9004,7 @@ const server = createServer(async (req, res) => {
|
|
|
8483
9004
|
clearPendingExtensionUiRequests(tab);
|
|
8484
9005
|
clearExtensionStatuses(tab);
|
|
8485
9006
|
clearExtensionWidgets(tab);
|
|
9007
|
+
resetNaturalConversationMode(tab);
|
|
8486
9008
|
}
|
|
8487
9009
|
sendJson(res, response.success === false ? 400 : 200, responseWithTab(response, tab));
|
|
8488
9010
|
return;
|