Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 27 additions & 48 deletions src/main/presenter/llmProviderPresenter/providers/geminiProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,7 @@ export class GeminiProvider extends BaseLLMProvider {
let isInThinkTag = false
let toolUseDetected = false
let usageMetadata: GenerateContentResponseUsageMetadata | undefined
let isNewThoughtFormatDetected = false

// 流处理循环
for await (const chunk of result) {
Expand Down Expand Up @@ -987,6 +988,7 @@ export class GeminiProvider extends BaseLLMProvider {
for (const part of chunk.candidates[0].content.parts) {
// 检查是否是思考内容 (新格式)
if ((part as any).thought === true && part.text) {
isNewThoughtFormatDetected = true
thoughtContent += part.text
} else if (part.text) {
content += part.text
Expand All @@ -1012,71 +1014,48 @@ export class GeminiProvider extends BaseLLMProvider {
type: 'reasoning',
reasoning_content: thoughtContent
}
thoughtContent = '' // 清空已发送的思考内容
}

if (!content) continue

buffer += content

// 处理思考标签
if (buffer.includes('<think>') && !isInThinkTag) {
const thinkStart = buffer.indexOf('<think>')
if (isNewThoughtFormatDetected) {
yield {
type: 'text',
content: content
}
} else {
buffer += content

// 发送<think>标签前的文本
if (thinkStart > 0) {
yield {
type: 'text',
content: buffer.substring(0, thinkStart)
if (buffer.includes('<think>') && !isInThinkTag) {
const thinkStart = buffer.indexOf('<think>')
if (thinkStart > 0) {
yield { type: 'text', content: buffer.substring(0, thinkStart) }
}
buffer = buffer.substring(thinkStart + 7)
isInThinkTag = true
}

buffer = buffer.substring(thinkStart + 7)
isInThinkTag = true
continue
}

// 处理思考标签结束
if (isInThinkTag && buffer.includes('</think>')) {
const thinkEnd = buffer.indexOf('</think>')
const reasoningContent = buffer.substring(0, thinkEnd)

// 发送推理内容
if (reasoningContent) {
yield {
type: 'reasoning',
reasoning_content: reasoningContent
if (isInThinkTag && buffer.includes('</think>')) {
const thinkEnd = buffer.indexOf('</think>')
const reasoningContent = buffer.substring(0, thinkEnd)
if (reasoningContent) {
yield {
type: 'reasoning',
reasoning_content: reasoningContent
}
}
buffer = buffer.substring(thinkEnd + 8)
isInThinkTag = false
}

buffer = buffer.substring(thinkEnd + 8)
isInThinkTag = false

// 如果还有剩余内容,继续处理
if (buffer) {
if (!isInThinkTag && buffer) {
yield {
type: 'text',
content: buffer
}
buffer = ''
}

continue
}

// 如果在思考标签内,不输出内容
if (isInThinkTag) {
continue
}

// 正常输出文本内容
yield {
type: 'text',
content: content
}

// 内容已经发送,清空buffer避免重复
buffer = ''
}

if (usageMetadata) {
Expand All @@ -1091,7 +1070,7 @@ export class GeminiProvider extends BaseLLMProvider {
}

// 处理剩余缓冲区内容
if (buffer) {
if (!isNewThoughtFormatDetected && buffer) {
if (isInThinkTag) {
yield {
type: 'reasoning',
Expand Down