Server launcher¶
OpencodeServer
¶
OpencodeServer(
*,
cwd: str | PathLike[str] | None = None,
config: Mapping[str, Any] | None = None,
isolated: bool = False,
env: Mapping[str, str] | None = None,
hostname: str = "127.0.0.1",
port: int = 0,
password: str | None = None,
binary: str | PathLike[str] = "opencode",
startup_timeout: float = 60.0,
)
Runs opencode serve as a child process for the lifetime of a with block.
The server listens on a free local port and requires a random password, which is passed to clients created
with :meth:client. It runs in --stdio mode, so it also exits if this process dies.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cwd
|
str | PathLike[str] | None
|
Directory the server runs in, which is its default project location. |
None
|
config
|
Mapping[str, Any] | None
|
OpenCode configuration as a mapping, passed through |
None
|
isolated
|
bool
|
Ignore project and Claude Code configuration found on disk, for reproducible embedded use. |
False
|
env
|
Mapping[str, str] | None
|
Extra environment variables for the server. |
None
|
hostname
|
str
|
Interface to bind. Keep |
'127.0.0.1'
|
port
|
int
|
Port to bind. |
0
|
password
|
str | None
|
Server password. A random one is generated by default. |
None
|
binary
|
str | PathLike[str]
|
The |
'opencode'
|
startup_timeout
|
float
|
Seconds to wait for the server to report its address. |
60.0
|
Example::
with OpencodeServer(cwd="/path/to/repo", isolated=True) as server:
client = server.client()
print(client.server.info().version)
password
instance-attribute
¶
password = (
password
if password is not None
else secrets.token_urlsafe(32)
)
process
property
¶
The opencode serve child process, or None before it starts.
client
¶
client(
*,
directory: str | PathLike[str] | None = None,
timeout: Timeout | None = None,
) -> Opencode
Returns a sync client connected to this server.
async_client
¶
async_client(
*,
directory: str | PathLike[str] | None = None,
timeout: Timeout | None = None,
) -> AsyncOpencode
Returns an async client connected to this server.
AsyncOpencodeServer
¶
AsyncOpencodeServer(**kwargs: Any)
Async wrapper around :class:OpencodeServer; takes the same arguments.
Example::
async with AsyncOpencodeServer(isolated=True) as server:
async with server.client() as client:
info = await client.server.info()
client
¶
client(
*,
directory: str | PathLike[str] | None = None,
timeout: Timeout | None = None,
) -> AsyncOpencode
Returns an async client connected to this server.