pi-agent-swarm 0.3.0 → 0.4.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.
@@ -7,7 +7,7 @@ import { SwarmController, type SwarmControllerDependencies } from "./swarm-contr
7
7
  import { safeTerminalLine } from "./text.js";
8
8
  import { registerSwarmTools } from "./tools.js";
9
9
 
10
- const USAGE = "Usage: /swarm, /swarm start, or /swarm <piagentswarm:v1:invite>";
10
+ const USAGE = "Usage: /swarm, /swarm start, /swarm requests <on|off>, or /swarm <piagentswarm:v1:invite>";
11
11
 
12
12
  type SwarmMenuModule = Pick<typeof import("./menu.js"), "showSwarmMenu">;
13
13
 
@@ -35,6 +35,9 @@ export function createPiSwarmExtension(dependencies: PiSwarmDependencies = {}):
35
35
  pi.on("turn_end", async (_event, ctx) => {
36
36
  controller.noteTurnEnd(ctx);
37
37
  });
38
+ pi.on("agent_settled", async (_event, ctx) => {
39
+ await controller.agentSettled(ctx);
40
+ });
38
41
  pi.on("session_info_changed", async (event, ctx) => {
39
42
  await controller.noteSessionInfoChanged(event.name, ctx);
40
43
  });
@@ -44,7 +47,16 @@ export function createPiSwarmExtension(dependencies: PiSwarmDependencies = {}):
44
47
  });
45
48
 
