-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall-opencode.py
More file actions
527 lines (411 loc) · 16 KB
/
install-opencode.py
File metadata and controls
527 lines (411 loc) · 16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
#!/usr/bin/env python3
"""
OpenCode Skills Installer
Install skills from the cycleuser/Skills repository to OpenCode's skill directory.
Converts SKILL.md files to OpenCode-compatible format.
"""
import argparse
import json
import os
import re
import shutil
import sys
import urllib.request
from pathlib import Path
from typing import Optional
REPO_URL = "https://github.com/cycleuser/Skills"
RAW_URL = "https://raw.githubusercontent.com/cycleuser/Skills/main"
OPENCODE_GLOBAL_DIR = Path.home() / ".config" / "opencode" / "skills"
OPENCODE_PROJECT_DIR = Path(".opencode") / "skills"
OPENCODE_COMMANDS_GLOBAL_DIR = Path.home() / ".config" / "opencode" / "commands"
OPENCODE_COMMANDS_PROJECT_DIR = Path(".opencode") / "commands"
SKILLS = [
"skill-manager",
"master-architect",
"python-project-developer",
"software-planner",
"coding-agent-patterns",
"iteration-manager",
"academic-writer",
"patent-writer",
]
COMMANDS = [
"architect",
"python-dev",
"plan",
"iterate",
"academic",
"patent",
"skills",
]
def convert_skill_to_opencode(content: str) -> str:
"""Convert SKILL.md content to OpenCode-compatible format."""
if not content.startswith("---"):
return content
parts = content.split("---", 2)
if len(parts) < 3:
return content
frontmatter = parts[1].strip()
body = parts[2]
lines = frontmatter.split("\n")
new_frontmatter = []
name = None
description_lines = []
in_description = False
license_val = "MIT"
metadata = {}
i = 0
while i < len(lines):
line = lines[i]
if line.startswith("name:"):
name = line.split(":", 1)[1].strip().strip('"')
new_frontmatter.append(f'name: {name}')
elif line.startswith("description:"):
desc_val = line.split(":", 1)[1].strip()
if desc_val.startswith("|"):
in_description = True
else:
description_lines.append(desc_val)
elif in_description:
if line and not line[0].isspace() and ":" in line:
in_description = False
i -= 1
elif line.strip():
description_lines.append(line.strip())
elif line.startswith("license:"):
license_val = line.split(":", 1)[1].strip().strip('"')
elif line.startswith("version:"):
metadata["version"] = line.split(":", 1)[1].strip().strip('"')
elif line.startswith("author:"):
metadata["author"] = line.split(":", 1)[1].strip().strip('"')
elif line.startswith("priority:"):
metadata["priority"] = line.split(":", 1)[1].strip()
elif line.startswith("auto_load:"):
metadata["auto_load"] = line.split(":", 1)[1].strip()
i += 1
description = " ".join(description_lines)
if len(description) > 1024:
description = description[:1021] + "..."
new_frontmatter.append(f'description: {description}')
new_frontmatter.append(f'license: {license_val}')
new_frontmatter.append('compatibility: opencode')
if metadata:
new_frontmatter.append('metadata:')
for key, value in metadata.items():
new_frontmatter.append(f' {key}: {value}')
return "---\n" + "\n".join(new_frontmatter) + "\n---" + body
def get_default_install_dir(global_install: bool = True) -> Path:
"""Get default installation directory."""
if global_install:
return OPENCODE_GLOBAL_DIR
return OPENCODE_PROJECT_DIR
def get_default_commands_dir(global_install: bool = True) -> Path:
"""Get default commands directory."""
if global_install:
return OPENCODE_COMMANDS_GLOBAL_DIR
return OPENCODE_COMMANDS_PROJECT_DIR
def download_file(url: str, dest: Path) -> bool:
"""Download a file from URL."""
try:
urllib.request.urlretrieve(url, dest)
return True
except Exception as e:
print(f"Error downloading {url}: {e}")
return False
def install_from_local(source_dir: Path, install_dir: Path, skills: Optional[list] = None) -> bool:
"""Install skills from local directory."""
source_skills = source_dir / "skills"
if not source_skills.exists():
print(f"Error: Skills directory not found at {source_skills}")
return False
install_dir.mkdir(parents=True, exist_ok=True)
if skills:
skill_list = skills
else:
skill_list = [d.name for d in source_skills.iterdir() if d.is_dir() and (d / "SKILL.md").exists()]
installed = []
failed = []
for skill_name in skill_list:
source_skill = source_skills / skill_name
dest_skill = install_dir / skill_name
if not source_skill.exists():
print(f" ⚠ Skill not found: {skill_name}")
failed.append(skill_name)
continue
skill_file = source_skill / "SKILL.md"
if not skill_file.exists():
print(f" ⚠ SKILL.md not found: {skill_name}")
failed.append(skill_name)
continue
if dest_skill.exists():
shutil.rmtree(dest_skill)
dest_skill.mkdir(parents=True, exist_ok=True)
content = skill_file.read_text()
converted = convert_skill_to_opencode(content)
(dest_skill / "SKILL.md").write_text(converted)
rules_src = source_skill / "rules"
if rules_src.exists():
rules_dest = dest_skill / "rules"
rules_dest.mkdir(exist_ok=True)
for rule_file in rules_src.glob("*.md"):
shutil.copy(rule_file, rules_dest / rule_file.name)
installed.append(skill_name)
print(f" ✓ Installed: {skill_name}")
print(f"\nInstalled {len(installed)} skill(s) to {install_dir}")
if failed:
print(f"Failed: {', '.join(failed)}")
return len(installed) > 0
def install_commands_from_local(source_dir: Path, commands_dir: Path, commands: Optional[list] = None) -> bool:
"""Install commands from local directory."""
source_commands = source_dir / "commands"
if not source_commands.exists():
print(f" ⚠ Commands directory not found at {source_commands}")
return False
commands_dir.mkdir(parents=True, exist_ok=True)
if commands:
command_list = commands
else:
command_list = [f.stem for f in source_commands.glob("*.md")]
installed = []
failed = []
for command_name in command_list:
source_file = source_commands / f"{command_name}.md"
dest_file = commands_dir / f"{command_name}.md"
if not source_file.exists():
print(f" ⚠ Command not found: {command_name}")
failed.append(command_name)
continue
shutil.copy(source_file, dest_file)
installed.append(command_name)
print(f" ✓ Installed command: /{command_name}")
print(f"\nInstalled {len(installed)} command(s) to {commands_dir}")
if failed:
print(f"Failed: {', '.join(failed)}")
return len(installed) > 0
def install_commands_from_github(commands_dir: Path, commands: Optional[list] = None) -> bool:
"""Install commands from GitHub repository."""
commands_dir.mkdir(parents=True, exist_ok=True)
if not commands:
commands = COMMANDS
installed = []
failed = []
for command_name in commands:
command_url = f"{RAW_URL}/commands/{command_name}.md"
dest_file = commands_dir / f"{command_name}.md"
if download_file(command_url, dest_file):
installed.append(command_name)
print(f" ✓ Installed command: /{command_name}")
else:
failed.append(command_name)
print(f" ✗ Failed: /{command_name}")
print(f"\nInstalled {len(installed)} command(s) to {commands_dir}")
if failed:
print(f"Failed: {', '.join(failed)}")
return len(installed) > 0
def install_from_github(install_dir: Path, skills: Optional[list] = None) -> bool:
"""Install skills from GitHub repository."""
print(f"Installing from {REPO_URL}")
install_dir.mkdir(parents=True, exist_ok=True)
if not skills:
skills = SKILLS
installed = []
failed = []
for skill_name in skills:
print(f" Installing {skill_name}...")
skill_dir = install_dir / skill_name
skill_dir.mkdir(parents=True, exist_ok=True)
skill_url = f"{RAW_URL}/skills/{skill_name}/SKILL.md"
temp_file = skill_dir / "SKILL.md.tmp"
if not download_file(skill_url, temp_file):
shutil.rmtree(skill_dir)
failed.append(skill_name)
print(f" ✗ Failed: {skill_name}")
continue
content = temp_file.read_text()
temp_file.unlink()
converted = convert_skill_to_opencode(content)
(skill_dir / "SKILL.md").write_text(converted)
rules_dir = skill_dir / "rules"
rules_dir.mkdir(exist_ok=True)
common_rules = [
"registry.md", "testing-protocol.md", "quality-metrics.md",
"iteration-workflow.md", "literature-search.md", "citation-format.md",
"paper-structure.md", "writing-style.md", "pre-development.md",
"interface-design.md", "documentation.md", "sample-data.md",
"project-structure.md", "cli-flags.md", "api-pattern.md",
"tools-integration.md", "context-management.md", "tool-safety.md",
"multi-provider.md", "memory-systems.md", "requirement-analysis.md",
"architecture-design.md", "task-decomposition.md", "iteration-protocol.md",
"quality-gates.md",
]
for rule in common_rules:
rule_url = f"{RAW_URL}/skills/{skill_name}/rules/{rule}"
rule_file = rules_dir / rule
download_file(rule_url, rule_file)
installed.append(skill_name)
print(f" ✓ Installed: {skill_name}")
print(f"\nInstalled {len(installed)} skill(s) to {install_dir}")
if failed:
print(f"Failed: {', '.join(failed)}")
return len(installed) > 0
def list_installed_skills(install_dir: Path) -> list:
"""List installed skills."""
skills = []
if not install_dir.exists():
return skills
for skill_dir in install_dir.iterdir():
if skill_dir.is_dir() and (skill_dir / "SKILL.md").exists():
skills.append(skill_dir.name)
return sorted(skills)
def get_skill_info(skill_path: Path) -> dict:
"""Parse skill metadata from SKILL.md."""
skill_file = skill_path / "SKILL.md"
if not skill_file.exists():
return {}
content = skill_file.read_text()
if content.startswith("---"):
parts = content.split("---", 2)
if len(parts) >= 3:
frontmatter = parts[1].strip()
info = {}
for line in frontmatter.split("\n"):
if ":" in line and not line.startswith(" "):
key, value = line.split(":", 1)
info[key.strip()] = value.strip()
return info
return {}
def show_skill_info(install_dir: Path, skill_name: str):
"""Show detailed information about a skill."""
skill_path = install_dir / skill_name
if not skill_path.exists():
print(f"Skill not found: {skill_name}")
return
info = get_skill_info(skill_path)
print(f"\n{skill_name}")
print("=" * len(skill_name))
print(f"Description: {info.get('description', 'No description')}")
print(f"License: {info.get('license', 'unknown')}")
rules_dir = skill_path / "rules"
if rules_dir.exists():
rules = [r.stem for r in rules_dir.glob("*.md")]
if rules:
print(f"\nRules: {', '.join(rules)}")
def main():
parser = argparse.ArgumentParser(
description="Install skills for OpenCode from cycleuser/Skills repository"
)
parser.add_argument(
"command",
choices=["install", "list", "info", "uninstall"],
help="Command to execute"
)
parser.add_argument(
"--source",
type=str,
default=None,
help="Source directory (default: GitHub)"
)
parser.add_argument(
"--target",
type=str,
default=None,
help="Target installation directory"
)
parser.add_argument(
"--global",
dest="global_install",
action="store_true",
default=True,
help="Install globally (default)"
)
parser.add_argument(
"--local",
dest="global_install",
action="store_false",
help="Install to project .opencode directory"
)
parser.add_argument(
"--skills",
type=str,
nargs="+",
default=None,
help="Specific skills to install"
)
args = parser.parse_args()
if args.target:
install_dir = Path(args.target)
commands_dir = Path(args.target).parent / "commands"
else:
install_dir = get_default_install_dir(args.global_install)
commands_dir = get_default_commands_dir(args.global_install)
if args.command == "install":
print("=" * 60)
print(" Installing Skills")
print("=" * 60)
print(f"Skills directory: {install_dir}")
if args.source:
source_path = Path(args.source)
if source_path.exists():
success = install_from_local(source_path, install_dir, args.skills)
print()
print("=" * 60)
print(" Installing Commands")
print("=" * 60)
print(f"Commands directory: {commands_dir}")
install_commands_from_local(source_path, commands_dir)
else:
print(f"Source not found: {args.source}")
sys.exit(1)
else:
success = install_from_github(install_dir, args.skills)
print()
print("=" * 60)
print(" Installing Commands")
print("=" * 60)
print(f"Commands directory: {commands_dir}")
install_commands_from_github(commands_dir)
if success:
print()
print("=" * 60)
print(" Installation Complete!")
print("=" * 60)
print()
print("Available commands:")
print(" /architect <task> - Architecture design")
print(" /python-dev <task> - Python development")
print(" /plan <task> - Software planning")
print(" /iterate <task> - Iterative testing")
print(" /academic <task> - Academic writing")
print(" /skills - List all skills")
print()
else:
sys.exit(1)
elif args.command == "list":
print(f"Installed skills in: {install_dir}")
skills = list_installed_skills(install_dir)
if skills:
for skill in skills:
info = get_skill_info(install_dir / skill)
desc = info.get("description", "No description")[:60]
print(f" {skill:30} {desc}...")
else:
print(" No skills installed")
elif args.command == "info":
if not args.skills:
print("Please specify skill name(s)")
sys.exit(1)
for skill_name in args.skills:
show_skill_info(install_dir, skill_name)
elif args.command == "uninstall":
if not args.skills:
print("Please specify skill name(s) to uninstall")
sys.exit(1)
for skill_name in args.skills:
skill_path = install_dir / skill_name
if skill_path.exists():
shutil.rmtree(skill_path)
print(f"✓ Uninstalled: {skill_name}")
else:
print(f"⚠ Not found: {skill_name}")
if __name__ == "__main__":
main()