|
| 1 | +# Tencent is pleased to support the open source community by making tRPC-Agent-Python available. |
| 2 | +# |
| 3 | +# Copyright (C) 2026 Tencent. All rights reserved. |
| 4 | +# |
| 5 | +# tRPC-Agent-Python is licensed under Apache-2.0. |
| 6 | +"""Skill Hub glue: fetch a skill from GitHub and install it into a local |
| 7 | +skills directory before the agent starts. |
| 8 | +
|
| 9 | +`trpc_agent_sdk.skills.hub.SkillSource.fetch()` only returns a `SkillBundle` |
| 10 | +in memory -- writing it to disk (and deciding on overwrite/skip semantics) is |
| 11 | +intentionally left to the caller, since that policy is harness-specific. This |
| 12 | +module shows the minimal version of that glue; a real harness (e.g. |
| 13 | +trpc-hermes's `SkillSpec` + `preinstall_skills_to_dir`) typically adds atomic |
| 14 | +staging and richer error handling on top of the same `SkillSource` adapters. |
| 15 | +""" |
| 16 | + |
| 17 | +from __future__ import annotations |
| 18 | + |
| 19 | +import os |
| 20 | +from pathlib import Path |
| 21 | + |
| 22 | +from trpc_agent_sdk.code_executors import create_local_workspace_runtime |
| 23 | +from trpc_agent_sdk.skills import BaseSkillRepository |
| 24 | +from trpc_agent_sdk.skills import SkillToolSet |
| 25 | +from trpc_agent_sdk.skills import create_default_skill_repository |
| 26 | +from trpc_agent_sdk.skills.hub import GitHubAuth |
| 27 | +from trpc_agent_sdk.skills.hub import GitHubSource |
| 28 | +from trpc_agent_sdk.skills.hub import validate_bundle_rel_path |
| 29 | +from trpc_agent_sdk.skills.hub import validate_skill_name |
| 30 | + |
| 31 | +# `skill-creator` is Anthropic's own skill for building and iterating on |
| 32 | +# skills -- a fitting "meta" skill to fetch through the Skill Hub for a demo. |
| 33 | +GITHUB_SKILL_IDENTIFIER = "anthropics/skills/skills/skill-creator" |
| 34 | +GITHUB_SKILL_NAME = "skill-creator" |
| 35 | + |
| 36 | + |
| 37 | +def install_skill_from_github(*, skills_dir: Path, skill_name: str, identifier: str) -> None: |
| 38 | + """Fetch `identifier` via `GitHubSource` and write it into `skills_dir/skill_name/`. |
| 39 | +
|
| 40 | + Skips the fetch entirely if the skill directory already exists, so |
| 41 | + re-running the demo doesn't re-download the skill every time. |
| 42 | + """ |
| 43 | + safe_name = validate_skill_name(skill_name) |
| 44 | + target_dir = skills_dir / safe_name |
| 45 | + if target_dir.exists(): |
| 46 | + return |
| 47 | + |
| 48 | + # Unauthenticated requests are capped at 60 req/hr, which is plenty for |
| 49 | + # this demo. Set GITHUB_TOKEN to raise that limit for repeated runs. |
| 50 | + source = GitHubSource(GitHubAuth(os.getenv("GITHUB_TOKEN") or None)) |
| 51 | + bundle = source.fetch(identifier) |
| 52 | + if bundle is None: |
| 53 | + raise RuntimeError( |
| 54 | + f"Could not fetch skill {identifier!r} from GitHub via the Skill Hub " |
| 55 | + "(hit the rate limit? set GITHUB_TOKEN in .env)." |
| 56 | + ) |
| 57 | + |
| 58 | + skills_dir.mkdir(parents=True, exist_ok=True) |
| 59 | + target_dir.mkdir(parents=True) |
| 60 | + for rel_path, content in bundle.files.items(): |
| 61 | + safe_rel_path = validate_bundle_rel_path(rel_path) |
| 62 | + dest = target_dir / safe_rel_path |
| 63 | + dest.parent.mkdir(parents=True, exist_ok=True) |
| 64 | + if isinstance(content, bytes): |
| 65 | + dest.write_bytes(content) |
| 66 | + else: |
| 67 | + dest.write_text(content, encoding="utf-8") |
| 68 | + |
| 69 | + |
| 70 | +def create_skill_tool_set(skills_dir: Path) -> tuple[SkillToolSet, BaseSkillRepository]: |
| 71 | + """Build a `SkillToolSet` backed by whatever has been installed into `skills_dir`.""" |
| 72 | + workspace_runtime = create_local_workspace_runtime() |
| 73 | + repository = create_default_skill_repository(str(skills_dir), workspace_runtime=workspace_runtime) |
| 74 | + skill_toolset = SkillToolSet(repository=repository) |
| 75 | + return skill_toolset, repository |
0 commit comments