EvoScientist 0.0.1.dev2__py3-none-any.whl → 0.0.1.dev4__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.
EvoScientist/paths.py ADDED
@@ -0,0 +1,44 @@
1
+ """Path resolution utilities for EvoScientist runtime directories."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import os
6
+ from datetime import datetime
7
+ from pathlib import Path
8
+
9
+
10
+ def _expand(path: str) -> Path:
11
+ return Path(path).expanduser()
12
+
13
+
14
+ def _env_path(key: str) -> Path | None:
15
+ value = os.getenv(key)
16
+ if not value:
17
+ return None
18
+ return _expand(value)
19
+
20
+
21
+ # Workspace root: directly under cwd (no hidden .evoscientist layer)
22
+ WORKSPACE_ROOT = _env_path("EVOSCIENTIST_WORKSPACE_DIR") or (Path.cwd() / "workspace")
23
+
24
+ RUNS_DIR = _env_path("EVOSCIENTIST_RUNS_DIR") or (WORKSPACE_ROOT / "runs")
25
+ MEMORY_DIR = _env_path("EVOSCIENTIST_MEMORY_DIR") or (WORKSPACE_ROOT / "memory")
26
+ USER_SKILLS_DIR = _env_path("EVOSCIENTIST_SKILLS_DIR") or (WORKSPACE_ROOT / "skills")
27
+
28
+
29
+ def ensure_dirs() -> None:
30
+ """Create runtime directories if they do not exist."""
31
+ for path in (WORKSPACE_ROOT, RUNS_DIR, MEMORY_DIR, USER_SKILLS_DIR):
32
+ path.mkdir(parents=True, exist_ok=True)
33
+
34
+
35
+ def default_workspace_dir() -> Path:
36
+ """Default workspace for non-CLI usage."""
37
+ return WORKSPACE_ROOT
38
+
39
+
40
+ def new_run_dir(session_id: str | None = None) -> Path:
41
+ """Create a new run directory name under RUNS_DIR (path only)."""
42
+ if session_id is None:
43
+ session_id = datetime.now().strftime("%Y%m%d_%H%M%S")
44
+ return RUNS_DIR / session_id
@@ -0,0 +1,391 @@
1
+ """Skill installation and management for EvoScientist.
2
+
3
+ This module provides functions for installing, listing, and uninstalling user skills.
4
+ Skills are installed to USER_SKILLS_DIR (./workspace/skills/).
5
+
6
+ Supported installation sources:
7
+ - Local directory paths
8
+ - GitHub URLs (https://github.com/owner/repo or .../tree/branch/path)
9
+ - GitHub shorthand (owner/repo@skill-name)
10
+
11
+ Usage:
12
+ from EvoScientist.skills_manager import install_skill, list_skills, uninstall_skill
13
+
14
+ # Install from local path
15
+ install_skill("./my-skill")
16
+
17
+ # Install from GitHub
18
+ install_skill("https://github.com/user/repo/tree/main/my-skill")
19
+
20
+ # List installed skills
21
+ for skill in list_skills():
22
+ print(skill["name"], skill["description"])
23
+
24
+ # Uninstall a skill
25
+ uninstall_skill("my-skill")
26
+ """
27
+
28
+ from __future__ import annotations
29
+
30
+ import os
31
+ import re
32
+ import shutil
33
+ import subprocess
34
+ import tempfile
35
+ from dataclasses import dataclass
36
+ from pathlib import Path
37
+
38
+ import yaml
39
+
40
+ from .paths import USER_SKILLS_DIR
41
+
42
+
43
+ @dataclass
44
+ class SkillInfo:
45
+ """Information about an installed skill."""
46
+
47
+ name: str
48
+ description: str
49
+ path: Path
50
+ source: str # "user" or "system"
51
+
52
+
53
+ def _parse_skill_md(skill_md_path: Path) -> dict[str, str]:
54
+ """Parse SKILL.md frontmatter to extract name and description.
55
+
56
+ SKILL.md format:
57
+ ---
58
+ name: skill-name
59
+ description: A brief description...
60
+ ---
61
+ # Skill Title
62
+ ...
63
+
64
+ Returns:
65
+ Dictionary with 'name' and 'description' keys.
66
+ """
67
+ content = skill_md_path.read_text(encoding="utf-8")
68
+
69
+ # Extract YAML frontmatter
70
+ frontmatter_match = re.match(r"^---\s*\n(.*?)\n---", content, re.DOTALL)
71
+ if not frontmatter_match:
72
+ # No frontmatter, use directory name
73
+ return {
74
+ "name": skill_md_path.parent.name,
75
+ "description": "(no description)",
76
+ }
77
+
78
+ try:
79
+ frontmatter = yaml.safe_load(frontmatter_match.group(1))
80
+ return {
81
+ "name": frontmatter.get("name", skill_md_path.parent.name),
82
+ "description": frontmatter.get("description", "(no description)"),
83
+ }
84
+ except yaml.YAMLError:
85
+ return {
86
+ "name": skill_md_path.parent.name,
87
+ "description": "(invalid frontmatter)",
88
+ }
89
+
90
+
91
+ def _parse_github_url(url: str) -> tuple[str, str | None, str | None]:
92
+ """Parse a GitHub URL into (repo, ref, path).
93
+
94
+ Supports formats:
95
+ https://github.com/owner/repo
96
+ https://github.com/owner/repo/tree/main/path/to/skill
97
+ github.com/owner/repo/tree/branch/path
98
+ owner/repo@skill-name (shorthand from skills.sh)
99
+
100
+ Returns:
101
+ (repo, ref_or_none, path_or_none)
102
+ """
103
+ # Shorthand: owner/repo@path
104
+ if "@" in url and "://" not in url:
105
+ repo, path = url.split("@", 1)
106
+ return repo.strip(), None, path.strip()
107
+
108
+ # Strip protocol and github.com prefix
109
+ cleaned = re.sub(r"^https?://", "", url)
110
+ cleaned = re.sub(r"^github\.com/", "", cleaned)
111
+ cleaned = cleaned.rstrip("/")
112
+
113
+ # Match: owner/repo/tree/ref/path...
114
+ m = re.match(r"^([^/]+/[^/]+)/tree/([^/]+)(?:/(.+))?$", cleaned)
115
+ if m:
116
+ return m.group(1), m.group(2), m.group(3)
117
+
118
+ # Match: owner/repo (no tree)
119
+ m = re.match(r"^([^/]+/[^/]+)$", cleaned)
120
+ if m:
121
+ return m.group(1), None, None
122
+
123
+ raise ValueError(f"Cannot parse GitHub URL: {url}")
124
+
125
+
126
+ def _clone_repo(repo: str, ref: str | None, dest: str) -> None:
127
+ """Shallow-clone a GitHub repo."""
128
+ clone_url = f"https://github.com/{repo}.git"
129
+ cmd = ["git", "clone", "--depth", "1"]
130
+ if ref:
131
+ cmd += ["--branch", ref]
132
+ cmd += [clone_url, dest]
133
+
134
+ result = subprocess.run(cmd, capture_output=True, text=True)
135
+ if result.returncode != 0:
136
+ raise RuntimeError(f"git clone failed: {result.stderr.strip()}")
137
+
138
+
139
+ def _is_github_url(source: str) -> bool:
140
+ """Check if the source looks like a GitHub URL or shorthand."""
141
+ if "github.com" in source.lower():
142
+ return True
143
+ if "://" in source:
144
+ return False # Non-GitHub URL
145
+ # Check for owner/repo@skill shorthand
146
+ if "@" in source and "/" in source.split("@")[0]:
147
+ return True
148
+ # Check for owner/repo format (but not local paths like ./foo or /foo)
149
+ if "/" in source and not source.startswith((".", "/")):
150
+ parts = source.split("/")
151
+ # GitHub shorthand: exactly 2 parts, both non-empty, no extensions
152
+ if len(parts) == 2 and all(parts) and "." not in parts[0]:
153
+ return True
154
+ return False
155
+
156
+
157
+ def _validate_skill_dir(path: Path) -> bool:
158
+ """Check if a directory contains a valid skill (has SKILL.md)."""
159
+ return (path / "SKILL.md").is_file()
160
+
161
+
162
+ def install_skill(source: str, dest_dir: str | None = None) -> dict:
163
+ """Install a skill from a local path or GitHub URL.
164
+
165
+ Args:
166
+ source: Local directory path or GitHub URL/shorthand.
167
+ dest_dir: Destination directory (defaults to USER_SKILLS_DIR).
168
+
169
+ Returns:
170
+ Dictionary with installation result:
171
+ - success: bool
172
+ - name: skill name (if successful)
173
+ - path: installed path (if successful)
174
+ - error: error message (if failed)
175
+ """
176
+ dest_dir = dest_dir or str(USER_SKILLS_DIR)
177
+ os.makedirs(dest_dir, exist_ok=True)
178
+
179
+ if _is_github_url(source):
180
+ return _install_from_github(source, dest_dir)
181
+ else:
182
+ return _install_from_local(source, dest_dir)
183
+
184
+
185
+ def _install_from_local(source: str, dest_dir: str) -> dict:
186
+ """Install a skill from a local directory path."""
187
+ source_path = Path(source).expanduser().resolve()
188
+
189
+ if not source_path.exists():
190
+ return {"success": False, "error": f"Path does not exist: {source}"}
191
+
192
+ if not source_path.is_dir():
193
+ return {"success": False, "error": f"Not a directory: {source}"}
194
+
195
+ if not _validate_skill_dir(source_path):
196
+ return {"success": False, "error": f"No SKILL.md found in: {source}"}
197
+
198
+ # Parse SKILL.md to get the skill name
199
+ skill_info = _parse_skill_md(source_path / "SKILL.md")
200
+ skill_name = skill_info["name"]
201
+
202
+ # Destination path
203
+ target_path = Path(dest_dir) / skill_name
204
+
205
+ # Remove existing if present
206
+ if target_path.exists():
207
+ shutil.rmtree(target_path)
208
+
209
+ # Copy skill directory
210
+ shutil.copytree(source_path, target_path)
211
+
212
+ return {
213
+ "success": True,
214
+ "name": skill_name,
215
+ "path": str(target_path),
216
+ "description": skill_info["description"],
217
+ }
218
+
219
+
220
+ def _install_from_github(source: str, dest_dir: str) -> dict:
221
+ """Install a skill from a GitHub URL or shorthand."""
222
+ try:
223
+ repo, ref, path = _parse_github_url(source)
224
+ except ValueError as e:
225
+ return {"success": False, "error": str(e)}
226
+
227
+ with tempfile.TemporaryDirectory(prefix="evoscientist-skill-") as tmp:
228
+ clone_dir = os.path.join(tmp, "repo")
229
+
230
+ try:
231
+ _clone_repo(repo, ref, clone_dir)
232
+ except RuntimeError as e:
233
+ return {"success": False, "error": str(e)}
234
+
235
+ # Determine the skill source directory
236
+ if path:
237
+ skill_source = Path(clone_dir) / path
238
+ else:
239
+ skill_source = Path(clone_dir)
240
+
241
+ if not skill_source.exists():
242
+ return {"success": False, "error": f"Path not found in repo: {path or '/'}"}
243
+
244
+ if not _validate_skill_dir(skill_source):
245
+ # Maybe the repo root contains multiple skills?
246
+ if not path:
247
+ # Try to find skills in repo root
248
+ found_skills = []
249
+ for entry in os.listdir(clone_dir):
250
+ entry_path = Path(clone_dir) / entry
251
+ if entry_path.is_dir() and _validate_skill_dir(entry_path):
252
+ found_skills.append(entry)
253
+
254
+ if found_skills:
255
+ return {
256
+ "success": False,
257
+ "error": (
258
+ f"Multiple skills found in repo. "
259
+ f"Please specify one: {', '.join(found_skills)}"
260
+ ),
261
+ }
262
+
263
+ return {"success": False, "error": f"No SKILL.md found in: {source}"}
264
+
265
+ # Parse skill info and copy
266
+ skill_info = _parse_skill_md(skill_source / "SKILL.md")
267
+ skill_name = skill_info["name"]
268
+ target_path = Path(dest_dir) / skill_name
269
+
270
+ if target_path.exists():
271
+ shutil.rmtree(target_path)
272
+
273
+ # Copy, excluding .git directory
274
+ def ignore_git(dir_name: str, files: list[str]) -> list[str]:
275
+ return [f for f in files if f == ".git"]
276
+
277
+ shutil.copytree(skill_source, target_path, ignore=ignore_git)
278
+
279
+ return {
280
+ "success": True,
281
+ "name": skill_name,
282
+ "path": str(target_path),
283
+ "description": skill_info["description"],
284
+ "source": source,
285
+ }
286
+
287
+
288
+ def list_skills(include_system: bool = False) -> list[SkillInfo]:
289
+ """List all installed user skills.
290
+
291
+ Args:
292
+ include_system: If True, also include system (built-in) skills.
293
+
294
+ Returns:
295
+ List of SkillInfo objects for each installed skill.
296
+ """
297
+ skills: list[SkillInfo] = []
298
+
299
+ # User skills
300
+ user_dir = Path(USER_SKILLS_DIR)
301
+ if user_dir.exists():
302
+ for entry in sorted(user_dir.iterdir()):
303
+ if entry.is_dir() and _validate_skill_dir(entry):
304
+ skill_md = entry / "SKILL.md"
305
+ info = _parse_skill_md(skill_md)
306
+ skills.append(
307
+ SkillInfo(
308
+ name=info["name"],
309
+ description=info["description"],
310
+ path=entry,
311
+ source="user",
312
+ )
313
+ )
314
+
315
+ # System skills (optional)
316
+ if include_system:
317
+ from .EvoScientist import SKILLS_DIR
318
+
319
+ system_dir = Path(SKILLS_DIR)
320
+ if system_dir.exists():
321
+ for entry in sorted(system_dir.iterdir()):
322
+ if entry.is_dir() and _validate_skill_dir(entry):
323
+ # Skip if user has overridden this skill
324
+ if any(s.name == entry.name for s in skills):
325
+ continue
326
+ skill_md = entry / "SKILL.md"
327
+ info = _parse_skill_md(skill_md)
328
+ skills.append(
329
+ SkillInfo(
330
+ name=info["name"],
331
+ description=info["description"],
332
+ path=entry,
333
+ source="system",
334
+ )
335
+ )
336
+
337
+ return skills
338
+
339
+
340
+ def uninstall_skill(name: str) -> dict:
341
+ """Uninstall a user-installed skill.
342
+
343
+ Args:
344
+ name: Name of the skill to uninstall.
345
+
346
+ Returns:
347
+ Dictionary with result:
348
+ - success: bool
349
+ - error: error message (if failed)
350
+ """
351
+ user_dir = Path(USER_SKILLS_DIR)
352
+ target_path = user_dir / name
353
+
354
+ if not target_path.exists():
355
+ # Try to find by directory name (in case name differs from dir name)
356
+ found = None
357
+ if user_dir.exists():
358
+ for entry in user_dir.iterdir():
359
+ if entry.is_dir() and _validate_skill_dir(entry):
360
+ info = _parse_skill_md(entry / "SKILL.md")
361
+ if info["name"] == name:
362
+ found = entry
363
+ break
364
+
365
+ if not found:
366
+ return {"success": False, "error": f"Skill not found: {name}"}
367
+ target_path = found
368
+
369
+ # Check if it's a user skill (not system)
370
+ if not str(target_path).startswith(str(user_dir)):
371
+ return {"success": False, "error": f"Cannot uninstall system skill: {name}"}
372
+
373
+ # Remove the skill directory
374
+ shutil.rmtree(target_path)
375
+
376
+ return {"success": True, "name": name}
377
+
378
+
379
+ def get_skill_info(name: str) -> SkillInfo | None:
380
+ """Get information about a specific skill.
381
+
382
+ Args:
383
+ name: Name of the skill.
384
+
385
+ Returns:
386
+ SkillInfo if found, None otherwise.
387
+ """
388
+ for skill in list_skills(include_system=True):
389
+ if skill.name == name:
390
+ return skill
391
+ return None
@@ -6,6 +6,9 @@ Provides:
6
6
  - ToolCallTracker: Incremental JSON parsing for tool parameters
7
7
  - ToolResultFormatter: Content-aware result formatting with Rich
8
8
  - Utility functions and constants
9
+ - SubAgentState / StreamState: Stream state tracking
10
+ - stream_agent_events: Async event generator
11
+ - Display functions: Rich rendering for streaming and final output
9
12
  """
10
13
 
11
14
  from .emitter import StreamEventEmitter, StreamEvent
@@ -25,6 +28,15 @@ from .utils import (
25
28
  truncate_with_line_hint,
26
29
  get_status_symbol,
27
30
  )
31
+ from .state import SubAgentState, StreamState, _parse_todo_items, _build_todo_stats
32
+ from .events import stream_agent_events
33
+ from .display import (
34
+ console,
35
+ formatter,
36
+ format_tool_result_compact,
37
+ create_streaming_display,
38
+ display_final_results,
39
+ )
28
40
 
29
41
  __all__ = [
30
42
  # Emitter
@@ -50,4 +62,17 @@ __all__ = [
50
62
  "count_lines",
51
63
  "truncate_with_line_hint",
52
64
  "get_status_symbol",
65
+ # State
66
+ "SubAgentState",
67
+ "StreamState",
68
+ "_parse_todo_items",
69
+ "_build_todo_stats",
70
+ # Events
71
+ "stream_agent_events",
72
+ # Display
73
+ "console",
74
+ "formatter",
75
+ "format_tool_result_compact",
76
+ "create_streaming_display",
77
+ "display_final_results",
53
78
  ]