Skip to content

Commit

Permalink
feat(langgraph): image attachment support (#998)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yonom authored Oct 14, 2024
1 parent 07a4f91 commit 7930c2d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
6 changes: 6 additions & 0 deletions packages/react-langgraph/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @assistant-ui/react-langgraph

## 0.0.19

### Patch Changes

- feat(langgraph): image attachment support

## 0.0.18

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/react-langgraph/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@assistant-ui/react-langgraph",
"version": "0.0.18",
"version": "0.0.19",
"license": "MIT",
"exports": {
".": {
Expand Down
35 changes: 31 additions & 4 deletions packages/react-langgraph/src/useLangGraphRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
} from "@assistant-ui/react";
import { convertLangchainMessages } from "./convertLangchainMessages";
import { useLangGraphMessages } from "./useLangGraphMessages";
import { SimpleImageAttachmentAdapter } from "@assistant-ui/react";

const getPendingToolCalls = (messages: LangChainMessage[]) => {
const pendingToolCalls = new Map<string, LangChainToolCall>();
Expand All @@ -26,12 +27,14 @@ const getPendingToolCalls = (messages: LangChainMessage[]) => {
export const useLangGraphRuntime = ({
threadId,
autoCancelPendingToolCalls,
unstable_allowImageAttachments,
stream,
onSwitchToNewThread,
onSwitchToThread,
}: {
threadId?: string | undefined;
autoCancelPendingToolCalls?: boolean | undefined;
unstable_allowImageAttachments?: boolean | undefined;
stream: (messages: LangChainMessage[]) => Promise<
AsyncGenerator<{
event: string;
Expand Down Expand Up @@ -69,10 +72,12 @@ export const useLangGraphRuntime = ({
threadId,
isRunning,
messages: threadMessages,
adapters: {
attachments: unstable_allowImageAttachments
? new SimpleImageAttachmentAdapter()
: undefined,
},
onNew: (msg) => {
if (msg.content.length !== 1 || msg.content[0]?.type !== "text")
throw new Error("Only text messages are supported");

const cancellations =
autoCancelPendingToolCalls !== false
? getPendingToolCalls(messages).map(
Expand All @@ -86,11 +91,33 @@ export const useLangGraphRuntime = ({
)
: [];

const allContent = [
...msg.content,
...(msg.attachments?.flatMap((a) => a.content) ?? []),
];

return handleSendMessage([
...cancellations,
{
type: "human",
content: msg.content[0].text,
content: allContent.map((part) => {
const type = part.type;
switch (type) {
case "text":
return { type: "text", text: part.text };
case "image":
return { type: "image_url", image_url: part.image };

case "tool-call":
throw new Error("Tool call appends are not supported yet.");

default:
const _exhaustiveCheck: never = type;
throw new Error(
`Unknown content part type: ${_exhaustiveCheck}`,
);
}
}),
},
]);
},
Expand Down

0 comments on commit 7930c2d

Please sign in to comment.