pi-agent-swarm 0.2.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 +5 -1
- package/dist/index.ts +230 -10
- package/dist/index.ts.map +4 -4
- package/package.json +1 -1
- package/src/auto-name.ts +169 -0
- package/src/pi-agent-swarm.ts +6 -0
- package/src/settings.ts +22 -0
- package/src/swarm-controller.ts +107 -0
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/pi-agent-swarm.ts
CHANGED
|
@@ -32,6 +32,12 @@ export function createPiSwarmExtension(dependencies: PiSwarmDependencies = {}):
|
|
|
32
32
|
pi.on("session_shutdown", async (event, ctx) => {
|
|
33
33
|
await controller.sessionShutdown(event, ctx);
|
|
34
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
|
+
});
|
|
35
41
|
pi.on("input", async (event, ctx) => {
|
|
36
42
|
if (event.source !== "interactive") return;
|
|
37
43
|
void controller.relaySteerInput(event.text, ctx);
|
package/src/settings.ts
CHANGED
|
@@ -3,11 +3,13 @@ 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";
|
|
6
7
|
import type { SwarmColorPaletteName } from "./color.js";
|
|
7
8
|
|
|
8
9
|
export const SWARM_SETTINGS_FILE = "pi-agent-swarm.json";
|
|
9
10
|
export const MAX_SWARM_SETTINGS_BYTES = 64 * 1024;
|
|
10
11
|
export const DEFAULT_SWARM_SETTINGS: Readonly<SwarmSettings> = Object.freeze({
|
|
12
|
+
autoName: true,
|
|
11
13
|
colorPalette: "muted",
|
|
12
14
|
confirmSessionLaunch: true,
|
|
13
15
|
externalCommand: "alacritty -e",
|
|
@@ -15,6 +17,8 @@ export const DEFAULT_SWARM_SETTINGS: Readonly<SwarmSettings> = Object.freeze({
|
|
|
15
17
|
});
|
|
16
18
|
|
|
17
19
|
export interface SwarmSettings {
|
|
20
|
+
autoName: boolean;
|
|
21
|
+
autoNameModel?: string;
|
|
18
22
|
colorPalette: SwarmColorPaletteName;
|
|
19
23
|
confirmSessionLaunch: boolean;
|
|
20
24
|
externalCommand: string;
|
|
@@ -76,6 +80,8 @@ export interface SwarmSettingsRuntimeOptions {
|
|
|
76
80
|
}
|
|
77
81
|
|
|
78
82
|
const SETTING_FIELDS = [
|
|
83
|
+
"autoName",
|
|
84
|
+
"autoNameModel",
|
|
79
85
|
"colorPalette",
|
|
80
86
|
"confirmSessionLaunch",
|
|
81
87
|
"externalCommand",
|
|
@@ -92,6 +98,18 @@ export function normalizeSwarmSettingsDocument(value: unknown): NormalizedSwarmS
|
|
|
92
98
|
const settings: SwarmSettings = { ...DEFAULT_SWARM_SETTINGS };
|
|
93
99
|
const sources = builtInSources();
|
|
94
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
|
+
}
|
|
95
113
|
if (Object.hasOwn(value, "colorPalette")) {
|
|
96
114
|
if (value.colorPalette !== "muted" && value.colorPalette !== "bright") return undefined;
|
|
97
115
|
settings.colorPalette = value.colorPalette;
|
|
@@ -166,6 +184,8 @@ export function createInMemorySwarmSettingsRuntime(): SwarmSettingsRuntime {
|
|
|
166
184
|
settings: { ...state.settings, ...canonical },
|
|
167
185
|
sources: {
|
|
168
186
|
...state.sources,
|
|
187
|
+
...(canonical.autoName !== undefined ? { autoName: "user" as const } : {}),
|
|
188
|
+
...(canonical.autoNameModel !== undefined ? { autoNameModel: "user" as const } : {}),
|
|
169
189
|
...(canonical.colorPalette !== undefined ? { colorPalette: "user" as const } : {}),
|
|
170
190
|
...(canonical.confirmSessionLaunch !== undefined ? { confirmSessionLaunch: "user" as const } : {}),
|
|
171
191
|
...(canonical.externalCommand ? { externalCommand: "user" as const } : {}),
|
|
@@ -341,6 +361,8 @@ function invalidLoad(path: string, reason: string): SwarmSettingsLoadResult {
|
|
|
341
361
|
|
|
342
362
|
function builtInSources(): Record<SwarmSettingsField, SwarmSettingsSource> {
|
|
343
363
|
return {
|
|
364
|
+
autoName: "built-in",
|
|
365
|
+
autoNameModel: "built-in",
|
|
344
366
|
colorPalette: "built-in",
|
|
345
367
|
confirmSessionLaunch: "built-in",
|
|
346
368
|
externalCommand: "built-in",
|
package/src/swarm-controller.ts
CHANGED
|
@@ -1,12 +1,23 @@
|
|
|
1
1
|
import { randomBytes } from "node:crypto";
|
|
2
2
|
import { readFile, realpath, rename, rm, stat, writeFile } from "node:fs/promises";
|
|
3
3
|
import { join, resolve } from "node:path";
|
|
4
|
+
import type { Api, Model } from "@earendil-works/pi-ai";
|
|
4
5
|
import type {
|
|
5
6
|
ExtensionAPI,
|
|
6
7
|
ExtensionContext,
|
|
7
8
|
SessionShutdownEvent,
|
|
8
9
|
SessionStartEvent,
|
|
9
10
|
} from "@earendil-works/pi-coding-agent";
|
|
11
|
+
import {
|
|
12
|
+
AUTO_NAME_TIMEOUT_MS,
|
|
13
|
+
AutoNamer,
|
|
14
|
+
buildAutoNamePrompt,
|
|
15
|
+
collectAutoNameMessages,
|
|
16
|
+
completeSessionTitle as defaultCompleteSessionTitle,
|
|
17
|
+
isDefaultManagerName,
|
|
18
|
+
resolveAutoNameModel,
|
|
19
|
+
toAutoName,
|
|
20
|
+
} from "./auto-name.js";
|
|
10
21
|
import {
|
|
11
22
|
normalizeSwarmColor,
|
|
12
23
|
pickSwarmColor,
|
|
@@ -104,6 +115,13 @@ export interface SwarmControllerDependencies {
|
|
|
104
115
|
/** Visible terminal columns, or undefined when not attached to a TTY. */
|
|
105
116
|
terminalColumns?(): number | undefined;
|
|
106
117
|
runtimeBaseDirectory?: string;
|
|
118
|
+
/** One-shot title completion for session auto-naming; tests substitute a stub. */
|
|
119
|
+
completeSessionTitle?(
|
|
120
|
+
ctx: ExtensionContext,
|
|
121
|
+
model: Model<Api>,
|
|
122
|
+
prompt: string,
|
|
123
|
+
signal?: AbortSignal,
|
|
124
|
+
): Promise<string>;
|
|
107
125
|
}
|
|
108
126
|
|
|
109
127
|
export interface SwarmSnapshot {
|
|
@@ -178,6 +196,13 @@ export class SwarmController {
|
|
|
178
196
|
private leadSessionId: string | undefined;
|
|
179
197
|
/** A human-started session is the default lead of its own (not yet started) group. */
|
|
180
198
|
private defaultLead = false;
|
|
199
|
+
private readonly autoNamer = new AutoNamer();
|
|
200
|
+
/** Auto-naming applies only to human-started sessions, never to spawned children. */
|
|
201
|
+
private autoNameEligible = false;
|
|
202
|
+
/** Set while the auto-namer applies its own name so the change is not read as manual. */
|
|
203
|
+
private applyingAutoName: string | undefined;
|
|
204
|
+
|
|
205
|
+
private autoNameInFlight = false;
|
|
181
206
|
private leadWatcher: ReturnType<typeof setInterval> | undefined;
|
|
182
207
|
private readonly resizeListener = () => this.onTerminalResize();
|
|
183
208
|
private resizeWatching = false;
|
|
@@ -202,6 +227,10 @@ export class SwarmController {
|
|
|
202
227
|
this.parentSessionId = undefined;
|
|
203
228
|
this.leadSessionId = undefined;
|
|
204
229
|
this.defaultLead = false;
|
|
230
|
+
this.autoNamer.reset();
|
|
231
|
+
this.autoNameEligible = false;
|
|
232
|
+
this.applyingAutoName = undefined;
|
|
233
|
+
this.autoNameInFlight = false;
|
|
205
234
|
this.pendingKickoffIds.clear();
|
|
206
235
|
this.relayChain = Promise.resolve();
|
|
207
236
|
this.startLeadWatcher();
|
|
@@ -233,6 +262,7 @@ export class SwarmController {
|
|
|
233
262
|
this.pi.setSessionName(`MANAGER-${ctx.sessionManager.getSessionId().slice(0, 8)}`);
|
|
234
263
|
}
|
|
235
264
|
this.defaultLead = true;
|
|
265
|
+
this.autoNameEligible = true;
|
|
236
266
|
await this.renderFooterStatus(ctx);
|
|
237
267
|
return;
|
|
238
268
|
}
|
|
@@ -1162,6 +1192,83 @@ export class SwarmController {
|
|
|
1162
1192
|
await this.renderFooterStatus(ctx);
|
|
1163
1193
|
}
|
|
1164
1194
|
|
|
1195
|
+
/**
|
|
1196
|
+
* Count completed turns and start the one-shot auto-name attempt for a
|
|
1197
|
+
* human-started session. The attempt runs in the background as an owned task
|
|
1198
|
+
* so shutdown waits for it; it never blocks the turn_end event path. The
|
|
1199
|
+
* returned promise resolves when the attempt settles (tests await it).
|
|
1200
|
+
*/
|
|
1201
|
+
noteTurnEnd(ctx: ExtensionContext): Promise<void> {
|
|
1202
|
+
if (!this.isCurrent(ctx)) return Promise.resolve();
|
|
1203
|
+
if (!this.autoNameEligible) return Promise.resolve();
|
|
1204
|
+
if (this.autoNameInFlight) return Promise.resolve();
|
|
1205
|
+
if (!this.autoNamer.noteTurn(this.pi.getSessionName(), this.settings.get().settings.autoName)) {
|
|
1206
|
+
return Promise.resolve();
|
|
1207
|
+
}
|
|
1208
|
+
this.autoNameInFlight = true;
|
|
1209
|
+
const task = this.runAutoName(ctx, ctx.sessionManager, this.generation).finally(() => {
|
|
1210
|
+
this.autoNameInFlight = false;
|
|
1211
|
+
});
|
|
1212
|
+
return this.track(task);
|
|
1213
|
+
}
|
|
1214
|
+
|
|
1215
|
+
/**
|
|
1216
|
+
* Keep the roster peer name and the footer badge in sync when the session
|
|
1217
|
+
* name changes outside the extension (for example `/name`), and treat a
|
|
1218
|
+
* custom name as the user taking over so auto-naming steps aside.
|
|
1219
|
+
*/
|
|
1220
|
+
async noteSessionInfoChanged(name: string | undefined, ctx: ExtensionContext): Promise<void> {
|
|
1221
|
+
if (!this.isCurrent(ctx)) return;
|
|
1222
|
+
if (name === undefined || name !== this.applyingAutoName) this.autoNamer.noteExternalName(name);
|
|
1223
|
+
if (name) this.membership?.transport.setName(name);
|
|
1224
|
+
await this.renderFooterStatus(ctx);
|
|
1225
|
+
}
|
|
1226
|
+
|
|
1227
|
+
/**
|
|
1228
|
+
* Best-effort naming: summarize the first turns into `MAN-<title>` with the
|
|
1229
|
+
* current (or configured) model. Any failure keeps the default MANAGER-<id>
|
|
1230
|
+
* name, is recorded as a session entry for diagnosis, and is retried on a
|
|
1231
|
+
* later turn until the attempt budget runs out. The session name is
|
|
1232
|
+
* re-checked right before applying so a rename that happened during the
|
|
1233
|
+
* completion always wins.
|
|
1234
|
+
*/
|
|
1235
|
+
private async runAutoName(ctx: ExtensionContext, owner: object, ownerGeneration: number): Promise<void> {
|
|
1236
|
+
try {
|
|
1237
|
+
const configured = this.settings.get().settings.autoNameModel;
|
|
1238
|
+
const model = resolveAutoNameModel(ctx, configured);
|
|
1239
|
+
if (!model) {
|
|
1240
|
+
if (configured) {
|
|
1241
|
+
this.notify(
|
|
1242
|
+
ctx,
|
|
1243
|
+
`Pi Agent Swarm could not resolve autoNameModel "${configured}"; keeping the default session name.`,
|
|
1244
|
+
"warning",
|
|
1245
|
+
);
|
|
1246
|
+
}
|
|
1247
|
+
return;
|
|
1248
|
+
}
|
|
1249
|
+
const messages = collectAutoNameMessages(ctx);
|
|
1250
|
+
if (messages.length === 0) return;
|
|
1251
|
+
const signal = AbortSignal.any([this.controller.signal, AbortSignal.timeout(AUTO_NAME_TIMEOUT_MS)]);
|
|
1252
|
+
const complete = this.deps.completeSessionTitle ?? defaultCompleteSessionTitle;
|
|
1253
|
+
const raw = await complete(ctx, model, buildAutoNamePrompt(messages), signal);
|
|
1254
|
+
if (!this.isCurrent(owner, ownerGeneration)) return;
|
|
1255
|
+
const name = toAutoName(raw);
|
|
1256
|
+
if (!name) return;
|
|
1257
|
+
const currentName = this.pi.getSessionName();
|
|
1258
|
+
if (currentName !== undefined && !isDefaultManagerName(currentName)) return;
|
|
1259
|
+
this.applyingAutoName = name;
|
|
1260
|
+
try {
|
|
1261
|
+
await this.setOwnName(ctx, name, signal);
|
|
1262
|
+
} finally {
|
|
1263
|
+
this.applyingAutoName = undefined;
|
|
1264
|
+
}
|
|
1265
|
+
} catch (error) {
|
|
1266
|
+
// Best-effort: any failure keeps the default MANAGER-<id> name and is
|
|
1267
|
+
// retried on a later turn; record it for offline diagnosis only.
|
|
1268
|
+
this.pi.appendEntry("pi-swarm-auto-name", { ok: false, error: safeTerminalLine(safeError(error)) });
|
|
1269
|
+
}
|
|
1270
|
+
}
|
|
1271
|
+
|
|
1165
1272
|
/**
|
|
1166
1273
|
* Best-effort, ordered relay of a direct user steer to the parent session so it
|
|
1167
1274
|
* stays aligned. This returns immediately: sends are chained so ordering is
|