codepp 0.0.437__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 (288) hide show
  1. code_puppy/__init__.py +10 -0
  2. code_puppy/__main__.py +10 -0
  3. code_puppy/agents/__init__.py +31 -0
  4. code_puppy/agents/agent_c_reviewer.py +155 -0
  5. code_puppy/agents/agent_code_puppy.py +117 -0
  6. code_puppy/agents/agent_code_reviewer.py +90 -0
  7. code_puppy/agents/agent_cpp_reviewer.py +132 -0
  8. code_puppy/agents/agent_creator_agent.py +638 -0
  9. code_puppy/agents/agent_golang_reviewer.py +151 -0
  10. code_puppy/agents/agent_helios.py +124 -0
  11. code_puppy/agents/agent_javascript_reviewer.py +160 -0
  12. code_puppy/agents/agent_manager.py +742 -0
  13. code_puppy/agents/agent_pack_leader.py +385 -0
  14. code_puppy/agents/agent_planning.py +165 -0
  15. code_puppy/agents/agent_python_programmer.py +169 -0
  16. code_puppy/agents/agent_python_reviewer.py +90 -0
  17. code_puppy/agents/agent_qa_expert.py +163 -0
  18. code_puppy/agents/agent_qa_kitten.py +208 -0
  19. code_puppy/agents/agent_scheduler.py +121 -0
  20. code_puppy/agents/agent_security_auditor.py +181 -0
  21. code_puppy/agents/agent_terminal_qa.py +323 -0
  22. code_puppy/agents/agent_typescript_reviewer.py +166 -0
  23. code_puppy/agents/base_agent.py +2156 -0
  24. code_puppy/agents/event_stream_handler.py +348 -0
  25. code_puppy/agents/json_agent.py +202 -0
  26. code_puppy/agents/pack/__init__.py +34 -0
  27. code_puppy/agents/pack/bloodhound.py +304 -0
  28. code_puppy/agents/pack/husky.py +327 -0
  29. code_puppy/agents/pack/retriever.py +393 -0
  30. code_puppy/agents/pack/shepherd.py +348 -0
  31. code_puppy/agents/pack/terrier.py +287 -0
  32. code_puppy/agents/pack/watchdog.py +367 -0
  33. code_puppy/agents/prompt_reviewer.py +145 -0
  34. code_puppy/agents/subagent_stream_handler.py +276 -0
  35. code_puppy/api/__init__.py +13 -0
  36. code_puppy/api/app.py +169 -0
  37. code_puppy/api/main.py +21 -0
  38. code_puppy/api/pty_manager.py +453 -0
  39. code_puppy/api/routers/__init__.py +12 -0
  40. code_puppy/api/routers/agents.py +36 -0
  41. code_puppy/api/routers/commands.py +217 -0
  42. code_puppy/api/routers/config.py +75 -0
  43. code_puppy/api/routers/sessions.py +234 -0
  44. code_puppy/api/templates/terminal.html +361 -0
  45. code_puppy/api/websocket.py +154 -0
  46. code_puppy/callbacks.py +692 -0
  47. code_puppy/chatgpt_codex_client.py +338 -0
  48. code_puppy/claude_cache_client.py +672 -0
  49. code_puppy/cli_runner.py +1073 -0
  50. code_puppy/command_line/__init__.py +1 -0
  51. code_puppy/command_line/add_model_menu.py +1092 -0
  52. code_puppy/command_line/agent_menu.py +662 -0
  53. code_puppy/command_line/attachments.py +395 -0
  54. code_puppy/command_line/autosave_menu.py +704 -0
  55. code_puppy/command_line/clipboard.py +527 -0
  56. code_puppy/command_line/colors_menu.py +532 -0
  57. code_puppy/command_line/command_handler.py +293 -0
  58. code_puppy/command_line/command_registry.py +150 -0
  59. code_puppy/command_line/config_commands.py +719 -0
  60. code_puppy/command_line/core_commands.py +867 -0
  61. code_puppy/command_line/diff_menu.py +865 -0
  62. code_puppy/command_line/file_path_completion.py +73 -0
  63. code_puppy/command_line/load_context_completion.py +52 -0
  64. code_puppy/command_line/mcp/__init__.py +10 -0
  65. code_puppy/command_line/mcp/base.py +32 -0
  66. code_puppy/command_line/mcp/catalog_server_installer.py +175 -0
  67. code_puppy/command_line/mcp/custom_server_form.py +688 -0
  68. code_puppy/command_line/mcp/custom_server_installer.py +195 -0
  69. code_puppy/command_line/mcp/edit_command.py +148 -0
  70. code_puppy/command_line/mcp/handler.py +138 -0
  71. code_puppy/command_line/mcp/help_command.py +147 -0
  72. code_puppy/command_line/mcp/install_command.py +214 -0
  73. code_puppy/command_line/mcp/install_menu.py +705 -0
  74. code_puppy/command_line/mcp/list_command.py +94 -0
  75. code_puppy/command_line/mcp/logs_command.py +235 -0
  76. code_puppy/command_line/mcp/remove_command.py +82 -0
  77. code_puppy/command_line/mcp/restart_command.py +100 -0
  78. code_puppy/command_line/mcp/search_command.py +123 -0
  79. code_puppy/command_line/mcp/start_all_command.py +135 -0
  80. code_puppy/command_line/mcp/start_command.py +117 -0
  81. code_puppy/command_line/mcp/status_command.py +184 -0
  82. code_puppy/command_line/mcp/stop_all_command.py +112 -0
  83. code_puppy/command_line/mcp/stop_command.py +80 -0
  84. code_puppy/command_line/mcp/test_command.py +107 -0
  85. code_puppy/command_line/mcp/utils.py +129 -0
  86. code_puppy/command_line/mcp/wizard_utils.py +334 -0
  87. code_puppy/command_line/mcp_completion.py +174 -0
  88. code_puppy/command_line/model_picker_completion.py +197 -0
  89. code_puppy/command_line/model_settings_menu.py +932 -0
  90. code_puppy/command_line/motd.py +96 -0
  91. code_puppy/command_line/onboarding_slides.py +179 -0
  92. code_puppy/command_line/onboarding_wizard.py +342 -0
  93. code_puppy/command_line/pin_command_completion.py +329 -0
  94. code_puppy/command_line/prompt_toolkit_completion.py +846 -0
  95. code_puppy/command_line/session_commands.py +302 -0
  96. code_puppy/command_line/shell_passthrough.py +145 -0
  97. code_puppy/command_line/skills_completion.py +160 -0
  98. code_puppy/command_line/uc_menu.py +893 -0
  99. code_puppy/command_line/utils.py +93 -0
  100. code_puppy/command_line/wiggum_state.py +78 -0
  101. code_puppy/config.py +1770 -0
  102. code_puppy/error_logging.py +134 -0
  103. code_puppy/gemini_code_assist.py +385 -0
  104. code_puppy/gemini_model.py +754 -0
  105. code_puppy/hook_engine/README.md +105 -0
  106. code_puppy/hook_engine/__init__.py +21 -0
  107. code_puppy/hook_engine/aliases.py +155 -0
  108. code_puppy/hook_engine/engine.py +221 -0
  109. code_puppy/hook_engine/executor.py +296 -0
  110. code_puppy/hook_engine/matcher.py +156 -0
  111. code_puppy/hook_engine/models.py +240 -0
  112. code_puppy/hook_engine/registry.py +106 -0
  113. code_puppy/hook_engine/validator.py +144 -0
  114. code_puppy/http_utils.py +361 -0
  115. code_puppy/keymap.py +128 -0
  116. code_puppy/main.py +10 -0
  117. code_puppy/mcp_/__init__.py +66 -0
  118. code_puppy/mcp_/async_lifecycle.py +286 -0
  119. code_puppy/mcp_/blocking_startup.py +469 -0
  120. code_puppy/mcp_/captured_stdio_server.py +275 -0
  121. code_puppy/mcp_/circuit_breaker.py +290 -0
  122. code_puppy/mcp_/config_wizard.py +507 -0
  123. code_puppy/mcp_/dashboard.py +308 -0
  124. code_puppy/mcp_/error_isolation.py +407 -0
  125. code_puppy/mcp_/examples/retry_example.py +226 -0
  126. code_puppy/mcp_/health_monitor.py +589 -0
  127. code_puppy/mcp_/managed_server.py +428 -0
  128. code_puppy/mcp_/manager.py +807 -0
  129. code_puppy/mcp_/mcp_logs.py +224 -0
  130. code_puppy/mcp_/registry.py +451 -0
  131. code_puppy/mcp_/retry_manager.py +337 -0
  132. code_puppy/mcp_/server_registry_catalog.py +1126 -0
  133. code_puppy/mcp_/status_tracker.py +355 -0
  134. code_puppy/mcp_/system_tools.py +209 -0
  135. code_puppy/mcp_prompts/__init__.py +1 -0
  136. code_puppy/mcp_prompts/hook_creator.py +103 -0
  137. code_puppy/messaging/__init__.py +255 -0
  138. code_puppy/messaging/bus.py +613 -0
  139. code_puppy/messaging/commands.py +167 -0
  140. code_puppy/messaging/markdown_patches.py +57 -0
  141. code_puppy/messaging/message_queue.py +361 -0
  142. code_puppy/messaging/messages.py +569 -0
  143. code_puppy/messaging/queue_console.py +271 -0
  144. code_puppy/messaging/renderers.py +311 -0
  145. code_puppy/messaging/rich_renderer.py +1158 -0
  146. code_puppy/messaging/spinner/__init__.py +83 -0
  147. code_puppy/messaging/spinner/console_spinner.py +240 -0
  148. code_puppy/messaging/spinner/spinner_base.py +95 -0
  149. code_puppy/messaging/subagent_console.py +460 -0
  150. code_puppy/model_factory.py +848 -0
  151. code_puppy/model_switching.py +63 -0
  152. code_puppy/model_utils.py +168 -0
  153. code_puppy/models.json +174 -0
  154. code_puppy/models_dev_api.json +1 -0
  155. code_puppy/models_dev_parser.py +592 -0
  156. code_puppy/plugins/__init__.py +186 -0
  157. code_puppy/plugins/agent_skills/__init__.py +22 -0
  158. code_puppy/plugins/agent_skills/config.py +175 -0
  159. code_puppy/plugins/agent_skills/discovery.py +136 -0
  160. code_puppy/plugins/agent_skills/downloader.py +392 -0
  161. code_puppy/plugins/agent_skills/installer.py +22 -0
  162. code_puppy/plugins/agent_skills/metadata.py +219 -0
  163. code_puppy/plugins/agent_skills/prompt_builder.py +60 -0
  164. code_puppy/plugins/agent_skills/register_callbacks.py +241 -0
  165. code_puppy/plugins/agent_skills/remote_catalog.py +322 -0
  166. code_puppy/plugins/agent_skills/skill_catalog.py +257 -0
  167. code_puppy/plugins/agent_skills/skills_install_menu.py +664 -0
  168. code_puppy/plugins/agent_skills/skills_menu.py +781 -0
  169. code_puppy/plugins/antigravity_oauth/__init__.py +10 -0
  170. code_puppy/plugins/antigravity_oauth/accounts.py +406 -0
  171. code_puppy/plugins/antigravity_oauth/antigravity_model.py +706 -0
  172. code_puppy/plugins/antigravity_oauth/config.py +42 -0
  173. code_puppy/plugins/antigravity_oauth/constants.py +133 -0
  174. code_puppy/plugins/antigravity_oauth/oauth.py +478 -0
  175. code_puppy/plugins/antigravity_oauth/register_callbacks.py +518 -0
  176. code_puppy/plugins/antigravity_oauth/storage.py +288 -0
  177. code_puppy/plugins/antigravity_oauth/test_plugin.py +319 -0
  178. code_puppy/plugins/antigravity_oauth/token.py +167 -0
  179. code_puppy/plugins/antigravity_oauth/transport.py +863 -0
  180. code_puppy/plugins/antigravity_oauth/utils.py +168 -0
  181. code_puppy/plugins/chatgpt_oauth/__init__.py +8 -0
  182. code_puppy/plugins/chatgpt_oauth/config.py +52 -0
  183. code_puppy/plugins/chatgpt_oauth/oauth_flow.py +329 -0
  184. code_puppy/plugins/chatgpt_oauth/register_callbacks.py +176 -0
  185. code_puppy/plugins/chatgpt_oauth/test_plugin.py +301 -0
  186. code_puppy/plugins/chatgpt_oauth/utils.py +523 -0
  187. code_puppy/plugins/claude_code_hooks/__init__.py +1 -0
  188. code_puppy/plugins/claude_code_hooks/config.py +137 -0
  189. code_puppy/plugins/claude_code_hooks/register_callbacks.py +175 -0
  190. code_puppy/plugins/claude_code_oauth/README.md +167 -0
  191. code_puppy/plugins/claude_code_oauth/SETUP.md +93 -0
  192. code_puppy/plugins/claude_code_oauth/__init__.py +25 -0
  193. code_puppy/plugins/claude_code_oauth/config.py +52 -0
  194. code_puppy/plugins/claude_code_oauth/register_callbacks.py +453 -0
  195. code_puppy/plugins/claude_code_oauth/test_plugin.py +283 -0
  196. code_puppy/plugins/claude_code_oauth/token_refresh_heartbeat.py +241 -0
  197. code_puppy/plugins/claude_code_oauth/utils.py +640 -0
  198. code_puppy/plugins/customizable_commands/__init__.py +0 -0
  199. code_puppy/plugins/customizable_commands/register_callbacks.py +152 -0
  200. code_puppy/plugins/example_custom_command/README.md +280 -0
  201. code_puppy/plugins/example_custom_command/register_callbacks.py +51 -0
  202. code_puppy/plugins/file_permission_handler/__init__.py +4 -0
  203. code_puppy/plugins/file_permission_handler/register_callbacks.py +470 -0
  204. code_puppy/plugins/frontend_emitter/__init__.py +25 -0
  205. code_puppy/plugins/frontend_emitter/emitter.py +121 -0
  206. code_puppy/plugins/frontend_emitter/register_callbacks.py +261 -0
  207. code_puppy/plugins/hook_creator/__init__.py +1 -0
  208. code_puppy/plugins/hook_creator/register_callbacks.py +33 -0
  209. code_puppy/plugins/hook_manager/__init__.py +1 -0
  210. code_puppy/plugins/hook_manager/config.py +290 -0
  211. code_puppy/plugins/hook_manager/hooks_menu.py +564 -0
  212. code_puppy/plugins/hook_manager/register_callbacks.py +227 -0
  213. code_puppy/plugins/oauth_puppy_html.py +228 -0
  214. code_puppy/plugins/scheduler/__init__.py +1 -0
  215. code_puppy/plugins/scheduler/register_callbacks.py +88 -0
  216. code_puppy/plugins/scheduler/scheduler_menu.py +522 -0
  217. code_puppy/plugins/scheduler/scheduler_wizard.py +341 -0
  218. code_puppy/plugins/shell_safety/__init__.py +6 -0
  219. code_puppy/plugins/shell_safety/agent_shell_safety.py +69 -0
  220. code_puppy/plugins/shell_safety/command_cache.py +156 -0
  221. code_puppy/plugins/shell_safety/register_callbacks.py +202 -0
  222. code_puppy/plugins/synthetic_status/__init__.py +1 -0
  223. code_puppy/plugins/synthetic_status/register_callbacks.py +132 -0
  224. code_puppy/plugins/synthetic_status/status_api.py +147 -0
  225. code_puppy/plugins/universal_constructor/__init__.py +13 -0
  226. code_puppy/plugins/universal_constructor/models.py +138 -0
  227. code_puppy/plugins/universal_constructor/register_callbacks.py +47 -0
  228. code_puppy/plugins/universal_constructor/registry.py +302 -0
  229. code_puppy/plugins/universal_constructor/sandbox.py +584 -0
  230. code_puppy/prompts/antigravity_system_prompt.md +1 -0
  231. code_puppy/pydantic_patches.py +356 -0
  232. code_puppy/reopenable_async_client.py +232 -0
  233. code_puppy/round_robin_model.py +150 -0
  234. code_puppy/scheduler/__init__.py +41 -0
  235. code_puppy/scheduler/__main__.py +9 -0
  236. code_puppy/scheduler/cli.py +118 -0
  237. code_puppy/scheduler/config.py +126 -0
  238. code_puppy/scheduler/daemon.py +280 -0
  239. code_puppy/scheduler/executor.py +155 -0
  240. code_puppy/scheduler/platform.py +19 -0
  241. code_puppy/scheduler/platform_unix.py +22 -0
  242. code_puppy/scheduler/platform_win.py +32 -0
  243. code_puppy/session_storage.py +338 -0
  244. code_puppy/status_display.py +257 -0
  245. code_puppy/summarization_agent.py +176 -0
  246. code_puppy/terminal_utils.py +418 -0
  247. code_puppy/tools/__init__.py +501 -0
  248. code_puppy/tools/agent_tools.py +603 -0
  249. code_puppy/tools/ask_user_question/__init__.py +26 -0
  250. code_puppy/tools/ask_user_question/constants.py +73 -0
  251. code_puppy/tools/ask_user_question/demo_tui.py +55 -0
  252. code_puppy/tools/ask_user_question/handler.py +232 -0
  253. code_puppy/tools/ask_user_question/models.py +304 -0
  254. code_puppy/tools/ask_user_question/registration.py +26 -0
  255. code_puppy/tools/ask_user_question/renderers.py +309 -0
  256. code_puppy/tools/ask_user_question/terminal_ui.py +329 -0
  257. code_puppy/tools/ask_user_question/theme.py +155 -0
  258. code_puppy/tools/ask_user_question/tui_loop.py +423 -0
  259. code_puppy/tools/browser/__init__.py +37 -0
  260. code_puppy/tools/browser/browser_control.py +289 -0
  261. code_puppy/tools/browser/browser_interactions.py +545 -0
  262. code_puppy/tools/browser/browser_locators.py +640 -0
  263. code_puppy/tools/browser/browser_manager.py +378 -0
  264. code_puppy/tools/browser/browser_navigation.py +251 -0
  265. code_puppy/tools/browser/browser_screenshot.py +179 -0
  266. code_puppy/tools/browser/browser_scripts.py +462 -0
  267. code_puppy/tools/browser/browser_workflows.py +221 -0
  268. code_puppy/tools/browser/chromium_terminal_manager.py +259 -0
  269. code_puppy/tools/browser/terminal_command_tools.py +534 -0
  270. code_puppy/tools/browser/terminal_screenshot_tools.py +552 -0
  271. code_puppy/tools/browser/terminal_tools.py +525 -0
  272. code_puppy/tools/command_runner.py +1346 -0
  273. code_puppy/tools/common.py +1409 -0
  274. code_puppy/tools/display.py +84 -0
  275. code_puppy/tools/file_modifications.py +886 -0
  276. code_puppy/tools/file_operations.py +802 -0
  277. code_puppy/tools/scheduler_tools.py +412 -0
  278. code_puppy/tools/skills_tools.py +244 -0
  279. code_puppy/tools/subagent_context.py +158 -0
  280. code_puppy/tools/tools_content.py +51 -0
  281. code_puppy/tools/universal_constructor.py +889 -0
  282. code_puppy/uvx_detection.py +242 -0
  283. code_puppy/version_checker.py +82 -0
  284. codepp-0.0.437.dist-info/METADATA +766 -0
  285. codepp-0.0.437.dist-info/RECORD +288 -0
  286. codepp-0.0.437.dist-info/WHEEL +4 -0
  287. codepp-0.0.437.dist-info/entry_points.txt +3 -0
  288. codepp-0.0.437.dist-info/licenses/LICENSE +21 -0
