fbf04d90db
Port the Oracle off the Python g4f container onto on-device providers: Chromium HTTP via Cronet, a shared cookie session, IterList retry, hosted OpenAI-compat aggregators, and same-origin WebView fallbacks instead of CORS fetch.
64 lines
1.8 KiB
Dart
64 lines
1.8 KiB
Dart
import 'dart:convert';
|
|
import 'package:http/http.dart' as http;
|
|
import '../http_client.dart';
|
|
import '../models.dart';
|
|
import 'base_provider.dart';
|
|
|
|
class AiChatOs extends BaseProvider {
|
|
@override
|
|
String get name => 'AiChatOs';
|
|
|
|
@override
|
|
bool get working => true;
|
|
|
|
@override
|
|
List<String> get models => [G4FModels.gpt4];
|
|
|
|
@override
|
|
String get defaultModel => G4FModels.gpt4;
|
|
|
|
@override
|
|
Stream<String> createAsyncGenerator(
|
|
String model,
|
|
List<Message> messages, {
|
|
bool stream = false,
|
|
}) async* {
|
|
final url = Uri.parse('https://api.aichatos.cloud/api/generateStream');
|
|
final userId = '#/chat/${DateTime.now().millisecondsSinceEpoch}';
|
|
|
|
final systemMessage = extractSystemMessage(messages);
|
|
final formattedPrompt = formatPrompt(messages);
|
|
|
|
final req = http.Request('POST', url);
|
|
req.headers.addAll({
|
|
'Content-Type': 'application/json',
|
|
'Origin': 'https://chat.aichatos.top',
|
|
'Referer': 'https://chat.aichatos.top/',
|
|
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36',
|
|
});
|
|
|
|
req.body = jsonEncode({
|
|
'prompt': formattedPrompt,
|
|
'userId': userId,
|
|
'network': true,
|
|
'system': systemMessage,
|
|
'withoutContext': false,
|
|
'stream': stream,
|
|
});
|
|
|
|
final response = await g4fHttp.send(req);
|
|
|
|
if (response.statusCode == 200) {
|
|
final resBytes = await response.stream.toBytes();
|
|
final fullText = utf8.decode(resBytes).trim();
|
|
if (fullText.isNotEmpty && !fullText.startsWith('<!DOCTYPE') && !fullText.contains('Error:')) {
|
|
yield fullText;
|
|
} else {
|
|
throw Exception('$name unusable body');
|
|
}
|
|
} else {
|
|
throw Exception('$name returned status ${response.statusCode}');
|
|
}
|
|
}
|
|
}
|