| | |
| | | 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 |
| | | } |
| | | 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) |
| | | } |
| | | |
| | | 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.')) |
| | | inputs[key] = await decodeBase64AndDecompress(decodeURIComponent(value)) |
| | | }), |
| | | ) |
| | | 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 { |
| | |
| | | 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], |
| | | })) |
| | |
| | | |
| | | export { |
| | | getProcessedInputsFromUrlParams, |
| | | getProcessedSystemVariablesFromUrlParams, |
| | | isValidGeneratedAnswer, |
| | | getLastAnswer, |
| | | buildChatItemTree, |