From fa6b90f7a8c77c2a81b1c30bb91a55fac8ad45e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B1=9F=E5=A4=8F=E5=B0=A7?= <3446798488@qq.com> Date: Thu, 22 May 2025 21:18:17 +0800 Subject: [PATCH 1/2] feat(swarm): Add an optional state update function to support custom state updates during handover --- libs/langgraph-swarm/src/handoff.ts | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/libs/langgraph-swarm/src/handoff.ts b/libs/langgraph-swarm/src/handoff.ts index 3f57ed222..6dede46fa 100644 --- a/libs/langgraph-swarm/src/handoff.ts +++ b/libs/langgraph-swarm/src/handoff.ts @@ -28,6 +28,7 @@ function _normalizeAgentName(agentName: string): string { interface CreateHandoffToolParams { agentName: string; description?: string; + updateState?: (state: any) => Record; } // type guard @@ -45,6 +46,7 @@ function isDynamicTool( const createHandoffTool = ({ agentName, description, + updateState, }: CreateHandoffToolParams) => { /** * Create a tool that can handoff control to the requested agent. @@ -56,6 +58,7 @@ const createHandoffTool = ({ * nodes as well as the tool names accepted by LLM providers * (the tool name will look like this: `transfer_to_`). * @param description - Optional description for the handoff tool. + * @param updateState - Optional function to customize state updates during handoff. */ const toolName = `transfer_to_${_normalizeAgentName(agentName)}`; const toolDescription = description || `Ask agent '${agentName}' for help`; @@ -74,13 +77,22 @@ const createHandoffTool = ({ // inject the current agent state const state = getCurrentTaskInput() as (typeof MessagesAnnotation)["State"]; + + // Base update object containing essential state updates + const baseUpdate = { + messages: state.messages.concat(toolMessage), + activeAgent: agentName, + }; + + // Merge custom updates with base updates if updateState function is provided + const finalUpdate = updateState + ? { ...baseUpdate, ...updateState(state) } + : baseUpdate; + return new Command({ goto: agentName, graph: Command.PARENT, - update: { - messages: state.messages.concat(toolMessage), - activeAgent: agentName, - }, + update: finalUpdate, }); }, { From 3eae4e79983a53c23ffee6f5a8223737f808e79e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B1=9F=E5=A4=8F=E5=B0=A7?= <3446798488@qq.com> Date: Thu, 22 May 2025 21:24:53 +0800 Subject: [PATCH 2/2] chore(swarm): code lint --- libs/langgraph-swarm/src/handoff.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/libs/langgraph-swarm/src/handoff.ts b/libs/langgraph-swarm/src/handoff.ts index 6dede46fa..a82253ad1 100644 --- a/libs/langgraph-swarm/src/handoff.ts +++ b/libs/langgraph-swarm/src/handoff.ts @@ -28,6 +28,7 @@ function _normalizeAgentName(agentName: string): string { interface CreateHandoffToolParams { agentName: string; description?: string; + // eslint-disable-next-line @typescript-eslint/no-explicit-any updateState?: (state: any) => Record; }