commitron 1.0.0rc4__py3-none-any.whl
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.
- commitron/__init__.py +1 -0
- commitron/__main__.py +3 -0
- commitron/ai.py +407 -0
- commitron/cli.py +617 -0
- commitron/config.py +289 -0
- commitron/credentials.py +124 -0
- commitron/git_ops.py +175 -0
- commitron/providers.py +337 -0
- commitron/safety.py +28 -0
- commitron-1.0.0rc4.dist-info/METADATA +393 -0
- commitron-1.0.0rc4.dist-info/RECORD +15 -0
- commitron-1.0.0rc4.dist-info/WHEEL +5 -0
- commitron-1.0.0rc4.dist-info/entry_points.txt +3 -0
- commitron-1.0.0rc4.dist-info/licenses/LICENSE +21 -0
- commitron-1.0.0rc4.dist-info/top_level.txt +1 -0
commitron/providers.py
ADDED
|
@@ -0,0 +1,337 @@
|
|
|
1
|
+
"""AI provider adapters. Each adapter owns its endpoint and authentication details."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
import json
|
|
5
|
+
import os
|
|
6
|
+
import time
|
|
7
|
+
import urllib.error
|
|
8
|
+
import urllib.parse
|
|
9
|
+
import urllib.request
|
|
10
|
+
from typing import Any, Dict, Protocol
|
|
11
|
+
|
|
12
|
+
from .credentials import get_credential
|
|
13
|
+
|
|
14
|
+
# ---------------------------------------------------------------------------
|
|
15
|
+
# Provider registry (extensible; new providers via register_provider)
|
|
16
|
+
# ---------------------------------------------------------------------------
|
|
17
|
+
|
|
18
|
+
PROVIDERS: Dict[str, Any] = {}
|
|
19
|
+
PROVIDER_KEY_VARS: Dict[str, str] = {}
|
|
20
|
+
DEFAULT_MODELS: Dict[str, str] = {}
|
|
21
|
+
DEFAULT_URLS: Dict[str, str] = {}
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def register_provider(
|
|
25
|
+
name: str,
|
|
26
|
+
provider_class: Any,
|
|
27
|
+
key_env: str = "",
|
|
28
|
+
default_model: str = "",
|
|
29
|
+
default_url: str = "",
|
|
30
|
+
) -> None:
|
|
31
|
+
"""Register a new AI provider adapter.
|
|
32
|
+
|
|
33
|
+
- name: lowercase provider name (e.g. "groq", "anthropic")
|
|
34
|
+
- provider_class: class(model, base_url) implementing the same contract
|
|
35
|
+
- key_env: environment variable for the API key ("", if no key required)
|
|
36
|
+
- default_model / default_url: fallbacks when user does not configure them
|
|
37
|
+
"""
|
|
38
|
+
PROVIDERS[name.lower()] = provider_class
|
|
39
|
+
if key_env:
|
|
40
|
+
PROVIDER_KEY_VARS[name.lower()] = key_env
|
|
41
|
+
if default_model:
|
|
42
|
+
DEFAULT_MODELS[name.lower()] = default_model
|
|
43
|
+
if default_url:
|
|
44
|
+
DEFAULT_URLS[name.lower()] = default_url
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
class ProviderError(RuntimeError):
|
|
48
|
+
"""Raised when a provider request or response fails."""
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
class AIProvider(Protocol):
|
|
52
|
+
"""Common contract implemented by every provider adapter."""
|
|
53
|
+
|
|
54
|
+
def generate(self, prompt: str, max_tokens: int) -> str:
|
|
55
|
+
"""Generate plain text from a prompt."""
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
class _NoRedirectHandler(urllib.request.HTTPRedirectHandler):
|
|
59
|
+
"""Do not forward provider credentials through unexpected redirects."""
|
|
60
|
+
|
|
61
|
+
def redirect_request(self, req, fp, code, msg, headers, newurl):
|
|
62
|
+
return None
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def _request_json(url: str, payload: Dict, headers: Dict[str, str]) -> Dict:
|
|
66
|
+
request = urllib.request.Request(
|
|
67
|
+
url, data=json.dumps(payload).encode("utf-8"),
|
|
68
|
+
headers={"Content-Type": "application/json", **headers}, method="POST",
|
|
69
|
+
)
|
|
70
|
+
opener = urllib.request.build_opener(_NoRedirectHandler())
|
|
71
|
+
default_retries = 1 if "localhost" in url or "127.0.0.1" in url else 3
|
|
72
|
+
max_retries = int(os.environ.get("COMMITRON_RETRY_COUNT", str(default_retries)))
|
|
73
|
+
base_delay = float(os.environ.get("COMMITRON_RETRY_DELAY", "1.0"))
|
|
74
|
+
verbose = os.environ.get("COMMITRON_VERBOSE", "").strip().lower() in {"1", "true", "yes"}
|
|
75
|
+
last_exc: BaseException | None = None
|
|
76
|
+
for attempt in range(1, max_retries + 1):
|
|
77
|
+
try:
|
|
78
|
+
with opener.open(request, timeout=90) as response:
|
|
79
|
+
raw_bytes = response.read(2_000_001)
|
|
80
|
+
if len(raw_bytes) > 2_000_000:
|
|
81
|
+
raise ProviderError("AI provider response exceeded the 2 MB safety limit.")
|
|
82
|
+
raw = raw_bytes.decode("utf-8")
|
|
83
|
+
data = json.loads(raw)
|
|
84
|
+
if not isinstance(data, dict):
|
|
85
|
+
raise ProviderError("AI provider returned an unexpected response.")
|
|
86
|
+
if "error" in data:
|
|
87
|
+
error = data["error"]
|
|
88
|
+
detail = error.get("message", str(error)) if isinstance(error, dict) else str(error)
|
|
89
|
+
raise ProviderError(str(detail))
|
|
90
|
+
return data
|
|
91
|
+
except urllib.error.HTTPError as exc:
|
|
92
|
+
# Retry transient errors: rate limits (429), server errors (5xx), timeouts (408/503).
|
|
93
|
+
# Client errors (4xx) other than 429 are not retried; report immediately.
|
|
94
|
+
if 400 <= exc.code < 500 and exc.code != 429:
|
|
95
|
+
detail = exc.read(1000).decode("utf-8", "replace")
|
|
96
|
+
raise ProviderError("AI provider returned HTTP {}: {}".format(exc.code, detail)) from exc
|
|
97
|
+
last_exc = exc
|
|
98
|
+
except (urllib.error.URLError, TimeoutError, OSError) as exc:
|
|
99
|
+
last_exc = exc
|
|
100
|
+
except (UnicodeDecodeError, json.JSONDecodeError) as exc:
|
|
101
|
+
# Malformed response may be transient; retry before failing.
|
|
102
|
+
last_exc = exc
|
|
103
|
+
except Exception as exc:
|
|
104
|
+
last_exc = exc
|
|
105
|
+
|
|
106
|
+
if attempt == max_retries:
|
|
107
|
+
raise ProviderError("AI request failed after {} attempts: {}".format(max_retries, str(last_exc))) from last_exc
|
|
108
|
+
delay = base_delay * (2 ** (attempt - 1))
|
|
109
|
+
if verbose:
|
|
110
|
+
print("[debug] AI request attempt {}/{} failed ({}); retrying in {:.1f}s...".format(
|
|
111
|
+
attempt, max_retries, type(last_exc).__name__, delay))
|
|
112
|
+
time.sleep(delay)
|
|
113
|
+
raise ProviderError("AI request failed.")
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def _extract_text(data: Dict, provider: str) -> str:
|
|
117
|
+
try:
|
|
118
|
+
if provider == "gemini":
|
|
119
|
+
return "\n".join(
|
|
120
|
+
part["text"] for part in data["candidates"][0]["content"]["parts"]
|
|
121
|
+
if isinstance(part, dict) and isinstance(part.get("text"), str)
|
|
122
|
+
)
|
|
123
|
+
if provider in {"openai", "groq", "deepseek", "openrouter"}:
|
|
124
|
+
content = data["choices"][0]["message"]["content"]
|
|
125
|
+
if isinstance(content, str):
|
|
126
|
+
return content
|
|
127
|
+
if isinstance(content, list):
|
|
128
|
+
text_parts = [
|
|
129
|
+
item.get("text", "") for item in content
|
|
130
|
+
if isinstance(item, dict) and isinstance(item.get("text"), str)
|
|
131
|
+
]
|
|
132
|
+
return "\n".join(text_parts)
|
|
133
|
+
if provider == "ollama":
|
|
134
|
+
return data["message"]["content"]
|
|
135
|
+
if provider == "anthropic":
|
|
136
|
+
return "\n".join(
|
|
137
|
+
part["text"] for part in data.get("content", [])
|
|
138
|
+
if isinstance(part, dict) and isinstance(part.get("text"), str)
|
|
139
|
+
)
|
|
140
|
+
except (KeyError, IndexError, TypeError) as exc:
|
|
141
|
+
raise ProviderError("{}. returned no usable text.".format(provider.title())) from exc
|
|
142
|
+
raise ProviderError("{}. returned no usable text.".format(provider.title()))
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
class BaseProvider:
|
|
146
|
+
"""Common helpers for HTTP-based provider adapters."""
|
|
147
|
+
|
|
148
|
+
def __init__(self, model: str, base_url: str):
|
|
149
|
+
self.model = model
|
|
150
|
+
self.base_url = base_url
|
|
151
|
+
|
|
152
|
+
def _request(self, endpoint: str, payload: Dict) -> str:
|
|
153
|
+
url = self.base_url.rstrip("/") + "/" + endpoint.lstrip("/")
|
|
154
|
+
data = _request_json(url, payload, self.headers())
|
|
155
|
+
return _extract_text(data, self.PROVIDER_NAME)
|
|
156
|
+
|
|
157
|
+
def headers(self) -> Dict[str, str]:
|
|
158
|
+
return {}
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
class GeminiProvider(BaseProvider):
|
|
162
|
+
PROVIDER_NAME = "gemini"
|
|
163
|
+
|
|
164
|
+
def generate(self, prompt: str, max_tokens: int) -> str:
|
|
165
|
+
api_key = get_credential(self.PROVIDER_NAME)
|
|
166
|
+
if not api_key:
|
|
167
|
+
raise ProviderError("No Gemini API key found. Set GEMINI_API_KEY or save one with 'commitron auth set gemini'.")
|
|
168
|
+
endpoint = urllib.parse.quote(self.model, safe="") + ":generateContent"
|
|
169
|
+
payload = {
|
|
170
|
+
"contents": [{"role": "user", "parts": [{"text": prompt}]}],
|
|
171
|
+
"generationConfig": {"temperature": 0.35, "maxOutputTokens": max_tokens},
|
|
172
|
+
}
|
|
173
|
+
return self._request(endpoint, payload)
|
|
174
|
+
|
|
175
|
+
def headers(self) -> Dict[str, str]:
|
|
176
|
+
return {"x-goog-api-key": get_credential(self.PROVIDER_NAME)}
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
class OpenAIProvider(BaseProvider):
|
|
180
|
+
PROVIDER_NAME = "openai"
|
|
181
|
+
|
|
182
|
+
def generate(self, prompt: str, max_tokens: int) -> str:
|
|
183
|
+
api_key = get_credential(self.PROVIDER_NAME)
|
|
184
|
+
if not api_key:
|
|
185
|
+
raise ProviderError("No OpenAI API key found. Set OPENAI_API_KEY or save one with 'commitron auth set openai'.")
|
|
186
|
+
endpoint = "chat/completions"
|
|
187
|
+
payload = {
|
|
188
|
+
"model": self.model,
|
|
189
|
+
"messages": [{"role": "user", "content": prompt}],
|
|
190
|
+
"temperature": 0.35,
|
|
191
|
+
"max_completion_tokens": max_tokens,
|
|
192
|
+
}
|
|
193
|
+
return self._request(endpoint, payload)
|
|
194
|
+
|
|
195
|
+
def headers(self) -> Dict[str, str]:
|
|
196
|
+
return {"Authorization": "Bearer " + get_credential(self.PROVIDER_NAME)}
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
class OllamaProvider(BaseProvider):
|
|
200
|
+
PROVIDER_NAME = "ollama"
|
|
201
|
+
|
|
202
|
+
def generate(self, prompt: str, max_tokens: int) -> str:
|
|
203
|
+
endpoint = "api/chat"
|
|
204
|
+
payload = {
|
|
205
|
+
"model": self.model,
|
|
206
|
+
"messages": [{"role": "user", "content": prompt}],
|
|
207
|
+
"stream": False,
|
|
208
|
+
"options": {"temperature": 0.35, "num_predict": max_tokens},
|
|
209
|
+
}
|
|
210
|
+
return self._request(endpoint, payload)
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
class AnthropicProvider(BaseProvider):
|
|
214
|
+
PROVIDER_NAME = "anthropic"
|
|
215
|
+
|
|
216
|
+
def generate(self, prompt: str, max_tokens: int) -> str:
|
|
217
|
+
api_key = get_credential(self.PROVIDER_NAME)
|
|
218
|
+
if not api_key:
|
|
219
|
+
raise ProviderError(
|
|
220
|
+
"No Anthropic API key found. Set ANTHROPIC_API_KEY or save one with 'commitron auth set anthropic'."
|
|
221
|
+
)
|
|
222
|
+
endpoint = "messages"
|
|
223
|
+
payload = {
|
|
224
|
+
"model": self.model or "claude-3-haiku-20240307",
|
|
225
|
+
"messages": [{"role": "user", "content": prompt}],
|
|
226
|
+
"max_tokens": max_tokens,
|
|
227
|
+
"temperature": 0.35,
|
|
228
|
+
}
|
|
229
|
+
return self._request(endpoint, payload)
|
|
230
|
+
|
|
231
|
+
def headers(self) -> Dict[str, str]:
|
|
232
|
+
return {
|
|
233
|
+
"x-api-key": get_credential(self.PROVIDER_NAME),
|
|
234
|
+
"anthropic-version": "2023-06-01",
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
class GroqProvider(BaseProvider):
|
|
239
|
+
PROVIDER_NAME = "groq"
|
|
240
|
+
|
|
241
|
+
def generate(self, prompt: str, max_tokens: int) -> str:
|
|
242
|
+
api_key = get_credential(self.PROVIDER_NAME)
|
|
243
|
+
if not api_key:
|
|
244
|
+
raise ProviderError(
|
|
245
|
+
"No Groq API key found. Set GROQ_API_KEY or save one with 'commitron auth set groq'."
|
|
246
|
+
)
|
|
247
|
+
endpoint = "chat/completions"
|
|
248
|
+
payload = {
|
|
249
|
+
"model": self.model or "llama-3.3-70b-versatile",
|
|
250
|
+
"messages": [{"role": "user", "content": prompt}],
|
|
251
|
+
"max_tokens": max_tokens,
|
|
252
|
+
"temperature": 0.35,
|
|
253
|
+
}
|
|
254
|
+
return self._request(endpoint, payload)
|
|
255
|
+
|
|
256
|
+
def headers(self) -> Dict[str, str]:
|
|
257
|
+
return {"Authorization": "Bearer " + get_credential(self.PROVIDER_NAME)}
|
|
258
|
+
|
|
259
|
+
|
|
260
|
+
class DeepSeekProvider(BaseProvider):
|
|
261
|
+
PROVIDER_NAME = "deepseek"
|
|
262
|
+
|
|
263
|
+
def generate(self, prompt: str, max_tokens: int) -> str:
|
|
264
|
+
api_key = get_credential(self.PROVIDER_NAME)
|
|
265
|
+
if not api_key:
|
|
266
|
+
raise ProviderError(
|
|
267
|
+
"No DeepSeek API key found. Set DEEPSEEK_API_KEY or save one with 'commitron auth set deepseek'."
|
|
268
|
+
)
|
|
269
|
+
endpoint = "chat/completions"
|
|
270
|
+
payload = {
|
|
271
|
+
"model": self.model or "deepseek-chat",
|
|
272
|
+
"messages": [{"role": "user", "content": prompt}],
|
|
273
|
+
"max_tokens": max_tokens,
|
|
274
|
+
"temperature": 0.35,
|
|
275
|
+
}
|
|
276
|
+
return self._request(endpoint, payload)
|
|
277
|
+
|
|
278
|
+
def headers(self) -> Dict[str, str]:
|
|
279
|
+
return {"Authorization": "Bearer " + get_credential(self.PROVIDER_NAME)}
|
|
280
|
+
|
|
281
|
+
|
|
282
|
+
class OpenRouterProvider(BaseProvider):
|
|
283
|
+
PROVIDER_NAME = "openrouter"
|
|
284
|
+
|
|
285
|
+
def generate(self, prompt: str, max_tokens: int) -> str:
|
|
286
|
+
api_key = get_credential(self.PROVIDER_NAME)
|
|
287
|
+
if not api_key:
|
|
288
|
+
raise ProviderError(
|
|
289
|
+
"No OpenRouter API key found. Set OPENROUTER_API_KEY or save one with 'commitron auth set openrouter'."
|
|
290
|
+
)
|
|
291
|
+
endpoint = "chat/completions"
|
|
292
|
+
payload = {
|
|
293
|
+
"model": self.model or "openai/gpt-4o-mini",
|
|
294
|
+
"messages": [{"role": "user", "content": prompt}],
|
|
295
|
+
"max_tokens": max_tokens,
|
|
296
|
+
"temperature": 0.35,
|
|
297
|
+
}
|
|
298
|
+
return self._request(endpoint, payload)
|
|
299
|
+
|
|
300
|
+
def headers(self) -> Dict[str, str]:
|
|
301
|
+
return {
|
|
302
|
+
"Authorization": "Bearer " + get_credential(self.PROVIDER_NAME),
|
|
303
|
+
"HTTP-Referer": "https://github.com/Error404-uv/commitron",
|
|
304
|
+
"X-Title": "commitron",
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
|
|
308
|
+
def create_provider(name: str, model: str = "", base_url: str = "") -> AIProvider:
|
|
309
|
+
"""Create the configured provider adapter without making a network request."""
|
|
310
|
+
provider_name = name.lower()
|
|
311
|
+
if provider_name not in PROVIDERS:
|
|
312
|
+
raise ProviderError(
|
|
313
|
+
"Unsupported AI provider: {}. Supported providers: {}."
|
|
314
|
+
.format(provider_name, ", ".join(sorted(PROVIDERS)))
|
|
315
|
+
)
|
|
316
|
+
resolved_model = model.strip() or DEFAULT_MODELS.get(provider_name, "")
|
|
317
|
+
resolved_url = base_url.strip() or DEFAULT_URLS.get(provider_name, "")
|
|
318
|
+
return PROVIDERS[provider_name](resolved_model, resolved_url)
|
|
319
|
+
|
|
320
|
+
|
|
321
|
+
# ---------------------------------------------------------------------------
|
|
322
|
+
# Bootstrap: register built-in providers (all have free tiers or no-key)
|
|
323
|
+
# ---------------------------------------------------------------------------
|
|
324
|
+
|
|
325
|
+
register_provider("gemini", GeminiProvider, "GEMINI_API_KEY", "gemini-2.5-flash",
|
|
326
|
+
"https://generativelanguage.googleapis.com/v1beta/models")
|
|
327
|
+
register_provider("openai", OpenAIProvider, "OPENAI_API_KEY", "gpt-4.1-mini",
|
|
328
|
+
"https://api.openai.com/v1")
|
|
329
|
+
register_provider("ollama", OllamaProvider, "", "qwen2.5-coder:7b", "http://localhost:11434")
|
|
330
|
+
register_provider("anthropic", AnthropicProvider, "ANTHROPIC_API_KEY", "claude-3-haiku-20240307",
|
|
331
|
+
"https://api.anthropic.com/v1")
|
|
332
|
+
register_provider("groq", GroqProvider, "GROQ_API_KEY", "llama-3.3-70b-versatile",
|
|
333
|
+
"https://api.groq.com/openai/v1")
|
|
334
|
+
register_provider("deepseek", DeepSeekProvider, "DEEPSEEK_API_KEY", "deepseek-chat",
|
|
335
|
+
"https://api.deepseek.com/v1")
|
|
336
|
+
register_provider("openrouter", OpenRouterProvider, "OPENROUTER_API_KEY", "openai/gpt-4o-mini",
|
|
337
|
+
"https://openrouter.ai/api/v1")
|
commitron/safety.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"""Local heuristic checks for likely credentials in content sent to an AI provider."""
|
|
2
|
+
import re
|
|
3
|
+
from typing import List, Tuple
|
|
4
|
+
|
|
5
|
+
# This intentionally errs toward warnings. It is not a complete secret scanner.
|
|
6
|
+
_PATTERNS: List[Tuple[str, re.Pattern]] = [
|
|
7
|
+
("private key", re.compile(r"-----BEGIN (?:RSA |EC |OPENSSH |DSA |ENCRYPTED )?PRIVATE KEY-----")),
|
|
8
|
+
("Google API key", re.compile(r"\bAIza[0-9A-Za-z_-]{30,}\b")),
|
|
9
|
+
("AWS access key", re.compile(r"\b(?:AKIA|ASIA)[0-9A-Z]{16}\b")),
|
|
10
|
+
("GitHub token", re.compile(r"\b(?:gh[pousr]_[A-Za-z0-9]{20,}|github_pat_[A-Za-z0-9_]{20,})\b")),
|
|
11
|
+
("GitLab token", re.compile(r"\bglpat-[A-Za-z0-9_-]{20,}\b")),
|
|
12
|
+
("Slack token", re.compile(r"\bxox[baprs]-[A-Za-z0-9-]{10,}\b")),
|
|
13
|
+
("Stripe secret key", re.compile(r"\b(?:sk|rk)_(?:live|test)_[A-Za-z0-9]{16,}\b")),
|
|
14
|
+
("npm token", re.compile(r"\bnpm_[A-Za-z0-9]{30,}\b")),
|
|
15
|
+
("JWT-like token", re.compile(r"\beyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}\b")),
|
|
16
|
+
(
|
|
17
|
+
"generic credential assignment",
|
|
18
|
+
re.compile(
|
|
19
|
+
r"(?i)\b(?:api[_-]?key|access[_-]?token|auth[_-]?token|client[_-]?secret|"
|
|
20
|
+
r"password|passwd|secret)\s*[:=]\s*['\"]?[^\s'\"]{8,}"
|
|
21
|
+
),
|
|
22
|
+
),
|
|
23
|
+
]
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def find_secret_warnings(text: str) -> List[str]:
|
|
27
|
+
"""Return unique warning labels for patterns found in the supplied text."""
|
|
28
|
+
return [label for label, pattern in _PATTERNS if pattern.search(text)]
|