feat: shell commands support revision (#413)

This commit is contained in:
sigoden 2024-04-15 05:58:45 +08:00 committed by GitHub
parent 6dc332930b
commit 38e594505b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 28 additions and 18 deletions

View File

@ -94,6 +94,14 @@ impl Input {
self.data_urls.clone() self.data_urls.clone()
} }
pub fn text(&self) -> String {
self.text.clone()
}
pub fn set_text(&mut self, text: String) {
self.text = text;
}
pub fn role(&self) -> Option<&Role> { pub fn role(&self) -> Option<&Role> {
self.context.role.as_ref() self.context.role.as_ref()
} }

View File

@ -148,14 +148,14 @@ fn start_interactive(config: &GlobalConfig) -> Result<()> {
repl.run() repl.run()
} }
fn execute(config: &GlobalConfig, input: Input) -> Result<()> { fn execute(config: &GlobalConfig, mut input: Input) -> Result<()> {
let client = init_client(config)?; let client = init_client(config)?;
config.read().maybe_print_send_tokens(&input); config.read().maybe_print_send_tokens(&input);
let mut eval_str = client.send_message(input.clone())?; let mut eval_str = client.send_message(input.clone())?;
if let Ok(true) = CODE_BLOCK_RE.is_match(&eval_str) { if let Ok(true) = CODE_BLOCK_RE.is_match(&eval_str) {
eval_str = extract_block(&eval_str); eval_str = extract_block(&eval_str);
} }
config.write().save_message(input, &eval_str)?; config.write().save_message(input.clone(), &eval_str)?;
config.read().maybe_copy(&eval_str); config.read().maybe_copy(&eval_str);
let render_options = config.read().get_render_options()?; let render_options = config.read().get_render_options()?;
let mut markdown_render = MarkdownRender::init(render_options)?; let mut markdown_render = MarkdownRender::init(render_options)?;
@ -165,39 +165,41 @@ fn execute(config: &GlobalConfig, input: Input) -> Result<()> {
} }
if stdout().is_terminal() { if stdout().is_terminal() {
println!("{}", markdown_render.render(&eval_str).trim()); println!("{}", markdown_render.render(&eval_str).trim());
let mut describe = false; let mut explain = false;
loop { loop {
let answer = Text::new("[e]xecute, [d]escribe, [a]bort: ") let answer = Text::new("[1]:execute [2]:explain [3]:revise [4]:exit")
.with_default("e") .with_default("1")
.with_validator(|input: &str| { .with_validator(|input: &str| match matches!(input, "1" | "2" | "3" | "4") {
match matches!(input, "E" | "e" | "D" | "d" | "A" | "a") {
true => Ok(Validation::Valid), true => Ok(Validation::Valid),
false => Ok(Validation::Invalid( false => Ok(Validation::Invalid(
"Invalid input, choice one of e, d or a".into(), "Select a number between 1 and 4.".into(),
)), )),
}
}) })
.prompt()?; .prompt()?;
println!();
match answer.as_str() { match answer.as_str() {
"E" | "e" => { "1" => {
let code = run_command(&eval_str)?; let code = run_command(&eval_str)?;
if code != 0 { if code != 0 {
process::exit(code); process::exit(code);
} }
} }
"D" | "d" => { "2" => {
if !describe { if !explain {
config.write().set_role(EXPLAIN_ROLE)?; config.write().set_role(EXPLAIN_ROLE)?;
} }
let input = Input::from_str(&eval_str, config.read().input_context()); let input = Input::from_str(&eval_str, config.read().input_context());
let abort = create_abort_signal(); let abort = create_abort_signal();
render_stream(&input, client.as_ref(), config, abort)?; render_stream(&input, client.as_ref(), config, abort)?;
describe = true; explain = true;
continue; continue;
} }
"3" => {
let revision = Text::new("Enter your revision:").prompt()?;
let text = format!("INPUT: {}\n{eval_str}\nINPUT: {revision}\n", input.text());
input.set_text(text);
return execute(config, input);
}
_ => {} _ => {}
} }
break; break;