Skip to content

An AI-powered search engine with a generative UI

chore: Add new models and model selector component

Yoshiki Miura255a5ee8ec5e
diff --git a/bun.lockb b/bun.lockb
index be643263..40eb6a2e 100755
Binary files a/bun.lockb and b/bun.lockb differ
diff --git a/components/chat-panel.tsx b/components/chat-panel.tsx
index 9ee6cac9..1feebb77 100644
--- a/components/chat-panel.tsx
+++ b/components/chat-panel.tsx
@@ -12,13 +12,21 @@ import { EmptyScreen } from './empty-screen'
import Textarea from 'react-textarea-autosize'
import { generateId } from 'ai'
import { useAppState } from '@/lib/utils/app-state'
+import { ModelSelector } from './model-selector'
interface ChatPanelProps {
messages: UIState
query?: string
+ onModelChange?: (modelId: string) => void
+ defaultModel?: string
}
-export function ChatPanel({ messages, query }: ChatPanelProps) {
+export function ChatPanel({
+ messages,
+ query,
+ onModelChange,
+ defaultModel
+}: ChatPanelProps) {
const [input, setInput] = useState('')
const [showEmptyScreen, setShowEmptyScreen] = useState(false)
const [, setMessages] = useUIState<typeof AI>()
@@ -28,19 +36,20 @@ export function ChatPanel({ messages, query }: ChatPanelProps) {
const router = useRouter()
const inputRef = useRef<HTMLTextAreaElement>(null)
const isFirstRender = useRef(true) // For development environment
+ const [selectedModel, setSelectedModel] = useState(defaultModel || 'gpt-4')
const [isComposing, setIsComposing] = useState(false) // Composition state
const [enterDisabled, setEnterDisabled] = useState(false) // Disable Enter after composition ends
- const handleCompositionStart = () => setIsComposing(true);
+ const handleCompositionStart = () => setIsComposing(true)
const handleCompositionEnd = () => {
- setIsComposing(false);
- setEnterDisabled(true);
+ setIsComposing(false)
+ setEnterDisabled(true)
setTimeout(() => {
- setEnterDisabled(false);
- }, 300);
- };
+ setEnterDisabled(false)
+ }, 300)
+ }
async function handleQuerySubmit(query: string, formData?: FormData) {
setInput(query)
@@ -132,6 +141,13 @@ export function ChatPanel({ messages, query }: ChatPanelProps) {
>
<form onSubmit={handleSubmit} className="max-w-2xl w-full px-6">
<div className="relative flex items-center w-full">
+ <ModelSelector
+ selectedModel={selectedModel}
+ onModelChange={modelId => {
+ setSelectedModel(modelId)
+ onModelChange?.(modelId)
+ }}
+ />
<Textarea
ref={inputRef}
name="input"
@@ -143,7 +159,7 @@ export function ChatPanel({ messages, query }: ChatPanelProps) {
placeholder="Ask a question..."
spellCheck={false}
value={input}
- className="resize-none w-full min-h-12 rounded-fill bg-muted border border-input pl-4 pr-10 pt-3 pb-1 text-sm ring-offset-background file:border-0 file:bg-transparent file:text-sm file:font-medium placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50'"
+ className="resize-none w-full min-h-12 rounded-fill bg-muted border border-input pl-4 pr-10 pt-3 pb-1 text-sm ring-offset-background file:border-0 file:bg-transparent file:text-sm file:font-medium placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50"
onChange={e => {
setInput(e.target.value)
setShowEmptyScreen(e.target.value.length === 0)
diff --git a/components/model-selector.tsx b/components/model-selector.tsx
new file mode 100644
index 00000000..32e12e3b
--- /dev/null
+++ b/components/model-selector.tsx
@@ -0,0 +1,83 @@
+'use client'
+
+import {
+ Select,
+ SelectContent,
+ SelectGroup,
+ SelectItem,
+ SelectLabel,
+ SelectTrigger,
+ SelectValue
+} from './ui/select'
+import Image from 'next/image'
+import { Model, models } from '@/lib/types/models'
+import { useLocalStorage } from '@/lib/hooks/use-local-storage'
+
+interface ModelSelectorProps {
+ selectedModel: string
+ onModelChange: (modelId: string) => void
+}
+
+// Group models by provider
+function groupModelsByProvider(models: Model[]) {
+ return models.reduce((groups, model) => {
+ const provider = model.provider
+ if (!groups[provider]) {
+ groups[provider] = []
+ }
+ groups[provider].push(model)
+ return groups
+ }, {} as Record<string, Model[]>)
+}
+
+export function ModelSelector({
+ selectedModel: initialModel,
+ onModelChange
+}: ModelSelectorProps) {
+ const [selectedModel, setSelectedModel] = useLocalStorage(
+ 'selectedModel',
+ initialModel
+ )
+ const groupedModels = groupModelsByProvider(models)
+
+ const handleModelChange = (modelId: string) => {
+ setSelectedModel(modelId)
+ onModelChange(modelId)
+ }
+
+ return (
+ <div className="absolute -top-8 left-2">
+ <Select
+ name="model"
+ value={selectedModel}
+ onValueChange={handleModelChange}
+ >
+ <SelectTrigger className="mr-2 h-7 text-xs border-none shadow-none focus:ring-0">
+ <SelectValue placeholder="Select model" />
+ </SelectTrigger>
+ <SelectContent className="max-h-[200px] overflow-y-auto">
+ {Object.entries(groupedModels).map(([provider, models]) => (
+ <SelectGroup key={provider}>
+ <SelectLabel className="text-xs sticky top-0 bg-background z-10">
+ {provider}
+ </SelectLabel>
+ {models.map(model => (
+ <SelectItem key={model.id} value={model.id} className="py-2">
+ <div className="flex items-center space-x-2">
+ <Image
+ src={`/providers/logos/${model.providerId}.svg`}
+ alt={model.provider}
+ width={14}
+ height={14}
+ />
+ <span className="text-xs font-medium">{model.name}</span>
+ </div>
+ </SelectItem>
+ ))}
+ </SelectGroup>
+ ))}
+ </SelectContent>
+ </Select>
+ </div>
+ )
+}
diff --git a/components/ui/select.tsx b/components/ui/select.tsx
new file mode 100644
index 00000000..aed4c298
--- /dev/null
+++ b/components/ui/select.tsx
@@ -0,0 +1,160 @@
+'use client'
+
+import * as React from 'react'
+import * as SelectPrimitive from '@radix-ui/react-select'
+import { Check, ChevronDown, ChevronUp } from 'lucide-react'
+
+import { cn } from '@/lib/utils'
+
+const Select = SelectPrimitive.Root
+
+const SelectGroup = SelectPrimitive.Group
+
+const SelectValue = SelectPrimitive.Value
+
+const SelectTrigger = React.forwardRef<
+ React.ElementRef<typeof SelectPrimitive.Trigger>,
+ React.ComponentPropsWithoutRef<typeof SelectPrimitive.Trigger>
+>(({ className, children, ...props }, ref) => (
+ <SelectPrimitive.Trigger
+ ref={ref}
+ className={cn(
+ 'flex h-10 w-full items-center justify-between rounded-md border border-input bg-background px-3 py-2 text-sm ring-offset-background placeholder:text-muted-foreground focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50 [&>span]:line-clamp-1',
+ className
+ )}
+ {...props}
+ >
+ {children}
+ <SelectPrimitive.Icon asChild>
+ <ChevronDown className="h-4 w-4 opacity-50" />
+ </SelectPrimitive.Icon>
+ </SelectPrimitive.Trigger>
+))
+SelectTrigger.displayName = SelectPrimitive.Trigger.displayName
+
+const SelectScrollUpButton = React.forwardRef<
+ React.ElementRef<typeof SelectPrimitive.ScrollUpButton>,
+ React.ComponentPropsWithoutRef<typeof SelectPrimitive.ScrollUpButton>
+>(({ className, ...props }, ref) => (
+ <SelectPrimitive.ScrollUpButton
+ ref={ref}
+ className={cn(
+ 'flex cursor-default items-center justify-center py-1',
+ className
+ )}
+ {...props}
+ >
+ <ChevronUp className="h-4 w-4" />
+ </SelectPrimitive.ScrollUpButton>
+))
+SelectScrollUpButton.displayName = SelectPrimitive.ScrollUpButton.displayName
+
+const SelectScrollDownButton = React.forwardRef<
+ React.ElementRef<typeof SelectPrimitive.ScrollDownButton>,
+ React.ComponentPropsWithoutRef<typeof SelectPrimitive.ScrollDownButton>
+>(({ className, ...props }, ref) => (
+ <SelectPrimitive.ScrollDownButton
+ ref={ref}
+ className={cn(
+ 'flex cursor-default items-center justify-center py-1',
+ className
+ )}
+ {...props}
+ >
+ <ChevronDown className="h-4 w-4" />
+ </SelectPrimitive.ScrollDownButton>
+))
+SelectScrollDownButton.displayName =
+ SelectPrimitive.ScrollDownButton.displayName
+
+const SelectContent = React.forwardRef<
+ React.ElementRef<typeof SelectPrimitive.Content>,
+ React.ComponentPropsWithoutRef<typeof SelectPrimitive.Content>
+>(({ className, children, position = 'popper', ...props }, ref) => (
+ <SelectPrimitive.Portal>
+ <SelectPrimitive.Content
+ ref={ref}
+ className={cn(
+ 'relative z-50 max-h-96 min-w-[8rem] overflow-hidden rounded-md border bg-popover text-popover-foreground shadow-md data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2',
+ position === 'popper' &&
+ 'data-[side=bottom]:translate-y-1 data-[side=left]:-translate-x-1 data-[side=right]:translate-x-1 data-[side=top]:-translate-y-1',
+ className
+ )}
+ position={position}
+ {...props}
+ >
+ <SelectScrollUpButton />
+ <SelectPrimitive.Viewport
+ className={cn(
+ 'p-1',
+ position === 'popper' &&
+ 'h-[var(--radix-select-trigger-height)] w-full min-w-[var(--radix-select-trigger-width)]'
+ )}
+ >
+ {children}
+ </SelectPrimitive.Viewport>
+ <SelectScrollDownButton />
+ </SelectPrimitive.Content>
+ </SelectPrimitive.Portal>
+))
+SelectContent.displayName = SelectPrimitive.Content.displayName
+
+const SelectLabel = React.forwardRef<
+ React.ElementRef<typeof SelectPrimitive.Label>,
+ React.ComponentPropsWithoutRef<typeof SelectPrimitive.Label>
+>(({ className, ...props }, ref) => (
+ <SelectPrimitive.Label
+ ref={ref}
+ className={cn('py-1.5 pl-1.5 pr-12 text-sm font-semibold', className)}
+ {...props}
+ />
+))
+SelectLabel.displayName = SelectPrimitive.Label.displayName
+
+const SelectItem = React.forwardRef<
+ React.ElementRef<typeof SelectPrimitive.Item>,
+ React.ComponentPropsWithoutRef<typeof SelectPrimitive.Item>
+>(({ className, children, ...props }, ref) => (
+ <SelectPrimitive.Item
+ ref={ref}
+ className={cn(
+ 'relative flex w-full cursor-default select-none items-center rounded-sm py-1.5 pl-1.5 pr-12 text-sm outline-none focus:bg-accent focus:text-accent-foreground data-[disabled]:pointer-events-none data-[disabled]:opacity-50',
+ className
+ )}
+ {...props}
+ >
+ <span className="absolute right-2 flex h-3.5 w-3.5 items-center justify-center">
+ <SelectPrimitive.ItemIndicator>
+ <Check className="h-4 w-4" />
+ </SelectPrimitive.ItemIndicator>
+ </span>
+
+ <SelectPrimitive.ItemText>{children}</SelectPrimitive.ItemText>
+ </SelectPrimitive.Item>
+))
+SelectItem.displayName = SelectPrimitive.Item.displayName
+
+const SelectSeparator = React.forwardRef<
+ React.ElementRef<typeof SelectPrimitive.Separator>,
+ React.ComponentPropsWithoutRef<typeof SelectPrimitive.Separator>
+>(({ className, ...props }, ref) => (
+ <SelectPrimitive.Separator
+ ref={ref}
+ className={cn('-mx-1 my-1 h-px bg-muted', className)}
+ {...props}
+ />
+))
+SelectSeparator.displayName = SelectPrimitive.Separator.displayName
+
+export {
+ Select,
+ SelectGroup,
+ SelectValue,
+ SelectTrigger,
+ SelectContent,
+ SelectLabel,
+ SelectItem,
+ SelectSeparator,
+ SelectScrollUpButton,
+ SelectScrollDownButton
+}
diff --git a/lib/hooks/use-local-storage.ts b/lib/hooks/use-local-storage.ts
new file mode 100644
index 00000000..9940682b
--- /dev/null
+++ b/lib/hooks/use-local-storage.ts
@@ -0,0 +1,41 @@
+'use client'
+
+import { useState, useEffect } from 'react'
+
+export function useLocalStorage<T>(key: string, initialValue: T) {
+ // State to store our value
+ // Pass initial state function to useState so logic is only executed once
+ const [storedValue, setStoredValue] = useState<T>(() => {
+ try {
+ // Get from local storage by key
+ if (typeof window === 'undefined') {
+ return initialValue
+ }
+
+ const item = window.localStorage.getItem(key)
+ // Parse stored json or if none return initialValue
+ return item ? JSON.parse(item) : initialValue
+ } catch (error) {
+ // If error also return initialValue
+ console.warn(`Error reading localStorage key "${key}":`, error)
+ return initialValue
+ }
+ })
+
+ // useEffect to update local storage when the state changes
+ useEffect(() => {
+ try {
+ // Allow value to be a function so we have same API as useState
+ const valueToStore = storedValue
+ // Save state
+ if (typeof window !== 'undefined') {
+ window.localStorage.setItem(key, JSON.stringify(valueToStore))
+ }
+ } catch (error) {
+ // A more advanced implementation would handle the error case
+ console.warn(`Error setting localStorage key "${key}":`, error)
+ }
+ }, [key, storedValue])
+
+ return [storedValue, setStoredValue] as const
+}
diff --git a/lib/types/models.ts b/lib/types/models.ts
new file mode 100644
index 00000000..64ce4fc4
--- /dev/null
+++ b/lib/types/models.ts
@@ -0,0 +1,27 @@
+export interface Model {
+ id: string
+ name: string
+ provider: string
+ providerId: string
+}
+
+export const models: Model[] = [
+ {
+ id: 'gpt-4o',
+ name: 'GPT-4o',
+ provider: 'OpenAI',
+ providerId: 'openai'
+ },
+ {
+ id: 'gpt-4o-mini',
+ name: 'GPT-4o mini',
+ provider: 'OpenAI',
+ providerId: 'openai'
+ },
+ {
+ id: 'claude-3.5-sonnet',
+ name: 'Claude 3.5 Sonnet',
+ provider: 'Anthropic',
+ providerId: 'anthropic'
+ }
+]
diff --git a/package.json b/package.json
index bf2f9426..5e81284f 100644
--- a/package.json
+++ b/package.json
@@ -21,6 +21,7 @@
"@radix-ui/react-dialog": "^1.0.5",
"@radix-ui/react-dropdown-menu": "^2.0.6",
"@radix-ui/react-label": "^2.0.2",
+ "@radix-ui/react-select": "^2.1.2",
"@radix-ui/react-separator": "^1.0.3",
"@radix-ui/react-slider": "^1.1.2",
"@radix-ui/react-slot": "^1.0.2",
diff --git a/public/providers/logos/anthropic.svg b/public/providers/logos/anthropic.svg
new file mode 100644
index 00000000..da352fb5
--- /dev/null
+++ b/public/providers/logos/anthropic.svg
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<svg width="256px" height="176px" viewBox="0 0 256 176" version="1.1" xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMidYMid">
+ <title>Anthropic</title>
+ <g fill="#181818">
+ <path d="M147.486878,0 C147.486878,0 217.568251,175.780074 217.568251,175.780074 C217.568251,175.780074 256,175.780074 256,175.780074 C256,175.780074 185.918621,0 185.918621,0 C185.918621,0 147.486878,0 147.486878,0 C147.486878,0 147.486878,0 147.486878,0 Z"></path>
+ <path d="M66.1828124,106.221191 C66.1828124,106.221191 90.1624677,44.4471185 90.1624677,44.4471185 C90.1624677,44.4471185 114.142128,106.221191 114.142128,106.221191 C114.142128,106.221191 66.1828124,106.221191 66.1828124,106.221191 C66.1828124,106.221191 66.1828124,106.221191 66.1828124,106.221191 Z M70.0705318,0 C70.0705318,0 0,175.780074 0,175.780074 C0,175.780074 39.179211,175.780074 39.179211,175.780074 C39.179211,175.780074 53.5097704,138.86606 53.5097704,138.86606 C53.5097704,138.86606 126.817544,138.86606 126.817544,138.86606 C126.817544,138.86606 141.145724,175.780074 141.145724,175.780074 C141.145724,175.780074 180.324935,175.780074 180.324935,175.780074 C180.324935,175.780074 110.254409,0 110.254409,0 C110.254409,0 70.0705318,0 70.0705318,0 C70.0705318,0 70.0705318,0 70.0705318,0 Z"></path>
+ </g>
+</svg>
diff --git a/public/providers/logos/openai.svg b/public/providers/logos/openai.svg
new file mode 100644
index 00000000..e04db75a
--- /dev/null
+++ b/public/providers/logos/openai.svg
@@ -0,0 +1 @@
+<svg viewBox="0 0 320 320" xmlns="http://www.w3.org/2000/svg"><path d="m297.06 130.97c7.26-21.79 4.76-45.66-6.85-65.48-17.46-30.4-52.56-46.04-86.84-38.68-15.25-17.18-37.16-26.95-60.13-26.81-35.04-.08-66.13 22.48-76.91 55.82-22.51 4.61-41.94 18.7-53.31 38.67-17.59 30.32-13.58 68.54 9.92 94.54-7.26 21.79-4.76 45.66 6.85 65.48 17.46 30.4 52.56 46.04 86.84 38.68 15.24 17.18 37.16 26.95 60.13 26.8 35.06.09 66.16-22.49 76.94-55.86 22.51-4.61 41.94-18.7 53.31-38.67 17.57-30.32 13.55-68.51-9.94-94.51zm-120.28 168.11c-14.03.02-27.62-4.89-38.39-13.88.49-.26 1.34-.73 1.89-1.07l63.72-36.8c3.26-1.85 5.26-5.32 5.24-9.07v-89.83l26.93 15.55c.29.14.48.42.52.74v74.39c-.04 33.08-26.83 59.9-59.91 59.97zm-128.84-55.03c-7.03-12.14-9.56-26.37-7.15-40.18.47.28 1.3.79 1.89 1.13l63.72 36.8c3.23 1.89 7.23 1.89 10.47 0l77.79-44.92v31.1c.02.32-.13.63-.38.83l-64.41 37.19c-28.69 16.52-65.33 6.7-81.92-21.95zm-16.77-139.09c7-12.16 18.05-21.46 31.21-26.29 0 .55-.03 1.52-.03 2.2v73.61c-.02 3.74 1.98 7.21 5.23 9.06l77.79 44.91-26.93 15.55c-.27.18-.61.21-.91.08l-64.42-37.22c-28.63-16.58-38.45-53.21-21.95-81.89zm221.26 51.49-77.79-44.92 26.93-15.54c.27-.18.61-.21.91-.08l64.42 37.19c28.68 16.57 38.51 53.26 21.94 81.94-7.01 12.14-18.05 21.44-31.2 26.28v-75.81c.03-3.74-1.96-7.2-5.2-9.06zm26.8-40.34c-.47-.29-1.3-.79-1.89-1.13l-63.72-36.8c-3.23-1.89-7.23-1.89-10.47 0l-77.79 44.92v-31.1c-.02-.32.13-.63.38-.83l64.41-37.16c28.69-16.55 65.37-6.7 81.91 22 6.99 12.12 9.52 26.31 7.15 40.1zm-168.51 55.43-26.94-15.55c-.29-.14-.48-.42-.52-.74v-74.39c.02-33.12 26.89-59.96 60.01-59.94 14.01 0 27.57 4.92 38.34 13.88-.49.26-1.33.73-1.89 1.07l-63.72 36.8c-3.26 1.85-5.26 5.31-5.24 9.06l-.04 89.79zm14.63-31.54 34.65-20.01 34.65 20v40.01l-34.65 20-34.65-20z"/></svg>
\ No newline at end of file