Skip to content

An AI-powered search engine with a generative UI

refactor: handle search mode server-side like model type

Yoshiki Miura05fb2a5c70a6
diff --git a/app/api/chat/route.ts b/app/api/chat/route.ts
index cc873ac3..13dd135a 100644
--- a/app/api/chat/route.ts
+++ b/app/api/chat/route.ts
@@ -2,6 +2,7 @@ import { cookies } from 'next/headers'
import { getCurrentUserId } from '@/lib/auth/get-current-user'
import { createChatStreamResponse } from '@/lib/streaming/create-chat-stream-response'
+import { SearchMode } from '@/lib/types/search'
import { selectModel } from '@/lib/utils/model-selection'
import { perfLog, perfTime } from '@/lib/utils/perf-logging'
import { resetAllCounters } from '@/lib/utils/perf-tracking'
@@ -20,7 +21,7 @@ export async function POST(req: Request) {
try {
const body = await req.json()
- const { message, chatId, trigger, messageId, isNewChat, searchMode } = body
+ const { message, chatId, trigger, messageId, isNewChat } = body
perfLog(
`API Route - Start: chatId=${chatId}, trigger=${trigger}, isNewChat=${isNewChat}`
@@ -72,6 +73,14 @@ export async function POST(req: Request) {
cookieStore
})
+ // Get search mode from cookie
+ const searchModeCookie = cookieStore.get('searchMode')?.value
+ const searchMode: SearchMode =
+ searchModeCookie &&
+ ['quick', 'planning', 'adaptive'].includes(searchModeCookie)
+ ? (searchModeCookie as SearchMode)
+ : 'adaptive'
+
if (!isProviderEnabled(selectedModel.providerId)) {
return new Response(
`Selected provider is not enabled ${selectedModel.providerId}`,
diff --git a/components/chat.tsx b/components/chat.tsx
index e58a382d..579862b9 100644
--- a/components/chat.tsx
+++ b/components/chat.tsx
@@ -14,9 +14,7 @@ import {
isToolCallPart,
isToolTypePart
} from '@/lib/types/dynamic-tools'
-import { SearchMode } from '@/lib/types/search'
import { cn } from '@/lib/utils'
-import { getCookie } from '@/lib/utils/cookies'
import { useAuthCheck } from '@/hooks/use-auth-check'
import { useFileDropzone } from '@/hooks/use-file-dropzone'
@@ -60,15 +58,6 @@ export function Chat({
})
const { isAuthenticated } = useAuthCheck()
- // Get search mode from cookie - initialize with cookie value
- const [searchMode] = useState<SearchMode>(() => {
- const savedMode = getCookie('searchMode')
- if (savedMode && ['quick', 'planning', 'adaptive'].includes(savedMode)) {
- return savedMode as SearchMode
- }
- return 'adaptive'
- })
-
const {
messages,
status,
@@ -102,8 +91,10 @@ export function Chat({
: trigger === 'submit-message'
? lastMessage
: undefined,
- isNewChat: trigger === 'submit-message' && messages.length === 1,
- searchMode // Include search mode in the request
+ isNewChat:
+ trigger === 'submit-message' &&
+ messages.length === 1 &&
+ savedMessages.length === 0
}
}
}