mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2026-01-08 06:19:05 +08:00
refactor: optimize block update logic and remove unused code
- Updated `throttledBlockUpdate` to handle asynchronous updates directly. - Removed the unused `throttledBlockDbUpdate` function and its related logic. - Added cancellation for throttled updates on error and completion to improve performance and reliability. - Cleaned up commented-out code for better readability.
This commit is contained in:
parent
6da1d08c9a
commit
c402a1d21f
@ -106,44 +106,46 @@ const updateExistingMessageAndBlocksInDB = async (
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 更新单个块的逻辑,用于更新消息中的单个块
|
// 更新单个块的逻辑,用于更新消息中的单个块
|
||||||
const throttledBlockUpdate = throttle((id, blockUpdate) => {
|
const throttledBlockUpdate = throttle(async (id, blockUpdate) => {
|
||||||
const state = store.getState()
|
// const state = store.getState()
|
||||||
const block = state.messageBlocks.entities[id]
|
// const block = state.messageBlocks.entities[id]
|
||||||
// throttle是异步函数,可能会在complete事件触发后才执行
|
// throttle是异步函数,可能会在complete事件触发后才执行
|
||||||
if (
|
// if (
|
||||||
blockUpdate.status === MessageBlockStatus.STREAMING &&
|
// blockUpdate.status === MessageBlockStatus.STREAMING &&
|
||||||
(block?.status === MessageBlockStatus.SUCCESS || block?.status === MessageBlockStatus.ERROR)
|
// (block?.status === MessageBlockStatus.SUCCESS || block?.status === MessageBlockStatus.ERROR)
|
||||||
)
|
// )
|
||||||
return
|
// return
|
||||||
|
|
||||||
store.dispatch(updateOneBlock({ id, changes: blockUpdate }))
|
store.dispatch(updateOneBlock({ id, changes: blockUpdate }))
|
||||||
|
await db.message_blocks.update(id, blockUpdate)
|
||||||
}, 150)
|
}, 150)
|
||||||
|
|
||||||
// 修改: 节流更新单个块的内容/状态到数据库 (仅用于 Text/Thinking Chunks)
|
const cancelThrottledBlockUpdate = throttledBlockUpdate.cancel
|
||||||
export const throttledBlockDbUpdate = throttle(
|
|
||||||
async (blockId: string, blockChanges: Partial<MessageBlock>) => {
|
// // 修改: 节流更新单个块的内容/状态到数据库 (仅用于 Text/Thinking Chunks)
|
||||||
// Check if blockId is valid before attempting update
|
// export const throttledBlockDbUpdate = throttle(
|
||||||
if (!blockId) {
|
// async (blockId: string, blockChanges: Partial<MessageBlock>) => {
|
||||||
console.warn('[DB Throttle Block Update] Attempted to update with null/undefined blockId. Skipping.')
|
// // Check if blockId is valid before attempting update
|
||||||
return
|
// if (!blockId) {
|
||||||
}
|
// console.warn('[DB Throttle Block Update] Attempted to update with null/undefined blockId. Skipping.')
|
||||||
const state = store.getState()
|
// return
|
||||||
const block = state.messageBlocks.entities[blockId]
|
// }
|
||||||
// throttle是异步函数,可能会在complete事件触发后才执行
|
// const state = store.getState()
|
||||||
if (
|
// const block = state.messageBlocks.entities[blockId]
|
||||||
blockChanges.status === MessageBlockStatus.STREAMING &&
|
// // throttle是异步函数,可能会在complete事件触发后才执行
|
||||||
(block?.status === MessageBlockStatus.SUCCESS || block?.status === MessageBlockStatus.ERROR)
|
// if (
|
||||||
)
|
// blockChanges.status === MessageBlockStatus.STREAMING &&
|
||||||
return
|
// (block?.status === MessageBlockStatus.SUCCESS || block?.status === MessageBlockStatus.ERROR)
|
||||||
try {
|
// )
|
||||||
await db.message_blocks.update(blockId, blockChanges)
|
// return
|
||||||
} catch (error) {
|
// try {
|
||||||
console.error(`[DB Throttle Block Update] Failed for block ${blockId}:`, error)
|
// } catch (error) {
|
||||||
}
|
// console.error(`[DB Throttle Block Update] Failed for block ${blockId}:`, error)
|
||||||
},
|
// }
|
||||||
300, // 可以调整节流间隔
|
// },
|
||||||
{ leading: false, trailing: true }
|
// 300, // 可以调整节流间隔
|
||||||
)
|
// { leading: false, trailing: true }
|
||||||
|
// )
|
||||||
|
|
||||||
// 新增: 通用的、非节流的函数,用于保存消息和块的更新到数据库
|
// 新增: 通用的、非节流的函数,用于保存消息和块的更新到数据库
|
||||||
const saveUpdatesToDB = async (
|
const saveUpdatesToDB = async (
|
||||||
@ -338,7 +340,7 @@ const fetchAndProcessAssistantResponseImpl = async (
|
|||||||
status: MessageBlockStatus.STREAMING
|
status: MessageBlockStatus.STREAMING
|
||||||
}
|
}
|
||||||
throttledBlockUpdate(lastBlockId, blockChanges)
|
throttledBlockUpdate(lastBlockId, blockChanges)
|
||||||
throttledBlockDbUpdate(lastBlockId, blockChanges)
|
// throttledBlockDbUpdate(lastBlockId, blockChanges)
|
||||||
} else {
|
} else {
|
||||||
const newBlock = createMainTextBlock(assistantMsgId, accumulatedContent, {
|
const newBlock = createMainTextBlock(assistantMsgId, accumulatedContent, {
|
||||||
status: MessageBlockStatus.STREAMING,
|
status: MessageBlockStatus.STREAMING,
|
||||||
@ -396,7 +398,7 @@ const fetchAndProcessAssistantResponseImpl = async (
|
|||||||
thinking_millsec: thinking_millsec
|
thinking_millsec: thinking_millsec
|
||||||
}
|
}
|
||||||
throttledBlockUpdate(lastBlockId, blockChanges)
|
throttledBlockUpdate(lastBlockId, blockChanges)
|
||||||
throttledBlockDbUpdate(lastBlockId, blockChanges)
|
// throttledBlockDbUpdate(lastBlockId, blockChanges)
|
||||||
} else {
|
} else {
|
||||||
const newBlock = createThinkingBlock(assistantMsgId, accumulatedThinking, {
|
const newBlock = createThinkingBlock(assistantMsgId, accumulatedThinking, {
|
||||||
status: MessageBlockStatus.STREAMING,
|
status: MessageBlockStatus.STREAMING,
|
||||||
@ -540,6 +542,7 @@ const fetchAndProcessAssistantResponseImpl = async (
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
onError: async (error) => {
|
onError: async (error) => {
|
||||||
|
cancelThrottledBlockUpdate()
|
||||||
console.dir(error, { depth: null })
|
console.dir(error, { depth: null })
|
||||||
const isErrorTypeAbort = isAbortError(error)
|
const isErrorTypeAbort = isAbortError(error)
|
||||||
let pauseErrorLanguagePlaceholder = ''
|
let pauseErrorLanguagePlaceholder = ''
|
||||||
@ -581,6 +584,8 @@ const fetchAndProcessAssistantResponseImpl = async (
|
|||||||
})
|
})
|
||||||
},
|
},
|
||||||
onComplete: async (status: AssistantMessageStatus, response?: Response) => {
|
onComplete: async (status: AssistantMessageStatus, response?: Response) => {
|
||||||
|
cancelThrottledBlockUpdate()
|
||||||
|
|
||||||
const finalStateOnComplete = getState()
|
const finalStateOnComplete = getState()
|
||||||
const finalAssistantMsg = finalStateOnComplete.messages.entities[assistantMsgId]
|
const finalAssistantMsg = finalStateOnComplete.messages.entities[assistantMsgId]
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user