Merge: search finds chats and opens them
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
// 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<JsonMap> perfChatSearchHits(String query) {
|
||||
final needle = query.trim().toLowerCase();
|
||||
final hits = <JsonMap>[];
|
||||
for (final row in perfChatRows()) {
|
||||
final title = '${row['title'] ?? ''}';
|
||||
final directory = '${row['directory'] ?? ''}';
|
||||
final chat = <String, dynamic>{
|
||||
'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;
|
||||
}
|
||||
@@ -264,3 +264,10 @@ JsonMap? perfChatReply(JsonMap request) {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// The `?chats=N` list rows, as chat_sessions serves them.
|
||||
List<JsonMap> perfChatRows() => [for (var i = 0; i < _chats; i++) _chatRow(i)];
|
||||
|
||||
/// The text of message `perf-m<m>` in the bench's short transcript, newest
|
||||
/// last (20 messages).
|
||||
String perfChatMessageText(int m) => m.isEven ? 'Question $m' : _answer(m);
|
||||
|
||||
@@ -28,6 +28,7 @@ import 'package:web/web.dart' as web;
|
||||
import 'package:web_socket_channel/web_socket_channel.dart';
|
||||
|
||||
import 'perf/backdrop_census.dart';
|
||||
import 'perf/chat_search_fixture.dart';
|
||||
import 'perf/chat_scroll_fixture.dart';
|
||||
import 'perf/chat_scroll_probe.dart';
|
||||
import 'perf/frame_log.dart';
|
||||
@@ -449,6 +450,24 @@ JsonMap? probeReply(JsonMap request) {
|
||||
}
|
||||
final query = (search['query'] as String).trim().toLowerCase();
|
||||
final kind = search['kind'] as String;
|
||||
if (kind == 'chat') {
|
||||
// The server answers chats as their own request (search_chats.go).
|
||||
return {
|
||||
'id': id,
|
||||
'type': 'search_results',
|
||||
'search_results': {
|
||||
'query': search['query'],
|
||||
'kind': kind,
|
||||
'results': perfChatSearchHits(query),
|
||||
'truncated': false,
|
||||
'scanned_steps': 0,
|
||||
'scanned_bytes': 0,
|
||||
'cap': 50,
|
||||
'scan_bound_bytes': 1048576,
|
||||
'scanned_chats': 3,
|
||||
},
|
||||
};
|
||||
}
|
||||
final results = [
|
||||
for (final entry in probeHits)
|
||||
if ((kind.isEmpty || entry.hit['kind'] == kind) &&
|
||||
|
||||
Reference in New Issue
Block a user