Skip to content

An AI-powered search engine with a generative UI

refactor: Update Copilot and FollowupPanel components to include selected model information in form data

Yoshiki Miurae5e02cc4b883
diff --git a/components/copilot.tsx b/components/copilot.tsx
index 276ee59e..8911321b 100644
--- a/components/copilot.tsx
+++ b/components/copilot.tsx
@@ -15,6 +15,8 @@ import {
} from 'ai/rsc'
import type { AI } from '@/app/actions'
import { useAppState } from '@/lib/utils/app-state'
+import { useLocalStorage } from '@/lib/hooks/use-local-storage'
+import { Model, models } from '@/lib/types/models'
export type CopilotProps = {
inquiry?: StreamableValue<PartialInquiry>
@@ -33,6 +35,7 @@ export const Copilot: React.FC<CopilotProps> = ({ inquiry }: CopilotProps) => {
const { submit } = useActions()
const { isGenerating, setIsGenerating } = useAppState()
const [object, setObject] = useState<PartialInquiry>()
+ const [selectedModel] = useLocalStorage<Model>('selectedModel', models[0])
const handleInputChange = (event: React.ChangeEvent<HTMLInputElement>) => {
setQuery(event.target.value)
@@ -84,9 +87,23 @@ export const Copilot: React.FC<CopilotProps> = ({ inquiry }: CopilotProps) => {
setCompleted(true)
setSkipped(skip || false)
- const formData = skip
- ? undefined
- : new FormData(e.target as HTMLFormElement)
+ // Always create FormData
+ const formData = new FormData()
+
+ // Add model information
+ formData.set('model', `${selectedModel.providerId}:${selectedModel.id}`)
+
+ // If not skipping, add form data from the event
+ if (!skip) {
+ const form = e.target as HTMLFormElement
+ const formEntries = Array.from(new FormData(form).entries())
+ formEntries.forEach(([key, value]) => {
+ if (key !== 'model') {
+ // Don't override model
+ formData.append(key, value)
+ }
+ })
+ }
const response = await submit(formData, skip)
setMessages(currentMessages => [...currentMessages, response])
diff --git a/components/followup-panel.tsx b/components/followup-panel.tsx
index 77a74702..81510173 100644
--- a/components/followup-panel.tsx
+++ b/components/followup-panel.tsx
@@ -8,12 +8,15 @@ import type { AI } from '@/app/actions'
import { UserMessage } from './user-message'
import { ArrowRight } from 'lucide-react'
import { useAppState } from '@/lib/utils/app-state'
+import { useLocalStorage } from '@/lib/hooks/use-local-storage'
+import { Model, models } from '@/lib/types/models'
export function FollowupPanel() {
const [input, setInput] = useState('')
const { submit } = useActions()
const [, setMessages] = useUIState<typeof AI>()
const { isGenerating, setIsGenerating } = useAppState()
+ const [selectedModel] = useLocalStorage<Model>('selectedModel', models[0])
const handleSubmit = async (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault()
@@ -24,6 +27,8 @@ export function FollowupPanel() {
setInput('')
const formData = new FormData(event.currentTarget as HTMLFormElement)
+ // Add model information to formData
+ formData.set('model', `${selectedModel.providerId}:${selectedModel.id}`)
const userMessage = {
id: Date.now(),
diff --git a/components/search-related.tsx b/components/search-related.tsx
index 840f1bdc..2e4cd3da 100644
--- a/components/search-related.tsx
+++ b/components/search-related.tsx
@@ -14,6 +14,8 @@ import { UserMessage } from './user-message'
import { PartialRelated } from '@/lib/schema/related'
import { Section } from './section'
import { Skeleton } from './ui/skeleton'
+import { useLocalStorage } from '@/lib/hooks/use-local-storage'
+import { Model, models } from '@/lib/types/models'
export interface SearchRelatedProps {
relatedQueries: StreamableValue<PartialRelated>
@@ -26,6 +28,7 @@ export const SearchRelated: React.FC<SearchRelatedProps> = ({
const [, setMessages] = useUIState<typeof AI>()
const [data, error, pending] = useStreamableValue(relatedQueries)
const [related, setRelated] = useState<PartialRelated>()
+ const [selectedModel] = useLocalStorage<Model>('selectedModel', models[0])
useEffect(() => {
if (!data) return
@@ -36,7 +39,7 @@ export const SearchRelated: React.FC<SearchRelatedProps> = ({
event.preventDefault()
const formData = new FormData(event.currentTarget as HTMLFormElement)
- // // Get the submitter of the form
+ // Get the submitter of the form
const submitter = (event.nativeEvent as SubmitEvent)
.submitter as HTMLInputElement
let query = ''
@@ -45,6 +48,9 @@ export const SearchRelated: React.FC<SearchRelatedProps> = ({
query = submitter.value
}
+ // Add model information to formData
+ formData.set('model', `${selectedModel.providerId}:${selectedModel.id}`)
+
const userMessage = {
id: Date.now(),
component: <UserMessage message={query} />