base.ts 15 KB

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