@@ -3,6 +3,7 @@ import { useWallet } from '@solana/wallet-adapter-react'
33import TextMessage from './TextMessage'
44import ConfirmationPrompt , { type ConfirmationData , type ConfirmationStatus } from './ConfirmationPrompt'
55import QuickActions from './QuickActions'
6+ import { useTransactionSigner , type SignStatus } from '../hooks/useTransactionSigner'
67
78interface ChatMessage {
89 id : string
@@ -33,6 +34,7 @@ const API_URL = '/api/chat'
3334
3435export default function ChatContainer ( ) {
3536 const { connected, publicKey } = useWallet ( )
37+ const { signAndBroadcast } = useTransactionSigner ( )
3638 const [ messages , setMessages ] = useState < Message [ ] > ( [ ] )
3739 const [ input , setInput ] = useState ( '' )
3840 const [ loading , setLoading ] = useState ( false )
@@ -56,6 +58,17 @@ export default function ChatContainer() {
5658 setMessages ( prev => [ ...prev , msg ] )
5759 } , [ ] )
5860
61+ const updateConfirmation = useCallback ( ( confirmId : string , patch : Partial < ConfirmationData > ) => {
62+ setMessages ( prev =>
63+ prev . map ( msg => {
64+ if ( isConfirmation ( msg ) && msg . data . id === confirmId ) {
65+ return { ...msg , data : { ...msg . data , ...patch } }
66+ }
67+ return msg
68+ } )
69+ )
70+ } , [ ] )
71+
5972 const sendToAgent = useCallback ( async ( userText : string ) => {
6073 // Build conversation history for the API
6174 const chatHistory = messages
@@ -92,15 +105,17 @@ export default function ChatContainer() {
92105
93106 // If the response includes a confirmation request, render it
94107 if ( data . confirmation ) {
108+ const confirmId = generateId ( )
95109 addMessage ( {
96110 id : generateId ( ) ,
97111 type : 'confirmation' ,
98112 data : {
99- id : generateId ( ) ,
113+ id : confirmId ,
100114 action : data . confirmation . action ?? 'Transaction' ,
101115 amount : data . confirmation . amount ,
102116 fee : data . confirmation . fee ,
103117 recipient : data . confirmation . recipient ,
118+ serializedTx : data . confirmation . serializedTx ,
104119 status : 'pending' ,
105120 } ,
106121 timestamp : new Date ( ) ,
@@ -147,32 +162,51 @@ export default function ChatContainer() {
147162 }
148163
149164 const handleConfirm = ( id : string ) => {
150- setMessages ( prev =>
151- prev . map ( msg => {
152- if ( isConfirmation ( msg ) && msg . data . id === id ) {
153- return { ...msg , data : { ...msg . data , status : 'confirmed' as ConfirmationStatus } }
154- }
155- return msg
156- } )
157- )
158- // In a real implementation, this would trigger wallet signing via useWallet
165+ updateConfirmation ( id , { status : 'confirmed' as ConfirmationStatus } )
159166 addMessage ( {
160167 id : generateId ( ) ,
161168 role : 'agent' ,
162- content : 'Transaction confirmed. Waiting for wallet signature.. .' ,
169+ content : 'Transaction confirmed (no on-chain transaction for this action) .' ,
163170 timestamp : new Date ( ) ,
164171 } )
165172 }
166173
167- const handleCancel = ( id : string ) => {
168- setMessages ( prev =>
169- prev . map ( msg => {
170- if ( isConfirmation ( msg ) && msg . data . id === id ) {
171- return { ...msg , data : { ...msg . data , status : 'cancelled' as ConfirmationStatus } }
172- }
173- return msg
174+ const handleSign = useCallback ( async ( confirmId : string , serializedTx : string ) => {
175+ updateConfirmation ( confirmId , { signStatus : 'signing' as SignStatus } )
176+
177+ const result = await signAndBroadcast ( serializedTx )
178+
179+ if ( result . signature ) {
180+ updateConfirmation ( confirmId , {
181+ status : 'confirmed' ,
182+ signStatus : 'confirmed' ,
183+ signature : result . signature ,
174184 } )
175- )
185+ addMessage ( {
186+ id : generateId ( ) ,
187+ role : 'agent' ,
188+ content : `Transaction confirmed: ${ result . signature } ` ,
189+ timestamp : new Date ( ) ,
190+ } )
191+ } else {
192+ updateConfirmation ( confirmId , {
193+ signStatus : 'error' ,
194+ txError : result . error ?? 'Transaction failed' ,
195+ } )
196+ addMessage ( {
197+ id : generateId ( ) ,
198+ role : 'agent' ,
199+ content : `Transaction failed: ${ result . error } ` ,
200+ timestamp : new Date ( ) ,
201+ error : true ,
202+ } )
203+ }
204+
205+ return result
206+ } , [ signAndBroadcast , updateConfirmation , addMessage ] )
207+
208+ const handleCancel = ( id : string ) => {
209+ updateConfirmation ( id , { status : 'cancelled' as ConfirmationStatus } )
176210 addMessage ( {
177211 id : generateId ( ) ,
178212 role : 'agent' ,
@@ -205,6 +239,7 @@ export default function ChatContainer() {
205239 data = { msg . data }
206240 onConfirm = { handleConfirm }
207241 onCancel = { handleCancel }
242+ onSign = { handleSign }
208243 />
209244 ) : (
210245 < TextMessage
0 commit comments