base.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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): Promise<any> => {
  274. const defaultOptions = {
  275. method: 'POST',
  276. url: `${API_PREFIX}/files/upload`,
  277. headers: {
  278. Authorization: `Bearer ${localStorage.getItem('console_token') || ''}`,
  279. },
  280. data: {},
  281. }
  282. options = {
  283. ...defaultOptions,
  284. ...options,
  285. headers: { ...defaultOptions.headers, ...options.headers },
  286. }
  287. return new Promise((resolve, reject) => {
  288. const xhr = options.xhr
  289. xhr.open(options.method, options.url)
  290. for (const key in options.headers)
  291. xhr.setRequestHeader(key, options.headers[key])
  292. xhr.withCredentials = true
  293. xhr.responseType = 'json'
  294. xhr.onreadystatechange = function () {
  295. if (xhr.readyState === 4) {
  296. if (xhr.status === 201)
  297. resolve(xhr.response)
  298. else
  299. reject(xhr)
  300. }
  301. }
  302. xhr.upload.onprogress = options.onprogress
  303. xhr.send(options.data)
  304. })
  305. }
  306. export const ssePost = (url: string, fetchOptions: FetchOptionType, { isPublicAPI = false, onData, onCompleted, onThought, onMessageEnd, onMessageReplace, onError, getAbortController }: IOtherOptions) => {
  307. const abortController = new AbortController()
  308. const options = Object.assign({}, baseOptions, {
  309. method: 'POST',
  310. signal: abortController.signal,
  311. }, fetchOptions)
  312. const contentType = options.headers.get('Content-Type')
  313. if (!contentType)
  314. options.headers.set('Content-Type', ContentType.json)
  315. getAbortController?.(abortController)
  316. const urlPrefix = isPublicAPI ? PUBLIC_API_PREFIX : API_PREFIX
  317. const urlWithPrefix = `${urlPrefix}${url.startsWith('/') ? url : `/${url}`}`
  318. const { body } = options
  319. if (body)
  320. options.body = JSON.stringify(body)
  321. globalThis.fetch(urlWithPrefix, options as RequestInit)
  322. .then((res) => {
  323. if (!/^(2|3)\d{2}$/.test(String(res.status))) {
  324. res.json().then((data: any) => {
  325. Toast.notify({ type: 'error', message: data.message || 'Server Error' })
  326. })
  327. onError?.('Server Error')
  328. return
  329. }
  330. return handleStream(res, (str: string, isFirstMessage: boolean, moreInfo: IOnDataMoreInfo) => {
  331. if (moreInfo.errorMessage) {
  332. // debugger
  333. onError?.(moreInfo.errorMessage, moreInfo.errorCode)
  334. if (moreInfo.errorMessage !== 'AbortError: The user aborted a request.')
  335. Toast.notify({ type: 'error', message: moreInfo.errorMessage })
  336. return
  337. }
  338. onData?.(str, isFirstMessage, moreInfo)
  339. }, onCompleted, onThought, onMessageEnd, onMessageReplace)
  340. }).catch((e) => {
  341. if (e.toString() !== 'AbortError: The user aborted a request.')
  342. Toast.notify({ type: 'error', message: e })
  343. onError?.(e)
  344. })
  345. }
  346. // base request
  347. export const request = <T>(url: string, options = {}, otherOptions?: IOtherOptions) => {
  348. return baseFetch<T>(url, options, otherOptions || {})
  349. }
  350. // request methods
  351. export const get = <T>(url: string, options = {}, otherOptions?: IOtherOptions) => {
  352. return request<T>(url, Object.assign({}, options, { method: 'GET' }), otherOptions)
  353. }
  354. // For public API
  355. export const getPublic = <T>(url: string, options = {}, otherOptions?: IOtherOptions) => {
  356. return get<T>(url, options, { ...otherOptions, isPublicAPI: true })
  357. }
  358. export const post = <T>(url: string, options = {}, otherOptions?: IOtherOptions) => {
  359. return request<T>(url, Object.assign({}, options, { method: 'POST' }), otherOptions)
  360. }
  361. export const postPublic = <T>(url: string, options = {}, otherOptions?: IOtherOptions) => {
  362. return post<T>(url, options, { ...otherOptions, isPublicAPI: true })
  363. }
  364. export const put = <T>(url: string, options = {}, otherOptions?: IOtherOptions) => {
  365. return request<T>(url, Object.assign({}, options, { method: 'PUT' }), otherOptions)
  366. }
  367. export const putPublic = <T>(url: string, options = {}, otherOptions?: IOtherOptions) => {
  368. return put<T>(url, options, { ...otherOptions, isPublicAPI: true })
  369. }
  370. export const del = <T>(url: string, options = {}, otherOptions?: IOtherOptions) => {
  371. return request<T>(url, Object.assign({}, options, { method: 'DELETE' }), otherOptions)
  372. }
  373. export const delPublic = <T>(url: string, options = {}, otherOptions?: IOtherOptions) => {
  374. return del<T>(url, options, { ...otherOptions, isPublicAPI: true })
  375. }
  376. export const patch = <T>(url: string, options = {}, otherOptions?: IOtherOptions) => {
  377. return request<T>(url, Object.assign({}, options, { method: 'PATCH' }), otherOptions)
  378. }
  379. export const patchPublic = <T>(url: string, options = {}, otherOptions?: IOtherOptions) => {
  380. return patch<T>(url, options, { ...otherOptions, isPublicAPI: true })
  381. }