Skip to content

Commit

Permalink
refactor(core): update commands and files
Browse files Browse the repository at this point in the history
  • Loading branch information
omarabid committed Dec 23, 2023
1 parent 0595d1d commit e2bad1b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
25 changes: 22 additions & 3 deletions core/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,27 +34,36 @@ pub fn config() -> Result<()> {
}

pub fn history() -> Result<()> {
// display commands history
db::display_commands()?;

Ok(())
}

pub fn undo() -> Result<()> {
// start spinner animation
let mut spinner = Spinner::new(Spinners::Dots2, "Communicating with Open AI".into());

// get last command
let last_command = db::get_last_command()?;
// get git status
let git_status = git::get_status()?;
// get git log
let git_log = git::get_log()?;

// get openai key from config
let openai_key = AppConfig::fetch()?.openai_key;
let config = OpenAIConfig::new().with_api_key(openai_key);

// create client
let client = Client::with_config(config);

// create prompt with git status and git log
let prompt = crate::PROMPT_UNDO
.replace("{git_status}", &git_status)
.replace("{git_log}", &git_log);

// create request
let request = async_openai::types::CreateChatCompletionRequestArgs::default()
.model("gpt-4")
.messages([
Expand All @@ -67,18 +76,23 @@ pub fn undo() -> Result<()> {
.build()?
.into(),
])
.max_tokens(50_u16)
.max_tokens(64_u16)
.build()?;

// make request
let response = async_std::task::block_on(
client
.chat() // Get the API "group" (completions, images, etc.) from the client
.create(request), // Make the API call in that "group"
)?;

// stop spinner animation
spinner.stop();

// print newline
println!("");

// get first choice
let returned_command = response
.choices
.first()
Expand All @@ -88,10 +102,13 @@ pub fn undo() -> Result<()> {
.as_ref()
.ok_or_else(|| Error::new("No content returned"))?;

// decode returned response
let decoded_command = crate::decode::decode_gpt_response(returned_command.to_string())?;

// print GPTResult
println!("{}", &decoded_command);

// match GPTResult and extract GPTResponse
let gpt_response: GPTResponse = match decoded_command {
GPTResult::Success(gpt_response) => gpt_response,
GPTResult::Failure(msg) => {
Expand All @@ -101,13 +118,15 @@ pub fn undo() -> Result<()> {
}
};

println!(":: Prooced with Command(s)?: \n [Y/n]");
// ask user if they want to proceed with the command(s)
print!(":: Prooced with Command(s)?: [Y/n] ");

// get user input
let mut input = String::new();
std::io::stdin().read_line(&mut input)?;

// if user input is Y, execute GPTResponse
if input.trim() == "Y" {
// execute GPTResponse
execute_gptresponse(gpt_response)?;
}

Expand Down
4 changes: 0 additions & 4 deletions core/src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,12 @@ pub fn get_log() -> Result<String> {
}

pub fn execute_command(command: String) -> Result<()> {
dbg!(&command);

let mut output = Command::new("git");

shlex::split(&command).unwrap().iter().for_each(|arg| {
output.arg(arg);
});

dbg!(&output);

let output_stdout = output.output().expect("failed to execute process").stdout;

let output = String::from_utf8(output_stdout)?;
Expand Down

0 comments on commit e2bad1b

Please sign in to comment.