0.环境
0.1 依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.6</version>
<relativePath/>
</parent>
<properties>
<java.version>17</java.version>
<agentscope.version>1.1.0-RC2</agentscope.version>
</properties>
<dependency>
<groupId>io.agentscope</groupId>
<artifactId>agentscope-harness</artifactId>
<version>${agentscope.version}</version>
</dependency>
<dependency>
<groupId>io.agentscope</groupId>
<artifactId>agentscope-core</artifactId>
<version>${agentscope.version}</version>
</dependency>
0.2 QuickStart
public class QuickstartExample {
static final String BASE_URL = "https://api.minimaxi.com/anthropic";
static final String MODEL_NAME = "MiniMax-M2.5";
public static void main(String[] args) throws Exception {
// 1. 准备工作区:第一次运行生成 AGENTS.md,后续运行复用
Path workspace = Paths.get(".agentscope/workspace");
initWorkspaceIfAbsent(workspace);
// 2. 构建模型
Model model = AnthropicChatModel.builder()
.apiKey(API_KEY)
.modelName(MODEL_NAME)
.baseUrl(BASE_URL)
.stream(true)
.build();
// 3. 构建 HarnessAgent:工作区注入、会话持久化、追踪日志默认开启;
// 这里显式启用对话压缩
// name纬度区别文件
HarnessAgent agent = HarnessAgent.builder()
.name("quickstart-agent")
.sysPrompt("你是一个帮助用户做笔记的助手。")
.model(model)
.workspace(workspace)
.compaction(CompactionConfig.builder()
.triggerMessages(30)
.keepMessages(10)
.flushBeforeCompact(true) // 压缩前把事实提取到日流水账
.build())
.build();
// 4. 同一个 RuntimeContext 发起两轮对话
// sessionId 相同 → 第二轮自动从 Session 恢复第一轮的状态
RuntimeContext ctx = RuntimeContext.builder()
.sessionId("demo-session")
.userId("alice")
.build();
Msg turn1 = agent.call(
Msg.builder().role(MsgRole.USER)
.textContent("我叫天宇,今天准备一个关于 ReAct 的技术分享。")
.build(),
ctx).block();
System.out.println("[turn1] " + turn1.getTextContent());
Msg turn2 = agent.call(
Msg.builder().role(MsgRole.USER)
.textContent("我叫什么?我今天要干什么?")
.build(),
ctx).block();
System.out.println("[turn2] " + turn2.getTextContent());
}
private static void initWorkspaceIfAbsent(Path workspace) throws Exception {
Files.createDirectories(workspace);
Path agentsMd = workspace.resolve("AGENTS.md");
if (Files.exists(agentsMd)) return;
Files.writeString(agentsMd, """
# 笔记助手
你是一个帮助用户整理笔记和知识的助手。
## 行为约定
- 主动记录用户提到的关键事实(姓名、计划、偏好等)
- 回答用简洁中文,必要时给出要点列表
- 对不确定的内容要主动说明,不要臆造
""");
}
}
- 工作区生成的文件
![image]()
2.架构
3.Skill
3.1 workspace自动注入
3.2 显示注入
- Windows 无 sh 需要 disableShellTool()
public static void main(String[] args) throws Exception {
String pythonDir = "D:\\Code\\Python";
String currentPath = System.getenv("Path");
if (currentPath == null || !currentPath.contains(pythonDir)) {
ProcessBuilder pb = new ProcessBuilder();
pb.environment().put("Path", pythonDir + ";" + pythonDir + "\\Scripts;" + (currentPath != null ? currentPath : ""));
System.out.println("[Env] 已将 Python 加入进程 PATH: " + pythonDir);
}
Path workspace = Paths.get(".agentscope/workspace").toAbsolutePath();
Files.createDirectories(workspace);
Model model = AnthropicChatModel.builder()
.apiKey(API_KEY)
.modelName(MODEL_NAME)
.baseUrl(BASE_URL)
.stream(true)
.build();
Toolkit toolkit = new Toolkit();
SkillBox skillBox = new SkillBox(toolkit);
Path skillsPath = workspace.resolve("skills");
if (Files.exists(skillsPath) && Files.isDirectory(skillsPath)) {
AgentSkillRepository skillRepo = new FileSystemSkillRepository(skillsPath);
for (AgentSkill skill : skillRepo.getAllSkills()) {
skillBox.registerSkill(skill);
System.out.println("[Skill] 已注册: " + skill.getName() + " - " + skill.getDescription());
}
} else {
System.out.println("[Skill] workspace/skills/ 目录不存在,跳过技能加载");
}
ShellCommandTool shellTool = new ShellCommandTool(
null,
Set.of("python", "py", "pip", "jshell", "java", "javac", "dir", "type", "echo", "where", "cmd"),
command -> true
);
skillBox.codeExecution()
.workDir(workspace.toString())
.withShell(shellTool)
.withWrite()
.enable();
System.out.println("[CodeExecution] 已启用, workDir=" + skillBox.getCodeExecutionWorkDir());
HarnessAgent agent = HarnessAgent.builder()
.name("demo-agent")
.sysPrompt("你是一个功能全面的智能助手,能够帮助用户完成各种任务。使用简洁中文回答。当需要执行计算或脚本时,使用 python-runner 技能。当前环境有 Python 3.9 可用,执行命令为 python,脚本后缀为 .py。重要:写脚本请用 write_text_file 工具,执行脚本请用 execute_shell_command 工具,两者共享同一工作目录。")
.model(model)
.workspace(workspace)
.toolkit(toolkit)
.skillRepository(new FileSystemSkillRepository(skillsPath))
.disableShellTool()
.disableFilesystemTools()
.compaction(CompactionConfig.builder()
.triggerMessages(50)
.keepMessages(20)
.build())
.build();
System.out.println("=== HarnessAgent Python Runner Demo ===\n");
RuntimeContext ctx = RuntimeContext.builder()
.sessionId("python-demo-session")
.userId("demo-user")
.build();
System.out.println("--- 第1轮: 用Python执行计算 ---");
Msg msg1 = Msg.builder().role(MsgRole.USER).textContent("执行一下lwx-test").build();
Msg reply1 = agent.call(msg1, ctx).block();
System.out.println("[助手] " + (reply1 != null ? reply1.getTextContent() : "无响应"));
System.out.println("\n=== Demo 完成 ===");
}
---
name: lwx-test
description: 当用户指明使用lwx-test SKILL时,才使用该SKILL
---
# 功能描述
执行 write_log.py
``
python D:\WorkSpace\AI\MyHarness\.agentscope\workspace\skills\lwx-test\write_log.py
``