This commit is contained in:
Siddharth Ganesan
2026-04-09 11:57:43 -07:00
parent a738a6d711
commit 69d69ee95b
2 changed files with 35 additions and 1 deletions

View File

@@ -368,6 +368,15 @@ export function useChat(
options?: { preserveExistingState?: boolean }
) => Promise<{ sawStreamError: boolean; sawComplete: boolean }>
>(async () => ({ sawStreamError: false, sawComplete: false }))
const attachToExistingStreamRef = useRef<
(opts: {
streamId: string
assistantId: string
expectedGen: number
initialBatch?: StreamBatchResponse | null
afterCursor?: string
}) => Promise<{ error: boolean; aborted: boolean }>
>(async () => ({ error: false, aborted: true }))
const retryReconnectRef = useRef<
(opts: { streamId: string; assistantId: string; gen: number }) => Promise<boolean>
>(async () => false)
@@ -617,7 +626,7 @@ export function useChat(
const reconnectResult =
snapshotEvents.length > 0
? await attachToExistingStream({
? await attachToExistingStreamRef.current({
streamId: activeStreamId,
assistantId,
expectedGen: gen,
@@ -1613,6 +1622,7 @@ export function useChat(
},
[fetchStreamBatch]
)
attachToExistingStreamRef.current = attachToExistingStream
const resumeOrFinalize = useCallback(
async (opts: {

View File

@@ -282,6 +282,12 @@ async function runCheckpointLoop(
)
}
if (isAborted(options, context)) {
cancelPendingTools(context)
context.awaitingAsyncContinuation = undefined
break
}
const results: Array<{
callId: string
name: string
@@ -289,6 +295,11 @@ async function runCheckpointLoop(
success: boolean
}> = []
for (const toolCallId of continuation.pendingToolCallIds) {
if (isAborted(options, context)) {
cancelPendingTools(context)
context.awaitingAsyncContinuation = undefined
break
}
const tool = context.toolCalls.get(toolCallId)
if (!tool || (!tool.result && !tool.error)) {
logger.error('Missing tool result for pending tool call', {
@@ -309,6 +320,12 @@ async function runCheckpointLoop(
})
}
if (isAborted(options, context)) {
cancelPendingTools(context)
context.awaitingAsyncContinuation = undefined
break
}
logger.info('Resuming with tool results', {
checkpointId: continuation.checkpointId,
runId: continuation.runId,
@@ -324,6 +341,13 @@ async function runCheckpointLoop(
checkpointId: continuation.checkpointId,
results,
}
if (isAborted(options, context)) {
cancelPendingTools(context)
context.awaitingAsyncContinuation = undefined
break
}
logger.info('Prepared resume request payload', {
route,
streamId: context.messageId,