@howaboua/pi-shepherdr 0.2.27 → 0.2.28-core.12.1.gda85fa2e8a0f
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 +48 -48
- package/index.ts +7 -1
- package/package.json +2 -9
- package/src/agent-identity.ts +3 -3
- package/src/agents-contract.ts +10 -14
- package/src/agents-discovery.ts +34 -54
- package/src/agents-focus.ts +23 -35
- package/src/agents-spawn.ts +24 -23
- package/src/agents-tool.ts +4 -19
- package/src/agents-work.ts +4 -3
- package/src/attachment-notes.ts +8 -7
- package/src/attachment-protocol.ts +3 -2
- package/src/attachment-routing.ts +1 -1
- package/src/attachment-target.ts +10 -18
- package/src/attachment.ts +14 -9
- package/src/board/discovery.ts +67 -0
- package/src/board/host.ts +93 -63
- package/src/board/membership.ts +16 -11
- package/src/board/records.ts +185 -0
- package/src/close-idle.ts +2 -9
- package/src/context-briefing.ts +230 -0
- package/src/context-routing.ts +1 -1
- package/src/control-panel/index.ts +2 -4
- package/src/control-panel/items.ts +1 -11
- package/src/control-panel/view.ts +3 -9
- package/src/controller.ts +50 -63
- package/src/core/AGENTS.md +7 -4
- package/src/core/admission-broker.mjs +235 -0
- package/src/core/admission-endpoint.ts +180 -0
- package/src/core/admission-owner.mjs +52 -0
- package/src/core/admission-startup.ts +130 -0
- package/src/core/admission.ts +181 -0
- package/src/core/agents.ts +102 -0
- package/src/core/backend.ts +110 -84
- package/src/core/client.ts +16 -0
- package/src/core/focus-channel-helper.mjs +6 -27
- package/src/core/focus-channel-wire.mjs +9 -6
- package/src/core/focus-channel.ts +5 -3
- package/src/core/focus-window.ts +15 -4
- package/src/core/helper-bootstrap.mjs +70 -0
- package/src/core/helper-context.mjs +98 -0
- package/src/core/helper-core.mjs +121 -0
- package/src/core/machines.ts +30 -0
- package/src/core/registration.ts +7 -2
- package/src/core/remote-endpoint.ts +80 -74
- package/src/core/remote-helper.ts +330 -0
- package/src/core/remote-reader.ts +3 -42
- package/src/core/remote-service.mjs +239 -0
- package/src/core/remote-transport.ts +290 -0
- package/src/core/spawn.ts +20 -20
- package/src/core/terminal-peer.mjs +57 -0
- package/src/core/terminal.ts +4 -2
- package/src/delivery.ts +39 -0
- package/src/fleet/catalog.ts +16 -58
- package/src/fleet/connection.ts +9 -18
- package/src/fleet/index.ts +38 -76
- package/src/fleet/inspection.ts +9 -22
- package/src/fleet/machine.ts +28 -12
- package/src/fleet/monitor.ts +8 -29
- package/src/fleet/persistence.ts +2 -2
- package/src/fleet/reconnect.ts +5 -3
- package/src/fleet/setup.ts +6 -16
- package/src/launch-contract.ts +0 -3
- package/src/launch-location.ts +4 -132
- package/src/launch.ts +18 -144
- package/src/messages/attachments.ts +1 -1
- package/src/messages/attribution.ts +9 -14
- package/src/messages/events.ts +5 -9
- package/src/messages/focus.ts +2 -7
- package/src/messages/guidance.ts +2 -43
- package/src/messages/peer-sharing.ts +133 -0
- package/src/monitor-events.ts +14 -43
- package/src/monitor-extension-messages.ts +5 -6
- package/src/monitor-record.ts +4 -0
- package/src/monitor-state.ts +1 -101
- package/src/monitor.ts +38 -66
- package/src/notes-discovery.ts +67 -0
- package/src/peer-inbox.ts +21 -27
- package/src/remote/context-relay.ts +25 -10
- package/src/remote/notes-reader.mjs +67 -0
- package/src/remote/shepherdr-channel.mjs +1 -1
- package/src/remote/shepherdr-checkpoints.mjs +49 -102
- package/src/remote/shepherdr-context.mjs +1 -1
- package/src/remote/shepherdr-peer.mjs +19 -24
- package/src/remote/shepherdr-session.mjs +18 -7
- package/src/saved-context.ts +62 -0
- package/src/settlement/collector.ts +29 -9
- package/src/settlement/index.ts +35 -6
- package/src/shared-context-host.ts +27 -33
- package/src/shared-context.ts +44 -15
- package/src/spawn-context.ts +11 -3
- package/src/spawn-ownership.ts +1 -1
- package/src/ssh-setup.ts +40 -73
- package/src/types.ts +4 -8
- package/src/voice-handoff.ts +1 -1
- package/src/widget.ts +7 -2
- package/src/worker-ownership.ts +2 -8
- package/src/worker-service.ts +6 -14
- package/src/core/context-relay.mjs +0 -111
- package/src/core/remote-exec.ts +0 -122
- package/src/core/remote-helper.mjs +0 -244
- package/src/core/remote-relay.ts +0 -191
- package/src/core/request.ts +0 -124
- package/src/herdr-client.ts +0 -364
- package/src/herdr.ts +0 -268
- package/src/machine-catalog.ts +0 -83
- package/src/monitor-event.ts +0 -84
- package/src/remote/client.ts +0 -127
- package/src/remote/deployment.ts +0 -179
- package/src/remote/focus-channels.ts +0 -89
- package/src/remote/shepherdr.mjs +0 -504
- package/src/remote/transport.ts +0 -369
package/src/herdr.ts
DELETED
|
@@ -1,268 +0,0 @@
|
|
|
1
|
-
import { CoreConnection } from "./core/backend.js";
|
|
2
|
-
import { type HerdrConnection, isHerdrErrorCode } from "./herdr-client.js";
|
|
3
|
-
import type {
|
|
4
|
-
AgentStatus,
|
|
5
|
-
PaneInfo,
|
|
6
|
-
SessionSnapshot,
|
|
7
|
-
WorkspaceInfo,
|
|
8
|
-
} from "./types.js";
|
|
9
|
-
|
|
10
|
-
const AGENT_STATUSES: ReadonlySet<string> = new Set([
|
|
11
|
-
"idle",
|
|
12
|
-
"working",
|
|
13
|
-
"blocked",
|
|
14
|
-
"done",
|
|
15
|
-
"unknown",
|
|
16
|
-
]);
|
|
17
|
-
|
|
18
|
-
export async function getSnapshot(
|
|
19
|
-
client: HerdrConnection,
|
|
20
|
-
): Promise<SessionSnapshot> {
|
|
21
|
-
const result = record(
|
|
22
|
-
await client.request<unknown>("session.snapshot", {}),
|
|
23
|
-
"session.snapshot result",
|
|
24
|
-
);
|
|
25
|
-
return parseSessionSnapshot(result["snapshot"]);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export async function getAgent(
|
|
29
|
-
client: HerdrConnection,
|
|
30
|
-
target: string,
|
|
31
|
-
): Promise<PaneInfo> {
|
|
32
|
-
const result = record(
|
|
33
|
-
await client.request<unknown>("agent.get", { target }),
|
|
34
|
-
"agent.get result",
|
|
35
|
-
);
|
|
36
|
-
return parsePaneInfo(result["agent"], "agent.get result.agent");
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
export async function getPane(
|
|
40
|
-
client: HerdrConnection,
|
|
41
|
-
paneId: string,
|
|
42
|
-
): Promise<PaneInfo> {
|
|
43
|
-
const result = record(
|
|
44
|
-
await client.request<unknown>("pane.get", { pane_id: paneId }),
|
|
45
|
-
"pane.get result",
|
|
46
|
-
);
|
|
47
|
-
return parsePaneInfo(result["pane"], "pane.get result.pane");
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
export async function getCurrentPane(
|
|
51
|
-
client: HerdrConnection,
|
|
52
|
-
): Promise<PaneInfo> {
|
|
53
|
-
const callerPaneId =
|
|
54
|
-
client instanceof CoreConnection
|
|
55
|
-
? process.env["HC_TERMINAL_ID"]
|
|
56
|
-
: process.env["HERDR_PANE_ID"];
|
|
57
|
-
if (!callerPaneId)
|
|
58
|
-
throw new Error(
|
|
59
|
-
"Cannot identify this terminal; run Pi inside Herdr and retry",
|
|
60
|
-
);
|
|
61
|
-
const result = record(
|
|
62
|
-
await client.request<unknown>("pane.current", {
|
|
63
|
-
caller_pane_id: callerPaneId,
|
|
64
|
-
}),
|
|
65
|
-
"pane.current result",
|
|
66
|
-
);
|
|
67
|
-
return parsePaneInfo(result["pane"], "pane.current result.pane");
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
export async function resolvePiAgent(
|
|
71
|
-
client: HerdrConnection,
|
|
72
|
-
target: string,
|
|
73
|
-
controllingPaneId = client instanceof CoreConnection
|
|
74
|
-
? process.env["HC_TERMINAL_ID"]
|
|
75
|
-
: process.env["HERDR_PANE_ID"],
|
|
76
|
-
): Promise<PaneInfo> {
|
|
77
|
-
const agent = await getAgent(client, target).catch((error: unknown) => {
|
|
78
|
-
if (error instanceof Error && isHerdrErrorCode(error, "agent_not_found")) {
|
|
79
|
-
error.message += "; Use the exact target returned by spawn/find";
|
|
80
|
-
}
|
|
81
|
-
throw error;
|
|
82
|
-
});
|
|
83
|
-
if (agent.agent !== "pi") throw new Error(`${target} is not a Pi agent`);
|
|
84
|
-
if (controllingPaneId && agent.pane_id === controllingPaneId) {
|
|
85
|
-
throw new Error("refusing to target the controlling Pi session");
|
|
86
|
-
}
|
|
87
|
-
return agent;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
export function resolveWorkspace(
|
|
91
|
-
snapshot: SessionSnapshot,
|
|
92
|
-
target: string,
|
|
93
|
-
): WorkspaceInfo {
|
|
94
|
-
const direct = snapshot.workspaces.find(
|
|
95
|
-
(workspace) => workspace.workspace_id === target,
|
|
96
|
-
);
|
|
97
|
-
if (direct) return direct;
|
|
98
|
-
const matches = snapshot.workspaces.filter(
|
|
99
|
-
(workspace) => workspace.label === target,
|
|
100
|
-
);
|
|
101
|
-
if (matches.length === 1) return matches[0]!;
|
|
102
|
-
if (matches.length > 1) {
|
|
103
|
-
throw new Error(
|
|
104
|
-
`workspace label ${JSON.stringify(target)} is ambiguous; use its Herdr ID`,
|
|
105
|
-
);
|
|
106
|
-
}
|
|
107
|
-
throw new Error(`no Herdr workspace matches ${JSON.stringify(target)}`);
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
export function sessionPath(agent: PaneInfo): string | undefined {
|
|
111
|
-
return agent.agent_session?.kind === "path"
|
|
112
|
-
? agent.agent_session.value
|
|
113
|
-
: undefined;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
export function parsePaneInfo(value: unknown, path = "pane"): PaneInfo {
|
|
117
|
-
const pane = record(value, path);
|
|
118
|
-
return {
|
|
119
|
-
...(pane["backend"] === "core" ? { backend: "core" as const } : {}),
|
|
120
|
-
pane_id: requiredString(pane, "pane_id", path),
|
|
121
|
-
terminal_id: requiredString(pane, "terminal_id", path),
|
|
122
|
-
...(pane["backend"] === "core"
|
|
123
|
-
? {
|
|
124
|
-
...(typeof pane["workspace_id"] === "string"
|
|
125
|
-
? { workspace_id: pane["workspace_id"] }
|
|
126
|
-
: {}),
|
|
127
|
-
...(typeof pane["tab_id"] === "string"
|
|
128
|
-
? { tab_id: pane["tab_id"] }
|
|
129
|
-
: {}),
|
|
130
|
-
}
|
|
131
|
-
: {
|
|
132
|
-
workspace_id: requiredString(pane, "workspace_id", path),
|
|
133
|
-
tab_id: requiredString(pane, "tab_id", path),
|
|
134
|
-
}),
|
|
135
|
-
agent_status: parseAgentStatus(
|
|
136
|
-
pane["agent_status"],
|
|
137
|
-
`${path}.agent_status`,
|
|
138
|
-
),
|
|
139
|
-
...optionalNullableString(pane, "agent", path),
|
|
140
|
-
...optionalNullableString(pane, "cwd", path),
|
|
141
|
-
...optionalNullableString(pane, "foreground_cwd", path),
|
|
142
|
-
...optionalNullableString(pane, "label", path),
|
|
143
|
-
...optionalNullableString(pane, "name", path),
|
|
144
|
-
...optionalNullableString(pane, "title", path),
|
|
145
|
-
...optionalBoolean(pane, "interactive_ready", path),
|
|
146
|
-
...optionalBoolean(pane, "launch_pending", path),
|
|
147
|
-
...parseAgentSession(pane["agent_session"], path),
|
|
148
|
-
};
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
function parseSessionSnapshot(value: unknown): SessionSnapshot {
|
|
152
|
-
const snapshot = record(value, "session.snapshot result.snapshot");
|
|
153
|
-
return {
|
|
154
|
-
agents: parseArray(snapshot, "agents", parsePaneInfo),
|
|
155
|
-
panes: parseArray(snapshot, "panes", parsePaneInfo),
|
|
156
|
-
tabs: parseArray(snapshot, "tabs", parseTabInfo),
|
|
157
|
-
workspaces: parseArray(snapshot, "workspaces", parseWorkspaceInfo),
|
|
158
|
-
};
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
function parseTabInfo(
|
|
162
|
-
value: unknown,
|
|
163
|
-
path: string,
|
|
164
|
-
): SessionSnapshot["tabs"][number] {
|
|
165
|
-
const tab = record(value, path);
|
|
166
|
-
return {
|
|
167
|
-
tab_id: requiredString(tab, "tab_id", path),
|
|
168
|
-
workspace_id: requiredString(tab, "workspace_id", path),
|
|
169
|
-
label: requiredString(tab, "label", path),
|
|
170
|
-
};
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
function parseWorkspaceInfo(value: unknown, path: string): WorkspaceInfo {
|
|
174
|
-
const workspace = record(value, path);
|
|
175
|
-
return {
|
|
176
|
-
workspace_id: requiredString(workspace, "workspace_id", path),
|
|
177
|
-
label: requiredString(workspace, "label", path),
|
|
178
|
-
};
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
function parseArray<T>(
|
|
182
|
-
parent: Record<string, unknown>,
|
|
183
|
-
key: string,
|
|
184
|
-
parse: (value: unknown, path: string) => T,
|
|
185
|
-
): T[] {
|
|
186
|
-
const value = parent[key];
|
|
187
|
-
if (!Array.isArray(value)) {
|
|
188
|
-
throw new Error(
|
|
189
|
-
`Herdr session.snapshot result.snapshot.${key} must be an array`,
|
|
190
|
-
);
|
|
191
|
-
}
|
|
192
|
-
return value.map((item, index) =>
|
|
193
|
-
parse(item, `session.snapshot result.snapshot.${key}[${index}]`),
|
|
194
|
-
);
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
function parseAgentStatus(value: unknown, path: string): AgentStatus {
|
|
198
|
-
if (typeof value === "string" && AGENT_STATUSES.has(value)) {
|
|
199
|
-
return value as AgentStatus;
|
|
200
|
-
}
|
|
201
|
-
throw new Error(`Herdr ${path} has invalid status ${JSON.stringify(value)}`);
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
function parseAgentSession(
|
|
205
|
-
value: unknown,
|
|
206
|
-
path: string,
|
|
207
|
-
): Pick<PaneInfo, "agent_session"> | Record<never, never> {
|
|
208
|
-
if (value === undefined) return {};
|
|
209
|
-
if (value === null) return { agent_session: null };
|
|
210
|
-
const session = record(value, `${path}.agent_session`);
|
|
211
|
-
const kind = session["kind"];
|
|
212
|
-
if (kind !== "id" && kind !== "path") {
|
|
213
|
-
throw new Error(`Herdr ${path}.agent_session.kind must be id or path`);
|
|
214
|
-
}
|
|
215
|
-
return {
|
|
216
|
-
agent_session: {
|
|
217
|
-
agent: requiredString(session, "agent", `${path}.agent_session`),
|
|
218
|
-
kind,
|
|
219
|
-
source: requiredString(session, "source", `${path}.agent_session`),
|
|
220
|
-
value: requiredString(session, "value", `${path}.agent_session`),
|
|
221
|
-
},
|
|
222
|
-
};
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
function optionalNullableString<
|
|
226
|
-
K extends "agent" | "cwd" | "foreground_cwd" | "label" | "name" | "title",
|
|
227
|
-
>(
|
|
228
|
-
value: Record<string, unknown>,
|
|
229
|
-
key: K,
|
|
230
|
-
path: string,
|
|
231
|
-
): Pick<PaneInfo, K> | Record<never, never> {
|
|
232
|
-
const field = value[key];
|
|
233
|
-
if (field === undefined) return {};
|
|
234
|
-
if (field === null || typeof field === "string") {
|
|
235
|
-
return { [key]: field } as Pick<PaneInfo, K>;
|
|
236
|
-
}
|
|
237
|
-
throw new Error(`Herdr ${path}.${key} must be string or null`);
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
function optionalBoolean<K extends "interactive_ready" | "launch_pending">(
|
|
241
|
-
value: Record<string, unknown>,
|
|
242
|
-
key: K,
|
|
243
|
-
path: string,
|
|
244
|
-
): Pick<PaneInfo, K> | Record<never, never> {
|
|
245
|
-
const field = value[key];
|
|
246
|
-
if (field === undefined) return {};
|
|
247
|
-
if (typeof field === "boolean") {
|
|
248
|
-
return { [key]: field } as Pick<PaneInfo, K>;
|
|
249
|
-
}
|
|
250
|
-
throw new Error(`Herdr ${path}.${key} must be boolean`);
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
function requiredString(
|
|
254
|
-
value: Record<string, unknown>,
|
|
255
|
-
key: string,
|
|
256
|
-
path: string,
|
|
257
|
-
): string {
|
|
258
|
-
const field = value[key];
|
|
259
|
-
if (typeof field === "string") return field;
|
|
260
|
-
throw new Error(`Herdr ${path}.${key} must be string`);
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
function record(value: unknown, path: string): Record<string, unknown> {
|
|
264
|
-
if (typeof value === "object" && value !== null && !Array.isArray(value)) {
|
|
265
|
-
return value as Record<string, unknown>;
|
|
266
|
-
}
|
|
267
|
-
throw new Error(`Herdr ${path} must be an object`);
|
|
268
|
-
}
|
package/src/machine-catalog.ts
DELETED
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
import { execFile } from "node:child_process";
|
|
2
|
-
import { isAbsolute } from "node:path";
|
|
3
|
-
import { promisify } from "node:util";
|
|
4
|
-
import { type Static, Type } from "typebox";
|
|
5
|
-
import { Check } from "typebox/value";
|
|
6
|
-
|
|
7
|
-
export const LOCAL_MACHINE = "local";
|
|
8
|
-
|
|
9
|
-
const Machine = Type.Object({
|
|
10
|
-
id: Type.String({ pattern: "^[a-f0-9]{32}$" }),
|
|
11
|
-
label: Type.String({ minLength: 1, pattern: "^[^\\x00-\\x1f\\x7f]+$" }),
|
|
12
|
-
target: Type.String({
|
|
13
|
-
minLength: 1,
|
|
14
|
-
pattern: "^[^-\\x00-\\x1f\\x7f][^\\x00-\\x1f\\x7f]*$",
|
|
15
|
-
}),
|
|
16
|
-
session: Type.String({
|
|
17
|
-
minLength: 1,
|
|
18
|
-
maxLength: 64,
|
|
19
|
-
pattern: "^[a-zA-Z0-9_.-]+$",
|
|
20
|
-
}),
|
|
21
|
-
enabled: Type.Boolean(),
|
|
22
|
-
});
|
|
23
|
-
const Catalog = Type.Array(Machine);
|
|
24
|
-
export type SshMachine = Static<typeof Machine>;
|
|
25
|
-
|
|
26
|
-
export async function withHerdrBinary<T>(
|
|
27
|
-
run: (binary: string) => Promise<T>,
|
|
28
|
-
): Promise<T> {
|
|
29
|
-
const inheritedBinary = process.env["HERDR_BIN_PATH"]?.trim() || "herdr";
|
|
30
|
-
let binary = inheritedBinary;
|
|
31
|
-
try {
|
|
32
|
-
try {
|
|
33
|
-
return await run(binary);
|
|
34
|
-
} catch (error) {
|
|
35
|
-
if (
|
|
36
|
-
!(error instanceof Error) ||
|
|
37
|
-
!("code" in error) ||
|
|
38
|
-
error.code !== "ENOENT" ||
|
|
39
|
-
!isAbsolute(binary) ||
|
|
40
|
-
!binary.endsWith(" (deleted)")
|
|
41
|
-
) {
|
|
42
|
-
throw error;
|
|
43
|
-
}
|
|
44
|
-
// Linux /proc executable paths can retain this suffix after replacement.
|
|
45
|
-
binary = binary.slice(0, -" (deleted)".length);
|
|
46
|
-
return await run(binary);
|
|
47
|
-
}
|
|
48
|
-
} catch (error) {
|
|
49
|
-
throw new Error(
|
|
50
|
-
`Herdr failed${binary !== inheritedBinary ? " after retrying stale HERDR_BIN_PATH" : ""}: ${error instanceof Error ? error.message : String(error)}`,
|
|
51
|
-
{ cause: error },
|
|
52
|
-
);
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
export async function readMachineCatalog(
|
|
57
|
-
signal?: AbortSignal,
|
|
58
|
-
): Promise<Record<string, SshMachine>> {
|
|
59
|
-
const { stdout } = await withHerdrBinary((binary) =>
|
|
60
|
-
promisify(execFile)(binary, ["machine", "list", "--json"], {
|
|
61
|
-
signal,
|
|
62
|
-
timeout: 10_000,
|
|
63
|
-
maxBuffer: 1024 * 1024,
|
|
64
|
-
encoding: "utf8",
|
|
65
|
-
}),
|
|
66
|
-
);
|
|
67
|
-
let value: unknown;
|
|
68
|
-
try {
|
|
69
|
-
value = JSON.parse(stdout);
|
|
70
|
-
} catch {
|
|
71
|
-
throw new Error("Herdr machine list returned invalid JSON");
|
|
72
|
-
}
|
|
73
|
-
if (
|
|
74
|
-
!Check(Catalog, value) ||
|
|
75
|
-
value.some((machine) => machine.session === "." || machine.session === "..")
|
|
76
|
-
) {
|
|
77
|
-
throw new Error("Herdr machine list returned an invalid catalog");
|
|
78
|
-
}
|
|
79
|
-
if (new Set(value.map((machine) => machine.id)).size !== value.length) {
|
|
80
|
-
throw new Error("Herdr machine list returned duplicate profile IDs");
|
|
81
|
-
}
|
|
82
|
-
return Object.fromEntries(value.map((machine) => [machine.id, machine]));
|
|
83
|
-
}
|
package/src/monitor-event.ts
DELETED
|
@@ -1,84 +0,0 @@
|
|
|
1
|
-
import { isAgentStatus } from "./activity.js";
|
|
2
|
-
import type { AgentStatus, HerdrEvent } from "./types.js";
|
|
3
|
-
|
|
4
|
-
export type MonitorEvent =
|
|
5
|
-
| { paneId: string; type: "closed" }
|
|
6
|
-
| {
|
|
7
|
-
pane: { pane_id: string; tab_id?: string; workspace_id?: string };
|
|
8
|
-
previousPaneId: string;
|
|
9
|
-
type: "moved";
|
|
10
|
-
}
|
|
11
|
-
| {
|
|
12
|
-
blockedMessage?: string;
|
|
13
|
-
paneId: string;
|
|
14
|
-
status: AgentStatus;
|
|
15
|
-
type: "status";
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
function blockedReason(data: Record<string, unknown>): string | undefined {
|
|
19
|
-
const labels = data["state_labels"];
|
|
20
|
-
if (
|
|
21
|
-
typeof labels === "object" &&
|
|
22
|
-
labels !== null &&
|
|
23
|
-
"blocked" in labels &&
|
|
24
|
-
typeof labels.blocked === "string"
|
|
25
|
-
) {
|
|
26
|
-
return labels.blocked;
|
|
27
|
-
}
|
|
28
|
-
return typeof data["title"] === "string" ? data["title"] : undefined;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export function parseMonitorEvent(event: HerdrEvent): MonitorEvent | undefined {
|
|
32
|
-
const paneId = event.data["pane_id"];
|
|
33
|
-
if (event.event === "pane.closed") {
|
|
34
|
-
if (typeof paneId !== "string") {
|
|
35
|
-
throw new Error("Herdr pane.closed event has no pane_id");
|
|
36
|
-
}
|
|
37
|
-
return { paneId, type: "closed" };
|
|
38
|
-
}
|
|
39
|
-
if (event.event === "pane.agent_status_changed") {
|
|
40
|
-
const status = event.data["agent_status"];
|
|
41
|
-
if (typeof paneId !== "string" || !isAgentStatus(status)) {
|
|
42
|
-
throw new Error("Herdr pane.agent_status_changed event is invalid");
|
|
43
|
-
}
|
|
44
|
-
const blockedMessage =
|
|
45
|
-
status === "blocked" ? blockedReason(event.data) : undefined;
|
|
46
|
-
return {
|
|
47
|
-
paneId,
|
|
48
|
-
status,
|
|
49
|
-
type: "status",
|
|
50
|
-
...(blockedMessage ? { blockedMessage } : {}),
|
|
51
|
-
};
|
|
52
|
-
}
|
|
53
|
-
if (event.event !== "pane.moved") return undefined;
|
|
54
|
-
const previousPaneId = event.data["previous_pane_id"];
|
|
55
|
-
const pane = event.data["pane"];
|
|
56
|
-
if (
|
|
57
|
-
typeof previousPaneId !== "string" ||
|
|
58
|
-
typeof pane !== "object" ||
|
|
59
|
-
pane === null ||
|
|
60
|
-
!("pane_id" in pane) ||
|
|
61
|
-
typeof pane.pane_id !== "string"
|
|
62
|
-
) {
|
|
63
|
-
throw new Error("Herdr pane.moved event is invalid");
|
|
64
|
-
}
|
|
65
|
-
if (
|
|
66
|
-
("workspace_id" in pane && typeof pane.workspace_id !== "string") ||
|
|
67
|
-
("tab_id" in pane && typeof pane.tab_id !== "string")
|
|
68
|
-
) {
|
|
69
|
-
throw new Error("Herdr pane.moved event has invalid location IDs");
|
|
70
|
-
}
|
|
71
|
-
return {
|
|
72
|
-
pane: {
|
|
73
|
-
pane_id: pane.pane_id,
|
|
74
|
-
...("workspace_id" in pane && typeof pane.workspace_id === "string"
|
|
75
|
-
? { workspace_id: pane.workspace_id }
|
|
76
|
-
: {}),
|
|
77
|
-
...("tab_id" in pane && typeof pane.tab_id === "string"
|
|
78
|
-
? { tab_id: pane.tab_id }
|
|
79
|
-
: {}),
|
|
80
|
-
},
|
|
81
|
-
previousPaneId,
|
|
82
|
-
type: "moved",
|
|
83
|
-
};
|
|
84
|
-
}
|
package/src/remote/client.ts
DELETED
|
@@ -1,127 +0,0 @@
|
|
|
1
|
-
import type { HerdrConnection } from "../herdr-client.js";
|
|
2
|
-
import type { SshMachine } from "../machine-catalog.js";
|
|
3
|
-
import type { AssistantReader } from "../session-reader.js";
|
|
4
|
-
import type {
|
|
5
|
-
LatestAssistant,
|
|
6
|
-
PaneInfo,
|
|
7
|
-
PeerDelivery,
|
|
8
|
-
PeerMessage,
|
|
9
|
-
SessionView,
|
|
10
|
-
} from "../types.js";
|
|
11
|
-
import { connectRemoteBridge } from "./deployment.js";
|
|
12
|
-
import { readPeerCommands } from "./shepherdr-peer.mjs";
|
|
13
|
-
import { RemoteTransport } from "./transport.js";
|
|
14
|
-
|
|
15
|
-
/** Operation-specific RPC shapes and responses; transport owns all live resources. */
|
|
16
|
-
export class RemoteHerdrClient
|
|
17
|
-
extends RemoteTransport
|
|
18
|
-
implements HerdrConnection, AssistantReader
|
|
19
|
-
{
|
|
20
|
-
static async connect(
|
|
21
|
-
config: SshMachine,
|
|
22
|
-
onClose: (error: Error) => void,
|
|
23
|
-
): Promise<RemoteHerdrClient> {
|
|
24
|
-
const child = await connectRemoteBridge(config);
|
|
25
|
-
const client = new RemoteHerdrClient(child, onClose);
|
|
26
|
-
try {
|
|
27
|
-
await client.ready();
|
|
28
|
-
return client;
|
|
29
|
-
} catch (error) {
|
|
30
|
-
client.close();
|
|
31
|
-
throw error;
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
async request<T>(
|
|
36
|
-
method: string,
|
|
37
|
-
params: object = {},
|
|
38
|
-
timeoutMs = 10_000,
|
|
39
|
-
): Promise<T> {
|
|
40
|
-
return (await this.call(
|
|
41
|
-
{ op: "request", method, params, timeoutMs },
|
|
42
|
-
timeoutMs + 1_000,
|
|
43
|
-
)) as T;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
async replyExtension(
|
|
47
|
-
agent: PaneInfo,
|
|
48
|
-
reply: import("../types.js").ExtensionReply,
|
|
49
|
-
): Promise<string> {
|
|
50
|
-
const result = await this.call(
|
|
51
|
-
{ op: "extension-reply", agent, reply },
|
|
52
|
-
60_000,
|
|
53
|
-
);
|
|
54
|
-
if (typeof result !== "string")
|
|
55
|
-
throw new Error(
|
|
56
|
-
"Invalid extension reply receipt; inspect the target before retrying",
|
|
57
|
-
);
|
|
58
|
-
return result;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
async commands(
|
|
62
|
-
agent: PaneInfo,
|
|
63
|
-
): Promise<import("../types.js").PeerCommand[]> {
|
|
64
|
-
const result = await this.call({ op: "commands", agent }, 26_000);
|
|
65
|
-
return readPeerCommands(result);
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
async sendMessage(
|
|
69
|
-
agent: PaneInfo,
|
|
70
|
-
message: PeerMessage,
|
|
71
|
-
): Promise<PeerDelivery> {
|
|
72
|
-
const result = await this.call(
|
|
73
|
-
{ op: "message", agent, message },
|
|
74
|
-
message.text.trim() === "/quit" ? 0 : 26_000,
|
|
75
|
-
);
|
|
76
|
-
if (
|
|
77
|
-
!result ||
|
|
78
|
-
typeof result !== "object" ||
|
|
79
|
-
!("command" in result) ||
|
|
80
|
-
typeof result.command !== "boolean"
|
|
81
|
-
)
|
|
82
|
-
throw new Error(
|
|
83
|
-
"Invalid peer acknowledgement; inspect the target before retrying",
|
|
84
|
-
);
|
|
85
|
-
return { command: result.command };
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
async machineIdentity(): Promise<import("../types.js").MachineIdentity> {
|
|
89
|
-
const result = await this.call({ op: "identity" });
|
|
90
|
-
if (
|
|
91
|
-
!result ||
|
|
92
|
-
typeof result !== "object" ||
|
|
93
|
-
!("host" in result) ||
|
|
94
|
-
typeof result.host !== "string" ||
|
|
95
|
-
!("session" in result) ||
|
|
96
|
-
typeof result.session !== "string"
|
|
97
|
-
)
|
|
98
|
-
throw new Error("Could not identify remote Herdr session");
|
|
99
|
-
return { host: result.host, session: result.session };
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
focus(
|
|
103
|
-
agent: PaneInfo,
|
|
104
|
-
request: import("../types.js").FocusRequest,
|
|
105
|
-
): Promise<unknown> {
|
|
106
|
-
return this.call({ op: "focus", agent, request }, 60_000);
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
async latest(path?: string): Promise<LatestAssistant | undefined> {
|
|
110
|
-
return (await this.view(path)).assistant;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
async view(path?: string): Promise<SessionView> {
|
|
114
|
-
return (await this.call({ op: "view", path })) as SessionView;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
async keybindings(): Promise<Record<string, unknown>> {
|
|
118
|
-
return (await this.call({ op: "keybindings" })) as Record<string, unknown>;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
async directory(
|
|
122
|
-
value: string | undefined,
|
|
123
|
-
fallback: string,
|
|
124
|
-
): Promise<string> {
|
|
125
|
-
return (await this.call({ op: "directory", value, fallback })) as string;
|
|
126
|
-
}
|
|
127
|
-
}
|