e13cb99ade
A headless xterm-compatible emulator (internal/termstate) follows each machine terminal's PTY output under the same lock as the 1 MiB tail. A terminal_output from before the retained tail (new page, reload, gap) now gets output_snapshot: an escape stream that resets the client and restores scrollback (1000 lines), main screen, the alternate screen when in use (via ?1049h so the program's exit brings the shell back), saved cursors, tabs, scroll region, cursor incl. pending wrap, modes (mouse tracking/encoding, bracketed paste, DECCKM, keypad, autowrap, origin, focus, ...), charsets, pen and title, as of output_end; frames continue there. Reads inside the tail stay raw. No Go library was adequate: charmbracelet/x/vt and vito/midterm scroll at 0.2-4 MB/s and hide modes/pen/region; vt10x is unmaintained. termstate runs at 160-200 MB/s. The app treats a snapshot reply as a replay without the dropped-output line, and every replay now starts with RIS so stale modes end. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
180 lines
5.5 KiB
Dart
180 lines
5.5 KiB
Dart
import 'dart:async';
|
|
import 'dart:convert';
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:pipelines_app/core/api_gateway.dart';
|
|
import 'package:pipelines_app/features/terminal/terminal_layout.dart';
|
|
import 'package:pipelines_app/features/terminal/terminal_machines.dart';
|
|
|
|
/// One machine's terminals as a server holds them, answering the terminal
|
|
/// requests and pushing frames like the real one.
|
|
class TerminalGateway implements ApiGateway {
|
|
TerminalGateway({this.stream = 's1'});
|
|
|
|
final _frames = StreamController<JsonMap>.broadcast();
|
|
final List<JsonMap> requests = [];
|
|
final Map<String, JsonMap> terminals = {};
|
|
final Map<String, String> output = {};
|
|
String stream;
|
|
int _next = 0;
|
|
|
|
/// When set, requests of this type fail with this message.
|
|
final Map<String, String> refuse = {};
|
|
|
|
/// The flow window terminal_output grants (0: an older server).
|
|
int window = 0;
|
|
|
|
/// Bytes a terminal's stream holds beyond its text's UTF-8 (negative
|
|
/// where runs of invalid bytes became one shorter replacement character).
|
|
final Map<String, int> byteShift = {};
|
|
|
|
/// A terminal's state as the server's snapshot: when set, a read from
|
|
/// before [snapshotFrom] gets it (as of the output's end) instead of raw
|
|
/// output.
|
|
final Map<String, String> snapshots = {};
|
|
int snapshotFrom = 1 << 30;
|
|
|
|
@override
|
|
Stream<JsonMap> get frames => _frames.stream;
|
|
|
|
List<JsonMap> bodies(String type) => [
|
|
for (final request in requests)
|
|
if (request['type'] == type)
|
|
Map<String, dynamic>.from(request['terminal_request'] as Map? ?? {}),
|
|
];
|
|
|
|
JsonMap add(String id, {String cwd = '/Users/op', String text = ''}) {
|
|
final record = <String, dynamic>{
|
|
'id': id,
|
|
'cwd': cwd,
|
|
'shell': '/bin/zsh',
|
|
'status': 'running',
|
|
'cols': 80,
|
|
'rows': 24,
|
|
'created_at': 1,
|
|
};
|
|
terminals[id] = record;
|
|
output[id] = text;
|
|
return record;
|
|
}
|
|
|
|
JsonMap _record(String id, {bool withOutput = false, int after = 0}) {
|
|
final text = output[id] ?? '';
|
|
final bytes = utf8.encode(text);
|
|
final start = after.clamp(0, bytes.length);
|
|
final snapshot = snapshots[id];
|
|
if (withOutput && snapshot != null && after < snapshotFrom) {
|
|
final end = bytes.length + (byteShift[id] ?? 0);
|
|
return {
|
|
...terminals[id]!,
|
|
'stream': stream,
|
|
'output': snapshot,
|
|
'output_start': end,
|
|
'output_end': end,
|
|
'output_snapshot': true,
|
|
if (window > 0) 'window': window,
|
|
};
|
|
}
|
|
return {
|
|
...terminals[id]!,
|
|
'stream': stream,
|
|
'output_end': bytes.length + (byteShift[id] ?? 0),
|
|
if (withOutput) 'output': utf8.decode(bytes.sublist(start)),
|
|
if (withOutput) 'output_start': start,
|
|
if (withOutput && window > 0) 'window': window,
|
|
};
|
|
}
|
|
|
|
/// The shell writes [text]: it is retained and pushed.
|
|
void write(String id, String text, {bool push = true}) {
|
|
final offset = utf8.encode(output[id] ?? '').length;
|
|
output[id] = (output[id] ?? '') + text;
|
|
if (push) {
|
|
pushOutput(id, text, offset);
|
|
}
|
|
}
|
|
|
|
void pushOutput(String id, String text, int offset, {int? end}) =>
|
|
_frames.add({
|
|
'type': 'terminal_event',
|
|
'terminal_event': {
|
|
'terminal_id': id,
|
|
'output': text,
|
|
'offset': offset,
|
|
'end': end ?? offset + utf8.encode(text).length,
|
|
},
|
|
});
|
|
|
|
void lifecycle(String id, Map<String, dynamic> changes) {
|
|
terminals[id] = {...terminals[id]!, ...changes};
|
|
_frames.add({
|
|
'type': 'terminal_event',
|
|
'terminal_event': {'terminal_id': id, 'terminal': _record(id)},
|
|
});
|
|
}
|
|
|
|
@override
|
|
Future<JsonMap> call(String type, [JsonMap fields = const {}]) async {
|
|
requests.add({'type': type, ...fields});
|
|
final refusal = refuse[type];
|
|
if (refusal != null) throw StateError(refusal);
|
|
final body = (fields['terminal_request'] as Map?) ?? const {};
|
|
final id = '${body['terminal_id'] ?? ''}';
|
|
switch (type) {
|
|
case 'terminal_list':
|
|
return {
|
|
'type': type,
|
|
'terminals': [for (final id in terminals.keys) _record(id)],
|
|
};
|
|
case 'terminal_open':
|
|
final next = 'term_${++_next}';
|
|
final cwd = '${body['cwd'] ?? ''}';
|
|
add(next, cwd: cwd.isEmpty ? '/Users/op' : cwd, text: r'$ ');
|
|
return {'type': type, 'terminal': _record(next)};
|
|
case 'terminal_output':
|
|
return {
|
|
'type': type,
|
|
'terminal': _record(
|
|
id,
|
|
withOutput: true,
|
|
after: (body['after'] as num?)?.toInt() ?? 0,
|
|
),
|
|
};
|
|
case 'terminal_close':
|
|
final record = {..._record(id), 'status': 'closed'};
|
|
terminals.remove(id);
|
|
return {'type': type, 'terminal': record};
|
|
case 'terminal_input':
|
|
case 'terminal_ack':
|
|
return {'type': type};
|
|
case 'terminal_resize':
|
|
return {'type': type, 'terminal': _record(id)};
|
|
}
|
|
throw StateError('unexpected $type');
|
|
}
|
|
|
|
Future<void> close() => _frames.close();
|
|
}
|
|
|
|
/// A machine list tests change by hand.
|
|
class TestMachines extends ValueNotifier<List<TerminalMachine>> {
|
|
TestMachines(super.value);
|
|
}
|
|
|
|
TerminalMachine machine(String id, TerminalGateway gateway, {String? name}) =>
|
|
TerminalMachine(id: id, name: name ?? id, gateway: gateway);
|
|
|
|
/// A layout store kept in memory.
|
|
class MemoryLayoutStore implements TerminalLayoutStore {
|
|
String? saved;
|
|
int saves = 0;
|
|
@override
|
|
Future<String?> load() async => saved;
|
|
@override
|
|
Future<bool> save(String value) async {
|
|
saved = value;
|
|
saves++;
|
|
return true;
|
|
}
|
|
}
|