11import { create } from 'zustand' ;
22import { subscribeWithSelector } from 'zustand/middleware' ;
3+ import type { StateCreator } from 'zustand' ;
34import { api } from '@/lib/api' ;
45import type { AgentRunWithMetrics } from '@/lib/api' ;
56
@@ -32,8 +33,12 @@ interface AgentState {
3233 pollingInterval : NodeJS . Timeout | null ;
3334}
3435
35- export const useAgentStore = create < AgentState > ( ) (
36- subscribeWithSelector ( ( set , get ) => ( {
36+ const agentStore : StateCreator <
37+ AgentState ,
38+ [ ] ,
39+ [ [ 'zustand/subscribeWithSelector' , never ] ] ,
40+ AgentState
41+ > = ( set , get ) => ( {
3742 // Initial state
3843 agentRuns : [ ] ,
3944 runningAgents : new Set ( ) ,
@@ -59,8 +64,8 @@ export const useAgentStore = create<AgentState>()(
5964 try {
6065 const runs = await api . listAgentRuns ( ) ;
6166 const runningIds = runs
62- . filter ( r => r . status === 'running' || r . status === 'pending' )
63- . map ( r => r . id ?. toString ( ) || '' )
67+ . filter ( ( r ) => r . status === 'running' || r . status === 'pending' )
68+ . map ( ( r ) => r . id ?. toString ( ) || '' )
6469 . filter ( Boolean ) ;
6570
6671 set ( {
@@ -83,7 +88,7 @@ export const useAgentStore = create<AgentState>()(
8388
8489 try {
8590 const output = await api . getAgentRunWithRealTimeMetrics ( runId ) . then ( run => run . output || '' ) ;
86- set ( state => ( {
91+ set ( ( state ) => ( {
8792 sessionOutputs : {
8893 ...state . sessionOutputs ,
8994 [ runId ] : output
@@ -107,7 +112,7 @@ export const useAgentStore = create<AgentState>()(
107112 const run = await api . getAgentRun ( runId ) ;
108113
109114 // Update local state immediately
110- set ( state => ( {
115+ set ( ( state ) => ( {
111116 agentRuns : [ run , ...state . agentRuns ] ,
112117 runningAgents : new Set ( [ ...state . runningAgents , runId . toString ( ) ] )
113118 } ) ) ;
@@ -127,8 +132,8 @@ export const useAgentStore = create<AgentState>()(
127132 await api . killAgentSession ( runId ) ;
128133
129134 // Update local state
130- set ( state => ( {
131- agentRuns : state . agentRuns . map ( r =>
135+ set ( ( state ) => ( {
136+ agentRuns : state . agentRuns . map ( ( r ) =>
132137 r . id === runId ? { ...r , status : 'cancelled' } : r
133138 ) ,
134139 runningAgents : new Set (
@@ -147,7 +152,7 @@ export const useAgentStore = create<AgentState>()(
147152 deleteAgentRun : async ( runId : number ) => {
148153 try {
149154 // First ensure the run is cancelled if it's still running
150- const run = get ( ) . agentRuns . find ( r => r . id === runId ) ;
155+ const run = get ( ) . agentRuns . find ( ( r ) => r . id === runId ) ;
151156 if ( run && ( run . status === 'running' || run . status === 'pending' ) ) {
152157 await api . killAgentSession ( runId ) ;
153158 }
@@ -156,8 +161,8 @@ export const useAgentStore = create<AgentState>()(
156161 // The run will remain in the database but won't be shown in the UI
157162
158163 // Update local state
159- set ( state => ( {
160- agentRuns : state . agentRuns . filter ( r => r . id !== runId ) ,
164+ set ( ( state ) => ( {
165+ agentRuns : state . agentRuns . filter ( ( r ) => r . id !== runId ) ,
161166 runningAgents : new Set (
162167 [ ...state . runningAgents ] . filter ( id => id !== runId . toString ( ) )
163168 ) ,
@@ -178,8 +183,8 @@ export const useAgentStore = create<AgentState>()(
178183
179184 // Handle real-time agent run updates
180185 handleAgentRunUpdate : ( run : AgentRunWithMetrics ) => {
181- set ( state => {
182- const existingIndex = state . agentRuns . findIndex ( r => r . id === run . id ) ;
186+ set ( ( state ) => {
187+ const existingIndex = state . agentRuns . findIndex ( ( r ) => r . id === run . id ) ;
183188 const updatedRuns = [ ...state . agentRuns ] ;
184189
185190 if ( existingIndex >= 0 ) {
@@ -189,8 +194,8 @@ export const useAgentStore = create<AgentState>()(
189194 }
190195
191196 const runningIds = updatedRuns
192- . filter ( r => r . status === 'running' || r . status === 'pending' )
193- . map ( r => r . id ?. toString ( ) || '' )
197+ . filter ( ( r ) => r . status === 'running' || r . status === 'pending' )
198+ . map ( ( r ) => r . id ?. toString ( ) || '' )
194199 . filter ( Boolean ) ;
195200
196201 return {
@@ -228,5 +233,8 @@ export const useAgentStore = create<AgentState>()(
228233 set ( { pollingInterval : null } ) ;
229234 }
230235 }
231- } ) )
236+ } ) ;
237+
238+ export const useAgentStore = create < AgentState > ( ) (
239+ subscribeWithSelector ( agentStore )
232240) ;
0 commit comments