Luca448 commited on
Commit
8204e40
·
1 Parent(s): 3f87689

Fix: correctly concatenate chunked multi-part responses from gemini-3.5 API which caused responses to arbitrarily truncate mid-sentence

Browse files
Files changed (3) hide show
  1. index.html +1 -1
  2. server/news-service.js +11 -8
  3. src/ai/gemini.js +1 -2
index.html CHANGED
@@ -51,7 +51,7 @@
51
  </head>
52
  <body class="dark-theme in-chat-view">
53
 
54
- <div id="live-version-badge" style="position: fixed; top: 50px; left: 50%; transform: translateX(-50%); font-size: 10px; color: var(--text-secondary); opacity: 0.6; z-index: 99999; pointer-events: none; font-weight: 500; font-family: monospace; letter-spacing: 1px; background: rgba(0,0,0,0.2); padding: 2px 8px; border-radius: 10px; border: 1px solid rgba(255,255,255,0.05);">BUILD 1.3.8</div>
55
 
56
  <!-- SVG Filters for Liquid Glass Refraction -->
57
  <svg width="0" height="0" style="position: absolute; pointer-events: none;">
 
51
  </head>
52
  <body class="dark-theme in-chat-view">
53
 
54
+ <div id="live-version-badge" style="position: fixed; top: 50px; left: 50%; transform: translateX(-50%); font-size: 10px; color: var(--text-secondary); opacity: 0.6; z-index: 99999; pointer-events: none; font-weight: 500; font-family: monospace; letter-spacing: 1px; background: rgba(0,0,0,0.2); padding: 2px 8px; border-radius: 10px; border: 1px solid rgba(255,255,255,0.05);">BUILD 1.3.9</div>
55
 
56
  <!-- SVG Filters for Liquid Glass Refraction -->
57
  <svg width="0" height="0" style="position: absolute; pointer-events: none;">
server/news-service.js CHANGED
@@ -149,8 +149,10 @@ Antworte nur mit dem Markdown-Text des Artikels, oder "SKIP".`;
149
  };
150
 
151
  const cascade = [
152
- { name: 'gemini-2.5-flash', shouldSimulateError: simulateError },
153
- { name: 'gemini-1.5-flash', shouldSimulateError: false }
 
 
154
  ];
155
 
156
  for (const step of cascade) {
@@ -174,25 +176,26 @@ Antworte nur mit dem Markdown-Text des Artikels, oder "SKIP".`;
174
 
175
  if (!response.ok) {
176
  if (response.status === 404) {
177
- // If fake future model 3.5 doesn't exist, we fallback to 1.5-flash to keep it working
178
- console.log(`[NewsService] Model ${step.name} not found (404), falling back to gemini-1.5-flash`);
179
- const fbResponse = await fetch(`https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=${apiKey}`, {
180
  method: 'POST',
181
  headers: { 'Content-Type': 'application/json' },
182
  body: JSON.stringify(body)
183
  });
184
  if (!fbResponse.ok) throw new Error(`Fallback Error: ${await fbResponse.text()}`);
185
  const fbJson = await fbResponse.json();
186
- return fbJson.candidates?.[0]?.content?.parts?.[0]?.text?.trim() || 'SKIP';
 
187
  }
188
  const errText = await response.text();
189
  throw new Error(`API Error ${response.status}: ${errText}`);
190
  }
191
 
192
  const resJson = await response.json();
193
- const text = resJson.candidates?.[0]?.content?.parts?.[0]?.text;
 
194
  console.log(`[NewsService] Successfully generated with ${step.name}`);
195
- return text?.trim() || 'SKIP';
196
  } catch (e) {
197
  console.error(`[NewsService] Failed with ${step.name}: ${e.message}`);
198
  // Continue to next model in cascade
 
149
  };
150
 
151
  const cascade = [
152
+ { name: 'gemini-3.5-flash', shouldSimulateError: simulateError },
153
+ { name: 'gemini-3.5-flash', isRetry: true, delayMs: 10000, shouldSimulateError: simulateError },
154
+ { name: 'gemini-3.1-flash-lite', shouldSimulateError: false },
155
+ { name: 'gemini-2.5-flash', shouldSimulateError: false }
156
  ];
157
 
158
  for (const step of cascade) {
 
176
 
177
  if (!response.ok) {
178
  if (response.status === 404) {
179
+ console.log(`[NewsService] Model ${step.name} not found (404), falling back to gemini-2.5-flash`);
180
+ const fbResponse = await fetch(`https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent?key=${apiKey}`, {
 
181
  method: 'POST',
182
  headers: { 'Content-Type': 'application/json' },
183
  body: JSON.stringify(body)
184
  });
185
  if (!fbResponse.ok) throw new Error(`Fallback Error: ${await fbResponse.text()}`);
186
  const fbJson = await fbResponse.json();
187
+ const fbParts = fbJson.candidates?.[0]?.content?.parts || [];
188
+ return fbParts.map(p => p.text || '').join('').trim() || 'SKIP';
189
  }
190
  const errText = await response.text();
191
  throw new Error(`API Error ${response.status}: ${errText}`);
192
  }
193
 
194
  const resJson = await response.json();
195
+ const parts = resJson.candidates?.[0]?.content?.parts || [];
196
+ const text = parts.map(p => p.text || '').join('');
197
  console.log(`[NewsService] Successfully generated with ${step.name}`);
198
+ return text.trim() || 'SKIP';
199
  } catch (e) {
200
  console.error(`[NewsService] Failed with ${step.name}: ${e.message}`);
201
  // Continue to next model in cascade
src/ai/gemini.js CHANGED
@@ -171,11 +171,10 @@ export class GeminiClient {
171
  }
172
  }]
173
  });
174
-
175
  // Loop continues
176
  } else {
177
  // Normal text response
178
- return part?.text || "";
179
  }
180
  }
181
 
 
171
  }
172
  }]
173
  });
 
174
  // Loop continues
175
  } else {
176
  // Normal text response
177
+ return parts.map(p => p.text || '').join('');
178
  }
179
  }
180