base.ts 14 KB

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