utils.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { addFileInfos, sortAgentSorts } from '../../tools/utils'
  2. import { UUID_NIL } from './constants'
  3. import type { ChatItem } from './types'
  4. async function decodeBase64AndDecompress(base64String: string) {
  5. const binaryString = atob(base64String)
  6. const compressedUint8Array = Uint8Array.from(binaryString, char => char.charCodeAt(0))
  7. const decompressedStream = new Response(compressedUint8Array).body?.pipeThrough(new DecompressionStream('gzip'))
  8. const decompressedArrayBuffer = await new Response(decompressedStream).arrayBuffer()
  9. return new TextDecoder().decode(decompressedArrayBuffer)
  10. }
  11. function getProcessedInputsFromUrlParams(): Record<string, any> {
  12. const urlParams = new URLSearchParams(window.location.search)
  13. const inputs: Record<string, any> = {}
  14. urlParams.forEach(async (value, key) => {
  15. inputs[key] = await decodeBase64AndDecompress(decodeURIComponent(value))
  16. })
  17. return inputs
  18. }
  19. function appendQAToChatList(chatList: ChatItem[], item: any) {
  20. // we append answer first and then question since will reverse the whole chatList later
  21. chatList.push({
  22. id: item.id,
  23. content: item.answer,
  24. agent_thoughts: addFileInfos(item.agent_thoughts ? sortAgentSorts(item.agent_thoughts) : item.agent_thoughts, item.message_files),
  25. feedback: item.feedback,
  26. isAnswer: true,
  27. citation: item.retriever_resources,
  28. message_files: item.message_files?.filter((file: any) => file.belongs_to === 'assistant') || [],
  29. })
  30. chatList.push({
  31. id: `question-${item.id}`,
  32. content: item.query,
  33. isAnswer: false,
  34. message_files: item.message_files?.filter((file: any) => file.belongs_to === 'user') || [],
  35. })
  36. }
  37. /**
  38. * Computes the latest thread messages from all messages of the conversation.
  39. * Same logic as backend codebase `api/core/prompt/utils/extract_thread_messages.py`
  40. *
  41. * @param fetchedMessages - The history chat list data from the backend, sorted by created_at in descending order. This includes all flattened history messages of the conversation.
  42. * @returns An array of ChatItems representing the latest thread.
  43. */
  44. function getPrevChatList(fetchedMessages: any[]) {
  45. const ret: ChatItem[] = []
  46. let nextMessageId = null
  47. for (const item of fetchedMessages) {
  48. if (!item.parent_message_id) {
  49. appendQAToChatList(ret, item)
  50. break
  51. }
  52. if (!nextMessageId) {
  53. appendQAToChatList(ret, item)
  54. nextMessageId = item.parent_message_id
  55. }
  56. else {
  57. if (item.id === nextMessageId || nextMessageId === UUID_NIL) {
  58. appendQAToChatList(ret, item)
  59. nextMessageId = item.parent_message_id
  60. }
  61. }
  62. }
  63. return ret.reverse()
  64. }
  65. export {
  66. getProcessedInputsFromUrlParams,
  67. getPrevChatList,
  68. }