Starting to realise how dumb the vibecoded UI was oops
This commit is contained in:
parent
ba845e7852
commit
caed6d373c
1 changed files with 162 additions and 141 deletions
|
|
@ -155,171 +155,192 @@ export const useStreamResponse = ({
|
||||||
|
|
||||||
// Use fullStream to get all events including tool calls, results, and text
|
// Use fullStream to get all events including tool calls, results, and text
|
||||||
for await (const event of result.fullStream) {
|
for await (const event of result.fullStream) {
|
||||||
console.log(JSON.stringify(event))
|
console.log({ 'result.fullStream.event': event })
|
||||||
setConversations((prev) => {
|
setConversations((prev) => {
|
||||||
const updated = [...prev]
|
const conv = prev.find((c) => c.id === conversationId)
|
||||||
const conv = updated.find((c) => c.id === conversationId)
|
|
||||||
if (!conv) {
|
if (!conv) {
|
||||||
console.error(`Missing conversation for ID ${conversationId}! Ignoring ${JSON.stringify(event)}`)
|
console.error(`Missing conversation for ID ${conversationId}! Ignoring ${JSON.stringify(event)}`)
|
||||||
return updated
|
return prev
|
||||||
}
|
}
|
||||||
const lastMessage = conv.messages?.at(-1)
|
const lastMessage = conv.messages?.at(-1)
|
||||||
|
console.log({ setConversations: event, conv: JSON.parse(JSON.stringify(conv)) })
|
||||||
|
let updatedMessage: Message | undefined
|
||||||
|
let newMessage: Message | undefined
|
||||||
try {
|
try {
|
||||||
if (event.type === 'reasoning') {
|
if (event.type === 'reasoning') {
|
||||||
if (lastMessage?.role === 'assistant' && lastMessage.type === 'reasoning') {
|
if (lastMessage?.role === 'assistant' && lastMessage.type === 'reasoning') {
|
||||||
// We have an existing reasoning block!
|
// We have an existing reasoning block!
|
||||||
lastMessage.content = lastMessage.content + event.textDelta.trim()
|
updatedMessage = {
|
||||||
|
...lastMessage,
|
||||||
|
content: lastMessage.content + event.textDelta.trim(),
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// We need a new reasoning block!
|
// We need a new reasoning block!
|
||||||
const currentReasoningStartTime = Date.now()
|
const currentReasoningStartTime = Date.now()
|
||||||
|
|
||||||
// Create a new assistant message for reasoning (even if we already have one)
|
// Create a new assistant message for reasoning (even if we already have one)
|
||||||
// This handles multiple reasoning sessions in the same conversation turn
|
// This handles multiple reasoning sessions in the same conversation turn
|
||||||
if (conv) {
|
newMessage = {
|
||||||
conv.messages.push({
|
role: 'assistant',
|
||||||
role: 'assistant',
|
type: 'reasoning',
|
||||||
type: 'reasoning',
|
content: event.textDelta.trim(),
|
||||||
content: event.textDelta.trim(),
|
isReasoningStreaming: true,
|
||||||
isReasoningStreaming: true,
|
reasoningStartTime: currentReasoningStartTime!,
|
||||||
reasoningStartTime: currentReasoningStartTime!,
|
|
||||||
})
|
|
||||||
debugMessages('Added new reasoning assistant message at index', conv.messages.length - 1)
|
|
||||||
debugMessages('Current messages:', JSON.stringify(conv.messages))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
|
// We're not thinking, so maybe close the last message?
|
||||||
// We're not thinking, so maybe close the last message?
|
if (lastMessage?.role === 'assistant' && lastMessage.type === 'reasoning') {
|
||||||
if (lastMessage?.role === 'assistant' && lastMessage.type === 'reasoning') {
|
updatedMessage = {
|
||||||
lastMessage.reasoningEndTime = Date.now()
|
...lastMessage,
|
||||||
}
|
reasoningEndTime: Date.now(),
|
||||||
|
isReasoningStreaming: false,
|
||||||
if (event.type === 'tool-call') {
|
}
|
||||||
if (event.toolName) {
|
|
||||||
conv.messages.push({
|
|
||||||
role: 'tool-call',
|
|
||||||
toolName: event.toolName,
|
|
||||||
toolArgs: event.args || {},
|
|
||||||
callId: event.toolCallId || 'unknown',
|
|
||||||
})
|
|
||||||
debugMessages('Added new tool-call message:', event.toolName, 'callId:', event.toolCallId)
|
|
||||||
debugMessages('Current messages:', JSON.stringify(conv.messages))
|
|
||||||
scrollToBottom(true)
|
|
||||||
} else {
|
|
||||||
console.warn('Tool call event missing toolName:', event)
|
|
||||||
}
|
}
|
||||||
} else if (event.type === 'tool-result') {
|
|
||||||
console.info(`TODO TOOL RESULT`, event)
|
|
||||||
// if (event.toolName && event.toolCallId) {
|
|
||||||
// setConversations((prev) => {
|
|
||||||
// const updated = [...prev]
|
|
||||||
// const conv = updated.find((c) => c.id === conversationId)
|
|
||||||
// if (conv) {
|
|
||||||
// conv.messages.push({
|
|
||||||
// role: 'tool-result',
|
|
||||||
// toolName: event.toolName,
|
|
||||||
// toolArgs: event.args || {},
|
|
||||||
// toolResult: event.result,
|
|
||||||
// callId: event.toolCallId,
|
|
||||||
// })
|
|
||||||
// debugMessages('Added new tool-result message:', event.toolName, 'callId:', event.toolCallId)
|
|
||||||
// debugMessages('Current messages:', JSON.stringify(conv.messages))
|
|
||||||
// }
|
|
||||||
// return updated
|
|
||||||
// })
|
|
||||||
// scrollToBottom(true)
|
|
||||||
// } else {
|
|
||||||
// console.warn('Tool result event missing toolName or toolCallId:', event)
|
|
||||||
// }
|
|
||||||
} else if (event.type === 'text-delta') {
|
|
||||||
console.info(`TODO TEXT DELTA`, event)
|
|
||||||
|
|
||||||
// // Check if we have an existing reasoning message that needs to be finalized
|
if (event.type === 'tool-call') {
|
||||||
// let needsNewAssistantMessage = false
|
if (event.toolName) {
|
||||||
// if (currentReasoningStartTime && !currentReasoningEndTime) {
|
newMessage = {
|
||||||
// currentReasoningEndTime = Date.now()
|
role: 'tool-call',
|
||||||
//
|
toolName: event.toolName,
|
||||||
// // Update reasoning end time and stop streaming
|
toolArgs: event.args || {},
|
||||||
// setConversations((prev) => {
|
callId: event.toolCallId || 'unknown',
|
||||||
// const updated = [...prev]
|
}
|
||||||
// const conv = updated.find((c) => c.id === conversationId)
|
scrollToBottom(true)
|
||||||
// if (conv && assistantMessageIndex >= 0) {
|
} else {
|
||||||
// const assistantMessage = conv.messages[assistantMessageIndex]
|
console.warn('Tool call event missing toolName:', event)
|
||||||
// if (assistantMessage && hasContent(assistantMessage) && assistantMessage.role === 'assistant') {
|
}
|
||||||
// assistantMessage.isReasoningStreaming = false
|
} else if (event.type === 'tool-result') {
|
||||||
// assistantMessage.reasoningEndTime = currentReasoningEndTime
|
console.info(`TODO TOOL RESULT`, event)
|
||||||
// }
|
// if (event.toolName && event.toolCallId) {
|
||||||
// }
|
// setConversations((prev) => {
|
||||||
// return updated
|
// const updated = [...prev]
|
||||||
// })
|
// const conv = updated.find((c) => c.id === conversationId)
|
||||||
//
|
// if (conv) {
|
||||||
// // Reset reasoning state after reasoning ends
|
// conv.messages.push({
|
||||||
// currentReasoningStartTime = undefined
|
// role: 'tool-result',
|
||||||
// currentReasoningEndTime = undefined
|
// toolName: event.toolName,
|
||||||
// // Force creation of new assistant message for content
|
// toolArgs: event.args || {},
|
||||||
// needsNewAssistantMessage = true
|
// toolResult: event.result,
|
||||||
// assistantMessageCreated = false
|
// callId: event.toolCallId,
|
||||||
// }
|
// })
|
||||||
//
|
// debugMessages('Added new tool-result message:', event.toolName, 'callId:', event.toolCallId)
|
||||||
// // Create assistant message on first text delta if not already created OR if we just finished reasoning
|
// debugMessages('Current messages:', JSON.stringify(conv.messages))
|
||||||
// if (!assistantMessageCreated || needsNewAssistantMessage) {
|
// }
|
||||||
// setConversations((prev) => {
|
// return updated
|
||||||
// const updated = [...prev]
|
// })
|
||||||
// const conv = updated.find((c) => c.id === conversationId)
|
// scrollToBottom(true)
|
||||||
// if (conv) {
|
// } else {
|
||||||
// conv.messages.push({ role: 'assistant', content: '' })
|
// console.warn('Tool result event missing toolName or toolCallId:', event)
|
||||||
// assistantMessageIndex = conv.messages.length - 1
|
// }
|
||||||
// debugMessages(
|
} else if (event.type === 'text-delta') {
|
||||||
// 'Added new content assistant message at index',
|
console.info(`TODO TEXT DELTA`, event)
|
||||||
// assistantMessageIndex,
|
|
||||||
// '(after reasoning:',
|
// // Check if we have an existing reasoning message that needs to be finalized
|
||||||
// needsNewAssistantMessage,
|
// let needsNewAssistantMessage = false
|
||||||
// ')'
|
// if (currentReasoningStartTime && !currentReasoningEndTime) {
|
||||||
// )
|
// currentReasoningEndTime = Date.now()
|
||||||
// debugMessages('Current messages:', JSON.stringify(conv.messages))
|
//
|
||||||
// }
|
// // Update reasoning end time and stop streaming
|
||||||
// return updated
|
// setConversations((prev) => {
|
||||||
// })
|
// const updated = [...prev]
|
||||||
// assistantMessageCreated = true
|
// const conv = updated.find((c) => c.id === conversationId)
|
||||||
// }
|
// if (conv && assistantMessageIndex >= 0) {
|
||||||
//
|
// const assistantMessage = conv.messages[assistantMessageIndex]
|
||||||
// aiResponse += event.textDelta
|
// if (assistantMessage && hasContent(assistantMessage) && assistantMessage.role === 'assistant') {
|
||||||
// aiResponseRef.current = aiResponse
|
// assistantMessage.isReasoningStreaming = false
|
||||||
//
|
// assistantMessage.reasoningEndTime = currentReasoningEndTime
|
||||||
// //custom extraction of <chat-title> tag
|
// }
|
||||||
// const titleMatch = aiResponse.match(/<chat-title>(.*?)<\/chat-title>/)
|
// }
|
||||||
// if (titleMatch) {
|
// return updated
|
||||||
// const title = titleMatch[1].trim()
|
// })
|
||||||
// setConversations((prev) => {
|
//
|
||||||
// const updated = [...prev]
|
// // Reset reasoning state after reasoning ends
|
||||||
// const conv = updated.find((c) => c.id === conversationId)
|
// currentReasoningStartTime = undefined
|
||||||
// if (conv) {
|
// currentReasoningEndTime = undefined
|
||||||
// conv.title = title
|
// // Force creation of new assistant message for content
|
||||||
// }
|
// needsNewAssistantMessage = true
|
||||||
// return updated
|
// assistantMessageCreated = false
|
||||||
// })
|
// }
|
||||||
// aiResponse = aiResponse.replace(/<chat-title>.*?<\/chat-title>/, '').trim()
|
//
|
||||||
// }
|
// // Create assistant message on first text delta if not already created OR if we just finished reasoning
|
||||||
//
|
// if (!assistantMessageCreated || needsNewAssistantMessage) {
|
||||||
// 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)
|
||||||
// if (conv) {
|
// if (conv) {
|
||||||
// // Update the specific assistant message we created, not just the last message
|
// conv.messages.push({ role: 'assistant', content: '' })
|
||||||
// const assistantMessage = conv.messages[assistantMessageIndex]
|
// assistantMessageIndex = conv.messages.length - 1
|
||||||
// if (assistantMessage && hasContent(assistantMessage) && assistantMessage.role === 'assistant') {
|
// debugMessages(
|
||||||
// assistantMessage.content = aiResponse
|
// 'Added new content assistant message at index',
|
||||||
// }
|
// assistantMessageIndex,
|
||||||
// } else {
|
// '(after reasoning:',
|
||||||
// }
|
// needsNewAssistantMessage,
|
||||||
// return updated
|
// ')'
|
||||||
// })
|
// )
|
||||||
//
|
// debugMessages('Current messages:', JSON.stringify(conv.messages))
|
||||||
// scrollToBottom(true)
|
// }
|
||||||
|
// return updated
|
||||||
|
// })
|
||||||
|
// assistantMessageCreated = true
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// aiResponse += event.textDelta
|
||||||
|
// aiResponseRef.current = aiResponse
|
||||||
|
//
|
||||||
|
// //custom extraction of <chat-title> tag
|
||||||
|
// const titleMatch = aiResponse.match(/<chat-title>(.*?)<\/chat-title>/)
|
||||||
|
// if (titleMatch) {
|
||||||
|
// const title = titleMatch[1].trim()
|
||||||
|
// setConversations((prev) => {
|
||||||
|
// const updated = [...prev]
|
||||||
|
// const conv = updated.find((c) => c.id === conversationId)
|
||||||
|
// if (conv) {
|
||||||
|
// conv.title = title
|
||||||
|
// }
|
||||||
|
// return updated
|
||||||
|
// })
|
||||||
|
// aiResponse = aiResponse.replace(/<chat-title>.*?<\/chat-title>/, '').trim()
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// setConversations((prev) => {
|
||||||
|
// const updated = [...prev]
|
||||||
|
// const conv = updated.find((c) => c.id === conversationId)
|
||||||
|
// if (conv) {
|
||||||
|
// // Update the specific assistant message we created, not just the last message
|
||||||
|
// const assistantMessage = conv.messages[assistantMessageIndex]
|
||||||
|
// if (assistantMessage && hasContent(assistantMessage) && assistantMessage.role === 'assistant') {
|
||||||
|
// assistantMessage.content = aiResponse
|
||||||
|
// }
|
||||||
|
// } else {
|
||||||
|
// }
|
||||||
|
// return updated
|
||||||
|
// })
|
||||||
|
//
|
||||||
|
// scrollToBottom(true)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('Error in full stream processing:', e)
|
console.error('Error in full stream processing:', e)
|
||||||
}
|
}
|
||||||
|
let updated = prev
|
||||||
|
if (updatedMessage || newMessage) {
|
||||||
|
updated = prev.map((c) => {
|
||||||
|
if (c.id !== conversationId) return c
|
||||||
|
const messages = c.messages
|
||||||
|
|
||||||
|
if (updatedMessage) {
|
||||||
|
messages.splice(-1, 1, updatedMessage)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (newMessage) {
|
||||||
|
messages.push(newMessage)
|
||||||
|
}
|
||||||
|
|
||||||
|
return { ...c, messages }
|
||||||
|
})
|
||||||
|
}
|
||||||
|
console.log({ updatedMessage, newMessage })
|
||||||
return updated
|
return updated
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue