commitwise 0.1.5__tar.gz → 0.1.6__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (27) hide show
  1. {commitwise-0.1.5 → commitwise-0.1.6}/PKG-INFO +1 -1
  2. commitwise-0.1.6/commitwise/__init__.py +1 -0
  3. commitwise-0.1.6/commitwise/ai/local_engine.py +69 -0
  4. commitwise-0.1.6/commitwise/ai/openai_engine.py +66 -0
  5. commitwise-0.1.6/commitwise/ai_source.py +56 -0
  6. {commitwise-0.1.5 → commitwise-0.1.6}/commitwise/cli.py +38 -1
  7. commitwise-0.1.6/commitwise/errors.py +106 -0
  8. {commitwise-0.1.5 → commitwise-0.1.6}/commitwise/git_utils.py +3 -3
  9. {commitwise-0.1.5 → commitwise-0.1.6}/commitwise.egg-info/PKG-INFO +1 -1
  10. {commitwise-0.1.5 → commitwise-0.1.6}/commitwise.egg-info/SOURCES.txt +1 -0
  11. commitwise-0.1.5/commitwise/__init__.py +0 -1
  12. commitwise-0.1.5/commitwise/ai/local_engine.py +0 -39
  13. commitwise-0.1.5/commitwise/ai/openai_engine.py +0 -35
  14. commitwise-0.1.5/commitwise/ai_source.py +0 -48
  15. {commitwise-0.1.5 → commitwise-0.1.6}/LICENSE +0 -0
  16. {commitwise-0.1.5 → commitwise-0.1.6}/README.md +0 -0
  17. {commitwise-0.1.5 → commitwise-0.1.6}/commitwise/ai/__init__.py +0 -0
  18. {commitwise-0.1.5 → commitwise-0.1.6}/commitwise/ai/base.py +0 -0
  19. {commitwise-0.1.5 → commitwise-0.1.6}/commitwise/cli_utils.py +0 -0
  20. {commitwise-0.1.5 → commitwise-0.1.6}/commitwise/config.py +0 -0
  21. {commitwise-0.1.5 → commitwise-0.1.6}/commitwise/file_source.py +0 -0
  22. {commitwise-0.1.5 → commitwise-0.1.6}/commitwise.egg-info/dependency_links.txt +0 -0
  23. {commitwise-0.1.5 → commitwise-0.1.6}/commitwise.egg-info/entry_points.txt +0 -0
  24. {commitwise-0.1.5 → commitwise-0.1.6}/commitwise.egg-info/requires.txt +0 -0
  25. {commitwise-0.1.5 → commitwise-0.1.6}/commitwise.egg-info/top_level.txt +0 -0
  26. {commitwise-0.1.5 → commitwise-0.1.6}/pyproject.toml +0 -0
  27. {commitwise-0.1.5 → commitwise-0.1.6}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: commitwise
3
- Version: 0.1.5
3
+ Version: 0.1.6
4
4
  Summary: Generate clean git commit messages from files or AI
5
5
  Author-email: Abolfazl Hassan Zade <abolfazl.hassanzade.81@gmail.com>
6
6
  License: MIT
