Show dismissed commands as an answer footer chip; move Copy to the left gutter
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.
This commit is contained in:
@@ -16,10 +16,15 @@ class ShellGateway implements ApiGateway {
|
||||
final calls = <({String type, JsonMap fields})>[];
|
||||
final _frames = StreamController<JsonMap>.broadcast();
|
||||
List<JsonMap> runs = const [];
|
||||
|
||||
/// The chat history `chat_messages` answers with.
|
||||
List<JsonMap> messages = const [];
|
||||
String? refusal;
|
||||
|
||||
/// Tombstone ids the `chat_shell_runs` readback reports.
|
||||
/// Dismissed ids the `chat_shell_runs` readback reports, and the dismissed
|
||||
/// runs it carries (a server that keeps them).
|
||||
List<String> dismissed = const [];
|
||||
List<JsonMap> dismissedRuns = const [];
|
||||
|
||||
/// The refusal of `chat_shell_dismiss` / `chat_shell_restore`, when set.
|
||||
String? dismissRefusal;
|
||||
@@ -47,9 +52,13 @@ class ShellGateway implements ApiGateway {
|
||||
final body = fields['chat_shell_run_request'] as JsonMap? ?? const {};
|
||||
switch (type) {
|
||||
case 'chat_messages':
|
||||
return chatPage(const []);
|
||||
return chatPage(messages);
|
||||
case 'chat_shell_runs':
|
||||
return {'chat_shell_runs': runs, 'chat_shell_dismissed': dismissed};
|
||||
return {
|
||||
'chat_shell_runs': runs,
|
||||
'chat_shell_dismissed': dismissed,
|
||||
'chat_shell_dismissed_runs': dismissedRuns,
|
||||
};
|
||||
case 'chat_shell_dismiss':
|
||||
if (dismissRefusal != null) throw StateError(dismissRefusal!);
|
||||
return {'chat_shell_dismissed': body['run_ids']};
|
||||
@@ -58,7 +67,7 @@ class ShellGateway implements ApiGateway {
|
||||
final ids = (body['run_ids'] as List).toSet();
|
||||
return {
|
||||
'chat_shell_runs': [
|
||||
for (final run in runs)
|
||||
for (final run in [...runs, ...dismissedRuns])
|
||||
if (ids.contains(run['id'])) run,
|
||||
],
|
||||
};
|
||||
@@ -75,7 +84,10 @@ class ShellGateway implements ApiGateway {
|
||||
return {'chat_shell_run': resumeReply};
|
||||
}
|
||||
return {
|
||||
'chat_shell_run': runs.firstWhere((r) => r['id'] == body['run_id']),
|
||||
'chat_shell_run': [
|
||||
...runs,
|
||||
...dismissedRuns,
|
||||
].firstWhere((r) => r['id'] == body['run_id']),
|
||||
};
|
||||
}
|
||||
return <String, dynamic>{};
|
||||
@@ -123,6 +135,7 @@ JsonMap shellRecord(
|
||||
bool terminal = false,
|
||||
String text = '',
|
||||
int outputStart = 0,
|
||||
int startedAt = 1000,
|
||||
}) => {
|
||||
'id': id,
|
||||
'session_id': 's',
|
||||
@@ -130,8 +143,8 @@ JsonMap shellRecord(
|
||||
'directory': '/home/op/project',
|
||||
'status': status,
|
||||
'exit_code': ?exitCode,
|
||||
'started_at': 1000,
|
||||
'ended_at': ?(status == 'running' ? null : 1000 + durationMs),
|
||||
'started_at': startedAt,
|
||||
'ended_at': ?(status == 'running' ? null : startedAt + durationMs),
|
||||
'duration_ms': durationMs,
|
||||
'output': output,
|
||||
'output_start': outputStart,
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/core/api_gateway.dart';
|
||||
import 'package:pipelines_app/features/chat/shell/chat_shell_history.dart';
|
||||
import 'package:pipelines_app/features/chat/shell/chat_shell_run.dart';
|
||||
|
||||
ChatShellRun _run(String id, int started) => ChatShellRun(
|
||||
id: id,
|
||||
command: 'echo $id',
|
||||
directory: '/w',
|
||||
status: 'exited',
|
||||
exitCode: 0,
|
||||
startedAt: started,
|
||||
);
|
||||
|
||||
JsonMap _reply(String id, int? created) => {
|
||||
'kind': 'assistant',
|
||||
'key': id,
|
||||
'created_at': ?created,
|
||||
};
|
||||
|
||||
void main() {
|
||||
test('each run attaches to the first reply at or after its start', () {
|
||||
final rows = [
|
||||
{'kind': 'user', 'created_at': 5},
|
||||
_reply('a', 10),
|
||||
{'kind': 'user', 'created_at': 15},
|
||||
_reply('b', 20),
|
||||
_reply('c', 30),
|
||||
];
|
||||
final attached = attachChatShellHistory(rows, [
|
||||
_run('late', 31),
|
||||
_run('before', 1),
|
||||
_run('exact', 20),
|
||||
_run('between', 12),
|
||||
]);
|
||||
expect(chatShellHistoryIds(attached[1]), ['before']);
|
||||
expect(chatShellHistoryIds(attached[3]), ['between', 'exact']);
|
||||
// Newer than every reply: the last reply.
|
||||
expect(chatShellHistoryIds(attached[4]), ['late']);
|
||||
expect(attached[0].containsKey(chatShellHistoryField), isFalse);
|
||||
expect(attached[2].containsKey(chatShellHistoryField), isFalse);
|
||||
expect(attached, hasLength(rows.length));
|
||||
});
|
||||
|
||||
test('a reply without a time is only the fallback last reply', () {
|
||||
final rows = [_reply('a', 10), _reply('pending', null)];
|
||||
final attached = attachChatShellHistory(rows, [
|
||||
_run('x', 5),
|
||||
_run('y', 50),
|
||||
]);
|
||||
expect(chatShellHistoryIds(attached[0]), ['x']);
|
||||
expect(chatShellHistoryIds(attached[1]), ['y']);
|
||||
});
|
||||
|
||||
test('with no reply the runs get their own row at the end', () {
|
||||
final rows = [
|
||||
{'kind': 'user', 'created_at': 5},
|
||||
];
|
||||
final attached = attachChatShellHistory(rows, [_run('b', 9), _run('a', 1)]);
|
||||
expect(attached, hasLength(2));
|
||||
expect(attached.last['kind'], chatShellHistoryRowKind);
|
||||
expect(chatShellHistoryIds(attached.last), ['a', 'b']);
|
||||
expect(attachChatShellHistory(rows, const []), same(rows));
|
||||
});
|
||||
|
||||
test('labels count the commands', () {
|
||||
expect(chatShellHistoryLabel(1), '1 command');
|
||||
expect(chatShellHistoryLabel(2), '2 commands');
|
||||
expect(chatShellHistorySemantics(2), contains('open command history'));
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,256 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/core/api_gateway.dart';
|
||||
import 'package:pipelines_app/features/chat/ui/transcript/chat_message_view.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');
|
||||
final chip = find.byKey(const Key('chat-shell-history-chip'));
|
||||
|
||||
JsonMap dismissed(String id, int started, {String command = 'true'}) => {
|
||||
...shellRecord(
|
||||
id,
|
||||
command: command,
|
||||
status: 'exited',
|
||||
exitCode: 0,
|
||||
output: 'out $id\r\n',
|
||||
text: 'out $id\n',
|
||||
durationMs: 50,
|
||||
startedAt: started,
|
||||
),
|
||||
'dismissed': true,
|
||||
'dismissed_at': started + 100,
|
||||
};
|
||||
|
||||
JsonMap reply(String id, int created, String text) => {
|
||||
'id': id,
|
||||
'role': 'assistant',
|
||||
'created_at': created,
|
||||
'parts': [
|
||||
{'type': 'text', 'text': text},
|
||||
],
|
||||
};
|
||||
|
||||
void size(WidgetTester tester, Size size) {
|
||||
tester.view.physicalSize = size;
|
||||
tester.view.devicePixelRatio = 1;
|
||||
addTearDown(tester.view.resetPhysicalSize);
|
||||
addTearDown(tester.view.resetDevicePixelRatio);
|
||||
}
|
||||
|
||||
Future<void> open(WidgetTester tester) async {
|
||||
await tester.pumpWidget(shellThread(gateway));
|
||||
await tester.pump();
|
||||
await tester.pump();
|
||||
}
|
||||
|
||||
void history(List<JsonMap> runs) {
|
||||
gateway.dismissedRuns = runs;
|
||||
gateway.dismissed = [for (final run in runs) '${run['id']}'];
|
||||
}
|
||||
|
||||
testWidgets('dismissed runs sit in the footer of the reply that followed', (
|
||||
tester,
|
||||
) async {
|
||||
size(tester, const Size(1400, 900));
|
||||
gateway.messages = [
|
||||
reply('a1', 10000, 'First answer'),
|
||||
{
|
||||
'id': 'u2',
|
||||
'role': 'user',
|
||||
'created_at': 11000,
|
||||
'parts': [
|
||||
{'type': 'text', 'text': 'Next question'},
|
||||
],
|
||||
},
|
||||
reply('a2', 20000, 'Second answer'),
|
||||
];
|
||||
history([
|
||||
dismissed('run_a', 12000, command: 'ls'),
|
||||
dismissed('run_b', 15000, command: 'pwd'),
|
||||
]);
|
||||
await open(tester);
|
||||
|
||||
expect(card(), findsNothing);
|
||||
expect(chip, findsOneWidget);
|
||||
expect(find.text('2 commands'), findsOneWidget);
|
||||
// The chip is in the second answer's footer, beside its other chips.
|
||||
final footer = find.ancestor(
|
||||
of: chip,
|
||||
matching: find.byKey(const Key('chat-message-footer')),
|
||||
);
|
||||
expect(footer, findsOneWidget);
|
||||
final message = find.ancestor(
|
||||
of: chip,
|
||||
matching: find.byType(ChatMessageView),
|
||||
);
|
||||
expect(
|
||||
find.descendant(of: message, matching: find.text('Second answer')),
|
||||
findsOneWidget,
|
||||
);
|
||||
expect(
|
||||
find.descendant(of: message, matching: find.text('First answer')),
|
||||
findsNothing,
|
||||
);
|
||||
});
|
||||
|
||||
testWidgets('a chat with no answer shows the chip on its own row', (
|
||||
tester,
|
||||
) async {
|
||||
history([dismissed('run_a', 1000)]);
|
||||
await open(tester);
|
||||
expect(find.text('1 command'), findsOneWidget);
|
||||
expect(
|
||||
find.ancestor(
|
||||
of: chip,
|
||||
matching: find.byKey(const Key('chat-message-footer')),
|
||||
),
|
||||
findsNothing,
|
||||
);
|
||||
});
|
||||
|
||||
testWidgets('dismissing a card moves it into the chip', (tester) async {
|
||||
gateway.runs = [
|
||||
shellRecord('run_done', status: 'exited', exitCode: 0, durationMs: 9),
|
||||
];
|
||||
await open(tester);
|
||||
expect(chip, findsNothing);
|
||||
await tester.tap(find.byKey(const Key('chat-shell-run-dismiss')));
|
||||
await tester.pump();
|
||||
expect(card(), findsNothing);
|
||||
expect(find.text('1 command'), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('wide: the sheet lists commands, opens output and restores', (
|
||||
tester,
|
||||
) async {
|
||||
size(tester, const Size(1400, 900));
|
||||
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.messages = [reply('a1', 10000, 'Answer')];
|
||||
history([dismissed('run_a', 5000, command: 'git status')]);
|
||||
await open(tester);
|
||||
|
||||
await tester.tap(chip);
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.byType(BottomSheet), findsOneWidget);
|
||||
expect(find.text('Dismissed commands'), findsOneWidget);
|
||||
expect(find.text('\$ git status'), findsOneWidget);
|
||||
expect(find.text('Exit 0 · 50 ms'), findsOneWidget);
|
||||
|
||||
// Its output, read-only, with Copy output and Send to agent.
|
||||
await tester.tap(find.byKey(const ValueKey('chat-shell-history-run_a')));
|
||||
await tester.pumpAndSettle();
|
||||
expect(gateway.bodies('chat_shell_run_output').last['run_id'], 'run_a');
|
||||
expect(terminalText(tester), 'out run_a');
|
||||
await tester.tap(find.byKey(const Key('chat-shell-history-copy')));
|
||||
await tester.pump();
|
||||
expect(copied, ['out run_a\n']);
|
||||
await tester.tap(find.byKey(const Key('chat-shell-history-share')));
|
||||
await tester.pump();
|
||||
expect(gateway.bodies('chat_shell_share').single['run_id'], 'run_a');
|
||||
await tester.pageBack();
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
// Restore puts the card back and the chip goes.
|
||||
await tester.tap(
|
||||
find.byKey(const ValueKey('chat-shell-history-restore-run_a')),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
expect(gateway.bodies('chat_shell_restore').single['run_ids'], ['run_a']);
|
||||
expect(find.textContaining('restored to the transcript'), findsOneWidget);
|
||||
Navigator.of(tester.element(find.byType(BottomSheet))).pop();
|
||||
await tester.pumpAndSettle();
|
||||
expect(card(), findsOneWidget);
|
||||
expect(chip, findsNothing);
|
||||
});
|
||||
|
||||
testWidgets('narrow: the history opens as a full-screen page', (
|
||||
tester,
|
||||
) async {
|
||||
size(tester, const Size(400, 800));
|
||||
gateway.messages = [reply('a1', 10000, 'Answer')];
|
||||
history([dismissed('run_a', 5000)]);
|
||||
await open(tester);
|
||||
await tester.tap(chip);
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.byType(BottomSheet), findsNothing);
|
||||
expect(
|
||||
find.descendant(
|
||||
of: find.byType(AppBar),
|
||||
matching: find.text('Dismissed commands'),
|
||||
),
|
||||
findsOneWidget,
|
||||
);
|
||||
await tester.tap(
|
||||
find.byKey(const ValueKey('chat-shell-history-restore-run_a')),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
expect(gateway.bodies('chat_shell_restore').single['run_ids'], ['run_a']);
|
||||
});
|
||||
|
||||
testWidgets('a run an older server purged says its output was not kept', (
|
||||
tester,
|
||||
) async {
|
||||
size(tester, const Size(1400, 900));
|
||||
history([
|
||||
{
|
||||
'id': 'run_old',
|
||||
'session_id': 's',
|
||||
'command': '',
|
||||
'directory': '/w',
|
||||
'status': 'exited',
|
||||
'exit_code': 0,
|
||||
'started_at': 1000,
|
||||
'dismissed': true,
|
||||
'purged': true,
|
||||
},
|
||||
]);
|
||||
await open(tester);
|
||||
await tester.tap(chip);
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.text('Command not kept'), findsOneWidget);
|
||||
expect(find.text('Output not kept'), findsOneWidget);
|
||||
expect(
|
||||
find.byKey(const ValueKey('chat-shell-history-restore-run_old')),
|
||||
findsNothing,
|
||||
);
|
||||
});
|
||||
|
||||
testWidgets('a refused restore shows the reason in the list', (tester) async {
|
||||
size(tester, const Size(1400, 900));
|
||||
gateway.restoreRefusal = 'these commands can no longer be restored';
|
||||
history([dismissed('run_a', 1000)]);
|
||||
await open(tester);
|
||||
await tester.tap(chip);
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(
|
||||
find.byKey(const ValueKey('chat-shell-history-restore-run_a')),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.byKey(const Key('chat-shell-history-refusal')), findsOneWidget);
|
||||
expect(find.textContaining('no longer be restored'), findsOneWidget);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user