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 `git push`) 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.";
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.brief.instructions = Some(built.instructions);
77 Ok(built.start_lines)
78}