@@ -0,0 +1,523 @@
1
+ """Utility helpers for the ChatGPT OAuth plugin."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import base64
6
+ import datetime
7
+ import hashlib
8
+ import json
9
+ import logging
10
+ import secrets
11
+ import time
12
+ from dataclasses import dataclass
13
+ from typing import Any, Dict, List, Optional
14
+ from urllib.parse import parse_qs as urllib_parse_qs
15
+ from urllib.parse import urlencode, urlparse
16
+
17
+ import requests
18
+
19
+ from .config import (
20
+ CHATGPT_OAUTH_CONFIG,
21
+ get_chatgpt_models_path,
22
+ get_token_storage_path,
23
+ )
24
+
25
+ logger = logging.getLogger(__name__)
26
+
27
+
28
+ @dataclass
29
+ class OAuthContext:
30
+ """Runtime state for an in-progress OAuth flow."""
31
+
32
+ state: str
33
+ code_verifier: str
34
+ code_challenge: str
35
+ created_at: float
36
+ redirect_uri: Optional[str] = None
37
+ expires_at: Optional[float] = None # Add expiration time
38
+
39
+ def is_expired(self) -> bool:
40
+ """Check if this OAuth context has expired."""
41
+ if self.expires_at is None:
42
+ # Default 5 minute expiration if not set
43
+ return time.time() - self.created_at > 300
44
+ return time.time() > self.expires_at
45
+
46
+
47
+ def _urlsafe_b64encode(data: bytes) -> str:
48
+ return base64.urlsafe_b64encode(data).decode("utf-8").rstrip("=")
49
+
50
+
51
+ def _generate_code_verifier() -> str:
52
+ return secrets.token_hex(64)
53
+
54
+
55
+ def _compute_code_challenge(code_verifier: str) -> str:
56
+ digest = hashlib.sha256(code_verifier.encode("utf-8")).digest()
57
+ return _urlsafe_b64encode(digest)
58
+
59
+
60
+ def prepare_oauth_context() -> OAuthContext:
61
+ """Create a fresh OAuth PKCE context."""
62
+ state = secrets.token_hex(32)
63
+ code_verifier = _generate_code_verifier()
64
+ code_challenge = _compute_code_challenge(code_verifier)
65
+
66
+ # Set expiration 4 minutes from now (OpenAI sessions are short)
67
+ expires_at = time.time() + 240
68
+
69
+ return OAuthContext(
70
+ state=state,
71
+ code_verifier=code_verifier,
72
+ code_challenge=code_challenge,
73
+ created_at=time.time(),
74
+ expires_at=expires_at,
75
+ )
76
+
77
+
78
+ def assign_redirect_uri(context: OAuthContext, port: int) -> str:
79
+ """Assign redirect URI for the given OAuth context."""
80
+ if context is None:
81
+ raise RuntimeError("OAuth context cannot be None")
82
+ host = CHATGPT_OAUTH_CONFIG["redirect_host"].rstrip("/")
83
+ path = CHATGPT_OAUTH_CONFIG["redirect_path"].lstrip("/")
84
+ required_port = CHATGPT_OAUTH_CONFIG.get("required_port")
85
+ if required_port and port != required_port:
86
+ raise RuntimeError(
87
+ f"OAuth flow must use port {required_port}; attempted to assign port {port}"
88
+ )
89
+ redirect_uri = f"{host}:{port}/{path}"
90
+ context.redirect_uri = redirect_uri
91
+ return redirect_uri
92
+
93
+
94
+ def build_authorization_url(context: OAuthContext) -> str:
95
+ """Return the OpenAI authorization URL with PKCE parameters."""
96
+ if not context.redirect_uri:
97
+ raise RuntimeError("Redirect URI has not been assigned for this OAuth context")
98
+
99
+ params = {
100
+ "response_type": "code",
101
+ "client_id": CHATGPT_OAUTH_CONFIG["client_id"],
102
+ "redirect_uri": context.redirect_uri,
103
+ "scope": CHATGPT_OAUTH_CONFIG["scope"],
104
+ "code_challenge": context.code_challenge,
105
+ "code_challenge_method": "S256",
106
+ "id_token_add_organizations": "true",
107
+ "codex_cli_simplified_flow": "true",
108
+ "state": context.state,
109
+ }
110
+ return f"{CHATGPT_OAUTH_CONFIG['auth_url']}?{urlencode(params)}"
111
+
112
+
113
+ def parse_authorization_error(url: str) -> Optional[str]:
114
+ """Parse error from OAuth callback URL."""
115
+ try:
116
+ parsed = urlparse(url)
117
+ params = urllib_parse_qs(parsed.query)
118
+ error = params.get("error", [None])[0]
119
+ error_description = params.get("error_description", [None])[0]
120
+ if error:
121
+ return f"{error}: {error_description or 'Unknown error'}"
122
+ except Exception as exc:
123
+ logger.error("Failed to parse OAuth error: %s", exc)
124
+ return None
125
+
126
+
127
+ def parse_jwt_claims(token: str) -> Optional[Dict[str, Any]]:
128
+ """Parse JWT token to extract claims."""
129
+ if not token or token.count(".") != 2:
130
+ return None
131
+ try:
132
+ _, payload, _ = token.split(".")
133
+ padded = payload + "=" * (-len(payload) % 4)
134
+ data = base64.urlsafe_b64decode(padded.encode())
135
+ return json.loads(data.decode())
136
+ except Exception as exc:
137
+ logger.error("Failed to parse JWT: %s", exc)
138
+ return None
139
+
140
+
141
+ def load_stored_tokens() -> Optional[Dict[str, Any]]:
142
+ try:
143
+ token_path = get_token_storage_path()
144
+ if token_path.exists():
145
+ with open(token_path, "r", encoding="utf-8") as handle:
146
+ return json.load(handle)
147
+ except Exception as exc:
148
+ logger.error("Failed to load tokens: %s", exc)
149
+ return None
150
+
151
+
152
+ def get_valid_access_token() -> Optional[str]:
153
+ """Get a valid access token, refreshing if expired.
154
+
155
+ Returns:
156
+ Valid access token string, or None if not authenticated or refresh failed.
157
+ """
158
+ tokens = load_stored_tokens()
159
+ if not tokens:
160
+ logger.debug("No stored ChatGPT OAuth tokens found")
161
+ return None
162
+
163
+ access_token = tokens.get("access_token")
164
+ if not access_token:
165
+ logger.debug("No access_token in stored tokens")
166
+ return None
167
+
168
+ # Check if token is expired by parsing JWT claims
169
+ claims = parse_jwt_claims(access_token)
170
+ if claims:
171
+ exp = claims.get("exp")
172
+ if exp and isinstance(exp, (int, float)):
173
+ # Add 30 second buffer before expiry
174
+ if time.time() > exp - 30:
175
+ logger.info("ChatGPT OAuth token expired, attempting refresh")
176
+ refreshed = refresh_access_token()
177
+ if refreshed:
178
+ return refreshed
179
+ logger.warning("Token refresh failed")
180
+ return None
181
+
182
+ return access_token
183
+
184
+
185
+ def refresh_access_token() -> Optional[str]:
186
+ """Refresh the access token using the refresh token.
187
+
188
+ Returns:
189
+ New access token if refresh succeeded, None otherwise.
190
+ """
191
+ tokens = load_stored_tokens()
192
+ if not tokens:
193
+ return None
194
+
195
+ refresh_token = tokens.get("refresh_token")
196
+ if not refresh_token:
197
+ logger.debug("No refresh_token available")
198
+ return None
199
+
200
+ payload = {
201
+ "grant_type": "refresh_token",
202
+ "refresh_token": refresh_token,
203
+ "client_id": CHATGPT_OAUTH_CONFIG["client_id"],
204
+ }
205
+
206
+ headers = {
207
+ "Content-Type": "application/x-www-form-urlencoded",
208
+ }
209
+
210
+ try:
211
+ response = requests.post(
212
+ CHATGPT_OAUTH_CONFIG["token_url"],
213
+ data=payload,
214
+ headers=headers,
215
+ timeout=30,
216
+ )
217
+
218
+ if response.status_code == 200:
219
+ new_tokens = response.json()
220
+ # Merge with existing tokens (preserve account_id, etc.)
221
+ tokens.update(
222
+ {
223
+ "access_token": new_tokens.get("access_token"),
224
+ "refresh_token": new_tokens.get("refresh_token", refresh_token),
225
+ "id_token": new_tokens.get("id_token", tokens.get("id_token")),
226
+ "last_refresh": datetime.datetime.now(datetime.timezone.utc)
227
+ .isoformat()
228
+ .replace("+00:00", "Z"),
229
+ }
230
+ )
231
+ if save_tokens(tokens):
232
+ logger.info("Successfully refreshed ChatGPT OAuth token")
233
+ return tokens["access_token"]
234
+ else:
235
+ logger.error(
236
+ "Token refresh failed: %s - %s", response.status_code, response.text
237
+ )
238
+ except Exception as exc:
239
+ logger.error("Token refresh error: %s", exc)
240
+
241
+ return None
242
+
243
+
244
+ def save_tokens(tokens: Dict[str, Any]) -> bool:
245
+ if tokens is None:
246
+ raise TypeError("tokens cannot be None")
247
+ try:
248
+ token_path = get_token_storage_path()
249
+ with open(token_path, "w", encoding="utf-8") as handle:
250
+ json.dump(tokens, handle, indent=2)
251
+ token_path.chmod(0o600)
252
+ return True
253
+ except Exception as exc:
254
+ logger.error("Failed to save tokens: %s", exc)
255
+ return False
256
+
257
+
258
+ def load_chatgpt_models() -> Dict[str, Any]:
259
+ try:
260
+ models_path = get_chatgpt_models_path()
261
+ if models_path.exists():
262
+ with open(models_path, "r", encoding="utf-8") as handle:
263
+ return json.load(handle)
264
+ except Exception as exc:
265
+ logger.error("Failed to load ChatGPT models: %s", exc)
266
+ return {}
267
+
268
+
269
+ def save_chatgpt_models(models: Dict[str, Any]) -> bool:
270
+ try:
271
+ models_path = get_chatgpt_models_path()
272
+ with open(models_path, "w", encoding="utf-8") as handle:
273
+ json.dump(models, handle, indent=2)
274
+ return True
275
+ except Exception as exc:
276
+ logger.error("Failed to save ChatGPT models: %s", exc)
277
+ return False
278
+
279
+
280
+ def exchange_code_for_tokens(
281
+ auth_code: str, context: OAuthContext
282
+ ) -> Optional[Dict[str, Any]]:
283
+ """Exchange authorization code for access tokens."""
284
+ if not context.redirect_uri:
285
+ raise RuntimeError("Redirect URI missing from OAuth context")
286
+
287
+ if context.is_expired():
288
+ logger.error("OAuth context expired, cannot exchange code")
289
+ return None
290
+
291
+ payload = {
292
+ "grant_type": "authorization_code",
293
+ "code": auth_code,
294
+ "redirect_uri": context.redirect_uri,
295
+ "client_id": CHATGPT_OAUTH_CONFIG["client_id"],
296
+ "code_verifier": context.code_verifier,
297
+ }
298
+
299
+ headers = {
300
+ "Content-Type": "application/x-www-form-urlencoded",
301
+ }
302
+
303
+ logger.info("Exchanging code for tokens: %s", CHATGPT_OAUTH_CONFIG["token_url"])
304
+ try:
305
+ response = requests.post(
306
+ CHATGPT_OAUTH_CONFIG["token_url"],
307
+ data=payload,
308
+ headers=headers,
309
+ timeout=30,
310
+ )
311
+ logger.info("Token exchange response: %s", response.status_code)
312
+ if response.status_code == 200:
313
+ token_data = response.json()
314
+ # Add timestamp
315
+ token_data["last_refresh"] = (
316
+ datetime.datetime.now(datetime.timezone.utc)
317
+ .isoformat()
318
+ .replace("+00:00", "Z")
319
+ )
320
+ return token_data
321
+ else:
322
+ logger.error(
323
+ "Token exchange failed: %s - %s",
324
+ response.status_code,
325
+ response.text,
326
+ )
327
+ # Try to parse OAuth error
328
+ if response.headers.get("content-type", "").startswith("application/json"):
329
+ try:
330
+ error_data = response.json()
331
+ if "error" in error_data:
332
+ logger.error(
333
+ "OAuth error: %s",
334
+ error_data.get("error_description", error_data["error"]),
335
+ )
336
+ except Exception:
337
+ pass
338
+ except Exception as exc:
339
+ logger.error("Token exchange error: %s", exc)
340
+ return None
341
+
342
+
343
+ # Default models available via ChatGPT Codex API
344
+ # These are the known models that work with ChatGPT OAuth tokens
345
+ # Based on codex-rs CLI and shell-scripts/codex-call.sh
346
+ DEFAULT_CODEX_MODELS = [
347
+ "gpt-5.4",
348
+ "gpt-5.3-instant",
349
+ "gpt-5.3-codex-spark",
350
+ "gpt-5.3-codex",
351
+ "gpt-5.2-codex",
352
+ "gpt-5.2",
353
+ ]
354
+
355
+ # Models that MUST always be registered, even if the /models endpoint
356
+ # doesn't return them (e.g. newly launched, not yet in the API catalogue).
357
+ # These are merged into whatever the endpoint returns.
358
+ REQUIRED_CODEX_MODELS = [
359
+ "gpt-5.4",
360
+ "gpt-5.3-instant",
361
+ "gpt-5.3-codex",
362
+ ]
363
+
364
+ # Per-model context length overrides (tokens).
365
+ # Models not listed here use CHATGPT_OAUTH_CONFIG["default_context_length"] (272,000).
366
+ CODEX_MODEL_CONTEXT_LENGTHS = {
367
+ "gpt-5.3-codex-spark": 131000,
368
+ "gpt-5.3-instant": 192000,
369
+ }
370
+
371
+
372
+ def _ensure_required_models(models: List[str]) -> List[str]:
373
+ """Merge REQUIRED_CODEX_MODELS into the given list, preserving order.
374
+
375
+ Any required model not already present is prepended so it appears first.
376
+ """
377
+ existing = set(models)
378
+ missing = [m for m in REQUIRED_CODEX_MODELS if m not in existing]
379
+ if missing:
380
+ logger.info("Injecting required models not returned by API: %s", missing)
381
+ return missing + models
382
+
383
+
384
+ def fetch_chatgpt_models(access_token: str, account_id: str) -> Optional[List[str]]:
385
+ """Fetch available models from ChatGPT Codex API.
386
+
387
+ Attempts to fetch models from the API, but falls back to a default list
388
+ of known Codex-compatible models if the API is unavailable.
389
+
390
+ Args:
391
+ access_token: OAuth access token for authentication
392
+ account_id: ChatGPT account ID (required for the API)
393
+
394
+ Returns:
395
+ List of model IDs, or default list if API fails
396
+ """
397
+ import platform
398
+
399
+ # Build the models URL with client version
400
+ client_version = CHATGPT_OAUTH_CONFIG.get("client_version", "0.72.0")
401
+ base_url = CHATGPT_OAUTH_CONFIG["api_base_url"].rstrip("/")
402
+ models_url = f"{base_url}/models"
403
+
404
+ # Build User-Agent to match codex-rs CLI format
405
+ originator = CHATGPT_OAUTH_CONFIG.get("originator", "codex_cli_rs")
406
+ os_name = platform.system()
407
+ if os_name == "Darwin":
408
+ os_name = "Mac OS"
409
+ os_version = platform.release()
410
+ arch = platform.machine()
411
+ user_agent = (
412
+ f"{originator}/{client_version} ({os_name} {os_version}; {arch}) "
413
+ "Terminal_Codex_CLI"
414
+ )
415
+
416
+ headers = {
417
+ "Authorization": f"Bearer {access_token}",
418
+ "ChatGPT-Account-Id": account_id,
419
+ "User-Agent": user_agent,
420
+ "originator": originator,
421
+ "Accept": "application/json",
422
+ }
423
+
424
+ # Query params
425
+ params = {"client_version": client_version}
426
+
427
+ try:
428
+ response = requests.get(models_url, headers=headers, params=params, timeout=30)
429
+
430
+ if response.status_code == 200:
431
+ # Parse JSON response
432
+ try:
433
+ data = response.json()
434
+ # The response has a "models" key with list of model objects
435
+ if "models" in data and isinstance(data["models"], list):
436
+ models = []
437
+ for model in data["models"]:
438
+ if model is None:
439
+ continue
440
+ model_id = (
441
+ model.get("slug") or model.get("id") or model.get("name")
442
+ )
443
+ if model_id:
444
+ models.append(model_id)
445
+ if models:
446
+ return _ensure_required_models(models)
447
+ except (json.JSONDecodeError, ValueError) as exc:
448
+ logger.warning("Failed to parse models response: %s", exc)
449
+
450
+ # API didn't return valid models, use default list
451
+ logger.info(
452
+ "Models endpoint returned %d, using default model list",
453
+ response.status_code,
454
+ )
455
+
456
+ except requests.exceptions.Timeout:
457
+ logger.warning("Timeout fetching models, using default list")
458
+ except requests.exceptions.RequestException as exc:
459
+ logger.warning("Network error fetching models: %s, using default list", exc)
460
+ except Exception as exc:
461
+ logger.warning("Error fetching models: %s, using default list", exc)
462
+
463
+ # Return default models when API fails
464
+ logger.info("Using default Codex models: %s", DEFAULT_CODEX_MODELS)
465
+ return DEFAULT_CODEX_MODELS
466
+
467
+
468
+ def add_models_to_extra_config(models: List[str]) -> bool:
469
+ """Add ChatGPT models to chatgpt_models.json configuration."""
470
+ try:
471
+ chatgpt_models = load_chatgpt_models()
472
+ added = 0
473
+ for model_name in models:
474
+ prefixed = f"{CHATGPT_OAUTH_CONFIG['prefix']}{model_name}"
475
+
476
+ # Determine supported settings based on model type
477
+ # All GPT-5.x models support reasoning_effort and verbosity
478
+ supported_settings = ["reasoning_effort", "verbosity"]
479
+
480
+ # Only codex models support xhigh reasoning effort
481
+ # Regular gpt-5.2 is capped at "high"
482
+ is_codex = "codex" in model_name.lower()
483
+
484
+ chatgpt_models[prefixed] = {
485
+ "type": "chatgpt_oauth",
486
+ "name": model_name,
487
+ "custom_endpoint": {
488
+ # Codex API uses chatgpt.com/backend-api/codex, not api.openai.com
489
+ "url": CHATGPT_OAUTH_CONFIG["api_base_url"],
490
+ },
491
+ "context_length": CODEX_MODEL_CONTEXT_LENGTHS.get(
492
+ model_name, CHATGPT_OAUTH_CONFIG["default_context_length"]
493
+ ),
494
+ "oauth_source": "chatgpt-oauth-plugin",
495
+ "supported_settings": supported_settings,
496
+ "supports_xhigh_reasoning": is_codex,
497
+ }
498
+ added += 1
499
+ if save_chatgpt_models(chatgpt_models):
500
+ logger.info("Added %s ChatGPT models", added)
501
+ return True
502
+ except Exception as exc:
503
+ logger.error("Error adding models to config: %s", exc)
504
+ return False
505
+
506
+
507
+ def remove_chatgpt_models() -> int:
508
+ """Remove ChatGPT OAuth models from chatgpt_models.json."""
509
+ try:
510
+ chatgpt_models = load_chatgpt_models()
511
+ to_remove = [
512
+ name
513
+ for name, config in chatgpt_models.items()
514
+ if config.get("oauth_source") == "chatgpt-oauth-plugin"
515
+ ]
516
+ for model_name in to_remove:
517
+ chatgpt_models.pop(model_name, None)
518
+ # Always save, even if no models were removed (to match test expectations)
519
+ if save_chatgpt_models(chatgpt_models):
520
+ return len(to_remove)
521
+ except Exception as exc:
522
+ logger.error("Error removing ChatGPT models: %s", exc)
523
+ return 0
@@ -0,0 +1 @@
1
+ """Claude Code hooks plugin."""
@@ -0,0 +1,137 @@
1
+ """
2
+ Configuration loader for Claude Code hooks.
3
+
4
+ Loads and merges hooks from multiple locations:
5
+ 1. ~/.code_puppy/hooks.json (global level) - always loaded if exists
6
+ 2. .claude/settings.json (project-level) - merged with global
7
+
8
+ Both configurations are loaded and merged so that hooks from both levels
9
+ coexist and execute together.
10
+ """
11
+
12
+ import json
13
+ import logging
14
+ import os
15
+ from pathlib import Path
16
+ from typing import Any, Dict, Optional
17
+
18
+ logger = logging.getLogger(__name__)
19
+
20
+ PROJECT_HOOKS_FILE = ".claude/settings.json"
21
+ GLOBAL_HOOKS_FILE = os.path.expanduser("~/.code_puppy/hooks.json")
22
+
23
+
24
+ def _deep_merge_hooks(base: Dict[str, Any], overlay: Dict[str, Any]) -> Dict[str, Any]:
25
+ """
26
+ Merge hook configurations, combining event types and hook groups.
27
+
28
+ When the same event type exists in both base and overlay, their hook groups
29
+ are concatenated (overlay hooks appear after base hooks).
30
+
31
+ Args:
32
+ base: Base configuration dictionary
33
+ overlay: Configuration to merge on top
34
+
35
+ Returns:
36
+ Merged configuration with all hooks from both sources
37
+ """
38
+ merged = dict(base)
39
+
40
+ for event_type, hook_groups in overlay.items():
41
+ if event_type.startswith("_"):
42
+ # Skip comment keys
43
+ merged[event_type] = hook_groups
44
+ continue
45
+
46
+ if event_type not in merged:
47
+ # New event type, just add it
48
+ merged[event_type] = hook_groups
49
+ elif isinstance(merged[event_type], list) and isinstance(hook_groups, list):
50
+ # Both are lists, concatenate them (overlay hooks come after)
51
+ merged[event_type] = merged[event_type] + hook_groups
52
+ logger.debug(
53
+ f"Merged {len(hook_groups)} hook group(s) for event '{event_type}'"
54
+ )
55
+ else:
56
+ # Type mismatch or unexpected structure, keep base
57
+ logger.warning(
58
+ f"Cannot merge event type '{event_type}': type mismatch or unexpected structure"
59
+ )
60
+
61
+ return merged
62
+
63
+
64
+ def load_hooks_config() -> Optional[Dict[str, Any]]:
65
+ """
66
+ Load and merge hooks configuration from available sources.
67
+
68
+ Priority order:
69
+ 1. ~/.code_puppy/hooks.json (global level) - always loaded if exists
70
+ 2. .claude/settings.json (project-level) - merged with global
71
+
72
+ Returns:
73
+ Configuration dictionary or None if no config found
74
+ """
75
+ merged_config: Dict[str, Any] = {}
76
+
77
+ # Load global hooks first
78
+ global_config_path = Path(GLOBAL_HOOKS_FILE)
79
+
80
+ if global_config_path.exists():
81
+ try:
82
+ with open(global_config_path, "r", encoding="utf-8") as f:
83
+ config = json.load(f)
84
+ if "hooks" in config and isinstance(config["hooks"], dict):
85
+ logger.info(
86
+ f"Loaded hooks configuration (wrapped format) from {GLOBAL_HOOKS_FILE}"
87
+ )
88
+ merged_config = _deep_merge_hooks(merged_config, config["hooks"])
89
+ elif isinstance(config, dict):
90
+ logger.info(f"Loaded hooks configuration from {GLOBAL_HOOKS_FILE}")
91
+ merged_config = _deep_merge_hooks(merged_config, config)
92
+ except json.JSONDecodeError as e:
93
+ logger.error(f"Invalid JSON in {GLOBAL_HOOKS_FILE}: {e}")
94
+ except Exception as e:
95
+ logger.error(f"Failed to load {GLOBAL_HOOKS_FILE}: {e}", exc_info=True)
96
+
97
+ # Load and merge project-level hooks
98
+ project_config_path = Path(os.getcwd()) / PROJECT_HOOKS_FILE
99
+
100
+ if project_config_path.exists():
101
+ try:
102
+ with open(project_config_path, "r", encoding="utf-8") as f:
103
+ config = json.load(f)
104
+ hooks_config = config.get("hooks")
105
+ if hooks_config:
106
+ logger.info(f"Merging hooks configuration from {project_config_path}")
107
+ merged_config = _deep_merge_hooks(merged_config, hooks_config)
108
+ else:
109
+ logger.debug(f"No 'hooks' section found in {project_config_path}")
110
+ except json.JSONDecodeError as e:
111
+ logger.error(f"Invalid JSON in {project_config_path}: {e}")
112
+ except Exception as e:
113
+ logger.error(f"Failed to load {project_config_path}: {e}", exc_info=True)
114
+
115
+ if not merged_config:
116
+ logger.debug("No hooks configuration found")
117
+ return None
118
+
119
+ event_count = len(
120
+ [event for event in merged_config.keys() if not event.startswith("_")]
121
+ )
122
+ logger.info(f"Hooks configuration ready ({event_count} event type(s))")
123
+ return merged_config
124
+
125
+
126
+ def get_hooks_config_paths() -> list:
127
+ """
128
+ Return list of hook configuration paths.
129
+
130
+ Returns paths in order of precedence (project-level first, then global).
131
+ Note: internally, hooks are loaded in reverse order (global first, then project)
132
+ so that project-level hooks can extend/append to global hooks.
133
+ """
134
+ return [
135
+ str(Path(os.getcwd()) / PROJECT_HOOKS_FILE),
136
+ GLOBAL_HOOKS_FILE,
137
+ ]