Skip to content

Commit 9866be7

Browse files
Merge remote-tracking branch 'upstream/main'
2 parents 2b04449 + 1815d60 commit 9866be7

File tree

23 files changed

+3715
-31
lines changed

23 files changed

+3715
-31
lines changed

spring-ai-alibaba-graph-core/src/main/java/com/alibaba/cloud/ai/graph/CompiledGraph.java

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,26 @@
1515
*/
1616
package com.alibaba.cloud.ai.graph;
1717

18+
import java.io.IOException;
19+
import java.util.ArrayList;
20+
import java.util.Arrays;
21+
import java.util.Collection;
22+
import java.util.Deque;
23+
import java.util.HashMap;
24+
import java.util.LinkedHashMap;
25+
import java.util.List;
26+
import java.util.Map;
27+
import java.util.Objects;
28+
import java.util.Optional;
29+
import java.util.Set;
30+
import java.util.concurrent.CompletableFuture;
31+
import java.util.concurrent.CompletionException;
32+
import java.util.concurrent.LinkedBlockingDeque;
33+
import java.util.function.Function;
34+
import java.util.function.Supplier;
35+
import java.util.stream.Collectors;
36+
import java.util.stream.Stream;
37+
1838
import com.alibaba.cloud.ai.graph.action.AsyncCommandAction;
1939
import com.alibaba.cloud.ai.graph.action.AsyncNodeActionWithConfig;
2040
import com.alibaba.cloud.ai.graph.action.Command;
@@ -29,24 +49,23 @@
2949
import com.alibaba.cloud.ai.graph.internal.edge.EdgeValue;
3050
import com.alibaba.cloud.ai.graph.internal.node.CommandNode;
3151
import com.alibaba.cloud.ai.graph.internal.node.ParallelNode;
52+
import com.alibaba.cloud.ai.graph.scheduling.ScheduleConfig;
53+
import com.alibaba.cloud.ai.graph.scheduling.ScheduledAgentTask;
3254
import com.alibaba.cloud.ai.graph.state.StateSnapshot;
3355
import com.alibaba.cloud.ai.graph.streaming.AsyncGeneratorUtils;
3456
import com.alibaba.cloud.ai.graph.utils.LifeListenerUtil;
3557
import org.slf4j.Logger;
3658
import org.slf4j.LoggerFactory;
37-
import org.springframework.util.CollectionUtils;
3859

39-
import java.io.IOException;
40-
import java.util.*;
41-
import java.util.concurrent.CompletableFuture;
42-
import java.util.concurrent.CompletionException;
43-
import java.util.concurrent.LinkedBlockingDeque;
44-
import java.util.function.Function;
45-
import java.util.function.Supplier;
46-
import java.util.stream.Collectors;
47-
import java.util.stream.Stream;
60+
import org.springframework.util.CollectionUtils;
4861

49-
import static com.alibaba.cloud.ai.graph.StateGraph.*;
62+
import static com.alibaba.cloud.ai.graph.StateGraph.END;
63+
import static com.alibaba.cloud.ai.graph.StateGraph.ERROR;
64+
import static com.alibaba.cloud.ai.graph.StateGraph.Edges;
65+
import static com.alibaba.cloud.ai.graph.StateGraph.NODE_AFTER;
66+
import static com.alibaba.cloud.ai.graph.StateGraph.NODE_BEFORE;
67+
import static com.alibaba.cloud.ai.graph.StateGraph.Nodes;
68+
import static com.alibaba.cloud.ai.graph.StateGraph.START;
5069
import static java.lang.String.format;
5170
import static java.util.concurrent.CompletableFuture.completedFuture;
5271
import static java.util.concurrent.CompletableFuture.failedFuture;
@@ -463,6 +482,15 @@ public Optional<OverAllState> invoke(Map<String, Object> inputs) throws GraphRun
463482
return this.invoke(stateCreate(inputs), RunnableConfig.builder().build());
464483
}
465484

