handle abort properly + finally clause to resolve (#6227)
This commit is contained in:
parent
55390de070
commit
9ea9b7d87d
@ -309,7 +309,7 @@ export const useChat = () => {
|
|||||||
let pendingDeltaCount = 0
|
let pendingDeltaCount = 0
|
||||||
const reasoningProcessor = new ReasoningProcessor()
|
const reasoningProcessor = new ReasoningProcessor()
|
||||||
const scheduleFlush = () => {
|
const scheduleFlush = () => {
|
||||||
if (rafScheduled) return
|
if (rafScheduled || abortController.signal.aborted) return
|
||||||
rafScheduled = true
|
rafScheduled = true
|
||||||
const doSchedule = (cb: () => void) => {
|
const doSchedule = (cb: () => void) => {
|
||||||
if (typeof requestAnimationFrame !== 'undefined') {
|
if (typeof requestAnimationFrame !== 'undefined') {
|
||||||
@ -321,6 +321,12 @@ export const useChat = () => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
doSchedule(() => {
|
doSchedule(() => {
|
||||||
|
// Check abort status before executing the scheduled callback
|
||||||
|
if (abortController.signal.aborted) {
|
||||||
|
rafScheduled = false
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
const currentContent = newAssistantThreadContent(
|
const currentContent = newAssistantThreadContent(
|
||||||
activeThread.id,
|
activeThread.id,
|
||||||
accumulatedText,
|
accumulatedText,
|
||||||
@ -367,7 +373,13 @@ export const useChat = () => {
|
|||||||
pendingDeltaCount = 0
|
pendingDeltaCount = 0
|
||||||
rafScheduled = false
|
rafScheduled = false
|
||||||
}
|
}
|
||||||
|
try {
|
||||||
for await (const part of completion) {
|
for await (const part of completion) {
|
||||||
|
// Check if aborted before processing each part
|
||||||
|
if (abortController.signal.aborted) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
// Error message
|
// Error message
|
||||||
if (!part.choices) {
|
if (!part.choices) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
@ -398,11 +410,27 @@ export const useChat = () => {
|
|||||||
scheduleFlush()
|
scheduleFlush()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} finally {
|
||||||
|
// Always clean up scheduled RAF when stream ends (either normally or via abort)
|
||||||
|
if (rafHandle !== undefined) {
|
||||||
|
if (typeof cancelAnimationFrame !== 'undefined') {
|
||||||
|
cancelAnimationFrame(rafHandle)
|
||||||
|
} else {
|
||||||
|
clearTimeout(rafHandle)
|
||||||
|
}
|
||||||
|
rafHandle = undefined
|
||||||
|
rafScheduled = false
|
||||||
|
}
|
||||||
|
|
||||||
|
// Only finalize and flush if not aborted
|
||||||
|
if (!abortController.signal.aborted) {
|
||||||
// Finalize reasoning (close any open think tags)
|
// Finalize reasoning (close any open think tags)
|
||||||
accumulatedText += reasoningProcessor.finalize()
|
accumulatedText += reasoningProcessor.finalize()
|
||||||
// Ensure any pending buffered content is rendered at the end
|
// Ensure any pending buffered content is rendered at the end
|
||||||
flushIfPending()
|
flushIfPending()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
const errorMessage =
|
const errorMessage =
|
||||||
error && typeof error === 'object' && 'message' in error
|
error && typeof error === 'object' && 'message' in error
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user