refactor(reasoning): simplify reasoning time tracking by removing unu… (#10360)

* refactor(reasoning): simplify reasoning time tracking by removing unused variables and logic

- Removed hasStartedThinking and reasoningBlockId variables as they are no longer needed.
- Updated onThinkingComplete callback to eliminate final_thinking_millsec parameter, streamlining the function.

* refactor(thinking): streamline thinking millisecond tracking and update event handling

- Removed unused thinking_millsec parameter from onThinkingComplete and adjusted related logic.
- Updated AiSdkToChunkAdapter to simplify reasoning-end event handling by removing unnecessary properties.
- Modified integration tests to reflect changes in thinking event structure.
This commit is contained in:
MyPrototypeWhat 2025-09-25 19:06:25 +08:00 committed by GitHub
parent caad0bc005
commit 05a318225c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 13 additions and 30 deletions

View File

@ -170,8 +170,7 @@ export class AiSdkToChunkAdapter {
case 'reasoning-end':
this.onChunk({
type: ChunkType.THINKING_COMPLETE,
text: (chunk.providerMetadata?.metadata?.thinking_content as string) || final.reasoningContent,
thinking_millsec: (chunk.providerMetadata?.metadata?.thinking_millsec as number) || 0
text: (chunk.providerMetadata?.metadata?.thinking_content as string) || final.reasoningContent
})
final.reasoningContent = ''
break

View File

@ -7,18 +7,14 @@ export default definePlugin({
transformStream: () => () => {
// === 时间跟踪状态 ===
let thinkingStartTime = 0
let hasStartedThinking = false
let accumulatedThinkingContent = ''
let reasoningBlockId = ''
return new TransformStream<TextStreamPart<ToolSet>, TextStreamPart<ToolSet>>({
transform(chunk: TextStreamPart<ToolSet>, controller: TransformStreamDefaultController<TextStreamPart<ToolSet>>) {
// === 处理 reasoning 类型 ===
if (chunk.type === 'reasoning-start') {
controller.enqueue(chunk)
hasStartedThinking = true
thinkingStartTime = performance.now()
reasoningBlockId = chunk.id
} else if (chunk.type === 'reasoning-delta') {
accumulatedThinkingContent += chunk.text
controller.enqueue({
@ -32,21 +28,6 @@ export default definePlugin({
}
}
})
} else if (chunk.type === 'reasoning-end' && hasStartedThinking) {
controller.enqueue({
type: 'reasoning-end',
id: reasoningBlockId,
providerMetadata: {
metadata: {
thinking_millsec: performance.now() - thinkingStartTime,
thinking_content: accumulatedThinkingContent
}
}
})
accumulatedThinkingContent = ''
hasStartedThinking = false
thinkingStartTime = 0
reasoningBlockId = ''
} else {
controller.enqueue(chunk)
}

View File

@ -15,22 +15,23 @@ export const createThinkingCallbacks = (deps: ThinkingCallbacksDependencies) =>
// 内部维护的状态
let thinkingBlockId: string | null = null
let _thinking_millsec = 0
return {
onThinkingStart: async () => {
if (blockManager.hasInitialPlaceholder) {
const changes = {
const changes: Partial<MessageBlock> = {
type: MessageBlockType.THINKING,
content: '',
status: MessageBlockStatus.STREAMING,
thinking_millsec: 0
thinking_millsec: _thinking_millsec
}
thinkingBlockId = blockManager.initialPlaceholderBlockId!
blockManager.smartBlockUpdate(thinkingBlockId, changes, MessageBlockType.THINKING, true)
} else if (!thinkingBlockId) {
const newBlock = createThinkingBlock(assistantMsgId, '', {
status: MessageBlockStatus.STREAMING,
thinking_millsec: 0
thinking_millsec: _thinking_millsec
})
thinkingBlockId = newBlock.id
await blockManager.handleBlockTransition(newBlock, MessageBlockType.THINKING)
@ -38,26 +39,27 @@ export const createThinkingCallbacks = (deps: ThinkingCallbacksDependencies) =>
},
onThinkingChunk: async (text: string, thinking_millsec?: number) => {
_thinking_millsec = thinking_millsec || 0
if (thinkingBlockId) {
const blockChanges: Partial<MessageBlock> = {
content: text,
status: MessageBlockStatus.STREAMING,
thinking_millsec: thinking_millsec || 0
thinking_millsec: _thinking_millsec
}
blockManager.smartBlockUpdate(thinkingBlockId, blockChanges, MessageBlockType.THINKING)
}
},
onThinkingComplete: (finalText: string, final_thinking_millsec?: number) => {
onThinkingComplete: (finalText: string) => {
if (thinkingBlockId) {
const changes = {
type: MessageBlockType.THINKING,
const changes: Partial<MessageBlock> = {
content: finalText,
status: MessageBlockStatus.SUCCESS,
thinking_millsec: final_thinking_millsec || 0
thinking_millsec: _thinking_millsec
}
blockManager.smartBlockUpdate(thinkingBlockId, changes, MessageBlockType.THINKING, true)
thinkingBlockId = null
_thinking_millsec = 0
} else {
logger.warn(
`[onThinkingComplete] Received thinking.complete but last block was not THINKING (was ${blockManager.lastBlockType}) or lastBlockId is null.`

View File

@ -410,7 +410,8 @@ describe('streamCallback Integration Tests', () => {
{ type: ChunkType.THINKING_START },
{ type: ChunkType.THINKING_DELTA, text: 'Let me think...', thinking_millsec: 1000 },
{ type: ChunkType.THINKING_DELTA, text: 'I need to consider...', thinking_millsec: 2000 },
{ type: ChunkType.THINKING_COMPLETE, text: 'Final thoughts', thinking_millsec: 3000 },
{ type: ChunkType.THINKING_DELTA, text: 'Final thoughts', thinking_millsec: 3000 },
{ type: ChunkType.THINKING_COMPLETE, text: 'Final thoughts' },
{ type: ChunkType.BLOCK_COMPLETE }
]