@h-rig/cli 0.0.6-alpha.3 → 0.0.6-alpha.30

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.
Files changed (47) hide show
  1. package/dist/bin/rig.js +3606 -1172
  2. package/dist/src/commands/_authority-runs.js +1 -0
  3. package/dist/src/commands/_cli-format.js +369 -0
  4. package/dist/src/commands/_connection-state.js +1 -3
  5. package/dist/src/commands/_doctor-checks.js +13 -27
  6. package/dist/src/commands/_help-catalog.js +388 -0
  7. package/dist/src/commands/_operator-surface.js +204 -0
  8. package/dist/src/commands/_operator-view.js +861 -56
  9. package/dist/src/commands/_parsers.js +0 -2
  10. package/dist/src/commands/_pi-frontend.js +841 -0
  11. package/dist/src/commands/_pi-install.js +4 -3
  12. package/dist/src/commands/_pi-worker-bridge-extension.js +759 -0
  13. package/dist/src/commands/_policy.js +0 -2
  14. package/dist/src/commands/_preflight.js +32 -109
  15. package/dist/src/commands/_run-driver-helpers.js +0 -2
  16. package/dist/src/commands/_server-client.js +161 -31
  17. package/dist/src/commands/_snapshot-upload.js +8 -23
  18. package/dist/src/commands/_task-picker.js +44 -16
  19. package/dist/src/commands/agent.js +9 -9
  20. package/dist/src/commands/browser.js +4 -6
  21. package/dist/src/commands/connect.js +132 -25
  22. package/dist/src/commands/dist.js +4 -6
  23. package/dist/src/commands/doctor.js +13 -27
  24. package/dist/src/commands/github.js +10 -25
  25. package/dist/src/commands/inbox.js +351 -31
  26. package/dist/src/commands/init.js +298 -71
  27. package/dist/src/commands/inspect.js +10 -12
  28. package/dist/src/commands/inspector.js +2 -4
  29. package/dist/src/commands/plugin.js +76 -22
  30. package/dist/src/commands/profile-and-review.js +8 -10
  31. package/dist/src/commands/queue.js +2 -3
  32. package/dist/src/commands/remote.js +18 -20
  33. package/dist/src/commands/repo-git-harness.js +6 -8
  34. package/dist/src/commands/run.js +1157 -122
  35. package/dist/src/commands/server.js +217 -33
  36. package/dist/src/commands/setup.js +17 -37
  37. package/dist/src/commands/task-report-bug.js +5 -7
  38. package/dist/src/commands/task-run-driver.js +660 -73
  39. package/dist/src/commands/task.js +1542 -252
  40. package/dist/src/commands/test.js +3 -5
  41. package/dist/src/commands/workspace.js +4 -6
  42. package/dist/src/commands.js +3599 -1159
  43. package/dist/src/index.js +3646 -1215
  44. package/dist/src/launcher.js +5 -3
  45. package/dist/src/report-bug.js +3 -3
  46. package/dist/src/runner.js +5 -19
  47. package/package.json +6 -4
@@ -1,9 +1,5 @@
1
1
  // @bun
2
- // packages/cli/src/commands/_operator-view.ts
3
- import { createInterface } from "readline";
4
-
5
2
  // packages/cli/src/commands/_server-client.ts
6
- import { spawnSync } from "child_process";
7
3
  import { existsSync as existsSync2, readFileSync as readFileSync2 } from "fs";
8
4
  import { resolve as resolve2 } from "path";
9
5
 
@@ -11,8 +7,6 @@ import { resolve as resolve2 } from "path";
11
7
  import { EventBus } from "@rig/runtime/control-plane/runtime/events";
12
8
  import { CliError } from "@rig/runtime/control-plane/errors";
13
9
  import { evaluate, loadPolicy, resolveAction } from "@rig/runtime/control-plane/runtime/guard";
14
- import { PluginManager } from "@rig/runtime/control-plane/runtime/plugins";
15
- import { loadRuntimeContextFromEnv } from "@rig/runtime/control-plane/runtime/context";
16
10
  import { buildBinary } from "@rig/runtime/control-plane/runtime/isolation";
17
11
  import { CliError as CliError2 } from "@rig/runtime/control-plane/errors";
18
12
 
@@ -96,13 +90,13 @@ function resolveSelectedConnection(projectRoot, options = {}) {
96
90
  const global = readGlobalConnections(options);
97
91
  const connection = global.connections[repo.selected];
98
92
  if (!connection) {
99
- throw new CliError2(`Selected Rig connection "${repo.selected}" was not found. Run \`rig connect list\` or \`rig connect use local\`.`, 1);
93
+ throw new CliError2(`Selected Rig server "${repo.selected}" was not found. Run \`rig server list\` or \`rig server use local\`.`, 1);
100
94
  }
101
95
  return { alias: repo.selected, connection };
102
96
  }
103
97
 
104
98
  // packages/cli/src/commands/_server-client.ts
105
- var cachedGitHubBearerToken;
99
+ var scopedGitHubBearerTokens = new Map;
106
100
  function cleanToken(value) {
107
101
  const trimmed = value?.trim();
108
102
  return trimmed ? trimmed : null;
@@ -119,25 +113,13 @@ function readPrivateRemoteSessionToken(projectRoot) {
119
113
  }
120
114
  }
121
115
  function readGitHubBearerTokenForRemote(projectRoot) {
122
- if (cachedGitHubBearerToken !== undefined)
123
- return cachedGitHubBearerToken;
116
+ const scopedKey = resolve2(projectRoot);
117
+ if (scopedGitHubBearerTokens.has(scopedKey))
118
+ return scopedGitHubBearerTokens.get(scopedKey) ?? null;
124
119
  const privateSession = readPrivateRemoteSessionToken(projectRoot);
125
- if (privateSession) {
126
- cachedGitHubBearerToken = privateSession;
127
- return cachedGitHubBearerToken;
128
- }
129
- const envToken = cleanToken(process.env.RIG_GITHUB_TOKEN) ?? cleanToken(process.env.GITHUB_TOKEN) ?? cleanToken(process.env.GH_TOKEN);
130
- if (envToken) {
131
- cachedGitHubBearerToken = envToken;
132
- return cachedGitHubBearerToken;
133
- }
134
- const result = spawnSync("gh", ["auth", "token"], {
135
- encoding: "utf8",
136
- timeout: 5000,
137
- stdio: ["ignore", "pipe", "ignore"]
138
- });
139
- cachedGitHubBearerToken = result.status === 0 ? cleanToken(result.stdout) : null;
140
- return cachedGitHubBearerToken;
120
+ if (privateSession)
121
+ return privateSession;
122
+ return cleanToken(process.env.RIG_SERVER_AUTH_TOKEN) ?? cleanToken(process.env.RIG_REMOTE_AUTH_TOKEN);
141
123
  }
