-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplatform_services.py
More file actions
250 lines (189 loc) · 8.54 KB
/
platform_services.py
File metadata and controls
250 lines (189 loc) · 8.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#!/usr/bin/env python3
"""
Platform Service Architecture for Universal Bot Plugin System
This module provides the core infrastructure for platform-agnostic services
that plugins can use without being tied to specific platforms like SimpleX.
"""
from abc import ABC, abstractmethod
from typing import Dict, List, Optional, Any
import logging
class PlatformService(ABC):
"""Base class for platform-specific services"""
def __init__(self, name: str, logger: Optional[logging.Logger] = None):
self.name = name
self.logger = logger or logging.getLogger(__name__)
@abstractmethod
async def is_available(self) -> bool:
"""Check if service is available on current platform"""
pass
@abstractmethod
def get_service_info(self) -> Dict[str, Any]:
"""Get service capabilities and metadata"""
pass
class MessageHistoryService(PlatformService):
"""Service for accessing message history"""
def __init__(self, logger: Optional[logging.Logger] = None):
super().__init__("message_history", logger)
@abstractmethod
async def get_recent_messages(self, chat_id: str, count: int = 10) -> List[Dict]:
"""Get recent messages from chat"""
pass
@abstractmethod
async def get_messages_by_criteria(self, chat_id: str, **kwargs) -> List[Dict]:
"""Get messages by various criteria (sender, time range, etc.)"""
pass
class ContactManagementService(PlatformService):
"""Service for managing contacts/users"""
def __init__(self, logger: Optional[logging.Logger] = None):
super().__init__("contact_management", logger)
@abstractmethod
async def get_contacts(self) -> List[Dict]:
"""Get all contacts"""
pass
@abstractmethod
async def get_contact_info(self, contact_id: str) -> Dict:
"""Get specific contact information"""
pass
class GroupManagementService(PlatformService):
"""Service for managing groups/channels"""
def __init__(self, logger: Optional[logging.Logger] = None):
super().__init__("group_management", logger)
@abstractmethod
async def get_groups(self) -> List[Dict]:
"""Get all groups"""
pass
@abstractmethod
async def get_group_info(self, group_id: str) -> Dict:
"""Get specific group information"""
pass
@abstractmethod
async def get_group_members(self, group_id: str) -> List[Dict]:
"""Get group members"""
pass
class FileService(PlatformService):
"""Service for file operations"""
def __init__(self, logger: Optional[logging.Logger] = None):
super().__init__("file_operations", logger)
@abstractmethod
async def download_file(self, file_info: Dict) -> str:
"""Download file and return local path"""
pass
@abstractmethod
async def send_file(self, chat_id: str, file_path: str, caption: str = "") -> bool:
"""Send file to chat"""
pass
class InviteManagementService(PlatformService):
"""Service for managing invitations and connections"""
def __init__(self, logger: Optional[logging.Logger] = None):
super().__init__("invite_management", logger)
@abstractmethod
async def generate_invite(self, requested_by: str) -> Optional[str]:
"""Generate connection invite"""
pass
@abstractmethod
async def list_pending_invites(self) -> List[Dict]:
"""List pending invitations"""
pass
class NotificationService(PlatformService):
"""Service for sending notifications to multiple targets"""
def __init__(self, logger: Optional[logging.Logger] = None):
super().__init__("notification", logger)
@abstractmethod
async def notify_groups(self, groups: List[str], message: str) -> Dict[str, bool]:
"""Send notification to multiple groups
Returns: Dict mapping group names to success status"""
pass
@abstractmethod
async def notify_users(self, users: List[str], message: str) -> Dict[str, bool]:
"""Send notification to multiple users
Returns: Dict mapping user names to success status"""
pass
@abstractmethod
async def bulk_notify(self, targets: Dict[str, List[str]], message: str) -> Dict[str, Dict[str, bool]]:
"""Send notifications to mixed targets
Args: targets = {'groups': ['group1'], 'users': ['user1']}
Returns: Dict with 'groups' and 'users' keys mapping to success status"""
pass
class AudioProcessingService(PlatformService):
"""Service for audio processing and speech-to-text functionality"""
def __init__(self, logger: Optional[logging.Logger] = None):
super().__init__("audio_processing", logger)
@abstractmethod
async def process_audio_file(self, file_path: str, context: Dict[str, Any]) -> Optional[str]:
"""Process audio file and return transcribed text
Args:
file_path: Path to the audio file
context: Additional context for processing (chat_id, user, etc.)
Returns:
Transcribed text or None if processing failed
"""
pass
@abstractmethod
async def get_supported_formats(self) -> List[str]:
"""Get list of supported audio formats"""
pass
@abstractmethod
async def estimate_processing_time(self, file_size: int) -> float:
"""Estimate processing time in seconds based on file size"""
pass
class PlatformStatusService(PlatformService):
"""Service for getting platform status and diagnostics"""
def __init__(self, logger: Optional[logging.Logger] = None):
super().__init__("platform_status", logger)
@abstractmethod
async def get_connection_info(self) -> Dict[str, Any]:
"""Get connection status information"""
pass
@abstractmethod
async def get_platform_health(self) -> Dict[str, Any]:
"""Get platform health metrics"""
pass
@abstractmethod
async def get_diagnostic_info(self) -> Dict[str, Any]:
"""Get detailed diagnostic information"""
pass
class PlatformServiceRegistry:
"""Central registry for platform-specific services"""
def __init__(self, logger: Optional[logging.Logger] = None):
self.services: Dict[str, PlatformService] = {}
self.logger = logger or logging.getLogger(__name__)
self.logger.info("🔧 SERVICE REGISTRY: Initialized platform service registry")
def register_service(self, service_name: str, service_provider: PlatformService):
"""Register a platform service (e.g., message_history, contact_management)"""
self.services[service_name] = service_provider
self.logger.info(f"🔧 SERVICE REGISTRY: Registered service '{service_name}'")
def get_service(self, service_name: str) -> Optional[PlatformService]:
"""Get a platform service if available"""
service = self.services.get(service_name)
if service:
self.logger.debug(f"🔧 SERVICE REGISTRY: Retrieved service '{service_name}'")
else:
self.logger.debug(f"🔧 SERVICE REGISTRY: Service '{service_name}' not available")
return service
def list_available_services(self) -> List[str]:
"""List all available services for current platform"""
return list(self.services.keys())
async def check_service_availability(self, service_name: str) -> bool:
"""Check if a service is both registered and available"""
service = self.get_service(service_name)
if not service:
return False
try:
return await service.is_available()
except Exception as e:
self.logger.error(f"🔧 SERVICE REGISTRY: Error checking availability of '{service_name}': {e}")
return False
def get_service_info(self, service_name: str) -> Optional[Dict[str, Any]]:
"""Get information about a specific service"""
service = self.get_service(service_name)
return service.get_service_info() if service else None
def get_all_services_info(self) -> Dict[str, Dict[str, Any]]:
"""Get information about all registered services"""
info = {}
for name, service in self.services.items():
try:
info[name] = service.get_service_info()
except Exception as e:
self.logger.error(f"🔧 SERVICE REGISTRY: Error getting info for '{name}': {e}")
info[name] = {"error": str(e)}
return info