Run shell commands from the chat composer and show them as cards
A draft starting with ! shows a Shell mode indicator and runs through chat_shell_run instead of being sent to the agent; the chat's actions menu gains Run command. Each run is a terminal-style card among the turns with live output, Stop, its exit status, Copy output and Send to agent.
This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/features/chat/domain/chat_commands.dart';
|
||||
import 'package:pipelines_app/features/chat/shell/chat_shell_command.dart';
|
||||
import 'package:pipelines_app/features/chat/shell/chat_shell_run.dart';
|
||||
|
||||
ChatShellRun _run(String id, int started, {String status = 'running'}) =>
|
||||
ChatShellRun(
|
||||
id: id,
|
||||
command: 'ls',
|
||||
directory: '/w',
|
||||
status: status,
|
||||
startedAt: started,
|
||||
);
|
||||
|
||||
void main() {
|
||||
test('a leading ! (after blanks) is shell mode and names the command', () {
|
||||
expect(chatDraftIsShell('!ls -la'), isTrue);
|
||||
expect(chatDraftIsShell(' ! git status'), isTrue);
|
||||
expect(chatDraftIsShell('say !hi'), isFalse);
|
||||
expect(chatDraftIsShell('/help'), isFalse);
|
||||
expect(chatShellCommandOf(' ! git status '), 'git status');
|
||||
expect(chatShellCommandOf('!'), '');
|
||||
expect(chatShellCommandOf('hello'), isNull);
|
||||
});
|
||||
|
||||
test('/help lists the shell hint after the commands', () {
|
||||
expect(chatCommandHelpText().split('\n').last, chatShellHelpLine);
|
||||
});
|
||||
|
||||
test('runs are placed among timed rows by start time', () {
|
||||
final rows = [
|
||||
{
|
||||
'kind': 'user',
|
||||
'key': 'u1',
|
||||
'message': {'created_at': 100},
|
||||
},
|
||||
{'kind': 'assistant', 'key': 'a1', 'created_at': 200},
|
||||
{'kind': 'user', 'key': 'local', 'message': <String, dynamic>{}},
|
||||
{'kind': 'assistant', 'key': 'a2', 'created_at': 400},
|
||||
];
|
||||
final merged = withChatShellRunRows(rows, [
|
||||
_run('late', 500),
|
||||
_run('early', 150),
|
||||
_run('mid', 300),
|
||||
]);
|
||||
expect(merged.map((row) => row['key']), [
|
||||
'u1',
|
||||
'chat-shell-run-early',
|
||||
'a1',
|
||||
'local',
|
||||
'chat-shell-run-mid',
|
||||
'a2',
|
||||
'chat-shell-run-late',
|
||||
]);
|
||||
expect(withChatShellRunRows(rows, const []), same(rows));
|
||||
});
|
||||
|
||||
test('status words follow the recorded outcome', () {
|
||||
ChatShellRun ended(String status, {int? code, String signal = ''}) =>
|
||||
ChatShellRun(
|
||||
id: 'r',
|
||||
command: 'x',
|
||||
directory: '/',
|
||||
status: status,
|
||||
startedAt: 1,
|
||||
exitCode: code,
|
||||
signal: signal,
|
||||
);
|
||||
expect(ended('exited', code: 0).state, ChatShellRunState.succeeded);
|
||||
expect(chatShellStatusLabel(ended('exited', code: 2)), 'Exit 2');
|
||||
expect(ended('exited', code: 2).state, ChatShellRunState.failed);
|
||||
expect(
|
||||
chatShellStatusLabel(ended('exited', signal: 'killed')),
|
||||
'Killed (killed)',
|
||||
);
|
||||
expect(chatShellStatusLabel(ended('stopped')), 'Stopped');
|
||||
expect(chatShellStatusLabel(ended('interrupted')), 'Interrupted');
|
||||
expect(chatShellDuration(250), '250 ms');
|
||||
expect(chatShellDuration(1500), '1.5 s');
|
||||
expect(chatShellDuration(125000), '2m 5s');
|
||||
});
|
||||
|
||||
test('live chunks join only when contiguous and mark a gap otherwise', () {
|
||||
var run = _run('r', 1);
|
||||
run = run.withChunk('abc', 0, 3).withChunk('def', 3, 6);
|
||||
expect(run.output, 'abcdef');
|
||||
expect(run.truncated, isFalse);
|
||||
expect(run.withChunk('abc', 0, 3).output, 'abcdef', reason: 'a repeat');
|
||||
final gapped = run.withChunk('xyz', 10, 13);
|
||||
expect(gapped.output, 'xyz');
|
||||
expect(gapped.truncated, isTrue);
|
||||
final read = ChatShellRun.fromJson({
|
||||
'id': 'r',
|
||||
'status': 'exited',
|
||||
'exit_code': 0,
|
||||
'output': 'tail',
|
||||
'output_start': 6,
|
||||
'output_bytes': 10,
|
||||
})!;
|
||||
expect(read.truncated, isTrue);
|
||||
expect(read.withChunk('more', 10, 14).output, 'tailmore');
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/ui/adaptive/segmented_bar_overflow.dart';
|
||||
|
||||
import 'chat_shell_fixtures.dart';
|
||||
|
||||
/// A desktop keyboard, where Enter sends.
|
||||
final _desktop = TargetPlatformVariant.only(TargetPlatform.linux);
|
||||
|
||||
void main() {
|
||||
late ShellGateway gateway;
|
||||
|
||||
setUp(() => gateway = ShellGateway());
|
||||
tearDown(() => gateway.close());
|
||||
|
||||
final field = find.byKey(const Key('chat-composer-field'));
|
||||
final shellMode = find.byKey(const Key('chat-composer-shell-mode'));
|
||||
|
||||
testWidgets('a leading ! shows Shell mode and runs instead of sending', (
|
||||
tester,
|
||||
) async {
|
||||
await tester.pumpWidget(shellThread(gateway));
|
||||
await tester.pump();
|
||||
|
||||
await tester.enterText(field, 'hello');
|
||||
await tester.pump();
|
||||
expect(shellMode, findsNothing);
|
||||
await tester.enterText(field, '!git status');
|
||||
await tester.pump();
|
||||
expect(shellMode, findsOneWidget);
|
||||
expect(find.text('Shell'), findsOneWidget);
|
||||
expect(find.byTooltip('Run command'), findsOneWidget);
|
||||
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.enter);
|
||||
await tester.pump();
|
||||
await tester.pump();
|
||||
expect(gateway.bodies('chat_shell_run').single['command'], 'git status');
|
||||
expect(gateway.calls.where((c) => c.type == 'chat_prompt'), isEmpty);
|
||||
expect(shellMode, findsNothing, reason: 'the draft was consumed');
|
||||
// The started run is drawn as a card at once.
|
||||
expect(find.text('\$ git status'), findsOneWidget);
|
||||
}, variant: _desktop);
|
||||
|
||||
testWidgets('a refused command keeps the draft and says why', (tester) async {
|
||||
gateway.refusal = 'this chat already runs 4 commands';
|
||||
await tester.pumpWidget(shellThread(gateway));
|
||||
await tester.pump();
|
||||
await tester.enterText(field, '!sleep 9');
|
||||
await tester.pump();
|
||||
await tester.tap(find.byKey(const Key('chat-composer-primary')));
|
||||
await tester.pump();
|
||||
await tester.pump();
|
||||
expect(find.textContaining('already runs 4 commands'), findsOneWidget);
|
||||
expect(shellMode, findsOneWidget);
|
||||
expect(gateway.calls.where((c) => c.type == 'chat_prompt'), isEmpty);
|
||||
});
|
||||
|
||||
testWidgets('a bare ! asks for a command and sends nothing', (tester) async {
|
||||
await tester.pumpWidget(shellThread(gateway));
|
||||
await tester.pump();
|
||||
await tester.enterText(field, '!');
|
||||
await tester.pump();
|
||||
await tester.tap(find.byKey(const Key('chat-composer-primary')));
|
||||
await tester.pump();
|
||||
expect(find.text('Type a command after !.'), findsOneWidget);
|
||||
expect(gateway.bodies('chat_shell_run'), isEmpty);
|
||||
});
|
||||
|
||||
testWidgets('Run command… in the actions menu runs the typed command', (
|
||||
tester,
|
||||
) async {
|
||||
await tester.pumpWidget(shellThread(gateway));
|
||||
await tester.pump();
|
||||
await tester.tap(find.byKey(segmentedMoreKey).hitTestable().first);
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(find.text('Run command…'));
|
||||
await tester.pumpAndSettle();
|
||||
await tester.enterText(
|
||||
find.byKey(const Key('chat-shell-run-field')),
|
||||
' ls -la ',
|
||||
);
|
||||
await tester.tap(find.byKey(const Key('chat-shell-run-confirm')));
|
||||
// The running card's indicator never settles; let the dialog close.
|
||||
await tester.pump();
|
||||
await tester.pump(const Duration(seconds: 1));
|
||||
expect(gateway.bodies('chat_shell_run').single['command'], 'ls -la');
|
||||
expect(find.text('\$ ls -la'), findsOneWidget);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:pipelines_app/core/api_gateway.dart';
|
||||
import 'package:pipelines_app/features/chat/conversation/chat_draft_store.dart';
|
||||
import 'package:pipelines_app/features/chat/conversation/chat_thread_view.dart';
|
||||
|
||||
import '../conversation/chat_thread_fixtures.dart';
|
||||
|
||||
/// A gateway for one open conversation (session `s`) that answers the shell
|
||||
/// command requests from recorded state and lets a test push their frames.
|
||||
class ShellGateway implements ApiGateway {
|
||||
final calls = <({String type, JsonMap fields})>[];
|
||||
final _frames = StreamController<JsonMap>.broadcast();
|
||||
List<JsonMap> runs = const [];
|
||||
String? refusal;
|
||||
int _serial = 0;
|
||||
|
||||
@override
|
||||
Stream<JsonMap> get frames => _frames.stream;
|
||||
|
||||
List<JsonMap> bodies(String type) => [
|
||||
for (final call in calls)
|
||||
if (call.type == type)
|
||||
(call.fields['chat_shell_run_request'] ??
|
||||
call.fields['chat_prompt'] ??
|
||||
call.fields['chat_query'])
|
||||
as JsonMap,
|
||||
];
|
||||
|
||||
@override
|
||||
Future<JsonMap> call(String type, [JsonMap fields = const {}]) async {
|
||||
calls.add((type: type, fields: fields));
|
||||
final body = fields['chat_shell_run_request'] as JsonMap? ?? const {};
|
||||
switch (type) {
|
||||
case 'chat_messages':
|
||||
return chatPage(const []);
|
||||
case 'chat_shell_runs':
|
||||
return {'chat_shell_runs': runs};
|
||||
case 'chat_shell_run':
|
||||
if (refusal != null) throw StateError(refusal!);
|
||||
return {
|
||||
'chat_shell_run': shellRecord(
|
||||
'run_${++_serial}',
|
||||
command: '${body['command']}',
|
||||
),
|
||||
};
|
||||
case 'chat_shell_run_output':
|
||||
return {
|
||||
'chat_shell_run': runs.firstWhere((r) => r['id'] == body['run_id']),
|
||||
};
|
||||
}
|
||||
return <String, dynamic>{};
|
||||
}
|
||||
|
||||
void emitRun(JsonMap run) => _frames.add({
|
||||
'type': 'chat_shell_run_event',
|
||||
'chat_shell_run_event': {
|
||||
'session_id': 's',
|
||||
'run_id': run['id'],
|
||||
'run': run,
|
||||
},
|
||||
});
|
||||
|
||||
void emitOutput(String id, String text, int offset) => _frames.add({
|
||||
'type': 'chat_shell_run_event',
|
||||
'chat_shell_run_event': {
|
||||
'session_id': 's',
|
||||
'run_id': id,
|
||||
'output': text,
|
||||
'offset': offset,
|
||||
'end': offset + text.length,
|
||||
},
|
||||
});
|
||||
|
||||
void close() => _frames.close();
|
||||
}
|
||||
|
||||
JsonMap shellRecord(
|
||||
String id, {
|
||||
String command = 'make test',
|
||||
String status = 'running',
|
||||
int? exitCode,
|
||||
String output = '',
|
||||
int durationMs = 0,
|
||||
}) => {
|
||||
'id': id,
|
||||
'session_id': 's',
|
||||
'command': command,
|
||||
'directory': '/home/op/project',
|
||||
'status': status,
|
||||
'exit_code': ?exitCode,
|
||||
'started_at': 1000,
|
||||
'ended_at': ?(status == 'running' ? null : 1000 + durationMs),
|
||||
'duration_ms': durationMs,
|
||||
'output': output,
|
||||
'output_bytes': output.length,
|
||||
};
|
||||
|
||||
Widget shellThread(ShellGateway gateway, {bool cupertino = false}) {
|
||||
final thread = ChatThreadView(
|
||||
gateway: gateway,
|
||||
sessionId: 's',
|
||||
syncToken: 0,
|
||||
onBack: () {},
|
||||
drafts: MemoryChatDraftStore(),
|
||||
);
|
||||
return cupertino
|
||||
? CupertinoApp(home: CupertinoPageScaffold(child: thread))
|
||||
: MaterialApp(home: Scaffold(body: thread));
|
||||
}
|
||||
@@ -0,0 +1,157 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
import 'chat_shell_fixtures.dart';
|
||||
|
||||
void main() {
|
||||
late ShellGateway gateway;
|
||||
|
||||
setUp(() => gateway = ShellGateway());
|
||||
tearDown(() => gateway.close());
|
||||
|
||||
Finder card() => find.bySemanticsIdentifier('chat-shell-run-card');
|
||||
|
||||
for (final cupertino in [false, true]) {
|
||||
final host = cupertino ? 'Cupertino' : 'Material';
|
||||
|
||||
testWidgets('$host: a running command streams output and stops', (
|
||||
tester,
|
||||
) async {
|
||||
gateway.runs = [shellRecord('run_a', output: 'line 1\n')];
|
||||
await tester.pumpWidget(shellThread(gateway, cupertino: cupertino));
|
||||
await tester.pump();
|
||||
|
||||
expect(card(), findsOneWidget);
|
||||
expect(find.text('\$ make test'), findsOneWidget);
|
||||
expect(find.byKey(const Key('chat-shell-run-running')), findsOneWidget);
|
||||
gateway.emitOutput('run_a', 'line 2\n', 7);
|
||||
await tester.pump();
|
||||
expect(find.text('line 1\nline 2\n'), findsOneWidget);
|
||||
expect(find.byKey(const Key('chat-shell-run-share')), findsNothing);
|
||||
|
||||
await tester.tap(find.byKey(const Key('chat-shell-run-stop')));
|
||||
await tester.pump();
|
||||
expect(gateway.bodies('chat_shell_stop').single['run_id'], 'run_a');
|
||||
expect(find.text('Stopping…'), findsOneWidget);
|
||||
|
||||
gateway.emitRun(
|
||||
shellRecord('run_a', status: 'stopped', durationMs: 2400),
|
||||
);
|
||||
await tester.pump();
|
||||
expect(find.byKey(const Key('chat-shell-run-running')), findsNothing);
|
||||
expect(find.text('Stopped · 2.4 s'), findsOneWidget);
|
||||
expect(find.byKey(const Key('chat-shell-run-stop')), findsNothing);
|
||||
// The streamed output stays after the lifecycle frame.
|
||||
expect(find.text('line 1\nline 2\n'), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('$host: finished commands show their exit status', (
|
||||
tester,
|
||||
) async {
|
||||
gateway.runs = [
|
||||
shellRecord(
|
||||
'run_ok',
|
||||
command: 'true',
|
||||
status: 'exited',
|
||||
exitCode: 0,
|
||||
output: 'fine\n',
|
||||
durationMs: 120,
|
||||
),
|
||||
shellRecord(
|
||||
'run_bad',
|
||||
command: 'false',
|
||||
status: 'exited',
|
||||
exitCode: 1,
|
||||
output: 'broken\n',
|
||||
durationMs: 3000,
|
||||
),
|
||||
];
|
||||
await tester.pumpWidget(shellThread(gateway, cupertino: cupertino));
|
||||
await tester.pump();
|
||||
|
||||
expect(card(), findsNWidgets(2));
|
||||
expect(find.text('Exit 0 · 120 ms'), findsOneWidget);
|
||||
expect(find.text('Exit 1 · 3.0 s'), findsOneWidget);
|
||||
expect(find.byKey(const Key('chat-shell-run-copy')), findsNWidgets(2));
|
||||
});
|
||||
|
||||
testWidgets('$host: Send to agent shares the run once', (tester) async {
|
||||
gateway.runs = [
|
||||
shellRecord('run_a', status: 'exited', exitCode: 0, output: 'ok\n'),
|
||||
];
|
||||
await tester.pumpWidget(shellThread(gateway, cupertino: cupertino));
|
||||
await tester.pump();
|
||||
|
||||
expect(gateway.calls.where((c) => c.type == 'chat_prompt'), isEmpty);
|
||||
await tester.tap(find.byKey(const Key('chat-shell-run-share')));
|
||||
await tester.pump();
|
||||
expect(gateway.bodies('chat_shell_share').single['run_id'], 'run_a');
|
||||
expect(find.text('Sent to agent'), findsOneWidget);
|
||||
await tester.tap(find.byKey(const Key('chat-shell-run-share')));
|
||||
await tester.pump();
|
||||
expect(gateway.bodies('chat_shell_share'), hasLength(1));
|
||||
});
|
||||
}
|
||||
|
||||
testWidgets('Copy output copies the retained output', (tester) async {
|
||||
final copied = <String>[];
|
||||
tester.binding.defaultBinaryMessenger.setMockMethodCallHandler(
|
||||
SystemChannels.platform,
|
||||
(call) async {
|
||||
if (call.method == 'Clipboard.setData') {
|
||||
copied.add('${(call.arguments as Map)['text']}');
|
||||
}
|
||||
return null;
|
||||
},
|
||||
);
|
||||
addTearDown(
|
||||
() => tester.binding.defaultBinaryMessenger.setMockMethodCallHandler(
|
||||
SystemChannels.platform,
|
||||
null,
|
||||
),
|
||||
);
|
||||
gateway.runs = [
|
||||
shellRecord('run_a', status: 'exited', exitCode: 0, output: 'a\nb\n'),
|
||||
];
|
||||
await tester.pumpWidget(shellThread(gateway));
|
||||
await tester.pump();
|
||||
await tester.tap(find.byKey(const Key('chat-shell-run-copy')));
|
||||
await tester.pump();
|
||||
expect(copied, ['a\nb\n']);
|
||||
expect(find.text('Copied'), findsOneWidget);
|
||||
await tester.pump(const Duration(seconds: 3));
|
||||
});
|
||||
|
||||
testWidgets('a large text scale keeps the card readable', (tester) async {
|
||||
gateway.runs = [
|
||||
shellRecord(
|
||||
'run_a',
|
||||
status: 'exited',
|
||||
exitCode: 2,
|
||||
output: List.generate(80, (i) => 'line $i').join('\n'),
|
||||
),
|
||||
];
|
||||
tester.view.physicalSize = const Size(400, 900);
|
||||
tester.view.devicePixelRatio = 1;
|
||||
addTearDown(tester.view.reset);
|
||||
await tester.pumpWidget(
|
||||
MediaQuery(
|
||||
data: const MediaQueryData(
|
||||
size: Size(400, 900),
|
||||
textScaler: TextScaler.linear(2),
|
||||
),
|
||||
child: shellThread(gateway),
|
||||
),
|
||||
);
|
||||
await tester.pump();
|
||||
expect(tester.takeException(), isNull);
|
||||
expect(find.textContaining('Exit 2'), findsOneWidget);
|
||||
final expand = find.byKey(const Key('chat-shell-run-expand'));
|
||||
await tester.ensureVisible(expand);
|
||||
await tester.pump();
|
||||
await tester.tap(expand);
|
||||
await tester.pump();
|
||||
expect(find.text('Collapse'), findsOneWidget);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user