Add web research and source file reading

This commit is contained in:
Hamza Ayed
2026-10-01 14:06:36 +03:00
parent 2d3778a92e
commit dc4d5bad0f
9 changed files with 310 additions and 23 deletions
@@ -205,6 +205,45 @@ class ApiRepository {
}
}
Future<String> searchWeb(String query, {String? model}) async {
final client = http.Client();
_activeRequestClient = client;
try {
final request = http.Request(
'POST',
Uri.parse('$_baseUrl/v1/web/search'),
)
..headers['Content-Type'] = 'application/json'
..body = jsonEncode({
'query': query,
'max_results': 5,
if (model != null) 'model': model,
});
final streamed = await client
.send(request)
.timeout(const Duration(minutes: 10));
final response = await http.Response.fromStream(streamed);
_checkStatus(response);
final data = jsonDecode(response.body) as Map<String, dynamic>;
final result = data['result'] as String? ?? '';
final sources = (data['sources'] as List<dynamic>? ?? const [])
.cast<Map<String, dynamic>>();
if (sources.isEmpty) return result;
final sourceList = sources.map((source) {
final index = source['index'];
final title = source['title'] as String? ?? 'مصدر';
final url = source['url'] as String? ?? '';
final status = source['status'] as String?;
final note = status == 'snippet_only' ? ' (مقتطف فقط؛ تعذر فتح الصفحة)' : '';
return '[$index] [$title]($url)$note';
}).join('\n');
return '$result\n\nالمصادر:\n$sourceList';
} finally {
if (identical(_activeRequestClient, client)) _activeRequestClient = null;
client.close();
}
}
Stream<String> completeStream(
List<ChatMessage> messages, {
String? model,