Skip to main content

blyte_cli/
orchestrator_text.rs

1use std::path::Path;
2
3use blyte_agent::orchestrator::{assemble, resolve_sources, start_lines, user_rules_path, Answers};
4use blyte_agent::state::State;
5
6pub const REFERENCE: &str = include_str!("../assets/orchestrator/reference.md");
7pub const REFERENCE_DISPATCH_TOOL: &str =
8    include_str!("../assets/orchestrator/reference-dispatch-tool.md");
9pub const DEFAULT_PROCESS: &str = include_str!("../assets/orchestrator/process.md");
10
11pub const SANDBOX_ON_PARAGRAPH: &str = "The shell is sandboxed: it can write only in the repository and temporary directories, cannot read\ncredentials and has no network. A command that must leave the sandbox (for example `git push`) takes\n`unsandboxed` with a reason and runs only when the user approves.";
12pub const SANDBOX_OFF_PARAGRAPH: &str = "The shell is not sandboxed.";
13
14pub fn sandbox_start_line(sandbox_on: bool, allowed_in_advance: usize) -> String {
15    if sandbox_on {
16        format!(
17            "sandbox: on (a command leaves it only with your approval; {allowed_in_advance} allowed in advance)"
18        )
19    } else {
20        "sandbox: off".to_string()
21    }
22}
23
24pub struct Built {
25    pub instructions: String,
26    pub start_lines: [String; 3],
27}
28
29pub fn build_with_sandbox(
30    dispatch_tool: bool,
31    answers: Answers,
32    root: &Path,
33    sandbox_on: bool,
34    allowed_in_advance: usize,
35) -> Result<Built, String> {
36    let (base_reference, default_process) = if dispatch_tool {
37        (REFERENCE_DISPATCH_TOOL, None)
38    } else {
39        (REFERENCE, Some(DEFAULT_PROCESS))
40    };
41    let sandbox_paragraph = if sandbox_on {
42        SANDBOX_ON_PARAGRAPH
43    } else {
44        SANDBOX_OFF_PARAGRAPH
45    };
46    let reference = format!("{}\n\n{sandbox_paragraph}", base_reference.trim());
47    let user_path = user_rules_path();
48    let sources = resolve_sources(user_path.as_deref(), root, default_process)?;
49    let instructions = assemble(&reference, answers, &sources);
50    let [process_line, repo_line] = start_lines(&sources);
51    let sandbox_line = sandbox_start_line(sandbox_on, allowed_in_advance);
52    Ok(Built {
53        instructions,
54        start_lines: [process_line, repo_line, sandbox_line],
55    })
56}
57
58pub fn build(dispatch_tool: bool, answers: Answers, root: &Path) -> Result<Built, String> {
59    let allowed = crate::approval_file::load()?.len();
60    build_with_sandbox(dispatch_tool, answers, root, true, allowed)
61}
62
63pub fn set_state_instructions(
64    state: &mut State,
65    dispatch_tool: bool,
66    root: &Path,
67    sandbox_on: bool,
68    allowed_in_advance: usize,
69) -> Result<[String; 3], String> {
70    let answers = if state.runtime.conversation.is_some() {
71        Answers::Person
72    } else {
73        Answers::Nobody
74    };
75    let built = build_with_sandbox(dispatch_tool, answers, root, sandbox_on, allowed_in_advance)?;
76    state.orchestrator.instructions = Some(built.instructions);
77    Ok(built.start_lines)
78}