base.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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. if (deleteContentType) {
  167. options.headers.delete('Content-Type')
  168. }
  169. else {
  170. const contentType = options.headers.get('Content-Type')
  171. if (!contentType)
  172. options.headers.set('Content-Type', ContentType.json)
  173. }
  174. const urlPrefix = isPublicAPI ? PUBLIC_API_PREFIX : API_PREFIX
  175. let urlWithPrefix = `${urlPrefix}${url.startsWith('/') ? url : `/${url}`}`
  176. const { method, params, body } = options
  177. // handle query
  178. if (method === 'GET' && params) {
  179. const paramsArray: string[] = []
  180. Object.keys(params).forEach(key =>
  181. paramsArray.push(`${key}=${encodeURIComponent(params[key])}`),
  182. )
  183. if (urlWithPrefix.search(/\?/) === -1)
  184. urlWithPrefix += `?${paramsArray.join('&')}`
  185. else
  186. urlWithPrefix += `&${paramsArray.join('&')}`
  187. delete options.params
  188. }
  189. if (body && bodyStringify)
  190. options.body = JSON.stringify(body)
  191. // Handle timeout
  192. return Promise.race([
  193. new Promise((resolve, reject) => {
  194. setTimeout(() => {
  195. reject(new Error('request timeout'))
  196. }, TIME_OUT)
  197. }),
  198. new Promise((resolve, reject) => {
  199. globalThis.fetch(urlWithPrefix, options as RequestInit)
  200. .then((res) => {
  201. const resClone = res.clone()
  202. // Error handler
  203. if (!/^(2|3)\d{2}$/.test(String(res.status))) {
  204. const bodyJson = res.json()
  205. switch (res.status) {
  206. case 401: {
  207. if (isPublicAPI) {
  208. Toast.notify({ type: 'error', message: 'Invalid token' })
  209. return bodyJson.then((data: T) => Promise.reject(data))
  210. }
  211. const loginUrl = `${globalThis.location.origin}/signin`
  212. if (IS_CE_EDITION) {
  213. bodyJson.then((data: ResponseError) => {
  214. if (data.code === 'not_setup') {
  215. globalThis.location.href = `${globalThis.location.origin}/install`
  216. }
  217. else {
  218. if (location.pathname === '/signin') {
  219. bodyJson.then((data: ResponseError) => {
  220. Toast.notify({ type: 'error', message: data.message })
  221. })
  222. }
  223. else {
  224. globalThis.location.href = loginUrl
  225. }
  226. }
  227. })
  228. return Promise.reject(Error('Unauthorized'))
  229. }
  230. globalThis.location.href = loginUrl
  231. break
  232. }
  233. case 403:
  234. bodyJson.then((data: ResponseError) => {
  235. Toast.notify({ type: 'error', message: data.message })
  236. if (data.code === 'already_setup')
  237. globalThis.location.href = `${globalThis.location.origin}/signin`
  238. })
  239. break
  240. // fall through
  241. default:
  242. bodyJson.then((data: ResponseError) => {
  243. Toast.notify({ type: 'error', message: data.message })
  244. })
  245. }
  246. return Promise.reject(resClone)
  247. }
  248. // handle delete api. Delete api not return content.
  249. if (res.status === 204) {
  250. resolve({ result: 'success' })
  251. return
  252. }
  253. // return data
  254. const data: Promise<T> = options.headers.get('Content-type') === ContentType.download ? res.blob() : res.json()
  255. resolve(needAllResponseContent ? resClone : data)
  256. })
  257. .catch((err) => {
  258. Toast.notify({ type: 'error', message: err })
  259. reject(err)
  260. })
  261. }),
  262. ]) as Promise<T>
  263. }
  264. export const upload = (options: any): Promise<any> => {
  265. const defaultOptions = {
  266. method: 'POST',
  267. url: `${API_PREFIX}/files/upload`,
  268. headers: {},
  269. data: {},
  270. }
  271. options = {
  272. ...defaultOptions,
  273. ...options,
  274. headers: { ...defaultOptions.headers, ...options.headers },
  275. }
  276. return new Promise((resolve, reject) => {
  277. const xhr = options.xhr
  278. xhr.open(options.method, options.url)
  279. for (const key in options.headers)
  280. xhr.setRequestHeader(key, options.headers[key])
  281. xhr.withCredentials = true
  282. xhr.responseType = 'json'
  283. xhr.onreadystatechange = function () {
  284. if (xhr.readyState === 4) {
  285. if (xhr.status === 201)
  286. resolve(xhr.response)
  287. else
  288. reject(xhr)
  289. }
  290. }
  291. xhr.upload.onprogress = options.onprogress
  292. xhr.send(options.data)
  293. })
  294. }
  295. export const ssePost = (url: string, fetchOptions: FetchOptionType, { isPublicAPI = false, onData, onCompleted, onThought, onMessageEnd, onError, getAbortController }: IOtherOptions) => {
  296. const abortController = new AbortController()
  297. const options = Object.assign({}, baseOptions, {
  298. method: 'POST',
  299. signal: abortController.signal,
  300. }, fetchOptions)
  301. const contentType = options.headers.get('Content-Type')
  302. if (!contentType)
  303. options.headers.set('Content-Type', ContentType.json)
  304. getAbortController?.(abortController)
  305. const urlPrefix = isPublicAPI ? PUBLIC_API_PREFIX : API_PREFIX
  306. const urlWithPrefix = `${urlPrefix}${url.startsWith('/') ? url : `/${url}`}`
  307. const { body } = options
  308. if (body)
  309. options.body = JSON.stringify(body)
  310. globalThis.fetch(urlWithPrefix, options as RequestInit)
  311. .then((res) => {
  312. if (!/^(2|3)\d{2}$/.test(String(res.status))) {
  313. res.json().then((data: any) => {
  314. Toast.notify({ type: 'error', message: data.message || 'Server Error' })
  315. })
  316. onError?.('Server Error')
  317. return
  318. }
  319. return handleStream(res, (str: string, isFirstMessage: boolean, moreInfo: IOnDataMoreInfo) => {
  320. if (moreInfo.errorMessage) {
  321. // debugger
  322. onError?.(moreInfo.errorMessage, moreInfo.errorCode)
  323. if (moreInfo.errorMessage !== 'AbortError: The user aborted a request.')
  324. Toast.notify({ type: 'error', message: moreInfo.errorMessage })
  325. return
  326. }
  327. onData?.(str, isFirstMessage, moreInfo)
  328. }, onCompleted, onThought, onMessageEnd)
  329. }).catch((e) => {
  330. if (e.toString() !== 'AbortError: The user aborted a request.')
  331. Toast.notify({ type: 'error', message: e })
  332. onError?.(e)
  333. })
  334. }
  335. // base request
  336. export const request = <T>(url: string, options = {}, otherOptions?: IOtherOptions) => {
  337. return baseFetch<T>(url, options, otherOptions || {})
  338. }
  339. // request methods
  340. export const get = <T>(url: string, options = {}, otherOptions?: IOtherOptions) => {
  341. return request<T>(url, Object.assign({}, options, { method: 'GET' }), otherOptions)
  342. }
  343. // For public API
  344. export const getPublic = <T>(url: string, options = {}, otherOptions?: IOtherOptions) => {
  345. return get<T>(url, options, { ...otherOptions, isPublicAPI: true })
  346. }
  347. export const post = <T>(url: string, options = {}, otherOptions?: IOtherOptions) => {
  348. return request<T>(url, Object.assign({}, options, { method: 'POST' }), otherOptions)
  349. }
  350. export const postPublic = <T>(url: string, options = {}, otherOptions?: IOtherOptions) => {
  351. return post<T>(url, options, { ...otherOptions, isPublicAPI: true })
  352. }
  353. export const put = <T>(url: string, options = {}, otherOptions?: IOtherOptions) => {
  354. return request<T>(url, Object.assign({}, options, { method: 'PUT' }), otherOptions)
  355. }
  356. export const putPublic = <T>(url: string, options = {}, otherOptions?: IOtherOptions) => {
  357. return put<T>(url, options, { ...otherOptions, isPublicAPI: true })
  358. }
  359. export const del = <T>(url: string, options = {}, otherOptions?: IOtherOptions) => {
  360. return request<T>(url, Object.assign({}, options, { method: 'DELETE' }), otherOptions)
  361. }
  362. export const delPublic = <T>(url: string, options = {}, otherOptions?: IOtherOptions) => {
  363. return del<T>(url, options, { ...otherOptions, isPublicAPI: true })
  364. }
  365. export const patch = <T>(url: string, options = {}, otherOptions?: IOtherOptions) => {
  366. return request<T>(url, Object.assign({}, options, { method: 'PATCH' }), otherOptions)
  367. }
  368. export const patchPublic = <T>(url: string, options = {}, otherOptions?: IOtherOptions) => {
  369. return patch<T>(url, options, { ...otherOptions, isPublicAPI: true })
  370. }