-
Notifications
You must be signed in to change notification settings - Fork 3k
Feat/python sdk #2779
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
Feat/python sdk #2779
Changes from 24 commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
793fe37
feat(sdk/python): scaffold Python SDK with uv + openapi-python-client…
Kking112 e6e9837
chore(sdk/python): remove obsolete requirements-dev.txt in favor of u…
Kking112 7b263eb
feat(sdk/python): Phase 2 - enhance generator to fetch OpenAPI from s…
Kking112 ab50f34
docs(sdk/python): check off Phase 2 tasks in TODO.md; generator suppo…
Kking112 8a10bb2
feat(sdk/python): Phase 3 - add openapi-python-client config; inject …
Kking112 4cc4344
feat(sdk/python): Phase 4 - extend OpenCodeClient with auth, retries,…
Kking112 ccc4875
test(sdk/python): Phase 5 - add wrapper tests (MockTransport), SSE pa…
Kking112 a3cd614
test(docs)(sdk/python): Phase 5 - add examples (sessions, file status…
Kking112 265151a
test(sdk/python): Phase 5 integration - start live opencode server an…
Kking112 8a1b617
ci(build)(sdk/python): Phase 6 - integrate Python SDK gen into bun ge…
Kking112 fa2348e
ci(release)(sdk/python): Phase 7 - add PyPI publishing workflow, impl…
Kking112 d650351
docs(sdk/python): add mkdocs site with installation, quickstart, usag…
Kking112 845912b
.
Kking112 3e0f431
chore(sdk/python): Phase 8 - cleanup: remove built docs site from VCS…
Kking112 78b815b
Prepare for inital PR
Kking112 ed7601f
added lock file
Kking112 12a10ad
Merge branch 'dev' into feat/python_sdk
Kking112 fcc2b72
Merge branch 'dev' into feat/python_sdk
Kking112 a7d13df
Merge branch 'dev' into feat/python_sdk
Kking112 7da96b0
Merge branch 'dev' into feat/python_sdk
Kking112 46a602a
ci(release): fix publish-python-sdk workflow - run version bump via u…
Kking112 9e74db3
chore(typecheck): normalize typecheck to use tsc; scope root typechec…
Kking112 b967ff3
.
Kking112 ddc03b3
Merge branch 'dev' into feat/python_sdk
Kking112 62f7da5
Merge branch 'dev' into feat/python_sdk
Kking112 9993732
Merge branch 'dev' into feat/python_sdk
Kking112 78c6615
fixed unintentional package.json edits that were made outside of the …
Kking112 263deed
fmt: package.json files
rekram1-node 1213b6e
disable new actions
rekram1-node f9c7d1f
cleanup
rekram1-node 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| name: publish-python-sdk | ||
|
|
||
| on: | ||
| release: | ||
| types: [published] | ||
| workflow_dispatch: | ||
|
|
||
| jobs: | ||
| publish: | ||
| runs-on: ubuntu-latest | ||
| permissions: | ||
| contents: read | ||
| steps: | ||
| - name: Checkout repository | ||
| uses: actions/checkout@v4 | ||
|
|
||
| - name: Setup Bun | ||
| uses: oven-sh/setup-bun@v1 | ||
| with: | ||
| bun-version: 1.2.21 | ||
|
|
||
| - name: Install dependencies (JS/Bun) | ||
| run: bun install | ||
|
|
||
| - name: Install uv | ||
| shell: bash | ||
| run: curl -LsSf https://astral.sh/uv/install.sh | sh | ||
|
|
||
| - name: Generate Python SDK from OpenAPI (CLI) | ||
| shell: bash | ||
| run: | | ||
| ~/.local/bin/uv run --project packages/sdk/python python packages/sdk/python/scripts/generate.py --source cli | ||
|
|
||
| - name: Sync Python dependencies | ||
| shell: bash | ||
| run: | | ||
| ~/.local/bin/uv sync --dev --project packages/sdk/python | ||
|
|
||
| - name: Set version from release tag | ||
| shell: bash | ||
| run: | | ||
| TAG="${GITHUB_REF_NAME:-}" | ||
| if [ -z "$TAG" ]; then | ||
| TAG="$(git describe --tags --abbrev=0 || echo 0.0.0)" | ||
| fi | ||
| echo "Using version: $TAG" | ||
| VERSION="$TAG" ~/.local/bin/uv run --project packages/sdk/python python - <<'PY' | ||
| import os, re, pathlib | ||
| root = pathlib.Path('packages/sdk/python') | ||
| pt = (root / 'pyproject.toml').read_text() | ||
| version = os.environ.get('VERSION','0.0.0').lstrip('v') | ||
| pt = re.sub(r'(?m)^(version\s*=\s*")[^"]+("\s*)$', f"\\1{version}\\2", pt) | ||
| (root / 'pyproject.toml').write_text(pt) | ||
| # Also update generator config override for consistency | ||
| cfgp = root / 'openapi-python-client.yaml' | ||
| if cfgp.exists(): | ||
| cfg = cfgp.read_text() | ||
| cfg = re.sub(r'(?m)^(package_version_override:\s*)\S+$', f"\\1{version}", cfg) | ||
| cfgp.write_text(cfg) | ||
| PY | ||
|
|
||
| - name: Build and publish to PyPI | ||
| env: | ||
| PYPI_TOKEN: ${{ secrets.PYPI_API_TOKEN }} | ||
| shell: bash | ||
| run: | | ||
| ~/.local/bin/uv run --project packages/sdk/python python packages/sdk/python/scripts/publish.py |
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 |
|---|---|---|
|
|
@@ -6,7 +6,7 @@ | |
| "packageManager": "[email protected]", | ||
| "scripts": { | ||
| "dev": "bun run packages/opencode/src/index.ts", | ||
| "typecheck": "bun turbo typecheck", | ||
| "typecheck": "bun turbo typecheck --filter=@opencode-ai/sdk", | ||
| "prepare": "husky" | ||
| }, | ||
| "workspaces": { | ||
|
|
||
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
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,22 @@ | ||
| __pycache__/ | ||
| *.py[cod] | ||
| *.egg-info/ | ||
| .build/ | ||
| build/ | ||
| dist/ | ||
| .coverage | ||
| htmlcov/ | ||
| .mypy_cache/ | ||
| .pytest_cache/ | ||
| .ruff_cache/ | ||
| .venv/ | ||
| .conda/ | ||
| .env | ||
| .DS_Store | ||
| openapi.json | ||
| site/ | ||
|
|
||
|
|
||
| # IDE | ||
| .vscode/ | ||
| .idea/ |
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,82 @@ | ||
| # Opencode Python SDK | ||
|
|
||
| This package provides a Python SDK for the Opencode API. It is generated using openapi-python-client (not Stainless). | ||
|
|
||
|
|
||
| Documentation | ||
| - Full docs: see `mkdocs` site under `packages/sdk/python/docs/` | ||
| - Preview locally: | ||
| ```bash | ||
| uv run --project packages/sdk/python mkdocs serve -f packages/sdk/python/mkdocs.yml | ||
| ``` | ||
|
|
||
| Badges | ||
| - PyPI: https://img.shields.io/pypi/v/opencode-ai?style=flat-square | ||
|
|
||
| Requirements | ||
| - Python 3.8+ | ||
| - uv (recommended) -> https://docs.astral.sh/uv/ | ||
| - openapi-python-client (invoked via `uvx`) | ||
|
|
||
| Install uv | ||
| ```bash | ||
| # macOS/Linux | ||
| curl -LsSf https://astral.sh/uv/install.sh | sh | ||
| ``` | ||
|
|
||
| Set up the environment (from this directory) | ||
| ```bash | ||
| uv sync --dev | ||
| ``` | ||
|
|
||
| Generate client code (from CLI-generated spec) | ||
| ```bash | ||
| # From repository root OR from this directory | ||
| uv run python packages/sdk/python/scripts/generate.py --source cli | ||
| ``` | ||
|
|
||
| Alternatively, fetch spec from a running server | ||
| ```bash | ||
| uv run python packages/sdk/python/scripts/generate.py --source server --server-url http://localhost:4096/doc | ||
| ``` | ||
|
|
||
| This will: | ||
| 1) Produce an OpenAPI spec from the local CLI or a running server | ||
| 2) Run openapi-python-client (via `uvx`) to generate client code | ||
| 3) Copy the generated Python package into src/opencode_ai | ||
|
|
||
| Usage (after generation) | ||
| ```python | ||
| from opencode_ai import OpenCodeClient | ||
|
|
||
| client = OpenCodeClient(base_url="http://localhost:4096") | ||
| print(client.get_config()) | ||
|
|
||
| # See examples/basic_usage.py for more details | ||
|
|
||
| # Streaming events (sync) | ||
| for event in client.subscribe_events(): | ||
| print(event) | ||
| break | ||
|
|
||
| # Error handling and retries | ||
| # Set retries>0 to enable exponential backoff for transient errors like 429/5xx | ||
| client = OpenCodeClient(retries=2, backoff_factor=0.1) | ||
|
|
||
| # Async usage example | ||
| # uv run --project packages/sdk/python python - <<'PY' | ||
| # import asyncio | ||
| # from opencode_ai import OpenCodeClient | ||
| # async def main(): | ||
| # client = OpenCodeClient() | ||
| # async for event in client.subscribe_events_async(): | ||
| # print(event) | ||
| # break | ||
| # asyncio.run(main()) | ||
| # PY | ||
| ``` | ||
|
|
||
| Notes | ||
| - We intentionally do not use Stainless for the Python SDK. | ||
| - The generator targets OpenAPI 3.1 emitted by the opencode server at /doc. | ||
| - See scripts/generate.py for details and customization points. |
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,19 @@ | ||
| # Generation workflow | ||
|
|
||
| The SDK is generated from the Opencode server's OpenAPI 3.1 spec. | ||
|
|
||
| Two source modes are supported: | ||
| - CLI (default): runs `bun dev generate` to emit the OpenAPI JSON | ||
| - Server: fetches `http://localhost:4096/doc` from a running server | ||
|
|
||
| Generator command | ||
| ```bash | ||
| # From repo root | ||
| uv run --project packages/sdk/python python packages/sdk/python/scripts/generate.py --source cli | ||
| # Or | ||
| uv run --project packages/sdk/python python packages/sdk/python/scripts/generate.py --source server --server-url http://localhost:4096/doc | ||
| ``` | ||
|
|
||
| Post-generation | ||
| - The generator injects `extras.py` (OpenCodeClient) and patches `__init__.py` to export it | ||
| - Code is formatted with `ruff` (imports) and `black` |
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,11 @@ | ||
| # Opencode Python SDK | ||
|
|
||
| The official Python client for the Opencode API, generated from the OpenAPI spec and extended with ergonomic helpers. | ||
|
|
||
| Highlights | ||
| - Provider-agnostic client generated from OpenAPI 3.1 | ||
| - Thin convenience wrapper (OpenCodeClient) for common tasks | ||
| - Sync and async SSE streaming for live event feeds | ||
| - First-class uv support for development | ||
|
|
||
| If you're new, start with Quickstart or Installation in the navigation. |
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 @@ | ||
| # Installation | ||
|
|
||
| Requirements | ||
| - Python 3.8+ | ||
| - uv (recommended) -> https://docs.astral.sh/uv/ | ||
|
|
||
| Install uv | ||
| ```bash | ||
| curl -LsSf https://astral.sh/uv/install.sh | sh | ||
| ``` | ||
|
|
||
| Project setup | ||
| ```bash | ||
| # From repo root or this directory | ||
| uv sync --dev --project packages/sdk/python | ||
| ``` | ||
|
|
||
| Using pip (alternative) | ||
| ```bash | ||
| pip install opencode-ai | ||
| ``` | ||
|
|
||
| Preview docs locally | ||
| ```bash | ||
| # From repo root | ||
| uv run --project packages/sdk/python mkdocs serve -f packages/sdk/python/mkdocs.yml | ||
| ``` |
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,24 @@ | ||
| # Publishing (maintainers) | ||
|
|
||
| Automated publishing runs on GitHub Releases. | ||
|
|
||
| Workflow | ||
| - Create a new Release (the tag value becomes the package version) | ||
| - The `publish-python-sdk` workflow will: | ||
| - Generate the SDK from OpenAPI (CLI path) | ||
| - Set the version in `pyproject.toml` and generator config | ||
| - Build wheel/sdist and upload to PyPI | ||
|
|
||
| Prerequisites | ||
| - Repository secret: `PYPI_API_TOKEN` | ||
|
|
||
| Manual publish | ||
| ```bash | ||
| # TestPyPI | ||
| REPOSITORY=testpypi PYPI_TOKEN=$TEST_PYPI_API_TOKEN \ | ||
| uv run --project packages/sdk/python python packages/sdk/python/scripts/publish.py | ||
|
|
||
| # PyPI | ||
| REPOSITORY=pypi PYPI_TOKEN=$PYPI_API_TOKEN \ | ||
| uv run --project packages/sdk/python python packages/sdk/python/scripts/publish.py | ||
| ``` |
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,22 @@ | ||
| # Quickstart | ||
|
|
||
| Create a client and make your first calls. | ||
|
|
||
| ```python | ||
| from opencode_ai import OpenCodeClient | ||
|
|
||
| client = OpenCodeClient(base_url="http://localhost:4096") | ||
|
|
||
| # List projects | ||
| for p in client.list_projects() or []: | ||
| print(p.id, p.directory) | ||
|
|
||
| # Get path info | ||
| path = client.get_path() | ||
| print(path.directory) | ||
|
|
||
| # Stream events (sync) | ||
| for event in client.subscribe_events(): | ||
| print(event) | ||
| break | ||
| ``` |
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,15 @@ | ||
| # Testing | ||
|
|
||
| Run unit, mock, and integration tests. | ||
|
|
||
| ```bash | ||
| # Sync dev dependencies | ||
| uv sync --dev --project packages/sdk/python | ||
|
|
||
| # Run tests | ||
| uv run --project packages/sdk/python pytest -q | ||
| ``` | ||
|
|
||
| Notes | ||
| - Integration test starts a headless opencode server via Bun in a subprocess | ||
| - SSE behavior is validated using real streaming from the server |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.