An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
chore: Process Event as a separate workflow, and add an ollama workflow
Yoshiki Miura1a3bbb630b76
diff --git a/app/actions.tsx b/app/actions.tsxindex d1cccddd..a003a72b 100644--- a/app/actions.tsx+++ b/app/actions.tsx@@ -6,12 +6,9 @@ import {getAIState,getMutableAIState} from 'ai/rsc'-import { CoreMessage, generateId, ToolResultPart } from 'ai'-import { Spinner } from '@/components/ui/spinner'+import { CoreMessage, generateId } from 'ai'import { Section } from '@/components/section'import { FollowupPanel } from '@/components/followup-panel'-import { inquire, researcher, taskManager, querySuggestor } from '@/lib/agents'-import { writer } from '@/lib/agents/writer'import { saveChat } from '@/lib/actions/chat'import { Chat } from '@/lib/types'import { AIMessage } from '@/lib/types'@@ -21,9 +18,8 @@ import SearchRelated from '@/components/search-related'import { CopilotDisplay } from '@/components/copilot-display'import RetrieveSection from '@/components/retrieve-section'import { VideoSearchSection } from '@/components/video-search-section'-import { transformToolMessages } from '@/lib/utils'import { AnswerSection } from '@/components/answer-section'-import { ErrorCard } from '@/components/error-card'+import { workflow } from '@/lib/actions/workflow'async function submit(formData?: FormData,@@ -52,9 +48,6 @@ async function submit(return { role, content } as CoreMessage})- // groupId is used to group the messages for collapse- const groupId = generateId()-const useSpecificAPI = process.env.USE_SPECIFIC_API_FOR_WRITER === 'true'const useOllamaProvider = !!(process.env.OLLAMA_MODEL && process.env.OLLAMA_BASE_URL@@ -100,174 +93,13 @@ async function submit(})}- async function processEvents() {- // Show the spinner- uiStream.append(<Spinner />)-- let action = { object: { next: 'proceed' } }- // If the user skips the task, we proceed to the search- if (!skip) action = (await taskManager(messages)) ?? action-- if (action.object.next === 'inquire') {- // Generate inquiry- const inquiry = await inquire(uiStream, messages)- uiStream.done()- isGenerating.done()- isCollapsed.done(false)- aiState.done({- ...aiState.get(),- messages: [- ...aiState.get().messages,- {- id: generateId(),- role: 'assistant',- content: `inquiry: ${inquiry?.question}`,- type: 'inquiry'- }- ]- })- return- }-- // Set the collapsed state to true- isCollapsed.done(true)-- // Generate the answer- let answer = ''- let stopReason = ''- let toolOutputs: ToolResultPart[] = []- let errorOccurred = false-- const streamText = createStreamableValue<string>()-- // If ANTHROPIC_API_KEY is set, update the UI with the answer- // If not, update the UI with a div- if (process.env.ANTHROPIC_API_KEY) {- uiStream.update(- <AnswerSection result={streamText.value} hasHeader={false} />- )- } else {- uiStream.update(<div />)- }-- // If useSpecificAPI is enabled, only function calls will be made- // If not using a tool, this model generates the answer- while (- useSpecificAPI- ? toolOutputs.length === 0 && answer.length === 0 && !errorOccurred- : (stopReason !== 'stop' || answer.length === 0) && !errorOccurred- ) {- // Search the web and generate the answer- const { fullResponse, hasError, toolResponses, finishReason } =- await researcher(uiStream, streamText, messages)- stopReason = finishReason || ''- answer = fullResponse- toolOutputs = toolResponses- errorOccurred = hasError-- if (toolOutputs.length > 0) {- toolOutputs.map(output => {- aiState.update({- ...aiState.get(),- messages: [- ...aiState.get().messages,- {- id: groupId,- role: 'tool',- content: JSON.stringify(output.result),- name: output.toolName,- type: 'tool'- }- ]- })- })- }- }-- // If useSpecificAPI is enabled, generate the answer using the specific model- if (useSpecificAPI && answer.length === 0 && !errorOccurred) {- // modify the messages to be used by the specific model- const modifiedMessages = transformToolMessages(messages)- const latestMessages = modifiedMessages.slice(maxMessages * -1)- const { response, hasError } = await writer(uiStream, latestMessages)- answer = response- errorOccurred = hasError- messages.push({- role: 'assistant',- content: answer- })- }-- if (!errorOccurred) {- const useGoogleProvider = process.env.GOOGLE_GENERATIVE_AI_API_KEY- const useOllamaProvider = !!(- process.env.OLLAMA_MODEL && process.env.OLLAMA_BASE_URL- )- let processedMessages = messages- // If using Google provider, we need to modify the messages- if (useGoogleProvider) {- processedMessages = transformToolMessages(messages)- }- if (useOllamaProvider) {- processedMessages = [{ role: 'assistant', content: answer }]- }-- streamText.done()- aiState.update({- ...aiState.get(),- messages: [- ...aiState.get().messages,- {- id: groupId,- role: 'assistant',- content: answer,- type: 'answer'- }- ]- })-- // Generate related queries- const relatedQueries = await querySuggestor(uiStream, processedMessages)- // Add follow-up panel- uiStream.append(- <Section title="Follow-up">- <FollowupPanel />- </Section>- )-- aiState.done({- ...aiState.get(),- messages: [- ...aiState.get().messages,- {- id: groupId,- role: 'assistant',- content: JSON.stringify(relatedQueries),- type: 'related'- },- {- id: groupId,- role: 'assistant',- content: 'followup',- type: 'followup'- }- ]- })- } else {- aiState.done(aiState.get())- streamText.done()- uiStream.append(- <ErrorCard- errorMessage={answer || 'An error occurred. Please try again.'}- />- )- }-- isGenerating.done(false)- uiStream.done()- }-- processEvents()+ // Run the agent workflow+ workflow(+ { uiStream, isCollapsed, isGenerating },+ aiState,+ messages,+ skip ?? false+ )return {id: generateId(),@@ -359,10 +191,10 @@ export const getUIStateFromAIState = (aiState: Chat) => {const chatId = aiState.chatIdconst isSharePage = aiState.isSharePage- // Ensure messages is an array of plain objects- const messages = Array.isArray(aiState.messages)- ? aiState.messages.map(msg => ({...msg}))- : [];+ // Ensure messages is an array of plain objects+ const messages = Array.isArray(aiState.messages)+ ? aiState.messages.map(msg => ({ ...msg }))+ : []return messages.map((message, index) => {diff --git a/lib/actions/workflow.tsx b/lib/actions/workflow.tsxnew file mode 100644index 00000000..d75bd261--- /dev/null+++ b/lib/actions/workflow.tsx@@ -0,0 +1,314 @@+'use server'++import React from 'react'+import { Spinner } from '@/components/ui/spinner'+import { Section } from '@/components/section'+import { FollowupPanel } from '@/components/followup-panel'+import { AnswerSection } from '@/components/answer-section'+import { ErrorCard } from '@/components/error-card'+import { transformToolMessages } from '@/lib/utils'+import {+ querySuggestor,+ inquire,+ researcher,+ taskManager,+ ollamaResearcher+} from '@/lib/agents'+import { createStreamableValue, createStreamableUI } from 'ai/rsc'+import { CoreMessage, generateId, ToolResultPart } from 'ai'+import { writer } from '../agents/writer'++export async function defaultWorkflow(+ uiState: {+ uiStream: ReturnType<typeof createStreamableUI>+ isCollapsed: ReturnType<typeof createStreamableValue>+ isGenerating: ReturnType<typeof createStreamableValue>+ },+ aiState: any,+ messages: CoreMessage[],+ skip: boolean+) {+ const { uiStream, isCollapsed, isGenerating } = uiState+ // Show the spinner+ uiStream.append(<Spinner />)++ let action = { object: { next: 'proceed' } }+ // If the user skips the task, we proceed to the search+ if (!skip) action = (await taskManager(messages)) ?? action++ if (action.object.next === 'inquire') {+ // Generate inquiry+ const inquiry = await inquire(uiStream, messages)+ uiStream.done()+ aiState.done({+ ...aiState.get(),+ messages: [+ ...aiState.get().messages,+ {+ id: generateId(),+ role: 'assistant',+ content: `inquiry: ${inquiry?.question}`,+ type: 'inquiry'+ }+ ]+ })++ isCollapsed.done(false)+ isGenerating.done(false)+ uiStream.done()+ return+ }++ // Set the collapsed state to true+ isCollapsed.done(true)++ // Generate the answer+ let answer = ''+ let stopReason = ''+ let toolOutputs: ToolResultPart[] = []+ let errorOccurred = false++ const streamText = createStreamableValue<string>()++ // If ANTHROPIC_API_KEY is set, update the UI with the answer+ // If not, update the UI with a div+ if (process.env.ANTHROPIC_API_KEY) {+ uiStream.update(+ <AnswerSection result={streamText.value} hasHeader={false} />+ )+ } else {+ uiStream.update(<div />)+ }++ // Determine the API usage based on environment variables+ const useSpecificAPI = process.env.USE_SPECIFIC_API_FOR_WRITER === 'true'+ const useOllamaProvider = !!(+ process.env.OLLAMA_MODEL && process.env.OLLAMA_BASE_URL+ )+ const maxMessages = useSpecificAPI ? 5 : useOllamaProvider ? 1 : 10+ // Limit the number of messages to the maximum+ messages.splice(0, Math.max(messages.length - maxMessages, 0))++ // If useSpecificAPI is enabled, only function calls will be made+ // If not using a tool, this model generates the answer+ while (+ useSpecificAPI+ ? toolOutputs.length === 0 && answer.length === 0 && !errorOccurred+ : (stopReason !== 'stop' || answer.length === 0) && !errorOccurred+ ) {+ // Search the web and generate the answer+ const { fullResponse, hasError, toolResponses, finishReason } =+ await researcher(uiStream, streamText, messages)+ stopReason = finishReason || ''+ answer = fullResponse+ toolOutputs = toolResponses+ errorOccurred = hasError++ if (toolOutputs.length > 0) {+ toolOutputs.map(output => {+ aiState.update({+ ...aiState.get(),+ messages: [+ ...aiState.get().messages,+ {+ id: generateId(),+ role: 'tool',+ content: JSON.stringify(output.result),+ name: output.toolName,+ type: 'tool'+ }+ ]+ })+ })+ }+ }++ // If useSpecificAPI is enabled, generate the answer using the specific model+ if (useSpecificAPI && answer.length === 0 && !errorOccurred) {+ // Modify the messages to be used by the specific model+ const modifiedMessages = transformToolMessages(messages)+ const latestMessages = modifiedMessages.slice(maxMessages * -1)+ const { response, hasError } = await writer(uiStream, latestMessages)+ answer = response+ errorOccurred = hasError+ messages.push({+ role: 'assistant',+ content: answer+ })+ }++ if (!errorOccurred) {+ const useGoogleProvider = process.env.GOOGLE_GENERATIVE_AI_API_KEY+ const useOllamaProvider = !!(+ process.env.OLLAMA_MODEL && process.env.OLLAMA_BASE_URL+ )+ let processedMessages = messages+ // If using Google provider, we need to modify the messages+ if (useGoogleProvider) {+ processedMessages = transformToolMessages(messages)+ }+ if (useOllamaProvider) {+ processedMessages = [{ role: 'assistant', content: answer }]+ }++ streamText.done()+ aiState.update({+ ...aiState.get(),+ messages: [+ ...aiState.get().messages,+ {+ id: generateId(),+ role: 'assistant',+ content: answer,+ type: 'answer'+ }+ ]+ })++ // Generate related queries+ const relatedQueries = await querySuggestor(uiStream, processedMessages)+ // Add follow-up panel+ uiStream.append(+ <Section title="Follow-up">+ <FollowupPanel />+ </Section>+ )++ aiState.done({+ ...aiState.get(),+ messages: [+ ...aiState.get().messages,+ {+ id: generateId(),+ role: 'assistant',+ content: JSON.stringify(relatedQueries),+ type: 'related'+ },+ {+ id: generateId(),+ role: 'assistant',+ content: 'followup',+ type: 'followup'+ }+ ]+ })+ } else {+ aiState.done(aiState.get())+ streamText.done()+ uiStream.append(+ <ErrorCard+ errorMessage={answer || 'An error occurred. Please try again.'}+ />+ )+ }++ isGenerating.done(false)+ uiStream.done()+}++export const ollamaWorkflow = async (+ uiState: {+ uiStream: ReturnType<typeof createStreamableUI>+ isCollapsed: ReturnType<typeof createStreamableValue>+ isGenerating: ReturnType<typeof createStreamableValue>+ },+ aiState: any,+ messages: CoreMessage[],+ skip: boolean+) => {+ const { uiStream, isCollapsed, isGenerating } = uiState+ let action = { object: { next: 'proceed' } }+ // If the user skips the task, we proceed to the search+ if (!skip) action = (await taskManager(messages)) ?? action++ if (action.object.next === 'inquire') {+ // Generate inquiry+ const inquiry = await inquire(uiStream, messages)+ isCollapsed.done(false)+ isGenerating.done(false)+ uiStream.done()+ aiState.done({+ ...aiState.get(),+ messages: [+ ...aiState.get().messages,+ {+ id: generateId(),+ role: 'assistant',+ content: `inquiry: ${inquiry?.question}`,+ type: 'inquiry'+ }+ ]+ })+ return+ }++ // Set the collapsed state to true+ isCollapsed.done(true)++ const { text, toolResults } = await ollamaResearcher(uiStream, messages)++ aiState.update({+ ...aiState.get(),+ messages: [+ ...aiState.get().messages,+ ...toolResults.map((toolResult: any) => ({+ id: generateId(),+ role: 'tool',+ content: JSON.stringify(toolResult.result),+ name: toolResult.toolName,+ type: 'tool'+ })),+ {+ id: generateId(),+ role: 'assistant',+ content: text,+ type: 'answer'+ }+ ]+ })++ // Generate related queries+ const relatedQueries = await querySuggestor(uiStream, messages)+ // Add follow-up panel+ uiStream.append(+ <Section title="Follow-up">+ <FollowupPanel />+ </Section>+ )++ aiState.done({+ ...aiState.get(),+ messages: [+ ...aiState.get().messages,+ {+ id: generateId(),+ role: 'assistant',+ content: JSON.stringify(relatedQueries),+ type: 'related'+ },+ {+ id: generateId(),+ role: 'assistant',+ content: 'followup',+ type: 'followup'+ }+ ]+ })+}++export async function workflow(+ uiState: {+ uiStream: ReturnType<typeof createStreamableUI>+ isCollapsed: ReturnType<typeof createStreamableValue>+ isGenerating: ReturnType<typeof createStreamableValue>+ },+ aiState: any,+ messages: CoreMessage[],+ skip: boolean+) {+ if (process.env.OLLAMA_MODEL && process.env.OLLAMA_BASE_URL) {+ return ollamaWorkflow(uiState, aiState, messages, skip)+ }++ return defaultWorkflow(uiState, aiState, messages, skip)+}diff --git a/lib/agents/query-suggestor.tsx b/lib/agents/query-suggestor.tsxindex f396e854..6bdff7dc 100644--- a/lib/agents/query-suggestor.tsx+++ b/lib/agents/query-suggestor.tsx@@ -25,14 +25,6 @@ export async function querySuggestor(For instance, if the original query was "Starship's third test flight key milestones", your output should follow this format:- "{- "related": [- "What were the primary objectives achieved during Starship's third test flight?",- "What factors contributed to the ultimate outcome of Starship's third test flight?",- "How will the results of the third test flight influence SpaceX's future development plans for Starship?"- ]- }"-Aim to create queries that progressively delve into more specific aspects, implications, or adjacent topics related to the initial query. The goal is to anticipate the user's potential information needs and guide them towards a more comprehensive understanding of the subject matter.Please match the language of the response to the user's language.`,messages: lastMessages,diff --git a/lib/agents/researcher.tsx b/lib/agents/researcher.tsxindex f4698357..1cd63e59 100644--- a/lib/agents/researcher.tsx+++ b/lib/agents/researcher.tsx@@ -1,8 +1,15 @@import { createStreamableUI, createStreamableValue } from 'ai/rsc'-import { CoreMessage, ToolCallPart, ToolResultPart, streamText } from 'ai'+import {+ CoreMessage,+ ToolCallPart,+ ToolResultPart,+ generateText,+ streamText+} from 'ai'import { getTools } from './tools'import { getModel, transformToolMessages } from '../utils'import { AnswerSection } from '@/components/answer-section'+import { Spinner } from '@/components/ui/spinner'export async function researcher(uiStream: ReturnType<typeof createStreamableUI>,@@ -112,3 +119,42 @@ export async function researcher(return { result, fullResponse, hasError, toolResponses, finishReason }}++export async function ollamaResearcher(+ uiStream: ReturnType<typeof createStreamableUI>,+ messages: CoreMessage[]+) {+ const fullResponse = ''+ const streamableText = createStreamableValue<string>()+ let toolResults: any[] = []++ const result = await generateText({+ model: getModel(),+ system: `As a professional search expert, you possess the ability to search for any information on the web.+ or any information on the web.+ For each user query, utilize the search results to their fullest potential to provide additional information and assistance in your response.+ If there are any images relevant to your answer, be sure to include them as well.+ Aim to directly address the user's question, augmenting your response with insights gleaned from the search results.+ `,+ messages: messages,+ tools: getTools({+ uiStream,+ fullResponse+ }),+ maxSteps: 5,+ onStepFinish: async event => {+ if (event.stepType === 'initial') {+ if (event.toolCalls) {+ uiStream.append(<AnswerSection result={streamableText.value} />)+ toolResults = event.toolResults+ } else {+ uiStream.update(<AnswerSection result={streamableText.value} />)+ }+ }+ }+ })++ streamableText.done(result.text)++ return { text: result.text, toolResults }+}diff --git a/lib/agents/tools/search.tsx b/lib/agents/tools/search.tsxindex 4c53538b..be98aece 100644--- a/lib/agents/tools/search.tsx+++ b/lib/agents/tools/search.tsx@@ -41,7 +41,6 @@ export const searchTool = ({ uiStream, fullResponse }: ToolProps) =>const searchAPI =(process.env.SEARCH_API as 'tavily' | 'exa' | 'searxng') || 'tavily'-const effectiveSearchDepth =searchAPI === 'searxng' &&process.env.SEARXNG_DEFAULT_DEPTH === 'advanced'@@ -55,7 +54,8 @@ export const searchTool = ({ uiStream, fullResponse }: ToolProps) =>try {if (searchAPI === 'searxng' && effectiveSearchDepth === 'advanced') {// API route for advanced SearXNG search- const baseUrl = process.env.NEXT_PUBLIC_BASE_URL || 'http://localhost:3000';+ const baseUrl =+ process.env.NEXT_PUBLIC_BASE_URL || 'http://localhost:3000'const response = await fetch(`${baseUrl}/api/advanced-search`, {method: 'POST',headers: { 'Content-Type': 'application/json' },@@ -278,4 +278,4 @@ async function searxngSearch(console.error('SearXNG API error:', error)throw error}-}\ No newline at end of file+}diff --git a/lib/schema/search.tsx b/lib/schema/search.tsxindex f0cb6e7d..822982be 100644--- a/lib/schema/search.tsx+++ b/lib/schema/search.tsx@@ -3,7 +3,9 @@ import { z } from 'zod'export const searchSchema = z.object({query: z.string().describe('The query to search for'),- max_results: z.number().describe('The maximum number of results to return'),+ max_results: z.coerce+ .number()+ .describe('The maximum number of results to return'),search_depth: z.enum(['basic', 'advanced']).describe('The depth of the search'),