Upload client.py
Browse files- backend/gemini/client.py +34 -5
backend/gemini/client.py
CHANGED
|
@@ -49,6 +49,8 @@ class GeminiConnection:
|
|
| 49 |
self._receive_task: Optional[asyncio.Task] = None
|
| 50 |
self._closed = False
|
| 51 |
self._tool_registry: Dict[str, Callable] = {}
|
|
|
|
|
|
|
| 52 |
|
| 53 |
def set_config(self, config_data: Dict[str, Any]) -> None:
|
| 54 |
self.config = config_data
|
|
@@ -91,6 +93,11 @@ class GeminiConnection:
|
|
| 91 |
for m in modalities
|
| 92 |
]
|
| 93 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
# Tools / Function calling
|
| 95 |
tools_config = self.config.get("tools")
|
| 96 |
if tools_config:
|
|
@@ -220,7 +227,11 @@ class GeminiConnection:
|
|
| 220 |
)
|
| 221 |
except Exception as e:
|
| 222 |
if "closed" not in str(e).lower():
|
| 223 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 224 |
|
| 225 |
async def send_text(self, text: str) -> None:
|
| 226 |
"""Send text message"""
|
|
@@ -231,7 +242,11 @@ class GeminiConnection:
|
|
| 231 |
await self.session.send_client_content(turns=[content], turn_complete=True)
|
| 232 |
except Exception as e:
|
| 233 |
if "closed" not in str(e).lower():
|
| 234 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 235 |
|
| 236 |
async def send_image(self, data: str) -> None:
|
| 237 |
"""Send image data frame"""
|
|
@@ -244,7 +259,11 @@ class GeminiConnection:
|
|
| 244 |
)
|
| 245 |
except Exception as e:
|
| 246 |
if "closed" not in str(e).lower():
|
| 247 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 248 |
|
| 249 |
async def send_file(self, file_uri: str, mime_type: str) -> None:
|
| 250 |
"""Send file reference into the active session"""
|
|
@@ -266,7 +285,11 @@ class GeminiConnection:
|
|
| 266 |
)
|
| 267 |
except Exception as e:
|
| 268 |
if "closed" not in str(e).lower():
|
| 269 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 270 |
# Gracefully swallow to keep session alive; caller notified via error packet
|
| 271 |
|
| 272 |
async def upload_file(self, file_bytes: bytes, mime_type: str) -> Dict[str, str]:
|
|
@@ -298,7 +321,13 @@ class GeminiConnection:
|
|
| 298 |
|
| 299 |
# GoAway notification
|
| 300 |
if hasattr(msg, "go_away") and msg.go_away:
|
| 301 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 302 |
|
| 303 |
# Session resumption update
|
| 304 |
if hasattr(msg, "session_resumption_update") and msg.session_resumption_update:
|
|
|
|
| 49 |
self._receive_task: Optional[asyncio.Task] = None
|
| 50 |
self._closed = False
|
| 51 |
self._tool_registry: Dict[str, Callable] = {}
|
| 52 |
+
self._send_fail_count = 0
|
| 53 |
+
self._last_send_error_time = 0.0
|
| 54 |
|
| 55 |
def set_config(self, config_data: Dict[str, Any]) -> None:
|
| 56 |
self.config = config_data
|
|
|
|
| 93 |
for m in modalities
|
| 94 |
]
|
| 95 |
|
| 96 |
+
# Context window compression — essential for sessions longer than 15 min (audio) or 2 min (audio+video)
|
| 97 |
+
config.context_window_compression = types.ContextWindowCompressionConfig(
|
| 98 |
+
sliding_window=types.SlidingWindow()
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
# Tools / Function calling
|
| 102 |
tools_config = self.config.get("tools")
|
| 103 |
if tools_config:
|
|
|
|
| 227 |
)
|
| 228 |
except Exception as e:
|
| 229 |
if "closed" not in str(e).lower():
|
| 230 |
+
self._send_fail_count += 1
|
| 231 |
+
now = time.time()
|
| 232 |
+
if now - self._last_send_error_time > 5:
|
| 233 |
+
print(f"[DEBUG][{self.client_id}] Send audio error: {e}")
|
| 234 |
+
self._last_send_error_time = now
|
| 235 |
|
| 236 |
async def send_text(self, text: str) -> None:
|
| 237 |
"""Send text message"""
|
|
|
|
| 242 |
await self.session.send_client_content(turns=[content], turn_complete=True)
|
| 243 |
except Exception as e:
|
| 244 |
if "closed" not in str(e).lower():
|
| 245 |
+
self._send_fail_count += 1
|
| 246 |
+
now = time.time()
|
| 247 |
+
if now - self._last_send_error_time > 5:
|
| 248 |
+
print(f"[DEBUG][{self.client_id}] Send text error: {e}")
|
| 249 |
+
self._last_send_error_time = now
|
| 250 |
|
| 251 |
async def send_image(self, data: str) -> None:
|
| 252 |
"""Send image data frame"""
|
|
|
|
| 259 |
)
|
| 260 |
except Exception as e:
|
| 261 |
if "closed" not in str(e).lower():
|
| 262 |
+
self._send_fail_count += 1
|
| 263 |
+
now = time.time()
|
| 264 |
+
if now - self._last_send_error_time > 5:
|
| 265 |
+
print(f"[DEBUG][{self.client_id}] Send image error: {e}")
|
| 266 |
+
self._last_send_error_time = now
|
| 267 |
|
| 268 |
async def send_file(self, file_uri: str, mime_type: str) -> None:
|
| 269 |
"""Send file reference into the active session"""
|
|
|
|
| 285 |
)
|
| 286 |
except Exception as e:
|
| 287 |
if "closed" not in str(e).lower():
|
| 288 |
+
self._send_fail_count += 1
|
| 289 |
+
now = time.time()
|
| 290 |
+
if now - self._last_send_error_time > 5:
|
| 291 |
+
print(f"[DEBUG][{self.client_id}] Send file error: {e}")
|
| 292 |
+
self._last_send_error_time = now
|
| 293 |
# Gracefully swallow to keep session alive; caller notified via error packet
|
| 294 |
|
| 295 |
async def upload_file(self, file_bytes: bytes, mime_type: str) -> Dict[str, str]:
|
|
|
|
| 321 |
|
| 322 |
# GoAway notification
|
| 323 |
if hasattr(msg, "go_away") and msg.go_away:
|
| 324 |
+
time_left = msg.go_away.time_left
|
| 325 |
+
# Convert protobuf Duration / timedelta to seconds for JSON serialization
|
| 326 |
+
if hasattr(time_left, "total_seconds"):
|
| 327 |
+
time_left = time_left.total_seconds()
|
| 328 |
+
elif hasattr(time_left, "seconds"):
|
| 329 |
+
time_left = time_left.seconds + getattr(time_left, "nanos", 0) / 1e9
|
| 330 |
+
return {"type": "go_away", "time_left": time_left}
|
| 331 |
|
| 332 |
# Session resumption update
|
| 333 |
if hasattr(msg, "session_resumption_update") and msg.session_resumption_update:
|