Files
pipelines/internal/server/terminal_handlers.go
T
alan c3458d8a7f Terminals: one active client per terminal; keystrokes skip the request queue
Several clients drawing one terminal (machine terminal or chat terminal
command) no longer interfere. The server tracks, per terminal, the
connections drawing it, most recently active first: the active client
(the opener, then whoever last typed or focused it) owns the PTY size and
is the only one whose emulator's automatic query answers reach the
process; other clients' answers are acknowledged and dropped, and their
sizes recorded but not applied. When the active connection closes the next
most recent client's size is applied.

Protocol (backwards compatible; all new fields optional):
- terminal_input / chat_shell_input: reply (emulator answer), cols/rows
  (the typist's size, applied before the input).
- terminal_resize / chat_shell_resize: activate (the client focused it).

Latency: PipelinesClient sends terminal_input, terminal_resize,
chat_shell_input and chat_shell_resize at once rather than behind the
previous request's reply. The server reads those onto a per-connection
terminal lane handled in order beside the ordinary request loop, and
terminal_open (up to 5 s waiting for su) now runs off the read loop.

Client: ChatShellTerminalSession routes what the emulator emits while
drawing output to onReply and reports focus through activate();
ChatShellInputPipe keeps answers and typing in separate requests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 23:37:16 +00:00

67 lines
2.2 KiB
Go

package server
import (
"context"
"errors"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
// isTerminalRequest reports the request types handleTerminalRequest answers.
func isTerminalRequest(kind string) bool {
switch kind {
case protocol.TerminalOpen, protocol.TerminalList, protocol.TerminalInput, protocol.TerminalResize,
protocol.TerminalOutput, protocol.TerminalClose:
return true
}
return false
}
// handleTerminalRequest answers one machine-terminal request from an
// authenticated connection. Opening or reading a terminal subscribes the
// connection to its output frames.
func handleTerminalRequest(ctx context.Context, request protocol.Message, m *terminalManager, clientID string, write func(protocol.Message) error) {
fail := func(err error) { _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()}) }
if m == nil {
fail(errors.New("terminals are not configured on this server"))
return
}
body := request.TerminalRequest
if body == nil {
body = &protocol.TerminalRequest{}
}
if request.Type != protocol.TerminalOpen && request.Type != protocol.TerminalList && body.TerminalID == "" {
fail(errors.New("a terminal_id is required"))
return
}
reply := protocol.Message{Type: request.Type, ID: request.ID}
var record protocol.TerminalRecord
var err error
switch request.Type {
case protocol.TerminalOpen:
record, err = m.Open(ctx, clientID, body.Cwd, body.Cols, body.Rows)
case protocol.TerminalList:
var records []protocol.TerminalRecord
records, err = m.List(ctx)
reply.Terminals = records
case protocol.TerminalInput:
err = m.ClientInput(clientID, body.TerminalID, body.Data, body.Reply, body.Cols, body.Rows)
case protocol.TerminalResize:
record, err = m.ClientResize(clientID, body.TerminalID, body.Cols, body.Rows, body.Activate)
case protocol.TerminalOutput:
m.clients.subscribe(clientID, body.TerminalID)
m.attachViewer(clientID, body.TerminalID)
record, err = m.Output(ctx, body.TerminalID, body.After)
case protocol.TerminalClose:
record, err = m.Close(ctx, body.TerminalID)
}
if err != nil {
fail(err)
return
}
if record.ID != "" {
reply.Terminal = &record
}
_ = write(reply)
}