feat:历史记录关于user_id限制

This commit is contained in:
liailing1026
2026-03-09 10:28:07 +08:00
parent 8926480e62
commit 14b79bc282
6 changed files with 69 additions and 19 deletions

View File

@@ -92,7 +92,12 @@ class Api {
setAgents = (data: Pick<Agent, 'Name' | 'Profile' | 'apiUrl' | 'apiKey' | 'apiModel'>[]) =>
websocket.send('set_agents', data)
getAgents = (user_id: string = 'default_user') => websocket.send('get_agents', { user_id })
getAgents = (user_id?: string) => {
// 优先使用传入的 user_id其次使用 localStorage 存储的,首次访问时不传
const storedUserId = localStorage.getItem('user_id')
const sendUserId = user_id || storedUserId
return websocket.send('get_agents', sendUserId ? { user_id: sendUserId } : {})
}
generateBasePlan = (data: {
goal: string
@@ -106,8 +111,9 @@ class Api {
message?: string
[key: string]: any
}) => void
}) =>
websocket.send(
}) => {
const storedUserId = localStorage.getItem('user_id')
return websocket.send(
'generate_base_plan',
{
'General Goal': data.goal,
@@ -115,10 +121,12 @@ class Api {
apiUrl: data.apiUrl,
apiKey: data.apiKey,
apiModel: data.apiModel,
...(storedUserId ? { user_id: storedUserId } : {}),
},
undefined,
data.onProgress,
)
}
/**
* 优化版流式执行计划(支持动态追加步骤)
@@ -830,10 +838,12 @@ class Api {
throw new Error('WebSocket未连接')
}
const storedUserId = localStorage.getItem('user_id')
const sendUserId = params.user_id || storedUserId
const rawResponse = await websocket.send('export', {
task_id: params.task_id,
export_type: params.export_type,
user_id: params.user_id || 'anonymous',
...(sendUserId ? { user_id: sendUserId } : {}),
})
const response = this.extractResponse<{

View File

@@ -15,6 +15,12 @@ const agentsStore = useAgentsStore()
// 如果agentsStore.agents不存在就读取默认配置的json文件
onMounted(async () => {
// 先调用 getAgents检查是否需要生成 user_id
const res = await api.getAgents()
if (res && res.user_id) {
console.log('[AgentRepo] 获取到 user_id:', res.user_id)
}
if (!agentsStore.agents.length) {
const res = await readConfig<Agent[]>('agent.json')
agentsStore.setAgents(res)

View File

@@ -115,9 +115,10 @@ const historyUpdatedHandler = () => {
const fetchPlans = async () => {
loading.value = true
const reqId = generateRequestId()
const userId = localStorage.getItem('user_id')
try {
const result = await websocket.send('get_plans', { id: reqId })
const result = await websocket.send('get_plans', { id: reqId, user_id: userId })
plans.value = (result.data || []) as PlanInfo[]
} catch (error) {
console.error('获取任务列表失败:', error)

View File

@@ -105,11 +105,18 @@ class WebSocketClient {
const resolvedGenerationId = generation_id || (data && typeof data === 'object' && !Array.isArray(data) && data.generation_id)
const resolvedExecutionId = execution_id || (data && typeof data === 'object' && !Array.isArray(data) && data.execution_id)
// user_id 在 data 里面(后端返回格式:{ data: { user_id: 'xxx', ... } }
const resolvedUserId = data && typeof data === 'object' && !Array.isArray(data) && data.user_id
if (resolvedUserId) {
localStorage.setItem('user_id', resolvedUserId)
}
// 直接返回 data保持原始数据结构数组或对象
handler.resolve({
data,
generation_id: resolvedGenerationId,
execution_id: resolvedExecutionId
execution_id: resolvedExecutionId,
user_id: resolvedUserId
})
} else {
handler.reject(new Error(error || 'Unknown error'))