File size: 6,798 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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/**
 * SAAP API Composable - Backend Integration
 * Centralized API service for SAAP FastAPI Backend
 */

import { ref, reactive } from 'vue'
import axios, { type AxiosResponse, type AxiosError } from 'axios'

// Types
interface SaapAgent {
  id: string
  name: string
  type: 'coordinator' | 'developer' | 'specialist' | 'analyst'
  color?: string
  avatar?: string
  description: string
  status: 'active' | 'inactive' | 'starting' | 'error'
  capabilities?: string[]
  personality?: {
    system_prompt?: string
    communication_style?: string
  }
  llm_config?: {
    model?: string
    temperature?: number
    max_tokens?: number
  }
  metrics?: {
    messages_processed?: number
    average_response_time?: number
    last_active?: string
    uptime?: string
    error_rate?: number
  }
  created_at?: string
  updated_at?: string
  tags?: string[]
}

interface ChatMessage {
  agent_id: string
  agent_name?: string
  user_message: string
  agent_response: string
  response_time?: number
  timestamp: string
  model?: string
}

interface SystemStatus {
  system: string
  status: string
  agents: {
    total: number
    active: number
    inactive: number
  }
  active_agents: Array<{
    id: string
    name: string
    type: string
  }>
  timestamp: string
}

// API Configuration
const API_BASE_URL = 'http://localhost:8000'

const api = axios.create({
  baseURL: API_BASE_URL,
  timeout: 30000,
  headers: {
    'Content-Type': 'application/json',
  },
})

// Request/Response interceptors
api.interceptors.request.use(
  (config) => {
    console.log(`🌐 API Request: ${config.method?.toUpperCase()} ${config.url}`)
    return config
  },
  (error) => {
    console.error('πŸ”₯ API Request Error:', error)
    return Promise.reject(error)
  }
)

api.interceptors.response.use(
  (response) => {
    console.log(`βœ… API Response: ${response.config.url} (${response.status})`)
    return response
  },
  (error: AxiosError) => {
    console.error(`❌ API Error: ${error.config?.url}`, error.response?.data)
    return Promise.reject(error)
  }
)

export const useApi = () => {
  const loading = ref(false)
  const error = ref<string | null>(null)
  
  // Helper function to handle API calls
  const handleApiCall = async <T>(
    apiCall: () => Promise<AxiosResponse<T>>,
    loadingState: boolean = true
  ): Promise<T | null> => {
    try {
      if (loadingState) loading.value = true
      error.value = null
      
      const response = await apiCall()
      return response.data
    } catch (err: any) {
      const errorMessage = err.response?.data?.detail || err.message || 'API Error'
      error.value = errorMessage
      console.error('API Error:', errorMessage)
      return null
    } finally {
      if (loadingState) loading.value = false
    }
  }

  // =====================================================
  // SYSTEM STATUS API
  // =====================================================
  
  const getSystemStatus = async (): Promise<SystemStatus | null> => {
    return handleApiCall(async () => {
      return await api.get<SystemStatus>('/api/v1/health')
    })
  }

  const getRootStatus = async () => {
    return handleApiCall(async () => {
      return await api.get('/')
    })
  }

  // =====================================================
  // AGENT MANAGEMENT API
  // =====================================================
  
  const getAgents = async (): Promise<SaapAgent[] | null> => {
    return handleApiCall(async () => {
      return await api.get<SaapAgent[]>('/api/v1/agents')
    })
  }

  const getAgent = async (agentId: string): Promise<SaapAgent | null> => {
    return handleApiCall(async () => {
      return await api.get<SaapAgent>(`/api/v1/agents/${agentId}`)
    })
  }

  const createAgent = async (agentData: Partial<SaapAgent>): Promise<any> => {
    return handleApiCall(async () => {
      return await api.post('/api/v1/agents', agentData)
    })
  }

  const updateAgent = async (agentId: string, agentData: Partial<SaapAgent>): Promise<any> => {
    return handleApiCall(async () => {
      return await api.put(`/api/v1/agents/${agentId}`, agentData)
    })
  }

  const deleteAgent = async (agentId: string): Promise<any> => {
    return handleApiCall(async () => {
      return await api.delete(`/api/v1/agents/${agentId}`)
    })
  }

  // =====================================================
  // AGENT LIFECYCLE API
  // =====================================================
  
  const startAgent = async (agentId: string): Promise<any> => {
    return handleApiCall(async () => {
      return await api.post(`/api/v1/agents/${agentId}/start`)
    })
  }

  const stopAgent = async (agentId: string): Promise<any> => {
    return handleApiCall(async () => {
      return await api.post(`/api/v1/agents/${agentId}/stop`)
    })
  }

  // =====================================================
  // AGENT COMMUNICATION API
  // =====================================================
  
  const chatWithAgent = async (agentId: string, message: string): Promise<ChatMessage | null> => {
    return handleApiCall(async () => {
      return await api.post<ChatMessage>(`/api/v1/agents/${agentId}/chat`, {
        message: message
      })
    })
  }

  // =====================================================
  // AGENT TEMPLATES API
  // =====================================================
  
  const getAgentTemplates = async (): Promise<any> => {
    return handleApiCall(async () => {
      return await api.get('/api/v1/templates/agents')
    })
  }

  const createAgentFromTemplate = async (templateName: string): Promise<any> => {
    return handleApiCall(async () => {
      return await api.post(`/api/v1/templates/agents/${templateName}`)
    })
  }

  // =====================================================
  // HEALTH & TESTING
  // =====================================================
  
  const testConnection = async (): Promise<boolean> => {
    try {
      loading.value = true
      const response = await api.get('/api/v1/health', { timeout: 5000 })
      return response.status === 200
    } catch (err) {
      console.error('❌ Backend connection failed:', err)
      return false
    } finally {
      loading.value = false
    }
  }

  return {
    // State
    loading,
    error,
    
    // System
    getSystemStatus,
    getRootStatus,
    testConnection,
    
    // Agent Management
    getAgents,
    getAgent,
    createAgent,
    updateAgent,
    deleteAgent,
    
    // Agent Lifecycle
    startAgent,
    stopAgent,
    
    // Communication
    chatWithAgent,
    
    // Templates
    getAgentTemplates,
    createAgentFromTemplate,
    
    // Configuration
    API_BASE_URL,
  }
}

// Export types for use in components
export type { SaapAgent, ChatMessage, SystemStatus }