88} from "@assistant-ui/react" ;
99import { v4 as uuidv4 } from "uuid" ;
1010import { useExternalMessageConverter } from "@assistant-ui/react" ;
11- import type { Message } from "@langchain/langgraph-sdk " ;
11+ import { BaseMessage , HumanMessage } from "@langchain/core/messages " ;
1212import { useToast } from "../hooks/use-toast" ;
1313import {
1414 convertToOpenAIFormat ,
@@ -25,7 +25,7 @@ function ChatLangChainComponent(): React.ReactElement {
2525 const { toast } = useToast ( ) ;
2626 const { threadsData, userData, graphData } = useGraphContext ( ) ;
2727 const { userId } = userData ;
28- const { getUserThreads, getThreadById } = threadsData ;
28+ const { getUserThreads, createThread , getThreadById } = threadsData ;
2929 const { messages, setMessages, streamMessage, switchSelectedThread } =
3030 graphData ;
3131 const [ isRunning , setIsRunning ] = useState ( false ) ;
@@ -73,16 +73,29 @@ function ChatLangChainComponent(): React.ReactElement {
7373
7474 setIsRunning ( true ) ;
7575
76+ let currentThreadId = threadId ;
77+ if ( ! currentThreadId ) {
78+ const thread = await createThread ( userId ) ;
79+ if ( ! thread ) {
80+ toast ( {
81+ title : "Error" ,
82+ description : "Thread creation failed." ,
83+ } ) ;
84+ return ;
85+ }
86+ setThreadId ( thread . thread_id ) ;
87+ currentThreadId = thread . thread_id ;
88+ }
89+
7690 try {
77- const humanMessage : Message = {
78- type : "human" ,
91+ const humanMessage = new HumanMessage ( {
7992 content : message . content [ 0 ] . text ,
8093 id : uuidv4 ( ) ,
81- } ;
94+ } ) ;
8295
8396 setMessages ( ( prevMessages ) => [ ...prevMessages , humanMessage ] ) ;
8497
85- await streamMessage ( threadId ?? undefined , {
98+ await streamMessage ( currentThreadId , {
8699 messages : [ convertToOpenAIFormat ( humanMessage ) ] ,
87100 } ) ;
88101 } finally {
@@ -92,9 +105,9 @@ function ChatLangChainComponent(): React.ReactElement {
92105 }
93106 }
94107
95- const threadMessages = useExternalMessageConverter < Message > ( {
108+ const threadMessages = useExternalMessageConverter < BaseMessage > ( {
96109 callback : convertLangchainMessages ,
97- messages,
110+ messages : messages ,
98111 isRunning,
99112 } ) ;
100113
0 commit comments