base.ts 11 KB

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