485+
/**
486+
* Schedule the graph execution with enhanced configuration options.
487+
* @param scheduleConfig the schedule configuration
488+
* @return a ScheduledGraphExecution instance for managing the scheduled task
489+
*/
490+
public ScheduledAgentTask schedule(ScheduleConfig scheduleConfig) {
491+
return new ScheduledAgentTask(this, scheduleConfig).start();
492+
}
493+
466494
private OverAllState stateCreate(Map<String, Object> inputs) {
467495
// Creates a new OverAllState instance using key strategies from the graph
468496
// and provided input data.

spring-ai-alibaba-graph-core/src/main/java/com/alibaba/cloud/ai/graph/agent/BaseAgent.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
import com.alibaba.cloud.ai.graph.action.AsyncNodeAction;
2323
import com.alibaba.cloud.ai.graph.exception.GraphRunnerException;
2424
import com.alibaba.cloud.ai.graph.exception.GraphStateException;
25+
import com.alibaba.cloud.ai.graph.scheduling.ScheduleConfig;
26+
import com.alibaba.cloud.ai.graph.scheduling.ScheduledAgentTask;
27+
28+
import org.springframework.scheduling.Trigger;
2529

2630
/**
2731
* Abstract base class for all agents in the graph system. Contains common properties and
@@ -95,4 +99,24 @@ public abstract AsyncNodeAction asAsyncNodeAction(String inputKeyFromParent, Str
9599
public abstract Optional<OverAllState> invoke(Map<String, Object> input)
96100
throws GraphStateException, GraphRunnerException;
97101

102+
/**
103+
* Schedule the agent task with trigger.
104+
* @param trigger the schedule configuration
105+
* @param input the agent input
106+
* @return a ScheduledAgentTask instance for managing the scheduled task
107+
*/
108+
public ScheduledAgentTask schedule(Trigger trigger, Map<String, Object> input)
109+
throws GraphStateException, GraphRunnerException {
110+
ScheduleConfig scheduleConfig = ScheduleConfig.builder().trigger(trigger).inputs(input).build();
111+
return schedule(scheduleConfig);
112+
}
113+
114+
/**
115+
* Schedule the agent task with trigger.
116+
* @param scheduleConfig the schedule configuration
117+
* @return a ScheduledAgentTask instance for managing the scheduled task
118+
*/
119+
public abstract ScheduledAgentTask schedule(ScheduleConfig scheduleConfig)
120+
throws GraphStateException, GraphRunnerException;
121+
98122
}

spring-ai-alibaba-graph-core/src/main/java/com/alibaba/cloud/ai/graph/agent/ReactAgent.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,13 @@
3333
import com.alibaba.cloud.ai.graph.action.NodeAction;
3434
import com.alibaba.cloud.ai.graph.node.LlmNode;
3535
import com.alibaba.cloud.ai.graph.node.ToolNode;
36+
import com.alibaba.cloud.ai.graph.scheduling.ScheduleConfig;
37+
import com.alibaba.cloud.ai.graph.scheduling.ScheduledAgentTask;
3638
import com.alibaba.cloud.ai.graph.state.strategy.AppendStrategy;
3739
import org.apache.commons.collections4.CollectionUtils;
3840

3941
import com.alibaba.cloud.ai.graph.state.strategy.ReplaceStrategy;
42+
4043
import org.springframework.ai.chat.client.ChatClient;
4144
import org.springframework.ai.chat.messages.AssistantMessage;
4245
import org.springframework.ai.chat.messages.Message;
@@ -112,6 +115,12 @@ public Optional<OverAllState> invoke(Map<String, Object> input) throws GraphStat
112115
return this.compiledGraph.invoke(input);
113116
}
114117

118+
@Override
119+
public ScheduledAgentTask schedule(ScheduleConfig scheduleConfig) throws GraphStateException {
120+
CompiledGraph compiledGraph = getAndCompileGraph();
121+
return compiledGraph.schedule(scheduleConfig);
122+
}
123+
115124
public StateGraph getStateGraph() {
116125
return graph;
117126
}
@@ -465,6 +474,9 @@ public ReactAgent build() throws GraphStateException {
465474
}
466475

467476
LlmNode.Builder llmNodeBuilder = LlmNode.builder().chatClient(chatClient).messagesKey(this.inputKey);
477+
if (outputKey != null && !outputKey.isEmpty()) {
478+
llmNodeBuilder.outputKey(outputKey);
479+
}
468480
if (CollectionUtils.isNotEmpty(tools)) {
469481
llmNodeBuilder.toolCallbacks(tools);
470482
}

spring-ai-alibaba-graph-core/src/main/java/com/alibaba/cloud/ai/graph/agent/a2a/A2aRemoteAgent.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,26 @@
1616

1717
package com.alibaba.cloud.ai.graph.agent.a2a;
1818

