import '../../../core/api_gateway.dart'; import '../data/search_repository.dart'; import 'search_models.dart'; import 'search_order.dart'; import 'search_query.dart'; /// Wording for a server whose search does not know the `chat` kind (it /// predates chat search and rejects the kind by name). const unsupportedChatSearchCopy = 'This server does not search chats. Update the Pipelines server to ' 'include them.'; final _unknownKind = RegExp(r'unknown search kind', caseSensitive: false); /// The Chats section's state: one `kind: chat` request per sent query, run /// beside (never instead of, nor ahead of) the run/artifact search, so the /// server's transcript readback cannot delay the other results. class SearchChatSlice { List results = const []; bool loading = false; bool loaded = false; bool truncated = false; String? error; void clear() { results = const []; loading = false; loaded = false; truncated = false; error = null; } /// Requests chats for [query]. [current] reports whether this request is /// still the newest edit; a stale reply is dropped without a change. Future load( SearchRepository repository, String query, { required bool Function() current, required void Function() changed, }) async { loading = true; error = null; changed(); try { final response = await repository.search( query: query, kind: SearchKind.chat, ); if (!current()) return; if (response == null) { results = const []; truncated = false; error = unsupportedChatSearchCopy; } else { results = searchHitsNewestFirst( response.results .where((hit) => hit['kind'] == SearchKind.chat.wire) .toList(growable: false), ); truncated = response.truncated; } } catch (failure) { if (!current()) return; results = const []; truncated = false; final text = searchFailureText(failure); error = _unknownKind.hasMatch(text) || text == unsupportedSearchCopy ? unsupportedChatSearchCopy : text; } loading = false; loaded = true; changed(); } /// A short honest note under the Chats section: why chats are missing, or /// that the server's bounds left some transcripts unsearched. String? get note { if (error != null) return error; if (truncated) { return "Some chats were not searched within the server's time or " 'result limit.'; } return null; } }