57 lines
2.0 KiB
Dart
57 lines
2.0 KiB
Dart
import 'package:manager_api_new/api.dart';
|
|
import 'package:mymuseum_visitapp/Models/visitContext.dart';
|
|
import 'package:mymuseum_visitapp/Models/AssistantResponse.dart';
|
|
|
|
class AssistantService {
|
|
final VisitAppContext visitAppContext;
|
|
final List<AiChatMessage> history = [];
|
|
|
|
AssistantService({required this.visitAppContext});
|
|
|
|
Future<AssistantResponse> chat({
|
|
required String message,
|
|
String? configurationId,
|
|
}) async {
|
|
final request = AiChatRequest(
|
|
message: message,
|
|
instanceId: visitAppContext.instanceId,
|
|
appType: AppType.Mobile,
|
|
configurationId: configurationId,
|
|
language: visitAppContext.language?.toUpperCase() ?? 'FR',
|
|
history: List.from(history),
|
|
);
|
|
|
|
final response = await visitAppContext.clientAPI.aiApi!.aiChat(request);
|
|
|
|
if (response == null) {
|
|
throw Exception('Empty response from assistant');
|
|
}
|
|
|
|
print("AI raw response: reply='${response.reply}' navigation.sectionId='${response.navigation?.sectionId}' navigation.sectionTitle='${response.navigation?.sectionTitle}' navigation.sectionType='${response.navigation?.sectionType}' cards=${response.cards?.length}");
|
|
|
|
final result = AssistantResponse(
|
|
reply: response.reply ?? '',
|
|
cards: response.cards
|
|
?.map((c) => AiCard(
|
|
title: c.title ?? '',
|
|
subtitle: c.subtitle ?? '',
|
|
icon: c.icon,
|
|
))
|
|
.toList(),
|
|
navigation: response.navigation != null
|
|
? AssistantNavigationAction(
|
|
sectionId: response.navigation!.sectionId ?? '',
|
|
sectionTitle: response.navigation!.sectionTitle ?? '',
|
|
sectionType: response.navigation!.sectionType ?? '',
|
|
imageUrl: response.navigation!.imageUrl,
|
|
)
|
|
: null,
|
|
);
|
|
|
|
history.add(AiChatMessage(role: 'user', content: message));
|
|
history.add(AiChatMessage(role: 'assistant', content: result.reply));
|
|
return result;
|
|
}
|
|
|
|
void clearHistory() => history.clear();
|
|
} |