@@ -0,0 +1 @@
1
+ __version__='0.1.6'
@@ -0,0 +1,69 @@
1
+ import requests
2
+
3
+ from requests.exceptions import Timeout, ConnectionError as ReqConnectionError
4
+
5
+ from commitwise.ai.base import AIEngine
6
+ from commitwise.errors import (
7
+ AIProviderUnavailable,
8
+ AIProviderTimeout,
9
+ AIProviderConnectionError,
10
+ AIProviderResponseInvalid,
11
+ )
12
+
13
+ DEFAULT_TIMEOUT = 60
14
+
15
+
16
+ class LocalAIEngine(AIEngine):
17
+ """
18
+ Local AI engine using Ollama.
19
+ """
20
+
21
+ def __init__(self, model: str, url: str):
22
+ self.model = model
23
+ self.url = url.rstrip("/")
24
+
25
+ def generate_commit(self, diff: str) -> str:
26
+ prompt = self.default_prompt + f"\n{diff}"
27
+
28
+ try:
29
+ response = requests.post(
30
+ f"{self.url}/api/generate",
31
+ json={
32
+ "model": self.model,
33
+ "prompt": prompt,
34
+ "stream": False,
35
+ },
36
+ timeout=DEFAULT_TIMEOUT,
37
+ )
38
+
39
+ except Timeout:
40
+ raise AIProviderTimeout(
41
+ "Local AI model did not respond in time.\n\n "
42
+ "Try staging fewer changes or using a faster model."
43
+ )
44
+
45
+ except ReqConnectionError:
46
+ raise AIProviderUnavailable(
47
+ "Local AI service is not reachable.\n\n "
48
+ "Make sure Ollama is running (ollama serve)."
49
+ )
50
+
51
+ except Exception as exc:
52
+ raise AIProviderConnectionError(
53
+ f"Unexpected error while communicating with local AI: {exc}"
54
+ ) from exc
55
+
56
+ else:
57
+ if response.status_code != 200:
58
+ raise AIProviderUnavailable(
59
+ f"Local AI request failed with status {response.status_code}."
60
+ )
61
+
62
+ data = response.json()
63
+ message = data.get("response", "").strip()
64
+
65
+ if not message:
66
+ raise AIProviderResponseInvalid(
67
+ "Local AI returned an empty commit message."
68
+ )
69
+ return message
@@ -0,0 +1,66 @@
1
+ from commitwise.ai.base import AIEngine
2
+ from commitwise.errors import (
3
+ AIProviderNotConfigured,
4
+ AIProviderAuthError,
5
+ AIProviderRateLimitError,
6
+ AIProviderTimeout,
7
+ AIProviderConnectionError,
8
+ AIProviderResponseInvalid,
9
+ )
10
+
11
+
12
+ from openai import OpenAI
13
+ from openai import (
14
+ AuthenticationError,
15
+ RateLimitError,
16
+ APIConnectionError,
17
+ APITimeoutError,
18
+ OpenAIError,
19
+ )
20
+
21
+
22
+ class OpenAIEngine(AIEngine):
23
+ """
24
+ OpenAI-based AI engine (optional).
25
+ """
26
+
27
+ def __init__(self, api_key: str, model: str):
28
+ if not api_key:
29
+ raise AIProviderNotConfigured("OPENAI_API_KEY is not set.")
30
+
31
+ self.client = OpenAI(api_key=api_key)
32
+ self.model = model
33
+
34
+ def generate_commit(self, diff) -> str:
35
+ prompt = self.default_prompt + f"\n{diff}"
36
+ try:
37
+ response = self.client.chat.completions.create(
38
+ model=self.model,
39
+ messages=[
40
+ {"role": "user", "content": prompt},
41
+ ],
42
+ )
43
+ except AuthenticationError:
44
+ raise AIProviderAuthError("Invalid OpenAI API key.")
45
+
46
+ except RateLimitError:
47
+ raise AIProviderRateLimitError("OpenAI rate limit exceeded.")
48
+
49
+ except APITimeoutError:
50
+ raise AIProviderTimeout("OpenAI request timed out.")
51
+
52
+ except APIConnectionError:
53
+ raise AIProviderConnectionError("Failed to connect to OpenAI API.")
54
+
55
+ except OpenAIError as exc:
56
+ raise AIProviderConnectionError(f"Unexpected OpenAI error: {exc}") from exc
57
+
58
+ try:
59
+ message = response.choices[0].message.content.strip()
60
+ except Exception:
61
+ raise AIProviderResponseInvalid("OpenAI returned an invalid response.")
62
+
63
+ if not message:
64
+ raise AIProviderResponseInvalid("OpenAI returned an empty commit message.")
65
+
66
+ return message
@@ -0,0 +1,56 @@
1
+ from commitwise.config import (
2
+ OPENAI_API_KEY,
3
+ LOCAL_MODEL,
4
+ LOCAL_API_URL,
5
+ )
6
+
7
+ from commitwise.ai.openai_engine import OpenAIEngine
8
+ from commitwise.ai.local_engine import LocalAIEngine
9
+ from commitwise.errors import AIError, AIProviderNotConfigured
10
+
11
+
12
+ def generate_ai_commit_message(diff: str) -> str:
13
+ """
14
+ Generate a git commit message using the best available AI provider.
15
+
16
+ Priority:
17
+ 1. OpenAI (if OPENAI_API_KEY is set)
18
+ 2. Local AI (Ollama)
19
+
20
+ Raises a clear error if no AI provider is available.
21
+ """
22
+
23
+ last_error: AIError | None = None
24
+
25
+ # Try OpenAI
26
+ if OPENAI_API_KEY:
27
+ try:
28
+ engine = OpenAIEngine(
29
+ api_key=OPENAI_API_KEY,
30
+ model="gpt-4.1-mini",
31
+ )
32
+ return engine.generate_commit(diff)
33
+ except AIError as exc:
34
+ last_error = exc
35
+
36
+ # Fallback to Local AI
37
+ if LOCAL_MODEL and LOCAL_API_URL:
38
+ try:
39
+ engine = LocalAIEngine(
40
+ model=LOCAL_MODEL,
41
+ url=LOCAL_API_URL,
42
+ )
43
+ return engine.generate_commit(diff)
44
+ except AIError as exc:
45
+ last_error = exc
46
+
47
+ if last_error:
48
+ raise last_error
49
+
50
+ raise AIProviderNotConfigured(
51
+ "No AI provider is configured.\n\n"
52
+ "To use AI commits, you must either:\n"
53
+ "- Set OPENAI_API_KEY to use OpenAI\n"
54
+ "- Or install and run a local AI model (e.g. Ollama)\n\n"
55
+ "https://ollama.com"
56
+ )
@@ -11,6 +11,13 @@ from commitwise.git_utils import (
11
11
  )
