Add generation status and cancel control
This commit is contained in:
@@ -15,8 +15,14 @@ class ApiRepository {
|
||||
String _baseUrl;
|
||||
String? _cachedUserId;
|
||||
Future<String>? _loadingUserId;
|
||||
http.Client? _activeRequestClient;
|
||||
String get baseUrl => _baseUrl;
|
||||
|
||||
void cancelActiveRequest() {
|
||||
_activeRequestClient?.close();
|
||||
_activeRequestClient = null;
|
||||
}
|
||||
|
||||
String newId() => _uuid.v4();
|
||||
|
||||
void setBaseUrl(String value) =>
|
||||
@@ -172,19 +178,31 @@ class ApiRepository {
|
||||
}
|
||||
|
||||
Future<String> askWorkspaceAgent(String task, {String? model}) async {
|
||||
final response = await http
|
||||
.post(
|
||||
Uri.parse('$_baseUrl/v1/agent/workspace'),
|
||||
headers: const {'Content-Type': 'application/json'},
|
||||
body: jsonEncode({'task': task, if (model != null) 'model': model}),
|
||||
)
|
||||
.timeout(const Duration(minutes: 10));
|
||||
_checkStatus(response);
|
||||
final data = jsonDecode(response.body) as Map<String, dynamic>;
|
||||
final result = data['result'] as String? ?? '';
|
||||
final files = (data['files'] as List<dynamic>? ?? const []).cast<String>();
|
||||
if (files.isEmpty) return result;
|
||||
return '$result\n\nالمصادر المقروءة: ${files.join('، ')}';
|
||||
final client = http.Client();
|
||||
_activeRequestClient = client;
|
||||
try {
|
||||
final request =
|
||||
http.Request('POST', Uri.parse('$_baseUrl/v1/agent/workspace'))
|
||||
..headers['Content-Type'] = 'application/json'
|
||||
..body = jsonEncode({
|
||||
'task': task,
|
||||
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 files =
|
||||
(data['files'] as List<dynamic>? ?? const []).cast<String>();
|
||||
if (files.isEmpty) return result;
|
||||
return '$result\n\nالمصادر المقروءة: ${files.join('، ')}';
|
||||
} finally {
|
||||
if (identical(_activeRequestClient, client)) _activeRequestClient = null;
|
||||
client.close();
|
||||
}
|
||||
}
|
||||
|
||||
Stream<String> completeStream(
|
||||
@@ -192,6 +210,7 @@ class ApiRepository {
|
||||
String? model,
|
||||
}) async* {
|
||||
final client = http.Client();
|
||||
_activeRequestClient = client;
|
||||
try {
|
||||
final request =
|
||||
http.Request('POST', Uri.parse('$_baseUrl/v1/chat/stream'))
|
||||
@@ -237,6 +256,7 @@ class ApiRepository {
|
||||
}
|
||||
if (!completed) throw Exception('انقطع بث الإجابة قبل اكتماله.');
|
||||
} finally {
|
||||
if (identical(_activeRequestClient, client)) _activeRequestClient = null;
|
||||
client.close();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user