Skip to content

Commit 146d821

Browse files
Improved AgentScope documentation, still a WIP
1 parent 3c61fb7 commit 146d821

123 files changed

Lines changed: 19120 additions & 32 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.

‎README.md‎

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,143 @@ public class FirstAgent {
164164
}
165165
```
166166

167+
## Common Operations
168+
169+
### Equip an agent with tools
170+
171+
Annotate a plain method with `@Tool` / `@ToolParam`, register it on a `Toolkit`, and attach the
172+
toolkit to the builder — the model decides when to call it:
173+
174+
```java
175+
import io.agentscope.core.ReActAgent;
176+
import io.agentscope.core.message.UserMessage;
177+
import io.agentscope.core.tool.Tool;
178+
import io.agentscope.core.tool.ToolParam;
179+
import io.agentscope.core.tool.Toolkit;
180+
181+
public class MathTools {
182+
@Tool(name = "calculate", description = "Evaluate a simple math expression")
183+
public String calculate(
184+
@ToolParam(name = "expression", description = "e.g. '123 * 456'") String expression) {
185+
return expression + " = " + /* evaluate */ 56088;
186+
}
187+
}
188+
189+
Toolkit toolkit = new Toolkit();
190+
toolkit.registerTool(new MathTools());
191+
192+
ReActAgent agent = ReActAgent.builder()
193+
.name("ToolAgent")
194+
.sysPrompt("You are a helpful assistant with access to tools.")
195+
.model("dashscope:qwen-max")
196+
.toolkit(toolkit)
197+
.build();
198+
199+
agent.call(new UserMessage("What is 123 * 456?")).block();
200+
```
201+
202+
For tools that need permission checks, async execution, or `RuntimeContext` injection, extend
203+
`ToolBase` and register with `toolkit.registerAgentTool(...)` instead of `@Tool`.
204+
205+
### Gate tool calls with the Permission System
206+
207+
Attach a `PermissionContextState` to require human approval before sensitive tools run:
208+
209+
```java
210+
import io.agentscope.core.permission.PermissionBehavior;
211+
import io.agentscope.core.permission.PermissionContextState;
212+
import io.agentscope.core.permission.PermissionMode;
213+
import io.agentscope.core.permission.PermissionRule;
214+
215+
PermissionContextState permissionContext = PermissionContextState.builder()
216+
.mode(PermissionMode.DEFAULT) // ACCEPT_EDITS / EXPLORE / BYPASS / DONT_ASK also available
217+
.addAllowRule("calculate", new PermissionRule("calculate", null, PermissionBehavior.ALLOW, "policy"))
218+
.addAskRule("delete_file", new PermissionRule("delete_file", null, PermissionBehavior.ASK, "policy"))
219+
.build();
220+
221+
ReActAgent agent = ReActAgent.builder()
222+
.name("GuardedAgent")
223+
.model("dashscope:qwen-max")
224+
.toolkit(toolkit) // the Toolkit from "Equip an agent with tools" above
225+
.permissionContext(permissionContext)
226+
.build();
227+
228+
// A call to a tool matched by an ASK rule returns early with
229+
// Msg.getGenerateReason() == GenerateReason.PERMISSION_ASKING for you to resume or cancel.
230+
```
231+
232+
### Handle errors
233+
234+
Both `call()` (`Mono<Msg>`) and `streamEvents()` (`Flux<AgentEvent>`) are Reactor-based — use
235+
`onErrorResume` to recover reactively, or wrap a blocking `.block()` in try/catch:
236+
237+
```java
238+
import reactor.core.publisher.Mono;
239+
240+
// Reactive recovery
241+
agent.call(new UserMessage("Hello"))
242+
.onErrorResume(error -> Mono.just(new UserMessage("Sorry, something went wrong.")))
243+
.block();
244+
245+
// try/catch around a blocking call
246+
try {
247+
agent.call(new UserMessage("Hello")).block();
248+
} catch (Exception e) {
249+
System.err.println("Error: " + e.getMessage());
250+
}
251+
```
252+
253+
### Spawn a subagent
254+
255+
Declare a subagent on a `HarnessAgent`, then let the model call it at runtime via the built-in
256+
`agent_spawn` / `agent_send` tools:
257+
258+
```java
259+
import io.agentscope.harness.agent.HarnessAgent;
260+
import io.agentscope.harness.agent.subagent.SubagentDeclaration;
261+
import io.agentscope.harness.agent.subagent.WorkspaceMode;
262+
import java.nio.file.Path;
263+
264+
SubagentDeclaration reviewer = SubagentDeclaration.builder()
265+
.name("code-reviewer")
266+
.description("Reviews code for security, performance, and readability issues.")
267+
.workspace(Path.of("./defs/code-reviewer"))
268+
.workspaceMode(WorkspaceMode.ISOLATED)
269+
.model("qwen3-max")
270+
.tools(java.util.List.of("read_file", "grep_files", "edit_file"))
271+
.build();
272+
273+
HarnessAgent agent = HarnessAgent.builder()
274+
.name("lead")
275+
.model("dashscope:qwen-plus")
276+
.workspace(Path.of(".agentscope/workspace"))
277+
.subagent(reviewer)
278+
.build();
279+
// The lead agent can now call agent_spawn("code-reviewer", ...) / agent_send(...) at runtime.
280+
```
281+
282+
### Configure model provider API keys
283+
284+
`.model("<provider>:<model-name>")` resolves through `ModelRegistry`, which reads the matching
285+
environment variable automatically — no explicit credential wiring needed:
286+
287+
| Provider | Model string prefix | Environment variable |
288+
|---|---|---|
289+
| DashScope | `dashscope:qwen-plus` | `DASHSCOPE_API_KEY` |
290+
| OpenAI | `openai:gpt-4.1` | `OPENAI_API_KEY` |
291+
| Anthropic | `anthropic:claude-sonnet-4-7` | `ANTHROPIC_API_KEY` |
292+
| Gemini | `gemini:gemini-2.0-flash` | `GEMINI_API_KEY` |
293+
| Ollama (local) | `ollama:llama3` | none (optional `OLLAMA_BASE_URL`, defaults to `http://localhost:11434`) |
294+
295+
```bash
296+
# Pick the row matching your provider from the table above
297+
export DASHSCOPE_API_KEY=sk-your-key-here
298+
export OPENAI_API_KEY=sk-your-key-here
299+
```
300+
301+
To set timeouts or a custom endpoint explicitly, build the provider model directly and pass the
302+
object instead of a string, e.g. `.model(DashScopeChatModel.builder().apiKey("...").modelName("qwen-max").build())`.
303+
167304
## AgentScope Service
168305
**[AgentScope Service](./agentscope-service)** — an Agent Control Plane built on AgentScope Harness that provides:
169306
+ **Control Plane.** It provides agent registration, discovery, and distributed coordination services for every agent in the enterprise. It works with mainstream agent runtimes including AgentScope, LangChain, ADK, and Claude / Qoder, giving you a single place to inspect agent metrics and operate on live sessions — for example, compressing session context.

