OPENCODE-PTY(1)

NAME

opencode-ptyOpenCode plugin for interactive PTY management - run background processes, send input, read output with regex filtering

SYNOPSIS

INFO

407 stars
24 forks
0 views

DESCRIPTION

OpenCode plugin for interactive PTY management - run background processes, send input, read output with regex filtering

README

opencode-pty

A plugin for OpenCode that provides interactive PTY (pseudo-terminal) management, enabling the AI agent to run background processes, send interactive input, and read output on demand.

Why?

OpenCode's built-in bash tool runs commands synchronously—the agent waits for completion. This works for quick commands, but not for:

  • Dev servers (npm run dev, cargo watch)
  • Watch modes (npm test -- --watch)
  • Long-running processes (database servers, tunnels)
  • Interactive programs (REPLs, prompts)

This plugin gives the agent full control over multiple terminal sessions, like tabs in a terminal app.

Features

  • Background Execution: Spawn processes that run independently
  • Multiple Sessions: Manage multiple PTYs simultaneously
  • Interactive Input: Send keystrokes, Ctrl+C, arrow keys, etc.
  • Output Buffer: Read output anytime with pagination (offset/limit)
  • Pattern Filtering: Search output using regex (like grep)
  • Exit Notifications: Get notified when processes finish (eliminates polling)
  • Permission Support: Respects OpenCode's bash permission settings
  • Session Lifecycle: Sessions persist until explicitly killed
  • Auto-cleanup: PTYs are cleaned up when OpenCode sessions end
  • Web UI: Modern React-based interface for session management
  • Real-time Streaming: WebSocket-based live output updates

Setup

Add the plugin to your OpenCode config:

{
  "$schema": "https://opencode.ai/config.json",
  "plugin": ["opencode-pty"]
}

That's it. OpenCode will automatically install the plugin on next run.

Updating

OpenCode automatically checks for and installs plugin updates on startup. You don't need to do anything manually!

If you ever need to force a clean reinstall, you can clear the cache:

rm -rf ~/.cache/opencode/node_modules/opencode-pty
opencode

Tools Provided

ToolDescription
pty_spawnCreate a new PTY session (command, args, workdir, env, title, notifyOnExit, timeoutSeconds)
pty_writeSend input to a PTY (text, escape sequences like \x03 for Ctrl+C)
pty_readRead output buffer with pagination and optional regex filtering
pty_listList all PTY sessions with status, PID, line count
pty_killTerminate a PTY, optionally cleanup the buffer

Slash Commands

This plugin provides slash commands that can be used in OpenCode chat:

CommandDescription
/pty-open-background-spyOpen the PTY web server interface in the browser
/pty-show-server-urlShow the URL of the running PTY web server instance

Web UI

This plugin includes a modern React-based web interface for monitoring and interacting with PTY sessions.

opencode-pty Web UI Demo

If you instruct the coding agent to run something in background, you have to name it "session", i.e. "run xy as a background SESSION". If you name it "task" or "process" or anything else, the agent will sometimes run it as background subprocess using &.

Starting the Web UI

  1. Run opencode with the plugin.
  2. Run slash command /pty-open-background-spy.

This will start the background sessions observer cockpit server and launch the browser with web UI.

Features

  • Session List: View all active PTY sessions with status indicators
  • Real-time Output: Live streaming of process output via WebSocket
  • Interactive Input: Send commands and input to running processes
  • Session Management: Kill sessions directly from the UI
  • Connection Status: Visual indicator of WebSocket connection status

REST API

The web server provides a REST API for session management:

MethodEndpointDescription
GET/api/sessionsList all PTY sessions
POST/api/sessionsCreate a new PTY session
GET/api/sessions/:idGet session details
POST/api/sessions/:id/inputSend input to a session
DELETE/api/sessions/:idKill a session (without cleanup)
DELETE/api/sessions/:id/cleanupKill and cleanup a session
GET/api/sessions/:id/buffer/plainGet session output buffer (returns { plain: string, byteLength: number })
GET/api/sessions/:id/buffer/rawGet session output buffer (raw data)
DELETE/api/sessionsClear all sessions
GET/healthServer health check with metrics

Session Creation

curl -X POST http://localhost:[PORT]/api/sessions \
  -H "Content-Type: application/json" \
  -d '{
    "command": "bash",
    "args": ["-c", "echo hello && sleep 10"],
    "description": "Test session",
    "timeoutSeconds": 5
  }'

Replace [PORT] with the actual port number shown in the server console output.

WebSocket Streaming

Connect to /ws for real-time updates:

