feat: allow the embedding in websites to customize sys.user_id (#16062)

This commit is contained in:
Panpan
2025-03-31 18:55:42 +08:00
committed by GitHub
parent 6cf258a809
commit 24b1a625b3
7 changed files with 83 additions and 24 deletions

View File

@@ -44,7 +44,10 @@ const OPTION_MAP = {
: ''}${IS_CE_EDITION
? `,
baseUrl: '${url}'`
: ''}
: ''},
systemVariables: {
// user_id: 'YOU CAN DEFINE USER ID HERE',
},
}
</script>
<script

View File

@@ -3,11 +3,16 @@ import type { IChatItem } from './chat/type'
import type { ChatItem, ChatItemInTree } from './types'
async function decodeBase64AndDecompress(base64String: string) {
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)
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>> {
@@ -16,12 +21,26 @@ async function getProcessedInputsFromUrlParams(): Promise<Record<string, any>> {
const entriesArray = Array.from(urlParams.entries())
await Promise.all(
entriesArray.map(async ([key, value]) => {
inputs[key] = await decodeBase64AndDecompress(decodeURIComponent(value))
if (!key.startsWith('sys.'))
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 {
return !!item && item.isAnswer && !item.id.startsWith('answer-placeholder-') && !item.isOpeningStatement
}
@@ -166,6 +185,7 @@ function getThreadMessages(tree: ChatItemInTree[], targetMessageId?: string): Ch
export {
getProcessedInputsFromUrlParams,
getProcessedSystemVariablesFromUrlParams,
isValidGeneratedAnswer,
getLastAnswer,
buildChatItemTree,

View File

@@ -1,5 +1,6 @@
import { CONVERSATION_ID_INFO } from '../base/chat/constants'
import { fetchAccessToken } from '@/service/share'
import { getProcessedSystemVariablesFromUrlParams } from '../base/chat/utils'
export const checkOrSetAccessToken = async () => {
const sharedToken = globalThis.location.pathname.split('/').slice(-1)[0]
@@ -12,7 +13,8 @@ export const checkOrSetAccessToken = async () => {
}
if (!accessTokenJson[sharedToken]) {
const res = await fetchAccessToken(sharedToken)
const sysUserId = (await getProcessedSystemVariablesFromUrlParams()).user_id
const res = await fetchAccessToken(sharedToken, sysUserId)
accessTokenJson[sharedToken] = res.access_token
localStorage.setItem('token', JSON.stringify(accessTokenJson))
}