(conversationId: string, chunk: Chunk)
| 468 | } |
| 469 | |
| 470 | function addChunkToMessage(conversationId: string, chunk: Chunk): void { |
| 471 | const conv = state.conversations[conversationId] |
| 472 | if (!conv) return |
| 473 | |
| 474 | if (chunk.messageId) { |
| 475 | const messageIndex = conv.messages.findIndex( |
| 476 | (msg) => msg.id === chunk.messageId, |
| 477 | ) |
| 478 | |
| 479 | if (messageIndex !== -1) { |
| 480 | queueMessageChunk(conversationId, messageIndex, chunk) |
| 481 | return |
| 482 | } else { |
| 483 | // Create new message with the chunk |
| 484 | const newMessage: Message = { |
| 485 | id: chunk.messageId, |
| 486 | role: 'assistant', |
| 487 | content: '', |
| 488 | timestamp: chunk.timestamp, |
| 489 | model: conv.model, |
| 490 | chunks: [chunk], |
| 491 | } |
| 492 | setState( |
| 493 | 'conversations', |
| 494 | conversationId, |
| 495 | 'messages', |
| 496 | conv.messages.length, |
| 497 | newMessage, |
| 498 | ) |
| 499 | return |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | // Find last assistant message |
| 504 | for (let i = conv.messages.length - 1; i >= 0; i--) { |
| 505 | const message = conv.messages[i] |
| 506 | if (message && message.role === 'assistant') { |
| 507 | queueMessageChunk(conversationId, i, chunk) |
| 508 | return |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | // Fallback: add to conversation's main chunks array if no assistant message found |
| 513 | addChunk(conversationId, chunk) |
| 514 | } |
| 515 | |
| 516 | function updateMessageUsage( |
| 517 | conversationId: string, |
no test coverage detected