46
49
  pi.registerCommand("swarm", {
47
- description: "Spawn a worker Pi session, or start/join a local group",
50
+ description: "Spawn a worker, manage a local group, or allow follow-up requests",
51
+ getArgumentCompletions(prefix) {
52
+ const normalized = prefix.trimStart();
53
+ const separator = normalized.search(/\s/u);
54
+ if (separator < 0) return argumentCompletions(["start", "requests"], normalized);
55
+ if (normalized.slice(0, separator) !== "requests") return null;
56
+ const value = normalized.slice(separator).trimStart();
57
+ if (/\s/u.test(value)) return null;
58
+ return argumentCompletions(["on", "off"], value);
59
+ },
48
60
  handler: async (rawArgs, ctx) => {
49
61
  const args = rawArgs.trim();
50
62
  if (ctx.mode !== "tui" && ctx.mode !== "rpc") {
@@ -54,6 +66,10 @@ export function createPiSwarmExtension(dependencies: PiSwarmDependencies = {}):
54
66
  await startGroupDirectly(controller, ctx);
55
67
  return;
56
68
  }
69
+ if (args === "requests on" || args === "requests off") {
70
+ setRequestPolicyDirectly(controller, ctx, args === "requests on");
71
+ return;
72
+ }
57
73
  if (args) {
58
74
  await joinDirectInvite(controller, args, ctx);
59
75
  return;
@@ -128,6 +144,19 @@ async function startGroupDirectly(controller: SwarmController, ctx: ExtensionCom
128
144
  }
129
145
  }
130
146
 
147
+ function argumentCompletions(values: readonly string[], prefix: string) {
148
+ const matches = values.filter((value) => value.startsWith(prefix)).map((value) => ({ value, label: value }));
149
+ return matches.length > 0 ? matches : null;
150
+ }
151
+
152
+ function setRequestPolicyDirectly(controller: SwarmController, ctx: ExtensionCommandContext, value: boolean): void {
153
+ if (controller.sessionSignal.aborted) return;
154
+ controller.setAcceptsRequests(ctx, value);
155
+ if (controller.isCurrent(ctx)) {
156
+ ctx.ui.notify(`Follow-up agent requests ${value ? "enabled" : "blocked"} for this session.`, "info");
157
+ }
158
+ }
159
+
131
160
  function menuSource(controller: SwarmController): SwarmMenuSource {
132
161
  return {
133
162
  snapshot: (signal) => controller.snapshot(signal),
@@ -160,6 +189,9 @@ function menuSource(controller: SwarmController): SwarmMenuSource {
160
189
  commandContext.ui.notify(`Theme set to ${palette}.`, "info");
161
190
  }
162
191
  },
192
+ setRequests: async (commandContext, value) => {
193
+ setRequestPolicyDirectly(controller, commandContext, value);
194
+ },
163
195
  };
164
196
  }
165
197
 
package/src/protocol.ts CHANGED
@@ -18,6 +18,7 @@ const ENDPOINT_ID = /^[a-f0-9]{24}$/u;
18
18
  const BASE64URL_32_BYTES = /^[A-Za-z0-9_-]{43}$/u;
19
19
  const BASE64URL_MAC = /^[A-Za-z0-9_-]{43}$/u;
20
20
  const ACK_CODES = new Set<SwarmAckCode>([
21
+ "context_handoff",
21
22
  "delivery_failed",
22
23
  "kickoff_consumed",
23
24
  "launch_mismatch",
@@ -36,9 +37,9 @@ export type SwarmMessageMode = "notify" | "request" | "reply" | "kickoff";
36
37
 
37
38
  /**
38
39
  * Routes a message: `undefined` or `"steer"` enter the model context, while
39
- * `"lead"` and `"shutdown"` stay on the control plane and never reach the model.
40
+ * `"lead"`, `"shutdown"`, and `"handoff"` stay on the control plane and never reach the model.
40
41
  */
41
- export type SwarmMessageKind = "lead" | "steer" | "shutdown";
42
+ export type SwarmMessageKind = "lead" | "steer" | "shutdown" | "handoff";
42
43
 
43
44
  export interface SwarmMessage {
44
45
  id: string;
@@ -72,6 +73,7 @@ export type SwarmLocalPeerDescription = Omit<SwarmPeerDescription, "endpointId">
72
73
  export type SwarmAckStatus = "accepted" | "duplicate" | "rejected" | "busy";
73
74
 
74
75
  export type SwarmAckCode =
76
+ | "context_handoff"
75
77
  | "delivery_failed"
76
78
  | "kickoff_consumed"
77
79
  | "launch_mismatch"
@@ -277,9 +279,18 @@ export function validateMessage(value: unknown): SwarmMessage {
277
279
  }
278
280
  const replyTo = optionalId(value.replyTo, "reply message id");
279
281
  const launchId = optionalId(value.launchId, "launch id");
280
- if (value.kind !== undefined && value.kind !== "lead" && value.kind !== "steer" && value.kind !== "shutdown") {
282
+ if (
283
+ value.kind !== undefined &&
284
+ value.kind !== "lead" &&
285
+ value.kind !== "steer" &&
286
+ value.kind !== "shutdown" &&
287
+ value.kind !== "handoff"
288
+ ) {
281
289
  throw new Error("Pi Agent Swarm message kind is invalid");
282
290
  }
291
+ if (value.kind === "handoff" && mode !== "notify") {
292
+ throw new Error("Pi Agent Swarm handoff must be a notify control message");
293
+ }
283
294
  if (mode === "reply" && !replyTo) throw new Error("Pi Agent Swarm reply requires a reply message id");
284
295
  if (mode === "kickoff" && !launchId) throw new Error("Pi Agent Swarm kickoff requires a launch id");
285
296
  return {
@@ -1,3 +1,5 @@
1
+ import type { ContextRecyclingLaunch } from "./context-recycling.js";
2
+
1
3
  const RELOAD_HANDOFFS = Symbol.for("pi-agent-swarm/reload-handoffs");
2
4
 
3
5
  export interface SwarmReloadHandoff {
@@ -9,6 +11,7 @@ export interface SwarmReloadHandoff {
9
11
  name?: string;
10
12
  color?: string;
11
13
  parentSessionId?: string;
14
+ contextRecycling?: ContextRecyclingLaunch;
12
15
  expiresAt: number;
13
16
  }
14
17