@h-rig/cli 0.0.6-alpha.40 → 0.0.6-alpha.42
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 +512 -551
- package/dist/src/commands/_operator-view.js +510 -530
- package/dist/src/commands/_pi-frontend.js +514 -534
- package/dist/src/commands/_pi-remote-session.js +671 -0
- package/dist/src/commands/_pi-worker-bridge-extension.js +352 -502
- package/dist/src/commands/_run-driver-helpers.js +2 -21
- package/dist/src/commands/run.js +510 -530
- package/dist/src/commands/task-run-driver.js +2 -21
- package/dist/src/commands/task.js +510 -530
- package/dist/src/commands.js +512 -551
- package/dist/src/index.js +512 -551
- package/package.json +6 -6
|
@@ -248,433 +248,272 @@ async function buildRunPiEventsWebSocketUrl(context, runId) {
|
|
|
248
248
|
return url.toString();
|
|
249
249
|
}
|
|
250
250
|
|
|
251
|
-
// packages/cli/src/commands/_pi-
|
|
251
|
+
// packages/cli/src/commands/_pi-remote-session.ts
|
|
252
|
+
import { AgentSession as PiAgentSession } from "@earendil-works/pi-coding-agent";
|
|
252
253
|
var TERMINAL_RUN_STATUSES = new Set(["completed", "failed", "stopped", "cancelled", "canceled", "closed", "merged", "needs_attention", "needs-attention"]);
|
|
253
|
-
|
|
254
|
+
function defaultTransport() {
|
|
255
|
+
return {
|
|
256
|
+
getSession: getRunPiSessionViaServer,
|
|
257
|
+
getMessages: getRunPiMessagesViaServer,
|
|
258
|
+
getStatus: getRunPiStatusViaServer,
|
|
259
|
+
getCommands: getRunPiCommandsViaServer,
|
|
260
|
+
sendPrompt: sendRunPiPromptViaServer,
|
|
261
|
+
sendShell: sendRunPiShellViaServer,
|
|
262
|
+
runCommand: runRunPiCommandViaServer,
|
|
263
|
+
abort: abortRunPiViaServer,
|
|
264
|
+
buildEventsWebSocketUrl: buildRunPiEventsWebSocketUrl
|
|
265
|
+
};
|
|
266
|
+
}
|
|
254
267
|
function recordOf(value) {
|
|
255
268
|
return value && typeof value === "object" && !Array.isArray(value) ? value : null;
|
|
256
269
|
}
|
|
257
|
-
function
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
return content;
|
|
273
|
-
if (!Array.isArray(content))
|
|
274
|
-
return asText(content);
|
|
275
|
-
return content.flatMap((part) => {
|
|
276
|
-
const item = recordOf(part);
|
|
277
|
-
if (!item)
|
|
278
|
-
return [];
|
|
279
|
-
if (typeof item.text === "string")
|
|
280
|
-
return [item.text];
|
|
281
|
-
if (typeof item.content === "string")
|
|
282
|
-
return [item.content];
|
|
283
|
-
if (item.type === "toolCall")
|
|
284
|
-
return [`\u23FA ${String(item.name ?? "tool")} ${asText(item.arguments ?? "")}`.trim()];
|
|
285
|
-
if (item.type === "toolResult")
|
|
286
|
-
return [`\u21B3 ${asText(item.content ?? item.result ?? "")}`.trim()];
|
|
287
|
-
return [];
|
|
288
|
-
}).join(`
|
|
289
|
-
`);
|
|
290
|
-
}
|
|
291
|
-
function appendTranscript(state, label, text) {
|
|
292
|
-
const trimmed = text.trimEnd();
|
|
293
|
-
if (!trimmed)
|
|
294
|
-
return;
|
|
295
|
-
const lines = trimmed.split(/\r?\n/);
|
|
296
|
-
state.transcript.push(`${label}: ${lines[0] ?? ""}`);
|
|
297
|
-
for (const line of lines.slice(1))
|
|
298
|
-
state.transcript.push(` ${line}`);
|
|
299
|
-
if (state.transcript.length > MAX_TRANSCRIPT_LINES) {
|
|
300
|
-
state.transcript.splice(0, state.transcript.length - MAX_TRANSCRIPT_LINES);
|
|
301
|
-
}
|
|
302
|
-
}
|
|
303
|
-
function nativePiUi(ctx) {
|
|
304
|
-
const ui = ctx.ui;
|
|
305
|
-
return typeof ui.emitSessionEvent === "function" && typeof ui.appendSessionMessages === "function" ? ui : null;
|
|
306
|
-
}
|
|
307
|
-
function syncNativeDisplayCwd(ctx, state) {
|
|
308
|
-
const ui = nativePiUi(ctx);
|
|
309
|
-
if (ui?.setDisplayCwd && state.cwd)
|
|
310
|
-
ui.setDisplayCwd(state.cwd);
|
|
311
|
-
}
|
|
312
|
-
function parseExtensionUiRequest(value) {
|
|
313
|
-
const request = recordOf(value) ?? {};
|
|
314
|
-
const requestId = String(request.requestId ?? request.id ?? `ui-${Date.now()}`);
|
|
315
|
-
const method = String(request.method ?? request.type ?? "input");
|
|
316
|
-
const prompt = asText(request.prompt ?? request.message ?? request.title ?? method);
|
|
317
|
-
const rawOptions = Array.isArray(request.options) ? request.options : Array.isArray(request.items) ? request.items : [];
|
|
318
|
-
const options = rawOptions.map((option) => {
|
|
319
|
-
const record = recordOf(option);
|
|
320
|
-
return record ? asText(record.label ?? record.value ?? record.name ?? option) : asText(option);
|
|
321
|
-
}).filter(Boolean);
|
|
322
|
-
return { requestId, method, prompt, options };
|
|
323
|
-
}
|
|
324
|
-
function renderBridgeWidget(state) {
|
|
325
|
-
const statusParts = [
|
|
326
|
-
state.wsConnected ? "live" : "connecting",
|
|
327
|
-
state.status,
|
|
328
|
-
state.model,
|
|
329
|
-
state.cwd
|
|
330
|
-
].filter(Boolean);
|
|
331
|
-
const lines = [`Rig worker session \xB7 ${statusParts.join(" \xB7 ")}`];
|
|
332
|
-
if (state.activity)
|
|
333
|
-
lines.push(state.activity);
|
|
334
|
-
if (state.commands.length > 0) {
|
|
335
|
-
lines.push(`Worker commands: ${state.commands.slice(0, 10).join(", ")}${state.commands.length > 10 ? ", \u2026" : ""}`);
|
|
336
|
-
}
|
|
337
|
-
lines.push("");
|
|
338
|
-
if (state.transcript.length > 0) {
|
|
339
|
-
lines.push(...state.transcript.slice(-MAX_TRANSCRIPT_LINES));
|
|
340
|
-
} else {
|
|
341
|
-
lines.push("Waiting for the worker session transcript\u2026 (/detach exits and leaves the worker running)");
|
|
342
|
-
}
|
|
343
|
-
if (state.pendingUi) {
|
|
344
|
-
lines.push("");
|
|
345
|
-
lines.push(`Worker needs input \xB7 ${state.pendingUi.method}`);
|
|
346
|
-
lines.push(state.pendingUi.prompt);
|
|
347
|
-
state.pendingUi.options.forEach((option, index) => lines.push(`${index + 1}. ${option}`));
|
|
348
|
-
lines.push("Reply in the editor below. /cancel dismisses this request.");
|
|
349
|
-
}
|
|
350
|
-
return lines;
|
|
351
|
-
}
|
|
352
|
-
function reportBridgeError(ctx, state, message) {
|
|
353
|
-
appendTranscript(state, "Error", message);
|
|
354
|
-
state.status = message;
|
|
355
|
-
try {
|
|
356
|
-
ctx.ui.notify(message, "error");
|
|
357
|
-
} catch {}
|
|
358
|
-
updatePiUi(ctx, state);
|
|
359
|
-
}
|
|
360
|
-
function updatePiUi(ctx, state) {
|
|
361
|
-
ctx.ui.setTitle("Rig \xB7 worker session");
|
|
362
|
-
ctx.ui.setStatus("rig-worker-pi", state.wsConnected ? "worker session live" : state.status);
|
|
363
|
-
syncNativeDisplayCwd(ctx, state);
|
|
364
|
-
if (state.nativeStream && nativePiUi(ctx)) {
|
|
365
|
-
ctx.ui.setWidget("rig-worker-pi-transcript", undefined);
|
|
366
|
-
return;
|
|
367
|
-
}
|
|
368
|
-
ctx.ui.setWorkingVisible(false);
|
|
369
|
-
ctx.ui.setWidget("rig-worker-pi-transcript", renderBridgeWidget(state), { placement: "aboveEditor" });
|
|
370
|
-
}
|
|
371
|
-
function applyStatus(state, payload) {
|
|
372
|
-
const status = recordOf(payload.status) ?? payload;
|
|
373
|
-
state.streaming = status.isStreaming === true || status.isCompacting === true || status.isBashRunning === true;
|
|
374
|
-
state.cwd = typeof status.cwd === "string" ? status.cwd : state.cwd;
|
|
375
|
-
state.model = typeof status.model === "string" ? status.model : state.model;
|
|
376
|
-
const pending = typeof status.pendingMessageCount === "number" ? status.pendingMessageCount : 0;
|
|
377
|
-
state.status = `${state.streaming ? "streaming" : "idle"}${pending ? ` \xB7 ${pending} queued` : ""}`;
|
|
378
|
-
}
|
|
379
|
-
function applyMessage(state, message) {
|
|
380
|
-
const record = recordOf(message);
|
|
381
|
-
if (!record)
|
|
382
|
-
return;
|
|
383
|
-
const role = String(record.role ?? "system");
|
|
384
|
-
const label = role === "assistant" ? "Pi" : role === "user" ? "You" : role === "tool" || role === "toolResult" ? "Tool" : "System";
|
|
385
|
-
appendTranscript(state, label, textFromContent(record.content ?? record.message ?? record.text ?? ""));
|
|
386
|
-
}
|
|
387
|
-
function applyPiEvent(ctx, state, eventValue) {
|
|
388
|
-
const event = recordOf(eventValue);
|
|
389
|
-
if (!event)
|
|
390
|
-
return;
|
|
391
|
-
const type = String(event.type ?? "event");
|
|
392
|
-
if (type === "agent_start") {
|
|
393
|
-
state.streaming = true;
|
|
394
|
-
state.status = "streaming";
|
|
395
|
-
} else if (type === "agent_end") {
|
|
396
|
-
state.streaming = false;
|
|
397
|
-
state.status = "idle";
|
|
398
|
-
} else if (type === "queue_update") {
|
|
399
|
-
const steering = Array.isArray(event.steering) ? event.steering.length : 0;
|
|
400
|
-
const followUp = Array.isArray(event.followUp) ? event.followUp.length : 0;
|
|
401
|
-
state.status = `queued \xB7 steer ${steering} \xB7 follow-up ${followUp}`;
|
|
402
|
-
}
|
|
403
|
-
const native = nativePiUi(ctx);
|
|
404
|
-
if (state.nativeStream && native?.emitSessionEvent) {
|
|
405
|
-
native.emitSessionEvent(eventValue);
|
|
406
|
-
return;
|
|
407
|
-
}
|
|
408
|
-
if (type === "agent_end") {
|
|
409
|
-
appendTranscript(state, "System", "Agent turn complete.");
|
|
410
|
-
return;
|
|
411
|
-
}
|
|
412
|
-
if (type === "message_start" || type === "message_end" || type === "turn_end") {
|
|
413
|
-
applyMessage(state, event.message);
|
|
414
|
-
return;
|
|
415
|
-
}
|
|
416
|
-
if (type === "message_update") {
|
|
417
|
-
const assistantEvent = recordOf(event.assistantMessageEvent);
|
|
418
|
-
const delta = typeof assistantEvent?.delta === "string" ? assistantEvent.delta : typeof assistantEvent?.text === "string" ? assistantEvent.text : "";
|
|
419
|
-
if (delta)
|
|
420
|
-
appendTranscript(state, assistantEvent?.type === "thinking_delta" ? "Thinking" : "Pi", delta);
|
|
421
|
-
return;
|
|
422
|
-
}
|
|
423
|
-
if (type === "tool_execution_start") {
|
|
424
|
-
appendTranscript(state, "Tool", `${String(event.toolName ?? "tool")} ${asText(event.args ?? "")}`.trim());
|
|
425
|
-
return;
|
|
426
|
-
}
|
|
427
|
-
if (type === "tool_execution_update") {
|
|
428
|
-
appendTranscript(state, "Tool", asText(event.partialResult ?? ""));
|
|
429
|
-
return;
|
|
430
|
-
}
|
|
431
|
-
if (type === "tool_execution_end") {
|
|
432
|
-
appendTranscript(state, event.isError === true ? "Error" : "Tool", asText(event.result ?? `${String(event.toolName ?? "tool")} complete`));
|
|
433
|
-
}
|
|
434
|
-
}
|
|
435
|
-
function firstPendingShell(state) {
|
|
436
|
-
return state.pendingShells[0];
|
|
437
|
-
}
|
|
438
|
-
function finishPendingShell(state, shell, result) {
|
|
439
|
-
const index = state.pendingShells.indexOf(shell);
|
|
440
|
-
if (index !== -1)
|
|
441
|
-
state.pendingShells.splice(index, 1);
|
|
442
|
-
shell.resolve(result);
|
|
443
|
-
}
|
|
444
|
-
function failPendingShell(state, shell, error) {
|
|
445
|
-
const index = state.pendingShells.indexOf(shell);
|
|
446
|
-
if (index !== -1)
|
|
447
|
-
state.pendingShells.splice(index, 1);
|
|
448
|
-
shell.reject(error);
|
|
449
|
-
}
|
|
450
|
-
function applyUiEvent(state, value) {
|
|
451
|
-
const event = recordOf(value);
|
|
452
|
-
if (!event)
|
|
453
|
-
return;
|
|
454
|
-
const type = String(event.type ?? "ui");
|
|
455
|
-
if (type === "shell.chunk") {
|
|
456
|
-
const pending = firstPendingShell(state);
|
|
457
|
-
const chunk = asText(event.chunk);
|
|
458
|
-
if (pending) {
|
|
459
|
-
pending.sawChunk = true;
|
|
460
|
-
pending.onData(Buffer.from(chunk));
|
|
461
|
-
} else {
|
|
462
|
-
appendTranscript(state, "Tool", chunk);
|
|
463
|
-
}
|
|
464
|
-
return;
|
|
465
|
-
}
|
|
466
|
-
if (type === "shell.end") {
|
|
467
|
-
const pending = firstPendingShell(state);
|
|
468
|
-
const output = asText(event.output ?? "");
|
|
469
|
-
const exitCode = typeof event.exitCode === "number" ? event.exitCode : event.isError === true ? 1 : 0;
|
|
470
|
-
if (pending) {
|
|
471
|
-
if (output && !pending.sawChunk)
|
|
472
|
-
pending.onData(Buffer.from(output));
|
|
473
|
-
finishPendingShell(state, pending, { exitCode });
|
|
474
|
-
} else {
|
|
475
|
-
appendTranscript(state, event.isError === true ? "Error" : "Tool", output || `exit ${String(exitCode)}`);
|
|
476
|
-
}
|
|
477
|
-
return;
|
|
478
|
-
}
|
|
479
|
-
if (type === "shell.start") {
|
|
480
|
-
if (!firstPendingShell(state))
|
|
481
|
-
appendTranscript(state, "Tool", `$ ${asText(event.command)}`);
|
|
482
|
-
return;
|
|
483
|
-
}
|
|
484
|
-
appendTranscript(state, "System", `${type}: ${asText(event)}`);
|
|
485
|
-
}
|
|
486
|
-
function applyEnvelope(ctx, state, envelopeValue) {
|
|
487
|
-
const envelope = recordOf(envelopeValue);
|
|
488
|
-
if (!envelope)
|
|
489
|
-
return;
|
|
490
|
-
const type = String(envelope.type ?? "");
|
|
491
|
-
if (type === "ready") {
|
|
492
|
-
const metadata = recordOf(envelope.metadata);
|
|
493
|
-
state.cwd = typeof metadata?.cwd === "string" ? metadata.cwd : state.cwd;
|
|
494
|
-
state.status = "worker Pi daemon ready";
|
|
495
|
-
if (!state.nativeStream)
|
|
496
|
-
appendTranscript(state, "System", "Connected to worker Pi daemon.");
|
|
497
|
-
} else if (type === "status.update") {
|
|
498
|
-
applyStatus(state, envelope);
|
|
499
|
-
} else if (type === "activity.update") {
|
|
500
|
-
const activity = recordOf(envelope.activity);
|
|
501
|
-
state.activity = [activity?.label, activity?.detail].map(asText).filter(Boolean).join(" \u2014 ");
|
|
502
|
-
} else if (type === "extension_ui_request") {
|
|
503
|
-
state.pendingUi = parseExtensionUiRequest(envelope.request);
|
|
504
|
-
appendTranscript(state, "System", `Extension UI request: ${state.pendingUi.prompt}`);
|
|
505
|
-
} else if (type === "pi.ui_event") {
|
|
506
|
-
applyUiEvent(state, envelope.event);
|
|
507
|
-
} else if (type === "pi.event") {
|
|
508
|
-
applyPiEvent(ctx, state, envelope.event);
|
|
509
|
-
} else if (type === "error") {
|
|
510
|
-
appendTranscript(state, "Error", asText(envelope.message ?? envelope.detail ?? "unknown error"));
|
|
511
|
-
}
|
|
512
|
-
syncNativeDisplayCwd(ctx, state);
|
|
270
|
+
function emptyRemoteStatus() {
|
|
271
|
+
return {
|
|
272
|
+
isStreaming: false,
|
|
273
|
+
isCompacting: false,
|
|
274
|
+
isBashRunning: false,
|
|
275
|
+
pendingMessageCount: 0,
|
|
276
|
+
steeringMessages: [],
|
|
277
|
+
followUpMessages: [],
|
|
278
|
+
model: null,
|
|
279
|
+
thinkingLevel: null,
|
|
280
|
+
sessionName: null,
|
|
281
|
+
cwd: null,
|
|
282
|
+
stats: null,
|
|
283
|
+
contextUsage: null
|
|
284
|
+
};
|
|
513
285
|
}
|
|
514
286
|
function resolveAttachReadyTimeoutMs() {
|
|
515
287
|
const raw = Number.parseInt(process.env.RIG_PI_ATTACH_TIMEOUT_MS ?? "", 10);
|
|
516
288
|
return Number.isFinite(raw) && raw > 0 ? raw : 10 * 60000;
|
|
517
289
|
}
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
return false;
|
|
554
|
-
}
|
|
555
|
-
await Bun.sleep(typeof session.retryAfterMs === "number" ? session.retryAfterMs : 750);
|
|
556
|
-
continue;
|
|
557
|
-
}
|
|
558
|
-
const sessionRecord = recordOf(session) ?? {};
|
|
559
|
-
applyEnvelope(ctx, state, { type: "ready", metadata: sessionRecord.metadata ?? sessionRecord });
|
|
560
|
-
updatePiUi(ctx, state);
|
|
561
|
-
return true;
|
|
562
|
-
}
|
|
563
|
-
}
|
|
564
|
-
function parseWsPayload(message) {
|
|
565
|
-
if (typeof message.data === "string")
|
|
566
|
-
return JSON.parse(message.data);
|
|
567
|
-
return JSON.parse(Buffer.from(message.data).toString("utf8"));
|
|
568
|
-
}
|
|
569
|
-
var BRIDGE_LOCAL_COMMANDS = new Set(["detach", "quit", "q", "stop"]);
|
|
570
|
-
function registerDaemonCommandsNatively(pi, options, ctx, state, commands, registered) {
|
|
571
|
-
for (const command of commands) {
|
|
572
|
-
const record = recordOf(command);
|
|
573
|
-
const name = typeof record?.name === "string" ? record.name : "";
|
|
574
|
-
if (!name || registered.has(name) || BRIDGE_LOCAL_COMMANDS.has(name))
|
|
575
|
-
continue;
|
|
576
|
-
registered.add(name);
|
|
577
|
-
const description = typeof record?.description === "string" ? record.description : undefined;
|
|
578
|
-
const source = typeof record?.source === "string" ? record.source : "worker";
|
|
579
|
-
try {
|
|
580
|
-
pi.registerCommand(name, {
|
|
581
|
-
description: `[worker ${source}] ${description ?? ""}`.trim(),
|
|
582
|
-
handler: async (args) => {
|
|
583
|
-
const text = `/${name}${args ? ` ${args}` : ""}`;
|
|
584
|
-
appendTranscript(state, "You", text);
|
|
585
|
-
try {
|
|
586
|
-
const result = await runRunPiCommandViaServer(options.context, options.runId, text);
|
|
587
|
-
const message = typeof result.message === "string" ? result.message : "worker command accepted";
|
|
588
|
-
appendTranscript(state, "System", message);
|
|
589
|
-
if (state.nativeStream)
|
|
590
|
-
ctx.ui.notify(message, "info");
|
|
591
|
-
} catch (error) {
|
|
592
|
-
reportBridgeError(ctx, state, error instanceof Error ? error.message : String(error));
|
|
593
|
-
}
|
|
594
|
-
updatePiUi(ctx, state);
|
|
595
|
-
}
|
|
596
|
-
});
|
|
597
|
-
} catch {}
|
|
598
|
-
}
|
|
599
|
-
}
|
|
600
|
-
async function connectWorkerStream(options, pi, ctx, state, registeredDaemonCommands) {
|
|
601
|
-
const ready = await waitForWorkerReady(options, ctx, state);
|
|
602
|
-
if (!ready)
|
|
603
|
-
return;
|
|
604
|
-
let catchupDone = false;
|
|
605
|
-
const buffered = [];
|
|
606
|
-
const wsUrl = await buildRunPiEventsWebSocketUrl(options.context, options.runId);
|
|
607
|
-
const socket = new WebSocket(wsUrl);
|
|
608
|
-
const closePromise = new Promise((resolve3) => {
|
|
290
|
+
|
|
291
|
+
class RigRemoteSessionController {
|
|
292
|
+
context;
|
|
293
|
+
runId;
|
|
294
|
+
status = emptyRemoteStatus();
|
|
295
|
+
transport;
|
|
296
|
+
hooks = {};
|
|
297
|
+
session = null;
|
|
298
|
+
socket = null;
|
|
299
|
+
closed = false;
|
|
300
|
+
pendingShells = [];
|
|
301
|
+
pendingCompactions = [];
|
|
302
|
+
constructor(input) {
|
|
303
|
+
this.context = input.context;
|
|
304
|
+
this.runId = input.runId;
|
|
305
|
+
this.transport = input.transport ?? defaultTransport();
|
|
306
|
+
}
|
|
307
|
+
ingestEnvelope(envelopeValue) {
|
|
308
|
+
this.applyEnvelope(envelopeValue);
|
|
309
|
+
}
|
|
310
|
+
bindSession(session) {
|
|
311
|
+
this.session = session;
|
|
312
|
+
}
|
|
313
|
+
setUiHooks(hooks) {
|
|
314
|
+
this.hooks = hooks;
|
|
315
|
+
}
|
|
316
|
+
async connect() {
|
|
317
|
+
const ready = await this.waitForReady();
|
|
318
|
+
if (!ready || this.closed)
|
|
319
|
+
return;
|
|
320
|
+
let catchupDone = false;
|
|
321
|
+
const buffered = [];
|
|
322
|
+
const wsUrl = await this.transport.buildEventsWebSocketUrl(this.context, this.runId);
|
|
323
|
+
const socket = new WebSocket(wsUrl);
|
|
324
|
+
this.socket = socket;
|
|
609
325
|
socket.onopen = () => {
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
updatePiUi(ctx, state);
|
|
326
|
+
this.hooks.onConnectionChange?.(true);
|
|
327
|
+
this.hooks.onStatusText?.("worker session live");
|
|
613
328
|
};
|
|
614
329
|
socket.onmessage = (message) => {
|
|
615
330
|
try {
|
|
616
|
-
const payload =
|
|
331
|
+
const payload = typeof message.data === "string" ? JSON.parse(message.data) : JSON.parse(Buffer.from(message.data).toString("utf8"));
|
|
617
332
|
if (!catchupDone)
|
|
618
333
|
buffered.push(payload);
|
|
619
|
-
else
|
|
620
|
-
applyEnvelope(
|
|
621
|
-
updatePiUi(ctx, state);
|
|
622
|
-
}
|
|
334
|
+
else
|
|
335
|
+
this.applyEnvelope(payload);
|
|
623
336
|
} catch (error) {
|
|
624
|
-
|
|
625
|
-
updatePiUi(ctx, state);
|
|
337
|
+
this.hooks.onError?.(`Unparseable worker Pi event: ${error instanceof Error ? error.message : String(error)}`);
|
|
626
338
|
}
|
|
627
339
|
};
|
|
628
340
|
socket.onerror = () => socket.close();
|
|
629
341
|
socket.onclose = () => {
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
resolve3();
|
|
342
|
+
this.hooks.onConnectionChange?.(false);
|
|
343
|
+
if (!this.closed)
|
|
344
|
+
this.hooks.onStatusText?.("worker Pi WebSocket disconnected");
|
|
634
345
|
};
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
346
|
+
try {
|
|
347
|
+
const [messagesPayload, statusPayload, commandsPayload] = await Promise.all([
|
|
348
|
+
this.transport.getMessages(this.context, this.runId),
|
|
349
|
+
this.transport.getStatus(this.context, this.runId),
|
|
350
|
+
this.transport.getCommands(this.context, this.runId)
|
|
351
|
+
]);
|
|
352
|
+
const messages = Array.isArray(messagesPayload.messages) ? messagesPayload.messages : [];
|
|
353
|
+
this.applyStatusPayload(statusPayload);
|
|
354
|
+
this.session?.replaceRemoteMessages(messages);
|
|
355
|
+
const commands = Array.isArray(commandsPayload.commands) ? commandsPayload.commands : [];
|
|
356
|
+
this.hooks.onCatchUp?.(messages, commands);
|
|
357
|
+
catchupDone = true;
|
|
358
|
+
for (const payload of buffered.splice(0))
|
|
359
|
+
this.applyEnvelope(payload);
|
|
360
|
+
} catch (error) {
|
|
361
|
+
catchupDone = true;
|
|
362
|
+
this.hooks.onError?.(`Worker Pi catch-up failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
close() {
|
|
366
|
+
this.closed = true;
|
|
367
|
+
this.socket?.close();
|
|
368
|
+
for (const shell of this.pendingShells.splice(0))
|
|
369
|
+
shell.reject(new Error("Remote session closed."));
|
|
370
|
+
for (const compaction of this.pendingCompactions.splice(0))
|
|
371
|
+
compaction.reject(new Error("Remote session closed."));
|
|
372
|
+
}
|
|
373
|
+
async sendPrompt(text, streamingBehavior) {
|
|
374
|
+
await this.transport.sendPrompt(this.context, this.runId, text, streamingBehavior);
|
|
375
|
+
}
|
|
376
|
+
async sendCommand(text) {
|
|
377
|
+
const result = await this.transport.runCommand(this.context, this.runId, text);
|
|
378
|
+
return typeof result.message === "string" ? result.message : "worker command accepted";
|
|
379
|
+
}
|
|
380
|
+
async sendShell(text) {
|
|
381
|
+
await this.transport.sendShell(this.context, this.runId, text);
|
|
382
|
+
}
|
|
383
|
+
async abort() {
|
|
384
|
+
await this.transport.abort(this.context, this.runId);
|
|
385
|
+
}
|
|
386
|
+
registerPendingShell(shell) {
|
|
387
|
+
this.pendingShells.push(shell);
|
|
388
|
+
}
|
|
389
|
+
failPendingShell(shell, error) {
|
|
390
|
+
const index = this.pendingShells.indexOf(shell);
|
|
391
|
+
if (index !== -1)
|
|
392
|
+
this.pendingShells.splice(index, 1);
|
|
393
|
+
shell.reject(error);
|
|
394
|
+
}
|
|
395
|
+
registerPendingCompaction(pending) {
|
|
396
|
+
this.pendingCompactions.push(pending);
|
|
397
|
+
}
|
|
398
|
+
async waitForReady() {
|
|
399
|
+
const startedAt = Date.now();
|
|
400
|
+
const deadline = startedAt + resolveAttachReadyTimeoutMs();
|
|
401
|
+
let consecutiveFailures = 0;
|
|
402
|
+
while (!this.closed) {
|
|
403
|
+
let requestFailed = false;
|
|
404
|
+
const session = await this.transport.getSession(this.context, this.runId).catch((error) => {
|
|
405
|
+
requestFailed = true;
|
|
406
|
+
return { ready: false, status: error instanceof Error ? error.message : String(error), retryAfterMs: 1000 };
|
|
407
|
+
});
|
|
408
|
+
if (session.ready !== false)
|
|
409
|
+
return true;
|
|
410
|
+
consecutiveFailures = requestFailed ? consecutiveFailures + 1 : 0;
|
|
411
|
+
const status = String(session.status ?? "starting");
|
|
412
|
+
if (TERMINAL_RUN_STATUSES.has(status.toLowerCase())) {
|
|
413
|
+
this.hooks.onError?.(`Run ended before the worker Pi daemon became ready: ${status}. Inspect with \`rig run show ${this.runId}\`.`);
|
|
414
|
+
return false;
|
|
415
|
+
}
|
|
416
|
+
this.hooks.onStatusText?.(consecutiveFailures >= 5 ? "Rig server unreachable \xB7 retrying \xB7 /detach to exit" : `waiting for worker Pi daemon \xB7 ${status} \xB7 /detach to exit`);
|
|
417
|
+
if (Date.now() >= deadline) {
|
|
418
|
+
this.hooks.onError?.(`Worker Pi daemon did not become ready (last status: ${status}). Set RIG_PI_ATTACH_TIMEOUT_MS to wait longer.`);
|
|
419
|
+
return false;
|
|
420
|
+
}
|
|
421
|
+
await Bun.sleep(typeof session.retryAfterMs === "number" ? session.retryAfterMs : 750);
|
|
422
|
+
}
|
|
423
|
+
return false;
|
|
424
|
+
}
|
|
425
|
+
applyEnvelope(envelopeValue) {
|
|
426
|
+
const envelope = recordOf(envelopeValue);
|
|
427
|
+
if (!envelope)
|
|
428
|
+
return;
|
|
429
|
+
const type = String(envelope.type ?? "");
|
|
430
|
+
if (type === "status.update") {
|
|
431
|
+
this.applyStatusPayload(envelope);
|
|
432
|
+
return;
|
|
433
|
+
}
|
|
434
|
+
if (type === "activity.update") {
|
|
435
|
+
const activity = recordOf(envelope.activity);
|
|
436
|
+
this.hooks.onActivity?.(String(activity?.label ?? ""), activity?.detail ? String(activity.detail) : undefined);
|
|
437
|
+
return;
|
|
438
|
+
}
|
|
439
|
+
if (type === "extension_ui_request") {
|
|
440
|
+
const request = recordOf(envelope.request);
|
|
441
|
+
if (request)
|
|
442
|
+
this.hooks.onExtensionUiRequest?.(request);
|
|
443
|
+
return;
|
|
444
|
+
}
|
|
445
|
+
if (type === "pi.ui_event") {
|
|
446
|
+
this.applyShellUiEvent(envelope.event);
|
|
447
|
+
return;
|
|
448
|
+
}
|
|
449
|
+
if (type === "pi.event") {
|
|
450
|
+
this.session?.handleRemoteSessionEvent(envelope.event);
|
|
451
|
+
this.settlePendingCompaction(envelope.event);
|
|
452
|
+
return;
|
|
453
|
+
}
|
|
454
|
+
if (type === "error") {
|
|
455
|
+
this.hooks.onError?.(String(envelope.message ?? envelope.detail ?? "unknown worker error"));
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
applyStatusPayload(payload) {
|
|
459
|
+
const status = recordOf(payload.status) ?? payload;
|
|
460
|
+
const next = this.status;
|
|
461
|
+
next.isStreaming = status.isStreaming === true;
|
|
462
|
+
next.isCompacting = status.isCompacting === true;
|
|
463
|
+
next.isBashRunning = status.isBashRunning === true;
|
|
464
|
+
next.pendingMessageCount = typeof status.pendingMessageCount === "number" ? status.pendingMessageCount : 0;
|
|
465
|
+
next.steeringMessages = Array.isArray(status.steeringMessages) ? status.steeringMessages.map(String) : [];
|
|
466
|
+
next.followUpMessages = Array.isArray(status.followUpMessages) ? status.followUpMessages.map(String) : [];
|
|
467
|
+
next.model = recordOf(status.model) ?? next.model;
|
|
468
|
+
next.thinkingLevel = typeof status.thinkingLevel === "string" ? status.thinkingLevel : next.thinkingLevel;
|
|
469
|
+
next.sessionName = typeof status.sessionName === "string" ? status.sessionName : next.sessionName;
|
|
470
|
+
next.cwd = typeof status.cwd === "string" ? status.cwd : next.cwd;
|
|
471
|
+
next.stats = recordOf(status.stats) ?? next.stats;
|
|
472
|
+
next.contextUsage = recordOf(status.contextUsage) ?? next.contextUsage;
|
|
473
|
+
}
|
|
474
|
+
applyShellUiEvent(value) {
|
|
475
|
+
const event = recordOf(value);
|
|
476
|
+
if (!event)
|
|
477
|
+
return;
|
|
478
|
+
const type = String(event.type ?? "");
|
|
479
|
+
const pending = this.pendingShells[0];
|
|
480
|
+
if (type === "shell.chunk" && pending) {
|
|
481
|
+
pending.sawChunk = true;
|
|
482
|
+
pending.onData(Buffer.from(String(event.chunk ?? "")));
|
|
483
|
+
return;
|
|
484
|
+
}
|
|
485
|
+
if (type === "shell.end" && pending) {
|
|
486
|
+
const output = String(event.output ?? "");
|
|
487
|
+
if (output && !pending.sawChunk)
|
|
488
|
+
pending.onData(Buffer.from(output));
|
|
489
|
+
const index = this.pendingShells.indexOf(pending);
|
|
490
|
+
if (index !== -1)
|
|
491
|
+
this.pendingShells.splice(index, 1);
|
|
492
|
+
pending.resolve({ exitCode: typeof event.exitCode === "number" ? event.exitCode : event.isError === true ? 1 : 0 });
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
settlePendingCompaction(eventValue) {
|
|
496
|
+
const event = recordOf(eventValue);
|
|
497
|
+
if (!event)
|
|
498
|
+
return;
|
|
499
|
+
const type = String(event.type ?? "");
|
|
500
|
+
if (type !== "compaction_end" || this.pendingCompactions.length === 0)
|
|
501
|
+
return;
|
|
502
|
+
const pending = this.pendingCompactions.shift();
|
|
503
|
+
const result = recordOf(event.result);
|
|
504
|
+
if (result)
|
|
505
|
+
pending.resolve(result);
|
|
506
|
+
else if (event.aborted === true)
|
|
507
|
+
pending.reject(new Error("Compaction aborted on the worker."));
|
|
646
508
|
else
|
|
647
|
-
|
|
648
|
-
applyMessage(state, message);
|
|
649
|
-
applyStatus(state, statusPayload);
|
|
650
|
-
const commands = Array.isArray(commandsPayload.commands) ? commandsPayload.commands : [];
|
|
651
|
-
state.commands = commands.flatMap((command) => {
|
|
652
|
-
const record = recordOf(command);
|
|
653
|
-
return typeof record?.name === "string" ? [`/${record.name}`] : [];
|
|
654
|
-
});
|
|
655
|
-
registerDaemonCommandsNatively(pi, options, ctx, state, commands, registeredDaemonCommands);
|
|
656
|
-
catchupDone = true;
|
|
657
|
-
for (const payload of buffered.splice(0))
|
|
658
|
-
applyEnvelope(ctx, state, payload);
|
|
659
|
-
updatePiUi(ctx, state);
|
|
660
|
-
} catch (error) {
|
|
661
|
-
appendTranscript(state, "Error", `Worker Pi catch-up failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
662
|
-
catchupDone = true;
|
|
663
|
-
updatePiUi(ctx, state);
|
|
509
|
+
pending.resolve({});
|
|
664
510
|
}
|
|
665
|
-
await closePromise;
|
|
666
511
|
}
|
|
667
|
-
function createRemoteBashOperations(
|
|
512
|
+
function createRemoteBashOperations(controller, excludeFromContext) {
|
|
668
513
|
return {
|
|
669
514
|
exec(command, _cwd, execOptions) {
|
|
670
515
|
return new Promise((resolve3, reject) => {
|
|
671
|
-
const pending = {
|
|
672
|
-
command,
|
|
673
|
-
onData: execOptions.onData,
|
|
674
|
-
resolve: resolve3,
|
|
675
|
-
reject,
|
|
676
|
-
sawChunk: false
|
|
677
|
-
};
|
|
516
|
+
const pending = { onData: execOptions.onData, resolve: resolve3, reject, sawChunk: false };
|
|
678
517
|
const cleanup = () => {
|
|
679
518
|
execOptions.signal?.removeEventListener("abort", onAbort);
|
|
680
519
|
if (timer)
|
|
@@ -682,12 +521,12 @@ function createRemoteBashOperations(options, state, excludeFromContext) {
|
|
|
682
521
|
};
|
|
683
522
|
const onAbort = () => {
|
|
684
523
|
cleanup();
|
|
685
|
-
failPendingShell(
|
|
524
|
+
controller.failPendingShell(pending, new Error("Remote worker shell command aborted locally."));
|
|
686
525
|
};
|
|
687
526
|
const timeoutMs = typeof execOptions.timeout === "number" && execOptions.timeout > 0 ? execOptions.timeout : 0;
|
|
688
527
|
const timer = timeoutMs > 0 ? setTimeout(() => {
|
|
689
528
|
cleanup();
|
|
690
|
-
failPendingShell(
|
|
529
|
+
controller.failPendingShell(pending, new Error(`Remote worker shell command timed out after ${timeoutMs}ms.`));
|
|
691
530
|
}, timeoutMs) : null;
|
|
692
531
|
const wrappedResolve = pending.resolve;
|
|
693
532
|
const wrappedReject = pending.reject;
|
|
@@ -700,125 +539,136 @@ function createRemoteBashOperations(options, state, excludeFromContext) {
|
|
|
700
539
|
wrappedReject(error);
|
|
701
540
|
};
|
|
702
541
|
execOptions.signal?.addEventListener("abort", onAbort, { once: true });
|
|
703
|
-
|
|
704
|
-
|
|
542
|
+
controller.registerPendingShell(pending);
|
|
543
|
+
controller.sendShell(`${excludeFromContext ? "!!" : "!"}${command}`).catch((error) => {
|
|
705
544
|
cleanup();
|
|
706
|
-
failPendingShell(
|
|
545
|
+
controller.failPendingShell(pending, error instanceof Error ? error : new Error(String(error)));
|
|
707
546
|
});
|
|
708
547
|
});
|
|
709
548
|
}
|
|
710
549
|
};
|
|
711
550
|
}
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
}
|
|
727
|
-
|
|
728
|
-
state.pendingUi = null;
|
|
729
|
-
return true;
|
|
730
|
-
}
|
|
731
|
-
async function routeInput(options, ctx, state, line) {
|
|
732
|
-
const text = line.trim();
|
|
733
|
-
if (!text)
|
|
734
|
-
return;
|
|
735
|
-
if (await answerPendingUi(options, state, text)) {
|
|
736
|
-
updatePiUi(ctx, state);
|
|
737
|
-
return;
|
|
738
|
-
}
|
|
739
|
-
if (text === "/detach" || text === "/quit" || text === "/q") {
|
|
740
|
-
appendTranscript(state, "System", "Detached locally; worker Pi daemon continues.");
|
|
741
|
-
updatePiUi(ctx, state);
|
|
742
|
-
ctx.shutdown();
|
|
743
|
-
return;
|
|
551
|
+
|
|
552
|
+
// packages/cli/src/commands/_pi-worker-bridge-extension.ts
|
|
553
|
+
function recordOf2(value) {
|
|
554
|
+
return value && typeof value === "object" && !Array.isArray(value) ? value : null;
|
|
555
|
+
}
|
|
556
|
+
function asText(value) {
|
|
557
|
+
if (typeof value === "string")
|
|
558
|
+
return value;
|
|
559
|
+
if (value === null || value === undefined)
|
|
560
|
+
return "";
|
|
561
|
+
if (typeof value === "number" || typeof value === "boolean")
|
|
562
|
+
return String(value);
|
|
563
|
+
try {
|
|
564
|
+
return JSON.stringify(value);
|
|
565
|
+
} catch {
|
|
566
|
+
return String(value);
|
|
744
567
|
}
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
568
|
+
}
|
|
569
|
+
async function answerExtensionUiRequest(options, ctx, request) {
|
|
570
|
+
const requestId = String(request.requestId ?? request.id ?? `ui-${Date.now()}`);
|
|
571
|
+
const method = String(request.method ?? request.type ?? "input");
|
|
572
|
+
const prompt = asText(request.prompt ?? request.message ?? request.title ?? method);
|
|
573
|
+
const rawOptions = Array.isArray(request.options) ? request.options : Array.isArray(request.items) ? request.items : [];
|
|
574
|
+
const choices = rawOptions.map((option) => {
|
|
575
|
+
const record = recordOf2(option);
|
|
576
|
+
return record ? asText(record.label ?? record.value ?? record.name ?? option) : asText(option);
|
|
577
|
+
}).filter(Boolean);
|
|
578
|
+
try {
|
|
579
|
+
if (method === "confirm") {
|
|
580
|
+
const confirmed = await ctx.ui.confirm("Worker request", prompt);
|
|
581
|
+
await respondRunPiExtensionUiViaServer(options.context, options.runId, requestId, { value: confirmed, confirmed });
|
|
582
|
+
return;
|
|
583
|
+
}
|
|
584
|
+
if (choices.length > 0) {
|
|
585
|
+
const selected = await ctx.ui.select(prompt, choices);
|
|
586
|
+
await respondRunPiExtensionUiViaServer(options.context, options.runId, requestId, selected === undefined ? { cancelled: true } : { value: selected });
|
|
587
|
+
return;
|
|
588
|
+
}
|
|
589
|
+
const value = await ctx.ui.input("Worker request", prompt);
|
|
590
|
+
await respondRunPiExtensionUiViaServer(options.context, options.runId, requestId, value === undefined ? { cancelled: true } : { value });
|
|
591
|
+
} catch (error) {
|
|
592
|
+
ctx.ui.notify(`Failed to answer worker UI request: ${error instanceof Error ? error.message : String(error)}`, "error");
|
|
751
593
|
}
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
const
|
|
758
|
-
const
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
594
|
+
}
|
|
595
|
+
var LOCALLY_OWNED_COMMANDS = new Set(["detach", "quit", "q", "stop", "settings", "model", "thinking", "compact", "session", "name", "reload"]);
|
|
596
|
+
function registerDaemonCommands(pi, options, ctx, commands, registered) {
|
|
597
|
+
for (const command of commands) {
|
|
598
|
+
const record = recordOf2(command);
|
|
599
|
+
const name = typeof record?.name === "string" ? record.name : "";
|
|
600
|
+
const source = typeof record?.source === "string" ? record.source : "worker";
|
|
601
|
+
if (!name || source === "builtin" || registered.has(name) || LOCALLY_OWNED_COMMANDS.has(name))
|
|
602
|
+
continue;
|
|
603
|
+
registered.add(name);
|
|
604
|
+
const description = typeof record?.description === "string" ? record.description : undefined;
|
|
605
|
+
try {
|
|
606
|
+
pi.registerCommand(name, {
|
|
607
|
+
description: `[worker ${source}] ${description ?? ""}`.trim(),
|
|
608
|
+
handler: async (args) => {
|
|
609
|
+
try {
|
|
610
|
+
const message = await options.controller.sendCommand(`/${name}${args ? ` ${args}` : ""}`);
|
|
611
|
+
ctx.ui.notify(message, "info");
|
|
612
|
+
} catch (error) {
|
|
613
|
+
ctx.ui.notify(error instanceof Error ? error.message : String(error), "error");
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
});
|
|
617
|
+
} catch {}
|
|
763
618
|
}
|
|
764
|
-
updatePiUi(ctx, state);
|
|
765
619
|
}
|
|
766
620
|
function createRigWorkerPiBridgeExtension(options) {
|
|
767
621
|
return (pi) => {
|
|
768
|
-
const state = {
|
|
769
|
-
transcript: [],
|
|
770
|
-
status: "starting worker Pi daemon bridge",
|
|
771
|
-
activity: "",
|
|
772
|
-
cwd: "",
|
|
773
|
-
model: "",
|
|
774
|
-
commands: [],
|
|
775
|
-
streaming: false,
|
|
776
|
-
pendingUi: null,
|
|
777
|
-
pendingShells: [],
|
|
778
|
-
wsConnected: false,
|
|
779
|
-
nativeStream: false
|
|
780
|
-
};
|
|
781
|
-
if (options.initialMessageSent)
|
|
782
|
-
appendTranscript(state, "System", "Initial message sent to worker Pi daemon.");
|
|
783
622
|
const registeredDaemonCommands = new Set;
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
623
|
+
pi.registerCommand("detach", {
|
|
624
|
+
description: "Detach from this run; the worker keeps going",
|
|
625
|
+
handler: async (_args, ctx) => {
|
|
626
|
+
ctx.ui.notify("Detached locally; the worker Pi daemon continues.", "info");
|
|
627
|
+
ctx.shutdown();
|
|
628
|
+
}
|
|
629
|
+
});
|
|
630
|
+
pi.registerCommand("stop", {
|
|
631
|
+
description: "Stop the worker Pi run and detach",
|
|
632
|
+
handler: async (_args, ctx) => {
|
|
633
|
+
await options.controller.abort();
|
|
634
|
+
ctx.ui.notify("Stop requested for the worker Pi daemon.", "info");
|
|
635
|
+
ctx.shutdown();
|
|
636
|
+
}
|
|
788
637
|
});
|
|
638
|
+
pi.on("user_bash", (event) => ({
|
|
639
|
+
operations: createRemoteBashOperations(options.controller, event.excludeFromContext === true)
|
|
640
|
+
}));
|
|
789
641
|
pi.on("session_start", async (_event, ctx) => {
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
642
|
+
ctx.ui.setTitle("Rig \xB7 worker session");
|
|
643
|
+
ctx.ui.setStatus("rig-worker-pi", "connecting to worker session");
|
|
644
|
+
ctx.ui.notify(`Attached to Rig run ${options.runId}. Native Pi, remote brain \u2014 /detach exits, /stop cancels the run.`, "info");
|
|
645
|
+
if (options.initialMessageSent)
|
|
646
|
+
ctx.ui.notify("Initial message sent to the worker Pi daemon.", "info");
|
|
647
|
+
const nativeUi = ctx.ui;
|
|
648
|
+
options.controller.setUiHooks({
|
|
649
|
+
onStatusText: (text) => ctx.ui.setStatus("rig-worker-pi", text),
|
|
650
|
+
onActivity: (label, detail) => ctx.ui.setStatus("rig-worker-pi", [label, detail].filter(Boolean).join(" \u2014 ")),
|
|
651
|
+
onConnectionChange: (connected) => ctx.ui.setStatus("rig-worker-pi", connected ? "worker session live" : "worker session disconnected"),
|
|
652
|
+
onError: (message) => ctx.ui.notify(message, "error"),
|
|
653
|
+
onExtensionUiRequest: (request) => {
|
|
654
|
+
answerExtensionUiRequest(options, ctx, request);
|
|
655
|
+
},
|
|
656
|
+
onCatchUp: (messages, commands) => {
|
|
657
|
+
if (nativeUi.appendSessionMessages)
|
|
658
|
+
nativeUi.appendSessionMessages(messages);
|
|
659
|
+
const cwd = options.controller.status.cwd;
|
|
660
|
+
if (nativeUi.setDisplayCwd && cwd)
|
|
661
|
+
nativeUi.setDisplayCwd(cwd);
|
|
662
|
+
registerDaemonCommands(pi, options, ctx, commands, registeredDaemonCommands);
|
|
798
663
|
}
|
|
799
|
-
if (!data.includes("\r") && !data.includes(`
|
|
800
|
-
`))
|
|
801
|
-
return;
|
|
802
|
-
const inlineText = data.replace(/[\r\n]+/g, "").trim();
|
|
803
|
-
const editorText = ctx.ui.getEditorText().trim();
|
|
804
|
-
const text = [editorText, inlineText].filter(Boolean).join(" ").trim();
|
|
805
|
-
if (!text)
|
|
806
|
-
return;
|
|
807
|
-
if (text.startsWith("!"))
|
|
808
|
-
return;
|
|
809
|
-
ctx.ui.setEditorText("");
|
|
810
|
-
routeInput(options, ctx, state, text).catch((error) => {
|
|
811
|
-
appendTranscript(state, "Error", error instanceof Error ? error.message : String(error));
|
|
812
|
-
updatePiUi(ctx, state);
|
|
813
|
-
});
|
|
814
|
-
return { consume: true };
|
|
815
664
|
});
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
updatePiUi(ctx, state);
|
|
665
|
+
options.controller.connect().catch((error) => {
|
|
666
|
+
ctx.ui.notify(error instanceof Error ? error.message : String(error), "error");
|
|
819
667
|
});
|
|
820
668
|
});
|
|
821
|
-
pi.on("session_shutdown", () => {
|
|
669
|
+
pi.on("session_shutdown", () => {
|
|
670
|
+
options.controller.close();
|
|
671
|
+
});
|
|
822
672
|
};
|
|
823
673
|
}
|
|
824
674
|
export {
|