Update backend/gemini/client.py
Browse files- backend/gemini/client.py +23 -14
backend/gemini/client.py
CHANGED
|
@@ -55,17 +55,16 @@ class GeminiConnection:
|
|
| 55 |
)
|
| 56 |
)
|
| 57 |
|
| 58 |
-
# Add session resumption
|
|
|
|
| 59 |
if resumption_handle:
|
| 60 |
# Reconnecting - provide the handle
|
| 61 |
-
# IMPORTANT: Do NOT set 'transparent=True' for Gemini API
|
| 62 |
live_config.session_resumption = types.SessionResumption(
|
| 63 |
handle=resumption_handle
|
| 64 |
)
|
| 65 |
print(f"[DEBUG][{self.client_id}] π Using resumption handle: {resumption_handle[:30]}...")
|
| 66 |
else:
|
| 67 |
# New session - enable session resumption
|
| 68 |
-
# Empty SessionResumption object enables the feature
|
| 69 |
live_config.session_resumption = types.SessionResumption()
|
| 70 |
print(f"[DEBUG][{self.client_id}] π Creating new session with resumption enabled")
|
| 71 |
|
|
@@ -113,7 +112,11 @@ class GeminiConnection:
|
|
| 113 |
print(f"[DEBUG][{self.client_id}] β Cannot send text: no session")
|
| 114 |
return
|
| 115 |
try:
|
| 116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
except Exception as e:
|
| 118 |
print(f"[DEBUG][{self.client_id}] Send text error: {e}")
|
| 119 |
|
|
@@ -142,11 +145,22 @@ class GeminiConnection:
|
|
| 142 |
print(f"[DEBUG][{self.client_id}] Send file error: {e}")
|
| 143 |
|
| 144 |
async def receive(self) -> Optional[Dict]:
|
| 145 |
-
"""Receive next message from Gemini"""
|
| 146 |
if not self.session:
|
| 147 |
return None
|
| 148 |
try:
|
| 149 |
async for message in self.session.receive():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
return self._parse_message(message)
|
| 151 |
except Exception as e:
|
| 152 |
print(f"[DEBUG][{self.client_id}] Receive error: {e}")
|
|
@@ -156,7 +170,7 @@ class GeminiConnection:
|
|
| 156 |
"""Parse SDK message - using snake_case field names as per official docs"""
|
| 157 |
result = {}
|
| 158 |
|
| 159 |
-
# Session resumption update (
|
| 160 |
if hasattr(message, 'session_resumption_update') and message.session_resumption_update:
|
| 161 |
update = message.session_resumption_update
|
| 162 |
result["session_resumption_update"] = {
|
|
@@ -166,20 +180,15 @@ class GeminiConnection:
|
|
| 166 |
update, 'last_consumed_client_message_index', None
|
| 167 |
)
|
| 168 |
}
|
| 169 |
-
# Store handle if resumable
|
| 170 |
-
if result["session_resumption_update"]["resumable"] and result["session_resumption_update"]["new_handle"]:
|
| 171 |
-
self.resumption_handle = result["session_resumption_update"]["new_handle"]
|
| 172 |
-
self.last_consumed_index = result["session_resumption_update"]["last_consumed_client_message_index"]
|
| 173 |
-
print(f"[DEBUG][{self.client_id}] π Session handle stored: {self.resumption_handle[:50]}...")
|
| 174 |
|
| 175 |
-
# Go away message
|
| 176 |
if hasattr(message, 'go_away') and message.go_away:
|
| 177 |
go_away = message.go_away
|
| 178 |
result["go_away"] = {
|
| 179 |
"time_left": getattr(go_away, 'time_left', 0)
|
| 180 |
}
|
| 181 |
|
| 182 |
-
# Server content
|
| 183 |
if hasattr(message, 'server_content') and message.server_content:
|
| 184 |
result["server_content"] = self._parse_server_content(message.server_content)
|
| 185 |
|
|
@@ -220,7 +229,7 @@ class GeminiConnection:
|
|
| 220 |
part_dict["text"] = part.text
|
| 221 |
content["model_turn"]["parts"].append(part_dict)
|
| 222 |
|
| 223 |
-
# Flags
|
| 224 |
if hasattr(server_content, 'turn_complete'):
|
| 225 |
content["turn_complete"] = server_content.turn_complete
|
| 226 |
|
|
|
|
| 55 |
)
|
| 56 |
)
|
| 57 |
|
| 58 |
+
# Add session resumption - Use SessionResumption (not SessionResumptionConfig)
|
| 59 |
+
# IMPORTANT: Do NOT set transparent=True for Gemini API - it's not supported [citation:2]
|
| 60 |
if resumption_handle:
|
| 61 |
# Reconnecting - provide the handle
|
|
|
|
| 62 |
live_config.session_resumption = types.SessionResumption(
|
| 63 |
handle=resumption_handle
|
| 64 |
)
|
| 65 |
print(f"[DEBUG][{self.client_id}] π Using resumption handle: {resumption_handle[:30]}...")
|
| 66 |
else:
|
| 67 |
# New session - enable session resumption
|
|
|
|
| 68 |
live_config.session_resumption = types.SessionResumption()
|
| 69 |
print(f"[DEBUG][{self.client_id}] π Creating new session with resumption enabled")
|
| 70 |
|
|
|
|
| 112 |
print(f"[DEBUG][{self.client_id}] β Cannot send text: no session")
|
| 113 |
return
|
| 114 |
try:
|
| 115 |
+
# Follow official sample format
|
| 116 |
+
await self.session.send_client_content(
|
| 117 |
+
turns=[text],
|
| 118 |
+
turn_complete=True
|
| 119 |
+
)
|
| 120 |
except Exception as e:
|
| 121 |
print(f"[DEBUG][{self.client_id}] Send text error: {e}")
|
| 122 |
|
|
|
|
| 145 |
print(f"[DEBUG][{self.client_id}] Send file error: {e}")
|
| 146 |
|
| 147 |
async def receive(self) -> Optional[Dict]:
|
| 148 |
+
"""Receive next message from Gemini following official sample pattern"""
|
| 149 |
if not self.session:
|
| 150 |
return None
|
| 151 |
try:
|
| 152 |
async for message in self.session.receive():
|
| 153 |
+
# Follow the official sample: check for session_resumption_update
|
| 154 |
+
if hasattr(message, 'session_resumption_update') and message.session_resumption_update:
|
| 155 |
+
update = message.session_resumption_update
|
| 156 |
+
# Store handle if resumable and has new_handle
|
| 157 |
+
if update.resumable and update.new_handle:
|
| 158 |
+
self.resumption_handle = update.new_handle
|
| 159 |
+
print(f"[DEBUG][{self.client_id}] π Session handle stored: {self.resumption_handle[:50]}...")
|
| 160 |
+
# Return the update to be forwarded to client
|
| 161 |
+
return self._parse_message(message)
|
| 162 |
+
|
| 163 |
+
# Return any message to be forwarded
|
| 164 |
return self._parse_message(message)
|
| 165 |
except Exception as e:
|
| 166 |
print(f"[DEBUG][{self.client_id}] Receive error: {e}")
|
|
|
|
| 170 |
"""Parse SDK message - using snake_case field names as per official docs"""
|
| 171 |
result = {}
|
| 172 |
|
| 173 |
+
# Session resumption update (from official sample)
|
| 174 |
if hasattr(message, 'session_resumption_update') and message.session_resumption_update:
|
| 175 |
update = message.session_resumption_update
|
| 176 |
result["session_resumption_update"] = {
|
|
|
|
| 180 |
update, 'last_consumed_client_message_index', None
|
| 181 |
)
|
| 182 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
|
| 184 |
+
# Go away message
|
| 185 |
if hasattr(message, 'go_away') and message.go_away:
|
| 186 |
go_away = message.go_away
|
| 187 |
result["go_away"] = {
|
| 188 |
"time_left": getattr(go_away, 'time_left', 0)
|
| 189 |
}
|
| 190 |
|
| 191 |
+
# Server content
|
| 192 |
if hasattr(message, 'server_content') and message.server_content:
|
| 193 |
result["server_content"] = self._parse_server_content(message.server_content)
|
| 194 |
|
|
|
|
| 229 |
part_dict["text"] = part.text
|
| 230 |
content["model_turn"]["parts"].append(part_dict)
|
| 231 |
|
| 232 |
+
# Flags from official sample
|
| 233 |
if hasattr(server_content, 'turn_complete'):
|
| 234 |
content["turn_complete"] = server_content.turn_complete
|
| 235 |
|