1 line
6.8 KiB
JSON
1 line
6.8 KiB
JSON
{"repo": "ali-sher191/tourease", "files": {"lib/services/ai_service.dart": {"e3bdb380fae614ae5923e9eceff5383152c47fb4": {"t": 1785745915.3142154, "n": 3221, "h": "0b821b5e02fd5deb", "c": "import 'dart:convert';\nimport 'package:http/http.dart' as http;\nimport 'package:flutter_dotenv/flutter_dotenv.dart';\nimport 'package:connectivity_plus/connectivity_plus.dart';\nimport 'package:flutter/foundation.dart';\n\nclass AiService {\n AiService._();\n static final AiService _instance = AiService._();\n factory AiService() => _instance;\n\n final String _baseUrl = 'https://api.deepseek.com/chat/completions';\n final List<Map<String, String>> _messages = [];\n DateTime? _lastRequestTime;\n\n void ensureInitialized() {\n if (_messages.isEmpty) {\n _messages.add({\n 'role': 'system',\n 'content': '''You are \"Tour Buddy\", a travel assistant for TourEase.\nHelp users book tours and explain features.\nRespond in English, Urdu, or Roman Urdu naturally.''',\n });\n }\n }\n\n Future<String> sendMessage(String userMessage) async {\n try {\n // Check connectivity\n final connectivityResult = await Connectivity().checkConnectivity();\n if (connectivityResult == ConnectivityResult.none) {\n return 'No internet connection. Please check your network and try again.';\n }\n\n // Simple rate limiting\n if (_lastRequestTime != null) {\n final timeSinceLastRequest = DateTime.now().difference(\n _lastRequestTime!,\n );\n if (timeSinceLastRequest.inSeconds < 1) {\n return 'Please wait a moment...';\n }\n }\n _lastRequestTime = DateTime.now();\n\n final apiKey =\n dotenv.env['DEEPSEEK_API_KEY'] ??\n 'sk-52bc3e9409de4d2fb843528bd78be28e';\n\n if (apiKey.isEmpty) {\n return 'DeepSeek API key is missing. Please add it to your .env file.';\n }\n\n // Add user message to history\n _messages.add({'role': 'user', 'content': userMessage});\n\n final response = await http\n .post(\n Uri.parse(_baseUrl),\n headers: {\n 'Authorization': 'Bearer $apiKey',\n 'Content-Type': 'application/json',\n },\n body: jsonEncode({\n 'model': 'deepseek-chat',\n 'messages': _messages,\n 'temperature': 0.7,\n 'max_tokens': 1024,\n }),\n )\n .timeout(const Duration(seconds: 30));\n\n if (response.statusCode == 200) {\n final data = jsonDecode(response.body);\n final aiText = data['choices'][0]['message']['content'] as String;\n\n // Add AI response to history\n _messages.add({'role': 'assistant', 'content': aiText});\n\n return aiText;\n } else {\n debugPrint(\n 'DeepSeek API Error: ${response.statusCode} - ${response.body}',\n );\n return 'DeepSeek API Error: ${response.statusCode}. Please check your API key and balance.';\n }\n } catch (e) {\n debugPrint('Unexpected error in AiService: $e');\n return 'Unexpected Error: $e';\n }\n }\n\n void resetChat() {\n _messages.clear();\n ensureInitialized();\n }\n\n List<String> getQuickSuggestions() {\n return [\n 'Show me popular tours',\n 'How does live tracking work?',\n 'What payment methods do you accept?',\n 'Tell me about Murree tours',\n 'How do I contact an agency?',\n ];\n }\n}\n"}, "1e211763e7472c2fcac2a286a9bb5b2e5740434a": {"t": 1785757877.6732845, "n": 3221, "h": "0b821b5e02fd5deb", "c": "import 'dart:convert';\nimport 'package:http/http.dart' as http;\nimport 'package:flutter_dotenv/flutter_dotenv.dart';\nimport 'package:connectivity_plus/connectivity_plus.dart';\nimport 'package:flutter/foundation.dart';\n\nclass AiService {\n AiService._();\n static final AiService _instance = AiService._();\n factory AiService() => _instance;\n\n final String _baseUrl = 'https://api.deepseek.com/chat/completions';\n final List<Map<String, String>> _messages = [];\n DateTime? _lastRequestTime;\n\n void ensureInitialized() {\n if (_messages.isEmpty) {\n _messages.add({\n 'role': 'system',\n 'content': '''You are \"Tour Buddy\", a travel assistant for TourEase.\nHelp users book tours and explain features.\nRespond in English, Urdu, or Roman Urdu naturally.''',\n });\n }\n }\n\n Future<String> sendMessage(String userMessage) async {\n try {\n // Check connectivity\n final connectivityResult = await Connectivity().checkConnectivity();\n if (connectivityResult == ConnectivityResult.none) {\n return 'No internet connection. Please check your network and try again.';\n }\n\n // Simple rate limiting\n if (_lastRequestTime != null) {\n final timeSinceLastRequest = DateTime.now().difference(\n _lastRequestTime!,\n );\n if (timeSinceLastRequest.inSeconds < 1) {\n return 'Please wait a moment...';\n }\n }\n _lastRequestTime = DateTime.now();\n\n final apiKey =\n dotenv.env['DEEPSEEK_API_KEY'] ??\n 'sk-52bc3e9409de4d2fb843528bd78be28e';\n\n if (apiKey.isEmpty) {\n return 'DeepSeek API key is missing. Please add it to your .env file.';\n }\n\n // Add user message to history\n _messages.add({'role': 'user', 'content': userMessage});\n\n final response = await http\n .post(\n Uri.parse(_baseUrl),\n headers: {\n 'Authorization': 'Bearer $apiKey',\n 'Content-Type': 'application/json',\n },\n body: jsonEncode({\n 'model': 'deepseek-chat',\n 'messages': _messages,\n 'temperature': 0.7,\n 'max_tokens': 1024,\n }),\n )\n .timeout(const Duration(seconds: 30));\n\n if (response.statusCode == 200) {\n final data = jsonDecode(response.body);\n final aiText = data['choices'][0]['message']['content'] as String;\n\n // Add AI response to history\n _messages.add({'role': 'assistant', 'content': aiText});\n\n return aiText;\n } else {\n debugPrint(\n 'DeepSeek API Error: ${response.statusCode} - ${response.body}',\n );\n return 'DeepSeek API Error: ${response.statusCode}. Please check your API key and balance.';\n }\n } catch (e) {\n debugPrint('Unexpected error in AiService: $e');\n return 'Unexpected Error: $e';\n }\n }\n\n void resetChat() {\n _messages.clear();\n ensureInitialized();\n }\n\n List<String> getQuickSuggestions() {\n return [\n 'Show me popular tours',\n 'How does live tracking work?',\n 'What payment methods do you accept?',\n 'Tell me about Murree tours',\n 'How do I contact an agency?',\n ];\n }\n}\n"}}}} |