wwf
3 天以前 a430284aa21e3ae1f0d5654e55b2ad2852519cc2
app/components/base/chat/utils.ts
@@ -3,42 +3,20 @@
import type { ChatItem, ChatItemInTree } from './types'
async function decodeBase64AndDecompress(base64String: string) {
  try {
    const binaryString = atob(base64String)
    const compressedUint8Array = Uint8Array.from(binaryString, char => char.charCodeAt(0))
    const decompressedStream = new Response(compressedUint8Array).body?.pipeThrough(new DecompressionStream('gzip'))
    const decompressedArrayBuffer = await new Response(decompressedStream).arrayBuffer()
    return new TextDecoder().decode(decompressedArrayBuffer)
  }
  catch {
    return undefined
  }
}
async function getProcessedInputsFromUrlParams(): Promise<Record<string, any>> {
function getProcessedInputsFromUrlParams(): Record<string, any> {
  const urlParams = new URLSearchParams(window.location.search)
  const inputs: Record<string, any> = {}
  const entriesArray = Array.from(urlParams.entries())
  await Promise.all(
    entriesArray.map(async ([key, value]) => {
      if (!key.startsWith('sys.'))
  urlParams.forEach(async (value, key) => {
        inputs[key] = await decodeBase64AndDecompress(decodeURIComponent(value))
    }),
  )
  })
  return inputs
}
async function getProcessedSystemVariablesFromUrlParams(): Promise<Record<string, any>> {
  const urlParams = new URLSearchParams(window.location.search)
  const systemVariables: Record<string, any> = {}
  const entriesArray = Array.from(urlParams.entries())
  await Promise.all(
    entriesArray.map(async ([key, value]) => {
      if (key.startsWith('sys.'))
        systemVariables[key.slice(4)] = await decodeBase64AndDecompress(decodeURIComponent(value))
    }),
  )
  return systemVariables
}
function isValidGeneratedAnswer(item?: ChatItem | ChatItemInTree): boolean {
@@ -122,7 +100,7 @@
  let targetNode: ChatItemInTree | undefined
  // find path to the target message
  const stack = tree.slice().reverse().map(rootNode => ({
  const stack = tree.toReversed().map(rootNode => ({
    node: rootNode,
    path: [rootNode],
  }))
@@ -185,7 +163,6 @@
export {
  getProcessedInputsFromUrlParams,
  getProcessedSystemVariablesFromUrlParams,
  isValidGeneratedAnswer,
  getLastAnswer,
  buildChatItemTree,