12
12
  from commitwise.file_source import read_commit_file
13
13
  from commitwise.ai_source import generate_ai_commit_message
14
+ from commitwise.errors import (
15
+ CommitWiseError,
16
+ NoStagedChangesError,
17
+ UserAbortError,
18
+ AIProviderTimeout,
19
+ AIProviderAuthError,
20
+ )
14
21
 
15
22
 
16
23
  def main() -> None:
@@ -50,8 +57,38 @@ def main() -> None:
50
57
  git_commit_with_message(message)
51
58
  return
52
59
 
60
+ except UserAbortError:
61
+ print("\nCommit aborted.")
62
+ sys.exit(0)
63
+
64
+ except AIProviderTimeout:
65
+ print(
66
+ "\n[X] AI provider timed out.\n\n"
67
+ "This may happen due to a large diff or slow local inference.\n"
68
+ "Try committing smaller changes or another AI provider."
69
+ )
70
+ sys.exit(2)
71
+
72
+ except AIProviderAuthError:
73
+ print(
74
+ "\n[X] AI authentication failed.\n\n"
75
+ "Please check your API key configuration."
76
+ )
77
+ sys.exit(3)
78
+
79
+ except NoStagedChangesError:
80
+ print(
81
+ "\n[X] No staged changes found.\n\n"
82
+ "Please run `git add` before using CommitWise."
83
+ )
84
+ sys.exit(0)
85
+
86
+ except CommitWiseError as e:
87
+ print(f"\n[X] {e}", file=sys.stderr)
88
+ sys.exit(1)
89
+
53
90
  except Exception as e:
54
- print(f"\n [X] Error: {e}", file=sys.stderr)
91
+ print(f"\n[X] {e}", file=sys.stderr)
55
92
  sys.exit(1)
56
93
 
57
94
 
