An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
Update AI state with answer and type
Yoshiki Miura4748661858f6
diff --git a/app/actions.tsx b/app/actions.tsxindex 2046145a..e940c8cc 100644--- a/app/actions.tsx+++ b/app/actions.tsx@@ -36,7 +36,7 @@ async function submit(formData?: FormData, skip?: boolean) {message.role !== 'tool' &&message.type !== 'followup' &&message.type !== 'related' &&- message.content !== 'end'+ message.type !== 'end')// goupeiId is used to group the messages for collapse@@ -175,6 +175,19 @@ async function submit(formData?: FormData, skip?: boolean) {}if (!errorOccurred) {+ aiState.update({+ ...aiState.get(),+ messages: [+ ...aiState.get().messages,+ {+ id: groupeId,+ role: 'assistant',+ content: answer,+ type: 'answer'+ }+ ]+ })+// Generate related queriesconst relatedQueries = await querySuggestor(uiStream, messages)// Add follow-up panel@@ -184,20 +197,10 @@ async function submit(formData?: FormData, skip?: boolean) {</Section>)- // Add the answer, related queries, and follow-up panel to the state- // Wait for 1 second before adding the answer to the state- await new Promise(resolve => setTimeout(resolve, 500))-- aiState.done({+ aiState.update({...aiState.get(),messages: [...aiState.get().messages,- {- id: groupeId,- role: 'assistant',- content: answer,- type: 'answer'- },{id: groupeId,role: 'assistant',@@ -216,6 +219,7 @@ async function submit(formData?: FormData, skip?: boolean) {isGenerating.done(false)uiStream.done()+ aiState.done(aiState.get())}processEvents()@@ -283,7 +287,8 @@ export const AI = createAI<AIState, UIState>({{id: nanoid(),role: 'assistant',- content: `end`+ content: `end`,+ type: 'end'}]@@ -304,7 +309,7 @@ export const getUIStateFromAIState = (aiState: Chat) => {.map(message => {const { role, content, id, type, name } = message- if (!type) return null+ if (!type || type === 'end') return nullswitch (role) {case 'user':diff --git a/lib/actions/chat.ts b/lib/actions/chat.tsindex 911d0fe8..eef7eb13 100644--- a/lib/actions/chat.ts+++ b/lib/actions/chat.ts@@ -41,33 +41,6 @@ export async function getChat(id: string, userId: string = 'anonymous') {return chat}-export async function getSharedChat(id: string) {- const chat = await redis.hgetall<Chat>(`chat:${id}`)-- if (!chat || !chat.sharedPath) {- return null- }-- return chat-}--export async function shareChat(id: string) {- const chat = await redis.hgetall<Chat>(`chat:${id}`)-- if (!chat) {- return null- }-- const payload = {- ...chat,- sharedPath: `/share/${chat.id}`- }-- await redis.hmset(`chat:${id}`, payload)-- return payload-}-export async function saveChat(chat: Chat, userId: string = 'anonymous') {const pipeline = redis.pipeline()pipeline.hmset(`chat:${chat.id}`, chat)diff --git a/lib/types/index.ts b/lib/types/index.tsindex 850d3c31..fcb89730 100644--- a/lib/types/index.ts+++ b/lib/types/index.ts@@ -34,4 +34,5 @@ export type AIMessage = {| 'input_related'| 'tool'| 'followup'+ | 'end'}