const ws = new WebSocket('ws://localhost:[PORT]/ws')

ws.onmessage = (event) => { const data = JSON.parse(event.data) if (data.type === 'raw_data') { console.log('New output:', data.rawData) } else if (data.type === 'session_list') { console.log('Session list:', data.sessions) } }

Replace [PORT] with the actual port number shown in the browser when running the slash command output.

Development

Future implementation will include:

App

  • A startup script that runs the server (in the same process).
  • The startup script will run bun vite with an environment variable set to the server URL
  • The client will use this environment variable for WebSocket and HTTP requests

This will ease the development on the client.

Usage Examples

Start a dev server

pty_spawn: command="npm", args=["run", "dev"], title="Dev Server"
→ Returns: pty_a1b2c3d4

Start a timed session

pty_spawn: command="npm", args=["run", "dev"], title="Dev Server", timeoutSeconds=600
→ Returns: pty_a1b2c3d4

Check server output

pty_read: id="pty_a1b2c3d4", limit=50
→ Shows last 50 lines of output

Filter for errors

pty_read: id="pty_a1b2c3d4", pattern="error|ERROR", ignoreCase=true
→ Shows only lines matching the pattern

Send Ctrl+C to stop

pty_write: id="pty_a1b2c3d4", data="\x03"
→ Sends interrupt signal

Kill and cleanup

pty_kill: id="pty_a1b2c3d4", cleanup=true
→ Terminates process and frees buffer

Run with exit notification

pty_spawn: command="npm", args=["run", "build"], title="Build", notifyOnExit=true
→ Returns: pty_a1b2c3d4

The AI agent will receive a notification when the build completes:

<pty_exited>
ID: pty_a1b2c3d4
Title: Build
Exit Code: 0
Output Lines: 42
Last Line: Build completed successfully.
</pty_exited>

Use pty_read to check the full output.

This eliminates the need for polling—perfect for long-running processes like builds, tests, or deployment scripts. If the process fails (non-zero exit code), the notification will suggest using pty_read with the pattern parameter to search for errors.

Configuration

Environment Variables

VariableDefaultDescription
PTY_MAX_BUFFER_LINES50000Maximum lines to keep in output buffer per session
PTY_WEB_HOSTNAME::1Hostname for the web server to bind to (IPv6 loopback by default)
PTY_WEB_PORT0 (random)Port for the web server (0 = random port)

Permissions

This plugin respects OpenCode's permission settings for the bash tool. Commands spawned via pty_spawn are checked against your permission.bash configuration.

{
  "$schema": "https://opencode.ai/config.json",
  "permission": {
    "bash": {
      "npm *": "allow",
      "git push": "deny",
      "terraform *": "deny"
    }
  }
}

[!IMPORTANT] Limitations compared to built-in bash tool:

  • "ask" permissions are treated as "deny": Since plugins cannot trigger OpenCode's permission prompt UI, commands matching an "ask" pattern will be denied. A toast notification will inform you when this happens. Configure explicit "allow" or "deny" for commands you want to use with PTY.
  • "external_directory" with "ask" is treated as "allow": When the working directory is outside the project and permission.external_directory is set to "ask", this plugin allows it (with a log message). Set to "deny" explicitly if you want to block external directories.

Example: Allow specific commands for PTY

{
  "$schema": "https://opencode.ai/config.json",
  "permission": {
    "bash": {
      "npm run dev": "allow",
      "npm run build": "allow",
      "npm test *": "allow",
      "cargo *": "allow",
      "python *": "allow"
    }
  }
}

How It Works

  1. Spawn: Creates a PTY using bun-pty, runs command in background
  2. Buffer: Output is captured into a rolling line buffer (ring buffer)
  3. Read: Agent can read buffer anytime with offset/limit pagination
  4. Filter: Optional regex pattern filters lines before pagination
  5. Write: Agent can send any input including escape sequences
  6. Lifecycle: Sessions track status (running/exited/killed), persist until cleanup
  7. Notify: When notifyOnExit is true, sends a message to the session when the process exits
  8. Web UI: React frontend connects via WebSocket for real-time updates

Session Lifecycle

spawn → running → [exited | killed]
                      ↓
              (stays in list until cleanup=true)

Sessions remain in the list after exit so the agent can:

  • Read final output
  • Check exit code
  • Compare logs between runs

Use pty_kill with cleanup=true to remove completely.

Local Development

