@howaboua/pi-shepherdr 0.1.1 → 0.1.3
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 +34 -0
- package/README.md +57 -46
- package/biome.json +1 -1
- package/changelog.ts +337 -0
- package/index.ts +95 -8
- package/package.json +24 -12
- package/profiles/explorer/profile.json +8 -0
- package/profiles/explorer/prompt.md +1 -0
- package/profiles/general/profile.json +8 -0
- package/profiles/general/prompt.md +1 -0
- package/profiles/reviewer/prepare.mjs +170 -0
- package/profiles/reviewer/profile.json +9 -0
- package/profiles/reviewer/prompt.md +1 -0
- package/src/activity.ts +20 -1
- package/src/agents-contract.ts +142 -0
- package/src/agents-operations.ts +338 -0
- package/src/agents-tool.ts +440 -0
- package/src/ask-answer.ts +297 -0
- package/src/controller.ts +178 -0
- package/src/fleet.ts +556 -0
- package/src/herdr-client.ts +10 -1
- package/src/herdr.ts +19 -6
- package/src/launch.ts +74 -14
- package/src/machine-menu.ts +158 -0
- package/src/machines-config.ts +148 -0
- package/src/messages.ts +142 -10
- package/src/monitor-events.ts +7 -5
- package/src/monitor-record.ts +34 -1
- package/src/monitor-state.ts +40 -4
- package/src/monitor.ts +124 -56
- package/src/profiles.ts +291 -0
- package/src/remote/shepherdr.mjs +520 -0
- package/src/remote-client.ts +426 -0
- package/src/session-reader.ts +218 -71
- package/src/settlement.ts +246 -42
- package/src/types.ts +42 -1
- package/src/widget.ts +23 -8
- package/src/master-config.ts +0 -61
- package/src/master-mode.ts +0 -107
- package/src/tool.ts +0 -195
package/src/master-mode.ts
DELETED
|
@@ -1,107 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
ExtensionAPI,
|
|
3
|
-
ExtensionContext,
|
|
4
|
-
} from "@earendil-works/pi-coding-agent";
|
|
5
|
-
import { enableMasterDirectory, isMasterDirectory } from "./master-config.js";
|
|
6
|
-
import type { AgentMonitor } from "./monitor.js";
|
|
7
|
-
|
|
8
|
-
const TOOL_NAME = "herdr_agents";
|
|
9
|
-
|
|
10
|
-
export function registerMasterMode(
|
|
11
|
-
pi: ExtensionAPI,
|
|
12
|
-
getMonitor: () => AgentMonitor,
|
|
13
|
-
): void {
|
|
14
|
-
let monitor: AgentMonitor | undefined;
|
|
15
|
-
pi.registerCommand("herdr", {
|
|
16
|
-
description: "Enable Herdr master mode for this session or directory",
|
|
17
|
-
getArgumentCompletions: (prefix) =>
|
|
18
|
-
["master", "json"]
|
|
19
|
-
.filter((action) => action.startsWith(prefix.trim().toLowerCase()))
|
|
20
|
-
.map((value) => ({ label: value, value })),
|
|
21
|
-
handler: async (args, ctx) => {
|
|
22
|
-
const action = args.trim().toLowerCase();
|
|
23
|
-
if (action !== "master" && action !== "json") {
|
|
24
|
-
ctx.ui.notify("Usage: /herdr [master|json]", "warning");
|
|
25
|
-
return;
|
|
26
|
-
}
|
|
27
|
-
const activeMonitor = await activateMaster(pi, getMonitor, ctx);
|
|
28
|
-
if (!activeMonitor) return;
|
|
29
|
-
monitor = activeMonitor;
|
|
30
|
-
if (action === "master") {
|
|
31
|
-
ctx.ui.notify("Herdr master enabled for this Pi session", "info");
|
|
32
|
-
return;
|
|
33
|
-
}
|
|
34
|
-
try {
|
|
35
|
-
const path = await enableMasterDirectory(ctx.cwd);
|
|
36
|
-
ctx.ui.notify(`Herdr master enabled in ${path}`, "info");
|
|
37
|
-
} catch (error) {
|
|
38
|
-
ctx.ui.notify(
|
|
39
|
-
`Herdr master is active for this session but could not save ${ctx.cwd}/.pi/shepherdr.json: ${error instanceof Error ? error.message : String(error)}`,
|
|
40
|
-
"error",
|
|
41
|
-
);
|
|
42
|
-
}
|
|
43
|
-
},
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
pi.on("session_start", async (_event, ctx) => {
|
|
47
|
-
monitor?.deactivate();
|
|
48
|
-
let master = false;
|
|
49
|
-
try {
|
|
50
|
-
master = await isMasterDirectory(ctx.cwd);
|
|
51
|
-
} catch (error) {
|
|
52
|
-
ctx.ui.notify(
|
|
53
|
-
`Herdr master config is invalid: ${error instanceof Error ? error.message : String(error)}`,
|
|
54
|
-
"error",
|
|
55
|
-
);
|
|
56
|
-
}
|
|
57
|
-
if (!master) {
|
|
58
|
-
setToolActive(pi, false);
|
|
59
|
-
return;
|
|
60
|
-
}
|
|
61
|
-
const activeMonitor = await activateMaster(pi, getMonitor, ctx);
|
|
62
|
-
if (activeMonitor) monitor = activeMonitor;
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
pi.on("session_shutdown", async () => {
|
|
66
|
-
monitor?.deactivate();
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
async function activateMaster(
|
|
71
|
-
pi: ExtensionAPI,
|
|
72
|
-
getMonitor: () => AgentMonitor,
|
|
73
|
-
ctx: ExtensionContext,
|
|
74
|
-
): Promise<AgentMonitor | undefined> {
|
|
75
|
-
if (process.env["HERDR_ENV"] !== "1" || !process.env["HERDR_SOCKET_PATH"]) {
|
|
76
|
-
setToolActive(pi, false);
|
|
77
|
-
ctx.ui.notify("Herdr master mode requires Pi to run inside Herdr", "error");
|
|
78
|
-
return undefined;
|
|
79
|
-
}
|
|
80
|
-
const monitor = getMonitor();
|
|
81
|
-
try {
|
|
82
|
-
await monitor.client.request("ping");
|
|
83
|
-
setToolActive(pi, true);
|
|
84
|
-
await monitor.activate(ctx);
|
|
85
|
-
return monitor;
|
|
86
|
-
} catch (error) {
|
|
87
|
-
setToolActive(pi, false);
|
|
88
|
-
monitor.deactivate();
|
|
89
|
-
ctx.ui.notify(
|
|
90
|
-
`Herdr master could not start: ${error instanceof Error ? error.message : String(error)}`,
|
|
91
|
-
"error",
|
|
92
|
-
);
|
|
93
|
-
return undefined;
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
function setToolActive(pi: ExtensionAPI, active: boolean): void {
|
|
98
|
-
const current = pi.getActiveTools();
|
|
99
|
-
const withoutTool = current.filter((name) => name !== TOOL_NAME);
|
|
100
|
-
const next = active ? [...withoutTool, TOOL_NAME] : withoutTool;
|
|
101
|
-
if (
|
|
102
|
-
next.length !== current.length ||
|
|
103
|
-
next.some((name, index) => name !== current[index])
|
|
104
|
-
) {
|
|
105
|
-
pi.setActiveTools(next);
|
|
106
|
-
}
|
|
107
|
-
}
|
package/src/tool.ts
DELETED
|
@@ -1,195 +0,0 @@
|
|
|
1
|
-
import { StringEnum, Type } from "@earendil-works/pi-ai";
|
|
2
|
-
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
3
|
-
import { getSnapshot, resolvePiAgent } from "./herdr.js";
|
|
4
|
-
import {
|
|
5
|
-
START_PLACEMENTS,
|
|
6
|
-
type StartAgentParams,
|
|
7
|
-
startAgent,
|
|
8
|
-
} from "./launch.js";
|
|
9
|
-
import type { AgentMonitor } from "./monitor.js";
|
|
10
|
-
import type { MonitoredAgent, PaneInfo, SessionSnapshot } from "./types.js";
|
|
11
|
-
|
|
12
|
-
const ACTIONS = ["list", "start", "watch", "unwatch", "send"] as const;
|
|
13
|
-
const FIRE_AND_FORGET =
|
|
14
|
-
"Do not poll; completion or blockage will be delivered automatically";
|
|
15
|
-
|
|
16
|
-
type ToolParams = StartAgentParams & {
|
|
17
|
-
action: (typeof ACTIONS)[number];
|
|
18
|
-
target?: string;
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
function required(value: string | undefined, field: string): string {
|
|
22
|
-
if (!value?.trim()) throw new Error(`${field} is required for this action`);
|
|
23
|
-
return value.trim();
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
function result(value: Record<string, unknown>) {
|
|
27
|
-
return {
|
|
28
|
-
content: [{ type: "text" as const, text: JSON.stringify(value) }],
|
|
29
|
-
details: value,
|
|
30
|
-
};
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
function labels(snapshot: SessionSnapshot) {
|
|
34
|
-
return {
|
|
35
|
-
workspaces: new Map(
|
|
36
|
-
snapshot.workspaces.map((workspace) => [
|
|
37
|
-
workspace.workspace_id,
|
|
38
|
-
workspace.label,
|
|
39
|
-
]),
|
|
40
|
-
),
|
|
41
|
-
tabs: new Map(snapshot.tabs.map((tab) => [tab.tab_id, tab.label])),
|
|
42
|
-
};
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
function compactAgent(
|
|
46
|
-
agent: PaneInfo,
|
|
47
|
-
snapshot: SessionSnapshot,
|
|
48
|
-
monitored: boolean,
|
|
49
|
-
): Record<string, unknown> {
|
|
50
|
-
const names = labels(snapshot);
|
|
51
|
-
return {
|
|
52
|
-
id: agent.pane_id,
|
|
53
|
-
...(agent.name ? { name: agent.name } : {}),
|
|
54
|
-
status: agent.agent_status,
|
|
55
|
-
cwd: agent.foreground_cwd ?? agent.cwd ?? null,
|
|
56
|
-
workspace: names.workspaces.get(agent.workspace_id) ?? agent.workspace_id,
|
|
57
|
-
workspaceId: agent.workspace_id,
|
|
58
|
-
tab: names.tabs.get(agent.tab_id) ?? agent.tab_id,
|
|
59
|
-
tabId: agent.tab_id,
|
|
60
|
-
monitored,
|
|
61
|
-
};
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
function findRecord(
|
|
65
|
-
monitor: AgentMonitor,
|
|
66
|
-
target: string,
|
|
67
|
-
): MonitoredAgent | undefined {
|
|
68
|
-
const matches = monitor
|
|
69
|
-
.list()
|
|
70
|
-
.filter((agent) => agent.paneId === target || agent.name === target);
|
|
71
|
-
if (matches.length > 1) {
|
|
72
|
-
throw new Error(
|
|
73
|
-
`monitored agent name ${JSON.stringify(target)} is ambiguous`,
|
|
74
|
-
);
|
|
75
|
-
}
|
|
76
|
-
return matches[0];
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
export function registerHerdrAgentsTool(
|
|
80
|
-
pi: ExtensionAPI,
|
|
81
|
-
getMonitor: () => AgentMonitor,
|
|
82
|
-
): void {
|
|
83
|
-
pi.registerTool({
|
|
84
|
-
name: "herdr_agents",
|
|
85
|
-
label: "Shepherdr",
|
|
86
|
-
description:
|
|
87
|
-
"Herdr Pi agents. start needs name and placement; new_tab needs workspace; pane placement needs pane ID. watch/unwatch need target; send needs target and prompt.",
|
|
88
|
-
promptGuidelines: [
|
|
89
|
-
"Act as an orchestrator: delegate project implementation, synthesize worker results, and report them to the user. Work directly only when explicitly asked or for configuration, documentation, and routine operations in the current working directory",
|
|
90
|
-
],
|
|
91
|
-
parameters: Type.Object({
|
|
92
|
-
action: StringEnum(ACTIONS),
|
|
93
|
-
target: Type.Optional(
|
|
94
|
-
Type.String({ description: "Agent name or pane ID" }),
|
|
95
|
-
),
|
|
96
|
-
name: Type.Optional(
|
|
97
|
-
Type.String({
|
|
98
|
-
description: "Agent name; [a-z][a-z0-9_-]{0,31}",
|
|
99
|
-
}),
|
|
100
|
-
),
|
|
101
|
-
label: Type.Optional(
|
|
102
|
-
Type.String({
|
|
103
|
-
description:
|
|
104
|
-
"Pi session label; also labels a new tab; defaults to name",
|
|
105
|
-
}),
|
|
106
|
-
),
|
|
107
|
-
placement: Type.Optional(StringEnum(START_PLACEMENTS)),
|
|
108
|
-
workspace: Type.Optional(
|
|
109
|
-
Type.String({ description: "Workspace label or ID" }),
|
|
110
|
-
),
|
|
111
|
-
pane: Type.Optional(Type.String()),
|
|
112
|
-
cwd: Type.Optional(
|
|
113
|
-
Type.String({
|
|
114
|
-
description:
|
|
115
|
-
"Working directory for a new location; defaults to current",
|
|
116
|
-
}),
|
|
117
|
-
),
|
|
118
|
-
prompt: Type.Optional(
|
|
119
|
-
Type.String({
|
|
120
|
-
description: "Initial task (start) or message (send)",
|
|
121
|
-
}),
|
|
122
|
-
),
|
|
123
|
-
}),
|
|
124
|
-
executionMode: "sequential",
|
|
125
|
-
async execute(_toolCallId, params: ToolParams, _signal, _onUpdate, ctx) {
|
|
126
|
-
const monitor = getMonitor();
|
|
127
|
-
const client = monitor.client;
|
|
128
|
-
if (params.action === "list") {
|
|
129
|
-
const snapshot = await getSnapshot(client);
|
|
130
|
-
return result({
|
|
131
|
-
agents: snapshot.agents
|
|
132
|
-
.filter(
|
|
133
|
-
(agent) =>
|
|
134
|
-
agent.agent === "pi" &&
|
|
135
|
-
agent.pane_id !== process.env["HERDR_PANE_ID"],
|
|
136
|
-
)
|
|
137
|
-
.map((agent) =>
|
|
138
|
-
compactAgent(agent, snapshot, monitor.isMonitored(agent.pane_id)),
|
|
139
|
-
),
|
|
140
|
-
workspaces: snapshot.workspaces.map((workspace) => ({
|
|
141
|
-
id: workspace.workspace_id,
|
|
142
|
-
label: workspace.label,
|
|
143
|
-
})),
|
|
144
|
-
});
|
|
145
|
-
}
|
|
146
|
-
if (params.action === "start") {
|
|
147
|
-
const started = await startAgent(client, monitor, params, ctx.cwd);
|
|
148
|
-
return result({
|
|
149
|
-
started: true,
|
|
150
|
-
id: started.id,
|
|
151
|
-
...(params.prompt?.trim() ? { next: FIRE_AND_FORGET } : {}),
|
|
152
|
-
});
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
const target = required(params.target, "target");
|
|
156
|
-
if (params.action === "unwatch") {
|
|
157
|
-
const record = findRecord(monitor, target);
|
|
158
|
-
if (!record) return result({ unwatched: false, target });
|
|
159
|
-
await monitor.unwatch(record.paneId);
|
|
160
|
-
return result({ unwatched: true, id: record.paneId });
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
const agent = await resolvePiAgent(client, target);
|
|
164
|
-
if (params.action === "watch") {
|
|
165
|
-
await monitor.watch(agent);
|
|
166
|
-
return result({
|
|
167
|
-
watched: true,
|
|
168
|
-
id: agent.pane_id,
|
|
169
|
-
next: FIRE_AND_FORGET,
|
|
170
|
-
});
|
|
171
|
-
}
|
|
172
|
-
if (params.action === "send") {
|
|
173
|
-
const prompt = required(params.prompt, "prompt");
|
|
174
|
-
if (!monitor.isMonitored(agent.pane_id)) await monitor.watch(agent);
|
|
175
|
-
const attempt = monitor.beginWork(agent.pane_id, prompt);
|
|
176
|
-
try {
|
|
177
|
-
await client.request("agent.prompt", {
|
|
178
|
-
target: agent.pane_id,
|
|
179
|
-
text: prompt,
|
|
180
|
-
});
|
|
181
|
-
monitor.acceptWork(attempt);
|
|
182
|
-
} catch (error) {
|
|
183
|
-
await monitor.handleWorkFailure(attempt, error);
|
|
184
|
-
throw error;
|
|
185
|
-
}
|
|
186
|
-
return result({
|
|
187
|
-
sent: true,
|
|
188
|
-
id: agent.pane_id,
|
|
189
|
-
next: FIRE_AND_FORGET,
|
|
190
|
-
});
|
|
191
|
-
}
|
|
192
|
-
throw new Error(`unsupported action ${params.action}`);
|
|
193
|
-
},
|
|
194
|
-
});
|
|
195
|
-
}
|