136 lines
3.2 KiB
JavaScript
136 lines
3.2 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')
|
|
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')
|
|
}
|
|
|
|
// ============ 会话接口 ============
|
|
|
|
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),
|
|
|
|
// 发送消息(流式)- 返回 EventSource 或使用 fetch
|
|
sendStream: (data) => {
|
|
const token = localStorage.getItem('access_token')
|
|
return fetch('/api/messages/stream', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': `Bearer ${token}`
|
|
},
|
|
body: JSON.stringify(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`)
|
|
}
|
|
|
|
// 默认导出
|
|
export default api |