feat:智能体配置加载数据库默认配置

This commit is contained in:
liailing1026
2026-03-13 14:55:58 +08:00
parent a9ae017504
commit 41832ae2d2
3 changed files with 78 additions and 45 deletions

View File

@@ -89,8 +89,16 @@ class Api {
return `hsl(${h}, ${s}%, ${l}%)`
}
setAgents = (data: Pick<Agent, 'Name' | 'Profile' | 'apiUrl' | 'apiKey' | 'apiModel'>[]) =>
websocket.send('set_agents', data)
setAgents = (data: Pick<Agent, 'Name' | 'Profile' | 'apiUrl' | 'apiKey' | 'apiModel'>[], saveToDb = false) => {
const storedUserId = localStorage.getItem('user_id')
// 为每个智能体添加 user_id
const payload = data.map(agent => ({
...agent,
...(storedUserId ? { user_id: storedUserId } : {})
}))
// saveToDb 为 true 时才写入数据库
return websocket.send('set_agents', { agents: payload, save_to_db: saveToDb })
}
getAgents = (user_id?: string) => {
// 优先使用传入的 user_id其次使用 localStorage 存储的,首次访问时不传

View File

@@ -15,16 +15,21 @@ const agentsStore = useAgentsStore()
// 如果agentsStore.agents不存在就读取默认配置的json文件
onMounted(async () => {
// 先调用 getAgents检查是否需要生成 user_id
// 先调用 getAgents获取用户的智能体配置(后端会自动回退到 default_user
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)
// 使用后端返回的智能体配置(数据库有就用数据库的,没有会用 default_user 的)
if (res && res.data && res.data.agents && res.data.agents.length > 0) {
agentsStore.setAgents(res.data.agents as Agent[])
} else if (!agentsStore.agents.length) {
// 只有在完全没有数据时才使用默认 json
const defaultAgents = await readConfig<Agent[]>('agent.json')
agentsStore.setAgents(defaultAgents)
}
await api.setAgents(
agentsStore.agents.map(item => pick(item, ['Name', 'Profile', 'Icon', 'Classification', 'apiUrl', 'apiKey', 'apiModel']))
)
@@ -98,9 +103,9 @@ const readFileContent = (file: File) => {
}))
agentsStore.setAgents(processedAgents)
// 调用API
// 调用APIsaveToDb=true 表示写入数据库
api
.setAgents(processedAgents)
.setAgents(processedAgents, true)
.then(() => {
success('智能体上传成功')
})