142
124
  async function ensureServerForCli(projectRoot) {
143
125
  try {
@@ -218,6 +200,15 @@ async function getRunLogsViaServer(context, runId, options = {}) {
218
200
  const payload = await requestServerJson(context, `${url.pathname}${url.search}`);
219
201
  return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : { entries: [] };
220
202
  }
203
+ async function getRunTimelineViaServer(context, runId, options = {}) {
204
+ const url = new URL(`http://rig.local/api/runs/${encodeURIComponent(runId)}/timeline`);
205
+ if (options.limit !== undefined)
206
+ url.searchParams.set("limit", String(options.limit));
207
+ if (options.cursor)
208
+ url.searchParams.set("cursor", options.cursor);
209
+ const payload = await requestServerJson(context, `${url.pathname}${url.search}`);
210
+ return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : { entries: [] };
211
+ }
221
212
  async function stopRunViaServer(context, runId) {
222
213
  const payload = await requestServerJson(context, "/api/runs/stop", {
223
214
  method: "POST",
@@ -234,9 +225,69 @@ async function steerRunViaServer(context, runId, message) {
234
225
  });
235
226
  return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : { ok: true };
236
227
  }
228
+ async function getRunPiSessionViaServer(context, runId) {
229
+ const payload = await requestServerJson(context, `/api/runs/${encodeURIComponent(runId)}/pi`);
230
+ return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : {};
231
+ }
232
+ async function getRunPiMessagesViaServer(context, runId) {
233
+ const payload = await requestServerJson(context, `/api/runs/${encodeURIComponent(runId)}/pi/messages`);
234
+ return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : { messages: [] };
235
+ }
236
+ async function getRunPiStatusViaServer(context, runId) {
237
+ const payload = await requestServerJson(context, `/api/runs/${encodeURIComponent(runId)}/pi/status`);
238
+ return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : {};
239
+ }
240
+ async function getRunPiCommandsViaServer(context, runId) {
241
+ const payload = await requestServerJson(context, `/api/runs/${encodeURIComponent(runId)}/pi/commands`);
242
+ return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : { commands: [] };
243
+ }
244
+ async function sendRunPiPromptViaServer(context, runId, text, streamingBehavior) {
245
+ const payload = await requestServerJson(context, `/api/runs/${encodeURIComponent(runId)}/pi/prompt`, {
246
+ method: "POST",
247
+ headers: { "content-type": "application/json" },
248
+ body: JSON.stringify({ text, streamingBehavior })
249
+ });
250
+ return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : { accepted: true };
251
+ }
252
+ async function sendRunPiShellViaServer(context, runId, text) {
253
+ const payload = await requestServerJson(context, `/api/runs/${encodeURIComponent(runId)}/pi/shell`, {
254
+ method: "POST",
255
+ headers: { "content-type": "application/json" },
256
+ body: JSON.stringify({ text })
257
+ });
258
+ return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : { accepted: true };
259
+ }
260
+ async function runRunPiCommandViaServer(context, runId, text) {
261
+ const payload = await requestServerJson(context, `/api/runs/${encodeURIComponent(runId)}/pi/commands/run`, {
262
+ method: "POST",
263
+ headers: { "content-type": "application/json" },
264
+ body: JSON.stringify({ text })
265
+ });
266
+ return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : { type: "done" };
267
+ }
268
+ async function respondRunPiExtensionUiViaServer(context, runId, requestId, valueOrCancel) {
269
+ const payload = await requestServerJson(context, `/api/runs/${encodeURIComponent(runId)}/pi/extension-ui/respond`, {
270
+ method: "POST",
271
+ headers: { "content-type": "application/json" },
272
+ body: JSON.stringify({ requestId, ...valueOrCancel })
273
+ });
274
+ return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : { accepted: true };
275
+ }
276
+ async function abortRunPiViaServer(context, runId) {
277
+ const payload = await requestServerJson(context, `/api/runs/${encodeURIComponent(runId)}/pi/abort`, { method: "POST" });
278
+ return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : { aborted: true };
279
+ }
280
+ async function buildRunPiEventsWebSocketUrl(context, runId) {
281
+ const server = await ensureServerForCli(context.projectRoot);
282
+ const url = new URL(`${server.baseUrl.replace(/\/+$/, "")}/api/runs/${encodeURIComponent(runId)}/pi/events`);
283
+ url.protocol = url.protocol === "https:" ? "wss:" : "ws:";
284
+ if (server.authToken)
285
+ url.searchParams.set("token", server.authToken);
286
+ return url.toString();
287
+ }
237
288
 
238
- // packages/cli/src/commands/_operator-view.ts
239
- var TERMINAL_RUN_STATUSES = new Set(["completed", "failed", "stopped", "cancelled", "canceled", "closed", "merged", "needs_attention", "needs-attention"]);
289
+ // packages/cli/src/commands/_operator-surface.ts
290
+ import { createInterface } from "readline";
240
291
  var CANONICAL_STAGES = [
241
292
  "Connect",
242
293
  "GitHub/task sync",
@@ -251,18 +302,753 @@ var CANONICAL_STAGES = [
251
302
  "Merge",
252
303
  "Complete"
253
304
  ];
305
+ function logDetail(log) {
306
+ return typeof log.detail === "string" ? log.detail.trim() : "";
307
+ }
308
+ function parseProviderProtocolLog(title, detail) {
309
+ if (title.trim().toLowerCase() !== "agent output")
310
+ return null;
311
+ if (!detail.startsWith("{") || !detail.endsWith("}"))
312
+ return null;
313
+ try {
314
+ const record = JSON.parse(detail);
315
+ if (!record || typeof record !== "object" || Array.isArray(record))
316
+ return null;
317
+ const type = record.type;
318
+ return typeof type === "string" && [
319
+ "assistant",
320
+ "message_start",
321
+ "message_update",
322
+ "message_end",
323
+ "stream_event",
324
+ "tool_result",
325
+ "tool_execution_start",
326
+ "tool_execution_update",
327
+ "tool_execution_end",
328
+ "turn_start",
329
+ "turn_end"
330
+ ].includes(type) ? record : null;
331
+ } catch {
332
+ return null;
333
+ }
334
+ }
335
+ function renderProviderProtocolLog(record) {
336
+ const type = typeof record.type === "string" ? record.type : "";
337
+ if (type === "tool_execution_start" || type === "tool_execution_update" || type === "tool_execution_end") {
338
+ const toolName = String(record.toolName ?? record.name ?? "tool");
339
+ const status = type === "tool_execution_start" ? "started" : type === "tool_execution_end" ? record.isError === true || record.result && typeof record.result === "object" && !Array.isArray(record.result) && record.result.isError === true ? "failed" : "completed" : "running";
340
+ return `[Pi tool] ${toolName} ${status}`;
341
+ }
342
+ return null;
343
+ }
344
+ function entryId(entry, fallback) {
345
+ return typeof entry.id === "string" && entry.id.trim() ? entry.id : fallback;
346
+ }
254
347
  function renderOperatorSnapshot(snapshot) {
255
348
  const run = snapshot.run.run && typeof snapshot.run.run === "object" ? snapshot.run.run : snapshot.run;
256
349
  const runId = String(run.runId ?? run.id ?? "run");
257
350
  const status = String(run.status ?? "unknown");
258
351
  const logs = snapshot.logs ?? [];
352
+ const latestByStage = new Map;
353
+ for (const log of logs) {
354
+ const title = String(log.title ?? "").toLowerCase();
355
+ const stageName = String(log.stage ?? "").toLowerCase();
356
+ const stage = CANONICAL_STAGES.find((candidate) => candidate.toLowerCase() === title || candidate.toLowerCase() === stageName);
357
+ if (stage)
358
+ latestByStage.set(stage, log);
359
+ }
259
360
  const stageLines = CANONICAL_STAGES.flatMap((stage) => {
260
- const match = logs.find((log) => String(log.title ?? "").toLowerCase() === stage.toLowerCase() || String(log.stage ?? "").toLowerCase() === stage.toLowerCase());
261
- return match ? [`${stage}: ${String(match.status ?? status)}`] : [];
361
+ const match = latestByStage.get(stage);
362
+ return match ? [`${stage}: ${String(match.status ?? status)}${logDetail(match) ? ` \u2014 ${logDetail(match)}` : ""}`] : [];
262
363
  });
263
364
  return [`Rig run ${runId}: ${status}`, ...stageLines].join(`
264
365
  `);
265
366
  }
367
+ function createPiRunStreamRenderer(output = process.stdout) {
368
+ let lastSnapshot = "";
369
+ const assistantTextById = new Map;
370
+ const seenTimeline = new Set;
371
+ const seenLogs = new Set;
372
+ const writeLine = (line) => output.write(`${line}
373
+ `);
374
+ return {
375
+ renderSnapshot(snapshot) {
376
+ const rendered = renderOperatorSnapshot(snapshot);
377
+ if (rendered && rendered !== lastSnapshot) {
378
+ writeLine(rendered);
379
+ lastSnapshot = rendered;
380
+ }
381
+ },
382
+ renderTimeline(entries) {
383
+ for (const [index, entry] of entries.entries()) {
384
+ const id = entryId(entry, `timeline:${index}:${String(entry.cursor ?? "")}`);
385
+ if (entry.type === "assistant_message" && typeof entry.text === "string") {
386
+ const text = entry.text;
387
+ const previousText = assistantTextById.get(id) ?? "";
388
+ if (!previousText && text.trim()) {
389
+ writeLine("[Pi assistant]");
390
+ }
391
+ if (text.startsWith(previousText)) {
392
+ const delta = text.slice(previousText.length);
393
+ if (delta)
394
+ output.write(delta);
395
+ } else if (text.trim() && text !== previousText) {
396
+ if (previousText)
397
+ writeLine(`
398
+ [Pi assistant]`);
399
+ output.write(text);
400
+ }
401
+ assistantTextById.set(id, text);
402
+ continue;
403
+ }
404
+ if (seenTimeline.has(id))
405
+ continue;
406
+ seenTimeline.add(id);
407
+ if (entry.type === "tool_execution_start" || entry.type === "tool_execution_update" || entry.type === "tool_execution_end" || entry.type === "mcp_tool_call") {
408
+ writeLine(`[Pi tool] ${String(entry.toolName ?? entry.name ?? entry.title ?? entry.type)} ${String(entry.status ?? entry.state ?? "")}`.trim());
409
+ continue;
410
+ }
411
+ if (entry.type === "timeline_warning") {
412
+ writeLine(`[Rig timeline] ${String(entry.detail ?? entry.message ?? "timeline unavailable")}`);
413
+ }
414
+ }
415
+ },
416
+ renderLogs(entries) {
417
+ for (const [index, entry] of entries.entries()) {
418
+ const id = entryId(entry, `log:${index}:${String(entry.createdAt ?? "")}:${String(entry.title ?? "")}`);
419
+ if (seenLogs.has(id))
420
+ continue;
421
+ seenLogs.add(id);
422
+ const title = String(entry.title ?? "");
423
+ if (CANONICAL_STAGES.some((stage) => stage.toLowerCase() === title.toLowerCase()))
424
+ continue;
425
+ const detail = logDetail(entry);
426
+ if (!detail)
427
+ continue;
428
+ const protocolRecord = parseProviderProtocolLog(title, detail);
429
+ if (protocolRecord) {
430
+ const protocolLine = renderProviderProtocolLog(protocolRecord);
431
+ if (protocolLine)
432
+ writeLine(protocolLine);
433
+ continue;
434
+ }
435
+ writeLine(`[${title || "Rig log"}] ${detail}`);
436
+ }
437
+ }
438
+ };
439
+ }
440
+ function createOperatorSurface(options = {}) {
441
+ const input = options.input ?? process.stdin;
442
+ const output = options.output ?? process.stdout;
443
+ const errorOutput = options.errorOutput ?? process.stderr;
444
+ const renderer = createPiRunStreamRenderer(output);
445
+ const writeLine = (line) => output.write(`${line}
446
+ `);
447
+ return {
448
+ mode: "pi-compatible-text",
449
+ ...renderer,
450
+ info: writeLine,
451
+ error: (message) => errorOutput.write(`${message}
452
+ `),
453
+ attachCommandInput(handler) {
454
+ if (options.interactive === false || !input.isTTY)
455
+ return null;
456
+ const rl = createInterface({ input, output: process.stdout, terminal: false });
457
+ rl.on("line", (line) => {
458
+ Promise.resolve(handler(line)).catch((error) => writeLine(`Operator command failed: ${error instanceof Error ? error.message : String(error)}`));
459
+ });
460
+ return { close: () => rl.close() };
461
+ }
462
+ };
463
+ }
464
+
465
+ // packages/cli/src/commands/_pi-frontend.ts
466
+ import { mkdtempSync, rmSync } from "fs";
467
+ import { tmpdir } from "os";
468
+ import { join } from "path";
469
+ import { main as runPiMain } from "@earendil-works/pi-coding-agent";
470
+
471
+ // packages/cli/src/commands/_pi-worker-bridge-extension.ts
472
+ var TERMINAL_RUN_STATUSES = new Set(["completed", "failed", "stopped", "cancelled", "canceled", "closed", "merged", "needs_attention", "needs-attention"]);
473
+ var MAX_TRANSCRIPT_LINES = 120;
474
+ function recordOf(value) {
475
+ return value && typeof value === "object" && !Array.isArray(value) ? value : null;
476
+ }
477
+ function asText(value) {
478
+ if (typeof value === "string")
479
+ return value;
480
+ if (value === null || value === undefined)
481
+ return "";
482
+ if (typeof value === "number" || typeof value === "boolean")
483
+ return String(value);
484
+ try {
485
+ return JSON.stringify(value);
486
+ } catch {
487
+ return String(value);
488
+ }
489
+ }
490
+ function textFromContent(content) {
491
+ if (typeof content === "string")
492
+ return content;
493
+ if (!Array.isArray(content))
494
+ return asText(content);
495
+ return content.flatMap((part) => {
496
+ const item = recordOf(part);
497
+ if (!item)
498
+ return [];
499
+ if (typeof item.text === "string")
500
+ return [item.text];
501
+ if (typeof item.content === "string")
502
+ return [item.content];
503
+ if (item.type === "toolCall")
504
+ return [`\u23FA ${String(item.name ?? "tool")} ${asText(item.arguments ?? "")}`.trim()];
505
+ if (item.type === "toolResult")
506
+ return [`\u21B3 ${asText(item.content ?? item.result ?? "")}`.trim()];
507
+ return [];
508
+ }).join(`
509
+ `);
510
+ }
511
+ function appendTranscript(state, label, text) {
512
+ const trimmed = text.trimEnd();
513
+ if (!trimmed)
514
+ return;
515
+ const lines = trimmed.split(/\r?\n/);
516
+ state.transcript.push(`${label}: ${lines[0] ?? ""}`);
517
+ for (const line of lines.slice(1))
518
+ state.transcript.push(` ${line}`);
519
+ if (state.transcript.length > MAX_TRANSCRIPT_LINES) {
520
+ state.transcript.splice(0, state.transcript.length - MAX_TRANSCRIPT_LINES);
521
+ }
522
+ }
523
+ function nativePiUi(ctx) {
524
+ const ui = ctx.ui;
525
+ return typeof ui.emitSessionEvent === "function" && typeof ui.appendSessionMessages === "function" ? ui : null;
526
+ }
527
+ function syncNativeDisplayCwd(ctx, state) {
528
+ const ui = nativePiUi(ctx);
529
+ if (ui?.setDisplayCwd && state.cwd)
530
+ ui.setDisplayCwd(state.cwd);
531
+ }
532
+ function parseExtensionUiRequest(value) {
533
+ const request = recordOf(value) ?? {};
534
+ const requestId = String(request.requestId ?? request.id ?? `ui-${Date.now()}`);
535
+ const method = String(request.method ?? request.type ?? "input");
536
+ const prompt = asText(request.prompt ?? request.message ?? request.title ?? method);
537
+ const rawOptions = Array.isArray(request.options) ? request.options : Array.isArray(request.items) ? request.items : [];
538
+ const options = rawOptions.map((option) => {
539
+ const record = recordOf(option);
540
+ return record ? asText(record.label ?? record.value ?? record.name ?? option) : asText(option);
541
+ }).filter(Boolean);
542
+ return { requestId, method, prompt, options };
543
+ }
544
+ function renderBridgeWidget(state) {
545
+ const statusParts = [
546
+ state.wsConnected ? "live WS" : "WS pending",
547
+ state.status,
548
+ state.model,
549
+ state.cwd
550
+ ].filter(Boolean);
551
+ const lines = [`Worker Pi daemon bridge \xB7 ${statusParts.join(" \xB7 ")}`];
552
+ if (state.activity)
553
+ lines.push(state.activity);
554
+ if (state.commands.length > 0) {
555
+ lines.push(`Worker commands: ${state.commands.slice(0, 10).join(", ")}${state.commands.length > 10 ? ", \u2026" : ""}`);
556
+ }
557
+ lines.push("");
558
+ if (state.transcript.length > 0) {
559
+ lines.push(...state.transcript.slice(-MAX_TRANSCRIPT_LINES));
560
+ } else {
561
+ lines.push("Waiting for worker Pi daemon transcript\u2026");
562
+ }
563
+ if (state.pendingUi) {
564
+ lines.push("");
565
+ lines.push(`Extension UI request \xB7 ${state.pendingUi.method}`);
566
+ lines.push(state.pendingUi.prompt);
567
+ state.pendingUi.options.forEach((option, index) => lines.push(`${index + 1}. ${option}`));
568
+ lines.push("Reply in the Pi editor. /cancel cancels this request.");
569
+ }
570
+ return lines;
571
+ }
572
+ function updatePiUi(ctx, state) {
573
+ ctx.ui.setTitle("Pi \xB7 Rig worker daemon");
574
+ ctx.ui.setStatus("rig-worker-pi", state.wsConnected ? "worker Pi WS live" : state.status);
575
+ syncNativeDisplayCwd(ctx, state);
576
+ if (state.nativeStream && nativePiUi(ctx)) {
577
+ ctx.ui.setWidget("rig-worker-pi-transcript", undefined);
578
+ return;
579
+ }
580
+ ctx.ui.setWorkingVisible(false);
581
+ ctx.ui.setWidget("rig-worker-pi-transcript", [`Worker Pi daemon bridge \xB7 degraded widget transcript`, ...renderBridgeWidget(state)], { placement: "aboveEditor" });
582
+ }
583
+ function applyStatus(state, payload) {
584
+ const status = recordOf(payload.status) ?? payload;
585
+ state.streaming = status.isStreaming === true || status.isCompacting === true || status.isBashRunning === true;
586
+ state.cwd = typeof status.cwd === "string" ? status.cwd : state.cwd;
587
+ state.model = typeof status.model === "string" ? status.model : state.model;
588
+ const pending = typeof status.pendingMessageCount === "number" ? status.pendingMessageCount : 0;
589
+ state.status = `${state.streaming ? "streaming" : "idle"}${pending ? ` \xB7 ${pending} queued` : ""}`;
590
+ }
591
+ function applyMessage(state, message) {
592
+ const record = recordOf(message);
593
+ if (!record)
594
+ return;
595
+ const role = String(record.role ?? "system");
596
+ const label = role === "assistant" ? "Pi" : role === "user" ? "You" : role === "tool" || role === "toolResult" ? "Tool" : "System";
597
+ appendTranscript(state, label, textFromContent(record.content ?? record.message ?? record.text ?? ""));
598
+ }
599
+ function applyPiEvent(ctx, state, eventValue) {
600
+ const event = recordOf(eventValue);
601
+ if (!event)
602
+ return;
603
+ const type = String(event.type ?? "event");
604
+ if (type === "agent_start") {
605
+ state.streaming = true;
606
+ state.status = "streaming";
607
+ } else if (type === "agent_end") {
608
+ state.streaming = false;
609
+ state.status = "idle";
610
+ } else if (type === "queue_update") {
611
+ const steering = Array.isArray(event.steering) ? event.steering.length : 0;
612
+ const followUp = Array.isArray(event.followUp) ? event.followUp.length : 0;
613
+ state.status = `queued \xB7 steer ${steering} \xB7 follow-up ${followUp}`;
614
+ }
615
+ const native = nativePiUi(ctx);
616
+ if (state.nativeStream && native?.emitSessionEvent) {
617
+ native.emitSessionEvent(eventValue);
618
+ return;
619
+ }
620
+ if (type === "agent_end") {
621
+ appendTranscript(state, "System", "Agent turn complete.");
622
+ return;
623
+ }
624
+ if (type === "message_start" || type === "message_end" || type === "turn_end") {
625
+ applyMessage(state, event.message);
626
+ return;
627
+ }
628
+ if (type === "message_update") {
629
+ const assistantEvent = recordOf(event.assistantMessageEvent);
630
+ const delta = typeof assistantEvent?.delta === "string" ? assistantEvent.delta : typeof assistantEvent?.text === "string" ? assistantEvent.text : "";
631
+ if (delta)
632
+ appendTranscript(state, assistantEvent?.type === "thinking_delta" ? "Thinking" : "Pi", delta);
633
+ return;
634
+ }
635
+ if (type === "tool_execution_start") {
636
+ appendTranscript(state, "Tool", `${String(event.toolName ?? "tool")} ${asText(event.args ?? "")}`.trim());
637
+ return;
638
+ }
639
+ if (type === "tool_execution_update") {
640
+ appendTranscript(state, "Tool", asText(event.partialResult ?? ""));
641
+ return;
642
+ }
643
+ if (type === "tool_execution_end") {
644
+ appendTranscript(state, event.isError === true ? "Error" : "Tool", asText(event.result ?? `${String(event.toolName ?? "tool")} complete`));
645
+ }
646
+ }
647
+ function firstPendingShell(state) {
648
+ return state.pendingShells[0];
649
+ }
650
+ function finishPendingShell(state, shell, result) {
651
+ const index = state.pendingShells.indexOf(shell);
652
+ if (index !== -1)
653
+ state.pendingShells.splice(index, 1);
654
+ shell.resolve(result);
655
+ }
656
+ function failPendingShell(state, shell, error) {
657
+ const index = state.pendingShells.indexOf(shell);
658
+ if (index !== -1)
659
+ state.pendingShells.splice(index, 1);
660
+ shell.reject(error);
661
+ }
662
+ function applyUiEvent(state, value) {
663
+ const event = recordOf(value);
664
+ if (!event)
665
+ return;
666
+ const type = String(event.type ?? "ui");
667
+ if (type === "shell.chunk") {
668
+ const pending = firstPendingShell(state);
669
+ const chunk = asText(event.chunk);
670
+ if (pending) {
671
+ pending.sawChunk = true;
672
+ pending.onData(Buffer.from(chunk));
673
+ } else {
674
+ appendTranscript(state, "Tool", chunk);
675
+ }
676
+ return;
677
+ }
678
+ if (type === "shell.end") {
679
+ const pending = firstPendingShell(state);
680
+ const output = asText(event.output ?? "");
681
+ const exitCode = typeof event.exitCode === "number" ? event.exitCode : event.isError === true ? 1 : 0;
682
+ if (pending) {
683
+ if (output && !pending.sawChunk)
684
+ pending.onData(Buffer.from(output));
685
+ finishPendingShell(state, pending, { exitCode });
686
+ } else {
687
+ appendTranscript(state, event.isError === true ? "Error" : "Tool", output || `exit ${String(exitCode)}`);
688
+ }
689
+ return;
690
+ }
691
+ if (type === "shell.start") {
692
+ if (!firstPendingShell(state))
693
+ appendTranscript(state, "Tool", `$ ${asText(event.command)}`);
694
+ return;
695
+ }
696
+ appendTranscript(state, "System", `${type}: ${asText(event)}`);
697
+ }
698
+ function applyEnvelope(ctx, state, envelopeValue) {
699
+ const envelope = recordOf(envelopeValue);
700
+ if (!envelope)
701
+ return;
702
+ const type = String(envelope.type ?? "");
703
+ if (type === "ready") {
704
+ const metadata = recordOf(envelope.metadata);
705
+ state.cwd = typeof metadata?.cwd === "string" ? metadata.cwd : state.cwd;
706
+ state.status = "worker Pi daemon ready";
707
+ if (!state.nativeStream)
708
+ appendTranscript(state, "System", "Connected to worker Pi daemon.");
709
+ } else if (type === "status.update") {
710
+ applyStatus(state, envelope);
711
+ } else if (type === "activity.update") {
712
+ const activity = recordOf(envelope.activity);
713
+ state.activity = [activity?.label, activity?.detail].map(asText).filter(Boolean).join(" \u2014 ");
714
+ } else if (type === "extension_ui_request") {
715
+ state.pendingUi = parseExtensionUiRequest(envelope.request);
716
+ appendTranscript(state, "System", `Extension UI request: ${state.pendingUi.prompt}`);
717
+ } else if (type === "pi.ui_event") {
718
+ applyUiEvent(state, envelope.event);
719
+ } else if (type === "pi.event") {
720
+ applyPiEvent(ctx, state, envelope.event);
721
+ } else if (type === "error") {
722
+ appendTranscript(state, "Error", asText(envelope.message ?? envelope.detail ?? "unknown error"));
723
+ }
724
+ syncNativeDisplayCwd(ctx, state);
725
+ }
726
+ async function waitForWorkerReady(options, ctx, state) {
727
+ while (true) {
728
+ const session = await getRunPiSessionViaServer(options.context, options.runId).catch((error) => ({
729
+ ready: false,
730
+ status: error instanceof Error ? error.message : String(error),
731
+ retryAfterMs: 1000
732
+ }));
733
+ if (session.ready === false) {
734
+ const status = String(session.status ?? "starting");
735
+ state.status = `waiting for worker Pi daemon \xB7 ${status}`;
736
+ updatePiUi(ctx, state);
737
+ if (TERMINAL_RUN_STATUSES.has(status.toLowerCase())) {
738
+ appendTranscript(state, "Error", `Run ended before worker Pi daemon became ready: ${status}`);
739
+ return false;
740
+ }
741
+ await Bun.sleep(typeof session.retryAfterMs === "number" ? session.retryAfterMs : 750);
742
+ continue;
743
+ }
744
+ const sessionRecord = recordOf(session) ?? {};
745
+ applyEnvelope(ctx, state, { type: "ready", metadata: sessionRecord.metadata ?? sessionRecord });
746
+ updatePiUi(ctx, state);
747
+ return true;
748
+ }
749
+ }
750
+ function parseWsPayload(message) {
751
+ if (typeof message.data === "string")
752
+ return JSON.parse(message.data);
753
+ return JSON.parse(Buffer.from(message.data).toString("utf8"));
754
+ }
755
+ async function connectWorkerStream(options, ctx, state) {
756
+ const ready = await waitForWorkerReady(options, ctx, state);
757
+ if (!ready)
758
+ return;
759
+ let catchupDone = false;
760
+ const buffered = [];
761
+ const wsUrl = await buildRunPiEventsWebSocketUrl(options.context, options.runId);
762
+ const socket = new WebSocket(wsUrl);
763
+ const closePromise = new Promise((resolve3) => {
764
+ socket.onopen = () => {
765
+ state.wsConnected = true;
766
+ state.status = "live worker Pi WebSocket connected";
767
+ updatePiUi(ctx, state);
768
+ };
769
+ socket.onmessage = (message) => {
770
+ try {
771
+ const payload = parseWsPayload(message);
772
+ if (!catchupDone)
773
+ buffered.push(payload);
774
+ else {
775
+ applyEnvelope(ctx, state, payload);
776
+ updatePiUi(ctx, state);
777
+ }
778
+ } catch (error) {
779
+ appendTranscript(state, "Error", `Unparseable worker Pi event: ${error instanceof Error ? error.message : String(error)}`);
780
+ updatePiUi(ctx, state);
781
+ }
782
+ };
783
+ socket.onerror = () => socket.close();
784
+ socket.onclose = () => {
785
+ state.wsConnected = false;
786
+ state.status = "worker Pi WebSocket disconnected";
787
+ updatePiUi(ctx, state);
788
+ resolve3();
789
+ };
790
+ });
791
+ try {
792
+ const [messagesPayload, statusPayload, commandsPayload] = await Promise.all([
793
+ getRunPiMessagesViaServer(options.context, options.runId),
794
+ getRunPiStatusViaServer(options.context, options.runId),
795
+ getRunPiCommandsViaServer(options.context, options.runId)
796
+ ]);
797
+ const messages = Array.isArray(messagesPayload.messages) ? messagesPayload.messages : [];
798
+ const native = nativePiUi(ctx);
799
+ if (state.nativeStream && native?.appendSessionMessages)
800
+ native.appendSessionMessages(messages);
801
+ else
802
+ for (const message of messages)
803
+ applyMessage(state, message);
804
+ applyStatus(state, statusPayload);
805
+ const commands = Array.isArray(commandsPayload.commands) ? commandsPayload.commands : [];
806
+ state.commands = commands.flatMap((command) => {
807
+ const record = recordOf(command);
808
+ return typeof record?.name === "string" ? [`/${record.name}`] : [];
809
+ });
810
+ catchupDone = true;
811
+ for (const payload of buffered.splice(0))
812
+ applyEnvelope(ctx, state, payload);
813
+ updatePiUi(ctx, state);
814
+ } catch (error) {
815
+ appendTranscript(state, "Error", `Worker Pi catch-up failed: ${error instanceof Error ? error.message : String(error)}`);
816
+ catchupDone = true;
817
+ updatePiUi(ctx, state);
818
+ }
819
+ await closePromise;
820
+ }
821
+ function createRemoteBashOperations(options, state, excludeFromContext) {
822
+ return {
823
+ exec(command, _cwd, execOptions) {
824
+ return new Promise((resolve3, reject) => {
825
+ const pending = {
826
+ command,
827
+ onData: execOptions.onData,
828
+ resolve: resolve3,
829
+ reject,
830
+ sawChunk: false
831
+ };
832
+ const cleanup = () => {
833
+ execOptions.signal?.removeEventListener("abort", onAbort);
834
+ if (timer)
835
+ clearTimeout(timer);
836
+ };
837
+ const onAbort = () => {
838
+ cleanup();
839
+ failPendingShell(state, pending, new Error("Remote worker shell command aborted locally."));
840
+ };
841
+ const timeoutMs = typeof execOptions.timeout === "number" && execOptions.timeout > 0 ? execOptions.timeout : 0;
842
+ const timer = timeoutMs > 0 ? setTimeout(() => {
843
+ cleanup();
844
+ failPendingShell(state, pending, new Error(`Remote worker shell command timed out after ${timeoutMs}ms.`));
845
+ }, timeoutMs) : null;
846
+ const wrappedResolve = pending.resolve;
847
+ const wrappedReject = pending.reject;
848
+ pending.resolve = (result) => {
849
+ cleanup();
850
+ wrappedResolve(result);
851
+ };
852
+ pending.reject = (error) => {
853
+ cleanup();
854
+ wrappedReject(error);
855
+ };
856
+ execOptions.signal?.addEventListener("abort", onAbort, { once: true });
857
+ state.pendingShells.push(pending);
858
+ sendRunPiShellViaServer(options.context, options.runId, `${excludeFromContext ? "!!" : "!"}${command}`).catch((error) => {
859
+ cleanup();
860
+ failPendingShell(state, pending, error instanceof Error ? error : new Error(String(error)));
861
+ });
862
+ });
863
+ }
864
+ };
865
+ }
866
+ async function answerPendingUi(options, state, line) {
867
+ const pending = state.pendingUi;
868
+ if (!pending)
869
+ return false;
870
+ if (line === "/cancel") {
871
+ await respondRunPiExtensionUiViaServer(options.context, options.runId, pending.requestId, { cancelled: true });
872
+ } else if (pending.method === "confirm") {
873
+ const confirmed = /^(y|yes|true|1)$/i.test(line);
874
+ await respondRunPiExtensionUiViaServer(options.context, options.runId, pending.requestId, { value: confirmed, confirmed });
875
+ } else if (pending.options.length > 0 && /^\d+$/.test(line)) {
876
+ const selected = pending.options[Math.max(0, Number(line) - 1)] ?? line;
877
+ await respondRunPiExtensionUiViaServer(options.context, options.runId, pending.requestId, { value: selected });
878
+ } else {
879
+ await respondRunPiExtensionUiViaServer(options.context, options.runId, pending.requestId, { value: line });
880
+ }
881
+ appendTranscript(state, "System", `Responded to extension UI request ${pending.requestId}.`);
882
+ state.pendingUi = null;
883
+ return true;
884
+ }
885
+ async function routeInput(options, ctx, state, line) {
886
+ const text = line.trim();
887
+ if (!text)
888
+ return;
889
+ if (await answerPendingUi(options, state, text)) {
890
+ updatePiUi(ctx, state);
891
+ return;
892
+ }
893
+ if (text === "/detach" || text === "/quit" || text === "/q") {
894
+ appendTranscript(state, "System", "Detached locally; worker Pi daemon continues.");
895
+ updatePiUi(ctx, state);
896
+ ctx.shutdown();
897
+ return;
898
+ }
899
+ if (text === "/stop") {
900
+ await abortRunPiViaServer(options.context, options.runId);
901
+ appendTranscript(state, "System", "Stop requested for worker Pi daemon.");
902
+ updatePiUi(ctx, state);
903
+ ctx.shutdown();
904
+ return;
905
+ }
906
+ if (text.startsWith("!")) {
907
+ appendTranscript(state, "You", text);
908
+ await sendRunPiShellViaServer(options.context, options.runId, text);
909
+ } else if (text.startsWith("/")) {
910
+ appendTranscript(state, "You", text);
911
+ const result = await runRunPiCommandViaServer(options.context, options.runId, text);
912
+ const message = typeof result.message === "string" ? result.message : "worker command accepted";
913
+ appendTranscript(state, "System", message);
914
+ } else {
915
+ appendTranscript(state, "You", text);
916
+ await sendRunPiPromptViaServer(options.context, options.runId, text, state.streaming ? "steer" : undefined);
917
+ }
918
+ updatePiUi(ctx, state);
919
+ }
920
+ function createRigWorkerPiBridgeExtension(options) {
921
+ return (pi) => {
922
+ const state = {
923
+ transcript: [],
924
+ status: "starting worker Pi daemon bridge",
925
+ activity: "",
926
+ cwd: "",
927
+ model: "",
928
+ commands: [],
929
+ streaming: false,
930
+ pendingUi: null,
931
+ pendingShells: [],
932
+ wsConnected: false,
933
+ nativeStream: false
934
+ };
935
+ if (options.initialMessageSent)
936
+ appendTranscript(state, "System", "Initial message sent to worker Pi daemon.");
937
+ let nativePiUiContextAvailable = false;
938
+ pi.on("user_bash", (event) => {
939
+ state.nativeStream = Boolean(state.nativeStream || nativePiUiContextAvailable);
940
+ return { operations: createRemoteBashOperations(options, state, event.excludeFromContext === true) };
941
+ });
942
+ pi.on("session_start", async (_event, ctx) => {
943
+ nativePiUiContextAvailable = Boolean(nativePiUi(ctx));
944
+ state.nativeStream = nativePiUiContextAvailable;
945
+ updatePiUi(ctx, state);
946
+ ctx.ui.notify(nativePiUiContextAvailable ? "Rig worker Pi native stream bridge loaded" : "Rig worker Pi bridge extension loaded (degraded widget fallback)", "info");
947
+ ctx.ui.onTerminalInput((data) => {
948
+ if (data.includes("\x04")) {
949
+ ctx.shutdown();
950
+ return { consume: true };
951
+ }
952
+ if (!data.includes("\r") && !data.includes(`
953
+ `))
954
+ return;
955
+ const inlineText = data.replace(/[\r\n]+/g, "").trim();
956
+ const editorText = ctx.ui.getEditorText().trim();
957
+ const text = [editorText, inlineText].filter(Boolean).join(" ").trim();
958
+ if (!text)
959
+ return;
960
+ if (text.startsWith("!"))
961
+ return;
962
+ ctx.ui.setEditorText("");
963
+ routeInput(options, ctx, state, text).catch((error) => {
964
+ appendTranscript(state, "Error", error instanceof Error ? error.message : String(error));
965
+ updatePiUi(ctx, state);
966
+ });
967
+ return { consume: true };
968
+ });
969
+ connectWorkerStream(options, ctx, state).catch((error) => {
970
+ appendTranscript(state, "Error", error instanceof Error ? error.message : String(error));
971
+ updatePiUi(ctx, state);
972
+ });
973
+ });
974
+ pi.on("session_shutdown", () => {});
975
+ };
976
+ }
977
+
978
+ // packages/cli/src/commands/_pi-frontend.ts
979
+ function setTemporaryEnv(updates) {
980
+ const previous = new Map;
981
+ for (const [key, value] of Object.entries(updates)) {
982
+ previous.set(key, process.env[key]);
983
+ process.env[key] = value;
984
+ }
985
+ return () => {
986
+ for (const [key, value] of previous) {
987
+ if (value === undefined)
988
+ delete process.env[key];
989
+ else
990
+ process.env[key] = value;
991
+ }
992
+ };
993
+ }
994
+ async function attachRunBundledPiFrontend(context, input) {
995
+ const tempRoot = mkdtempSync(join(tmpdir(), "rig-pi-frontend-"));
996
+ const cwd = join(tempRoot, "workspace");
997
+ const agentDir = join(tempRoot, "agent");
998
+ const sessionDir = join(tempRoot, "sessions");
999
+ const previousCwd = process.cwd();
1000
+ const restoreEnv = setTemporaryEnv({
1001
+ PI_CODING_AGENT_DIR: agentDir,
1002
+ PI_CODING_AGENT_SESSION_DIR: sessionDir,
1003
+ PI_OFFLINE: "1",
1004
+ PI_SKIP_VERSION_CHECK: "1"
1005
+ });
1006
+ let detached = false;
1007
+ try {
1008
+ await Bun.$`mkdir -p ${cwd} ${agentDir} ${sessionDir}`.quiet();
1009
+ process.chdir(cwd);
1010
+ await runPiMain([
1011
+ "--offline",
1012
+ "--no-session",
1013
+ "--no-tools",
1014
+ "--no-builtin-tools",
1015
+ "--no-skills",
1016
+ "--no-prompt-templates",
1017
+ "--no-themes",
1018
+ "--no-context-files",
1019
+ "--no-approve"
1020
+ ], {
1021
+ extensionFactories: [
1022
+ createRigWorkerPiBridgeExtension({
1023
+ context,
1024
+ runId: input.runId,
1025
+ initialMessageSent: input.steered === true
1026
+ })
1027
+ ]
1028
+ });
1029
+ detached = true;
1030
+ } finally {
1031
+ process.chdir(previousCwd);
1032
+ restoreEnv();
1033
+ rmSync(tempRoot, { recursive: true, force: true });
1034
+ }
1035
+ let run = { runId: input.runId, status: "unknown" };
1036
+ try {
1037
+ run = await getRunDetailsViaServer(context, input.runId);
1038
+ } catch {}
1039
+ return {
1040
+ run,
1041
+ logs: [],
1042
+ timeline: [],
1043
+ timelineCursor: null,
1044
+ steered: input.steered === true,
1045
+ detached,
1046
+ rendered: "actual bundled Pi frontend hosted Rig worker Pi bridge extension"
1047
+ };
1048
+ }
1049
+
1050
+ // packages/cli/src/commands/_operator-view.ts
1051
+ var TERMINAL_RUN_STATUSES2 = new Set(["completed", "failed", "stopped", "cancelled", "canceled", "closed", "merged", "needs_attention", "needs-attention"]);
266
1052
  function runStatusFromPayload(payload) {
267
1053
  const run = payload.run && typeof payload.run === "object" && !Array.isArray(payload.run) ? payload.run : payload;
268
1054
  return String(run.status ?? "unknown").toLowerCase();
@@ -284,57 +1070,76 @@ async function applyOperatorCommand(context, input, deps = {}) {
284
1070
  await (deps.steer ?? steerRunViaServer)(context, input.runId, userMessage);
285
1071
  return { action: "continue", message: "Steering message queued." };
286
1072
  }
287
- async function readOperatorSnapshot(context, runId) {
1073
+ async function readOperatorSnapshot(context, runId, options = {}) {
288
1074
  const run = await getRunDetailsViaServer(context, runId);
289
1075
  const logsPage = await getRunLogsViaServer(context, runId, { limit: 100 });
290
- const entries = Array.isArray(logsPage.entries) ? logsPage.entries.filter((entry) => Boolean(entry && typeof entry === "object" && !Array.isArray(entry))) : [];
291
- return { run, logs: entries, rendered: renderOperatorSnapshot({ run, logs: entries }) };
1076
+ const timelinePage = await getRunTimelineViaServer(context, runId, { limit: 200, ...options.timelineCursor ? { cursor: options.timelineCursor } : {} }).catch((error) => ({
1077
+ entries: [{
1078
+ id: `timeline-unavailable:${runId}`,
1079
+ type: "timeline_warning",
1080
+ detail: `Selected Rig server did not provide run timeline events: ${error instanceof Error ? error.message : String(error)}`,
1081
+ createdAt: new Date().toISOString()
1082
+ }],
1083
+ nextCursor: options.timelineCursor ?? null
1084
+ }));
1085
+ const logs = Array.isArray(logsPage.entries) ? logsPage.entries.filter((entry) => Boolean(entry && typeof entry === "object" && !Array.isArray(entry))).toReversed() : [];
1086
+ const timeline = Array.isArray(timelinePage.entries) ? timelinePage.entries.filter((entry) => Boolean(entry && typeof entry === "object" && !Array.isArray(entry))) : [];
1087
+ const timelineCursor = typeof timelinePage.nextCursor === "string" ? timelinePage.nextCursor : options.timelineCursor ?? null;
1088
+ return { run, logs, timeline, timelineCursor, rendered: renderOperatorSnapshot({ run, logs, timeline }) };
292
1089
  }
293
1090
  async function attachRunOperatorView(context, input) {
294
1091
  let steered = false;
295
1092
  if (input.message?.trim()) {
296
- await steerRunViaServer(context, input.runId, input.message.trim());
1093
+ await sendRunPiPromptViaServer(context, input.runId, input.message.trim(), "steer").catch(() => steerRunViaServer(context, input.runId, input.message.trim()));
297
1094
  steered = true;
298
1095
  }
1096
+ if (input.follow && !input.once && input.interactive !== false && context.outputMode === "text" && Boolean(process.stdin.isTTY && process.stdout.isTTY)) {
1097
+ return attachRunBundledPiFrontend(context, {
1098
+ runId: input.runId,
1099
+ steered
1100
+ });
1101
+ }
1102
+ const surface = createOperatorSurface({ interactive: input.interactive !== false });
299
1103
  let snapshot = await readOperatorSnapshot(context, input.runId);
300
1104
  if (context.outputMode === "text") {
301
- console.log(snapshot.rendered);
1105
+ surface.renderSnapshot(snapshot);
1106
+ surface.renderTimeline(snapshot.timeline);
1107
+ surface.renderLogs(snapshot.logs);
302
1108
  if (steered)
303
- console.log("Steering message queued.");
1109
+ surface.info("Message submitted to worker Pi.");
304
1110
  }
305
1111
  let detached = false;
306
- let rl = null;
1112
+ let commandInput = null;
307
1113
  if (input.follow && !input.once && context.outputMode === "text") {
308
1114
  if (input.interactive !== false && process.stdin.isTTY) {
309
- console.log("Controls: /user <message>, /stop, /detach");
310
- rl = createInterface({ input: process.stdin, output: process.stdout, terminal: false });
311
- rl.on("line", (line) => {
312
- applyOperatorCommand(context, { runId: input.runId, line }).then((result) => {
313
- if (result.message)
314
- console.log(result.message);
315
- if (result.action === "detach" || result.action === "stopped") {
316
- detached = true;
317
- rl?.close();
318
- }
319
- }).catch((error) => console.log(`Operator command failed: ${error instanceof Error ? error.message : String(error)}`));
1115
+ surface.info("Controls: /user <message>, /stop, /detach");
1116
+ commandInput = surface.attachCommandInput(async (line) => {
1117
+ const result = await applyOperatorCommand(context, { runId: input.runId, line });
1118
+ if (result.message)
1119
+ surface.info(result.message);
1120
+ if (result.action === "detach" || result.action === "stopped") {
1121
+ detached = true;
1122
+ commandInput?.close();
1123
+ }
320
1124
  });
321
1125
  }
322
- let lastRendered = snapshot.rendered;
323
1126
  const pollMs = Math.max(250, Math.trunc(input.pollMs ?? 2000));
324
- while (!detached && !TERMINAL_RUN_STATUSES.has(runStatusFromPayload(snapshot.run))) {
1127
+ let timelineCursor = snapshot.timelineCursor;
1128
+ while (!detached && !TERMINAL_RUN_STATUSES2.has(runStatusFromPayload(snapshot.run))) {
325
1129
  await Bun.sleep(pollMs);
326
- snapshot = await readOperatorSnapshot(context, input.runId);
327
- if (snapshot.rendered !== lastRendered) {
328
- console.log(snapshot.rendered);
329
- lastRendered = snapshot.rendered;
330
- }
1130
+ snapshot = await readOperatorSnapshot(context, input.runId, { timelineCursor });
1131
+ timelineCursor = snapshot.timelineCursor;
1132
+ surface.renderSnapshot(snapshot);
1133
+ surface.renderTimeline(snapshot.timeline);
1134
+ surface.renderLogs(snapshot.logs);
331
1135
  }
332
- rl?.close();
1136
+ commandInput?.close();
333
1137
  }
334
1138
  return { ...snapshot, steered, detached };
335
1139
  }
336
1140
  export {
337
1141
  renderOperatorSnapshot,
1142
+ createPiRunStreamRenderer,
338
1143
  attachRunOperatorView,
339
1144
  applyOperatorCommand
340
1145
  };