Skip to content

Commit 5a7e8f0

Browse files
committed
feat: enhance reasoning handling in Gemini and Vertex handlers
1 parent 3b26b07 commit 5a7e8f0

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

src/api/providers/gemini.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,28 @@ export class GeminiHandler extends BaseProvider implements SingleCompletionHandl
8181
let lastUsageMetadata: GenerateContentResponseUsageMetadata | undefined
8282

8383
for await (const chunk of result) {
84-
if (chunk.text) {
84+
// Process candidates and their parts to separate thoughts from content
85+
if (chunk.candidates && chunk.candidates.length > 0) {
86+
const candidate = chunk.candidates[0]
87+
if (candidate.content && candidate.content.parts) {
88+
for (const part of candidate.content.parts) {
89+
if (part.thought) {
90+
// This is a thinking/reasoning part
91+
if (part.text) {
92+
yield { type: "reasoning", text: part.text }
93+
}
94+
} else {
95+
// This is regular content
96+
if (part.text) {
97+
yield { type: "text", text: part.text }
98+
}
99+
}
100+
}
101+
}
102+
}
103+
104+
// Fallback to the original text property if no candidates structure
105+
else if (chunk.text) {
85106
yield { type: "text", text: chunk.text }
86107
}
87108

@@ -121,7 +142,7 @@ export class GeminiHandler extends BaseProvider implements SingleCompletionHandl
121142
id,
122143
info,
123144
thinkingConfig: this.options.modelMaxThinkingTokens
124-
? { thinkingBudget: this.options.modelMaxThinkingTokens }
145+
? { thinkingBudget: this.options.modelMaxThinkingTokens, includeThoughts: true }
125146
: undefined,
126147
maxOutputTokens: this.options.modelMaxTokens ?? info.maxTokens ?? undefined,
127148
}

src/api/providers/vertex.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export class VertexHandler extends GeminiHandler implements SingleCompletionHand
2424
id,
2525
info,
2626
thinkingConfig: this.options.modelMaxThinkingTokens
27-
? { thinkingBudget: this.options.modelMaxThinkingTokens }
27+
? { thinkingBudget: this.options.modelMaxThinkingTokens, includeThoughts: true }
2828
: undefined,
2929
maxOutputTokens: this.options.modelMaxTokens ?? info.maxTokens ?? undefined,
3030
}

0 commit comments

Comments
 (0)