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.
66 lines
2.2 KiB
Dart
66 lines
2.2 KiB
Dart
import '../../../core/api_gateway.dart';
|
|
import 'chat_shell_run.dart';
|
|
|
|
/// The row field that carries an answer's dismissed command ids, oldest
|
|
/// first: what its footer's "N commands" chip lists.
|
|
const String chatShellHistoryField = 'shellHistory';
|
|
|
|
/// The transcript row kind of the history chip a chat with no answer shows.
|
|
const String chatShellHistoryRowKind = 'shell_history';
|
|
|
|
/// Attaches each dismissed run to an answer's footer as [chatShellHistoryField]:
|
|
/// the first assistant reply created at or after the run started, or the last
|
|
/// reply for a run newer than every reply. A reply without a recorded time is
|
|
/// only chosen as that last reply. With no assistant reply at all, one
|
|
/// [chatShellHistoryRowKind] row at the end holds every run.
|
|
List<JsonMap> attachChatShellHistory(
|
|
List<JsonMap> rows,
|
|
List<ChatShellRun> dismissed,
|
|
) {
|
|
if (dismissed.isEmpty) return rows;
|
|
final replies = [
|
|
for (var i = 0; i < rows.length; i++)
|
|
if (rows[i]['kind'] == 'assistant') i,
|
|
];
|
|
final ordered = [...dismissed]
|
|
..sort((a, b) => a.startedAt.compareTo(b.startedAt));
|
|
if (replies.isEmpty) {
|
|
return [
|
|
...rows,
|
|
{
|
|
'kind': chatShellHistoryRowKind,
|
|
'key': 'chat-shell-history',
|
|
chatShellHistoryField: [for (final run in ordered) run.id],
|
|
},
|
|
];
|
|
}
|
|
final assigned = <int, List<String>>{};
|
|
for (final run in ordered) {
|
|
final index = replies.firstWhere((i) {
|
|
final created = rows[i]['created_at'];
|
|
return created is num && created >= run.startedAt;
|
|
}, orElse: () => replies.last);
|
|
(assigned[index] ??= []).add(run.id);
|
|
}
|
|
return [
|
|
for (var i = 0; i < rows.length; i++)
|
|
if (assigned[i] case final ids?)
|
|
{...rows[i], chatShellHistoryField: ids}
|
|
else
|
|
rows[i],
|
|
];
|
|
}
|
|
|
|
/// The dismissed command ids [row] carries, oldest first.
|
|
List<String> chatShellHistoryIds(JsonMap row) => [
|
|
for (final id in row[chatShellHistoryField] as List? ?? const []) '$id',
|
|
];
|
|
|
|
/// The chip's label, e.g. `2 commands`.
|
|
String chatShellHistoryLabel(int count) =>
|
|
count == 1 ? '1 command' : '$count commands';
|
|
|
|
/// The chip's accessible name.
|
|
String chatShellHistorySemantics(int count) =>
|
|
'${chatShellHistoryLabel(count)} dismissed, open command history';
|