Skip to content

Commit c1030c5

Browse files
committed
fix: improve gemini stream response handling and URL generation
1 parent c5ad25c commit c1030c5

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

src/adapter/gemini.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,14 @@ export class GeminiAdapter extends BaseAdapter {
6060
topK: 40,
6161
topP: 0.95,
6262
maxOutputTokens: 8192,
63-
responseMimeType: "text/plain"
6463
}
6564
};
6665
}
6766

6867
public getTextGenerationUrl(_apiUrl: string): string {
6968
const operationName = this.isStreamEnabled() ? 'streamGenerateContent' : 'generateContent';
70-
return `${this.config.baseUrl}/${this.getModel()}:${operationName}`;
69+
const baseUrl = `${this.config.baseUrl}/${this.getModel()}:${operationName}`;
70+
return this.isStreamEnabled() ? `${baseUrl}?alt=sse` : baseUrl;
7171
}
7272

7373
public parseResponse(response: HttpResponse<GeminiResponse | OpenAiChatCompletion>): string {
@@ -115,15 +115,23 @@ export class GeminiAdapter extends BaseAdapter {
115115
targetText: string
116116
): string {
117117
try {
118-
const cleanedText = streamData.text.startsWith(',')
119-
? streamData.text.slice(1)
120-
: streamData.text;
118+
let cleanedText = streamData.text;
119+
120+
// Remove "data: " prefix if present
121+
if (cleanedText.startsWith('data: ')) {
122+
cleanedText = cleanedText.slice(5);
123+
}
124+
// Remove leading comma if present
125+
if (cleanedText.startsWith(',')) {
126+
cleanedText = cleanedText.slice(1);
127+
}
121128

122129
const parsedChunk = JSON.parse(cleanedText);
123130
const text = parsedChunk.candidates?.[0]?.content?.parts?.[0]?.text;
124131

125132
if (text) {
126133
targetText += text;
134+
127135
query.onStream({
128136
result: {
129137
from: query.detectFrom,

0 commit comments

Comments
 (0)