gac 1.13.0__py3-none-any.whl → 3.8.1__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.
Files changed (54) hide show
  1. gac/__version__.py +1 -1
  2. gac/ai.py +33 -47
  3. gac/ai_utils.py +113 -41
  4. gac/auth_cli.py +214 -0
  5. gac/cli.py +72 -2
  6. gac/config.py +63 -6
  7. gac/config_cli.py +26 -5
  8. gac/constants.py +178 -2
  9. gac/git.py +158 -12
  10. gac/init_cli.py +40 -125
  11. gac/language_cli.py +378 -0
  12. gac/main.py +868 -158
  13. gac/model_cli.py +429 -0
  14. gac/oauth/__init__.py +27 -0
  15. gac/oauth/claude_code.py +464 -0
  16. gac/oauth/qwen_oauth.py +323 -0
  17. gac/oauth/token_store.py +81 -0
  18. gac/preprocess.py +3 -3
  19. gac/prompt.py +573 -226
  20. gac/providers/__init__.py +49 -0
  21. gac/providers/anthropic.py +11 -1
  22. gac/providers/azure_openai.py +101 -0
  23. gac/providers/cerebras.py +11 -1
  24. gac/providers/chutes.py +11 -1
  25. gac/providers/claude_code.py +112 -0
  26. gac/providers/custom_anthropic.py +6 -2
  27. gac/providers/custom_openai.py +6 -3
  28. gac/providers/deepseek.py +11 -1
  29. gac/providers/fireworks.py +11 -1
  30. gac/providers/gemini.py +11 -1
  31. gac/providers/groq.py +5 -1
  32. gac/providers/kimi_coding.py +67 -0
  33. gac/providers/lmstudio.py +12 -1
  34. gac/providers/minimax.py +11 -1
  35. gac/providers/mistral.py +48 -0
  36. gac/providers/moonshot.py +48 -0
  37. gac/providers/ollama.py +11 -1
  38. gac/providers/openai.py +11 -1
  39. gac/providers/openrouter.py +11 -1
  40. gac/providers/qwen.py +76 -0
  41. gac/providers/replicate.py +110 -0
  42. gac/providers/streamlake.py +11 -1
  43. gac/providers/synthetic.py +11 -1
  44. gac/providers/together.py +11 -1
  45. gac/providers/zai.py +11 -1
  46. gac/security.py +1 -1
  47. gac/utils.py +272 -4
  48. gac/workflow_utils.py +217 -0
  49. {gac-1.13.0.dist-info → gac-3.8.1.dist-info}/METADATA +90 -27
  50. gac-3.8.1.dist-info/RECORD +56 -0
  51. {gac-1.13.0.dist-info → gac-3.8.1.dist-info}/WHEEL +1 -1
  52. gac-1.13.0.dist-info/RECORD +0 -41
  53. {gac-1.13.0.dist-info → gac-3.8.1.dist-info}/entry_points.txt +0 -0
  54. {gac-1.13.0.dist-info → gac-3.8.1.dist-info}/licenses/LICENSE +0 -0
