blyte_cli/
conversation_text.rs1use std::path::Path;
2
3use blyte_agent::conversation::{assemble, resolve_sources, start_lines, user_rules_path, Answers};
4use blyte_agent::state::State;
5
6pub const REFERENCE: &str = include_str!("../assets/conversation/reference.md");
7pub const REFERENCE_DISPATCH_TOOL: &str =
8 include_str!("../assets/conversation/reference-dispatch-tool.md");
9pub const DEFAULT_PROCESS: &str = include_str!("../assets/conversation/process.md");
10
11pub const SANDBOX_ON_PARAGRAPH: &str = "The shell is sandboxed: the shell and the write tools can write only in `.wt/tasks` and temporary directories,\ncannot read credentials and have no network. The shell and the write tools can also write in the worktree of an\nopen edit started with `blyte edit-start`, until it finishes; `git commit` there is not possible (`edit-finish`\ncommits). A command that must leave the sandbox (for example a package install) takes `unsandboxed` with a reason and\nruns only when the user approves, and a write or edit outside those directories also needs the user's approval.\nTo publish a request, use the `publish` tool; it runs only when the user approves it on the page.";
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 sandbox_off_process;
37 let (base_reference, default_process) = if dispatch_tool {
38 (REFERENCE_DISPATCH_TOOL, None)
39 } else if sandbox_on {
40 (REFERENCE, Some(DEFAULT_PROCESS))
41 } else {
42 sandbox_off_process = DEFAULT_PROCESS.replace(
43 "unsandboxed commands the user approves",
44 "commands the user approves",
45 );
46 (REFERENCE, Some(sandbox_off_process.as_str()))
47 };
48 let sandbox_paragraph = if sandbox_on {
49 SANDBOX_ON_PARAGRAPH
50 } else {
51 SANDBOX_OFF_PARAGRAPH
52 };
53 let reference = format!("{}\n\n{sandbox_paragraph}", base_reference.trim());
54 let user_path = user_rules_path();
55 let sources = resolve_sources(user_path.as_deref(), root, default_process)?;
56 let instructions = assemble(&reference, answers, &sources);
57 let [process_line, repo_line] = start_lines(&sources);
58 let sandbox_line = sandbox_start_line(sandbox_on, allowed_in_advance);
59 Ok(Built {
60 instructions,
61 start_lines: [process_line, repo_line, sandbox_line],
62 })
63}
64
65pub fn build(dispatch_tool: bool, answers: Answers, root: &Path) -> Result<Built, String> {
66 let allowed = crate::approval_file::load()?.len();
67 build_with_sandbox(dispatch_tool, answers, root, true, allowed)
68}
69
70pub fn set_state_instructions(
71 state: &mut State,
72 dispatch_tool: bool,
73 root: &Path,
74 sandbox_on: bool,
75 allowed_in_advance: usize,
76) -> Result<[String; 3], String> {
77 let answers = if state.runtime.conversation.is_some() {
78 Answers::Person
79 } else {
80 Answers::Nobody
81 };
82 let built = build_with_sandbox(dispatch_tool, answers, root, sandbox_on, allowed_in_advance)?;
83 state.brief.instructions = Some(built.instructions);
84 Ok(built.start_lines)
85}