123 lines
3.9 KiB
JavaScript
123 lines
3.9 KiB
JavaScript
import axios from 'axios'
|
|
|
|
// 创建 axios 实例
|
|
const api = axios.create({
|
|
baseURL: '/api',
|
|
timeout: 30000,
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
}
|
|
})
|
|
|
|
// 请求拦截器:添加认证 token
|
|
api.interceptors.request.use(
|
|
(config) => {
|
|
const token = localStorage.getItem('access_token')
|
|
if (token) {
|
|
config.headers.Authorization = `Bearer ${token}`
|
|
}
|
|
return config
|
|
},
|
|
(error) => {
|
|
return Promise.reject(error)
|
|
}
|
|
)
|
|
|
|
// 响应拦截器:处理通用错误
|
|
api.interceptors.response.use(
|
|
(response) => response.data,
|
|
(error) => {
|
|
if (error.response?.status === 401) {
|
|
localStorage.removeItem('access_token')
|
|
localStorage.removeItem('user')
|
|
// 使用 Vue Router 跳转,避免 SPA 路由丢失
|
|
if (window.__VUE_ROUTER__) {
|
|
window.__VUE_ROUTER__.push('/auth')
|
|
} else {
|
|
window.location.href = '/auth'
|
|
}
|
|
}
|
|
return Promise.reject(error.response?.data || error.message)
|
|
}
|
|
)
|
|
|
|
// ============ 认证接口 ============
|
|
|
|
export const authAPI = {
|
|
login: (data) => api.post('/auth/login', data),
|
|
register: (data) => api.post('/auth/register', data),
|
|
logout: () => api.post('/auth/logout'),
|
|
getMe: () => api.get('/auth/me'),
|
|
listUsers: () => api.get('/auth/users'),
|
|
updateUserPermission: (userId, data) => api.put(`/auth/users/${userId}`, data),
|
|
getSettings: () => api.get('/auth/settings'),
|
|
updateSettings: (data) => api.put('/auth/settings', data)
|
|
}
|
|
|
|
// ============ 会话接口 ============
|
|
|
|
export const conversationsAPI = {
|
|
list: (params) => api.get('/conversations/', { params }),
|
|
create: (data) => api.post('/conversations/', data),
|
|
get: (id) => api.get(`/conversations/${id}`),
|
|
update: (id, data) => api.put(`/conversations/${id}`, data),
|
|
delete: (id) => api.delete(`/conversations/${id}`)
|
|
}
|
|
|
|
// ============ 消息接口 ============
|
|
|
|
export const messagesAPI = {
|
|
list: (conversationId, params) => api.get('/messages/', { params: { conversation_id: conversationId, ...params } }),
|
|
send: (data) => api.post('/messages/', data),
|
|
delete: (id) => api.delete(`/messages/${id}`)
|
|
}
|
|
|
|
// ============ 工具接口 ============
|
|
|
|
export const toolsAPI = {
|
|
list: (params) => api.get('/tools/', { params }),
|
|
get: (name) => api.get(`/tools/${name}`),
|
|
execute: (name, data) => api.post(`/tools/${name}/execute`, data)
|
|
}
|
|
|
|
// ============ LLM Provider 接口 ============
|
|
|
|
export const providersAPI = {
|
|
list: () => api.get('/providers/'),
|
|
create: (data) => api.post('/providers/', data),
|
|
get: (id) => api.get(`/providers/${id}`),
|
|
update: (id, data) => api.put(`/providers/${id}`, data),
|
|
delete: (id) => api.delete(`/providers/${id}`),
|
|
test: (id) => api.post(`/providers/${id}/test`)
|
|
}
|
|
|
|
// ============ Agent 接口 ============
|
|
|
|
export const agentsAPI = {
|
|
list: () => api.get('/agents/'),
|
|
create: (data) => api.post('/agents/', data),
|
|
get: (id) => api.get(`/agents/${id}`),
|
|
update: (id, data) => api.put(`/agents/${id}`, data),
|
|
delete: (id) => api.delete(`/agents/${id}`)
|
|
}
|
|
|
|
// ============ 聊天室接口 ============
|
|
|
|
export const chatRoomsAPI = {
|
|
list: (params) => api.get('/chat-rooms/', { params }),
|
|
create: (data) => api.post('/chat-rooms/', data),
|
|
get: (id) => api.get(`/chat-rooms/${id}`),
|
|
update: (id, data) => api.put(`/chat-rooms/${id}`, data),
|
|
delete: (id) => api.delete(`/chat-rooms/${id}`),
|
|
getMessages: (id) => api.get(`/chat-rooms/${id}/messages`),
|
|
start: (id) => `/api/chat-rooms/${id}/start`,
|
|
// 注意: start 返回路径字符串,由调用方使用 fetch 处理 SSE 流
|
|
stop: (id) => api.post(`/chat-rooms/${id}/stop`),
|
|
reset: (id) => api.post(`/chat-rooms/${id}/reset`),
|
|
addAgent: (roomId, data) => api.post(`/chat-rooms/${roomId}/agents`, data),
|
|
updateAgent: (roomId, agentId, data) => api.put(`/chat-rooms/${roomId}/agents/${agentId}`, data),
|
|
deleteAgent: (roomId, agentId) => api.delete(`/chat-rooms/${roomId}/agents/${agentId}`)
|
|
}
|
|
|
|
export default api
|