Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ node_modules

# output
out
dist
# dist is committed so a `github:` install has the built entrypoint
#dist
*.tgz

# code coverage
Expand Down
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
legacy-peer-deps=true
64 changes: 56 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ Add the plugin to your [OpenCode config](https://opencode.ai/docs/config/):

### OpenCode V2

OpenCode V2 uses the new plugin API. You can load `opencode-pty/v2` and optionally configure options (such as a fixed web UI port):
OpenCode V2 uses the new plugin API. Load the package by its bare npm ref — the `.` entrypoint is a dual-mode export that serves both runtimes — and optionally configure options (such as a fixed web UI port):

```json
{
"$schema": "https://opencode.ai/config.json",
"plugin": [
"plugins": [
{
"package": "opencode-pty/v2",
"package": "opencode-pty",
"options": {
"port": 4200,
"hostname": "127.0.0.1",
Expand All @@ -61,6 +61,34 @@ OpenCode V2 uses the new plugin API. You can load `opencode-pty/v2` and optional
}
```

Notes for V2:

- V2 reads the `plugins` array (object form). The V1 `plugin` field is silently ignored — a V1-shaped array produces zero plugin loads.
- The previous `opencode-pty/v2` subpath ref does not install: the V2 loader parses subpath refs as npm install specs ([#67](https://github.com/shekohex/opencode-pty/issues/67)). The bare ref works via the dual-mode default export; the `./v2` subpath remains valid for direct-import consumers.
- A plugin must export a default definition with an `id` and a `setup` (or `effect`) function. The `.` entrypoint now exports exactly that alongside the V1 `server` entrypoint.
- If the npm ref fails to install through OpenCode's bundled npm with `<git dep preparation failed>`, use a local clone (below). That failure happens for any package with dependencies, not just this one.

#### Install from a local clone

```bash
git clone https://github.com/shekohex/opencode-pty.git
cd opencode-pty
npm install --legacy-peer-deps # plain npm fails on react@18 vs @opentui/keymap peer
```

Then point a directory entry at the clone:

```json
{
"$schema": "https://opencode.ai/config.json",
"plugins": ["/absolute/path/to/opencode-pty"]
}
```

A directory entry resolves a root `server` file, else `index`, with a `.ts`, `.tsx`, or `.js`
extension; `package.json` `main` and `exports` are not followed for a path entry. This
repository's root `index.ts` is that entrypoint, so the clone loads without a build step.

| Option | Type | Default | Description |
| --- | --- | --- | --- |
| `port` | `number` | `0` (ephemeral) | Fixed port for the PTY Web UI observer server |
Expand Down Expand Up @@ -297,7 +325,27 @@ This eliminates the need for polling—perfect for long-running processes like b

### Permissions

This plugin respects OpenCode's [permission settings](https://opencode.ai/docs/permissions/) for the `bash` tool. Commands spawned via `pty_spawn` are checked against your `permission.bash` configuration.
This plugin respects OpenCode's [permission settings](https://opencode.ai/docs/permissions/). Commands spawned via `pty_spawn`, and command lines sent with `pty_write`, are checked with the same permission surface the host's own shell tool uses.

On **V2** that surface is `ctx.permission.assert`: the plugin asks the host to evaluate the session's rules and only spawns once the check resolves. A `deny` rule blocks the spawn, and an `ask` rule raises OpenCode's normal permission prompt — the answer is honoured, including saved approvals. V2 names the shell action `shell`:

```json
{
"$schema": "https://opencode.ai/config.json",
"permissions": [
{ "action": "shell", "resource": "npm *", "effect": "allow" },
{ "action": "shell", "resource": "git push", "effect": "deny" },
{ "action": "shell", "resource": "terraform *", "effect": "deny" }
]
}
```

Two V2 edge cases are deliberate:

- A check that cannot be attributed to a session is **denied** rather than allowed. Every PTY tool call carries its session, so this only affects calls made outside a tool invocation, and denying keeps a configured `deny` rule from being bypassed.
- A host that exposes no `ctx.permission` domain gets a startup warning and no checks, matching the pre-2.0 fallback below. This plugin cannot read `permissions` itself on V2 — the plugin context has no config reader — so the host is the only authority.

On **V1** the plugin reads `permission.bash` from the host config:

```json
{
Expand All @@ -313,12 +361,12 @@ This plugin respects OpenCode's [permission settings](https://opencode.ai/docs/p
```

> [!IMPORTANT]
> **Limitations compared to built-in bash tool:**
> **V1 limitations compared to the 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.
> - **"ask" permissions are treated as "deny"**: Since the V1 plugin API 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. (V2 does not have this limitation — `ctx.permission.assert` prompts.)
> - **"external_directory" with "ask" is treated as "allow"**: When the working directory is outside the project and `permission.external_directory` is set to "ask", the V1 path allows it (with a log message). Set to "deny" explicitly if you want to block external directories.

#### Example: Allow specific commands for PTY
#### Example: Allow specific commands for PTY (V1)

```json
{
Expand Down
10 changes: 10 additions & 0 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export { PTYPlugin } from './src/plugin.ts';
export { PTYPlugin as server } from './src/plugin.ts';
export { Plugin } from './src/v2/index.ts';
declare const _default: {
id: string;
server: (context: import("./src/plugin/types.ts").PluginContext) => Promise<import("./src/plugin/types.ts").PluginResult>;
setup: (context: import("./src/v2/types.ts").PluginContextV2) => Promise<void> | void;
};
export default _default;
//# sourceMappingURL=index.d.ts.map
16 changes: 16 additions & 0 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions dist/src/adapters/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { HostAdapter } from './types.ts';
export * from './types.ts';
export * from './v1/index.ts';
export * from './v2/index.ts';
/**
* Installs a host adapter by connecting its notifier and permission authorizer
* to the core PTY manager and permission dispatcher.
*/
export declare function installHostAdapter(adapter: HostAdapter): void;
//# sourceMappingURL=index.d.ts.map
18 changes: 18 additions & 0 deletions dist/src/adapters/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions dist/src/adapters/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import type { PTYSession } from '../plugin/pty/types.ts';
/**
* Host-agnostic interface for handling session exit notifications.
* Allows decoupling PTY lifecycle from any specific OpenCode client SDK.
*/
export interface SessionNotifier {
sendExitNotification(session: PTYSession, exitCode: number): Promise<void> | void;
}
/**
* The tool invocation a permission check belongs to.
*
* Hosts that evaluate permissions per session need this to attribute the check
* to a session, an agent and a tool call. It is optional because a check can be
* made outside a tool call; hosts that require it decide what an absent value
* means (the V2 adapter denies, see `V2PermissionAuthorizer`).
*/
export interface PermissionRequestContext {
sessionID?: string;
agent?: string;
messageID?: string;
callID?: string;
}
/**
* Host-agnostic authorizer for validating command and workdir execution permissions.
*/
export interface PermissionAuthorizer {
checkCommand(command: string, args: string[], context?: PermissionRequestContext): Promise<void>;
checkWorkdir(workdir: string, context?: PermissionRequestContext): Promise<void>;
}
/**
* Common host adapter contract bridging a host environment (e.g. OpenCode V1, V2, Standalone)
* with the core PTY manager and execution environment.
*/
export interface HostAdapter {
readonly id: string;
readonly notifier?: SessionNotifier;
readonly permissions?: PermissionAuthorizer;
onSessionDeleted?(sessionId: string): void;
}
//# sourceMappingURL=types.d.ts.map
1 change: 1 addition & 0 deletions dist/src/adapters/types.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions dist/src/adapters/v1/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import type { PluginContext } from '../../plugin/types.ts';
import type { HostAdapter } from '../types.ts';
export { V1NotificationAdapter } from './notifications.ts';
export { V1PermissionAuthorizer } from './permissions.ts';
export declare function createV1Adapter(context: PluginContext): HostAdapter;
//# sourceMappingURL=index.d.ts.map
18 changes: 18 additions & 0 deletions dist/src/adapters/v1/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions dist/src/adapters/v1/notifications.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { OpencodeClient } from '@opencode-ai/sdk';
import type { PTYSession } from '../../plugin/pty/types.ts';
import type { SessionNotifier } from '../types.ts';
export declare class V1NotificationAdapter implements SessionNotifier {
private manager;
constructor(client?: OpencodeClient);
init(client: OpencodeClient): void;
sendExitNotification(session: PTYSession, exitCode: number): Promise<void>;
}
//# sourceMappingURL=notifications.d.ts.map
17 changes: 17 additions & 0 deletions dist/src/adapters/v1/notifications.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions dist/src/adapters/v1/permissions.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import type { PluginClient } from '../../plugin/types.ts';
import type { PermissionAuthorizer } from '../types.ts';
type PermissionAction = 'allow' | 'ask' | 'deny';
type BashPermissions = PermissionAction | Record<string, PermissionAction>;
export interface PermissionConfig {
bash?: BashPermissions;
external_directory?: PermissionAction;
}
export declare class V1PermissionAuthorizer implements PermissionAuthorizer {
private client;
private directory;
constructor(client: PluginClient | null, directory: string | null);
private getPermissionConfig;
private showToast;
private denyWithToast;
private handleAskPermission;
checkCommand(command: string, args: string[]): Promise<void>;
checkWorkdir(workdir: string): Promise<void>;
}
export {};
//# sourceMappingURL=permissions.d.ts.map
84 changes: 84 additions & 0 deletions dist/src/adapters/v1/permissions.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading