cf9f71f27c
Dismissed shell runs leave the transcript and fold into a "N commands" chip in the footer of the first answer at or after each run (the last answer for newer runs, or a row of its own without any answer). The chip opens the turn sheet (wide) or a full-screen page (narrow) listing the commands; each opens its output read-only with Copy output, Send to agent and Restore. On a wide conversation the answer's Copy button moves out of the footer into a 48 px gutter left of the reading column (chatCopyGutter), shown on hover or focus on desktops and always on touch; narrow widths keep it in the footer.
288 lines
8.7 KiB
Dart
288 lines
8.7 KiB
Dart
// Operator shell commands for the full-app probe (`?shell=1`, with
|
|
// `?chats=N`): every fixture chat lists four recorded terminal runs
|
|
// (succeeded, failed, stopped and an interactive login still waiting for a
|
|
// token), and chat_shell_run/stop/share/input answer as the server does, with
|
|
// the frames that follow; chat_shell_dismiss/restore move runs into and out
|
|
// of the dismissed list the readback reports (`?shell_dismissed=a,b` starts
|
|
// with those run ids dismissed). The login prompt hides what is typed, as a
|
|
// real terminal's password prompt does, and finishes on Enter.
|
|
import 'dart:convert';
|
|
|
|
import 'package:pipelines_app/core/api_gateway.dart';
|
|
|
|
bool _enabled = false;
|
|
DateTime _base = DateTime.now();
|
|
int _loginEnd = utf8.encode(_loginPrompt).length;
|
|
int _tokenLength = 0;
|
|
final Set<String> _dismissed = {};
|
|
|
|
List<String> _runIds(Object? body) => [
|
|
if (body is Map && body['run_ids'] is List)
|
|
for (final id in body['run_ids'] as List) '$id',
|
|
];
|
|
|
|
void configureChatShellFixture(Map<String, String> params) {
|
|
_enabled = params['shell'] == '1';
|
|
_base = DateTime.now().subtract(const Duration(minutes: 3));
|
|
_loginEnd = utf8.encode(_loginPrompt).length;
|
|
_dismissed
|
|
..clear()
|
|
..addAll([
|
|
for (final id in (params['shell_dismissed'] ?? '').split(','))
|
|
if (id.trim().isNotEmpty) id.trim(),
|
|
]);
|
|
}
|
|
|
|
const String _loginPrompt =
|
|
'\x1b[1m!\x1b[0m First copy your one-time code: \x1b[1;33mA1B2-C3D4\x1b[0m\r\n'
|
|
'Open this URL to continue in your web browser: '
|
|
'\x1b[4mhttps://github.com/login/device\x1b[0m\r\n'
|
|
'\x1b[32m?\x1b[0m \x1b[1mPaste an authentication token\x1b[0m: ';
|
|
|
|
int _at(int seconds) =>
|
|
_base.add(Duration(seconds: seconds)).millisecondsSinceEpoch;
|
|
|
|
String _session(JsonMap request) {
|
|
final body = request['chat_shell_run_request'] ?? request['chat_query'];
|
|
return body is Map ? '${body['session_id'] ?? ''}' : '';
|
|
}
|
|
|
|
JsonMap _run(
|
|
String session,
|
|
String id,
|
|
String command,
|
|
int started, {
|
|
String status = 'exited',
|
|
int? exitCode,
|
|
int duration = 0,
|
|
String output = '',
|
|
}) => {
|
|
'terminal': true,
|
|
'cols': 100,
|
|
'rows': 30,
|
|
'id': id,
|
|
'session_id': session,
|
|
'command': command,
|
|
'directory': '/Users/operator/src/pipelines',
|
|
'status': status,
|
|
'exit_code': ?exitCode,
|
|
'started_at': _at(started),
|
|
if (status != 'running') 'ended_at': _at(started) + duration,
|
|
'duration_ms': duration,
|
|
'output': output,
|
|
'output_bytes': utf8.encode(output).length,
|
|
};
|
|
|
|
List<JsonMap> _runs(String session) => [
|
|
_run(
|
|
session,
|
|
'run_status',
|
|
'git status --short',
|
|
0,
|
|
exitCode: 0,
|
|
duration: 84,
|
|
output:
|
|
' \x1b[31mM\x1b[0m app/lib/features/chat/shell/chat_shell_run_card.dart\r\n'
|
|
'\x1b[31m??\x1b[0m app/notes/chat-shell-commands.md\r\n',
|
|
),
|
|
_run(
|
|
session,
|
|
'run_tests',
|
|
'go test ./internal/server -run ChatShell',
|
|
20,
|
|
exitCode: 1,
|
|
duration: 4210,
|
|
output:
|
|
'${[for (final name in _tests) '=== RUN TestChatShell$name\r\n\x1b[32m--- PASS\x1b[0m: TestChatShell$name (0.0${name.length % 9}s)\r\n'].join()}'
|
|
'\x1b[1;31m--- FAIL\x1b[0m: TestChatShellStopInterruptsTheGroup (0.21s)\r\n'
|
|
' chat_shell_run_test.go:112: stopped record = {Status:exited}\r\n'
|
|
'\x1b[31mFAIL\x1b[0m\r\n'
|
|
'\x1b[31mFAIL\x1b[0m\tgit.a-wan.net/alan/pipelines/internal/server\t0.611s\r\n'
|
|
'\x1b[31mFAIL\x1b[0m\r\n',
|
|
),
|
|
_run(
|
|
session,
|
|
'run_watch',
|
|
'tail -f /var/log/system.log',
|
|
40,
|
|
status: 'stopped',
|
|
duration: 15200,
|
|
output: 'Nov 9 15:58:01 host kernel[0]: wake reason: EC.LidOpen\r\n^C',
|
|
),
|
|
_run(
|
|
session,
|
|
'run_login',
|
|
'gh auth login --hostname github.com',
|
|
60,
|
|
status: 'running',
|
|
output: _loginPrompt,
|
|
),
|
|
];
|
|
|
|
JsonMap? chatShellReply(JsonMap request) {
|
|
if (!_enabled) return null;
|
|
final id = request['id'];
|
|
final session = _session(request);
|
|
final body = request['chat_shell_run_request'];
|
|
final runId = body is Map ? '${body['run_id'] ?? ''}' : '';
|
|
switch ('${request['type']}') {
|
|
case 'chat_shell_runs':
|
|
return {
|
|
'id': id,
|
|
'type': 'chat_shell_runs',
|
|
'chat_shell_runs': [
|
|
for (final run in _runs(session))
|
|
if (!_dismissed.contains(run['id'])) run,
|
|
],
|
|
'chat_shell_dismissed': _dismissed.toList(),
|
|
'chat_shell_dismissed_runs': [
|
|
for (final run in _runs(session))
|
|
if (_dismissed.contains(run['id']))
|
|
{...run, 'dismissed': true, 'dismissed_at': _at(200)},
|
|
],
|
|
};
|
|
case 'chat_shell_dismiss':
|
|
final ids = _runIds(body);
|
|
_dismissed.addAll(ids);
|
|
return {
|
|
'id': id,
|
|
'type': 'chat_shell_dismiss',
|
|
'chat_shell_dismissed': ids,
|
|
};
|
|
case 'chat_shell_restore':
|
|
final ids = _runIds(body).toSet();
|
|
_dismissed.removeAll(ids);
|
|
return {
|
|
'id': id,
|
|
'type': 'chat_shell_restore',
|
|
'chat_shell_runs': [
|
|
for (final run in _runs(session))
|
|
if (ids.contains(run['id'])) run,
|
|
],
|
|
};
|
|
case 'chat_shell_run':
|
|
return {
|
|
'id': id,
|
|
'type': 'chat_shell_run',
|
|
'chat_shell_run': _run(
|
|
session,
|
|
'run_new',
|
|
body is Map ? '${body['command'] ?? ''}' : '',
|
|
180,
|
|
status: 'running',
|
|
),
|
|
};
|
|
case 'chat_shell_stop':
|
|
return {'id': id, 'type': 'chat_shell_stop'};
|
|
case 'chat_shell_run_output':
|
|
return {
|
|
'id': id,
|
|
'type': 'chat_shell_run_output',
|
|
'chat_shell_run': _runs(session).firstWhere(
|
|
(run) => run['id'] == runId,
|
|
orElse: () => _runs(session).first,
|
|
),
|
|
};
|
|
case 'chat_shell_share':
|
|
return {'id': id, 'type': 'chat_prompt'};
|
|
case 'chat_shell_input':
|
|
return {'id': id, 'type': 'chat_shell_input'};
|
|
case 'chat_shell_resize':
|
|
return {'id': id, 'type': 'chat_shell_resize'};
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/// The frames the server pushes after a run starts or is stopped.
|
|
List<JsonMap> chatShellFollowUps(JsonMap request) {
|
|
if (!_enabled) return const [];
|
|
final session = _session(request);
|
|
JsonMap frame(JsonMap body) => {
|
|
'type': 'chat_shell_run_event',
|
|
'chat_shell_run_event': {'session_id': session, ...body},
|
|
};
|
|
switch ('${request['type']}') {
|
|
case 'chat_shell_dismiss':
|
|
return [
|
|
for (final runId in _runIds(request['chat_shell_run_request']))
|
|
frame({'run_id': runId, 'dismissed': true}),
|
|
];
|
|
case 'chat_shell_restore':
|
|
final ids = _runIds(request['chat_shell_run_request']).toSet();
|
|
return [
|
|
for (final run in _runs(session))
|
|
if (ids.contains(run['id'])) frame({'run_id': run['id'], 'run': run}),
|
|
];
|
|
case 'chat_shell_run':
|
|
const text = 'hello from the fixture shell\n';
|
|
final body = request['chat_shell_run_request'];
|
|
return [
|
|
frame({
|
|
'run_id': 'run_new',
|
|
'output': text,
|
|
'offset': 0,
|
|
'end': text.length,
|
|
}),
|
|
frame({
|
|
'run_id': 'run_new',
|
|
'run': _run(
|
|
session,
|
|
'run_new',
|
|
body is Map ? '${body['command'] ?? ''}' : '',
|
|
180,
|
|
exitCode: 0,
|
|
duration: 37,
|
|
),
|
|
}),
|
|
];
|
|
case 'chat_shell_stop':
|
|
final run = _runs(session).last
|
|
..['status'] = 'stopped'
|
|
..['duration_ms'] = 9100;
|
|
return [
|
|
frame({'run_id': run['id'], 'run': run}),
|
|
];
|
|
case 'chat_shell_input':
|
|
// The token prompt does not echo; Enter completes the login.
|
|
final body = request['chat_shell_run_request'];
|
|
final data = body is Map ? '${body['data'] ?? ''}' : '';
|
|
_tokenLength += data.replaceAll('\r', '').length;
|
|
if (!data.contains('\r')) return const [];
|
|
final done =
|
|
'\r\n\x1b[2m(received a $_tokenLength-character token)\x1b[0m\r\n'
|
|
'\x1b[32m\u2713\x1b[0m Configured git protocol\r\n'
|
|
'\x1b[32m\u2713\x1b[0m Logged in as \x1b[1moperator\x1b[0m\r\n';
|
|
final start = _loginEnd;
|
|
_loginEnd += utf8.encode(done).length;
|
|
return [
|
|
frame({
|
|
'run_id': 'run_login',
|
|
'output': done,
|
|
'offset': start,
|
|
'end': _loginEnd,
|
|
}),
|
|
frame({
|
|
'run_id': 'run_login',
|
|
'run': _run(
|
|
session,
|
|
'run_login',
|
|
'gh auth login --hostname github.com',
|
|
60,
|
|
exitCode: 0,
|
|
duration: 48200,
|
|
)..['output_bytes'] = _loginEnd,
|
|
}),
|
|
];
|
|
}
|
|
return const [];
|
|
}
|
|
|
|
const _tests = [
|
|
'RunStreamsToEveryWatcherAndPersists',
|
|
'RunLimitsAndRefusals',
|
|
'ShareQueuesAnOrdinaryPrompt',
|
|
'RetainsBoundedTail',
|
|
'ShutdownAndRestartInterrupt',
|
|
'OutputKeepsCharactersWhole',
|
|
];
|