65 lines
1.2 KiB
TypeScript
Raw Normal View History

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'
export interface WriteParams {
/**
* 1:撰写 2:回复
*/
type: 1 | 2
/**
* 1 2
*/
prompt: string
/**
*
*/
originalContent: string
/**
*
*/
length: number
/**
*
*/
format: number
/**
*
*/
tone: number
/**
*
*/
language: number
}
2024-07-08 00:41:20 +08:00
export const writeStream = ({
data,
onClose,
onMessage,
onError,
ctrl
}: {
data: WriteParams
onMessage?: (res: any) => void
onError?: (...args: any[]) => void
onClose?: (...args: any[]) => void
ctrl: AbortController
}) => {
2024-07-07 18:14:03 +08:00
// return request.post({ url: '/ai/write/generate-stream', data })
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
})
}