dify自定义userId

1,727 阅读1分钟

1、WebAPP地址:github.com/langgenius/…

2、修改user

import { type NextRequest } from 'next/server'
import { ChatClient } from 'dify-client'
import { v4 } from 'uuid'
import { API_KEY, API_URL, APP_ID } from '@/config'

// const userPrefix = `user_${APP_ID}:`

export const getInfo = (request: NextRequest) => {
  const sessionId = request.cookies.get('session_id')?.value || v4()
  const user = request.cookies.get('accountId')?.value
  return {
    sessionId,
    user,
  }
}

export const setSession = (sessionId: string) => {
  return { 'Set-Cookie': `session_id=${sessionId}` }
}

export const client = new ChatClient(API_KEY, API_URL || undefined)

3、下面是自定义逻辑

function GetUrlParamNew(name: string, link: string) {
  var name: string = escape(name),
    link = link == null || link == "" ? window.location.href : link,
    r = null;
  link = link.substring(link.indexOf("?"), link.length);
  /* 构造一个含有目标参数的正则表达式对象 */
  /* 匹配目标参数 */
  var reg = new RegExp(name + "=([^&]+)(?=&|$)");
  r = escape(link.substr(1))
    .replace(/%3D/g, "=")
    .replace(/%26/g, "&")
    .replace("searchKey", "searchkey")
    .match(reg);
  /* 返回参数值 */
  if (r != null) return decodeURIComponent(unescape(r[1]));
  return "";
}
if (typeof window !== 'undefined') {
  Cookies.set('accountId', GetUrlParamNew('accountId', window.location.href));
}