gac/model_cli.py ADDED
@@ -0,0 +1,429 @@
1
+ """CLI for managing gac model configuration in $HOME/.gac.env."""
2
+
3
+ import os
4
+ from pathlib import Path
5
+
6
+ import click
7
+ import questionary
8
+ from dotenv import dotenv_values, load_dotenv, set_key
9
+
10
+ GAC_ENV_PATH = Path.home() / ".gac.env"
11
+
12
+
13
+ def _should_show_rtl_warning_for_init() -> bool:
14
+ """Check if RTL warning should be shown based on init's GAC_ENV_PATH.
15
+
16
+ Returns:
17
+ True if warning should be shown, False if user previously confirmed
18
+ """
19
+ if GAC_ENV_PATH.exists():
20
+ load_dotenv(GAC_ENV_PATH)
21
+ rtl_confirmed = os.getenv("GAC_RTL_CONFIRMED", "false").lower() in ("true", "1", "yes", "on")
22
+ return not rtl_confirmed
23
+ return True # Show warning if no config exists
24
+
25
+
26
+ def _show_rtl_warning_for_init(language_name: str) -> bool:
27
+ """Show RTL language warning for init command and save preference to GAC_ENV_PATH.
28
+
29
+ Args:
30
+ language_name: Name of the RTL language
31
+
32
+ Returns:
33
+ True if user wants to proceed, False if they cancel
34
+ """
35
+
36
+ terminal_width = 80 # Use default width
37
+ title = "⚠️ RTL Language Detected".center(terminal_width)
38
+
39
+ click.echo()
40
+ click.echo(click.style(title, fg="yellow", bold=True))
41
+ click.echo()
42
+ click.echo("Right-to-left (RTL) languages may not display correctly in gac due to terminal limitations.")
43
+ click.echo("However, the commit messages will work fine and should be readable in Git clients")
44
+ click.echo("that properly support RTL text (like most web interfaces and modern tools).\n")
45
+
46
+ proceed = questionary.confirm("Do you want to proceed anyway?").ask()
47
+ if proceed:
48
+ # Remember that user has confirmed RTL acceptance
49
+ set_key(str(GAC_ENV_PATH), "GAC_RTL_CONFIRMED", "true")
50
+ return True
51
+ else:
52
+ click.echo("RTL language setup cancelled.")
53
+ return False
54
+
55
+
56
+ def _prompt_required_text(prompt: str) -> str | None:
57
+ """Prompt until a non-empty string is provided or the user cancels."""
58
+ while True:
59
+ response = questionary.text(prompt).ask()
60
+ if response is None:
61
+ return None
62
+ value = response.strip()
63
+ if value:
64
+ return value # type: ignore[no-any-return]
65
+ click.echo("A value is required. Please try again.")
66
+
67
+
68
+ def _load_existing_env() -> dict[str, str]:
69
+ """Ensure the env file exists and return its current values."""
70
+ existing_env: dict[str, str] = {}
71
+ if GAC_ENV_PATH.exists():
72
+ click.echo(f"$HOME/.gac.env already exists at {GAC_ENV_PATH}. Values will be updated.")
73
+ existing_env = {k: v for k, v in dotenv_values(str(GAC_ENV_PATH)).items() if v is not None}
74
+ else:
75
+ GAC_ENV_PATH.touch()
76
+ click.echo(f"Created $HOME/.gac.env at {GAC_ENV_PATH}.")
77
+ return existing_env
78
+
79
+
80
+ def _configure_model(existing_env: dict[str, str]) -> bool:
81
+ """Run the provider/model/API key configuration flow."""
82
+ providers = [
83
+ ("Anthropic", "claude-haiku-4-5"),
84
+ ("Azure OpenAI", "gpt-5-mini"),
85
+ ("Cerebras", "zai-glm-4.6"),
86
+ ("Chutes", "zai-org/GLM-4.6-FP8"),
87
+ ("Claude Code (OAuth)", "claude-sonnet-4-5"),
88
+ ("Custom (Anthropic)", ""),
89
+ ("Custom (OpenAI)", ""),
90
+ ("DeepSeek", "deepseek-chat"),
91
+ ("Fireworks", "accounts/fireworks/models/gpt-oss-20b"),
92
+ ("Gemini", "gemini-2.5-flash"),
93
+ ("Groq", "meta-llama/llama-4-maverick-17b-128e-instruct"),
94
+ ("Kimi for Coding", "kimi-for-coding"),
95
+ ("LM Studio", "gemma3"),
96
+ ("MiniMax.io", "MiniMax-M2"),
97
+ ("Mistral", "mistral-small-latest"),
98
+ ("Moonshot AI", "kimi-k2-thinking-turbo"),
99
+ ("Ollama", "gemma3"),
100
+ ("OpenAI", "gpt-5-mini"),
101
+ ("OpenRouter", "openrouter/auto"),
102
+ ("Qwen.ai (OAuth)", "qwen3-coder-plus"),
103
+ ("Replicate", "openai/gpt-oss-120b"),
104
+ ("Streamlake", ""),
105
+ ("Synthetic.new", "hf:zai-org/GLM-4.6"),
106
+ ("Together AI", "openai/gpt-oss-20B"),
107
+ ("Z.AI", "glm-4.5-air"),
108
+ ("Z.AI Coding", "glm-4.6"),
109
+ ]
110
+ provider_names = [p[0] for p in providers]
111
+ provider = questionary.select(
112
+ "Select your provider:", choices=provider_names, use_shortcuts=True, use_arrow_keys=True, use_jk_keys=False
113
+ ).ask()
114
+ if not provider:
115
+ click.echo("Provider selection cancelled. Exiting.")
116
+ return False
117
+ provider_key = provider.lower().replace(".", "").replace(" ", "-").replace("(", "").replace(")", "")
118
+
119
+ is_azure_openai = provider_key == "azure-openai"
120
+ is_claude_code = provider_key == "claude-code-oauth"
121
+ is_custom_anthropic = provider_key == "custom-anthropic"
122
+ is_custom_openai = provider_key == "custom-openai"
123
+ is_lmstudio = provider_key == "lm-studio"
124
+ is_ollama = provider_key == "ollama"
125
+ is_qwen = provider_key == "qwenai-oauth"
126
+ is_streamlake = provider_key == "streamlake"
127
+ is_zai = provider_key in ("zai", "zai-coding")
128
+
129
+ if provider_key == "claude-code-oauth":
130
+ provider_key = "claude-code"
131
+ elif provider_key == "kimi-for-coding":
132
+ provider_key = "kimi-coding"
133
+ elif provider_key == "minimaxio":
134
+ provider_key = "minimax"
135
+ elif provider_key == "moonshot-ai":
136
+ provider_key = "moonshot"
137
+ elif provider_key == "qwenai-oauth":
138
+ provider_key = "qwen"
139
+ elif provider_key == "syntheticnew":
140
+ provider_key = "synthetic"
141
+
142
+ if is_streamlake:
143
+ endpoint_id = _prompt_required_text("Enter the Streamlake inference endpoint ID (required):")
144
+ if endpoint_id is None:
145
+ click.echo("Streamlake configuration cancelled. Exiting.")
146
+ return False
147
+ model_to_save = endpoint_id
148
+ else:
149
+ model_suggestion = dict(providers)[provider]
150
+ if model_suggestion == "":
151
+ model_prompt = "Enter the model (required):"
152
+ else:
153
+ model_prompt = f"Enter the model (default: {model_suggestion}):"
154
+ model = questionary.text(model_prompt, default=model_suggestion).ask()
155
+ if model is None:
156
+ click.echo("Model entry cancelled. Exiting.")
157
+ return False
158
+ model_to_save = model.strip() if model.strip() else model_suggestion
159
+
160
+ set_key(str(GAC_ENV_PATH), "GAC_MODEL", f"{provider_key}:{model_to_save}")
161
+ click.echo(f"Set GAC_MODEL={provider_key}:{model_to_save}")
162
+
163
+ if is_custom_anthropic:
164
+ base_url = _prompt_required_text("Enter the custom Anthropic-compatible base URL (required):")
165
+ if base_url is None:
166
+ click.echo("Custom Anthropic base URL entry cancelled. Exiting.")
167
+ return False
168
+ set_key(str(GAC_ENV_PATH), "CUSTOM_ANTHROPIC_BASE_URL", base_url)
169
+ click.echo(f"Set CUSTOM_ANTHROPIC_BASE_URL={base_url}")
170
+
171
+ api_version = questionary.text(
172
+ "Enter the API version (optional, press Enter for default: 2023-06-01):", default="2023-06-01"
173
+ ).ask()
174
+ if api_version and api_version != "2023-06-01":
175
+ set_key(str(GAC_ENV_PATH), "CUSTOM_ANTHROPIC_VERSION", api_version)
176
+ click.echo(f"Set CUSTOM_ANTHROPIC_VERSION={api_version}")
177
+ elif is_azure_openai:
178
+ # Check for existing endpoint
179
+ existing_endpoint = existing_env.get("AZURE_OPENAI_ENDPOINT")
180
+ if existing_endpoint:
181
+ click.echo(f"\nAZURE_OPENAI_ENDPOINT is already configured: {existing_endpoint}")
182
+ endpoint_action = questionary.select(
183
+ "What would you like to do?",
184
+ choices=[
185
+ "Keep existing endpoint",
186
+ "Enter new endpoint",
187
+ ],
188
+ use_shortcuts=True,
189
+ use_arrow_keys=True,
190
+ use_jk_keys=False,
191
+ ).ask()
192
+
193
+ if endpoint_action == "Enter new endpoint":
194
+ endpoint = _prompt_required_text("Enter the Azure OpenAI endpoint (required):")
195
+ if endpoint is None:
196
+ click.echo("Azure OpenAI endpoint entry cancelled. Exiting.")
197
+ return False
198
+ set_key(str(GAC_ENV_PATH), "AZURE_OPENAI_ENDPOINT", endpoint)
199
+ click.echo(f"Set AZURE_OPENAI_ENDPOINT={endpoint}")
200
+ else:
201
+ endpoint = existing_endpoint
202
+ click.echo(f"Keeping existing AZURE_OPENAI_ENDPOINT={endpoint}")
203
+ else:
204
+ endpoint = _prompt_required_text("Enter the Azure OpenAI endpoint (required):")
205
+ if endpoint is None:
206
+ click.echo("Azure OpenAI endpoint entry cancelled. Exiting.")
207
+ return False
208
+ set_key(str(GAC_ENV_PATH), "AZURE_OPENAI_ENDPOINT", endpoint)
209
+ click.echo(f"Set AZURE_OPENAI_ENDPOINT={endpoint}")
210
+
211
+ # Check for existing API version
212
+ existing_api_version = existing_env.get("AZURE_OPENAI_API_VERSION")
213
+ if existing_api_version:
214
+ click.echo(f"\nAZURE_OPENAI_API_VERSION is already configured: {existing_api_version}")
215
+ version_action = questionary.select(
216
+ "What would you like to do?",
217
+ choices=[
218
+ "Keep existing version",
219
+ "Enter new version",
220
+ ],
221
+ use_shortcuts=True,
222
+ use_arrow_keys=True,
223
+ use_jk_keys=False,
224
+ ).ask()
225
+
226
+ if version_action == "Enter new version":
227
+ api_version = questionary.text(
228
+ "Enter the Azure OpenAI API version (required, e.g., 2025-01-01-preview):",
229
+ default="2025-01-01-preview",
230
+ ).ask()
231
+ if api_version is None or not api_version.strip():
232
+ click.echo("Azure OpenAI API version entry cancelled. Exiting.")
233
+ return False
234
+ api_version = api_version.strip()
235
+ set_key(str(GAC_ENV_PATH), "AZURE_OPENAI_API_VERSION", api_version)
236
+ click.echo(f"Set AZURE_OPENAI_API_VERSION={api_version}")
237
+ else:
238
+ api_version = existing_api_version
239
+ click.echo(f"Keeping existing AZURE_OPENAI_API_VERSION={api_version}")
240
+ else:
241
+ api_version = questionary.text(
242
+ "Enter the Azure OpenAI API version (required, e.g., 2025-01-01-preview):", default="2025-01-01-preview"
243
+ ).ask()
244
+ if api_version is None or not api_version.strip():
245
+ click.echo("Azure OpenAI API version entry cancelled. Exiting.")
246
+ return False
247
+ api_version = api_version.strip()
248
+ set_key(str(GAC_ENV_PATH), "AZURE_OPENAI_API_VERSION", api_version)
249
+ click.echo(f"Set AZURE_OPENAI_API_VERSION={api_version}")
250
+ elif is_custom_openai:
251
+ base_url = _prompt_required_text("Enter the custom OpenAI-compatible base URL (required):")
252
+ if base_url is None:
253
+ click.echo("Custom OpenAI base URL entry cancelled. Exiting.")
254
+ return False
255
+ set_key(str(GAC_ENV_PATH), "CUSTOM_OPENAI_BASE_URL", base_url)
256
+ click.echo(f"Set CUSTOM_OPENAI_BASE_URL={base_url}")
257
+ elif is_ollama:
258
+ url_default = "http://localhost:11434"
259
+ url = questionary.text(f"Enter the Ollama API URL (default: {url_default}):", default=url_default).ask()
260
+ if url is None:
261
+ click.echo("Ollama URL entry cancelled. Exiting.")
262
+ return False
263
+ url_to_save = url.strip() if url.strip() else url_default
264
+ set_key(str(GAC_ENV_PATH), "OLLAMA_API_URL", url_to_save)
265
+ click.echo(f"Set OLLAMA_API_URL={url_to_save}")
266
+ elif is_lmstudio:
267
+ url_default = "http://localhost:1234"
268
+ url = questionary.text(f"Enter the LM Studio API URL (default: {url_default}):", default=url_default).ask()
269
+ if url is None:
270
+ click.echo("LM Studio URL entry cancelled. Exiting.")
271
+ return False
272
+ url_to_save = url.strip() if url.strip() else url_default
273
+ set_key(str(GAC_ENV_PATH), "LMSTUDIO_API_URL", url_to_save)
274
+ click.echo(f"Set LMSTUDIO_API_URL={url_to_save}")
275
+
276
+ # Handle Claude Code OAuth separately
277
+ if is_claude_code:
278
+ from gac.oauth.claude_code import authenticate_and_save
279
+ from gac.oauth.token_store import TokenStore
280
+
281
+ token_store = TokenStore()
282
+ existing_token_data = token_store.get_token("claude-code")
283
+ if existing_token_data:
284
+ click.echo("\n✓ Claude Code access token already configured.")
285
+ action = questionary.select(
286
+ "What would you like to do?",
287
+ choices=[
288
+ "Keep existing token",
289
+ "Re-authenticate (get new token)",
290
+ ],
291
+ use_shortcuts=True,
292
+ use_arrow_keys=True,
293
+ use_jk_keys=False,
294
+ ).ask()
295
+
296
+ if action is None or action.startswith("Keep existing"):
297
+ if action is None:
298
+ click.echo("Claude Code configuration cancelled. Keeping existing token.")
299
+ else:
300
+ click.echo("Keeping existing Claude Code token")
301
+ return True
302
+ else:
303
+ click.echo("\n🔐 Starting Claude Code OAuth authentication...")
304
+ if not authenticate_and_save(quiet=False):
305
+ click.echo("❌ Claude Code authentication failed. Keeping existing token.")
306
+ return False
307
+ return True
308
+ else:
309
+ click.echo("\n🔐 Starting Claude Code OAuth authentication...")
310
+ click.echo(" (Your browser will open automatically)\n")
311
+ if not authenticate_and_save(quiet=False):
312
+ click.echo("\n❌ Claude Code authentication failed. Exiting.")
313
+ return False
314
+ return True
315
+
316
+ # Handle Qwen OAuth separately
317
+ if is_qwen:
318
+ from gac.oauth import QwenOAuthProvider, TokenStore
319
+
320
+ token_store = TokenStore()
321
+ qwen_token = token_store.get_token("qwen")
322
+ if qwen_token:
323
+ click.echo("\n✓ Qwen access token already configured.")
324
+ action = questionary.select(
325
+ "What would you like to do?",
326
+ choices=[
327
+ "Keep existing token",
328
+ "Re-authenticate (get new token)",
329
+ ],
330
+ use_shortcuts=True,
331
+ use_arrow_keys=True,
332
+ use_jk_keys=False,
333
+ ).ask()
334
+
335
+ if action is None or action.startswith("Keep existing"):
336
+ if action is None:
337
+ click.echo("Qwen configuration cancelled. Keeping existing token.")
338
+ else:
339
+ click.echo("Keeping existing Qwen token")
340
+ return True
341
+ else:
342
+ click.echo("\n🔐 Starting Qwen OAuth authentication...")
343
+ provider = QwenOAuthProvider(token_store)
344
+ try:
345
+ provider.initiate_auth(open_browser=True)
346
+ click.echo("✅ Qwen authentication completed successfully!")
347
+ return True
348
+ except Exception as e:
349
+ click.echo(f"❌ Qwen authentication failed: {e}")
350
+ return False
351
+ else:
352
+ click.echo("\n🔐 Starting Qwen OAuth authentication...")
353
+ click.echo(" (Your browser will open automatically)\n")
354
+ provider = QwenOAuthProvider(token_store)
355
+ try:
356
+ provider.initiate_auth(open_browser=True)
357
+ click.echo("\n✅ Qwen authentication completed successfully!")
358
+ return True
359
+ except Exception as e:
360
+ click.echo(f"\n❌ Qwen authentication failed: {e}")
361
+ return False
362
+
363
+ # Determine API key name based on provider
364
+ if is_lmstudio:
365
+ api_key_name = "LMSTUDIO_API_KEY"
366
+ elif is_zai:
367
+ api_key_name = "ZAI_API_KEY"
368
+ else:
369
+ api_key_name = f"{provider_key.upper().replace('-', '_')}_API_KEY"
370
+
371
+ # Check if API key already exists
372
+ existing_key = existing_env.get(api_key_name)
373
+
374
+ if existing_key:
375
+ # Key exists - offer options
376
+ click.echo(f"\n{api_key_name} is already configured.")
377
+ action = questionary.select(
378
+ "What would you like to do?",
379
+ choices=[
380
+ "Keep existing key",
381
+ "Enter new key",
382
+ ],
383
+ use_shortcuts=True,
384
+ use_arrow_keys=True,
385
+ use_jk_keys=False,
386
+ ).ask()
387
+
388
+ if action is None:
389
+ click.echo("API key configuration cancelled. Keeping existing key.")
390
+ elif action.startswith("Keep existing"):
391
+ click.echo(f"Keeping existing {api_key_name}")
392
+ elif action.startswith("Enter new"):
393
+ api_key = questionary.password("Enter your new API key (input hidden):").ask()
394
+ if api_key and api_key.strip():
395
+ set_key(str(GAC_ENV_PATH), api_key_name, api_key)
396
+ click.echo(f"Updated {api_key_name} (hidden)")
397
+ else:
398
+ click.echo(f"No key entered. Keeping existing {api_key_name}")
399
+ else:
400
+ # No existing key - prompt for new one
401
+ api_key_prompt = "Enter your API key (input hidden, can be set later):"
402
+ if is_ollama or is_lmstudio:
403
+ click.echo(
404
+ "This provider typically runs locally. API keys are optional unless your instance requires authentication."
405
+ )
406
+ api_key_prompt = "Enter your API key (optional, press Enter to skip):"
407
+
408
+ api_key = questionary.password(api_key_prompt).ask()
409
+ if api_key and api_key.strip():
410
+ set_key(str(GAC_ENV_PATH), api_key_name, api_key)
411
+ click.echo(f"Set {api_key_name} (hidden)")
412
+ elif is_ollama or is_lmstudio:
413
+ click.echo("Skipping API key. You can add one later if needed.")
414
+ else:
415
+ click.echo("No API key entered. You can add one later by editing ~/.gac.env")
416
+
417
+ return True
418
+
419
+
420
+ @click.command()
421
+ def model() -> None:
422
+ """Interactively update provider/model/API key without language prompts."""
423
+ click.echo("Welcome to gac model configuration!\n")
424
+
425
+ existing_env = _load_existing_env()
426
+ if not _configure_model(existing_env):
427
+ return
428
+
429
+ click.echo(f"\nModel configuration complete. You can edit {GAC_ENV_PATH} to update values later.")
gac/oauth/__init__.py ADDED
@@ -0,0 +1,27 @@
1
+ """OAuth authentication utilities for GAC."""
2
+
3
+ from .claude_code import (
4
+ authenticate_and_save,
5
+ is_token_expired,
6
+ load_stored_token,
7
+ perform_oauth_flow,
8
+ refresh_token_if_expired,
9
+ remove_token,
10
+ save_token,
11
+ )
12
+ from .qwen_oauth import QwenDeviceFlow, QwenOAuthProvider
13
+ from .token_store import OAuthToken, TokenStore
14
+
15
+ __all__ = [
16
+ "authenticate_and_save",
17
+ "is_token_expired",
18
+ "load_stored_token",
19
+ "OAuthToken",
20
+ "perform_oauth_flow",
21
+ "QwenDeviceFlow",
22
+ "QwenOAuthProvider",
23
+ "refresh_token_if_expired",
24
+ "remove_token",
25
+ "save_token",
26
+ "TokenStore",
27
+ ]