feat:1.数据库存储功能添加(初版)2.后端REST API版本代码清理

This commit is contained in:
liailing1026
2026-02-25 10:55:51 +08:00
parent f736cd104a
commit 2140cfaf92
35 changed files with 3912 additions and 2981 deletions

View File

@@ -4,6 +4,7 @@ import { v4 as uuidv4 } from 'uuid'
import { store } from '../index'
import type { IRawStepTask, IApiStepTask } from './agents'
import type { Node, Edge } from '@vue-flow/core'
import { useAgentsStoreHook } from './agents'
/**
* 分支数据接口
@@ -243,6 +244,107 @@ export const useSelectionStore = defineStore('selection', () => {
}
}
// ==================== 数据库持久化方法 ====================
/**
* 从数据库恢复分支数据
* @param dbBranches 从数据库读取的分支数据数组
*/
function restoreBranchesFromDB(dbBranches: IBranchData[]) {
// 清除现有分支
clearFlowBranches()
// 恢复分支数据到 store
dbBranches.forEach((branch) => {
if (branch && branch.id && branch.tasks) {
addFlowBranch({
parentNodeId: branch.parentNodeId,
branchContent: branch.branchContent,
branchType: branch.branchType,
nodes: branch.nodes || [],
edges: branch.edges || [],
tasks: branch.tasks,
})
}
})
}
/**
* 保存分支数据到数据库
* @param taskId 任务ID
* @returns Promise<boolean> 是否保存成功
*/
async function saveBranchesToDB(taskId: string): Promise<boolean> {
// 导入 api避免循环导入问题
const { default: api } = await import('@/api')
const branches = getAllFlowBranches()
const result = await api.saveBranches(taskId, branches)
return result
}
/**
* 保存任务过程分支数据到数据库
* @param TaskID 大任务ID数据库主键
* @returns Promise<boolean> 是否保存成功
* @description 存储结构: Map<taskStepId, Map<agentGroupKey, IBranchData[]>>
*/
async function saveTaskProcessBranchesToDB(TaskID: string): Promise<boolean> {
// 导入 api避免循环导入问题
const { default: api } = await import('@/api')
// 将 Map 转换为普通对象,便于序列化
const branchesMap = getAllTaskProcessBranches()
const branchesObj: Record<string, Record<string, any[]>> = {}
for (const [taskStepId, agentMap] of branchesMap.entries()) {
branchesObj[taskStepId] = {}
for (const [agentGroupKey, branches] of agentMap.entries()) {
branchesObj[taskStepId][agentGroupKey] = branches
}
}
const result = await api.saveTaskProcessBranches(TaskID, branchesObj)
return result
}
/**
* 从数据库恢复任务过程分支数据
* @param dbBranches 从数据库读取的任务过程分支数据
* @param TaskID 大任务ID用于获取全局 __CURRENT_TASK_ID__
* @description 数据格式: { stepId小任务UUID: { agentGroupKey: [IBranchData...] } }
*/
function restoreTaskProcessBranchesFromDB(dbBranches: Record<string, Record<string, any[]>>) {
// 清除现有数据
taskProcessBranchesMap.value.clear()
if (!dbBranches) {
return
}
// 恢复数据
for (const [taskStepId, agentMap] of Object.entries(dbBranches)) {
if (typeof agentMap !== 'object' || agentMap === null) {
continue
}
// 获取或创建该步骤的 Map
if (!taskProcessBranchesMap.value.has(taskStepId)) {
taskProcessBranchesMap.value.set(taskStepId, new Map())
}
const stepMap = taskProcessBranchesMap.value.get(taskStepId)!
// 恢复 agent combinations
for (const [agentGroupKey, branches] of Object.entries(agentMap)) {
if (Array.isArray(branches)) {
stepMap.set(agentGroupKey, branches)
}
}
}
}
// ==================== Agent 组合 TaskProcess 数据存储 ====================
/**
* Agent 组合 TaskProcess 数据映射
@@ -270,20 +372,34 @@ export const useSelectionStore = defineStore('selection', () => {
/**
* 存储 agent 组合的 TaskProcess 数据
* @param taskId 任务 ID
* @param stepId 步骤 ID小任务 UUID用于作为 agentTaskProcessMap 的第一层主键)
* @param agents Agent 列表
* @param taskProcess TaskProcess 数据
* @param taskProcess TaskProcess 数据(支持完整格式或简化格式)
* @description 存储结构: Map<stepId, Map<agentGroupKey, { process, brief }>>
* agentTaskProcessMap = { stepId: { agentGroupKey: { process, brief } } }
* 注意:简化格式 { process, brief } 与后端数据库格式一致
*/
function setAgentTaskProcess(taskId: string, agents: string[], taskProcess: IApiStepTask) {
function setAgentTaskProcess(
stepId: string,
agents: string[],
taskProcess: IApiStepTask | { process: any; brief: any },
) {
const groupKey = getAgentGroupKey(agents)
// 获取或创建该任务的 Map
if (!agentTaskProcessMap.value.has(taskId)) {
agentTaskProcessMap.value.set(taskId, new Map())
// 获取或创建该步骤的 Map
if (!agentTaskProcessMap.value.has(stepId)) {
agentTaskProcessMap.value.set(stepId, new Map())
}
const stepMap = agentTaskProcessMap.value.get(stepId)!
// 统一转换为简化格式 { process, brief },与后端期望格式一致
const simplifiedData = {
process: (taskProcess as IApiStepTask).process || taskProcess.process || [],
brief: (taskProcess as IApiStepTask).brief || taskProcess.brief || {},
}
// 存储该 agent 组合的 TaskProcess 数据
agentTaskProcessMap.value.get(taskId)!.set(groupKey, taskProcess)
stepMap.set(groupKey, simplifiedData)
}
/**
@@ -321,6 +437,97 @@ export const useSelectionStore = defineStore('selection', () => {
agentTaskProcessMap.value.clear()
}
/**
* 获取指定步骤的所有 agent 组合的 TaskProcess 数据
* @param stepId 步骤 ID小任务 UUID
* @returns agent_combinations 对象格式 { "[\"AgentA\",\"AgentB\"]": { process, brief }, ... }
* @description 存储结构: Map<stepId, Map<agentGroupKey, { process, brief }>>
* 注意:简化设计后,第一层 key 就是 stepId
*/
function getAgentCombinations(
stepId: string,
): Record<string, { process: any; brief: any }> | undefined {
// 直接使用 stepId 作为第一层 key
const stepMap = agentTaskProcessMap.value.get(stepId)
if (!stepMap) {
return undefined
}
// 将 Map 转换为普通对象
const result: Record<string, { process: any; brief: any }> = {}
for (const [key, value] of stepMap.entries()) {
result[key] = value
}
return result
}
// ==================== 数据库持久化恢复方法 ====================
/**
* 从数据库 assigned_agents 字段恢复 agent 组合数据
* @param assignedAgents 从数据库读取的 assigned_agents 数据
* @param taskId 大任务 ID此参数已不再使用因为简化后直接用 stepId 作为 key
* @description 数据格式: { step_id小任务UUID: { current: [...], confirmed_groups: [...], agent_combinations: {...} } }
* 存储结构: Map<stepId, Map<agentGroupKey, IApiStepTask>>
* agentTaskProcessMap = { stepId: { agentGroupKey: data } }
*/
function restoreAgentCombinationsFromDB(assignedAgents: Record<string, any>, taskId: string) {
// 获取 agents store 实例
const agentsStore = useAgentsStoreHook()
// 清除现有数据
clearAllAgentTaskProcess()
agentsStore.clearAllConfirmedAgentGroups()
if (!assignedAgents) {
return
}
// 🆕 简化版本:直接使用 stepId 作为第一层 key
// 遍历每个步骤的数据
for (const [stepId, stepData] of Object.entries(assignedAgents)) {
if (typeof stepData !== 'object' || stepData === null) {
continue
}
// 获取或创建该步骤的 Map
if (!agentTaskProcessMap.value.has(stepId)) {
agentTaskProcessMap.value.set(stepId, new Map<string, IApiStepTask>())
}
const stepMap = agentTaskProcessMap.value.get(stepId)!
// 恢复 agent_combinations 到 stepMap
// 格式: { agentGroupKey: { process, brief } }
if (stepData.agent_combinations) {
for (const [agentGroupKey, combinationData] of Object.entries(
stepData.agent_combinations,
)) {
stepMap.set(agentGroupKey, combinationData as IApiStepTask)
}
}
// 恢复 confirmed_groups
if (stepData.confirmed_groups) {
agentsStore.setConfirmedAgentGroups(stepId, stepData.confirmed_groups)
}
// 恢复 current当前选中的 agent 组合)
if (stepData.current) {
agentsStore.setSelectedAgentGroup(stepId, stepData.current)
// 同步更新 agentRawPlan 中对应步骤的 AgentSelection
const planData = agentsStore.agentRawPlan.data
if (planData && planData['Collaboration Process']) {
const process = planData['Collaboration Process']
const step = process.find((s: any) => s.Id === stepId)
if (step) {
step.AgentSelection = stepData.current
}
}
}
}
}
// ==================== 当前生效的任务过程分支 ====================
/**
* 当前生效的任务过程分支映射
@@ -450,7 +657,15 @@ export const useSelectionStore = defineStore('selection', () => {
getAgentTaskProcess,
hasAgentTaskProcess,
clearAgentTaskProcess,
getAgentCombinations,
clearAllAgentTaskProcess,
// ==================== 数据库持久化方法 ====================
restoreBranchesFromDB,
saveBranchesToDB,
restoreAgentCombinationsFromDB,
saveTaskProcessBranchesToDB,
restoreTaskProcessBranchesFromDB,
}
})