11import { create } from 'zustand'
22import { persist } from 'zustand/middleware'
3+ import React from 'react'
34
45interface AgentConfig {
56 privateKey : string
@@ -18,22 +19,50 @@ interface AgentState {
1819 isInitialized : boolean
1920 isInitializing : boolean
2021
22+ // Session management
23+ sessionExpiry : number | null
24+
2125 // Actions
2226 setConfig : ( config : AgentConfig ) => void
2327 initializeAgent : ( ) => Promise < void >
2428 resetAgent : ( ) => void
2529 setInitializing : ( loading : boolean ) => void
30+ checkSession : ( ) => boolean
31+ restoreAgent : ( ) => Promise < void >
2632}
2733
2834export const useAgentStore = create < AgentState > ( ) (
2935 persist (
30- ( set : any , get : any ) => ( {
36+ ( set : any , get : any ) => {
37+ // Check session on store initialization
38+ const checkAndRestoreSession = ( ) => {
39+ const { sessionExpiry, isInitialized, config } = get ( )
40+ if ( sessionExpiry && isInitialized && config ) {
41+ const now = Date . now ( )
42+ if ( now > sessionExpiry ) {
43+ // Session expired, reset
44+ set ( {
45+ agentKit : null ,
46+ isInitialized : false ,
47+ sessionExpiry : null
48+ } )
49+ return false
50+ }
51+ // Session is valid, but we need to reinitialize the agentKit
52+ // since it can't be serialized
53+ return true
54+ }
55+ return false
56+ }
57+
58+ return {
3159 // Initial state
3260 config : null ,
3361 isConfigured : false ,
3462 agentKit : null ,
3563 isInitialized : false ,
3664 isInitializing : false ,
65+ sessionExpiry : null ,
3766
3867 // Actions
3968 setConfig : ( config : AgentConfig ) => {
@@ -47,6 +76,67 @@ export const useAgentStore = create<AgentState>()(
4776 set ( { isInitializing } )
4877 } ,
4978
79+ checkSession : ( ) => {
80+ const { sessionExpiry, isInitialized } = get ( )
81+ if ( ! sessionExpiry || ! isInitialized ) return false
82+
83+ const now = Date . now ( )
84+ if ( now > sessionExpiry ) {
85+ // Session expired, reset agent
86+ set ( {
87+ agentKit : null ,
88+ isInitialized : false ,
89+ sessionExpiry : null
90+ } )
91+ return false
92+ }
93+ return true
94+ } ,
95+
96+ restoreAgent : async ( ) => {
97+ const { config, sessionExpiry } = get ( )
98+ if ( ! config || ! sessionExpiry ) return
99+
100+ const now = Date . now ( )
101+ if ( now > sessionExpiry ) {
102+ // Session expired
103+ set ( {
104+ agentKit : null ,
105+ isInitialized : false ,
106+ sessionExpiry : null
107+ } )
108+ return
109+ }
110+
111+ try {
112+ set ( { isInitializing : true } )
113+
114+ const { PolkadotAgentKit } = await import ( "@polkadot-agent-kit/sdk" )
115+
116+ const agentKit = new PolkadotAgentKit ( {
117+ privateKey : config . privateKey ,
118+ keyType : config . keyType ,
119+ chains : config . chains as any ,
120+ } )
121+
122+ await agentKit . initializeApi ( )
123+
124+ set ( {
125+ agentKit,
126+ isInitialized : true ,
127+ isInitializing : false
128+ } )
129+
130+ console . log ( '[AgentStore] Agent restored successfully' )
131+ } catch ( error ) {
132+ console . error ( '[AgentStore] Failed to restore agent:' , error )
133+ set ( {
134+ isInitialized : false ,
135+ isInitializing : false
136+ } )
137+ }
138+ } ,
139+
50140 initializeAgent : async ( ) => {
51141 const { config } = get ( )
52142 if ( ! config || ! config . isConfigured ) {
@@ -66,10 +156,14 @@ export const useAgentStore = create<AgentState>()(
66156
67157 await agentKit . initializeApi ( )
68158
159+ // Set session expiry to 15 minutes from now
160+ const sessionExpiry = Date . now ( ) + ( 15 * 60 * 1000 ) // 15 minutes
161+
69162 set ( {
70163 agentKit,
71164 isInitialized : true ,
72- isInitializing : false
165+ isInitializing : false ,
166+ sessionExpiry
73167 } )
74168
75169 console . log ( '[AgentStore] Agent initialized successfully' )
@@ -89,16 +183,31 @@ export const useAgentStore = create<AgentState>()(
89183 isConfigured : false ,
90184 agentKit : null ,
91185 isInitialized : false ,
92- isInitializing : false
186+ isInitializing : false ,
187+ sessionExpiry : null
93188 } )
94189 }
190+ }
95191 } ) ,
96192 {
97193 name : 'polkadot-agent-store' ,
98194 partialize : ( state : any ) => ( {
99195 config : state . config ,
100- isConfigured : state . isConfigured
196+ isConfigured : state . isConfigured ,
197+ isInitialized : state . isInitialized ,
198+ sessionExpiry : state . sessionExpiry
101199 } )
102200 }
103201 )
104- )
202+
203+ // Hook to automatically restore agent on page load
204+ export const useAgentRestore = ( ) => {
205+ const { isInitialized, agentKit, restoreAgent, checkSession } = useAgentStore ( )
206+
207+ React . useEffect ( ( ) => {
208+ // Check if we have a valid session but no agent instance
209+ if ( checkSession ( ) && ! agentKit ) {
210+ restoreAgent ( )
211+ }
212+ } , [ ] )
213+ }
0 commit comments