Skip to content

Add interactive terminal support (pty.attach, persistent_pty.attach) - #11

Merged
shrijayan merged 2 commits into
mainfrom
feat/pty-attach
Oct 22, 2026
Merged

shrijayan merged 2 commits into
mainfrom
feat/pty-attach

Conversation

@shrijayan

Copy link
Copy Markdown
Owner

Implements the two WebSocket endpoints the generator omits: pty.connect and persistentPty.connect (OpenCode v2.1.2).

  • client.pty.attach(pty_id, cursor=...) and client.experimental.persistent_pty.attach(pty_id, cursor=, role=, attachment_id=, takeover=, on_event=) return PtyConnection / AsyncPtyConnection with send, resize, recv, iteration, close and context-manager support. Async is asyncio-only.
  • Optional extra: pip install "opencode-python-sdk[pty]" (websockets>=14,<17), imported lazily; missing → PtyConnectionError with install hint.
  • Codegen: codegen/render.py gains a MIXINS table so generated resources inherit the hand-written opencode/_pty.py mixins.
  • examples/terminal.py bridges a local TTY to a remote PTY; README section.
  • Tests: fake in-process WebSocket server (both endpoints, sync+async, 4404 close, 403 handshake, missing extra), live echo hi test. Lowest-deps CI job now installs .[pty].

Protocol implemented

  • Ticket: POST …/connect-token with x-opencode-ticket: 1 → single-use 60 s ticket passed as ?ticket=. Basic auth is also sent on the handshake.
  • pty.connect: query location[directory], cursor (-1 skips replay), ticket. Server sends replay (text, 64 KiB chunks), then a binary control frame 0x00+{"cursor":n}, then live text output. Input: UTF-8 text/binary. Resize via PUT /api/pty/:id.
  • persistentPty.connect: query ticket, cursor (byte offset), role (controller|observer), attachment_id, takeover, input_protocol=1. Text frames are JSON events (attached first, replay_complete, resized, controller_changed, …); binary frames are output. Input frames: u8 type (0 control/resize, 1 input) + big-endian u16 cols, u16 rows + payload. cursor tracks endOffset + output bytes for resume.
  • Close codes: 1000 terminal ended (iteration stops), 4404 not found/exited/unavailable (raises PtyConnectionError).

Verified live against opencode 2.1.2 for both endpoints. Do not merge without review (merges release to PyPI).

Implements the two WebSocket endpoints the generator omits (pty.connect and
persistentPty.connect) as hand-written mixins on the generated resources,
with sync and asyncio connections built on the optional websockets extra.
@shrijayan
shrijayan merged commit 158b2ed into main Oct 22, 2026
16 checks passed
@shrijayan
shrijayan deleted the feat/pty-attach branch October 22, 2026 10:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant