File size: 6,163 Bytes
4343907
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// useAgentStatus Composable
// Manages agent lifecycle operations and status tracking

import { ref, computed, onMounted, onUnmounted, type Ref } from 'vue'
import { useWebSocket } from './useWebSocket'
import type { Agent, AgentStatus, WebSocketEvent } from '@/types'

interface AgentStatusComposable {
  // State
  isStarting: Ref<boolean>
  isStopping: Ref<boolean>
  isRestarting: Ref<boolean>
  isLoading: Ref<boolean>
  error: Ref<string | null>
  
  // Computed
  canStart: Ref<boolean>
  canStop: Ref<boolean>
  canRestart: Ref<boolean>
  
  // Actions
  startAgent: () => Promise<void>
  stopAgent: () => Promise<void>
  restartAgent: () => Promise<void>
  refreshStatus: () => Promise<void>
  clearError: () => void
}

export function useAgentStatus(agent: Agent): AgentStatusComposable {
  // State
  const isStarting = ref(false)
  const isStopping = ref(false)
  const isRestarting = ref(false)
  const error = ref<string | null>(null)
  
  // WebSocket for real-time updates
  const { connected, on, off } = useWebSocket()
  
  // Computed
  const isLoading = computed(() => 
    isStarting.value || isStopping.value || isRestarting.value
  )
  
  const canStart = computed(() => 
    agent.status === 'inactive' && !isLoading.value
  )
  
  const canStop = computed(() => 
    ['active', 'processing', 'idle'].includes(agent.status) && !isLoading.value
  )
  
  const canRestart = computed(() => 
    agent.status === 'error' && !isLoading.value
  )
  
  // API client
  async function apiCall(method: string, endpoint: string, data?: any) {
    const response = await fetch(`/api/v1${endpoint}`, {
      method,
      headers: {
        'Content-Type': 'application/json',
      },
      body: data ? JSON.stringify(data) : undefined,
    })
    
    if (!response.ok) {
      const errorData = await response.json().catch(() => ({}))
      throw new Error(errorData.message || `HTTP ${response.status}`)
    }
    
    return response.json()
  }
  
  // Actions
  async function startAgent(): Promise<void> {
    if (!canStart.value) return
    
    isStarting.value = true
    error.value = null
    
    try {
      await apiCall('POST', `/agents/${agent.id}/start`)
      // Status will be updated via WebSocket
    } catch (err) {
      error.value = err instanceof Error ? err.message : 'Failed to start agent'
      throw err
    } finally {
      isStarting.value = false
    }
  }
  
  async function stopAgent(): Promise<void> {
    if (!canStop.value) return
    
    isStopping.value = true
    error.value = null
    
    try {
      await apiCall('POST', `/agents/${agent.id}/stop`)
      // Status will be updated via WebSocket
    } catch (err) {
      error.value = err instanceof Error ? err.message : 'Failed to stop agent'
      throw err
    } finally {
      isStopping.value = false
    }
  }
  
  async function restartAgent(): Promise<void> {
    if (!canRestart.value) return
    
    isRestarting.value = true
    error.value = null
    
    try {
      await apiCall('POST', `/agents/${agent.id}/restart`)
      // Status will be updated via WebSocket
    } catch (err) {
      error.value = err instanceof Error ? err.message : 'Failed to restart agent'
      throw err
    } finally {
      isRestarting.value = false
    }
  }
  
  async function refreshStatus(): Promise<void> {
    try {
      const response = await apiCall('GET', `/agents/${agent.id}`)
      // Update agent status through parent component or store
      // This assumes the parent handles the agent object updates
      Object.assign(agent, response.data)
    } catch (err) {
      error.value = err instanceof Error ? err.message : 'Failed to refresh status'
      throw err
    }
  }
  
  function clearError(): void {
    error.value = null
  }
  
  // WebSocket event handlers
  function handleAgentStatusChanged(event: WebSocketEvent) {
    if (event.data.agentId === agent.id) {
      // Update agent status
      Object.assign(agent, event.data)
      
      // Clear loading states if status changed
      if (event.data.status !== agent.status) {
        isStarting.value = false
        isStopping.value = false
        isRestarting.value = false
      }
    }
  }
  
  // Lifecycle
  onMounted(() => {
    // Subscribe to WebSocket events
    on('agent_status_changed', handleAgentStatusChanged)
    
    // Initial status refresh if needed
    if (connected.value) {
      refreshStatus().catch(() => {
        // Silently handle initial refresh errors
      })
    }
  })
  
  onUnmounted(() => {
    // Unsubscribe from WebSocket events
    off('agent_status_changed', handleAgentStatusChanged)
  })
  
  return {
    // State
    isStarting,
    isStopping,
    isRestarting,
    isLoading,
    error,
    
    // Computed
    canStart,
    canStop,
    canRestart,
    
    // Actions
    startAgent,
    stopAgent,
    restartAgent,
    refreshStatus,
    clearError
  }
}

// Agent Status Store (optional Pinia store)
export interface AgentStatusState {
  agents: Record<string, Agent>
  loading: boolean
  error: string | null
}

// Helper functions
export function getAgentStatusColor(status: AgentStatus): string {
  const colorMap = {
    active: 'var(--saap-success)',
    inactive: 'var(--saap-neutral-400)',
    processing: 'var(--saap-warning)',
    error: 'var(--saap-error)',
    idle: 'var(--saap-info)'
  }
  
  return colorMap[status] || colorMap.inactive
}

export function getAgentTypeIcon(type: string): string {
  const iconMap = {
    generalist: 'brain',
    specialist: 'target',
    coordinator: 'network'
  }
  
  return iconMap[type as keyof typeof iconMap] || 'help-circle'
}

export function formatAgentUptime(timestamp: string): string {
  const start = new Date(timestamp)
  const now = new Date()
  const diff = now.getTime() - start.getTime()
  
  const days = Math.floor(diff / (1000 * 60 * 60 * 24))
  const hours = Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60))
  const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60))
  
  if (days > 0) {
    return `${days}d ${hours}h`
  }
  
  if (hours > 0) {
    return `${hours}h ${minutes}m`
  }
  
  return `${minutes}m`
}