|
import axios from "axios"; |
|
import { getDefaultHeaders } from "./session"; |
|
|
|
|
|
const isHFSpace = window.location.hostname.includes("hf.space"); |
|
const API_URL = isHFSpace |
|
? "" |
|
: import.meta.env.VITE_API_URL || "http://localhost:8000"; |
|
|
|
|
|
const api = axios.create({ |
|
baseURL: API_URL, |
|
...(isHFSpace && { |
|
baseURL: window.location.origin, |
|
}), |
|
}); |
|
|
|
|
|
api.interceptors.request.use((config) => { |
|
|
|
const noSessionRoutes = [ |
|
"/api/universe/generate", |
|
"/api/generate-image", |
|
"/api/text-to-speech", |
|
]; |
|
|
|
if (noSessionRoutes.includes(config.url)) { |
|
return config; |
|
} |
|
|
|
|
|
if (!config.headers["x-session-id"]) { |
|
throw new Error("Session ID is required for this request"); |
|
} |
|
|
|
return config; |
|
}); |
|
|
|
|
|
const handleApiError = (error) => { |
|
console.error("API Error:", { |
|
status: error.response?.status, |
|
statusText: error.response?.statusText, |
|
data: error.response?.data, |
|
config: { |
|
method: error.config?.method, |
|
url: error.config?.url, |
|
data: error.config?.data, |
|
}, |
|
}); |
|
|
|
if (error.response) { |
|
|
|
throw new Error( |
|
error.response.data?.message || |
|
`Erreur ${error.response.status}: ${error.response.statusText}` |
|
); |
|
} else if (error.request) { |
|
|
|
throw new Error("Aucune réponse du serveur"); |
|
} else { |
|
|
|
throw new Error( |
|
"Une erreur est survenue lors de la configuration de la requête" |
|
); |
|
} |
|
}; |
|
|
|
|
|
export const storyApi = { |
|
start: async (sessionId) => { |
|
try { |
|
const response = await api.post( |
|
"/api/chat", |
|
{ |
|
message: "restart", |
|
}, |
|
{ |
|
headers: getDefaultHeaders(sessionId), |
|
} |
|
); |
|
return response.data; |
|
} catch (error) { |
|
return handleApiError(error); |
|
} |
|
}, |
|
|
|
makeChoice: async (choiceId, sessionId) => { |
|
try { |
|
const response = await api.post( |
|
"/api/chat", |
|
{ |
|
message: "choice", |
|
choice_id: choiceId, |
|
}, |
|
{ |
|
headers: getDefaultHeaders(sessionId), |
|
} |
|
); |
|
return response.data; |
|
} catch (error) { |
|
return handleApiError(error); |
|
} |
|
}, |
|
|
|
generateImage: async ( |
|
prompt, |
|
width = 512, |
|
height = 512, |
|
sessionId = null |
|
) => { |
|
try { |
|
const config = { |
|
prompt, |
|
width, |
|
height, |
|
}; |
|
|
|
const options = {}; |
|
if (sessionId) { |
|
options.headers = getDefaultHeaders(sessionId); |
|
} |
|
|
|
const response = await api.post("/api/generate-image", config, options); |
|
return response.data; |
|
} catch (error) { |
|
return handleApiError(error); |
|
} |
|
}, |
|
|
|
|
|
narrate: async (text, sessionId) => { |
|
try { |
|
const response = await api.post( |
|
"/api/text-to-speech", |
|
{ |
|
text, |
|
}, |
|
{ |
|
headers: getDefaultHeaders(sessionId), |
|
} |
|
); |
|
return response.data; |
|
} catch (error) { |
|
return handleApiError(error); |
|
} |
|
}, |
|
}; |
|
|
|
|
|
export const WS_URL = import.meta.env.VITE_WS_URL || "ws://localhost:8000/ws"; |
|
|
|
export const universeApi = { |
|
generate: async () => { |
|
try { |
|
const response = await api.post("/api/universe/generate"); |
|
return response.data; |
|
} catch (error) { |
|
return handleApiError(error); |
|
} |
|
}, |
|
}; |
|
|
|
|
|
export default api; |
|
|