Skip to main content

codetether_agent/agent/builtin/
system_prompt.rs

1//! Prompt builders for built-in agents.
2//!
3//! This module merges raw prompt templates with AGENTS.md content and extra
4//! guidance snippets.
5//!
6//! # Examples
7//!
8//! ```ignore
9//! let prompt = build_system_prompt(std::path::Path::new("."));
10//! ```
11
12use crate::agent::build_guidance::BUILD_GITHUB_AUTH_GUIDANCE;
13use std::path::Path;
14
15use super::agents_md::load_all_agents_md;
16use super::prompts::{BUILD_MODE_GUARDRAIL, BUILD_SYSTEM_PROMPT, PLAN_SYSTEM_PROMPT};
17
18/// Builds the build-agent prompt for a working directory.
19///
20/// # Examples
21///
22/// ```ignore
23/// let prompt = build_system_prompt(std::path::Path::new("."));
24/// ```
25pub fn build_system_prompt(cwd: &Path) -> String {
26    let base_prompt = BUILD_SYSTEM_PROMPT.replace("{cwd}", &cwd.display().to_string());
27    let agents_section = render_agents_section(cwd);
28    format!("{base_prompt}{agents_section}{BUILD_GITHUB_AUTH_GUIDANCE}{BUILD_MODE_GUARDRAIL}")
29}
30
31/// Builds the plan-agent prompt for a working directory.
32///
33/// # Examples
34///
35/// ```ignore
36/// let prompt = build_plan_system_prompt(std::path::Path::new("."));
37/// ```
38#[allow(dead_code)]
39pub fn build_plan_system_prompt(cwd: &Path) -> String {
40    let base_prompt = PLAN_SYSTEM_PROMPT.replace("{cwd}", &cwd.display().to_string());
41    format!("{base_prompt}{}", render_agents_section(cwd))
42}
43
44fn render_agents_section(cwd: &Path) -> String {
45    let agents_files = load_all_agents_md(cwd);
46    if agents_files.is_empty() {
47        return String::new();
48    }
49    let mut section = String::from(
50        "\n\n## Project Instructions (AGENTS.md)\n\nThe following instructions were loaded from AGENTS.md files in the project.\nFollow these project-specific guidelines when working on this codebase.\n\n",
51    );
52    for (content, path) in agents_files.iter().rev() {
53        section.push_str(&format!("### From {}\n\n{}\n\n", path.display(), content));
54    }
55    section
56}