44 lines
1.4 KiB
Dart
44 lines
1.4 KiB
Dart
import 'dart:convert';
|
|
import 'package:http/http.dart' as http;
|
|
import 'package:tablet_app/Models/tabletContext.dart';
|
|
import 'package:tablet_app/Models/AssistantResponse.dart';
|
|
|
|
class AssistantService {
|
|
final TabletAppContext tabletAppContext;
|
|
final List<Map<String, String>> _history = [];
|
|
|
|
AssistantService({required this.tabletAppContext});
|
|
|
|
Future<AssistantResponse> chat({required String message, String? configurationId}) async {
|
|
final host = tabletAppContext.host ?? '';
|
|
final instanceId = tabletAppContext.instanceId ?? '';
|
|
final language = tabletAppContext.language ?? 'FR';
|
|
|
|
_history.add({'role': 'user', 'content': message});
|
|
|
|
final body = {
|
|
'message': message,
|
|
'instanceId': instanceId,
|
|
'appType': 'Tablet',
|
|
'configurationId': configurationId,
|
|
'language': language,
|
|
'history': _history.take(10).toList(),
|
|
};
|
|
|
|
final response = await http.post(
|
|
Uri.parse('$host/api/ai/chat'),
|
|
headers: {'Content-Type': 'application/json'},
|
|
body: jsonEncode(body),
|
|
);
|
|
|
|
if (response.statusCode == 200) {
|
|
final data = jsonDecode(response.body) as Map<String, dynamic>;
|
|
final result = AssistantResponse.fromJson(data);
|
|
_history.add({'role': 'assistant', 'content': result.reply});
|
|
return result;
|
|
} else {
|
|
throw Exception('Assistant error: ${response.statusCode}');
|
|
}
|
|
}
|
|
}
|