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 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 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 chatFindUnits(List rows, List keys) { final units = []; 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 _textParts(Map 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 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 chatFindMatches( List units, String query, { required bool caseSensitive, int cap = chatFindLocalCap, }) { final matches = []; 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;