Skip to content

An AI-powered search engine with a generative UI

refactor: replace CoreMessage with ModelMessage across utility functions and remove unused code

Yoshiki Miuradb309800bf7c
diff --git a/lib/agents/generate-related-questions.ts b/lib/agents/generate-related-questions.ts
index 907e3244..0d5ebe7a 100644
--- a/lib/agents/generate-related-questions.ts
+++ b/lib/agents/generate-related-questions.ts
@@ -1,5 +1,6 @@
import { relatedSchema } from '@/lib/schema/related'
-import { CoreMessage, generateObject } from 'ai'
+import { openai } from '@ai-sdk/openai'
+import { ModelMessage, generateObject } from 'ai'
import {
getModel,
getToolCallModel,
@@ -7,13 +8,13 @@ import {
} from '../utils/registry'
export async function generateRelatedQuestions(
- messages: CoreMessage[],
+ messages: ModelMessage[],
model: string
) {
const lastMessages = messages.slice(-1).map(message => ({
...message,
role: 'user'
- })) as CoreMessage[]
+ })) as ModelMessage[]
const supportedModel = isToolCallSupported(model)
const currentModel = supportedModel
@@ -21,7 +22,8 @@ export async function generateRelatedQuestions(
: getToolCallModel(model)
const result = await generateObject({
- model: currentModel,
+ // model: currentModel, //
+ model: openai('gpt-4o-mini'),
system: `As a professional web researcher, your task is to generate a set of three queries that explore the subject matter more deeply, building upon the initial query and the information uncovered in its search results.
For instance, if the original query was "Starship's third test flight key milestones", your output should follow this format:
diff --git a/lib/agents/manual-researcher.ts b/lib/agents/manual-researcher.ts
deleted file mode 100644
index ce7a5613..00000000
--- a/lib/agents/manual-researcher.ts
+++ /dev/null
@@ -1,70 +0,0 @@
-import { CoreMessage, smoothStream, streamText } from 'ai'
-import { getModel } from '../utils/registry'
-
-const BASE_SYSTEM_PROMPT = `
-Instructions:
-
-You are a helpful AI assistant providing accurate information.
-
-1. Provide comprehensive and detailed responses to user questions
-2. Use markdown to structure your responses with appropriate headings
-3. Acknowledge when you are uncertain about specific details
-4. Focus on maintaining high accuracy in your responses
-`
-
-const SEARCH_ENABLED_PROMPT = `
-${BASE_SYSTEM_PROMPT}
-
-When analyzing search results:
-1. Analyze the provided search results carefully to answer the user's question
-2. Always cite sources using the [number](url) format, matching the order of search results
-3. If multiple sources are relevant, include all of them using comma-separated citations
-4. Only use information that has a URL available for citation
-5. If the search results don't contain relevant information, acknowledge this and provide a general response
-
-Citation Format:
-[number](url)
-`
-
-const SEARCH_DISABLED_PROMPT = `
-${BASE_SYSTEM_PROMPT}
-
-Important:
-1. Provide responses based on your general knowledge
-2. Be clear about any limitations in your knowledge
-3. Suggest when searching for additional information might be beneficial
-`
-
-interface ManualResearcherConfig {
- messages: CoreMessage[]
- model: string
- isSearchEnabled?: boolean
-}
-
-type ManualResearcherReturn = Parameters<typeof streamText>[0]
-
-export function manualResearcher({
- messages,
- model,
- isSearchEnabled = true
-}: ManualResearcherConfig): ManualResearcherReturn {
- try {
- const currentDate = new Date().toLocaleString()
- const systemPrompt = isSearchEnabled
- ? SEARCH_ENABLED_PROMPT
- : SEARCH_DISABLED_PROMPT
-
- return {
- model: getModel(model),
- system: `${systemPrompt}\nCurrent date and time: ${currentDate}`,
- messages,
- temperature: 0.6,
- topP: 1,
- topK: 40,
- experimental_transform: smoothStream()
- }
- } catch (error) {
- console.error('Error in manualResearcher:', error)
- throw error
- }
-}
diff --git a/lib/streaming/parse-tool-call.ts b/lib/streaming/parse-tool-call.ts
deleted file mode 100644
index 28396129..00000000
--- a/lib/streaming/parse-tool-call.ts
+++ /dev/null
@@ -1,64 +0,0 @@
-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 with default empty arrays
- 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/streaming/tool-execution.ts b/lib/streaming/tool-execution.ts
deleted file mode 100644
index eeb1a1b4..00000000
--- a/lib/streaming/tool-execution.ts
+++ /dev/null
@@ -1,127 +0,0 @@
-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 { getModel } 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,
- searchMode: boolean
-): Promise<ToolExecutionResult> {
- // If search mode is disabled, return empty tool call
- if (!searchMode) {
- return { toolCallDataAnnotation: null, toolCallMessages: [] }
- }
-
- // 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 defaultMaxResults = model?.includes('ollama') ? 5 : 20
-
- // Generate tool selection using XML format
- const toolSelectionResponse = await generateText({
- model: getModel(model),
- 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 - ${defaultMaxResults} 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/types/index.ts b/lib/types/index.ts
index 6ba9f7a7..f2733d11 100644
--- a/lib/types/index.ts
+++ b/lib/types/index.ts
@@ -1,5 +1,3 @@
-import { CoreMessage, JSONValue, Message } from 'ai'
-
export type SearchResults = {
images: SearchResultImage[]
results: SearchResultItem[]
@@ -57,39 +55,6 @@ export type SerperSearchResultItem = {
position: number
}
-export interface Chat extends Record<string, any> {
- id: string
- title: string
- createdAt: Date
- userId: string
- path: string
- messages: ExtendedCoreMessage[] // Note: Changed from AIMessage to ExtendedCoreMessage
- sharePath?: string
-}
-
-// ExtendedCoreMessage for saveing annotations
-export type ExtendedCoreMessage = Omit<CoreMessage, 'role' | 'content'> & {
- role: CoreMessage['role'] | 'data'
- content: CoreMessage['content'] | JSONValue
-}
-
-export type AIMessage = {
- role: 'user' | 'assistant' | 'system' | 'function' | 'data' | 'tool'
- content: string
- id: string
- name?: string
- type?:
- | 'answer'
- | 'related'
- | 'skip'
- | 'inquiry'
- | 'input'
- | 'input_related'
- | 'tool'
- | 'followup'
- | 'end'
-}
-
export interface SearXNGResult {
title: string
url: string
diff --git a/lib/utils/context-window.ts b/lib/utils/context-window.ts
index 94dfda82..23dafa1e 100644
--- a/lib/utils/context-window.ts
+++ b/lib/utils/context-window.ts
@@ -1,4 +1,4 @@
-import { CoreMessage } from 'ai'
+import { ModelMessage } from 'ai'
import { Model } from '../types/models'
const DEFAULT_CONTEXT_WINDOW = 128_000
@@ -23,11 +23,11 @@ export function getMaxAllowedTokens(model: Model): number {
}
export function truncateMessages(
- messages: CoreMessage[],
+ messages: ModelMessage[],
maxTokens: number
-): CoreMessage[] {
+): ModelMessage[] {
let totalTokens = 0
- const tempMessages: CoreMessage[] = []
+ const tempMessages: ModelMessage[] = []
for (let i = messages.length - 1; i >= 0; i--) {
const message = messages[i]
diff --git a/lib/utils/index.ts b/lib/utils/index.ts
index da992d56..5f9a0693 100644
--- a/lib/utils/index.ts
+++ b/lib/utils/index.ts
@@ -1,16 +1,6 @@
import { type Model } from '@/lib/types/models'
-import {
- convertToCoreMessages,
- CoreMessage,
- CoreToolMessage,
- generateId,
- JSONValue,
- Message,
- ToolInvocation
-} from 'ai'
import { type ClassValue, clsx } from 'clsx'
import { twMerge } from 'tailwind-merge'
-import { ExtendedCoreMessage } from '../types'
// Function to generate a UUID
export function generateUUID(): string {
@@ -26,27 +16,6 @@ export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs))
}
-/**
- * Takes an array of AIMessage and modifies each message where the role is 'tool'.
- * Changes the role to 'assistant' and converts the content to a JSON string.
- * Returns the modified messages as an array of CoreMessage.
- *
- * @param aiMessages - Array of AIMessage
- * @returns modifiedMessages - Array of modified messages
- */
-export function transformToolMessages(messages: CoreMessage[]): CoreMessage[] {
- return messages.map(message =>
- message.role === 'tool'
- ? {
- ...message,
- role: 'assistant',
- content: JSON.stringify(message.content),
- type: 'tool'
- }
- : message
- ) as CoreMessage[]
-}
-
/**
* Sanitizes a URL by replacing spaces with '%20'
* @param url - The URL to sanitize
@@ -66,203 +35,3 @@ export function getDefaultModelId(models: Model[]): string {
}
return createModelId(models[0])
}
-
-function addToolMessageToChat({
- toolMessage,
- messages
-}: {
- toolMessage: CoreToolMessage
- messages: Array<Message>
-}): Array<Message> {
- return messages.map(message => {
- if (message.toolInvocations) {
- return {
- ...message,
- toolInvocations: message.toolInvocations.map(toolInvocation => {
- const toolResult = toolMessage.content.find(
- tool => tool.toolCallId === toolInvocation.toolCallId
- )
-
- if (toolResult) {
- return {
- ...toolInvocation,
- state: 'result',
- result: toolResult.result
- }
- }
-
- return toolInvocation
- })
- }
- }
-
- return message
- })
-}
-
-export function convertToUIMessages(
- messages: Array<ExtendedCoreMessage>
-): Array<Message> {
- let pendingAnnotations: JSONValue[] = []
- let pendingReasoning: string | undefined = undefined
- let pendingReasoningTime: number | undefined = undefined
-
- return messages.reduce((chatMessages: Array<Message>, message) => {
- // Handle tool messages
- if (message.role === 'tool') {
- return addToolMessageToChat({
- toolMessage: message as CoreToolMessage,
- messages: chatMessages
- })
- }
-
- // Data messages are used to capture annotations, including reasoning.
- if (message.role === 'data') {
- if (
- message.content !== null &&
- message.content !== undefined &&
- typeof message.content !== 'string'
- ) {
- const content = message.content as JSONValue
- if (
- content &&
- typeof content === 'object' &&
- 'type' in content &&
- 'data' in content
- ) {
- if (content.type === 'reasoning') {
- // If content.data is an object, capture its reasoning and time;
- // otherwise treat it as a simple string.
- if (typeof content.data === 'object' && content.data !== null) {
- pendingReasoning = (content.data as any).reasoning
- pendingReasoningTime = (content.data as any).time
- } else {
- pendingReasoning = content.data as string
- pendingReasoningTime = 0
- }
- } else {
- pendingAnnotations.push(content)
- }
- }
- }
- return chatMessages
- }
-
- // Build the text content and tool invocations from message.content.
- let textContent = ''
- let toolInvocations: Array<ToolInvocation> = []
-
- if (message.content) {
- if (typeof message.content === 'string') {
- textContent = message.content
- } else if (Array.isArray(message.content)) {
- for (const content of message.content) {
- if (content && typeof content === 'object' && 'type' in content) {
- if (content.type === 'text' && 'text' in content) {
- textContent += content.text
- } else if (
- content.type === 'tool-call' &&
- 'toolCallId' in content &&
- 'toolName' in content &&
- 'args' in content
- ) {
- toolInvocations.push({
- state: 'call',
- toolCallId: content.toolCallId,
- toolName: content.toolName,
- args: content.args
- } as ToolInvocation)
- }
- }
- }
- }
- }
-
- // For assistant messages, assemble annotations from any stashed data.
- let annotations: JSONValue[] | undefined = undefined
- if (message.role === 'assistant') {
- if (pendingAnnotations.length > 0 || pendingReasoning !== undefined) {
- annotations = [
- ...pendingAnnotations,
- ...(pendingReasoning !== undefined
- ? [
- {
- type: 'reasoning',
- data: {
- reasoning: pendingReasoning,
- time: pendingReasoningTime ?? 0
- }
- }
- ]
- : [])
- ]
- }
- }
-
- // Create the new message. Note: we do not include a top-level "reasoning" property.
- const newMessage: Message = {
- id: generateId(),
- role: message.role,
- content: textContent,
- toolInvocations: toolInvocations.length > 0 ? toolInvocations : undefined,
- annotations: annotations
- }
-
- chatMessages.push(newMessage)
-
- // Clear pending state after processing an assistant message.
- if (message.role === 'assistant') {
- pendingAnnotations = []
- pendingReasoning = undefined
- pendingReasoningTime = undefined
- }
-
- return chatMessages
- }, [])
-}
-
-export function convertToExtendedCoreMessages(
- messages: Message[]
-): ExtendedCoreMessage[] {
- const result: ExtendedCoreMessage[] = []
-
- for (const message of messages) {
- // Convert annotations to data messages
- if (message.annotations && message.annotations.length > 0) {
- message.annotations.forEach(annotation => {
- result.push({
- role: 'data',
- content: annotation
- })
- })
- }
-
- // Convert reasoning to data message with unified structure (including time)
- if (message.reasoning) {
- const reasoningTime = (message as any).reasoningTime ?? 0
- const reasoningData =
- typeof message.reasoning === 'string'
- ? { reasoning: message.reasoning, time: reasoningTime }
- : {
- ...(message.reasoning as Record<string, unknown>),
- time:
- (message as any).reasoningTime ??
- (message.reasoning as any).time ??
- 0
- }
- result.push({
- role: 'data',
- content: {
- type: 'reasoning',
- data: reasoningData
- } as JSONValue
- })
- }
-
- // Convert current message
- const converted = convertToCoreMessages([message])
- result.push(...converted)
- }
-
- return result
-}
diff --git a/lib/utils/message-utils.ts b/lib/utils/message-utils.ts
index fa65b2c4..78793527 100644
--- a/lib/utils/message-utils.ts
+++ b/lib/utils/message-utils.ts
@@ -1,13 +1,5 @@
import { type Message as DBMessage } from '@/lib/db/schema'
-import { CoreMessage as SDKCoreMessage, UIMessage } from 'ai'
-
-// Type definition for the CoreMessage from AI SDK
-interface CoreMessage {
- role: string
- content: string | Array<{ type: string; text: string }> | null
- id?: string
- // Add other properties that might be in CoreMessage here
-}
+import { ModelMessage, UIMessage } from 'ai'
// Interface matching the expected DB message input format
interface DatabaseMessageInput {
@@ -21,7 +13,7 @@ interface DatabaseMessageInput {
* @returns Database-compatible message object
*/
export function convertMessageForDB(
- message: SDKCoreMessage
+ message: ModelMessage
): DatabaseMessageInput {
// Handle case where content might be a string, array, or null
let parts: any
@@ -60,7 +52,7 @@ export function convertMessageForDB(
* @returns Array of database-compatible message objects
*/
export function convertMessagesForDB(
- messages: SDKCoreMessage[]
+ messages: ModelMessage[]
): DatabaseMessageInput[] {
return messages.map(convertMessageForDB)
}
@@ -72,7 +64,7 @@ export function convertMessagesForDB(
* @returns Extracted title string, truncated to maxLength
*/
export function extractTitleFromMessage(
- message: SDKCoreMessage,
+ message: ModelMessage,
maxLength = 100
): string {
if (!message.content) return 'New Chat'
diff --git a/lib/utils/registry.ts b/lib/utils/registry.ts
index 42d08652..896ff546 100644
--- a/lib/utils/registry.ts
+++ b/lib/utils/registry.ts
@@ -18,9 +18,6 @@ export const registry = createProviderRegistry({
anthropic,
google,
groq,
- ollama: createOllama({
- baseURL: `${process.env.OLLAMA_BASE_URL}/api`
- }),
azure: createAzure({
apiKey: process.env.AZURE_API_KEY,
resourceName: process.env.AZURE_RESOURCE_NAME,
@@ -48,16 +45,6 @@ export function getModel(model: string) {
baseURL: `${process.env.OLLAMA_BASE_URL}/api`
})
- // if model is deepseek-r1, add reasoning middleware
- if (model.includes('deepseek-r1')) {
- return wrapLanguageModel({
- model: ollama(modelName),
- middleware: extractReasoningMiddleware({
- tagName: 'think'
- })
- })
- }
-
// if ollama provider, set simulateStreaming to true
return ollama(modelName, {
simulateStreaming: true