File size: 6,945 Bytes
3e646d9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * JARVIS API Service
 * Centralized axios-based API calls to the FastAPI backend.
 */

import axios from 'axios'

// Base URL β€” uses Vite proxy in dev, env var in production
const BASE_URL = import.meta.env.VITE_API_URL || '/api'

const api = axios.create({
  baseURL: BASE_URL,
  timeout: 120_000,  // 2 min β€” model inference can be slow
})

// ── Request interceptor ────────────────────────────
api.interceptors.request.use(
  (config) => {
    // Could add auth headers here in future
    return config
  },
  (error) => Promise.reject(error),
)

// ── Response interceptor ───────────────────────────
api.interceptors.response.use(
  (res) => res,
  (error) => {
    const message =
      error.response?.data?.detail ||
      error.response?.data?.message ||
      error.message ||
      'Unknown error'
    return Promise.reject(new Error(message))
  },
)

// ───────────────────────────────────────────────────
// CHAT
// ───────────────────────────────────────────────────
export const chatAPI = {
  /**
   * Send a chat message and receive AI response.
   * @param {string} message
   * @param {string|null} sessionId
   * @param {string|null} imageCaption
   */
  sendMessage: async (message, sessionId = null, imageCaption = null) => {
    const res = await api.post('/chat/message', {
      message,
      session_id: sessionId,
      image_caption: imageCaption,
    })
    return res.data
  },

  /**
   * Get conversation history for a session.
   */
  getHistory: async (sessionId, limit = 50) => {
    const res = await api.get(`/chat/history/${sessionId}`, {
      params: { limit },
    })
    return res.data
  },
}

// ───────────────────────────────────────────────────
// IMAGE
// ───────────────────────────────────────────────────
export const imageAPI = {
  /**
   * Upload image file for analysis + optional question.
   * @param {File} file
   * @param {string|null} question
   * @param {string|null} sessionId
   */
  analyzeImage: async (file, question = null, sessionId = null) => {
    const formData = new FormData()
    formData.append('file', file)
    if (question)   formData.append('question', question)
    if (sessionId)  formData.append('session_id', sessionId)

    const res = await api.post('/image/analyze', formData, {
      headers: { 'Content-Type': 'multipart/form-data' },
      timeout: 180_000, // vision models can be slow
    })
    return res.data
  },

  /**
   * Quick caption only β€” no LLM.
   */
  quickCaption: async (file) => {
    const formData = new FormData()
    formData.append('file', file)
    const res = await api.post('/image/caption', formData, {
      headers: { 'Content-Type': 'multipart/form-data' },
    })
    return res.data
  },
}

// ───────────────────────────────────────────────────
// VOICE
// ───────────────────────────────────────────────────
export const voiceAPI = {
  /**
   * Transcribe audio Blob β†’ text.
   * @param {Blob} audioBlob
   * @param {string} language  optional ISO language code
   */
  transcribe: async (audioBlob, language = null) => {
    const formData = new FormData()
    formData.append('file', audioBlob, 'recording.webm')
    if (language) formData.append('language', language)

    const res = await api.post('/voice/transcribe', formData, {
      headers: { 'Content-Type': 'multipart/form-data' },
      timeout: 60_000,
    })
    return res.data
  },

  /**
   * Full voice round-trip: audio β†’ text β†’ AI β†’ audio.
   * @param {Blob} audioBlob
   * @param {string|null} sessionId
   * @param {boolean} ttsEnabled
   */
  voiceInteract: async (audioBlob, sessionId = null, ttsEnabled = true) => {
    const formData = new FormData()
    formData.append('file', audioBlob, 'recording.webm')
    if (sessionId) formData.append('session_id', sessionId)
    formData.append('tts_enabled', ttsEnabled.toString())

    const res = await api.post('/voice/interact', formData, {
      headers: { 'Content-Type': 'multipart/form-data' },
      timeout: 180_000,
    })
    return res.data
  },

  /**
   * Text-to-speech: returns audio blob.
   * @param {string} text
   */
  speak: async (text) => {
    const res = await api.post(
      '/voice/speak',
      { text },
      { responseType: 'blob', timeout: 60_000 },
    )
    return res.data  // audio/wav Blob
  },
}

// ───────────────────────────────────────────────────
// TOOLS
// ───────────────────────────────────────────────────
export const toolsAPI = {
  /**
   * Execute a tool directly.
   * @param {string} toolName
   * @param {string} argument
   */
  executeTool: async (toolName, argument) => {
    const res = await api.post('/tools/execute', {
      tool_name: toolName,
      argument,
    })
    return res.data
  },

  listTools: async () => {
    const res = await api.get('/tools/list')
    return res.data
  },
}

// ───────────────────────────────────────────────────
// SESSIONS
// ───────────────────────────────────────────────────
export const sessionsAPI = {
  listSessions: async () => {
    const res = await api.get('/sessions/')
    return res.data
  },

  deleteSession: async (sessionId) => {
    const res = await api.delete(`/sessions/${sessionId}`)
    return res.data
  },

  renameSession: async (sessionId, title) => {
    const res = await api.patch(`/sessions/${sessionId}`, { title })
    return res.data
  },
}

// ───────────────────────────────────────────────────
// HEALTH
// ───────────────────────────────────────────────────
export const healthAPI = {
  check: async () => {
    const res = await api.get('/health')
    return res.data
  },
}

export default api