Skip to main content

pixie_pi/
prompt.rs

1//! System prompt builder for the coding agent.
2
3use std::path::Path;
4
5use crate::skills::Skills;
6
7/// Options for building the system prompt.
8pub struct PromptOptions<'a> {
9    pub cwd: &'a Path,
10    pub tool_names: &'a [String],
11    pub extra: Option<&'a str>,
12    /// Discovered skills, if any (surfaced by name + description only).
13    pub skills: Option<&'a Skills>,
14}
15
16/// Build the coding-assistant system prompt, including live environment info.
17pub fn build_system_prompt(opts: PromptOptions<'_>) -> String {
18    let platform = if cfg!(target_os = "macos") {
19        "macOS"
20    } else if cfg!(target_os = "linux") {
21        "Linux"
22    } else if cfg!(target_os = "windows") {
23        "Windows"
24    } else {
25        "Unix"
26    };
27    let shell = std::env::var("SHELL").unwrap_or_else(|_| {
28        if cfg!(windows) {
29            "cmd.exe".into()
30        } else {
31            "/bin/sh".into()
32        }
33    });
34    let date = chrono::Local::now().format("%Y-%m-%d").to_string();
35    let cwd_display = opts.cwd.to_string_lossy();
36
37    let tool_list = if opts.tool_names.is_empty() {
38        "(no tools available)".to_string()
39    } else {
40        opts.tool_names.join(", ")
41    };
42
43    let mut prompt = format!(
44        "You are pixie-pi, an expert software engineering agent operating in a terminal. \
45You help the user with coding tasks: reading, writing, and editing code, running commands, and searching the codebase. \
46You work autonomously and use your tools to accomplish the task, then report the outcome concisely.
47
48# Working directory
49{cwd_display}
50
51# Environment
52- Platform: {platform}
53- Shell: {shell}
54- Date: {date}
55- Tools: {tool_list}
56
57# How to work
58- Prefer the smallest correct change. Use `edit` for precise changes to existing files; use `write` only for new files or complete rewrites.
59- Read a file before editing it so your oldText matches exactly (including indentation and whitespace).
60- `edit` applies all entries in `edits[]` against the ORIGINAL file, not incrementally. Each `oldText` must be unique and non-overlapping. Merge nearby changes into one edit.
61- Use `read` (not cat/sed) to inspect files. Use `grep` to search contents and `find` to locate files by name — both respect .gitignore.
62- Use `bash` to run commands, build, and test. Inspect output before declaring success; if a command fails, read the error and fix it.
63- Think step by step when the task is non-trivial, but do not narrate every action.
64- Stay within the working directory; do not modify files outside it unless explicitly asked.
65
66# Output style
67- Be concise. No filler (\"Great question!\", \"Certainly!\"). State what you did and what to verify.
68- When the task is done, summarize the result and any next steps. If you hit a blocker, say so plainly.
69"
70    );
71
72    if let Some(extra) = opts.extra {
73        prompt.push_str("\n# Additional instructions\n");
74        prompt.push_str(extra);
75        prompt.push('\n');
76    }
77
78    if let Some(skills) = opts.skills {
79        if !skills.skills.is_empty() {
80            prompt.push_str("\n# Skills\n\n");
81            prompt.push_str(
82                "Skills are on-demand capability modules. Each is listed with its name and a \
83                 short description of when to use it. When the user's request matches a skill, \
84                 invoke it with the `skill` tool — that loads the skill's full instructions, \
85                 which you then follow. Do not invoke a skill unless it is relevant. A skill may \
86                 reference supporting files (scripts, references) under its own directory; read \
87                 those with the `read` tool.\n\nAvailable skills:\n",
88            );
89            for s in &skills.skills {
90                let desc = if s.description.is_empty() {
91                    "(no description)"
92                } else {
93                    s.description.as_str()
94                };
95                prompt.push_str(&format!("- **{}**: {}\n", s.name, desc));
96            }
97        }
98    }
99
100    prompt
101}
102
103#[cfg(test)]
104mod tests {
105    use super::*;
106    use crate::skills::{Skill, SkillSource};
107    use std::path::PathBuf;
108
109    #[test]
110    fn prompt_lists_discovered_skills_by_name_and_description() {
111        let skills = Skills {
112            skills: vec![Skill {
113                name: "deploy".into(),
114                description: "How to ship to prod".into(),
115                body: String::new(),
116                dir: PathBuf::from("."),
117                source: SkillSource::Project,
118            }],
119        };
120        let p = build_system_prompt(PromptOptions {
121            cwd: std::path::Path::new("."),
122            tool_names: &["read".to_string()],
123            extra: None,
124            skills: Some(&skills),
125        });
126        assert!(p.contains("# Skills"), "skills section present");
127        assert!(p.contains("**deploy**"), "skill name listed");
128        assert!(p.contains("How to ship to prod"), "skill description listed");
129        assert!(p.contains("`skill` tool"), "guidance to invoke via skill tool");
130    }
131
132    #[test]
133    fn prompt_has_no_skills_section_when_none() {
134        let p = build_system_prompt(PromptOptions {
135            cwd: std::path::Path::new("."),
136            tool_names: &["read".to_string()],
137            extra: None,
138            skills: None,
139        });
140        assert!(!p.contains("# Skills"));
141    }
142}