An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
refactor: address Copilot type safety review comments
Yoshiki Miura33d265f2f5dd
diff --git a/components/chat.tsx b/components/chat.tsxindex 204796bf..bab37337 100644--- a/components/chat.tsx+++ b/components/chat.tsx@@ -10,6 +10,11 @@ import { z } from 'zod'import { generateId } from '@/lib/db/schema'import { UploadedFile } from '@/lib/types'import type { UIDataTypes, UIMessage, UITools } from '@/lib/types/ai'+import {+ isDynamicToolPart,+ isToolCallPart,+ isToolTypePart+} from '@/lib/types/dynamic-tools'import { Model } from '@/lib/types/models'import { cn } from '@/lib/utils'@@ -297,25 +302,28 @@ export function Chat({}) => {// Find the tool name from the message partslet toolName = 'unknown'- const allParts = messages.flatMap(m => m.parts || [])-- for (const part of allParts) {- const p = part as any- if (p.type === 'tool-call' && p.toolCallId === toolCallId) {- toolName = p.toolName- break- } else if (- p.type?.startsWith('tool-') &&- p.toolCallId === toolCallId- ) {- toolName = p.type.substring(5) // Remove 'tool-' prefix- break- } else if (- p.type === 'dynamic-tool' &&- p.toolCallId === toolCallId- ) {- toolName = p.toolName- break++ // Optimize by breaking early once found+ outerLoop: for (const message of messages) {+ if (!message.parts) continue++ for (const part of message.parts) {+ if (isToolCallPart(part) && part.toolCallId === toolCallId) {+ toolName = part.toolName+ break outerLoop+ } else if (+ isToolTypePart(part) &&+ part.toolCallId === toolCallId+ ) {+ toolName = part.type.substring(5) // Remove 'tool-' prefix+ break outerLoop+ } else if (+ isDynamicToolPart(part) &&+ part.toolCallId === toolCallId+ ) {+ toolName = part.toolName+ break outerLoop+ }}}diff --git a/components/render-message.tsx b/components/render-message.tsxindex aefd638d..b28cd51a 100644--- a/components/render-message.tsx+++ b/components/render-message.tsx@@ -1,6 +1,7 @@import { UseChatHelpers } from '@ai-sdk/react'import type { UIDataTypes, UIMessage, UITools } from '@/lib/types/ai'+import type { DynamicToolPart } from '@/lib/types/dynamic-tools'import { AnswerSection } from './answer-section'import { DynamicToolDisplay } from './dynamic-tool-display'@@ -101,7 +102,7 @@ export function RenderMessage({return (<DynamicToolDisplaykey={`${messageId}-dynamic-tool-${index}`}- part={part as any}+ part={part as DynamicToolPart}/>)case 'text':diff --git a/lib/agents/researcher-with-dynamic-tools.ts b/lib/agents/researcher-with-dynamic-tools.tsindex a86c5ec9..dc95671d 100644--- a/lib/agents/researcher-with-dynamic-tools.ts+++ b/lib/agents/researcher-with-dynamic-tools.ts@@ -45,12 +45,7 @@ Citation Format:[number](url)`-interface DynamicToolConfig {- name: string- description: string- handler?: (params: any) => Promise<any>- mcpClient?: any // Replace with actual MCP client type-}+import type { DynamicToolConfig } from '@/lib/types/dynamic-tools'export function researcherWithDynamicTools({model,diff --git a/lib/tools/dynamic.ts b/lib/tools/dynamic.tsindex 3b5879a0..d725fb76 100644--- a/lib/tools/dynamic.ts+++ b/lib/tools/dynamic.ts@@ -1,6 +1,8 @@import { dynamicTool } from 'ai'import { z } from 'zod'+import type { MCPClient } from '@/lib/types/dynamic-tools'+/*** Creates a dynamic tool that can be used for runtime-defined tools* such as MCP tools or user-defined functions@@ -32,7 +34,7 @@ export function createDynamicTool(export function createMCPTool(toolName: string,description: string,- mcpClient: any // Replace with actual MCP client type+ mcpClient: MCPClient) {return createDynamicTool(`mcp__${toolName}`, description, async input => {// Execute the MCP tool@@ -46,7 +48,7 @@ export function createMCPTool(export function createCustomTool(name: string,description: string,- handler: (params: any) => Promise<any>+ handler: (params: unknown) => Promise<unknown>) {return createDynamicTool(`dynamic__${name}`, description, handler)}diff --git a/lib/types/dynamic-tools.ts b/lib/types/dynamic-tools.tsnew file mode 100644index 00000000..a5990ba6--- /dev/null+++ b/lib/types/dynamic-tools.ts@@ -0,0 +1,88 @@+/**+ * Type definitions for dynamic tools+ */++// MCP Client interface+export interface MCPClient {+ connect(): Promise<void>+ disconnect(): Promise<void>+ callTool(toolName: string, input: unknown): Promise<unknown>+ sendRequest(request: object): Promise<object>+}++// Dynamic tool configuration+export interface DynamicToolConfig {+ name: string+ description: string+ handler?: (params: unknown) => Promise<unknown>+ mcpClient?: MCPClient+}++// Dynamic tool part types matching AI SDK v5+export type DynamicToolPart =+ | DynamicToolPartInputStreaming+ | DynamicToolPartInputAvailable+ | DynamicToolPartOutputAvailable+ | DynamicToolPartOutputError++interface DynamicToolPartBase {+ type: 'dynamic-tool'+ toolCallId: string+ toolName: string+}++export interface DynamicToolPartInputStreaming extends DynamicToolPartBase {+ state: 'input-streaming'+ input: unknown+}++export interface DynamicToolPartInputAvailable extends DynamicToolPartBase {+ state: 'input-available'+ input: unknown+}++export interface DynamicToolPartOutputAvailable extends DynamicToolPartBase {+ state: 'output-available'+ input: unknown+ output: unknown+}++export interface DynamicToolPartOutputError extends DynamicToolPartBase {+ state: 'output-error'+ input: unknown+ errorText: string+}++// Type guards+export function isDynamicToolPart(part: unknown): part is DynamicToolPart {+ return (+ typeof part === 'object' &&+ part !== null &&+ 'type' in part &&+ part.type === 'dynamic-tool'+ )+}++export function isToolCallPart(+ part: unknown+): part is { type: 'tool-call'; toolCallId: string; toolName: string } {+ return (+ typeof part === 'object' &&+ part !== null &&+ 'type' in part &&+ part.type === 'tool-call'+ )+}++export function isToolTypePart(+ part: unknown+): part is { type: string; toolCallId: string } {+ return (+ typeof part === 'object' &&+ part !== null &&+ 'type' in part &&+ 'toolCallId' in part &&+ typeof part.type === 'string' &&+ part.type.startsWith('tool-')+ )+}diff --git a/lib/utils/message-mapping.ts b/lib/utils/message-mapping.tsindex 4c27d0a2..d46036e9 100644--- a/lib/utils/message-mapping.ts+++ b/lib/utils/message-mapping.ts@@ -1,5 +1,6 @@import { generateId } from '@/lib/db/schema'import type { UIDataTypes, UIMessage, UITools } from '@/lib/types/ai'+import type { DynamicToolPart } from '@/lib/types/dynamic-tools'import type {DBMessagePart,DBMessagePartSelect,@@ -160,13 +161,13 @@ export function mapUIMessagePartsToDBParts(const toolName = getToolNameFromType(part.toolName)const toolInputColumn = `tool_${toolName}_input` as keyof DBMessagePart- const result: any = {+ const result = {...basePart,type: `tool-${toolName}`,tool_toolCallId: part.toolCallId,tool_state: 'input-available' as ToolState,[toolInputColumn]: part.args- }+ } as DBMessagePart// Store additional metadata for dynamic toolsif (toolName === 'dynamic') {@@ -186,7 +187,7 @@ export function mapUIMessagePartsToDBParts(const toolOutputColumn =`tool_${resultToolName}_output` as keyof DBMessagePart- const toolResult: any = {+ const toolResult = {...basePart,type: `tool-${resultToolName}`,tool_toolCallId: part.toolCallId,@@ -195,7 +196,7 @@ export function mapUIMessagePartsToDBParts(: ('output-available' as ToolState),tool_errorText: part.isError ? String(part.result) : undefined,[toolOutputColumn]: !part.isError ? part.result : undefined- }+ } as DBMessagePart// Preserve dynamic tool metadata from the corresponding tool-callif (resultToolName === 'dynamic') {@@ -224,7 +225,7 @@ export function mapUIMessagePartsToDBParts(// Dynamic tool parts from AI SDK v5case 'dynamic-tool':- const dynamicPart = part as any+ const dynamicPart = part as DynamicToolPartreturn {...basePart,type: 'tool-dynamic',