@h-rig/cli 0.0.6-alpha.22 → 0.0.6-alpha.24
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/dist/bin/rig.js +951 -494
- package/dist/src/commands/_operator-view.js +644 -228
- package/dist/src/commands/_pi-frontend.js +843 -0
- package/dist/src/commands/_pi-worker-bridge-extension.js +761 -0
- package/dist/src/commands/_preflight.js +1 -81
- package/dist/src/commands/_server-client.js +80 -1
- package/dist/src/commands/run.js +644 -228
- package/dist/src/commands/task-run-driver.js +51 -5
- package/dist/src/commands/task.js +648 -312
- package/dist/src/commands.js +951 -494
- package/dist/src/index.js +951 -494
- package/package.json +6 -6
|
@@ -1,10 +1,8 @@
|
|
|
1
1
|
// @bun
|
|
2
|
-
var __require = import.meta.require;
|
|
3
|
-
|
|
4
2
|
// packages/cli/src/commands/task.ts
|
|
5
|
-
import { readFileSync as
|
|
3
|
+
import { readFileSync as readFileSync3 } from "fs";
|
|
6
4
|
import { spawnSync } from "child_process";
|
|
7
|
-
import { resolve as
|
|
5
|
+
import { resolve as resolve3 } from "path";
|
|
8
6
|
import { cancel as cancel2, confirm, isCancel as isCancel2 } from "@clack/prompts";
|
|
9
7
|
|
|
10
8
|
// packages/cli/src/runner.ts
|
|
@@ -361,6 +359,66 @@ async function steerRunViaServer(context, runId, message) {
|
|
|
361
359
|
});
|
|
362
360
|
return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : { ok: true };
|
|
363
361
|
}
|
|
362
|
+
async function getRunPiSessionViaServer(context, runId) {
|
|
363
|
+
const payload = await requestServerJson(context, `/api/runs/${encodeURIComponent(runId)}/pi`);
|
|
364
|
+
return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : {};
|
|
365
|
+
}
|
|
366
|
+
async function getRunPiMessagesViaServer(context, runId) {
|
|
367
|
+
const payload = await requestServerJson(context, `/api/runs/${encodeURIComponent(runId)}/pi/messages`);
|
|
368
|
+
return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : { messages: [] };
|
|
369
|
+
}
|
|
370
|
+
async function getRunPiStatusViaServer(context, runId) {
|
|
371
|
+
const payload = await requestServerJson(context, `/api/runs/${encodeURIComponent(runId)}/pi/status`);
|
|
372
|
+
return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : {};
|
|
373
|
+
}
|
|
374
|
+
async function getRunPiCommandsViaServer(context, runId) {
|
|
375
|
+
const payload = await requestServerJson(context, `/api/runs/${encodeURIComponent(runId)}/pi/commands`);
|
|
376
|
+
return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : { commands: [] };
|
|
377
|
+
}
|
|
378
|
+
async function sendRunPiPromptViaServer(context, runId, text, streamingBehavior) {
|
|
379
|
+
const payload = await requestServerJson(context, `/api/runs/${encodeURIComponent(runId)}/pi/prompt`, {
|
|
380
|
+
method: "POST",
|
|
381
|
+
headers: { "content-type": "application/json" },
|
|
382
|
+
body: JSON.stringify({ text, streamingBehavior })
|
|
383
|
+
});
|
|
384
|
+
return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : { accepted: true };
|
|
385
|
+
}
|
|
386
|
+
async function sendRunPiShellViaServer(context, runId, text) {
|
|
387
|
+
const payload = await requestServerJson(context, `/api/runs/${encodeURIComponent(runId)}/pi/shell`, {
|
|
388
|
+
method: "POST",
|
|
389
|
+
headers: { "content-type": "application/json" },
|
|
390
|
+
body: JSON.stringify({ text })
|
|
391
|
+
});
|
|
392
|
+
return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : { accepted: true };
|
|
393
|
+
}
|
|
394
|
+
async function runRunPiCommandViaServer(context, runId, text) {
|
|
395
|
+
const payload = await requestServerJson(context, `/api/runs/${encodeURIComponent(runId)}/pi/commands/run`, {
|
|
396
|
+
method: "POST",
|
|
397
|
+
headers: { "content-type": "application/json" },
|
|
398
|
+
body: JSON.stringify({ text })
|
|
399
|
+
});
|
|
400
|
+
return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : { type: "done" };
|
|
401
|
+
}
|
|
402
|
+
async function respondRunPiExtensionUiViaServer(context, runId, requestId, valueOrCancel) {
|
|
403
|
+
const payload = await requestServerJson(context, `/api/runs/${encodeURIComponent(runId)}/pi/extension-ui/respond`, {
|
|
404
|
+
method: "POST",
|
|
405
|
+
headers: { "content-type": "application/json" },
|
|
406
|
+
body: JSON.stringify({ requestId, ...valueOrCancel })
|
|
407
|
+
});
|
|
408
|
+
return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : { accepted: true };
|
|
409
|
+
}
|
|
410
|
+
async function abortRunPiViaServer(context, runId) {
|
|
411
|
+
const payload = await requestServerJson(context, `/api/runs/${encodeURIComponent(runId)}/pi/abort`, { method: "POST" });
|
|
412
|
+
return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : { aborted: true };
|
|
413
|
+
}
|
|
414
|
+
async function buildRunPiEventsWebSocketUrl(context, runId) {
|
|
415
|
+
const server = await ensureServerForCli(context.projectRoot);
|
|
416
|
+
const url = new URL(`${server.baseUrl.replace(/\/+$/, "")}/api/runs/${encodeURIComponent(runId)}/pi/events`);
|
|
417
|
+
url.protocol = url.protocol === "https:" ? "wss:" : "ws:";
|
|
418
|
+
if (server.authToken)
|
|
419
|
+
url.searchParams.set("token", server.authToken);
|
|
420
|
+
return url.toString();
|
|
421
|
+
}
|
|
364
422
|
async function submitTaskRunViaServer(context, input) {
|
|
365
423
|
const isTaskRun = Boolean(input.taskId);
|
|
366
424
|
const endpoint = isTaskRun ? "/api/runs/task" : "/api/runs/adhoc";
|
|
@@ -393,79 +451,6 @@ async function submitTaskRunViaServer(context, input) {
|
|
|
393
451
|
return { runId };
|
|
394
452
|
}
|
|
395
453
|
|
|
396
|
-
// packages/cli/src/commands/_pi-install.ts
|
|
397
|
-
import { existsSync as existsSync3, readFileSync as readFileSync3, rmSync } from "fs";
|
|
398
|
-
import { homedir as homedir2 } from "os";
|
|
399
|
-
import { resolve as resolve3 } from "path";
|
|
400
|
-
var PI_RIG_PACKAGE_NAME = "@h-rig/pi-rig";
|
|
401
|
-
var LEGACY_PI_RIG_PACKAGE_NAME = "@rig/pi-rig";
|
|
402
|
-
async function defaultCommandRunner(command, options = {}) {
|
|
403
|
-
const proc = Bun.spawn(command, { cwd: options.cwd, stdout: "pipe", stderr: "pipe" });
|
|
404
|
-
const [stdout, stderr, exitCode] = await Promise.all([
|
|
405
|
-
new Response(proc.stdout).text(),
|
|
406
|
-
new Response(proc.stderr).text(),
|
|
407
|
-
proc.exited
|
|
408
|
-
]);
|
|
409
|
-
return { exitCode, stdout, stderr };
|
|
410
|
-
}
|
|
411
|
-
function resolvePiRigExtensionPath(homeDir) {
|
|
412
|
-
return resolve3(homeDir, ".pi", "agent", "extensions", "pi-rig");
|
|
413
|
-
}
|
|
414
|
-
function resolvePiHomeDir(inputHomeDir) {
|
|
415
|
-
return inputHomeDir ?? process.env.RIG_PI_HOME_DIR?.trim() ?? homedir2();
|
|
416
|
-
}
|
|
417
|
-
function piListContainsPiRig(output) {
|
|
418
|
-
return output.split(/\r?\n/).some((line) => {
|
|
419
|
-
const normalized = line.trim();
|
|
420
|
-
return normalized.includes(PI_RIG_PACKAGE_NAME) || normalized.includes(LEGACY_PI_RIG_PACKAGE_NAME) || /(?:^|[\\/])packages[\\/]pi-rig(?:$|\s)/.test(normalized);
|
|
421
|
-
});
|
|
422
|
-
}
|
|
423
|
-
async function safeRun(runner, command, options) {
|
|
424
|
-
try {
|
|
425
|
-
return await runner(command, options);
|
|
426
|
-
} catch (error) {
|
|
427
|
-
return { exitCode: 1, stdout: "", stderr: error instanceof Error ? error.message : String(error) };
|
|
428
|
-
}
|
|
429
|
-
}
|
|
430
|
-
async function checkPiRigInstall(input = {}) {
|
|
431
|
-
const home = resolvePiHomeDir(input.homeDir);
|
|
432
|
-
const extensionPath = resolvePiRigExtensionPath(home);
|
|
433
|
-
if (process.env.RIG_TEST_FAKE_PI_INSTALL === "1") {
|
|
434
|
-
return {
|
|
435
|
-
extensionPath,
|
|
436
|
-
pi: { ok: true, label: "pi", detail: "fake-pi" },
|
|
437
|
-
piRig: { ok: true, label: "pi-rig global extension", detail: extensionPath }
|
|
438
|
-
};
|
|
439
|
-
}
|
|
440
|
-
const exists = input.exists ?? existsSync3;
|
|
441
|
-
const runner = input.commandRunner ?? defaultCommandRunner;
|
|
442
|
-
const piResult = await safeRun(runner, ["pi", "--version"]);
|
|
443
|
-
const piListResult = piResult.exitCode === 0 ? await safeRun(runner, ["pi", "list"]) : { exitCode: 1, stdout: "", stderr: "" };
|
|
444
|
-
const listedPiRig = piListResult.exitCode === 0 && piListContainsPiRig(`${piListResult.stdout}
|
|
445
|
-
${piListResult.stderr}`);
|
|
446
|
-
const legacyBridge = exists(resolve3(extensionPath, "index.ts"));
|
|
447
|
-
const hasPiRig = listedPiRig;
|
|
448
|
-
return {
|
|
449
|
-
extensionPath,
|
|
450
|
-
pi: {
|
|
451
|
-
ok: piResult.exitCode === 0,
|
|
452
|
-
label: "pi",
|
|
453
|
-
detail: (piResult.stdout || piResult.stderr).trim() || undefined,
|
|
454
|
-
hint: piResult.exitCode === 0 ? undefined : "Install Pi or run `rig init --yes` to install/update the Pi runtime."
|
|
455
|
-
},
|
|
456
|
-
piRig: {
|
|
457
|
-
ok: hasPiRig,
|
|
458
|
-
label: "pi-rig global extension",
|
|
459
|
-
detail: hasPiRig ? piListResult.stdout.trim() || PI_RIG_PACKAGE_NAME : legacyBridge ? `${extensionPath} (legacy bridge; reinstall required)` : undefined,
|
|
460
|
-
hint: hasPiRig ? undefined : "Run `rig init --yes` to install/enable the global pi-rig package with `pi install`."
|
|
461
|
-
}
|
|
462
|
-
};
|
|
463
|
-
}
|
|
464
|
-
async function buildPiSetupChecks(input = {}) {
|
|
465
|
-
const status = await checkPiRigInstall(input);
|
|
466
|
-
return [status.pi, status.piRig];
|
|
467
|
-
}
|
|
468
|
-
|
|
469
454
|
// packages/cli/src/commands/_preflight.ts
|
|
470
455
|
function preflightCheck(id, label, status, detail, remediation) {
|
|
471
456
|
return {
|
|
@@ -623,14 +608,7 @@ async function runFastTaskRunPreflight(context, options = {}) {
|
|
|
623
608
|
}
|
|
624
609
|
}
|
|
625
610
|
if ((options.runtimeAdapter ?? "pi") === "pi") {
|
|
626
|
-
|
|
627
|
-
ok: false,
|
|
628
|
-
label: "pi/pi-rig checks",
|
|
629
|
-
hint: message(error)
|
|
630
|
-
}]);
|
|
631
|
-
for (const pi of piChecks) {
|
|
632
|
-
checks.push(preflightCheck(pi.label === "pi" ? "pi" : "pi-rig", pi.label, pi.ok ? "pass" : "fail", pi.detail, pi.hint ?? (pi.ok ? undefined : "Run `rig init --yes` to install/update Pi and enable pi-rig.")));
|
|
633
|
-
}
|
|
611
|
+
checks.push(preflightCheck("runtime", "worker Pi SDK session daemon", "pass", selectedServer?.connectionKind === "remote" ? "remote worker-owned runtime" : "bundled server-owned runtime"));
|
|
634
612
|
} else {
|
|
635
613
|
checks.push(preflightCheck("runtime", "runtime adapter", "pass", options.runtimeAdapter));
|
|
636
614
|
}
|
|
@@ -967,52 +945,593 @@ async function selectTaskWithTextPicker(tasks, io = {}) {
|
|
|
967
945
|
return Number.isFinite(index) ? tasks[index] ?? null : null;
|
|
968
946
|
}
|
|
969
947
|
|
|
970
|
-
// packages/cli/src/commands/
|
|
948
|
+
// packages/cli/src/commands/_pi-frontend.ts
|
|
949
|
+
import { mkdtempSync, rmSync } from "fs";
|
|
950
|
+
import { tmpdir } from "os";
|
|
951
|
+
import { join } from "path";
|
|
952
|
+
import { main as runPiMain } from "@earendil-works/pi-coding-agent";
|
|
953
|
+
|
|
954
|
+
// packages/cli/src/commands/_pi-worker-bridge-extension.ts
|
|
971
955
|
var TERMINAL_RUN_STATUSES = new Set(["completed", "failed", "stopped", "cancelled", "canceled", "closed", "merged", "needs_attention", "needs-attention"]);
|
|
972
|
-
var
|
|
973
|
-
|
|
974
|
-
"
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
"
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
"
|
|
982
|
-
|
|
983
|
-
"Merge",
|
|
984
|
-
"Complete"
|
|
985
|
-
];
|
|
986
|
-
var GREEN = "\x1B[32m";
|
|
987
|
-
var BLUE = "\x1B[34m";
|
|
988
|
-
var MAGENTA = "\x1B[35m";
|
|
989
|
-
var YELLOW = "\x1B[33m";
|
|
990
|
-
var RED = "\x1B[31m";
|
|
991
|
-
var DIM = "\x1B[2m";
|
|
992
|
-
var BOLD = "\x1B[1m";
|
|
993
|
-
var RESET = "\x1B[0m";
|
|
994
|
-
async function loadPiTuiRuntime() {
|
|
956
|
+
var MAX_TRANSCRIPT_LINES = 120;
|
|
957
|
+
function recordOf(value) {
|
|
958
|
+
return value && typeof value === "object" && !Array.isArray(value) ? value : null;
|
|
959
|
+
}
|
|
960
|
+
function asText(value) {
|
|
961
|
+
if (typeof value === "string")
|
|
962
|
+
return value;
|
|
963
|
+
if (value === null || value === undefined)
|
|
964
|
+
return "";
|
|
965
|
+
if (typeof value === "number" || typeof value === "boolean")
|
|
966
|
+
return String(value);
|
|
995
967
|
try {
|
|
996
|
-
return
|
|
968
|
+
return JSON.stringify(value);
|
|
997
969
|
} catch {
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
970
|
+
return String(value);
|
|
971
|
+
}
|
|
972
|
+
}
|
|
973
|
+
function textFromContent(content) {
|
|
974
|
+
if (typeof content === "string")
|
|
975
|
+
return content;
|
|
976
|
+
if (!Array.isArray(content))
|
|
977
|
+
return asText(content);
|
|
978
|
+
return content.flatMap((part) => {
|
|
979
|
+
const item = recordOf(part);
|
|
980
|
+
if (!item)
|
|
981
|
+
return [];
|
|
982
|
+
if (typeof item.text === "string")
|
|
983
|
+
return [item.text];
|
|
984
|
+
if (typeof item.content === "string")
|
|
985
|
+
return [item.content];
|
|
986
|
+
if (item.type === "toolCall")
|
|
987
|
+
return [`\u23FA ${String(item.name ?? "tool")} ${asText(item.arguments ?? "")}`.trim()];
|
|
988
|
+
if (item.type === "toolResult")
|
|
989
|
+
return [`\u21B3 ${asText(item.content ?? item.result ?? "")}`.trim()];
|
|
990
|
+
return [];
|
|
991
|
+
}).join(`
|
|
992
|
+
`);
|
|
993
|
+
}
|
|
994
|
+
function appendTranscript(state, label, text) {
|
|
995
|
+
const trimmed = text.trimEnd();
|
|
996
|
+
if (!trimmed)
|
|
997
|
+
return;
|
|
998
|
+
const lines = trimmed.split(/\r?\n/);
|
|
999
|
+
state.transcript.push(`${label}: ${lines[0] ?? ""}`);
|
|
1000
|
+
for (const line of lines.slice(1))
|
|
1001
|
+
state.transcript.push(` ${line}`);
|
|
1002
|
+
if (state.transcript.length > MAX_TRANSCRIPT_LINES) {
|
|
1003
|
+
state.transcript.splice(0, state.transcript.length - MAX_TRANSCRIPT_LINES);
|
|
1004
|
+
}
|
|
1005
|
+
}
|
|
1006
|
+
function nativePiUi(ctx) {
|
|
1007
|
+
const ui = ctx.ui;
|
|
1008
|
+
return typeof ui.emitSessionEvent === "function" && typeof ui.appendSessionMessages === "function" ? ui : null;
|
|
1009
|
+
}
|
|
1010
|
+
function syncNativeDisplayCwd(ctx, state) {
|
|
1011
|
+
const ui = nativePiUi(ctx);
|
|
1012
|
+
if (ui?.setDisplayCwd && state.cwd)
|
|
1013
|
+
ui.setDisplayCwd(state.cwd);
|
|
1014
|
+
}
|
|
1015
|
+
function parseExtensionUiRequest(value) {
|
|
1016
|
+
const request = recordOf(value) ?? {};
|
|
1017
|
+
const requestId = String(request.requestId ?? request.id ?? `ui-${Date.now()}`);
|
|
1018
|
+
const method = String(request.method ?? request.type ?? "input");
|
|
1019
|
+
const prompt = asText(request.prompt ?? request.message ?? request.title ?? method);
|
|
1020
|
+
const rawOptions = Array.isArray(request.options) ? request.options : Array.isArray(request.items) ? request.items : [];
|
|
1021
|
+
const options = rawOptions.map((option) => {
|
|
1022
|
+
const record = recordOf(option);
|
|
1023
|
+
return record ? asText(record.label ?? record.value ?? record.name ?? option) : asText(option);
|
|
1024
|
+
}).filter(Boolean);
|
|
1025
|
+
return { requestId, method, prompt, options };
|
|
1026
|
+
}
|
|
1027
|
+
function renderBridgeWidget(state) {
|
|
1028
|
+
const statusParts = [
|
|
1029
|
+
state.wsConnected ? "live WS" : "WS pending",
|
|
1030
|
+
state.status,
|
|
1031
|
+
state.model,
|
|
1032
|
+
state.cwd
|
|
1033
|
+
].filter(Boolean);
|
|
1034
|
+
const lines = [`Worker Pi daemon bridge \xB7 ${statusParts.join(" \xB7 ")}`];
|
|
1035
|
+
if (state.activity)
|
|
1036
|
+
lines.push(state.activity);
|
|
1037
|
+
if (state.commands.length > 0) {
|
|
1038
|
+
lines.push(`Worker commands: ${state.commands.slice(0, 10).join(", ")}${state.commands.length > 10 ? ", \u2026" : ""}`);
|
|
1039
|
+
}
|
|
1040
|
+
lines.push("");
|
|
1041
|
+
if (state.transcript.length > 0) {
|
|
1042
|
+
lines.push(...state.transcript.slice(-MAX_TRANSCRIPT_LINES));
|
|
1043
|
+
} else {
|
|
1044
|
+
lines.push("Waiting for worker Pi daemon transcript\u2026");
|
|
1045
|
+
}
|
|
1046
|
+
if (state.pendingUi) {
|
|
1047
|
+
lines.push("");
|
|
1048
|
+
lines.push(`Extension UI request \xB7 ${state.pendingUi.method}`);
|
|
1049
|
+
lines.push(state.pendingUi.prompt);
|
|
1050
|
+
state.pendingUi.options.forEach((option, index) => lines.push(`${index + 1}. ${option}`));
|
|
1051
|
+
lines.push("Reply in the Pi editor. /cancel cancels this request.");
|
|
1052
|
+
}
|
|
1053
|
+
return lines;
|
|
1054
|
+
}
|
|
1055
|
+
function updatePiUi(ctx, state) {
|
|
1056
|
+
ctx.ui.setTitle("Pi \xB7 Rig worker daemon");
|
|
1057
|
+
ctx.ui.setStatus("rig-worker-pi", state.wsConnected ? "worker Pi WS live" : state.status);
|
|
1058
|
+
syncNativeDisplayCwd(ctx, state);
|
|
1059
|
+
if (state.nativeStream && nativePiUi(ctx)) {
|
|
1060
|
+
ctx.ui.setWidget("rig-worker-pi-transcript", undefined);
|
|
1061
|
+
return;
|
|
1062
|
+
}
|
|
1063
|
+
ctx.ui.setWorkingVisible(false);
|
|
1064
|
+
ctx.ui.setWidget("rig-worker-pi-transcript", [`Worker Pi daemon bridge \xB7 degraded widget transcript`, ...renderBridgeWidget(state)], { placement: "aboveEditor" });
|
|
1065
|
+
}
|
|
1066
|
+
function applyStatus(state, payload) {
|
|
1067
|
+
const status = recordOf(payload.status) ?? payload;
|
|
1068
|
+
state.streaming = status.isStreaming === true || status.isCompacting === true || status.isBashRunning === true;
|
|
1069
|
+
state.cwd = typeof status.cwd === "string" ? status.cwd : state.cwd;
|
|
1070
|
+
state.model = typeof status.model === "string" ? status.model : state.model;
|
|
1071
|
+
const pending = typeof status.pendingMessageCount === "number" ? status.pendingMessageCount : 0;
|
|
1072
|
+
state.status = `${state.streaming ? "streaming" : "idle"}${pending ? ` \xB7 ${pending} queued` : ""}`;
|
|
1073
|
+
}
|
|
1074
|
+
function applyMessage(state, message2) {
|
|
1075
|
+
const record = recordOf(message2);
|
|
1076
|
+
if (!record)
|
|
1077
|
+
return;
|
|
1078
|
+
const role = String(record.role ?? "system");
|
|
1079
|
+
const label = role === "assistant" ? "Pi" : role === "user" ? "You" : role === "tool" || role === "toolResult" ? "Tool" : "System";
|
|
1080
|
+
appendTranscript(state, label, textFromContent(record.content ?? record.message ?? record.text ?? ""));
|
|
1081
|
+
}
|
|
1082
|
+
function applyPiEvent(ctx, state, eventValue) {
|
|
1083
|
+
const event = recordOf(eventValue);
|
|
1084
|
+
if (!event)
|
|
1085
|
+
return;
|
|
1086
|
+
const type = String(event.type ?? "event");
|
|
1087
|
+
if (type === "agent_start") {
|
|
1088
|
+
state.streaming = true;
|
|
1089
|
+
state.status = "streaming";
|
|
1090
|
+
} else if (type === "agent_end") {
|
|
1091
|
+
state.streaming = false;
|
|
1092
|
+
state.status = "idle";
|
|
1093
|
+
} else if (type === "queue_update") {
|
|
1094
|
+
const steering = Array.isArray(event.steering) ? event.steering.length : 0;
|
|
1095
|
+
const followUp = Array.isArray(event.followUp) ? event.followUp.length : 0;
|
|
1096
|
+
state.status = `queued \xB7 steer ${steering} \xB7 follow-up ${followUp}`;
|
|
1097
|
+
}
|
|
1098
|
+
const native = nativePiUi(ctx);
|
|
1099
|
+
if (state.nativeStream && native?.emitSessionEvent) {
|
|
1100
|
+
native.emitSessionEvent(eventValue);
|
|
1101
|
+
return;
|
|
1102
|
+
}
|
|
1103
|
+
if (type === "agent_end") {
|
|
1104
|
+
appendTranscript(state, "System", "Agent turn complete.");
|
|
1105
|
+
return;
|
|
1106
|
+
}
|
|
1107
|
+
if (type === "message_start" || type === "message_end" || type === "turn_end") {
|
|
1108
|
+
applyMessage(state, event.message);
|
|
1109
|
+
return;
|
|
1110
|
+
}
|
|
1111
|
+
if (type === "message_update") {
|
|
1112
|
+
const assistantEvent = recordOf(event.assistantMessageEvent);
|
|
1113
|
+
const delta = typeof assistantEvent?.delta === "string" ? assistantEvent.delta : typeof assistantEvent?.text === "string" ? assistantEvent.text : "";
|
|
1114
|
+
if (delta)
|
|
1115
|
+
appendTranscript(state, assistantEvent?.type === "thinking_delta" ? "Thinking" : "Pi", delta);
|
|
1116
|
+
return;
|
|
1117
|
+
}
|
|
1118
|
+
if (type === "tool_execution_start") {
|
|
1119
|
+
appendTranscript(state, "Tool", `${String(event.toolName ?? "tool")} ${asText(event.args ?? "")}`.trim());
|
|
1120
|
+
return;
|
|
1121
|
+
}
|
|
1122
|
+
if (type === "tool_execution_update") {
|
|
1123
|
+
appendTranscript(state, "Tool", asText(event.partialResult ?? ""));
|
|
1124
|
+
return;
|
|
1125
|
+
}
|
|
1126
|
+
if (type === "tool_execution_end") {
|
|
1127
|
+
appendTranscript(state, event.isError === true ? "Error" : "Tool", asText(event.result ?? `${String(event.toolName ?? "tool")} complete`));
|
|
1128
|
+
}
|
|
1129
|
+
}
|
|
1130
|
+
function firstPendingShell(state) {
|
|
1131
|
+
return state.pendingShells[0];
|
|
1132
|
+
}
|
|
1133
|
+
function finishPendingShell(state, shell, result) {
|
|
1134
|
+
const index = state.pendingShells.indexOf(shell);
|
|
1135
|
+
if (index !== -1)
|
|
1136
|
+
state.pendingShells.splice(index, 1);
|
|
1137
|
+
shell.resolve(result);
|
|
1138
|
+
}
|
|
1139
|
+
function failPendingShell(state, shell, error) {
|
|
1140
|
+
const index = state.pendingShells.indexOf(shell);
|
|
1141
|
+
if (index !== -1)
|
|
1142
|
+
state.pendingShells.splice(index, 1);
|
|
1143
|
+
shell.reject(error);
|
|
1144
|
+
}
|
|
1145
|
+
function applyUiEvent(state, value) {
|
|
1146
|
+
const event = recordOf(value);
|
|
1147
|
+
if (!event)
|
|
1148
|
+
return;
|
|
1149
|
+
const type = String(event.type ?? "ui");
|
|
1150
|
+
if (type === "shell.chunk") {
|
|
1151
|
+
const pending = firstPendingShell(state);
|
|
1152
|
+
const chunk = asText(event.chunk);
|
|
1153
|
+
if (pending) {
|
|
1154
|
+
pending.sawChunk = true;
|
|
1155
|
+
pending.onData(Buffer.from(chunk));
|
|
1156
|
+
} else {
|
|
1157
|
+
appendTranscript(state, "Tool", chunk);
|
|
1158
|
+
}
|
|
1159
|
+
return;
|
|
1160
|
+
}
|
|
1161
|
+
if (type === "shell.end") {
|
|
1162
|
+
const pending = firstPendingShell(state);
|
|
1163
|
+
const output = asText(event.output ?? "");
|
|
1164
|
+
const exitCode = typeof event.exitCode === "number" ? event.exitCode : event.isError === true ? 1 : 0;
|
|
1165
|
+
if (pending) {
|
|
1166
|
+
if (output && !pending.sawChunk)
|
|
1167
|
+
pending.onData(Buffer.from(output));
|
|
1168
|
+
finishPendingShell(state, pending, { exitCode });
|
|
1169
|
+
} else {
|
|
1170
|
+
appendTranscript(state, event.isError === true ? "Error" : "Tool", output || `exit ${String(exitCode)}`);
|
|
1171
|
+
}
|
|
1172
|
+
return;
|
|
1173
|
+
}
|
|
1174
|
+
if (type === "shell.start") {
|
|
1175
|
+
if (!firstPendingShell(state))
|
|
1176
|
+
appendTranscript(state, "Tool", `$ ${asText(event.command)}`);
|
|
1177
|
+
return;
|
|
1178
|
+
}
|
|
1179
|
+
appendTranscript(state, "System", `${type}: ${asText(event)}`);
|
|
1180
|
+
}
|
|
1181
|
+
function applyEnvelope(ctx, state, envelopeValue) {
|
|
1182
|
+
const envelope = recordOf(envelopeValue);
|
|
1183
|
+
if (!envelope)
|
|
1184
|
+
return;
|
|
1185
|
+
const type = String(envelope.type ?? "");
|
|
1186
|
+
if (type === "ready") {
|
|
1187
|
+
const metadata = recordOf(envelope.metadata);
|
|
1188
|
+
state.cwd = typeof metadata?.cwd === "string" ? metadata.cwd : state.cwd;
|
|
1189
|
+
state.status = "worker Pi daemon ready";
|
|
1190
|
+
if (!state.nativeStream)
|
|
1191
|
+
appendTranscript(state, "System", "Connected to worker Pi daemon.");
|
|
1192
|
+
} else if (type === "status.update") {
|
|
1193
|
+
applyStatus(state, envelope);
|
|
1194
|
+
} else if (type === "activity.update") {
|
|
1195
|
+
const activity = recordOf(envelope.activity);
|
|
1196
|
+
state.activity = [activity?.label, activity?.detail].map(asText).filter(Boolean).join(" \u2014 ");
|
|
1197
|
+
} else if (type === "extension_ui_request") {
|
|
1198
|
+
state.pendingUi = parseExtensionUiRequest(envelope.request);
|
|
1199
|
+
appendTranscript(state, "System", `Extension UI request: ${state.pendingUi.prompt}`);
|
|
1200
|
+
} else if (type === "pi.ui_event") {
|
|
1201
|
+
applyUiEvent(state, envelope.event);
|
|
1202
|
+
} else if (type === "pi.event") {
|
|
1203
|
+
applyPiEvent(ctx, state, envelope.event);
|
|
1204
|
+
} else if (type === "error") {
|
|
1205
|
+
appendTranscript(state, "Error", asText(envelope.message ?? envelope.detail ?? "unknown error"));
|
|
1206
|
+
}
|
|
1207
|
+
syncNativeDisplayCwd(ctx, state);
|
|
1208
|
+
}
|
|
1209
|
+
async function waitForWorkerReady(options, ctx, state) {
|
|
1210
|
+
while (true) {
|
|
1211
|
+
const session = await getRunPiSessionViaServer(options.context, options.runId).catch((error) => ({
|
|
1212
|
+
ready: false,
|
|
1213
|
+
status: error instanceof Error ? error.message : String(error),
|
|
1214
|
+
retryAfterMs: 1000
|
|
1215
|
+
}));
|
|
1216
|
+
if (session.ready === false) {
|
|
1217
|
+
const status = String(session.status ?? "starting");
|
|
1218
|
+
state.status = `waiting for worker Pi daemon \xB7 ${status}`;
|
|
1219
|
+
updatePiUi(ctx, state);
|
|
1220
|
+
if (TERMINAL_RUN_STATUSES.has(status.toLowerCase())) {
|
|
1221
|
+
appendTranscript(state, "Error", `Run ended before worker Pi daemon became ready: ${status}`);
|
|
1222
|
+
return false;
|
|
1223
|
+
}
|
|
1224
|
+
await Bun.sleep(typeof session.retryAfterMs === "number" ? session.retryAfterMs : 750);
|
|
1225
|
+
continue;
|
|
1226
|
+
}
|
|
1227
|
+
const sessionRecord = recordOf(session) ?? {};
|
|
1228
|
+
applyEnvelope(ctx, state, { type: "ready", metadata: sessionRecord.metadata ?? sessionRecord });
|
|
1229
|
+
updatePiUi(ctx, state);
|
|
1230
|
+
return true;
|
|
1231
|
+
}
|
|
1232
|
+
}
|
|
1233
|
+
function parseWsPayload(message2) {
|
|
1234
|
+
if (typeof message2.data === "string")
|
|
1235
|
+
return JSON.parse(message2.data);
|
|
1236
|
+
return JSON.parse(Buffer.from(message2.data).toString("utf8"));
|
|
1237
|
+
}
|
|
1238
|
+
async function connectWorkerStream(options, ctx, state) {
|
|
1239
|
+
const ready = await waitForWorkerReady(options, ctx, state);
|
|
1240
|
+
if (!ready)
|
|
1241
|
+
return;
|
|
1242
|
+
let catchupDone = false;
|
|
1243
|
+
const buffered = [];
|
|
1244
|
+
const wsUrl = await buildRunPiEventsWebSocketUrl(options.context, options.runId);
|
|
1245
|
+
const socket = new WebSocket(wsUrl);
|
|
1246
|
+
const closePromise = new Promise((resolve3) => {
|
|
1247
|
+
socket.onopen = () => {
|
|
1248
|
+
state.wsConnected = true;
|
|
1249
|
+
state.status = "live worker Pi WebSocket connected";
|
|
1250
|
+
updatePiUi(ctx, state);
|
|
1251
|
+
};
|
|
1252
|
+
socket.onmessage = (message2) => {
|
|
1253
|
+
try {
|
|
1254
|
+
const payload = parseWsPayload(message2);
|
|
1255
|
+
if (!catchupDone)
|
|
1256
|
+
buffered.push(payload);
|
|
1257
|
+
else {
|
|
1258
|
+
applyEnvelope(ctx, state, payload);
|
|
1259
|
+
updatePiUi(ctx, state);
|
|
1260
|
+
}
|
|
1261
|
+
} catch (error) {
|
|
1262
|
+
appendTranscript(state, "Error", `Unparseable worker Pi event: ${error instanceof Error ? error.message : String(error)}`);
|
|
1263
|
+
updatePiUi(ctx, state);
|
|
1264
|
+
}
|
|
1265
|
+
};
|
|
1266
|
+
socket.onerror = () => socket.close();
|
|
1267
|
+
socket.onclose = () => {
|
|
1268
|
+
state.wsConnected = false;
|
|
1269
|
+
state.status = "worker Pi WebSocket disconnected";
|
|
1270
|
+
updatePiUi(ctx, state);
|
|
1271
|
+
resolve3();
|
|
1272
|
+
};
|
|
1273
|
+
});
|
|
1274
|
+
try {
|
|
1275
|
+
const [messagesPayload, statusPayload, commandsPayload] = await Promise.all([
|
|
1276
|
+
getRunPiMessagesViaServer(options.context, options.runId),
|
|
1277
|
+
getRunPiStatusViaServer(options.context, options.runId),
|
|
1278
|
+
getRunPiCommandsViaServer(options.context, options.runId)
|
|
1005
1279
|
]);
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1280
|
+
const messages = Array.isArray(messagesPayload.messages) ? messagesPayload.messages : [];
|
|
1281
|
+
const native = nativePiUi(ctx);
|
|
1282
|
+
if (state.nativeStream && native?.appendSessionMessages)
|
|
1283
|
+
native.appendSessionMessages(messages);
|
|
1284
|
+
else
|
|
1285
|
+
for (const message2 of messages)
|
|
1286
|
+
applyMessage(state, message2);
|
|
1287
|
+
applyStatus(state, statusPayload);
|
|
1288
|
+
const commands = Array.isArray(commandsPayload.commands) ? commandsPayload.commands : [];
|
|
1289
|
+
state.commands = commands.flatMap((command) => {
|
|
1290
|
+
const record = recordOf(command);
|
|
1291
|
+
return typeof record?.name === "string" ? [`/${record.name}`] : [];
|
|
1292
|
+
});
|
|
1293
|
+
catchupDone = true;
|
|
1294
|
+
for (const payload of buffered.splice(0))
|
|
1295
|
+
applyEnvelope(ctx, state, payload);
|
|
1296
|
+
updatePiUi(ctx, state);
|
|
1297
|
+
} catch (error) {
|
|
1298
|
+
appendTranscript(state, "Error", `Worker Pi catch-up failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
1299
|
+
catchupDone = true;
|
|
1300
|
+
updatePiUi(ctx, state);
|
|
1301
|
+
}
|
|
1302
|
+
await closePromise;
|
|
1303
|
+
}
|
|
1304
|
+
function createRemoteBashOperations(options, state, excludeFromContext) {
|
|
1305
|
+
return {
|
|
1306
|
+
exec(command, _cwd, execOptions) {
|
|
1307
|
+
return new Promise((resolve3, reject) => {
|
|
1308
|
+
const pending = {
|
|
1309
|
+
command,
|
|
1310
|
+
onData: execOptions.onData,
|
|
1311
|
+
resolve: resolve3,
|
|
1312
|
+
reject,
|
|
1313
|
+
sawChunk: false
|
|
1314
|
+
};
|
|
1315
|
+
const cleanup = () => {
|
|
1316
|
+
execOptions.signal?.removeEventListener("abort", onAbort);
|
|
1317
|
+
if (timer)
|
|
1318
|
+
clearTimeout(timer);
|
|
1319
|
+
};
|
|
1320
|
+
const onAbort = () => {
|
|
1321
|
+
cleanup();
|
|
1322
|
+
failPendingShell(state, pending, new Error("Remote worker shell command aborted locally."));
|
|
1323
|
+
};
|
|
1324
|
+
const timeoutMs = typeof execOptions.timeout === "number" && execOptions.timeout > 0 ? execOptions.timeout : 0;
|
|
1325
|
+
const timer = timeoutMs > 0 ? setTimeout(() => {
|
|
1326
|
+
cleanup();
|
|
1327
|
+
failPendingShell(state, pending, new Error(`Remote worker shell command timed out after ${timeoutMs}ms.`));
|
|
1328
|
+
}, timeoutMs) : null;
|
|
1329
|
+
const wrappedResolve = pending.resolve;
|
|
1330
|
+
const wrappedReject = pending.reject;
|
|
1331
|
+
pending.resolve = (result) => {
|
|
1332
|
+
cleanup();
|
|
1333
|
+
wrappedResolve(result);
|
|
1334
|
+
};
|
|
1335
|
+
pending.reject = (error) => {
|
|
1336
|
+
cleanup();
|
|
1337
|
+
wrappedReject(error);
|
|
1338
|
+
};
|
|
1339
|
+
execOptions.signal?.addEventListener("abort", onAbort, { once: true });
|
|
1340
|
+
state.pendingShells.push(pending);
|
|
1341
|
+
sendRunPiShellViaServer(options.context, options.runId, `${excludeFromContext ? "!!" : "!"}${command}`).catch((error) => {
|
|
1342
|
+
cleanup();
|
|
1343
|
+
failPendingShell(state, pending, error instanceof Error ? error : new Error(String(error)));
|
|
1344
|
+
});
|
|
1345
|
+
});
|
|
1346
|
+
}
|
|
1347
|
+
};
|
|
1348
|
+
}
|
|
1349
|
+
async function answerPendingUi(options, state, line) {
|
|
1350
|
+
const pending = state.pendingUi;
|
|
1351
|
+
if (!pending)
|
|
1352
|
+
return false;
|
|
1353
|
+
if (line === "/cancel") {
|
|
1354
|
+
await respondRunPiExtensionUiViaServer(options.context, options.runId, pending.requestId, { cancelled: true });
|
|
1355
|
+
} else if (pending.method === "confirm") {
|
|
1356
|
+
const confirmed = /^(y|yes|true|1)$/i.test(line);
|
|
1357
|
+
await respondRunPiExtensionUiViaServer(options.context, options.runId, pending.requestId, { value: confirmed, confirmed });
|
|
1358
|
+
} else if (pending.options.length > 0 && /^\d+$/.test(line)) {
|
|
1359
|
+
const selected = pending.options[Math.max(0, Number(line) - 1)] ?? line;
|
|
1360
|
+
await respondRunPiExtensionUiViaServer(options.context, options.runId, pending.requestId, { value: selected });
|
|
1361
|
+
} else {
|
|
1362
|
+
await respondRunPiExtensionUiViaServer(options.context, options.runId, pending.requestId, { value: line });
|
|
1363
|
+
}
|
|
1364
|
+
appendTranscript(state, "System", `Responded to extension UI request ${pending.requestId}.`);
|
|
1365
|
+
state.pendingUi = null;
|
|
1366
|
+
return true;
|
|
1367
|
+
}
|
|
1368
|
+
async function routeInput(options, ctx, state, line) {
|
|
1369
|
+
const text = line.trim();
|
|
1370
|
+
if (!text)
|
|
1371
|
+
return;
|
|
1372
|
+
if (await answerPendingUi(options, state, text)) {
|
|
1373
|
+
updatePiUi(ctx, state);
|
|
1374
|
+
return;
|
|
1375
|
+
}
|
|
1376
|
+
if (text === "/detach" || text === "/quit" || text === "/q") {
|
|
1377
|
+
appendTranscript(state, "System", "Detached locally; worker Pi daemon continues.");
|
|
1378
|
+
updatePiUi(ctx, state);
|
|
1379
|
+
ctx.shutdown();
|
|
1380
|
+
return;
|
|
1381
|
+
}
|
|
1382
|
+
if (text === "/stop") {
|
|
1383
|
+
await abortRunPiViaServer(options.context, options.runId);
|
|
1384
|
+
appendTranscript(state, "System", "Stop requested for worker Pi daemon.");
|
|
1385
|
+
updatePiUi(ctx, state);
|
|
1386
|
+
ctx.shutdown();
|
|
1387
|
+
return;
|
|
1388
|
+
}
|
|
1389
|
+
if (text.startsWith("!")) {
|
|
1390
|
+
appendTranscript(state, "You", text);
|
|
1391
|
+
await sendRunPiShellViaServer(options.context, options.runId, text);
|
|
1392
|
+
} else if (text.startsWith("/")) {
|
|
1393
|
+
appendTranscript(state, "You", text);
|
|
1394
|
+
const result = await runRunPiCommandViaServer(options.context, options.runId, text);
|
|
1395
|
+
const message2 = typeof result.message === "string" ? result.message : "worker command accepted";
|
|
1396
|
+
appendTranscript(state, "System", message2);
|
|
1397
|
+
} else {
|
|
1398
|
+
appendTranscript(state, "You", text);
|
|
1399
|
+
await sendRunPiPromptViaServer(options.context, options.runId, text, state.streaming ? "steer" : undefined);
|
|
1400
|
+
}
|
|
1401
|
+
updatePiUi(ctx, state);
|
|
1402
|
+
}
|
|
1403
|
+
function createRigWorkerPiBridgeExtension(options) {
|
|
1404
|
+
return (pi) => {
|
|
1405
|
+
const state = {
|
|
1406
|
+
transcript: [],
|
|
1407
|
+
status: "starting worker Pi daemon bridge",
|
|
1408
|
+
activity: "",
|
|
1409
|
+
cwd: "",
|
|
1410
|
+
model: "",
|
|
1411
|
+
commands: [],
|
|
1412
|
+
streaming: false,
|
|
1413
|
+
pendingUi: null,
|
|
1414
|
+
pendingShells: [],
|
|
1415
|
+
wsConnected: false,
|
|
1416
|
+
nativeStream: false
|
|
1013
1417
|
};
|
|
1418
|
+
if (options.initialMessageSent)
|
|
1419
|
+
appendTranscript(state, "System", "Initial message sent to worker Pi daemon.");
|
|
1420
|
+
let nativePiUiContextAvailable = false;
|
|
1421
|
+
pi.on("user_bash", (event) => {
|
|
1422
|
+
state.nativeStream = Boolean(state.nativeStream || nativePiUiContextAvailable);
|
|
1423
|
+
return { operations: createRemoteBashOperations(options, state, event.excludeFromContext === true) };
|
|
1424
|
+
});
|
|
1425
|
+
pi.on("session_start", async (_event, ctx) => {
|
|
1426
|
+
nativePiUiContextAvailable = Boolean(nativePiUi(ctx));
|
|
1427
|
+
state.nativeStream = nativePiUiContextAvailable;
|
|
1428
|
+
updatePiUi(ctx, state);
|
|
1429
|
+
ctx.ui.notify(nativePiUiContextAvailable ? "Rig worker Pi native stream bridge loaded" : "Rig worker Pi bridge extension loaded (degraded widget fallback)", "info");
|
|
1430
|
+
ctx.ui.onTerminalInput((data) => {
|
|
1431
|
+
if (data.includes("\x04")) {
|
|
1432
|
+
ctx.shutdown();
|
|
1433
|
+
return { consume: true };
|
|
1434
|
+
}
|
|
1435
|
+
if (!data.includes("\r") && !data.includes(`
|
|
1436
|
+
`))
|
|
1437
|
+
return;
|
|
1438
|
+
const inlineText = data.replace(/[\r\n]+/g, "").trim();
|
|
1439
|
+
const editorText = ctx.ui.getEditorText().trim();
|
|
1440
|
+
const text = [editorText, inlineText].filter(Boolean).join(" ").trim();
|
|
1441
|
+
if (!text)
|
|
1442
|
+
return;
|
|
1443
|
+
if (text.startsWith("!"))
|
|
1444
|
+
return;
|
|
1445
|
+
ctx.ui.setEditorText("");
|
|
1446
|
+
routeInput(options, ctx, state, text).catch((error) => {
|
|
1447
|
+
appendTranscript(state, "Error", error instanceof Error ? error.message : String(error));
|
|
1448
|
+
updatePiUi(ctx, state);
|
|
1449
|
+
});
|
|
1450
|
+
return { consume: true };
|
|
1451
|
+
});
|
|
1452
|
+
connectWorkerStream(options, ctx, state).catch((error) => {
|
|
1453
|
+
appendTranscript(state, "Error", error instanceof Error ? error.message : String(error));
|
|
1454
|
+
updatePiUi(ctx, state);
|
|
1455
|
+
});
|
|
1456
|
+
});
|
|
1457
|
+
pi.on("session_shutdown", () => {});
|
|
1458
|
+
};
|
|
1459
|
+
}
|
|
1460
|
+
|
|
1461
|
+
// packages/cli/src/commands/_pi-frontend.ts
|
|
1462
|
+
function setTemporaryEnv(updates) {
|
|
1463
|
+
const previous = new Map;
|
|
1464
|
+
for (const [key, value] of Object.entries(updates)) {
|
|
1465
|
+
previous.set(key, process.env[key]);
|
|
1466
|
+
process.env[key] = value;
|
|
1467
|
+
}
|
|
1468
|
+
return () => {
|
|
1469
|
+
for (const [key, value] of previous) {
|
|
1470
|
+
if (value === undefined)
|
|
1471
|
+
delete process.env[key];
|
|
1472
|
+
else
|
|
1473
|
+
process.env[key] = value;
|
|
1474
|
+
}
|
|
1475
|
+
};
|
|
1476
|
+
}
|
|
1477
|
+
async function attachRunBundledPiFrontend(context, input) {
|
|
1478
|
+
const tempRoot = mkdtempSync(join(tmpdir(), "rig-pi-frontend-"));
|
|
1479
|
+
const cwd = join(tempRoot, "workspace");
|
|
1480
|
+
const agentDir = join(tempRoot, "agent");
|
|
1481
|
+
const sessionDir = join(tempRoot, "sessions");
|
|
1482
|
+
const previousCwd = process.cwd();
|
|
1483
|
+
const restoreEnv = setTemporaryEnv({
|
|
1484
|
+
PI_CODING_AGENT_DIR: agentDir,
|
|
1485
|
+
PI_CODING_AGENT_SESSION_DIR: sessionDir,
|
|
1486
|
+
PI_OFFLINE: "1",
|
|
1487
|
+
PI_SKIP_VERSION_CHECK: "1"
|
|
1488
|
+
});
|
|
1489
|
+
let detached = false;
|
|
1490
|
+
try {
|
|
1491
|
+
await Bun.$`mkdir -p ${cwd} ${agentDir} ${sessionDir}`.quiet();
|
|
1492
|
+
process.chdir(cwd);
|
|
1493
|
+
await runPiMain([
|
|
1494
|
+
"--offline",
|
|
1495
|
+
"--no-session",
|
|
1496
|
+
"--no-tools",
|
|
1497
|
+
"--no-builtin-tools",
|
|
1498
|
+
"--no-skills",
|
|
1499
|
+
"--no-prompt-templates",
|
|
1500
|
+
"--no-themes",
|
|
1501
|
+
"--no-context-files",
|
|
1502
|
+
"--no-approve"
|
|
1503
|
+
], {
|
|
1504
|
+
extensionFactories: [
|
|
1505
|
+
createRigWorkerPiBridgeExtension({
|
|
1506
|
+
context,
|
|
1507
|
+
runId: input.runId,
|
|
1508
|
+
initialMessageSent: input.steered === true
|
|
1509
|
+
})
|
|
1510
|
+
]
|
|
1511
|
+
});
|
|
1512
|
+
detached = true;
|
|
1513
|
+
} finally {
|
|
1514
|
+
process.chdir(previousCwd);
|
|
1515
|
+
restoreEnv();
|
|
1516
|
+
rmSync(tempRoot, { recursive: true, force: true });
|
|
1014
1517
|
}
|
|
1518
|
+
let run = { runId: input.runId, status: "unknown" };
|
|
1519
|
+
try {
|
|
1520
|
+
run = await getRunDetailsViaServer(context, input.runId);
|
|
1521
|
+
} catch {}
|
|
1522
|
+
return {
|
|
1523
|
+
run,
|
|
1524
|
+
logs: [],
|
|
1525
|
+
timeline: [],
|
|
1526
|
+
timelineCursor: null,
|
|
1527
|
+
steered: input.steered === true,
|
|
1528
|
+
detached,
|
|
1529
|
+
rendered: "actual bundled Pi frontend hosted Rig worker Pi bridge extension"
|
|
1530
|
+
};
|
|
1015
1531
|
}
|
|
1532
|
+
|
|
1533
|
+
// packages/cli/src/commands/_operator-view.ts
|
|
1534
|
+
var TERMINAL_RUN_STATUSES2 = new Set(["completed", "failed", "stopped", "cancelled", "canceled", "closed", "merged", "needs_attention", "needs-attention"]);
|
|
1016
1535
|
function runStatusFromPayload(payload) {
|
|
1017
1536
|
const run = payload.run && typeof payload.run === "object" && !Array.isArray(payload.run) ? payload.run : payload;
|
|
1018
1537
|
return String(run.status ?? "unknown").toLowerCase();
|
|
@@ -1051,198 +1570,15 @@ async function readOperatorSnapshot(context, runId, options = {}) {
|
|
|
1051
1570
|
const timelineCursor = typeof timelinePage.nextCursor === "string" ? timelinePage.nextCursor : options.timelineCursor ?? null;
|
|
1052
1571
|
return { run, logs, timeline, timelineCursor, rendered: renderOperatorSnapshot({ run, logs, timeline }) };
|
|
1053
1572
|
}
|
|
1054
|
-
function unwrapRun(runPayload) {
|
|
1055
|
-
return runPayload.run && typeof runPayload.run === "object" && !Array.isArray(runPayload.run) ? runPayload.run : runPayload;
|
|
1056
|
-
}
|
|
1057
|
-
function logDetail2(log) {
|
|
1058
|
-
return typeof log.detail === "string" ? log.detail.trim() : "";
|
|
1059
|
-
}
|
|
1060
|
-
function logTitle(log) {
|
|
1061
|
-
return typeof log.title === "string" ? log.title.trim() : "";
|
|
1062
|
-
}
|
|
1063
|
-
function renderAssistantTextFromTimeline(entries) {
|
|
1064
|
-
const assistant = entries.filter((entry) => entry.type === "assistant_message" && typeof entry.text === "string").at(-1);
|
|
1065
|
-
const text = typeof assistant?.text === "string" ? assistant.text.trimEnd() : "";
|
|
1066
|
-
if (!text)
|
|
1067
|
-
return [];
|
|
1068
|
-
return [`${BLUE}${BOLD}Remote Pi assistant${RESET}`, ...text.split(/\r?\n/).slice(-18)];
|
|
1069
|
-
}
|
|
1070
|
-
function renderToolLines(entries) {
|
|
1071
|
-
return entries.filter((entry) => entry.type === "tool_execution_start" || entry.type === "tool_execution_update" || entry.type === "tool_execution_end" || entry.type === "mcp_tool_call").slice(-8).map((entry) => `${DIM}[tool]${RESET} ${String(entry.toolName ?? entry.name ?? entry.title ?? entry.type)} ${String(entry.status ?? entry.state ?? "")}`.trim());
|
|
1072
|
-
}
|
|
1073
|
-
function renderStageLines(logs) {
|
|
1074
|
-
const latestByStage = new Map;
|
|
1075
|
-
for (const log of logs) {
|
|
1076
|
-
const title = logTitle(log).toLowerCase();
|
|
1077
|
-
const stageName = String(log.stage ?? "").toLowerCase();
|
|
1078
|
-
const stage = CANONICAL_STAGES2.find((candidate) => candidate.toLowerCase() === title || candidate.toLowerCase() === stageName);
|
|
1079
|
-
if (stage)
|
|
1080
|
-
latestByStage.set(stage, log);
|
|
1081
|
-
}
|
|
1082
|
-
return CANONICAL_STAGES2.map((stage) => {
|
|
1083
|
-
const log = latestByStage.get(stage);
|
|
1084
|
-
const status = String(log?.status ?? "pending");
|
|
1085
|
-
const detail = log ? logDetail2(log) : "";
|
|
1086
|
-
const color = status === "completed" ? GREEN : status === "failed" || status === "rejected" ? RED : status === "pending" ? DIM : YELLOW;
|
|
1087
|
-
const mark = status === "completed" ? "\u2713" : status === "pending" ? "\xB7" : status === "failed" ? "\u2717" : "\u25B6";
|
|
1088
|
-
return `${color}${mark} ${stage}${RESET}${detail ? ` ${DIM}\u2014 ${detail.slice(0, 140)}${RESET}` : ""}`;
|
|
1089
|
-
});
|
|
1090
|
-
}
|
|
1091
|
-
function renderEventLines(logs) {
|
|
1092
|
-
return logs.filter((log) => !CANONICAL_STAGES2.some((stage) => stage.toLowerCase() === logTitle(log).toLowerCase())).slice(-12).flatMap((log) => {
|
|
1093
|
-
const title = logTitle(log) || "Rig event";
|
|
1094
|
-
const detail = logDetail2(log);
|
|
1095
|
-
if (!detail)
|
|
1096
|
-
return [];
|
|
1097
|
-
const tone = String(log.tone ?? "");
|
|
1098
|
-
const color = tone === "error" ? RED : tone === "tool" ? MAGENTA : DIM;
|
|
1099
|
-
return [`${color}[${title}]${RESET} ${detail.slice(0, 220)}`];
|
|
1100
|
-
});
|
|
1101
|
-
}
|
|
1102
|
-
|
|
1103
|
-
class RigRunComponent {
|
|
1104
|
-
truncateToWidth;
|
|
1105
|
-
snapshot = { run: {}, logs: [], timeline: [] };
|
|
1106
|
-
localEvents = [];
|
|
1107
|
-
constructor(truncateToWidth) {
|
|
1108
|
-
this.truncateToWidth = truncateToWidth;
|
|
1109
|
-
}
|
|
1110
|
-
update(snapshot) {
|
|
1111
|
-
this.snapshot = snapshot;
|
|
1112
|
-
}
|
|
1113
|
-
addLocalEvent(message2) {
|
|
1114
|
-
this.localEvents.push(`${new Date().toLocaleTimeString()} ${message2}`);
|
|
1115
|
-
this.localEvents = this.localEvents.slice(-8);
|
|
1116
|
-
}
|
|
1117
|
-
invalidate() {}
|
|
1118
|
-
render(width) {
|
|
1119
|
-
const run = unwrapRun(this.snapshot.run);
|
|
1120
|
-
const runId = String(run.runId ?? run.id ?? "run");
|
|
1121
|
-
const status = String(run.status ?? "unknown");
|
|
1122
|
-
const worker = typeof run.worktreePath === "string" && run.worktreePath.trim() ? run.worktreePath.trim() : typeof run.projectRoot === "string" && run.projectRoot.trim() ? run.projectRoot.trim() : "worker workspace pending";
|
|
1123
|
-
const lines = [
|
|
1124
|
-
`${BOLD}Rig Pi frontend${RESET} ${DIM}(local Pi TUI \u2192 Rig server \u2192 worker Pi backend)${RESET}`,
|
|
1125
|
-
`${BOLD}${runId}${RESET} \xB7 ${status} \xB7 ${DIM}${worker}${RESET}`,
|
|
1126
|
-
"",
|
|
1127
|
-
`${BOLD}Rig flow${RESET}`,
|
|
1128
|
-
...renderStageLines(this.snapshot.logs ?? []),
|
|
1129
|
-
"",
|
|
1130
|
-
...renderAssistantTextFromTimeline(this.snapshot.timeline ?? []),
|
|
1131
|
-
...renderToolLines(this.snapshot.timeline ?? []),
|
|
1132
|
-
"",
|
|
1133
|
-
`${BOLD}Rig / Pi events${RESET}`,
|
|
1134
|
-
...renderEventLines(this.snapshot.logs ?? []),
|
|
1135
|
-
...this.localEvents.map((event) => `${GREEN}[frontend]${RESET} ${event}`),
|
|
1136
|
-
""
|
|
1137
|
-
];
|
|
1138
|
-
return lines.slice(-42).map((line) => this.truncateToWidth(line, Math.max(10, width)));
|
|
1139
|
-
}
|
|
1140
|
-
}
|
|
1141
|
-
|
|
1142
|
-
class RigInputComponent {
|
|
1143
|
-
matchesKey;
|
|
1144
|
-
truncateToWidth;
|
|
1145
|
-
input;
|
|
1146
|
-
status = "Type text, /skill:..., or remote Pi slash commands. Local: /stop /detach.";
|
|
1147
|
-
constructor(InputCtor, matchesKey, truncateToWidth, onSubmit, onEscape) {
|
|
1148
|
-
this.matchesKey = matchesKey;
|
|
1149
|
-
this.truncateToWidth = truncateToWidth;
|
|
1150
|
-
this.input = new InputCtor;
|
|
1151
|
-
this.input.onSubmit = (value) => {
|
|
1152
|
-
const text = value.trim();
|
|
1153
|
-
this.input.setValue("");
|
|
1154
|
-
if (text)
|
|
1155
|
-
Promise.resolve(onSubmit(text));
|
|
1156
|
-
};
|
|
1157
|
-
this.input.onEscape = onEscape;
|
|
1158
|
-
}
|
|
1159
|
-
handleInput(data) {
|
|
1160
|
-
if (this.matchesKey(data, "ctrl+d")) {
|
|
1161
|
-
this.input.onEscape?.();
|
|
1162
|
-
return;
|
|
1163
|
-
}
|
|
1164
|
-
this.input.handleInput?.(data);
|
|
1165
|
-
}
|
|
1166
|
-
setStatus(status) {
|
|
1167
|
-
this.status = status;
|
|
1168
|
-
}
|
|
1169
|
-
invalidate() {
|
|
1170
|
-
this.input.invalidate();
|
|
1171
|
-
}
|
|
1172
|
-
render(width) {
|
|
1173
|
-
return [
|
|
1174
|
-
`${DIM}${this.truncateToWidth(this.status, Math.max(10, width))}${RESET}`,
|
|
1175
|
-
`${GREEN}${BOLD}You \u2192 worker Pi:${RESET}`,
|
|
1176
|
-
...this.input.render(width)
|
|
1177
|
-
];
|
|
1178
|
-
}
|
|
1179
|
-
}
|
|
1180
|
-
async function attachRunPiTuiFrontend(context, input) {
|
|
1181
|
-
const piTui = await loadPiTuiRuntime();
|
|
1182
|
-
const terminal = new piTui.ProcessTerminal;
|
|
1183
|
-
const tui = new piTui.TUI(terminal);
|
|
1184
|
-
const root = new piTui.Container;
|
|
1185
|
-
const runView = new RigRunComponent(piTui.truncateToWidth);
|
|
1186
|
-
let detached = false;
|
|
1187
|
-
let steered = input.steered === true;
|
|
1188
|
-
let latest = await readOperatorSnapshot(context, input.runId);
|
|
1189
|
-
let timelineCursor = latest.timelineCursor;
|
|
1190
|
-
runView.update(latest);
|
|
1191
|
-
if (steered)
|
|
1192
|
-
runView.addLocalEvent("initial message queued to worker Pi.");
|
|
1193
|
-
const stop = () => {
|
|
1194
|
-
detached = true;
|
|
1195
|
-
tui.stop();
|
|
1196
|
-
};
|
|
1197
|
-
const inputView = new RigInputComponent(piTui.Input, piTui.matchesKey, piTui.truncateToWidth, async (line) => {
|
|
1198
|
-
if (line === "/detach" || line === "/quit" || line === "/q") {
|
|
1199
|
-
runView.addLocalEvent("detached from run.");
|
|
1200
|
-
stop();
|
|
1201
|
-
return;
|
|
1202
|
-
}
|
|
1203
|
-
if (line === "/stop") {
|
|
1204
|
-
await stopRunViaServer(context, input.runId);
|
|
1205
|
-
runView.addLocalEvent("stop requested.");
|
|
1206
|
-
stop();
|
|
1207
|
-
return;
|
|
1208
|
-
}
|
|
1209
|
-
await steerRunViaServer(context, input.runId, line);
|
|
1210
|
-
steered = true;
|
|
1211
|
-
runView.addLocalEvent(`queued to worker Pi: ${line.slice(0, 160)}`);
|
|
1212
|
-
tui.requestRender();
|
|
1213
|
-
}, stop);
|
|
1214
|
-
root.addChild(runView);
|
|
1215
|
-
root.addChild(inputView);
|
|
1216
|
-
tui.addChild(root);
|
|
1217
|
-
tui.setFocus(inputView.input);
|
|
1218
|
-
tui.start();
|
|
1219
|
-
tui.requestRender(true);
|
|
1220
|
-
const pollMs = Math.max(250, Math.trunc(input.pollMs ?? 1000));
|
|
1221
|
-
try {
|
|
1222
|
-
while (!detached && !TERMINAL_RUN_STATUSES.has(runStatusFromPayload(latest.run))) {
|
|
1223
|
-
await Bun.sleep(pollMs);
|
|
1224
|
-
latest = await readOperatorSnapshot(context, input.runId, { timelineCursor });
|
|
1225
|
-
timelineCursor = latest.timelineCursor;
|
|
1226
|
-
runView.update(latest);
|
|
1227
|
-
inputView.setStatus(`Remote worker ${runStatusFromPayload(latest.run)}. Input is forwarded to worker Pi; /stop /detach are local controls.`);
|
|
1228
|
-
tui.requestRender();
|
|
1229
|
-
}
|
|
1230
|
-
} finally {
|
|
1231
|
-
if (!detached)
|
|
1232
|
-
tui.stop();
|
|
1233
|
-
}
|
|
1234
|
-
return { ...latest, timelineCursor, steered, detached, rendered: renderOperatorSnapshot(latest) };
|
|
1235
|
-
}
|
|
1236
1573
|
async function attachRunOperatorView(context, input) {
|
|
1237
1574
|
let steered = false;
|
|
1238
1575
|
if (input.message?.trim()) {
|
|
1239
|
-
await steerRunViaServer(context, input.runId, input.message.trim());
|
|
1576
|
+
await sendRunPiPromptViaServer(context, input.runId, input.message.trim(), "steer").catch(() => steerRunViaServer(context, input.runId, input.message.trim()));
|
|
1240
1577
|
steered = true;
|
|
1241
1578
|
}
|
|
1242
1579
|
if (input.follow && !input.once && input.interactive !== false && context.outputMode === "text" && Boolean(process.stdin.isTTY && process.stdout.isTTY)) {
|
|
1243
|
-
return
|
|
1580
|
+
return attachRunBundledPiFrontend(context, {
|
|
1244
1581
|
runId: input.runId,
|
|
1245
|
-
pollMs: input.pollMs,
|
|
1246
1582
|
steered
|
|
1247
1583
|
});
|
|
1248
1584
|
}
|
|
@@ -1253,7 +1589,7 @@ async function attachRunOperatorView(context, input) {
|
|
|
1253
1589
|
surface.renderTimeline(snapshot.timeline);
|
|
1254
1590
|
surface.renderLogs(snapshot.logs);
|
|
1255
1591
|
if (steered)
|
|
1256
|
-
surface.info("
|
|
1592
|
+
surface.info("Message submitted to worker Pi.");
|
|
1257
1593
|
}
|
|
1258
1594
|
let detached = false;
|
|
1259
1595
|
let commandInput = null;
|
|
@@ -1272,7 +1608,7 @@ async function attachRunOperatorView(context, input) {
|
|
|
1272
1608
|
}
|
|
1273
1609
|
const pollMs = Math.max(250, Math.trunc(input.pollMs ?? 2000));
|
|
1274
1610
|
let timelineCursor = snapshot.timelineCursor;
|
|
1275
|
-
while (!detached && !
|
|
1611
|
+
while (!detached && !TERMINAL_RUN_STATUSES2.has(runStatusFromPayload(snapshot.run))) {
|
|
1276
1612
|
await Bun.sleep(pollMs);
|
|
1277
1613
|
snapshot = await readOperatorSnapshot(context, input.runId, { timelineCursor });
|
|
1278
1614
|
timelineCursor = snapshot.timelineCursor;
|
|
@@ -1624,7 +1960,7 @@ async function executeTask(context, args, options) {
|
|
|
1624
1960
|
const fileFlag = takeOption(rest.slice(1), "--file");
|
|
1625
1961
|
let content;
|
|
1626
1962
|
if (fileFlag.value) {
|
|
1627
|
-
content =
|
|
1963
|
+
content = readFileSync3(resolve3(context.projectRoot, fileFlag.value), "utf-8");
|
|
1628
1964
|
} else {
|
|
1629
1965
|
content = await readStdin();
|
|
1630
1966
|
}
|