@howaboua/pi-shepherdr 0.2.13 → 0.2.14

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.
@@ -14,10 +14,11 @@ import type {
14
14
  SessionView,
15
15
  } from "./types.js";
16
16
 
17
- const BRIDGE_VERSION = 13;
17
+ const BRIDGE_VERSION = 14;
18
18
  const REMOTE_HELPER = "~/.pi/agent/shepherdr.mjs";
19
19
  const REMOTE_PEER_HELPER = "~/.pi/agent/shepherdr-peer.mjs";
20
20
  const REMOTE_SESSION_HELPER = "~/.pi/agent/shepherdr-session.mjs";
21
+ const REMOTE_SESSION_WATCH_HELPER = "~/.pi/agent/shepherdr-session-watch.mjs";
21
22
  const REMOTE_CHECKPOINT_HELPER = "~/.pi/agent/shepherdr-checkpoints.mjs";
22
23
  const REMOTE_CONTEXT_HELPER = "~/.pi/agent/shepherdr-context.mjs";
23
24
  const MAX_FRAME_BYTES = 8 * 1024 * 1024;
@@ -202,34 +203,44 @@ export class RemoteHerdrClient implements HerdrConnection, AssistantReader {
202
203
  config: SshMachine,
203
204
  onClose: (error: Error) => void,
204
205
  ): Promise<RemoteHerdrClient> {
205
- const [source, peerSource, sessionSource, contextSource, checkpointSource] =
206
- await Promise.all([
207
- readFile(
208
- fileURLToPath(new URL("./remote/shepherdr.mjs", import.meta.url)),
206
+ const [
207
+ source,
208
+ peerSource,
209
+ sessionSource,
210
+ contextSource,
211
+ checkpointSource,
212
+ sessionWatchSource,
213
+ ] = await Promise.all([
214
+ readFile(
215
+ fileURLToPath(new URL("./remote/shepherdr.mjs", import.meta.url)),
216
+ ),
217
+ readFile(
218
+ fileURLToPath(new URL("./remote/shepherdr-peer.mjs", import.meta.url)),
219
+ ),
220
+ readFile(
221
+ fileURLToPath(
222
+ new URL("./remote/shepherdr-session.mjs", import.meta.url),
209
223
  ),
210
- readFile(
211
- fileURLToPath(
212
- new URL("./remote/shepherdr-peer.mjs", import.meta.url),
213
- ),
224
+ ),
225
+ readFile(
226
+ fileURLToPath(
227
+ new URL("./remote/shepherdr-context.mjs", import.meta.url),
214
228
  ),
215
- readFile(
216
- fileURLToPath(
217
- new URL("./remote/shepherdr-session.mjs", import.meta.url),
218
- ),
229
+ ),
230
+ readFile(
231
+ fileURLToPath(
232
+ new URL("./remote/shepherdr-checkpoints.mjs", import.meta.url),
219
233
  ),
220
- readFile(
221
- fileURLToPath(
222
- new URL("./remote/shepherdr-context.mjs", import.meta.url),
223
- ),
234
+ ),
235
+ readFile(
236
+ fileURLToPath(
237
+ new URL("./remote/shepherdr-session-watch.mjs", import.meta.url),
224
238
  ),
225
- readFile(
226
- fileURLToPath(
227
- new URL("./remote/shepherdr-checkpoints.mjs", import.meta.url),
228
- ),
229
- ),
230
- ]);
239
+ ),
240
+ ]);
231
241
  await deploy(config, peerSource, REMOTE_PEER_HELPER);
232
242
  await deploy(config, sessionSource, REMOTE_SESSION_HELPER);
243
+ await deploy(config, sessionWatchSource, REMOTE_SESSION_WATCH_HELPER);
233
244
  await deploy(config, checkpointSource, REMOTE_CHECKPOINT_HELPER);
234
245
  await deploy(config, contextSource, REMOTE_CONTEXT_HELPER);
235
246
  await deploy(config, source, REMOTE_HELPER);
@@ -306,6 +317,37 @@ export class RemoteHerdrClient implements HerdrConnection, AssistantReader {
306
317
  onEvent: (event: HerdrEvent) => void,
307
318
  onDisconnect: (error?: Error) => void,
308
319
  signal?: AbortSignal,
320
+ ): Promise<() => void> {
321
+ return this.subscribeTo(
322
+ { op: "subscribe", subscriptions },
323
+ onEvent,
324
+ onDisconnect,
325
+ signal,
326
+ );
327
+ }
328
+
329
+ async watch(
330
+ paths: string[],
331
+ onChange: (path: string) => void,
332
+ onError: (error: Error) => void,
333
+ signal: AbortSignal,
334
+ ): Promise<() => void> {
335
+ return this.subscribeTo(
336
+ { op: "session_subscribe", paths },
337
+ (event) => {
338
+ if (typeof event.data["path"] === "string")
339
+ onChange(event.data["path"]);
340
+ },
341
+ (error) => onError(error ?? new Error("Session activity stream closed")),
342
+ signal,
343
+ );
344
+ }
345
+
346
+ private async subscribeTo(
347
+ request: Record<string, unknown>,
348
+ onEvent: (event: HerdrEvent) => void,
349
+ onDisconnect: (error?: Error) => void,
350
+ signal?: AbortSignal,
309
351
  ): Promise<() => void> {
310
352
  signal?.throwIfAborted();
311
353
  const id = randomUUID();
@@ -318,7 +360,7 @@ export class RemoteHerdrClient implements HerdrConnection, AssistantReader {
318
360
  );
319
361
  };
320
362
  try {
321
- const ready = this.call({ id, op: "subscribe", subscriptions }, 11_000);
363
+ const ready = this.call({ ...request, id }, 11_000);
322
364
  signal?.addEventListener("abort", unsubscribe, { once: true });
323
365
  if (signal?.aborted) unsubscribe();
324
366
  await ready;
@@ -1,4 +1,5 @@
1
1
  import { open, stat } from "node:fs/promises";
2
+ import { watchSessions } from "./remote/shepherdr-session-watch.mjs";
2
3
  import type {
3
4
  AskResult,
4
5
  LatestAssistant,
@@ -118,6 +119,7 @@ function askFromInput(
118
119
  return {
119
120
  toolCallId,
120
121
  handoff: args["handoff"] === true,
122
+ ...(args["delivery"] === "steer" ? { delivery: "steer" as const } : {}),
121
123
  prompts,
122
124
  };
123
125
  }
@@ -143,6 +145,7 @@ function askResult(
143
145
  if (message["isError"] === true) return [id, { status: "rejected" }];
144
146
  if (message["isError"] !== false) return [id, { status: "unknown" }];
145
147
  const details = record(message["details"]);
148
+ if (details?.["pending"] === true) return undefined;
146
149
  if (details?.["dismissed"] === true) return [id, { status: "rejected" }];
147
150
  const rawResponses = details?.["responses"];
148
151
  const rawResponseCount = Array.isArray(rawResponses)
@@ -175,7 +178,12 @@ function askResult(
175
178
  return [
176
179
  id,
177
180
  {
178
- status: "accepted",
181
+ status:
182
+ responses &&
183
+ responses.length > 0 &&
184
+ responses.length === rawResponseCount
185
+ ? "accepted"
186
+ : "unknown",
179
187
  ...(responses && responses.length === rawResponseCount
180
188
  ? { responses }
181
189
  : {}),
@@ -236,6 +244,27 @@ async function readSessionView(
236
244
  }
237
245
  }
238
246
  }
247
+ if (
248
+ entry["type"] === "custom" &&
249
+ entry["customType"] === "pi-ask-pending"
250
+ ) {
251
+ const update = record(entry["data"]);
252
+ if (
253
+ update?.["version"] === 1 &&
254
+ update["state"] === "closed" &&
255
+ typeof update["id"] === "string"
256
+ ) {
257
+ resolved.add(update["id"]);
258
+ const result = askResult({
259
+ role: "toolResult",
260
+ toolName: "ask",
261
+ toolCallId: update["id"],
262
+ isError: false,
263
+ details: update,
264
+ });
265
+ if (result && !askResults.has(result[0])) askResults.set(...result);
266
+ }
267
+ }
239
268
  if (
240
269
  !input &&
241
270
  entry["type"] === "custom_message" &&
@@ -251,7 +280,8 @@ async function readSessionView(
251
280
  const result = askResult(message);
252
281
  if (result && !askResults.has(result[0])) askResults.set(...result);
253
282
  const resolvedId = resolvedToolCallId(message);
254
- if (resolvedId) resolved.add(resolvedId);
283
+ if (resolvedId && record(message["details"])?.["pending"] !== true)
284
+ resolved.add(resolvedId);
255
285
  if (!assistant) {
256
286
  assistant = assistantFromMessage(id, message);
257
287
  if (assistant) assistantDepth = currentDepth;
@@ -270,7 +300,9 @@ async function readSessionView(
270
300
  .map(askCall)
271
301
  .find(
272
302
  (candidate) =>
273
- candidate !== undefined && !resolved.has(candidate.toolCallId),
303
+ candidate !== undefined &&
304
+ candidate.delivery !== "steer" &&
305
+ !resolved.has(candidate.toolCallId),
274
306
  );
275
307
  }
276
308
  }
@@ -307,6 +339,14 @@ async function readSessionView(
307
339
  }
308
340
 
309
341
  export class SessionReader {
342
+ async watch(
343
+ paths: string[],
344
+ onChange: (path: string) => void,
345
+ onError: (error: Error) => void,
346
+ signal: AbortSignal,
347
+ ): Promise<() => void> {
348
+ return watchSessions(paths, onChange, onError, signal);
349
+ }
310
350
  private readonly cache = new Map<
311
351
  string,
312
352
  { mtimeMs: number; result: SessionView; size: number }
@@ -340,6 +380,12 @@ export class SessionReader {
340
380
  }
341
381
 
342
382
  export interface AssistantReader {
383
+ watch(
384
+ paths: string[],
385
+ onChange: (path: string) => void,
386
+ onError: (error: Error) => void,
387
+ signal: AbortSignal,
388
+ ): Promise<() => void>;
343
389
  latest(path?: string): Promise<LatestAssistant | undefined>;
344
390
  view(path?: string): Promise<SessionView>;
345
391
  }
package/src/types.ts CHANGED
@@ -78,6 +78,7 @@ export interface MonitoredAgent {
78
78
  readonly activity: AgentActivity;
79
79
  readonly cwd?: string;
80
80
  readonly lastAssistantId?: string;
81
+ readonly lastQuestionId?: string;
81
82
  readonly name?: string;
82
83
  readonly paneId: string;
83
84
  readonly tabId: string;
@@ -119,6 +120,7 @@ export interface LatestInput {
119
120
  }
120
121
 
121
122
  export interface PendingAsk {
123
+ delivery?: "steer";
122
124
  handoff: boolean;
123
125
  prompts: Array<{
124
126
  body?: string;