(messages: MessageRecord[])
| 159 | * Extracts the first meaningful user message from conversation messages. |
| 160 | */ |
| 161 | export const extractFirstUserMessage = (messages: MessageRecord[]): string => { |
| 162 | const userMessage = messages |
| 163 | // First try filtering out slash commands. |
| 164 | .filter((msg) => { |
| 165 | const content = partListUnionToString(msg.content); |
| 166 | return ( |
| 167 | !content.startsWith('/') && |
| 168 | !content.startsWith('?') && |
| 169 | content.trim().length > 0 |
| 170 | ); |
| 171 | }) |
| 172 | .find((msg) => msg.type === 'user'); |
| 173 | |
| 174 | let content: string; |
| 175 | |
| 176 | if (!userMessage) { |
| 177 | // Fallback to first user message even if it's a slash command |
| 178 | const firstMsg = messages.find((msg) => msg.type === 'user'); |
| 179 | if (!firstMsg) return 'Empty conversation'; |
| 180 | content = cleanMessage(partListUnionToString(firstMsg.content)); |
| 181 | } else { |
| 182 | content = cleanMessage(partListUnionToString(userMessage.content)); |
| 183 | } |
| 184 | |
| 185 | return content; |
| 186 | }; |
| 187 | |
| 188 | /** |
| 189 | * Formats a timestamp as relative time. |
no test coverage detected