diff --git a/gemini_flash.py b/gemini_flash.py index 6ccadf3..00080f5 100644 --- a/gemini_flash.py +++ b/gemini_flash.py @@ -53,21 +53,18 @@ class SXNGPlugin(Plugin): payload = {"contents": [{"parts": [{"text": prompt}]}], "generationConfig": {"maxOutputTokens": self.max_tokens, "temperature": self.temperature}} conn.request("POST", path, body=json.dumps(payload), headers={"Content-Type": "application/json"}) res = conn.getresponse() - - if res.status != 200: - return + if res.status != 200: return decoder = json.JSONDecoder() buffer = "" - - for chunk in res: - if not chunk: continue + while True: + chunk = res.read(128) + if not chunk: break buffer += chunk.decode('utf-8') while buffer: buffer = buffer.lstrip() if not buffer: break - try: obj, idx = decoder.raw_decode(buffer) candidates = obj.get('candidates', []) @@ -77,14 +74,10 @@ class SXNGPlugin(Plugin): if parts: text = parts[0].get('text', '') if text: yield text - buffer = buffer[idx:] - except json.JSONDecodeError: - break - + except json.JSONDecodeError: break conn.close() - except Exception: - pass + except Exception: pass def generate_openrouter(): try: @@ -108,7 +101,7 @@ class SXNGPlugin(Plugin): buffer = "" while True: - chunk = res.read(1024) + chunk = res.read(128) if not chunk: break buffer += chunk.decode('utf-8') while "\n" in buffer: @@ -142,7 +135,22 @@ class SXNGPlugin(Plugin): js_q = json.dumps(search.search_query.query) html_payload = f''' -