pi-agent-swarm 0.1.0 → 0.3.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.
- package/LICENSE +1 -0
- package/README.md +28 -11
- package/dist/chunks/chunk-77JHO3GV.ts +144 -0
- package/dist/chunks/chunk-77JHO3GV.ts.map +7 -0
- package/dist/chunks/menu-RH6GY6U4.ts +119 -0
- package/dist/chunks/menu-RH6GY6U4.ts.map +7 -0
- package/dist/index.ts +372 -113
- package/dist/index.ts.map +4 -4
- package/package.json +1 -1
- package/src/auto-name.ts +169 -0
- package/src/color.ts +58 -8
- package/src/footer.ts +63 -0
- package/src/menu.ts +54 -320
- package/src/pi-agent-swarm.ts +24 -39
- package/src/settings.ts +33 -0
- package/src/swarm-controller.ts +209 -19
- package/src/transport.ts +4 -0
- package/dist/chunks/chunk-XD4GFZPB.ts +0 -39
- package/dist/chunks/chunk-XD4GFZPB.ts.map +0 -7
- package/dist/chunks/menu-MZS6N2VS.ts +0 -348
- package/dist/chunks/menu-MZS6N2VS.ts.map +0 -7
package/src/menu.ts
CHANGED
|
@@ -1,186 +1,60 @@
|
|
|
1
1
|
import type { ExtensionCommandContext } from "@earendil-works/pi-coding-agent";
|
|
2
2
|
import type { MenuDefinition } from "@narumitw/pi-tui-kit";
|
|
3
|
-
import
|
|
3
|
+
import { SWARM_COLORS, type SwarmColorPaletteName } from "./color.js";
|
|
4
4
|
import type { SpawnSessionInput, SwarmSnapshot } from "./swarm-controller.js";
|
|
5
|
-
import { safeError
|
|
5
|
+
import { safeError } from "./text.js";
|
|
6
6
|
|
|
7
|
-
export interface SwarmMenuState extends SwarmSnapshot
|
|
8
|
-
settingsPath: string;
|
|
9
|
-
}
|
|
7
|
+
export interface SwarmMenuState extends SwarmSnapshot {}
|
|
10
8
|
|
|
11
9
|
export interface SwarmMenuSource {
|
|
12
10
|
snapshot(signal?: AbortSignal): Promise<SwarmMenuState>;
|
|
13
11
|
spawn(ctx: ExtensionCommandContext, input: SpawnSessionInput, signal?: AbortSignal): Promise<void>;
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
ctx: ExtensionCommandContext,
|
|
18
|
-
options: { targetSessionId: string; text: string; mode: "notify" | "request" },
|
|
19
|
-
signal?: AbortSignal,
|
|
20
|
-
): Promise<void>;
|
|
21
|
-
updateSettings(patch: SwarmSettingsPatch, signal?: AbortSignal): Promise<void>;
|
|
22
|
-
setAcceptsRequests(value: boolean): void;
|
|
23
|
-
leave(): Promise<void>;
|
|
12
|
+
rename(ctx: ExtensionCommandContext, name: string, signal?: AbortSignal): Promise<void>;
|
|
13
|
+
setColor(ctx: ExtensionCommandContext, color: string, signal?: AbortSignal): Promise<void>;
|
|
14
|
+
setPalette(ctx: ExtensionCommandContext, palette: SwarmColorPaletteName, signal?: AbortSignal): Promise<void>;
|
|
24
15
|
}
|
|
25
16
|
|
|
26
|
-
type Screen =
|
|
27
|
-
|
|
28
|
-
| "join"
|
|
29
|
-
| "sessions"
|
|
30
|
-
| "invite"
|
|
31
|
-
| "requestPolicy"
|
|
32
|
-
| "settings"
|
|
33
|
-
| "settingsInvalid"
|
|
34
|
-
| "status"
|
|
35
|
-
| "help"
|
|
36
|
-
| "leave";
|
|
37
|
-
type Action = "spawn" | "start" | "join" | "send" | "setConfirmation" | "setPinToLeadWorkspace" | "setPolicy" | "leave";
|
|
17
|
+
type Screen = "main";
|
|
18
|
+
type Action = "spawn" | "rename" | "setColor" | "setTheme";
|
|
38
19
|
|
|
39
20
|
const DIRECTION_OPTIONS = ["Right", "Down", "Left", "Up"] as const;
|
|
21
|
+
const PALETTE_OPTIONS = [
|
|
22
|
+
{ label: "muted (default, low-saturation GitHub colors)", value: "muted" },
|
|
23
|
+
{ label: "bright (vivid GitHub colors)", value: "bright" },
|
|
24
|
+
] as const satisfies readonly { label: string; value: SwarmColorPaletteName }[];
|
|
40
25
|
|
|
41
26
|
export function createSwarmMenu(source: SwarmMenuSource) {
|
|
42
27
|
const getState = ({ signal }: { signal?: AbortSignal } = {}) => source.snapshot(signal);
|
|
43
28
|
const menu: MenuDefinition<SwarmMenuState, Screen, Action> = {
|
|
44
29
|
start: "main",
|
|
45
30
|
screens: {
|
|
46
|
-
main: (
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
lines: [
|
|
51
|
-
"Paste a local piagentswarm:v1 bearer invite.",
|
|
52
|
-
"Anyone holding it can message this Pi session while both processes are running.",
|
|
53
|
-
],
|
|
54
|
-
placeholder: "piagentswarm:v1:…",
|
|
55
|
-
action: "join",
|
|
56
|
-
hint: "back",
|
|
57
|
-
}),
|
|
58
|
-
sessions: ({ state }) => ({
|
|
59
|
-
kind: "browse",
|
|
60
|
-
title: "Pi Agent Swarm sessions",
|
|
61
|
-
lines: state.peers.length
|
|
62
|
-
? ["Authenticated live sessions in this ephemeral local group."]
|
|
63
|
-
: ["No other live sessions are currently authenticated."],
|
|
64
|
-
items: state.peers.map((peer) => ({
|
|
65
|
-
id: peer.sessionId,
|
|
66
|
-
label: safeTerminalLine(peer.name ?? peer.sessionId),
|
|
67
|
-
statusText: peer.acceptsRequests ? "requests allowed" : "messages only",
|
|
68
|
-
description: safeTerminalLine(peer.cwd),
|
|
69
|
-
details: [
|
|
70
|
-
`Session: ${safeTerminalLine(peer.sessionId)}`,
|
|
71
|
-
`Process: ${peer.pid}`,
|
|
72
|
-
...(peer.launchId ? [`Launch: ${safeTerminalLine(peer.launchId)}`] : []),
|
|
73
|
-
],
|
|
74
|
-
})),
|
|
75
|
-
viewportSize: "adaptive",
|
|
76
|
-
hint: "back",
|
|
77
|
-
}),
|
|
78
|
-
invite: ({ state }) => ({
|
|
79
|
-
kind: "review",
|
|
80
|
-
title: "Pi Agent Swarm bearer invite",
|
|
81
|
-
lines: [
|
|
82
|
-
"Copy this only into another local Pi session you trust.",
|
|
83
|
-
"Pi Agent Swarm does not save the invite, but any copied bearer remains usable until its holder discards it.",
|
|
84
|
-
],
|
|
85
|
-
content: state.invite ?? "Invite is unavailable.",
|
|
86
|
-
format: { kind: "text" },
|
|
87
|
-
viewportSize: "adaptive",
|
|
88
|
-
hint: "back",
|
|
89
|
-
}),
|
|
90
|
-
requestPolicy: ({ state }) => ({
|
|
91
|
-
kind: "choice",
|
|
92
|
-
title: "Incoming agent requests",
|
|
93
|
-
lines: [
|
|
94
|
-
"Messages always enter context without starting a turn.",
|
|
95
|
-
"Allowed requests may spend model tokens and edit this workspace concurrently.",
|
|
96
|
-
],
|
|
97
|
-
items: [
|
|
98
|
-
{ id: "block", label: "Messages only", description: "Do not start remote-request turns" },
|
|
99
|
-
{ id: "allow", label: "Allow requests", description: "Permit one turn per request" },
|
|
100
|
-
],
|
|
101
|
-
action: "setPolicy",
|
|
102
|
-
currentItemId: state.acceptsRequests ? "allow" : "block",
|
|
103
|
-
initialItemId: state.acceptsRequests ? "allow" : "block",
|
|
104
|
-
viewportSize: 4,
|
|
105
|
-
hint: "back",
|
|
106
|
-
}),
|
|
107
|
-
settings: ({ state }) => ({
|
|
108
|
-
kind: "settings",
|
|
109
|
-
title: "Pi Agent Swarm Settings",
|
|
110
|
-
lines: [`User settings · ${safeTerminalLine(state.settingsPath)}`],
|
|
31
|
+
main: () => ({
|
|
32
|
+
kind: "actions",
|
|
33
|
+
title: "Pi Agent Swarm",
|
|
34
|
+
lines: ["Spawn workers and manage this session's badge."],
|
|
111
35
|
items: [
|
|
112
36
|
{
|
|
113
|
-
id: "
|
|
114
|
-
label: "
|
|
115
|
-
|
|
116
|
-
currentValue: state.settings.confirmSessionLaunch ? "Ask" : "Skip",
|
|
117
|
-
values: ["Ask", "Skip"],
|
|
118
|
-
action: "setConfirmation",
|
|
37
|
+
id: "spawn",
|
|
38
|
+
label: "Spawn",
|
|
39
|
+
action: "spawn",
|
|
119
40
|
},
|
|
120
41
|
{
|
|
121
|
-
id: "
|
|
122
|
-
label: "
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
42
|
+
id: "rename",
|
|
43
|
+
label: "Rename",
|
|
44
|
+
action: "rename",
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
id: "color",
|
|
48
|
+
label: "Set color",
|
|
49
|
+
action: "setColor",
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
id: "theme",
|
|
53
|
+
label: "Set theme",
|
|
54
|
+
action: "setTheme",
|
|
128
55
|
},
|
|
129
56
|
],
|
|
130
|
-
|
|
131
|
-
settingsInvalid: ({ state }) => ({
|
|
132
|
-
kind: "detail",
|
|
133
|
-
title: "Pi Agent Swarm Settings · Read only",
|
|
134
|
-
lines: [
|
|
135
|
-
`Invalid settings file. Fix ${safeTerminalLine(state.settingsPath)} and run /reload. The file will not be overwritten.`,
|
|
136
|
-
...(state.issue ? [`Issue: ${safeTerminalLine(state.issue.message)}`] : []),
|
|
137
|
-
],
|
|
138
|
-
hint: "back",
|
|
139
|
-
}),
|
|
140
|
-
status: ({ state }) => ({
|
|
141
|
-
kind: "detail",
|
|
142
|
-
title: "Pi Agent Swarm status",
|
|
143
|
-
lines: state.connected
|
|
144
|
-
? [
|
|
145
|
-
"State: connected",
|
|
146
|
-
`Session: ${safeTerminalLine(state.self?.name ?? state.self?.sessionId ?? "unknown")}`,
|
|
147
|
-
`Cwd: ${safeTerminalLine(state.self?.cwd ?? "unknown")}`,
|
|
148
|
-
`Other live sessions: ${state.peers.length}`,
|
|
149
|
-
`Incoming requests: ${state.acceptsRequests ? "allowed" : "blocked"}`,
|
|
150
|
-
`Launch confirmation: ${state.settings.confirmSessionLaunch ? "Ask" : "Skip"}`,
|
|
151
|
-
`Pin new windows to lead workspace: ${state.settings.pinToLeadWorkspace ? "On" : "Off"}`,
|
|
152
|
-
"Delivery acknowledgement means extension acceptance, not remote task completion.",
|
|
153
|
-
]
|
|
154
|
-
: [
|
|
155
|
-
"State: disconnected",
|
|
156
|
-
"No socket, group secret, or background discovery is active.",
|
|
157
|
-
`Launch confirmation: ${state.settings.confirmSessionLaunch ? "Ask" : "Skip"}`,
|
|
158
|
-
`Pin new windows to lead workspace: ${state.settings.pinToLeadWorkspace ? "On" : "Off"}`,
|
|
159
|
-
],
|
|
160
|
-
hint: "back",
|
|
161
|
-
}),
|
|
162
|
-
help: () => ({
|
|
163
|
-
kind: "detail",
|
|
164
|
-
title: "Pi Agent Swarm help",
|
|
165
|
-
lines: [
|
|
166
|
-
"Pi Agent Swarm connects explicit sessions owned by one OS user.",
|
|
167
|
-
"New Pi session creates a separate process in an external terminal window and preserves the parent.",
|
|
168
|
-
"Notify messages do not start turns, requests require recipient permission, and replies do not auto-trigger another turn.",
|
|
169
|
-
"Groups, invites, peer state, and message deduplication are ephemeral.",
|
|
170
|
-
],
|
|
171
|
-
hint: "back",
|
|
172
|
-
}),
|
|
173
|
-
leave: ({ state }) => ({
|
|
174
|
-
kind: "review",
|
|
175
|
-
title: "Leave Pi Agent Swarm group?",
|
|
176
|
-
lines: [
|
|
177
|
-
`Other live sessions: ${state.peers.length}`,
|
|
178
|
-
"Leaving closes this session's socket and forgets its in-memory bearer invite.",
|
|
179
|
-
],
|
|
180
|
-
content: "Already delivered peer messages remain in each Pi session transcript.",
|
|
181
|
-
format: { kind: "text" },
|
|
182
|
-
confirm: { id: "leave", label: "Leave group", action: "leave" },
|
|
183
|
-
hint: "back",
|
|
57
|
+
hint: "close",
|
|
184
58
|
}),
|
|
185
59
|
},
|
|
186
60
|
actions: {
|
|
@@ -203,93 +77,34 @@ export function createSwarmMenu(source: SwarmMenuSource) {
|
|
|
203
77
|
);
|
|
204
78
|
return { kind: "close" };
|
|
205
79
|
},
|
|
206
|
-
|
|
207
|
-
const
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
);
|
|
212
|
-
if (!confirmed || signal.aborted) return { kind: "stay" };
|
|
213
|
-
await source.start(ctx, signal);
|
|
80
|
+
rename: async ({ ctx, signal, state }) => {
|
|
81
|
+
const name = await ctx.ui.input("Rename session", state.self?.name ?? "Enter a new session name", {
|
|
82
|
+
signal,
|
|
83
|
+
});
|
|
84
|
+
if (name === undefined || !name.trim() || signal.aborted) return { kind: "stay" };
|
|
85
|
+
await source.rename(ctx, name, signal);
|
|
214
86
|
return { kind: "stay" };
|
|
215
87
|
},
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
if (!confirmed || signal.aborted) return { kind: "stay" };
|
|
224
|
-
await source.join(ctx, value, signal);
|
|
225
|
-
return { kind: "to", screen: "main" };
|
|
88
|
+
setColor: async ({ ctx, signal }) => {
|
|
89
|
+
const choice = await ctx.ui.select("Session color", [...SWARM_COLORS.map((color) => color.name)], {
|
|
90
|
+
signal,
|
|
91
|
+
});
|
|
92
|
+
if (!choice || signal.aborted) return { kind: "stay" };
|
|
93
|
+
await source.setColor(ctx, choice, signal);
|
|
94
|
+
return { kind: "stay" };
|
|
226
95
|
},
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
const labels = state.peers.map(
|
|
232
|
-
(peer) => `${safeTerminalLine(peer.name ?? peer.sessionId)} · ${safeTerminalLine(peer.sessionId)}`,
|
|
233
|
-
);
|
|
234
|
-
const selected = await ctx.ui.select("Send to Pi session", labels, { signal });
|
|
235
|
-
if (!selected || signal.aborted) return { kind: "stay" };
|
|
236
|
-
const peer = state.peers[labels.indexOf(selected)];
|
|
237
|
-
if (!peer) return { kind: "rejected", error: new Error("Selected peer is stale") };
|
|
238
|
-
const modeLabel = await ctx.ui.select(
|
|
239
|
-
"Delivery mode",
|
|
240
|
-
["Message only", "Agent request — may start a paid turn"],
|
|
96
|
+
setTheme: async ({ ctx, signal }) => {
|
|
97
|
+
const choice = await ctx.ui.select(
|
|
98
|
+
"Roster theme (badge palette)",
|
|
99
|
+
PALETTE_OPTIONS.map((option) => option.label),
|
|
241
100
|
{ signal },
|
|
242
101
|
);
|
|
243
|
-
if (!
|
|
244
|
-
const
|
|
245
|
-
if (!
|
|
246
|
-
await source.
|
|
247
|
-
ctx,
|
|
248
|
-
{
|
|
249
|
-
targetSessionId: peer.sessionId,
|
|
250
|
-
text,
|
|
251
|
-
mode: modeLabel.startsWith("Agent request") ? "request" : "notify",
|
|
252
|
-
},
|
|
253
|
-
signal,
|
|
254
|
-
);
|
|
102
|
+
if (!choice || signal.aborted) return { kind: "stay" };
|
|
103
|
+
const palette = PALETTE_OPTIONS.find((option) => option.label === choice)?.value;
|
|
104
|
+
if (!palette) return { kind: "stay" };
|
|
105
|
+
await source.setPalette(ctx, palette, signal);
|
|
255
106
|
return { kind: "stay" };
|
|
256
107
|
},
|
|
257
|
-
setConfirmation: ({ ctx, signal, value }) =>
|
|
258
|
-
saveSettingsPatch(
|
|
259
|
-
source,
|
|
260
|
-
ctx,
|
|
261
|
-
signal,
|
|
262
|
-
{ confirmSessionLaunch: value !== "Skip" },
|
|
263
|
-
`Confirm new sessions: ${value}.`,
|
|
264
|
-
),
|
|
265
|
-
setPinToLeadWorkspace: ({ ctx, signal, value }) =>
|
|
266
|
-
saveSettingsPatch(
|
|
267
|
-
source,
|
|
268
|
-
ctx,
|
|
269
|
-
signal,
|
|
270
|
-
{ pinToLeadWorkspace: value !== "Off" },
|
|
271
|
-
`Pin new windows to lead workspace: ${value}.`,
|
|
272
|
-
),
|
|
273
|
-
setPolicy: async ({ ctx, signal, itemId }) => {
|
|
274
|
-
const allow = itemId === "allow";
|
|
275
|
-
if (itemId !== "allow" && itemId !== "block") {
|
|
276
|
-
return { kind: "rejected", error: new Error("Pi Agent Swarm request policy is invalid") };
|
|
277
|
-
}
|
|
278
|
-
if (allow) {
|
|
279
|
-
const confirmed = await ctx.ui.confirm(
|
|
280
|
-
"Allow incoming agent requests?",
|
|
281
|
-
"A trusted invite holder may start paid model turns that can edit the current workspace.",
|
|
282
|
-
{ signal },
|
|
283
|
-
);
|
|
284
|
-
if (!confirmed || signal.aborted) return { kind: "stay" };
|
|
285
|
-
}
|
|
286
|
-
source.setAcceptsRequests(allow);
|
|
287
|
-
return { kind: "to", screen: "requestPolicy" };
|
|
288
|
-
},
|
|
289
|
-
leave: async () => {
|
|
290
|
-
await source.leave();
|
|
291
|
-
return { kind: "to", screen: "main" };
|
|
292
|
-
},
|
|
293
108
|
},
|
|
294
109
|
};
|
|
295
110
|
return { menu, getState };
|
|
@@ -314,84 +129,3 @@ export async function showSwarmMenu(
|
|
|
314
129
|
},
|
|
315
130
|
});
|
|
316
131
|
}
|
|
317
|
-
|
|
318
|
-
async function saveSettingsPatch(
|
|
319
|
-
source: SwarmMenuSource,
|
|
320
|
-
ctx: ExtensionCommandContext,
|
|
321
|
-
signal: AbortSignal,
|
|
322
|
-
patch: SwarmSettingsPatch,
|
|
323
|
-
successMessage: string,
|
|
324
|
-
) {
|
|
325
|
-
if (signal.aborted) return { kind: "rejected" as const };
|
|
326
|
-
try {
|
|
327
|
-
await source.updateSettings(patch, signal);
|
|
328
|
-
if (signal.aborted) return { kind: "rejected" as const };
|
|
329
|
-
ctx.ui.notify(successMessage, "info");
|
|
330
|
-
return { kind: "stay" as const };
|
|
331
|
-
} catch (error) {
|
|
332
|
-
if (!signal.aborted) {
|
|
333
|
-
ctx.ui.notify(`Could not save Pi Agent Swarm settings; the previous value remains: ${safeError(error)}`, "error");
|
|
334
|
-
}
|
|
335
|
-
return { kind: "rejected" as const };
|
|
336
|
-
}
|
|
337
|
-
}
|
|
338
|
-
|
|
339
|
-
function settingsMenuItem(state: SwarmMenuState) {
|
|
340
|
-
return state.issue
|
|
341
|
-
? {
|
|
342
|
-
id: "settings",
|
|
343
|
-
label: "Settings",
|
|
344
|
-
description: "Read-only until the invalid settings file is fixed.",
|
|
345
|
-
to: "settingsInvalid" as const,
|
|
346
|
-
}
|
|
347
|
-
: { id: "settings", label: "Settings", to: "settings" as const };
|
|
348
|
-
}
|
|
349
|
-
|
|
350
|
-
function mainScreen(state: SwarmMenuState) {
|
|
351
|
-
if (!state.connected) {
|
|
352
|
-
return {
|
|
353
|
-
kind: "actions" as const,
|
|
354
|
-
title: "Pi Agent Swarm · disconnected",
|
|
355
|
-
lines: ["Local Pi sessions with configured terminal launches and messaging."],
|
|
356
|
-
items: [
|
|
357
|
-
{
|
|
358
|
-
id: "spawn",
|
|
359
|
-
label: "New Pi session…",
|
|
360
|
-
action: "spawn" as const,
|
|
361
|
-
busyLabel: "Launching",
|
|
362
|
-
},
|
|
363
|
-
{ id: "join", label: "Join with invite", to: "join" as const },
|
|
364
|
-
{ id: "start", label: "Start local group", action: "start" as const },
|
|
365
|
-
settingsMenuItem(state),
|
|
366
|
-
{ id: "status", label: "Status", to: "status" as const },
|
|
367
|
-
{ id: "help", label: "Help", to: "help" as const },
|
|
368
|
-
],
|
|
369
|
-
hint: "close" as const,
|
|
370
|
-
};
|
|
371
|
-
}
|
|
372
|
-
return {
|
|
373
|
-
kind: "actions" as const,
|
|
374
|
-
title: `Pi Agent Swarm · ${safeTerminalLine(state.self?.name ?? state.self?.sessionId ?? "connected")}`,
|
|
375
|
-
lines: [
|
|
376
|
-
`${state.peers.length} other live session${state.peers.length === 1 ? "" : "s"} · incoming requests ${state.acceptsRequests ? "allowed" : "blocked"}`,
|
|
377
|
-
"Delivery acknowledgement confirms extension acceptance only.",
|
|
378
|
-
],
|
|
379
|
-
items: [
|
|
380
|
-
{
|
|
381
|
-
id: "spawn",
|
|
382
|
-
label: "New Pi session…",
|
|
383
|
-
action: "spawn" as const,
|
|
384
|
-
busyLabel: "Launching",
|
|
385
|
-
},
|
|
386
|
-
{ id: "send", label: "Send message", action: "send" as const },
|
|
387
|
-
{ id: "sessions", label: "Sessions", to: "sessions" as const },
|
|
388
|
-
{ id: "invite", label: "Invite another session", to: "invite" as const },
|
|
389
|
-
{ id: "policy", label: "Request policy", to: "requestPolicy" as const },
|
|
390
|
-
settingsMenuItem(state),
|
|
391
|
-
{ id: "status", label: "Status", to: "status" as const },
|
|
392
|
-
{ id: "help", label: "Help", to: "help" as const },
|
|
393
|
-
{ id: "leave", label: "Leave group…", to: "leave" as const },
|
|
394
|
-
],
|
|
395
|
-
hint: "close" as const,
|
|
396
|
-
};
|
|
397
|
-
}
|
package/src/pi-agent-swarm.ts
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
import type { ExtensionAPI, ExtensionCommandContext } from "@earendil-works/pi-coding-agent";
|
|
2
|
-
import { normalizeSwarmColor } from "./color.js";
|
|
3
2
|
import type { SwarmMenuSource } from "./menu.js";
|
|
4
3
|
import { parseInvite } from "./protocol.js";
|
|
5
4
|
import { renderSwarmMessage, SWARM_MESSAGE_TYPE } from "./renderer.js";
|
|
6
5
|
import { createSwarmSettingsRuntime, type SwarmSettingsRuntime } from "./settings.js";
|
|
7
6
|
import { SwarmController, type SwarmControllerDependencies } from "./swarm-controller.js";
|
|
8
|
-
import {
|
|
7
|
+
import { safeTerminalLine } from "./text.js";
|
|
9
8
|
import { registerSwarmTools } from "./tools.js";
|
|
10
9
|
|
|
11
10
|
const USAGE = "Usage: /swarm, /swarm start, or /swarm <piagentswarm:v1:invite>";
|
|
@@ -33,13 +32,19 @@ export function createPiSwarmExtension(dependencies: PiSwarmDependencies = {}):
|
|
|
33
32
|
pi.on("session_shutdown", async (event, ctx) => {
|
|
34
33
|
await controller.sessionShutdown(event, ctx);
|
|
35
34
|
});
|
|
35
|
+
pi.on("turn_end", async (_event, ctx) => {
|
|
36
|
+
controller.noteTurnEnd(ctx);
|
|
37
|
+
});
|
|
38
|
+
pi.on("session_info_changed", async (event, ctx) => {
|
|
39
|
+
await controller.noteSessionInfoChanged(event.name, ctx);
|
|
40
|
+
});
|
|
36
41
|
pi.on("input", async (event, ctx) => {
|
|
37
42
|
if (event.source !== "interactive") return;
|
|
38
43
|
void controller.relaySteerInput(event.text, ctx);
|
|
39
44
|
});
|
|
40
45
|
|
|
41
46
|
pi.registerCommand("swarm", {
|
|
42
|
-
description: "Spawn
|
|
47
|
+
description: "Spawn a worker Pi session, or start/join a local group",
|
|
43
48
|
handler: async (rawArgs, ctx) => {
|
|
44
49
|
const args = rawArgs.trim();
|
|
45
50
|
if (ctx.mode !== "tui" && ctx.mode !== "rpc") {
|
|
@@ -56,7 +61,7 @@ export function createPiSwarmExtension(dependencies: PiSwarmDependencies = {}):
|
|
|
56
61
|
const ownerSignal = controller.sessionSignal;
|
|
57
62
|
const menuModule = await loadMenu();
|
|
58
63
|
if (ownerSignal.aborted || !controller.isCurrent(ctx)) return;
|
|
59
|
-
await menuModule.showSwarmMenu(ctx, menuSource(controller
|
|
64
|
+
await menuModule.showSwarmMenu(ctx, menuSource(controller), {
|
|
60
65
|
signal: ownerSignal,
|
|
61
66
|
isCurrent: () => controller.isCurrent(ctx) && !ownerSignal.aborted,
|
|
62
67
|
});
|
|
@@ -79,7 +84,7 @@ export function createPiSwarmExtension(dependencies: PiSwarmDependencies = {}):
|
|
|
79
84
|
pi.registerCommand("color", {
|
|
80
85
|
description: "Set this session's color (palette name or #rrggbb)",
|
|
81
86
|
handler: async (rawArgs, ctx) => {
|
|
82
|
-
const color =
|
|
87
|
+
const color = controller.normalizeColor(rawArgs.trim());
|
|
83
88
|
if (!color) {
|
|
84
89
|
throw new Error("Usage: /color <red|orange|yellow|green|cyan|blue|magenta|purple|#rrggbb>");
|
|
85
90
|
}
|
|
@@ -123,17 +128,9 @@ async function startGroupDirectly(controller: SwarmController, ctx: ExtensionCom
|
|
|
123
128
|
}
|
|
124
129
|
}
|
|
125
130
|
|
|
126
|
-
function menuSource(
|
|
127
|
-
controller: SwarmController,
|
|
128
|
-
settings: SwarmSettingsRuntime,
|
|
129
|
-
ctx: ExtensionCommandContext,
|
|
130
|
-
): SwarmMenuSource {
|
|
131
|
+
function menuSource(controller: SwarmController): SwarmMenuSource {
|
|
131
132
|
return {
|
|
132
|
-
snapshot:
|
|
133
|
-
...(await controller.snapshot(signal)),
|
|
134
|
-
...settings.get(),
|
|
135
|
-
settingsPath: settings.getPath(),
|
|
136
|
-
}),
|
|
133
|
+
snapshot: (signal) => controller.snapshot(signal),
|
|
137
134
|
spawn: async (commandContext, input, signal) => {
|
|
138
135
|
const result = await controller.spawn(commandContext, input, signal);
|
|
139
136
|
if (controller.isCurrent(commandContext)) {
|
|
@@ -143,38 +140,26 @@ function menuSource(
|
|
|
143
140
|
);
|
|
144
141
|
}
|
|
145
142
|
},
|
|
146
|
-
|
|
147
|
-
await controller.
|
|
143
|
+
rename: async (commandContext, name, signal) => {
|
|
144
|
+
await controller.setOwnName(commandContext, name, signal);
|
|
148
145
|
if (controller.isCurrent(commandContext)) {
|
|
149
|
-
commandContext.ui.notify(
|
|
146
|
+
commandContext.ui.notify(`Session renamed to ${safeTerminalLine(name)}.`, "info");
|
|
150
147
|
}
|
|
151
148
|
},
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
149
|
+
setColor: async (commandContext, color, signal) => {
|
|
150
|
+
const normalized = controller.normalizeColor(color);
|
|
151
|
+
if (!normalized) throw new Error("Pi Agent Swarm color is invalid");
|
|
152
|
+
await controller.setOwnColor(commandContext, normalized, signal);
|
|
153
|
+
if (controller.isCurrent(commandContext)) {
|
|
154
|
+
commandContext.ui.notify(`Color set to ${normalized}.`, "info");
|
|
157
155
|
}
|
|
158
156
|
},
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
if (!result.acknowledgement.accepted) {
|
|
162
|
-
throw new Error(
|
|
163
|
-
`Target rejected the message: ${safeTerminalLine(result.acknowledgement.error ?? "unknown reason")}`,
|
|
164
|
-
);
|
|
165
|
-
}
|
|
157
|
+
setPalette: async (commandContext, palette, signal) => {
|
|
158
|
+
await controller.setColorPalette(commandContext, palette, signal);
|
|
166
159
|
if (controller.isCurrent(commandContext)) {
|
|
167
|
-
commandContext.ui.notify(
|
|
168
|
-
`Target accepted ${safeTerminalLine(result.message.id)}. This does not prove task completion.`,
|
|
169
|
-
"info",
|
|
170
|
-
);
|
|
160
|
+
commandContext.ui.notify(`Theme set to ${palette}.`, "info");
|
|
171
161
|
}
|
|
172
162
|
},
|
|
173
|
-
updateSettings: async (patch) => {
|
|
174
|
-
await settings.update(patch);
|
|
175
|
-
},
|
|
176
|
-
setAcceptsRequests: (value) => controller.setAcceptsRequests(ctx, value),
|
|
177
|
-
leave: () => controller.leave(ctx),
|
|
178
163
|
};
|
|
179
164
|
}
|
|
180
165
|
|
package/src/settings.ts
CHANGED
|
@@ -3,16 +3,23 @@ import { constants } from "node:fs";
|
|
|
3
3
|
import { lstat, mkdir, open, rename, rm, writeFile } from "node:fs/promises";
|
|
4
4
|
import { basename, dirname, join } from "node:path";
|
|
5
5
|
import { getAgentDir } from "@earendil-works/pi-coding-agent";
|
|
6
|
+
import { isAutoNameModel } from "./auto-name.js";
|
|
7
|
+
import type { SwarmColorPaletteName } from "./color.js";
|
|
6
8
|
|
|
7
9
|
export const SWARM_SETTINGS_FILE = "pi-agent-swarm.json";
|
|
8
10
|
export const MAX_SWARM_SETTINGS_BYTES = 64 * 1024;
|
|
9
11
|
export const DEFAULT_SWARM_SETTINGS: Readonly<SwarmSettings> = Object.freeze({
|
|
12
|
+
autoName: true,
|
|
13
|
+
colorPalette: "muted",
|
|
10
14
|
confirmSessionLaunch: true,
|
|
11
15
|
externalCommand: "alacritty -e",
|
|
12
16
|
pinToLeadWorkspace: false,
|
|
13
17
|
});
|
|
14
18
|
|
|
15
19
|
export interface SwarmSettings {
|
|
20
|
+
autoName: boolean;
|
|
21
|
+
autoNameModel?: string;
|
|
22
|
+
colorPalette: SwarmColorPaletteName;
|
|
16
23
|
confirmSessionLaunch: boolean;
|
|
17
24
|
externalCommand: string;
|
|
18
25
|
pinToLeadWorkspace: boolean;
|
|
@@ -73,6 +80,9 @@ export interface SwarmSettingsRuntimeOptions {
|
|
|
73
80
|
}
|
|
74
81
|
|
|
75
82
|
const SETTING_FIELDS = [
|
|
83
|
+
"autoName",
|
|
84
|
+
"autoNameModel",
|
|
85
|
+
"colorPalette",
|
|
76
86
|
"confirmSessionLaunch",
|
|
77
87
|
"externalCommand",
|
|
78
88
|
"pinToLeadWorkspace",
|
|
@@ -88,6 +98,23 @@ export function normalizeSwarmSettingsDocument(value: unknown): NormalizedSwarmS
|
|
|
88
98
|
const settings: SwarmSettings = { ...DEFAULT_SWARM_SETTINGS };
|
|
89
99
|
const sources = builtInSources();
|
|
90
100
|
|
|
101
|
+
if (Object.hasOwn(value, "autoName")) {
|
|
102
|
+
if (typeof value.autoName !== "boolean") return undefined;
|
|
103
|
+
settings.autoName = value.autoName;
|
|
104
|
+
sources.autoName = "user";
|
|
105
|
+
}
|
|
106
|
+
if (Object.hasOwn(value, "autoNameModel")) {
|
|
107
|
+
if (typeof value.autoNameModel !== "string" || !isAutoNameModel(value.autoNameModel.trim())) {
|
|
108
|
+
return undefined;
|
|
109
|
+
}
|
|
110
|
+
settings.autoNameModel = value.autoNameModel.trim();
|
|
111
|
+
sources.autoNameModel = "user";
|
|
112
|
+
}
|
|
113
|
+
if (Object.hasOwn(value, "colorPalette")) {
|
|
114
|
+
if (value.colorPalette !== "muted" && value.colorPalette !== "bright") return undefined;
|
|
115
|
+
settings.colorPalette = value.colorPalette;
|
|
116
|
+
sources.colorPalette = "user";
|
|
117
|
+
}
|
|
91
118
|
if (Object.hasOwn(value, "confirmSessionLaunch")) {
|
|
92
119
|
if (typeof value.confirmSessionLaunch !== "boolean") return undefined;
|
|
93
120
|
settings.confirmSessionLaunch = value.confirmSessionLaunch;
|
|
@@ -157,6 +184,9 @@ export function createInMemorySwarmSettingsRuntime(): SwarmSettingsRuntime {
|
|
|
157
184
|
settings: { ...state.settings, ...canonical },
|
|
158
185
|
sources: {
|
|
159
186
|
...state.sources,
|
|
187
|
+
...(canonical.autoName !== undefined ? { autoName: "user" as const } : {}),
|
|
188
|
+
...(canonical.autoNameModel !== undefined ? { autoNameModel: "user" as const } : {}),
|
|
189
|
+
...(canonical.colorPalette !== undefined ? { colorPalette: "user" as const } : {}),
|
|
160
190
|
...(canonical.confirmSessionLaunch !== undefined ? { confirmSessionLaunch: "user" as const } : {}),
|
|
161
191
|
...(canonical.externalCommand ? { externalCommand: "user" as const } : {}),
|
|
162
192
|
...(canonical.pinToLeadWorkspace !== undefined ? { pinToLeadWorkspace: "user" as const } : {}),
|
|
@@ -331,6 +361,9 @@ function invalidLoad(path: string, reason: string): SwarmSettingsLoadResult {
|
|
|
331
361
|
|
|
332
362
|
function builtInSources(): Record<SwarmSettingsField, SwarmSettingsSource> {
|
|
333
363
|
return {
|
|
364
|
+
autoName: "built-in",
|
|
365
|
+
autoNameModel: "built-in",
|
|
366
|
+
colorPalette: "built-in",
|
|
334
367
|
confirmSessionLaunch: "built-in",
|
|
335
368
|
externalCommand: "built-in",
|
|
336
369
|
pinToLeadWorkspace: "built-in",
|