Files
sionrui/frontend/app/web-gold/src/api/agent.js

142 lines
3.5 KiB
JavaScript
Raw Normal View History

2026-02-22 21:36:47 +08:00
/**
* 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可选首次对话不传
2026-02-23 01:04:00 +08:00
* @param {string} [options.modelMode] - 模型模式pro-深度版 standard-标准版
2026-02-22 21:36:47 +08:00
* @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,
2026-02-23 01:04:00 +08:00
modelMode = 'pro',
2026-02-22 21:36:47 +08:00
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,
2026-02-23 01:04:00 +08:00
conversationId,
modelMode
2026-02-22 21:36:47 +08:00
}),
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
})
}
2026-02-25 18:21:25 +08:00
/**
* 获取会话列表
* @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 }
})
}