@@ -0,0 +1,106 @@
1
+ """
2
+ Centralized error definitions for CommitWise.
3
+
4
+ All domain-level errors should live here.
5
+ CLI layer is responsible for translating these errors
6
+ into user-friendly message
7
+ """
8
+
9
+
10
+ class CommitWiseError(Exception):
11
+ """Base exception for all CommitWise erros."""
12
+
13
+ pass
14
+
15
+
16
+ class NoStagedChangesError(CommitWiseError):
17
+ """Raised when no changes are staged for commit."""
18
+
19
+ pass
20
+
21
+
22
+ class InvalidUsageError(CommitWiseError):
23
+ """Raised when CLI arguments or usage is invalid"""
24
+
25
+ pass
26
+
27
+
28
+ class UserAbortError(CommitWiseError):
29
+ """Raised when user aborts an action (e.g. commit confirmation)"""
30
+
31
+ pass
32
+
33
+
34
+ class AIError(CommitWiseError):
35
+ """Base exception for all AI-related error"""
36
+
37
+ pass
38
+
39
+
40
+ class AIProviderNotConfigured(AIError):
41
+ """
42
+ No AI provider is configured.
43
+
44
+ Example:
45
+ - No OPENAI_API_KEY
46
+ - Ollama not installed
47
+ """
48
+
49
+ pass
50
+
51
+
52
+ class AIProviderUnavailable(AIError):
53
+ """
54
+ Provider exists but not reachable.
55
+
56
+ Example:
57
+ - Ollama installed but service not running
58
+ - OpenAI endpoint unreachable
59
+ """
60
+
61
+ pass
62
+
63
+
64
+ class AIProviderTimeout(AIError):
65
+ """
66
+ Provider did not respond in time.
67
+
68
+ Common causes:
69
+ - Large diff
70
+ - Slow local inference
71
+ - Network latency
72
+ """
73
+
74
+ pass
75
+
76
+
77
+ class AIProviderConnectionError(AIError):
78
+ """
79
+ Connection failed during request.
80
+
81
+ Example:
82
+ - Connection reset
83
+ - DNS failure
84
+ """
85
+
86
+ pass
87
+
88
+
89
+ class AIProviderResponseInvalid(AIError):
90
+ """
91
+ AI responded but output is invalid or empty.
92
+ """
93
+
94
+ pass
95
+
96
+
97
+ class AIProviderAuthError(AIError):
98
+ """Invalid or missing AIProvider API key"""
99
+
100
+ pass
101
+
102
+
103
+ class AIProviderRateLimitError(AIError):
104
+ """AIProvider rate limit exceeded"""
105
+
106
+ pass
@@ -3,6 +3,7 @@ import tempfile
3
3
  import os
4
4
 
5
5
  from commitwise.cli_utils import read_single_key
6
+ from commitwise.errors import NoStagedChangesError
6
7
 
7
8
 
8
9
  def get_staged_diff() -> str:
@@ -16,9 +17,8 @@ def get_staged_diff() -> str:
16
17
  diff = result.stdout.strip()
17
18
 
18
19
  if not diff:
19
- raise RuntimeError(
20
- "No staged changes found.\nPlease run `git add` before using CommitWise."
21
- )
20
+ raise NoStagedChangesError("No staged changes found.")
21
+
22
22
  return diff
23
23
 
24
24
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: commitwise
3
- Version: 0.1.5
3
+ Version: 0.1.6
4
4
  Summary: Generate clean git commit messages from files or AI
5
5
  Author-email: Abolfazl Hassan Zade <abolfazl.hassanzade.81@gmail.com>
6
6
  License: MIT
@@ -6,6 +6,7 @@ commitwise/ai_source.py
6
6
  commitwise/cli.py
7
7
  commitwise/cli_utils.py
8
8
  commitwise/config.py
9
+ commitwise/errors.py
9
10
  commitwise/file_source.py
10
11
  commitwise/git_utils.py
11
12
  commitwise.egg-info/PKG-INFO
