Skip to content

An AI-powered search engine with a generative UI

refactor: Update chat route to save complete response and related questions

Yoshiki Miuracde63bb1e674
diff --git a/app/api/chat/route.ts b/app/api/chat/route.ts
index 82ea1e22..6d16e765 100644
--- a/app/api/chat/route.ts
+++ b/app/api/chat/route.ts
@@ -44,8 +44,7 @@ export async function POST(req: Request) {
type: 'related-questions',
data: {
items: []
- },
- status: 'loading'
+ }
}
// Notify related questions loading
@@ -60,8 +59,7 @@ export async function POST(req: Request) {
// Update the annotation with the related questions
annotation = {
...annotation,
- data: relatedQuestions.object,
- status: 'done'
+ data: relatedQuestions.object
}
// Send related questions to client
diff --git a/components/chat.tsx b/components/chat.tsx
index 67a84665..91a52c19 100644
--- a/components/chat.tsx
+++ b/components/chat.tsx
@@ -22,6 +22,7 @@ export function Chat({
append
} = useChat({
initialMessages: savedMessages,
+ id: 'chat',
body: {
id
},
diff --git a/components/related-questions.tsx b/components/related-questions.tsx
index 55f88e08..a74aafb9 100644
--- a/components/related-questions.tsx
+++ b/components/related-questions.tsx
@@ -6,6 +6,7 @@ import { ArrowRight, Repeat2 } from 'lucide-react'
import { Skeleton } from './ui/skeleton'
import { JSONValue } from 'ai'
import { CollapsibleMessage } from './collapsible-message'
+import { useChat } from 'ai/react'
export interface RelatedQuestionsProps {
annotations: JSONValue[]
@@ -19,7 +20,6 @@ interface RelatedQuestionsAnnotation extends Record<string, JSONValue> {
data: {
items: Array<{ query: string }>
}
- status: 'loading' | 'done'
}
export const RelatedQuestions: React.FC<RelatedQuestionsProps> = ({
@@ -28,18 +28,17 @@ export const RelatedQuestions: React.FC<RelatedQuestionsProps> = ({
isOpen,
onOpenChange
}) => {
+ const { isLoading } = useChat({
+ id: 'chat'
+ })
+
if (!annotations) {
return null
}
- const lastRelatedQuestionsAnnotation = annotations.find(
- (a): a is RelatedQuestionsAnnotation =>
- a !== null &&
- typeof a === 'object' &&
- 'type' in a &&
- a.type === 'related-questions' &&
- a.status === 'done'
- )
+ const lastRelatedQuestionsAnnotation = annotations[
+ annotations.length - 1
+ ] as RelatedQuestionsAnnotation
const header = (
<div className="flex items-center gap-1">
@@ -49,7 +48,11 @@ export const RelatedQuestions: React.FC<RelatedQuestionsProps> = ({
)
const relatedQuestions = lastRelatedQuestionsAnnotation?.data
- if (!relatedQuestions) {
+ if (!relatedQuestions && !isLoading) {
+ return null
+ }
+
+ if (!relatedQuestions || isLoading) {
return (
<CollapsibleMessage
role="assistant"
diff --git a/components/search-section.tsx b/components/search-section.tsx
index 4cadf1a3..2285baa7 100644
--- a/components/search-section.tsx
+++ b/components/search-section.tsx
@@ -7,6 +7,7 @@ import { Section, ToolArgsSection } from './section'
import type { SearchResults as TypeSearchResults } from '@/lib/types'
import { ToolInvocation } from 'ai'
import { CollapsibleMessage } from './collapsible-message'
+import { useChat } from 'ai/react'
interface SearchSectionProps {
tool: ToolInvocation
@@ -19,7 +20,10 @@ export function SearchSection({
isOpen,
onOpenChange
}: SearchSectionProps) {
- const isLoading = tool.state === 'call'
+ const { isLoading } = useChat({
+ id: 'chat'
+ })
+ const isToolLoading = tool.state === 'call'
const searchResults: TypeSearchResults =
tool.state === 'result' ? tool.result : undefined
const query = tool.args.query as string | undefined
@@ -50,13 +54,13 @@ export function SearchSection({
/>
</Section>
)}
- {!isLoading && searchResults?.results ? (
+ {isLoading && isToolLoading ? (
+ <DefaultSkeleton />
+ ) : searchResults?.results ? (
<Section title="Sources">
<SearchResults results={searchResults.results} />
</Section>
- ) : (
- <DefaultSkeleton />
- )}
+ ) : null}
</CollapsibleMessage>
)
}