Update gemini_client/core.py
Browse files- gemini_client/core.py +49 -0
gemini_client/core.py
CHANGED
|
@@ -874,8 +874,57 @@ class AsyncChatbot:
|
|
| 874 |
continue
|
| 875 |
|
| 876 |
if not body:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 877 |
error_details = f"Failed to parse response body. No valid data found.\nStatus: {resp.status_code}\nResponse snippet: {resp.text[:2000]}..."
|
| 878 |
console.log(f"[red]{error_details}[/red]")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 879 |
return {"content": error_details, "error": True}
|
| 880 |
|
| 881 |
# Extract data from the response
|
|
|
|
| 874 |
continue
|
| 875 |
|
| 876 |
if not body:
|
| 877 |
+
# Attempt fallback: Deep search for content in all_parsed_parts
|
| 878 |
+
console.log("[yellow]Standard body not found. Attempting deep search for content...[/yellow]")
|
| 879 |
+
|
| 880 |
+
content_candidate = None
|
| 881 |
+
|
| 882 |
+
def deep_find_content(obj, min_len=50):
|
| 883 |
+
if isinstance(obj, str):
|
| 884 |
+
if len(obj) > min_len:
|
| 885 |
+
return obj
|
| 886 |
+
return None
|
| 887 |
+
if isinstance(obj, list):
|
| 888 |
+
# Heuristic: The answer is often in a list like [ "text", 1 ] or [ "text", 0 ]
|
| 889 |
+
if len(obj) == 2 and isinstance(obj[0], str) and isinstance(obj[1], int) and len(obj[0]) > min_len:
|
| 890 |
+
return obj[0]
|
| 891 |
+
|
| 892 |
+
for item in obj:
|
| 893 |
+
res = deep_find_content(item, min_len)
|
| 894 |
+
if res: return res
|
| 895 |
+
if isinstance(obj, dict):
|
| 896 |
+
for k, v in obj.items():
|
| 897 |
+
res = deep_find_content(v, min_len)
|
| 898 |
+
if res: return res
|
| 899 |
+
return None
|
| 900 |
+
|
| 901 |
+
for part in all_parsed_parts:
|
| 902 |
+
# Search validation: Look for strings that look like SRT or just substantial text
|
| 903 |
+
found = deep_find_content(part)
|
| 904 |
+
if found:
|
| 905 |
+
content_candidate = found
|
| 906 |
+
break
|
| 907 |
+
|
| 908 |
+
if content_candidate:
|
| 909 |
+
console.log("[green]✓ Found content via deep search![/green]")
|
| 910 |
+
# Construct valid result with found content
|
| 911 |
+
return {
|
| 912 |
+
"content": content_candidate,
|
| 913 |
+
"conversation_id": self.conversation_id,
|
| 914 |
+
"response_id": self.response_id,
|
| 915 |
+
"choice_id": self.choice_id,
|
| 916 |
+
"model": self.model.model_name,
|
| 917 |
+
"images": [],
|
| 918 |
+
"error": False,
|
| 919 |
+
"choices": [{"id": "fallback", "content": content_candidate}]
|
| 920 |
+
}
|
| 921 |
+
|
| 922 |
error_details = f"Failed to parse response body. No valid data found.\nStatus: {resp.status_code}\nResponse snippet: {resp.text[:2000]}..."
|
| 923 |
console.log(f"[red]{error_details}[/red]")
|
| 924 |
+
|
| 925 |
+
# Debug: Dump all_parsed_parts keys or summary
|
| 926 |
+
console.log(f"[red]Parsed parts count: {len(all_parsed_parts)}[/red]")
|
| 927 |
+
|
| 928 |
return {"content": error_details, "error": True}
|
| 929 |
|
| 930 |
# Extract data from the response
|