@howaboua/pi-shepherdr 0.1.6 → 0.2.0
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 +9 -0
- package/README.md +20 -5
- package/package.json +3 -3
- package/src/agents-contract.ts +10 -3
- package/src/agents-operations.ts +54 -15
- package/src/agents-tool.ts +32 -18
- package/src/controller.ts +4 -8
- package/src/delivery.ts +24 -0
- package/src/fleet.ts +59 -63
- package/src/herdr.ts +17 -1
- package/src/launch.ts +0 -25
- package/src/machine-catalog.ts +60 -0
- package/src/messages.ts +66 -11
- package/src/monitor-record.ts +6 -19
- package/src/monitor-state.ts +14 -5
- package/src/monitor.ts +10 -7
- package/src/remote/shepherdr.mjs +29 -13
- package/src/remote-client.ts +17 -18
- package/src/session-reader.ts +21 -11
- package/src/settlement.ts +15 -13
- package/src/types.ts +9 -4
- package/src/widget.ts +4 -4
- package/src/machine-menu.ts +0 -163
- package/src/machines-config.ts +0 -148
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
|
-
}
|