A multi-agent collaborative orchestration method and system based on a declarative state diagram
By using a declarative state diagram orchestration engine and immutable state management, the problems of scalability, state consistency and resource utilization in multi-agent collaborative systems are solved, multi-mode collaboration and session management are realized, and the reliability and efficiency of the system are improved.
Patent Information
- Application Number
- CN202611303945.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-08-26
- Publication Date
- 2026-11-06
AI Technical Summary
Existing multi-agent collaborative systems have shortcomings in scalability, state management, single collaboration mode, session lifecycle management, and reliability in concurrent scenarios, resulting in problems such as poor system scalability, data inconsistency, low resource utilization, chaotic task scheduling, and lack of session management.
It adopts a declarative state graph orchestration engine, defines collaboration processes through configuration, supports multiple collaboration modes, achieves immutable state management, and provides complete session lifecycle management, including session caching and timeout eviction mechanisms, to ensure data consistency and resource controllability.
It improves the system's scalability and development efficiency, eliminates the risk of data inconsistency in concurrent scenarios, supports multiple collaboration modes, optimizes resource utilization and task execution controllability, and avoids memory leaks and non-terminating dialogue failures.
Smart Images

Figure CN123048145A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the fields of artificial intelligence and distributed systems technology, specifically to a multi-agent collaborative orchestration method and system based on declarative state diagrams. Background Technology
[0002] With the rapid development of large language model technology, multi-agent systems based on large language models have become an important research direction in the field of artificial intelligence. Multi-agent systems decompose complex tasks into multiple sub-tasks, which are completed collaboratively by agents with different professional capabilities, and have shown broad application prospects in scenarios such as code review, document generation, data analysis, and knowledge graph construction.
[0003] Existing multi-agent cooperative systems mainly adopt the following three technical solutions: Option 1: Hard-coded Pipeline. Represented by LangChain's AgentExecutor, this approach hard-codes the execution order and interaction logic of agents within the program. AgentExecutor implements agent functionality through looped execution of tool calls and observation of feedback, with each collaborative process corresponding to an independent execution logic. However, traditional Agent components only support loop steps and not conditional steps. An Agent application can only execute along a single path, unable to execute different routing branches based on different conditions. Furthermore, multiple Agents are difficult to integrate and collaborate, resulting in poor scalability. Adding new collaborative modes requires modifying the core scheduling code, leading to high maintenance costs.
[0004] Option 2: Centralized Controller. Represented by AutoGen's GroupChat, this approach uses a central controller (GroupChatManager) to uniformly manage all agents. The controller maintains the global dialogue history and determines the next agent to speak. All agent messages are forwarded through the controller, making it a performance bottleneck and a single point of failure. Furthermore, GroupChat lacks a mechanism to verify the identities of participating agents, posing security risks in cross-organizational scenarios. The inability to pass or persist historical messages between sessions limits the continuity across sessions. Multiple agents in a group chat may engage in endless debates, ultimately requiring a manually set maximum number of rounds for truncation.
[0005] Option 3: Simple parallel execution. Represented by CrewAI's `Process.parallel`, this approach employs a strict sequential execution model, where the output of each agent serves as the input for the next. While this design works in simple scenarios, as the system scales, the throughput of the entire pipeline becomes limited by the slowest agent, resulting in low resource utilization; end-to-end latency increases linearly with the number of agents. Furthermore, frameworks like CrewAI often experience a significant increase in failure rate due to issues such as instruction races and state illusions. The core problem lies in the lack of native state management and collaborative governance capabilities in large language models.
[0006] The aforementioned existing technologies have the following main drawbacks: 1. Poor scalability. All the above solutions tightly couple the collaboration logic with the scheduling code. Taking LangChain as an example, traditional Agent components suffer from difficulties in multi-Agent collaboration and poor scalability. The tight coupling between LangGraph and LangChain limits independent control capabilities and increases the maintenance cost of asynchronous workflows. Forcing dynamic collaboration requirements onto a static orchestration framework leads to an exponential increase in maintenance costs.
[0007] 2. Chaotic State Management. Existing solutions use mutable global state objects to share data among agents. In parallel execution scenarios, multiple agents modifying the same state object simultaneously can lead to data inconsistency. For example, when two agents simultaneously read and write to the shared state, a race condition may occur—one agent's work is silently overwritten by another, without error reporting or alerting, ultimately delivering an incorrect result. Multiple independent nodes concurrently modifying the shared state without strong consistency guarantees will inevitably cause conflicts. The existing framework lacks a unified orchestration mechanism and fault tolerance, resulting in system instability and debugging difficulties.
[0008] 3. Limited Collaboration Modes. Existing solutions only support two basic modes: sequential and parallel. LangChain's AgentExecutor only supports loop steps and lacks conditional steps. For complex tasks requiring multi-stage coordination (such as decomposition, parallelization, and final review), developers need to manually orchestrate multiple execution flows, which cannot be achieved through declarative configuration.
[0009] 4. Lack of session lifecycle management. Existing solutions lack session management mechanisms for long-running tasks. When task execution time exceeds expectations, resources cannot be automatically released. When the system needs to shut down, it cannot gracefully wait for ongoing tasks to complete, leading to data loss or resource leaks.
[0010] 5. Reliability issues in concurrent scenarios. In multi-agent concurrent collaboration scenarios, problems such as chaotic task scheduling, resource contention, inconsistent states, aggravated error propagation, and lack of observability become prominent. Multi-agent systems based on large language models often experience a decrease in reliability when the number of agents is increased. The root cause lies in the concurrency control problem—agents concurrently read and write shared states, and the long inference window of large language models further exacerbates the risks of stale reads, lost updates, and inconsistent results.
[0011] To address the above shortcomings, this invention aims to solve the following technical problems: (1) Provide a declarative state graph orchestration engine, enabling collaborative processes to be defined through configuration rather than coding, and adding new collaborative modes does not require modification of the core scheduling code; (2) Implement immutable state management, ensuring data consistency in concurrent scenarios by returning a new state object each time execution; (3) Support unified scheduling of multiple collaborative modes, automatically selecting the optimal mode based on task characteristics through the mode selection component; (4) Provide complete session lifecycle management, including session caching, timeout eviction mechanism, and graceful shutdown mechanism.
[0012] Therefore, how to address the shortcomings of existing technologies is a technical problem that urgently needs to be solved by those skilled in the art. Summary of the Invention
[0013] To achieve the objectives of this invention, this application provides a multi-agent collaborative orchestration method based on declarative state diagrams, comprising: Step S1: In response to the received multi-agent cooperative task, determine the target cooperative mode from a variety of preset cooperative modes according to the task characteristics of the cooperative task. The task characteristics include at least the number of agents, task dependencies, and consistency requirements. Step S2: Based on the target collaboration mode, construct a declarative state diagram corresponding to the target collaboration mode. The declarative state diagram contains multiple nodes and edges connecting the nodes. The nodes include execution nodes for representing task decomposition or execution and sentinel nodes for representing the start and end of the execution process. The edges include unconditional edges representing unconditional transitions and conditional edges bound to state predicates. Step S3: Using the current state as input, execute the node logic corresponding to the current node in sequence according to the topology of the declarative state diagram, and generate a new immutable state based on the execution result after each node execution. The new immutable state contains a message list and variable mapping. Step S4: Determine the next node based on the new immutable state and the outgoing edge type of the current node. If the current node has a conditional edge, the state predicate bound to the conditional edge is called to evaluate the new immutable state, and the target edge is determined from the conditional edges based on the evaluation result and the process moves to the next node pointed to by the target edge. If the current node does not have a conditional edge or all conditional edges are not satisfied, the next node is determined based on the unconditional edge. Step S5: Repeat the execution of node logic and state transitions until the current node becomes a termination sentinel node or the preset termination condition is met, and then output the final execution result.
[0014] In some specific embodiments, in step S1, the multiple preset collaboration modes include sequential mode, parallel mode, hierarchical mode, consensus mode, and polling mode. Determining the target collaboration mode from the multiple preset collaboration modes based on the task characteristics of the collaborative task includes: When the number of agents is equal to 1, the target cooperation mode is determined to be a sequential mode; When the number of agents is greater than 1 and the task dependency is independent, the target cooperation mode is determined to be parallel mode. When the number of agents is greater than 1 and the task dependency is a complex dependency, the target collaboration mode is determined to be a hierarchical mode. When the consistency requirement is higher than a preset threshold, the target collaboration mode is determined to be a consensus mode. When the task type of the collaborative task is iterative optimization type, the target collaboration mode is determined to be polling mode.
[0015] In some specific embodiments, the step of constructing a declarative state diagram corresponding to the target collaboration mode includes: When the target collaboration mode is a sequential mode, a chain state diagram is constructed, and the execution nodes in the chain state diagram are connected sequentially according to the execution order. When the target collaboration mode is parallel mode, a fan-out-convergence state graph is constructed. The fan-out-convergence state graph includes multiple parallel edges fanning out from the starting sentinel node to multiple parallel execution nodes, and multiple convergence edges converging from the multiple parallel execution nodes to the merging node. The merging node is used to merge the output states of each parallel execution node after it is determined that all parallel execution nodes have been executed. When the target collaboration mode is a hierarchical mode, a tree-like state diagram is constructed, which includes a leader node and multiple worker nodes generated by the leader node after decomposing the task. When the target collaboration mode is a consensus mode, an independent path-convergence state graph is constructed. The independent path-convergence state graph includes multiple independent execution nodes and convergence edges connected to the consensus node. The consensus node is used to collect the opinions output by each independent execution node and make consensus decisions. When the target collaboration mode is polling mode, a circular state diagram is constructed. Multiple execution nodes in the circular state diagram are executed sequentially, and the next iteration is determined based on the quality assessment results.
[0016] In some specific embodiments, step S3 includes: Before executing the current node, obtain the input state of the current node, which includes the current message list, the current variable mapping, and the current execution step count; Execute the node logic corresponding to the current node, the node logic including calling the agent corresponding to the current node to process the input state and generate node output results, the node output results including appending messages and / or updating variables; A new immutable state is created based on the input state and the node output result. The message list in the new immutable state is a new list obtained by appending the appended message to the current message list. The variable mapping in the new immutable state is a new mapping obtained by applying the updated variable to the current variable mapping. The execution step number in the new immutable state is the current execution step number plus 1.
[0017] In some specific embodiments, step S4 includes: Obtain all outgoing edges of the current node in the declarative state graph, including unconditional edges and conditional edges; The conditional edges in all the outgoing edges are evaluated in a preset order: the state predicate bound to the conditional edge is called to determine the new immutable state. If the state predicate returns a true value, the conditional edge is determined as the target edge, and the evaluation of subsequent conditional edges is stopped. If the target edge exists, then move to the next node pointed to by the target edge; If all conditional edges are not satisfied and there is an unconditional edge, then move to the next node pointed to by the unconditional edge; If there are no outgoing edges that meet the conditions, then proceed to the terminating sentinel node.
[0018] In some specific embodiments, step S5 includes: After each node completes its execution, it checks whether the current node is a termination sentinel node. If so, execution stops and the current state is output as the final execution result. After each node is executed, it is determined whether the current cumulative number of execution steps has reached the maximum number of steps threshold. If the maximum number of steps threshold is reached, execution is stopped, the current state is marked as an abnormal termination state, and the execution result containing the reason for the abnormal termination is output. During each node execution process, if an exception is detected in the current node execution, the exception is captured and the current state is marked as an error state. The process is then transferred to the predefined error handling node in the declarative state diagram or directly to the termination sentinel node.
[0019] In some specific embodiments, session lifecycle management is also included: Upon receiving the collaborative task, a corresponding session is created for the collaborative task, and the session includes a session identifier, the current state, and the last active time; Each time the status is updated, the last active time of the session is updated to the current time; All sessions are scanned at preset time intervals, and sessions whose difference between the last active time and the current time exceeds a preset survival time threshold are identified as expired sessions and evicted. When the system shuts down, it stops accepting new tasks, waits for ongoing tasks to complete, and forcibly terminates unfinished tasks after a timeout.
[0020] To achieve the same inventive objective, this application also provides a multi-agent cooperative orchestration system based on declarative state diagrams, comprising: The mode selection module is used to respond to a received multi-agent collaborative task and determine a target collaborative mode from a variety of preset collaborative modes based on the task characteristics of the collaborative task. The task characteristics include at least the number of agents, task dependencies, and consistency requirements. The state graph construction module is used to construct a declarative state graph corresponding to the target collaboration mode based on the target collaboration mode. The declarative state graph contains multiple nodes and edges connecting the nodes. The nodes include execution nodes for representing task decomposition or execution and sentinel nodes for representing the start and end of the execution process. The edges include unconditional edges representing unconditional transitions and conditional edges bound to state predicates. The state diagram execution module is used to take the current state as input, execute the node logic corresponding to the current node in sequence according to the topology of the declarative state diagram, and generate a new immutable state based on the execution result after each node execution. The new immutable state contains a message list and variable mapping. The state transition module is used to determine the next node based on the new immutable state and the outgoing edge type of the current node. If the current node has a conditional edge, the state predicate bound to the conditional edge is called to evaluate the new immutable state, and the target edge is determined from the conditional edges based on the evaluation result and the transition is made to the next node pointed to by the target edge. If the current node does not have a conditional edge or all conditional edges are not satisfied, the next node is determined based on the unconditional edge. The execution control module is used to control the state diagram execution module and the state transition module to repeatedly execute node logic and state transitions until the current node is a termination sentinel node or a preset termination condition is met, and then output the final execution result.
[0021] In some specific embodiments, a session management module is also included, which is used for: Upon receiving the collaborative task, a corresponding session is created for the collaborative task, and the session includes a session identifier, the current state, and the last active time; Each time the status is updated, the last active time of the session is updated to the current time; All sessions are scanned at preset time intervals, and sessions whose difference between the last active time and the current time exceeds a preset survival time threshold are identified as expired sessions and evicted. When the system shuts down, it stops accepting new tasks, waits for ongoing tasks to complete, and forcibly terminates unfinished tasks after a timeout.
[0022] In some specific embodiments, the multiple preset collaboration modes include sequential mode, parallel mode, hierarchical mode, consensus mode, and polling mode; The state diagram construction module constructs a corresponding declarative state diagram based on the target collaboration pattern, including: When the target collaboration mode is a sequential mode, a chain state diagram is constructed; When the target collaboration mode is parallel mode, a fan-out-convergence state diagram is constructed. When the target collaboration mode is a hierarchical mode, a tree-like state diagram is constructed. When the target collaboration mode is a consensus mode, an independent path-convergence state graph is constructed. When the target collaboration mode is polling mode, a circular state diagram is constructed.
[0023] The beneficial effects of the above technical solution are as follows: This invention provides a multi-agent collaborative orchestration method and system based on declarative state graphs, which has significant advantages over existing technologies in terms of scalability, concurrency safety, collaborative mode coverage, resource utilization, and execution controllability. Specifically, this invention defines the collaborative process as the configuration of nodes and edges rather than hard-coded logic through a declarative state graph orchestration engine. This eliminates the need to modify the core scheduling code when adding new collaborative modes, significantly improving development efficiency, effectively reducing maintenance costs, and fully verifying the effectiveness of the open / closed principle. Regarding concurrency safety, this invention adopts an immutable state management mechanism, with all state fields being final. Each node execution updates the state by creating a new state object rather than modifying the original object, completely eliminating race conditions and data inconsistency risks in parallel scenarios. This effectively solves the pain point of existing solutions where the work of one agent is silently overwritten by another agent. This invention supports five collaboration modes: sequential, parallel, hierarchical, consensus, and polling, covering the vast majority of typical multi-agent collaboration scenarios. This far exceeds the existing solutions that only support the two basic modes of sequential and parallel collaboration, greatly expanding the system's applicability. Regarding resource management, this invention effectively reduces memory consumption through session caching and TTL timeout eviction mechanisms, preventing memory leaks in long-running scenarios. In terms of execution controllability, the maximum step limit mechanism effectively prevents infinite loops, and the graceful shutdown mechanism ensures that tasks currently executing are completed without data loss when the system shuts down, effectively solving the common non-terminating dialogue failure modes in existing technologies. In summary, this invention provides a complete technical solution for multi-agent collaborative orchestration that is highly scalable, concurrency-safe, mode-rich, resource-controllable, and predictable in execution. Attached Figure Description
[0024] To more clearly illustrate the technical solutions in the embodiments of this application, the accompanying drawings used in the description of the embodiments will be briefly introduced below. Obviously, the accompanying drawings described below are only some embodiments of this application. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0025] Figure 1 A flowchart illustrating a multi-agent collaborative orchestration method based on a declarative state diagram, provided as an embodiment of the present invention; Figure 2 A schematic diagram of the structure of a multi-agent collaborative orchestration system based on a declarative state diagram is provided as an embodiment of the present invention; Figure 3 This is a three-layer architecture diagram of a multi-agent collaborative orchestration system based on a declarative state diagram multi-agent collaborative orchestration method, provided as an embodiment of the present invention. Detailed Implementation
[0026] The technical solutions of the present invention will be clearly and completely described below with reference to the accompanying drawings of the embodiments of the present invention. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments.
[0027] Examples of the embodiments are shown in the accompanying drawings, wherein the same or similar symbols denote the same or similar elements or elements having the same or similar functions throughout. The embodiments described below with reference to the accompanying drawings are exemplary and intended to explain the invention, and should not be construed as limiting the invention.
[0028] Example 1 One embodiment of the present invention provides a multi-agent cooperative orchestration method based on declarative state diagrams, referring to... Figure 1 , Figure 3 As shown, it includes: Step S1: In response to the received multi-agent cooperative task, determine the target cooperative mode from a variety of preset cooperative modes according to the task characteristics of the cooperative task. The task characteristics include at least the number of agents, task dependencies, and consistency requirements. In a specific embodiment of the present invention, in step S1, the multiple preset collaboration modes include sequential mode, parallel mode, hierarchical mode, consensus mode, and polling mode. Determining the target collaboration mode from the multiple preset collaboration modes based on the task characteristics of the collaborative task includes: When the number of agents is equal to 1, the target cooperation mode is determined to be a sequential mode; When the number of agents is greater than 1 and the task dependency is independent, the target cooperation mode is determined to be parallel mode. When the number of agents is greater than 1 and the task dependency is a complex dependency, the target collaboration mode is determined to be a hierarchical mode. When the consistency requirement is higher than a preset threshold, the target collaboration mode is determined to be a consensus mode. When the task type of the collaborative task is iterative optimization type, the target collaboration mode is determined to be polling mode.
[0029] Step S2: Based on the target collaboration mode, construct a declarative state diagram corresponding to the target collaboration mode. The declarative state diagram contains multiple nodes and edges connecting the nodes. The nodes include execution nodes for representing task decomposition or execution and sentinel nodes for representing the start and end of the execution process. The edges include unconditional edges representing unconditional transitions and conditional edges bound to state predicates. In a specific embodiment of the present invention, the step of constructing a declarative state diagram corresponding to the target collaboration mode according to the target collaboration mode includes: When the target collaboration mode is a sequential mode, a chain state diagram is constructed, and the execution nodes in the chain state diagram are connected sequentially according to the execution order. When the target collaboration mode is parallel mode, a fan-out-convergence state graph is constructed. The fan-out-convergence state graph includes multiple parallel edges fanning out from the starting sentinel node to multiple parallel execution nodes, and multiple convergence edges converging from the multiple parallel execution nodes to the merging node. The merging node is used to merge the output states of each parallel execution node after it is determined that all parallel execution nodes have been executed. When the target collaboration mode is a hierarchical mode, a tree-like state diagram is constructed, which includes a leader node and multiple worker nodes generated by the leader node after decomposing the task. When the target collaboration mode is a consensus mode, an independent path-convergence state graph is constructed. The independent path-convergence state graph includes multiple independent execution nodes and convergence edges connected to the consensus node. The consensus node is used to collect the opinions output by each independent execution node and make consensus decisions. When the target collaboration mode is polling mode, a circular state diagram is constructed. Multiple execution nodes in the circular state diagram are executed sequentially, and the next iteration is determined based on the quality assessment results.
[0030] Step S3: Using the current state as input, execute the node logic corresponding to the current node in sequence according to the topology of the declarative state diagram, and generate a new immutable state based on the execution result after each node execution. The new immutable state contains a message list and variable mapping. In a specific embodiment of the present invention, step S3 includes: Before executing the current node, obtain the input state of the current node, which includes the current message list, the current variable mapping, and the current execution step count; Execute the node logic corresponding to the current node, the node logic including calling the agent corresponding to the current node to process the input state and generate node output results, the node output results including appending messages and / or updating variables; A new immutable state is created based on the input state and the node output result. The message list in the new immutable state is a new list obtained by appending the appended message to the current message list. The variable mapping in the new immutable state is a new mapping obtained by applying the updated variable to the current variable mapping. The execution step number in the new immutable state is the current execution step number plus 1.
[0031] Step S4: Determine the next node based on the new immutable state and the outgoing edge type of the current node. If the current node has a conditional edge, the state predicate bound to the conditional edge is called to evaluate the new immutable state, and the target edge is determined from the conditional edges based on the evaluation result and the process moves to the next node pointed to by the target edge. If the current node does not have a conditional edge or all conditional edges are not satisfied, the next node is determined based on the unconditional edge. In a specific embodiment of the present invention, step S4 includes: Obtain all outgoing edges of the current node in the declarative state graph, including unconditional edges and conditional edges; The conditional edges in all the outgoing edges are evaluated in a preset order: the state predicate bound to the conditional edge is called to determine the new immutable state. If the state predicate returns a true value, the conditional edge is determined as the target edge, and the evaluation of subsequent conditional edges is stopped. If the target edge exists, then move to the next node pointed to by the target edge; If all conditional edges are not satisfied and there is an unconditional edge, then move to the next node pointed to by the unconditional edge; If there are no outgoing edges that meet the conditions, then proceed to the terminating sentinel node.
[0032] Step S5: Repeat the execution of node logic and state transitions until the current node becomes a termination sentinel node or the preset termination condition is met, and then output the final execution result.
[0033] In one specific embodiment of the present invention, step S5 includes: After each node completes its execution, it checks whether the current node is a termination sentinel node. If so, execution stops and the current state is output as the final execution result. After each node is executed, it is determined whether the current cumulative number of execution steps has reached the maximum number of steps threshold. If the maximum number of steps threshold is reached, execution is stopped, the current state is marked as an abnormal termination state, and the execution result containing the reason for the abnormal termination is output. During each node execution process, if an exception is detected in the current node execution, the exception is captured and the current state is marked as an error state. The process is then transferred to the predefined error handling node in the declarative state diagram or directly to the termination sentinel node.
[0034] In one specific embodiment of the present invention, session lifecycle management is also included: Upon receiving the collaborative task, a corresponding session is created for the collaborative task, and the session includes a session identifier, the current state, and the last active time; Each time the status is updated, the last active time of the session is updated to the current time; All sessions are scanned at preset time intervals, and sessions whose difference between the last active time and the current time exceeds a preset survival time threshold are identified as expired sessions and evicted. When the system shuts down, it stops accepting new tasks, waits for ongoing tasks to complete, and forcibly terminates unfinished tasks after a timeout.
[0035] The multi-agent collaborative orchestration system based on declarative state diagrams provided by this invention adopts a three-layer architecture design, namely, scheduling layer, execution layer and communication layer.
[0036] The scheduling layer is the core control layer of the system, which includes three core components: TeamOrchestrator, AgentStateGraph, and ModeSelector.
[0037] As the system's entry point, TeamOrchestrator is responsible for receiving task requests from external sources. Task requests are passed in as TaskRequest objects, which contain fields such as taskId (a unique identifier for the task), taskType (task type, such as CODE_REVIEW, DOC_GENERATION, DATA_ANALYSIS, etc.), agents (a list of participating agents), and mode (the specified collaboration mode; if not specified, the system automatically selects one). Upon receiving a task request, TeamOrchestrator first calls the selectMode method of ModeSelector to determine the collaboration mode, then creates a TeamSession object and executes the orchestration logic through AgentStateGraph. It's important to note that TeamOrchestrator does not directly call any agents; instead, it schedules indirectly through the state graph engine, achieving complete decoupling between the scheduling and execution logic.
[0038] AgentStateGraph is the core innovative component of this invention, providing three public APIs for constructing the state graph: addNode(nodeId, node), addEdge(fromId, toId), and addConditionalEdge(fromId, toId, predicate). Internally, it uses a Map.<String, Node> Store the definitions of all nodes using a Map. <String, List <edge>The adjacency list is stored to represent the transition relationships between nodes. During execution, AgentStateGraph uses a depth-first traversal strategy, starting from the START sentinel node and executing each node it visits in turn, and using predicate evaluation to determine the direction of the conditional branch.
[0039] ModeSelector is responsible for automatically selecting the optimal collaboration mode based on task characteristics. Its selectMode method receives a Task object as a parameter and performs a comprehensive evaluation from three dimensions: agentCount (number of agents), dependency (task dependencies), and consistency (consistency requirements). The specific selection logic will be described in detail in subsequent embodiments.
[0040] The execution layer consists of multiple ReAct agents. Each ReAct agent contains three core components: an LLM caller, a tool manager, and a memory manager.
[0041] The LLM caller encapsulates calls to large language models, supporting both synchronous (chat method) and streaming (streamChat method) modes. Through the LLMAdapter interface, the system can adapt to various large language model providers, including but not limited to Dify, RagFlow, and custom-implemented adapters. When an agent needs to call a large language model, the LLM caller assembles the current message list and system prompts into a request, sends it to the large language model server, and receives the returned response.
[0042] The tool manager manages the toolset available to the agent, supporting built-in tools such as file_read, file_write, and code_execute, as well as custom tools defined by developers. Tool call results are fed back to the large language model in the form of observations, serving as the basis for the next round of inference.
[0043] The memory manager maintains short-term memory and long-term memory. Short-term memory is the message list for the current session, using a sliding window mechanism to retain the most recent N messages (20 by default), automatically discarding any excess messages to prevent performance degradation of large language models due to excessively long contexts. Long-term memory is a cross-session knowledge base, supporting persistent storage and cross-session reuse of key information.
[0044] The communication layer provides three core components: EventBus, Mailbox, and SessionCache.
[0045] EventBus implements an event notification mechanism based on the observer pattern. Agents publish events using the `publish(event)` method, and subscribers register callback functions using the `subscribe(eventType, handler)` method. Supported event types include, but are not limited to: `TASK_STARTED` (task started), `TASK_COMPLETED` (task completed), `AGENT_ERROR` (agent error), and `STATE_CHANGED` (state change).
[0046] Mailbox provides each agent with an independent message mailbox for receiving messages from other agents. Mailbox is implemented using a BlockingQueue and supports both send(message) and receive(timeout) operations. The message format is {from, to, type, content, timestamp}, where from is the sender agent ID, to is the receiver agent ID, type is the message type, content is the message content, and timestamp is the sending timestamp.
[0047] SessionCache uses ConcurrentHashMap to store sessions. The default TTL (Time To Live) for a session is 30 minutes. Each time a session is active (such as sending a message or updating its status), the system automatically refreshes the session's lastActiveTime. ScheduledExecutor scans the session cache every 5 minutes and automatically evicts all expired sessions.
[0048] Taking document generation tasks as an example, this document describes in detail the complete process of AgentStateGraph executing the state graph.
[0049] Suppose the system receives a document generation task, which requires generating a technical solution document. ModeSelector selects HIERARCHICAL (hierarchical mode) based on the task characteristics and constructs a tree-like state diagram containing a Leader node and multiple Worker nodes. The state diagram structure is: START → Leader (decompose task) → Worker1 (write chapter 1), Worker2 (write chapter 2), Worker3 (write chapter 3) → Leader (review and integrate) → END.
[0050] AgentStateGraph begins execution from the START sentinel node. The START node is a special node that does not execute any business logic; its execute method directly returns the input state and is only used to mark the execution entry point.
[0051] Starting from the START node, there exists an unconditional edge pointing to the Leader node. The engine updates the currentNodeId to "leader" and then proceeds to the next iteration.
[0052] When the Leader node executes, the engine calls the `node.execute(state)` method. The LeaderAgent corresponding to the Leader node first calls the large language model to analyze the original task, decomposing the document generation task into three sub-tasks: Chapter 1 (Introduction and Background), Chapter 2 (Detailed Technical Solution), and Chapter 3 (Summary and Outlook). After decomposition, the Leader node updates the variables in TeamState, adding the list of sub-tasks and the mapping relationship between each sub-task and the Worker nodes. After execution, the engine creates a new immutable state `state` based on the current state and the output of the Leader node.
[0053] Next, the engine evaluates the outgoing edge conditions of the Leader node. The Leader node has three unconditional edges, pointing to Worker1, Worker2, and Worker3 respectively. The engine executes the three Worker nodes sequentially according to the edge definition order. It should be noted that the three Worker nodes are executed in parallel; the engine uses a thread pool to concurrently call the execute method of the three Worker nodes. Each Worker node retrieves the subtask description assigned to it from the TeamState variables, calls the corresponding agent to process it, generates the corresponding chapter content, and appends the chapter content to the TeamState messages list.
[0054] Once all Worker nodes have completed their execution, the engine returns execution control to the Leader node. The Leader node then executes again, invoking the large language model to review and integrate the chapter content generated by the three Worker nodes, eliminating duplicate content, adjusting the logical connections between chapters, and finally generating a complete document. After the Leader node completes its execution, the engine evaluates its outgoing edge conditions. If an unconditional edge points to the END sentinel node, the engine updates the currentNodeId to "END".
[0055] At the start of the next iteration, the engine detects that currentNodeId is "END", stops the iteration, and outputs the current state as the final execution result.
[0056] During the above execution process, the engine continuously maintains a step counter, incrementing the step count by 1 after each node execution. When the step count reaches maxSteps (default 100), the engine automatically terminates execution and marks the status as ERROR to prevent infinite loops.
[0057] Example 2 One embodiment of the present invention provides a multi-agent cooperative orchestration system based on declarative state diagrams, referring to... Figure 2 As shown, it includes: The mode selection module 10 is used to respond to a received multi-agent collaborative task and determine a target collaborative mode from a variety of preset collaborative modes according to the task characteristics of the collaborative task. The task characteristics include at least the number of agents, task dependencies, and consistency requirements. The state diagram construction module 20 is used to construct a declarative state diagram corresponding to the target collaboration mode according to the target collaboration mode. The declarative state diagram includes multiple nodes and edges connecting the nodes. The nodes include execution nodes for representing task decomposition or execution and sentinel nodes for representing the start and end of the execution process. The edges include unconditional edges representing unconditional transitions and conditional edges bound to state predicates. The state diagram execution module 30 is used to take the current state as input, execute the node logic corresponding to the current node in sequence according to the topology of the declarative state diagram, and generate a new immutable state based on the execution result after each node execution is completed. The new immutable state contains a message list and variable mapping. The state transition module 40 is used to determine the next node based on the new immutable state and the outgoing edge type of the current node. If the current node has a conditional edge, the state predicate bound to the conditional edge is called to evaluate the new immutable state, and the target edge is determined from the conditional edges based on the evaluation result and the transition is made to the next node pointed to by the target edge. If the current node does not have a conditional edge or all conditional edges are not satisfied, the next node is determined based on the unconditional edge. The execution control module 50 is used to control the state diagram execution module and the state transition module to repeatedly execute node logic and state transitions until the current node is a termination sentinel node or a preset termination condition is met, and then output the final execution result.
[0058] In one specific embodiment of the present invention, a session management module is further included, the session management module being used for: Upon receiving the collaborative task, a corresponding session is created for the collaborative task, and the session includes a session identifier, the current state, and the last active time; Each time the status is updated, the last active time of the session is updated to the current time; All sessions are scanned at preset time intervals, and sessions whose difference between the last active time and the current time exceeds a preset survival time threshold are identified as expired sessions and evicted. When the system shuts down, it stops accepting new tasks, waits for ongoing tasks to complete, and forcibly terminates unfinished tasks after a timeout.
[0059] In one specific embodiment of the present invention, the multiple preset collaboration modes include sequential mode, parallel mode, hierarchical mode, consensus mode, and polling mode; The state diagram construction module constructs a corresponding declarative state diagram based on the target collaboration pattern, including: When the target collaboration mode is a sequential mode, a chain state diagram is constructed; When the target collaboration mode is parallel mode, a fan-out-convergence state diagram is constructed. When the target collaboration mode is a hierarchical mode, a tree-like state diagram is constructed. When the target collaboration mode is a consensus mode, an independent path-convergence state graph is constructed. When the target collaboration mode is polling mode, a circular state diagram is constructed.
[0060] The above description is merely a specific embodiment of the present invention, but the scope of protection of the present invention is not limited thereto. Any variations or substitutions that can be easily conceived by those skilled in the art within the technical scope disclosed in the present invention should be included within the scope of protection of the present invention. Therefore, the scope of protection of the present invention should be determined by the scope of the claims.
[0061] The various embodiments in this specification are described in a progressive manner, with each embodiment focusing on its differences from other embodiments. Similar or identical parts between embodiments can be referred to interchangeably. The embodiments of the present invention are described with reference to flowchart illustrations and / or block diagrams of methods, terminal devices (systems), and computer program products according to embodiments of the present invention. It should be understood that each block of the flowchart illustrations and / or block diagrams, and combinations of blocks in the flowchart illustrations and / or block diagrams, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, special-purpose computer, embedded processor, or other programmable data processing terminal device to produce a machine, such that the instructions, which execute via the processor of the computer or other programmable data processing terminal device, generate instructions for implementing the flowchart illustrations and / or block diagrams. Figure 1 One or more processes and / or boxes Figure 1 The computer program instructions may also be stored in a computer-readable storage medium that can direct a computer or other programmable data processing terminal device to operate in a particular manner, such that the instructions stored in the computer-readable storage medium produce an article of manufacture including instruction means, which are implemented in a process Figure 1 One or more processes and / or boxes Figure 1 The functions specified in one or more boxes. These computer program instructions may also be loaded onto a computer or other programmable data processing terminal equipment to cause a series of operational steps to be performed on the computer or other programmable terminal equipment to produce a computer-implemented process, thereby providing instructions that execute on the computer or other programmable terminal equipment for implementing the process. Figure 1 One or more processes and / or boxes Figure 1 The steps of the functions specified in one or more boxes. Although preferred embodiments of the invention have been described, those skilled in the art, upon learning the basic inventive concept, can make other changes and modifications to these embodiments. Therefore, the appended claims are intended to be interpreted as including the preferred embodiments as well as all changes and modifications falling within the scope of the embodiments of the invention. Finally, it should be noted that in this document, relational terms such as "first" and "second" are used only to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or terminal device that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or terminal device. Without further limitations, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or terminal device that includes said element.
[0062] The methods and apparatus provided by the present invention have been described in detail above. Specific examples have been used to illustrate the principles and implementation methods of the present invention. The descriptions of the above embodiments are only for the purpose of helping to understand the methods and core ideas of the present invention. At the same time, for those skilled in the art, there will be changes in the specific implementation methods and application scope based on the ideas of the present invention. Therefore, the content of this specification should not be construed as a limitation of the present invention.
[0063] In the description of this specification, references to terms such as "one embodiment," "some embodiments," "example," "specific example," "a specific embodiment," or "some examples," etc., indicate that a specific feature, structure, material, or characteristic described in connection with that embodiment or example is included in at least one embodiment or example of the invention. In this specification, illustrative expressions of terms do not necessarily refer to the same embodiment or example. Furthermore, the specific features, structures, materials, or characteristics described may be combined in any suitable manner in one or more embodiments or examples.
[0064] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of this application, and are not intended to limit them. Although this application has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some of the technical features. Such modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of this application.< / edge>
Claims
1. A multi-agent collaborative orchestration method based on declarative state diagrams, characterized in that, include: Step S1: In response to the received multi-agent cooperative task, determine the target cooperative mode from a variety of preset cooperative modes according to the task characteristics of the cooperative task. The task characteristics include at least the number of agents, task dependencies, and consistency requirements. Step S2: Based on the target collaboration mode, construct a declarative state diagram corresponding to the target collaboration mode. The declarative state diagram contains multiple nodes and edges connecting the nodes. The nodes include execution nodes for representing task decomposition or execution and sentinel nodes for representing the start and end of the execution process. The edges include unconditional edges representing unconditional transitions and conditional edges bound to state predicates. Step S3: Using the current state as input, execute the node logic corresponding to the current node in sequence according to the topology of the declarative state diagram, and generate a new immutable state based on the execution result after each node execution. The new immutable state contains a message list and variable mapping. Step S4: Determine the next node based on the new immutable state and the outgoing edge type of the current node. If the current node has a conditional edge, the state predicate bound to the conditional edge is called to evaluate the new immutable state, and the target edge is determined from the conditional edges based on the evaluation result and the process moves to the next node pointed to by the target edge. If the current node does not have a conditional edge or all conditional edges are not satisfied, the next node is determined based on the unconditional edge. Step S5: Repeat the execution of node logic and state transitions until the current node becomes a termination sentinel node or the preset termination condition is met, and then output the final execution result.
2. The multi-agent cooperative orchestration method based on declarative state diagrams according to claim 1, characterized in that, In step S1, the multiple preset collaboration modes include sequential mode, parallel mode, hierarchical mode, consensus mode, and polling mode. Determining the target collaboration mode from the multiple preset collaboration modes based on the task characteristics of the collaborative task includes: When the number of agents is equal to 1, the target cooperation mode is determined to be a sequential mode; When the number of agents is greater than 1 and the task dependency is independent, the target cooperation mode is determined to be parallel mode. When the number of agents is greater than 1 and the task dependency is a complex dependency, the target collaboration mode is determined to be a hierarchical mode. When the consistency requirement is higher than a preset threshold, the target collaboration mode is determined to be a consensus mode. When the task type of the collaborative task is iterative optimization type, the target collaboration mode is determined to be polling mode.
3. The multi-agent cooperative orchestration method based on declarative state diagrams according to claim 1 or 2, characterized in that, The step of constructing a declarative state diagram corresponding to the target collaboration pattern includes: When the target collaboration mode is a sequential mode, a chain state diagram is constructed, and the execution nodes in the chain state diagram are connected sequentially according to the execution order. When the target collaboration mode is parallel mode, a fan-out-convergence state graph is constructed. The fan-out-convergence state graph includes multiple parallel edges fanning out from the starting sentinel node to multiple parallel execution nodes, and multiple convergence edges converging from the multiple parallel execution nodes to the merging node. The merging node is used to merge the output states of each parallel execution node after it is determined that all parallel execution nodes have been executed. When the target collaboration mode is a hierarchical mode, a tree-like state diagram is constructed, which includes a leader node and multiple worker nodes generated by the leader node after decomposing the task. When the target collaboration mode is a consensus mode, an independent path-convergence state graph is constructed. The independent path-convergence state graph includes multiple independent execution nodes and convergence edges connected to the consensus node. The consensus node is used to collect the opinions output by each independent execution node and make consensus decisions. When the target collaboration mode is polling mode, a circular state diagram is constructed. Multiple execution nodes in the circular state diagram are executed sequentially, and the next iteration is determined based on the quality assessment results.
4. The multi-agent collaborative orchestration method based on declarative state diagrams according to claim 1, characterized in that, Step S3 includes: Before executing the current node, obtain the input state of the current node, which includes the current message list, the current variable mapping, and the current execution step count; Execute the node logic corresponding to the current node, the node logic including calling the agent corresponding to the current node to process the input state and generate node output results, the node output results including appending messages and / or updating variables; A new immutable state is created based on the input state and the node output result. The message list in the new immutable state is a new list obtained by appending the appended message to the current message list. The variable mapping in the new immutable state is a new mapping obtained by applying the updated variable to the current variable mapping. The execution step number in the new immutable state is the current execution step number plus 1.
5. The multi-agent cooperative orchestration method based on declarative state diagrams according to claim 1, characterized in that, Step S4 includes: Obtain all outgoing edges of the current node in the declarative state graph, including unconditional edges and conditional edges; The conditional edges in all the outgoing edges are evaluated in a preset order: the state predicate bound to the conditional edge is called to determine the new immutable state. If the state predicate returns a true value, the conditional edge is determined as the target edge, and the evaluation of subsequent conditional edges is stopped. If the target edge exists, then move to the next node pointed to by the target edge; If all conditional edges are not satisfied and there is an unconditional edge, then move to the next node pointed to by the unconditional edge; If there are no outgoing edges that meet the conditions, then proceed to the terminating sentinel node.
6. The multi-agent cooperative orchestration method based on declarative state diagrams according to claim 1, characterized in that, Step S5 includes: After each node completes its execution, it checks whether the current node is a termination sentinel node. If so, execution stops and the current state is output as the final execution result. After each node is executed, it is determined whether the current cumulative number of execution steps has reached the maximum number of steps threshold. If the maximum number of steps threshold is reached, execution is stopped, the current state is marked as an abnormal termination state, and the execution result containing the reason for the abnormal termination is output. During each node execution process, if an exception is detected in the current node execution, the exception is captured and the current state is marked as an error state. The process is then transferred to the predefined error handling node in the declarative state diagram or directly to the termination sentinel node.
7. The multi-agent cooperative orchestration method based on declarative state diagrams according to claim 1, characterized in that, It also includes session lifecycle management: Upon receiving the collaborative task, a corresponding session is created for the collaborative task, and the session includes a session identifier, the current state, and the last active time; Each time the status is updated, the last active time of the session is updated to the current time; All sessions are scanned at preset time intervals, and sessions whose difference between the last active time and the current time exceeds a preset survival time threshold are identified as expired sessions and evicted. When the system shuts down, it stops accepting new tasks, waits for ongoing tasks to complete, and forcibly terminates unfinished tasks after a timeout.
8. A multi-agent cooperative orchestration system based on declarative state diagrams, characterized in that, include: The mode selection module is used to respond to a received multi-agent collaborative task and determine a target collaborative mode from a variety of preset collaborative modes based on the task characteristics of the collaborative task. The task characteristics include at least the number of agents, task dependencies, and consistency requirements. The state graph construction module is used to construct a declarative state graph corresponding to the target collaboration mode based on the target collaboration mode. The declarative state graph contains multiple nodes and edges connecting the nodes. The nodes include execution nodes for representing task decomposition or execution and sentinel nodes for representing the start and end of the execution process. The edges include unconditional edges representing unconditional transitions and conditional edges bound to state predicates. The state diagram execution module is used to take the current state as input, execute the node logic corresponding to the current node in sequence according to the topology of the declarative state diagram, and generate a new immutable state based on the execution result after each node execution. The new immutable state contains a message list and variable mapping. The state transition module is used to determine the next node based on the new immutable state and the outgoing edge type of the current node. If the current node has a conditional edge, the state predicate bound to the conditional edge is called to evaluate the new immutable state, and the target edge is determined from the conditional edges based on the evaluation result and the transition is made to the next node pointed to by the target edge. If the current node does not have a conditional edge or all conditional edges are not satisfied, the next node is determined based on the unconditional edge. The execution control module is used to control the state diagram execution module and the state transition module to repeatedly execute node logic and state transitions until the current node is a termination sentinel node or a preset termination condition is met, and then output the final execution result.
9. The multi-agent cooperative orchestration method based on declarative state diagrams according to claim 1, characterized in that, It also includes a session management module, which is used for: Upon receiving the collaborative task, a corresponding session is created for the collaborative task, and the session includes a session identifier, the current state, and the last active time; Each time the status is updated, the last active time of the session is updated to the current time; All sessions are scanned at preset time intervals, and sessions whose difference between the last active time and the current time exceeds a preset survival time threshold are identified as expired sessions and evicted. When the system shuts down, it stops accepting new tasks, waits for ongoing tasks to complete, and forcibly terminates unfinished tasks after a timeout.
10. The multi-agent collaborative orchestration method based on declarative state diagrams according to claim 1, characterized in that, The various preset collaboration modes include sequential mode, parallel mode, hierarchical mode, consensus mode, and polling mode; The state diagram construction module constructs a corresponding declarative state diagram based on the target collaboration pattern, including: When the target collaboration mode is a sequential mode, a chain state diagram is constructed; When the target collaboration mode is parallel mode, a fan-out-convergence state diagram is constructed. When the target collaboration mode is a hierarchical mode, a tree-like state diagram is constructed. When the target collaboration mode is a consensus mode, an independent path-convergence state graph is constructed. When the target collaboration mode is polling mode, a circular state diagram is constructed.