git clone https://github.com/shekohex/opencode-pty.git
cd opencode-pty
bun ci          # install packages from bun.lock
bun lint        # Runs Biome linting checks
bun format      # Runs Biome formatting checks
bun typecheck   # Runs TypeScript type checking
bun build:dev   # Build the React app for development
bun unittest    # Runs the unit tests
bun test:e2e    # Runs the e2e tests

To load a local checkout in OpenCode:

{
  "$schema": "https://opencode.ai/config.json",
  "plugin": ["file:///absolute/path/to/opencode-pty/index.ts"]
}

Diagrams

Sequence

Use Case 1 – Opening the PTY Monitor Web UI

sequenceDiagram
    participant User as Human User
    participant Chat as OpenCode Chat
    participant Core as OpenCode Core
    participant Plugin as PTY Plugin
    participant Manager as PTY Manager (in Plugin)
    participant WS as WebSocket Server (in Plugin)
    participant Browser as Web Browser
Note over Plugin,WS: Plugin starts/owns the WS server and Manager

User-&gt;&gt;Chat: Types /pty-open-background-spy
Chat-&gt;&gt;Core: Slash command received
Core-&gt;&gt;Plugin: Dispatches to registered command handler
Plugin-&gt;&gt;Browser: open(server.url.origin)
activate Browser
Browser-&gt;&gt;WS: Connects → ws://.../ws
WS-&gt;&gt;Manager: Queries for current sessions (manager.list())
Manager--&gt;&gt;WS: Returns session data
WS--&gt;&gt;Browser: Sends session_list + subscribes to updates
Browser--&gt;&gt;User: PTY monitor UI appears (sessions + terminals)

Use Case 2 – Starting a Long-Running Background Process

sequenceDiagram
    participant User
    participant Chat as OpenCode Chat
    participant Agent as AI Agent
    participant Plugin as PTY Plugin
    participant Manager as PTY Manager
    participant PTY as bun-pty Process
    participant WS as WebSocket Server
    participant UI as PTY Web UI (optional)
User-&gt;&gt;Chat: &quot;start vite dev server in background&quot;
Chat-&gt;&gt;Agent: User message
Agent-&gt;&gt;Plugin: Calls pty_spawn(command=&quot;vite&quot;, args=[&quot;dev&quot;], ...)
Plugin-&gt;&gt;Manager: spawn(options)
Manager-&gt;&gt;PTY: Launches real process
activate PTY
PTY--&gt;&gt;Manager: stdout/stderr chunks
Manager-&gt;&gt;Manager: Appends to RingBuffer
Manager-&gt;&gt;WS: Publishes raw_data + session_update
alt UI already open
    WS--&gt;&gt;UI: Real-time terminal output
    UI--&gt;&gt;User: Live xterm.js view
end
Plugin--&gt;&gt;Agent: Returns session info
Agent--&gt;&gt;Chat: &quot;Dev server started (ID: pty_abc123)&quot;
Chat--&gt;&gt;User: Confirmation message

Use Case 3 – Sending Interactive Input to a Running Session

sequenceDiagram
    participant User
    participant UI as PTY Web UI
    participant WS as WebSocket Server
    participant Manager as PTY Manager
    participant PTY as bun-pty Process
%% Variant A: Human typing in browser (most common)
User-&gt;&gt;UI: Types &quot;rs&lt;Enter&gt;&quot; or pastes text
UI-&gt;&gt;WS: Sends {type:&quot;input&quot;, sessionId, data:&quot;rs\n&quot;}
WS-&gt;&gt;Manager: write(sessionId, data)
Manager-&gt;&gt;PTY: process.write(data)
PTY--&gt;&gt;Manager: New output (restart message, etc.)
Manager-&gt;&gt;WS: Publishes raw_data
WS--&gt;&gt;UI: Updates xterm.js live

%% Variant B: AI sending input
Note over User,UI: Alternative path – AI controlled
Agent-&gt;&gt;Plugin: pty_write(id, &quot;\x03&quot;)  // e.g. Ctrl+C
Plugin-&gt;&gt;Manager: write(id, data)
Manager-&gt;&gt;PTY: process.write(&quot;\x03&quot;)

Use Case 4 – Reading Output / Logs On Demand

sequenceDiagram
    participant User
    participant Chat
    participant Agent as AI Agent
    participant Plugin as PTY Plugin
    participant Manager as PTY Manager
    participant Buffer as RingBuffer
