base.ts 14 KB

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