Starting to realise how dumb the vibecoded UI was oops

This commit is contained in:
Glen Maddern 2025-06-25 19:42:07 +10:00
commit caed6d373c

View file

@ -155,55 +155,57 @@ 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') {
lastMessage.reasoningEndTime = Date.now() updatedMessage = {
...lastMessage,
reasoningEndTime: Date.now(),
isReasoningStreaming: false,
}
} }
if (event.type === 'tool-call') { if (event.type === 'tool-call') {
if (event.toolName) { if (event.toolName) {
conv.messages.push({ newMessage = {
role: 'tool-call', role: 'tool-call',
toolName: event.toolName, toolName: event.toolName,
toolArgs: event.args || {}, toolArgs: event.args || {},
callId: event.toolCallId || 'unknown', callId: event.toolCallId || 'unknown',
}) }
debugMessages('Added new tool-call message:', event.toolName, 'callId:', event.toolCallId)
debugMessages('Current messages:', JSON.stringify(conv.messages))
scrollToBottom(true) scrollToBottom(true)
} else { } else {
console.warn('Tool call event missing toolName:', event) console.warn('Tool call event missing toolName:', event)
@ -317,9 +319,28 @@ export const useStreamResponse = ({
// //
// scrollToBottom(true) // 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
}) })
} }