import '../../../core/api_gateway.dart'; /// The server's search narrowing vocabulary (`internal/protocol/search.go`), /// in the control's display order. The empty [wire] value means every kind. enum SearchKind { all('', 'All kinds'), pipeline('pipeline', 'Pipelines'), run('run', 'Runs'), step('step', 'Steps'), field('field', 'Fields'), artifact('artifact', 'Artifacts'), version('version', 'Versions'); const SearchKind(this.wire, this.label); final String wire; final String label; /// A stale or unknown value falls back to [SearchKind.all] rather than /// inventing an option the server does not accept. static SearchKind fromWire(String? wire) { for (final kind in values) { if (kind.wire == wire) return kind; } return SearchKind.all; } } /// A query after `t:`/`type:` tokens have been recognised. [query] is the text /// the server should match; [kind] is the effective narrowing. class ParsedSearchQuery { const ParsedSearchQuery(this.query, this.kind, this.hasType); final String query; final SearchKind kind; final bool hasType; } /// The bounded `search_results` body from the server. Every bound the server /// reports is kept, so the screen can show a truncation/scan indicator instead /// of pretending a capped reply was complete. class SearchResponse { const SearchResponse({ this.query = '', this.kind = SearchKind.all, this.results = const [], this.truncated = false, this.scannedSteps = 0, this.scannedBytes = 0, this.cap = 0, this.scanBoundBytes = 0, }); factory SearchResponse.fromJson(JsonMap json) { final rows = json['results']; return SearchResponse( query: '${json['query'] ?? ''}', kind: SearchKind.fromWire('${json['kind'] ?? ''}'), results: rows is List ? rows .whereType() .map((row) => Map.from(row)) .toList(growable: false) : const [], truncated: json['truncated'] == true, scannedSteps: _integer(json['scanned_steps']), scannedBytes: _integer(json['scanned_bytes']), cap: _integer(json['cap']), scanBoundBytes: _integer(json['scan_bound_bytes']), ); } final String query; final SearchKind kind; final List results; final bool truncated; final int scannedSteps; final int scannedBytes; final int cap; final int scanBoundBytes; } int _integer(Object? value) { if (value is int) return value; if (value is num) return value.toInt(); return int.tryParse('$value') ?? 0; }