vunknown for OpenCode server 2.1.2
Drive OpenCode from Python, fully typed.¶
All 142 operations of the OpenCode server API, sync and async, with pydantic models for every request, response and event. Generated from the same contract as the official TypeScript client.
from opencode import OpencodeServer
with OpencodeServer(cwd="/path/to/repo") as server: # (1)!
client = server.client()
session = client.session.create(title="Review")
client.session.prompt(session.id, text="Summarize the open TODOs")
client.session.wait(session.id)
for message in client.message.list(session.id): # (2)!
print(message.type)
- Starts
opencode serveon a free port with a random password, and stops it on exit. - Iterating a page fetches the following pages as needed.
import asyncio
from opencode import AsyncOpencode
async def main() -> None:
async with AsyncOpencode("http://127.0.0.1:4096") as client:
session = await client.session.create(title="Review")
await client.session.prompt(session.id, text="Hello")
async for message in client.message.list(session.id):
print(message.type)
asyncio.run(main()) # (1)!
- Works with asyncio and trio.
01
Complete¶
Every operation of the OpenCode 2.1 API, on Opencode and AsyncOpencode, with 96 typed event classes.
02
Generated from the source¶
The contract is exported from OpenCode's own Effect HttpApi, so names and shapes match the TypeScript client.
03
Safe retries¶
Only retries when a request cannot have changed server state. A prompt is never sent twice.
04
Forward compatible¶
New fields, enum values, union variants and events from newer servers are kept, not rejected.
06
Batteries included¶
Launch opencode serve, paginate lists, and stream server-sent events with one call each.
Where to next¶
- Connect to a server you already run, or start one from Python.
- Read the sessions and prompts guide for the core workflow.
- Browse the API reference, or search it with ++slash++.
- Using an LLM? Point it at
llms.txtorllms-full.txt.
Unofficial
This package is community-maintained and not affiliated with or endorsed by the OpenCode team. If the OpenCode maintainers want to take it over, the repository and the PyPI name will be transferred.