User-&gt;&gt;Chat: &quot;show me the last 200 lines of the dev server&quot;
Chat-&gt;&gt;Agent: User question
Agent-&gt;&gt;Plugin: pty_read(id, offset?, limit=200, pattern?)
Plugin-&gt;&gt;Manager: read / search request
Manager-&gt;&gt;Buffer: read(offset, limit) or search(pattern)
Buffer--&gt;&gt;Manager: Matching / paginated lines
Manager--&gt;&gt;Plugin: Raw or formatted lines
Plugin--&gt;&gt;Agent: Text response
Agent--&gt;&gt;Chat: &quot;Here are the logs:\n\n1 | [vite] ... \n...&quot;
Chat--&gt;&gt;User: Logs displayed in chat

Use Case 5A – Killing / Cleaning Up a Session via Web UI

sequenceDiagram
    participant User
    participant UI as PTY Web UI
    participant HTTP as HTTP Server
    participant Manager as PTY Manager
    participant PTY as bun-pty Process
    participant WS as WebSocket Server
Note over PTY: Assuming PTY is active (running process)&lt;br&gt;notifyOnExit = false (no chat notification,&lt;br&gt;but WS/UI always gets status updates)

activate PTY

User-&gt;&gt;UI: Clicks &quot;Kill&quot; / &quot;×&quot; on session
UI-&gt;&gt;HTTP: DELETE /api/sessions/:id  (or /cleanup)
HTTP-&gt;&gt;Manager: kill(id, cleanup?)
Manager-&gt;&gt;PTY: Sends SIGTERM (if running)
PTY--&gt;&gt;Manager: onExit event (code, signal)
deactivate PTY
Manager-&gt;&gt;WS: Publishes session_update (status: killed/exited)
WS--&gt;&gt;UI: UI updates → shows &quot;exited&quot; or removes entry

Use Case 5B – Killing / Cleaning Up a Session via Agent

sequenceDiagram
    participant User
    participant Chat as OpenCode Chat
    participant Agent as AI Agent
    participant Plugin as PTY Plugin
    participant Manager as PTY Manager
    participant PTY as bun-pty Process
    participant WS as WebSocket Server
    participant UI as PTY Web UI (optional)
Note over PTY: Assuming PTY is active (running process)&lt;br&gt;notifyOnExit = false (no chat notification,&lt;br&gt;but WS/UI always gets status updates)

activate PTY

User-&gt;&gt;Chat: &quot;kill the dev server&quot;
Chat-&gt;&gt;Agent: User request
Agent-&gt;&gt;Plugin: pty_kill(id, cleanup=true)
Plugin-&gt;&gt;Manager: kill(id, true)
Manager-&gt;&gt;PTY: SIGTERM + remove from list (if cleanup)
PTY--&gt;&gt;Manager: onExit event (code, signal)
deactivate PTY
Manager-&gt;&gt;WS: Broadcast session_update (status: killed/exited)
alt UI open
    WS--&gt;&gt;UI: UI updates → shows &quot;exited&quot; or removes entry
end
Plugin--&gt;&gt;Agent: Success response
Agent--&gt;&gt;Chat: &quot;Session killed&quot;
Chat--&gt;&gt;User: Confirmation in chat

Use Case 6 – Automatic Exit Notification

sequenceDiagram
    participant PTY as bun-pty Process
    participant Manager as PTY Manager
    participant Plugin as PTY Plugin
    participant Chat as OpenCode Chat
    participant Agent as AI Agent
    participant User
%%{init: {&#39;sequence&#39;: {&#39;messageAlign&#39;: &#39;left&#39;}}}%%

activate PTY
Note over PTY: Long-running process (dev server, tests, etc.)
PTY--&gt;&gt;Manager: Process exits → exitCode
deactivate PTY
alt notifyOnExit was true when spawned
    Manager-&gt;&gt;Plugin: Triggers exit notification
    Plugin-&gt;&gt;Chat: Sends formatted message via SDK&lt;br&gt;&lt;pty_exited&gt;&lt;br&gt;ID: pty_abc123&lt;br&gt;Exit: 0&lt;br&gt;Lines: 342&lt;br&gt;Last: Server running at http://localhost:5173&lt;br&gt;&lt;/pty_exited&gt;
    Chat--&gt;&gt;User: Notification appears in chat
    Chat-&gt;&gt;Agent: Triggers agent with exit message
end
Manager-&gt;&gt;WS: Publishes final session_update (status: exited)
alt UI open
    WS--&gt;&gt;UI: UI shows red &quot;exited&quot; badge / stops live output
end

License

MIT

Contributing

Contributions are welcome! Please open an issue or submit a PR.

Credits

  • OpenCode - The AI coding assistant this plugin extends
  • bun-pty - Cross-platform PTY for Bun

SEE ALSO

clihub5/8/2026OPENCODE-PTY(1)