base.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /* eslint-disable no-new, prefer-promise-reject-errors */
  2. import { API_PREFIX, IS_CE_EDITION, PUBLIC_API_PREFIX } from '@/config'
  3. import Toast from '@/app/components/base/toast'
  4. const TIME_OUT = 100000
  5. const ContentType = {
  6. json: 'application/json',
  7. stream: 'text/event-stream',
  8. form: 'application/x-www-form-urlencoded; charset=UTF-8',
  9. download: 'application/octet-stream', // for download
  10. upload: 'multipart/form-data', // for upload
  11. }
  12. const baseOptions = {
  13. method: 'GET',
  14. mode: 'cors',
  15. credentials: 'include', // always send cookies、HTTP Basic authentication.
  16. headers: new Headers({
  17. 'Content-Type': ContentType.json,
  18. }),
  19. redirect: 'follow',
  20. }
  21. export type IOnDataMoreInfo = {
  22. conversationId?: string
  23. taskId?: string
  24. messageId: string
  25. errorMessage?: string
  26. }
  27. export type IOnData = (message: string, isFirstMessage: boolean, moreInfo: IOnDataMoreInfo) => void
  28. export type IOnCompleted = (hasError?: boolean) => void
  29. export type IOnError = (msg: string) => void
  30. type IOtherOptions = {
  31. isPublicAPI?: boolean
  32. needAllResponseContent?: boolean
  33. onData?: IOnData // for stream
  34. onError?: IOnError
  35. onCompleted?: IOnCompleted // for stream
  36. getAbortController?: (abortController: AbortController) => void
  37. }
  38. function unicodeToChar(text: string) {
  39. return text.replace(/\\u[0-9a-f]{4}/g, (_match, p1) => {
  40. return String.fromCharCode(parseInt(p1, 16))
  41. })
  42. }
  43. export function format(text: string) {
  44. let res = text.trim()
  45. if (res.startsWith('\n'))
  46. res = res.replace('\n', '')
  47. return res.replaceAll('\n', '<br/>').replaceAll('```', '')
  48. }
  49. const handleStream = (response: any, onData: IOnData, onCompleted?: IOnCompleted) => {
  50. if (!response.ok)
  51. throw new Error('Network response was not ok')
  52. const reader = response.body.getReader()
  53. const decoder = new TextDecoder('utf-8')
  54. let buffer = ''
  55. let bufferObj: any
  56. let isFirstMessage = true
  57. function read() {
  58. let hasError = false
  59. reader.read().then((result: any) => {
  60. if (result.done) {
  61. onCompleted && onCompleted()
  62. return
  63. }
  64. buffer += decoder.decode(result.value, { stream: true })
  65. const lines = buffer.split('\n')
  66. try {
  67. lines.forEach((message) => {
  68. if (message.startsWith('data: ')) { // check if it starts with data:
  69. // console.log(message);
  70. try {
  71. bufferObj = JSON.parse(message.substring(6)) // remove data: and parse as json
  72. }
  73. catch (e) {
  74. // mute handle message cut off
  75. onData('', isFirstMessage, {
  76. conversationId: bufferObj?.conversation_id,
  77. messageId: bufferObj?.id,
  78. })
  79. return
  80. }
  81. if (bufferObj.status === 400 || !bufferObj.event) {
  82. onData('', false, {
  83. conversationId: undefined,
  84. messageId: '',
  85. errorMessage: bufferObj.message,
  86. })
  87. hasError = true
  88. onCompleted && onCompleted(true)
  89. return
  90. }
  91. // can not use format here. Because message is splited.
  92. onData(unicodeToChar(bufferObj.answer), isFirstMessage, {
  93. conversationId: bufferObj.conversation_id,
  94. taskId: bufferObj.task_id,
  95. messageId: bufferObj.id,
  96. })
  97. isFirstMessage = false
  98. }
  99. })
  100. buffer = lines[lines.length - 1]
  101. }
  102. catch (e) {
  103. onData('', false, {
  104. conversationId: undefined,
  105. messageId: '',
  106. errorMessage: `${e}`,
  107. })
  108. hasError = true
  109. onCompleted && onCompleted(true)
  110. return
  111. }
  112. if (!hasError)
  113. read()
  114. })
  115. }
  116. read()
  117. }
  118. const baseFetch = (
  119. url: string,
  120. fetchOptions: any,
  121. {
  122. isPublicAPI = false,
  123. needAllResponseContent,
  124. }: IOtherOptions,
  125. ) => {
  126. const options = Object.assign({}, baseOptions, fetchOptions)
  127. if (isPublicAPI) {
  128. const sharedToken = globalThis.location.pathname.split('/').slice(-1)[0]
  129. options.headers.set('Authorization', `bearer ${sharedToken}`)
  130. }
  131. const urlPrefix = isPublicAPI ? PUBLIC_API_PREFIX : API_PREFIX
  132. let urlWithPrefix = `${urlPrefix}${url.startsWith('/') ? url : `/${url}`}`
  133. const { method, params, body } = options
  134. // handle query
  135. if (method === 'GET' && params) {
  136. const paramsArray: string[] = []
  137. Object.keys(params).forEach(key =>
  138. paramsArray.push(`${key}=${encodeURIComponent(params[key])}`),
  139. )
  140. if (urlWithPrefix.search(/\?/) === -1)
  141. urlWithPrefix += `?${paramsArray.join('&')}`
  142. else
  143. urlWithPrefix += `&${paramsArray.join('&')}`
  144. delete options.params
  145. }
  146. if (body)
  147. options.body = JSON.stringify(body)
  148. // Handle timeout
  149. return Promise.race([
  150. new Promise((resolve, reject) => {
  151. setTimeout(() => {
  152. reject(new Error('request timeout'))
  153. }, TIME_OUT)
  154. }),
  155. new Promise((resolve, reject) => {
  156. globalThis.fetch(urlWithPrefix, options)
  157. .then((res: any) => {
  158. const resClone = res.clone()
  159. // Error handler
  160. if (!/^(2|3)\d{2}$/.test(res.status)) {
  161. const bodyJson = res.json()
  162. switch (res.status) {
  163. case 401: {
  164. if (isPublicAPI) {
  165. Toast.notify({ type: 'error', message: 'Invalid token' })
  166. return
  167. }
  168. const loginUrl = `${globalThis.location.origin}/signin`
  169. if (IS_CE_EDITION) {
  170. bodyJson.then((data: any) => {
  171. if (data.code === 'not_setup') {
  172. globalThis.location.href = `${globalThis.location.origin}/install`
  173. }
  174. else {
  175. if (location.pathname === '/signin') {
  176. bodyJson.then((data: any) => {
  177. Toast.notify({ type: 'error', message: data.message })
  178. })
  179. }
  180. else {
  181. globalThis.location.href = loginUrl
  182. }
  183. }
  184. })
  185. return Promise.reject()
  186. }
  187. globalThis.location.href = loginUrl
  188. break
  189. }
  190. case 403:
  191. new Promise(() => {
  192. bodyJson.then((data: any) => {
  193. Toast.notify({ type: 'error', message: data.message })
  194. if (data.code === 'already_setup')
  195. globalThis.location.href = `${globalThis.location.origin}/signin`
  196. })
  197. })
  198. break
  199. // fall through
  200. default:
  201. new Promise(() => {
  202. bodyJson.then((data: any) => {
  203. Toast.notify({ type: 'error', message: data.message })
  204. })
  205. })
  206. }
  207. return Promise.reject(resClone)
  208. }
  209. // handle delete api. Delete api not return content.
  210. if (res.status === 204) {
  211. resolve({ result: 'success' })
  212. return
  213. }
  214. // return data
  215. const data = options.headers.get('Content-type') === ContentType.download ? res.blob() : res.json()
  216. resolve(needAllResponseContent ? resClone : data)
  217. })
  218. .catch((err) => {
  219. Toast.notify({ type: 'error', message: err })
  220. reject(err)
  221. })
  222. }),
  223. ])
  224. }
  225. export const upload = (options: any): Promise<any> => {
  226. const defaultOptions = {
  227. method: 'POST',
  228. url: `${API_PREFIX}/files/upload`,
  229. headers: {},
  230. data: {},
  231. }
  232. options = {
  233. ...defaultOptions,
  234. ...options,
  235. headers: { ...defaultOptions.headers, ...options.headers },
  236. }
  237. return new Promise((resolve, reject) => {
  238. const xhr = options.xhr
  239. xhr.open(options.method, options.url)
  240. for (const key in options.headers)
  241. xhr.setRequestHeader(key, options.headers[key])
  242. xhr.withCredentials = true
  243. xhr.responseType = 'json'
  244. xhr.onreadystatechange = function () {
  245. if (xhr.readyState === 4) {
  246. if (xhr.status === 201)
  247. resolve(xhr.response)
  248. else
  249. reject(xhr)
  250. }
  251. }
  252. xhr.upload.onprogress = options.onprogress
  253. xhr.send(options.data)
  254. })
  255. }
  256. export const ssePost = (url: string, fetchOptions: any, { isPublicAPI = false, onData, onCompleted, onError, getAbortController }: IOtherOptions) => {
  257. const abortController = new AbortController()
  258. const options = Object.assign({}, baseOptions, {
  259. method: 'POST',
  260. signal: abortController.signal,
  261. }, fetchOptions)
  262. getAbortController?.(abortController)
  263. const urlPrefix = isPublicAPI ? PUBLIC_API_PREFIX : API_PREFIX
  264. const urlWithPrefix = `${urlPrefix}${url.startsWith('/') ? url : `/${url}`}`
  265. const { body } = options
  266. if (body)
  267. options.body = JSON.stringify(body)
  268. globalThis.fetch(urlWithPrefix, options)
  269. .then((res: any) => {
  270. // debugger
  271. if (!/^(2|3)\d{2}$/.test(res.status)) {
  272. new Promise(() => {
  273. res.json().then((data: any) => {
  274. Toast.notify({ type: 'error', message: data.message || 'Server Error' })
  275. })
  276. })
  277. onError?.('Server Error')
  278. return
  279. }
  280. return handleStream(res, (str: string, isFirstMessage: boolean, moreInfo: IOnDataMoreInfo) => {
  281. if (moreInfo.errorMessage) {
  282. Toast.notify({ type: 'error', message: moreInfo.errorMessage })
  283. return
  284. }
  285. onData?.(str, isFirstMessage, moreInfo)
  286. }, onCompleted)
  287. }).catch((e) => {
  288. // debugger
  289. Toast.notify({ type: 'error', message: e })
  290. onError?.(e)
  291. })
  292. }
  293. export const request = (url: string, options = {}, otherOptions?: IOtherOptions) => {
  294. return baseFetch(url, options, otherOptions || {})
  295. }
  296. export const get = (url: string, options = {}, otherOptions?: IOtherOptions) => {
  297. return request(url, Object.assign({}, options, { method: 'GET' }), otherOptions)
  298. }
  299. // For public API
  300. export const getPublic = (url: string, options = {}, otherOptions?: IOtherOptions) => {
  301. return get(url, options, { ...otherOptions, isPublicAPI: true })
  302. }
  303. export const post = (url: string, options = {}, otherOptions?: IOtherOptions) => {
  304. return request(url, Object.assign({}, options, { method: 'POST' }), otherOptions)
  305. }
  306. export const postPublic = (url: string, options = {}, otherOptions?: IOtherOptions) => {
  307. return post(url, options, { ...otherOptions, isPublicAPI: true })
  308. }
  309. export const put = (url: string, options = {}, otherOptions?: IOtherOptions) => {
  310. return request(url, Object.assign({}, options, { method: 'PUT' }), otherOptions)
  311. }
  312. export const putPublic = (url: string, options = {}, otherOptions?: IOtherOptions) => {
  313. return put(url, options, { ...otherOptions, isPublicAPI: true })
  314. }
  315. export const del = (url: string, options = {}, otherOptions?: IOtherOptions) => {
  316. return request(url, Object.assign({}, options, { method: 'DELETE' }), otherOptions)
  317. }
  318. export const delPublic = (url: string, options = {}, otherOptions?: IOtherOptions) => {
  319. return del(url, options, { ...otherOptions, isPublicAPI: true })
  320. }
  321. export const patch = (url: string, options = {}, otherOptions?: IOtherOptions) => {
  322. return request(url, Object.assign({}, options, { method: 'PATCH' }), otherOptions)
  323. }
  324. export const patchPublic = (url: string, options = {}, otherOptions?: IOtherOptions) => {
  325. return patch(url, options, { ...otherOptions, isPublicAPI: true })
  326. }