提交 | 用户 | 时间
|
820397
|
1 |
import request from '@/config/axios' |
H |
2 |
import { fetchEventSource } from '@microsoft/fetch-event-source' |
|
3 |
import { getAccessToken } from '@/utils/auth' |
|
4 |
import { config } from '@/config/axios/config' |
|
5 |
|
|
6 |
// 聊天VO |
|
7 |
export interface ChatMessageVO { |
|
8 |
id: number // 编号 |
|
9 |
conversationId: number // 对话编号 |
|
10 |
type: string // 消息类型 |
|
11 |
userId: string // 用户编号 |
|
12 |
roleId: string // 角色编号 |
|
13 |
model: number // 模型标志 |
|
14 |
modelId: number // 模型编号 |
|
15 |
content: string // 聊天内容 |
|
16 |
tokens: number // 消耗 Token 数量 |
|
17 |
createTime: Date // 创建时间 |
|
18 |
roleAvatar: string // 角色头像 |
|
19 |
userAvatar: string // 创建时间 |
|
20 |
} |
|
21 |
|
|
22 |
// AI chat 聊天 |
|
23 |
export const ChatMessageApi = { |
|
24 |
// 消息列表 |
|
25 |
getChatMessageListByConversationId: async (conversationId: number | null) => { |
|
26 |
return await request.get({ |
|
27 |
url: `/ai/chat/message/list-by-conversation-id?conversationId=${conversationId}` |
|
28 |
}) |
|
29 |
}, |
|
30 |
|
|
31 |
// 发送 Stream 消息 |
|
32 |
// 为什么不用 axios 呢?因为它不支持 SSE 调用 |
|
33 |
sendChatMessageStream: async ( |
|
34 |
conversationId: number, |
|
35 |
content: string, |
|
36 |
ctrl, |
|
37 |
enableContext: boolean, |
|
38 |
onMessage, |
|
39 |
onError, |
|
40 |
onClose |
|
41 |
) => { |
|
42 |
const token = getAccessToken() |
|
43 |
return fetchEventSource(`${config.base_url}/ai/chat/message/send-stream`, { |
|
44 |
method: 'post', |
|
45 |
headers: { |
|
46 |
'Content-Type': 'application/json', |
|
47 |
Authorization: `Bearer ${token}` |
|
48 |
}, |
|
49 |
openWhenHidden: true, |
|
50 |
body: JSON.stringify({ |
|
51 |
conversationId, |
|
52 |
content, |
|
53 |
useContext: enableContext |
|
54 |
}), |
|
55 |
onmessage: onMessage, |
|
56 |
onerror: onError, |
|
57 |
onclose: onClose, |
|
58 |
signal: ctrl.signal |
|
59 |
}) |
|
60 |
}, |
|
61 |
|
|
62 |
// 删除消息 |
|
63 |
deleteChatMessage: async (id: string) => { |
|
64 |
return await request.delete({ url: `/ai/chat/message/delete?id=${id}` }) |
|
65 |
}, |
|
66 |
|
|
67 |
// 删除指定对话的消息 |
|
68 |
deleteByConversationId: async (conversationId: number) => { |
|
69 |
return await request.delete({ |
|
70 |
url: `/ai/chat/message/delete-by-conversation-id?conversationId=${conversationId}` |
|
71 |
}) |
|
72 |
}, |
|
73 |
|
|
74 |
// 获得消息分页 |
|
75 |
getChatMessagePage: async (params: any) => { |
|
76 |
return await request.get({ url: '/ai/chat/message/page', params }) |
|
77 |
}, |
|
78 |
|
|
79 |
// 管理员删除消息 |
|
80 |
deleteChatMessageByAdmin: async (id: number) => { |
|
81 |
return await request.delete({ url: `/ai/chat/message/delete-by-admin?id=${id}` }) |
|
82 |
} |
|
83 |
} |