Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5effa97

Browse files
AlbumenJCopilot
andauthored
feat: support ag-ui (agentscope-ai#150)
## AgentScope-Java Version 1.0.1 ## Description Fix agentscope-ai#127 ## Checklist Please check the following items before code is ready to be reviewed. - [ ] Code has been formatted with `mvn spotless:apply` - [ ] All tests are passing (`mvn test`) - [ ] Javadoc comments are complete and follow project conventions - [ ] Related documentation has been updated (e.g. links, examples, etc.) - [ ] Code is ready for review --------- Co-authored-by: Copilot <[email protected]>
1 parent 4154ef6 commit 5effa97

62 files changed

Lines changed: 10470 additions & 26 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/maven-ci.yml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,23 @@ jobs:
3131

3232
build:
3333
runs-on: ubuntu-latest
34+
env:
35+
MVND_VERSION: '1.0.3'
3436
steps:
3537
- uses: actions/checkout@v4
3638
- name: Set up JDK 17
3739
uses: actions/setup-java@v4
3840
with:
3941
java-version: '17'
4042
distribution: 'temurin'
41-
cache: maven
43+
44+
- name: Cache Maven repository
45+
uses: actions/cache@v4
46+
with:
47+
path: ~/.m2/repository
48+
key: maven-${{ runner.os }}-${{ hashFiles('**/pom.xml') }}
49+
restore-keys: |
50+
maven-${{ runner.os }}-
4251
4352
- name: Build and Test with Coverage
4453
run: mvn -B clean verify

agentscope-dependencies-bom/pom.xml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,20 @@
206206
<version>2.1.0</version>
207207
</dependency>
208208

209+
<!-- Spring WebFlux for AG-UI support -->
210+
<dependency>
211+
<groupId>org.springframework</groupId>
212+
<artifactId>spring-webflux</artifactId>
213+
<version>6.1.8</version>
214+
</dependency>
215+
216+
<!-- Spring Boot AutoConfiguration -->
217+
<dependency>
218+
<groupId>org.springframework.boot</groupId>
219+
<artifactId>spring-boot-autoconfigure</artifactId>
220+
<version>3.5.0</version>
221+
</dependency>
222+
209223
<!-- MySQL JDBC Driver -->
210224
<dependency>
211225
<groupId>com.mysql</groupId>
@@ -232,7 +246,7 @@
232246
<artifactId>redisson</artifactId>
233247
<version>3.37.0</version>
234248
</dependency>
235-
249+
236250
<!-- A2A dependencies -->
237251
<dependency>
238252
<groupId>io.github.a2asdk</groupId>

agentscope-examples/advanced/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
<dependency>
7070
<groupId>io.agentscope</groupId>
7171
<artifactId>agentscope-extensions-autocontext-memory</artifactId>
72+
<version>${revision}</version>
7273
</dependency>
7374

7475
<!-- Spring Boot for web examples -->

agentscope-examples/agui/pom.xml

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Copyright 2024-2025 the original author or authors.
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ You may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ http://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
-->
17+
18+
<project xmlns="http://maven.apache.org/POM/4.0.0"
19+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
20+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
21+
<modelVersion>4.0.0</modelVersion>
22+
23+
<parent>
24+
<groupId>io.agentscope</groupId>
25+
<artifactId>agentscope-examples</artifactId>
26+
<version>${revision}</version>
27+
</parent>
28+
29+
<artifactId>agentscope-examples-agui</artifactId>
30+
<packaging>jar</packaging>
31+
32+
<name>AgentScope Examples - AG-UI</name>
33+
<description>AG-UI Protocol example with frontend integration</description>
34+
35+
<dependencies>
36+
<!-- AgentScope Core -->
37+
<dependency>
38+
<groupId>io.agentscope</groupId>
39+
<artifactId>agentscope-core</artifactId>
40+
</dependency>
41+
42+
<!-- AG-UI WebFlux Spring Boot Starter -->
43+
<dependency>
44+
<groupId>io.agentscope</groupId>
45+
<artifactId>agentscope-agui-spring-boot-starter</artifactId>
46+
</dependency>
47+
48+
<!-- Spring Boot WebFlux -->
49+
<dependency>
50+
<groupId>org.springframework.boot</groupId>
51+
<artifactId>spring-boot-starter-webflux</artifactId>
52+
</dependency>
53+
</dependencies>
54+
55+
</project>
56+
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
* http://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.examples.agui;
17+
18+
import org.springframework.boot.SpringApplication;
19+
import org.springframework.boot.autoconfigure.SpringBootApplication;
20+
21+
/**
22+
* AG-UI Example Application.
23+
*
24+
* <p>This application demonstrates how to expose AgentScope agents via the AG-UI protocol
25+
* using Spring WebFlux.
26+
*
27+
* <p><b>Usage:</b>
28+
* <ol>
29+
* <li>Set the DASHSCOPE_API_KEY environment variable</li>
30+
* <li>Run this application</li>
31+
* <li>Open http://localhost:8080 in a browser</li>
32+
* <li>Or use curl: curl -X POST http://localhost:8080/agui/run -H "Content-Type: application/json" -d '{"threadId":"test","runId":"1","messages":[{"id":"m1","role":"user","content":"Hello!"}]}'</li>
33+
* </ol>
34+
*/
35+
@SpringBootApplication
36+
public class AguiExampleApplication {
37+
38+
public static void main(String[] args) {
39+
SpringApplication.run(AguiExampleApplication.class, args);
40+
printStartupInfo();
41+
}
42+
43+
private static void printStartupInfo() {
44+
System.out.println("\n=== AG-UI Example Application Started ===");
45+
System.out.println("Open: http://localhost:8080");
46+
System.out.println("API: POST http://localhost:8080/agui/run");
47+
System.out.println("\nExample curl command:");
48+
System.out.println(" curl -N -X POST http://localhost:8080/agui/run \\");
49+
System.out.println(" -H \"Content-Type: application/json\" \\");
50+
System.out.println(
51+
" -d"
52+
+ " '{\"threadId\":\"test\",\"runId\":\"1\",\"messages\":[{\"id\":\"m1\",\"role\":\"user\",\"content\":\"Hello!\"}]}'");
53+
System.out.println("\nPress Ctrl+C to stop.");
54+
}
55+
}
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
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+
* http://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.examples.agui.config;
17+
18+
import io.agentscope.core.ReActAgent;
19+
import io.agentscope.core.agent.Agent;
20+
import io.agentscope.core.agui.registry.AguiAgentRegistry;
21+
import io.agentscope.core.formatter.dashscope.DashScopeChatFormatter;
22+
import io.agentscope.core.memory.InMemoryMemory;
23+
import io.agentscope.core.model.DashScopeChatModel;
24+
import io.agentscope.core.tool.Toolkit;
25+
import io.agentscope.examples.agui.tools.ExampleTools;
26+
import org.springframework.beans.factory.annotation.Autowired;
27+
import org.springframework.context.annotation.Configuration;
28+
29+
/**
30+
* Configuration class that registers agents with the AG-UI registry.
31+
*
32+
* <p>This example demonstrates how to register multiple agents with different IDs.
33+
* Clients can select which agent to use via:
34+
* <ul>
35+
* <li>URL path variable: {@code POST /agui/run/{agentId}}</li>
36+
* <li>HTTP header: {@code X-Agent-Id: agentId}</li>
37+
* <li>Request body: {@code forwardedProps.agentId}</li>
38+
* </ul>
39+
*/
40+
@Configuration
41+
public class AgentConfiguration {
42+
43+
@Autowired
44+
public void configureAgents(AguiAgentRegistry registry) {
45+
// Register a factory for the default agent
46+
// Using a factory ensures each request gets a fresh agent instance
47+
registry.registerFactory("default", this::createDefaultAgent);
48+
49+
// Register additional agents with different IDs
50+
// Example: a simple chat agent without tools
51+
registry.registerFactory("chat", this::createChatAgent);
52+
53+
// Example: an agent specialized for calculations
54+
registry.registerFactory("calculator", this::createCalculatorAgent);
55+
56+
System.out.println("Registered agents with AG-UI registry: default, chat, calculator");
57+
System.out.println("Access agents via:");
58+
System.out.println(" - POST /agui/run (uses default-agent-id from config)");
59+
System.out.println(" - POST /agui/run/chat (uses 'chat' agent)");
60+
System.out.println(" - POST /agui/run with X-Agent-Id header");
61+
}
62+
63+
/**
64+
* Create the default agent instance.
65+
*
66+
* <p>This agent is configured with:
67+
* <ul>
68+
* <li>DashScope qwen-plus model with streaming enabled</li>
69+
* <li>Example tools (get_weather, calculate)</li>
70+
* <li>In-memory conversation memory</li>
71+
* </ul>
72+
*/
73+
private Agent createDefaultAgent() {
74+
String apiKey = getRequiredApiKey();
75+
76+
// Create toolkit with example tools
77+
Toolkit toolkit = new Toolkit();
78+
toolkit.registerTool(new ExampleTools());
79+
80+
// Create the agent
81+
return ReActAgent.builder()
82+
.name("AG-UI Assistant")
83+
.sysPrompt(
84+
"You are a helpful AI assistant exposed via the AG-UI protocol. "
85+
+ "You can help users with various tasks including weather queries "
86+
+ "and calculations. Be concise and helpful in your responses.")
87+
.model(
88+
DashScopeChatModel.builder().apiKey(apiKey).modelName("qwen-plus").stream(
89+
true)
90+
.enableThinking(false)
91+
.formatter(new DashScopeChatFormatter())
92+
.build())
93+
.toolkit(toolkit)
94+
.memory(new InMemoryMemory())
95+
.maxIters(10)
96+
.build();
97+
}
98+
99+
/**
100+
* Create a simple chat agent without tools.
101+
*
102+
* <p>This agent is a pure conversational assistant.
103+
*/
104+
private Agent createChatAgent() {
105+
String apiKey = getRequiredApiKey();
106+
107+
return ReActAgent.builder()
108+
.name("Chat Assistant")
109+
.sysPrompt(
110+
"You are a friendly conversational assistant. "
111+
+ "Engage in natural conversation and help users "
112+
+ "with general questions and discussions.")
113+
.model(
114+
DashScopeChatModel.builder().apiKey(apiKey).modelName("qwen-plus").stream(
115+
true)
116+
.formatter(new DashScopeChatFormatter())
117+
.build())
118+
.memory(new InMemoryMemory())
119+
.maxIters(1)
120+
.build();
121+
}
122+
123+
/**
124+
* Create a calculator agent specialized for mathematical operations.
125+
*/
126+
private Agent createCalculatorAgent() {
127+
String apiKey = getRequiredApiKey();
128+
129+
// Create toolkit with only calculation tools
130+
Toolkit toolkit = new Toolkit();
131+
toolkit.registerTool(new ExampleTools());
132+
133+
return ReActAgent.builder()
134+
.name("Calculator Agent")
135+
.sysPrompt(
136+
"You are a mathematical assistant specialized in calculations. "
137+
+ "Use the calculate tool to perform mathematical operations. "
138+
+ "Always show your work and explain the results.")
139+
.model(
140+
DashScopeChatModel.builder().apiKey(apiKey).modelName("qwen-plus").stream(
141+
true)
142+
.formatter(new DashScopeChatFormatter())
143+
.build())
144+
.toolkit(toolkit)
145+
.memory(new InMemoryMemory())
146+
.maxIters(5)
147+
.build();
148+
}
149+
150+
private String getRequiredApiKey() {
151+
String apiKey = System.getenv("DASHSCOPE_API_KEY");
152+
if (apiKey == null || apiKey.isEmpty()) {
153+
throw new IllegalStateException(
154+
"DASHSCOPE_API_KEY environment variable is required. "
155+
+ "Please set it before starting the application.");
156+
}
157+
return apiKey;
158+
}
159+
}

0 commit comments

Comments
 (0)