refactor: zero-latency stream and ui cleanup
This commit is contained in:
+26
-16
@@ -53,21 +53,18 @@ class SXNGPlugin(Plugin):
|
|||||||
payload = {"contents": [{"parts": [{"text": prompt}]}], "generationConfig": {"maxOutputTokens": self.max_tokens, "temperature": self.temperature}}
|
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"})
|
conn.request("POST", path, body=json.dumps(payload), headers={"Content-Type": "application/json"})
|
||||||
res = conn.getresponse()
|
res = conn.getresponse()
|
||||||
|
if res.status != 200: return
|
||||||
if res.status != 200:
|
|
||||||
return
|
|
||||||
|
|
||||||
decoder = json.JSONDecoder()
|
decoder = json.JSONDecoder()
|
||||||
buffer = ""
|
buffer = ""
|
||||||
|
while True:
|
||||||
for chunk in res:
|
chunk = res.read(128)
|
||||||
if not chunk: continue
|
if not chunk: break
|
||||||
buffer += chunk.decode('utf-8')
|
buffer += chunk.decode('utf-8')
|
||||||
|
|
||||||
while buffer:
|
while buffer:
|
||||||
buffer = buffer.lstrip()
|
buffer = buffer.lstrip()
|
||||||
if not buffer: break
|
if not buffer: break
|
||||||
|
|
||||||
try:
|
try:
|
||||||
obj, idx = decoder.raw_decode(buffer)
|
obj, idx = decoder.raw_decode(buffer)
|
||||||
candidates = obj.get('candidates', [])
|
candidates = obj.get('candidates', [])
|
||||||
@@ -77,14 +74,10 @@ class SXNGPlugin(Plugin):
|
|||||||
if parts:
|
if parts:
|
||||||
text = parts[0].get('text', '')
|
text = parts[0].get('text', '')
|
||||||
if text: yield text
|
if text: yield text
|
||||||
|
|
||||||
buffer = buffer[idx:]
|
buffer = buffer[idx:]
|
||||||
except json.JSONDecodeError:
|
except json.JSONDecodeError: break
|
||||||
break
|
|
||||||
|
|
||||||
conn.close()
|
conn.close()
|
||||||
except Exception:
|
except Exception: pass
|
||||||
pass
|
|
||||||
|
|
||||||
def generate_openrouter():
|
def generate_openrouter():
|
||||||
try:
|
try:
|
||||||
@@ -108,7 +101,7 @@ class SXNGPlugin(Plugin):
|
|||||||
|
|
||||||
buffer = ""
|
buffer = ""
|
||||||
while True:
|
while True:
|
||||||
chunk = res.read(1024)
|
chunk = res.read(128)
|
||||||
if not chunk: break
|
if not chunk: break
|
||||||
buffer += chunk.decode('utf-8')
|
buffer += chunk.decode('utf-8')
|
||||||
while "\n" in buffer:
|
while "\n" in buffer:
|
||||||
@@ -142,7 +135,22 @@ class SXNGPlugin(Plugin):
|
|||||||
js_q = json.dumps(search.search_query.query)
|
js_q = json.dumps(search.search_query.query)
|
||||||
|
|
||||||
html_payload = f'''
|
html_payload = f'''
|
||||||
<article id="sxng-stream-box" class="answer" style="display:none; margin-bottom: 1rem;">
|
<style>
|
||||||
|
#sxng-stream-box {{
|
||||||
|
display: none !important;
|
||||||
|
padding: 1rem !important;
|
||||||
|
margin-top: 0 !important;
|
||||||
|
margin-bottom: 1.5rem !important;
|
||||||
|
border-bottom: 1px solid var(--color-result-border);
|
||||||
|
}}
|
||||||
|
#sxng-stream-data {{
|
||||||
|
margin: 0 !important;
|
||||||
|
line-height: 1.6;
|
||||||
|
font-size: 0.95rem;
|
||||||
|
color: var(--color-result-description);
|
||||||
|
}}
|
||||||
|
</style>
|
||||||
|
<article id="sxng-stream-box" class="answer">
|
||||||
<p id="sxng-stream-data" style="white-space: pre-wrap;"></p>
|
<p id="sxng-stream-data" style="white-space: pre-wrap;"></p>
|
||||||
</article>
|
</article>
|
||||||
<script>
|
<script>
|
||||||
@@ -171,7 +179,9 @@ class SXNGPlugin(Plugin):
|
|||||||
|
|
||||||
const chunk = decoder.decode(value);
|
const chunk = decoder.decode(value);
|
||||||
if (chunk) {{
|
if (chunk) {{
|
||||||
if (shell.style.display === 'none') shell.style.display = 'block';
|
if (shell.style.getPropertyValue('display') !== 'block') {{
|
||||||
|
shell.style.setProperty('display', 'block', 'important');
|
||||||
|
}}
|
||||||
data.innerText += chunk;
|
data.innerText += chunk;
|
||||||
}}
|
}}
|
||||||
}}
|
}}
|
||||||
|
|||||||
Reference in New Issue
Block a user