1
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { ModelOption, ExpertResult } from '../../types';
|
||||
import { getSynthesisPrompt } from './prompts';
|
||||
import { withRetry } from '../utils/retry';
|
||||
|
||||
export const streamSynthesisResponse = async (
|
||||
ai: any,
|
||||
@@ -13,7 +14,7 @@ export const streamSynthesisResponse = async (
|
||||
): Promise<void> => {
|
||||
const prompt = getSynthesisPrompt(historyContext, query, expertResults);
|
||||
|
||||
const synthesisStream = await ai.models.generateContentStream({
|
||||
const synthesisStream = await withRetry(() => ai.models.generateContentStream({
|
||||
model: model,
|
||||
contents: prompt,
|
||||
config: {
|
||||
@@ -22,23 +23,28 @@ export const streamSynthesisResponse = async (
|
||||
includeThoughts: true
|
||||
}
|
||||
}
|
||||
});
|
||||
}));
|
||||
|
||||
for await (const chunk of synthesisStream) {
|
||||
if (signal.aborted) break;
|
||||
try {
|
||||
for await (const chunk of synthesisStream) {
|
||||
if (signal.aborted) break;
|
||||
|
||||
let chunkText = "";
|
||||
let chunkThought = "";
|
||||
let chunkText = "";
|
||||
let chunkThought = "";
|
||||
|
||||
if (chunk.candidates?.[0]?.content?.parts) {
|
||||
for (const part of chunk.candidates[0].content.parts) {
|
||||
if (part.thought) {
|
||||
chunkThought += (part.text || "");
|
||||
} else if (part.text) {
|
||||
chunkText += part.text;
|
||||
}
|
||||
}
|
||||
onChunk(chunkText, chunkThought);
|
||||
if (chunk.candidates?.[0]?.content?.parts) {
|
||||
for (const part of chunk.candidates[0].content.parts) {
|
||||
if (part.thought) {
|
||||
chunkThought += (part.text || "");
|
||||
} else if (part.text) {
|
||||
chunkText += part.text;
|
||||
}
|
||||
}
|
||||
onChunk(chunkText, chunkThought);
|
||||
}
|
||||
}
|
||||
} catch (streamError) {
|
||||
console.error("Synthesis stream interrupted:", streamError);
|
||||
throw streamError;
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user