index.ts 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import { fetchEventSource } from '@microsoft/fetch-event-source'
  2. import request from '@/config/axios'
  3. import { getAccessToken } from '@/utils/auth'
  4. import { config } from '@/config/axios/config'
  5. import { AiWriteTypeEnum } from '@/views/ai/utils/constants'
  6. export interface WriteVO {
  7. type: AiWriteTypeEnum.WRITING | AiWriteTypeEnum.REPLY // 1:撰写 2:回复
  8. prompt: string // 写作内容提示 1。撰写 2回复
  9. originalContent: string // 原文
  10. length: number // 长度
  11. format: number // 格式
  12. tone: number // 语气
  13. language: number // 语言
  14. userId?: number // 用户编号
  15. platform?: string // 平台
  16. model?: string // 模型
  17. generatedContent?: string // 生成的内容
  18. errorMessage: string // 错误信息
  19. createTime?: Date // 创建时间
  20. }
  21. // TODO @hhero:搞成 WriteApi,类似 ConversationApi 一样。这样更有类的概念,后续引入某个 Api,然后调用它的方法就可以了。
  22. export const writeStream = ({
  23. data,
  24. onClose,
  25. onMessage,
  26. onError,
  27. ctrl
  28. }: {
  29. data: WriteVO
  30. onMessage?: (res: any) => void
  31. onError?: (...args: any[]) => void
  32. onClose?: (...args: any[]) => void
  33. ctrl: AbortController
  34. }) => {
  35. const token = getAccessToken()
  36. return fetchEventSource(`${config.base_url}/ai/write/generate-stream`, {
  37. method: 'post',
  38. headers: {
  39. 'Content-Type': 'application/json',
  40. Authorization: `Bearer ${token}`
  41. },
  42. openWhenHidden: true,
  43. body: JSON.stringify(data),
  44. onmessage: onMessage,
  45. onerror: onError,
  46. onclose: onClose,
  47. signal: ctrl.signal
  48. })
  49. }
  50. // AI 写作 API
  51. export const WriteApi = {
  52. // 查询AI 写作分页
  53. getWritePage: async (params: any) => {
  54. return await request.get({ url: `/ai/write/page`, params })
  55. },
  56. // 删除AI 写作
  57. deleteWrite: async (id: number) => {
  58. return await request.delete({ url: `/ai/write/delete?id=` + id })
  59. }
  60. }