// Server-shaped `kind: chat` search replies over the `?chats=N` sessions // (tool/perf/perf_fixture.dart), so Search can show and open chat hits in // captures. Without `?chats=` there are no chats and no hits. import 'package:pipelines_app/core/api_gateway.dart'; import 'perf_fixture.dart'; /// Hits the way `internal/server/search_chats.go` answers: a title or /// directory match from the registry row, else the newest transcript message /// whose text matches, with a snippet around the match. One hit per chat, best /// match first. A blank query lists every chat. List perfChatSearchHits(String query) { final needle = query.trim().toLowerCase(); final hits = []; for (final row in perfChatRows()) { final title = '${row['title'] ?? ''}'; final directory = '${row['directory'] ?? ''}'; final chat = { 'session_id': row['id'], 'title': title, 'directory': directory, 'backend': row['backend'] ?? 'opencode', 'updated_at': ?row['updated_at'], }; String field; int score; if (needle.isEmpty) { field = ''; score = 0; } else if (title.toLowerCase().contains(needle)) { field = 'title'; score = 3; } else if (directory.toLowerCase().contains(needle)) { field = 'directory'; score = 2; } else { final match = _newestMatch(needle); if (match == null) continue; field = 'message'; score = 1; chat ..['message_id'] = 'perf-m${match.index}' ..['role'] = match.index.isEven ? 'user' : 'assistant' ..['snippet'] = match.snippet; } final updated = row['updated_at']; hits.add({ 'kind': 'chat', 'target': 'chat:${row['id']}', 'score': score, 'matched_field': field, if (updated is int) 'recency': DateTime.fromMillisecondsSinceEpoch( updated, isUtc: true, ).toIso8601String(), 'chat': chat, }); } hits.sort((a, b) => (b['score'] as int).compareTo(a['score'] as int)); return hits; } ({int index, String snippet})? _newestMatch(String needle) { for (var m = 19; m >= 0; m--) { final line = perfChatMessageText(m).replaceAll(RegExp(r'\s+'), ' '); final at = line.toLowerCase().indexOf(needle); if (at < 0) continue; final start = at - 40 < 0 ? 0 : at - 40; final end = at + needle.length + 40 > line.length ? line.length : at + needle.length + 40; final snippet = '${start > 0 ? '…' : ''}${line.substring(start, end)}' '${end < line.length ? '…' : ''}'; return (index: m, snippet: snippet); } return null; }