An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
feat: migrate researcher agent to streamText with smoothStream
Yoshiki Miuraee9a1f7984aa
diff --git a/lib/agents/researcher.ts b/lib/agents/researcher.tsindex a7fb233b..aafa68bc 100644--- a/lib/agents/researcher.ts+++ b/lib/agents/researcher.ts@@ -1,9 +1,10 @@import {- Experimental_Agent as Agent,+ type ModelMessage,+ smoothStream,stepCountIs,+ streamText,tool,- UIMessage,- UIMessageStreamWriter+ type UIMessageStreamWriter} from 'ai'import type {@@ -12,7 +13,7 @@ import type {ResearcherResponse,ResearcherTools} from '@/lib/types/agent'-import { Model } from '@/lib/types/models'+import { type Model } from '@/lib/types/models'import { fetchTool } from '../tools/fetch'import { createQuestionTool } from '../tools/question'@@ -155,23 +156,24 @@ export function createResearcher({const shouldForceTodoWrite =searchMode === 'planning' && writer && 'todoWrite' in todoTools- // Create and return the agent with enhanced type safety- const agent = new Agent<ResearcherTools>({+ // Create streamText-based agent with smoothStream support+ const agentConfig = {model: getModel(model),system: `${systemPrompt}\nCurrent date and time: ${currentDate}`,tools,activeTools: activeToolsList,stopWhen: stepCountIs(maxSteps),abortSignal,+ experimental_transform: smoothStream({ chunking: 'word' }), // Enable smooth streaming...(modelConfig?.providerOptions && {providerOptions: modelConfig.providerOptions}),// Force todoWrite tool on first step for planning mode...(shouldForceTodoWrite && {- prepareStep: async ({ stepNumber }) => {+ prepareStep: async ({ stepNumber }: { stepNumber: number }) => {if (stepNumber === 0) {return {- toolChoice: { type: 'tool', toolName: 'todoWrite' }+ toolChoice: { type: 'tool', toolName: 'todoWrite' } as const}}return undefined@@ -190,9 +192,20 @@ export function createResearcher({})}}- })+ }++ // Create Agent-compatible wrapper with streamText+ const agent: ResearcherAgent = {+ tools,+ stream: ({ messages }: { messages: ModelMessage[] }) => {+ return streamText({+ ...agentConfig,+ messages+ })+ }+ }- return agent as ResearcherAgent+ return agent} catch (error) {console.error('Error in createResearcher:', error)throw error@@ -207,6 +220,9 @@ export function getResearcherTools(agent: ResearcherAgent): ResearcherTools {// Helper function to create a respond wrapper with type safetyexport function createResearcherRespond(agent: ResearcherAgent) {return (options: ResearcherRespondOptions): ResearcherResponse => {+ if (!agent.respond) {+ throw new Error('respond method not available - use stream instead')+ }return agent.respond(options)}}diff --git a/lib/types/agent.ts b/lib/types/agent.tsindex a9236218..c9b888aa 100644--- a/lib/types/agent.ts+++ b/lib/types/agent.ts@@ -2,6 +2,8 @@ import type {Experimental_Agent as Agent,Experimental_InferAgentUIMessage as InferAgentUIMessage,InferUITools,+ ModelMessage,+ StreamTextResult,UIMessage,UIToolInvocation} from 'ai'@@ -18,11 +20,23 @@ export type ResearcherTools = {askQuestion: ReturnType<typeof createQuestionTool>} & ReturnType<typeof createTodoTools>-// Type for the researcher agent-export type ResearcherAgent = Agent<ResearcherTools>+// Type for the researcher agent (streamText-based)+export type ResearcherAgent = {+ tools: ResearcherTools+ stream: (options: {+ messages: ModelMessage[]+ }) => StreamTextResult<any, never>+ generate?: (options: {+ messages?: ModelMessage[]+ prompt?: string+ }) => Promise<any>+ respond?: (options: ResearcherRespondOptions) => ResearcherResponse+}// Infer UI message type for researcher agent-export type ResearcherUIMessage = InferAgentUIMessage<ResearcherAgent>+// Note: Using Agent type for UI message inference+type _AgentType = Agent<ResearcherTools>+export type ResearcherUIMessage = InferAgentUIMessage<_AgentType>// Infer UI tools type for researcher agentexport type ResearcherUITools = InferUITools<ResearcherTools>