improved google gemini

This commit is contained in:
Mic 2025-08-28 21:04:00 +02:00
parent 4ef246afc3
commit 5a3ea0b380
3 changed files with 78 additions and 63 deletions

View file

@ -345,7 +345,7 @@ class MessagesArea extends HTMLElement {
} }
// Check for newlines in the batch // Check for newlines in the batch
if (tokens.includes('\n')) { if (tokens.endsWith('\n')) {
this.flushAccumulatingMessage(); this.flushAccumulatingMessage();
} }
} }

View file

@ -72,9 +72,10 @@ export class GoogleGemini {
fetchResponse = async (messages) => { fetchResponse = async (messages) => {
// Smart streaming: disabilita streaming per risposte piccole // Smart streaming: disabilita streaming per risposte piccole
const messageLength = messages.map(m => m.parts?.map(p => p.text).join('') || '').join(''); const messageLength = messages.map(m => m.parts?.map(p => p.text).join('') || '').join('').length;
const shouldStream = false; ///this.stream && (messageLength > 500 || !this.adaptiveStreaming); console.log(">>>>>>>>>> Google Gemini messageLength: " + messageLength);
//console.log(">>>>>>>>>> Google Gemini shouldStream: " + shouldStream); const shouldStream = this.stream && (messageLength > 200 || !this.adaptiveStreaming);
console.log(">>>>>>>>>> Google Gemini shouldStream: " + shouldStream);
try { try {
let google_gemini_body = { let google_gemini_body = {
contents:messages contents:messages

View file

@ -42,16 +42,15 @@ let tokenBatch = '';
let batchTimer = null; let batchTimer = null;
let timeoutTimer = null; let timeoutTimer = null;
let lastBatchTime = 0; let lastBatchTime = 0;
let batchStartTime = 0;
// Function to send batched tokens // Function to send batched tokens
function sendTokenBatch(force = false, reason = 'unknown') { function sendTokenBatch(force = false, reason = 'unknown') {
if (tokenBatch && (force || tokenBatch.length >= TOKEN_BATCH_SIZE || performance.now() - lastBatchTime >= TOKEN_BATCH_DELAY)) { if (tokenBatch && (force || tokenBatch.length >= TOKEN_BATCH_SIZE || performance.now() - lastBatchTime >= TOKEN_BATCH_DELAY)) {
console.log(`>>>>>>>>>>>>> Sending token batch (reason: ${reason}):`, tokenBatch);
postMessage({ type: 'tokenBatch', payload: { tokens: tokenBatch } }); postMessage({ type: 'tokenBatch', payload: { tokens: tokenBatch } });
// Reset batch state // Reset batch state
tokenBatch = ''; tokenBatch = '';
lastBatchTime = performance.now(); lastBatchTime = performance.now();
batchStartTime = 0;
// Clear all timers // Clear all timers
if (batchTimer) { if (batchTimer) {
clearTimeout(batchTimer); clearTimeout(batchTimer);
@ -67,10 +66,6 @@ function sendTokenBatch(force = false, reason = 'unknown') {
// Function to add token to batch // Function to add token to batch
function addTokenToBatch(token) { function addTokenToBatch(token) {
tokenBatch += token; tokenBatch += token;
// Set batch start time for the first token
if (tokenBatch.length === token.length) {
batchStartTime = performance.now();
}
// Send immediately if batch is full // Send immediately if batch is full
if (tokenBatch.length >= TOKEN_BATCH_SIZE) { if (tokenBatch.length >= TOKEN_BATCH_SIZE) {
sendTokenBatch(true, 'size-limit'); sendTokenBatch(true, 'size-limit');
@ -119,6 +114,11 @@ self.onmessage = async function(event) {
throw new Error("[ThunderAI] Google Gemini API request failed: " + response.status + " " + response.statusText + ", Detail: " + error_message + " " + errorDetail); throw new Error("[ThunderAI] Google Gemini API request failed: " + response.status + " " + response.statusText + ", Detail: " + error_message + " " + errorDetail);
} }
// Check if the response is streaming (SSE/chunks)
const contentType = response.headers.get('content-type') || '';
const isStreaming = contentType.includes('text/event-stream') || contentType.includes('application/x-ndjson');
if (isStreaming) {
const reader = response.body.getReader(); const reader = response.body.getReader();
const decoder = new TextDecoder("utf-8"); const decoder = new TextDecoder("utf-8");
let buffer = ''; let buffer = '';
@ -175,6 +175,20 @@ self.onmessage = async function(event) {
} }
} }
} }
} else {
// Non-streaming: send the entire text in a single batch
try {
const responseJson = await response.json();
const text = responseJson.candidates?.[0]?.content?.parts?.[0]?.text || '';
assistantResponseAccumulator = text;
postMessage({ type: 'tokenBatch', payload: { tokens: text } });
postMessage({ type: 'tokensDone' });
conversationHistory.push({ role: 'model', parts: [{ "text": assistantResponseAccumulator }] });
assistantResponseAccumulator = '';
} catch (e) {
taLog.error("Error parsing non-streaming response: " + e);
}
}
} else if (event.data.type === 'stop') { } else if (event.data.type === 'stop') {
stopStreaming = true; stopStreaming = true;
} }