(role, content, customId = null, metadata = {})
| 93 | }; |
| 94 | |
| 95 | const addMessage = (role, content, customId = null, metadata = {}) => { |
| 96 | let updatedChat = null; |
| 97 | setChats((prev) => { |
| 98 | const newChats = prev.map((chat) => { |
| 99 | if (chat.id === activeChatId) { |
| 100 | const newMessage = { |
| 101 | id: customId || generateId(), |
| 102 | role, |
| 103 | content, |
| 104 | timestamp: Date.now(), |
| 105 | ...metadata, |
| 106 | }; |
| 107 | |
| 108 | const updatedMessages = [...chat.messages, newMessage]; |
| 109 | |
| 110 | let updatedTitle = chat.title; |
| 111 | if (chat.messages.length === 0 && role === "user") { |
| 112 | updatedTitle = |
| 113 | content.substring(0, 40) + |
| 114 | (content.length > 40 ? "..." : ""); |
| 115 | } |
| 116 | |
| 117 | updatedChat = { |
| 118 | ...chat, |
| 119 | messages: updatedMessages, |
| 120 | title: updatedTitle, |
| 121 | }; |
| 122 | return updatedChat; |
| 123 | } |
| 124 | return chat; |
| 125 | }); |
| 126 | return newChats; |
| 127 | }); |
| 128 | return updatedChat; |
| 129 | }; |
| 130 | const updateMessage = (messageId, updates) => { |
| 131 | setChats((prev) => |
| 132 | prev.map((chat) => { |
no test coverage detected