An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
feat: Implement manual tool selection with XML parsing and search execution
Yoshiki Miurabdd3b93c144b
diff --git a/app/api/chat/route.ts b/app/api/chat/route.tsindex 54c1f577..7a208bb4 100644--- a/app/api/chat/route.ts+++ b/app/api/chat/route.ts@@ -33,6 +33,7 @@ export async function POST(req: Request) {}const supportsToolCalling = provider === 'openai'+ // const supportsToolCalling = truereturn supportsToolCalling? createToolCallingStreamResponse({diff --git a/components/chat-messages.tsx b/components/chat-messages.tsxindex 39b8fec4..588cf592 100644--- a/components/chat-messages.tsx+++ b/components/chat-messages.tsx@@ -47,6 +47,8 @@ export function ChatMessages({}))}+ console.log('messages', messages)+return (<div className="relative mx-auto px-4 w-full">{messages.map(message => (diff --git a/lib/streaming/create-manual-tool-stream.ts b/lib/streaming/create-manual-tool-stream.tsindex 23715075..6a02dd18 100644--- a/lib/streaming/create-manual-tool-stream.ts+++ b/lib/streaming/create-manual-tool-stream.ts@@ -2,10 +2,17 @@ import {convertToCoreMessages,createDataStreamResponse,DataStreamWriter,+ generateId,+ generateText,+ smoothStream,streamText} from 'ai'+import { z } from 'zod'+import { searchSchema } from '../schema/search'+import { search } from '../tools/search'import { getModel } from '../utils/registry'import { handleStreamFinish } from './handle-stream-finish'+import { parseToolCallXml } from './parse-tool-call'import { BaseStreamConfig } from './types'export function createManualToolStreamResponse(config: BaseStreamConfig) {@@ -13,12 +20,86 @@ export function createManualToolStreamResponse(config: BaseStreamConfig) {execute: async (dataStream: DataStreamWriter) => {try {const coreMessages = convertToCoreMessages(config.messages)- console.log(config.model)+ const model = getModel(config.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,+ 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</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)++ const toolCallAnnotation = {+ type: 'tool_call',+ data: {+ toolCallId: `call_${generateId()}`,+ toolName: toolCall.tool,+ args: JSON.stringify(toolCall.parameters)+ }+ }+ dataStream.writeMessageAnnotation(toolCallAnnotation)++ 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+ )++ dataStream.writeMessageAnnotation({+ ...toolCallAnnotation,+ data: {+ ...toolCallAnnotation.data,+ result: JSON.stringify(searchResults)+ }+ })++ // Proceed with the main streamconst result = streamText({- model: getModel(config.model),- system: 'You are a helpful assistant.',+ model,+ system:+ 'You are a helpful assistant. Use the selected tool and parameters to provide the best response.',messages: coreMessages,+ experimental_transform: smoothStream({ chunking: 'word' }),onFinish: async result => {await handleStreamFinish({responseMessages: result.response.messages,diff --git a/lib/streaming/parse-tool-call.ts b/lib/streaming/parse-tool-call.tsnew file mode 100644index 00000000..260da598--- /dev/null+++ b/lib/streaming/parse-tool-call.ts@@ -0,0 +1,62 @@+import { z } from 'zod'++export interface ToolCall<T = unknown> {+ tool: string+ parameters?: T+}++function getTagContent(xml: string, tag: string): string {+ const match = xml.match(new RegExp(`<${tag}>(.*?)</${tag}>`, 's'))+ return match ? match[1].trim() : ''+}++export function parseToolCallXml<T>(+ xml: string,+ schema?: z.ZodType<T>+): ToolCall<T> {+ const toolCallContent = getTagContent(xml, 'tool_call')+ if (!toolCallContent) {+ console.warn('No tool_call tag found in response')+ return { tool: '' }+ }++ const tool = getTagContent(toolCallContent, 'tool')+ if (!tool) return { tool: '' }++ const parametersXml = getTagContent(toolCallContent, 'parameters')+ if (!parametersXml || !schema) return { tool }++ try {+ // Extract all parameter values using tag names from schema+ const rawParameters: Record<string, string> = {}+ if (schema instanceof z.ZodObject) {+ Object.keys(schema.shape).forEach(key => {+ const value = getTagContent(parametersXml, key)+ if (value) rawParameters[key] = value+ })+ }++ // Parse parameters using the provided schema+ const parameters = schema.parse({+ ...rawParameters,+ // Convert comma-separated strings to arrays for array fields+ include_domains: rawParameters.include_domains+ ?.split(',')+ .map(d => d.trim())+ .filter(Boolean),+ exclude_domains: rawParameters.exclude_domains+ ?.split(',')+ .map(d => d.trim())+ .filter(Boolean),+ // Convert string to number for numeric fields+ max_results: rawParameters.max_results+ ? parseInt(rawParameters.max_results, 10)+ : undefined+ })++ return { tool, parameters }+ } catch (error) {+ console.error('Failed to parse parameters:', error)+ return { tool }+ }+}diff --git a/lib/tools/search.ts b/lib/tools/search.tsindex 4f263723..f46cfb99 100644--- a/lib/tools/search.ts+++ b/lib/tools/search.ts@@ -1,14 +1,14 @@-import { tool } from 'ai'-import Exa from 'exa-js'import { searchSchema } from '@/lib/schema/search'-import { sanitizeUrl } from '@/lib/utils'import {SearchResultImage,- SearchResults,SearchResultItem,+ SearchResults,SearXNGResponse,SearXNGResult} from '@/lib/types'+import { sanitizeUrl } from '@/lib/utils'+import { tool } from 'ai'+import Exa from 'exa-js'export const searchTool = tool({description: 'Search the web for information',@@ -86,6 +86,28 @@ export const searchTool = tool({}})+export async function search(+ query: string,+ maxResults: number = 10,+ searchDepth: 'basic' | 'advanced' = 'basic',+ includeDomains: string[] = [],+ excludeDomains: string[] = []+): Promise<SearchResults> {+ return searchTool.execute(+ {+ query,+ max_results: maxResults,+ search_depth: searchDepth,+ include_domains: includeDomains,+ exclude_domains: excludeDomains+ },+ {+ toolCallId: 'search',+ messages: []+ }+ )+}+async function tavilySearch(query: string,maxResults: number = 10,