An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
refactor: Update chat route and components
Yoshiki Miura811df8bcf3d4
diff --git a/app/api/chat/route.ts b/app/api/chat/route.tsindex a5a76065..70c5959f 100644--- a/app/api/chat/route.ts+++ b/app/api/chat/route.ts@@ -1,11 +1,19 @@import { streamText, createDataStreamResponse } from 'ai'import { researcher } from '@/lib/agents/researcher'import { generateRelatedQuestions } from '@/lib/agents/generate-related-questions'+import { cookies } from 'next/headers'export const maxDuration = 30+const DEFAULT_MODEL = 'openai:gpt-4o-mini'+export async function POST(req: Request) {- const { messages, model } = await req.json()+ const { messages } = await req.json()++ // Get the model from the cookie+ const cookieStore = await cookies()+ const modelFromCookie = cookieStore.get('selected-model')?.value+ const model = modelFromCookie || DEFAULT_MODELreturn createDataStreamResponse({execute: dataStream => {diff --git a/components/chat-panel.tsx b/components/chat-panel.tsxindex e85f80c0..9f9eb0b9 100644--- a/components/chat-panel.tsx+++ b/components/chat-panel.tsx@@ -100,15 +100,7 @@ export function ChatPanel({<Plus className="h-4 w-4" /></Button>)}- {messages.length === 0 && (- <ModelSelector- selectedModelId={selectedModelId}- onModelChange={id => {- setSelectedModelId(id)- onModelChange?.(id)- }}- />- )}+ {messages.length === 0 && <ModelSelector />}<Textarearef={inputRef}name="input"diff --git a/components/chat.tsx b/components/chat.tsxindex 716fe501..0591fdb0 100644--- a/components/chat.tsx+++ b/components/chat.tsx@@ -3,17 +3,8 @@import { useChat } from 'ai/react'import { ChatMessages } from './chat-messages'import { ChatPanel } from './chat-panel'-import { useLocalStorage } from '@/lib/hooks/use-local-storage'-import { models } from '@/lib/types/models'-import { getDefaultModelId } from '@/lib/utils'-import { Spinner } from './ui/spinner'export function Chat() {- const [selectedModelId, setSelectedModelId] = useLocalStorage<string>(- 'selectedModel',- getDefaultModelId(models)- )-const {messages,input,@@ -23,11 +14,7 @@ export function Chat() {setMessages,stop,append- } = useChat({- body: {- model: selectedModelId- }- })+ } = useChat()return (<div className="flex flex-col w-full max-w-3xl pt-10 pb-16 mx-auto stretch">diff --git a/components/model-selector.tsx b/components/model-selector.tsxindex 55ae734b..b9f66d9c 100644--- a/components/model-selector.tsx+++ b/components/model-selector.tsx@@ -12,11 +12,8 @@ import {import Image from 'next/image'import { Model, models } from '@/lib/types/models'import { createModelId } from '@/lib/utils'--interface ModelSelectorProps {- selectedModelId: string- onModelChange: (id: string) => void-}+import { getCookie, setCookie } from '@/lib/utils/cookies'+import { useEffect, useState } from 'react'function groupModelsByProvider(models: Model[]) {return models.reduce((groups, model) => {@@ -29,12 +26,21 @@ function groupModelsByProvider(models: Model[]) {}, {} as Record<string, Model[]>)}-export function ModelSelector({- selectedModelId,- onModelChange-}: ModelSelectorProps) {+export function ModelSelector() {+ const [selectedModelId, setSelectedModelId] = useState<string>('')++ useEffect(() => {+ const savedModel = getCookie('selected-model')+ if (savedModel) {+ setSelectedModelId(savedModel)+ }+ }, [])+const handleModelChange = (id: string) => {- onModelChange(id)+ if (!id) return++ setCookie('selected-model', id)+ setSelectedModelId(id)}const groupedModels = groupModelsByProvider(models)diff --git a/lib/utils/cookies.ts b/lib/utils/cookies.tsnew file mode 100644index 00000000..a3cfa634--- /dev/null+++ b/lib/utils/cookies.ts@@ -0,0 +1,21 @@+export function setCookie(name: string, value: string, days = 30) {+ const date = new Date()+ date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000)+ const expires = `expires=${date.toUTCString()}`+ document.cookie = `${name}=${value};${expires};path=/`+}++export function getCookie(name: string): string | null {+ const cookies = document.cookie.split(';')+ for (const cookie of cookies) {+ const [cookieName, cookieValue] = cookie.trim().split('=')+ if (cookieName === name) {+ return cookieValue+ }+ }+ return null+}++export function deleteCookie(name: string) {+ document.cookie = `${name}=;expires=Thu, 01 Jan 1970 00:00:00 GMT;path=/`+}