Terminals¶
Connections returned by client.pty.attach() and client.experimental.persistent_pty.attach(). See the
terminals guide.
PtyConnection
¶
PtyConnection(
ws: ClientConnection,
protocol: _Protocol,
resize: Callable[[int, int], object],
on_event: Callable[[dict[str, Any]], object]
| None = None,
)
A synchronous connection to a remote terminal.
Iterate over it, or call :meth:recv, for output bytes; it stops when the terminal ends. Control frames
are handled internally: :attr:cursor holds the latest output position the server reported, and
on_event (persistent terminals) receives every JSON event.
Example::
with client.pty.attach(pty.id) as term:
term.send("echo hi\n")
for chunk in term:
print(chunk.decode(errors="replace"), end="")
cursor
property
¶
cursor: int | None
Output position to pass as cursor when reattaching, once the server has reported one.
attached
property
¶
The attached event of a persistent terminal (attachment id, role, info, replay offsets).
close_code
property
¶
close_code: int | None
The WebSocket close code once the connection has closed.
recv
¶
Returns the next output chunk, or None once the terminal has ended.
Raises:
| Type | Description |
|---|---|
TimeoutError
|
No output within |
PtyConnectionError
|
The server closed the connection abnormally ( |
AsyncPtyConnection
¶
AsyncPtyConnection(
ws: ClientConnection,
protocol: _Protocol,
resize: Callable[[int, int], Awaitable[object]],
on_event: Callable[[dict[str, Any]], object]
| None = None,
)
An asyncio connection to a remote terminal. Same interface as :class:PtyConnection, with awaitables.
Example::
async with await client.pty.attach(pty.id) as term:
await term.send("echo hi\n")
async for chunk in term:
print(chunk.decode(errors="replace"), end="")
cursor
property
¶
cursor: int | None
Output position to pass as cursor when reattaching, once the server has reported one.
attached
property
¶
The attached event of a persistent terminal (attachment id, role, info, replay offsets).
close_code
property
¶
close_code: int | None
The WebSocket close code once the connection has closed.
recv
async
¶
recv() -> bytes | None
Returns the next output chunk, or None once the terminal has ended.
Raises:
| Type | Description |
|---|---|
PtyConnectionError
|
The server closed the connection abnormally ( |
wait_attached
async
¶
Reads frames until the attached event of a persistent terminal; attach already does this.
PtyConnectionError
¶
PtyConnectionError(
message: str,
*,
status: int | None = None,
code: int | None = None,
reason: str = "",
)
Bases: OpencodeError
The terminal WebSocket could not be opened or closed abnormally.
Attributes:
| Name | Type | Description |
|---|---|---|
status |
HTTP status of a refused handshake ( |
|
code |
WebSocket close code ( |
|
reason |
WebSocket close reason. |