|
| 1 | +/** Parse Pi system prompt into structured components for Cursor's RequestContext.rules. */ |
| 2 | + |
| 3 | +export interface PiContextFile { |
| 4 | + path: string; |
| 5 | + content: string; |
| 6 | +} |
| 7 | + |
| 8 | +export interface PiSkillRef { |
| 9 | + name: string; |
| 10 | + description: string; |
| 11 | + location: string; |
| 12 | +} |
| 13 | + |
| 14 | +export interface ParsedPiContext { |
| 15 | + contextFiles: PiContextFile[]; |
| 16 | + skills: PiSkillRef[]; |
| 17 | + cleanedPrompt: string; |
| 18 | +} |
| 19 | + |
| 20 | +// Stable structural markers only — no description text that may change across Pi versions. |
| 21 | +const CONTEXT_HEADING = "# Project Context"; |
| 22 | +const SKILLS_OPEN = "<available_skills>"; |
| 23 | +const SKILLS_CLOSE = "</available_skills>"; |
| 24 | + |
| 25 | +const SKILL_RE = |
| 26 | + /<skill>\s*<name>([\s\S]*?)<\/name>\s*<description>([\s\S]*?)<\/description>\s*<location>([\s\S]*?)<\/location>\s*<\/skill>/g; |
| 27 | + |
| 28 | +function unescapeXml(s: string): string { |
| 29 | + return s |
| 30 | + .replace(/&/g, "&") |
| 31 | + .replace(/</g, "<") |
| 32 | + .replace(/>/g, ">") |
| 33 | + .replace(/"/g, '"') |
| 34 | + .replace(/'/g, "'"); |
| 35 | +} |
| 36 | + |
| 37 | +function extractContextFiles(prompt: string): PiContextFile[] { |
| 38 | + const start = prompt.indexOf(CONTEXT_HEADING); |
| 39 | + if (start === -1) return []; |
| 40 | + |
| 41 | + let end = prompt.length; |
| 42 | + for (const marker of [SKILLS_OPEN, "\nCurrent date: "]) { |
| 43 | + const idx = prompt.indexOf(marker, start); |
| 44 | + if (idx !== -1 && idx < end) end = idx; |
| 45 | + } |
| 46 | + |
| 47 | + return prompt |
| 48 | + .slice(start, end) |
| 49 | + .split(/^(?=## \/)/m) |
| 50 | + .slice(1) |
| 51 | + .flatMap((block) => { |
| 52 | + const nl = block.indexOf("\n"); |
| 53 | + if (nl === -1) return []; |
| 54 | + const path = block.slice(3, nl).trim(); |
| 55 | + const content = block.slice(nl + 1).trim(); |
| 56 | + return path && content ? [{ path, content }] : []; |
| 57 | + }); |
| 58 | +} |
| 59 | + |
| 60 | +function extractSkills(prompt: string): PiSkillRef[] { |
| 61 | + const openIdx = prompt.indexOf(SKILLS_OPEN); |
| 62 | + if (openIdx === -1) return []; |
| 63 | + |
| 64 | + const closeIdx = prompt.indexOf(SKILLS_CLOSE, openIdx); |
| 65 | + if (closeIdx === -1) return []; |
| 66 | + |
| 67 | + const xml = prompt.slice(openIdx, closeIdx + SKILLS_CLOSE.length); |
| 68 | + const skills: PiSkillRef[] = []; |
| 69 | + |
| 70 | + for (const m of xml.matchAll(SKILL_RE)) { |
| 71 | + if (!m[1] || !m[2] || !m[3]) continue; |
| 72 | + const name = unescapeXml(m[1].trim()); |
| 73 | + const description = unescapeXml(m[2].trim()); |
| 74 | + const location = unescapeXml(m[3].trim()); |
| 75 | + if (name && description && location) { |
| 76 | + skills.push({ name, description, location }); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + return skills; |
| 81 | +} |
| 82 | + |
| 83 | +const PRESERVED_PATTERNS = [ |
| 84 | + /^Pi documentation[^\n]*(?:\n- [^\n]*)*/m, |
| 85 | + /^Current date: .+$/m, |
| 86 | + /^Current working directory: .+$/m, |
| 87 | +]; |
| 88 | + |
| 89 | +function buildCleanedPrompt(original: string, hasExtracted: boolean): string { |
| 90 | + const lines = PRESERVED_PATTERNS.map((re) => original.match(re)?.[0]).filter( |
| 91 | + (s): s is string => s != null, |
| 92 | + ); |
| 93 | + |
| 94 | + return lines.length === 0 && !hasExtracted ? original : lines.join("\n"); |
| 95 | +} |
| 96 | + |
| 97 | +export function parsePiSystemPrompt(systemPrompt: string): ParsedPiContext { |
| 98 | + if (!systemPrompt) { |
| 99 | + return { contextFiles: [], skills: [], cleanedPrompt: "" }; |
| 100 | + } |
| 101 | + |
| 102 | + try { |
| 103 | + const contextFiles = extractContextFiles(systemPrompt); |
| 104 | + const skills = extractSkills(systemPrompt); |
| 105 | + const hasExtracted = contextFiles.length > 0 || skills.length > 0; |
| 106 | + |
| 107 | + return { |
| 108 | + contextFiles, |
| 109 | + skills, |
| 110 | + cleanedPrompt: buildCleanedPrompt(systemPrompt, hasExtracted), |
| 111 | + }; |
| 112 | + } catch { |
| 113 | + return { contextFiles: [], skills: [], cleanedPrompt: systemPrompt }; |
| 114 | + } |
| 115 | +} |
0 commit comments