base.ts 14 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 { 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. Toast.notify({ type: 'error', message: 'Invalid token' })
  218. return bodyJson.then((data: T) => Promise.reject(data))
  219. }
  220. const loginUrl = `${globalThis.location.origin}/signin`
  221. if (IS_CE_EDITION) {
  222. bodyJson.then((data: ResponseError) => {
  223. if (data.code === 'not_setup') {
  224. globalThis.location.href = `${globalThis.location.origin}/install`
  225. }
  226. else {
  227. if (location.pathname === '/signin') {
  228. bodyJson.then((data: ResponseError) => {
  229. Toast.notify({ type: 'error', message: data.message })
  230. })
  231. }
  232. else {
  233. globalThis.location.href = loginUrl
  234. }
  235. }
  236. })
  237. return Promise.reject(Error('Unauthorized'))
  238. }
  239. globalThis.location.href = loginUrl
  240. break
  241. }
  242. case 403:
  243. bodyJson.then((data: ResponseError) => {
  244. Toast.notify({ type: 'error', message: data.message })
  245. if (data.code === 'already_setup')
  246. globalThis.location.href = `${globalThis.location.origin}/signin`
  247. })
  248. break
  249. // fall through
  250. default:
  251. bodyJson.then((data: ResponseError) => {
  252. Toast.notify({ type: 'error', message: data.message })
  253. })
  254. }
  255. return Promise.reject(resClone)
  256. }
  257. // handle delete api. Delete api not return content.
  258. if (res.status === 204) {
  259. resolve({ result: 'success' })
  260. return
  261. }
  262. // return data
  263. const data: Promise<T> = options.headers.get('Content-type') === ContentType.download ? res.blob() : res.json()
  264. resolve(needAllResponseContent ? resClone : data)
  265. })
  266. .catch((err) => {
  267. Toast.notify({ type: 'error', message: err })
  268. reject(err)
  269. })
  270. }),
  271. ]) as Promise<T>
  272. }
  273. export const upload = (options: any, isPublicAPI?: boolean): Promise<any> => {
  274. const urlPrefix = isPublicAPI ? PUBLIC_API_PREFIX : API_PREFIX
  275. let token = ''
  276. if (isPublicAPI) {
  277. const sharedToken = globalThis.location.pathname.split('/').slice(-1)[0]
  278. const accessToken = localStorage.getItem('token') || JSON.stringify({ [sharedToken]: '' })
  279. let accessTokenJson = { [sharedToken]: '' }
  280. try {
  281. accessTokenJson = JSON.parse(accessToken)
  282. }
  283. catch (e) {
  284. }
  285. token = accessTokenJson[sharedToken]
  286. }
  287. else {
  288. const accessToken = localStorage.getItem('console_token') || ''
  289. token = accessToken
  290. }
  291. const defaultOptions = {
  292. method: 'POST',
  293. url: `${urlPrefix}/files/upload`,
  294. headers: {
  295. Authorization: `Bearer ${token}`,
  296. },
  297. data: {},
  298. }
  299. options = {
  300. ...defaultOptions,
  301. ...options,
  302. headers: { ...defaultOptions.headers, ...options.headers },
  303. }
  304. return new Promise((resolve, reject) => {
  305. const xhr = options.xhr
  306. xhr.open(options.method, options.url)
  307. for (const key in options.headers)
  308. xhr.setRequestHeader(key, options.headers[key])
  309. xhr.withCredentials = true
  310. xhr.responseType = 'json'
  311. xhr.onreadystatechange = function () {
  312. if (xhr.readyState === 4) {
  313. if (xhr.status === 201)
  314. resolve(xhr.response)
  315. else
  316. reject(xhr)
  317. }
  318. }
  319. xhr.upload.onprogress = options.onprogress
  320. xhr.send(options.data)
  321. })
  322. }
  323. export const ssePost = (url: string, fetchOptions: FetchOptionType, { isPublicAPI = false, onData, onCompleted, onThought, onMessageEnd, onMessageReplace, onError, getAbortController }: IOtherOptions) => {
  324. const abortController = new AbortController()
  325. const options = Object.assign({}, baseOptions, {
  326. method: 'POST',
  327. signal: abortController.signal,
  328. }, fetchOptions)
  329. const contentType = options.headers.get('Content-Type')
  330. if (!contentType)
  331. options.headers.set('Content-Type', ContentType.json)
  332. getAbortController?.(abortController)
  333. const urlPrefix = isPublicAPI ? PUBLIC_API_PREFIX : API_PREFIX
  334. const urlWithPrefix = `${urlPrefix}${url.startsWith('/') ? url : `/${url}`}`
  335. const { body } = options
  336. if (body)
  337. options.body = JSON.stringify(body)
  338. globalThis.fetch(urlWithPrefix, options as RequestInit)
  339. .then((res) => {
  340. if (!/^(2|3)\d{2}$/.test(String(res.status))) {
  341. res.json().then((data: any) => {
  342. Toast.notify({ type: 'error', message: data.message || 'Server Error' })
  343. })
  344. onError?.('Server Error')
  345. return
  346. }
  347. return handleStream(res, (str: string, isFirstMessage: boolean, moreInfo: IOnDataMoreInfo) => {
  348. if (moreInfo.errorMessage) {
  349. // debugger
  350. onError?.(moreInfo.errorMessage, moreInfo.errorCode)
  351. if (moreInfo.errorMessage !== 'AbortError: The user aborted a request.')
  352. Toast.notify({ type: 'error', message: moreInfo.errorMessage })
  353. return
  354. }
  355. onData?.(str, isFirstMessage, moreInfo)
  356. }, onCompleted, onThought, onMessageEnd, onMessageReplace)
  357. }).catch((e) => {
  358. if (e.toString() !== 'AbortError: The user aborted a request.')
  359. Toast.notify({ type: 'error', message: e })
  360. onError?.(e)
  361. })
  362. }
  363. // base request
  364. export const request = <T>(url: string, options = {}, otherOptions?: IOtherOptions) => {
  365. return baseFetch<T>(url, options, otherOptions || {})
  366. }
  367. // request methods
  368. export const get = <T>(url: string, options = {}, otherOptions?: IOtherOptions) => {
  369. return request<T>(url, Object.assign({}, options, { method: 'GET' }), otherOptions)
  370. }
  371. // For public API
  372. export const getPublic = <T>(url: string, options = {}, otherOptions?: IOtherOptions) => {
  373. return get<T>(url, options, { ...otherOptions, isPublicAPI: true })
  374. }
  375. export const post = <T>(url: string, options = {}, otherOptions?: IOtherOptions) => {
  376. return request<T>(url, Object.assign({}, options, { method: 'POST' }), otherOptions)
  377. }
  378. export const postPublic = <T>(url: string, options = {}, otherOptions?: IOtherOptions) => {
  379. return post<T>(url, options, { ...otherOptions, isPublicAPI: true })
  380. }
  381. export const put = <T>(url: string, options = {}, otherOptions?: IOtherOptions) => {
  382. return request<T>(url, Object.assign({}, options, { method: 'PUT' }), otherOptions)
  383. }
  384. export const putPublic = <T>(url: string, options = {}, otherOptions?: IOtherOptions) => {
  385. return put<T>(url, options, { ...otherOptions, isPublicAPI: true })
  386. }
  387. export const del = <T>(url: string, options = {}, otherOptions?: IOtherOptions) => {
  388. return request<T>(url, Object.assign({}, options, { method: 'DELETE' }), otherOptions)
  389. }
  390. export const delPublic = <T>(url: string, options = {}, otherOptions?: IOtherOptions) => {
  391. return del<T>(url, options, { ...otherOptions, isPublicAPI: true })
  392. }
  393. export const patch = <T>(url: string, options = {}, otherOptions?: IOtherOptions) => {
  394. return request<T>(url, Object.assign({}, options, { method: 'PATCH' }), otherOptions)
  395. }
  396. export const patchPublic = <T>(url: string, options = {}, otherOptions?: IOtherOptions) => {
  397. return patch<T>(url, options, { ...otherOptions, isPublicAPI: true })
  398. }