base.ts 15 KB

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