Download packages/coding-agent/src/core/system-prompt.ts from SaylorTwift/pi: direct link, hf CLI and curl.
- Browser
- Download file 6.28 kB
-
https://huggingface.co/SaylorTwift/pi/resolve/main/packages/coding-agent/src/core/system-prompt.ts
- Command line
-
hf download hf://SaylorTwift/pi/packages/coding-agent/src/core/system-prompt.ts
-
curl -L -o system-prompt.ts https://huggingface.co/SaylorTwift/pi/resolve/main/packages/coding-agent/src/core/system-prompt.ts
6.28 kB
| /** | |
| * System prompt construction and project context loading | |
| */ | |
| import { getDocsPath, getExamplesPath, getReadmePath } from "../config.ts"; | |
| import { formatSkillsForPrompt, type Skill } from "./skills.ts"; | |
| export interface BuildSystemPromptOptions { | |
| /** Custom system prompt (replaces default). */ | |
| customPrompt?: string; | |
| /** Tools to include in prompt. Default: [read, bash, edit, write] */ | |
| selectedTools?: string[]; | |
| /** Optional one-line tool snippets keyed by tool name. */ | |
| toolSnippets?: Record<string, string>; | |
| /** Additional guideline bullets appended to the default system prompt guidelines. */ | |
| promptGuidelines?: string[]; | |
| /** Text to append to system prompt. */ | |
| appendSystemPrompt?: string; | |
| /** Working directory. */ | |
| cwd: string; | |
| /** Pre-loaded context files. */ | |
| contextFiles?: Array<{ path: string; content: string }>; | |
| /** Pre-loaded skills. */ | |
| skills?: Skill[]; | |
| } | |
| /** Build the system prompt with tools, guidelines, and context */ | |
| export function buildSystemPrompt(options: BuildSystemPromptOptions): string { | |
| const { | |
| customPrompt, | |
| selectedTools, | |
| toolSnippets, | |
| promptGuidelines, | |
| appendSystemPrompt, | |
| cwd, | |
| contextFiles: providedContextFiles, | |
| skills: providedSkills, | |
| } = options; | |
| const promptCwd = cwd.replace(/\\/g, "/"); | |
| const appendSection = appendSystemPrompt ? `\n\n${appendSystemPrompt}` : ""; | |
| const contextFiles = providedContextFiles ?? []; | |
| const skills = providedSkills ?? []; | |
| const tools = selectedTools || ["read", "bash", "edit", "write"]; | |
| const skillFileReadTool = (["read", "bash"] as const).find((tool) => tools.includes(tool)); | |
| if (customPrompt) { | |
| let prompt = customPrompt; | |
| if (appendSection) { | |
| prompt += appendSection; | |
| } | |
| // Append project context files | |
| if (contextFiles.length > 0) { | |
| prompt += "\n\n<project_context>\n\n"; | |
| prompt += "Project-specific instructions and guidelines:\n\n"; | |
| for (const { path: filePath, content } of contextFiles) { | |
| prompt += `<project_instructions path="${filePath}">\n${content}\n</project_instructions>\n\n`; | |
| } | |
| prompt += "</project_context>\n"; | |
| } | |
| // Append skills when a tool capable of reading their files is available. | |
| if (skillFileReadTool && skills.length > 0) { | |
| prompt += formatSkillsForPrompt(skills, skillFileReadTool); | |
| } | |
| prompt += `\nCurrent working directory: ${promptCwd}\n`; | |
| return prompt; | |
| } | |
| // Get absolute paths to documentation and examples | |
| const readmePath = getReadmePath(); | |
| const docsPath = getDocsPath(); | |
| const examplesPath = getExamplesPath(); | |
| // Build tools list based on selected tools. | |
| // A tool appears in Available tools only when the caller provides a one-line snippet. | |
| const visibleTools = tools.filter((name) => !!toolSnippets?.[name]); | |
| const toolsList = | |
| visibleTools.length > 0 ? visibleTools.map((name) => `- ${name}: ${toolSnippets![name]}`).join("\n") : "(none)"; | |
| // Build guidelines based on which tools are actually available | |
| const guidelinesList: string[] = []; | |
| const guidelinesSet = new Set<string>(); | |
| const addGuideline = (guideline: string): void => { | |
| if (guidelinesSet.has(guideline)) { | |
| return; | |
| } | |
| guidelinesSet.add(guideline); | |
| guidelinesList.push(guideline); | |
| }; | |
| const hasBash = tools.includes("bash"); | |
| const hasPowerShell = tools.includes("powershell"); | |
| const hasGrep = tools.includes("grep"); | |
| const hasFind = tools.includes("find"); | |
| const hasLs = tools.includes("ls"); | |
| // File exploration guidelines | |
| if ((hasBash || hasPowerShell) && !hasGrep && !hasFind && !hasLs) { | |
| if (hasBash && hasPowerShell) { | |
| addGuideline("Use bash or PowerShell for file operations like listing, searching, and finding files"); | |
| } else if (hasPowerShell) { | |
| addGuideline("Use PowerShell for file operations like listing, searching, and finding files"); | |
| } else { | |
| addGuideline("Use bash for file operations like ls, rg, find"); | |
| } | |
| } | |
| for (const guideline of promptGuidelines ?? []) { | |
| const normalized = guideline.trim(); | |
| if (normalized.length > 0) { | |
| addGuideline(normalized); | |
| } | |
| } | |
| // Always include these | |
| addGuideline("Be concise in your responses"); | |
| addGuideline("Show file paths clearly when working with files"); | |
| const guidelines = guidelinesList.map((g) => `- ${g}`).join("\n"); | |
| let prompt = `You are an expert coding assistant operating inside pi, a coding agent harness. You help users by reading files, executing commands, editing code, and writing new files. | |
| Available tools: | |
| ${toolsList} | |
| In addition to the tools above, you may have access to other custom tools depending on the project. | |
| Guidelines: | |
| ${guidelines} | |
| Pi documentation (read only when the user asks about pi itself, its SDK, extensions, themes, skills, or TUI): | |
| - Main documentation: ${readmePath} | |
| - Additional docs: ${docsPath} | |
| - Examples: ${examplesPath} (extensions, custom tools, SDK) | |
| - When reading pi docs or examples, resolve docs/... under Additional docs and examples/... under Examples, not the current working directory | |
| - When asked about: extensions (docs/extensions.md, examples/extensions/), themes (docs/themes.md), skills (docs/skills.md), prompt templates (docs/prompt-templates.md), TUI components (docs/tui.md), keybindings (docs/keybindings.md), SDK integrations (docs/sdk.md), custom providers (docs/custom-provider.md), adding models (docs/models.md), pi packages (docs/packages.md), environment variables (docs/environment-variables.md) | |
| - When working on pi topics, read the docs and examples, and follow .md cross-references before implementing | |
| - Always read pi .md files completely and follow links to related docs (e.g., tui.md for TUI API details)`; | |
| if (appendSection) { | |
| prompt += appendSection; | |
| } | |
| // Append project context files | |
| if (contextFiles.length > 0) { | |
| prompt += "\n\n<project_context>\n\n"; | |
| prompt += "Project-specific instructions and guidelines:\n\n"; | |
| for (const { path: filePath, content } of contextFiles) { | |
| prompt += `<project_instructions path="${filePath}">\n${content}\n</project_instructions>\n\n`; | |
| } | |
| prompt += "</project_context>\n"; | |
| } | |
| // Append skills when a tool capable of reading their files is available. | |
| if (skillFileReadTool && skills.length > 0) { | |
| prompt += formatSkillsForPrompt(skills, skillFileReadTool); | |
| } | |
| prompt += `\nCurrent working directory: ${promptCwd}`; | |
| return prompt; | |
| } | |