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> _history = []; AssistantService({required this.tabletAppContext}); Future 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; final result = AssistantResponse.fromJson(data); _history.add({'role': 'assistant', 'content': result.reply}); return result; } else { throw Exception('Assistant error: ${response.statusCode}'); } } }