Add web research and source file reading
This commit is contained in:
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user