@h-rig/cli 0.0.6-alpha.8 → 0.0.6-alpha.81
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 +18374 -8205
- package/dist/src/app/board.d.ts +23 -0
- package/dist/src/app/board.js +1815 -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 +151 -0
- package/dist/src/app-opentui/adapters/common.d.ts +53 -0
- package/dist/src/app-opentui/adapters/common.js +149 -0
- package/dist/src/app-opentui/adapters/doctor.d.ts +11 -0
- package/dist/src/app-opentui/adapters/doctor.js +780 -0
- package/dist/src/app-opentui/adapters/fleet.d.ts +16 -0
- package/dist/src/app-opentui/adapters/fleet.js +874 -0
- package/dist/src/app-opentui/adapters/inbox.d.ts +33 -0
- package/dist/src/app-opentui/adapters/inbox.js +1454 -0
- package/dist/src/app-opentui/adapters/init.d.ts +13 -0
- package/dist/src/app-opentui/adapters/init.js +2357 -0
- package/dist/src/app-opentui/adapters/pi-attach.d.ts +16 -0
- package/dist/src/app-opentui/adapters/pi-attach.js +1295 -0
- package/dist/src/app-opentui/adapters/run-detail.d.ts +20 -0
- package/dist/src/app-opentui/adapters/run-detail.js +893 -0
- package/dist/src/app-opentui/adapters/server.d.ts +14 -0
- package/dist/src/app-opentui/adapters/server.js +798 -0
- package/dist/src/app-opentui/adapters/tasks.d.ts +55 -0
- package/dist/src/app-opentui/adapters/tasks.js +3472 -0
- package/dist/src/app-opentui/bootstrap.d.ts +16 -0
- package/dist/src/app-opentui/bootstrap.js +19509 -0
- package/dist/src/app-opentui/drone.d.ts +12 -0
- package/dist/src/app-opentui/drone.js +227 -0
- package/dist/src/app-opentui/events.d.ts +7 -0
- package/dist/src/app-opentui/events.js +28 -0
- package/dist/src/app-opentui/index.d.ts +8 -0
- package/dist/src/app-opentui/index.js +3477 -0
- package/dist/src/app-opentui/intent.d.ts +3 -0
- package/dist/src/app-opentui/intent.js +211 -0
- package/dist/src/app-opentui/layout.d.ts +15 -0
- package/dist/src/app-opentui/layout.js +44 -0
- package/dist/src/app-opentui/list-search.d.ts +8 -0
- package/dist/src/app-opentui/list-search.js +43 -0
- package/dist/src/app-opentui/pi-host-child.d.ts +2 -0
- package/dist/src/app-opentui/pi-host-child.js +778 -0
- package/dist/src/app-opentui/pi-pty-host.d.ts +64 -0
- package/dist/src/app-opentui/pi-pty-host.js +384 -0
- package/dist/src/app-opentui/registry.d.ts +4 -0
- package/dist/src/app-opentui/registry.js +6835 -0
- package/dist/src/app-opentui/render/graphics.d.ts +39 -0
- package/dist/src/app-opentui/render/graphics.js +537 -0
- package/dist/src/app-opentui/render/image-visual-layer-native-canvas.d.ts +2 -0
- package/dist/src/app-opentui/render/image-visual-layer-native-canvas.js +1480 -0
- package/dist/src/app-opentui/render/image-visual-layer-worker.d.ts +1 -0
- package/dist/src/app-opentui/render/image-visual-layer-worker.js +1541 -0
- package/dist/src/app-opentui/render/image-visual-layer.d.ts +93 -0
- package/dist/src/app-opentui/render/image-visual-layer.js +945 -0
- package/dist/src/app-opentui/render/panels.d.ts +10 -0
- package/dist/src/app-opentui/render/panels.js +201 -0
- package/dist/src/app-opentui/render/scene.d.ts +16 -0
- package/dist/src/app-opentui/render/scene.js +110 -0
- package/dist/src/app-opentui/render/text.d.ts +10 -0
- package/dist/src/app-opentui/render/text.js +121 -0
- package/dist/src/app-opentui/render/type-bar.d.ts +14 -0
- package/dist/src/app-opentui/render/type-bar.js +137 -0
- package/dist/src/app-opentui/runtime.d.ts +2 -0
- package/dist/src/app-opentui/runtime.js +3406 -0
- package/dist/src/app-opentui/scenes/doctor.d.ts +2 -0
- package/dist/src/app-opentui/scenes/doctor.js +91 -0
- package/dist/src/app-opentui/scenes/error.d.ts +2 -0
- package/dist/src/app-opentui/scenes/error.js +190 -0
- package/dist/src/app-opentui/scenes/fleet.d.ts +3 -0
- package/dist/src/app-opentui/scenes/fleet.js +207 -0
- package/dist/src/app-opentui/scenes/handoff.d.ts +2 -0
- package/dist/src/app-opentui/scenes/handoff.js +147 -0
- package/dist/src/app-opentui/scenes/help.d.ts +3 -0
- package/dist/src/app-opentui/scenes/help.js +138 -0
- package/dist/src/app-opentui/scenes/inbox.d.ts +2 -0
- package/dist/src/app-opentui/scenes/inbox.js +118 -0
- package/dist/src/app-opentui/scenes/init.d.ts +2 -0
- package/dist/src/app-opentui/scenes/init.js +94 -0
- package/dist/src/app-opentui/scenes/main.d.ts +2 -0
- package/dist/src/app-opentui/scenes/main.js +96 -0
- package/dist/src/app-opentui/scenes/run-detail.d.ts +2 -0
- package/dist/src/app-opentui/scenes/run-detail.js +139 -0
- package/dist/src/app-opentui/scenes/server.d.ts +2 -0
- package/dist/src/app-opentui/scenes/server.js +88 -0
- package/dist/src/app-opentui/scenes/tasks.d.ts +3 -0
- package/dist/src/app-opentui/scenes/tasks.js +230 -0
- package/dist/src/app-opentui/state.d.ts +4 -0
- package/dist/src/app-opentui/state.js +286 -0
- package/dist/src/app-opentui/theme.d.ts +36 -0
- package/dist/src/app-opentui/theme.js +88 -0
- package/dist/src/app-opentui/types.d.ts +188 -0
- package/dist/src/app-opentui/types.js +1 -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 +10996 -6637
- package/dist/src/index.d.ts +4 -0
- package/dist/src/index.js +10197 -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 +16 -6
|
@@ -0,0 +1,874 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __returnValue = (v) => v;
|
|
4
|
+
function __exportSetter(name, newValue) {
|
|
5
|
+
this[name] = __returnValue.bind(null, newValue);
|
|
6
|
+
}
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, {
|
|
10
|
+
get: all[name],
|
|
11
|
+
enumerable: true,
|
|
12
|
+
configurable: true,
|
|
13
|
+
set: __exportSetter.bind(all, name)
|
|
14
|
+
});
|
|
15
|
+
};
|
|
16
|
+
var __esm = (fn, res) => () => (fn && (res = fn(fn = 0)), res);
|
|
17
|
+
|
|
18
|
+
// packages/cli/src/runner.ts
|
|
19
|
+
import { EventBus } from "@rig/runtime/control-plane/runtime/events";
|
|
20
|
+
import { CliError as RuntimeCliError } from "@rig/runtime/control-plane/errors";
|
|
21
|
+
import { evaluate, loadPolicy, resolveAction } from "@rig/runtime/control-plane/runtime/guard";
|
|
22
|
+
import { buildBinary } from "@rig/runtime/control-plane/runtime/isolation";
|
|
23
|
+
var CliError;
|
|
24
|
+
var init_runner = __esm(() => {
|
|
25
|
+
CliError = class CliError extends RuntimeCliError {
|
|
26
|
+
hint;
|
|
27
|
+
constructor(message, exitCode = 1, options = {}) {
|
|
28
|
+
super(message, exitCode);
|
|
29
|
+
if (options.hint?.trim()) {
|
|
30
|
+
this.hint = options.hint.trim();
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
// packages/cli/src/commands/_connection-state.ts
|
|
37
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs";
|
|
38
|
+
import { homedir } from "os";
|
|
39
|
+
import { dirname, resolve } from "path";
|
|
40
|
+
function resolveGlobalConnectionsPath(env = process.env) {
|
|
41
|
+
const explicit = env.RIG_CONNECTIONS_FILE?.trim();
|
|
42
|
+
if (explicit)
|
|
43
|
+
return resolve(explicit);
|
|
44
|
+
const stateDir = env.RIG_GLOBAL_STATE_DIR?.trim();
|
|
45
|
+
if (stateDir)
|
|
46
|
+
return resolve(stateDir, "connections.json");
|
|
47
|
+
return resolve(homedir(), ".rig", "connections.json");
|
|
48
|
+
}
|
|
49
|
+
function resolveRepoConnectionPath(projectRoot) {
|
|
50
|
+
return resolve(projectRoot, ".rig", "state", "connection.json");
|
|
51
|
+
}
|
|
52
|
+
function readJsonFile(path) {
|
|
53
|
+
if (!existsSync(path))
|
|
54
|
+
return null;
|
|
55
|
+
try {
|
|
56
|
+
return JSON.parse(readFileSync(path, "utf8"));
|
|
57
|
+
} catch (error) {
|
|
58
|
+
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>`." });
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
function writeJsonFile(path, value) {
|
|
62
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
63
|
+
writeFileSync(path, `${JSON.stringify(value, null, 2)}
|
|
64
|
+
`, "utf8");
|
|
65
|
+
}
|
|
66
|
+
function normalizeConnection(value) {
|
|
67
|
+
if (!value || typeof value !== "object" || Array.isArray(value))
|
|
68
|
+
return null;
|
|
69
|
+
const record = value;
|
|
70
|
+
if (record.kind === "local")
|
|
71
|
+
return { kind: "local", mode: "auto" };
|
|
72
|
+
if (record.kind === "remote" && typeof record.baseUrl === "string" && record.baseUrl.trim()) {
|
|
73
|
+
const baseUrl = record.baseUrl.trim().replace(/\/+$/, "");
|
|
74
|
+
return { kind: "remote", baseUrl };
|
|
75
|
+
}
|
|
76
|
+
return null;
|
|
77
|
+
}
|
|
78
|
+
function readGlobalConnections(options = {}) {
|
|
79
|
+
const path = resolveGlobalConnectionsPath(options.env ?? process.env);
|
|
80
|
+
const payload = readJsonFile(path);
|
|
81
|
+
if (!payload || typeof payload !== "object" || Array.isArray(payload)) {
|
|
82
|
+
return { connections: {} };
|
|
83
|
+
}
|
|
84
|
+
const rawConnections = payload.connections;
|
|
85
|
+
const connections = {};
|
|
86
|
+
if (rawConnections && typeof rawConnections === "object" && !Array.isArray(rawConnections)) {
|
|
87
|
+
for (const [alias, raw] of Object.entries(rawConnections)) {
|
|
88
|
+
const connection = normalizeConnection(raw);
|
|
89
|
+
if (connection)
|
|
90
|
+
connections[alias] = connection;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return { connections };
|
|
94
|
+
}
|
|
95
|
+
function readRepoConnection(projectRoot) {
|
|
96
|
+
const payload = readJsonFile(resolveRepoConnectionPath(projectRoot));
|
|
97
|
+
if (!payload || typeof payload !== "object" || Array.isArray(payload))
|
|
98
|
+
return null;
|
|
99
|
+
const record = payload;
|
|
100
|
+
const selected = typeof record.selected === "string" ? record.selected.trim() : "";
|
|
101
|
+
if (!selected)
|
|
102
|
+
return null;
|
|
103
|
+
return {
|
|
104
|
+
selected,
|
|
105
|
+
project: typeof record.project === "string" ? record.project : undefined,
|
|
106
|
+
linkedAt: typeof record.linkedAt === "string" ? record.linkedAt : undefined,
|
|
107
|
+
serverProjectRoot: typeof record.serverProjectRoot === "string" && record.serverProjectRoot.trim() ? record.serverProjectRoot.trim() : undefined
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
function writeRepoConnection(projectRoot, state) {
|
|
111
|
+
writeJsonFile(resolveRepoConnectionPath(projectRoot), state);
|
|
112
|
+
}
|
|
113
|
+
function resolveSelectedConnection(projectRoot, options = {}) {
|
|
114
|
+
const repo = readRepoConnection(projectRoot);
|
|
115
|
+
if (!repo)
|
|
116
|
+
return null;
|
|
117
|
+
if (repo.selected === "local")
|
|
118
|
+
return { alias: "local", connection: { kind: "local", mode: "auto" }, serverProjectRoot: repo.serverProjectRoot };
|
|
119
|
+
const global = readGlobalConnections(options);
|
|
120
|
+
const connection = global.connections[repo.selected];
|
|
121
|
+
if (!connection) {
|
|
122
|
+
throw new CliError(`Selected Rig server "${repo.selected}" was not found. Run \`rig server list\` or \`rig server use local\`.`, 1);
|
|
123
|
+
}
|
|
124
|
+
return { alias: repo.selected, connection, serverProjectRoot: repo.serverProjectRoot };
|
|
125
|
+
}
|
|
126
|
+
function writeRepoServerProjectRoot(projectRoot, serverProjectRoot) {
|
|
127
|
+
const repo = readRepoConnection(projectRoot);
|
|
128
|
+
if (!repo)
|
|
129
|
+
return;
|
|
130
|
+
writeRepoConnection(projectRoot, { ...repo, serverProjectRoot });
|
|
131
|
+
}
|
|
132
|
+
var init__connection_state = __esm(() => {
|
|
133
|
+
init_runner();
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
// packages/cli/src/commands/_server-client.ts
|
|
137
|
+
var exports__server_client = {};
|
|
138
|
+
__export(exports__server_client, {
|
|
139
|
+
updateWorkspaceTaskViaServer: () => updateWorkspaceTaskViaServer,
|
|
140
|
+
switchServerProjectRootViaServer: () => switchServerProjectRootViaServer,
|
|
141
|
+
submitTaskRunViaServer: () => submitTaskRunViaServer,
|
|
142
|
+
stopRunViaServer: () => stopRunViaServer,
|
|
143
|
+
steerRunViaServer: () => steerRunViaServer,
|
|
144
|
+
setServerPhaseListener: () => setServerPhaseListener,
|
|
145
|
+
setGitHubBearerTokenForCurrentProcess: () => setGitHubBearerTokenForCurrentProcess,
|
|
146
|
+
sendRunPiShellViaServer: () => sendRunPiShellViaServer,
|
|
147
|
+
sendRunPiPromptViaServer: () => sendRunPiPromptViaServer,
|
|
148
|
+
selectNextWorkspaceTaskViaServer: () => selectNextWorkspaceTaskViaServer,
|
|
149
|
+
runRunPiCommandViaServer: () => runRunPiCommandViaServer,
|
|
150
|
+
resumeRunViaServer: () => resumeRunViaServer,
|
|
151
|
+
respondRunPiExtensionUiViaServer: () => respondRunPiExtensionUiViaServer,
|
|
152
|
+
resolveServerConnectionLabel: () => resolveServerConnectionLabel,
|
|
153
|
+
requestServerJson: () => requestServerJson,
|
|
154
|
+
registerProjectViaServer: () => registerProjectViaServer,
|
|
155
|
+
prepareRemoteCheckoutViaServer: () => prepareRemoteCheckoutViaServer,
|
|
156
|
+
postGitHubTokenViaServer: () => postGitHubTokenViaServer,
|
|
157
|
+
listWorkspaceTasksViaServer: () => listWorkspaceTasksViaServer,
|
|
158
|
+
listRunsViaServer: () => listRunsViaServer,
|
|
159
|
+
listGitHubProjectsViaServer: () => listGitHubProjectsViaServer,
|
|
160
|
+
getWorkspaceTaskViaServer: () => getWorkspaceTaskViaServer,
|
|
161
|
+
getRunTimelineViaServer: () => getRunTimelineViaServer,
|
|
162
|
+
getRunPiStatusViaServer: () => getRunPiStatusViaServer,
|
|
163
|
+
getRunPiSessionViaServer: () => getRunPiSessionViaServer,
|
|
164
|
+
getRunPiMessagesViaServer: () => getRunPiMessagesViaServer,
|
|
165
|
+
getRunPiCommandsViaServer: () => getRunPiCommandsViaServer,
|
|
166
|
+
getRunPiCapabilitiesViaServer: () => getRunPiCapabilitiesViaServer,
|
|
167
|
+
getRunLogsViaServer: () => getRunLogsViaServer,
|
|
168
|
+
getRunDetailsViaServer: () => getRunDetailsViaServer,
|
|
169
|
+
getGitHubProjectStatusFieldViaServer: () => getGitHubProjectStatusFieldViaServer,
|
|
170
|
+
getGitHubAuthStatusViaServer: () => getGitHubAuthStatusViaServer,
|
|
171
|
+
ensureTaskLabelsViaServer: () => ensureTaskLabelsViaServer,
|
|
172
|
+
ensureServerForCli: () => ensureServerForCli,
|
|
173
|
+
buildRunPiEventsWebSocketUrl: () => buildRunPiEventsWebSocketUrl,
|
|
174
|
+
abortRunPiViaServer: () => abortRunPiViaServer
|
|
175
|
+
});
|
|
176
|
+
import { existsSync as existsSync2, readFileSync as readFileSync2 } from "fs";
|
|
177
|
+
import { resolve as resolve2 } from "path";
|
|
178
|
+
import { ensureLocalRigServerConnection } from "@rig/runtime/local-server";
|
|
179
|
+
function setServerPhaseListener(listener) {
|
|
180
|
+
const previous = serverPhaseListener;
|
|
181
|
+
serverPhaseListener = listener;
|
|
182
|
+
return previous;
|
|
183
|
+
}
|
|
184
|
+
function reportServerPhase(label) {
|
|
185
|
+
serverPhaseListener?.(label);
|
|
186
|
+
}
|
|
187
|
+
function cleanToken(value) {
|
|
188
|
+
const trimmed = value?.trim();
|
|
189
|
+
return trimmed ? trimmed : null;
|
|
190
|
+
}
|
|
191
|
+
function setGitHubBearerTokenForCurrentProcess(token, projectRoot) {
|
|
192
|
+
const scopedKey = resolve2(projectRoot ?? process.cwd());
|
|
193
|
+
scopedGitHubBearerTokens.set(scopedKey, cleanToken(token ?? undefined));
|
|
194
|
+
}
|
|
195
|
+
function readPrivateRemoteSessionToken(projectRoot) {
|
|
196
|
+
const path = resolve2(projectRoot, ".rig", "state", "github-auth.json");
|
|
197
|
+
if (!existsSync2(path))
|
|
198
|
+
return null;
|
|
199
|
+
try {
|
|
200
|
+
const parsed = JSON.parse(readFileSync2(path, "utf8"));
|
|
201
|
+
return cleanToken(typeof parsed.apiSessionToken === "string" ? parsed.apiSessionToken : typeof parsed.sessionToken === "string" ? parsed.sessionToken : undefined);
|
|
202
|
+
} catch {
|
|
203
|
+
return null;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
function readGitHubBearerTokenForRemote(projectRoot) {
|
|
207
|
+
const scopedKey = resolve2(projectRoot);
|
|
208
|
+
if (scopedGitHubBearerTokens.has(scopedKey))
|
|
209
|
+
return scopedGitHubBearerTokens.get(scopedKey) ?? null;
|
|
210
|
+
const privateSession = readPrivateRemoteSessionToken(projectRoot);
|
|
211
|
+
if (privateSession)
|
|
212
|
+
return privateSession;
|
|
213
|
+
return cleanToken(process.env.RIG_SERVER_AUTH_TOKEN) ?? cleanToken(process.env.RIG_REMOTE_AUTH_TOKEN);
|
|
214
|
+
}
|
|
215
|
+
function readStoredGitHubAuthToken(projectRoot) {
|
|
216
|
+
const path = resolve2(projectRoot, ".rig", "state", "github-auth.json");
|
|
217
|
+
if (!existsSync2(path))
|
|
218
|
+
return null;
|
|
219
|
+
try {
|
|
220
|
+
const parsed = JSON.parse(readFileSync2(path, "utf8"));
|
|
221
|
+
return cleanToken(typeof parsed.token === "string" ? parsed.token : undefined);
|
|
222
|
+
} catch {
|
|
223
|
+
return null;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
function readLocalConnectionFallbackToken(projectRoot) {
|
|
227
|
+
return readGitHubBearerTokenForRemote(projectRoot) ?? cleanToken(process.env.RIG_GITHUB_TOKEN) ?? readStoredGitHubAuthToken(projectRoot);
|
|
228
|
+
}
|
|
229
|
+
async function ensureServerForCli(projectRoot) {
|
|
230
|
+
try {
|
|
231
|
+
const selected = resolveSelectedConnection(projectRoot);
|
|
232
|
+
if (selected?.connection.kind === "remote") {
|
|
233
|
+
reportServerPhase(`Connecting to ${selected.alias}\u2026`);
|
|
234
|
+
const authToken = readGitHubBearerTokenForRemote(projectRoot);
|
|
235
|
+
const serverProjectRoot = selected.serverProjectRoot ?? await backfillRemoteServerProjectRoot(projectRoot, selected.connection.baseUrl, authToken);
|
|
236
|
+
return {
|
|
237
|
+
baseUrl: selected.connection.baseUrl,
|
|
238
|
+
authToken,
|
|
239
|
+
connectionKind: "remote",
|
|
240
|
+
serverProjectRoot
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
reportServerPhase("Starting local Rig server\u2026");
|
|
244
|
+
const connection = await ensureLocalRigServerConnection(projectRoot);
|
|
245
|
+
return {
|
|
246
|
+
baseUrl: connection.baseUrl,
|
|
247
|
+
authToken: connection.authToken ?? readLocalConnectionFallbackToken(projectRoot),
|
|
248
|
+
connectionKind: "local",
|
|
249
|
+
serverProjectRoot: resolve2(projectRoot)
|
|
250
|
+
};
|
|
251
|
+
} catch (error) {
|
|
252
|
+
if (error instanceof Error) {
|
|
253
|
+
throw new CliError(error.message, 1);
|
|
254
|
+
}
|
|
255
|
+
throw error;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
async function backfillRemoteServerProjectRoot(projectRoot, baseUrl, authToken) {
|
|
259
|
+
const repo = readRepoConnection(projectRoot);
|
|
260
|
+
const slug = repo?.project?.trim();
|
|
261
|
+
if (!slug)
|
|
262
|
+
return null;
|
|
263
|
+
try {
|
|
264
|
+
const response = await fetch(`${baseUrl}/api/projects/${encodeURIComponent(slug)}`, {
|
|
265
|
+
headers: mergeHeaders(undefined, authToken)
|
|
266
|
+
});
|
|
267
|
+
if (!response.ok)
|
|
268
|
+
return null;
|
|
269
|
+
const payload = await response.json();
|
|
270
|
+
const project = payload.project && typeof payload.project === "object" && !Array.isArray(payload.project) ? payload.project : null;
|
|
271
|
+
const checkouts = Array.isArray(project?.checkouts) ? project.checkouts : [];
|
|
272
|
+
const latestCheckout = [...checkouts].reverse().find((entry) => Boolean(entry && typeof entry === "object" && !Array.isArray(entry) && typeof entry.path === "string"));
|
|
273
|
+
const path = typeof latestCheckout?.path === "string" && latestCheckout.path.trim() ? latestCheckout.path.trim() : null;
|
|
274
|
+
if (path)
|
|
275
|
+
writeRepoServerProjectRoot(projectRoot, path);
|
|
276
|
+
return path;
|
|
277
|
+
} catch {
|
|
278
|
+
return null;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
function appendTaskFilterParams(url, filters) {
|
|
282
|
+
if (filters.assignee)
|
|
283
|
+
url.searchParams.set("assignee", filters.assignee);
|
|
284
|
+
if (filters.state)
|
|
285
|
+
url.searchParams.set("state", filters.state);
|
|
286
|
+
if (filters.status)
|
|
287
|
+
url.searchParams.set("status", filters.status);
|
|
288
|
+
if (filters.limit !== undefined)
|
|
289
|
+
url.searchParams.set("limit", String(filters.limit));
|
|
290
|
+
}
|
|
291
|
+
function mergeHeaders(headers, authToken) {
|
|
292
|
+
const merged = new Headers(headers);
|
|
293
|
+
if (authToken) {
|
|
294
|
+
merged.set("authorization", `Bearer ${authToken}`);
|
|
295
|
+
}
|
|
296
|
+
return merged;
|
|
297
|
+
}
|
|
298
|
+
function diagnosticMessage(payload) {
|
|
299
|
+
if (!payload || typeof payload !== "object" || Array.isArray(payload))
|
|
300
|
+
return null;
|
|
301
|
+
const record = payload;
|
|
302
|
+
const diagnostics = Array.isArray(record.diagnostics) ? record.diagnostics : [];
|
|
303
|
+
const messages = diagnostics.flatMap((entry) => {
|
|
304
|
+
if (!entry || typeof entry !== "object" || Array.isArray(entry))
|
|
305
|
+
return [];
|
|
306
|
+
const diagnostic = entry;
|
|
307
|
+
const kind = typeof diagnostic.kind === "string" ? diagnostic.kind : "task-source";
|
|
308
|
+
const message = typeof diagnostic.message === "string" ? diagnostic.message : null;
|
|
309
|
+
return message ? [`${kind}: ${message}`] : [];
|
|
310
|
+
});
|
|
311
|
+
return messages.length > 0 ? messages.join("; ") : null;
|
|
312
|
+
}
|
|
313
|
+
async function probeServerReachability(baseUrl, authToken) {
|
|
314
|
+
try {
|
|
315
|
+
const response = await fetch(`${baseUrl.replace(/\/+$/, "")}/api/server/status`, {
|
|
316
|
+
headers: mergeHeaders(undefined, authToken),
|
|
317
|
+
signal: AbortSignal.timeout(1500)
|
|
318
|
+
});
|
|
319
|
+
return response.ok;
|
|
320
|
+
} catch {
|
|
321
|
+
return false;
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
function cachedServerReachability(projectRoot, baseUrl, authToken) {
|
|
325
|
+
const key = resolve2(projectRoot);
|
|
326
|
+
const cached = serverReachabilityCache.get(key);
|
|
327
|
+
if (cached)
|
|
328
|
+
return cached;
|
|
329
|
+
const probe = probeServerReachability(baseUrl, authToken);
|
|
330
|
+
serverReachabilityCache.set(key, probe);
|
|
331
|
+
return probe;
|
|
332
|
+
}
|
|
333
|
+
function describeSelectedServer(projectRoot, server) {
|
|
334
|
+
try {
|
|
335
|
+
const selected = resolveSelectedConnection(projectRoot);
|
|
336
|
+
if (selected) {
|
|
337
|
+
return {
|
|
338
|
+
alias: selected.alias,
|
|
339
|
+
target: selected.connection.kind === "remote" ? selected.connection.baseUrl : server.baseUrl
|
|
340
|
+
};
|
|
341
|
+
}
|
|
342
|
+
} catch {}
|
|
343
|
+
return { alias: server.connectionKind === "remote" ? "remote" : "local", target: server.baseUrl };
|
|
344
|
+
}
|
|
345
|
+
async function buildServerFailureContext(projectRoot, server) {
|
|
346
|
+
const { alias, target } = describeSelectedServer(projectRoot, server);
|
|
347
|
+
const reachable = await cachedServerReachability(projectRoot, server.baseUrl, server.authToken);
|
|
348
|
+
const reachability = reachable ? "server is reachable" : "server is unreachable";
|
|
349
|
+
return {
|
|
350
|
+
contextLine: `Currently connected to: ${alias} at ${target} (${reachability}).`,
|
|
351
|
+
hint: "Check the selected server with `rig server status`, or switch with `rig server use <alias|local>`."
|
|
352
|
+
};
|
|
353
|
+
}
|
|
354
|
+
async function requestServerJson(context, pathname, init = {}) {
|
|
355
|
+
const server = await ensureServerForCli(context.projectRoot);
|
|
356
|
+
const headers = mergeHeaders(init.headers, server.authToken);
|
|
357
|
+
if (server.serverProjectRoot)
|
|
358
|
+
headers.set("x-rig-project-root", server.serverProjectRoot);
|
|
359
|
+
reportServerPhase(`${(init.method ?? "GET").toUpperCase()} ${pathname.split("?")[0]}\u2026`);
|
|
360
|
+
let response;
|
|
361
|
+
try {
|
|
362
|
+
response = await fetch(`${server.baseUrl}${pathname}`, {
|
|
363
|
+
...init,
|
|
364
|
+
headers
|
|
365
|
+
});
|
|
366
|
+
} catch (error) {
|
|
367
|
+
const failure = await buildServerFailureContext(context.projectRoot, server);
|
|
368
|
+
throw new CliError(`Rig server request failed: ${error instanceof Error ? error.message : String(error)}
|
|
369
|
+
${failure.contextLine}`, 1, { hint: failure.hint });
|
|
370
|
+
}
|
|
371
|
+
const text = await response.text();
|
|
372
|
+
const payload = text.trim().length > 0 ? (() => {
|
|
373
|
+
try {
|
|
374
|
+
return JSON.parse(text);
|
|
375
|
+
} catch {
|
|
376
|
+
return null;
|
|
377
|
+
}
|
|
378
|
+
})() : null;
|
|
379
|
+
if (!response.ok) {
|
|
380
|
+
const diagnostics = diagnosticMessage(payload);
|
|
381
|
+
const rawDetail = diagnostics ?? (text || response.statusText);
|
|
382
|
+
const detail = diagnostics ? rawDetail : rawDetail.split(`
|
|
383
|
+
`).map((line) => line.trim()).find((line) => line.length > 0)?.slice(0, 200) ?? response.statusText;
|
|
384
|
+
const failure = await buildServerFailureContext(context.projectRoot, server);
|
|
385
|
+
throw new CliError(`Rig server request failed (${response.status}): ${detail}
|
|
386
|
+
${failure.contextLine}`, 1, { hint: failure.hint });
|
|
387
|
+
}
|
|
388
|
+
return payload;
|
|
389
|
+
}
|
|
390
|
+
async function listWorkspaceTasksViaServer(context, filters = {}) {
|
|
391
|
+
const url = new URL("http://rig.local/api/workspace/tasks");
|
|
392
|
+
appendTaskFilterParams(url, filters);
|
|
393
|
+
const payload = await requestServerJson(context, `${url.pathname}${url.search}`);
|
|
394
|
+
if (!Array.isArray(payload)) {
|
|
395
|
+
throw new CliError("Rig server returned an invalid task list payload.", 1, { hint: "Check the selected server with `rig server status`; mixed CLI/server versions can mismatch \u2014 try `rig doctor`." });
|
|
396
|
+
}
|
|
397
|
+
return payload.flatMap((entry) => entry && typeof entry === "object" && !Array.isArray(entry) ? [entry] : []);
|
|
398
|
+
}
|
|
399
|
+
async function getWorkspaceTaskViaServer(context, taskId) {
|
|
400
|
+
const payload = await requestServerJson(context, `/api/workspace/tasks/${encodeURIComponent(taskId)}`);
|
|
401
|
+
if (!payload || typeof payload !== "object" || Array.isArray(payload))
|
|
402
|
+
return null;
|
|
403
|
+
const task = payload.task;
|
|
404
|
+
return task && typeof task === "object" && !Array.isArray(task) ? task : null;
|
|
405
|
+
}
|
|
406
|
+
async function selectNextWorkspaceTaskViaServer(context, filters = {}) {
|
|
407
|
+
const url = new URL("http://rig.local/api/workspace/tasks/next");
|
|
408
|
+
appendTaskFilterParams(url, filters);
|
|
409
|
+
const payload = await requestServerJson(context, `${url.pathname}${url.search}`);
|
|
410
|
+
if (!payload || typeof payload !== "object" || Array.isArray(payload)) {
|
|
411
|
+
throw new CliError("Rig server returned an invalid next-task payload.", 1, { hint: "Check the selected server with `rig server status`; try `rig task list` to see the raw task set." });
|
|
412
|
+
}
|
|
413
|
+
const record = payload;
|
|
414
|
+
const rawTask = record.task;
|
|
415
|
+
const task = rawTask && typeof rawTask === "object" && !Array.isArray(rawTask) ? rawTask : null;
|
|
416
|
+
const count = typeof record.count === "number" && Number.isFinite(record.count) ? record.count : task ? 1 : 0;
|
|
417
|
+
return { task, count };
|
|
418
|
+
}
|
|
419
|
+
async function getGitHubAuthStatusViaServer(context) {
|
|
420
|
+
const payload = await requestServerJson(context, "/api/github/auth/status");
|
|
421
|
+
return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : {};
|
|
422
|
+
}
|
|
423
|
+
async function postGitHubTokenViaServer(context, token, options = {}) {
|
|
424
|
+
const payload = await requestServerJson(context, "/api/github/auth/token", {
|
|
425
|
+
method: "POST",
|
|
426
|
+
headers: { "content-type": "application/json" },
|
|
427
|
+
body: JSON.stringify({ token, selectedRepo: options.selectedRepo, projectRoot: options.projectRoot })
|
|
428
|
+
});
|
|
429
|
+
return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : {};
|
|
430
|
+
}
|
|
431
|
+
async function prepareRemoteCheckoutViaServer(context, input) {
|
|
432
|
+
const payload = await requestServerJson(context, `/api/projects/${encodeURIComponent(input.repoSlug)}/prepare-checkout`, {
|
|
433
|
+
method: "POST",
|
|
434
|
+
headers: { "content-type": "application/json" },
|
|
435
|
+
body: JSON.stringify({ checkout: input.checkout, repoUrl: input.repoUrl, baseDir: input.baseDir })
|
|
436
|
+
});
|
|
437
|
+
return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : {};
|
|
438
|
+
}
|
|
439
|
+
async function registerProjectViaServer(context, input) {
|
|
440
|
+
const payload = await requestServerJson(context, "/api/projects", {
|
|
441
|
+
method: "POST",
|
|
442
|
+
headers: { "content-type": "application/json" },
|
|
443
|
+
body: JSON.stringify(input)
|
|
444
|
+
});
|
|
445
|
+
return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : {};
|
|
446
|
+
}
|
|
447
|
+
function sleep(ms) {
|
|
448
|
+
return new Promise((resolve3) => setTimeout(resolve3, ms));
|
|
449
|
+
}
|
|
450
|
+
function isRetryableProjectRootSwitchError(error) {
|
|
451
|
+
if (!(error instanceof Error))
|
|
452
|
+
return false;
|
|
453
|
+
const message = error.message.toLowerCase();
|
|
454
|
+
return message.includes("rig server request failed (401): auth-required") || message.includes("rig server request failed (401): github-token-required") || message.includes("rig server request failed (502)") || message.includes("rig server request failed (503)") || message.includes("bad gateway") || message.includes("fetch failed") || message.includes("econnrefused") || message.includes("connection refused");
|
|
455
|
+
}
|
|
456
|
+
async function switchServerProjectRootViaServer(context, projectRoot, options = {}) {
|
|
457
|
+
const timeoutMs = options.timeoutMs ?? 30000;
|
|
458
|
+
const pollMs = options.pollMs ?? 1000;
|
|
459
|
+
const deadline = Date.now() + timeoutMs;
|
|
460
|
+
let lastError;
|
|
461
|
+
let switched = null;
|
|
462
|
+
while (Date.now() < deadline) {
|
|
463
|
+
try {
|
|
464
|
+
switched = await requestServerJson(context, "/api/server/project-root", {
|
|
465
|
+
method: "POST",
|
|
466
|
+
headers: { "content-type": "application/json" },
|
|
467
|
+
body: JSON.stringify({ projectRoot })
|
|
468
|
+
});
|
|
469
|
+
break;
|
|
470
|
+
} catch (error) {
|
|
471
|
+
lastError = error;
|
|
472
|
+
if (!isRetryableProjectRootSwitchError(error))
|
|
473
|
+
throw error;
|
|
474
|
+
await sleep(pollMs);
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
if (!switched) {
|
|
478
|
+
throw new CliError(`Rig server did not accept project-root switch to ${projectRoot} before timeout (${lastError instanceof Error ? lastError.message : String(lastError ?? "no response")}).`, 1);
|
|
479
|
+
}
|
|
480
|
+
const record = switched && typeof switched === "object" && !Array.isArray(switched) ? switched : {};
|
|
481
|
+
if (record.ok === true) {
|
|
482
|
+
writeRepoServerProjectRoot(context.projectRoot, projectRoot);
|
|
483
|
+
return { ok: true, switched: record };
|
|
484
|
+
}
|
|
485
|
+
throw new CliError(`Rig server rejected project-root scope ${projectRoot}: ${String(record.message ?? record.error ?? "unknown error")}`, 1);
|
|
486
|
+
}
|
|
487
|
+
async function listRunsViaServer(context, options = {}) {
|
|
488
|
+
const url = new URL("http://rig.local/api/runs");
|
|
489
|
+
if (options.limit !== undefined)
|
|
490
|
+
url.searchParams.set("limit", String(options.limit));
|
|
491
|
+
const payload = await requestServerJson(context, `${url.pathname}${url.search}`);
|
|
492
|
+
const runs = Array.isArray(payload) ? payload : payload && typeof payload === "object" && !Array.isArray(payload) && Array.isArray(payload.runs) ? payload.runs : [];
|
|
493
|
+
return runs.filter((entry) => Boolean(entry && typeof entry === "object" && !Array.isArray(entry)));
|
|
494
|
+
}
|
|
495
|
+
async function getRunDetailsViaServer(context, runId) {
|
|
496
|
+
const payload = await requestServerJson(context, `/api/runs/${encodeURIComponent(runId)}`);
|
|
497
|
+
return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : {};
|
|
498
|
+
}
|
|
499
|
+
async function getRunLogsViaServer(context, runId, options = {}) {
|
|
500
|
+
const url = new URL(`http://rig.local/api/runs/${encodeURIComponent(runId)}/logs`);
|
|
501
|
+
if (options.limit !== undefined)
|
|
502
|
+
url.searchParams.set("limit", String(options.limit));
|
|
503
|
+
if (options.cursor)
|
|
504
|
+
url.searchParams.set("cursor", options.cursor);
|
|
505
|
+
const payload = await requestServerJson(context, `${url.pathname}${url.search}`);
|
|
506
|
+
return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : { entries: [] };
|
|
507
|
+
}
|
|
508
|
+
async function getRunTimelineViaServer(context, runId, options = {}) {
|
|
509
|
+
const url = new URL(`http://rig.local/api/runs/${encodeURIComponent(runId)}/timeline`);
|
|
510
|
+
if (options.limit !== undefined)
|
|
511
|
+
url.searchParams.set("limit", String(options.limit));
|
|
512
|
+
if (options.cursor)
|
|
513
|
+
url.searchParams.set("cursor", options.cursor);
|
|
514
|
+
const payload = await requestServerJson(context, `${url.pathname}${url.search}`);
|
|
515
|
+
return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : { entries: [] };
|
|
516
|
+
}
|
|
517
|
+
async function ensureTaskLabelsViaServer(context) {
|
|
518
|
+
const payload = await requestServerJson(context, "/api/workspace/task-labels", { method: "POST" });
|
|
519
|
+
return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : {};
|
|
520
|
+
}
|
|
521
|
+
async function listGitHubProjectsViaServer(context, owner) {
|
|
522
|
+
const url = new URL("http://rig.local/api/github/projects");
|
|
523
|
+
url.searchParams.set("owner", owner);
|
|
524
|
+
const payload = await requestServerJson(context, `${url.pathname}${url.search}`);
|
|
525
|
+
return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : { projects: [] };
|
|
526
|
+
}
|
|
527
|
+
async function getGitHubProjectStatusFieldViaServer(context, projectId) {
|
|
528
|
+
const payload = await requestServerJson(context, `/api/github/projects/${encodeURIComponent(projectId)}/status-field`);
|
|
529
|
+
return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : {};
|
|
530
|
+
}
|
|
531
|
+
async function updateWorkspaceTaskViaServer(context, input) {
|
|
532
|
+
const payload = await requestServerJson(context, "/api/tasks/update", {
|
|
533
|
+
method: "POST",
|
|
534
|
+
headers: { "content-type": "application/json" },
|
|
535
|
+
body: JSON.stringify(input)
|
|
536
|
+
});
|
|
537
|
+
return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : { ok: true };
|
|
538
|
+
}
|
|
539
|
+
async function resumeRunViaServer(context, runId, options) {
|
|
540
|
+
let targetRunId = runId?.trim() || null;
|
|
541
|
+
if (!targetRunId) {
|
|
542
|
+
const candidates = (await listRunsViaServer(context)).filter((run) => RESUMABLE_RUN_STATUSES.has(String(run.status ?? ""))).sort((left, right) => String(right.updatedAt ?? "").localeCompare(String(left.updatedAt ?? "")));
|
|
543
|
+
targetRunId = typeof candidates[0]?.runId === "string" ? candidates[0].runId : null;
|
|
544
|
+
}
|
|
545
|
+
if (!targetRunId) {
|
|
546
|
+
throw new CliError(options.restart ? "No run is available to restart." : "No resumable run is available.", 2, { hint: "List runs with `rig run list`, then pass an explicit id: `rig run resume <run-id>`." });
|
|
547
|
+
}
|
|
548
|
+
const payload = await requestServerJson(context, "/api/runs/resume", {
|
|
549
|
+
method: "POST",
|
|
550
|
+
headers: { "content-type": "application/json" },
|
|
551
|
+
body: JSON.stringify({ runId: targetRunId, createdAt: new Date().toISOString(), restart: options.restart })
|
|
552
|
+
});
|
|
553
|
+
const record = payload && typeof payload === "object" && !Array.isArray(payload) ? payload : {};
|
|
554
|
+
if (record.ok === false) {
|
|
555
|
+
const message = typeof record.error === "string" && record.error.trim() ? record.error : "run resume failed";
|
|
556
|
+
throw new CliError(`${options.restart ? "restart" : "resume"} failed for ${targetRunId}: ${message}`, 1);
|
|
557
|
+
}
|
|
558
|
+
return { ok: true, runId: targetRunId, ...record };
|
|
559
|
+
}
|
|
560
|
+
async function resolveServerConnectionLabel(projectRoot) {
|
|
561
|
+
try {
|
|
562
|
+
const selected = resolveSelectedConnection(projectRoot);
|
|
563
|
+
if (!selected)
|
|
564
|
+
return "no server selected";
|
|
565
|
+
if (selected.connection.kind === "remote") {
|
|
566
|
+
return selected.connection.baseUrl.replace(/^https?:\/\//, "");
|
|
567
|
+
}
|
|
568
|
+
return `local (${selected.alias})`;
|
|
569
|
+
} catch {
|
|
570
|
+
return "no server selected";
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
async function stopRunViaServer(context, runId) {
|
|
574
|
+
const payload = await requestServerJson(context, "/api/runs/stop", {
|
|
575
|
+
method: "POST",
|
|
576
|
+
headers: { "content-type": "application/json" },
|
|
577
|
+
body: JSON.stringify({ runId })
|
|
578
|
+
});
|
|
579
|
+
return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : { ok: true, runId };
|
|
580
|
+
}
|
|
581
|
+
async function steerRunViaServer(context, runId, message) {
|
|
582
|
+
const payload = await requestServerJson(context, `/api/runs/${encodeURIComponent(runId)}/steer`, {
|
|
583
|
+
method: "POST",
|
|
584
|
+
headers: { "content-type": "application/json" },
|
|
585
|
+
body: JSON.stringify({ message })
|
|
586
|
+
});
|
|
587
|
+
return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : { ok: true };
|
|
588
|
+
}
|
|
589
|
+
async function getRunPiSessionViaServer(context, runId) {
|
|
590
|
+
const payload = await requestServerJson(context, `/api/runs/${encodeURIComponent(runId)}/pi`);
|
|
591
|
+
return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : {};
|
|
592
|
+
}
|
|
593
|
+
async function getRunPiMessagesViaServer(context, runId) {
|
|
594
|
+
const payload = await requestServerJson(context, `/api/runs/${encodeURIComponent(runId)}/pi/messages`);
|
|
595
|
+
return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : { messages: [] };
|
|
596
|
+
}
|
|
597
|
+
async function getRunPiStatusViaServer(context, runId) {
|
|
598
|
+
const payload = await requestServerJson(context, `/api/runs/${encodeURIComponent(runId)}/pi/status`);
|
|
599
|
+
return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : {};
|
|
600
|
+
}
|
|
601
|
+
async function getRunPiCommandsViaServer(context, runId) {
|
|
602
|
+
const payload = await requestServerJson(context, `/api/runs/${encodeURIComponent(runId)}/pi/commands`);
|
|
603
|
+
return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : { commands: [] };
|
|
604
|
+
}
|
|
605
|
+
async function getRunPiCapabilitiesViaServer(context, runId) {
|
|
606
|
+
const payload = await requestServerJson(context, `/api/runs/${encodeURIComponent(runId)}/pi/capabilities`);
|
|
607
|
+
return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : { capabilities: null };
|
|
608
|
+
}
|
|
609
|
+
async function sendRunPiPromptViaServer(context, runId, text, streamingBehavior) {
|
|
610
|
+
const payload = await requestServerJson(context, `/api/runs/${encodeURIComponent(runId)}/pi/prompt`, {
|
|
611
|
+
method: "POST",
|
|
612
|
+
headers: { "content-type": "application/json" },
|
|
613
|
+
body: JSON.stringify({ text, streamingBehavior })
|
|
614
|
+
});
|
|
615
|
+
return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : { accepted: true };
|
|
616
|
+
}
|
|
617
|
+
async function sendRunPiShellViaServer(context, runId, text) {
|
|
618
|
+
const payload = await requestServerJson(context, `/api/runs/${encodeURIComponent(runId)}/pi/shell`, {
|
|
619
|
+
method: "POST",
|
|
620
|
+
headers: { "content-type": "application/json" },
|
|
621
|
+
body: JSON.stringify({ text })
|
|
622
|
+
});
|
|
623
|
+
return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : { accepted: true };
|
|
624
|
+
}
|
|
625
|
+
async function runRunPiCommandViaServer(context, runId, text) {
|
|
626
|
+
const payload = await requestServerJson(context, `/api/runs/${encodeURIComponent(runId)}/pi/commands/run`, {
|
|
627
|
+
method: "POST",
|
|
628
|
+
headers: { "content-type": "application/json" },
|
|
629
|
+
body: JSON.stringify({ text })
|
|
630
|
+
});
|
|
631
|
+
return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : { type: "done" };
|
|
632
|
+
}
|
|
633
|
+
async function respondRunPiExtensionUiViaServer(context, runId, requestId, valueOrCancel) {
|
|
634
|
+
const payload = await requestServerJson(context, `/api/runs/${encodeURIComponent(runId)}/pi/extension-ui/respond`, {
|
|
635
|
+
method: "POST",
|
|
636
|
+
headers: { "content-type": "application/json" },
|
|
637
|
+
body: JSON.stringify({ requestId, ...valueOrCancel })
|
|
638
|
+
});
|
|
639
|
+
return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : { accepted: true };
|
|
640
|
+
}
|
|
641
|
+
async function abortRunPiViaServer(context, runId) {
|
|
642
|
+
const payload = await requestServerJson(context, `/api/runs/${encodeURIComponent(runId)}/pi/abort`, { method: "POST" });
|
|
643
|
+
return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : { aborted: true };
|
|
644
|
+
}
|
|
645
|
+
async function buildRunPiEventsWebSocketUrl(context, runId) {
|
|
646
|
+
const server = await ensureServerForCli(context.projectRoot);
|
|
647
|
+
const url = new URL(`${server.baseUrl.replace(/\/+$/, "")}/api/runs/${encodeURIComponent(runId)}/pi/events`);
|
|
648
|
+
url.protocol = url.protocol === "https:" ? "wss:" : "ws:";
|
|
649
|
+
if (server.authToken)
|
|
650
|
+
url.searchParams.set("token", server.authToken);
|
|
651
|
+
if (server.serverProjectRoot)
|
|
652
|
+
url.searchParams.set("rigProjectRoot", server.serverProjectRoot);
|
|
653
|
+
return url.toString();
|
|
654
|
+
}
|
|
655
|
+
async function submitTaskRunViaServer(context, input) {
|
|
656
|
+
const isTaskRun = Boolean(input.taskId);
|
|
657
|
+
const endpoint = isTaskRun ? "/api/runs/task" : "/api/runs/adhoc";
|
|
658
|
+
const payload = await requestServerJson(context, endpoint, {
|
|
659
|
+
method: "POST",
|
|
660
|
+
headers: {
|
|
661
|
+
"content-type": "application/json"
|
|
662
|
+
},
|
|
663
|
+
body: JSON.stringify({
|
|
664
|
+
runId: input.runId,
|
|
665
|
+
taskId: input.taskId,
|
|
666
|
+
title: input.title,
|
|
667
|
+
runtimeAdapter: input.runtimeAdapter,
|
|
668
|
+
model: input.model,
|
|
669
|
+
runtimeMode: input.runtimeMode,
|
|
670
|
+
interactionMode: input.interactionMode,
|
|
671
|
+
initialPrompt: input.initialPrompt,
|
|
672
|
+
baselineMode: input.baselineMode,
|
|
673
|
+
prMode: input.prMode,
|
|
674
|
+
executionTarget: "local"
|
|
675
|
+
})
|
|
676
|
+
});
|
|
677
|
+
if (!payload || typeof payload !== "object" || Array.isArray(payload)) {
|
|
678
|
+
throw new CliError("Rig server returned an invalid run submission payload.", 1, { hint: "Check `rig server status` and retry; `rig run list` shows whether the run was created anyway." });
|
|
679
|
+
}
|
|
680
|
+
const runId = payload.runId;
|
|
681
|
+
if (typeof runId !== "string" || runId.trim().length === 0) {
|
|
682
|
+
throw new CliError("Rig server returned no runId for the submitted run.", 1, { hint: "Check `rig run list` \u2014 the run may still have been created; otherwise retry the submission." });
|
|
683
|
+
}
|
|
684
|
+
return { runId };
|
|
685
|
+
}
|
|
686
|
+
var scopedGitHubBearerTokens, serverPhaseListener = null, serverReachabilityCache, RESUMABLE_RUN_STATUSES;
|
|
687
|
+
var init__server_client = __esm(() => {
|
|
688
|
+
init_runner();
|
|
689
|
+
init__connection_state();
|
|
690
|
+
scopedGitHubBearerTokens = new Map;
|
|
691
|
+
serverReachabilityCache = new Map;
|
|
692
|
+
RESUMABLE_RUN_STATUSES = new Set([
|
|
693
|
+
"created",
|
|
694
|
+
"preparing",
|
|
695
|
+
"running",
|
|
696
|
+
"validating",
|
|
697
|
+
"reviewing",
|
|
698
|
+
"stopped",
|
|
699
|
+
"failed",
|
|
700
|
+
"needs-attention",
|
|
701
|
+
"needs_attention"
|
|
702
|
+
]);
|
|
703
|
+
});
|
|
704
|
+
|
|
705
|
+
// packages/cli/src/app-opentui/adapters/common.ts
|
|
706
|
+
function adapterContextFromRuntime(runtime, renderer) {
|
|
707
|
+
return {
|
|
708
|
+
projectRoot: runtime.getState().projectRoot,
|
|
709
|
+
renderer,
|
|
710
|
+
ensureRuntime: () => runtime.getRunnerContext(),
|
|
711
|
+
emit: (event) => runtime.emit(event),
|
|
712
|
+
getState: () => runtime.getState()
|
|
713
|
+
};
|
|
714
|
+
}
|
|
715
|
+
function projectRootOf(ctx) {
|
|
716
|
+
const root = ctx.rig?.projectRoot ?? ctx.projectRoot ?? ctx.getState().projectRoot;
|
|
717
|
+
if (typeof root === "string" && root.trim())
|
|
718
|
+
return root;
|
|
719
|
+
throw new Error("Rig app adapter requires a projectRoot.");
|
|
720
|
+
}
|
|
721
|
+
function normalizeAppError(error) {
|
|
722
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
723
|
+
const rawHint = error instanceof Error ? error.hint : undefined;
|
|
724
|
+
return {
|
|
725
|
+
message,
|
|
726
|
+
...typeof rawHint === "string" && rawHint.trim() ? { hint: rawHint.trim() } : {}
|
|
727
|
+
};
|
|
728
|
+
}
|
|
729
|
+
function emitStarted(ctx, label, optimistic) {
|
|
730
|
+
ctx.emit({ type: "action.started", label, ...optimistic ? { optimistic } : {} });
|
|
731
|
+
}
|
|
732
|
+
function emitProgress(ctx, label, detail, data) {
|
|
733
|
+
ctx.emit({ type: "action.progress", label, ...detail ? { detail } : {}, ...data ? { data } : {} });
|
|
734
|
+
}
|
|
735
|
+
function emitCompleted(ctx, label, data) {
|
|
736
|
+
ctx.emit({ type: "action.completed", label, ...data ? { data } : {} });
|
|
737
|
+
}
|
|
738
|
+
function emitFailed(ctx, label, error, data) {
|
|
739
|
+
if (data)
|
|
740
|
+
patchData(ctx, data);
|
|
741
|
+
const normalized = normalizeAppError(error);
|
|
742
|
+
ctx.emit({
|
|
743
|
+
type: "action.failed",
|
|
744
|
+
label,
|
|
745
|
+
message: normalized.message,
|
|
746
|
+
...normalized.hint ? { hint: normalized.hint } : {},
|
|
747
|
+
cause: error
|
|
748
|
+
});
|
|
749
|
+
}
|
|
750
|
+
function patchData(ctx, data) {
|
|
751
|
+
ctx.emit({ type: "data.patch", data });
|
|
752
|
+
}
|
|
753
|
+
function patchFooter(ctx, footer) {
|
|
754
|
+
ctx.emit({ type: "footer.patch", footer });
|
|
755
|
+
}
|
|
756
|
+
function stringField(record, keys, fallback = "") {
|
|
757
|
+
for (const key of keys) {
|
|
758
|
+
const value = record[key];
|
|
759
|
+
if (typeof value === "string" && value.trim())
|
|
760
|
+
return value.trim();
|
|
761
|
+
if (typeof value === "number" && Number.isFinite(value))
|
|
762
|
+
return String(value);
|
|
763
|
+
}
|
|
764
|
+
return fallback;
|
|
765
|
+
}
|
|
766
|
+
function compactRecord(record, keys) {
|
|
767
|
+
const out = {};
|
|
768
|
+
for (const key of keys) {
|
|
769
|
+
const value = record[key];
|
|
770
|
+
if (value !== undefined && value !== null && value !== "")
|
|
771
|
+
out[key] = value;
|
|
772
|
+
}
|
|
773
|
+
return out;
|
|
774
|
+
}
|
|
775
|
+
function payloadString(intent, key) {
|
|
776
|
+
const value = intent.action.payload?.[key];
|
|
777
|
+
return typeof value === "string" && value.trim() ? value.trim() : undefined;
|
|
778
|
+
}
|
|
779
|
+
|
|
780
|
+
// packages/cli/src/app-opentui/adapters/fleet.ts
|
|
781
|
+
function normalizeRunRecord(record) {
|
|
782
|
+
const runId = stringField(record, ["runId", "id"]);
|
|
783
|
+
if (!runId)
|
|
784
|
+
return null;
|
|
785
|
+
const title = stringField(record, ["title", "taskTitle", "taskId"], "untitled run");
|
|
786
|
+
const taskId = stringField(record, ["taskId"]);
|
|
787
|
+
const updatedAt = stringField(record, ["updatedAt", "createdAt"]);
|
|
788
|
+
return {
|
|
789
|
+
runId,
|
|
790
|
+
status: stringField(record, ["status", "state"], "unknown"),
|
|
791
|
+
title,
|
|
792
|
+
raw: record,
|
|
793
|
+
...taskId ? { taskId } : {},
|
|
794
|
+
...updatedAt ? { updatedAt } : {}
|
|
795
|
+
};
|
|
796
|
+
}
|
|
797
|
+
async function refreshFleet(ctx, options = {}) {
|
|
798
|
+
const label = "Refreshing fleet";
|
|
799
|
+
emitStarted(ctx, label);
|
|
800
|
+
try {
|
|
801
|
+
const { listRunsViaServer: listRunsViaServer2, resolveServerConnectionLabel: resolveServerConnectionLabel2 } = await Promise.resolve().then(() => (init__server_client(), exports__server_client));
|
|
802
|
+
const projectRoot = projectRootOf(ctx);
|
|
803
|
+
emitProgress(ctx, label, "contacting selected Rig server");
|
|
804
|
+
const [runs, serverLabel] = await Promise.all([
|
|
805
|
+
listRunsViaServer2({ projectRoot }, { limit: options.limit ?? 50 }),
|
|
806
|
+
resolveServerConnectionLabel2(projectRoot)
|
|
807
|
+
]);
|
|
808
|
+
const normalized = runs.flatMap((run) => {
|
|
809
|
+
const entry = normalizeRunRecord(run);
|
|
810
|
+
return entry ? [entry] : [];
|
|
811
|
+
});
|
|
812
|
+
patchData(ctx, { fleet: { runs: normalized, refreshedAt: new Date().toISOString() } });
|
|
813
|
+
patchFooter(ctx, { server: serverLabel, runs: String(normalized.length) });
|
|
814
|
+
emitCompleted(ctx, label, { count: normalized.length });
|
|
815
|
+
return normalized;
|
|
816
|
+
} catch (error) {
|
|
817
|
+
emitFailed(ctx, label, error);
|
|
818
|
+
throw error;
|
|
819
|
+
}
|
|
820
|
+
}
|
|
821
|
+
function createFleetAdapter() {
|
|
822
|
+
return {
|
|
823
|
+
id: "fleet",
|
|
824
|
+
async handleIntent(runtime, intent) {
|
|
825
|
+
if (intent.scene !== "fleet" && intent.action.kind !== "run-stop")
|
|
826
|
+
return false;
|
|
827
|
+
const ctx = adapterContextFromRuntime(runtime);
|
|
828
|
+
if (intent.action.kind === "run-stop") {
|
|
829
|
+
const runId = payloadString(intent, "runId");
|
|
830
|
+
if (runId)
|
|
831
|
+
await stopFleetRun(ctx, runId);
|
|
832
|
+
else
|
|
833
|
+
await refreshFleet(ctx, { limit: 50 });
|
|
834
|
+
return true;
|
|
835
|
+
}
|
|
836
|
+
if (intent.action.kind === "refresh" || intent.action.kind === "navigate") {
|
|
837
|
+
await refreshFleet(ctx, { limit: 50 });
|
|
838
|
+
return true;
|
|
839
|
+
}
|
|
840
|
+
return false;
|
|
841
|
+
}
|
|
842
|
+
};
|
|
843
|
+
}
|
|
844
|
+
async function stopFleetRun(ctx, runId) {
|
|
845
|
+
const cleanRunId = runId.trim();
|
|
846
|
+
const label = `Stopping ${cleanRunId.slice(0, 8)}`;
|
|
847
|
+
if (!cleanRunId) {
|
|
848
|
+
const error = new Error("Stop requires a run id.");
|
|
849
|
+
emitFailed(ctx, label, error, { runId });
|
|
850
|
+
throw error;
|
|
851
|
+
}
|
|
852
|
+
emitStarted(ctx, label, {
|
|
853
|
+
fleetPatch: { runId: cleanRunId, status: "stopping" },
|
|
854
|
+
selectedRun: cleanRunId
|
|
855
|
+
});
|
|
856
|
+
patchData(ctx, { selectedRunId: cleanRunId, optimisticRun: { runId: cleanRunId, status: "stopping" } });
|
|
857
|
+
patchFooter(ctx, { run: `${cleanRunId.slice(0, 8)} stopping` });
|
|
858
|
+
try {
|
|
859
|
+
const { stopRunViaServer: stopRunViaServer2 } = await Promise.resolve().then(() => (init__server_client(), exports__server_client));
|
|
860
|
+
const result = await stopRunViaServer2({ projectRoot: projectRootOf(ctx) }, cleanRunId);
|
|
861
|
+
emitCompleted(ctx, label, { runId: cleanRunId, result: compactRecord(result, ["ok", "runId", "status", "stopped"]) });
|
|
862
|
+
await refreshFleet(ctx, { limit: 50 }).catch(() => []);
|
|
863
|
+
return result;
|
|
864
|
+
} catch (error) {
|
|
865
|
+
emitFailed(ctx, label, error, { runId: cleanRunId, revert: { status: "unknown" } });
|
|
866
|
+
throw error;
|
|
867
|
+
}
|
|
868
|
+
}
|
|
869
|
+
export {
|
|
870
|
+
stopFleetRun,
|
|
871
|
+
refreshFleet,
|
|
872
|
+
normalizeRunRecord,
|
|
873
|
+
createFleetAdapter
|
|
874
|
+
};
|