From 1808e6719b03cc680aa3e5346c4d7594cd142842 Mon Sep 17 00:00:00 2001 From: Simon Farshid Date: Fri, 12 Jul 2024 12:53:23 -0700 Subject: [PATCH] feat: add fromCoreMessages converter (#477) --- .../react/src/runtimes/edge/EdgeChatAdapter.ts | 4 ++-- .../runtimes/edge/converters/fromCoreMessage.ts | 17 +++++++++++++++++ .../runtimes/edge/converters/toCoreMessage.ts | 11 ----------- .../runtimes/edge/converters/toCoreMessages.ts | 13 +++++++++++++ .../edge/converters/toLanguageModelMessages.ts | 2 +- packages/react/src/runtimes/edge/index.ts | 2 +- 6 files changed, 34 insertions(+), 15 deletions(-) create mode 100644 packages/react/src/runtimes/edge/converters/fromCoreMessage.ts delete mode 100644 packages/react/src/runtimes/edge/converters/toCoreMessage.ts create mode 100644 packages/react/src/runtimes/edge/converters/toCoreMessages.ts diff --git a/packages/react/src/runtimes/edge/EdgeChatAdapter.ts b/packages/react/src/runtimes/edge/EdgeChatAdapter.ts index 2461fef4f..dee7fc207 100644 --- a/packages/react/src/runtimes/edge/EdgeChatAdapter.ts +++ b/packages/react/src/runtimes/edge/EdgeChatAdapter.ts @@ -1,6 +1,6 @@ import { ChatModelAdapter, ChatModelRunOptions } from "../local"; import { ChatModelRunResult } from "../local/ChatModelAdapter"; -import { toCoreMessage } from "./converters/toCoreMessage"; +import { toCoreMessages } from "./converters/toCoreMessages"; import { toLanguageModelTools } from "./converters/toLanguageModelTools"; import { EdgeRuntimeRequestOptions } from "./EdgeRuntimeRequestOptions"; import { assistantDecoderStream } from "./streams/assistantDecoderStream"; @@ -38,7 +38,7 @@ export class EdgeChatAdapter implements ChatModelAdapter { }, body: JSON.stringify({ system: config.system, - messages: messages.map(toCoreMessage), + messages: toCoreMessages(messages), tools: toLanguageModelTools( config.tools, ) as EdgeRuntimeRequestOptions["tools"], diff --git a/packages/react/src/runtimes/edge/converters/fromCoreMessage.ts b/packages/react/src/runtimes/edge/converters/fromCoreMessage.ts new file mode 100644 index 000000000..32e2e0789 --- /dev/null +++ b/packages/react/src/runtimes/edge/converters/fromCoreMessage.ts @@ -0,0 +1,17 @@ +import { generateId } from "../../../internal"; +import { ThreadMessage, CoreMessage } from "../../../types"; + +export const fromCoreMessages = (message: CoreMessage[]): ThreadMessage[] => { + return message.map((message) => { + return { + ...message, + id: generateId(), + createdAt: new Date(), + ...(message.role === "assistant" + ? { + status: { type: "done" }, + } + : undefined), + } as ThreadMessage; + }); +}; diff --git a/packages/react/src/runtimes/edge/converters/toCoreMessage.ts b/packages/react/src/runtimes/edge/converters/toCoreMessage.ts deleted file mode 100644 index 48cd7bc2e..000000000 --- a/packages/react/src/runtimes/edge/converters/toCoreMessage.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { ThreadMessage, CoreMessage } from "../../../types"; - -export const toCoreMessage = (message: ThreadMessage): CoreMessage => { - return { - role: message.role, - content: message.content.map((part) => { - if (part.type === "ui") throw new Error("UI parts are not supported"); - return part; - }), - } as CoreMessage; -}; diff --git a/packages/react/src/runtimes/edge/converters/toCoreMessages.ts b/packages/react/src/runtimes/edge/converters/toCoreMessages.ts new file mode 100644 index 000000000..8a2175920 --- /dev/null +++ b/packages/react/src/runtimes/edge/converters/toCoreMessages.ts @@ -0,0 +1,13 @@ +import { ThreadMessage, CoreMessage } from "../../../types"; + +export const toCoreMessages = (message: ThreadMessage[]): CoreMessage[] => { + return message.map((message) => { + return { + role: message.role, + content: message.content.map((part) => { + if (part.type === "ui") throw new Error("UI parts are not supported"); + return part; + }), + } as CoreMessage; + }); +}; diff --git a/packages/react/src/runtimes/edge/converters/toLanguageModelMessages.ts b/packages/react/src/runtimes/edge/converters/toLanguageModelMessages.ts index 8b367830d..f1cbf5c1e 100644 --- a/packages/react/src/runtimes/edge/converters/toLanguageModelMessages.ts +++ b/packages/react/src/runtimes/edge/converters/toLanguageModelMessages.ts @@ -69,7 +69,7 @@ const assistantMessageSplitter = () => { }; export function toLanguageModelMessages( - message: CoreMessage[] | ThreadMessage[], + message: readonly CoreMessage[] | readonly ThreadMessage[], ): LanguageModelV1Message[] { return message.flatMap((message) => { const role = message.role; diff --git a/packages/react/src/runtimes/edge/index.ts b/packages/react/src/runtimes/edge/index.ts index cb4849d5d..6887e33ec 100644 --- a/packages/react/src/runtimes/edge/index.ts +++ b/packages/react/src/runtimes/edge/index.ts @@ -4,4 +4,4 @@ export type { EdgeRuntimeOptions } from "./EdgeChatAdapter"; export { toLanguageModelMessages } from "./converters/toLanguageModelMessages"; export { fromLanguageModelMessages } from "./converters/fromLanguageModelMessages"; -export { toCoreMessage } from "./converters/toCoreMessage"; +export { fromCoreMessages } from "./converters/fromCoreMessage";