@@ -1 +0,0 @@
1
- __version__='0.1.5'
@@ -1,39 +0,0 @@
1
- import requests
2
-
3
- from commitwise.ai.base import AIEngine
4
-
5
-
6
- class LocalAIEngine(AIEngine):
7
- """
8
- Local AI engine using Ollama.
9
- """
10
-
11
- def __init__(self, model: str, url: str):
12
- self.model = model
13
- self.url = url.rstrip("/")
14
-
15
- def generate_commit(self, diff: str) -> str:
16
- prompt = self.default_prompt + f"\n{diff}"
17
-
18
- response = requests.post(
19
- f"{self.url}/api/generate",
20
- json={
21
- "model": self.model,
22
- "prompt": prompt,
23
- "stream": False,
24
- },
25
- timeout=60,
26
- )
27
-
28
- if response.status_code != 200:
29
- raise RuntimeError(
30
- f"Local AI request failed (status {response.status_code})"
31
- )
32
-
33
- data = response.json()
34
- message = data.get("response", "").strip()
35
-
36
- if not message:
37
- raise RuntimeError("Local AI returned an empty commit message.")
38
-
39
- return message
@@ -1,35 +0,0 @@
1
- from commitwise.ai.base import AIEngine
2
-
3
-
4
- class OpenAIEngine(AIEngine):
5
- """
6
- OpenAI-based AI engine (optional).
7
- """
8
-
9
- def __init__(self, api_key: str, model: str):
10
- self.api_key = api_key
11
- self.model = model
12
-
13
- def generate_commit(self, diff) -> str:
14
- try:
15
- from openai import OpenAI
16
- except ImportError as exc:
17
- raise RuntimeError(
18
- "OpenAI SDK is not installed. Install it with: pip install openai"
19
- ) from exc
20
- client = OpenAI(api_key=self.api_key)
21
-
22
- prompt = self.default_prompt + f"\n{diff}"
23
-
24
- response = client.chat.completions.create(
25
- model=self.model,
26
- message=[
27
- {"role": "user", "content": prompt},
28
- ],
29
- )
30
-
31
- message = response.choices[0].message.content.strip()
32
-
33
- if not message:
34
- raise RuntimeError("OpenAI returned an empty commit message.")
35
- return message
@@ -1,48 +0,0 @@
1
- from commitwise.config import (
2
- OPENAI_API_KEY,
3
- LOCAL_MODEL,
4
- LOCAL_API_URL,
5
- )
6
-
7
- from commitwise.ai.openai_engine import OpenAIEngine
8
- from commitwise.ai.local_engine import LocalAIEngine
9
-
10
-
11
- def generate_ai_commit_message(diff: str) -> str:
12
- """
13
- Generate a git commit message using the best available AI provider.
14
-
15
- Priority:
16
- 1. OpenAI (if OPENAI_API_KEY is set)
17
- 2. Local AI (Ollama)
18
-
19
- Raises a clear error if no AI provider is available.
20
- """
21
- # Try OpenAI
22
- if OPENAI_API_KEY:
23
- try:
24
- engine = OpenAIEngine(
25
- api_key=OPENAI_API_KEY,
26
- model="gpt-4.1-mini",
27
- )
28
- return engine.generate_commit(diff)
29
- except Exception as exc:
30
- raise RuntimeError(
31
- "Failed to generate commit message using OpenAI."
32
- ) from exc
33
-
34
- # Fallback to Local AI
35
- try:
36
- engine = LocalAIEngine(
37
- model=LOCAL_MODEL,
38
- url=LOCAL_API_URL,
39
- )
40
- return engine.generate_commit(diff)
41
- except Exception as exc:
42
- raise RuntimeError(
43
- "No AI provider available.\n\n"
44
- "To use AI commits, you must either:\n"
45
- "- Set OPENAI_API_KEY to use OpenAI\n"
46
- "- Or install and run a local AI model (e.g. Ollama)\n\n"
47
- "https://ollama.com"
48
- ) from exc
File without changes
File without changes
File without changes
File without changes