Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.declaw.ai/llms.txt

Use this file to discover all available pages before exploring further.

import "github.com/declaw-ai/declaw-go"
sbx.PTY is the *PTY sub-object available on every Sandbox instance. It provides methods to create interactive terminal sessions, send input, resize, stream output, and kill sessions.

sbx.PTY.Create()

Create a new PTY session. The sandbox spawns an interactive shell and returns a *PtyHandle.
handle, err := sbx.PTY.Create(ctx, declaw.PtySize{Cols: 120, Rows: 30})
fmt.Println("PID:", handle.PID)
If no PtySize is provided, the default 80x24 is used:
handle, err := sbx.PTY.Create(ctx)
size
PtySize
default:"PtySize{Cols: 80, Rows: 24}"
Initial terminal dimensions (optional variadic argument).
Returns (*PtyHandle, error)

PtyHandle

PtyHandle is returned by PTY.Create(). It exposes the full session lifecycle.

handle.PID

Type int — the process ID of the shell process backing the PTY.

handle.SendInput()

Forward keystrokes or text to the shell.
err := handle.SendInput(ctx, []byte("echo hello\n"))
err = handle.SendInput(ctx, []byte{0x03}) // Ctrl-C
data
[]byte
required
Raw bytes to send to the PTY stdin.
Returns error

handle.SetSize()

Change the remote terminal dimensions. Fires SIGWINCH inside so ncurses apps redraw.
err := handle.SetSize(ctx, 160, 50)
cols
int
required
Number of columns.
rows
int
required
Number of rows.
Returns error

handle.Stream()

Returns a read-only channel that receives output data from the PTY as []byte chunks. The channel is closed when the PTY session ends or the context is canceled.
ch, err := handle.Stream(ctx)
if err != nil {
    log.Fatal(err)
}
for data := range ch {
    fmt.Print(string(data))
}
The stream uses Server-Sent Events (SSE). Output chunks are base64-decoded automatically. Returns (<-chan []byte, error)

handle.Kill()

Terminate the PTY session (SIGKILL to the process group).
err := handle.Kill(ctx)
Returns error

PtySize

type PtySize struct {
    Cols int
    Rows int
}

Example: interactive PTY session

ctx := context.Background()

// Create PTY
handle, err := sbx.PTY.Create(ctx, declaw.PtySize{Cols: 100, Rows: 30})
if err != nil {
    log.Fatal(err)
}
defer handle.Kill(ctx)

// Send a command
err = handle.SendInput(ctx, []byte("ls -la && exit\n"))
if err != nil {
    log.Fatal(err)
}

// Stream output
ch, err := handle.Stream(ctx)
if err != nil {
    log.Fatal(err)
}
for data := range ch {
    fmt.Print(string(data))
}