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 pushes the request's branch and opens a pull request, asking the user first only when `blyte.toml` has `[surface] publish = \"ask\"` (merging is never done by the surface).";
12pub const SANDBOX_ON_NO_REPO_PARAGRAPH: &str = "The shell is sandboxed: the shell and the write tools can write only in temporary directories, cannot read\ncredentials and have no network. Files in this folder are read-only without the user's approval, and writes are\nrefused while an in-place run is editing it. To hand work off, call `blyte` with `dispatch` and the `task`\nargument (do not write task files yourself). A command that must leave the sandbox (for example a package\ninstall) takes `unsandboxed` with a reason and runs only when the user approves, and a write or edit in the\nfolder also needs the user's approval.";
13pub const SANDBOX_OFF_PARAGRAPH: &str = "The shell is not sandboxed.";
14pub const SANDBOX_OFF_NO_REPO_PARAGRAPH: &str = "The shell is not sandboxed. Writes to this folder are refused while an in-place run is editing it; hand work\noff with `blyte` (`dispatch` and the `task` argument).";
15
16pub fn sandbox_start_line(sandbox_on: bool, allowed_in_advance: usize) -> String {
17 if sandbox_on {
18 format!(
19 "sandbox: on (a command leaves it only with your approval; {allowed_in_advance} allowed in advance)"
20 )
21 } else {
22 "sandbox: off".to_string()
23 }
24}
25
26pub struct Built {
27 pub instructions: String,
28 pub start_lines: [String; 3],
29}
30
31pub fn build_with_sandbox(
32 dispatch_tool: bool,
33 answers: Answers,
34 root: &Path,
35 sandbox_on: bool,
36 allowed_in_advance: usize,
37) -> Result<Built, String> {
38 let sandbox_off_process;
39 let (base_reference, default_process) = if dispatch_tool {
40 (REFERENCE_DISPATCH_TOOL, None)
41 } else if sandbox_on {
42 (REFERENCE, Some(DEFAULT_PROCESS))
43 } else {
44 sandbox_off_process = DEFAULT_PROCESS.replace(
45 "unsandboxed commands the user approves",
46 "commands the user approves",
47 );
48 (REFERENCE, Some(sandbox_off_process.as_str()))
49 };
50 let is_repo = blyte_agent::sandbox::is_git_repository(root);
51 let sandbox_paragraph = match (sandbox_on, is_repo) {
52 (true, true) => SANDBOX_ON_PARAGRAPH,
53 (true, false) => SANDBOX_ON_NO_REPO_PARAGRAPH,
54 (false, true) => SANDBOX_OFF_PARAGRAPH,
55 (false, false) => SANDBOX_OFF_NO_REPO_PARAGRAPH,
56 };
57 let reference = format!("{}\n\n{sandbox_paragraph}", base_reference.trim());
58 let user_path = user_rules_path();
59 let sources = resolve_sources(user_path.as_deref(), root, default_process)?;
60 let instructions = assemble(&reference, answers, &sources);
61 let [process_line, repo_line] = start_lines(&sources);
62 let sandbox_line = sandbox_start_line(sandbox_on, allowed_in_advance);
63 Ok(Built {
64 instructions,
65 start_lines: [process_line, repo_line, sandbox_line],
66 })
67}
68
69pub fn set_state_instructions(
70 state: &mut State,
71 dispatch_tool: bool,
72 root: &Path,
73 sandbox_on: bool,
74 allowed_in_advance: usize,
75) -> Result<[String; 3], String> {
76 let answers = if state.runtime.conversation.is_some() {
77 Answers::Person
78 } else {
79 Answers::Nobody
80 };
81 let built = build_with_sandbox(dispatch_tool, answers, root, sandbox_on, allowed_in_advance)?;
82 state.brief.instructions = Some(built.instructions);
83 Ok(built.start_lines)
84}