- 前端移除未使用的 ArrowRightOutlined 导入 - 优化智能体列表排序逻辑,避免修改原数组 - 后端使用 Stream API 简化收藏智能体 ID 获取逻辑
142 lines
3.5 KiB
JavaScript
142 lines
3.5 KiB
JavaScript
/**
|
||
* AI 智能体 API
|
||
*/
|
||
import request from '@/api/http'
|
||
import { fetchEventSource } from '@microsoft/fetch-event-source'
|
||
import tokenManager from '@gold/utils/token-manager'
|
||
import { API_BASE } from '@gold/config/api'
|
||
|
||
const BASE_URL = `${API_BASE.APP_TIK}`
|
||
|
||
/**
|
||
* 获取启用的智能体列表
|
||
*/
|
||
export function getAgentList() {
|
||
return request({
|
||
url: `${BASE_URL}/agent/list`,
|
||
method: 'get'
|
||
})
|
||
}
|
||
|
||
/**
|
||
* 流式对话(SSE)
|
||
* @param {Object} options - 请求配置
|
||
* @param {number} options.agentId - 智能体ID
|
||
* @param {string} options.content - 用户输入内容
|
||
* @param {string} [options.conversationId] - 会话ID(可选,首次对话不传)
|
||
* @param {string} [options.modelMode] - 模型模式:pro-深度版 standard-标准版
|
||
* @param {AbortController} [options.ctrl] - 取消控制器
|
||
* @param {Function} options.onMessage - 消息回调
|
||
* @param {Function} [options.onError] - 错误回调
|
||
* @param {Function} [options.onClose] - 关闭回调
|
||
*/
|
||
export async function sendChatStream(options) {
|
||
const {
|
||
agentId,
|
||
content,
|
||
conversationId,
|
||
modelMode = 'pro',
|
||
ctrl,
|
||
onMessage,
|
||
onError,
|
||
onClose
|
||
} = options || {}
|
||
|
||
const token = tokenManager.getAccessToken()
|
||
|
||
return fetchEventSource(`${BASE_URL}/dify/chat/stream`, {
|
||
method: 'post',
|
||
headers: {
|
||
'Content-Type': 'application/json',
|
||
'Authorization': `Bearer ${token}`,
|
||
'tenant-id': import.meta.env?.VITE_TENANT_ID
|
||
},
|
||
openWhenHidden: true,
|
||
body: JSON.stringify({
|
||
agentId,
|
||
content,
|
||
conversationId,
|
||
modelMode
|
||
}),
|
||
onmessage: (event) => {
|
||
if (typeof onMessage === 'function') {
|
||
try {
|
||
const data = JSON.parse(event.data)
|
||
// 解析 CommonResult 包装
|
||
const result = data.code === 0 ? data.data : data
|
||
onMessage(result)
|
||
} catch (e) {
|
||
console.error('解析 SSE 数据失败:', e)
|
||
}
|
||
}
|
||
},
|
||
onerror: (err) => {
|
||
if (typeof onError === 'function') {
|
||
onError(err)
|
||
}
|
||
throw err // 不重试
|
||
},
|
||
onclose: () => {
|
||
if (typeof onClose === 'function') {
|
||
onClose()
|
||
}
|
||
},
|
||
signal: ctrl ? ctrl.signal : undefined
|
||
})
|
||
}
|
||
|
||
/**
|
||
* 获取会话列表
|
||
* @param {Object} params - 请求参数
|
||
* @param {number} params.agentId - 智能体ID
|
||
* @param {string} [params.lastId] - 上一页最后一条记录ID
|
||
* @param {number} [params.limit] - 返回条数,默认20
|
||
*/
|
||
export function getConversations(params) {
|
||
return request({
|
||
url: `${BASE_URL}/dify/conversations`,
|
||
method: 'get',
|
||
params
|
||
})
|
||
}
|
||
|
||
/**
|
||
* 获取会话历史消息
|
||
* @param {Object} params - 请求参数
|
||
* @param {number} params.agentId - 智能体ID
|
||
* @param {string} params.conversationId - 会话ID
|
||
* @param {string} [params.firstId] - 当前页第一条记录ID
|
||
* @param {number} [params.limit] - 返回条数,默认20
|
||
*/
|
||
export function getMessages(params) {
|
||
return request({
|
||
url: `${BASE_URL}/dify/messages`,
|
||
method: 'get',
|
||
params
|
||
})
|
||
}
|
||
|
||
/**
|
||
* 添加智能体收藏
|
||
* @param {number} agentId - 智能体ID
|
||
*/
|
||
export function addFavorite(agentId) {
|
||
return request({
|
||
url: `${BASE_URL}/agent/favorite/create`,
|
||
method: 'post',
|
||
params: { agentId }
|
||
})
|
||
}
|
||
|
||
/**
|
||
* 取消智能体收藏
|
||
* @param {number} agentId - 智能体ID
|
||
*/
|
||
export function removeFavorite(agentId) {
|
||
return request({
|
||
url: `${BASE_URL}/agent/favorite/delete`,
|
||
method: 'delete',
|
||
params: { agentId }
|
||
})
|
||
}
|