Skip to content

An AI-powered search engine with a generative UI

feat: implement streaming support for AI tools

Yoshiki Miurae2aaa1fcdc86
diff --git a/components/artifact/search-artifact-content.tsx b/components/artifact/search-artifact-content.tsx
index f5c12a8a..8d2ce320 100644
--- a/components/artifact/search-artifact-content.tsx
+++ b/components/artifact/search-artifact-content.tsx
@@ -12,8 +12,10 @@ import {
} from '@/components/video-search-results'
export function SearchArtifactContent({ tool }: { tool: ToolPart<'search'> }) {
+ // Handle streaming output states
+ const output = tool.state === 'output-available' ? tool.output : undefined
const searchResults: TypeSearchResults | undefined =
- tool.state === 'output-available' ? tool.output : undefined
+ output?.state === 'complete' ? output : undefined
const query = tool.input?.query
const hasResults =
diff --git a/components/fetch-section.tsx b/components/fetch-section.tsx
index 0ee1d832..40912110 100644
--- a/components/fetch-section.tsx
+++ b/components/fetch-section.tsx
@@ -33,6 +33,11 @@ export function FetchSection({
const isToolLoading =
tool.state === 'input-streaming' || tool.state === 'input-available'
+ // Handle streaming output states
+ const output = tool.state === 'output-available' ? tool.output : undefined
+ const isFetching = output?.state === 'fetching'
+ const fetchResults = output?.state === 'complete' ? output : undefined
+
// Determine the status based on tool output availability
let displayStatus: 'fetching' | 'success' | 'error' = 'fetching'
let error: string | undefined
@@ -43,11 +48,11 @@ export function FetchSection({
if (tool.state === 'output-error') {
displayStatus = 'error'
error = tool.errorText || 'Failed to retrieve content'
- } else if (!tool.output) {
+ } else if (!output || isFetching) {
displayStatus = 'fetching'
- } else {
- // Success state - we have output
- const data = tool.output as SearchResultsType
+ } else if (fetchResults) {
+ // Success state - we have complete output
+ const data = fetchResults as SearchResultsType
if (data?.results?.[0]) {
displayStatus = 'success'
title = data.results[0].title
diff --git a/components/search-section.tsx b/components/search-section.tsx
index df21d5ff..92f87463 100644
--- a/components/search-section.tsx
+++ b/components/search-section.tsx
@@ -44,11 +44,16 @@ export function SearchSection({
const isToolLoading =
tool.state === 'input-streaming' || tool.state === 'input-available'
+
+ // Handle streaming output states
+ const output = tool.state === 'output-available' ? tool.output : undefined
+ const isSearching = output?.state === 'searching'
const searchResults: TypeSearchResults | undefined =
- tool.state === 'output-available' ? tool.output : undefined
+ output?.state === 'complete' ? output : undefined
+
const isError = tool.state === 'output-error'
const errorMessage = tool.errorText || 'Search failed'
- const query = tool.input?.query || ''
+ const query = tool.input?.query || output?.query || ''
const includeDomains = tool.input?.include_domains
const includeDomainsString =
includeDomains && includeDomains.length > 0
@@ -65,7 +70,7 @@ export function SearchSection({
const header = (
<ProcessHeader
onInspect={() => open(tool)}
- isLoading={isLoading && isToolLoading}
+ isLoading={isLoading && (isToolLoading || isSearching)}
ariaExpanded={isOpen}
label={
<div className="flex items-center gap-2 min-w-0 overflow-hidden">
@@ -157,7 +162,7 @@ export function SearchSection({
</div>
</div>
</Section>
- ) : isLoading && isToolLoading ? (
+ ) : (isLoading && isToolLoading) || isSearching ? (
<SearchSkeleton />
) : searchResults?.results && searchResults.results.length > 0 ? (
<Section title="Sources">
diff --git a/components/tool-todo-display.tsx b/components/tool-todo-display.tsx
index 9cdae201..7f8b65cf 100644
--- a/components/tool-todo-display.tsx
+++ b/components/tool-todo-display.tsx
@@ -77,10 +77,16 @@ export function ToolTodoDisplay({
<span className="inline-flex items-center gap-2 min-w-0 overflow-hidden">
<ListTodo className="size-4 text-muted-foreground shrink-0" />
<span className="truncate">
- {state === 'output-available'
+ {state === 'output-available' && output
? tool === 'todoRead'
- ? (output as any)?.summary || output?.message || 'Todo list'
- : output?.message || 'Updated tasks'
+ ? (typeof output === 'object' &&
+ 'summary' in output &&
+ typeof output.summary === 'string'
+ ? output.summary
+ : null) ||
+ output.message ||
+ 'Todo list'
+ : output.message || 'Updated tasks'
: tool === 'todoWrite'
? 'Updating tasks...'
: 'Reading tasks...'}
@@ -153,7 +159,12 @@ export function ToolTodoDisplay({
todos={output?.todos}
message={
tool === 'todoRead'
- ? (output as any)?.summary || output?.message
+ ? (output &&
+ typeof output === 'object' &&
+ 'summary' in output &&
+ typeof output.summary === 'string'
+ ? output.summary
+ : undefined) || output?.message
: output?.message
}
completedCount={completedCount}
@@ -164,7 +175,9 @@ export function ToolTodoDisplay({
/>
) : state === 'output-error' ? (
<div className="px-3 pb-3 text-xs text-destructive">{`Todo tool failed${
- (output as any)?.message ? `: ${(output as any).message}` : ''
+ output && 'message' in output && output.message
+ ? `: ${output.message}`
+ : ''
}`}</div>
) : null}
</div>
diff --git a/lib/agents/researcher.ts b/lib/agents/researcher.ts
index 9d12ac40..f28e46a4 100644
--- a/lib/agents/researcher.ts
+++ b/lib/agents/researcher.ts
@@ -3,14 +3,15 @@ import {
stepCountIs,
tool,
UIMessage,
- UIMessageStreamWriter} from 'ai'
+ UIMessageStreamWriter
+} from 'ai'
import type {
ResearcherAgent,
ResearcherRespondOptions,
ResearcherResponse,
- ResearcherTools,
- ResearcherUIMessage} from '@/lib/types/agent'
+ ResearcherTools
+} from '@/lib/types/agent'
import { Model } from '@/lib/types/models'
import { fetchTool } from '../tools/fetch'
@@ -27,36 +28,48 @@ import {
QUICK_MODE_PROMPT
} from './prompts/search-mode-prompts'
-// Enhanced wrapper function with better type safety
+// Enhanced wrapper function with better type safety and streaming support
function wrapSearchToolForQuickMode<
T extends ReturnType<typeof createSearchTool>
>(originalTool: T): T {
return tool({
description: originalTool.description,
inputSchema: originalTool.inputSchema,
- execute: async (params: any, context: any) => {
+ async *execute(params, context) {
const executeFunc = originalTool.execute
if (!executeFunc) {
throw new Error('Search tool execute function is not defined')
}
// Force optimized type for quick mode
- const result = await executeFunc(
- {
- ...params,
- type: 'optimized'
- },
- context
- )
-
- return (
- result || {
+ const modifiedParams = {
+ ...params,
+ type: 'optimized' as const
+ }
+
+ // Execute the original tool and pass through all yielded values
+ const result = executeFunc(modifiedParams, context)
+
+ // Handle AsyncIterable (streaming) case
+ if (
+ result &&
+ typeof result === 'object' &&
+ Symbol.asyncIterator in result
+ ) {
+ for await (const chunk of result) {
+ yield chunk
+ }
+ } else {
+ // Fallback for non-streaming (shouldn't happen with new implementation)
+ const finalResult = await result
+ yield finalResult || {
+ state: 'complete' as const,
results: [],
images: [],
query: params.query,
number_of_results: 0
}
- )
+ }
}
}) as T
}
diff --git a/lib/tools/fetch.ts b/lib/tools/fetch.ts
index d1b996ea..bbaa6270 100644
--- a/lib/tools/fetch.ts
+++ b/lib/tools/fetch.ts
@@ -161,7 +161,13 @@ export const fetchTool = tool({
description:
'Fetch content from any URL. By default uses "regular" type which performs fast, direct HTML fetching without external APIs - ideal for most websites. Only use "api" type when you need: 1) PDF content extraction, 2) Complex JavaScript-rendered pages, 3) Better markdown formatting, 4) Table extraction. The "api" type requires Jina or Tavily API keys.',
inputSchema: fetchSchema,
- execute: async ({ url, type = 'regular' }) => {
+ async *execute({ url, type = 'regular' }) {
+ // Yield initial fetching state
+ yield {
+ state: 'fetching' as const,
+ url
+ }
+
let results: SearchResultsType
if (type === 'regular') {
@@ -177,7 +183,11 @@ export const fetchTool = tool({
}
}
- return results
+ // Yield final results with complete state
+ yield {
+ state: 'complete' as const,
+ ...results
+ }
}
})
diff --git a/lib/tools/search.ts b/lib/tools/search.ts
index 842c436c..c41e2f99 100644
--- a/lib/tools/search.ts
+++ b/lib/tools/search.ts
@@ -18,7 +18,7 @@ export function createSearchTool(fullModel: string) {
description:
'Search the web for information. For YouTube/video content, use type="general" with content_types:["video"] for optimal visual presentation with thumbnails.',
inputSchema: getSearchSchemaForModel(fullModel),
- execute: async (
+ async *execute(
{
query,
type = 'optimized',
@@ -29,7 +29,12 @@ export function createSearchTool(fullModel: string) {
exclude_domains = []
},
context
- ) => {
+ ) {
+ // Yield initial searching state
+ yield {
+ state: 'searching' as const,
+ query
+ }
// Ensure max_results is at least 10
const minResults = 10
const effectiveMaxResults = Math.max(
@@ -137,7 +142,12 @@ export function createSearchTool(fullModel: string) {
}
console.log('completed search')
- return searchResult
+
+ // Yield final results with complete state
+ yield {
+ state: 'complete' as const,
+ ...searchResult
+ }
}
})
}
@@ -162,8 +172,8 @@ export async function search(
content_types: ['web'],
max_results: maxResults,
search_depth: searchDepth,
- include_domains: includeDomains as any,
- exclude_domains: excludeDomains as any
+ include_domains: includeDomains,
+ exclude_domains: excludeDomains
},
{
toolCallId: 'search',
@@ -180,7 +190,11 @@ export async function search(
// Collect all results from the async iterable
let searchResults: SearchResults | null = null
for await (const chunk of result) {
- searchResults = chunk
+ // Only assign when we get the complete result
+ if ('state' in chunk && chunk.state === 'complete') {
+ const { state, ...rest } = chunk
+ searchResults = rest as SearchResults
+ }
}
return (
searchResults ?? { results: [], images: [], query, number_of_results: 0 }
diff --git a/lib/types/agent.ts b/lib/types/agent.ts
index 5f0bb815..a9236218 100644
--- a/lib/types/agent.ts
+++ b/lib/types/agent.ts
@@ -3,7 +3,8 @@ import type {
Experimental_InferAgentUIMessage as InferAgentUIMessage,
InferUITools,
UIMessage,
- UIToolInvocation} from 'ai'
+ UIToolInvocation
+} from 'ai'
import type { fetchTool } from '../tools/fetch'
import type { createQuestionTool } from '../tools/question'