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>
This commit is contained in:
@@ -15,6 +15,8 @@ class ChatShellRunsRepository {
|
||||
String? data,
|
||||
int? after,
|
||||
List<String>? runIds,
|
||||
bool reply = false,
|
||||
bool activate = false,
|
||||
}) => {
|
||||
'chat_shell_run_request': {
|
||||
'session_id': sessionId,
|
||||
@@ -25,6 +27,8 @@ class ChatShellRunsRepository {
|
||||
'data': ?data,
|
||||
'after': ?after,
|
||||
'run_ids': ?runIds,
|
||||
if (reply) 'reply': true,
|
||||
if (activate) 'activate': true,
|
||||
},
|
||||
};
|
||||
|
||||
@@ -43,16 +47,46 @@ class ChatShellRunsRepository {
|
||||
);
|
||||
|
||||
/// Writes typed text (control characters and escape sequences included)
|
||||
/// to a running command's terminal.
|
||||
Future<void> input(String sessionId, String runId, String data) => gateway
|
||||
.call('chat_shell_input', _body(sessionId, runId: runId, data: data));
|
||||
/// to a running command's terminal. [reply] marks the emulator's automatic
|
||||
/// answer to a query, which the server passes on only from the terminal's
|
||||
/// active client; typing carries the [size] this client draws it at.
|
||||
Future<void> input(
|
||||
String sessionId,
|
||||
String runId,
|
||||
String data, {
|
||||
bool reply = false,
|
||||
(int, int)? size,
|
||||
}) => gateway.call(
|
||||
'chat_shell_input',
|
||||
_body(
|
||||
sessionId,
|
||||
runId: runId,
|
||||
data: data,
|
||||
reply: reply,
|
||||
cols: size?.$1,
|
||||
rows: size?.$2,
|
||||
),
|
||||
);
|
||||
|
||||
/// Sets a running command's terminal size.
|
||||
Future<void> resize(String sessionId, String runId, int cols, int rows) =>
|
||||
gateway.call(
|
||||
'chat_shell_resize',
|
||||
_body(sessionId, runId: runId, cols: cols, rows: rows),
|
||||
);
|
||||
/// Reports the size this client draws a running command's terminal at;
|
||||
/// [activate] makes it the active client (it focused the terminal), whose
|
||||
/// size the process takes.
|
||||
Future<void> resize(
|
||||
String sessionId,
|
||||
String runId,
|
||||
int cols,
|
||||
int rows, {
|
||||
bool activate = false,
|
||||
}) => gateway.call(
|
||||
'chat_shell_resize',
|
||||
_body(
|
||||
sessionId,
|
||||
runId: runId,
|
||||
cols: cols,
|
||||
rows: rows,
|
||||
activate: activate,
|
||||
),
|
||||
);
|
||||
|
||||
Future<void> stop(String sessionId, String runId) =>
|
||||
gateway.call('chat_shell_stop', _body(sessionId, runId: runId));
|
||||
|
||||
@@ -11,30 +11,51 @@ const int chatShellTerminalScrollback = 5000;
|
||||
///
|
||||
/// It keeps no copy of typed input: [Terminal.onOutput] hands each key or
|
||||
/// paste straight to [onInput], which sends it.
|
||||
///
|
||||
/// Several clients may draw one terminal. The emulator's automatic answers
|
||||
/// to queries in the output (cursor position, device attributes) go to
|
||||
/// [onReply] instead, so the server can let only the active client's
|
||||
/// answers through; a view gaining focus calls [activate], which makes this
|
||||
/// client the active one at its size ([onActivate]).
|
||||
class ChatShellTerminalSession {
|
||||
ChatShellTerminalSession(
|
||||
ChatShellRun run, {
|
||||
required this.onInput,
|
||||
required this.onResize,
|
||||
this.onReply,
|
||||
this.onActivate,
|
||||
}) : terminal = _ChatShellTerminal() {
|
||||
// Drawn at the process's own size until a view lays it out at its own.
|
||||
if (run.cols > 0 && run.rows > 0) terminal.resize(run.cols, run.rows);
|
||||
terminal.onOutput = _output;
|
||||
terminal.onResize = (cols, rows, _, _) => onResize(cols, rows);
|
||||
terminal.onResize = (cols, rows, _, _) {
|
||||
_viewSize = (cols, rows);
|
||||
onResize(cols, rows);
|
||||
};
|
||||
replay(run);
|
||||
}
|
||||
|
||||
final Terminal terminal;
|
||||
|
||||
/// Receives input for the process; only called while the run is live.
|
||||
/// Receives what the operator types for the process; only called while
|
||||
/// the run is live.
|
||||
final void Function(String data) onInput;
|
||||
|
||||
/// Receives the size the view laid the terminal out at.
|
||||
final void Function(int cols, int rows) onResize;
|
||||
|
||||
/// Receives the emulator's automatic answers to queries in the live
|
||||
/// output; [onInput] receives them when this is null.
|
||||
final void Function(String data)? onReply;
|
||||
|
||||
/// Receives this client's size when a view of it gains focus.
|
||||
final void Function(int cols, int rows)? onActivate;
|
||||
|
||||
int _end = 0;
|
||||
bool _live = false;
|
||||
bool _replaying = false;
|
||||
bool _drawing = false;
|
||||
(int, int)? _viewSize;
|
||||
|
||||
/// The absolute output byte this terminal has drawn up to.
|
||||
int get end => _end;
|
||||
@@ -42,6 +63,16 @@ class ChatShellTerminalSession {
|
||||
/// Whether typing reaches a running process.
|
||||
bool get live => _live;
|
||||
|
||||
/// The size a view last laid the terminal out at; null before any did.
|
||||
(int, int)? get viewSize => _viewSize;
|
||||
|
||||
/// A view of this terminal gained focus: this client becomes the one the
|
||||
/// process is sized for and whose answers it receives.
|
||||
void activate() {
|
||||
final size = _viewSize;
|
||||
if (_live && size != null) onActivate?.call(size.$1, size.$2);
|
||||
}
|
||||
|
||||
/// Draws [run]'s held output from scratch: on open, after a gap and after
|
||||
/// a readback replaced the card's copy. Answers the emulator gives to old
|
||||
/// queries in that output are not sent: the process asked them long ago.
|
||||
@@ -78,7 +109,13 @@ class ChatShellTerminalSession {
|
||||
replay(run);
|
||||
return;
|
||||
}
|
||||
terminal.write(_drawable(run, text));
|
||||
// What the emulator sends while drawing output answers that output.
|
||||
_drawing = true;
|
||||
try {
|
||||
terminal.write(_drawable(run, text));
|
||||
} finally {
|
||||
_drawing = false;
|
||||
}
|
||||
_end = end;
|
||||
}
|
||||
|
||||
@@ -113,7 +150,13 @@ class ChatShellTerminalSession {
|
||||
}
|
||||
|
||||
void _output(String data) {
|
||||
if (_live && !_replaying) onInput(data);
|
||||
if (!_live || _replaying) return;
|
||||
final reply = onReply;
|
||||
if (_drawing && reply != null) {
|
||||
reply(data);
|
||||
} else {
|
||||
onInput(data);
|
||||
}
|
||||
}
|
||||
|
||||
/// Plain-pipe output has bare newlines, which a terminal draws as a line
|
||||
|
||||
@@ -94,6 +94,7 @@ class _ChatShellTerminalViewState extends State<ChatShellTerminalView> {
|
||||
final TerminalController _controller = TerminalController();
|
||||
final FocusNode _focus = FocusNode(debugLabel: 'chat-shell-terminal');
|
||||
final FocusNode _outputFocus = FocusNode(debugLabel: 'chat-shell-output');
|
||||
bool _focused = false;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
@@ -101,10 +102,20 @@ class _ChatShellTerminalViewState extends State<ChatShellTerminalView> {
|
||||
// A paste while the terminal has focus goes to the process, not to the
|
||||
// chat composer.
|
||||
ChatComposerPasteFocus.registerInput(_focus);
|
||||
_focus.addListener(_focusChanged);
|
||||
}
|
||||
|
||||
/// Focusing a terminal makes this client the one it is sized for, the
|
||||
/// way typing in it does.
|
||||
void _focusChanged() {
|
||||
final focused = _focus.hasFocus;
|
||||
if (focused && !_focused) widget.session.activate();
|
||||
_focused = focused;
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_focus.removeListener(_focusChanged);
|
||||
ChatComposerPasteFocus.unregisterInput(_focus);
|
||||
_controller.dispose();
|
||||
_focus.dispose();
|
||||
|
||||
@@ -43,7 +43,9 @@ class ChatShellTerminals {
|
||||
() => ChatShellTerminalSession(
|
||||
run,
|
||||
onInput: (data) => _input(run.id, data),
|
||||
onReply: (data) => _input(run.id, data, reply: true),
|
||||
onResize: (cols, rows) => _resized(run.id, cols, rows),
|
||||
onActivate: (cols, rows) => _activated(run.id, cols, rows),
|
||||
),
|
||||
);
|
||||
|
||||
@@ -69,17 +71,35 @@ class ChatShellTerminals {
|
||||
_sessions[run.id]?.replay(run);
|
||||
}
|
||||
|
||||
void _input(String runId, String data) {
|
||||
void _input(String runId, String data, {bool reply = false}) {
|
||||
if (_disposed) return;
|
||||
_inputs
|
||||
.putIfAbsent(
|
||||
runId,
|
||||
() => ChatShellInputPipe(
|
||||
(data) => _repository.input(_sessionId, runId, data),
|
||||
(data, reply) => _repository.input(
|
||||
_sessionId,
|
||||
runId,
|
||||
data,
|
||||
reply: reply,
|
||||
// Typing makes this client the active one at its size.
|
||||
size: reply ? null : _sessions[runId]?.viewSize,
|
||||
),
|
||||
(error) => onError(runId, error),
|
||||
),
|
||||
)
|
||||
.add(data);
|
||||
.add(data, reply: reply);
|
||||
}
|
||||
|
||||
/// A view of [runId] gained focus: it becomes the active client and the
|
||||
/// process takes its size at once.
|
||||
void _activated(String runId, int cols, int rows) {
|
||||
if (_disposed || !(_sessions[runId]?.live ?? false)) return;
|
||||
_resizes.remove(runId)?.cancel();
|
||||
_sentSizes[runId] = (cols, rows);
|
||||
_repository
|
||||
.resize(_sessionId, runId, cols, rows, activate: true)
|
||||
.catchError((Object error) => onError(runId, error));
|
||||
}
|
||||
|
||||
void _resized(String runId, int cols, int rows) {
|
||||
@@ -110,16 +130,25 @@ class ChatShellTerminals {
|
||||
/// machine terminal's), in order: what arrives
|
||||
/// while a request is in flight goes in the next one. Nothing is kept once
|
||||
/// sent, and a refusal discards what was waiting rather than retyping it.
|
||||
///
|
||||
/// The emulator's automatic answers ([add] with reply) travel in requests
|
||||
/// of their own, marked so, never merged with typing on either side.
|
||||
class ChatShellInputPipe {
|
||||
ChatShellInputPipe(this._send, this._failed);
|
||||
|
||||
final Future<void> Function(String data) _send;
|
||||
final Future<void> Function(String data, bool reply) _send;
|
||||
final void Function(Object error) _failed;
|
||||
final StringBuffer _pending = StringBuffer();
|
||||
|
||||
/// Waiting text in order, each run of typing or of answers in one buffer.
|
||||
final List<(StringBuffer, bool)> _pending = [];
|
||||
bool _busy = false;
|
||||
|
||||
void add(String data) {
|
||||
_pending.write(data);
|
||||
void add(String data, {bool reply = false}) {
|
||||
if (data.isEmpty) return;
|
||||
if (_pending.isEmpty || _pending.last.$2 != reply) {
|
||||
_pending.add((StringBuffer(), reply));
|
||||
}
|
||||
_pending.last.$1.write(data);
|
||||
if (!_busy) unawaited(_pump());
|
||||
}
|
||||
|
||||
@@ -127,19 +156,22 @@ class ChatShellInputPipe {
|
||||
_busy = true;
|
||||
try {
|
||||
while (_pending.isNotEmpty) {
|
||||
var waiting = _pending.toString();
|
||||
_pending.clear();
|
||||
final (buffer, reply) = _pending.first;
|
||||
var waiting = buffer.toString();
|
||||
buffer.clear();
|
||||
var cut = waiting.length;
|
||||
if (cut > chatShellInputChunk) {
|
||||
cut = chatShellInputChunk;
|
||||
// Never split a surrogate pair across two requests.
|
||||
final unit = waiting.codeUnitAt(cut - 1);
|
||||
if (unit >= 0xD800 && unit <= 0xDBFF) cut--;
|
||||
_pending.write(waiting.substring(cut));
|
||||
buffer.write(waiting.substring(cut));
|
||||
} else {
|
||||
_pending.removeAt(0);
|
||||
}
|
||||
final chunk = waiting.substring(0, cut);
|
||||
waiting = '';
|
||||
await _send(chunk);
|
||||
await _send(chunk, reply);
|
||||
}
|
||||
} catch (error) {
|
||||
_pending.clear();
|
||||
|
||||
Reference in New Issue
Block a user