6ca415c134
Chat hits sort by the chat's last use whatever field matched, and run, step, field, artifact and pipeline hits sort by their own date, before the result cap applies so the cap drops the oldest. Each kind's row scan also walks newest first. Score still reports the match tier. The app sorts both groups newest first as well.
298 lines
9.4 KiB
Dart
298 lines
9.4 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
import '../../../core/api_gateway.dart';
|
|
import '../data/search_repository.dart';
|
|
import 'search_chat_slice.dart';
|
|
import 'search_models.dart';
|
|
import 'search_order.dart';
|
|
import 'search_query.dart';
|
|
import 'search_row.dart';
|
|
|
|
/// The body the results area shows, in the browser's precedence order: an error
|
|
/// takes precedence over retained results, a refresh keeps results on screen,
|
|
/// and a first load shows the searching row.
|
|
enum SearchBodyKind { idle, searching, results, empty, error }
|
|
|
|
/// Debounced, stale-safe omni-search state. It owns no widgets and no socket:
|
|
/// the screen listens to it and the repository talks to the shared gateway.
|
|
class OmniSearchController extends ChangeNotifier {
|
|
OmniSearchController({
|
|
required this.repository,
|
|
this.debounce = const Duration(milliseconds: 250),
|
|
});
|
|
|
|
final SearchRepository repository;
|
|
final Duration debounce;
|
|
final SearchSequencer _sequencer = SearchSequencer();
|
|
final SearchChatSlice _chats = SearchChatSlice();
|
|
|
|
Timer? _timer;
|
|
bool _disposed = false;
|
|
|
|
String _query = '';
|
|
SearchKind _selectedKind = SearchKind.all;
|
|
List<JsonMap> _results = const [];
|
|
bool _loading = false;
|
|
bool _loaded = false;
|
|
bool _truncated = false;
|
|
int _scannedSteps = 0;
|
|
int _scannedBytes = 0;
|
|
int _cap = 0;
|
|
int _scanBoundBytes = 0;
|
|
String? _error;
|
|
|
|
String get query => _query;
|
|
SearchKind get selectedKind => _selectedKind;
|
|
ParsedSearchQuery get parsed => parseSearchQuery(_query, _selectedKind);
|
|
List<JsonMap> get results => _results;
|
|
bool get loading => _loading;
|
|
bool get loaded => _loaded;
|
|
bool get truncated => _truncated;
|
|
int get scannedSteps => _scannedSteps;
|
|
int get scannedBytes => _scannedBytes;
|
|
int get cap => _cap;
|
|
int get scanBoundBytes => _scanBoundBytes;
|
|
|
|
/// The failure the body shows: the run search's, or with the Chats kind
|
|
/// selected (no run search is sent) the chat search's.
|
|
String? get error =>
|
|
_error ?? (parsed.kind == SearchKind.chat ? _chats.error : null);
|
|
|
|
/// Chat hits for the Chats section: shown for every kind that searches
|
|
/// chats (all kinds, or Chats alone).
|
|
List<JsonMap> get shownChats =>
|
|
_searchesChats(parsed.kind) ? _chats.results : const [];
|
|
|
|
/// The Chats section's note: why chats are missing, or that the server's
|
|
/// bounds left some unsearched. With Chats selected alone a failure is the
|
|
/// body's error instead, so only the bounds note remains.
|
|
String? get chatsNote => switch (parsed.kind) {
|
|
SearchKind.all => _chats.note,
|
|
SearchKind.chat when _chats.error == null => _chats.note,
|
|
_ => null,
|
|
};
|
|
|
|
/// Whether the Chats section is drawn as its own group above the others.
|
|
bool get showsChatSection =>
|
|
parsed.kind == SearchKind.all &&
|
|
(shownChats.isNotEmpty || chatsNote != null);
|
|
|
|
static bool _searchesChats(SearchKind kind) =>
|
|
kind == SearchKind.all || kind == SearchKind.chat;
|
|
|
|
bool get _hasShown => shownResults.isNotEmpty || shownChats.isNotEmpty;
|
|
|
|
/// The results already in hand, re-narrowed to the effective kind so the list
|
|
/// always matches the control while a narrowed request loads.
|
|
List<JsonMap> get shownResults {
|
|
final kind = parsed.kind;
|
|
if (kind == SearchKind.all) return _results;
|
|
return _results
|
|
.where((hit) => hit['kind'] == kind.wire)
|
|
.toList(growable: false);
|
|
}
|
|
|
|
SearchBodyKind get bodyKind {
|
|
if (error != null) return SearchBodyKind.error;
|
|
if (_hasShown || showsChatSection) return SearchBodyKind.results;
|
|
if ((_loading && _results.isEmpty) || _chats.loading) {
|
|
return SearchBodyKind.searching;
|
|
}
|
|
if (emptyMessage != null) return SearchBodyKind.empty;
|
|
return SearchBodyKind.idle;
|
|
}
|
|
|
|
/// A thin progress report above retained results while a newer query loads.
|
|
bool get showProgress =>
|
|
(_loading && _results.isNotEmpty) ||
|
|
(_chats.loading && _chats.results.isNotEmpty);
|
|
|
|
String? get emptyMessage {
|
|
if (error != null || _loading || _chats.loading || _hasShown) return null;
|
|
final effective = parsed;
|
|
if (effective.query.trim().isEmpty) {
|
|
if (!_loaded || effective.kind == SearchKind.all) return null;
|
|
final label = effective.kind.label.toLowerCase();
|
|
return _truncated
|
|
? "No $label found within the server's scan limit."
|
|
: 'No $label yet.';
|
|
}
|
|
final bounded = _truncated ? " within the server's scan limit" : '';
|
|
if (effective.kind == SearchKind.all) {
|
|
return 'No matches for "${effective.query}"$bounded.';
|
|
}
|
|
return 'No ${effective.kind.label.toLowerCase()} match '
|
|
'"${effective.query}"$bounded.';
|
|
}
|
|
|
|
/// The server's own result/scan bounds, so a capped answer is not presented as
|
|
/// a complete one. A truncated reply keeps its warning even with no rows;
|
|
/// before any loaded reply there is nothing to report.
|
|
String? get boundsText {
|
|
if (!_loaded) return null;
|
|
// A new first load has no current result set yet, so do not carry the
|
|
// previous reply's warning while the searching state is on screen.
|
|
if (_loading && _results.isEmpty) return null;
|
|
if (_results.isEmpty && !_truncated) return null;
|
|
final count = _results.length;
|
|
final parts = <String>[];
|
|
if (_truncated) {
|
|
parts.add("Results truncated by the server's result or scan limit.");
|
|
}
|
|
parts.add('$count ${count == 1 ? 'result' : 'results'}');
|
|
if (_cap > 0) parts.add('cap $_cap');
|
|
parts.add('scanned $_scannedSteps steps');
|
|
parts.add('${humanBytes(_scannedBytes)} scanned');
|
|
if (_scanBoundBytes > 0) {
|
|
parts.add('scan limit ${humanBytes(_scanBoundBytes)}');
|
|
}
|
|
return parts.join(' · ');
|
|
}
|
|
|
|
/// Record a keystroke. Blank text with no kind clears locally and sends
|
|
/// nothing; valid kind-only browsing is sendable like the browser's search.
|
|
void updateQuery(String value) {
|
|
_query = value;
|
|
_error = null;
|
|
_cancelTimer();
|
|
final effective = parsed;
|
|
if (!searchQuerySendable(effective.query, effective.kind)) {
|
|
_sequencer.edit('');
|
|
_clearResults();
|
|
_notify();
|
|
return;
|
|
}
|
|
_loading = true;
|
|
final edit = _sequencer.edit(value);
|
|
_notify();
|
|
_timer = Timer(debounce, () {
|
|
_timer = null;
|
|
final sent = _sequencer.begin(edit);
|
|
if (sent != null) unawaited(_send(sent));
|
|
});
|
|
}
|
|
|
|
/// A kind change removes recognised type tokens from the visible text and
|
|
/// sends at once, matching the browser's selector behaviour.
|
|
void selectKind(SearchKind kind) {
|
|
_query = parseSearchQuery(_query, _selectedKind).query;
|
|
_selectedKind = kind;
|
|
_error = null;
|
|
_notify();
|
|
unawaited(sendNow());
|
|
}
|
|
|
|
/// Enter: send the current query immediately, skipping the debounce.
|
|
Future<void> submit() => sendNow();
|
|
|
|
/// Retry the current query after a failure.
|
|
Future<void> retry() => sendNow();
|
|
|
|
Future<void> sendNow() async {
|
|
_cancelTimer();
|
|
final effective = parseSearchQuery(_query, _selectedKind);
|
|
if (!searchQuerySendable(effective.query, effective.kind)) {
|
|
_sequencer.edit('');
|
|
_clearResults();
|
|
_notify();
|
|
return;
|
|
}
|
|
final sent = _sequencer.begin(_sequencer.edit(_query));
|
|
if (sent == null) return;
|
|
await _send(sent);
|
|
}
|
|
|
|
Future<void> _send(SearchEdit sent) async {
|
|
final effective = parseSearchQuery(sent.query, _selectedKind);
|
|
final query = effective.query.trim();
|
|
final chatsOnly = effective.kind == SearchKind.chat;
|
|
// The run/artifact search goes first; the chat search follows as its own
|
|
// request, so a slow transcript readback never holds the other results.
|
|
final runs = chatsOnly
|
|
? null
|
|
: repository.search(query: query, kind: effective.kind);
|
|
if (_searchesChats(effective.kind) && (query.isNotEmpty || chatsOnly)) {
|
|
unawaited(
|
|
_chats.load(
|
|
repository,
|
|
query,
|
|
current: () => _sequencer.accepts(sent.token) && !_disposed,
|
|
changed: _notify,
|
|
),
|
|
);
|
|
} else {
|
|
_chats.clear();
|
|
}
|
|
if (runs == null) {
|
|
_results = const [];
|
|
_truncated = false;
|
|
_loading = false;
|
|
_loaded = true;
|
|
_error = null;
|
|
_notify();
|
|
return;
|
|
}
|
|
_loading = true;
|
|
_error = null;
|
|
_notify();
|
|
try {
|
|
final response = await runs;
|
|
if (!_sequencer.accepts(sent.token) || _disposed) return;
|
|
if (response == null) {
|
|
_loading = false;
|
|
_loaded = true;
|
|
_error = unsupportedSearchCopy;
|
|
_notify();
|
|
return;
|
|
}
|
|
_results = searchHitsNewestFirst(response.results);
|
|
_truncated = response.truncated;
|
|
_scannedSteps = response.scannedSteps;
|
|
_scannedBytes = response.scannedBytes;
|
|
_cap = response.cap;
|
|
_scanBoundBytes = response.scanBoundBytes;
|
|
_loading = false;
|
|
_loaded = true;
|
|
_error = null;
|
|
_notify();
|
|
} catch (error) {
|
|
if (!_sequencer.accepts(sent.token) || _disposed) return;
|
|
_loading = false;
|
|
_loaded = true;
|
|
_error = searchFailureText(error);
|
|
_notify();
|
|
}
|
|
}
|
|
|
|
void _clearResults() {
|
|
_chats.clear();
|
|
_results = const [];
|
|
_truncated = false;
|
|
_scannedSteps = 0;
|
|
_scannedBytes = 0;
|
|
_cap = 0;
|
|
_scanBoundBytes = 0;
|
|
_loading = false;
|
|
_loaded = false;
|
|
_error = null;
|
|
}
|
|
|
|
void _cancelTimer() {
|
|
_timer?.cancel();
|
|
_timer = null;
|
|
}
|
|
|
|
void _notify() {
|
|
if (!_disposed) notifyListeners();
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_disposed = true;
|
|
_cancelTimer();
|
|
super.dispose();
|
|
}
|
|
}
|