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/package.json
CHANGED
package/src/auto-name.ts
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import { type Api, type AssistantMessage, contentText, type Model } from "@earendil-works/pi-ai";
|
|
2
|
+
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
3
|
+
import { safeTerminalLine } from "./text.js";
|
|
4
|
+
|
|
5
|
+
/** Prefix applied to auto-generated session names, replacing the MANAGER-<id> default. */
|
|
6
|
+
export const AUTO_NAME_PREFIX = "MAN-";
|
|
7
|
+
/** Auto-naming runs once, after this many completed turns. */
|
|
8
|
+
export const AUTO_NAME_TURN_THRESHOLD = 3;
|
|
9
|
+
/** Maximum length of the generated title (the part after `MAN-`), in Unicode code points. */
|
|
10
|
+
export const AUTO_NAME_MAX_TITLE_CHARS = 24;
|
|
11
|
+
/** Per-message and total caps on the conversation excerpt sent to the title model. */
|
|
12
|
+
export const AUTO_NAME_MAX_MESSAGE_CHARS = 500;
|
|
13
|
+
export const AUTO_NAME_MAX_CONTEXT_CHARS = 2_000;
|
|
14
|
+
/** Wall-clock budget for the one-shot title completion. */
|
|
15
|
+
export const AUTO_NAME_TIMEOUT_MS = 120_000;
|
|
16
|
+
|
|
17
|
+
/** Maximum title-completion attempts per session before auto-naming gives up. */
|
|
18
|
+
export const AUTO_NAME_MAX_ATTEMPTS = 3;
|
|
19
|
+
|
|
20
|
+
// Matches exactly what sessionStart generates: `MANAGER-` plus the first 8
|
|
21
|
+
// characters of the session id. Session ids are not guaranteed to be hex (UUIDs
|
|
22
|
+
// can put a hyphen inside the first 8 characters), so accept any non-space run.
|
|
23
|
+
const DEFAULT_MANAGER_NAME_PATTERN = /^MANAGER-\S{8}$/u;
|
|
24
|
+
const AUTO_NAME_MODEL_PATTERN = /^[^\s/]+\/[^\s/]+$/u;
|
|
25
|
+
|
|
26
|
+
/** The default name assigned to human-started sessions before auto-naming kicks in. */
|
|
27
|
+
export function isDefaultManagerName(name: string | undefined): boolean {
|
|
28
|
+
return name !== undefined && DEFAULT_MANAGER_NAME_PATTERN.test(name);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Validate the `provider/id` shape accepted by the `autoNameModel` setting. */
|
|
32
|
+
export function isAutoNameModel(value: string): boolean {
|
|
33
|
+
return AUTO_NAME_MODEL_PATTERN.test(value);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Turn raw model output into a `MAN-<title>` session name. Strips terminal
|
|
38
|
+
* controls, quotes, and markdown noise, keeps the first line, truncates to
|
|
39
|
+
* AUTO_NAME_MAX_TITLE_CHARS code points, and returns undefined when nothing
|
|
40
|
+
* usable remains. 24 CJK characters stay well under the 200-byte name budget.
|
|
41
|
+
*/
|
|
42
|
+
export function toAutoName(raw: string | undefined): string | undefined {
|
|
43
|
+
if (!raw) return undefined;
|
|
44
|
+
const line = safeTerminalLine(raw)
|
|
45
|
+
.replace(/^[\s"'`“”‘’#>*_~-]+/u, "")
|
|
46
|
+
.replace(/[\s"'`“”‘’.,;:!?。,;:!?…·#>*_~-]+$/u, "")
|
|
47
|
+
.trim();
|
|
48
|
+
if (!line) return undefined;
|
|
49
|
+
const title = [...line].slice(0, AUTO_NAME_MAX_TITLE_CHARS).join("");
|
|
50
|
+
return `${AUTO_NAME_PREFIX}${title}`;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Collect the first AUTO_NAME_TURN_THRESHOLD user messages from the current
|
|
55
|
+
* branch, each clipped to AUTO_NAME_MAX_MESSAGE_CHARS and capped in total.
|
|
56
|
+
*/
|
|
57
|
+
export function collectAutoNameMessages(ctx: ExtensionContext): string[] {
|
|
58
|
+
const messages: string[] = [];
|
|
59
|
+
let total = 0;
|
|
60
|
+
for (const entry of ctx.sessionManager.getBranch()) {
|
|
61
|
+
if (messages.length >= AUTO_NAME_TURN_THRESHOLD || total >= AUTO_NAME_MAX_CONTEXT_CHARS) break;
|
|
62
|
+
if (entry.type !== "message") continue;
|
|
63
|
+
const message = (entry as { message?: unknown }).message;
|
|
64
|
+
if (!isRecord(message) || message.role !== "user") continue;
|
|
65
|
+
const content = (message as { content?: unknown }).content;
|
|
66
|
+
if (typeof content !== "string" && !Array.isArray(content)) continue;
|
|
67
|
+
const text = safeTerminalLine(contentText(content as Parameters<typeof contentText>[0]));
|
|
68
|
+
if (!text) continue;
|
|
69
|
+
const clipped = [...text].slice(0, AUTO_NAME_MAX_MESSAGE_CHARS).join("");
|
|
70
|
+
messages.push(clipped);
|
|
71
|
+
total += clipped.length;
|
|
72
|
+
}
|
|
73
|
+
return messages;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** Prompt asking the model for a bare, short session title in the user's language. */
|
|
77
|
+
export function buildAutoNamePrompt(messages: readonly string[]): string {
|
|
78
|
+
return [
|
|
79
|
+
"Summarize the user's goal in this conversation as a short session title.",
|
|
80
|
+
"",
|
|
81
|
+
"Rules:",
|
|
82
|
+
"- Reply with only the title: no quotes, no prefix, no trailing punctuation.",
|
|
83
|
+
"- Use the same language as the user messages.",
|
|
84
|
+
`- Keep it under ${AUTO_NAME_MAX_TITLE_CHARS} characters.`,
|
|
85
|
+
'- Name the task or topic, for example "修复登录页样式" or "Add payments API retry".',
|
|
86
|
+
"",
|
|
87
|
+
"User messages:",
|
|
88
|
+
...messages.map((message, index) => `${index + 1}. ${message}`),
|
|
89
|
+
].join("\n");
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Resolve the model used for the title completion: the configured
|
|
94
|
+
* `provider/id` override when set, otherwise the session's current model.
|
|
95
|
+
* Returns undefined when the override cannot be resolved.
|
|
96
|
+
*/
|
|
97
|
+
export function resolveAutoNameModel(ctx: ExtensionContext, configured: string | undefined): Model<Api> | undefined {
|
|
98
|
+
if (!configured) return ctx.model;
|
|
99
|
+
const slash = configured.indexOf("/");
|
|
100
|
+
return ctx.modelRegistry.find(configured.slice(0, slash), configured.slice(slash + 1));
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** Default one-shot title completion through the session's model registry. */
|
|
104
|
+
export async function completeSessionTitle(
|
|
105
|
+
ctx: ExtensionContext,
|
|
106
|
+
model: Model<Api>,
|
|
107
|
+
prompt: string,
|
|
108
|
+
signal?: AbortSignal,
|
|
109
|
+
): Promise<string> {
|
|
110
|
+
const message: AssistantMessage = await ctx.modelRegistry.complete(
|
|
111
|
+
model,
|
|
112
|
+
{ messages: [{ role: "user", content: prompt, timestamp: Date.now() }] },
|
|
113
|
+
{ signal },
|
|
114
|
+
);
|
|
115
|
+
return contentText(message.content);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Per-session state machine for auto-name attempts. The controller feeds
|
|
120
|
+
* completed turns and external name changes in; the namer fires on the
|
|
121
|
+
* AUTO_NAME_TURN_THRESHOLD-th turn while the session still carries its
|
|
122
|
+
* default manager name and the user has not taken over naming. A failed
|
|
123
|
+
* attempt is retried on later turns, up to AUTO_NAME_MAX_ATTEMPTS attempts,
|
|
124
|
+
* because local single-threaded models can queue or drop one request.
|
|
125
|
+
*/
|
|
126
|
+
export class AutoNamer {
|
|
127
|
+
private turnsSeen = 0;
|
|
128
|
+
private attempts = 0;
|
|
129
|
+
private userNamed = false;
|
|
130
|
+
|
|
131
|
+
reset(): void {
|
|
132
|
+
this.turnsSeen = 0;
|
|
133
|
+
this.attempts = 0;
|
|
134
|
+
this.userNamed = false;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Record a name change produced outside the auto-namer. A default manager
|
|
139
|
+
* name is the extension's own bootstrap and ignored; anything else means the
|
|
140
|
+
* user (or a handoff) owns the name and auto-naming stays out of the way.
|
|
141
|
+
*/
|
|
142
|
+
noteExternalName(name: string | undefined): void {
|
|
143
|
+
if (isDefaultManagerName(name)) return;
|
|
144
|
+
this.userNamed = true;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Count one completed turn and report whether an auto-name attempt should
|
|
149
|
+
* start now. Each returned true consumes one attempt; after a successful
|
|
150
|
+
* naming the session no longer carries the default name, so later turns
|
|
151
|
+
* permanently settle via the userNamed path.
|
|
152
|
+
*/
|
|
153
|
+
noteTurn(currentName: string | undefined, enabled: boolean): boolean {
|
|
154
|
+
if (this.userNamed) return false;
|
|
155
|
+
this.turnsSeen += 1;
|
|
156
|
+
if (this.turnsSeen < AUTO_NAME_TURN_THRESHOLD) return false;
|
|
157
|
+
if (currentName !== undefined && !isDefaultManagerName(currentName)) {
|
|
158
|
+
this.userNamed = true;
|
|
159
|
+
return false;
|
|
160
|
+
}
|
|
161
|
+
if (!enabled || this.attempts >= AUTO_NAME_MAX_ATTEMPTS) return false;
|
|
162
|
+
this.attempts += 1;
|
|
163
|
+
return true;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
168
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
169
|
+
}
|
package/src/color.ts
CHANGED
|
@@ -5,7 +5,48 @@ export interface SwarmColor {
|
|
|
5
5
|
hex: string;
|
|
6
6
|
}
|
|
7
7
|
|
|
8
|
-
export
|
|
8
|
+
export type SwarmColorPaletteName = "muted" | "bright";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Roster palettes derived from GitHub Linguist language colors (the language bar on
|
|
12
|
+
* github.com), one language per hue slot (see scripts/color-draft.mjs for the visual
|
|
13
|
+
* draft). `muted` is the default low-saturation set; its yellow deliberately matches
|
|
14
|
+
* the footer's ROLE badge (ROLE_BADGE_BACKGROUND) so a yellow session reads as one
|
|
15
|
+
* warm block. `bright` keeps the same slots at GitHub's original vivid tones. Both
|
|
16
|
+
* palettes share slot order, so a seed or stored name keeps its hue when the
|
|
17
|
+
* `colorPalette` setting switches.
|
|
18
|
+
*/
|
|
19
|
+
export const SWARM_COLOR_PALETTES: Record<SwarmColorPaletteName, readonly SwarmColor[]> = {
|
|
20
|
+
muted: [
|
|
21
|
+
{ name: "red", hex: "#c22d40" }, // Scala
|
|
22
|
+
{ name: "orange", hex: "#b07219" }, // Java
|
|
23
|
+
{ name: "yellow", hex: "#ffc85a" }, // matches ROLE_BADGE_BACKGROUND in footer.ts
|
|
24
|
+
{ name: "green", hex: "#41b883" }, // Vue
|
|
25
|
+
{ name: "cyan", hex: "#4298b8" }, // Groovy
|
|
26
|
+
{ name: "blue", hex: "#3178c6" }, // TypeScript
|
|
27
|
+
{ name: "magenta", hex: "#b83998" }, // Erlang
|
|
28
|
+
{ name: "purple", hex: "#8847b9" }, // Elixir
|
|
29
|
+
],
|
|
30
|
+
bright: [
|
|
31
|
+
{ name: "red", hex: "#db5855" }, // Clojure
|
|
32
|
+
{ name: "orange", hex: "#dea584" }, // Rust
|
|
33
|
+
{ name: "yellow", hex: "#f1e05a" }, // JavaScript
|
|
34
|
+
{ name: "green", hex: "#89e051" }, // Shell
|
|
35
|
+
{ name: "cyan", hex: "#00add8" }, // Go
|
|
36
|
+
{ name: "blue", hex: "#3572a5" }, // Python
|
|
37
|
+
{ name: "magenta", hex: "#f34b7d" }, // C++
|
|
38
|
+
{ name: "purple", hex: "#a97bff" }, // Kotlin
|
|
39
|
+
],
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
/** Default roster palette: the muted GitHub-derived set. */
|
|
43
|
+
export const SWARM_COLORS: readonly SwarmColor[] = SWARM_COLOR_PALETTES.muted;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Pre-palette Tailwind colors. Kept only so hexes assigned by older versions still
|
|
47
|
+
* resolve to a palette name in labels; never used for new assignments.
|
|
48
|
+
*/
|
|
49
|
+
const LEGACY_SWARM_COLORS: readonly SwarmColor[] = [
|
|
9
50
|
{ name: "red", hex: "#ef4444" },
|
|
10
51
|
{ name: "orange", hex: "#f97316" },
|
|
11
52
|
{ name: "yellow", hex: "#eab308" },
|
|
@@ -16,25 +57,34 @@ export const SWARM_COLORS: readonly SwarmColor[] = [
|
|
|
16
57
|
{ name: "purple", hex: "#8b5cf6" },
|
|
17
58
|
];
|
|
18
59
|
|
|
60
|
+
/** The palette behind a `colorPalette` setting value. */
|
|
61
|
+
export function swarmColorPalette(name: SwarmColorPaletteName): readonly SwarmColor[] {
|
|
62
|
+
return SWARM_COLOR_PALETTES[name];
|
|
63
|
+
}
|
|
64
|
+
|
|
19
65
|
const HEX_RE = /^#[0-9a-f]{6}$/u;
|
|
20
|
-
const
|
|
21
|
-
const
|
|
66
|
+
const HEX_TO_NAME = new Map<string, string>();
|
|
67
|
+
for (const palette of [SWARM_COLOR_PALETTES.muted, SWARM_COLOR_PALETTES.bright, LEGACY_SWARM_COLORS]) {
|
|
68
|
+
for (const color of palette) {
|
|
69
|
+
if (!HEX_TO_NAME.has(color.hex)) HEX_TO_NAME.set(color.hex, color.name);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
22
72
|
|
|
23
73
|
/** Accept a palette name or a #rrggbb hex; return a normalized #rrggbb hex, or undefined. */
|
|
24
|
-
export function normalizeSwarmColor(value: unknown): string | undefined {
|
|
74
|
+
export function normalizeSwarmColor(value: unknown, palette: readonly SwarmColor[] = SWARM_COLORS): string | undefined {
|
|
25
75
|
if (typeof value !== "string") return undefined;
|
|
26
76
|
const trimmed = value.trim().toLowerCase();
|
|
27
|
-
const named =
|
|
28
|
-
if (named) return named;
|
|
77
|
+
const named = palette.find((color) => color.name === trimmed);
|
|
78
|
+
if (named) return named.hex;
|
|
29
79
|
if (HEX_RE.test(trimmed)) return trimmed;
|
|
30
80
|
return undefined;
|
|
31
81
|
}
|
|
32
82
|
|
|
33
83
|
/** Deterministic palette color from a seed string (stable, no coordination needed). */
|
|
34
|
-
export function pickSwarmColor(seed: string): string {
|
|
84
|
+
export function pickSwarmColor(seed: string, palette: readonly SwarmColor[] = SWARM_COLORS): string {
|
|
35
85
|
let hash = 0;
|
|
36
86
|
for (let i = 0; i < seed.length; i += 1) hash = (hash * 31 + seed.charCodeAt(i)) >>> 0;
|
|
37
|
-
return
|
|
87
|
+
return palette[hash % palette.length].hex;
|
|
38
88
|
}
|
|
39
89
|
|
|
40
90
|
/** Palette name for a hex, or undefined when the hex is off-palette. */
|
package/src/footer.ts
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { truncateToWidth, visibleWidth } from "@earendil-works/pi-tui";
|
|
2
|
+
import { badge, contrastTextColor } from "./color.js";
|
|
3
|
+
import { safeTerminalLine } from "./text.js";
|
|
4
|
+
|
|
5
|
+
/** Uniform warning-cream background for the footer role badge (LEADER and WORKER alike). */
|
|
6
|
+
export const ROLE_BADGE_BACKGROUND = "#ffc85a";
|
|
7
|
+
/**
|
|
8
|
+
* Fraction of the terminal columns the roster badge may occupy. The built-in footer
|
|
9
|
+
* segments (cwd, git, model, context usage) take the rest; their width is not knowable
|
|
10
|
+
* from the extension API, so the budget scales with the window instead of a fixed reserve.
|
|
11
|
+
*/
|
|
12
|
+
export const FOOTER_WIDTH_FRACTION = 0.55;
|
|
13
|
+
/** A short session id is the UUID prefix used by MANAGER-<id> naming and /lead matching. */
|
|
14
|
+
export const SHORT_ID_LENGTH = 8;
|
|
15
|
+
/** Column count assumed when the terminal does not report one (headless, piped output). */
|
|
16
|
+
export const DEFAULT_TERMINAL_COLUMNS = 80;
|
|
17
|
+
/** Below this a truncated name fragment carries no recognizable identity; drop to MINIMAL. */
|
|
18
|
+
const MIN_NAME_BUDGET = 8;
|
|
19
|
+
/** The ` ● ` prefix and trailing space around a non-empty name badge. */
|
|
20
|
+
const NAME_BADGE_OVERHEAD = 4;
|
|
21
|
+
|
|
22
|
+
export interface FooterBadgeInput {
|
|
23
|
+
name?: string;
|
|
24
|
+
sessionId: string;
|
|
25
|
+
color: string;
|
|
26
|
+
role: "LEADER" | "WORKER";
|
|
27
|
+
columns: number;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Build the roster footer badge, degrading through three width tiers:
|
|
32
|
+
* FULL (name + full id + role), COMPACT (name truncated with an ellipsis + 8-column
|
|
33
|
+
* short id + role), and MINIMAL (color dot + role). The role badge always survives:
|
|
34
|
+
* telling LEADER from WORKER at a glance is the badge's primary purpose. The short id
|
|
35
|
+
* is a first-class identifier (MANAGER-<prefix> naming and /lead prefix matching), so
|
|
36
|
+
* unlike a truncated name it carries no ellipsis.
|
|
37
|
+
*/
|
|
38
|
+
export function buildRosterBadge(input: FooterBadgeInput): string {
|
|
39
|
+
const name = input.name ? safeTerminalLine(input.name) : "";
|
|
40
|
+
const sessionId = safeTerminalLine(input.sessionId);
|
|
41
|
+
const roleBadge = badge(` ${input.role} `, ROLE_BADGE_BACKGROUND, "#000000");
|
|
42
|
+
const nameBadge = (value: string) =>
|
|
43
|
+
badge(value ? ` ● ${value} ` : " ● ", input.color, contrastTextColor(input.color));
|
|
44
|
+
const available = Math.max(0, Math.floor(input.columns * FOOTER_WIDTH_FRACTION));
|
|
45
|
+
|
|
46
|
+
// FULL: the complete identifier set, unchanged from the original single-mode badge.
|
|
47
|
+
const full = `${nameBadge(name)}${badge(` ${sessionId} `, "#ffffff", "#000000")}${roleBadge}`;
|
|
48
|
+
if (visibleWidth(full) <= available) return full;
|
|
49
|
+
|
|
50
|
+
// COMPACT: shorten the id first (it is recoverable through /lead prefix matching),
|
|
51
|
+
// then give the name whatever budget remains.
|
|
52
|
+
const shortId = [...sessionId].slice(0, SHORT_ID_LENGTH).join("");
|
|
53
|
+
const shortIdBadge = badge(` ${shortId} `, "#ffffff", "#000000");
|
|
54
|
+
const nameBudget = available - visibleWidth(roleBadge) - visibleWidth(shortIdBadge) - NAME_BADGE_OVERHEAD;
|
|
55
|
+
if (nameBudget >= MIN_NAME_BUDGET) {
|
|
56
|
+
const truncated = visibleWidth(name) > nameBudget ? truncateToWidth(name, nameBudget, "…") : name;
|
|
57
|
+
return `${nameBadge(truncated)}${shortIdBadge}${roleBadge}`;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// MINIMAL: the swarm-colored dot is the identity; the full name and id stay
|
|
61
|
+
// available through the /swarm menu and message envelopes.
|
|
62
|
+
return `${nameBadge("")}${roleBadge}`;
|
|
63
|
+
}
|