working tool call ui!
This commit is contained in:
parent
769475cecc
commit
6ebb156fe6
1 changed files with 38 additions and 26 deletions
|
|
@ -102,6 +102,9 @@ export const useStreamResponse = ({
|
||||||
|
|
||||||
const streamResponse = async (messages: Message[]) => {
|
const streamResponse = async (messages: Message[]) => {
|
||||||
let aiResponse = ''
|
let aiResponse = ''
|
||||||
|
let assistantMessageIndex = -1 // Track the index of our assistant message
|
||||||
|
let assistantMessageCreated = false // Track if we've created the assistant message yet
|
||||||
|
|
||||||
debugLog(`[useStreamResponse] streamResponse called with ${messages.length} messages`)
|
debugLog(`[useStreamResponse] streamResponse called with ${messages.length} messages`)
|
||||||
debugLog(`[useStreamResponse] Messages:`, messages)
|
debugLog(`[useStreamResponse] Messages:`, messages)
|
||||||
debugLog(`[useStreamResponse] Current conversationId:`, conversationId)
|
debugLog(`[useStreamResponse] Current conversationId:`, conversationId)
|
||||||
|
|
@ -123,19 +126,6 @@ export const useStreamResponse = ({
|
||||||
try {
|
try {
|
||||||
const modelInstance = getModelInstance(selectedModel, apiKey)
|
const modelInstance = getModelInstance(selectedModel, apiKey)
|
||||||
|
|
||||||
debugLog(`[useStreamResponse] Adding empty assistant message to conversation ${conversationId}`)
|
|
||||||
setConversations((prev) => {
|
|
||||||
const updated = [...prev]
|
|
||||||
const conv = updated.find((c) => c.id === conversationId)
|
|
||||||
if (conv) {
|
|
||||||
debugLog(`[useStreamResponse] Found conversation, adding assistant message. Current message count: ${conv.messages.length}`)
|
|
||||||
conv.messages.push({ role: 'assistant', content: '' })
|
|
||||||
debugLog(`[useStreamResponse] After adding assistant message, message count: ${conv.messages.length}`)
|
|
||||||
} else {
|
|
||||||
debugLog(`[useStreamResponse] Could not find conversation with id: ${conversationId}`)
|
|
||||||
}
|
|
||||||
return updated
|
|
||||||
})
|
|
||||||
debugLog(`[useStreamResponse] Setting stream started to true`)
|
debugLog(`[useStreamResponse] Setting stream started to true`)
|
||||||
setStreamStarted(true)
|
setStreamStarted(true)
|
||||||
|
|
||||||
|
|
@ -243,6 +233,22 @@ 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)
|
||||||
|
if (!assistantMessageCreated) {
|
||||||
|
debugLog(`[useStreamResponse] Creating assistant message after tool calls/results`)
|
||||||
|
setConversations((prev) => {
|
||||||
|
const updated = [...prev]
|
||||||
|
const conv = updated.find((c) => c.id === conversationId)
|
||||||
|
if (conv) {
|
||||||
|
conv.messages.push({ role: 'assistant', content: '' })
|
||||||
|
assistantMessageIndex = conv.messages.length - 1
|
||||||
|
debugLog(`[useStreamResponse] Created assistant message at index ${assistantMessageIndex}`)
|
||||||
|
}
|
||||||
|
return updated
|
||||||
|
})
|
||||||
|
assistantMessageCreated = true
|
||||||
|
}
|
||||||
|
|
||||||
aiResponse += event.textDelta
|
aiResponse += event.textDelta
|
||||||
aiResponseRef.current = aiResponse
|
aiResponseRef.current = aiResponse
|
||||||
|
|
||||||
|
|
@ -267,9 +273,13 @@ export const useStreamResponse = ({
|
||||||
const conv = updated.find((c) => c.id === conversationId)
|
const conv = updated.find((c) => c.id === conversationId)
|
||||||
if (conv) {
|
if (conv) {
|
||||||
debugLog(`[useStreamResponse] Updating conversation message content (length: ${aiResponse.length})`)
|
debugLog(`[useStreamResponse] Updating conversation message content (length: ${aiResponse.length})`)
|
||||||
const lastMessage = conv.messages[conv.messages.length - 1]
|
// Update the specific assistant message we created, not just the last message
|
||||||
if (hasContent(lastMessage)) {
|
const assistantMessage = conv.messages[assistantMessageIndex]
|
||||||
lastMessage.content = aiResponse
|
if (assistantMessage && hasContent(assistantMessage) && assistantMessage.role === 'assistant') {
|
||||||
|
assistantMessage.content = aiResponse
|
||||||
|
debugLog(`[useStreamResponse] Updated assistant message at index ${assistantMessageIndex}`)
|
||||||
|
} else {
|
||||||
|
debugLog(`[useStreamResponse] Could not find assistant message at index ${assistantMessageIndex}`, assistantMessage)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
debugLog(`[useStreamResponse] Could not find conversation with id: ${conversationId}`)
|
debugLog(`[useStreamResponse] Could not find conversation with id: ${conversationId}`)
|
||||||
|
|
@ -295,16 +305,18 @@ export const useStreamResponse = ({
|
||||||
console.error('[useStreamResponse] Error stack:', error.stack)
|
console.error('[useStreamResponse] Error stack:', error.stack)
|
||||||
console.error('[useStreamResponse] Error message:', error.message)
|
console.error('[useStreamResponse] Error message:', error.message)
|
||||||
}
|
}
|
||||||
// Remove the assistant message if there was an error
|
// Remove the assistant message if there was an error and we created one
|
||||||
setConversations((prev) => {
|
if (assistantMessageCreated) {
|
||||||
const updated = [...prev]
|
setConversations((prev) => {
|
||||||
const conv = updated.find((c) => c.id === conversationId)
|
const updated = [...prev]
|
||||||
if (conv && conv.messages[conv.messages.length - 1].role === 'assistant') {
|
const conv = updated.find((c) => c.id === conversationId)
|
||||||
debugLog('[useStreamResponse] Removing assistant message due to error')
|
if (conv && assistantMessageIndex >= 0 && conv.messages[assistantMessageIndex]?.role === 'assistant') {
|
||||||
conv.messages.pop()
|
debugLog('[useStreamResponse] Removing assistant message due to error')
|
||||||
}
|
conv.messages.splice(assistantMessageIndex, 1)
|
||||||
return updated
|
}
|
||||||
})
|
return updated
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
debugLog('[useStreamResponse] Cleaning up stream state')
|
debugLog('[useStreamResponse] Cleaning up stream state')
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue