Files
pipelines/internal/server/chat_shell_terminal_unix.go
alan 355b722a1d Run chat shell commands in a pseudo-terminal with typed input and resize
Commands start in a PTY (creack/pty; macOS and Linux) of the client's size
with TERM=xterm-256color in their own session. chat_shell_input writes typed
UTF-8 text in order through a bounded queue; chat_shell_resize sets the size.
Output stays the raw terminal stream with byte offsets as sequence numbers,
chat_shell_run_output resumes from an offset, and a plain-text rendering is
persisted for Copy output and Send to agent. Input is never stored or logged.
2026-11-09 19:57:46 +00:00

34 lines
1.1 KiB
Go

//go:build darwin || linux
package server
import (
"os"
"os/exec"
"syscall"
"github.com/creack/pty"
)
// chatShellTerminals is true where operator commands run in a pseudo-terminal.
const chatShellTerminals = true
// startChatShellTerminal starts cmd as the leader of a new session whose
// controlling terminal is a fresh pseudo-terminal of cols x rows, and returns
// the terminal's controller side: the command's merged output is read from
// it, typed input written to it. The session id is also its process group,
// so Stop's group signals reach everything it started.
func startChatShellTerminal(cmd *exec.Cmd, cols, rows int) (*os.File, error) {
return pty.StartWithAttrs(cmd, chatShellWinsize(cols, rows), &syscall.SysProcAttr{Setsid: true, Setctty: true})
}
// resizeChatShellTerminal sets the terminal's size; the kernel signals the
// foreground process group with SIGWINCH.
func resizeChatShellTerminal(terminal *os.File, cols, rows int) error {
return pty.Setsize(terminal, chatShellWinsize(cols, rows))
}
func chatShellWinsize(cols, rows int) *pty.Winsize {
return &pty.Winsize{Cols: uint16(cols), Rows: uint16(rows)}
}