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,392 @@
1
+ """Remote skill downloader/installer.
2
+
3
+ Downloads a remote skill ZIP and installs it into the local skills directory.
4
+
5
+ Security notes:
6
+ - Defends against zip-slip path traversal.
7
+ - Defends (somewhat) against zip bombs by capping total uncompressed size.
8
+
9
+ This module never raises to callers; failures are returned as InstallResult.
10
+ """
11
+
12
+ from __future__ import annotations
13
+
14
+ import logging
15
+ import shutil
16
+ import tempfile
17
+ import zipfile
18
+ from pathlib import Path
19
+ from typing import Optional
20
+
21
+ import httpx
22
+
23
+ from code_puppy.plugins.agent_skills.discovery import refresh_skill_cache
24
+ from code_puppy.plugins.agent_skills.installer import InstallResult
25
+
26
+ logger = logging.getLogger(__name__)
27
+
28
+ _DEFAULT_SKILLS_DIR = Path.home() / ".code_puppy" / "skills"
29
+ _MAX_UNCOMPRESSED_BYTES = 50 * 1024 * 1024 # 50MB
30
+
31
+
32
+ def _zip_entry_parts(name: str) -> list[str]:
33
+ """Return safe-ish path parts for a zip entry.
34
+
35
+ Zip files use POSIX-style separators, but malicious zips sometimes include
36
+ backslashes. We normalize to '/' then split.
37
+ """
38
+
39
+ normalized = name.replace("\\", "/")
40
+ return [part for part in normalized.split("/") if part not in {"", "."}]
41
+
42
+
43
+ def _safe_rmtree(path: Path) -> bool:
44
+ """Remove a directory tree, logging errors instead of raising."""
45
+
46
+ try:
47
+ if not path.exists():
48
+ return True
49
+ shutil.rmtree(path)
50
+ return True
51
+ except Exception as e:
52
+ logger.warning(f"Failed to remove directory {path}: {e}")
53
+ return False
54
+
55
+
56
+ def _download_to_file(url: str, dest: Path) -> bool:
57
+ """Download a URL to a local file path with streaming."""
58
+
59
+ headers = {
60
+ "Accept": "application/zip, application/octet-stream, */*",
61
+ "User-Agent": "code-puppy/skill-downloader",
62
+ }
63
+
64
+ try:
65
+ dest.parent.mkdir(parents=True, exist_ok=True)
66
+
67
+ with httpx.Client(timeout=30, headers=headers, follow_redirects=True) as client:
68
+ with client.stream("GET", url) as response:
69
+ response.raise_for_status()
70
+
71
+ with dest.open("wb") as f:
72
+ for chunk in response.iter_bytes():
73
+ if chunk:
74
+ f.write(chunk)
75
+
76
+ logger.info(f"Downloaded skill zip to {dest}")
77
+ return True
78
+
79
+ except httpx.HTTPStatusError as e:
80
+ logger.warning(
81
+ "Skill download failed with HTTP status: "
82
+ f"{e.response.status_code} {e.response.reason_phrase}"
83
+ )
84
+ return False
85
+ except (httpx.ConnectError, httpx.TimeoutException, httpx.NetworkError) as e:
86
+ logger.warning(f"Skill download network failure: {e}")
87
+ return False
88
+ except Exception as e:
89
+ logger.exception(f"Unexpected error downloading {url}: {e}")
90
+ return False
91
+
92
+
93
+ def _is_within_directory(base_dir: Path, candidate: Path) -> bool:
94
+ """Check that a path is safely contained within a directory."""
95
+
96
+ try:
97
+ base_resolved = base_dir.resolve()
98
+ candidate_resolved = candidate.resolve()
99
+ candidate_resolved.relative_to(base_resolved)
100
+ return True
101
+ except Exception:
102
+ return False
103
+
104
+
105
+ def _validate_zip_safety(zf: zipfile.ZipFile) -> Optional[str]:
106
+ """Return an error message if unsafe, otherwise None."""
107
+
108
+ total_uncompressed = 0
109
+
110
+ for info in zf.infolist():
111
+ # Directory entries are fine.
112
+ if info.is_dir():
113
+ continue
114
+
115
+ total_uncompressed += int(info.file_size or 0)
116
+ if total_uncompressed > _MAX_UNCOMPRESSED_BYTES:
117
+ return (
118
+ "ZIP appears too large when uncompressed "
119
+ f"(>{_MAX_UNCOMPRESSED_BYTES} bytes)"
120
+ )
121
+
122
+ # Basic zip-slip protection: reject absolute paths and parent traversals.
123
+ name = info.filename
124
+ normalized = name.replace("\\", "/")
125
+ if normalized.startswith("/"):
126
+ return f"Unsafe zip entry path (absolute): {name}"
127
+
128
+ parts = _zip_entry_parts(name)
129
+ if ".." in parts:
130
+ return f"Unsafe zip entry path (traversal): {name}"
131
+
132
+ return None
133
+
134
+
135
+ def _safe_extract_zip(zf: zipfile.ZipFile, extract_dir: Path) -> bool:
136
+ """Safely extract zip contents into extract_dir."""
137
+
138
+ try:
139
+ extract_dir.mkdir(parents=True, exist_ok=True)
140
+
141
+ for info in zf.infolist():
142
+ parts = _zip_entry_parts(info.filename)
143
+
144
+ # Skip weird metadata folders.
145
+ if parts and parts[0] == "__MACOSX":
146
+ continue
147
+
148
+ dest_path = extract_dir.joinpath(*parts)
149
+
150
+ if not _is_within_directory(extract_dir, dest_path):
151
+ logger.warning(
152
+ f"Blocked zip entry outside extraction dir: {info.filename}"
153
+ )
154
+ return False
155
+
156
+ if info.is_dir():
157
+ dest_path.mkdir(parents=True, exist_ok=True)
158
+ continue
159
+
160
+ dest_path.parent.mkdir(parents=True, exist_ok=True)
161
+
162
+ with zf.open(info, "r") as src, dest_path.open("wb") as dst:
163
+ shutil.copyfileobj(src, dst)
164
+
165
+ return True
166
+
167
+ except Exception as e:
168
+ logger.exception(f"Failed to extract zip safely: {e}")
169
+ return False
170
+
171
+
172
+ def _determine_extracted_root(extract_dir: Path) -> Optional[Path]:
173
+ """Determine where the skill files live inside an extracted zip.
174
+
175
+ Supports:
176
+ - Files at the zip root
177
+ - Files inside a single top-level folder
178
+
179
+ Returns:
180
+ Path to the directory containing SKILL.md, or None.
181
+ """
182
+
183
+ try:
184
+ if (extract_dir / "SKILL.md").is_file():
185
+ return extract_dir
186
+
187
+ children = [p for p in extract_dir.iterdir() if p.name != "__MACOSX"]
188
+ dirs = [p for p in children if p.is_dir()]
189
+ files = [p for p in children if p.is_file()]
190
+
191
+ # If it's root-level but SKILL.md missing, no good.
192
+ if files:
193
+ return None
194
+
195
+ if len(dirs) == 1:
196
+ candidate = dirs[0]
197
+ if (candidate / "SKILL.md").is_file():
198
+ return candidate
199
+
200
+ return None
201
+
202
+ except Exception as e:
203
+ logger.warning(f"Failed to inspect extracted zip directory {extract_dir}: {e}")
204
+ return None
205
+
206
+
207
+ def _stage_normalized_install(
208
+ extracted_root: Path, skill_name: str, staging_base: Path
209
+ ) -> Optional[Path]:
210
+ """Copy extracted content into staging_base/<skill_name>."""
211
+
212
+ try:
213
+ staged_skill_dir = staging_base / skill_name
214
+ if staged_skill_dir.exists():
215
+ _safe_rmtree(staged_skill_dir)
216
+
217
+ shutil.copytree(extracted_root, staged_skill_dir)
218
+
219
+ if not (staged_skill_dir / "SKILL.md").is_file():
220
+ logger.warning(
221
+ f"Staged skill is missing SKILL.md: {(staged_skill_dir / 'SKILL.md')}"
222
+ )
223
+ return None
224
+
225
+ return staged_skill_dir
226
+
227
+ except Exception as e:
228
+ logger.exception(f"Failed to stage normalized install for {skill_name}: {e}")
229
+ return None
230
+
231
+
232
+ def download_and_install_skill(
233
+ skill_name: str,
234
+ download_url: str,
235
+ target_dir: Optional[Path] = None,
236
+ force: bool = False,
237
+ ) -> InstallResult:
238
+ """Download and install a remote skill zip.
239
+
240
+ Args:
241
+ skill_name: Skill name (directory name under target_dir).
242
+ download_url: Absolute URL to the skill .zip.
243
+ target_dir: Base skills directory. Defaults to ~/.code_puppy/skills.
244
+ force: If True, delete any existing install first.
245
+
246
+ Returns:
247
+ InstallResult indicating success/failure.
248
+ """
249
+
250
+ skill_name = skill_name.strip()
251
+ if not skill_name:
252
+ return InstallResult(success=False, message="skill_name is required")
253
+
254
+ # Prevent path traversal via skill_name.
255
+ if Path(skill_name).name != skill_name or skill_name in {".", ".."}:
256
+ return InstallResult(
257
+ success=False, message="skill_name must be a simple directory name"
258
+ )
259
+
260
+ base_dir = target_dir or _DEFAULT_SKILLS_DIR
261
+ skill_dir = base_dir / skill_name
262
+
263
+ try:
264
+ if skill_dir.exists():
265
+ if not force:
266
+ return InstallResult(
267
+ success=False,
268
+ message=f"Skill already installed at {skill_dir} (use force=True to reinstall)",
269
+ installed_path=skill_dir,
270
+ )
271
+
272
+ logger.info(
273
+ f"Force reinstall enabled; removing existing skill at {skill_dir}"
274
+ )
275
+ if not _safe_rmtree(skill_dir):
276
+ return InstallResult(
277
+ success=False,
278
+ message=f"Failed to remove existing skill directory: {skill_dir}",
279
+ installed_path=skill_dir,
280
+ )
281
+
282
+ base_dir.mkdir(parents=True, exist_ok=True)
283
+
284
+ with tempfile.TemporaryDirectory(prefix="code_puppy_skill_") as tmp:
285
+ tmp_dir = Path(tmp)
286
+ tmp_zip = tmp_dir / f"{skill_name}.zip"
287
+ extract_dir = tmp_dir / "extracted"
288
+ staging_dir = tmp_dir / "staging"
289
+ staging_dir.mkdir(parents=True, exist_ok=True)
290
+
291
+ if not _download_to_file(download_url, tmp_zip):
292
+ return InstallResult(
293
+ success=False,
294
+ message=f"Failed to download skill zip from {download_url}",
295
+ )
296
+
297
+ try:
298
+ with zipfile.ZipFile(tmp_zip, "r") as zf:
299
+ unsafe_reason = _validate_zip_safety(zf)
300
+ if unsafe_reason:
301
+ logger.warning(
302
+ f"Rejected unsafe zip for {skill_name}: {unsafe_reason}"
303
+ )
304
+ return InstallResult(
305
+ success=False,
306
+ message=f"Rejected unsafe zip: {unsafe_reason}",
307
+ )
308
+
309
+ if not _safe_extract_zip(zf, extract_dir):
310
+ return InstallResult(
311
+ success=False,
312
+ message="Failed to extract skill zip safely",
313
+ )
314
+ except zipfile.BadZipFile:
315
+ logger.warning(f"Downloaded file is not a valid zip: {tmp_zip}")
316
+ return InstallResult(
317
+ success=False, message="Downloaded file is not a valid zip"
318
+ )
319
+ except Exception as e:
320
+ logger.exception(f"Failed to open/extract zip for {skill_name}: {e}")
321
+ return InstallResult(success=False, message="Failed to extract zip")
322
+
323
+ extracted_root = _determine_extracted_root(extract_dir)
324
+ if extracted_root is None:
325
+ logger.warning(
326
+ "Extracted zip layout not recognized or missing SKILL.md. "
327
+ f"extract_dir={extract_dir}"
328
+ )
329
+ return InstallResult(
330
+ success=False,
331
+ message="Extracted zip missing SKILL.md or has unexpected layout",
332
+ )
333
+
334
+ staged_skill_dir = _stage_normalized_install(
335
+ extracted_root=extracted_root,
336
+ skill_name=skill_name,
337
+ staging_base=staging_dir,
338
+ )
339
+ if staged_skill_dir is None:
340
+ return InstallResult(
341
+ success=False,
342
+ message="Failed to stage extracted skill (missing SKILL.md)",
343
+ )
344
+
345
+ # Move staged install into final destination.
346
+ try:
347
+ if skill_dir.exists():
348
+ # Shouldn't happen (handled earlier), but be safe.
349
+ if force:
350
+ _safe_rmtree(skill_dir)
351
+ else:
352
+ return InstallResult(
353
+ success=False,
354
+ message=f"Skill directory already exists: {skill_dir}",
355
+ installed_path=skill_dir,
356
+ )
357
+
358
+ shutil.move(str(staged_skill_dir), str(skill_dir))
359
+ except Exception as e:
360
+ logger.exception(f"Failed to install skill into {skill_dir}: {e}")
361
+ # Cleanup partial install.
362
+ _safe_rmtree(skill_dir)
363
+ return InstallResult(
364
+ success=False, message="Failed to move skill into place"
365
+ )
366
+
367
+ # Post-install verification.
368
+ if not (skill_dir / "SKILL.md").is_file():
369
+ logger.warning(f"Installed skill missing SKILL.md: {skill_dir}")
370
+ _safe_rmtree(skill_dir)
371
+ return InstallResult(
372
+ success=False,
373
+ message="Installed skill is missing SKILL.md",
374
+ installed_path=skill_dir,
375
+ )
376
+
377
+ try:
378
+ refresh_skill_cache()
379
+ except Exception as e:
380
+ # Cache refresh failure should not poison a successful install.
381
+ logger.warning(f"Skill installed but failed to refresh skill cache: {e}")
382
+
383
+ logger.info(f"Installed skill '{skill_name}' into {skill_dir}")
384
+ return InstallResult(
385
+ success=True,
386
+ message=f"Installed skill '{skill_name}'",
387
+ installed_path=skill_dir,
388
+ )
389
+
390
+ except Exception as e:
391
+ logger.exception(f"Unexpected error installing skill {skill_name}: {e}")
392
+ return InstallResult(success=False, message="Unexpected error installing skill")
@@ -0,0 +1,22 @@
1
+ """Agent skills installation helpers.
2
+
3
+ This module currently provides the shared InstallResult type used by skill
4
+ installers (e.g. local installers, remote zip downloaders).
5
+
6
+ It is intentionally small so other modules can depend on a stable result shape.
7
+ """
8
+
9
+ from __future__ import annotations
10
+
11
+ from dataclasses import dataclass
12
+ from pathlib import Path
13
+ from typing import Optional
14
+
15
+
16
+ @dataclass(frozen=True, slots=True)
17
+ class InstallResult:
18
+ """Result of a skill install attempt."""
19
+
20
+ success: bool
21
+ message: str
22
+ installed_path: Optional[Path] = None
@@ -0,0 +1,219 @@
1
+ """Skill metadata parsing - extracts info from SKILL.md frontmatter."""
2
+
3
+ import logging
4
+ import re
5
+ from dataclasses import dataclass, field
6
+ from pathlib import Path
7
+ from typing import List, Optional
8
+
9
+ logger = logging.getLogger(__name__)
10
+
11
+ # Regex pattern to match YAML frontmatter between --- delimiters
12
+ FRONTMATTER_PATTERN = re.compile(r"^---\s*\n(.*?)\n---\s*\n", re.DOTALL)
13
+
14
+ # Regex patterns for parsing simple key-value pairs from YAML-like frontmatter
15
+ KEY_VALUE_PATTERN = re.compile(r"^([a-zA-Z_][a-zA-Z0-9_]*):\s*(.*)$", re.MULTILINE)
16
+ LIST_PATTERN = re.compile(r"^\s+-\s+(.+)$", re.MULTILINE)
17
+
18
+
19
+ @dataclass
20
+ class SkillMetadata:
21
+ """Parsed skill metadata from SKILL.md frontmatter."""
22
+
23
+ name: str
24
+ description: str
25
+ path: Path
26
+ version: Optional[str] = None
27
+ author: Optional[str] = None
28
+ tags: List[str] = field(default_factory=list)
29
+
30
+
31
+ def _unquote(value: str) -> str:
32
+ """Remove quotes from a YAML string value if present."""
33
+ value = value.strip()
34
+ if (value.startswith('"') and value.endswith('"')) or (
35
+ value.startswith("'") and value.endswith("'")
36
+ ):
37
+ return value[1:-1]
38
+ return value
39
+
40
+
41
+ def parse_yaml_frontmatter(content: str) -> dict:
42
+ """Extract YAML frontmatter from SKILL.md content.
43
+
44
+ Frontmatter is between --- delimiters at the start of file.
45
+ Uses simple regex parsing to avoid heavy yaml dependency.
46
+
47
+ Args:
48
+ content: The full content of the SKILL.md file.
49
+
50
+ Returns:
51
+ Dictionary containing parsed frontmatter key-value pairs.
52
+ Returns empty dict if no frontmatter found or parsing fails.
53
+ """
54
+ match = FRONTMATTER_PATTERN.match(content)
55
+ if not match:
56
+ logger.debug("No frontmatter found in content")
57
+ return {}
58
+
59
+ frontmatter = match.group(1)
60
+ result: dict = {}
61
+ current_key: Optional[str] = None
62
+ current_list: List[str] = []
63
+
64
+ for line in frontmatter.split("\n"):
65
+ stripped = line.strip()
66
+
67
+ # Skip empty lines and comments
68
+ if not stripped or stripped.startswith("#"):
69
+ continue
70
+
71
+ # Check if this is a list item
72
+ list_match = LIST_PATTERN.match(line)
73
+ if list_match and current_key:
74
+ current_list.append(_unquote(list_match.group(1)))
75
+ continue
76
+
77
+ # Check if this is a key-value pair
78
+ kv_match = KEY_VALUE_PATTERN.match(line)
79
+ if kv_match:
80
+ # Save any accumulated list items from previous key
81
+ if current_key and current_list:
82
+ result[current_key] = current_list
83
+ current_list = []
84
+
85
+ key, value = kv_match.groups()
86
+ key = key.strip()
87
+ value = value.strip()
88
+
89
+ # If value is empty, this might be a list start
90
+ if not value:
91
+ current_key = key
92
+ result[key] = [] # Initialize as empty list
93
+ else:
94
+ result[key] = _unquote(value)
95
+ current_key = None
96
+
97
+ # Handle case where list items were at the end
98
+ if current_key and current_list:
99
+ result[current_key] = current_list
100
+
101
+ return result
102
+
103
+
104
+ def parse_skill_metadata(skill_path: Path) -> Optional[SkillMetadata]:
105
+ """Parse metadata from a skill's SKILL.md file.
106
+
107
+ Args:
108
+ skill_path: Path to the skill directory (not the SKILL.md file)
109
+
110
+ Returns:
111
+ SkillMetadata if successful, None if parsing fails.
112
+ """
113
+ if not skill_path.exists():
114
+ logger.warning(f"Skill path does not exist: {skill_path}")
115
+ return None
116
+
117
+ skill_md_path = skill_path / "SKILL.md"
118
+ if not skill_md_path.exists():
119
+ logger.warning(f"SKILL.md not found in skill directory: {skill_path}")
120
+ return None
121
+
122
+ try:
123
+ content = skill_md_path.read_text(encoding="utf-8")
124
+ except Exception as e:
125
+ logger.error(f"Failed to read SKILL.md at {skill_md_path}: {e}")
126
+ return None
127
+
128
+ frontmatter = parse_yaml_frontmatter(content)
129
+ if not frontmatter:
130
+ logger.warning(f"No valid frontmatter found in {skill_md_path}")
131
+ return None
132
+
133
+ # Required fields
134
+ name = frontmatter.get("name")
135
+ if not name:
136
+ logger.error(
137
+ f"'name' is required in frontmatter but not found in {skill_md_path}"
138
+ )
139
+ return None
140
+
141
+ description = frontmatter.get("description")
142
+ if not description:
143
+ logger.error(
144
+ f"'description' is required in frontmatter but not found in {skill_md_path}"
145
+ )
146
+ return None
147
+
148
+ # Handle tags - could be a list or a comma-separated string
149
+ tags: List[str] = []
150
+ raw_tags = frontmatter.get("tags", [])
151
+ if isinstance(raw_tags, list):
152
+ tags = raw_tags
153
+ elif isinstance(raw_tags, str):
154
+ tags = [tag.strip() for tag in raw_tags.split(",") if tag.strip()]
155
+
156
+ return SkillMetadata(
157
+ name=name,
158
+ description=description,
159
+ path=skill_path,
160
+ version=frontmatter.get("version"),
161
+ author=frontmatter.get("author"),
162
+ tags=tags,
163
+ )
164
+
165
+
166
+ def load_full_skill_content(skill_path: Path) -> Optional[str]:
167
+ """Load the complete SKILL.md content for activation.
168
+
169
+ Args:
170
+ skill_path: Path to the skill directory
171
+
172
+ Returns:
173
+ Full file content as string, or None if not found.
174
+ """
175
+ if not skill_path.exists():
176
+ logger.warning(f"Skill path does not exist: {skill_path}")
177
+ return None
178
+
179
+ skill_md_path = skill_path / "SKILL.md"
180
+ if not skill_md_path.exists():
181
+ logger.warning(f"SKILL.md not found in skill directory: {skill_path}")
182
+ return None
183
+
184
+ try:
185
+ return skill_md_path.read_text(encoding="utf-8")
186
+ except Exception as e:
187
+ logger.error(f"Failed to read SKILL.md at {skill_md_path}: {e}")
188
+ return None
189
+
190
+
191
+ def get_skill_resources(skill_path: Path) -> List[Path]:
192
+ """List all resource files bundled with a skill.
193
+
194
+ Returns paths to all non-SKILL.md files in the skill directory.
195
+
196
+ Args:
197
+ skill_path: Path to the skill directory
198
+
199
+ Returns:
200
+ List of paths to resource files (excluding SKILL.md).
201
+ """
202
+ if not skill_path.exists():
203
+ logger.warning(f"Skill path does not exist: {skill_path}")
204
+ return []
205
+
206
+ if not skill_path.is_dir():
207
+ logger.warning(f"Skill path is not a directory: {skill_path}")
208
+ return []
209
+
210
+ resources: List[Path] = []
211
+ try:
212
+ for item in skill_path.iterdir():
213
+ if item.is_file() and item.name != "SKILL.md":
214
+ resources.append(item)
215
+ except Exception as e:
216
+ logger.error(f"Failed to list resources in {skill_path}: {e}")
217
+ return []
218
+
219
+ return sorted(resources) # Sort for consistent ordering
@@ -0,0 +1,60 @@
1
+ """Build available_skills XML for system prompt injection."""
2
+
3
+ from typing import TYPE_CHECKING, List
4
+
5
+ if TYPE_CHECKING:
6
+ from .metadata import SkillMetadata
7
+
8
+
9
+ def build_available_skills_xml(skills: List["SkillMetadata"]) -> str:
10
+ """Build Claude-optimized XML listing available skills.
11
+
12
+ Args:
13
+ skills: List of SkillMetadata objects to include in the XML.
14
+
15
+ Returns:
16
+ XML string listing available skills in the format:
17
+ <available_skills>
18
+ <skill>
19
+ <name>skill-name</name>
20
+ <description>What the skill does...</description>
21
+ </skill>
22
+ ...
23
+ </available_skills>
24
+
25
+ To use a skill, call activate_skill(skill_name) to load full instructions.
26
+ """
27
+ if not skills:
28
+ return "<available_skills></available_skills>"
29
+
30
+ xml_parts = ["<available_skills>"]
31
+
32
+ for skill in skills:
33
+ xml_parts.append(" <skill>")
34
+ xml_parts.append(f" <name>{skill.name}</name>")
35
+ if skill.description:
36
+ # Escape any XML special characters in the description
37
+ escaped_desc = (
38
+ skill.description.replace("&", "&amp;")
39
+ .replace("<", "&lt;")
40
+ .replace(">", "&gt;")
41
+ .replace('"', "&quot;")
42
+ .replace("'", "&#39;")
43
+ )
44
+ xml_parts.append(f" <description>{escaped_desc}</description>")
45
+ xml_parts.append(" </skill>")
46
+
47
+ xml_parts.append("</available_skills>")
48
+
49
+ return "\n".join(xml_parts)
50
+
51
+
52
+ def build_skills_guidance() -> str:
53
+ """Return guidance text for how to use skills."""
54
+ return """
55
+ # Agent Skills
56
+
57
+ When `<available_skills>` appears in context, match user tasks to skill descriptions.
58
+ Call `activate_skill(skill_name)` to load full instructions before starting the task.
59
+ Use `list_or_search_skills(query)` to search for relevant skills.
60
+ """