Skip to content

Commit b4057b8

Browse files
committed
fix(apps): add session agent kit api
1 parent 06c59cf commit b4057b8

File tree

3 files changed

+122
-7
lines changed

3 files changed

+122
-7
lines changed

apps/playground/app/chat/page.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { Badge } from "@/components/ui/badge"
77
import { ScrollArea } from "@/components/ui/scroll-area"
88
import { Send, Bot } from "lucide-react"
99
import Sidebar from "@/components/sidebar"
10-
import { useAgentStore } from "@/stores/agent-store"
10+
import { useAgentStore, useAgentRestore } from "@/stores/agent-store"
1111

1212

1313
interface ChatMessage {
@@ -35,6 +35,9 @@ export default function ChatPage() {
3535

3636
const [chatMessages, setChatMessages] = useState<ChatMessage[]>([])
3737

38+
// Restore agent session on page load
39+
useAgentRestore()
40+
3841
// Initialize client-side state
3942
useEffect(() => {
4043
setIsClient(true)

apps/playground/app/developer/page.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
CollapsibleTrigger,
1717
} from "@/components/ui/collapsible"
1818
import { ChevronDown } from "lucide-react"
19-
import { useAgentStore } from "@/stores/agent-store"
19+
import { useAgentStore, useAgentRestore } from "@/stores/agent-store"
2020

2121
type ToolLike = { name?: string; description?: string; schema?: any; schemaJson?: any; call: (args: any) => Promise<any> }
2222
type EndpointKey = "assets" | "swap" | "bifrost" | "staking"
@@ -49,6 +49,9 @@ export default function DeveloperPage() {
4949
const [formData, setFormData] = useState<Record<string, any>>({})
5050
const [parsedSchema, setParsedSchema] = useState<any>(null)
5151

52+
// Restore agent session on page load
53+
useAgentRestore()
54+
5255
// Initialize tools when agentKit is available
5356
useEffect(() => {
5457
const initializeTools = async () => {

apps/playground/stores/agent-store.ts

Lines changed: 114 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { create } from 'zustand'
22
import { persist } from 'zustand/middleware'
3+
import React from 'react'
34

45
interface 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

2834
export 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

Comments
 (0)