-
Notifications
You must be signed in to change notification settings - Fork 1
feat(sdk): 实现 Python SDK 最小可用(Client + Adapter + GenericAdapter + Lea… #17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| # GeneHub Python SDK | ||
|
|
||
| GeneHub 的 Python 客户端与产品适配层,与 TypeScript SDK 核心能力对齐,供 Python 侧 Agent 接入 GeneHub。 | ||
|
|
||
| ## 用途 | ||
|
|
||
| - **GeneHubClient**:封装 Registry HTTP API,支持搜索基因、获取详情、获取 Manifest、发布基因。 | ||
| - **GeneAdapter**:产品适配器抽象基类,定义 `detect` / `install` / `uninstall` / `is_installed` / `list`。 | ||
| - **GenericAdapter**:基于文件系统的通用适配器,将基因写入 `.genehub/genes/`(可配置目录)。 | ||
| - **LearningEngine**:标准学习协议引擎,创建学习任务、检查结果(最小可用)。 | ||
|
|
||
| ## 目录结构 | ||
|
|
||
| ``` | ||
| packages/sdk/python/ | ||
| ├── pyproject.toml | ||
| ├── README.md | ||
| ├── src/ | ||
| │ └── genehub_sdk/ | ||
| │ ├── __init__.py | ||
| │ ├── client.py # GeneHubClient | ||
| │ ├── types.py # Gene / GeneManifest / 适配器相关类型 | ||
| │ ├── adapters/ | ||
| │ │ ├── __init__.py | ||
| │ │ ├── base.py # GeneAdapter 抽象基类 | ||
| │ │ └── generic.py # GenericAdapter | ||
| │ └── learning/ | ||
| │ ├── __init__.py | ||
| │ └── engine.py # LearningEngine | ||
| └── tests/ | ||
| ├── test_client.py | ||
| ├── test_generic_adapter.py | ||
| └── test_learning_engine.py | ||
| ``` | ||
|
|
||
| ## 使用方法 | ||
|
|
||
| ### 安装 | ||
|
|
||
| 在项目根目录使用 uv(推荐): | ||
|
|
||
| ```bash | ||
| uv add ./packages/sdk/python | ||
| # 或从 PyPI(发布后):uv add genehub-sdk | ||
| ``` | ||
|
|
||
| ### 客户端 | ||
|
|
||
| ```python | ||
| from genehub_sdk import GeneHubClient | ||
|
|
||
| client = GeneHubClient(base_url="https://registry.genehub.dev", token="ghb_xxx") | ||
| genes = client.search_genes(q="code") | ||
| gene = client.get_gene("clean-code") | ||
| manifest = client.get_manifest("clean-code", version="1.0.0") | ||
| published = client.publish(manifest) | ||
| ``` | ||
|
|
||
| ### 适配器 | ||
|
|
||
| ```python | ||
| from genehub_sdk import GenericAdapter | ||
| from genehub_sdk.adapters import GeneAdapter | ||
|
|
||
| adapter: GeneAdapter = GenericAdapter(genes_dir="/path/to/genes") | ||
| if adapter.detect(): | ||
| adapter.install(manifest, options={"force": True}) | ||
| adapter.uninstall("clean-code") | ||
| print(adapter.list()) | ||
| ``` | ||
|
|
||
| ### 学习引擎 | ||
|
|
||
| ```python | ||
| from genehub_sdk import GeneHubClient | ||
| from genehub_sdk.adapters import GenericAdapter | ||
| from genehub_sdk.learning import LearningEngine | ||
|
|
||
| engine = LearningEngine(workspace_dir=".", adapter=GenericAdapter(), client=GeneHubClient(...)) | ||
| task = engine.create_learning_task(manifest) | ||
| result = engine.check_result(manifest["slug"]) | ||
| ``` | ||
|
|
||
| ## 技术栈 | ||
|
|
||
| - Python 3.12+ | ||
| - httpx(HTTP 客户端) | ||
| - PyYAML(Manifest 序列化) | ||
| - 测试:pytest、pytest-httpx | ||
| - Lint:Ruff | ||
|
|
||
| ## 参考 | ||
|
|
||
| - TypeScript SDK:`packages/sdk/typescript/src/` | ||
| - API 文档:`docs/architecture.md` 第 6 节 | ||
| - 学习协议:`docs/gene-learning-protocol.md` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| [project] | ||
| name = "genehub-sdk" | ||
| version = "0.1.0" | ||
| description = "GeneHub Python SDK - API 客户端与产品适配器(最小可用)" | ||
| readme = "README.md" | ||
| requires-python = ">=3.12" | ||
| dependencies = [ | ||
| "httpx>=0.27.0", | ||
| "pyyaml>=6.0", | ||
| ] | ||
|
|
||
| [project.optional-dependencies] | ||
| dev = [ | ||
| "pytest>=8.0.0", | ||
| "pytest-httpx>=0.30.0", | ||
| "ruff>=0.8.0", | ||
| ] | ||
|
|
||
| [build-system] | ||
| requires = ["hatchling"] | ||
| build-backend = "hatchling.build" | ||
|
|
||
| [tool.hatch.build.targets.wheel] | ||
| packages = ["src/genehub_sdk"] | ||
|
|
||
| [tool.ruff] | ||
| target-version = "py312" | ||
| line-length = 100 | ||
|
|
||
| [tool.ruff.lint] | ||
| select = ["E", "F", "I", "UP", "B", "C4", "SIM"] | ||
| ignore = ["E501"] | ||
|
|
||
| [tool.ruff.lint.isort] | ||
| known-first-party = ["genehub_sdk"] | ||
|
|
||
| [tool.pytest.ini_options] | ||
| testpaths = ["tests"] | ||
| pythonpath = ["src"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| """GeneHub Python SDK:API 客户端与产品适配器。""" | ||
|
|
||
| from genehub_sdk.adapters import GeneAdapter, GenericAdapter | ||
| from genehub_sdk.client import GeneHubClient, GeneHubError | ||
| from genehub_sdk.learning import LearningEngine | ||
| from genehub_sdk.types import ( | ||
| Gene, | ||
| GeneManifest, | ||
| InstalledGene, | ||
| InstallOptions, | ||
| InstallResult, | ||
| UninstallResult, | ||
| ) | ||
|
|
||
| __all__ = [ | ||
| "GeneHubClient", | ||
| "GeneHubError", | ||
| "Gene", | ||
| "GeneManifest", | ||
| "GeneAdapter", | ||
| "GenericAdapter", | ||
| "InstallOptions", | ||
| "InstallResult", | ||
| "InstalledGene", | ||
| "UninstallResult", | ||
| "LearningEngine", | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| """产品适配器:基类与 Generic 文件系统适配器。""" | ||
|
|
||
| from genehub_sdk.adapters.base import GeneAdapter | ||
| from genehub_sdk.adapters.generic import GenericAdapter | ||
|
|
||
| __all__ = ["GeneAdapter", "GenericAdapter"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| """产品适配器抽象基类,与 types/adapter.ts GeneAdapter 对齐。""" | ||
|
|
||
| from abc import ABC, abstractmethod | ||
|
|
||
| from genehub_sdk.types import ( | ||
| GeneManifest, | ||
| InstalledGene, | ||
| InstallOptions, | ||
| InstallResult, | ||
| UninstallOptions, | ||
| UninstallResult, | ||
| ) | ||
|
|
||
|
|
||
| class GeneAdapter(ABC): | ||
| """将 Gene Manifest 注入到目标产品的适配器接口。""" | ||
|
|
||
| @property | ||
| @abstractmethod | ||
| def product(self) -> str: | ||
| """产品标识,如 generic / openclaw / nanobot。""" | ||
| ... | ||
|
|
||
| @abstractmethod | ||
| def detect(self) -> bool: | ||
| """检测当前环境是否支持该适配器。""" | ||
| ... | ||
|
|
||
| @abstractmethod | ||
| def install( | ||
| self, manifest: GeneManifest, options: InstallOptions | None = None | ||
| ) -> InstallResult: | ||
| """安装基因。""" | ||
| ... | ||
|
|
||
| @abstractmethod | ||
| def uninstall(self, slug: str, options: UninstallOptions | None = None) -> UninstallResult: | ||
| """卸载基因。""" | ||
| ... | ||
|
|
||
| @abstractmethod | ||
| def is_installed(self, slug: str) -> bool: | ||
| """是否已安装该基因。""" | ||
| ... | ||
|
|
||
| @abstractmethod | ||
| def list(self) -> list[InstalledGene]: | ||
| """列出已安装的基因。""" | ||
| ... | ||
|
|
||
| def get_installed_version(self, slug: str) -> str | None: | ||
| """返回已安装版本号,未安装返回 None。默认通过 list 查找。""" | ||
| for g in self.list(): | ||
| if g["slug"] == slug: | ||
| return g["version"] | ||
| return None |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
README 示例里调用了
client.search_genes(q="code"),但GeneHubClient.search_genes的参数名是query(或直接用第一个位置参数)。按当前示例会触发TypeError: unexpected keyword argument 'q';建议将示例改为client.search_genes("code")或client.search_genes(query="code")。