/** * Auto session title extension. * * Uses the current model via a direct `complete()` call to generate a session * title from the first user prompt, without spawning a nested pi agent session. * * Usage: * - Copy to ~/.pi/agent/extensions/ or .pi/extensions/ * - Start a new session and send the first prompt */ import { complete } from "@mariozechner/pi-ai"; import type { ExtensionAPI, SessionEntry } from "@mariozechner/pi-coding-agent"; const TITLE_SYSTEM_PROMPT = `Generate a short session title for a coding-agent conversation. Rules: - Output only the title - No quotes - No markdown - No trailing punctuation - Prefer 3 to 7 words - Be specific to the user's task`; function getPriorUserMessageCount(branch: SessionEntry[]): number { return branch.filter((entry) => entry.type === "message" && entry.message.role === "user").length; } function normalizePrompt(prompt: string): string { return prompt.replace(/\s+/g, " ").trim().slice(0, 2_000); } function normalizeTitle(title: string): string { return title.replace(/^['"`]+|['"`]+$/g, "").replace(/\s+/g, " ").trim().slice(0, 80); } export default function (pi: ExtensionAPI) { let isGeneratingTitle = false; // Files this extension instance has already observed a turn for. Used to // detect "first turn since this session was opened in this process," which // (combined with header.parentSession) lets fresh forks auto-title from // their next prompt even though they inherit prior user messages from the // parent. Resumed sessions opened in this process see their first turn too, // but only fresh forks have header.parentSession set without a name yet, // so the combined check stays narrow. const seenSessionFiles = new Set(); pi.on("before_agent_start", async (event, ctx) => { if (isGeneratingTitle) return; if (pi.getSessionName()) return; if (!ctx.model) return; const sessionFile = ctx.sessionManager.getSessionFile(); const firstTurnThisProcess = sessionFile === undefined || !seenSessionFiles.has(sessionFile); if (sessionFile !== undefined) { seenSessionFiles.add(sessionFile); } const priorUserMessages = getPriorUserMessageCount(ctx.sessionManager.getBranch()); if (priorUserMessages > 0) { // Allow fresh forks (header.parentSession set) on their very first // turn since the session was opened. This covers wings' mid-chain // forks, which carry ancestor user messages but should still be // titled from the user's first post-fork prompt. const header = ctx.sessionManager.getHeader(); const isFreshFork = !!header?.parentSession && firstTurnThisProcess; if (!isFreshFork) return; } const prompt = normalizePrompt(event.prompt); if (!prompt) return; const auth = await ctx.modelRegistry.getApiKeyAndHeaders(ctx.model); if (!auth.ok || !auth.apiKey) return; isGeneratingTitle = true; try { const response = await complete( ctx.model, { systemPrompt: TITLE_SYSTEM_PROMPT, messages: [ { role: "user", content: [{ type: "text", text: prompt }], timestamp: Date.now(), }, ], }, { apiKey: auth.apiKey, headers: auth.headers, reasoningEffort: "none", signal: ctx.signal, }, ); if (response.stopReason === "aborted") { return; } const title = normalizeTitle( response.content .filter((part): part is { type: "text"; text: string } => part.type === "text") .map((part) => part.text) .join(" "), ); if (title) { pi.setSessionName(title); } } catch { // Title generation is best-effort and must not block the main turn. } finally { isGeneratingTitle = false; } }); }