@howaboua/pi-shepherdr 0.2.16 → 0.2.18
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 +14 -0
- package/README.md +39 -7
- package/index.ts +1 -1
- package/package.json +3 -3
- package/src/agent-identity.ts +17 -0
- package/src/agents-contract.ts +38 -28
- package/src/agents-discovery.ts +9 -6
- package/src/agents-reply.ts +68 -0
- package/src/agents-spawn.ts +1 -10
- package/src/agents-tool.ts +23 -44
- package/src/agents-work.ts +22 -18
- package/src/controller.ts +1 -1
- package/src/extension-reply.ts +35 -0
- package/src/herdr-client.ts +29 -1
- package/src/messages.ts +82 -129
- package/src/{monitor-questions.ts → monitor-extension-messages.ts} +37 -111
- package/src/monitor-record.ts +19 -6
- package/src/monitor-state.ts +71 -25
- package/src/monitor.ts +20 -24
- package/src/peer-commands.ts +29 -15
- package/src/peer-inbox.ts +54 -1
- package/src/remote/shepherdr-channel.mjs +0 -1
- package/src/remote/shepherdr-peer.mjs +101 -18
- package/src/remote/shepherdr-session.mjs +106 -190
- package/src/remote/shepherdr.mjs +12 -2
- package/src/remote-client.ts +22 -1
- package/src/session-reader.ts +3 -364
- package/src/settlement.ts +41 -23
- package/src/types.ts +20 -16
- package/src/agents-answer.ts +0 -130
- package/src/ask-answer.ts +0 -377
|
@@ -1,19 +1,13 @@
|
|
|
1
1
|
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
2
|
-
import { sameAgentIdentity } from "./
|
|
2
|
+
import { sameAgentIdentity } from "./agent-identity.js";
|
|
3
3
|
import { getAgent, sessionPath } from "./herdr.js";
|
|
4
4
|
import { type HerdrConnection, isHerdrErrorCode } from "./herdr-client.js";
|
|
5
5
|
import type { MonitorState } from "./monitor-state.js";
|
|
6
|
+
import { extensionMessageKey } from "./remote/shepherdr-session.mjs";
|
|
6
7
|
import type { AssistantReader } from "./session-reader.js";
|
|
7
|
-
import type {
|
|
8
|
-
AskResult,
|
|
9
|
-
MonitoredAgent,
|
|
10
|
-
PaneInfo,
|
|
11
|
-
PendingAsk,
|
|
12
|
-
} from "./types.js";
|
|
8
|
+
import type { ExtensionMessage, MonitoredAgent, PaneInfo } from "./types.js";
|
|
13
9
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
interface MonitorQuestionsOptions {
|
|
10
|
+
interface MonitorExtensionMessagesOptions {
|
|
17
11
|
client: HerdrConnection;
|
|
18
12
|
reader: AssistantReader;
|
|
19
13
|
state: MonitorState;
|
|
@@ -21,13 +15,13 @@ interface MonitorQuestionsOptions {
|
|
|
21
15
|
ctx: ExtensionContext,
|
|
22
16
|
record: MonitoredAgent,
|
|
23
17
|
panel: PaneInfo,
|
|
24
|
-
|
|
18
|
+
message: ExtensionMessage,
|
|
25
19
|
) => void;
|
|
26
20
|
persist: () => void;
|
|
27
21
|
}
|
|
28
22
|
|
|
29
|
-
//
|
|
30
|
-
export class
|
|
23
|
+
// Extension messages are attention events, not settlements of delegated work.
|
|
24
|
+
export class MonitorExtensionMessages {
|
|
31
25
|
private context: ExtensionContext | undefined;
|
|
32
26
|
private generation = 0;
|
|
33
27
|
private panels: PaneInfo[] = [];
|
|
@@ -36,11 +30,11 @@ export class MonitorQuestions {
|
|
|
36
30
|
private unsubscribe: (() => void) | undefined;
|
|
37
31
|
private refreshTail = Promise.resolve();
|
|
38
32
|
private readonly checks = new Map<string, { dirty: boolean }>();
|
|
39
|
-
|
|
33
|
+
|
|
40
34
|
private warned = false;
|
|
41
|
-
private readonly options:
|
|
35
|
+
private readonly options: MonitorExtensionMessagesOptions;
|
|
42
36
|
|
|
43
|
-
constructor(options:
|
|
37
|
+
constructor(options: MonitorExtensionMessagesOptions) {
|
|
44
38
|
this.options = options;
|
|
45
39
|
}
|
|
46
40
|
|
|
@@ -51,7 +45,6 @@ export class MonitorQuestions {
|
|
|
51
45
|
|
|
52
46
|
stop(): void {
|
|
53
47
|
this.generation++;
|
|
54
|
-
for (const controller of this.answers) controller.abort();
|
|
55
48
|
this.context = undefined;
|
|
56
49
|
this.panels = [];
|
|
57
50
|
this.key = "";
|
|
@@ -64,86 +57,6 @@ export class MonitorQuestions {
|
|
|
64
57
|
this.refreshTail = Promise.resolve();
|
|
65
58
|
}
|
|
66
59
|
|
|
67
|
-
submitAnswer(
|
|
68
|
-
panel: PaneInfo,
|
|
69
|
-
askId: string,
|
|
70
|
-
submit: (signal: AbortSignal) => Promise<void>,
|
|
71
|
-
signal: AbortSignal,
|
|
72
|
-
): Promise<AskResult | undefined> {
|
|
73
|
-
signal.throwIfAborted();
|
|
74
|
-
const path = sessionPath(panel);
|
|
75
|
-
if (!path) return Promise.resolve(undefined);
|
|
76
|
-
const controller = new AbortController();
|
|
77
|
-
this.answers.add(controller);
|
|
78
|
-
return new Promise((resolve, reject) => {
|
|
79
|
-
let closed = false;
|
|
80
|
-
let unsubscribe: (() => void) | undefined;
|
|
81
|
-
let submitted = false;
|
|
82
|
-
let checking = false;
|
|
83
|
-
let dirty = false;
|
|
84
|
-
const finish = (result?: AskResult, error?: unknown) => {
|
|
85
|
-
if (closed) return;
|
|
86
|
-
closed = true;
|
|
87
|
-
clearTimeout(timer);
|
|
88
|
-
signal.removeEventListener("abort", cancel);
|
|
89
|
-
controller.signal.removeEventListener("abort", stopped);
|
|
90
|
-
controller.abort();
|
|
91
|
-
unsubscribe?.();
|
|
92
|
-
this.answers.delete(controller);
|
|
93
|
-
if (error !== undefined) reject(error);
|
|
94
|
-
else resolve(result);
|
|
95
|
-
};
|
|
96
|
-
const cancel = () => finish(undefined, signal.reason);
|
|
97
|
-
const stopped = () => finish(undefined, controller.signal.reason);
|
|
98
|
-
const timer = setTimeout(() => finish(), ASK_RESULT_TIMEOUT_MS);
|
|
99
|
-
signal.addEventListener("abort", cancel, { once: true });
|
|
100
|
-
controller.signal.addEventListener("abort", stopped, { once: true });
|
|
101
|
-
const check = () => {
|
|
102
|
-
dirty = true;
|
|
103
|
-
if (closed || !submitted || checking) return;
|
|
104
|
-
checking = true;
|
|
105
|
-
void (async () => {
|
|
106
|
-
while (dirty && !closed) {
|
|
107
|
-
dirty = false;
|
|
108
|
-
const current = await getAgent(this.options.client, panel.pane_id);
|
|
109
|
-
if (closed) return;
|
|
110
|
-
if (!sameAgentIdentity(panel, current)) return finish();
|
|
111
|
-
const view = await this.options.reader.view(path);
|
|
112
|
-
if (closed) return;
|
|
113
|
-
// Identity may change while the session read is in flight.
|
|
114
|
-
const after = await getAgent(this.options.client, panel.pane_id);
|
|
115
|
-
if (closed) return;
|
|
116
|
-
if (!sameAgentIdentity(panel, after)) return finish();
|
|
117
|
-
// Ask can leave the UI before its close record is persisted.
|
|
118
|
-
const result = view.askResults?.[askId];
|
|
119
|
-
if (result) return finish(result);
|
|
120
|
-
}
|
|
121
|
-
})()
|
|
122
|
-
.catch((error) => finish(undefined, error))
|
|
123
|
-
.finally(() => {
|
|
124
|
-
checking = false;
|
|
125
|
-
if (dirty && !closed) check();
|
|
126
|
-
});
|
|
127
|
-
};
|
|
128
|
-
void (async () => {
|
|
129
|
-
// The reader belongs to this machine. Cover persistence before Enter.
|
|
130
|
-
const close = await this.options.reader.watch(
|
|
131
|
-
[path],
|
|
132
|
-
check,
|
|
133
|
-
(error) => finish(undefined, error),
|
|
134
|
-
controller.signal,
|
|
135
|
-
);
|
|
136
|
-
if (closed) return close();
|
|
137
|
-
unsubscribe = close;
|
|
138
|
-
await submit(controller.signal);
|
|
139
|
-
if (closed) return;
|
|
140
|
-
submitted = true;
|
|
141
|
-
// Read after subscription and submit ACK even if no event was observed.
|
|
142
|
-
check();
|
|
143
|
-
})().catch((error) => finish(undefined, error));
|
|
144
|
-
});
|
|
145
|
-
}
|
|
146
|
-
|
|
147
60
|
refresh(panels: PaneInfo[] = this.panels): Promise<void> {
|
|
148
61
|
this.panels = panels.filter(
|
|
149
62
|
(panel) =>
|
|
@@ -222,20 +135,33 @@ export class MonitorQuestions {
|
|
|
222
135
|
if (!sameAgentIdentity(panel, current)) continue;
|
|
223
136
|
const view = await this.options.reader.view(path);
|
|
224
137
|
if (generation !== this.generation || !this.context) return;
|
|
138
|
+
const after = await getAgent(
|
|
139
|
+
this.options.client,
|
|
140
|
+
panel.pane_id,
|
|
141
|
+
).catch((error: unknown) => {
|
|
142
|
+
if (isHerdrErrorCode(error, "agent_not_found")) return undefined;
|
|
143
|
+
throw error;
|
|
144
|
+
});
|
|
145
|
+
if (generation !== this.generation || !this.context) return;
|
|
146
|
+
if (!after || !sameAgentIdentity(panel, after)) continue;
|
|
225
147
|
const record = this.options.state.byTerminal(current.terminal_id);
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
record.
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
)
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
148
|
+
if (!record || record.paneId !== current.pane_id) continue;
|
|
149
|
+
const messages = view.extensionMessages ?? [];
|
|
150
|
+
let changed = this.options.state.retainExtensionNotifications(
|
|
151
|
+
record.terminalId,
|
|
152
|
+
messages.map(extensionMessageKey),
|
|
153
|
+
);
|
|
154
|
+
for (const message of messages) {
|
|
155
|
+
// Blocking messages arrive with the worker's settlement.
|
|
156
|
+
if (message.blocking) continue;
|
|
157
|
+
const key = extensionMessageKey(message);
|
|
158
|
+
if (record.notifiedExtensionMessages?.includes(key)) continue;
|
|
159
|
+
this.options.notify(this.context, record, current, message);
|
|
160
|
+
changed =
|
|
161
|
+
this.options.state.markExtensionMessage(record.terminalId, key) ||
|
|
162
|
+
changed;
|
|
163
|
+
}
|
|
164
|
+
if (changed) this.options.persist();
|
|
239
165
|
}
|
|
240
166
|
}
|
|
241
167
|
})()
|
|
@@ -251,7 +177,7 @@ export class MonitorQuestions {
|
|
|
251
177
|
if (this.warned) return;
|
|
252
178
|
this.warned = true;
|
|
253
179
|
this.context?.ui.notify(
|
|
254
|
-
`
|
|
180
|
+
`Worker extension notifications unavailable: ${error instanceof Error ? error.message : String(error)}. Use agents read to inspect workers; reconnect through /herdr`,
|
|
255
181
|
"warning",
|
|
256
182
|
);
|
|
257
183
|
}
|
package/src/monitor-record.ts
CHANGED
|
@@ -104,20 +104,31 @@ export function parseMonitoredAgent(
|
|
|
104
104
|
}
|
|
105
105
|
const cwd = optionalString(record, "cwd");
|
|
106
106
|
const lastAssistantId = optionalString(record, "lastAssistantId");
|
|
107
|
-
const
|
|
107
|
+
const notifiedExtensionMessages =
|
|
108
|
+
record["notifiedExtensionMessages"] === undefined
|
|
109
|
+
? undefined
|
|
110
|
+
: Array.isArray(record["notifiedExtensionMessages"]) &&
|
|
111
|
+
record["notifiedExtensionMessages"].every(
|
|
112
|
+
(value) => typeof value === "string",
|
|
113
|
+
)
|
|
114
|
+
? (record["notifiedExtensionMessages"] as string[])
|
|
115
|
+
: false;
|
|
108
116
|
const name = optionalString(record, "name");
|
|
117
|
+
const instructions = optionalString(record, "instructions");
|
|
109
118
|
const activity = parsedActivity(record["activity"]);
|
|
110
119
|
if (
|
|
111
120
|
cwd === false ||
|
|
112
121
|
lastAssistantId === false ||
|
|
113
|
-
|
|
122
|
+
notifiedExtensionMessages === false ||
|
|
114
123
|
name === false ||
|
|
124
|
+
instructions === false ||
|
|
115
125
|
!activity
|
|
116
126
|
) {
|
|
117
127
|
return undefined;
|
|
118
128
|
}
|
|
119
129
|
return {
|
|
120
130
|
scope: record["scope"],
|
|
131
|
+
...(instructions ? { instructions } : {}),
|
|
121
132
|
activity,
|
|
122
133
|
paneId: record["paneId"],
|
|
123
134
|
terminalId: record["terminalId"],
|
|
@@ -125,7 +136,7 @@ export function parseMonitoredAgent(
|
|
|
125
136
|
tabId: record["tabId"],
|
|
126
137
|
...(cwd ? { cwd } : {}),
|
|
127
138
|
...(lastAssistantId ? { lastAssistantId } : {}),
|
|
128
|
-
...(
|
|
139
|
+
...(notifiedExtensionMessages ? { notifiedExtensionMessages } : {}),
|
|
129
140
|
...(name ? { name } : {}),
|
|
130
141
|
};
|
|
131
142
|
}
|
|
@@ -135,7 +146,7 @@ export function recordForPanel(
|
|
|
135
146
|
activity: AgentActivity,
|
|
136
147
|
scope: MonitoredAgent["scope"],
|
|
137
148
|
lastAssistantId?: string,
|
|
138
|
-
|
|
149
|
+
notifiedExtensionMessages?: string[],
|
|
139
150
|
): MonitoredAgent {
|
|
140
151
|
const cwd = panel.foreground_cwd ?? panel.cwd;
|
|
141
152
|
return {
|
|
@@ -148,7 +159,7 @@ export function recordForPanel(
|
|
|
148
159
|
...(panel.name ? { name: panel.name } : {}),
|
|
149
160
|
...(cwd ? { cwd } : {}),
|
|
150
161
|
...(lastAssistantId ? { lastAssistantId } : {}),
|
|
151
|
-
...(
|
|
162
|
+
...(notifiedExtensionMessages ? { notifiedExtensionMessages } : {}),
|
|
152
163
|
};
|
|
153
164
|
}
|
|
154
165
|
|
|
@@ -158,6 +169,7 @@ export function sameMonitorRecord(
|
|
|
158
169
|
): boolean {
|
|
159
170
|
return (
|
|
160
171
|
left.scope === right.scope &&
|
|
172
|
+
left.instructions === right.instructions &&
|
|
161
173
|
left.paneId === right.paneId &&
|
|
162
174
|
left.terminalId === right.terminalId &&
|
|
163
175
|
left.workspaceId === right.workspaceId &&
|
|
@@ -165,7 +177,8 @@ export function sameMonitorRecord(
|
|
|
165
177
|
left.cwd === right.cwd &&
|
|
166
178
|
left.name === right.name &&
|
|
167
179
|
left.lastAssistantId === right.lastAssistantId &&
|
|
168
|
-
left.
|
|
180
|
+
JSON.stringify(left.notifiedExtensionMessages) ===
|
|
181
|
+
JSON.stringify(right.notifiedExtensionMessages) &&
|
|
169
182
|
sameAgentActivity(left.activity, right.activity)
|
|
170
183
|
);
|
|
171
184
|
}
|
package/src/monitor-state.ts
CHANGED
|
@@ -81,10 +81,30 @@ export class MonitorState {
|
|
|
81
81
|
return this.agents.get(terminalId);
|
|
82
82
|
}
|
|
83
83
|
|
|
84
|
-
|
|
84
|
+
markExtensionMessage(terminalId: string, id: string): boolean {
|
|
85
85
|
const record = this.byTerminal(terminalId);
|
|
86
|
-
if (!record || record.
|
|
87
|
-
this.agents.set(terminalId, {
|
|
86
|
+
if (!record || record.notifiedExtensionMessages?.includes(id)) return false;
|
|
87
|
+
this.agents.set(terminalId, {
|
|
88
|
+
...record,
|
|
89
|
+
notifiedExtensionMessages: [
|
|
90
|
+
...(record.notifiedExtensionMessages ?? []),
|
|
91
|
+
id,
|
|
92
|
+
],
|
|
93
|
+
});
|
|
94
|
+
return true;
|
|
95
|
+
}
|
|
96
|
+
retainExtensionNotifications(terminalId: string, live: string[]): boolean {
|
|
97
|
+
const record = this.byTerminal(terminalId);
|
|
98
|
+
if (!record?.notifiedExtensionMessages) return false;
|
|
99
|
+
const retained = record.notifiedExtensionMessages.filter((id) =>
|
|
100
|
+
live.includes(id),
|
|
101
|
+
);
|
|
102
|
+
if (retained.length === record.notifiedExtensionMessages.length)
|
|
103
|
+
return false;
|
|
104
|
+
this.agents.set(terminalId, {
|
|
105
|
+
...record,
|
|
106
|
+
notifiedExtensionMessages: retained,
|
|
107
|
+
});
|
|
88
108
|
return true;
|
|
89
109
|
}
|
|
90
110
|
|
|
@@ -102,15 +122,20 @@ export class MonitorState {
|
|
|
102
122
|
const activity = reportCurrent
|
|
103
123
|
? ({ attemptId: randomUUID(), phase: "working" } as const)
|
|
104
124
|
: (existing?.activity ?? activityForPanel(panel));
|
|
105
|
-
const record =
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
125
|
+
const record = {
|
|
126
|
+
...(existing?.instructions
|
|
127
|
+
? { instructions: existing.instructions }
|
|
128
|
+
: {}),
|
|
129
|
+
...recordForPanel(
|
|
130
|
+
panel,
|
|
131
|
+
activity,
|
|
132
|
+
reportSettled ? "persistent" : (existing?.scope ?? "task"),
|
|
133
|
+
reportCurrent
|
|
134
|
+
? undefined
|
|
135
|
+
: (existing?.lastAssistantId ?? lastAssistantId),
|
|
136
|
+
existing?.notifiedExtensionMessages,
|
|
137
|
+
),
|
|
138
|
+
};
|
|
114
139
|
if (existing && existing.terminalId !== record.terminalId) {
|
|
115
140
|
this.agents.delete(existing.terminalId);
|
|
116
141
|
}
|
|
@@ -136,11 +161,11 @@ export class MonitorState {
|
|
|
136
161
|
`prompt submission to ${record.name ?? paneId} is still unresolved`,
|
|
137
162
|
);
|
|
138
163
|
}
|
|
139
|
-
if (record.activity.phase === "working" && record.activity.task) {
|
|
140
|
-
throw new Error("Agent already has delegated work; use send for updates");
|
|
141
|
-
}
|
|
142
164
|
const attempt = {
|
|
143
|
-
attemptId:
|
|
165
|
+
attemptId:
|
|
166
|
+
record.activity.phase === "working"
|
|
167
|
+
? (record.activity.attemptId ?? randomUUID())
|
|
168
|
+
: randomUUID(),
|
|
144
169
|
terminalId: record.terminalId,
|
|
145
170
|
};
|
|
146
171
|
this.pendingSubmissions.add(attempt.attemptId);
|
|
@@ -150,13 +175,25 @@ export class MonitorState {
|
|
|
150
175
|
attemptId: attempt.attemptId,
|
|
151
176
|
phase: "submitting",
|
|
152
177
|
previous: record.activity,
|
|
153
|
-
task,
|
|
178
|
+
task: activityTask(record.activity) ?? task,
|
|
154
179
|
...(expectedUserAfter !== undefined ? { expectedUserAfter } : {}),
|
|
155
180
|
},
|
|
156
181
|
});
|
|
157
182
|
return attempt;
|
|
158
183
|
}
|
|
159
184
|
|
|
185
|
+
noteMessage(paneId: string, message: string): boolean {
|
|
186
|
+
const record = this.byPane(paneId);
|
|
187
|
+
if (!record) return false;
|
|
188
|
+
const prior = record.instructions ?? activityTask(record.activity);
|
|
189
|
+
this.agents.set(record.terminalId, {
|
|
190
|
+
...record,
|
|
191
|
+
instructions:
|
|
192
|
+
prior && prior !== message ? `${prior}\n\n${message}` : message,
|
|
193
|
+
});
|
|
194
|
+
return true;
|
|
195
|
+
}
|
|
196
|
+
|
|
160
197
|
endSubmission(attempt: WorkAttempt | undefined): void {
|
|
161
198
|
if (attempt) this.pendingSubmissions.delete(attempt.attemptId);
|
|
162
199
|
}
|
|
@@ -351,13 +388,16 @@ export class MonitorState {
|
|
|
351
388
|
activity = { phase: "settled", status: panel.agent_status };
|
|
352
389
|
}
|
|
353
390
|
|
|
354
|
-
const updated =
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
391
|
+
const updated = {
|
|
392
|
+
...(record.instructions ? { instructions: record.instructions } : {}),
|
|
393
|
+
...recordForPanel(
|
|
394
|
+
panel,
|
|
395
|
+
activity,
|
|
396
|
+
record.scope,
|
|
397
|
+
record.lastAssistantId,
|
|
398
|
+
record.notifiedExtensionMessages,
|
|
399
|
+
),
|
|
400
|
+
};
|
|
361
401
|
if (!sameMonitorRecord(record, updated)) changed = true;
|
|
362
402
|
if (record.terminalId !== updated.terminalId) {
|
|
363
403
|
this.agents.delete(record.terminalId);
|
|
@@ -390,7 +430,13 @@ export class MonitorState {
|
|
|
390
430
|
lastAssistantId?: string,
|
|
391
431
|
): boolean {
|
|
392
432
|
const record = this.byTerminal(terminalId);
|
|
393
|
-
if (
|
|
433
|
+
if (
|
|
434
|
+
!record ||
|
|
435
|
+
activityTask(record.activity) !== task ||
|
|
436
|
+
(record.activity.phase === "submitting" &&
|
|
437
|
+
this.pendingSubmissions.has(record.activity.attemptId))
|
|
438
|
+
)
|
|
439
|
+
return false;
|
|
394
440
|
this.agents.set(terminalId, {
|
|
395
441
|
...record,
|
|
396
442
|
scope: "persistent",
|
package/src/monitor.ts
CHANGED
|
@@ -8,7 +8,7 @@ import { type HerdrConnection, isDispatchRejected } from "./herdr-client.js";
|
|
|
8
8
|
import { injectAgentEvent } from "./messages.js";
|
|
9
9
|
import { parseMonitorEvent } from "./monitor-event.js";
|
|
10
10
|
import { MonitorEvents } from "./monitor-events.js";
|
|
11
|
-
import {
|
|
11
|
+
import { MonitorExtensionMessages } from "./monitor-extension-messages.js";
|
|
12
12
|
import { MonitorState, type WorkAttempt } from "./monitor-state.js";
|
|
13
13
|
import { type AssistantReader, SessionReader } from "./session-reader.js";
|
|
14
14
|
import {
|
|
@@ -17,6 +17,7 @@ import {
|
|
|
17
17
|
type SettlementRequest,
|
|
18
18
|
} from "./settlement.js";
|
|
19
19
|
import type {
|
|
20
|
+
ExtensionReply,
|
|
20
21
|
HerdrEvent,
|
|
21
22
|
MonitoredAgent,
|
|
22
23
|
MonitoringIssue,
|
|
@@ -47,7 +48,7 @@ export class AgentMonitor {
|
|
|
47
48
|
private readonly state = new MonitorState();
|
|
48
49
|
private readonly events: MonitorEvents;
|
|
49
50
|
private readonly settlements: SettlementReporter;
|
|
50
|
-
private readonly
|
|
51
|
+
private readonly extensionMessages: MonitorExtensionMessages;
|
|
51
52
|
private context: ExtensionContext | undefined;
|
|
52
53
|
private activationGeneration = 0;
|
|
53
54
|
|
|
@@ -58,16 +59,16 @@ export class AgentMonitor {
|
|
|
58
59
|
this.onRefresh = options.onRefresh;
|
|
59
60
|
this.selfPaneId = options.selfPaneId;
|
|
60
61
|
const reader = options.reader ?? new SessionReader();
|
|
61
|
-
this.
|
|
62
|
+
this.extensionMessages = new MonitorExtensionMessages({
|
|
62
63
|
client: this.client,
|
|
63
64
|
reader,
|
|
64
65
|
state: this.state,
|
|
65
|
-
notify: (ctx, record, agent,
|
|
66
|
+
notify: (ctx, record, agent, message) => {
|
|
66
67
|
injectAgentEvent(pi, ctx, {
|
|
67
68
|
agent,
|
|
68
|
-
|
|
69
|
+
extensionMessages: [message],
|
|
69
70
|
record,
|
|
70
|
-
status: "
|
|
71
|
+
status: "extension-message",
|
|
71
72
|
labels: {},
|
|
72
73
|
agentToolName: "agents",
|
|
73
74
|
machine: this.machine,
|
|
@@ -110,7 +111,7 @@ export class AgentMonitor {
|
|
|
110
111
|
this.deactivate();
|
|
111
112
|
const generation = this.activationGeneration;
|
|
112
113
|
this.context = ctx;
|
|
113
|
-
this.
|
|
114
|
+
this.extensionMessages.start(ctx);
|
|
114
115
|
const dropped = this.state.restore(restored, this.selfPaneId);
|
|
115
116
|
if (dropped > 0) {
|
|
116
117
|
ctx.ui.notify(
|
|
@@ -129,7 +130,7 @@ export class AgentMonitor {
|
|
|
129
130
|
this.activationGeneration += 1;
|
|
130
131
|
this.context = undefined;
|
|
131
132
|
this.events.stop();
|
|
132
|
-
this.
|
|
133
|
+
this.extensionMessages.stop();
|
|
133
134
|
this.settlements.stop();
|
|
134
135
|
}
|
|
135
136
|
|
|
@@ -174,7 +175,7 @@ export class AgentMonitor {
|
|
|
174
175
|
new Error(`${paneId} was unwatched before its work settled`),
|
|
175
176
|
);
|
|
176
177
|
this.persist();
|
|
177
|
-
await this.
|
|
178
|
+
await this.extensionMessages.refresh();
|
|
178
179
|
await this.events.refresh();
|
|
179
180
|
return true;
|
|
180
181
|
}
|
|
@@ -192,9 +193,13 @@ export class AgentMonitor {
|
|
|
192
193
|
claimWork(
|
|
193
194
|
attempt: WorkAttempt,
|
|
194
195
|
signal: AbortSignal,
|
|
195
|
-
|
|
196
|
-
): Promise<ClaimedSettlement> {
|
|
197
|
-
return this.settlements.claim(attempt, signal,
|
|
196
|
+
replyingTo?: ExtensionReply,
|
|
197
|
+
): Promise<ClaimedSettlement> | undefined {
|
|
198
|
+
return this.settlements.claim(attempt, signal, replyingTo);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
noteMessage(paneId: string, message: string): void {
|
|
202
|
+
if (this.state.noteMessage(paneId, message)) this.persist();
|
|
198
203
|
}
|
|
199
204
|
|
|
200
205
|
releaseWorkClaim(attempt: WorkAttempt | undefined, error: unknown): void {
|
|
@@ -205,15 +210,6 @@ export class AgentMonitor {
|
|
|
205
210
|
return this.settlements.view(panel);
|
|
206
211
|
}
|
|
207
212
|
|
|
208
|
-
submitAskAnswer(
|
|
209
|
-
panel: PaneInfo,
|
|
210
|
-
askId: string,
|
|
211
|
-
submit: (signal: AbortSignal) => Promise<void>,
|
|
212
|
-
signal: AbortSignal,
|
|
213
|
-
) {
|
|
214
|
-
return this.questions.submitAnswer(panel, askId, submit, signal);
|
|
215
|
-
}
|
|
216
|
-
|
|
217
213
|
acceptWork(attempt: WorkAttempt | undefined): void {
|
|
218
214
|
if (this.state.acceptWork(attempt)) this.persist();
|
|
219
215
|
}
|
|
@@ -267,7 +263,7 @@ export class AgentMonitor {
|
|
|
267
263
|
return;
|
|
268
264
|
}
|
|
269
265
|
if (this.list().length === 0) {
|
|
270
|
-
await this.
|
|
266
|
+
await this.extensionMessages.refresh([]);
|
|
271
267
|
if (persistRestoration) this.persist();
|
|
272
268
|
else this.onRefresh();
|
|
273
269
|
return;
|
|
@@ -287,7 +283,7 @@ export class AgentMonitor {
|
|
|
287
283
|
}
|
|
288
284
|
if (changed || persistRestoration) this.persist();
|
|
289
285
|
else this.onRefresh();
|
|
290
|
-
await this.
|
|
286
|
+
await this.extensionMessages.refresh(snapshot.agents);
|
|
291
287
|
for (const completion of completions) {
|
|
292
288
|
void this.report(completion);
|
|
293
289
|
}
|
|
@@ -327,7 +323,7 @@ export class AgentMonitor {
|
|
|
327
323
|
}
|
|
328
324
|
|
|
329
325
|
private refreshAfterEvent(): void {
|
|
330
|
-
void this.
|
|
326
|
+
void this.extensionMessages.refresh();
|
|
331
327
|
// MonitorEvents reports failures through onWarning before rejecting awaited updates.
|
|
332
328
|
void this.events.refresh().catch(() => undefined);
|
|
333
329
|
}
|
package/src/peer-commands.ts
CHANGED
|
@@ -26,7 +26,31 @@ export function createPeerCommands(pi: ExtensionAPI) {
|
|
|
26
26
|
}
|
|
27
27
|
>();
|
|
28
28
|
pi.on("session_shutdown", () => pending.clear());
|
|
29
|
+
const available = (configured?: Record<string, string>) => {
|
|
30
|
+
let allowed: Record<string, string>;
|
|
31
|
+
try {
|
|
32
|
+
allowed = configured ?? readExtensionCommands();
|
|
33
|
+
} catch {
|
|
34
|
+
throw new Error(
|
|
35
|
+
"Command permissions could not be read; built-in commands remain available. Ask the user to check Shepherdr's Agent commands settings",
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
return Object.entries(builtins)
|
|
39
|
+
.map(([name, description]) => ({ name, description }))
|
|
40
|
+
.concat(
|
|
41
|
+
pi.getCommands().flatMap((entry) => {
|
|
42
|
+
const description = allowed[entry.name];
|
|
43
|
+
return entry.source === "extension" &&
|
|
44
|
+
Object.hasOwn(allowed, entry.name) &&
|
|
45
|
+
description !== undefined &&
|
|
46
|
+
!Object.hasOwn(builtins, entry.name)
|
|
47
|
+
? [{ name: entry.name, description }]
|
|
48
|
+
: [];
|
|
49
|
+
}),
|
|
50
|
+
);
|
|
51
|
+
};
|
|
29
52
|
return {
|
|
53
|
+
available,
|
|
30
54
|
async handle(args: string, ctx: ExtensionCommandContext): Promise<boolean> {
|
|
31
55
|
const intent = pending.get(args);
|
|
32
56
|
if (!intent) return false;
|
|
@@ -73,22 +97,12 @@ export function createPeerCommands(pi: ExtensionAPI) {
|
|
|
73
97
|
!entry ||
|
|
74
98
|
(entry.source === "extension" && !Object.hasOwn(allowed, name))
|
|
75
99
|
) {
|
|
76
|
-
const available = Object.entries(builtins).concat(
|
|
77
|
-
commands
|
|
78
|
-
.filter(
|
|
79
|
-
(entry) =>
|
|
80
|
-
entry.source === "extension" &&
|
|
81
|
-
Object.hasOwn(allowed, entry.name),
|
|
82
|
-
)
|
|
83
|
-
.flatMap((entry): [string, string][] => {
|
|
84
|
-
const description = allowed[entry.name];
|
|
85
|
-
return description === undefined
|
|
86
|
-
? []
|
|
87
|
-
: [[entry.name, description]];
|
|
88
|
-
}),
|
|
89
|
-
);
|
|
90
100
|
throw new Error(
|
|
91
|
-
`Command /${name} is unavailable. Available commands:\n${available
|
|
101
|
+
`Command /${name} is unavailable. Available commands:\n${available(
|
|
102
|
+
allowed,
|
|
103
|
+
)
|
|
104
|
+
.map(({ name, description }) => `/${name}: ${description}`)
|
|
105
|
+
.join("\n")}`,
|
|
92
106
|
);
|
|
93
107
|
}
|
|
94
108
|
return {
|