-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsession-namer.ts
More file actions
266 lines (216 loc) · 8.75 KB
/
session-namer.ts
File metadata and controls
266 lines (216 loc) · 8.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/**
* Session Namer
*
* Auto-generates a short descriptive session name using Haiku after the
* first user request. Re-generates on compaction or via /session-name-refresh.
* Appends a mode emoji (📋 plan, 🧠 ask) based on the most recent non-agent mode.
*
* All AI calls run in the background — never blocks the agent loop.
*/
import { complete, getModel } from "@mariozechner/pi-ai";
import type { ExtensionAPI, ExtensionContext } from "@mariozechner/pi-coding-agent";
import { sendControlMessage } from "../lib/control-channel.ts";
// ── Constants ────────────────────────────────────────────────────────────
const HAIKU = getModel("anthropic", "claude-haiku-4-5");
const NAME_PROMPT = `Generate a short descriptive name (3-6 words) for this coding session based on the conversation below.
Rules:
- Capture the primary task or topic
- Use a concise phrase (not a full sentence)
- No quotes, no punctuation at the end
- Lowercase start unless a proper noun
Conversation:
`;
/** Max chars of conversation context sent to Haiku. */
const MAX_CONTEXT_CHARS = 2000;
/** Matches paired XML-style tags with content (e.g. `<skill>…</skill>`). */
const TAG_BLOCK_RE = /<\/?[a-z_-]+(?:\s[^>]*)?>[\s\S]*?<\/[a-z_-]+>/gi;
/** Matches lone/unpaired XML-style tags. */
const LONE_TAG_RE = /<\/?[a-z_-]+(?:\s[^>]*)?>/gi;
/** Matches trailing mode emoji suffixes on persisted names. */
const EMOJI_SUFFIX_RE = /\s*[📋🧠]+\s*$/;
const PLAN_ASK_ENTRY_TYPE = "plan-ask-mode-state";
/**
* Custom entry type that pins the session name.
* Any extension can write `pi.appendEntry("session-name-pin", { name })` to
* claim the session name. Session-namer will not overwrite a pinned name.
*/
const SESSION_NAME_PIN_TYPE = "session-name-pin";
type TrackedMode = "plan" | "ask";
const MODE_EMOJI: Record<TrackedMode, string> = {
plan: "📋",
ask: "🧠",
};
// ── Helpers ──────────────────────────────────────────────────────────────
/** Remove XML tags leaked from skill expansion and collapse whitespace. */
function stripTags(text: string): string {
return text.replace(TAG_BLOCK_RE, " ").replace(LONE_TAG_RE, " ").replace(/\s{2,}/g, " ").trim();
}
/** Extract plain text from a message content field. */
function extractText(content: string | Array<{ type: string; text?: string }>): string {
if (typeof content === "string") return content;
return content
.filter((b) => b.type === "text")
.map((b) => b.text ?? "")
.join("\n");
}
/** Check whether any extension has pinned the session name. */
function hasPinnedName(ctx: ExtensionContext): boolean {
return ctx.sessionManager.getEntries().some((entry) => {
const e = entry as { type: string; customType?: string };
return e.type === "custom" && e.customType === SESSION_NAME_PIN_TYPE;
});
}
/** Count user messages on the current branch. */
function countUserTurns(ctx: ExtensionContext): number {
return ctx.sessionManager.getBranch()
.filter((entry) => entry.type === "message" && entry.message.role === "user")
.length;
}
// ── Extension ────────────────────────────────────────────────────────────
/** Session Namer extension — auto-names sessions via Haiku. */
export default function sessionNamerExtension(pi: ExtensionAPI): void {
let userTurnCount = 0;
let generating = false;
let lastNonAgentMode: TrackedMode | null = null;
let baseName: string | null = null;
let pinned = false;
/** Reset all state to initial values. */
function resetState(): void {
userTurnCount = 0;
generating = false;
lastNonAgentMode = null;
baseName = null;
pinned = false;
}
/** Restore name, mode, and turn count from a (re)loaded session. */
function restoreFromSession(ctx: ExtensionContext): void {
syncModeFromSession(ctx);
pinned = hasPinnedName(ctx);
const existing = pi.getSessionName();
if (existing) {
const stripped = existing.replace(EMOJI_SUFFIX_RE, "").trim();
if (stripped) baseName = stripped;
}
userTurnCount = countUserTurns(ctx);
}
/** Scan session entries for plan-ask mode state. */
function syncModeFromSession(ctx: ExtensionContext): void {
const modeEntries = ctx.sessionManager.getEntries()
.filter((entry) => {
const e = entry as { type: string; customType?: string };
return e.type === "custom" && e.customType === PLAN_ASK_ENTRY_TYPE;
}) as Array<{ data?: { mode?: string } }>;
const last = modeEntries.at(-1);
const mode = last?.data?.mode;
if (mode === "plan" || mode === "ask") lastNonAgentMode = mode;
}
/** Apply the current name (base + mode emoji) to the session. Skips if pinned. */
function applyName(): void {
if (!baseName || pinned) return;
const name = lastNonAgentMode ? `${baseName} ${MODE_EMOJI[lastNonAgentMode]}` : baseName;
pi.setSessionName(name);
sendControlMessage({ type: "session_name", name: baseName });
}
/** Collect conversation text from the current branch, stripped and truncated. */
function collectContext(ctx: ExtensionContext): string {
const parts: string[] = [];
let chars = 0;
for (const entry of ctx.sessionManager.getBranch()) {
if (entry.type !== "message") continue;
const { role } = entry.message;
if (role !== "user" && role !== "assistant") continue;
const text = extractText(entry.message.content);
if (!text) continue;
const remaining = MAX_CONTEXT_CHARS - chars;
if (remaining <= 0) break;
const chunk = text.slice(0, remaining);
parts.push(`${role}: ${chunk}`);
chars += chunk.length;
}
return stripTags(parts.join("\n\n"));
}
/** Call Haiku to generate a session name from context text. */
async function callHaiku(contextText: string, ctx: ExtensionContext): Promise<string | undefined> {
if (!HAIKU) return undefined;
const auth = await ctx.modelRegistry.getApiKeyAndHeaders(HAIKU);
if (!auth.ok) return undefined;
const response = await complete(HAIKU, {
messages: [{
role: "user" as const,
content: [{ type: "text" as const, text: NAME_PROMPT + contextText }],
timestamp: Date.now(),
}],
}, { apiKey: auth.apiKey, headers: auth.headers });
const text = response.content
.filter((c): c is { type: "text"; text: string } => c.type === "text")
.map((c) => c.text)
.join("")
.trim()
.replace(/^["']|["']$/g, "")
.split("\n")[0]
?.trim();
return text || undefined;
}
/**
* Fire-and-forget name generation.
* Silently catches errors — naming is best-effort.
*/
function generateInBackground(ctx: ExtensionContext, contextOverride?: string): void {
if (generating || pinned) return;
generating = true;
const contextText = stripTags(contextOverride ?? collectContext(ctx));
if (!contextText) {
generating = false;
return;
}
callHaiku(contextText, ctx)
.then((name) => {
if (name) {
baseName = name;
applyName();
}
})
.catch(() => {})
.finally(() => { generating = false; });
}
// ── Events ───────────────────────────────────────────────────────
pi.on("session_start", async (event, ctx) => {
resetState();
// For a brand-new session there's nothing to restore yet; for all other
// reasons (startup/reload/resume/fork) the session file already carries state.
if (event.reason === "new") return;
restoreFromSession(ctx);
if (baseName) applyName();
});
pi.on("before_agent_start", async (event, ctx) => {
userTurnCount++;
if (userTurnCount === 1 && event.prompt) {
generateInBackground(ctx, `user: ${event.prompt}`);
}
});
pi.on("agent_end", async (_event, ctx) => {
syncModeFromSession(ctx);
if (baseName) applyName();
});
pi.on("session_compact", async (_event, ctx) => {
const compaction = ctx.sessionManager.getBranch()
.filter((e) => e.type === "compaction" && "summary" in e)
.pop() as { summary: string } | undefined;
if (compaction?.summary) {
generateInBackground(ctx, compaction.summary);
}
});
// session_switch folded into session_start above (pi 0.65+).
// ── Command ──────────────────────────────────────────────────────
pi.registerCommand("session-name-refresh", {
description: "Re-generate the session name using AI",
handler: async (_args, ctx) => {
if (generating) {
ctx.ui.notify("Name generation already in progress", "info");
return;
}
ctx.ui.notify("Regenerating session name…", "info");
generateInBackground(ctx);
},
});
}