@howaboua/pi-shepherdr 0.2.5 → 0.2.6
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 +8 -0
- package/README.md +3 -1
- package/package.json +3 -3
- package/src/agents-answer.ts +108 -0
- package/src/agents-contract.ts +18 -1
- package/src/agents-discovery.ts +211 -0
- package/src/agents-spawn.ts +168 -0
- package/src/agents-tool.ts +11 -195
- package/src/agents-work.ts +194 -0
- package/src/ask-answer.ts +54 -2
- package/src/messages.ts +9 -3
- package/src/monitor.ts +2 -1
- package/src/remote/shepherdr-session.mjs +294 -0
- package/src/remote/shepherdr.mjs +5 -233
- package/src/remote-client.ts +9 -2
- package/src/session-reader.ts +58 -0
- package/src/settlement.ts +19 -1
- package/src/types.ts +6 -0
- package/src/agents-operations.ts +0 -413
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
# @howaboua/pi-shepherdr
|
|
2
2
|
|
|
3
|
+
## 0.2.6
|
|
4
|
+
|
|
5
|
+
- Fixed exact Ask answer confirmation and retry reconciliation without resending input.
|
|
6
|
+
|
|
7
|
+
- Keep answers pending while the original Ask is still open.
|
|
8
|
+
- Preserve accepted receipts when the worker's reply fails.
|
|
9
|
+
- Report dismissed Asks as rejected.
|
|
10
|
+
|
|
3
11
|
## 0.2.5
|
|
4
12
|
|
|
5
13
|
- GPT-6 Sol and Luna now share Astra's Codex support.
|
package/README.md
CHANGED
|
@@ -61,7 +61,9 @@ Call the `agents` tool with `action: "help"` before first use, then send flat re
|
|
|
61
61
|
| `watch` | Push future settlement from an existing Pi agent |
|
|
62
62
|
| `unwatch` | Stop reporting an agent |
|
|
63
63
|
|
|
64
|
-
`spawn
|
|
64
|
+
`spawn` and `assign` block by default. Set `blocking: false` when the controller should continue other work immediately. Task completion and blockage are then delivered automatically.
|
|
65
|
+
|
|
66
|
+
`answer` requires the pending `ask_id` from `read` or a blocked report. It returns `accepted` only when that exact Ask persisted the supplied responses; an accepted retry sends no input.
|
|
65
67
|
|
|
66
68
|
Questions, status updates and replies use `send`. It returns after submission, does not accept `blocking`, and never creates or changes a watch or task. Use `assign` only to delegate work whose result you need, not to exchange coordination messages.
|
|
67
69
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@howaboua/pi-shepherdr",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.6",
|
|
4
4
|
"description": "Persistent blocking and asynchronous Pi agents through Herdr.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -43,7 +43,7 @@
|
|
|
43
43
|
"pack:dry": "npm pack --dry-run"
|
|
44
44
|
},
|
|
45
45
|
"peerDependencies": {
|
|
46
|
-
"@howaboua/pi-codex-conversion": ">=3.0.
|
|
46
|
+
"@howaboua/pi-codex-conversion": ">=3.0.38",
|
|
47
47
|
"@earendil-works/pi-ai": ">=0.84.3",
|
|
48
48
|
"@earendil-works/pi-coding-agent": ">=0.84.4",
|
|
49
49
|
"@earendil-works/pi-tui": ">=0.84.3",
|
|
@@ -55,7 +55,7 @@
|
|
|
55
55
|
}
|
|
56
56
|
},
|
|
57
57
|
"devDependencies": {
|
|
58
|
-
"@howaboua/pi-codex-conversion": "3.0.
|
|
58
|
+
"@howaboua/pi-codex-conversion": "3.0.38",
|
|
59
59
|
"@biomejs/biome": "^2.5.10",
|
|
60
60
|
"@earendil-works/pi-ai": "0.87.0",
|
|
61
61
|
"@earendil-works/pi-coding-agent": "0.87.0",
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import type { AgentToolUpdateCallback } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import type { AgentsParams } from "./agents-contract.js";
|
|
3
|
+
import {
|
|
4
|
+
dispatchAgentWork,
|
|
5
|
+
settlementResult,
|
|
6
|
+
toolResult,
|
|
7
|
+
} from "./agents-work.js";
|
|
8
|
+
import {
|
|
9
|
+
type AskAnswer,
|
|
10
|
+
answersMatch,
|
|
11
|
+
prepareAskAnswer,
|
|
12
|
+
sameAgentIdentity,
|
|
13
|
+
} from "./ask-answer.js";
|
|
14
|
+
import type { ConnectedMachine } from "./fleet.js";
|
|
15
|
+
import { getAgent } from "./herdr.js";
|
|
16
|
+
import type { PaneInfo } from "./types.js";
|
|
17
|
+
|
|
18
|
+
export async function answerAgent(
|
|
19
|
+
runtime: ConnectedMachine,
|
|
20
|
+
panel: PaneInfo,
|
|
21
|
+
params: AgentsParams,
|
|
22
|
+
signal: AbortSignal,
|
|
23
|
+
onUpdate: AgentToolUpdateCallback<Record<string, unknown>>,
|
|
24
|
+
) {
|
|
25
|
+
const answers = params.answers as AskAnswer[];
|
|
26
|
+
const askId = params.ask_id!.trim();
|
|
27
|
+
const view = await runtime.monitor.view(panel);
|
|
28
|
+
const prior = view.askResults?.[askId];
|
|
29
|
+
if (prior) {
|
|
30
|
+
const accepted =
|
|
31
|
+
prior.status === "accepted" && answersMatch(answers, prior.responses);
|
|
32
|
+
return toolResult({
|
|
33
|
+
answered: accepted,
|
|
34
|
+
ask_id: askId,
|
|
35
|
+
machine: runtime.machine,
|
|
36
|
+
status:
|
|
37
|
+
prior.status === "rejected"
|
|
38
|
+
? "rejected"
|
|
39
|
+
: accepted
|
|
40
|
+
? "accepted"
|
|
41
|
+
: "unknown",
|
|
42
|
+
target: panel.pane_id,
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
if (view.ask?.toolCallId !== askId) {
|
|
46
|
+
return toolResult({
|
|
47
|
+
answered: false,
|
|
48
|
+
ask_id: askId,
|
|
49
|
+
machine: runtime.machine,
|
|
50
|
+
status: "unknown",
|
|
51
|
+
target: panel.pane_id,
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
const prepared = await prepareAskAnswer(
|
|
55
|
+
runtime.client,
|
|
56
|
+
runtime.monitor,
|
|
57
|
+
panel,
|
|
58
|
+
answers,
|
|
59
|
+
signal,
|
|
60
|
+
askId,
|
|
61
|
+
);
|
|
62
|
+
const task = `Answer: ${prepared.ask.prompts
|
|
63
|
+
.map((prompt) => prompt.title)
|
|
64
|
+
.join(", ")}`;
|
|
65
|
+
const { settlement, warning } = await dispatchAgentWork(
|
|
66
|
+
runtime,
|
|
67
|
+
panel,
|
|
68
|
+
task,
|
|
69
|
+
true,
|
|
70
|
+
signal,
|
|
71
|
+
onUpdate,
|
|
72
|
+
prepared.submit,
|
|
73
|
+
{ answeringAskId: askId },
|
|
74
|
+
);
|
|
75
|
+
const current = await getAgent(runtime.client, panel.pane_id);
|
|
76
|
+
const result = (await runtime.monitor.view(current)).askResults?.[askId];
|
|
77
|
+
const accepted =
|
|
78
|
+
sameAgentIdentity(panel, current) &&
|
|
79
|
+
result?.status === "accepted" &&
|
|
80
|
+
answersMatch(answers, result.responses);
|
|
81
|
+
const {
|
|
82
|
+
machine: _workerMachine,
|
|
83
|
+
status: workerStatus,
|
|
84
|
+
target: _workerTarget,
|
|
85
|
+
...workerResult
|
|
86
|
+
} = settlement
|
|
87
|
+
? settlementResult(runtime.machine, settlement, {
|
|
88
|
+
reportWorkerError: true,
|
|
89
|
+
})
|
|
90
|
+
: {};
|
|
91
|
+
return toolResult(
|
|
92
|
+
{
|
|
93
|
+
...workerResult,
|
|
94
|
+
answered: accepted,
|
|
95
|
+
ask_id: askId,
|
|
96
|
+
machine: runtime.machine,
|
|
97
|
+
status:
|
|
98
|
+
result?.status === "rejected"
|
|
99
|
+
? "rejected"
|
|
100
|
+
: accepted
|
|
101
|
+
? "accepted"
|
|
102
|
+
: "unknown",
|
|
103
|
+
target: panel.pane_id,
|
|
104
|
+
...(workerStatus ? { worker_status: workerStatus } : {}),
|
|
105
|
+
},
|
|
106
|
+
warning,
|
|
107
|
+
);
|
|
108
|
+
}
|
package/src/agents-contract.ts
CHANGED
|
@@ -42,7 +42,7 @@ const ACTION_FIELDS: Record<(typeof ACTIONS)[number], ReadonlySet<string>> = {
|
|
|
42
42
|
send: new Set(["action", "machine", "target", "message"]),
|
|
43
43
|
assign: new Set(["action", "machine", "target", "message", "blocking"]),
|
|
44
44
|
read: new Set(["action", "machine", "target", "source", "lines"]),
|
|
45
|
-
answer: new Set(["action", "machine", "target", "
|
|
45
|
+
answer: new Set(["action", "machine", "target", "ask_id", "answers"]),
|
|
46
46
|
};
|
|
47
47
|
|
|
48
48
|
const AskAnswerParameters = Type.Object(
|
|
@@ -86,6 +86,7 @@ const AgentsRequest = Type.Object(
|
|
|
86
86
|
status: Type.Optional(StringEnum(STATUSES)),
|
|
87
87
|
source: Type.Optional(StringEnum(READ_SOURCES)),
|
|
88
88
|
lines: Type.Optional(Type.Integer({ minimum: 1, maximum: 200 })),
|
|
89
|
+
ask_id: Type.Optional(Type.String()),
|
|
89
90
|
answers: Type.Optional(Type.Array(AskAnswerParameters)),
|
|
90
91
|
},
|
|
91
92
|
{ additionalProperties: false },
|
|
@@ -96,6 +97,14 @@ export const AgentsParameters = Type.Object({ action: StringEnum(ACTIONS) });
|
|
|
96
97
|
export type AgentsParams = Static<typeof AgentsRequest>;
|
|
97
98
|
export type AgentsToolParams = Static<typeof AgentsParameters>;
|
|
98
99
|
|
|
100
|
+
export function requiredAgentField(
|
|
101
|
+
value: string | undefined,
|
|
102
|
+
field: string,
|
|
103
|
+
): string {
|
|
104
|
+
if (!value?.trim()) throw new Error(`${field} is required for this action`);
|
|
105
|
+
return value.trim();
|
|
106
|
+
}
|
|
107
|
+
|
|
99
108
|
export function parseAgentsRequest(input: unknown): AgentsParams {
|
|
100
109
|
let value = input;
|
|
101
110
|
if (typeof input === "string") {
|
|
@@ -123,6 +132,14 @@ export function parseAgentsRequest(input: unknown): AgentsParams {
|
|
|
123
132
|
"send is message-only; omit blocking. Use assign to delegate work",
|
|
124
133
|
);
|
|
125
134
|
}
|
|
135
|
+
const request = value as Record<string, unknown>;
|
|
136
|
+
const askId = request["ask_id"];
|
|
137
|
+
if (action === "answer" && (typeof askId !== "string" || !askId.trim())) {
|
|
138
|
+
throw new Error("ask_id is required for answer");
|
|
139
|
+
}
|
|
140
|
+
if (action === "answer" && !Array.isArray(request["answers"])) {
|
|
141
|
+
throw new Error("answers is required for answer");
|
|
142
|
+
}
|
|
126
143
|
const unknown = Object.keys(value).filter(
|
|
127
144
|
(key) => !ACTION_FIELDS[action as keyof typeof ACTION_FIELDS].has(key),
|
|
128
145
|
);
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
import type { AgentsParams, READ_SOURCES } from "./agents-contract.js";
|
|
2
|
+
import type { AgentFleet, ConnectedMachine } from "./fleet.js";
|
|
3
|
+
import { loadAgentProfiles } from "./profiles.js";
|
|
4
|
+
import type { AgentStatus, PaneInfo, SessionSnapshot } from "./types.js";
|
|
5
|
+
|
|
6
|
+
const MAX_LIST_ITEMS = 30;
|
|
7
|
+
const MAX_TERMINAL_READ_CHARS = 36_000;
|
|
8
|
+
|
|
9
|
+
export async function agentsHelp(): Promise<Record<string, unknown>> {
|
|
10
|
+
const profiles = await loadAgentProfiles();
|
|
11
|
+
return {
|
|
12
|
+
actions: {
|
|
13
|
+
help: "",
|
|
14
|
+
list: "machine?",
|
|
15
|
+
find: "query? status? machine?",
|
|
16
|
+
spawn:
|
|
17
|
+
"agent_type label message name? machine? placement? workspace? pane? cwd? base? blocking?",
|
|
18
|
+
watch: "target machine?",
|
|
19
|
+
unwatch: "target machine?",
|
|
20
|
+
send: "target message machine?",
|
|
21
|
+
assign: "target message machine? blocking?",
|
|
22
|
+
read: "target machine? source? lines?",
|
|
23
|
+
answer: "target ask_id answers machine?",
|
|
24
|
+
},
|
|
25
|
+
rules: {
|
|
26
|
+
machine:
|
|
27
|
+
"Omit for local (host running Pi); list/find omit for all machines. Remote: profile ID from list, not label/hostname",
|
|
28
|
+
target: "Use spawn/find target exactly",
|
|
29
|
+
label: "2-3 words; tab/session",
|
|
30
|
+
answers: "[{selections?:string[],other?:string,comment?:string}]",
|
|
31
|
+
ask_id: "Exact pending Ask ID",
|
|
32
|
+
send: "Peer questions, updates, replies; submission only, no wait or watch",
|
|
33
|
+
assign: "Delegate a task to an existing agent",
|
|
34
|
+
blocking:
|
|
35
|
+
"spawn/assign default true; false pushes task settlement; never poll. Reviewer spawns always block; await review before working its scope",
|
|
36
|
+
watch:
|
|
37
|
+
"Explicit watch persists until unwatch; automatic task watches end on finish/failure, not blockage",
|
|
38
|
+
prompt:
|
|
39
|
+
"Only task + inaccessible context; no method/evidence/reporting boilerplate",
|
|
40
|
+
slash:
|
|
41
|
+
"Leading / uses target Pi commands/skills/templates; extension commands are submission-only, even with assign/spawn; TUI-only commands unavailable",
|
|
42
|
+
reuse:
|
|
43
|
+
"Reuse only same investigation; reviews independent; new scope = new agent",
|
|
44
|
+
...(profiles.has("general")
|
|
45
|
+
? {
|
|
46
|
+
general: "Only when requested/orchestrating",
|
|
47
|
+
}
|
|
48
|
+
: {}),
|
|
49
|
+
},
|
|
50
|
+
profiles: Object.fromEntries(
|
|
51
|
+
[...profiles].map(([name, profile]) => [name, profile.description]),
|
|
52
|
+
),
|
|
53
|
+
advanced: "herdr --skill: workspace/tab/pane/process/focus/layout/terminal",
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function labels(snapshot: SessionSnapshot) {
|
|
58
|
+
return {
|
|
59
|
+
panes: new Map(snapshot.panes.map((pane) => [pane.pane_id, pane.label])),
|
|
60
|
+
tabs: new Map(snapshot.tabs.map((tab) => [tab.tab_id, tab.label])),
|
|
61
|
+
workspaces: new Map(
|
|
62
|
+
snapshot.workspaces.map((workspace) => [
|
|
63
|
+
workspace.workspace_id,
|
|
64
|
+
workspace.label,
|
|
65
|
+
]),
|
|
66
|
+
),
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function compactAgent(
|
|
71
|
+
agent: PaneInfo,
|
|
72
|
+
snapshot: SessionSnapshot,
|
|
73
|
+
machine: string,
|
|
74
|
+
monitored: boolean,
|
|
75
|
+
): Record<string, unknown> {
|
|
76
|
+
const names = labels(snapshot);
|
|
77
|
+
return {
|
|
78
|
+
machine,
|
|
79
|
+
target: agent.pane_id,
|
|
80
|
+
...(agent.name ? { name: agent.name } : {}),
|
|
81
|
+
...(agent.label || names.panes.get(agent.pane_id)
|
|
82
|
+
? { label: agent.label ?? names.panes.get(agent.pane_id) }
|
|
83
|
+
: {}),
|
|
84
|
+
status: agent.agent_status,
|
|
85
|
+
cwd: agent.foreground_cwd ?? agent.cwd ?? null,
|
|
86
|
+
workspace: names.workspaces.get(agent.workspace_id) ?? agent.workspace_id,
|
|
87
|
+
tab: names.tabs.get(agent.tab_id) ?? agent.tab_id,
|
|
88
|
+
monitored,
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function matchesAgent(
|
|
93
|
+
agent: Record<string, unknown>,
|
|
94
|
+
query: string | undefined,
|
|
95
|
+
status: AgentStatus | undefined,
|
|
96
|
+
): boolean {
|
|
97
|
+
if (status && agent["status"] !== status) return false;
|
|
98
|
+
if (!query) return true;
|
|
99
|
+
const haystack = Object.values(agent)
|
|
100
|
+
.filter((value) => typeof value === "string")
|
|
101
|
+
.join("\n")
|
|
102
|
+
.toLowerCase();
|
|
103
|
+
return haystack.includes(query.toLowerCase());
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export async function listFleetAgents(
|
|
107
|
+
fleet: AgentFleet,
|
|
108
|
+
params: Pick<AgentsParams, "machine" | "query" | "status">,
|
|
109
|
+
): Promise<Record<string, unknown>> {
|
|
110
|
+
const [profiles, machines] = await Promise.all([
|
|
111
|
+
loadAgentProfiles(),
|
|
112
|
+
fleet.snapshots(params.machine),
|
|
113
|
+
]);
|
|
114
|
+
const agents = machines.flatMap((machine) => {
|
|
115
|
+
if (!machine.snapshot) return [];
|
|
116
|
+
return machine.snapshot.agents
|
|
117
|
+
.filter(
|
|
118
|
+
(agent) =>
|
|
119
|
+
agent.agent === "pi" &&
|
|
120
|
+
(!machine.local || agent.pane_id !== process.env["HERDR_PANE_ID"]),
|
|
121
|
+
)
|
|
122
|
+
.map((agent) =>
|
|
123
|
+
compactAgent(
|
|
124
|
+
agent,
|
|
125
|
+
machine.snapshot!,
|
|
126
|
+
machine.id,
|
|
127
|
+
machine.monitoredPaneIds?.has(agent.pane_id) ?? false,
|
|
128
|
+
),
|
|
129
|
+
)
|
|
130
|
+
.filter((agent) => matchesAgent(agent, params.query, params.status));
|
|
131
|
+
});
|
|
132
|
+
const workspaces = machines.flatMap((machine) =>
|
|
133
|
+
(machine.snapshot?.workspaces ?? []).map((workspace) => ({
|
|
134
|
+
machine: machine.id,
|
|
135
|
+
id: workspace.workspace_id,
|
|
136
|
+
label: workspace.label,
|
|
137
|
+
})),
|
|
138
|
+
);
|
|
139
|
+
return {
|
|
140
|
+
profiles: Object.fromEntries(
|
|
141
|
+
[...profiles].map(([name, profile]) => [name, profile.description]),
|
|
142
|
+
),
|
|
143
|
+
machines: machines.map(
|
|
144
|
+
({
|
|
145
|
+
snapshot: _snapshot,
|
|
146
|
+
monitoredPaneIds: _monitoredPaneIds,
|
|
147
|
+
...machine
|
|
148
|
+
}) => machine,
|
|
149
|
+
),
|
|
150
|
+
agents: agents.slice(0, MAX_LIST_ITEMS),
|
|
151
|
+
workspaces: workspaces.slice(0, MAX_LIST_ITEMS),
|
|
152
|
+
...(agents.length > MAX_LIST_ITEMS
|
|
153
|
+
? { moreAgents: agents.length - MAX_LIST_ITEMS }
|
|
154
|
+
: {}),
|
|
155
|
+
...(workspaces.length > MAX_LIST_ITEMS
|
|
156
|
+
? { moreWorkspaces: workspaces.length - MAX_LIST_ITEMS }
|
|
157
|
+
: {}),
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
export async function findFleetAgents(
|
|
162
|
+
fleet: AgentFleet,
|
|
163
|
+
params: Pick<AgentsParams, "machine" | "query" | "status">,
|
|
164
|
+
): Promise<Record<string, unknown>> {
|
|
165
|
+
const listed = await listFleetAgents(fleet, params);
|
|
166
|
+
return {
|
|
167
|
+
agents: listed["agents"],
|
|
168
|
+
...(listed["moreAgents"] === undefined
|
|
169
|
+
? {}
|
|
170
|
+
: { moreAgents: listed["moreAgents"] }),
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
export async function readAgentTerminal(
|
|
175
|
+
runtime: ConnectedMachine,
|
|
176
|
+
panel: PaneInfo,
|
|
177
|
+
source: Exclude<(typeof READ_SOURCES)[number], "latest">,
|
|
178
|
+
lines: number,
|
|
179
|
+
): Promise<Record<string, unknown>> {
|
|
180
|
+
const value = await runtime.client.request<unknown>("agent.read", {
|
|
181
|
+
target: panel.pane_id,
|
|
182
|
+
source,
|
|
183
|
+
format: "text",
|
|
184
|
+
lines,
|
|
185
|
+
strip_ansi: true,
|
|
186
|
+
});
|
|
187
|
+
if (
|
|
188
|
+
typeof value !== "object" ||
|
|
189
|
+
value === null ||
|
|
190
|
+
!("read" in value) ||
|
|
191
|
+
typeof value.read !== "object" ||
|
|
192
|
+
value.read === null ||
|
|
193
|
+
!("text" in value.read) ||
|
|
194
|
+
typeof value.read.text !== "string"
|
|
195
|
+
) {
|
|
196
|
+
throw new Error("Herdr agent.read returned no text");
|
|
197
|
+
}
|
|
198
|
+
const truncated = value.read.text.length > MAX_TERMINAL_READ_CHARS;
|
|
199
|
+
return {
|
|
200
|
+
machine: runtime.machine,
|
|
201
|
+
target: panel.pane_id,
|
|
202
|
+
status: panel.agent_status,
|
|
203
|
+
text: truncated
|
|
204
|
+
? `${value.read.text.slice(0, MAX_TERMINAL_READ_CHARS)}\n…`
|
|
205
|
+
: value.read.text,
|
|
206
|
+
...(truncated ||
|
|
207
|
+
("truncated" in value.read && value.read.truncated === true)
|
|
208
|
+
? { truncated: true }
|
|
209
|
+
: {}),
|
|
210
|
+
};
|
|
211
|
+
}
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import type { AgentToolUpdateCallback } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import {
|
|
3
|
+
type AgentsParams,
|
|
4
|
+
requiredAgentField as required,
|
|
5
|
+
shouldBlockAgentSpawn,
|
|
6
|
+
} from "./agents-contract.js";
|
|
7
|
+
import {
|
|
8
|
+
allocateAgentName,
|
|
9
|
+
dispatchAgentWork,
|
|
10
|
+
reportProgress,
|
|
11
|
+
settlementResult,
|
|
12
|
+
toolResult,
|
|
13
|
+
} from "./agents-work.js";
|
|
14
|
+
import type { AgentFleet, ConnectedMachine } from "./fleet.js";
|
|
15
|
+
import { isDispatchRejected } from "./herdr-client.js";
|
|
16
|
+
import {
|
|
17
|
+
resolvePreparationDirectory,
|
|
18
|
+
rollbackStartedAgent,
|
|
19
|
+
startAgent,
|
|
20
|
+
} from "./launch.js";
|
|
21
|
+
import { attributeAgentPrompt } from "./messages.js";
|
|
22
|
+
import {
|
|
23
|
+
loadAgentProfiles,
|
|
24
|
+
prepareProfileMessage,
|
|
25
|
+
profileAgentArgs,
|
|
26
|
+
} from "./profiles.js";
|
|
27
|
+
|
|
28
|
+
function agentLabel(value: string | undefined): string {
|
|
29
|
+
const label = required(value, "label");
|
|
30
|
+
const words = label.split(/\s+/u);
|
|
31
|
+
if (words.length < 2 || words.length > 3) {
|
|
32
|
+
throw new Error("label must contain 2 or 3 words");
|
|
33
|
+
}
|
|
34
|
+
return label;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export async function spawnAgent(
|
|
38
|
+
fleet: AgentFleet,
|
|
39
|
+
runtime: ConnectedMachine,
|
|
40
|
+
params: AgentsParams,
|
|
41
|
+
signal: AbortSignal,
|
|
42
|
+
onUpdate: AgentToolUpdateCallback<Record<string, unknown>>,
|
|
43
|
+
) {
|
|
44
|
+
const profiles = await loadAgentProfiles();
|
|
45
|
+
const profileName = required(params.agent_type, "agent_type");
|
|
46
|
+
const profile = profiles.get(profileName);
|
|
47
|
+
if (!profile) {
|
|
48
|
+
throw new Error(
|
|
49
|
+
`unknown agent_type ${JSON.stringify(profileName)}; available: ${[...profiles.keys()].join(", ")}`,
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
const label = agentLabel(params.label);
|
|
53
|
+
const name = params.name?.trim() || (await allocateAgentName(runtime, label));
|
|
54
|
+
const placement =
|
|
55
|
+
params.placement ??
|
|
56
|
+
(runtime.local && process.env["HERDR_WORKSPACE_ID"]
|
|
57
|
+
? "new_tab"
|
|
58
|
+
: "new_workspace");
|
|
59
|
+
const workspace =
|
|
60
|
+
params.workspace ??
|
|
61
|
+
(placement === "new_tab" && runtime.local
|
|
62
|
+
? process.env["HERDR_WORKSPACE_ID"]
|
|
63
|
+
: undefined);
|
|
64
|
+
const startParams = {
|
|
65
|
+
name,
|
|
66
|
+
label,
|
|
67
|
+
placement,
|
|
68
|
+
...(workspace ? { workspace } : {}),
|
|
69
|
+
...(params.pane ? { pane: params.pane } : {}),
|
|
70
|
+
...(params.cwd ? { cwd: params.cwd } : {}),
|
|
71
|
+
};
|
|
72
|
+
const cwd = await resolvePreparationDirectory(
|
|
73
|
+
runtime.client,
|
|
74
|
+
startParams,
|
|
75
|
+
runtime.fallbackCwd,
|
|
76
|
+
runtime.resolveDirectory,
|
|
77
|
+
);
|
|
78
|
+
const input = required(params.message, "message");
|
|
79
|
+
const message = await prepareProfileMessage(
|
|
80
|
+
profile,
|
|
81
|
+
{
|
|
82
|
+
cwd,
|
|
83
|
+
message: input,
|
|
84
|
+
...(params.base ? { base: params.base } : {}),
|
|
85
|
+
},
|
|
86
|
+
{ targetLocal: runtime.local },
|
|
87
|
+
);
|
|
88
|
+
const attributedMessage = await attributeAgentPrompt(
|
|
89
|
+
fleet.connected().client,
|
|
90
|
+
input.startsWith("/") ? input : message,
|
|
91
|
+
"task",
|
|
92
|
+
);
|
|
93
|
+
if (input.startsWith("/") && message !== input)
|
|
94
|
+
attributedMessage.context = message;
|
|
95
|
+
reportProgress(onUpdate, `Spawning ${label}`, {
|
|
96
|
+
machine: runtime.machine,
|
|
97
|
+
name,
|
|
98
|
+
profile: profile.name,
|
|
99
|
+
status: "starting",
|
|
100
|
+
});
|
|
101
|
+
const started = await startAgent(
|
|
102
|
+
runtime.client,
|
|
103
|
+
startParams,
|
|
104
|
+
runtime.fallbackCwd,
|
|
105
|
+
runtime.resolveDirectory,
|
|
106
|
+
{
|
|
107
|
+
agentArgs: profileAgentArgs(profile, {
|
|
108
|
+
targetLocal: runtime.local,
|
|
109
|
+
}),
|
|
110
|
+
},
|
|
111
|
+
);
|
|
112
|
+
let promptSubmissionStarted = false;
|
|
113
|
+
let promptAccepted = false;
|
|
114
|
+
let dispatch;
|
|
115
|
+
const blocking = shouldBlockAgentSpawn(profile.name, params.blocking);
|
|
116
|
+
try {
|
|
117
|
+
dispatch = await dispatchAgentWork(
|
|
118
|
+
runtime,
|
|
119
|
+
started.agent,
|
|
120
|
+
message,
|
|
121
|
+
blocking,
|
|
122
|
+
signal,
|
|
123
|
+
onUpdate,
|
|
124
|
+
async () => {
|
|
125
|
+
promptSubmissionStarted = true;
|
|
126
|
+
const receipt = await runtime.client.sendMessage(
|
|
127
|
+
started.agent,
|
|
128
|
+
attributedMessage,
|
|
129
|
+
);
|
|
130
|
+
promptAccepted = true;
|
|
131
|
+
return receipt;
|
|
132
|
+
},
|
|
133
|
+
{ expectUserMessage: true },
|
|
134
|
+
);
|
|
135
|
+
} catch (error) {
|
|
136
|
+
if (
|
|
137
|
+
!promptAccepted &&
|
|
138
|
+
(!promptSubmissionStarted || isDispatchRejected(error))
|
|
139
|
+
) {
|
|
140
|
+
return rollbackStartedAgent(runtime.client, started, error);
|
|
141
|
+
}
|
|
142
|
+
throw error;
|
|
143
|
+
}
|
|
144
|
+
return toolResult(
|
|
145
|
+
dispatch.command
|
|
146
|
+
? {
|
|
147
|
+
spawned: true,
|
|
148
|
+
commandSubmitted: true,
|
|
149
|
+
machine: runtime.machine,
|
|
150
|
+
target: started.id,
|
|
151
|
+
name,
|
|
152
|
+
}
|
|
153
|
+
: dispatch.settlement
|
|
154
|
+
? {
|
|
155
|
+
spawned: true,
|
|
156
|
+
...settlementResult(runtime.machine, dispatch.settlement),
|
|
157
|
+
}
|
|
158
|
+
: {
|
|
159
|
+
spawned: true,
|
|
160
|
+
machine: runtime.machine,
|
|
161
|
+
target: started.id,
|
|
162
|
+
name,
|
|
163
|
+
status: "working",
|
|
164
|
+
next: "Completion or blockage will be delivered automatically; do not poll",
|
|
165
|
+
},
|
|
166
|
+
dispatch.warning,
|
|
167
|
+
);
|
|
168
|
+
}
|