message-utils.ts 973 B

12345678910111213141516171819202122232425
  1. import type { UIMessage } from 'ai'
  2. /**
  3. * Prepares messages for API transmission by cleaning and limiting history
  4. */
  5. export function prepareMessagesForAPI(messages: UIMessage[]): UIMessage[] {
  6. // [Joshen] Specifically limiting the chat history that get's sent to reduce the
  7. // size of the context that goes into the model. This should always be an odd number
  8. // as much as possible so that the first message is always the user's
  9. const MAX_CHAT_HISTORY = 7
  10. const slicedMessages = messages.slice(-MAX_CHAT_HISTORY)
  11. // Filter out results from messages before sending to the model
  12. const cleanedMessages = slicedMessages.map((_message) => {
  13. const message = _message as UIMessage & { results?: unknown }
  14. const cleanedMessage = { ...message } as UIMessage & { results?: unknown }
  15. if (message.role === 'assistant' && message.results) {
  16. delete cleanedMessage.results
  17. }
  18. return cleanedMessage as UIMessage
  19. })
  20. return cleanedMessages
  21. }