An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
refactor: separate tool execution logic from stream handling
Yoshiki Miura405f413ea018
diff --git a/app/api/chat/route.ts b/app/api/chat/route.tsindex 7a208bb4..a0549f39 100644--- a/app/api/chat/route.ts+++ b/app/api/chat/route.ts@@ -1,6 +1,6 @@import { createManualToolStreamResponse } from '@/lib/streaming/create-manual-tool-stream'import { createToolCallingStreamResponse } from '@/lib/streaming/create-tool-calling-stream'-import { isProviderEnabled } from '@/lib/utils/registry'+import { isProviderEnabled, isToolCallSupported } from '@/lib/utils/registry'import { cookies } from 'next/headers'export const maxDuration = 30@@ -32,8 +32,7 @@ export async function POST(req: Request) {})}- const supportsToolCalling = provider === 'openai'- // const supportsToolCalling = true+ const supportsToolCalling = isToolCallSupported(model)return supportsToolCalling? createToolCallingStreamResponse({diff --git a/lib/agents/researcher.ts b/lib/agents/researcher.tsindex b1ff21c4..4c4ffbb5 100644--- a/lib/agents/researcher.ts+++ b/lib/agents/researcher.ts@@ -46,7 +46,7 @@ export function researcher({videoSearch: videoSearchTool},maxSteps: 5,- experimental_transform: smoothStream()+ experimental_transform: smoothStream({ chunking: 'word' })}} catch (error) {console.error('Error in chatResearcher:', error)diff --git a/lib/streaming/create-manual-tool-stream.ts b/lib/streaming/create-manual-tool-stream.tsindex ae2ebeb3..782152cd 100644--- a/lib/streaming/create-manual-tool-stream.ts+++ b/lib/streaming/create-manual-tool-stream.ts@@ -1,22 +1,15 @@-import { openai } from '@ai-sdk/openai'import {convertToCoreMessages,- CoreMessage,createDataStreamResponse,DataStreamWriter,- generateId,- generateText,JSONValue,smoothStream,streamText} from 'ai'-import { z } from 'zod'-import { searchSchema } from '../schema/search'-import { search } from '../tools/search'import { ExtendedCoreMessage } from '../types'import { getModel } from '../utils/registry'import { handleStreamFinish } from './handle-stream-finish'-import { parseToolCallXml } from './parse-tool-call'+import { executeToolCall } from './tool-execution'import { BaseStreamConfig } from './types'export function createManualToolStreamResponse(config: BaseStreamConfig) {@@ -25,112 +18,10 @@ export function createManualToolStreamResponse(config: BaseStreamConfig) {try {const coreMessages = convertToCoreMessages(config.messages)const model = getModel(config.model)- const toolCallModel = openai('gpt-4o-mini')- // Convert Zod schema to string representation- const searchSchemaString = Object.entries(searchSchema.shape)- .map(([key, value]) => {- const description = value.description- const isOptional = value instanceof z.ZodOptional- return `- ${key}${isOptional ? ' (optional)' : ''}: ${description}`- })- .join('\n')+ const { toolCallDataAnnotation, toolCallMessages } =+ await executeToolCall(coreMessages, dataStream)- // Generate tool selection using XML format- const toolSelectionResponse = await generateText({- model: toolCallModel,- system: `You are a helpful assistant that selects appropriate tools and their parameters.- Do not include any other text in your response.- Respond in XML format with the following structure:- <tool_call>- <tool>tool_name</tool>- <parameters>- <query>search query text</query>- <max_results>number - 5 by default</max_results>- <search_depth>basic or advanced</search_depth>- <include_domains>domain1,domain2</include_domains>- <exclude_domains>domain1,domain2</exclude_domains>- </parameters>- </tool_call>-- Available tools: search-- Search parameters:- ${searchSchemaString}-- If you don't need a tool, respond with <tool_call><tool></tool></tool_call>`,- messages: coreMessages- })-- // Add debug log- console.log('Raw XML response:', toolSelectionResponse.text)-- // Parse the tool selection XML using the search schema- const toolCall = parseToolCallXml(- toolSelectionResponse.text,- searchSchema- )- console.log('Parsed tool call:', toolCall)-- let toolCallMessages: CoreMessage[] = []- let toolCallDataAnnotation: ExtendedCoreMessage | null = null-- if (toolCall && toolCall.tool !== '') {- const toolCallAnnotation = {- type: 'tool_call',- data: {- state: 'call',- toolCallId: `call_${generateId()}`,- toolName: toolCall.tool,- args: JSON.stringify(toolCall.parameters)- }- }- dataStream.writeData(toolCallAnnotation)-- let toolCallResult: JSONValue = {}- if (toolCall.tool === 'search') {- toolCallResult = await search(- toolCall.parameters?.query ?? '',- toolCall.parameters?.max_results,- toolCall.parameters?.search_depth as 'basic' | 'advanced',- toolCall.parameters?.include_domains,- toolCall.parameters?.exclude_domains- )- } else {- throw new Error('Invalid tool name')- }-- const updatedToolCallAnnotation = {- ...toolCallAnnotation,- data: {- ...toolCallAnnotation.data,- result: JSON.stringify(toolCallResult),- state: 'result'- }- }- dataStream.writeMessageAnnotation(updatedToolCallAnnotation)-- toolCallDataAnnotation = {- role: 'data',- content: {- type: 'tool_call',- data: updatedToolCallAnnotation.data- }- }-- toolCallMessages = [- {- role: 'assistant' as const,- content: `Tool call result: ${JSON.stringify(toolCallResult)}`- },- {- role: 'user' as const,- content: 'Now answer the user question.'- }- ]- }-- // Proceed with the main streamconst result = streamText({model,system:diff --git a/lib/streaming/tool-execution.ts b/lib/streaming/tool-execution.tsnew file mode 100644index 00000000..a11cf229--- /dev/null+++ b/lib/streaming/tool-execution.ts@@ -0,0 +1,121 @@+import {+ CoreMessage,+ DataStreamWriter,+ generateId,+ generateText,+ JSONValue+} from 'ai'+import { z } from 'zod'+import { searchSchema } from '../schema/search'+import { search } from '../tools/search'+import { ExtendedCoreMessage } from '../types'+import { getToolCallModel } from '../utils/registry'+import { parseToolCallXml } from './parse-tool-call'++interface ToolExecutionResult {+ toolCallDataAnnotation: ExtendedCoreMessage | null+ toolCallMessages: CoreMessage[]+}++export async function executeToolCall(+ coreMessages: CoreMessage[],+ dataStream: DataStreamWriter,+ model?: string+): Promise<ToolExecutionResult> {+ const toolCallModel = getToolCallModel(model)+ // Convert Zod schema to string representation+ const searchSchemaString = Object.entries(searchSchema.shape)+ .map(([key, value]) => {+ const description = value.description+ const isOptional = value instanceof z.ZodOptional+ return `- ${key}${isOptional ? ' (optional)' : ''}: ${description}`+ })+ .join('\n')++ // Generate tool selection using XML format+ const toolSelectionResponse = await generateText({+ model: toolCallModel,+ system: `You are an intelligent assistant that analyzes conversations to select the most appropriate tools and their parameters.+ You excel at understanding context to determine when and how to use available tools, including crafting effective search queries.+ Current date: ${new Date().toISOString().split('T')[0]}++ Do not include any other text in your response.+ Respond in XML format with the following structure:+ <tool_call>+ <tool>tool_name</tool>+ <parameters>+ <query>search query text</query>+ <max_results>number - 5 by default</max_results>+ <search_depth>basic or advanced</search_depth>+ <include_domains>domain1,domain2</include_domains>+ <exclude_domains>domain1,domain2</exclude_domains>+ </parameters>+ </tool_call>++ Available tools: search++ Search parameters:+ ${searchSchemaString}++ If you don't need a tool, respond with <tool_call><tool></tool></tool_call>`,+ messages: coreMessages+ })++ // Parse the tool selection XML using the search schema+ const toolCall = parseToolCallXml(toolSelectionResponse.text, searchSchema)++ if (!toolCall || toolCall.tool === '') {+ return { toolCallDataAnnotation: null, toolCallMessages: [] }+ }++ const toolCallAnnotation = {+ type: 'tool_call',+ data: {+ state: 'call',+ toolCallId: `call_${generateId()}`,+ toolName: toolCall.tool,+ args: JSON.stringify(toolCall.parameters)+ }+ }+ dataStream.writeData(toolCallAnnotation)++ // Support for search tool only for now+ const searchResults = await search(+ toolCall.parameters?.query ?? '',+ toolCall.parameters?.max_results,+ toolCall.parameters?.search_depth as 'basic' | 'advanced',+ toolCall.parameters?.include_domains,+ toolCall.parameters?.exclude_domains+ )++ const updatedToolCallAnnotation = {+ ...toolCallAnnotation,+ data: {+ ...toolCallAnnotation.data,+ result: JSON.stringify(searchResults),+ state: 'result'+ }+ }+ dataStream.writeMessageAnnotation(updatedToolCallAnnotation)++ const toolCallDataAnnotation: ExtendedCoreMessage = {+ role: 'data',+ content: {+ type: 'tool_call',+ data: updatedToolCallAnnotation.data+ } as JSONValue+ }++ const toolCallMessages: CoreMessage[] = [+ {+ role: 'assistant',+ content: `Tool call result: ${JSON.stringify(searchResults)}`+ },+ {+ role: 'user',+ content: 'Now answer the user question.'+ }+ ]++ return { toolCallDataAnnotation, toolCallMessages }+}diff --git a/lib/utils/registry.ts b/lib/utils/registry.tsindex 60640b38..3ad6d04c 100644--- a/lib/utils/registry.ts+++ b/lib/utils/registry.ts@@ -93,3 +93,21 @@ export function isProviderEnabled(providerId: string): boolean {return false}}++export function getToolCallModel(model?: string) {+ const provider = model?.split(':')[0]+ switch (provider) {+ case 'groq':+ return getModel('groq:llama3.3-70b-versatile')+ case 'deepseek':+ return getModel('deepseek:deepseek-chat')+ default:+ return getModel('openai:gpt-4o-mini')+ }+}++export function isToolCallSupported(model?: string) {+ const modelName = model?.split(':')[1]++ return modelName?.includes('deepseek-r1')+}