‎agentscope-core/README.md‎

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
# agentscope-core
2+
3+
The flagship, low-level module of AgentScope Java. It provides `ReActAgent` — a
4+
reasoning-acting loop that wires together messages/events, tools, the permission
5+
system, structured output, model resolution, and state persistence — without any
6+
sandboxing, workspace, or orchestration layer on top. Reach for it when you want
7+
direct control over agent behavior; reach for `agentscope-harness` when you also
8+
need a filesystem workspace, subagents, or skills.
9+
10+
## Installation
11+
12+
```xml
13+
<dependency>
14+
<groupId>io.agentscope</groupId>
15+
<artifactId>agentscope-core</artifactId>
16+
<version>${agentscope.version}</version>
17+
</dependency>
18+
```
19+
20+
Model providers (DashScope, OpenAI, Anthropic, Gemini, Ollama, …) ship as separate
21+
extension modules and are discovered via Java SPI at runtime — add the one you need
22+
alongside `agentscope-core`, e.g. `agentscope-extensions-model-dashscope`.
23+
24+
## Quick Start
25+
26+
### A bare ReActAgent
27+
28+
```java
29+
import io.agentscope.core.ReActAgent;
30+
import io.agentscope.core.agent.RuntimeContext;
31+
import io.agentscope.core.message.Msg;
32+
import io.agentscope.core.message.UserMessage;
33+
import java.util.List;
34+
35+
ReActAgent agent =
36+
ReActAgent.builder()
37+
.name("assistant")
38+
.sysPrompt("You are a helpful assistant.")
39+
// Resolved by ModelRegistry; reads DASHSCOPE_API_KEY from the environment.
40+
// Swap the prefix to switch providers: "openai:...", "anthropic:...", "gemini:...".
41+
.model("dashscope:qwen-plus")
42+
.build();
43+
44+
Msg result =
45+
agent.call(List.of(new UserMessage("What files are in the current directory?")),
46+
RuntimeContext.empty())
47+
.block();
48+
System.out.println(result.getTextContent());
49+
```
50+
51+
### Tool calling
52+
53+
Annotate any method with `@Tool` / `@ToolParam`, register the instance with a
54+
`Toolkit`, and attach it to the agent builder — the LLM decides when to call it.
55+
56+
```java
57+
import io.agentscope.core.ReActAgent;
58+
import io.agentscope.core.tool.Tool;
59+
import io.agentscope.core.tool.ToolParam;
60+
import io.agentscope.core.tool.Toolkit;
61+
62+
public class WeatherTools {
63+
@Tool(name = "get_weather", description = "Get current weather for a city")
64+
public String getWeather(
65+
@ToolParam(name = "city", description = "City name") String city) {
66+
return "Sunny, 22C in " + city;
67+
}
68+
}
69+
70+
Toolkit toolkit = new Toolkit();
71+
toolkit.registerTool(new WeatherTools());
72+
73+
ReActAgent agent =
74+
ReActAgent.builder()
75+
.name("assistant")
76+
.sysPrompt("You are a helpful assistant.")
77+
.model("dashscope:qwen-plus")
78+
.toolkit(toolkit)
79+
.build();
80+
```
81+
82+
### Structured output
83+
84+
Pass a Java class (or a raw JSON Schema `JsonNode`) as the second argument to
85+
`call` and read the parsed result back off the returned `Msg`:
86+
87+
```java
88+
import io.agentscope.core.agent.RuntimeContext;
89+
import io.agentscope.core.message.Msg;
90+
import io.agentscope.core.message.UserMessage;
91+
import java.util.List;
92+
93+
public record WeatherResponse(String location, String temperature, String condition) {}
94+
95+
Msg result =
96+
agent.call(
97+
List.of(new UserMessage("What's the weather in SF?")),
98+
WeatherResponse.class,
99+
RuntimeContext.empty())
100+
.block();
101+
102+
WeatherResponse weather = result.getStructuredData(WeatherResponse.class);
103+
System.out.println(weather.location() + ": " + weather.temperature());
104+
```
105+
106+
### Permission system
107+
108+
Tool calls go through a three-way decision (`ALLOW` / `DENY` / `ASK`) governed by a
109+
`PermissionMode` plus explicit rules. Attach a `PermissionContextState` on the
110+
builder:
111+
112+
```java
113+
import io.agentscope.core.permission.PermissionBehavior;
114+
import io.agentscope.core.permission.PermissionContextState;
115+
import io.agentscope.core.permission.PermissionMode;
116+
import io.agentscope.core.permission.PermissionRule;
117+
118+
PermissionContextState permCtx =
119+
PermissionContextState.builder()
120+
.mode(PermissionMode.DEFAULT)
121+
.addAllowRule(
122+
"get_weather",
123+
new PermissionRule(
124+
"get_weather", null, PermissionBehavior.ALLOW, "userSettings"))
125+
.build();
126+
127+
ReActAgent agent =
128+
ReActAgent.builder()
129+
.name("assistant")
130+
.sysPrompt("You are a helpful assistant.")
131+
.model("dashscope:qwen-plus")
132+
.permissionContext(permCtx)
133+
.build();
134+
```
135+
136+
### Multi-session state persistence
137+
138+
`ReActAgent` is stateless between calls — one instance can serve many users and
139+
sessions concurrently. Set a `stateStore` and pass a per-call `RuntimeContext`
140+
carrying the `(userId, sessionId)` pair; the agent loads and saves `AgentState`
141+
automatically:
142+
143+
```java
144+
import io.agentscope.core.ReActAgent;
145+
import io.agentscope.core.agent.RuntimeContext;
146+
import io.agentscope.core.message.UserMessage;
147+
import io.agentscope.core.state.JsonFileAgentStateStore;
148+
import java.nio.file.Paths;
149+
150+
ReActAgent agent =
151+
ReActAgent.builder()
152+
.name("assistant")
153+
.sysPrompt("You are a helpful assistant.")
154+
.model("dashscope:qwen-plus")
155+
.stateStore(new JsonFileAgentStateStore(
156+
Paths.get(System.getProperty("user.home"), ".agentscope/sessions")))
157+
.build();
158+
159+
agent.call(List.of(new UserMessage("Hello")),
160+
RuntimeContext.builder().userId("alice").sessionId("session-1").build()).block();
161+
```
162+
163+
## Learn more
164+
165+
- [Agent](../docs/v2/en/docs/building-blocks/agent.md) — builder fields, streaming
166+
events, `RuntimeContext`, human-in-the-loop, structured output, and state
167+
persistence in full detail.
168+
- [Model](../docs/v2/en/docs/building-blocks/model.md) — credentials, `ModelRegistry`,
169+
and the provider extension modules.
170+
- [Tool](../docs/v2/en/docs/building-blocks/tool.md) — `@Tool`/`@ToolParam`,
171+
`Toolkit`, MCP clients, and tool groups.
172+
- [Permission System](../docs/v2/en/docs/building-blocks/permission-system.md) —
173+
modes, rules, and human-in-the-loop confirmation.

0 commit comments

Comments
 (0)