8bd219e636
A find bar over the transcript: query, Match case, n of N (counted from the newest), Enter for an older match and Shift+Enter for a newer one. Matches are painted over the rendered text, code blocks included, without restyling it; the current match is scrolled into view. Matches in history not loaded yet come from the server's chat:<id>-scoped search and are reached by loading older pages. Esc closes and returns to the composer. The key is the pane's, so a focused terminal keeps Ctrl+F. Fixture answers scoped searches for captures; cheatsheet lists the key. The scoped search matches the query as typed, spaces included, like the client. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
147 lines
4.6 KiB
Dart
147 lines
4.6 KiB
Dart
import '../../../core/api_gateway.dart';
|
|
|
|
/// One piece of transcript text find searches, as the transcript shows it:
|
|
/// a user turn's text (its text parts joined by line breaks, as its bubble
|
|
/// draws them) or one text part of an answer. [rowKey] is the transcript
|
|
/// row's stable key; [text] is exactly the string the row renders.
|
|
class ChatFindUnit {
|
|
const ChatFindUnit(this.rowIndex, this.rowKey, this.text, this.messageIds);
|
|
|
|
final int rowIndex;
|
|
final String rowKey;
|
|
final String text;
|
|
|
|
/// The stored messages the row holds.
|
|
final List<String> messageIds;
|
|
}
|
|
|
|
/// A match's character range in its unit's text.
|
|
typedef ChatFindRange = ({int start, int end});
|
|
|
|
/// One match in the loaded transcript.
|
|
class ChatFindMatch {
|
|
const ChatFindMatch(this.unit, this.start, this.end, this.ordinal);
|
|
|
|
final ChatFindUnit unit;
|
|
final int start;
|
|
final int end;
|
|
|
|
/// Which occurrence in [unit] this is, from its start.
|
|
final int ordinal;
|
|
|
|
String get rowKey => unit.rowKey;
|
|
|
|
/// Whether [other] is the same occurrence in the same text.
|
|
bool sameAs(ChatFindMatch other) =>
|
|
other.rowKey == rowKey &&
|
|
other.unit.text == unit.text &&
|
|
other.ordinal == ordinal;
|
|
}
|
|
|
|
/// A match the server found in a message the transcript has not loaded yet.
|
|
class ChatFindRemoteHit {
|
|
const ChatFindRemoteHit(this.messageId, this.part, this.offset);
|
|
|
|
final String messageId;
|
|
final int part;
|
|
final int offset;
|
|
|
|
/// The hits of a scoped chat search reply, newest message first.
|
|
static List<ChatFindRemoteHit> fromReply(JsonMap reply) {
|
|
final results = reply['search_results'];
|
|
final list = results is Map ? results['results'] : null;
|
|
if (list is! List) return const [];
|
|
return [
|
|
for (final result in list)
|
|
if (result is Map && result['chat'] is Map)
|
|
ChatFindRemoteHit(
|
|
'${(result['chat'] as Map)['message_id'] ?? ''}',
|
|
_int((result['chat'] as Map)['part']),
|
|
_int((result['chat'] as Map)['offset']),
|
|
),
|
|
];
|
|
}
|
|
|
|
static int _int(Object? value) => value is num ? value.toInt() : 0;
|
|
}
|
|
|
|
/// The text units of transcript [rows] (keyed by [keys]), in display order.
|
|
/// Only what reads as conversation is searched: user turns and the text of
|
|
/// answers, not activity, cards or notices.
|
|
List<ChatFindUnit> chatFindUnits(List<JsonMap> rows, List<String> keys) {
|
|
final units = <ChatFindUnit>[];
|
|
for (var index = 0; index < rows.length && index < keys.length; index++) {
|
|
final row = rows[index];
|
|
final kind = row['kind'];
|
|
if (kind == 'user') {
|
|
final message = row['message'] is Map ? row['message'] as Map : row;
|
|
final text = _textParts(message).join('\n');
|
|
if (text.trim().isEmpty) continue;
|
|
final id = '${row['key'] ?? message['id'] ?? ''}';
|
|
units.add(ChatFindUnit(index, keys[index], text, [id]));
|
|
} else if (kind == 'assistant') {
|
|
final ids = [
|
|
for (final id in row['messageIds'] as List? ?? const []) '$id',
|
|
];
|
|
for (final text in _textParts(row)) {
|
|
if (text.trim().isEmpty) continue;
|
|
units.add(ChatFindUnit(index, keys[index], text, ids));
|
|
}
|
|
}
|
|
}
|
|
return units;
|
|
}
|
|
|
|
List<String> _textParts(Map<dynamic, dynamic> source) {
|
|
final parts = source['parts'];
|
|
if (parts is! List) return const [];
|
|
return [
|
|
for (final part in parts)
|
|
if (part is Map &&
|
|
(part['type'] == null || part['type'] == 'text') &&
|
|
part['text'] is String)
|
|
part['text'] as String,
|
|
];
|
|
}
|
|
|
|
/// The non-overlapping occurrences of [query] in [text].
|
|
List<ChatFindRange> chatFindRanges(
|
|
String text,
|
|
String query, {
|
|
required bool caseSensitive,
|
|
}) {
|
|
if (query.isEmpty || text.length < query.length) return const [];
|
|
final pattern = RegExp(RegExp.escape(query), caseSensitive: caseSensitive);
|
|
return [
|
|
for (final match in pattern.allMatches(text))
|
|
(start: match.start, end: match.end),
|
|
];
|
|
}
|
|
|
|
/// Every match of [query] in [units], in display order (oldest first).
|
|
List<ChatFindMatch> chatFindMatches(
|
|
List<ChatFindUnit> units,
|
|
String query, {
|
|
required bool caseSensitive,
|
|
int cap = chatFindLocalCap,
|
|
}) {
|
|
final matches = <ChatFindMatch>[];
|
|
for (final unit in units) {
|
|
var ordinal = 0;
|
|
for (final range in chatFindRanges(
|
|
unit.text,
|
|
query,
|
|
caseSensitive: caseSensitive,
|
|
)) {
|
|
matches.add(ChatFindMatch(unit, range.start, range.end, ordinal++));
|
|
if (matches.length >= cap) return matches;
|
|
}
|
|
}
|
|
return matches;
|
|
}
|
|
|
|
/// How many matches find counts in the loaded transcript, and in history
|
|
/// it has not loaded, before it reports "N+".
|
|
const chatFindLocalCap = 2000;
|
|
const chatFindRemoteCap = 1000;
|