Skip to content

Commit

Permalink
fix: typescript fixes (#448)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yonom authored Jul 10, 2024
1 parent b8fbb41 commit 69f2e31
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 60 deletions.
21 changes: 13 additions & 8 deletions packages/react-ai-sdk/src/core/VercelModelAdapter.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
// @ts-nocheck TOOD

"use client";

import type {
AssistantContentPart,
TextContentPart,
} from "@assistant-ui/react";
import type {
ChatModelAdapter,
ChatModelRunOptions,
} from "@assistant-ui/react";
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 {
Expand All @@ -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") {
Expand All @@ -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 });
Expand Down
5 changes: 2 additions & 3 deletions packages/react-ai-sdk/src/ui/utils/useVercelAIThreadSync.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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.`,
);
Expand Down
3 changes: 2 additions & 1 deletion packages/react/src/primitives/message/MessageContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ const MessageContentPartComponent: FC<MessageContentPartComponentProps> = ({
return <Tool part={part} status={status} addResult={addResult} />;
}
default:
throw new Error(`Unknown content part type: ${type}`);
const unhandledType: never = type;
throw new Error(`Unknown content part type: ${unhandledType}`);
}
};

Expand Down
40 changes: 15 additions & 25 deletions packages/react/src/runtimes/edge/createEdgeRuntimeAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
Expand Down Expand Up @@ -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}`);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
}
}
},
Expand Down
4 changes: 2 additions & 2 deletions packages/react/src/runtimes/edge/streams/assistantStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
}
}
},
Expand Down
4 changes: 2 additions & 2 deletions packages/react/src/runtimes/edge/streams/runResultStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
}
}
},
Expand Down
26 changes: 13 additions & 13 deletions packages/react/src/runtimes/edge/useEdgeRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@ import { runResultStream } from "./streams/runResultStream";
import { useLocalRuntime } from "..";
import { useMemo } from "react";

export type AsyncIterableStream<T> = AsyncIterable<T> & ReadableStream<T>;

export function asAsyncIterable<T>(
source: ReadableStream<T>,
): AsyncIterableStream<T> {
const ais: AsyncIterableStream<T> = source as AsyncIterableStream<T>;
ais[Symbol.asyncIterator] = () => {
const reader = source.getReader();
return {
async next(): Promise<IteratorResult<T, undefined>> {
const { done, value } = await reader.read();
return done ? { done: true, value: undefined } : { done: false, value };
},
};
): AsyncIterable<T> {
return {
[Symbol.asyncIterator]: () => {
const reader = source.getReader();
return {
async next(): Promise<IteratorResult<T, undefined>> {
const { done, value } = await reader.read();
return done
? { done: true, value: undefined }
: { done: false, value };
},
};
},
};
return ais;
}

type EdgeRuntimeOptions = { api: string };
Expand Down
2 changes: 0 additions & 2 deletions packages/react/src/ui/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// TODO figure out the export format

export {
ThreadConfigProvider,
useThreadConfig,
Expand Down

0 comments on commit 69f2e31

Please sign in to comment.