11"use client" ;
22
3- import { useChat } from "@ai-sdk/react" ;
3+ import { type UseChatHelpers , useChat } from "@ai-sdk/react" ;
44import type { ThirdwebAiMessage } from "@thirdweb-dev/ai-sdk-provider" ;
5- import { DefaultChatTransport } from "ai" ;
5+ import {
6+ DefaultChatTransport ,
7+ lastAssistantMessageIsCompleteWithToolCalls ,
8+ } from "ai" ;
69import { useMemo , useState } from "react" ;
710import { defineChain , prepareTransaction } from "thirdweb" ;
811import {
@@ -31,30 +34,19 @@ import { Loader } from "../../../../components/loader";
3134import { THIRDWEB_CLIENT } from "../../../../lib/client" ;
3235
3336export function ChatContainer ( ) {
34- const [ sessionId , setSessionId ] = useState ( "" ) ;
35-
3637 const { messages, sendMessage, status, addToolResult } =
3738 useChat < ThirdwebAiMessage > ( {
3839 transport : new DefaultChatTransport ( {
3940 api : "/api/chat" ,
4041 } ) ,
41- onFinish : ( { message } ) => {
42- setSessionId ( message . metadata ?. session_id ?? "" ) ;
43- } ,
42+ sendAutomaticallyWhen : lastAssistantMessageIsCompleteWithToolCalls ,
4443 } ) ;
4544 const [ input , setInput ] = useState ( "" ) ;
4645
4746 const handleSubmit = ( e : React . FormEvent < HTMLFormElement > ) => {
4847 e . preventDefault ( ) ;
4948 if ( input . trim ( ) ) {
50- sendMessage (
51- { text : input } ,
52- {
53- body : {
54- sessionId,
55- } ,
56- } ,
57- ) ;
49+ sendMessage ( { text : input } ) ;
5850 setInput ( "" ) ;
5951 }
6052 } ;
@@ -101,7 +93,6 @@ export function ChatContainer() {
10193 addToolResult = { addToolResult }
10294 sendMessage = { sendMessage }
10395 toolCallId = { part . toolCallId }
104- sessionId = { sessionId }
10596 />
10697 ) ;
10798 case "tool-sign_swap" :
@@ -113,7 +104,6 @@ export function ChatContainer() {
113104 addToolResult = { addToolResult }
114105 sendMessage = { sendMessage }
115106 toolCallId = { part . toolCallId }
116- sessionId = { sessionId }
117107 />
118108 ) ;
119109 default :
@@ -158,14 +148,13 @@ type SignTransactionButtonProps = {
158148 { type : "tool-sign_transaction" }
159149 > [ "input" ]
160150 | undefined ;
161- addToolResult : ReturnType < typeof useChat < ThirdwebAiMessage > > [ "addToolResult" ] ;
151+ addToolResult : UseChatHelpers < ThirdwebAiMessage > [ "addToolResult" ] ;
152+ sendMessage : UseChatHelpers < ThirdwebAiMessage > [ "sendMessage" ] ;
162153 toolCallId : string ;
163- sendMessage : ReturnType < typeof useChat < ThirdwebAiMessage > > [ "sendMessage" ] ;
164- sessionId : string ;
165154} ;
166155
167156const SignTransactionButton = ( props : SignTransactionButtonProps ) => {
168- const { input, addToolResult, toolCallId, sendMessage, sessionId } = props ;
157+ const { input, addToolResult, toolCallId, sendMessage } = props ;
169158 const transactionData : {
170159 chain_id : number ;
171160 to : string ;
@@ -209,21 +198,9 @@ const SignTransactionButton = (props: SignTransactionButtonProps) => {
209198 chain_id : transaction . chain . id ,
210199 } ,
211200 } ) ;
212- sendMessage ( undefined , {
213- body : {
214- sessionId,
215- } ,
216- } ) ;
217201 } }
218202 onError = { ( error ) => {
219- sendMessage (
220- { text : `Transaction failed: ${ error . message } ` } ,
221- {
222- body : {
223- sessionId,
224- } ,
225- } ,
226- ) ;
203+ sendMessage ( { text : `Transaction failed: ${ error . message } ` } ) ;
227204 } }
228205 >
229206 Sign Transaction
@@ -241,13 +218,12 @@ type SignSwapButtonProps = {
241218 { type : "tool-sign_swap" }
242219 > [ "input" ]
243220 | undefined ;
244- addToolResult : ReturnType < typeof useChat < ThirdwebAiMessage > > [ "addToolResult" ] ;
221+ addToolResult : UseChatHelpers < ThirdwebAiMessage > [ "addToolResult" ] ;
222+ sendMessage : UseChatHelpers < ThirdwebAiMessage > [ "sendMessage" ] ;
245223 toolCallId : string ;
246- sendMessage : ReturnType < typeof useChat < ThirdwebAiMessage > > [ "sendMessage" ] ;
247- sessionId : string ;
248224} ;
249225const SignSwapButton = ( props : SignSwapButtonProps ) => {
250- const { input, addToolResult, toolCallId, sendMessage, sessionId } = props ;
226+ const { input, addToolResult, toolCallId, sendMessage } = props ;
251227 const transactionData : {
252228 chain_id : number ;
253229 to : string ;
@@ -293,21 +269,9 @@ const SignSwapButton = (props: SignSwapButtonProps) => {
293269 chain_id : transaction . chain . id ,
294270 } ,
295271 } ) ;
296- sendMessage ( undefined , {
297- body : {
298- sessionId,
299- } ,
300- } ) ;
301272 } }
302273 onError = { ( error ) => {
303- sendMessage (
304- { text : `Transaction failed: ${ error . message } ` } ,
305- {
306- body : {
307- sessionId,
308- } ,
309- } ,
310- ) ;
274+ sendMessage ( { text : `Transaction failed: ${ error . message } ` } ) ;
311275 } }
312276 >
313277 Sign swap
0 commit comments