From 69f2e31525b71f66707bdf075d25758a2e51721c Mon Sep 17 00:00:00 2001 From: Simon Farshid Date: Wed, 10 Jul 2024 14:42:29 -0700 Subject: [PATCH] fix: typescript fixes (#448) --- .../src/core/VercelModelAdapter.tsx | 21 ++++++---- .../src/ui/utils/useVercelAIThreadSync.tsx | 5 +-- .../src/primitives/message/MessageContent.tsx | 3 +- .../src/runtimes/edge/createEdgeRuntimeAPI.ts | 40 +++++++------------ .../edge/streams/assistantDecoderStream.ts | 4 +- .../edge/streams/assistantEncoderStream.ts | 4 +- .../runtimes/edge/streams/assistantStream.ts | 4 +- .../runtimes/edge/streams/runResultStream.ts | 4 +- .../react/src/runtimes/edge/useEdgeRuntime.ts | 26 ++++++------ packages/react/src/ui/index.ts | 2 - 10 files changed, 53 insertions(+), 60 deletions(-) diff --git a/packages/react-ai-sdk/src/core/VercelModelAdapter.tsx b/packages/react-ai-sdk/src/core/VercelModelAdapter.tsx index 3c18a7739..502d5ca0b 100644 --- a/packages/react-ai-sdk/src/core/VercelModelAdapter.tsx +++ b/packages/react-ai-sdk/src/core/VercelModelAdapter.tsx @@ -1,11 +1,5 @@ -// @ts-nocheck TOOD - "use client"; -import type { - AssistantContentPart, - TextContentPart, -} from "@assistant-ui/react"; import type { ChatModelAdapter, ChatModelRunOptions, @@ -13,6 +7,7 @@ import type { import type { ToolCallContentPart } from "@assistant-ui/react"; import { type CoreTool, type LanguageModel, streamText } from "ai"; import { convertToCoreMessage } from "./convertToCoreMessage"; +import { CoreAssistantContentPart } from "@assistant-ui/react"; // TODO multiple roundtrip support export class VercelModelAdapter implements ChatModelAdapter { @@ -31,9 +26,10 @@ export class VercelModelAdapter implements ChatModelAdapter { : {}), }); - const content: AssistantContentPart[] = []; + const content: CoreAssistantContentPart[] = []; for await (const aiPart of fullStream) { - switch (aiPart.type) { + const partType = aiPart.type; + switch (partType) { case "text-delta": { let part = content.at(-1); if (!part || part.type !== "text") { @@ -58,23 +54,32 @@ export class VercelModelAdapter implements ChatModelAdapter { break; } + // @ts-expect-error case "tool-result": { const toolCall = content.findIndex( + // @ts-expect-error (c) => c.type === "tool-call" && c.toolCallId === aiPart.toolCallId, ); if (toolCall === -1) { throw new Error( + // @ts-expect-error `Tool call ${aiPart.toolCallId} not found in the content stream. This is likely an internal bug in assistant-ui.`, ); } content[toolCall] = { ...(content[toolCall] as ToolCallContentPart), + // @ts-expect-error result: aiPart.result, }; break; } + + default: { + const unhandledType: "error" | "finish" = partType; + throw new Error(`Unknown content part type: ${unhandledType}`); + } } onUpdate({ content }); diff --git a/packages/react-ai-sdk/src/ui/utils/useVercelAIThreadSync.tsx b/packages/react-ai-sdk/src/ui/utils/useVercelAIThreadSync.tsx index 2f2fb3265..18b8ee1a9 100644 --- a/packages/react-ai-sdk/src/ui/utils/useVercelAIThreadSync.tsx +++ b/packages/react-ai-sdk/src/ui/utils/useVercelAIThreadSync.tsx @@ -35,8 +35,7 @@ const vercelToThreadMessage = ( [symbolInnerAIMessage]: messages, }; - const role = firstMessage.role; - switch (role) { + switch (firstMessage.role) { case "user": if (messages.length > 1) { throw new Error( @@ -106,7 +105,7 @@ const vercelToThreadMessage = ( } default: - const _unsupported: "function" | "tool" = role; + const _unsupported: "function" | "tool" = firstMessage.role; throw new Error( `You have a message with an unsupported role. The role ${_unsupported} is not supported.`, ); diff --git a/packages/react/src/primitives/message/MessageContent.tsx b/packages/react/src/primitives/message/MessageContent.tsx index 86687af8c..d29490b3a 100644 --- a/packages/react/src/primitives/message/MessageContent.tsx +++ b/packages/react/src/primitives/message/MessageContent.tsx @@ -93,7 +93,8 @@ const MessageContentPartComponent: FC = ({ return ; } default: - throw new Error(`Unknown content part type: ${type}`); + const unhandledType: never = type; + throw new Error(`Unknown content part type: ${unhandledType}`); } }; diff --git a/packages/react/src/runtimes/edge/createEdgeRuntimeAPI.ts b/packages/react/src/runtimes/edge/createEdgeRuntimeAPI.ts index c134ab273..53046e504 100644 --- a/packages/react/src/runtimes/edge/createEdgeRuntimeAPI.ts +++ b/packages/react/src/runtimes/edge/createEdgeRuntimeAPI.ts @@ -106,29 +106,19 @@ export function convertToLanguageModelMessage( case "user": { const msg: LanguageModelV1Message = { role: "user", - content: - // TODO testing - typeof message.content === "string" - ? [ - { - type: "text", - text: message.content, - }, - ] - : message.content.map((part): LanguageModelV1TextPart => { - switch (part.type) { - case "text": { - return part; - } - - // TODO support image parts - default: { - throw new Error( - `Unspported content part type: ${part.type}`, - ); - } - } - }), + content: message.content.map((part): LanguageModelV1TextPart => { + switch (part.type) { + case "text": { + return part; + } + + // TODO support image parts + default: { + const unhandledType: "image" = part.type; + throw new Error(`Unspported content part type: ${unhandledType}`); + } + } + }), }; return [msg]; } @@ -204,8 +194,8 @@ export function convertToLanguageModelMessage( } default: { - const _exhaustiveCheck: never = role; - throw new Error(`Invalid message role: ${_exhaustiveCheck}`); + const unhandledRole: never = role; + throw new Error(`Unknown message role: ${unhandledRole}`); } } } diff --git a/packages/react/src/runtimes/edge/streams/assistantDecoderStream.ts b/packages/react/src/runtimes/edge/streams/assistantDecoderStream.ts index 0e4ce4fb3..dbc72f5f7 100644 --- a/packages/react/src/runtimes/edge/streams/assistantDecoderStream.ts +++ b/packages/react/src/runtimes/edge/streams/assistantDecoderStream.ts @@ -41,8 +41,8 @@ export function assistantDecoderStream() { break; } default: { - const _exhaustiveCheck: never = code; - throw new Error(`Unhandled chunk type: ${_exhaustiveCheck}`); + const unhandledType: never = code; + throw new Error(`Unhandled chunk type: ${unhandledType}`); } } }, diff --git a/packages/react/src/runtimes/edge/streams/assistantEncoderStream.ts b/packages/react/src/runtimes/edge/streams/assistantEncoderStream.ts index d2826abab..cf1f10dc2 100644 --- a/packages/react/src/runtimes/edge/streams/assistantEncoderStream.ts +++ b/packages/react/src/runtimes/edge/streams/assistantEncoderStream.ts @@ -45,8 +45,8 @@ export function assistantEncoderStream() { break; } default: { - const _exhaustiveCheck: never = chunkType; - throw new Error(`Unhandled chunk type: ${_exhaustiveCheck}`); + const unhandledType: never = chunkType; + throw new Error(`Unhandled chunk type: ${unhandledType}`); } } }, diff --git a/packages/react/src/runtimes/edge/streams/assistantStream.ts b/packages/react/src/runtimes/edge/streams/assistantStream.ts index 8b5628934..477c0e487 100644 --- a/packages/react/src/runtimes/edge/streams/assistantStream.ts +++ b/packages/react/src/runtimes/edge/streams/assistantStream.ts @@ -27,8 +27,8 @@ export function assistantStream() { } default: { - const _exhaustiveCheck: never = chunkType; - throw new Error(`Unhandled chunk type: ${_exhaustiveCheck}`); + const unhandledType: never = chunkType; + throw new Error(`Unhandled chunk type: ${unhandledType}`); } } }, diff --git a/packages/react/src/runtimes/edge/streams/runResultStream.ts b/packages/react/src/runtimes/edge/streams/runResultStream.ts index 615f01e33..9000af798 100644 --- a/packages/react/src/runtimes/edge/streams/runResultStream.ts +++ b/packages/react/src/runtimes/edge/streams/runResultStream.ts @@ -38,8 +38,8 @@ export function runResultStream() { throw chunk.error; } default: { - const _exhaustiveCheck: never = chunkType; - throw new Error(`Unhandled chunk type: ${_exhaustiveCheck}`); + const unhandledType: never = chunkType; + throw new Error(`Unhandled chunk type: ${unhandledType}`); } } }, diff --git a/packages/react/src/runtimes/edge/useEdgeRuntime.ts b/packages/react/src/runtimes/edge/useEdgeRuntime.ts index 4a6b03f61..6f52a071a 100644 --- a/packages/react/src/runtimes/edge/useEdgeRuntime.ts +++ b/packages/react/src/runtimes/edge/useEdgeRuntime.ts @@ -5,22 +5,22 @@ import { runResultStream } from "./streams/runResultStream"; import { useLocalRuntime } from ".."; import { useMemo } from "react"; -export type AsyncIterableStream = AsyncIterable & ReadableStream; - export function asAsyncIterable( source: ReadableStream, -): AsyncIterableStream { - const ais: AsyncIterableStream = source as AsyncIterableStream; - ais[Symbol.asyncIterator] = () => { - const reader = source.getReader(); - return { - async next(): Promise> { - const { done, value } = await reader.read(); - return done ? { done: true, value: undefined } : { done: false, value }; - }, - }; +): AsyncIterable { + return { + [Symbol.asyncIterator]: () => { + const reader = source.getReader(); + return { + async next(): Promise> { + const { done, value } = await reader.read(); + return done + ? { done: true, value: undefined } + : { done: false, value }; + }, + }; + }, }; - return ais; } type EdgeRuntimeOptions = { api: string }; diff --git a/packages/react/src/ui/index.ts b/packages/react/src/ui/index.ts index 86ebe642f..3fad797b6 100644 --- a/packages/react/src/ui/index.ts +++ b/packages/react/src/ui/index.ts @@ -1,5 +1,3 @@ -// TODO figure out the export format - export { ThreadConfigProvider, useThreadConfig,