import { basename } from "node:path"; import type { ExtensionCommandContext } from "@earendil-works/pi-coding-agent"; import { skillPromptTokens, textTokens } from "../estimate"; import { compactList } from "../format"; import type { Line, Section } from "../types"; /** System prompt breakdown from the structured prompt options. */ export const systemPromptSection = (ctx: ExtensionCommandContext): Section => { const opts = ctx.getSystemPromptOptions(); const fullText = ctx.getSystemPrompt() ?? ""; const fullTokens = textTokens(fullText); const lines: Line[] = []; const custom = textTokens(opts?.customPrompt); if (custom > 0) lines.push({ label: "custom prompt", tokens: custom }); const guidelines = textTokens(opts?.promptGuidelines?.join("\n")); if (guidelines > 0) lines.push({ label: "guidelines", tokens: guidelines }); const snippets = textTokens( opts?.toolSnippets ? Object.values(opts.toolSnippets).join("\n") : undefined, ); if (snippets > 0) lines.push({ label: "tool snippets", tokens: snippets }); const appended = textTokens(opts?.appendSystemPrompt); if (appended > 0) lines.push({ label: "appended prompt", tokens: appended }); const files = opts?.contextFiles ?? []; const fileLines = files.map((f) => ({ label: basename(f.path), tokens: textTokens(f.content) })); if (files.length > 0) { lines.push({ label: `context files (${files.length})`, tokens: fileLines.reduce((a, b) => a + b.tokens, 0), detail: compactList(fileLines), }); } const skills = opts?.skills ?? []; const skillLines = skills.map((s) => ({ label: s.name, tokens: skillPromptTokens(s.name, s.description, s.filePath), })); if (skills.length > 0) { lines.push({ label: `skills (${skills.length})`, tokens: skillLines.reduce((a, b) => a + b.tokens, 0), detail: compactList(skillLines), }); } const partsSum = lines.reduce((a, b) => a + b.tokens, 0); const rest = Math.max(0, fullTokens - partsSum); if (rest > 0) lines.push({ label: "base prompt / other", tokens: rest }); return { title: "System prompt", total: Math.max(fullTokens, partsSum), lines, }; };