Skip to content

Commit 40445a3

Browse files
committed
```
feat: Enhance commit summary generation with optional description - Updated the APIClient to include a parameter for generating detailed commit descriptions using a local LLM client. - Modified the CommitDocGenHook to support the new parameter, allowing for more flexible commit summary generation. - Adjusted the commit_code function to accept a flag for description generation, improving user control over commit message details. - Refined LLMClient's generate_commit_summary method to conditionally include a description based on the new parameter. ```
1 parent 682eccc commit 40445a3

5 files changed

Lines changed: 28 additions & 34 deletions

File tree

penify_hook/api_client.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import json
22
import os
33
import requests
4+
from .llm_client import LLMClient
45

56
class APIClient:
67
def __init__(self, api_url, api_token: str = None, bearer_token: str = None):
@@ -111,7 +112,7 @@ def get_supported_file_types(self) -> list[str]:
111112
else:
112113
return ["py", "js", "ts", "java", "kt", "cs", "c"]
113114

114-
def generate_commit_summary_with_llm(self, diff, message, repo_details, llm_client, jira_context=None):
115+
def generate_commit_summary_with_llm(self, diff, message, generate_description: bool, repo_details, llm_client : LLMClient, jira_context=None):
115116
"""
116117
Generate a commit summary using a local LLM client instead of the API.
117118
@@ -126,7 +127,7 @@ def generate_commit_summary_with_llm(self, diff, message, repo_details, llm_clie
126127
Dict with title and description for the commit
127128
"""
128129
try:
129-
return llm_client.generate_commit_summary(diff, message, repo_details, jira_context)
130+
return llm_client.generate_commit_summary(diff, message, generate_description, repo_details, jira_context)
130131
except Exception as e:
131132
print(f"Error using local LLM: {e}")
132133
# Fall back to API for commit summary

penify_hook/commands/commit_commands.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
except ImportError:
1818
JiraClient = None
1919

