An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
Update dependencies and fix UI layout
Yoshiki Miuraf580c0bed624
diff --git a/app/action.tsx b/app/action.tsxindex 6b1260a3..08fa9603 100644--- a/app/action.tsx+++ b/app/action.tsx@@ -1,16 +1,18 @@import {- StreamableValue,createAI,createStreamableUI,createStreamableValue,getMutableAIState} from 'ai/rsc'-import { ExperimentalMessage } from 'ai'+import { ExperimentalMessage, nanoid, ToolResultPart } from 'ai'import { Spinner } from '@/components/ui/spinner'import { Section } from '@/components/section'import { FollowupPanel } from '@/components/followup-panel'import { inquire, researcher, taskManager, querySuggestor } from '@/lib/agents'import { writer } from '@/lib/agents/writer'+import { saveChat } from '@/lib/actions/chat'+import { Chat } from '@/lib/types'+import { AIMessage } from '@/lib/types'async function submit(formData?: FormData, skip?: boolean) {'use server'@@ -19,8 +21,10 @@ async function submit(formData?: FormData, skip?: boolean) {const uiStream = createStreamableUI()const isGenerating = createStreamableValue(true)const isCollapsed = createStreamableValue(false)+ const messages: ExperimentalMessage[] = aiState.get().messages as any[]++ console.log('submit', aiState.get())- const messages: ExperimentalMessage[] = aiState.get() as anyconst useSpecificAPI = process.env.USE_SPECIFIC_API_FOR_WRITER === 'true'const maxMessages = useSpecificAPI ? 5 : 10// Limit the number of messages to the maximum@@ -34,11 +38,25 @@ async function submit(formData?: FormData, skip?: boolean) {: formData? JSON.stringify(Object.fromEntries(formData)): null+ const type = skip ? 'skip' : userInput ? 'input' : 'selection'// Add the user message to the stateif (content) {- const message = { role: 'user', content }- messages.push(message as ExperimentalMessage)- aiState.update([...(aiState.get() as any), message])+ aiState.update({+ ...aiState.get(),+ messages: [+ ...aiState.get().messages,+ {+ id: 'hello',+ role: 'user',+ content,+ type+ }+ ]+ })+ messages.push({+ role: 'user',+ content+ })}async function processEvents() {@@ -48,15 +66,11 @@ async function submit(formData?: FormData, skip?: boolean) {if (action.object.next === 'inquire') {// Generate inquiry- const inquiry = await inquire(uiStream, messages)+ await inquire(uiStream, messages)uiStream.done()isGenerating.done()isCollapsed.done(false)- aiState.done([- ...aiState.get(),- { role: 'assistant', content: `inquiry: ${inquiry?.question}` }- ])return}@@ -65,7 +79,7 @@ async function submit(formData?: FormData, skip?: boolean) {// Generate the answerlet answer = ''- let toolOutputs = []+ let toolOutputs: ToolResultPart[] = []let errorOccurred = falseconst streamText = createStreamableValue<string>()uiStream.update(<Spinner />)@@ -87,14 +101,28 @@ async function submit(formData?: FormData, skip?: boolean) {answer = fullResponsetoolOutputs = toolResponseserrorOccurred = hasError++ // if (toolOutputs.length > 0) {+ // toolOutputs.map(output => {+ // aiState.update({+ // ...aiState.get(),+ // messages: []+ // })+ // })+ // }}// If useSpecificAPI is enabled, generate the answer using the specific modelif (useSpecificAPI && answer.length === 0) {// modify the messages to be used by the specific model- const modifiedMessages = messages.map(msg =>- msg.role === 'tool'- ? { ...msg, role: 'assistant', content: JSON.stringify(msg.content) }+ const modifiedMessages = aiState.get().messages.map(msg =>+ msg.role === 'function'+ ? {+ ...msg,+ role: 'assistant',+ content: JSON.stringify(msg.content),+ type: 'tool'+ }: msg) as ExperimentalMessage[]answer = await writer(uiStream, streamText, modifiedMessages)@@ -102,9 +130,25 @@ async function submit(formData?: FormData, skip?: boolean) {streamText.done()}+ // aiState.update({+ // ...aiState.get(),+ // messages: []+ // })+if (!errorOccurred) {// Generate related queries- await querySuggestor(uiStream, messages)+ const relatedQueries = await querySuggestor(uiStream, messages)+ // aiState.update({+ // ...aiState.get(),+ // messages: [+ // {+ // id: nanoid(),+ // role: 'assistant',+ // content: JSON.stringify(relatedQueries),+ // type: 'related'+ // }+ // ]+ // })// Add follow-up paneluiStream.append(@@ -116,42 +160,105 @@ async function submit(formData?: FormData, skip?: boolean) {isGenerating.done(false)uiStream.done()- aiState.done([...aiState.get(), { role: 'assistant', content: answer }])+ aiState.done(aiState.get())++ console.log('aiState', aiState.get())}processEvents()return {- id: Date.now(),+ id: nanoid(),isGenerating: isGenerating.value,component: uiStream.value,isCollapsed: isCollapsed.value}}-// Define the initial state of the AI. It can be any JSON object.-const initialAIState: {- role: 'user' | 'assistant' | 'system' | 'function' | 'tool'- content: string- id?: string- name?: string-}[] = []--// The initial UI state that the client will keep track of, which contains the message IDs and their UI nodes.-const initialUIState: {- id: number- isGenerating?: StreamableValue<boolean>- isCollapsed?: StreamableValue<boolean>+export type AIState = {+ messages: AIMessage[]+ chatId: string+}++export type UIState = {+ id: stringcomponent: React.ReactNode-}[] = []+ isGenerating?: boolean+ isCollapsed?: boolean+}[]++const initialAIState: AIState = {+ chatId: nanoid(),+ messages: []+}++const initialUIState: UIState = []// AI is a provider you wrap your application with so you can access AI and UI state in your components.-export const AI = createAI({+export const AI = createAI<AIState, UIState>({actions: {submit},- // Each state can be any shape of object, but for chat applications- // it makes sense to have an array of messages. Or you may prefer something like { id: number, messages: Message[] }initialUIState,initialAIState+ // unstable_onGetUIState: async () => {+ // 'use server'++ // const aiState = getAIState()+ // if (aiState) {+ // const uiState = getUIStateFromAIState(aiState)+ // return uiState+ // } else {+ // return+ // }+ // return []+ // },+ // unstable_onSetAIState: async ({ state, done }) => {+ // 'use server'++ // const { chatId, messages } = state+ // const createdAt = new Date()+ // const userId = 'anonymous'+ // const path = `/search/${chatId}`+ // const title =+ // JSON.parse(messages[0].content)?.input?.substring(0, 100) || 'Untitled'+ // const chat: Chat = { id: chatId, createdAt, userId, path, title, messages }+ // await saveChat(chat)+ // }})++export const getUIStateFromAIState = (aiState: Chat) => {+ return {+ id: aiState.chatId,+ component: <div></div>+ }+ // return aiState.messages+ // .filter(message => message.role !== 'system')+ // .map((message, index) => ({+ // id: `${aiState.chatId}-${index}`,+ // display:+ // message.role === 'function' ? (+ // message.name === 'listStocks' ? (+ // <BotCard>+ // <Stocks props={JSON.parse(message.content)} />+ // </BotCard>+ // ) : message.name === 'showStockPrice' ? (+ // <BotCard>+ // <Stock props={JSON.parse(message.content)} />+ // </BotCard>+ // ) : message.name === 'showStockPurchase' ? (+ // <BotCard>+ // <Purchase props={JSON.parse(message.content)} />+ // </BotCard>+ // ) : message.name === 'getEvents' ? (+ // <BotCard>+ // <Events props={JSON.parse(message.content)} />+ // </BotCard>+ // ) : null+ // ) : message.role === 'user' ? (+ // <UserMessage>{message.content}</UserMessage>+ // ) : (+ // <BotMessage content={message.content} />+ // )+ // }))+}diff --git a/app/layout.tsx b/app/layout.tsxindex 05260219..2b1c9175 100644--- a/app/layout.tsx+++ b/app/layout.tsx@@ -56,8 +56,8 @@ export default function RootLayout({><Header /><AI>{children}</AI>- <Footer /><Sidebar />+ <Footer /></ThemeProvider></body></html>diff --git a/bun.lockb b/bun.lockbindex de40b9ef..63e4d371 100755Binary files a/bun.lockb and b/bun.lockb differdiff --git a/components/chat-messages.tsx b/components/chat-messages.tsxindex 1d351477..8b99266d 100644--- a/components/chat-messages.tsx+++ b/components/chat-messages.tsx@@ -3,15 +3,16 @@ import type { AI } from '@/app/action'import { CollapsibleMessage } from './collapsible-message'export function ChatMessages() {- const [messages, setMessages] = useUIState<typeof AI>()+ const [messages] = useUIState<typeof AI>()return (<>{messages.map((message: {- id: number+ id: stringcomponent: React.ReactNodeisCollapsed?: StreamableValue<boolean>+ isGenerating?: StreamableValue<boolean>}) => (<CollapsibleMessagekey={message.id}diff --git a/components/chat-panel.tsx b/components/chat-panel.tsxindex b4a2c188..9ee9ee44 100644--- a/components/chat-panel.tsx+++ b/components/chat-panel.tsx@@ -7,12 +7,13 @@ import { Input } from './ui/input'import { Button } from './ui/button'import { ArrowRight, Plus, Square } from 'lucide-react'import { EmptyScreen } from './empty-screen'+import { nanoid } from 'ai'export function ChatPanel() {const [input, setInput] = useState('')const [messages, setMessages] = useUIState<typeof AI>()const [aiMessages, setAiMessages] = useAIState<typeof AI>()- const { submit } = useActions<typeof AI>()+ const { submit } = useActions()const [isButtonPressed, setIsButtonPressed] = useState(false)const inputRef = useRef<HTMLInputElement>(null)const [showEmptyScreen, setShowEmptyScreen] = useState(false)@@ -37,24 +38,25 @@ export function ChatPanel() {setMessages(currentMessages => [...currentMessages,{- id: Date.now(),+ id: nanoid(),component: <UserMessage message={input} />}])+ console.log('ai: ', aiMessages)+// Submit and get response messageconst formData = new FormData(e.currentTarget)const responseMessage = await submit(formData)setMessages(currentMessages => [...currentMessages, responseMessage as any])-- setInput('')+ console.log('responseMessage', responseMessage)}// Clear messagesconst handleClear = () => {setIsButtonPressed(true)- setMessages([])- setAiMessages([])+ // setMessages([])+ // setAiMessages(null)}useEffect(() => {diff --git a/components/collapsible-message.tsx b/components/collapsible-message.tsxindex 29ba41ce..43231248 100644--- a/components/collapsible-message.tsx+++ b/components/collapsible-message.tsx@@ -12,7 +12,7 @@ import { Separator } from './ui/separator'interface CollapsibleMessageProps {message: {- id: number+ id: stringisCollapsed?: StreamableValue<boolean>component: React.ReactNode}diff --git a/components/copilot.tsx b/components/copilot.tsxindex 83b8eccd..5d19dca5 100644--- a/components/copilot.tsx+++ b/components/copilot.tsx@@ -8,7 +8,7 @@ import { Button } from './ui/button'import { Card } from './ui/card'import { ArrowRight, Check, FastForward, Sparkles } from 'lucide-react'import { useActions, useStreamableValue, useUIState } from 'ai/rsc'-import { AI } from '@/app/action'+import type { AI } from '@/app/action'import { IconLogo } from './ui/icons'import { cn } from '@/lib/utils'@@ -25,8 +25,8 @@ export const Copilot: React.FC<CopilotProps> = ({ inquiry }: CopilotProps) => {[key: string]: boolean}>({})const [isButtonDisabled, setIsButtonDisabled] = useState(true)- const [messages, setMessages] = useUIState<typeof AI>()- const { submit } = useActions<typeof AI>()+ const [, setMessages] = useUIState<typeof AI>()+ const { submit } = useActions()const handleInputChange = (event: React.ChangeEvent<HTMLInputElement>) => {setQuery(event.target.value)@@ -73,8 +73,8 @@ export const Copilot: React.FC<CopilotProps> = ({ inquiry }: CopilotProps) => {? undefined: new FormData(e.target as HTMLFormElement)- const responseMessage = await submit(formData, skip)- setMessages(currentMessages => [...currentMessages, responseMessage])+ const response = await submit(formData, skip)+ setMessages(currentMessages => [...currentMessages, response])}const handleSkip = (e: React.MouseEvent<HTMLButtonElement>) => {diff --git a/components/followup-panel.tsx b/components/followup-panel.tsxindex 92b54f98..3afa00f6 100644--- a/components/followup-panel.tsx+++ b/components/followup-panel.tsx@@ -10,7 +10,7 @@ import { ArrowRight } from 'lucide-react'export function FollowupPanel() {const [input, setInput] = useState('')- const { submit } = useActions<typeof AI>()+ const { submit } = useActions()const [, setMessages] = useUIState<typeof AI>()const handleSubmit = async (event: React.FormEvent<HTMLFormElement>) => {diff --git a/components/history-item.tsx b/components/history-item.tsxindex 94e9cf01..e7c9795a 100644--- a/components/history-item.tsx+++ b/components/history-item.tsx@@ -1,6 +1,5 @@import React from 'react'import Link from 'next/link'-import { buttonVariants } from '@/components/ui/button'type HistoryItemProps = {query: stringdiff --git a/components/history.tsx b/components/history.tsxindex a74bb92f..cc53e8b1 100644--- a/components/history.tsx+++ b/components/history.tsx@@ -27,7 +27,7 @@ export function History({ location }: HistoryProps) {{location === 'header' ? <Menu /> : <ChevronLeft size={16} />}</Button></SheetTrigger>- <SheetContent className="w-64">+ <SheetContent className="w-64 rounded-tl-lg rounded-bl-lg"><SheetHeader><SheetTitle>Search history</SheetTitle></SheetHeader>diff --git a/components/search-related.tsx b/components/search-related.tsxindex 319ea714..c7d8e318 100644--- a/components/search-related.tsx+++ b/components/search-related.tsx@@ -15,7 +15,7 @@ export interface SearchRelatedProps {export const SearchRelated: React.FC<SearchRelatedProps> = ({relatedQueries}) => {- const { submit } = useActions<typeof AI>()+ const { submit } = useActions()const [, setMessages] = useUIState<typeof AI>()const [data, error, pending] =useStreamableValue<PartialRelated>(relatedQueries)diff --git a/components/ui/sheet.tsx b/components/ui/sheet.tsxindex 1b78df0d..86915b83 100644--- a/components/ui/sheet.tsx+++ b/components/ui/sheet.tsx@@ -21,7 +21,7 @@ const SheetOverlay = React.forwardRef<>(({ className, ...props }, ref) => (<SheetPrimitive.OverlayclassName={cn(- 'fixed inset-0 z-50 bg-black/20 data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0',+ 'fixed inset-0 z-50 bg-black/30 data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0',className)}{...props}diff --git a/lib/actions/chat.ts b/lib/actions/chat.tsindex d118094d..911d0fe8 100644--- a/lib/actions/chat.ts+++ b/lib/actions/chat.ts@@ -31,7 +31,7 @@ export async function getChats(userId?: string | null) {}}-export async function getChat(id: string, userId: string = 'morphic') {+export async function getChat(id: string, userId: string = 'anonymous') {const chat = await redis.hgetall<Chat>(`chat:${id}`)if (!chat) {@@ -68,7 +68,7 @@ export async function shareChat(id: string) {return payload}-export async function saveChat(chat: Chat, userId: string = 'morphic') {+export async function saveChat(chat: Chat, userId: string = 'anonymous') {const pipeline = redis.pipeline()pipeline.hmset(`chat:${chat.id}`, chat)pipeline.zadd(`user:chat:${chat.userId}`, {diff --git a/lib/agents/query-suggestor.tsx b/lib/agents/query-suggestor.tsxindex 6c31d955..f55d73ae 100644--- a/lib/agents/query-suggestor.tsx+++ b/lib/agents/query-suggestor.tsx@@ -4,6 +4,7 @@ import { PartialRelated, relatedSchema } from '@/lib/schema/related'import { Section } from '@/components/section'import SearchRelated from '@/components/search-related'import { OpenAI } from '@ai-sdk/openai'+import { PartialInquiry } from '../schema/inquiry'export async function querySuggestor(uiStream: ReturnType<typeof createStreamableUI>,@@ -21,6 +22,7 @@ export async function querySuggestor(</Section>)+ let finalRelatedQueries: PartialRelated = {}await experimental_streamObject({model: openai.chat(process.env.OPENAI_API_MODEL || 'gpt-4-turbo'),system: `As a professional web researcher, your task is to generate a set of three queries that explore the subject matter more deeply, building upon the initial query and the information uncovered in its search results.@@ -43,9 +45,12 @@ export async function querySuggestor(.then(async result => {for await (const obj of result.partialObjectStream) {objectStream.update(obj)+ finalRelatedQueries = obj}}).finally(() => {objectStream.done()})++ return finalRelatedQueries}diff --git a/lib/agents/researcher.tsx b/lib/agents/researcher.tsxindex ca3b7824..3a01bfaa 100644--- a/lib/agents/researcher.tsx+++ b/lib/agents/researcher.tsx@@ -11,7 +11,6 @@ import { OpenAI } from '@ai-sdk/openai'import { BotMessage } from '@/components/message'import Exa from 'exa-js'import { Card } from '@/components/ui/card'-import { SearchResults } from '../types'import { SearchSection } from '@/components/search-section'export async function researcher(diff --git a/lib/types/index.ts b/lib/types/index.tsindex 1341b647..463e91eb 100644--- a/lib/types/index.ts+++ b/lib/types/index.ts@@ -1,5 +1,3 @@-import { Message } from 'ai'-export type SearchResults = {images: string[]results: SearchResultItem[]@@ -18,6 +16,14 @@ export interface Chat extends Record<string, any> {createdAt: DateuserId: stringpath: string- messages: Message[]+ messages: AIMessage[]sharePath?: string}++export type AIMessage = {+ role: 'user' | 'assistant' | 'system' | 'function' | 'data' | 'tool'+ content: string+ id: string+ name?: string+ type?: 'answer' | 'related' | 'skip' | 'selection' | 'input' | 'tool'+}diff --git a/lib/utils/index.ts b/lib/utils/index.tsindex d32b0fe6..7db7716e 100644--- a/lib/utils/index.ts+++ b/lib/utils/index.ts@@ -1,6 +1,26 @@import { type ClassValue, clsx } from 'clsx'import { twMerge } from 'tailwind-merge'+import { AIMessage } from '../types'+import { ExperimentalMessage } from 'ai'export function cn(...inputs: ClassValue[]) {return twMerge(clsx(inputs))}++export function convertToExperimentalMessages(+ messages: AIMessage[]+): ExperimentalMessage[] {+ return messages+ .filter(+ message =>+ message.role === 'tool' ||+ message.role === 'assistant' ||+ message.role === 'user'+ )+ .map(message => {+ return {+ content: message.content,+ role: message.role+ } as ExperimentalMessage+ })+}diff --git a/package.json b/package.jsonindex 165dbc23..8f9632f3 100644--- a/package.json+++ b/package.json@@ -25,7 +25,7 @@"@radix-ui/react-tooltip": "^1.0.7","@tailwindcss/typography": "^0.5.12","@upstash/redis": "^1.30.0",- "ai": "3.0.14",+ "ai": "3.0.16","class-variance-authority": "^0.7.0","clsx": "^2.1.0","embla-carousel-react": "^8.0.0",