Skip to content

Commit

Permalink
feat: Add draft functionality for git diffs
Browse files Browse the repository at this point in the history
  • Loading branch information
jnsahaj committed Nov 3, 2024
1 parent 9003cba commit c0c487e
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/ai_prompt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,11 @@ impl AIPrompt {

let user_prompt = format!(
"Generate a concise git commit message written in present tense for the following code diff with the given specifications below:\n\
\nMessage language: en\
\nThe output response must be in format:\n<type>(<optional scope>): <commit message>\
\nFocus on being accurate and concise.\
\nChoose a type from the type-to-description JSON below that best describes the git diff:\n{}\
\nCommit message must be a maximum of 72 characters.\
\nAdd a description after the commit message.\
\nExclude anything unnecessary such as translation. Your entire response will be passed directly into git commit.\
\n\nCode diff:\n```diff\n{}\n```",
conventional_types,
Expand Down
13 changes: 13 additions & 0 deletions src/command.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use std::io::Write;
use std::process::Stdio;

use crate::error::LumenError;
use crate::git_entity::git_commit::GitCommit;
use crate::git_entity::git_diff::GitDiff;
use crate::git_entity::GitEntity;
use crate::provider::AIProvider;
use crate::provider::LumenProvider;
Expand Down Expand Up @@ -96,4 +98,15 @@ impl LumenCommand {
let git_entity = GitEntity::Commit(GitCommit::new(sha)?);
self.explain(&git_entity).await
}

pub async fn draft(&self) -> Result<(), LumenError> {
let result = self
.provider
.draft(GitEntity::Diff(GitDiff::new(true)?))
.await?;

print!("{result}");
std::io::stdout().flush()?;
Ok(())
}
}
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ enum Commands {
staged: bool,
},
List,
Draft,
}

#[tokio::main]
Expand Down Expand Up @@ -88,6 +89,7 @@ async fn run() -> Result<(), LumenError> {
command.explain(&git_entity).await?
}
Commands::List => command.list().await?,
Commands::Draft => command.draft().await?,
}

Ok(())
Expand Down
4 changes: 4 additions & 0 deletions src/provider/claude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,8 @@ impl AIProvider for ClaudeProvider {
let res = get_completion_result(&self.client, &self.api_key, payload).await?;
Ok(res)
}

async fn draft(&self, git_entity: GitEntity) -> Result<String, Box<dyn std::error::Error>> {
todo!()
}
}
4 changes: 4 additions & 0 deletions src/provider/groq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,8 @@ impl AIProvider for GroqProvider {
let res = get_completion_result(&self.client, &self.api_key, payload).await?;
Ok(res)
}

async fn draft(&self, git_entity: GitEntity) -> Result<String, Box<dyn std::error::Error>> {
todo!()
}
}
9 changes: 9 additions & 0 deletions src/provider/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub mod phind;
#[async_trait]
pub trait AIProvider {
async fn explain(&self, git_entity: GitEntity) -> Result<String, Box<dyn std::error::Error>>;
async fn draft(&self, git_entity: GitEntity) -> Result<String, Box<dyn std::error::Error>>;
}

pub enum LumenProvider {
Expand Down Expand Up @@ -66,4 +67,12 @@ impl AIProvider for LumenProvider {
LumenProvider::Claude(provider) => provider.explain(git_entity).await,
}
}
async fn draft(&self, git_entity: GitEntity) -> Result<String, Box<dyn std::error::Error>> {
match self {
LumenProvider::OpenAI(provider) => provider.draft(git_entity).await,
LumenProvider::Phind(provider) => provider.draft(git_entity).await,
LumenProvider::Groq(provider) => provider.draft(git_entity).await,
LumenProvider::Claude(provider) => provider.draft(git_entity).await,
}
}
}
4 changes: 4 additions & 0 deletions src/provider/openai.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,8 @@ impl AIProvider for OpenAIProvider {
let res = get_completion_result(&self.client, &self.api_key, payload).await?;
Ok(res)
}

async fn draft(&self, git_entity: GitEntity) -> Result<String, Box<dyn std::error::Error>> {
todo!()
}
}
21 changes: 21 additions & 0 deletions src/provider/phind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,25 @@ impl AIProvider for PhindProvider {
let res = Self::get_main_text(&response).await?;
Ok(res)
}

async fn draft(&self, git_entity: GitEntity) -> Result<String, Box<dyn std::error::Error>> {
let request = self
.create_request(AIPrompt::build_draft_prompt(&git_entity))
.await?;

let headers = Self::create_headers()?;

let response = self
.client
.post("https://https.extension.phind.com/agent/")
.headers(headers)
.json(&request)
.send()
.await?
.text()
.await?;

let res = Self::get_main_text(&response).await?;
Ok(res)
}
}

0 comments on commit c0c487e

Please sign in to comment.