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(dispatch_tool: bool, answers: Answers, root: &Path) -> Result<String, String> {
30    render_with_sandbox(dispatch_tool, answers, root, true)
31}
32
33pub fn render_with_sandbox(
34    dispatch_tool: bool,
35    answers: Answers,
36    root: &Path,
37    sandbox_on: bool,
38) -> Result<String, String> {
39    let allowed_in_advance = if sandbox_on {
40        crate::approval_file::load()?.len()
41    } else {
42        0
43    };
44    let built = conversation_text::build_with_sandbox(
45        dispatch_tool,
46        answers,
47        root,
48        sandbox_on,
49        allowed_in_advance,
50    )?;
51    let mut out = String::new();
52    for line in &built.start_lines {
53        out.push_str(line);
54        out.push('\n');
55    }
56    out.push('\n');
57    out.push_str(&built.instructions);
58    if !built.instructions.ends_with("\n\n") {
59        if !built.instructions.ends_with('\n') {
60            out.push('\n');
61        }
62        out.push('\n');
63    }
64    out.push_str("## Always sent\n");
65    out.push_str(CONVERSATION_FIXED);
66    if !CONVERSATION_FIXED.ends_with('\n') {
67        out.push('\n');
68    }
69    out.push('\n');
70    out.push_str("## Tool descriptions\n");
71    let prev_approval = blyte_agent::tools::approval();
72    if sandbox_on {
73        blyte_agent::tools::set_approval(Some(ApprovalConfig {
74            rules: Vec::new(),
75            approver: Arc::new(NoApprover),
76        }));
77    } else {
78        blyte_agent::tools::set_approval(None);
79    }
80    let specs = blyte_agent::tools::specs();
81    blyte_agent::tools::set_approval(prev_approval);
82    for tool in specs {
83        out.push_str(&format!("### {}\n{}\n", tool.name, tool.description));
84        if tool.name == "bash" {
85            if let Some(props) = tool
86                .parameters
87                .get("properties")
88                .and_then(|v| v.as_object())
89            {
90                for prop in ["unsandboxed", "reason"] {
91                    if let Some(desc) = props
92                        .get(prop)
93                        .and_then(|v| v.get("description"))
94                        .and_then(|v| v.as_str())
95                    {
96                        out.push_str(&format!("- `{prop}`: {desc}\n"));
97                    }
98                }
99            }
100        }
101        out.push('\n');
102    }
103    out.push_str("The request and a description of the environment are added when a run starts.\n");
104    Ok(out)
105}