fix: preserve thinking time when stopping reply

Fixes #11886

Signed-off-by: Calvin <calvinvwei@gmail.com>
This commit is contained in:
Calvin 2025-12-13 22:57:37 +08:00
parent 5bd550bfb4
commit a4eeb6c4b1
3 changed files with 41 additions and 9 deletions

View File

@ -29,10 +29,20 @@ interface BaseCallbacksDependencies {
assistantMsgId: string
saveUpdatesToDB: any
assistant: Assistant
getCurrentThinkingInfo?: () => { blockId: string | null; millsec: number }
}
export const createBaseCallbacks = (deps: BaseCallbacksDependencies) => {
const { blockManager, dispatch, getState, topicId, assistantMsgId, saveUpdatesToDB, assistant } = deps
const {
blockManager,
dispatch,
getState,
topicId,
assistantMsgId,
saveUpdatesToDB,
assistant,
getCurrentThinkingInfo
} = deps
const startTime = Date.now()
const notificationService = NotificationService.getInstance()
@ -111,13 +121,28 @@ export const createBaseCallbacks = (deps: BaseCallbacksDependencies) => {
if (currentMessage) {
const allBlockRefs = findAllBlocks(currentMessage)
const blockState = getState().messageBlocks
// 获取当前思考信息(如果有),用于保留实际思考时间
const thinkingInfo = getCurrentThinkingInfo?.()
for (const blockRef of allBlockRefs) {
const block = blockState.entities[blockRef.id]
if (block && block.status === MessageBlockStatus.STREAMING && block.id !== possibleBlockId) {
// 构建更新对象
const changes: Record<string, any> = {
status: isErrorTypeAbort ? MessageBlockStatus.PAUSED : MessageBlockStatus.ERROR
}
// 如果是 thinking block 且有思考时间信息,保留实际思考时间
if (
block.type === MessageBlockType.THINKING &&
thinkingInfo?.blockId === block.id &&
thinkingInfo?.millsec &&
thinkingInfo.millsec > 0
) {
changes.thinking_millsec = thinkingInfo.millsec
}
dispatch(
updateOneBlock({
id: block.id,
changes: { status: isErrorTypeAbort ? MessageBlockStatus.PAUSED : MessageBlockStatus.ERROR }
changes
})
)
}

View File

@ -23,6 +23,12 @@ interface CallbacksDependencies {
export const createCallbacks = (deps: CallbacksDependencies) => {
const { blockManager, dispatch, getState, topicId, assistantMsgId, saveUpdatesToDB, assistant } = deps
// 首先创建 thinkingCallbacks ,以便传递 getCurrentThinkingInfo 给 baseCallbacks
const thinkingCallbacks = createThinkingCallbacks({
blockManager,
assistantMsgId
})
// 创建基础回调
const baseCallbacks = createBaseCallbacks({
blockManager,
@ -31,13 +37,8 @@ export const createCallbacks = (deps: CallbacksDependencies) => {
topicId,
assistantMsgId,
saveUpdatesToDB,
assistant
})
// 创建各类回调
const thinkingCallbacks = createThinkingCallbacks({
blockManager,
assistantMsgId
assistant,
getCurrentThinkingInfo: thinkingCallbacks.getCurrentThinkingInfo
})
const toolCallbacks = createToolCallbacks({

View File

@ -19,6 +19,12 @@ export const createThinkingCallbacks = (deps: ThinkingCallbacksDependencies) =>
let thinking_millsec_now: number = 0
return {
// 获取当前思考时间(用于停止回复时保留思考时间)
getCurrentThinkingInfo: () => ({
blockId: thinkingBlockId,
millsec: thinking_millsec_now > 0 ? performance.now() - thinking_millsec_now : 0
}),
onThinkingStart: async () => {
if (blockManager.hasInitialPlaceholder) {
const changes: Partial<MessageBlock> = {