fix: Create assistant message immediately when reasoning starts
- Create assistant message as soon as first reasoning event arrives - This ensures reasoning block is visible from the beginning of thinking - Prevent duplicate message creation in text-delta handler - Move reasoning end time logic to text-delta start (when thinking finishes) This fixes the issue where reasoning blocks weren't appearing because the assistant message was only created after reasoning completed.
This commit is contained in:
parent
bfe2bd5816
commit
b810f639aa
1 changed files with 41 additions and 22 deletions
|
|
@ -203,10 +203,32 @@ export const useStreamResponse = ({
|
||||||
if (event.type === 'reasoning') {
|
if (event.type === 'reasoning') {
|
||||||
debugLog(`[useStreamResponse] Reasoning event:`, event)
|
debugLog(`[useStreamResponse] Reasoning event:`, event)
|
||||||
|
|
||||||
// Track reasoning start time
|
// Track reasoning start time and create assistant message if needed
|
||||||
if (!reasoningStartTime) {
|
if (!reasoningStartTime) {
|
||||||
reasoningStartTime = Date.now()
|
reasoningStartTime = Date.now()
|
||||||
debugLog(`[useStreamResponse] Reasoning started at:`, reasoningStartTime)
|
debugLog(`[useStreamResponse] Reasoning started at:`, reasoningStartTime)
|
||||||
|
|
||||||
|
// Create assistant message immediately when reasoning starts
|
||||||
|
if (!assistantMessageCreated) {
|
||||||
|
debugLog(`[useStreamResponse] Creating assistant message for reasoning`)
|
||||||
|
setConversations((prev) => {
|
||||||
|
const updated = [...prev]
|
||||||
|
const conv = updated.find((c) => c.id === conversationId)
|
||||||
|
if (conv) {
|
||||||
|
conv.messages.push({
|
||||||
|
role: 'assistant',
|
||||||
|
content: '',
|
||||||
|
reasoning: '',
|
||||||
|
isReasoningStreaming: true,
|
||||||
|
reasoningStartTime: reasoningStartTime
|
||||||
|
})
|
||||||
|
assistantMessageIndex = conv.messages.length - 1
|
||||||
|
debugLog(`[useStreamResponse] Created assistant message at index ${assistantMessageIndex}`)
|
||||||
|
}
|
||||||
|
return updated
|
||||||
|
})
|
||||||
|
assistantMessageCreated = true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
reasoning += (event as any).textDelta || ''
|
reasoning += (event as any).textDelta || ''
|
||||||
|
|
@ -269,16 +291,9 @@ export const useStreamResponse = ({
|
||||||
} else if (event.type === 'text-delta') {
|
} else if (event.type === 'text-delta') {
|
||||||
debugLog(`[useStreamResponse] Text delta:`, event.textDelta)
|
debugLog(`[useStreamResponse] Text delta:`, event.textDelta)
|
||||||
|
|
||||||
// Create assistant message on first text delta (after all tool calls/results)
|
// Create assistant message on first text delta if not already created
|
||||||
if (!assistantMessageCreated) {
|
if (!assistantMessageCreated) {
|
||||||
debugLog(`[useStreamResponse] Creating assistant message after tool calls/results`)
|
debugLog(`[useStreamResponse] Creating assistant message after tool calls/results`)
|
||||||
|
|
||||||
// Mark reasoning end time when text starts (reasoning is done)
|
|
||||||
if (reasoningStartTime && !reasoningEndTime) {
|
|
||||||
reasoningEndTime = Date.now()
|
|
||||||
debugLog(`[useStreamResponse] Reasoning ended at:`, reasoningEndTime, 'Duration:', reasoningEndTime - reasoningStartTime, 'ms')
|
|
||||||
}
|
|
||||||
|
|
||||||
setConversations((prev) => {
|
setConversations((prev) => {
|
||||||
const updated = [...prev]
|
const updated = [...prev]
|
||||||
const conv = updated.find((c) => c.id === conversationId)
|
const conv = updated.find((c) => c.id === conversationId)
|
||||||
|
|
@ -290,9 +305,14 @@ export const useStreamResponse = ({
|
||||||
return updated
|
return updated
|
||||||
})
|
})
|
||||||
assistantMessageCreated = true
|
assistantMessageCreated = true
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mark reasoning end time when text starts (reasoning is done)
|
||||||
|
if (reasoningStartTime && !reasoningEndTime) {
|
||||||
|
reasoningEndTime = Date.now()
|
||||||
|
debugLog(`[useStreamResponse] Reasoning ended at:`, reasoningEndTime, 'Duration:', reasoningEndTime - reasoningStartTime, 'ms')
|
||||||
|
|
||||||
// Update reasoning end time and stop streaming
|
// Update reasoning end time and stop streaming
|
||||||
if (reasoningEndTime) {
|
|
||||||
setConversations((prev) => {
|
setConversations((prev) => {
|
||||||
const updated = [...prev]
|
const updated = [...prev]
|
||||||
const conv = updated.find((c) => c.id === conversationId)
|
const conv = updated.find((c) => c.id === conversationId)
|
||||||
|
|
@ -306,7 +326,6 @@ export const useStreamResponse = ({
|
||||||
return updated
|
return updated
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
aiResponse += event.textDelta
|
aiResponse += event.textDelta
|
||||||
aiResponseRef.current = aiResponse
|
aiResponseRef.current = aiResponse
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue