-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcodec_marketplace.py
More file actions
529 lines (444 loc) Β· 20.6 KB
/
codec_marketplace.py
File metadata and controls
529 lines (444 loc) Β· 20.6 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
528
529
#!/usr/bin/env python3
"""
CODEC Skill Marketplace β Install, search, publish, and manage community skills.
Usage:
python3 codec_marketplace.py install <skill-name>
python3 codec_marketplace.py search <query>
python3 codec_marketplace.py list
python3 codec_marketplace.py update
python3 codec_marketplace.py remove <skill-name>
python3 codec_marketplace.py info <skill-name>
python3 codec_marketplace.py publish <file.py>
Or via CODEC voice: "Hey CODEC, install bitcoin price skill"
"""
import hashlib
import json
import logging
import os
import sys
from datetime import datetime
log = logging.getLogger(__name__)
# ββ Config ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
REGISTRY_URL = "https://raw.githubusercontent.com/AVADSA25/codec-skills/main/registry.json"
SKILLS_BASE_URL = "https://raw.githubusercontent.com/AVADSA25/codec-skills/main"
SKILLS_DIR = os.path.expanduser("~/.codec/skills")
MARKETPLACE_META = os.path.join(SKILLS_DIR, ".marketplace.json")
CACHE_DIR = os.path.expanduser("~/.codec/marketplace_cache")
os.makedirs(SKILLS_DIR, exist_ok=True)
os.makedirs(CACHE_DIR, exist_ok=True)
# ββ Helpers ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def _load_marketplace_meta() -> dict:
"""Load local tracking of installed marketplace skills."""
if os.path.exists(MARKETPLACE_META):
try:
with open(MARKETPLACE_META) as f:
return json.load(f)
except Exception:
pass
return {"installed": {}, "last_update": ""}
def _save_marketplace_meta(meta: dict) -> None:
with open(MARKETPLACE_META, "w") as f:
json.dump(meta, f, indent=2)
def _load_cached_registry() -> dict:
cache_path = os.path.join(CACHE_DIR, "registry.json")
if os.path.exists(cache_path):
try:
with open(cache_path) as f:
return json.load(f)
except Exception:
pass
# Minimal built-in fallback so the tool is still usable offline
return {"skills": [], "categories": []}
def _fetch_registry(silent: bool = False) -> dict:
"""Fetch the skill registry from GitHub, fall back to cache on error."""
try:
import requests
r = requests.get(REGISTRY_URL, timeout=15)
if r.status_code == 200:
data = r.json()
with open(os.path.join(CACHE_DIR, "registry.json"), "w") as f:
json.dump(data, f, indent=2)
return data
if not silent:
print(f"Registry fetch failed: HTTP {r.status_code} β using cached registry")
return _load_cached_registry()
except Exception as e:
if not silent:
print(f"Network error: {e} β using cached registry")
return _load_cached_registry()
def _verify_sha256(content: str, expected_hash: str) -> bool:
"""Verify SHA-256 checksum of downloaded skill content."""
actual = hashlib.sha256(content.encode("utf-8")).hexdigest()
return actual == expected_hash.lower().strip()
def _download_skill(skill_entry: dict, registry: dict) -> str | None:
"""Download a skill .py file from GitHub raw, with SHA-256 verification."""
try:
import requests
base_url = registry.get("base_url", "") or SKILLS_BASE_URL
url = f"{base_url}/{skill_entry['file']}"
r = requests.get(url, timeout=30)
if r.status_code == 200:
code = r.text
expected_hash = skill_entry.get("sha256")
if expected_hash:
if not _verify_sha256(code, expected_hash):
actual = hashlib.sha256(code.encode("utf-8")).hexdigest()
msg = (
f"SHA-256 mismatch for '{skill_entry.get('name', 'unknown')}': "
f"expected {expected_hash}, got {actual}. "
f"Skill rejected β possible tampering or corrupted download."
)
log.warning(msg)
print(f"\n !! CHECKSUM FAILED: {msg}")
return None
else:
log.warning(
"No sha256 checksum in registry for skill '%s' β "
"skipping integrity verification.",
skill_entry.get("name", "unknown"),
)
return code
print(f"Download failed: HTTP {r.status_code} from {url}")
return None
except Exception as e:
print(f"Download error: {e}")
return None
def _install_deps(deps: list) -> None:
import re, subprocess, sys
for dep in deps:
if not re.match(r'^[a-zA-Z0-9_.\-]+$', dep):
print(f" β οΈ Skipping suspicious dependency name: {dep}")
continue
try:
__import__(dep.replace("-", "_"))
except ImportError:
print(f" Installing dependency: {dep}")
subprocess.run(
[sys.executable, "-m", "pip", "install", dep, "--break-system-packages", "--quiet"],
capture_output=True
)
# ββ Commands βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def cmd_install(name: str, interactive: bool = True) -> bool:
"""Install a skill from the marketplace."""
registry = _fetch_registry()
skills = registry.get("skills", [])
# Find skill by name or display_name
match = next(
(s for s in skills
if s["name"] == name or s.get("display_name", "").lower() == name.lower()),
None
)
if not match:
# Try fuzzy: partial name match
match = next(
(s for s in skills if name.lower() in s["name"].lower()),
None
)
if not match:
print(f"β Skill '{name}' not found in marketplace.")
print(f" Try: codec search {name.split('-')[0]}")
return False
# Already installed at same version?
meta = _load_marketplace_meta()
existing = meta["installed"].get(match["name"])
if existing and existing.get("version") == match["version"]:
print(f"β
'{match.get('display_name', match.get('name', 'unknown'))}' v{match['version']} is already installed.")
return True
if existing:
print(f"β¬οΈ Updating '{match.get('display_name', match.get('name', 'unknown'))}' from v{existing.get('version','?')} β v{match['version']}β¦")
# Install dependencies
deps = match.get("dependencies", [])
if deps:
print(f"π¦ Dependencies: {', '.join(deps)}")
_install_deps(deps)
print(f"\n Name: {match.get('display_name', match.get('name', 'unknown'))}")
print(f" Author: {match['author']}" + (" β verified" if match.get("verified") else ""))
print(f" Description: {match['description']}")
print(f" Triggers: {', '.join(match.get('triggers', [])[:4])}")
print(f" Category: {match.get('category', 'general')}")
if not match.get("verified"):
print("\n β οΈ Community skill β not verified by AVA Digital. Review before production use.")
if interactive:
try:
confirm = input("\nInstall? [Y/n] ").strip().lower()
if confirm == "n":
print("Cancelled.")
return False
except (EOFError, KeyboardInterrupt):
pass # Non-interactive (voice/tests) β proceed
print(f"\nDownloading '{match.get('display_name', match.get('name', 'unknown'))}' v{match['version']}β¦")
code = _download_skill(match, registry)
if not code:
return False
py_name = match["name"].replace("-", "_") + ".py"
dest = os.path.join(SKILLS_DIR, py_name)
with open(dest, "w") as f:
f.write(code)
meta["installed"][match["name"]] = {
"version": match["version"],
"file": py_name,
"installed_at": datetime.now().isoformat(),
"author": match["author"],
"verified": match.get("verified", False),
}
_save_marketplace_meta(meta)
print(f"\nβ
'{match.get('display_name', match.get('name', 'unknown'))}' installed β {dest}")
print(f" Restart CODEC to activate: pm2 restart ava-autopilot")
return True
def cmd_search(query: str) -> None:
"""Search for skills in the marketplace."""
registry = _fetch_registry()
q = query.lower()
results = [
s for s in registry.get("skills", [])
if q in f"{s['name']} {s.get('display_name','')} {s.get('description','')} {' '.join(s.get('triggers',[]))} {s.get('category','')}".lower()
]
if not results:
print(f"No skills found for '{query}'. Try a broader term.")
return
meta = _load_marketplace_meta()
print(f"\nπ {len(results)} skill(s) matching '{query}':\n")
for s in results:
installed = " β
installed" if s["name"] in meta.get("installed", {}) else ""
verified = " β" if s.get("verified") else ""
print(f" {s['name']:<26} {s.get('display_name','')}{verified}{installed}")
print(f" {'':<26} {s.get('description','')}")
print(f" {'':<26} triggers: {', '.join(s.get('triggers',[])[:3])}")
print()
print(f" Install: codec install <skill-name>")
def cmd_list() -> None:
"""List all installed skills (built-in + marketplace)."""
meta = _load_marketplace_meta()
marketplace_files = {v.get("file") for v in meta.get("installed", {}).values()}
rows = []
for fname in sorted(os.listdir(SKILLS_DIR)):
if not fname.endswith(".py") or fname.startswith("_"):
continue
source = "marketplace" if fname in marketplace_files else "built-in"
rows.append((fname[:-3], source))
print(f"\nπ¦ CODEC Skills ({len(rows)} installed):\n")
for name, source in rows:
icon = "π" if source == "marketplace" else "π¦"
print(f" {icon} {name:<32} ({source})")
built_in = sum(1 for _, s in rows if s == "built-in")
marketplace = sum(1 for _, s in rows if s == "marketplace")
print(f"\n π¦ Built-in: {built_in} π Marketplace: {marketplace}")
print(f" Browse: codec search <query> | Install: codec install <name>")
def cmd_update() -> None:
"""Update all marketplace skills to latest versions."""
registry = _fetch_registry()
meta = _load_marketplace_meta()
installed = meta.get("installed", {})
if not installed:
print("No marketplace skills installed.")
return
updated = 0
for name, info in installed.items():
remote = next((s for s in registry.get("skills", []) if s["name"] == name), None)
if not remote:
print(f" {name}: not in registry (may have been removed)")
continue
if remote["version"] == info.get("version"):
print(f" {name}: up to date (v{info.get('version')})")
continue
print(f" β¬οΈ {name}: v{info.get('version','?')} β v{remote['version']}")
code = _download_skill(remote, registry)
if code:
with open(os.path.join(SKILLS_DIR, info["file"]), "w") as f:
f.write(code)
meta["installed"][name]["version"] = remote["version"]
meta["installed"][name]["updated_at"] = datetime.now().isoformat()
updated += 1
if updated:
meta["last_update"] = datetime.now().isoformat()
_save_marketplace_meta(meta)
print(f"\nβ
Updated {updated} skill(s). Restart CODEC: pm2 restart ava-autopilot")
else:
print("\nAll marketplace skills are up to date.")
def cmd_remove(name: str) -> None:
"""Remove a marketplace skill."""
meta = _load_marketplace_meta()
if name not in meta.get("installed", {}):
print(f"'{name}' is not a marketplace skill. Use 'codec list' to see installed skills.")
return
info = meta["installed"][name]
filepath = os.path.join(SKILLS_DIR, info["file"])
try:
confirm = input(f"Remove '{name}' ({info['file']})? [Y/n] ").strip().lower()
if confirm == "n":
print("Cancelled.")
return
except (EOFError, KeyboardInterrupt):
pass
if os.path.exists(filepath):
os.remove(filepath)
del meta["installed"][name]
_save_marketplace_meta(meta)
print(f"β
Removed '{name}'. Restart CODEC: pm2 restart ava-autopilot")
def cmd_info(name: str) -> None:
"""Show detailed info about a marketplace skill."""
registry = _fetch_registry()
skill = next((s for s in registry.get("skills", []) if s["name"] == name), None)
if not skill:
print(f"Skill '{name}' not found in marketplace.")
return
meta = _load_marketplace_meta()
installed = name in meta.get("installed", {})
print(f"\nπ¦ {skill.get('display_name', name)}")
print(f" Name: {skill['name']}")
print(f" Version: {skill['version']}")
print(f" Author: {skill['author']}" + (" β verified" if skill.get("verified") else ""))
print(f" Category: {skill.get('category', 'general')}")
print(f" Description: {skill['description']}")
print(f" Triggers: {', '.join(skill.get('triggers', []))}")
print(f" Dependencies: {', '.join(skill.get('dependencies', [])) or 'none'}")
print(f" Status: {'β
installed' if installed else 'not installed'}")
if installed:
info = meta["installed"][name]
print(f" Installed: {info.get('installed_at', '')[:10]}")
print()
def cmd_publish(filepath: str) -> None:
"""Guide user through publishing a skill to the marketplace."""
if not os.path.exists(filepath):
print(f"File not found: {filepath}")
return
import re
with open(filepath) as f:
code = f.read()
issues = []
if "SKILL_TRIGGERS" not in code: issues.append("Missing SKILL_TRIGGERS = [...]")
if "SKILL_DESCRIPTION" not in code: issues.append("Missing SKILL_DESCRIPTION = '...'")
if "def run(" not in code: issues.append("Missing def run(task, context=None)")
triggers_match = re.search(r"SKILL_TRIGGERS\s*=\s*\[([^\]]+)\]", code)
trigger_count = len(triggers_match.group(1).split(",")) if triggers_match else 0
print(f"\nπ CODEC Skill Publish Guide")
print("=" * 42)
if issues:
print("\nβ οΈ Issues found in your skill file:")
for issue in issues:
print(f" - {issue}")
else:
print(f"\nβ
Skill format looks good! ({trigger_count} triggers found)")
print("""
To publish to the CODEC marketplace:
1. Fork https://github.com/AVADSA25/codec-skills
2. Create skills/your-skill-name/your_skill.py
3. Create skills/your-skill-name/skill.json with metadata
4. Add your entry to registry.json
5. Open a Pull Request
skill.json template:""")
basename = os.path.basename(filepath).replace(".py", "")
print(json.dumps({
"name": basename.replace("_", "-"),
"display_name": basename.replace("_", " ").title(),
"description": "What this skill does",
"version": "1.0.0",
"author": "your-github-username",
"author_github": "your-github-username",
"triggers": ["trigger one", "trigger two", "trigger three"],
"category": "utility",
"dependencies": [],
"file": f"{basename.replace('_','-')}/{basename}.py",
"verified": False
}, indent=2))
print(f"\n Guidelines: https://github.com/AVADSA25/codec-skills/blob/main/CONTRIBUTING.md")
# ββ CODEC Skill (voice-accessible) ββββββββββββββββββββββββββββββββββββββββββ
SKILL_NAME = "marketplace"
SKILL_TRIGGERS = [
"install skill", "marketplace", "skill marketplace", "search skills",
"browse skills", "available skills", "codec install", "skill store",
"download skill", "find skill"
]
SKILL_DESCRIPTION = "Browse and install skills from the CODEC Skill Marketplace"
def run(task: str, context: str = "") -> str:
"""Voice-accessible marketplace entry point."""
lower = task.lower()
if any(w in lower for w in ["install ", "download "]):
for prefix in ["install skill ", "install ", "download skill ", "download "]:
if prefix in lower:
name = lower.split(prefix, 1)[1].strip().rstrip(".").replace(" ", "-")
registry = _fetch_registry(silent=True)
match = next(
(s for s in registry.get("skills", []) if s["name"] == name or name in s["name"]),
None
)
if not match:
return f"Skill '{name}' not found in marketplace. Say 'search skills {name}' to browse."
code = _download_skill(match, registry)
if not code:
return f"Failed to download '{name}' β check your internet connection."
py_name = match["name"].replace("-", "_") + ".py"
dest = os.path.join(SKILLS_DIR, py_name)
with open(dest, "w") as f:
f.write(code)
meta = _load_marketplace_meta()
meta["installed"][match["name"]] = {
"version": match["version"],
"file": py_name,
"installed_at": datetime.now().isoformat(),
"author": match["author"],
"verified": match.get("verified", False),
}
_save_marketplace_meta(meta)
return f"Installed {match.get('display_name', match.get('name', 'unknown'))} v{match['version']}. Restart CODEC to activate."
if any(w in lower for w in ["search ", "find ", "browse "]):
for prefix in ["search skills ", "search skill ", "search ", "find skill ", "find ", "browse skills ", "browse "]:
if prefix in lower:
query = lower.split(prefix, 1)[1].strip().rstrip(".")
registry = _fetch_registry(silent=True)
results = [
s for s in registry.get("skills", [])
if query in f"{s['name']} {s.get('description','')} {' '.join(s.get('triggers',[]))}".lower()
]
if results:
lines = [f"Found {len(results)} skill(s) for '{query}':"]
for s in results[:5]:
v = " (verified)" if s.get("verified") else ""
lines.append(f" {s['name']} β {s['description']}{v}")
lines.append("Say 'install [name]' to install one.")
return "\n".join(lines)
return f"No skills found for '{query}'. Try a broader search term."
# Default: marketplace summary
registry = _fetch_registry(silent=True)
total = len(registry.get("skills", []))
meta = _load_marketplace_meta()
inst = len(meta.get("installed", {}))
cats = registry.get("categories", [])
return (
f"CODEC Skill Marketplace: {total} skills available, {inst} installed.\n"
f"Categories: {', '.join(cats[:6])}.\n"
f"Say 'search skills [topic]' or 'install skill [name]'."
)
# ββ CLI Entry Point ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
if __name__ == "__main__":
HELP = """\
CODEC Skill Marketplace
βββββββββββββββββββββββββββββββββββββββ
codec install <name> Install a skill
codec search <query> Search available skills
codec list List all installed skills
codec update Update all marketplace skills
codec remove <name> Uninstall a marketplace skill
codec info <name> Show skill details
codec publish <file.py> Publishing guide
"""
if len(sys.argv) < 2:
print(HELP)
sys.exit(0)
cmd = sys.argv[1]
arg = sys.argv[2] if len(sys.argv) > 2 else ""
commands = {
"install": lambda: cmd_install(arg),
"search": lambda: cmd_search(arg),
"list": lambda: cmd_list(),
"update": lambda: cmd_update(),
"remove": lambda: cmd_remove(arg),
"info": lambda: cmd_info(arg),
"publish": lambda: cmd_publish(arg),
}
if cmd in commands:
commands[cmd]()
else:
print(f"Unknown command: {cmd}\n")
print(HELP)
sys.exit(1)