Skip to content

Commit b790832

Browse files
AlbumenJzhouyou9505
authored andcommitted
feat(graph): Support Agent Scope API (alibaba#2385)
1 parent 9effd90 commit b790832

File tree

14 files changed

+624
-0
lines changed

14 files changed

+624
-0
lines changed

spring-ai-alibaba-graph-core/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,12 @@
258258
<scope>test</scope>
259259
</dependency>
260260

261+
<dependency>
262+
<groupId>org.springframework.ai</groupId>
263+
<artifactId>spring-ai-openai</artifactId>
264+
<scope>provided</scope>
265+
</dependency>
266+
261267
</dependencies>
262268

263269
<reporting>
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/*
2+
* Copyright 2024-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.agentscope.core;
17+
18+
import io.agentscope.core.memory.Memory;
19+
import io.agentscope.core.message.Msg;
20+
import io.agentscope.core.model.Model;
21+
import io.agentscope.core.tool.Toolkit;
22+
import reactor.core.publisher.Flux;
23+
24+
import java.util.List;
25+
26+
public class ReActAgent {
27+
28+
private final String name;
29+
30+
private final String sysPrompt;
31+
32+
private final Model model;
33+
34+
private final Toolkit toolkit;
35+
36+
private final Memory memory;
37+
38+
private final boolean parallelToolCalls;
39+
40+
private final int maxIters;
41+
42+
public ReActAgent(String name, String sysPrompt, Model model, Toolkit toolkit, Memory memory,
43+
boolean parallelToolCalls, int maxIters) {
44+
this.name = name;
45+
this.sysPrompt = sysPrompt;
46+
this.model = model;
47+
this.toolkit = toolkit;
48+
this.memory = memory;
49+
this.parallelToolCalls = parallelToolCalls;
50+
this.maxIters = maxIters;
51+
}
52+
53+
public Flux<Msg> stream(Msg msg) {
54+
return Flux.empty();
55+
}
56+
57+
public Msg call(Msg msg) {
58+
return null;
59+
}
60+
61+
public Flux<Msg> stream(List<Msg> msg) {
62+
return Flux.empty();
63+
}
64+
65+
public Msg call(List<Msg> msg) {
66+
return null;
67+
}
68+
69+
public static Builder builder() {
70+
return new Builder();
71+
}
72+
73+
public static class Builder {
74+
75+
private String name;
76+
77+
private String sysPrompt;
78+
79+
private Model model;
80+
81+
private Toolkit toolkit;
82+
83+
private Memory memory;
84+
85+
private boolean parallelToolCalls;
86+
87+
private int maxIters;
88+
89+
private Builder() {
90+
}
91+
92+
public Builder name(String name) {
93+
this.name = name;
94+
return this;
95+
}
96+
97+
public Builder sysPrompt(String sysPrompt) {
98+
this.sysPrompt = sysPrompt;
99+
return this;
100+
}
101+
102+
public Builder model(Model model) {
103+
this.model = model;
104+
return this;
105+
}
106+
107+
public Builder toolkit(Toolkit toolkit) {
108+
this.toolkit = toolkit;
109+
return this;
110+
}
111+
112+
public Builder memory(Memory memory) {
113+
this.memory = memory;
114+
return this;
115+
}
116+
117+
public Builder parallelToolCalls(boolean parallelToolCalls) {
118+
this.parallelToolCalls = parallelToolCalls;
119+
return this;
120+
}
121+
122+
public Builder maxIters(int maxIters) {
123+
this.maxIters = maxIters;
124+
return this;
125+
}
126+
127+
public ReActAgent build() {
128+
return new ReActAgent(name, sysPrompt, model, toolkit, memory, parallelToolCalls, maxIters);
129+
}
130+
131+
}
132+
133+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2024-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.agentscope.core.memory;
17+
18+
import org.springframework.ai.chat.messages.Message;
19+
20+
import java.util.List;
21+
import java.util.concurrent.CopyOnWriteArrayList;
22+
23+
public class InMemoryMemory implements Memory {
24+
25+
private final List<Message> messages = new CopyOnWriteArrayList<>();
26+
27+
@Override
28+
public void addMessage(Message message) {
29+
messages.add(message);
30+
}
31+
32+
@Override
33+
public List<Message> getMessages() {
34+
return List.copyOf(messages);
35+
}
36+
37+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright 2024-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.agentscope.core.memory;
17+
18+
import org.springframework.ai.chat.messages.Message;
19+
20+
import java.util.List;
21+
22+
public interface Memory {
23+
24+
void addMessage(Message message);
25+
26+
List<Message> getMessages();
27+
28+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright 2024-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.agentscope.core.message;
17+
18+
public class ContentBlock {
19+
20+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2024-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.agentscope.core.message;
17+
18+
public class Msg {
19+
20+
private final String name;
21+
22+
private final MsgRole role;
23+
24+
private final ContentBlock content;
25+
26+
private Msg(String name, MsgRole role, ContentBlock content) {
27+
this.name = name;
28+
this.role = role;
29+
this.content = content;
30+
}
31+
32+
public static Builder builder() {
33+
return new Builder();
34+
}
35+
36+
public static class Builder {
37+
38+
private String name;
39+
40+
private MsgRole role;
41+
42+
private ContentBlock content;
43+
44+
public Builder name(String name) {
45+
this.name = name;
46+
return this;
47+
}
48+
49+
public Builder role(MsgRole role) {
50+
this.role = role;
51+
return this;
52+
}
53+
54+
public Builder content(ContentBlock content) {
55+
this.content = content;
56+
return this;
57+
}
58+
59+
public Msg build() {
60+
return new Msg(name, role, content);
61+
}
62+
63+
}
64+
65+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright 2024-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.agentscope.core.message;
17+
18+
public enum MsgRole {
19+
20+
USER, ASSISTANT, SYSTEM
21+
22+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2024-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.agentscope.core.message;
17+
18+
public class TextBlock extends ContentBlock {
19+
20+
private final String text;
21+
22+
private TextBlock(String text) {
23+
this.text = text;
24+
}
25+
26+
public static Builder builder() {
27+
return new Builder();
28+
}
29+
30+
public static class Builder {
31+
32+
private String text;
33+
34+
public Builder text(String text) {
35+
this.text = text;
36+
return this;
37+
}
38+
39+
public TextBlock build() {
40+
return new TextBlock(text);
41+
}
42+
43+
}
44+
45+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright 2024-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.agentscope.core.model;
17+
18+
import org.springframework.ai.chat.model.ChatModel;
19+
20+
public interface Model {
21+
22+
ChatModel chatModel();
23+
24+
}

0 commit comments

Comments
 (0)