base.ts 11 KB

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