-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwrapper.py
More file actions
353 lines (292 loc) · 12 KB
/
Copy pathwrapper.py
File metadata and controls
353 lines (292 loc) · 12 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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
import json
import threading
import time
import uuid
from concurrent.futures import ThreadPoolExecutor
from typing import Any, Callable, Dict, List, Optional
from queue import Queue
from websockets.asyncio.client import ClientConnection
from ....logger import Logger
from .....scribe import scribe
class WebSocketEvent:
"""Represents a websocket event with metadata."""
def __init__(
self,
event_type: str,
data: Any,
direction: str,
timestamp: Optional[float] = None,
):
self.event_type = event_type
self.data = data
self.direction = direction # 'inbound' or 'outbound'
self.timestamp = timestamp or time.time()
self.event_id = str(uuid.uuid4())
def to_dict(self) -> Dict[str, Any]:
"""Convert event to dictionary for logging."""
return {
"event_id": self.event_id,
"event_type": self.event_type,
"data": self.data,
"direction": self.direction,
"timestamp": self.timestamp,
}
def connect_with_maxim_wrapper():
pass
class OpenAIRealtimeWebsocketWrapper(ClientConnection):
"""
A WebSocket ClientConnection that logs OpenAI Realtime API events while maintaining
full compatibility with the websockets.ClientConnection interface.
This wrapper extends websockets.ClientConnection and can be used as a drop-in replacement
while adding OpenAI-specific logging and event processing capabilities.
"""
def __init__(
self, logger: Logger, session_id: Optional[str] = None, *args, **kwargs
):
# OpenAI-specific setup
self.maxim_logger = logger
self.session_id = session_id or str(uuid.uuid4())
# Event processing
self.event_callbacks: List[Callable[[WebSocketEvent], None]] = []
self.executor = ThreadPoolExecutor(
max_workers=1, thread_name_prefix="ws-processor"
)
# Stats
self.events_processed = 0
self.connection_start_time = time.time()
# Call parent constructor
super().__init__(*args, **kwargs)
scribe().info(
f"[MaximSDK][OpenAIRealtimeWebsocketWrapper] Initialized with session_id: {self.session_id}"
)
async def send(self, message, **kwargs):
"""Override send to log outbound messages."""
try:
# Process the message for logging
await self._process_outbound_message(message)
# Call the parent send method
result = await super().send(message, **kwargs)
scribe().debug(
f"[MaximSDK][OpenAIRealtimeWebsocketWrapper] Sent message successfully"
)
return result
except Exception as e:
scribe().warning(
f"[MaximSDK][OpenAIRealtimeWebsocketWrapper] Failed to send message: {e}"
)
raise
async def recv(self, **kwargs):
"""Override recv to log inbound messages."""
try:
# Call the parent recv method
message = await super().recv(**kwargs)
# Process the message for logging
await self._process_inbound_message(message)
scribe().debug(
f"[MaximSDK][OpenAIRealtimeWebsocketWrapper] Received message successfully"
)
return message
except Exception as e:
scribe().warning(
f"[MaximSDK][OpenAIRealtimeWebsocketWrapper] Failed to receive message: {e}"
)
raise
async def recv_streaming(self, **kwargs):
"""Override recv_streaming to log inbound streaming messages."""
try:
# Call the parent recv_streaming method
stream = await super().recv_streaming(**kwargs)
# TODO: Consider how to handle streaming message logging
scribe().debug(
f"[MaximSDK][OpenAIRealtimeWebsocketWrapper] Received streaming message"
)
return stream
except Exception as e:
scribe().warning(
f"[MaximSDK][OpenAIRealtimeWebsocketWrapper] Failed to receive streaming message: {e}"
)
raise
async def close(self, code=1000, reason="", **kwargs):
"""Override close to log connection closure and cleanup."""
try:
scribe().info(
f"[MaximSDK][OpenAIRealtimeWebsocketWrapper] Closing connection with code: {code}, reason: {reason}"
)
# Call the parent close method
result = await super().close(code, reason, **kwargs)
# Cleanup resources
self._cleanup()
return result
except Exception as e:
scribe().warning(
f"[MaximSDK][OpenAIRealtimeWebsocketWrapper] Error during close: {e}"
)
raise
async def ping(self, data=b"", **kwargs):
"""Override ping to log ping frames."""
try:
scribe().debug(f"[MaximSDK][OpenAIRealtimeWebsocketWrapper] Sending ping")
# Call the parent ping method
result = await super().ping(data, **kwargs)
return result
except Exception as e:
scribe().warning(
f"[MaximSDK][OpenAIRealtimeWebsocketWrapper] Failed to send ping: {e}"
)
raise
async def pong(self, data=b"", **kwargs):
"""Override pong to log pong frames."""
try:
scribe().debug(f"[MaximSDK][OpenAIRealtimeWebsocketWrapper] Sending pong")
# Call the parent pong method
result = await super().pong(data, **kwargs)
return result
except Exception as e:
scribe().warning(
f"[MaximSDK][OpenAIRealtimeWebsocketWrapper] Failed to send pong: {e}"
)
raise
def add_event_callback(self, callback: Callable[[WebSocketEvent], None]):
"""Add a callback function to be called for each processed event."""
self.event_callbacks.append(callback)
scribe().debug(
f"[MaximSDK][OpenAIRealtimeWebsocketWrapper] Added event callback. Total callbacks: {len(self.event_callbacks)}"
)
def remove_event_callback(self, callback: Callable[[WebSocketEvent], None]):
"""Remove an event callback."""
if callback in self.event_callbacks:
self.event_callbacks.remove(callback)
scribe().debug(
f"[MaximSDK][OpenAIRealtimeWebsocketWrapper] Removed event callback. Total callbacks: {len(self.event_callbacks)}"
)
async def _process_inbound_message(self, message: Any):
"""Process and log an inbound message."""
try:
# Parse the message
if isinstance(message, str):
try:
data = json.loads(message)
event_type = (
data.get("type", "unknown") if isinstance(data, dict) else "raw"
)
except json.JSONDecodeError:
data = message
event_type = "raw"
else:
data = message
event_type = "binary" if isinstance(message, bytes) else "raw"
# Create event for processing
event = WebSocketEvent(
event_type=event_type, data=data, direction="inbound"
)
# Process event asynchronously
self._process_event_async(event)
self.events_processed += 1
except Exception as e:
scribe().warning(
f"[MaximSDK][OpenAIRealtimeWebsocketWrapper] Failed to process inbound message: {e}"
)
async def _process_outbound_message(self, message: Any):
"""Process and log an outbound message."""
try:
# Parse the message
if isinstance(message, str):
try:
data = json.loads(message)
event_type = (
data.get("type", "unknown") if isinstance(data, dict) else "raw"
)
except json.JSONDecodeError:
data = message
event_type = "raw"
else:
data = message
event_type = "binary" if isinstance(message, bytes) else "raw"
# Create event for processing
event = WebSocketEvent(
event_type=event_type, data=data, direction="outbound"
)
# Process event asynchronously
self._process_event_async(event)
self.events_processed += 1
except Exception as e:
scribe().warning(
f"[MaximSDK][OpenAIRealtimeWebsocketWrapper] Failed to process outbound message: {e}"
)
def _process_event_async(self, event: WebSocketEvent):
"""Process a websocket event asynchronously."""
try:
# Submit to thread pool for parallel processing
self.executor.submit(self._process_event_sync, event)
except Exception as e:
scribe().warning(
f"[MaximSDK][OpenAIRealtimeWebsocketWrapper] Failed to submit event for processing: {e}"
)
def _process_event_sync(self, event: WebSocketEvent):
"""Synchronously process and log the event."""
try:
# Call custom callbacks
for callback in self.event_callbacks:
try:
callback(event)
except Exception as e:
scribe().warning(
f"[MaximSDK][OpenAIRealtimeWebsocketWrapper] Event callback failed: {e}"
)
# Log to Maxim if logger is available
if self.maxim_logger:
try:
# TODO: Implement Maxim-specific logging here
pass
except Exception as e:
scribe().warning(
f"[MaximSDK][OpenAIRealtimeWebsocketWrapper] Maxim logging failed: {e}"
)
scribe().debug(
f"[MaximSDK][OpenAIRealtimeWebsocketWrapper] Processed {event.direction} event: {event.event_type}"
)
except Exception as e:
scribe().warning(
f"[MaximSDK][OpenAIRealtimeWebsocketWrapper] Failed to process event {event.event_id}: {e}"
)
def get_stats(self) -> Dict[str, Any]:
"""Get connection and processing statistics."""
duration = time.time() - self.connection_start_time
return {
"session_id": self.session_id,
"connection_duration": duration,
"events_processed": self.events_processed,
"active_callbacks": len(self.event_callbacks),
"state": str(self.state) if hasattr(self, "state") else "unknown",
"local_address": (
str(self.local_address) if hasattr(self, "local_address") else None
),
"remote_address": (
str(self.remote_address) if hasattr(self, "remote_address") else None
),
}
def _cleanup(self):
"""Cleanup resources during connection closure."""
try:
# Shutdown event processor
self.executor.shutdown(wait=False)
stats = self.get_stats()
scribe().info(
f"[MaximSDK][OpenAIRealtimeWebsocketWrapper] Connection closed. Final stats: {stats}"
)
except Exception as e:
scribe().warning(
f"[MaximSDK][OpenAIRealtimeWebsocketWrapper] Error during cleanup: {e}"
)
def __enter__(self):
"""Context manager entry."""
return self
def __exit__(self, exc_type, exc_val, exc_tb):
"""Context manager exit."""
try:
# Note: In async context, close() should be awaited, but this is sync context
self._cleanup()
except Exception as e:
scribe().warning(
f"[MaximSDK][OpenAIRealtimeWebsocketWrapper] Error in context manager exit: {e}"
)