@h-rig/cli 0.0.6-alpha.9 → 0.0.6-alpha.90
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/bin/build-rig-binaries.d.ts +2 -0
- package/dist/bin/rig.d.ts +2 -0
- package/dist/bin/rig.js +11052 -6668
- package/dist/src/app/board.d.ts +23 -0
- package/dist/src/app/board.js +1786 -0
- package/dist/src/app/drone-ui.d.ts +37 -0
- package/dist/src/app/drone-ui.js +294 -0
- package/dist/src/app/theme.d.ts +47 -0
- package/dist/src/app/theme.js +150 -0
- package/dist/src/commands/_async-ui.d.ts +13 -0
- package/dist/src/commands/_async-ui.js +223 -0
- package/dist/src/commands/_authority-runs.d.ts +22 -0
- package/dist/src/commands/_authority-runs.js +2 -3
- package/dist/src/commands/_cli-format.d.ts +49 -0
- package/dist/src/commands/_cli-format.js +447 -0
- package/dist/src/commands/_connection-state.d.ts +44 -0
- package/dist/src/commands/_connection-state.js +30 -11
- package/dist/src/commands/_doctor-checks.d.ts +52 -0
- package/dist/src/commands/_doctor-checks.js +180 -44
- package/dist/src/commands/_help-catalog.d.ts +51 -0
- package/dist/src/commands/_help-catalog.js +442 -0
- package/dist/src/commands/_json-output.d.ts +11 -0
- package/dist/src/commands/_json-output.js +56 -0
- package/dist/src/commands/_operator-surface.d.ts +34 -0
- package/dist/src/commands/_operator-surface.js +220 -0
- package/dist/src/commands/_operator-view.d.ts +30 -0
- package/dist/src/commands/_operator-view.js +803 -73
- package/dist/src/commands/_parsers.d.ts +15 -0
- package/dist/src/commands/_parsers.js +18 -11
- package/dist/src/commands/_paths.d.ts +11 -0
- package/dist/src/commands/_pi-frontend.d.ts +27 -0
- package/dist/src/commands/_pi-frontend.js +742 -0
- package/dist/src/commands/_pi-install.d.ts +42 -0
- package/dist/src/commands/_pi-install.js +5 -4
- package/dist/src/commands/_policy.d.ts +8 -0
- package/dist/src/commands/_policy.js +12 -5
- package/dist/src/commands/_preflight.d.ts +22 -0
- package/dist/src/commands/_preflight.js +190 -128
- package/dist/src/commands/_probes.d.ts +1 -0
- package/dist/src/commands/_run-driver-helpers.d.ts +99 -0
- package/dist/src/commands/_run-driver-helpers.js +75 -22
- package/dist/src/commands/_run-replay.d.ts +24 -0
- package/dist/src/commands/_run-replay.js +142 -0
- package/dist/src/commands/_server-client.d.ts +186 -0
- package/dist/src/commands/_server-client.js +360 -61
- package/dist/src/commands/_snapshot-upload.d.ts +39 -0
- package/dist/src/commands/_snapshot-upload.js +163 -39
- package/dist/src/commands/_spinner.d.ts +25 -0
- package/dist/src/commands/_spinner.js +65 -0
- package/dist/src/commands/_task-picker.d.ts +9 -0
- package/dist/src/commands/_task-picker.js +172 -19
- package/dist/src/commands/agent.d.ts +3 -0
- package/dist/src/commands/agent.js +39 -20
- package/dist/src/commands/browser.d.ts +65 -0
- package/dist/src/commands/browser.js +334 -51
- package/dist/src/commands/connect.d.ts +7 -0
- package/dist/src/commands/connect.js +272 -33
- package/dist/src/commands/dist.d.ts +28 -0
- package/dist/src/commands/dist.js +19 -12
- package/dist/src/commands/doctor.d.ts +3 -0
- package/dist/src/commands/doctor.js +378 -45
- package/dist/src/commands/github.d.ts +3 -0
- package/dist/src/commands/github.js +375 -53
- package/dist/src/commands/inbox.d.ts +30 -0
- package/dist/src/commands/inbox.js +760 -70
- package/dist/src/commands/init.d.ts +74 -0
- package/dist/src/commands/init.js +963 -119
- package/dist/src/commands/inspect.d.ts +3 -0
- package/dist/src/commands/inspect.js +588 -32
- package/dist/src/commands/inspector.d.ts +3 -0
- package/dist/src/commands/inspector.js +20 -13
- package/dist/src/commands/pi.d.ts +3 -0
- package/dist/src/commands/pi.js +177 -0
- package/dist/src/commands/plugin.d.ts +16 -0
- package/dist/src/commands/plugin.js +95 -27
- package/dist/src/commands/profile-and-review.d.ts +4 -0
- package/dist/src/commands/profile-and-review.js +26 -19
- package/dist/src/commands/queue.d.ts +3 -0
- package/dist/src/commands/queue.js +32 -12
- package/dist/src/commands/remote.d.ts +3 -0
- package/dist/src/commands/remote.js +43 -36
- package/dist/src/commands/repo-git-harness.d.ts +5 -0
- package/dist/src/commands/repo-git-harness.js +22 -15
- package/dist/src/commands/run.d.ts +3 -0
- package/dist/src/commands/run.js +1379 -159
- package/dist/src/commands/server.d.ts +7 -0
- package/dist/src/commands/server.js +502 -57
- package/dist/src/commands/setup.d.ts +16 -0
- package/dist/src/commands/setup.js +390 -63
- package/dist/src/commands/stats.d.ts +12 -0
- package/dist/src/commands/stats.js +1051 -0
- package/dist/src/commands/task-report-bug.d.ts +19 -0
- package/dist/src/commands/task-report-bug.js +260 -62
- package/dist/src/commands/task-run-driver.d.ts +132 -0
- package/dist/src/commands/task-run-driver.js +886 -130
- package/dist/src/commands/task.d.ts +14 -0
- package/dist/src/commands/task.js +1644 -314
- package/dist/src/commands/test.d.ts +3 -0
- package/dist/src/commands/test.js +15 -8
- package/dist/src/commands/workspace.d.ts +3 -0
- package/dist/src/commands/workspace.js +18 -11
- package/dist/src/commands.d.ts +29 -0
- package/dist/src/commands.js +10967 -6637
- package/dist/src/index.d.ts +4 -0
- package/dist/src/index.js +10168 -5787
- package/dist/src/launcher.d.ts +61 -0
- package/dist/src/launcher.js +77 -13
- package/dist/src/report-bug.d.ts +44 -0
- package/dist/src/report-bug.js +3 -3
- package/dist/src/runner.d.ts +47 -0
- package/dist/src/runner.js +16 -22
- package/dist/src/withMutedConsole.d.ts +2 -0
- package/package.json +13 -6
|
@@ -1,16 +1,19 @@
|
|
|
1
1
|
// @bun
|
|
2
|
-
// packages/cli/src/commands/inbox.ts
|
|
3
|
-
import { writeFileSync } from "fs";
|
|
4
|
-
import { resolve } from "path";
|
|
5
|
-
|
|
6
2
|
// packages/cli/src/runner.ts
|
|
7
3
|
import { EventBus } from "@rig/runtime/control-plane/runtime/events";
|
|
8
|
-
import { CliError } from "@rig/runtime/control-plane/errors";
|
|
4
|
+
import { CliError as RuntimeCliError } from "@rig/runtime/control-plane/errors";
|
|
9
5
|
import { evaluate, loadPolicy, resolveAction } from "@rig/runtime/control-plane/runtime/guard";
|
|
10
|
-
import { PluginManager } from "@rig/runtime/control-plane/runtime/plugins";
|
|
11
|
-
import { loadRuntimeContextFromEnv } from "@rig/runtime/control-plane/runtime/context";
|
|
12
6
|
import { buildBinary } from "@rig/runtime/control-plane/runtime/isolation";
|
|
13
|
-
|
|
7
|
+
|
|
8
|
+
class CliError extends RuntimeCliError {
|
|
9
|
+
hint;
|
|
10
|
+
constructor(message, exitCode = 1, options = {}) {
|
|
11
|
+
super(message, exitCode);
|
|
12
|
+
if (options.hint?.trim()) {
|
|
13
|
+
this.hint = options.hint.trim();
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
}
|
|
14
17
|
function takeOption(args, option) {
|
|
15
18
|
const rest = [];
|
|
16
19
|
let value;
|
|
@@ -19,7 +22,7 @@ function takeOption(args, option) {
|
|
|
19
22
|
if (current === option) {
|
|
20
23
|
const next = args[index + 1];
|
|
21
24
|
if (!next || next.startsWith("-")) {
|
|
22
|
-
throw new CliError(`Missing value for ${option}`);
|
|
25
|
+
throw new CliError(`Missing value for ${option}`, 1, { hint: `Provide a value after ${option}, e.g. \`${option} <value>\`.` });
|
|
23
26
|
}
|
|
24
27
|
value = next;
|
|
25
28
|
index += 1;
|
|
@@ -38,12 +41,698 @@ Usage: ${usage}`);
|
|
|
38
41
|
}
|
|
39
42
|
}
|
|
40
43
|
|
|
44
|
+
// packages/cli/src/app/drone-ui.ts
|
|
45
|
+
import { ProcessTerminal, TUI, Text, Input, SelectList, matchesKey } from "@earendil-works/pi-tui";
|
|
46
|
+
|
|
47
|
+
// packages/cli/src/app/theme.ts
|
|
48
|
+
var RIG_PALETTE = {
|
|
49
|
+
ink: "#f2f3f6",
|
|
50
|
+
ink2: "#aeb0ba",
|
|
51
|
+
ink3: "#6c6e79",
|
|
52
|
+
ink4: "#44464f",
|
|
53
|
+
accent: "#ccff4d",
|
|
54
|
+
accentDim: "#a9d63f",
|
|
55
|
+
cyan: "#56d8ff",
|
|
56
|
+
red: "#ff5d5d",
|
|
57
|
+
yellow: "#ffd24d"
|
|
58
|
+
};
|
|
59
|
+
function hexToRgb(hex) {
|
|
60
|
+
const value = hex.replace("#", "");
|
|
61
|
+
return [
|
|
62
|
+
Number.parseInt(value.slice(0, 2), 16),
|
|
63
|
+
Number.parseInt(value.slice(2, 4), 16),
|
|
64
|
+
Number.parseInt(value.slice(4, 6), 16)
|
|
65
|
+
];
|
|
66
|
+
}
|
|
67
|
+
function fg(hex) {
|
|
68
|
+
const [r, g, b] = hexToRgb(hex);
|
|
69
|
+
return (text) => `\x1B[38;2;${r};${g};${b}m${text}\x1B[39m`;
|
|
70
|
+
}
|
|
71
|
+
var ink = fg(RIG_PALETTE.ink);
|
|
72
|
+
var ink2 = fg(RIG_PALETTE.ink2);
|
|
73
|
+
var ink3 = fg(RIG_PALETTE.ink3);
|
|
74
|
+
var ink4 = fg(RIG_PALETTE.ink4);
|
|
75
|
+
var accent = fg(RIG_PALETTE.accent);
|
|
76
|
+
var accentDim = fg(RIG_PALETTE.accentDim);
|
|
77
|
+
var cyan = fg(RIG_PALETTE.cyan);
|
|
78
|
+
var red = fg(RIG_PALETTE.red);
|
|
79
|
+
var yellow = fg(RIG_PALETTE.yellow);
|
|
80
|
+
function bold(text) {
|
|
81
|
+
return `\x1B[1m${text}\x1B[22m`;
|
|
82
|
+
}
|
|
83
|
+
var DRONE_ART = [
|
|
84
|
+
" .-=-. .-=-. ",
|
|
85
|
+
" ( !!! ) ( !!! ) ",
|
|
86
|
+
" '-=-'._ _.'-=-' ",
|
|
87
|
+
" '._ _.' ",
|
|
88
|
+
" '=$$$$$$$=.' ",
|
|
89
|
+
" =$$$$$$$$$$$= ",
|
|
90
|
+
" $$$@@@@@@@@@@$$$ ",
|
|
91
|
+
" $$$@@ @@$$$ ",
|
|
92
|
+
" $$@ ? @$$$ ",
|
|
93
|
+
" $$$@ '-' @$$$ ",
|
|
94
|
+
" $$$@@ @@$$$ ",
|
|
95
|
+
" $$$@@@@@@@@@@$$$ ",
|
|
96
|
+
" =$$$$$$$$$$$= ",
|
|
97
|
+
" '=$$$$$$$=.' ",
|
|
98
|
+
" _.' '._ ",
|
|
99
|
+
" .-=-.' '.-=-. ",
|
|
100
|
+
" ( !!! ) ( !!! ) ",
|
|
101
|
+
" '-=-' '-=-' "
|
|
102
|
+
];
|
|
103
|
+
var EYE_FRAMES = ["@", "o", "."];
|
|
104
|
+
var DRONE_WIDTH = DRONE_ART[0].length;
|
|
105
|
+
var DRONE_HEIGHT = DRONE_ART.length;
|
|
106
|
+
var MICRO_BLADES = ["---", "\\\\\\", "|||", "///"];
|
|
107
|
+
function microDroneFrame(tick) {
|
|
108
|
+
const blade = MICRO_BLADES[tick % MICRO_BLADES.length];
|
|
109
|
+
const eye = EYE_FRAMES[Math.floor(tick / 2) % EYE_FRAMES.length];
|
|
110
|
+
return `(${blade})${eye}(${blade})`;
|
|
111
|
+
}
|
|
112
|
+
var MICRO_DRONE_FRAMES = Array.from({ length: 12 }, (_, index) => microDroneFrame(index));
|
|
113
|
+
function renderMicroDroneFrame(tick) {
|
|
114
|
+
const blade = MICRO_BLADES[tick % MICRO_BLADES.length];
|
|
115
|
+
const eye = EYE_FRAMES[Math.floor(tick / 2) % EYE_FRAMES.length];
|
|
116
|
+
return `${ink4("(")}${cyan(blade)}${ink4(")")}${bold(accent(eye))}${ink4("(")}${cyan(blade)}${ink4(")")}`;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// packages/cli/src/commands/_spinner.ts
|
|
120
|
+
var SPINNER_FRAMES = ["\u280B", "\u2819", "\u2839", "\u2838", "\u283C", "\u2834", "\u2826", "\u2827", "\u2807", "\u280F"];
|
|
121
|
+
function createTtySpinner(input) {
|
|
122
|
+
const output = input.output ?? process.stdout;
|
|
123
|
+
const isTty = output.isTTY === true;
|
|
124
|
+
const frames = input.frames && input.frames.length > 0 ? input.frames : SPINNER_FRAMES;
|
|
125
|
+
let label = input.label;
|
|
126
|
+
let frame = 0;
|
|
127
|
+
let paused = false;
|
|
128
|
+
let stopped = false;
|
|
129
|
+
let lastPrintedLabel = "";
|
|
130
|
+
const render = () => {
|
|
131
|
+
if (stopped || paused)
|
|
132
|
+
return;
|
|
133
|
+
if (!isTty) {
|
|
134
|
+
if (label !== lastPrintedLabel) {
|
|
135
|
+
output.write(`${label}
|
|
136
|
+
`);
|
|
137
|
+
lastPrintedLabel = label;
|
|
138
|
+
}
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
frame = (frame + 1) % frames.length;
|
|
142
|
+
const glyph = frames[frame] ?? frames[0] ?? "";
|
|
143
|
+
output.write(`\r\x1B[2K${input.styleFrame ? input.styleFrame(glyph) : glyph} ${label}`);
|
|
144
|
+
};
|
|
145
|
+
const clearLine = () => {
|
|
146
|
+
if (isTty)
|
|
147
|
+
output.write("\r\x1B[2K");
|
|
148
|
+
};
|
|
149
|
+
render();
|
|
150
|
+
const timer = isTty ? setInterval(render, input.intervalMs ?? 120) : null;
|
|
151
|
+
return {
|
|
152
|
+
setLabel(next) {
|
|
153
|
+
label = next;
|
|
154
|
+
render();
|
|
155
|
+
},
|
|
156
|
+
pause() {
|
|
157
|
+
paused = true;
|
|
158
|
+
clearLine();
|
|
159
|
+
},
|
|
160
|
+
resume() {
|
|
161
|
+
if (stopped)
|
|
162
|
+
return;
|
|
163
|
+
paused = false;
|
|
164
|
+
render();
|
|
165
|
+
},
|
|
166
|
+
stop(finalLine) {
|
|
167
|
+
if (stopped)
|
|
168
|
+
return;
|
|
169
|
+
stopped = true;
|
|
170
|
+
if (timer)
|
|
171
|
+
clearInterval(timer);
|
|
172
|
+
clearLine();
|
|
173
|
+
if (finalLine)
|
|
174
|
+
output.write(`${finalLine}
|
|
175
|
+
`);
|
|
176
|
+
}
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// packages/cli/src/app/drone-ui.ts
|
|
181
|
+
function droneNote(message, title) {
|
|
182
|
+
if (title)
|
|
183
|
+
console.log(` ${accentDim("\u25C7")} ${bold(ink2(title))}`);
|
|
184
|
+
for (const line of message.split(`
|
|
185
|
+
`)) {
|
|
186
|
+
console.log(` ${ink4("\u2502")} ${line}`);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// packages/cli/src/commands/_cli-format.ts
|
|
191
|
+
import pc from "picocolors";
|
|
192
|
+
var themeDim = (value) => ink3(value);
|
|
193
|
+
var themeFaint = (value) => ink4(value);
|
|
194
|
+
function stringField(record, key, fallback = "") {
|
|
195
|
+
const value = record[key];
|
|
196
|
+
return typeof value === "string" && value.trim() ? value.trim() : fallback;
|
|
197
|
+
}
|
|
198
|
+
function statusColor(status) {
|
|
199
|
+
const normalized = status.toLowerCase();
|
|
200
|
+
if (["completed", "merged", "closed", "done", "accepted", "pass", "selected", "approved", "running"].includes(normalized))
|
|
201
|
+
return accent;
|
|
202
|
+
if (["failed", "needs_attention", "needs-attention", "blocked", "error", "rejected"].includes(normalized))
|
|
203
|
+
return red;
|
|
204
|
+
if (["reviewing", "validating", "in_progress", "in-progress", "remote", "preparing", "closing-out"].includes(normalized))
|
|
205
|
+
return cyan;
|
|
206
|
+
if (["ready", "open", "queued", "created", "local", "pending"].includes(normalized))
|
|
207
|
+
return yellow;
|
|
208
|
+
return themeDim;
|
|
209
|
+
}
|
|
210
|
+
function firstString(record, keys, fallback = "") {
|
|
211
|
+
for (const key of keys) {
|
|
212
|
+
const value = stringField(record, key);
|
|
213
|
+
if (value)
|
|
214
|
+
return value;
|
|
215
|
+
}
|
|
216
|
+
return fallback;
|
|
217
|
+
}
|
|
218
|
+
function requestIdOf(entry) {
|
|
219
|
+
return firstString(entry, ["requestId", "id", "approvalId", "inputId"], "(unknown-request)");
|
|
220
|
+
}
|
|
221
|
+
function shouldUseClackOutput() {
|
|
222
|
+
return Boolean(process.stdout.isTTY) && process.env.RIG_CLI_PLAIN_HELP !== "1";
|
|
223
|
+
}
|
|
224
|
+
function printFormattedOutput(message, options = {}) {
|
|
225
|
+
if (!shouldUseClackOutput()) {
|
|
226
|
+
console.log(message);
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
droneNote(message, options.title);
|
|
230
|
+
}
|
|
231
|
+
function formatStatusPill(status) {
|
|
232
|
+
const label = status || "unknown";
|
|
233
|
+
return statusColor(label)(`\u25CF ${label}`);
|
|
234
|
+
}
|
|
235
|
+
function formatSection(title, subtitle) {
|
|
236
|
+
return `${pc.bold(accent("\u25C6"))} ${pc.bold(title)}${subtitle ? themeDim(` \u2014 ${subtitle}`) : ""}`;
|
|
237
|
+
}
|
|
238
|
+
function formatNextSteps(steps) {
|
|
239
|
+
if (steps.length === 0)
|
|
240
|
+
return [];
|
|
241
|
+
return [pc.bold("Next"), ...steps.map((step) => `${accent("\u203A")} ${step}`)];
|
|
242
|
+
}
|
|
243
|
+
function formatInboxList(kind, entries) {
|
|
244
|
+
const title = kind === "approvals" ? "Approval inbox" : "Input inbox";
|
|
245
|
+
if (entries.length === 0) {
|
|
246
|
+
return [
|
|
247
|
+
formatSection(title, "empty"),
|
|
248
|
+
themeDim(kind === "approvals" ? "No pending approvals." : "No pending user-input requests."),
|
|
249
|
+
"",
|
|
250
|
+
...formatNextSteps(["Check runs: `rig run status`", "Start work: `rig task run --next`"])
|
|
251
|
+
].join(`
|
|
252
|
+
`);
|
|
253
|
+
}
|
|
254
|
+
const lines = [formatSection(title, `${entries.length} pending`)];
|
|
255
|
+
for (const entry of entries) {
|
|
256
|
+
const record = entry.record && typeof entry.record === "object" && !Array.isArray(entry.record) ? entry.record : entry;
|
|
257
|
+
const runId = firstString(entry, ["runId"], firstString(record, ["runId"]));
|
|
258
|
+
const taskId = firstString(entry, ["taskId"], firstString(record, ["taskId", "task"]));
|
|
259
|
+
const requestId = requestIdOf(record);
|
|
260
|
+
const status = firstString(record, ["status", "state"], "pending");
|
|
261
|
+
const prompt = firstString(record, ["prompt", "message", "reason", "title", "summary"], kind === "approvals" ? "Approval requested" : "Input requested");
|
|
262
|
+
lines.push(`${themeFaint("\u2502")} ${pc.bold(requestId)} ${formatStatusPill(status)} ${prompt}`);
|
|
263
|
+
lines.push(`${themeFaint("\u2502")} ${themeDim("run ")} ${runId || "(unknown-run)"}${taskId ? themeDim(` task ${taskId}`) : ""}`);
|
|
264
|
+
}
|
|
265
|
+
lines.push("", ...formatNextSteps(kind === "approvals" ? ["Resolve: `rig inbox approve --run <run-id> --request <request-id> --decision approve|reject`", "Rejoin: `rig run attach <run-id> --follow`"] : ["Respond: `rig inbox respond --run <run-id> --request <request-id> --answer key=value`", "Rejoin: `rig run attach <run-id> --follow`"]));
|
|
266
|
+
return lines.join(`
|
|
267
|
+
`);
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
// packages/cli/src/commands/_server-client.ts
|
|
271
|
+
import { existsSync as existsSync2, readFileSync as readFileSync2 } from "fs";
|
|
272
|
+
import { resolve as resolve2 } from "path";
|
|
273
|
+
import { ensureLocalRigServerConnection } from "@rig/runtime/local-server";
|
|
274
|
+
|
|
275
|
+
// packages/cli/src/commands/_connection-state.ts
|
|
276
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs";
|
|
277
|
+
import { homedir } from "os";
|
|
278
|
+
import { dirname, resolve } from "path";
|
|
279
|
+
function resolveGlobalConnectionsPath(env = process.env) {
|
|
280
|
+
const explicit = env.RIG_CONNECTIONS_FILE?.trim();
|
|
281
|
+
if (explicit)
|
|
282
|
+
return resolve(explicit);
|
|
283
|
+
const stateDir = env.RIG_GLOBAL_STATE_DIR?.trim();
|
|
284
|
+
if (stateDir)
|
|
285
|
+
return resolve(stateDir, "connections.json");
|
|
286
|
+
return resolve(homedir(), ".rig", "connections.json");
|
|
287
|
+
}
|
|
288
|
+
function resolveRepoConnectionPath(projectRoot) {
|
|
289
|
+
return resolve(projectRoot, ".rig", "state", "connection.json");
|
|
290
|
+
}
|
|
291
|
+
function readJsonFile(path) {
|
|
292
|
+
if (!existsSync(path))
|
|
293
|
+
return null;
|
|
294
|
+
try {
|
|
295
|
+
return JSON.parse(readFileSync(path, "utf8"));
|
|
296
|
+
} catch (error) {
|
|
297
|
+
throw new CliError(`Invalid Rig connection state at ${path}: ${error instanceof Error ? error.message : String(error)}`, 1, { hint: "Fix or delete that file, then re-select a server with `rig server use <alias|local>`." });
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
function writeJsonFile(path, value) {
|
|
301
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
302
|
+
writeFileSync(path, `${JSON.stringify(value, null, 2)}
|
|
303
|
+
`, "utf8");
|
|
304
|
+
}
|
|
305
|
+
function normalizeConnection(value) {
|
|
306
|
+
if (!value || typeof value !== "object" || Array.isArray(value))
|
|
307
|
+
return null;
|
|
308
|
+
const record = value;
|
|
309
|
+
if (record.kind === "local")
|
|
310
|
+
return { kind: "local", mode: "auto" };
|
|
311
|
+
if (record.kind === "remote" && typeof record.baseUrl === "string" && record.baseUrl.trim()) {
|
|
312
|
+
const baseUrl = record.baseUrl.trim().replace(/\/+$/, "");
|
|
313
|
+
return { kind: "remote", baseUrl };
|
|
314
|
+
}
|
|
315
|
+
return null;
|
|
316
|
+
}
|
|
317
|
+
function readGlobalConnections(options = {}) {
|
|
318
|
+
const path = resolveGlobalConnectionsPath(options.env ?? process.env);
|
|
319
|
+
const payload = readJsonFile(path);
|
|
320
|
+
if (!payload || typeof payload !== "object" || Array.isArray(payload)) {
|
|
321
|
+
return { connections: {} };
|
|
322
|
+
}
|
|
323
|
+
const rawConnections = payload.connections;
|
|
324
|
+
const connections = {};
|
|
325
|
+
if (rawConnections && typeof rawConnections === "object" && !Array.isArray(rawConnections)) {
|
|
326
|
+
for (const [alias, raw] of Object.entries(rawConnections)) {
|
|
327
|
+
const connection = normalizeConnection(raw);
|
|
328
|
+
if (connection)
|
|
329
|
+
connections[alias] = connection;
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
return { connections };
|
|
333
|
+
}
|
|
334
|
+
function readRepoConnection(projectRoot) {
|
|
335
|
+
const payload = readJsonFile(resolveRepoConnectionPath(projectRoot));
|
|
336
|
+
if (!payload || typeof payload !== "object" || Array.isArray(payload))
|
|
337
|
+
return null;
|
|
338
|
+
const record = payload;
|
|
339
|
+
const selected = typeof record.selected === "string" ? record.selected.trim() : "";
|
|
340
|
+
if (!selected)
|
|
341
|
+
return null;
|
|
342
|
+
return {
|
|
343
|
+
selected,
|
|
344
|
+
project: typeof record.project === "string" ? record.project : undefined,
|
|
345
|
+
linkedAt: typeof record.linkedAt === "string" ? record.linkedAt : undefined,
|
|
346
|
+
serverProjectRoot: typeof record.serverProjectRoot === "string" && record.serverProjectRoot.trim() ? record.serverProjectRoot.trim() : undefined
|
|
347
|
+
};
|
|
348
|
+
}
|
|
349
|
+
function writeRepoConnection(projectRoot, state) {
|
|
350
|
+
writeJsonFile(resolveRepoConnectionPath(projectRoot), state);
|
|
351
|
+
}
|
|
352
|
+
function resolveSelectedConnection(projectRoot, options = {}) {
|
|
353
|
+
const repo = readRepoConnection(projectRoot);
|
|
354
|
+
if (!repo)
|
|
355
|
+
return null;
|
|
356
|
+
if (repo.selected === "local")
|
|
357
|
+
return { alias: "local", connection: { kind: "local", mode: "auto" }, serverProjectRoot: repo.serverProjectRoot };
|
|
358
|
+
const global = readGlobalConnections(options);
|
|
359
|
+
const connection = global.connections[repo.selected];
|
|
360
|
+
if (!connection) {
|
|
361
|
+
throw new CliError(`Selected Rig server "${repo.selected}" was not found. Run \`rig server list\` or \`rig server use local\`.`, 1);
|
|
362
|
+
}
|
|
363
|
+
return { alias: repo.selected, connection, serverProjectRoot: repo.serverProjectRoot };
|
|
364
|
+
}
|
|
365
|
+
function writeRepoServerProjectRoot(projectRoot, serverProjectRoot) {
|
|
366
|
+
const repo = readRepoConnection(projectRoot);
|
|
367
|
+
if (!repo)
|
|
368
|
+
return;
|
|
369
|
+
writeRepoConnection(projectRoot, { ...repo, serverProjectRoot });
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
// packages/cli/src/commands/_server-client.ts
|
|
373
|
+
var scopedGitHubBearerTokens = new Map;
|
|
374
|
+
var serverPhaseListener = null;
|
|
375
|
+
function setServerPhaseListener(listener) {
|
|
376
|
+
const previous = serverPhaseListener;
|
|
377
|
+
serverPhaseListener = listener;
|
|
378
|
+
return previous;
|
|
379
|
+
}
|
|
380
|
+
function reportServerPhase(label) {
|
|
381
|
+
serverPhaseListener?.(label);
|
|
382
|
+
}
|
|
383
|
+
function cleanToken(value) {
|
|
384
|
+
const trimmed = value?.trim();
|
|
385
|
+
return trimmed ? trimmed : null;
|
|
386
|
+
}
|
|
387
|
+
function readPrivateRemoteSessionToken(projectRoot) {
|
|
388
|
+
const path = resolve2(projectRoot, ".rig", "state", "github-auth.json");
|
|
389
|
+
if (!existsSync2(path))
|
|
390
|
+
return null;
|
|
391
|
+
try {
|
|
392
|
+
const parsed = JSON.parse(readFileSync2(path, "utf8"));
|
|
393
|
+
return cleanToken(typeof parsed.apiSessionToken === "string" ? parsed.apiSessionToken : typeof parsed.sessionToken === "string" ? parsed.sessionToken : undefined);
|
|
394
|
+
} catch {
|
|
395
|
+
return null;
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
function readGitHubBearerTokenForRemote(projectRoot) {
|
|
399
|
+
const scopedKey = resolve2(projectRoot);
|
|
400
|
+
if (scopedGitHubBearerTokens.has(scopedKey))
|
|
401
|
+
return scopedGitHubBearerTokens.get(scopedKey) ?? null;
|
|
402
|
+
const privateSession = readPrivateRemoteSessionToken(projectRoot);
|
|
403
|
+
if (privateSession)
|
|
404
|
+
return privateSession;
|
|
405
|
+
return cleanToken(process.env.RIG_SERVER_AUTH_TOKEN) ?? cleanToken(process.env.RIG_REMOTE_AUTH_TOKEN);
|
|
406
|
+
}
|
|
407
|
+
function readStoredGitHubAuthToken(projectRoot) {
|
|
408
|
+
const path = resolve2(projectRoot, ".rig", "state", "github-auth.json");
|
|
409
|
+
if (!existsSync2(path))
|
|
410
|
+
return null;
|
|
411
|
+
try {
|
|
412
|
+
const parsed = JSON.parse(readFileSync2(path, "utf8"));
|
|
413
|
+
return cleanToken(typeof parsed.token === "string" ? parsed.token : undefined);
|
|
414
|
+
} catch {
|
|
415
|
+
return null;
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
function readLocalConnectionFallbackToken(projectRoot) {
|
|
419
|
+
return readGitHubBearerTokenForRemote(projectRoot) ?? cleanToken(process.env.RIG_GITHUB_TOKEN) ?? readStoredGitHubAuthToken(projectRoot);
|
|
420
|
+
}
|
|
421
|
+
async function ensureServerForCli(projectRoot) {
|
|
422
|
+
try {
|
|
423
|
+
const selected = resolveSelectedConnection(projectRoot);
|
|
424
|
+
if (selected?.connection.kind === "remote") {
|
|
425
|
+
reportServerPhase(`Connecting to ${selected.alias}\u2026`);
|
|
426
|
+
const authToken = readGitHubBearerTokenForRemote(projectRoot);
|
|
427
|
+
const serverProjectRoot = selected.serverProjectRoot ?? await backfillRemoteServerProjectRoot(projectRoot, selected.connection.baseUrl, authToken);
|
|
428
|
+
return {
|
|
429
|
+
baseUrl: selected.connection.baseUrl,
|
|
430
|
+
authToken,
|
|
431
|
+
connectionKind: "remote",
|
|
432
|
+
serverProjectRoot
|
|
433
|
+
};
|
|
434
|
+
}
|
|
435
|
+
reportServerPhase("Starting local Rig server\u2026");
|
|
436
|
+
const connection = await ensureLocalRigServerConnection(projectRoot);
|
|
437
|
+
return {
|
|
438
|
+
baseUrl: connection.baseUrl,
|
|
439
|
+
authToken: connection.authToken ?? readLocalConnectionFallbackToken(projectRoot),
|
|
440
|
+
connectionKind: "local",
|
|
441
|
+
serverProjectRoot: resolve2(projectRoot)
|
|
442
|
+
};
|
|
443
|
+
} catch (error) {
|
|
444
|
+
if (error instanceof Error) {
|
|
445
|
+
throw new CliError(error.message, 1);
|
|
446
|
+
}
|
|
447
|
+
throw error;
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
async function backfillRemoteServerProjectRoot(projectRoot, baseUrl, authToken) {
|
|
451
|
+
const repo = readRepoConnection(projectRoot);
|
|
452
|
+
const slug = repo?.project?.trim();
|
|
453
|
+
if (!slug)
|
|
454
|
+
return null;
|
|
455
|
+
try {
|
|
456
|
+
const response = await fetch(`${baseUrl}/api/projects/${encodeURIComponent(slug)}`, {
|
|
457
|
+
headers: mergeHeaders(undefined, authToken)
|
|
458
|
+
});
|
|
459
|
+
if (!response.ok)
|
|
460
|
+
return null;
|
|
461
|
+
const payload = await response.json();
|
|
462
|
+
const project = payload.project && typeof payload.project === "object" && !Array.isArray(payload.project) ? payload.project : null;
|
|
463
|
+
const checkouts = Array.isArray(project?.checkouts) ? project.checkouts : [];
|
|
464
|
+
const latestCheckout = [...checkouts].reverse().find((entry) => Boolean(entry && typeof entry === "object" && !Array.isArray(entry) && typeof entry.path === "string"));
|
|
465
|
+
const path = typeof latestCheckout?.path === "string" && latestCheckout.path.trim() ? latestCheckout.path.trim() : null;
|
|
466
|
+
if (path)
|
|
467
|
+
writeRepoServerProjectRoot(projectRoot, path);
|
|
468
|
+
return path;
|
|
469
|
+
} catch {
|
|
470
|
+
return null;
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
function mergeHeaders(headers, authToken) {
|
|
474
|
+
const merged = new Headers(headers);
|
|
475
|
+
if (authToken) {
|
|
476
|
+
merged.set("authorization", `Bearer ${authToken}`);
|
|
477
|
+
}
|
|
478
|
+
return merged;
|
|
479
|
+
}
|
|
480
|
+
function diagnosticMessage(payload) {
|
|
481
|
+
if (!payload || typeof payload !== "object" || Array.isArray(payload))
|
|
482
|
+
return null;
|
|
483
|
+
const record = payload;
|
|
484
|
+
const diagnostics = Array.isArray(record.diagnostics) ? record.diagnostics : [];
|
|
485
|
+
const messages = diagnostics.flatMap((entry) => {
|
|
486
|
+
if (!entry || typeof entry !== "object" || Array.isArray(entry))
|
|
487
|
+
return [];
|
|
488
|
+
const diagnostic = entry;
|
|
489
|
+
const kind = typeof diagnostic.kind === "string" ? diagnostic.kind : "task-source";
|
|
490
|
+
const message = typeof diagnostic.message === "string" ? diagnostic.message : null;
|
|
491
|
+
return message ? [`${kind}: ${message}`] : [];
|
|
492
|
+
});
|
|
493
|
+
return messages.length > 0 ? messages.join("; ") : null;
|
|
494
|
+
}
|
|
495
|
+
var serverReachabilityCache = new Map;
|
|
496
|
+
async function probeServerReachability(baseUrl, authToken) {
|
|
497
|
+
try {
|
|
498
|
+
const response = await fetch(`${baseUrl.replace(/\/+$/, "")}/api/server/status`, {
|
|
499
|
+
headers: mergeHeaders(undefined, authToken),
|
|
500
|
+
signal: AbortSignal.timeout(1500)
|
|
501
|
+
});
|
|
502
|
+
return response.ok;
|
|
503
|
+
} catch {
|
|
504
|
+
return false;
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
function cachedServerReachability(projectRoot, baseUrl, authToken) {
|
|
508
|
+
const key = resolve2(projectRoot);
|
|
509
|
+
const cached = serverReachabilityCache.get(key);
|
|
510
|
+
if (cached)
|
|
511
|
+
return cached;
|
|
512
|
+
const probe = probeServerReachability(baseUrl, authToken);
|
|
513
|
+
serverReachabilityCache.set(key, probe);
|
|
514
|
+
return probe;
|
|
515
|
+
}
|
|
516
|
+
function describeSelectedServer(projectRoot, server) {
|
|
517
|
+
try {
|
|
518
|
+
const selected = resolveSelectedConnection(projectRoot);
|
|
519
|
+
if (selected) {
|
|
520
|
+
return {
|
|
521
|
+
alias: selected.alias,
|
|
522
|
+
target: selected.connection.kind === "remote" ? selected.connection.baseUrl : server.baseUrl
|
|
523
|
+
};
|
|
524
|
+
}
|
|
525
|
+
} catch {}
|
|
526
|
+
return { alias: server.connectionKind === "remote" ? "remote" : "local", target: server.baseUrl };
|
|
527
|
+
}
|
|
528
|
+
async function buildServerFailureContext(projectRoot, server) {
|
|
529
|
+
const { alias, target } = describeSelectedServer(projectRoot, server);
|
|
530
|
+
const reachable = await cachedServerReachability(projectRoot, server.baseUrl, server.authToken);
|
|
531
|
+
const reachability = reachable ? "server is reachable" : "server is unreachable";
|
|
532
|
+
return {
|
|
533
|
+
contextLine: `Currently connected to: ${alias} at ${target} (${reachability}).`,
|
|
534
|
+
hint: "Check the selected server with `rig server status`, or switch with `rig server use <alias|local>`."
|
|
535
|
+
};
|
|
536
|
+
}
|
|
537
|
+
async function requestServerJson(context, pathname, init = {}) {
|
|
538
|
+
const server = await ensureServerForCli(context.projectRoot);
|
|
539
|
+
const headers = mergeHeaders(init.headers, server.authToken);
|
|
540
|
+
if (server.serverProjectRoot)
|
|
541
|
+
headers.set("x-rig-project-root", server.serverProjectRoot);
|
|
542
|
+
reportServerPhase(`${(init.method ?? "GET").toUpperCase()} ${pathname.split("?")[0]}\u2026`);
|
|
543
|
+
let response;
|
|
544
|
+
try {
|
|
545
|
+
response = await fetch(`${server.baseUrl}${pathname}`, {
|
|
546
|
+
...init,
|
|
547
|
+
headers
|
|
548
|
+
});
|
|
549
|
+
} catch (error) {
|
|
550
|
+
const failure = await buildServerFailureContext(context.projectRoot, server);
|
|
551
|
+
throw new CliError(`Rig server request failed: ${error instanceof Error ? error.message : String(error)}
|
|
552
|
+
${failure.contextLine}`, 1, { hint: failure.hint });
|
|
553
|
+
}
|
|
554
|
+
const text = await response.text();
|
|
555
|
+
const payload = text.trim().length > 0 ? (() => {
|
|
556
|
+
try {
|
|
557
|
+
return JSON.parse(text);
|
|
558
|
+
} catch {
|
|
559
|
+
return null;
|
|
560
|
+
}
|
|
561
|
+
})() : null;
|
|
562
|
+
if (!response.ok) {
|
|
563
|
+
const diagnostics = diagnosticMessage(payload);
|
|
564
|
+
const rawDetail = diagnostics ?? (text || response.statusText);
|
|
565
|
+
const detail = diagnostics ? rawDetail : rawDetail.split(`
|
|
566
|
+
`).map((line) => line.trim()).find((line) => line.length > 0)?.slice(0, 200) ?? response.statusText;
|
|
567
|
+
const failure = await buildServerFailureContext(context.projectRoot, server);
|
|
568
|
+
throw new CliError(`Rig server request failed (${response.status}): ${detail}
|
|
569
|
+
${failure.contextLine}`, 1, { hint: failure.hint });
|
|
570
|
+
}
|
|
571
|
+
return payload;
|
|
572
|
+
}
|
|
573
|
+
var RESUMABLE_RUN_STATUSES = new Set([
|
|
574
|
+
"created",
|
|
575
|
+
"preparing",
|
|
576
|
+
"running",
|
|
577
|
+
"validating",
|
|
578
|
+
"reviewing",
|
|
579
|
+
"stopped",
|
|
580
|
+
"failed",
|
|
581
|
+
"needs-attention",
|
|
582
|
+
"needs_attention"
|
|
583
|
+
]);
|
|
584
|
+
|
|
585
|
+
// packages/cli/src/commands/_async-ui.ts
|
|
586
|
+
import pc2 from "picocolors";
|
|
587
|
+
var DONE_SYMBOL = pc2.green("\u25C7");
|
|
588
|
+
var FAIL_SYMBOL = pc2.red("\u25A0");
|
|
589
|
+
var activeUpdate = null;
|
|
590
|
+
async function withSpinner(label, work, options = {}) {
|
|
591
|
+
if (options.outputMode === "json") {
|
|
592
|
+
return work(() => {});
|
|
593
|
+
}
|
|
594
|
+
if (activeUpdate) {
|
|
595
|
+
const outer = activeUpdate;
|
|
596
|
+
outer(label);
|
|
597
|
+
return work(outer);
|
|
598
|
+
}
|
|
599
|
+
const output = options.output ?? process.stderr;
|
|
600
|
+
const isTty = output.isTTY === true;
|
|
601
|
+
let lastLabel = label;
|
|
602
|
+
if (!isTty) {
|
|
603
|
+
output.write(`${label}
|
|
604
|
+
`);
|
|
605
|
+
const update2 = (next) => {
|
|
606
|
+
lastLabel = next;
|
|
607
|
+
};
|
|
608
|
+
activeUpdate = update2;
|
|
609
|
+
const previousListener2 = setServerPhaseListener(update2);
|
|
610
|
+
try {
|
|
611
|
+
return await work(update2);
|
|
612
|
+
} finally {
|
|
613
|
+
activeUpdate = null;
|
|
614
|
+
setServerPhaseListener(previousListener2);
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
const spinner = createTtySpinner({
|
|
618
|
+
label,
|
|
619
|
+
output,
|
|
620
|
+
frames: MICRO_DRONE_FRAMES,
|
|
621
|
+
styleFrame: (frame) => renderMicroDroneFrame(Math.max(0, MICRO_DRONE_FRAMES.indexOf(frame)))
|
|
622
|
+
});
|
|
623
|
+
const update = (next) => {
|
|
624
|
+
lastLabel = next;
|
|
625
|
+
spinner.setLabel(next);
|
|
626
|
+
};
|
|
627
|
+
activeUpdate = update;
|
|
628
|
+
const previousListener = setServerPhaseListener(update);
|
|
629
|
+
try {
|
|
630
|
+
const result = await work(update);
|
|
631
|
+
spinner.stop(options.doneLabel ? `${DONE_SYMBOL} ${options.doneLabel}` : undefined);
|
|
632
|
+
return result;
|
|
633
|
+
} catch (error) {
|
|
634
|
+
spinner.stop(`${FAIL_SYMBOL} ${lastLabel}`);
|
|
635
|
+
throw error;
|
|
636
|
+
} finally {
|
|
637
|
+
activeUpdate = null;
|
|
638
|
+
setServerPhaseListener(previousListener);
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
|
|
41
642
|
// packages/cli/src/commands/inbox.ts
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
643
|
+
async function listInboxRecords(context, kind, filters) {
|
|
644
|
+
const params = new URLSearchParams;
|
|
645
|
+
if (filters.run)
|
|
646
|
+
params.set("runId", filters.run);
|
|
647
|
+
if (filters.task)
|
|
648
|
+
params.set("taskId", filters.task);
|
|
649
|
+
const query = params.size > 0 ? `?${params.toString()}` : "";
|
|
650
|
+
const payload = await requestServerJson(context, `/api/inbox/${kind}${query}`);
|
|
651
|
+
const records = Array.isArray(payload) ? payload : [];
|
|
652
|
+
return filters.pendingOnly ? records.filter((entry) => (entry.status ?? "pending") !== "resolved") : records;
|
|
653
|
+
}
|
|
654
|
+
async function readPendingInboxCounts(context) {
|
|
655
|
+
try {
|
|
656
|
+
const [approvals, inputs] = await Promise.all([
|
|
657
|
+
listInboxRecords(context, "approvals", { pendingOnly: true }),
|
|
658
|
+
listInboxRecords(context, "inputs", { pendingOnly: true })
|
|
659
|
+
]);
|
|
660
|
+
return { approvals: approvals.length, inputs: inputs.length };
|
|
661
|
+
} catch {
|
|
662
|
+
return null;
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
async function printPendingInboxFooter(context) {
|
|
666
|
+
if (context.outputMode !== "text")
|
|
667
|
+
return;
|
|
668
|
+
const counts = await readPendingInboxCounts(context);
|
|
669
|
+
if (!counts || counts.approvals === 0 && counts.inputs === 0)
|
|
670
|
+
return;
|
|
671
|
+
const parts = [];
|
|
672
|
+
if (counts.approvals > 0)
|
|
673
|
+
parts.push(`${counts.approvals} approval${counts.approvals === 1 ? "" : "s"}`);
|
|
674
|
+
if (counts.inputs > 0)
|
|
675
|
+
parts.push(`${counts.inputs} input request${counts.inputs === 1 ? "" : "s"}`);
|
|
676
|
+
console.log(`
|
|
677
|
+
\u26A0 ${parts.join(" and ")} pending \u2014 run \`rig inbox\` to review.`);
|
|
678
|
+
}
|
|
679
|
+
function renderList(context, kind, records) {
|
|
680
|
+
if (context.outputMode === "text") {
|
|
681
|
+
printFormattedOutput(formatInboxList(kind, records));
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
async function watchInbox(context, filters) {
|
|
685
|
+
const render = async () => {
|
|
686
|
+
const [approvals, inputs] = await Promise.all([
|
|
687
|
+
listInboxRecords(context, "approvals", { ...filters, pendingOnly: true }),
|
|
688
|
+
listInboxRecords(context, "inputs", { ...filters, pendingOnly: true })
|
|
689
|
+
]);
|
|
690
|
+
console.clear();
|
|
691
|
+
console.log(`rig inbox \u2014 watching (Ctrl-C to stop) \u2014 ${new Date().toLocaleTimeString()}`);
|
|
692
|
+
renderList(context, "approvals", approvals);
|
|
693
|
+
renderList(context, "inputs", inputs);
|
|
694
|
+
if (approvals.length === 0 && inputs.length === 0) {
|
|
695
|
+
console.log("Nothing pending.");
|
|
696
|
+
} else {
|
|
697
|
+
console.log(`
|
|
698
|
+
Resolve with: rig inbox approve --run <id> --request <id> --decision approve|reject`);
|
|
699
|
+
}
|
|
700
|
+
};
|
|
701
|
+
await render();
|
|
702
|
+
const server = await ensureServerForCli(context.projectRoot);
|
|
703
|
+
const relevant = /"type":"rig\.(approval|user-input)\./;
|
|
704
|
+
const fallbackTimer = setInterval(() => {
|
|
705
|
+
render();
|
|
706
|
+
}, 30000);
|
|
707
|
+
fallbackTimer.unref?.();
|
|
708
|
+
for (;; ) {
|
|
709
|
+
try {
|
|
710
|
+
const response = await fetch(`${server.baseUrl}/api/server/events`, {
|
|
711
|
+
headers: {
|
|
712
|
+
...server.authToken ? { authorization: `Bearer ${server.authToken}` } : {},
|
|
713
|
+
...server.serverProjectRoot ? { "x-rig-project-root": server.serverProjectRoot } : {},
|
|
714
|
+
accept: "text/event-stream"
|
|
715
|
+
}
|
|
716
|
+
});
|
|
717
|
+
if (!response.ok || !response.body)
|
|
718
|
+
throw new Error(`SSE ${response.status}`);
|
|
719
|
+
const reader = response.body.getReader();
|
|
720
|
+
const decoder = new TextDecoder;
|
|
721
|
+
for (;; ) {
|
|
722
|
+
const { done, value } = await reader.read();
|
|
723
|
+
if (done)
|
|
724
|
+
break;
|
|
725
|
+
const chunk = decoder.decode(value, { stream: true });
|
|
726
|
+
if (relevant.test(chunk)) {
|
|
727
|
+
await render();
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
} catch {
|
|
731
|
+
await render();
|
|
732
|
+
await new Promise((resolveSleep) => setTimeout(resolveSleep, 2000));
|
|
733
|
+
}
|
|
734
|
+
}
|
|
735
|
+
}
|
|
47
736
|
async function executeInbox(context, args) {
|
|
48
737
|
const [command = "approvals", ...rest] = args;
|
|
49
738
|
switch (command) {
|
|
@@ -53,19 +742,22 @@ async function executeInbox(context, args) {
|
|
|
53
742
|
pending = run.rest;
|
|
54
743
|
const task = takeOption(pending, "--task");
|
|
55
744
|
pending = task.rest;
|
|
56
|
-
requireNoExtraArgs(pending, "
|
|
57
|
-
const
|
|
58
|
-
|
|
59
|
-
runId: entry.runId,
|
|
60
|
-
record
|
|
61
|
-
})));
|
|
62
|
-
if (context.outputMode === "text") {
|
|
63
|
-
for (const approval of approvals) {
|
|
64
|
-
console.log(JSON.stringify(approval));
|
|
65
|
-
}
|
|
66
|
-
}
|
|
745
|
+
requireNoExtraArgs(pending, "rig inbox approvals [--run <id>] [--task <id>]");
|
|
746
|
+
const approvals = await withSpinner("Reading approvals from server\u2026", () => listInboxRecords(context, "approvals", { run: run.value, task: task.value }), { outputMode: context.outputMode });
|
|
747
|
+
renderList(context, "approvals", approvals);
|
|
67
748
|
return { ok: true, group: "inbox", command, details: { approvals } };
|
|
68
749
|
}
|
|
750
|
+
case "inputs": {
|
|
751
|
+
let pending = rest;
|
|
752
|
+
const run = takeOption(pending, "--run");
|
|
753
|
+
pending = run.rest;
|
|
754
|
+
const task = takeOption(pending, "--task");
|
|
755
|
+
pending = task.rest;
|
|
756
|
+
requireNoExtraArgs(pending, "rig inbox inputs [--run <id>] [--task <id>]");
|
|
757
|
+
const requests = await withSpinner("Reading input requests from server\u2026", () => listInboxRecords(context, "inputs", { run: run.value, task: task.value }), { outputMode: context.outputMode });
|
|
758
|
+
renderList(context, "inputs", requests);
|
|
759
|
+
return { ok: true, group: "inbox", command, details: { requests } };
|
|
760
|
+
}
|
|
69
761
|
case "approve": {
|
|
70
762
|
let pending = rest;
|
|
71
763
|
const run = takeOption(pending, "--run");
|
|
@@ -76,40 +768,24 @@ async function executeInbox(context, args) {
|
|
|
76
768
|
pending = decision.rest;
|
|
77
769
|
const note = takeOption(pending, "--note");
|
|
78
770
|
pending = note.rest;
|
|
79
|
-
requireNoExtraArgs(pending, "
|
|
771
|
+
requireNoExtraArgs(pending, "rig inbox approve --run <id> --request <id> --decision approve|reject [--note <text>]");
|
|
80
772
|
if (!run.value || !request.value || !decision.value) {
|
|
81
|
-
throw new
|
|
773
|
+
throw new CliError("approve requires --run, --request, and --decision. List pending requests with `rig inbox approvals`.");
|
|
82
774
|
}
|
|
83
775
|
if (decision.value !== "approve" && decision.value !== "reject") {
|
|
84
|
-
throw new
|
|
776
|
+
throw new CliError("decision must be approve or reject.", 1, { hint: "Re-run as `rig inbox approve --run <id> --request <id> --decision approve|reject`." });
|
|
85
777
|
}
|
|
86
|
-
const
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
const run = takeOption(pending, "--run");
|
|
98
|
-
pending = run.rest;
|
|
99
|
-
const task = takeOption(pending, "--task");
|
|
100
|
-
pending = task.rest;
|
|
101
|
-
requireNoExtraArgs(pending, "bun run rig inbox inputs [--run <id>] [--task <id>]");
|
|
102
|
-
const runs = listAuthorityRuns(context.projectRoot).filter((entry) => (!run.value || entry.runId === run.value) && (!task.value || entry.taskId === task.value));
|
|
103
|
-
const requests = runs.flatMap((entry) => readJsonlFile(resolve(resolveAuthorityRunDir(context.projectRoot, entry.runId), "user-input.jsonl")).map((record) => ({
|
|
104
|
-
runId: entry.runId,
|
|
105
|
-
record
|
|
106
|
-
})));
|
|
107
|
-
if (context.outputMode === "text") {
|
|
108
|
-
for (const request of requests) {
|
|
109
|
-
console.log(JSON.stringify(request));
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
return { ok: true, group: "inbox", command, details: { requests } };
|
|
778
|
+
const result = await withSpinner(`Resolving approval ${request.value}\u2026`, () => requestServerJson(context, "/api/inbox/approvals/resolve", {
|
|
779
|
+
method: "POST",
|
|
780
|
+
headers: { "content-type": "application/json" },
|
|
781
|
+
body: JSON.stringify({
|
|
782
|
+
runId: run.value,
|
|
783
|
+
requestId: request.value,
|
|
784
|
+
decision: decision.value,
|
|
785
|
+
note: note.value ?? null
|
|
786
|
+
})
|
|
787
|
+
}), { outputMode: context.outputMode });
|
|
788
|
+
return { ok: true, group: "inbox", command, details: { result } };
|
|
113
789
|
}
|
|
114
790
|
case "respond": {
|
|
115
791
|
let pending = rest;
|
|
@@ -122,11 +798,11 @@ async function executeInbox(context, args) {
|
|
|
122
798
|
for (let index = 0;index < pending.length; index += 1) {
|
|
123
799
|
const current = pending[index];
|
|
124
800
|
if (current === "--answer") {
|
|
125
|
-
const
|
|
126
|
-
if (!
|
|
127
|
-
throw new
|
|
801
|
+
const next = pending[index + 1];
|
|
802
|
+
if (!next || next.startsWith("-")) {
|
|
803
|
+
throw new CliError("Missing value for --answer", 1, { hint: "Pass key=value pairs, e.g. `rig inbox respond --run <id> --request <id> --answer key=value`." });
|
|
128
804
|
}
|
|
129
|
-
answers.push(
|
|
805
|
+
answers.push(next);
|
|
130
806
|
index += 1;
|
|
131
807
|
continue;
|
|
132
808
|
}
|
|
@@ -134,27 +810,41 @@ async function executeInbox(context, args) {
|
|
|
134
810
|
remaining.push(current);
|
|
135
811
|
}
|
|
136
812
|
}
|
|
137
|
-
requireNoExtraArgs(remaining, "
|
|
813
|
+
requireNoExtraArgs(remaining, "rig inbox respond --run <id> --request <id> --answer key=value [--answer key=value]");
|
|
138
814
|
if (!run.value || !request.value || answers.length === 0) {
|
|
139
|
-
throw new
|
|
815
|
+
throw new CliError("respond requires --run, --request, and at least one --answer. List pending requests with `rig inbox inputs`.");
|
|
140
816
|
}
|
|
141
817
|
const parsedAnswers = Object.fromEntries(answers.map((entry) => {
|
|
142
818
|
const [key, ...restValue] = entry.split("=");
|
|
143
819
|
return [key, restValue.join("=")];
|
|
144
820
|
}));
|
|
145
|
-
const
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
821
|
+
const result = await withSpinner(`Sending response for ${request.value}\u2026`, () => requestServerJson(context, "/api/inbox/inputs/respond", {
|
|
822
|
+
method: "POST",
|
|
823
|
+
headers: { "content-type": "application/json" },
|
|
824
|
+
body: JSON.stringify({
|
|
825
|
+
runId: run.value,
|
|
826
|
+
requestId: request.value,
|
|
827
|
+
answers: parsedAnswers
|
|
828
|
+
})
|
|
829
|
+
}), { outputMode: context.outputMode });
|
|
830
|
+
return { ok: true, group: "inbox", command, details: { result } };
|
|
831
|
+
}
|
|
832
|
+
case "watch": {
|
|
833
|
+
let pending = rest;
|
|
834
|
+
const run = takeOption(pending, "--run");
|
|
835
|
+
pending = run.rest;
|
|
836
|
+
const task = takeOption(pending, "--task");
|
|
837
|
+
pending = task.rest;
|
|
838
|
+
requireNoExtraArgs(pending, "rig inbox watch [--run <id>] [--task <id>]");
|
|
839
|
+
return await watchInbox(context, { run: run.value, task: task.value });
|
|
153
840
|
}
|
|
154
841
|
default:
|
|
155
|
-
throw new
|
|
842
|
+
throw new CliError(`Unknown inbox command: ${command}. Use approvals|inputs|approve|respond|watch.`);
|
|
156
843
|
}
|
|
157
844
|
}
|
|
158
845
|
export {
|
|
846
|
+
readPendingInboxCounts,
|
|
847
|
+
printPendingInboxFooter,
|
|
848
|
+
listInboxRecords,
|
|
159
849
|
executeInbox
|
|
160
850
|
};
|