An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
Refactor message filtering and add 'end' message for history reload
Yoshiki Miurac286c263db46
diff --git a/app/actions.tsx b/app/actions.tsxindex 5ca93fb0..c7516ea8 100644--- a/app/actions.tsx+++ b/app/actions.tsx@@ -31,7 +31,14 @@ async function submit(formData?: FormData, skip?: boolean) {// Get the messages from the state, filter out the tool messagesconst messages: ExperimentalMessage[] = [...(aiState.get().messages as any[])- ].filter(message => message.role !== 'tool')+ ].filter(+ message =>+ message.role !== 'tool' &&+ message.type !== undefined &&+ message.type !== 'followup' &&+ message.type !== 'related' &&+ message.type !== 'inquiry'+ )// goupeiId is used to group the messages for collapseconst groupeId = nanoid()@@ -269,13 +276,23 @@ export const AI = createAI<AIState, UIState>({const title =JSON.parse(messages[0].content)?.input?.substring(0, 100) || 'Untitled'+ // Add an 'end' message at the end to determine if the history needs to be reloaded+ const updatedMessages: AIMessage[] = [+ ...messages,+ {+ id: nanoid(),+ role: 'assistant',+ content: `end`+ }+ ]+const chat: Chat = {id: chatId,createdAt,userId,path,title,- messages+ messages: updatedMessages}await saveChat(chat)}@@ -288,8 +305,6 @@ export const getUIStateFromAIState = (aiState: Chat) => {if (!type) return null- console.log(role, type)-switch (role) {case 'user':switch (type) {@@ -297,7 +312,6 @@ export const getUIStateFromAIState = (aiState: Chat) => {case 'input_related':const json = JSON.parse(content)const value = type === 'input' ? json.input : json.related_query- console.log(json, value)return {id,component: (diff --git a/components/chat.tsx b/components/chat.tsxindex 22be44de..4b4fc9d7 100644--- a/components/chat.tsx+++ b/components/chat.tsx@@ -23,6 +23,7 @@ export function Chat({ id }: ChatProps) {}, [id, path, messages])useEffect(() => {+ console.log('aiState', aiState.messages[aiState.messages.length - 1]?.type)if (aiState.messages[aiState.messages.length - 1]?.type === 'followup') {// Refresh the page after 1 second to chat history updatessetTimeout(() => {diff --git a/components/sidebar.tsx b/components/sidebar.tsxindex 19eb58d9..7b04764a 100644--- a/components/sidebar.tsx+++ b/components/sidebar.tsx@@ -8,7 +8,7 @@ const loadChats = cache(async (userId?: string) => {export async function Sidebar() {const chats = await loadChats('anonymous')- console.log('chats loaded')+ console.log('history loaded')return (<div className="h-screen p-2 fixed top-0 right-0 flex-col justify-center pb-24 hidden sm:flex"><History location="sidebar" chats={chats} />diff --git a/lib/agents/query-suggestor.tsx b/lib/agents/query-suggestor.tsxindex 05ecd1e1..c7e7b4f4 100644--- a/lib/agents/query-suggestor.tsx+++ b/lib/agents/query-suggestor.tsx@@ -50,7 +50,6 @@ export async function querySuggestor(}}).finally(() => {- console.log('done', finalRelatedQueries)objectStream.done()})