base.ts 13 KB

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