Skip to main content

blyte_cli/
instructions_cmd.rs

1//! Implementation of the `blyte instructions` command.
2
3use std::path::{Path, PathBuf};
4use std::process::Command;
5use std::sync::Arc;
6
7use blyte_agent::approval::{ApprovalConfig, NoApprover};
8use blyte_agent::conversation::Answers;
9use blyte_agent::run::CONVERSATION_FIXED;
10
11use crate::conversation_text;
12
13pub fn repo_root() -> Result<PathBuf, String> {
14    let out = Command::new("git")
15        .args(["rev-parse", "--show-toplevel"])
16        .output()
17        .map_err(|_| "not inside a git repository".to_string())?;
18    if !out.status.success() {
19        return Err("not inside a git repository".to_string());
20    }
21    let text = String::from_utf8_lossy(&out.stdout);
22    let trimmed = text.trim();
23    if trimmed.is_empty() {
24        return Err("not inside a git repository".to_string());
25    }
26    Ok(PathBuf::from(trimmed))
27}
28
29pub fn render_with_sandbox(
30    dispatch_tool: bool,
31    answers: Answers,
32    root: &Path,
33    sandbox_on: bool,
34) -> Result<String, String> {
35    let allowed_in_advance = if sandbox_on {
36        crate::approval_file::load()?.len()
37    } else {
38        0
39    };
40    let built = conversation_text::build_with_sandbox(
41        dispatch_tool,
42        answers,
43        root,
44        sandbox_on,
45        allowed_in_advance,
46    )?;
47    let mut out = String::new();
48    for line in &built.start_lines {
49        out.push_str(line);
50        out.push('\n');
51    }
52    out.push('\n');
53    out.push_str(&built.instructions);
54    if !built.instructions.ends_with("\n\n") {
55        if !built.instructions.ends_with('\n') {
56            out.push('\n');
57        }
58        out.push('\n');
59    }
60    out.push_str("## Always sent\n");
61    out.push_str(CONVERSATION_FIXED);
62    if !CONVERSATION_FIXED.ends_with('\n') {
63        out.push('\n');
64    }
65    out.push('\n');
66    out.push_str("## Tool descriptions\n");
67    let prev_approval = blyte_agent::tools::approval();
68    if sandbox_on {
69        blyte_agent::tools::set_approval(Some(ApprovalConfig {
70            rules: Vec::new(),
71            approver: Arc::new(NoApprover),
72        }));
73    } else {
74        blyte_agent::tools::set_approval(None);
75    }
76    let specs = blyte_agent::tools::specs();
77    blyte_agent::tools::set_approval(prev_approval);
78    for tool in specs {
79        out.push_str(&format!("### {}\n{}\n", tool.name, tool.description));
80        if tool.name == "bash" {
81            if let Some(props) = tool
82                .parameters
83                .get("properties")
84                .and_then(|v| v.as_object())
85            {
86                for prop in ["unsandboxed", "reason"] {
87                    if let Some(desc) = props
88                        .get(prop)
89                        .and_then(|v| v.get("description"))
90                        .and_then(|v| v.as_str())
91                    {
92                        out.push_str(&format!("- `{prop}`: {desc}\n"));
93                    }
94                }
95            }
96        }
97        out.push('\n');
98    }
99    out.push_str("The request and a description of the environment are added when a run starts.\n");
100    Ok(out)
101}