Upload client.py
Browse files- backend/gemini/client.py +14 -1
backend/gemini/client.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
import os
|
| 2 |
import asyncio
|
| 3 |
import base64
|
|
@@ -232,6 +233,9 @@ class GeminiConnection:
|
|
| 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"""
|
|
@@ -247,6 +251,9 @@ class GeminiConnection:
|
|
| 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"""
|
|
@@ -264,6 +271,9 @@ class GeminiConnection:
|
|
| 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"""
|
|
@@ -290,6 +300,9 @@ class GeminiConnection:
|
|
| 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]:
|
|
@@ -297,7 +310,7 @@ class GeminiConnection:
|
|
| 297 |
try:
|
| 298 |
print(f"[DEBUG][{self.client_id}] π€ Uploading file ({len(file_bytes)} bytes, {mime_type})")
|
| 299 |
file_obj = await self.client.aio.files.upload(
|
| 300 |
-
file=file_bytes,
|
| 301 |
config={"mime_type": mime_type},
|
| 302 |
)
|
| 303 |
print(f"[DEBUG][{self.client_id}] β
File uploaded: {file_obj.uri}")
|
|
|
|
| 1 |
+
import io
|
| 2 |
import os
|
| 3 |
import asyncio
|
| 4 |
import base64
|
|
|
|
| 233 |
if now - self._last_send_error_time > 5:
|
| 234 |
print(f"[DEBUG][{self.client_id}] Send audio error: {e}")
|
| 235 |
self._last_send_error_time = now
|
| 236 |
+
if self._send_fail_count >= 5:
|
| 237 |
+
print(f"[DEBUG][{self.client_id}] π Too many send failures, closing connection")
|
| 238 |
+
await self.close()
|
| 239 |
|
| 240 |
async def send_text(self, text: str) -> None:
|
| 241 |
"""Send text message"""
|
|
|
|
| 251 |
if now - self._last_send_error_time > 5:
|
| 252 |
print(f"[DEBUG][{self.client_id}] Send text error: {e}")
|
| 253 |
self._last_send_error_time = now
|
| 254 |
+
if self._send_fail_count >= 5:
|
| 255 |
+
print(f"[DEBUG][{self.client_id}] π Too many send failures, closing connection")
|
| 256 |
+
await self.close()
|
| 257 |
|
| 258 |
async def send_image(self, data: str) -> None:
|
| 259 |
"""Send image data frame"""
|
|
|
|
| 271 |
if now - self._last_send_error_time > 5:
|
| 272 |
print(f"[DEBUG][{self.client_id}] Send image error: {e}")
|
| 273 |
self._last_send_error_time = now
|
| 274 |
+
if self._send_fail_count >= 5:
|
| 275 |
+
print(f"[DEBUG][{self.client_id}] π Too many send failures, closing connection")
|
| 276 |
+
await self.close()
|
| 277 |
|
| 278 |
async def send_file(self, file_uri: str, mime_type: str) -> None:
|
| 279 |
"""Send file reference into the active session"""
|
|
|
|
| 300 |
if now - self._last_send_error_time > 5:
|
| 301 |
print(f"[DEBUG][{self.client_id}] Send file error: {e}")
|
| 302 |
self._last_send_error_time = now
|
| 303 |
+
if self._send_fail_count >= 5:
|
| 304 |
+
print(f"[DEBUG][{self.client_id}] π Too many send failures, closing connection")
|
| 305 |
+
await self.close()
|
| 306 |
# Gracefully swallow to keep session alive; caller notified via error packet
|
| 307 |
|
| 308 |
async def upload_file(self, file_bytes: bytes, mime_type: str) -> Dict[str, str]:
|
|
|
|
| 310 |
try:
|
| 311 |
print(f"[DEBUG][{self.client_id}] π€ Uploading file ({len(file_bytes)} bytes, {mime_type})")
|
| 312 |
file_obj = await self.client.aio.files.upload(
|
| 313 |
+
file=io.BytesIO(file_bytes),
|
| 314 |
config={"mime_type": mime_type},
|
| 315 |
)
|
| 316 |
print(f"[DEBUG][{self.client_id}] β
File uploaded: {file_obj.uri}")
|