connect method

void connect({
  1. String? botId,
  2. String? promptId,
  3. String? userId,
  4. String? language,
  5. String? theme,
})

Implementation

void connect({
  String? botId,
  String? promptId,
  String? userId,
  String? language,
  String? theme,
}) async {
  print("Connect.... Using botId: ${botId ?? defaultBotId}, promptId: ${promptId ?? defaultPromptId}"); // Log which IDs are used
  connectionState = ConnectionState.connecting;
  notifyListeners();

  try {
    final roomName = 'room-${(1000 + DateTime.now().millisecondsSinceEpoch % 9000)}';
    final participantName = 'user-${(1000 + DateTime.now().millisecondsSinceEpoch % 9000)}';

    final connectionDetails = await tokenService.fetchConnectionDetails(
      roomName: roomName,
      participantName: participantName,
      // <<< --- USE PASSED ID OR DEFAULT ID --- >>>
      botId: botId ?? defaultBotId,
      promptId: promptId ?? defaultPromptId,
      userId: userId,
      language: language ?? 'en',
      theme: theme ?? 'dark',
    );

    print("Fetched Connection Details: Server: ${connectionDetails.serverUrl}, Token: ${connectionDetails.participantToken.substring(0, 10)}..., connecting to room..."); // Avoid printing full token

    await room.connect(
      connectionDetails.serverUrl,
      connectionDetails.participantToken,
    );

    print("Connected to room");

    if (room.localParticipant != null) {
      await room.localParticipant!.setMicrophoneEnabled(true);
      print("Microphone enabled");
    } else {
      print("Error: Local participant is null after connect.");
      throw Exception("Local participant is null after connect.");
    }

    connectionState = ConnectionState.connected;
    appScreenState = AppScreenState.agent;
    notifyListeners();
  } catch (error, stackTrace) { // Added stackTrace for better debugging
    print('Connection error: $error');
    print('Connection StackTrace: $stackTrace');

    connectionState = ConnectionState.disconnected;
    appScreenState = AppScreenState.welcome;
    notifyListeners();
  }
}