Skip to content

Commit

Permalink
feat: add fromCoreMessages converter (#477)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yonom authored Jul 12, 2024
1 parent d106515 commit 1808e67
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 15 deletions.
4 changes: 2 additions & 2 deletions packages/react/src/runtimes/edge/EdgeChatAdapter.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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"],
Expand Down
17 changes: 17 additions & 0 deletions packages/react/src/runtimes/edge/converters/fromCoreMessage.ts
Original file line number Diff line number Diff line change
@@ -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;
});
};
11 changes: 0 additions & 11 deletions packages/react/src/runtimes/edge/converters/toCoreMessage.ts

This file was deleted.

13 changes: 13 additions & 0 deletions packages/react/src/runtimes/edge/converters/toCoreMessages.ts
Original file line number Diff line number Diff line change
@@ -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;
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/runtimes/edge/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

0 comments on commit 1808e67

Please sign in to comment.