An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
Add question tool with human-in-the-loop confirmation
Yoshiki Miurae5ecb4cdfc3e
diff --git a/components/chat-messages.tsx b/components/chat-messages.tsxindex 5b032c33..80dc3c3b 100644--- a/components/chat-messages.tsx+++ b/components/chat-messages.tsx@@ -10,6 +10,7 @@ interface ChatMessagesProps {onQuerySelect: (query: string) => voidisLoading: booleanchatId?: string+ addToolResult?: (params: { toolCallId: string; result: any }) => void}export function ChatMessages({@@ -17,7 +18,8 @@ export function ChatMessages({data,onQuerySelect,isLoading,- chatId+ chatId,+ addToolResult}: ChatMessagesProps) {const [openStates, setOpenStates] = useState<Record<string, boolean>>({})const manualToolCallId = 'manual-tool-call'@@ -103,6 +105,7 @@ export function ChatMessages({onOpenChange={handleOpenChange}onQuerySelect={onQuerySelect}chatId={chatId}+ addToolResult={addToolResult}/></div>))}@@ -113,6 +116,7 @@ export function ChatMessages({tool={lastToolData}isOpen={getIsOpen(manualToolCallId)}onOpenChange={open => handleOpenChange(manualToolCallId, open)}+ addToolResult={addToolResult}/>) : (<Spinner />diff --git a/components/chat.tsx b/components/chat.tsxindex 7d16278c..5aeb190b 100644--- a/components/chat.tsx+++ b/components/chat.tsx@@ -29,7 +29,8 @@ export function Chat({stop,append,data,- setData+ setData,+ addToolResult} = useChat({initialMessages: savedMessages,id: CHAT_ID,@@ -70,6 +71,7 @@ export function Chat({onQuerySelect={onQuerySelect}isLoading={isLoading}chatId={id}+ addToolResult={addToolResult}/><ChatPanelinput={input}diff --git a/components/question-confirmation.tsx b/components/question-confirmation.tsxnew file mode 100644index 00000000..9e46721f--- /dev/null+++ b/components/question-confirmation.tsx@@ -0,0 +1,70 @@+'use client'++import { Button } from '@/components/ui/button'+import {+ Card,+ CardContent,+ CardFooter,+ CardHeader,+ CardTitle+} from '@/components/ui/card'+import { ToolInvocation } from 'ai'++interface QuestionConfirmationProps {+ toolInvocation: ToolInvocation+ onConfirm: (toolCallId: string, approved: boolean) => void+}++interface QuestionOption {+ value: string+ label: string+}++export function QuestionConfirmation({+ toolInvocation,+ onConfirm+}: QuestionConfirmationProps) {+ const { question, options, allowsInput, inputLabel, inputPlaceholder } =+ toolInvocation.args++ return (+ <Card className="my-4 border border-yellow-200 bg-yellow-50">+ <CardHeader>+ <CardTitle className="text-lg">+ AI wants to ask a clarifying question+ </CardTitle>+ </CardHeader>+ <CardContent>+ <p className="font-medium mb-2">{question}</p>+ {options && options.length > 0 && (+ <div>+ <p className="text-sm text-gray-500 mb-1">With these options:</p>+ <ul className="list-disc pl-5">+ {options.map((option: QuestionOption, i: number) => (+ <li key={i}>{option.label}</li>+ ))}+ </ul>+ </div>+ )}+ {allowsInput && (+ <p className="text-sm text-gray-500 mt-2">+ {inputLabel+ ? `With free input: ${inputLabel}`+ : 'With free text input option'}+ </p>+ )}+ </CardContent>+ <CardFooter className="flex justify-end gap-2">+ <Button+ variant="outline"+ onClick={() => onConfirm(toolInvocation.toolCallId, false)}+ >+ Decline+ </Button>+ <Button onClick={() => onConfirm(toolInvocation.toolCallId, true)}>+ Allow Question+ </Button>+ </CardFooter>+ </Card>+ )+}diff --git a/components/render-message.tsx b/components/render-message.tsxindex 3549a347..3b0e3bb6 100644--- a/components/render-message.tsx+++ b/components/render-message.tsx@@ -13,6 +13,7 @@ interface RenderMessageProps {onOpenChange: (id: string, open: boolean) => voidonQuerySelect: (query: string) => voidchatId?: string+ addToolResult?: (params: { toolCallId: string; result: any }) => void}export function RenderMessage({@@ -21,7 +22,8 @@ export function RenderMessage({getIsOpen,onOpenChange,onQuerySelect,- chatId+ chatId,+ addToolResult}: RenderMessageProps) {const relatedQuestions = useMemo(() =>@@ -101,6 +103,7 @@ export function RenderMessage({tool={tool}isOpen={getIsOpen(tool.toolCallId)}onOpenChange={open => onOpenChange(tool.toolCallId, open)}+ addToolResult={addToolResult}/>))}{message.parts?.map((part, index) => {@@ -114,6 +117,7 @@ export function RenderMessage({onOpenChange={open =>onOpenChange(part.toolInvocation.toolCallId, open)}+ addToolResult={addToolResult}/>)case 'text':diff --git a/components/tool-section.tsx b/components/tool-section.tsxindex da93863a..5544a585 100644--- a/components/tool-section.tsx+++ b/components/tool-section.tsx@@ -1,17 +1,47 @@'use client'import { ToolInvocation } from 'ai'+import { QuestionConfirmation } from './question-confirmation'+import RetrieveSection from './retrieve-section'import { SearchSection } from './search-section'import { VideoSearchSection } from './video-search-section'-import RetrieveSection from './retrieve-section'interface ToolSectionProps {tool: ToolInvocationisOpen: booleanonOpenChange: (open: boolean) => void+ addToolResult?: (params: { toolCallId: string; result: any }) => void}-export function ToolSection({ tool, isOpen, onOpenChange }: ToolSectionProps) {+export function ToolSection({+ tool,+ isOpen,+ onOpenChange,+ addToolResult+}: ToolSectionProps) {+ // Special handling for ask_question tool+ if (+ tool.toolName === 'ask_question' &&+ tool.state === 'call' &&+ addToolResult+ ) {+ return (+ <QuestionConfirmation+ toolInvocation={tool}+ onConfirm={(toolCallId, approved) => {+ // If approved, return the original args so the question can be displayed+ // If declined, return a message that the user declined+ addToolResult({+ toolCallId,+ result: approved+ ? tool.args+ : { declined: true, message: 'User declined this question' }+ })+ }}+ />+ )+ }+switch (tool.toolName) {case 'search':return (diff --git a/lib/agents/researcher.ts b/lib/agents/researcher.tsindex f42996b4..f3140b6e 100644--- a/lib/agents/researcher.ts+++ b/lib/agents/researcher.ts@@ -1,4 +1,5 @@import { CoreMessage, smoothStream, streamText } from 'ai'+import { askQuestionTool } from '../tools/question'import { retrieveTool } from '../tools/retrieve'import { searchTool } from '../tools/search'import { videoSearchTool } from '../tools/video-search'@@ -7,17 +8,26 @@ import { getModel } from '../utils/registry'const SYSTEM_PROMPT = `Instructions:-You are a helpful AI assistant with access to real-time web search, content retrieval, and video search capabilities.+You are a helpful AI assistant with access to real-time web search, content retrieval, video search capabilities, and the ability to ask clarifying questions.+When asked a question, you should:-1. Search for relevant information using the search tool when needed-2. Use the retrieve tool to get detailed content from specific URLs-3. Use the video search tool when looking for video content-4. Analyze all search results to provide accurate, up-to-date information-5. Always cite sources using the [number](url) format, matching the order of search results. If multiple sources are relevant, include all of them, and comma separate them. Only use information that has a URL available for citation.-6. If results are not relevant or helpful, rely on your general knowledge-7. Provide comprehensive and detailed responses based on search results, ensuring thorough coverage of the user's question-8. Use markdown to structure your responses. Use headings to break up the content into sections.-9. **Use the retrieve tool only with user-provided URLs.**+1. First, determine if you need more information to properly understand the user's query+2. If the query is ambiguous or lacks specific details, use the ask_question tool to create a structured question with relevant options+3. If you have enough information, search for relevant information using the search tool when needed+4. Use the retrieve tool to get detailed content from specific URLs+5. Use the video search tool when looking for video content+6. Analyze all search results to provide accurate, up-to-date information+7. Always cite sources using the [number](url) format, matching the order of search results. If multiple sources are relevant, include all of them, and comma separate them. Only use information that has a URL available for citation.+8. If results are not relevant or helpful, rely on your general knowledge+9. Provide comprehensive and detailed responses based on search results, ensuring thorough coverage of the user's question+10. Use markdown to structure your responses. Use headings to break up the content into sections.+11. **Use the retrieve tool only with user-provided URLs.**++When using the ask_question tool:+- Create clear, concise questions+- Provide relevant predefined options+- Enable free-form input when appropriate+- Match the language to the user's language (except option values which must be in English)Citation Format:[number](url)@@ -44,10 +54,11 @@ export function researcher({tools: {search: searchTool,retrieve: retrieveTool,- videoSearch: videoSearchTool+ videoSearch: videoSearchTool,+ ask_question: askQuestionTool},experimental_activeTools: searchMode- ? ['search', 'retrieve', 'videoSearch']+ ? ['search', 'retrieve', 'videoSearch', 'ask_question']: [],maxSteps: searchMode ? 5 : 1,experimental_transform: smoothStream({ chunking: 'word' })diff --git a/lib/streaming/create-tool-calling-stream.ts b/lib/streaming/create-tool-calling-stream.tsindex 30e4de7d..fdee0841 100644--- a/lib/streaming/create-tool-calling-stream.ts+++ b/lib/streaming/create-tool-calling-stream.ts@@ -1,6 +1,7 @@import { researcher } from '@/lib/agents/researcher'import {convertToCoreMessages,+ CoreMessage,createDataStreamResponse,DataStreamWriter,streamText@@ -10,6 +11,19 @@ import { isReasoningModel } from '../utils/registry'import { handleStreamFinish } from './handle-stream-finish'import { BaseStreamConfig } from './types'+// Function to check if a message contains ask_question tool invocation+function containsAskQuestionTool(message: CoreMessage) {+ // For CoreMessage format, we check the content array+ if (message.role !== 'assistant' || !Array.isArray(message.content)) {+ return false+ }++ // Check if any content item is a tool-call with ask_question tool+ return message.content.some(+ item => item.type === 'tool-call' && item.toolName === 'ask_question'+ )+}+export function createToolCallingStreamResponse(config: BaseStreamConfig) {return createDataStreamResponse({execute: async (dataStream: DataStreamWriter) => {@@ -32,13 +46,29 @@ export function createToolCallingStreamResponse(config: BaseStreamConfig) {const result = streamText({...researcherConfig,onFinish: async result => {+ // Check if the last message contains an ask_question tool invocation+ const shouldSkipRelatedQuestions =+ isReasoningModel(modelId) ||+ (result.response.messages.length > 0 &&+ containsAskQuestionTool(+ result.response.messages[+ result.response.messages.length - 1+ ] as CoreMessage+ ))++ console.log(+ 'shouldSkipRelatedQuestions',+ shouldSkipRelatedQuestions,+ JSON.stringify(result.response.messages)+ )+await handleStreamFinish({responseMessages: result.response.messages,originalMessages: messages,model: modelId,chatId,dataStream,- skipRelatedQuestions: isReasoningModel(modelId)+ skipRelatedQuestions: shouldSkipRelatedQuestions})}})@@ -50,7 +80,7 @@ export function createToolCallingStreamResponse(config: BaseStreamConfig) {}},onError: error => {- console.error('Stream error:', error)+ // console.error('Stream error:', error)return error instanceof Error ? error.message : String(error)}})diff --git a/lib/tools/question.ts b/lib/tools/question.tsnew file mode 100644index 00000000..c913154a--- /dev/null+++ b/lib/tools/question.ts@@ -0,0 +1,28 @@+import { tool } from 'ai'+import { z } from 'zod'++export const askQuestionTool = tool({+ description:+ 'Ask a clarifying question with multiple options when more information is needed',+ parameters: z.object({+ question: z.string().describe('The main question to ask the user'),+ options: z+ .array(+ z.object({+ value: z.string().describe('Option identifier (always in English)'),+ label: z.string().describe('Display text for the option')+ })+ )+ .describe('List of predefined options'),+ allowsInput: z.boolean().describe('Whether to allow free-form text input'),+ inputLabel: z+ .string()+ .optional()+ .describe('Label for free-form input field'),+ inputPlaceholder: z+ .string()+ .optional()+ .describe('Placeholder text for input field')+ })+ // execute function removed to enable frontend confirmation+})