2024-07-07 18:14:03 +08:00
|
|
|
|
import { fetchEventSource } from '@microsoft/fetch-event-source'
|
|
|
|
|
|
|
|
|
|
import { getAccessToken } from '@/utils/auth'
|
|
|
|
|
import { config } from '@/config/axios/config'
|
2024-07-10 12:49:54 +08:00
|
|
|
|
import { AiWriteTypeEnum } from '@/views/ai/utils/constants'
|
2024-07-07 18:14:03 +08:00
|
|
|
|
|
2024-07-09 00:44:27 +08:00
|
|
|
|
export interface WriteVO {
|
2024-07-10 12:49:54 +08:00
|
|
|
|
type: AiWriteTypeEnum.WRITING | AiWriteTypeEnum.REPLY // 1:撰写 2:回复
|
2024-07-09 00:44:27 +08:00
|
|
|
|
prompt: string // 写作内容提示 1。撰写 2回复
|
|
|
|
|
originalContent: string // 原文
|
|
|
|
|
length: number // 长度
|
|
|
|
|
format: number // 格式
|
|
|
|
|
tone: number // 语气
|
|
|
|
|
language: number // 语言
|
2024-07-07 18:14:03 +08:00
|
|
|
|
}
|
2024-07-08 12:39:49 +08:00
|
|
|
|
|
2024-07-10 12:49:54 +08:00
|
|
|
|
// TODO @hhero:搞成 WriteApi,类似 ConversationApi 一样。这样更有类的概念,后续引入某个 Api,然后调用它的方法就可以了。
|
2024-07-08 00:41:20 +08:00
|
|
|
|
export const writeStream = ({
|
|
|
|
|
data,
|
|
|
|
|
onClose,
|
|
|
|
|
onMessage,
|
|
|
|
|
onError,
|
|
|
|
|
ctrl
|
|
|
|
|
}: {
|
2024-07-09 00:44:27 +08:00
|
|
|
|
data: WriteVO
|
2024-07-08 00:41:20 +08:00
|
|
|
|
onMessage?: (res: any) => void
|
|
|
|
|
onError?: (...args: any[]) => void
|
|
|
|
|
onClose?: (...args: any[]) => void
|
|
|
|
|
ctrl: AbortController
|
|
|
|
|
}) => {
|
2024-07-07 18:14:03 +08:00
|
|
|
|
const token = getAccessToken()
|
|
|
|
|
return fetchEventSource(`${config.base_url}/ai/write/generate-stream`, {
|
|
|
|
|
method: 'post',
|
|
|
|
|
headers: {
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
Authorization: `Bearer ${token}`
|
|
|
|
|
},
|
|
|
|
|
openWhenHidden: true,
|
|
|
|
|
body: JSON.stringify(data),
|
|
|
|
|
onmessage: onMessage,
|
|
|
|
|
onerror: onError,
|
|
|
|
|
onclose: onClose,
|
|
|
|
|
signal: ctrl.signal
|
|
|
|
|
})
|
|
|
|
|
}
|