Skip to content

Commit

Permalink
fix(langgraph): allow complex content in ai messages (#983)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yonom authored Oct 13, 2024
1 parent 0a3bd06 commit ea90b84
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 41 deletions.
5 changes: 5 additions & 0 deletions .changeset/curvy-keys-joke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@assistant-ui/react-langgraph": patch
---

fix(langgraph): allow complex content in ai messages
62 changes: 25 additions & 37 deletions packages/react-langgraph/src/convertLangchainMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,29 @@ import { LangChainMessage } from "./types";
import { ToolCallContentPart } from "@assistant-ui/react";
import { ThreadUserMessage } from "@assistant-ui/react";

const contentToParts = (content: LangChainMessage["content"]) => {
if (typeof content === "string")
return [{ type: "text" as const, text: content }];
return content.map((part): ThreadUserMessage["content"][number] => {
switch (part.type) {
case "text":
return { type: "text", text: part.text };
case "image_url":
if (typeof part.image_url === "string") {
return { type: "image", image: part.image_url };
} else {
return {
type: "image",
image: part.image_url.url,
};
}
default:
const _exhaustiveCheck: never = part;
throw new Error(`Unknown content part type: ${_exhaustiveCheck}`);
}
});
};

export const convertLangchainMessages: useExternalMessageConverter.Callback<
LangChainMessage
> = (message) => {
Expand All @@ -16,52 +39,17 @@ export const convertLangchainMessages: useExternalMessageConverter.Callback<
content: [{ type: "text", text: message.content }],
};
case "human":
let content: ThreadUserMessage["content"];

if (typeof message.content === "string") {
content = [{ type: "text" as const, text: message.content }];
} else {
content = message.content.map((part) => {
if (typeof part === "string") {
return { type: "text", text: part };
} else {
const type = part.type;
switch (type) {
case "text":
return { type: "text" as const, text: part.text };

case "image_url":
return {
type: "image" as const,
image:
typeof part.image_url === "string"
? part.image_url
: part.image_url.url,
};
default:
const unhandledType: never = type;
throw new Error(
`Unhandled content part type: ${unhandledType}`,
);
}
}
});
}

return {
role: "user",
id: message.id,
content,
content: contentToParts(message.content),
};
case "ai":
return {
role: "assistant",
id: message.id,
content: [
{
type: "text",
text: message.content,
},
...contentToParts(message.content),
...(message.tool_calls?.map(
(chunk): ToolCallContentPart => ({
type: "tool-call",
Expand Down
10 changes: 6 additions & 4 deletions packages/react-langgraph/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ type MessageContentImageUrl = {
image_url: string | { url: string };
};

type MessageContentComplex = MessageContentText | MessageContentImageUrl;
type UserMessageContentComplex = MessageContentText | MessageContentImageUrl;
type AssistantMessageContentComplex = MessageContentText;

type MessageContent = string | MessageContentComplex[];
type UserMessageContent = string | UserMessageContentComplex[];
type AssistantMessageContent = string | AssistantMessageContentComplex[];

export type LangChainMessage =
| {
Expand All @@ -33,7 +35,7 @@ export type LangChainMessage =
| {
id?: string;
type: "human";
content: MessageContent;
content: UserMessageContent;
}
| {
id?: string;
Expand All @@ -45,7 +47,7 @@ export type LangChainMessage =
| {
id?: string;
type: "ai";
content: string;
content: AssistantMessageContent;
tool_call_chunks?: LangChainToolCallChunk[];
tool_calls?: LangChainToolCall[];
};
Expand Down

0 comments on commit ea90b84

Please sign in to comment.