Skip to content

An AI-powered search engine with a generative UI

refactor: Replace Message type with UIMessage across chat components and API

Yoshiki Miuradb8a2462659e
diff --git a/app/api/chat/route.ts b/app/api/chat/route.ts
index b67a3ba4..3f2fba19 100644
--- a/app/api/chat/route.ts
+++ b/app/api/chat/route.ts
@@ -22,9 +22,6 @@ export async function POST(req: Request) {
const isSharePage = referer?.includes('/share/')
const userId = await getCurrentUserId()
- console.log('message', message)
- console.log('chatId', chatId)
-
if (isSharePage) {
return new Response('Chat API is not available on share pages', {
status: 403,
diff --git a/components/chat-panel.tsx b/components/chat-panel.tsx
index 57d4c040..8c908238 100644
--- a/components/chat-panel.tsx
+++ b/components/chat-panel.tsx
@@ -2,7 +2,7 @@
import { Model } from '@/lib/types/models'
import { cn } from '@/lib/utils'
-import { Message } from 'ai'
+import { UIMessage } from '@ai-sdk/react'
import { ArrowUp, ChevronDown, MessageCirclePlus, Square } from 'lucide-react'
import { useRouter } from 'next/navigation'
import { useEffect, useRef, useState } from 'react'
@@ -19,8 +19,8 @@ interface ChatPanelProps {
handleInputChange: (e: React.ChangeEvent<HTMLTextAreaElement>) => void
handleSubmit: (e: React.FormEvent<HTMLFormElement>) => void
isLoading: boolean
- messages: Message[]
- setMessages: (messages: Message[]) => void
+ messages: UIMessage[]
+ setMessages: (messages: UIMessage[]) => void
query?: string
stop: () => void
append: (message: any) => void
diff --git a/components/render-message.tsx b/components/render-message.tsx
index ed575e32..a119590f 100644
--- a/components/render-message.tsx
+++ b/components/render-message.tsx
@@ -1,11 +1,11 @@
-import { Message } from 'ai'
+import { UIMessage } from '@ai-sdk/react'
import { AnswerSection } from './answer-section'
import { ReasoningSection } from './reasoning-section'
import { ToolSection } from './tool-section'
import { UserMessage } from './user-message'
interface RenderMessageProps {
- message: Message
+ message: UIMessage
messageId: string
getIsOpen: (id: string) => boolean
onOpenChange: (id: string, open: boolean) => void
@@ -76,7 +76,7 @@ export function RenderMessage({
<ReasoningSection
key={`${messageId}-reasoning-${index}`}
content={{
- reasoning: part.reasoning,
+ reasoning: part.text,
isDone: index !== (message.parts?.length ?? 0) - 1
}}
isOpen={getIsOpen(messageId)}
diff --git a/components/user-message.tsx b/components/user-message.tsx
index 9fc32b7f..a39930cc 100644
--- a/components/user-message.tsx
+++ b/components/user-message.tsx
@@ -1,7 +1,7 @@
'use client'
import { cn } from '@/lib/utils'
-import { Message } from 'ai'
+import { UIMessage } from '@ai-sdk/react'
import { Pencil } from 'lucide-react'
import React, { useState } from 'react'
import TextareaAutosize from 'react-textarea-autosize'
@@ -9,7 +9,7 @@ import { CollapsibleMessage } from './collapsible-message'
import { Button } from './ui/button'
type UserMessageProps = {
- message: Message
+ message: UIMessage
messageId?: string
onUpdateMessage?: (messageId: string, newContent: string) => Promise<void>
}
diff --git a/lib/streaming/create-tool-calling-stream.ts b/lib/streaming/create-tool-calling-stream.ts
index abdbfb66..86657214 100644
--- a/lib/streaming/create-tool-calling-stream.ts
+++ b/lib/streaming/create-tool-calling-stream.ts
@@ -1,6 +1,5 @@
import { researcher } from '@/lib/agents/researcher'
import { openai } from '@ai-sdk/openai'
-import { UIMessage } from '@ai-sdk/react'
import {
appendClientMessage,
convertToModelMessages,
@@ -31,13 +30,7 @@ export function createToolCallingStreamResponse(config: BaseStreamConfig) {
model: openai('gpt-4o-mini')
})
- const userMessageToSave: UIMessage = {
- id: message.id,
- role: message.role,
- parts: [{ type: 'text', text: message.content }]
- }
-
- await saveChatMessage(chatId, userMessageToSave, userId, title)
+ await saveChatMessage(chatId, message, userId, title)
} else {
if (chat.userId !== userId) {
// TODO: Handle this
diff --git a/lib/streaming/types.ts b/lib/streaming/types.ts
index d207ff18..97c3ce0c 100644
--- a/lib/streaming/types.ts
+++ b/lib/streaming/types.ts
@@ -1,8 +1,8 @@
-import { Message } from 'ai'
+import { UIMessage } from '@ai-sdk/react'
import { Model } from '../types/models'
export interface BaseStreamConfig {
- message: Message
+ message: UIMessage
model: Model
chatId: string
searchMode: boolean