mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2026-01-05 20:41:30 +08:00
feat: gemini thinking summary support
- Removed unnecessary content accumulation and streamlined chunk processing logic. - Introduced distinct handling for 'thinking' and 'text' parts, ensuring accurate onChunk calls for both types. - Enhanced timing tracking for reasoning content, improving overall responsiveness in streaming scenarios.
This commit is contained in:
parent
c8e6872fb8
commit
e5ded81d9b
@ -508,7 +508,6 @@ export default class GeminiProvider extends BaseProvider {
|
|||||||
let time_first_token_millsec = 0
|
let time_first_token_millsec = 0
|
||||||
|
|
||||||
if (stream instanceof GenerateContentResponse) {
|
if (stream instanceof GenerateContentResponse) {
|
||||||
let content = ''
|
|
||||||
const time_completion_millsec = new Date().getTime() - start_time_millsec
|
const time_completion_millsec = new Date().getTime() - start_time_millsec
|
||||||
|
|
||||||
const toolResults: Awaited<ReturnType<typeof parseAndCallTools>> = []
|
const toolResults: Awaited<ReturnType<typeof parseAndCallTools>> = []
|
||||||
@ -523,16 +522,18 @@ export default class GeminiProvider extends BaseProvider {
|
|||||||
if (part.functionCall) {
|
if (part.functionCall) {
|
||||||
functionCalls.push(part.functionCall)
|
functionCalls.push(part.functionCall)
|
||||||
}
|
}
|
||||||
if (part.text) {
|
const text = part.text || ''
|
||||||
content += part.text
|
if (part.thought) {
|
||||||
onChunk({ type: ChunkType.TEXT_DELTA, text: part.text })
|
onChunk({ type: ChunkType.THINKING_DELTA, text })
|
||||||
|
onChunk({ type: ChunkType.THINKING_COMPLETE, text })
|
||||||
|
} else if (part.text) {
|
||||||
|
onChunk({ type: ChunkType.TEXT_DELTA, text })
|
||||||
|
onChunk({ type: ChunkType.TEXT_COMPLETE, text })
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
if (content.length) {
|
|
||||||
onChunk({ type: ChunkType.TEXT_COMPLETE, text: content })
|
|
||||||
}
|
|
||||||
if (functionCalls.length) {
|
if (functionCalls.length) {
|
||||||
toolResults.push(...(await processToolCalls(functionCalls)))
|
toolResults.push(...(await processToolCalls(functionCalls)))
|
||||||
}
|
}
|
||||||
@ -565,16 +566,35 @@ export default class GeminiProvider extends BaseProvider {
|
|||||||
} as BlockCompleteChunk)
|
} as BlockCompleteChunk)
|
||||||
} else {
|
} else {
|
||||||
let content = ''
|
let content = ''
|
||||||
|
let thinkingContent = ''
|
||||||
for await (const chunk of stream) {
|
for await (const chunk of stream) {
|
||||||
if (window.keyv.get(EVENT_NAMES.CHAT_COMPLETION_PAUSED)) break
|
if (window.keyv.get(EVENT_NAMES.CHAT_COMPLETION_PAUSED)) break
|
||||||
|
|
||||||
if (time_first_token_millsec == 0) {
|
if (chunk.candidates?.[0]?.content?.parts && chunk.candidates[0].content.parts.length > 0) {
|
||||||
time_first_token_millsec = new Date().getTime()
|
const parts = chunk.candidates[0].content.parts
|
||||||
}
|
for (const part of parts) {
|
||||||
|
if (!part.text) {
|
||||||
if (chunk.text !== undefined) {
|
continue
|
||||||
content += chunk.text
|
} else if (part.thought) {
|
||||||
onChunk({ type: ChunkType.TEXT_DELTA, text: chunk.text })
|
if (time_first_token_millsec === 0) {
|
||||||
|
time_first_token_millsec = new Date().getTime()
|
||||||
|
}
|
||||||
|
thinkingContent += part.text
|
||||||
|
onChunk({ type: ChunkType.THINKING_DELTA, text: part.text || '' })
|
||||||
|
} else {
|
||||||
|
if (time_first_token_millsec == 0) {
|
||||||
|
time_first_token_millsec = new Date().getTime()
|
||||||
|
} else {
|
||||||
|
onChunk({
|
||||||
|
type: ChunkType.THINKING_COMPLETE,
|
||||||
|
text: thinkingContent,
|
||||||
|
thinking_millsec: new Date().getTime() - time_first_token_millsec
|
||||||
|
})
|
||||||
|
}
|
||||||
|
content += part.text
|
||||||
|
onChunk({ type: ChunkType.TEXT_DELTA, text: part.text })
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (chunk.candidates?.[0]?.finishReason) {
|
if (chunk.candidates?.[0]?.finishReason) {
|
||||||
@ -643,6 +663,7 @@ export default class GeminiProvider extends BaseProvider {
|
|||||||
const start_time_millsec = new Date().getTime()
|
const start_time_millsec = new Date().getTime()
|
||||||
|
|
||||||
if (!streamOutput) {
|
if (!streamOutput) {
|
||||||
|
onChunk({ type: ChunkType.LLM_RESPONSE_CREATED })
|
||||||
const response = await chat.sendMessage({
|
const response = await chat.sendMessage({
|
||||||
message: messageContents as PartUnion,
|
message: messageContents as PartUnion,
|
||||||
config: {
|
config: {
|
||||||
@ -650,7 +671,6 @@ export default class GeminiProvider extends BaseProvider {
|
|||||||
abortSignal: abortController.signal
|
abortSignal: abortController.signal
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
onChunk({ type: ChunkType.LLM_RESPONSE_CREATED })
|
|
||||||
return await processStream(response, 0).then(cleanup)
|
return await processStream(response, 0).then(cleanup)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user