20-
def commit_code(api_url, token, message, open_terminal,
20+
def commit_code(api_url, token, message, open_terminal, generate_description,
2121
llm_model=None, llm_api_base=None, llm_api_key=None,
2222
jira_url=None, jira_user=None, jira_api_token=None):
2323
"""
@@ -65,7 +65,7 @@ def commit_code(api_url, token, message, open_terminal,
6565
# Pass the LLM client and JIRA client to CommitDocGenHook
6666
gf_path = recursive_search_git_folder(os.getcwd())
6767
analyzer = CommitDocGenHook(gf_path, api_client, llm_client, jira_client)
68-
analyzer.run(message, open_terminal)
68+
analyzer.run(message, open_terminal, generate_description)
6969
except Exception as e:
7070
print(f"Error: {e}")
7171
sys.exit(1)

penify_hook/commit_analyzer.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def __init__(self, repo_path: str, api_client: APIClient, llm_client=None, jira_
1616
self.llm_client = llm_client # Add LLM client as an optional parameter
1717
self.jira_client = jira_client # Add JIRA client as an optional parameter
1818

19-
def get_summary(self, instruction: str):
19+
def get_summary(self, instruction: str, generate_description: bool) -> dict:
2020
"""Generate a summary for the commit based on the staged changes.
2121
2222
This function retrieves the differences of the staged changes in the
@@ -57,13 +57,13 @@ def get_summary(self, instruction: str):
5757
# Use LLM client if provided, otherwise use API client
5858
if self.llm_client:
5959
return self.api_client.generate_commit_summary_with_llm(
60-
diff, instruction, self.repo_details, self.llm_client, jira_context
60+
diff, instruction, generate_description, self.repo_details, self.llm_client, jira_context
6161
)
6262
else:
6363
return self.api_client.generate_commit_summary(diff, instruction, self.repo_details, jira_context)
6464

6565

66-
def run(self, msg: Optional[str], edit_commit_message: bool):
66+
def run(self, msg: Optional[str], edit_commit_message: bool, generate_description: bool):
6767
"""Run the post-commit hook.
6868
6969
This method retrieves the list of modified files from the last commit
@@ -81,7 +81,7 @@ def run(self, msg: Optional[str], edit_commit_message: bool):
8181
Raises:
8282
Exception: If there is an error generating the commit summary.
8383
"""
84-
summary: dict = self.get_summary(msg)
84+
summary: dict = self.get_summary(msg, generate_description)
8585
if not summary:
8686
raise Exception("Error generating commit summary")
8787

penify_hook/llm_client.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def __init__(self, model: str = None, api_base: str = None, api_key: str = None)
2727
if api_key:
2828
os.environ["OPENAI_API_KEY"] = api_key
2929

30-
def generate_commit_summary(self, diff: str, message: str, repo_details: Dict, jira_context: Dict = None) -> Dict:
30+
def generate_commit_summary(self, diff: str, message: str, generate_description: bool, repo_details: Dict, jira_context: Dict = None) -> Dict:
3131
"""
3232
Generate a commit summary using the LLM.
3333
@@ -92,7 +92,7 @@ def generate_commit_summary(self, diff: str, message: str, repo_details: Dict, j
9292
9393
Please provide:
9494
1. A short, focused commit title (50-72 characters) in a Semantic Commit Messages format. Format: <type>(<scope>): <subject>
95-
2. A detailed description that explains what was changed, why it was changed in both business and technical aspects, and any important context
95+
{'2. A detailed description that explains what was changed, why it was changed in both business and technical aspects, and any important context' if generate_description else ''}
9696
9797
List of Semantic Commit Message types that you can use:
9898
feat: (new feature for the user, not a new feature for build script)
@@ -103,8 +103,9 @@ def generate_commit_summary(self, diff: str, message: str, repo_details: Dict, j
103103
test: (adding missing tests, refactoring tests; no production code change)
104104
chore: (updating grunt tasks etc; no production code change)
105105
106-
Format your response as valid JSON with 'title' and 'description' keys.
106+
Format your response as valid JSON with 'title' {"and 'description'" if generate_description else ''} keys.
107107
"""
108+
print(prompt)
108109

109110
try:
110111
# Call the LLM using litellm

penify_hook/main.py

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -155,32 +155,24 @@ def main():
155155
elif args.subcommand == "commit":
156156
# For commit, token is now optional - some functionality may be limited without it
157157
open_terminal = args.terminal
158+
generate_description = args.description
158159

159-
# Get LLM configuration
160-
llm_model = args.llm_model
161-
llm_api_base = args.llm_api_base
162-
llm_api_key = args.llm_api_key
160+
# Try to get from config
161+
llm_config = get_llm_config()
162+
llm_model = llm_config.get('model')
163+
llm_api_base = llm_config.get('api_base')
164+
llm_api_key = llm_config.get('api_key')
163165

164-
if not llm_model:
165-
# Try to get from config
166-
llm_config = get_llm_config()
167-
llm_model = llm_config.get('model')
168-
llm_api_base = llm_config.get('api_base') if not llm_api_base else llm_api_base
169-
llm_api_key = llm_config.get('api_key') if not llm_api_key else llm_api_key
170166

171-
# Get JIRA configuration
172-
jira_url = args.jira_url
173-
jira_user = args.jira_user
174-
jira_api_token = args.jira_api_token
175-
176-
if not jira_url or not jira_user or not jira_api_token:
177-
# Try to get from config
178-
jira_config = get_jira_config()
179-
jira_url = jira_url or jira_config.get('url')
180-
jira_user = jira_user or jira_config.get('username')
181-
jira_api_token = jira_api_token or jira_config.get('api_token')
182-
183-
commit_code(API_URL, token, args.message, open_terminal,
167+
168+
# Try to get from config
169+
jira_config = get_jira_config()
170+
jira_url = jira_config.get('url')
171+
jira_user = jira_config.get('username')
172+
jira_api_token = jira_config.get('api_token')
173+
174+
175+
commit_code(API_URL, token, args.message, open_terminal, generate_description,
184176
llm_model, llm_api_base, llm_api_key,
185177
jira_url, jira_user, jira_api_token)
186178

0 commit comments

Comments
 (0)