Skip to main content

resources/
prompt_files.rs

1//! SYSTEM.md / APPEND_SYSTEM.md discovery and prompt inputs (DefaultResourceLoader in core/resource-loader.ts).
2
3use crate::paths::{resolve_path, strip_bom};
4use config::paths::CONFIG_DIR_NAME;
5use std::fs;
6use std::path::Path;
7
8/// A prompt input is a file path when that file exists (relative to the process cwd, like Node), else literal text.
9/// An unreadable file falls back to the input text.
10pub fn resolve_prompt_input(input: Option<&str>) -> Option<String> {
11    let input = input.filter(|i| !i.is_empty())?;
12    if Path::new(input).exists() {
13        return Some(fs::read_to_string(input).map_or_else(|_| input.to_string(), |t| strip_bom(&t).to_string()));
14    }
15    Some(input.to_string())
16}
17
18/// `<cwd>/.pi/<name>` when the project is trusted and it exists, else `<agent_dir>/<name>` when it exists.
19fn discover(cwd: &str, agent_dir: &str, name: &str, project_trusted: bool) -> Option<String> {
20    let project = jsrt::path::join(&[cwd, CONFIG_DIR_NAME, name]);
21    if project_trusted && Path::new(&project).exists() {
22        return Some(project);
23    }
24    let global = jsrt::path::join(&[agent_dir, name]);
25    Path::new(&global).exists().then_some(global)
26}
27
28/// The resolved base system prompt and append prompts, with the source paths that exist.
29#[derive(Debug, PartialEq, Eq)]
30pub struct PromptFiles {
31    pub system_prompt: Option<String>,
32    pub system_prompt_source: Option<String>,
33    pub append_system_prompt: Vec<String>,
34    pub append_sources: Vec<String>,
35}
36
37/// Explicit inputs win; otherwise SYSTEM.md and APPEND_SYSTEM.md are discovered. A source path is resolved against
38/// the process cwd, where its existence was checked (`~` names `home`).
39pub fn load_prompt_files(
40    cwd: &str,
41    agent_dir: &str,
42    home: &str,
43    project_trusted: bool,
44    system_prompt: Option<&str>,
45    append_system_prompt: Option<&[String]>,
46) -> PromptFiles {
47    let discovered = discover(cwd, agent_dir, "SYSTEM.md", project_trusted);
48    let source = system_prompt.map(String::from).or(discovered);
49    let appends: Vec<String> = match append_system_prompt {
50        Some(list) => list.to_vec(),
51        None => discover(cwd, agent_dir, "APPEND_SYSTEM.md", project_trusted)
52            .into_iter()
53            .collect(),
54    };
55    let existing = |s: &String| {
56        Path::new(s)
57            .exists()
58            .then(|| resolve_path(s, &process_cwd(), home, false))
59    };
60    PromptFiles {
61        system_prompt: resolve_prompt_input(source.as_deref()),
62        system_prompt_source: source.as_ref().and_then(existing),
63        append_system_prompt: appends.iter().filter_map(|s| resolve_prompt_input(Some(s))).collect(),
64        append_sources: appends.iter().filter_map(existing).collect(),
65    }
66}
67
68fn process_cwd() -> String {
69    std::env::current_dir()
70        .map(|p| p.to_string_lossy().into_owned())
71        .unwrap_or_else(|_| "/".to_string())
72}