base.ts 15 KB

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