-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(core): stage changes for core files and update Cargo.toml
- Loading branch information
Showing
5 changed files
with
164 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,3 +20,4 @@ async-openai = "0.17.0" | |
reqwest = "0.11.22" | ||
comfy-table = "7.1.0" | ||
spinners = "4.1.1" | ||
shlex = "1.2.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
use comfy_table::Table; | ||
use utils::error::{Error, Result}; | ||
|
||
#[derive(Debug)] | ||
pub enum GPTResult { | ||
Success(GPTResponse), | ||
Failure(String), | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct GPTResponse { | ||
pub status: ResponseStatus, | ||
pub explanation: String, | ||
pub commands: Vec<String>, | ||
} | ||
|
||
#[derive(Debug)] | ||
pub enum ResponseStatus { | ||
Success, | ||
NotValid, | ||
NotPossible, | ||
} | ||
|
||
pub fn decode_gpt_response(response: String) -> Result<GPTResult> { | ||
let response = response.trim(); | ||
|
||
if response.starts_with("NOT_VALID:") { | ||
let explanation = response.replace("NOT_VALID:", ""); | ||
|
||
return Ok(GPTResult::Success(GPTResponse { | ||
status: ResponseStatus::NotValid, | ||
explanation, | ||
commands: vec![], | ||
})); | ||
} | ||
|
||
if response.starts_with("NOT_POSSIBLE:") { | ||
let explanation = response.replace("NOT_POSSIBLE:", ""); | ||
|
||
return Ok(GPTResult::Success(GPTResponse { | ||
status: ResponseStatus::NotPossible, | ||
explanation, | ||
commands: vec![], | ||
})); | ||
} | ||
|
||
if response.starts_with("git") { | ||
// Sanitize commands | ||
let commands: Vec<String> = response | ||
.split("&&") | ||
.map(|command| { | ||
if command.trim().starts_with("git") { | ||
Ok(command.trim().replace("git ", "")) | ||
} else { | ||
Err(Error::new("One or more commands do not start with 'git'")) | ||
} | ||
}) | ||
.collect::<Result<Vec<String>>>()?; | ||
|
||
return Ok(GPTResult::Success(GPTResponse { | ||
status: ResponseStatus::Success, | ||
explanation: String::from(""), | ||
commands, | ||
})); | ||
} | ||
|
||
Err(Error::new("Could not decode GPT response")) | ||
} | ||
|
||
// implement display for GPTResult | ||
// use comfy_table::Table to display the commands | ||
impl std::fmt::Display for GPTResult { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
match self { | ||
GPTResult::Success(response) => { | ||
let mut table = Table::new(); | ||
|
||
table.set_header(vec!["#", "Command"]); | ||
|
||
response | ||
.commands | ||
.iter() | ||
.enumerate() | ||
.for_each(|(i, command)| { | ||
table.add_row(vec![i.to_string(), format!("git {}", command.to_string())]); | ||
}); | ||
|
||
write!(f, "{}", table) | ||
} | ||
GPTResult::Failure(error) => write!(f, "{}", error), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters