From f3ef4c77f5d4795b8a028397f9097427ce74e69e Mon Sep 17 00:00:00 2001 From: icarus Date: Sun, 14 Sep 2025 00:00:07 +0800 Subject: [PATCH] feat(agents): add CRUD operations for agents in store Add setAgents, addAgent, removeAgent and updateAgent actions to manage agents in the store. The updateAgent action uses lodash's mergeWith to handle array references properly and includes error logging when agent is not found. --- src/renderer/src/store/agents.ts | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/src/renderer/src/store/agents.ts b/src/renderer/src/store/agents.ts index 1b0c087175..aea5b7e02e 100644 --- a/src/renderer/src/store/agents.ts +++ b/src/renderer/src/store/agents.ts @@ -1,7 +1,10 @@ +import { loggerService } from '@logger' import { createSlice, PayloadAction } from '@reduxjs/toolkit' import { DEFAULT_CONTEXTCOUNT, DEFAULT_TEMPERATURE } from '@renderer/config/constant' import { AgentEntity, AssistantPreset, AssistantSettings } from '@renderer/types' +import { cloneDeep, mergeWith } from 'lodash' +const logger = loggerService.withContext('Agents') export interface AgentsState { /** They are actually assistant presets. * They should not be in this slice. However, since redux will be removed @@ -53,6 +56,28 @@ const assistantsSlice = createSlice({ } } } + }, + setAgents: (state, action: PayloadAction) => { + state.agentsNew = action.payload + }, + addAgent: (state, action: PayloadAction) => { + state.agentsNew.push(action.payload) + }, + removeAgent: (state, action: PayloadAction<{ id: string }>) => { + state.agentsNew = state.agentsNew.filter((agent) => agent.id !== action.payload.id) + }, + updateAgent: (state, action: PayloadAction & { id: string }>) => { + const { id, ...update } = action.payload + const agent = state.agentsNew.find((agent) => agent.id === id) + if (agent) { + mergeWith(agent, update, (_, srcVal) => { + // cut reference + if (Array.isArray(srcVal)) return cloneDeep(srcVal) + else return undefined + }) + } else { + logger.warn('Agent not found when trying to update') + } } } }) @@ -62,7 +87,11 @@ export const { addAssistantPreset, removeAssistantPreset, updateAssistantPreset, - updateAssistantPresetSettings + updateAssistantPresetSettings, + setAgents, + addAgent, + removeAgent, + updateAgent } = assistantsSlice.actions export default assistantsSlice.reducer