base.ts 18 KB

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