19+
import java.util.HashMap;
20+
import java.util.Map;
21+
import java.util.Optional;
22+
1923
import com.alibaba.cloud.ai.graph.CompileConfig;
20-
import com.alibaba.cloud.ai.graph.OverAllState;
21-
import com.alibaba.cloud.ai.graph.KeyStrategy;
2224
import com.alibaba.cloud.ai.graph.CompiledGraph;
25+
import com.alibaba.cloud.ai.graph.KeyStrategy;
2326
import com.alibaba.cloud.ai.graph.KeyStrategyFactory;
27+
import com.alibaba.cloud.ai.graph.OverAllState;
2428
import com.alibaba.cloud.ai.graph.StateGraph;
2529
import com.alibaba.cloud.ai.graph.action.AsyncNodeAction;
2630
import com.alibaba.cloud.ai.graph.action.NodeAction;
2731
import com.alibaba.cloud.ai.graph.agent.BaseAgent;
2832
import com.alibaba.cloud.ai.graph.exception.GraphRunnerException;
2933
import com.alibaba.cloud.ai.graph.exception.GraphStateException;
34+
import com.alibaba.cloud.ai.graph.scheduling.ScheduleConfig;
35+
import com.alibaba.cloud.ai.graph.scheduling.ScheduledAgentTask;
3036
import com.alibaba.cloud.ai.graph.state.strategy.AppendStrategy;
3137
import io.a2a.spec.AgentCard;
3238

33-
import java.util.HashMap;
34-
import java.util.Map;
35-
import java.util.Optional;
36-
3739
import static com.alibaba.cloud.ai.graph.action.AsyncNodeAction.node_async;
3840

3941
public class A2aRemoteAgent extends BaseAgent {
@@ -87,6 +89,11 @@ public AsyncNodeAction asAsyncNodeAction(String inputKeyFromParent, String outpu
8789
return node_async(new A2aNode(agentCard, inputKeyFromParent, outputKeyToParent, streaming));
8890
}
8991

92+
@Override
93+
public ScheduledAgentTask schedule(ScheduleConfig scheduleConfig) throws GraphStateException, GraphRunnerException {
94+
throw new UnsupportedOperationException("A2aRemoteAgent has not support schedule.");
95+
}
96+
9097
@Override
9198
public Optional<OverAllState> invoke(Map<String, Object> input) throws GraphStateException, GraphRunnerException {
9299
if (this.compiledGraph == null) {

spring-ai-alibaba-graph-core/src/main/java/com/alibaba/cloud/ai/graph/agent/flow/agent/FlowAgent.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import com.alibaba.cloud.ai.graph.agent.ReactAgent;
2727
import com.alibaba.cloud.ai.graph.agent.flow.builder.FlowGraphBuilder;
2828
import com.alibaba.cloud.ai.graph.exception.GraphStateException;
29+
import com.alibaba.cloud.ai.graph.scheduling.ScheduleConfig;
30+
import com.alibaba.cloud.ai.graph.scheduling.ScheduledAgentTask;
2931

3032
import static com.alibaba.cloud.ai.graph.action.AsyncNodeAction.node_async;
3133

@@ -86,6 +88,12 @@ public AsyncNodeAction asAsyncNodeAction(String inputKeyFromParent, String outpu
8688
new ReactAgent.SubGraphNodeAdapter(inputKeyFromParent, outputKeyToParent, this.compiledGraph));
8789
}
8890

91+
@Override
92+
public ScheduledAgentTask schedule(ScheduleConfig scheduleConfig) throws GraphStateException {
93+
CompiledGraph compiledGraph = getAndCompileGraph();
94+
return compiledGraph.schedule(scheduleConfig);
95+
}
96+
8997
public CompiledGraph getAndCompileGraph() throws GraphStateException {
9098
if (this.compileConfig == null) {
9199
this.compiledGraph = graph.compile();

spring-ai-alibaba-graph-core/src/main/java/com/alibaba/cloud/ai/graph/agent/flow/node/RoutingEdgeAction.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ public RoutingEdgeAction(ChatModel chatModel, BaseAgent current, List<BaseAgent>
5656
}
5757
sb.append("\n\n");
5858
sb.append("Return the agent name to delegate the task to.");
59+
sb.append("\n\n");
60+
sb.append(
61+
"It should be emphasized that the returned result only requires the agent name and no other content.");
62+
sb.append("\n\n");
63+
sb.append(
64+
"For example, if you want to delegate the task to the agent named 'agent1', you should return 'agent1'.");
5965

6066
this.chatClient = ChatClient.builder(chatModel).defaultSystem(sb.toString()).build();
6167
this.taskKey = current.outputKey();

0 commit comments

Comments
 (0)