( chatHistory: ChatHistoryItem[], )
| 193 | * @returns The pruned chat history ending with assistant or tool message |
| 194 | */ |
| 195 | export function pruneLastMessage( |
| 196 | chatHistory: ChatHistoryItem[], |
| 197 | ): ChatHistoryItem[] { |
| 198 | if (chatHistory.length === 0) { |
| 199 | return chatHistory; |
| 200 | } |
| 201 | |
| 202 | if (chatHistory.length === 1) { |
| 203 | // Only one message - always return empty array |
| 204 | return []; |
| 205 | } |
| 206 | |
| 207 | const secondToLastIndex = chatHistory.length - 2; |
| 208 | const secondToLastItem = chatHistory[secondToLastIndex]; |
| 209 | |
| 210 | if ( |
| 211 | secondToLastItem.message.role === "assistant" && |
| 212 | (secondToLastItem.message as any).toolCalls?.length > 0 |
| 213 | ) { |
| 214 | return chatHistory.slice(0, -2); |
| 215 | } else if (secondToLastItem.message.role === "user") { |
| 216 | return chatHistory.slice(0, -2); |
| 217 | } |
| 218 | |
| 219 | return chatHistory.slice(0, -1); |
| 220 | } |
| 221 | |
| 222 | export function getHistoryForLLM( |
| 223 | fullHistory: ChatHistoryItem[], |
no outgoing calls
no test coverage detected