Skip to content

An AI-powered search engine with a generative UI

fix: resolve TypeScript errors after AI SDK v5 migration

Yoshiki Miurac85a9c960123
diff --git a/components/artifact/retrieve-artifact-content.tsx b/components/artifact/retrieve-artifact-content.tsx
index 474012ac..ec05bbfe 100644
--- a/components/artifact/retrieve-artifact-content.tsx
+++ b/components/artifact/retrieve-artifact-content.tsx
@@ -15,7 +15,7 @@ const MAX_CONTENT_LENGTH = 1000
export function RetrieveArtifactContent({ tool }: { tool: ToolPart<'retrieve'> }) {
const searchResults: TypeSearchResults | undefined =
- tool.state === 'output-available' ? tool.output : undefined
+ tool.state === 'output-available' ? (tool.output ?? undefined) : undefined
const url = tool.input?.url
if (!searchResults?.results) {
diff --git a/components/artifact/search-artifact-content.tsx b/components/artifact/search-artifact-content.tsx
index 8ce14cb5..fb049f8d 100644
--- a/components/artifact/search-artifact-content.tsx
+++ b/components/artifact/search-artifact-content.tsx
@@ -8,7 +8,7 @@ import { SearchResultsImageSection } from '@/components/search-results-image'
import { Section, ToolArgsSection } from '@/components/section'
export function SearchArtifactContent({ tool }: { tool: ToolPart<'search'> }) {
- const searchResults: TypeSearchResults =
+ const searchResults: TypeSearchResults | undefined =
tool.state === 'output-available' ? tool.output : undefined
const query = tool.input?.query
diff --git a/components/artifact/tool-invocation-content.tsx b/components/artifact/tool-invocation-content.tsx
index 4790e1db..c2372d82 100644
--- a/components/artifact/tool-invocation-content.tsx
+++ b/components/artifact/tool-invocation-content.tsx
@@ -13,11 +13,11 @@ export function ToolInvocationContent({
}) {
switch (part.type) {
case 'tool-search':
- return <SearchArtifactContent tool={part} />
+ return <SearchArtifactContent tool={part as ToolPart<'search'>} />
case 'tool-retrieve':
- return <RetrieveArtifactContent tool={part} />
+ return <RetrieveArtifactContent tool={part as ToolPart<'retrieve'>} />
case 'tool-videoSearch':
- return <VideoSearchArtifactContent tool={part} />
+ return <VideoSearchArtifactContent tool={part as ToolPart<'videoSearch'>} />
default:
return <div className="p-4">Details for this tool are not available</div>
}
diff --git a/components/chat-messages.tsx b/components/chat-messages.tsx
index 2380f1a1..4469726e 100644
--- a/components/chat-messages.tsx
+++ b/components/chat-messages.tsx
@@ -4,7 +4,6 @@ import { useState } from 'react'
import { UseChatHelpers } from '@ai-sdk/react'
import type { UIMessage, UIDataTypes, UITools } from '@/lib/types/ai'
-import { UIMessage } from 'ai'
import { cn } from '@/lib/utils'
diff --git a/components/chat.tsx b/components/chat.tsx
index d34ceb35..03a8617d 100644
--- a/components/chat.tsx
+++ b/components/chat.tsx
@@ -50,7 +50,7 @@ export function Chat({
regenerate,
addToolResult
} = useChat<UIMessage<unknown, UIDataTypes, UITools>>({
- url: '/api/chat',
+ api: '/api/chat',
body: {
chatId: id
},
@@ -327,7 +327,9 @@ export function Chat({
onQuerySelect={onQuerySelect}
status={status}
chatId={id}
- addToolResult={addToolResult}
+ addToolResult={({ toolCallId, result }: { toolCallId: string; result: any }) => {
+ addToolResult({ toolCallId, output: result })
+ }}
scrollContainerRef={scrollContainerRef}
onUpdateMessage={handleUpdateAndReloadMessage}
reload={handleReloadFrom}
diff --git a/components/question-confirmation.tsx b/components/question-confirmation.tsx
index a678796b..875b76aa 100644
--- a/components/question-confirmation.tsx
+++ b/components/question-confirmation.tsx
@@ -21,12 +21,26 @@ interface QuestionOption {
label: string
}
+interface QuestionInput {
+ question: string
+ options: QuestionOption[]
+ allowsInput?: boolean
+ inputLabel?: string
+ inputPlaceholder?: string
+}
+
+interface QuestionOutput {
+ selectedOptions?: string[]
+ inputText?: string
+ skipped?: boolean
+}
+
export function QuestionConfirmation({
toolInvocation,
onConfirm,
isCompleted = false
}: QuestionConfirmationProps) {
- const { question, options, allowsInput, inputLabel, inputPlaceholder } =
+ const { question = '', options = [], allowsInput = false, inputLabel = '', inputPlaceholder = '' } =
toolInvocation.input || {}
// Get result data if available
@@ -78,23 +92,26 @@ export function QuestionConfirmation({
// Get options to display (from result or local state)
const getDisplayedOptions = (): string[] => {
- if (resultData && Array.isArray(resultData.selectedOptions)) {
- return resultData.selectedOptions
+ const result = resultData as QuestionOutput | null
+ if (result && Array.isArray(result.selectedOptions)) {
+ return result.selectedOptions
}
return selectedOptions
}
// Get input text to display (from result or local state)
const getDisplayedInputText = (): string => {
- if (resultData && resultData.inputText) {
- return resultData.inputText
+ const result = resultData as QuestionOutput | null
+ if (result && result.inputText) {
+ return result.inputText
}
return inputText
}
// Check if question was skipped
const wasSkipped = (): boolean => {
- if (resultData && resultData.skipped) {
+ const result = resultData as QuestionOutput | null
+ if (result && result.skipped) {
return true
}
return skipped
diff --git a/components/render-message.tsx b/components/render-message.tsx
index df739601..d03b69db 100644
--- a/components/render-message.tsx
+++ b/components/render-message.tsx
@@ -35,7 +35,7 @@ export function RenderMessage({
if (message.role === 'user') {
return (
<>
- {message.parts?.map((part, index) => {
+ {message.parts?.map((part: any, index: number) => {
switch (part.type) {
case 'text':
return (
@@ -67,10 +67,10 @@ export function RenderMessage({
return (
<>
- {message.parts?.map((part, index) => {
+ {message.parts?.map((part: any, index: number) => {
// Check if this is the last text part in the array
const textParts =
- message.parts?.filter(part => part.type === 'text') || []
+ message.parts?.filter((part: any) => part.type === 'text') || []
const isLastTextPart =
part.type === 'text' &&
textParts.indexOf(part) === textParts.length - 1
diff --git a/components/retrieve-section.tsx b/components/retrieve-section.tsx
index 2d8331b8..38e1f8fd 100644
--- a/components/retrieve-section.tsx
+++ b/components/retrieve-section.tsx
@@ -29,7 +29,7 @@ export function RetrieveSection({
const isChatLoading = status === 'submitted' || status === 'streaming'
const isLoading = isToolLoading || isChatLoading
- const data: SearchResultsType =
+ const data: SearchResultsType | undefined =
tool.state === 'output-available' ? tool.output : undefined
const url = tool.input?.url
diff --git a/components/search-section.tsx b/components/search-section.tsx
index 7f35fb0d..36a99da4 100644
--- a/components/search-section.tsx
+++ b/components/search-section.tsx
@@ -29,7 +29,7 @@ export function SearchSection({
const isLoading = status === 'submitted' || status === 'streaming'
const isToolLoading = tool.state === 'input-streaming' || tool.state === 'input-available'
- const searchResults: TypeSearchResults =
+ const searchResults: TypeSearchResults | undefined =
tool.state === 'output-available' ? tool.output : undefined
const query = tool.input?.query
const includeDomains = tool.input?.include_domains
diff --git a/components/tool-section.tsx b/components/tool-section.tsx
index 27bb72b9..de4530cf 100644
--- a/components/tool-section.tsx
+++ b/components/tool-section.tsx
@@ -89,7 +89,7 @@ export function ToolSection({
status={status}
/>
)
- case 'tool-relatedQuestions':
+ case 'tool-relatedQuestions' as any:
return (
<RelatedQuestions
tool={tool}
diff --git a/lib/streaming/create-chat-stream-response.ts b/lib/streaming/create-chat-stream-response.ts
index f9e27fab..b2cd880f 100644
--- a/lib/streaming/create-chat-stream-response.ts
+++ b/lib/streaming/create-chat-stream-response.ts
@@ -53,7 +53,7 @@ export async function createChatStreamResponse(
}
const previousMessages = await getChatMessages(chatId)
- const messagesToModel = [...previousMessages, message]
+ const messagesToModel = [...previousMessages as any[], message]
const result = researcher({
messages: convertToModelMessages(messagesToModel),
@@ -66,7 +66,7 @@ export async function createChatStreamResponse(
writer.merge(
result.toUIMessageStream({
- id: generateUUID(),
+ generateId: () => generateUUID(),
experimental_sendFinish: false,
onFinish: ({ responseMessage }) => {
// Store the messages for later use
@@ -81,7 +81,7 @@ export async function createChatStreamResponse(
)
writer.merge(
relatedQuestions.toUIMessageStream({
- id: generateUUID(),
+ generateId: () => generateUUID(),
onFinish: ({ responseMessage }) => {
// If there is a first response message, merge it with the new message
if (firstResponseMessage) {
diff --git a/lib/tools/search.ts b/lib/tools/search.ts
index ca3efaf9..85071309 100644
--- a/lib/tools/search.ts
+++ b/lib/tools/search.ts
@@ -110,7 +110,7 @@ export async function search(
includeDomains: string[] = [],
excludeDomains: string[] = []
): Promise<SearchResults> {
- return searchTool.execute(
+ return searchTool.execute?.(
{
query,
max_results: maxResults,
@@ -122,5 +122,5 @@ export async function search(
toolCallId: 'search',
messages: []
}
- )
+ ) ?? Promise.resolve({ results: [], images: [], query, number_of_results: 0 })
}
diff --git a/lib/types/ai.ts b/lib/types/ai.ts
index de4f79d1..d7cd0299 100644
--- a/lib/types/ai.ts
+++ b/lib/types/ai.ts
@@ -1,10 +1,10 @@
-import type { Message, InferUITool } from 'ai'
+import type { UIMessage as AIMessage, InferUITool } from 'ai'
import { searchTool } from '@/lib/tools/search'
import { retrieveTool } from '@/lib/tools/retrieve'
import { videoSearchTool } from '@/lib/tools/video-search'
import { askQuestionTool } from '@/lib/tools/question'
-export type UIMessage<TMetadata = unknown, TDataTypes = UIDataTypes, TTools = UITools> = Message
+export type UIMessage<TMetadata = unknown, TDataTypes = UIDataTypes, TTools = UITools> = AIMessage
export type UIDataTypes = {
sources?: any[]