@howaboua/pi-shepherdr 0.1.6 → 0.2.1
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/CHANGELOG.md +25 -0
- package/README.md +25 -6
- package/index.ts +2 -0
- package/package.json +4 -4
- package/src/agents-contract.ts +11 -6
- package/src/agents-operations.ts +99 -23
- package/src/agents-tool.ts +77 -45
- package/src/controller.ts +4 -8
- package/src/delivery.ts +100 -1
- package/src/fleet.ts +59 -63
- package/src/herdr-client.ts +26 -2
- package/src/herdr.ts +17 -1
- package/src/launch.ts +0 -25
- package/src/machine-catalog.ts +77 -0
- package/src/messages.ts +75 -14
- package/src/monitor-record.ts +6 -19
- package/src/monitor-state.ts +36 -6
- package/src/monitor.ts +14 -9
- package/src/peer-inbox.ts +237 -0
- package/src/remote/shepherdr-peer.mjs +225 -0
- package/src/remote/shepherdr.mjs +33 -13
- package/src/remote-client.ts +55 -21
- package/src/session-reader.ts +21 -11
- package/src/settlement.ts +15 -13
- package/src/types.ts +19 -4
- package/src/widget.ts +4 -4
- package/tsconfig.json +7 -1
- package/src/machine-menu.ts +0 -163
- package/src/machines-config.ts +0 -148
package/tsconfig.json
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"extends": "../../tsconfig.base.json",
|
|
3
|
-
"
|
|
3
|
+
"compilerOptions": { "allowJs": true, "checkJs": true },
|
|
4
|
+
"include": [
|
|
5
|
+
"index.ts",
|
|
6
|
+
"src/**/*.ts",
|
|
7
|
+
"src/remote/shepherdr-peer.mjs",
|
|
8
|
+
"tests/**/*.ts"
|
|
9
|
+
]
|
|
4
10
|
}
|
package/src/machine-menu.ts
DELETED
|
@@ -1,163 +0,0 @@
|
|
|
1
|
-
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
2
|
-
import type { AgentFleet } from "./fleet.js";
|
|
3
|
-
import {
|
|
4
|
-
isMachineName,
|
|
5
|
-
LOCAL_MACHINE,
|
|
6
|
-
readMachinesConfig,
|
|
7
|
-
writeMachinesConfig,
|
|
8
|
-
} from "./machines-config.js";
|
|
9
|
-
import type { MachineStatus } from "./types.js";
|
|
10
|
-
|
|
11
|
-
function parseSshArguments(value: string): string[] {
|
|
12
|
-
const parts: string[] = [];
|
|
13
|
-
let current = "";
|
|
14
|
-
let quote: "'" | '"' | undefined;
|
|
15
|
-
let escaped = false;
|
|
16
|
-
let started = false;
|
|
17
|
-
for (const character of value) {
|
|
18
|
-
if (escaped) {
|
|
19
|
-
current += character;
|
|
20
|
-
escaped = false;
|
|
21
|
-
started = true;
|
|
22
|
-
continue;
|
|
23
|
-
}
|
|
24
|
-
if (character === "\\" && quote !== "'") {
|
|
25
|
-
escaped = true;
|
|
26
|
-
started = true;
|
|
27
|
-
continue;
|
|
28
|
-
}
|
|
29
|
-
if (quote) {
|
|
30
|
-
if (character === quote) quote = undefined;
|
|
31
|
-
else current += character;
|
|
32
|
-
continue;
|
|
33
|
-
}
|
|
34
|
-
if (character === "'" || character === '"') {
|
|
35
|
-
quote = character;
|
|
36
|
-
started = true;
|
|
37
|
-
} else if (/\s/.test(character)) {
|
|
38
|
-
if (started) {
|
|
39
|
-
parts.push(current);
|
|
40
|
-
current = "";
|
|
41
|
-
started = false;
|
|
42
|
-
}
|
|
43
|
-
} else {
|
|
44
|
-
current += character;
|
|
45
|
-
started = true;
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
if (quote) throw new Error("SSH arguments contain an unclosed quote");
|
|
49
|
-
if (escaped) throw new Error("SSH arguments end with an incomplete escape");
|
|
50
|
-
if (started) parts.push(current);
|
|
51
|
-
if (parts.length === 0) throw new Error("SSH target is required");
|
|
52
|
-
const target = parts.pop()!;
|
|
53
|
-
return [...parts, "--", target];
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
export async function showMachineMenu(
|
|
57
|
-
fleet: AgentFleet,
|
|
58
|
-
ctx: ExtensionContext,
|
|
59
|
-
): Promise<void> {
|
|
60
|
-
const config = await readMachinesConfig().catch((error) => {
|
|
61
|
-
ctx.ui.notify(
|
|
62
|
-
`Shepherdr machine config is invalid: ${error instanceof Error ? error.message : String(error)}`,
|
|
63
|
-
"error",
|
|
64
|
-
);
|
|
65
|
-
return undefined;
|
|
66
|
-
});
|
|
67
|
-
if (!config) return;
|
|
68
|
-
const statuses: MachineStatus[] = fleet.isActive()
|
|
69
|
-
? fleet.statuses()
|
|
70
|
-
: Object.keys(config.machines).map((name) => ({
|
|
71
|
-
local: false,
|
|
72
|
-
name,
|
|
73
|
-
status: "unavailable" as const,
|
|
74
|
-
}));
|
|
75
|
-
const selected = await ctx.ui.select("Shepherdr machines", [
|
|
76
|
-
"Add machine",
|
|
77
|
-
...statuses
|
|
78
|
-
.filter((machine) => !machine.local)
|
|
79
|
-
.map(
|
|
80
|
-
(machine) =>
|
|
81
|
-
`${machine.status === "connected" ? "●" : machine.status === "connecting" ? "◌" : "○"} ${machine.name} · ${machine.status}${machine.monitoringIssue ? " · monitoring needs attention" : ""}`,
|
|
82
|
-
),
|
|
83
|
-
]);
|
|
84
|
-
if (!selected) return;
|
|
85
|
-
if (selected === "Add machine") {
|
|
86
|
-
await addMachine(ctx, fleet);
|
|
87
|
-
return;
|
|
88
|
-
}
|
|
89
|
-
const name = selected.slice(2).split(" · ", 1)[0];
|
|
90
|
-
if (!name) return;
|
|
91
|
-
const status = statuses.find((machine) => machine.name === name);
|
|
92
|
-
const action = await ctx.ui.select(name, [
|
|
93
|
-
...(status?.monitoringIssue ? ["Retry monitoring"] : []),
|
|
94
|
-
...(status?.status !== "connected" ? ["Connect"] : []),
|
|
95
|
-
"Remove",
|
|
96
|
-
]);
|
|
97
|
-
if (action === "Connect" || action === "Retry monitoring") {
|
|
98
|
-
if (!fleet.isActive()) {
|
|
99
|
-
ctx.ui.notify(
|
|
100
|
-
"Fleet unavailable; run /herdr connect to retry",
|
|
101
|
-
"warning",
|
|
102
|
-
);
|
|
103
|
-
return;
|
|
104
|
-
}
|
|
105
|
-
ctx.ui.notify(fleet.connect(name), "info");
|
|
106
|
-
} else if (action === "Remove") {
|
|
107
|
-
const confirmed = await ctx.ui.confirm(
|
|
108
|
-
`Remove ${name}?`,
|
|
109
|
-
"The remote Shepherdr helper will remain available for other controllers",
|
|
110
|
-
);
|
|
111
|
-
if (!confirmed) return;
|
|
112
|
-
delete config.machines[name];
|
|
113
|
-
await writeMachinesConfig(config);
|
|
114
|
-
await fleet.reload();
|
|
115
|
-
ctx.ui.notify(`Removed ${name}`, "info");
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
async function addMachine(
|
|
120
|
-
ctx: ExtensionContext,
|
|
121
|
-
fleet: AgentFleet,
|
|
122
|
-
): Promise<void> {
|
|
123
|
-
const name = (await ctx.ui.input("Machine name", "desktop"))?.trim();
|
|
124
|
-
if (!name) return;
|
|
125
|
-
if (!isMachineName(name)) {
|
|
126
|
-
ctx.ui.notify("Machine name must match [a-z][a-z0-9_-]{0,31}", "error");
|
|
127
|
-
return;
|
|
128
|
-
}
|
|
129
|
-
const ssh = (
|
|
130
|
-
await ctx.ui.input("SSH options and target (target last)", name)
|
|
131
|
-
)?.trim();
|
|
132
|
-
if (!ssh) return;
|
|
133
|
-
let sshArguments: string[];
|
|
134
|
-
try {
|
|
135
|
-
sshArguments = parseSshArguments(ssh);
|
|
136
|
-
} catch (error) {
|
|
137
|
-
ctx.ui.notify(
|
|
138
|
-
error instanceof Error ? error.message : String(error),
|
|
139
|
-
"error",
|
|
140
|
-
);
|
|
141
|
-
return;
|
|
142
|
-
}
|
|
143
|
-
const session = (
|
|
144
|
-
await ctx.ui.input("Herdr session", "default (leave blank)")
|
|
145
|
-
)?.trim();
|
|
146
|
-
const config = await readMachinesConfig();
|
|
147
|
-
if (name === LOCAL_MACHINE || Object.hasOwn(config.machines, name)) {
|
|
148
|
-
ctx.ui.notify(`Machine ${name} already exists`, "error");
|
|
149
|
-
return;
|
|
150
|
-
}
|
|
151
|
-
config.machines[name] = {
|
|
152
|
-
command: ["ssh", "-o", "BatchMode=yes", ...sshArguments],
|
|
153
|
-
herdr: "herdr",
|
|
154
|
-
node: "node",
|
|
155
|
-
...(session && session !== "default (leave blank)" ? { session } : {}),
|
|
156
|
-
};
|
|
157
|
-
const path = await writeMachinesConfig(config);
|
|
158
|
-
await fleet.reload();
|
|
159
|
-
ctx.ui.notify(
|
|
160
|
-
`Added ${name} in ${path}; Shepherdr manages ~/.pi/agent/shepherdr.mjs remotely`,
|
|
161
|
-
"info",
|
|
162
|
-
);
|
|
163
|
-
}
|
package/src/machines-config.ts
DELETED
|
@@ -1,148 +0,0 @@
|
|
|
1
|
-
import { randomUUID } from "node:crypto";
|
|
2
|
-
import { mkdir, readFile, rename, unlink, writeFile } from "node:fs/promises";
|
|
3
|
-
import { homedir } from "node:os";
|
|
4
|
-
import { dirname, join } from "node:path";
|
|
5
|
-
|
|
6
|
-
const MACHINE_NAME = /^[a-z][a-z0-9_-]{0,31}$/;
|
|
7
|
-
|
|
8
|
-
export const LOCAL_MACHINE = "local";
|
|
9
|
-
|
|
10
|
-
export function isMachineName(value: string): boolean {
|
|
11
|
-
return MACHINE_NAME.test(value);
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export interface RemoteMachineConfig {
|
|
15
|
-
command: string[];
|
|
16
|
-
herdr: string;
|
|
17
|
-
node: string;
|
|
18
|
-
session?: string;
|
|
19
|
-
socket?: string;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export interface MachinesConfig {
|
|
23
|
-
machines: Record<string, RemoteMachineConfig>;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
function agentDir(): string {
|
|
27
|
-
return (
|
|
28
|
-
process.env["PI_CODING_AGENT_DIR"]?.trim() ||
|
|
29
|
-
join(homedir(), ".pi", "agent")
|
|
30
|
-
);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
function machinesConfigPath(): string {
|
|
34
|
-
return join(agentDir(), "shepherdr.json");
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
function optionalString(
|
|
38
|
-
value: Record<string, unknown>,
|
|
39
|
-
key: string,
|
|
40
|
-
): string | undefined {
|
|
41
|
-
const candidate = value[key];
|
|
42
|
-
if (candidate === undefined) return undefined;
|
|
43
|
-
if (typeof candidate !== "string" || !candidate.trim()) {
|
|
44
|
-
throw new Error(`${key} must be a non-empty string`);
|
|
45
|
-
}
|
|
46
|
-
return candidate.trim();
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
function parseMachine(name: string, value: unknown): RemoteMachineConfig {
|
|
50
|
-
if (!isMachineName(name)) {
|
|
51
|
-
throw new Error(
|
|
52
|
-
`machine name ${JSON.stringify(name)} must match ${MACHINE_NAME.source}`,
|
|
53
|
-
);
|
|
54
|
-
}
|
|
55
|
-
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
56
|
-
throw new Error(`machine ${JSON.stringify(name)} must be an object`);
|
|
57
|
-
}
|
|
58
|
-
const record = value as Record<string, unknown>;
|
|
59
|
-
const command = record["command"];
|
|
60
|
-
if (
|
|
61
|
-
!Array.isArray(command) ||
|
|
62
|
-
command.length === 0 ||
|
|
63
|
-
command.some((part) => typeof part !== "string" || !part)
|
|
64
|
-
) {
|
|
65
|
-
throw new Error(
|
|
66
|
-
`machine ${JSON.stringify(name)} command must be a non-empty string array`,
|
|
67
|
-
);
|
|
68
|
-
}
|
|
69
|
-
const session = optionalString(record, "session");
|
|
70
|
-
const socket = optionalString(record, "socket");
|
|
71
|
-
if (session && socket) {
|
|
72
|
-
throw new Error(
|
|
73
|
-
`machine ${JSON.stringify(name)} cannot set both session and socket`,
|
|
74
|
-
);
|
|
75
|
-
}
|
|
76
|
-
return {
|
|
77
|
-
command: command as string[],
|
|
78
|
-
herdr: optionalString(record, "herdr") ?? "herdr",
|
|
79
|
-
node: optionalString(record, "node") ?? "node",
|
|
80
|
-
...(session ? { session } : {}),
|
|
81
|
-
...(socket ? { socket } : {}),
|
|
82
|
-
};
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
export async function readMachinesConfig(): Promise<MachinesConfig> {
|
|
86
|
-
const path = machinesConfigPath();
|
|
87
|
-
let text: string;
|
|
88
|
-
try {
|
|
89
|
-
text = await readFile(path, "utf8");
|
|
90
|
-
} catch (error) {
|
|
91
|
-
if ((error as NodeJS.ErrnoException).code === "ENOENT") {
|
|
92
|
-
return { machines: {} };
|
|
93
|
-
}
|
|
94
|
-
throw new Error(
|
|
95
|
-
`could not read ${path}: ${error instanceof Error ? error.message : String(error)}`,
|
|
96
|
-
);
|
|
97
|
-
}
|
|
98
|
-
let value: unknown;
|
|
99
|
-
try {
|
|
100
|
-
value = JSON.parse(text);
|
|
101
|
-
} catch (error) {
|
|
102
|
-
throw new Error(
|
|
103
|
-
`could not parse ${path}: ${error instanceof Error ? error.message : String(error)}`,
|
|
104
|
-
);
|
|
105
|
-
}
|
|
106
|
-
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
107
|
-
throw new Error(`${path} must contain a JSON object`);
|
|
108
|
-
}
|
|
109
|
-
const record = value as Record<string, unknown>;
|
|
110
|
-
const machines = record["machines"] ?? {};
|
|
111
|
-
if (
|
|
112
|
-
typeof machines !== "object" ||
|
|
113
|
-
machines === null ||
|
|
114
|
-
Array.isArray(machines)
|
|
115
|
-
) {
|
|
116
|
-
throw new Error(`${path} machines must be an object`);
|
|
117
|
-
}
|
|
118
|
-
const parsed = Object.fromEntries(
|
|
119
|
-
Object.entries(machines).map(([name, machine]) => [
|
|
120
|
-
name,
|
|
121
|
-
parseMachine(name, machine),
|
|
122
|
-
]),
|
|
123
|
-
);
|
|
124
|
-
if (LOCAL_MACHINE in parsed)
|
|
125
|
-
throw new Error(
|
|
126
|
-
`remote machine ${JSON.stringify(LOCAL_MACHINE)} conflicts with local`,
|
|
127
|
-
);
|
|
128
|
-
return { machines: parsed };
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
export async function writeMachinesConfig(
|
|
132
|
-
config: MachinesConfig,
|
|
133
|
-
): Promise<string> {
|
|
134
|
-
const path = machinesConfigPath();
|
|
135
|
-
await mkdir(dirname(path), { recursive: true });
|
|
136
|
-
const temporaryPath = `${path}.${process.pid}.${randomUUID()}.tmp`;
|
|
137
|
-
try {
|
|
138
|
-
await writeFile(temporaryPath, `${JSON.stringify(config, null, 2)}\n`, {
|
|
139
|
-
encoding: "utf8",
|
|
140
|
-
mode: 0o600,
|
|
141
|
-
});
|
|
142
|
-
await rename(temporaryPath, path);
|
|
143
|
-
} catch (error) {
|
|
144
|
-
await unlink(temporaryPath).catch(() => undefined);
|
|
145
|
-
throw error;
|
|
146
|
-
}
|
|
147
|
-
return path;
|
|
148
|
-
}
|