Update backend/gemini/client.py
Browse files- backend/gemini/client.py +49 -8
backend/gemini/client.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import os
|
| 2 |
import asyncio
|
|
|
|
| 3 |
from typing import Dict, Optional, Any
|
| 4 |
from google import genai
|
| 5 |
from google.genai import types
|
|
@@ -63,8 +64,7 @@ class GeminiConnection:
|
|
| 63 |
)
|
| 64 |
)
|
| 65 |
|
| 66 |
-
# Add session resumption using SessionResumptionConfig
|
| 67 |
-
# IMPORTANT: Do NOT set transparent=True on Gemini API [citation:2]
|
| 68 |
if resumption_handle:
|
| 69 |
live_config.session_resumption = types.SessionResumptionConfig(
|
| 70 |
handle=resumption_handle
|
|
@@ -113,17 +113,38 @@ class GeminiConnection:
|
|
| 113 |
await self._receive_queue.put(None)
|
| 114 |
|
| 115 |
async def send_audio(self, data: str) -> None:
|
| 116 |
-
"""
|
|
|
|
|
|
|
|
|
|
| 117 |
if not self.session:
|
|
|
|
| 118 |
return
|
| 119 |
try:
|
| 120 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
except Exception as e:
|
| 122 |
print(f"[DEBUG][{self.client_id}] Send audio error: {e}")
|
|
|
|
|
|
|
| 123 |
|
| 124 |
async def send_text(self, text: str) -> None:
|
| 125 |
"""Send text message to Gemini"""
|
| 126 |
if not self.session:
|
|
|
|
| 127 |
return
|
| 128 |
try:
|
| 129 |
await self.session.send_client_content(
|
|
@@ -134,17 +155,37 @@ class GeminiConnection:
|
|
| 134 |
print(f"[DEBUG][{self.client_id}] Send text error: {e}")
|
| 135 |
|
| 136 |
async def send_image(self, data: str) -> None:
|
| 137 |
-
"""
|
|
|
|
|
|
|
|
|
|
| 138 |
if not self.session:
|
|
|
|
| 139 |
return
|
| 140 |
try:
|
| 141 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
except Exception as e:
|
| 143 |
print(f"[DEBUG][{self.client_id}] Send image error: {e}")
|
|
|
|
|
|
|
| 144 |
|
| 145 |
async def send_file(self, file_uri: str, mime_type: str) -> None:
|
| 146 |
"""Send file reference to Gemini"""
|
| 147 |
if not self.session:
|
|
|
|
| 148 |
return
|
| 149 |
try:
|
| 150 |
await self.session.send_client_content(
|
|
@@ -171,7 +212,7 @@ class GeminiConnection:
|
|
| 171 |
"""Parse SDK message - following official docs field names"""
|
| 172 |
result = {}
|
| 173 |
|
| 174 |
-
# Session resumption update
|
| 175 |
if hasattr(message, 'session_resumption_update') and message.session_resumption_update:
|
| 176 |
update = message.session_resumption_update
|
| 177 |
result["session_resumption_update"] = {
|
|
@@ -186,7 +227,7 @@ class GeminiConnection:
|
|
| 186 |
self.resumption_handle = result["session_resumption_update"]["new_handle"]
|
| 187 |
print(f"[DEBUG][{self.client_id}] π Session handle stored: {self.resumption_handle[:50]}...")
|
| 188 |
|
| 189 |
-
# Go away message
|
| 190 |
if hasattr(message, 'go_away') and message.go_away:
|
| 191 |
go_away = message.go_away
|
| 192 |
result["go_away"] = {
|
|
|
|
| 1 |
import os
|
| 2 |
import asyncio
|
| 3 |
+
import base64
|
| 4 |
from typing import Dict, Optional, Any
|
| 5 |
from google import genai
|
| 6 |
from google.genai import types
|
|
|
|
| 64 |
)
|
| 65 |
)
|
| 66 |
|
| 67 |
+
# Add session resumption using SessionResumptionConfig
|
|
|
|
| 68 |
if resumption_handle:
|
| 69 |
live_config.session_resumption = types.SessionResumptionConfig(
|
| 70 |
handle=resumption_handle
|
|
|
|
| 113 |
await self._receive_queue.put(None)
|
| 114 |
|
| 115 |
async def send_audio(self, data: str) -> None:
|
| 116 |
+
"""
|
| 117 |
+
Send audio data to Gemini using the correct SDK structure.
|
| 118 |
+
The data is expected to be base64-encoded PCM audio.
|
| 119 |
+
"""
|
| 120 |
if not self.session:
|
| 121 |
+
print(f"[DEBUG][{self.client_id}] β Cannot send audio: no session")
|
| 122 |
return
|
| 123 |
try:
|
| 124 |
+
# Decode base64 string to bytes if needed
|
| 125 |
+
# Assuming data is base64 string from frontend
|
| 126 |
+
audio_bytes = base64.b64decode(data) if isinstance(data, str) else data
|
| 127 |
+
|
| 128 |
+
# Use the correct structure with Part and Blob
|
| 129 |
+
await self.session.send_realtime_input(
|
| 130 |
+
media_chunks=[
|
| 131 |
+
types.Part(
|
| 132 |
+
inline_data=types.Blob(
|
| 133 |
+
data=audio_bytes,
|
| 134 |
+
mime_type="audio/pcm"
|
| 135 |
+
)
|
| 136 |
+
)
|
| 137 |
+
]
|
| 138 |
+
)
|
| 139 |
except Exception as e:
|
| 140 |
print(f"[DEBUG][{self.client_id}] Send audio error: {e}")
|
| 141 |
+
import traceback
|
| 142 |
+
traceback.print_exc()
|
| 143 |
|
| 144 |
async def send_text(self, text: str) -> None:
|
| 145 |
"""Send text message to Gemini"""
|
| 146 |
if not self.session:
|
| 147 |
+
print(f"[DEBUG][{self.client_id}] β Cannot send text: no session")
|
| 148 |
return
|
| 149 |
try:
|
| 150 |
await self.session.send_client_content(
|
|
|
|
| 155 |
print(f"[DEBUG][{self.client_id}] Send text error: {e}")
|
| 156 |
|
| 157 |
async def send_image(self, data: str) -> None:
|
| 158 |
+
"""
|
| 159 |
+
Send image data to Gemini using the correct SDK structure.
|
| 160 |
+
The data is expected to be base64-encoded JPEG image.
|
| 161 |
+
"""
|
| 162 |
if not self.session:
|
| 163 |
+
print(f"[DEBUG][{self.client_id}] β Cannot send image: no session")
|
| 164 |
return
|
| 165 |
try:
|
| 166 |
+
# Decode base64 string to bytes
|
| 167 |
+
image_bytes = base64.b64decode(data) if isinstance(data, str) else data
|
| 168 |
+
|
| 169 |
+
# Use the correct structure with Part and Blob
|
| 170 |
+
await self.session.send_realtime_input(
|
| 171 |
+
media_chunks=[
|
| 172 |
+
types.Part(
|
| 173 |
+
inline_data=types.Blob(
|
| 174 |
+
data=image_bytes,
|
| 175 |
+
mime_type="image/jpeg"
|
| 176 |
+
)
|
| 177 |
+
)
|
| 178 |
+
]
|
| 179 |
+
)
|
| 180 |
except Exception as e:
|
| 181 |
print(f"[DEBUG][{self.client_id}] Send image error: {e}")
|
| 182 |
+
import traceback
|
| 183 |
+
traceback.print_exc()
|
| 184 |
|
| 185 |
async def send_file(self, file_uri: str, mime_type: str) -> None:
|
| 186 |
"""Send file reference to Gemini"""
|
| 187 |
if not self.session:
|
| 188 |
+
print(f"[DEBUG][{self.client_id}] β Cannot send file: no session")
|
| 189 |
return
|
| 190 |
try:
|
| 191 |
await self.session.send_client_content(
|
|
|
|
| 212 |
"""Parse SDK message - following official docs field names"""
|
| 213 |
result = {}
|
| 214 |
|
| 215 |
+
# Session resumption update
|
| 216 |
if hasattr(message, 'session_resumption_update') and message.session_resumption_update:
|
| 217 |
update = message.session_resumption_update
|
| 218 |
result["session_resumption_update"] = {
|
|
|
|
| 227 |
self.resumption_handle = result["session_resumption_update"]["new_handle"]
|
| 228 |
print(f"[DEBUG][{self.client_id}] π Session handle stored: {self.resumption_handle[:50]}...")
|
| 229 |
|
| 230 |
+
# Go away message
|
| 231 |
if hasattr(message, 'go_away') and message.go_away:
|
| 232 |
go_away = message.go_away
|
| 233 |
result["go_away"] = {
|