-
Notifications
You must be signed in to change notification settings - Fork 80
/
main.rs
148 lines (130 loc) · 4.22 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#![warn(clippy::all, clippy::pedantic, clippy::nursery)]
use bat::PrettyPrinter;
use clap::Parser;
use colored::Colorize;
use config::Config;
use question::{Answer, Question};
use reqwest::blocking::Client;
use serde_json::json;
use spinners::{Spinner, Spinners};
use std::process::Command;
mod config;
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct Cli {
/// Description of the command to execute
prompt: Vec<String>,
/// Run the generated program without asking for confirmation
#[clap(short = 'y', long)]
force: bool,
}
fn main() {
let cli = Cli::parse();
let config = Config::new();
let client = Client::new();
let mut spinner = Spinner::new(Spinners::BouncingBar, "Generating your command...".into());
let api_addr = format!("{}/completions", config.api_base);
let response = client
.post(api_addr)
.json(&json!({
"top_p": 1,
"stop": "```",
"temperature": 0,
"suffix": "\n```",
"max_tokens": 1000,
"presence_penalty": 0,
"frequency_penalty": 0,
"model": "text-davinci-003",
"prompt": build_prompt(&cli.prompt.join(" ")),
}))
.header("Authorization", format!("Bearer {}", config.api_key))
.send()
.unwrap();
let status_code = response.status();
if status_code.is_client_error() {
let response_body = response.json::<serde_json::Value>().unwrap();
let error_message = response_body["error"]["message"].as_str().unwrap();
spinner.stop_and_persist(
"✖".red().to_string().as_str(),
format!("API error: \"{error_message}\"").red().to_string(),
);
std::process::exit(1);
} else if status_code.is_server_error() {
spinner.stop_and_persist(
"✖".red().to_string().as_str(),
format!("OpenAI is currently experiencing problems. Status code: {status_code}")
.red()
.to_string(),
);
std::process::exit(1);
}
let code = response.json::<serde_json::Value>().unwrap()["choices"][0]["text"]
.as_str()
.unwrap()
.trim()
.to_string();
spinner.stop_and_persist(
"✔".green().to_string().as_str(),
"Got some code!".green().to_string(),
);
PrettyPrinter::new()
.input_from_bytes(code.as_bytes())
.language("bash")
.grid(true)
.print()
.unwrap();
let should_run = if cli.force {
true
} else {
Question::new(
">> Run the generated program? [Y/n]"
.bright_black()
.to_string()
.as_str(),
)
.yes_no()
.until_acceptable()
.default(Answer::YES)
.ask()
.expect("Couldn't ask question.")
== Answer::YES
};
if should_run {
config.write_to_history(code.as_str());
spinner = Spinner::new(Spinners::BouncingBar, "Executing...".into());
let output = Command::new("bash")
.arg("-c")
.arg(code.as_str())
.output()
.unwrap_or_else(|_| {
spinner.stop_and_persist(
"✖".red().to_string().as_str(),
"Failed to execute the generated program.".red().to_string(),
);
std::process::exit(1);
});
if !output.status.success() {
spinner.stop_and_persist(
"✖".red().to_string().as_str(),
"The program threw an error.".red().to_string(),
);
println!("{}", String::from_utf8_lossy(&output.stderr));
std::process::exit(1);
}
spinner.stop_and_persist(
"✔".green().to_string().as_str(),
"Command ran successfully".green().to_string(),
);
println!("{}", String::from_utf8_lossy(&output.stdout));
}
}
fn build_prompt(prompt: &str) -> String {
let os_hint = if cfg!(target_os = "macos") {
" (on macOS)"
} else if cfg!(target_os = "linux") {
" (on Linux)"
} else {
""
};
format!("{prompt}{os_hint}:\n```bash\n#!/bin/bash\n")
}