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

@@ -5,6 +5,7 @@ import { getAgentMapIcon } from '@/layout/components/config'
import { store } from '../index'
import { useStorage } from '@vueuse/core'
import type { IExecuteRawResponse } from '@/api'
import api from '@/api'
import { useConfigStore } from '@/stores/modules/config.ts'
export interface Agent {
Name: string
@@ -102,14 +103,14 @@ export interface IRawPlanResponse {
* @returns TaskProcess 数组
*/
export function convertToTaskProcess(
actions: { id: string; type: string; agent: string; description: string; inputs?: string[] }[]
actions: { id: string; type: string; agent: string; description: string; inputs?: string[] }[],
): TaskProcess[] {
return actions.map(action => ({
return actions.map((action) => ({
ID: action.id,
ActionType: action.type,
AgentName: action.agent,
Description: action.description,
ImportantInput: action.inputs || []
ImportantInput: action.inputs || [],
}))
}
@@ -129,16 +130,16 @@ export const useAgentsStore = defineStore('agents', () => {
agents.value = agent
}
// 🆕 新的按任务ID存储的评分数据
// 新的按任务ID存储的评分数据
const taskScoreDataMap = useStorage<Record<string, ITaskScoreData>>(
`${storageKey}-task-score-data`,
{},
)
// 🆕 预加载状态追踪(用于避免重复预加载)
// 预加载状态追踪(用于避免重复预加载)
const preloadingTaskIds = ref<Set<string>>(new Set())
// 🆕 获取指定任务的评分数据按任务ID获取
// 获取指定任务的评分数据按任务ID获取
function getTaskScoreData(taskId: string): IAgentSelectModifyAddRequest | null {
if (!taskId) {
console.warn('⚠️ getTaskScoreData: taskId 为空')
@@ -147,7 +148,9 @@ export const useAgentsStore = defineStore('agents', () => {
const taskScoreData = taskScoreDataMap.value[taskId]
if (taskScoreData) {
console.log(`✅ 使用任务 ${taskId} 的缓存评分数据,维度数: ${taskScoreData.aspectList.length}`)
console.log(
`✅ 使用任务 ${taskId} 的缓存评分数据,维度数: ${taskScoreData.aspectList.length}`,
)
return {
aspectList: taskScoreData.aspectList,
agentScores: taskScoreData.agentScores,
@@ -284,6 +287,11 @@ export const useAgentsStore = defineStore('agents', () => {
confirmedAgentGroupsMap.value.clear()
}
// 设置指定任务的确认的agent组合列表用于从数据库恢复
function setConfirmedAgentGroups(taskId: string, groups: string[][]) {
confirmedAgentGroupsMap.value.set(taskId, groups)
}
const planModificationWindow = ref(false)
const planTaskWindow = ref(false)
const agentAllocationDialog = ref(false)
@@ -350,7 +358,7 @@ export const useAgentsStore = defineStore('agents', () => {
function setCurrentTask(task: IRawStepTask) {
const existingTask = currentTask.value
// 🆕 智能判断:如果是同一个任务,保留用户修改过的数据AgentSelection、TaskProcess、Collaboration_Brief_frontEnd
// 智能判断:如果是同一个任务,保留用户修改过的数据
if (existingTask && existingTask.Id === task.Id) {
currentTask.value = {
...task,
@@ -360,7 +368,7 @@ export const useAgentsStore = defineStore('agents', () => {
existingTask.Collaboration_Brief_frontEnd || task.Collaboration_Brief_frontEnd,
}
// 🆕 同步更新主流程数据(让执行结果卡片和任务大纲都能联动)
// 同步更新主流程数据(让执行结果卡片和任务大纲都能联动)
syncCurrentTaskToMainProcess(currentTask.value)
console.log('🔄 setCurrentTask: 保留同一任务的分支数据', {
@@ -431,10 +439,11 @@ export const useAgentsStore = defineStore('agents', () => {
// 🆕 更新当前任务的 AgentSelection 和 TaskProcess用于在 AgentAllocation 中切换 agent 组合)
// 此函数专门用于强制更新,不会被 setCurrentTask 的"智能保留"逻辑阻止
function updateCurrentAgentSelection(
async function updateCurrentAgentSelection(
agentSelection: string[],
taskProcess: TaskProcess[],
collaborationBrief: any,
agentCombinations?: Record<string, { process: any; brief: any }>,
) {
if (currentTask.value) {
// 直接更新 currentTask不保留旧数据
@@ -453,7 +462,40 @@ export const useAgentsStore = defineStore('agents', () => {
taskName: currentTask.value.StepName,
newAgentSelection: agentSelection,
taskProcessLength: taskProcess.length,
agentCombinationsKeys: agentCombinations ? Object.keys(agentCombinations) : undefined,
})
// 🆕 保存 assigned_agents 到数据库
const dbTaskId = (window as any).__CURRENT_TASK_ID__
const stepId = currentTask.value?.Id
// 获取已确认的 agent 组合列表
const confirmedGroups = stepId ? getConfirmedAgentGroups(stepId) : []
console.log('[updateCurrentAgentSelection] 保存到数据库:', {
dbTaskId,
stepId,
agents: agentSelection,
confirmedGroupsCount: confirmedGroups?.length,
agentCombinationsKeys: agentCombinations ? Object.keys(agentCombinations) : undefined,
})
if (dbTaskId && stepId) {
try {
console.log('[updateCurrentAgentSelection] 调用 APIconfirmedGroups:', confirmedGroups)
await api.updateAssignedAgents({
task_id: dbTaskId,
step_id: stepId,
agents: agentSelection,
confirmed_groups: confirmedGroups,
agent_combinations: agentCombinations,
})
} catch (error) {
console.error('❌ 保存 assigned_agents 到数据库失败:', error)
}
} else {
console.warn('[updateCurrentAgentSelection] dbTaskId 或 stepId 为空,跳过保存', {
dbTaskId,
stepId,
})
}
}
}
@@ -542,9 +584,9 @@ export const useAgentsStore = defineStore('agents', () => {
agents,
brief: currentTask.value?.Collaboration_Brief_frontEnd || {
template: '',
data: {}
data: {},
},
process: []
process: [],
}
}
@@ -602,6 +644,7 @@ export const useAgentsStore = defineStore('agents', () => {
addConfirmedAgentGroup,
clearConfirmedAgentGroups,
clearAllConfirmedAgentGroups,
setConfirmedAgentGroups,
// 停止填充状态
hasStoppedFilling,
setHasStoppedFilling,

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,
}
})