base.ts 18 KB

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