base.ts 12 KB

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