提交 | 用户 | 时间
|
820397
|
1 |
import { fetchEventSource } from '@microsoft/fetch-event-source' |
H |
2 |
|
|
3 |
import { getAccessToken } from '@/utils/auth' |
|
4 |
import { config } from '@/config/axios/config' |
|
5 |
import { AiWriteTypeEnum } from '@/views/ai/utils/constants' |
|
6 |
import request from '@/config/axios' |
|
7 |
|
|
8 |
export interface WriteVO { |
|
9 |
type: AiWriteTypeEnum.WRITING | AiWriteTypeEnum.REPLY // 1:撰写 2:回复 |
|
10 |
prompt: string // 写作内容提示 1。撰写 2回复 |
|
11 |
originalContent: string // 原文 |
|
12 |
length: number // 长度 |
|
13 |
format: number // 格式 |
|
14 |
tone: number // 语气 |
|
15 |
language: number // 语言 |
|
16 |
userId?: number // 用户编号 |
|
17 |
platform?: string // 平台 |
|
18 |
model?: string // 模型 |
|
19 |
generatedContent?: string // 生成的内容 |
|
20 |
errorMessage?: string // 错误信息 |
|
21 |
createTime?: Date // 创建时间 |
|
22 |
} |
|
23 |
|
|
24 |
export interface AiWritePageReqVO extends PageParam { |
|
25 |
userId?: number // 用户编号 |
|
26 |
type?: AiWriteTypeEnum // 写作类型 |
|
27 |
platform?: string // 平台 |
|
28 |
createTime?: [string, string] // 创建时间 |
|
29 |
} |
|
30 |
|
|
31 |
export interface AiWriteRespVo { |
|
32 |
id: number |
|
33 |
userId: number |
|
34 |
type: number |
|
35 |
platform: string |
|
36 |
model: string |
|
37 |
prompt: string |
|
38 |
generatedContent: string |
|
39 |
originalContent: string |
|
40 |
length: number |
|
41 |
format: number |
|
42 |
tone: number |
|
43 |
language: number |
|
44 |
errorMessage: string |
|
45 |
createTime: string |
|
46 |
} |
|
47 |
|
|
48 |
export const WriteApi = { |
|
49 |
writeStream: ({ |
|
50 |
data, |
|
51 |
onClose, |
|
52 |
onMessage, |
|
53 |
onError, |
|
54 |
ctrl |
|
55 |
}: { |
|
56 |
data: WriteVO |
|
57 |
onMessage?: (res: any) => void |
|
58 |
onError?: (...args: any[]) => void |
|
59 |
onClose?: (...args: any[]) => void |
|
60 |
ctrl: AbortController |
|
61 |
}) => { |
|
62 |
const token = getAccessToken() |
|
63 |
return fetchEventSource(`${config.base_url}/ai/write/generate-stream`, { |
|
64 |
method: 'post', |
|
65 |
headers: { |
|
66 |
'Content-Type': 'application/json', |
|
67 |
Authorization: `Bearer ${token}` |
|
68 |
}, |
|
69 |
openWhenHidden: true, |
|
70 |
body: JSON.stringify(data), |
|
71 |
onmessage: onMessage, |
|
72 |
onerror: onError, |
|
73 |
onclose: onClose, |
|
74 |
signal: ctrl.signal |
|
75 |
}) |
|
76 |
}, |
|
77 |
// 获取写作列表 |
|
78 |
getWritePage: (params: AiWritePageReqVO) => { |
|
79 |
return request.get<PageResult<AiWriteRespVo[]>>({ url: `/ai/write/page`, params }) |
|
80 |
}, |
|
81 |
// 删除写作 |
|
82 |
deleteWrite(id: number) { |
|
83 |
return request.delete({ url: `/ai/write/delete`, params: { id } }) |
|
84 |
} |
|
85 |
} |