import http from './axios'
|
|
/**
|
* 上传文件,chatId 为空表示公共知识库文件。
|
*
|
* @param {File} file
|
* @param {number} chatId 绑定的会话,null 表示不绑定
|
* @param {Function} onProgress 上传进度回调(0-100)
|
* @param {boolean} autoIngest 上传后是否立即向量化,默认 true
|
*
|
* 超时给到 10 分钟:autoIngest 为 true 时,这个请求要等解析 + embedding +
|
* 写向量库全部完成才返回,大文档几分钟很正常。
|
*/
|
export function uploadFile(file, chatId = null, onProgress = null, autoIngest = true, categoryId = null) {
|
const form = new FormData()
|
form.append('file', file)
|
if (chatId != null) {
|
form.append('chatId', chatId)
|
}
|
return http.post('/files/upload', form, {
|
params: { autoIngest, categoryId },
|
headers: { 'Content-Type': 'multipart/form-data' },
|
timeout: 600000,
|
onUploadProgress: (event) => {
|
if (onProgress && event.total) {
|
onProgress(Math.round((event.loaded / event.total) * 100))
|
}
|
}
|
})
|
}
|
|
/** 文件列表 */
|
export function listFiles(chatId = null) {
|
return http.get('/files', { params: chatId != null ? { chatId } : {} })
|
}
|
|
/** 删除文件(同时清理磁盘文件与向量集合) */
|
export function deleteFile(fileId) {
|
return http.delete(`/files/${fileId}`)
|
}
|
|
/**
|
* 触发向量化入库。
|
* 这是耗时接口(解析 + embedding),大文档可能数分钟,需给足超时。
|
*/
|
export function ingestFile(fileId) {
|
return http.post(`/files/${fileId}/ingest`, null, { timeout: 900000 })
|
}
|
|
/** RAG 服务健康状态与已拥有的知识库集合 */
|
export function ragHealth() {
|
return http.get('/rag/health')
|
}
|
|
/**
|
* 把知识库集合绑定到会话(全量覆盖语义)。
|
*
|
* 返回值是**后端实际绑定成功**的集合列表,可能与传入的不一致 ——
|
* 已失效的集合(文档被删、被取消全局标记)会被后端忽略。
|
* 调用方应与传入列表比对,把被剔除的项从本地状态里同步掉。
|
*/
|
export function bindCollections(chatId, collectionNames) {
|
return http.post('/rag/bind', { chatId, collectionNames })
|
}
|
|
/** 查询会话已绑定的集合 */
|
export function listBindings(chatId) {
|
return http.get(`/rag/bind/${chatId}`)
|
}
|
|
/** 解绑会话的全部知识库 */
|
export function unbindCollections(chatId) {
|
return http.delete(`/rag/bind/${chatId}`)
|
}
|
|
/** 直接调用后端代理的向量检索(调试用) */
|
export function searchRag(payload) {
|
return http.post('/rag/search', payload)
|
}
|
|
// ============================================================
|
// 全局知识库(管理员维护,全员可绑定)
|
// ============================================================
|
|
/**
|
* 管理员上传全局知识库文件。
|
*
|
* @param {boolean} autoIngest 上传后是否立即向量化,默认 true ——
|
* 全局知识库若不入库,所有用户都检索不到,留着未入库状态没有意义。
|
*/
|
export function uploadGlobalFile(file, onProgress = null, autoIngest = true, categoryId = null) {
|
const form = new FormData()
|
form.append('file', file)
|
return http.post('/files/global/upload', form, {
|
params: { autoIngest, categoryId },
|
headers: { 'Content-Type': 'multipart/form-data' },
|
timeout: 600000,
|
onUploadProgress: (event) => {
|
if (onProgress && event.total) {
|
onProgress(Math.round((event.loaded / event.total) * 100))
|
}
|
}
|
})
|
}
|
|
/** 列出全部全局知识库文件(任何登录用户都可调用,用于发现可绑定的公共知识库) */
|
export function listGlobalFiles() {
|
return http.get('/files/global')
|
}
|
|
/** 设为 / 取消全局知识库(仅管理员) */
|
export function setFileGlobal(fileId, isGlobal) {
|
return http.post(`/files/${fileId}/global`, null, { params: { isGlobal } })
|
}
|
|
// ============================================================
|
// 图片 OCR
|
// ============================================================
|
|
/** 对话中上传图片时允许的类型(与后端白名单保持一致) */
|
export const IMAGE_EXTENSIONS = ['png', 'jpg', 'jpeg', 'webp', 'bmp', 'gif', 'tif', 'tiff']
|
|
/**
|
* 上传图片并识别其中的文字。
|
*
|
* 返回体含:
|
* - text:识别出的完整文本,随消息发给大模型作为上下文
|
* - fileId:图片在服务端的 id,用于在消息里展示原图
|
* - imageUrl:图片访问地址
|
* - blocks:各文本块及包围盒,可用于在图上标注
|
*
|
* @param {File} file
|
* @param {number} chatId 绑定的会话,可为空
|
* @param {Function} onProgress 上传进度回调
|
*/
|
export function recognizeImage(file, chatId = null, onProgress = null) {
|
const form = new FormData()
|
form.append('file', file)
|
// OCR 首次可能加载模型,超时给足
|
return http.post('/files/ocr', form, {
|
params: chatId != null ? { chatId } : {},
|
headers: { 'Content-Type': 'multipart/form-data' },
|
timeout: 300000,
|
onUploadProgress: (event) => {
|
if (onProgress && event.total) {
|
onProgress(Math.round((event.loaded / event.total) * 100))
|
}
|
}
|
})
|
}
|
|
/**
|
* 暂存一段附件文本,返回短 id。
|
*
|
* 图片 OCR 文字可能几千字,直接放进 SSE 的 query string 会超出
|
* Tomcat 的请求行上限(中文 URL 编码约 9 字节/字),
|
* 得到 "Request header is too large" 这种难以理解的 400。
|
* 因此先 POST 存下来,发送消息时只带返回的 id。
|
*/
|
export function storeAttachmentText(text) {
|
return http.post('/attachments/text', { text })
|
}
|