From e345ab6eb0d54acab00a91ac5375ff8f80d89819 Mon Sep 17 00:00:00 2001 From: xuehuitian45 <13069167198@163.com> Date: Thu, 11 Dec 2025 20:45:35 +0800 Subject: [PATCH 1/2] feat: add simple sandbox tool example --- examples/simple_sandbox_tool_example/pom.xml | 67 +++++++++ .../agentscope/AgentScopeApplication.java | 56 ++++++++ .../agentscope/config/AgentConfig.java | 116 +++++++++++++++ .../agentscope/controller/ChatController.java | 77 ++++++++++ .../example/agentscope/model/ChatRequest.java | 42 ++++++ .../agentscope/model/ChatResponse.java | 78 +++++++++++ .../example/agentscope/model/ToolInfo.java | 50 +++++++ .../agentscope/service/ChatService.java | 132 ++++++++++++++++++ .../agentscope/service/ResourceService.java | 18 +++ .../agentscope/tools/CalculatorTool.java | 93 ++++++++++++ .../example/agentscope/tools/WeatherTool.java | 108 ++++++++++++++ 11 files changed, 837 insertions(+) create mode 100644 examples/simple_sandbox_tool_example/pom.xml create mode 100644 examples/simple_sandbox_tool_example/src/main/java/com/example/agentscope/AgentScopeApplication.java create mode 100644 examples/simple_sandbox_tool_example/src/main/java/com/example/agentscope/config/AgentConfig.java create mode 100644 examples/simple_sandbox_tool_example/src/main/java/com/example/agentscope/controller/ChatController.java create mode 100644 examples/simple_sandbox_tool_example/src/main/java/com/example/agentscope/model/ChatRequest.java create mode 100644 examples/simple_sandbox_tool_example/src/main/java/com/example/agentscope/model/ChatResponse.java create mode 100644 examples/simple_sandbox_tool_example/src/main/java/com/example/agentscope/model/ToolInfo.java create mode 100644 examples/simple_sandbox_tool_example/src/main/java/com/example/agentscope/service/ChatService.java create mode 100644 examples/simple_sandbox_tool_example/src/main/java/com/example/agentscope/service/ResourceService.java create mode 100644 examples/simple_sandbox_tool_example/src/main/java/com/example/agentscope/tools/CalculatorTool.java create mode 100644 examples/simple_sandbox_tool_example/src/main/java/com/example/agentscope/tools/WeatherTool.java diff --git a/examples/simple_sandbox_tool_example/pom.xml b/examples/simple_sandbox_tool_example/pom.xml new file mode 100644 index 00000000..4d7b5c8f --- /dev/null +++ b/examples/simple_sandbox_tool_example/pom.xml @@ -0,0 +1,67 @@ + + + 4.0.0 + + + org.springframework.boot + spring-boot-starter-parent + 3.2.0 + + + + com.example + agentscope-examples-simple + 1.0.0 + AgentScope Spring MVC Demo + AgentScope simple sample project using Spring MVC + + + 17 + 17 + 17 + UTF-8 + + + + + + org.springframework.boot + spring-boot-starter-web + + + + + io.agentscope + agentscope-core + 0.2.0 + + + + + io.agentscope + agentscope-runtime-agentscope + 1.0.0-BETA1 + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + org.projectlombok + lombok + + + + + + + + diff --git a/examples/simple_sandbox_tool_example/src/main/java/com/example/agentscope/AgentScopeApplication.java b/examples/simple_sandbox_tool_example/src/main/java/com/example/agentscope/AgentScopeApplication.java new file mode 100644 index 00000000..76cdd041 --- /dev/null +++ b/examples/simple_sandbox_tool_example/src/main/java/com/example/agentscope/AgentScopeApplication.java @@ -0,0 +1,56 @@ +package com.example.agentscope; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * AgentScope Spring Boot application main class + */ +@SpringBootApplication +public class AgentScopeApplication { + + public static void main(String[] args) { + SpringApplication.run(AgentScopeApplication.class, args); + + System.out.println("\n" + "=".repeat(70)); + System.out.println("๐ŸŽ‰ AgentScope Demo started successfully!"); + System.out.println("=".repeat(70)); + System.out.println(); + + System.out.println("๐Ÿ“ก API endpoints:"); + System.out.println(" โ€ข Health check: http://localhost:8080/api/chat/health"); + System.out.println(" โ€ข Send message: http://localhost:8080/api/chat"); + System.out.println(" โ€ข View tools: http://localhost:8080/api/chat/tools"); + System.out.println(" โ€ข Reset conversation: http://localhost:8080/api/chat/reset"); + System.out.println(); + + System.out.println("๐Ÿ› ๏ธ Available tools:"); + System.out.println(" โ€ข Weather tool - query city weather and forecasts"); + System.out.println(" โ€ข Calculator tool - math operations (add, subtract, multiply, divide, power, sqrt)"); + System.out.println(" โ€ข Sandbox tool - sandbox browser search"); + System.out.println(); + + System.out.println("๐Ÿ’ก Quick tests:"); + System.out.println(" curl -X POST http://localhost:8080/api/chat \\"); + System.out.println(" -H \"Content-Type: application/json\" \\"); + System.out.println(" -d '{\"message\": \"How is the weather in Beijing today?\"}'"); + System.out.println(); + + System.out.println("curl -X POST http://localhost:8080/api/chat \\\n" + + " -H \"Content-Type: application/json\" \\\n" + + " -d '{\"message\": \"Use the browser tool to search on Baidu for today'\\''s gold price\"}'"); + + System.out.println(" curl -X POST http://localhost:8080/api/chat \\"); + System.out.println(" -H \"Content-Type: application/json\" \\"); + System.out.println(" -d '{\"message\": \"Calculate 123 + 456\"}'"); + System.out.println(); + + System.out.println("๐Ÿ“š More info:"); + System.out.println(" โ€ข See README.md for detailed docs"); + System.out.println(" โ€ข See QUICKSTART.md to get started quickly"); + System.out.println(" โ€ข Import AgentScope-API.postman_collection.json to test with Postman"); + System.out.println(); + System.out.println("=".repeat(70) + "\n"); + } +} + diff --git a/examples/simple_sandbox_tool_example/src/main/java/com/example/agentscope/config/AgentConfig.java b/examples/simple_sandbox_tool_example/src/main/java/com/example/agentscope/config/AgentConfig.java new file mode 100644 index 00000000..0da135e1 --- /dev/null +++ b/examples/simple_sandbox_tool_example/src/main/java/com/example/agentscope/config/AgentConfig.java @@ -0,0 +1,116 @@ +package com.example.agentscope.config; + +import com.example.agentscope.tools.CalculatorTool; +import com.example.agentscope.tools.WeatherTool; +import io.agentscope.core.ReActAgent; +import io.agentscope.core.memory.InMemoryMemory; +import io.agentscope.core.model.DashScopeChatModel; +import io.agentscope.core.tool.Toolkit; +import io.agentscope.runtime.engine.agents.agentscope.tools.ToolkitInit; +import io.agentscope.runtime.engine.services.sandbox.SandboxService; +import io.agentscope.runtime.sandbox.box.BrowserSandbox; +import io.agentscope.runtime.sandbox.box.Sandbox; +import io.agentscope.runtime.sandbox.manager.SandboxManager; +import io.agentscope.runtime.sandbox.manager.client.config.BaseClientConfig; +import io.agentscope.runtime.sandbox.manager.client.config.DockerClientConfig; +import io.agentscope.runtime.sandbox.manager.model.ManagerConfig; +import org.checkerframework.checker.units.qual.A; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Scope; + +/** + * AgentScope configuration class + * Configure Agent, Model, and Toolkit + */ +@Configuration +public class AgentConfig { + + @Value("${DASHSCOPE_API_KEY:#{null}}") + private String apiKey; + + /** + * Configure the DashScope chat model + */ + @Bean + public DashScopeChatModel chatModel() { + String key = apiKey != null ? apiKey : System.getenv("DASHSCOPE_API_KEY"); + + if (key == null || key.isEmpty()) { + throw new IllegalStateException( + "DASHSCOPE_API_KEY is not configured. Please set env var: export DASHSCOPE_API_KEY=your-api-key" + ); + } + + return DashScopeChatModel.builder() + .apiKey(key) + .modelName("qwen3-max") + .build(); + } + + /** + * Reuse SandboxService as a singleton + */ + @Bean + public SandboxService sandboxService() { + BaseClientConfig clientConfig = DockerClientConfig.builder().build(); + ManagerConfig managerConfig = ManagerConfig.builder() + .containerDeployment(clientConfig) + .build(); + + SandboxService service = new SandboxService( + new SandboxManager(managerConfig) + ); + service.start(); + return service; + } + + /** + * Configure the toolkit and register all tools + */ + @Bean + public Toolkit createToolkit(SandboxService sandboxService) { + Toolkit toolkit = new Toolkit(); + WeatherTool weatherTool = new WeatherTool(); + CalculatorTool calculatorTool = new CalculatorTool(); + + toolkit.registerTool(weatherTool); + toolkit.registerTool(calculatorTool); + try { + Sandbox sandbox = sandboxService.connect("userId", "sessionId", BrowserSandbox.class); + toolkit.registerTool(ToolkitInit.BrowserNavigateTool(sandbox)); + if (sandbox instanceof BrowserSandbox browserSandbox) { + String desktopUrl = browserSandbox.getDesktopUrl(); + System.out.println("GUI Desktop URL: " + desktopUrl); + } + } catch (Exception ignored) { + } + return toolkit; + } + + /** + * Create an independent Agent for each request to avoid shared state + */ + @Bean + @Scope("prototype") + public ReActAgent createAgentInstance(DashScopeChatModel chatModel, Toolkit toolkit) { + return ReActAgent.builder() + .name("Smart Assistant") + .sysPrompt(""" + You are an intelligent assistant named "XiaoZhi". You can help users: + 1. Query weather information + 2. Perform mathematical calculations + + Use the available tools to answer user questions and provide accurate and helpful responses. + Be friendly, professional, and clear in your answers. + """) + .model(chatModel) + .toolkit(toolkit) + .memory(new InMemoryMemory()) + .maxIters(5) + .build(); + } +} + diff --git a/examples/simple_sandbox_tool_example/src/main/java/com/example/agentscope/controller/ChatController.java b/examples/simple_sandbox_tool_example/src/main/java/com/example/agentscope/controller/ChatController.java new file mode 100644 index 00000000..d85badd8 --- /dev/null +++ b/examples/simple_sandbox_tool_example/src/main/java/com/example/agentscope/controller/ChatController.java @@ -0,0 +1,77 @@ +package com.example.agentscope.controller; + +import com.example.agentscope.model.ChatRequest; +import com.example.agentscope.model.ChatResponse; +import com.example.agentscope.service.ChatService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +/** + * Chat API controller + * Provide external REST API endpoints + */ +@RestController +@RequestMapping("/chat") +@CrossOrigin(origins = "*") +public class ChatController { + + @Autowired + private ChatService chatService; + + /** + * Health check endpoint + */ + @GetMapping("/health") + public ResponseEntity health() { + return ResponseEntity.ok("AgentScope service is running โœ“"); + } + + /** + * Send a message to the Agent + * + * @param request Chat request + * @return Chat response + */ + @PostMapping + public ResponseEntity chat(@RequestBody ChatRequest request) { + try { + ChatResponse response = chatService.chat(request); + return ResponseEntity.ok(response); + } catch (Exception e) { + ChatResponse errorResponse = new ChatResponse(); + errorResponse.setSuccess(false); + errorResponse.setMessage("An error occurred while processing the request: " + e.getMessage()); + errorResponse.setError(e.getClass().getSimpleName()); + return ResponseEntity.internalServerError().body(errorResponse); + } + } + + /** + * Get available tool list + */ + @GetMapping("/tools") + public ResponseEntity getTools() { + try { + return ResponseEntity.ok(chatService.getAvailableTools()); + } catch (Exception e) { + return ResponseEntity.internalServerError() + .body("Failed to get tool list: " + e.getMessage()); + } + } + + /** + * Reset conversation history + */ + @PostMapping("/reset") + public ResponseEntity reset() { + try { + chatService.resetMemory(); + return ResponseEntity.ok("Conversation history reset โœ“"); + } catch (Exception e) { + return ResponseEntity.internalServerError() + .body("Reset failed: " + e.getMessage()); + } + } +} + diff --git a/examples/simple_sandbox_tool_example/src/main/java/com/example/agentscope/model/ChatRequest.java b/examples/simple_sandbox_tool_example/src/main/java/com/example/agentscope/model/ChatRequest.java new file mode 100644 index 00000000..24caeb87 --- /dev/null +++ b/examples/simple_sandbox_tool_example/src/main/java/com/example/agentscope/model/ChatRequest.java @@ -0,0 +1,42 @@ +package com.example.agentscope.model; + +/** + * Chat request model + */ +public class ChatRequest { + + private String message; + private String userName; + + public ChatRequest() { + } + + public ChatRequest(String message) { + this.message = message; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public String getUserName() { + return userName; + } + + public void setUserName(String userName) { + this.userName = userName; + } + + @Override + public String toString() { + return "ChatRequest{" + + "message='" + message + '\'' + + ", userName='" + userName + '\'' + + '}'; + } +} + diff --git a/examples/simple_sandbox_tool_example/src/main/java/com/example/agentscope/model/ChatResponse.java b/examples/simple_sandbox_tool_example/src/main/java/com/example/agentscope/model/ChatResponse.java new file mode 100644 index 00000000..56c47c0c --- /dev/null +++ b/examples/simple_sandbox_tool_example/src/main/java/com/example/agentscope/model/ChatResponse.java @@ -0,0 +1,78 @@ +package com.example.agentscope.model; + +/** + * Chat response model + */ +public class ChatResponse { + + private boolean success; + private String message; + private String agentName; + private Long timestamp; + private Long processingTime; + private String error; + + public ChatResponse() { + } + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public String getAgentName() { + return agentName; + } + + public void setAgentName(String agentName) { + this.agentName = agentName; + } + + public Long getTimestamp() { + return timestamp; + } + + public void setTimestamp(Long timestamp) { + this.timestamp = timestamp; + } + + public Long getProcessingTime() { + return processingTime; + } + + public void setProcessingTime(Long processingTime) { + this.processingTime = processingTime; + } + + public String getError() { + return error; + } + + public void setError(String error) { + this.error = error; + } + + @Override + public String toString() { + return "ChatResponse{" + + "success=" + success + + ", message='" + message + '\'' + + ", agentName='" + agentName + '\'' + + ", timestamp=" + timestamp + + ", processingTime=" + processingTime + + ", error='" + error + '\'' + + '}'; + } +} + diff --git a/examples/simple_sandbox_tool_example/src/main/java/com/example/agentscope/model/ToolInfo.java b/examples/simple_sandbox_tool_example/src/main/java/com/example/agentscope/model/ToolInfo.java new file mode 100644 index 00000000..53704c02 --- /dev/null +++ b/examples/simple_sandbox_tool_example/src/main/java/com/example/agentscope/model/ToolInfo.java @@ -0,0 +1,50 @@ +package com.example.agentscope.model; + +import java.util.Map; + +/** + * Tool info model + */ +public class ToolInfo { + + private String name; + private String description; + private Map parameters; + + public ToolInfo() { + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public Map getParameters() { + return parameters; + } + + public void setParameters(Map parameters) { + this.parameters = parameters; + } + + @Override + public String toString() { + return "ToolInfo{" + + "name='" + name + '\'' + + ", description='" + description + '\'' + + ", parameters=" + parameters + + '}'; + } +} + diff --git a/examples/simple_sandbox_tool_example/src/main/java/com/example/agentscope/service/ChatService.java b/examples/simple_sandbox_tool_example/src/main/java/com/example/agentscope/service/ChatService.java new file mode 100644 index 00000000..3e9f1cb5 --- /dev/null +++ b/examples/simple_sandbox_tool_example/src/main/java/com/example/agentscope/service/ChatService.java @@ -0,0 +1,132 @@ +package com.example.agentscope.service; + +import com.example.agentscope.model.ChatRequest; +import com.example.agentscope.model.ChatResponse; +import com.example.agentscope.model.ToolInfo; +import com.example.agentscope.tools.CalculatorTool; +import com.example.agentscope.tools.WeatherTool; +import io.agentscope.core.ReActAgent; +import io.agentscope.core.memory.InMemoryMemory; +import io.agentscope.core.message.Msg; +import io.agentscope.core.message.MsgRole; +import io.agentscope.core.message.TextBlock; +import io.agentscope.core.model.DashScopeChatModel; +import io.agentscope.core.tool.Toolkit; +import io.agentscope.runtime.engine.agents.agentscope.tools.ToolkitInit; +import io.agentscope.runtime.engine.services.sandbox.SandboxService; +import io.agentscope.runtime.sandbox.box.BrowserSandbox; +import io.agentscope.runtime.sandbox.box.Sandbox; +import org.checkerframework.checker.units.qual.A; +import org.springframework.beans.factory.ObjectProvider; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +/** + * Chat service + * Handle interaction logic with the Agent + */ +@Service +public class ChatService { + @Autowired + private ObjectProvider agentProvider; + + @Autowired + private Toolkit toolkit; + + + /** + * Handle chat requests + */ + public ChatResponse chat(ChatRequest request) { + long startTime = System.currentTimeMillis(); + + System.out.println("\n" + "=".repeat(50)); + System.out.println("๐Ÿ“จ Received user message: " + request.getMessage()); + System.out.println("=".repeat(50)); + + try { + // Create user message + Msg userMsg = Msg.builder() + .name(request.getUserName() != null ? request.getUserName() : "user") + .role(MsgRole.USER) + .content(List.of( + TextBlock.builder() + .text(request.getMessage()) + .build() + )) + .build(); + + // Invoke the Agent + ReActAgent agent = agentProvider.getObject(); + Msg responseMsg = agent.call(userMsg).block(); + + long duration = System.currentTimeMillis() - startTime; + System.out.println("\nโœ… Processing completed, duration: " + duration + "ms"); + System.out.println("=".repeat(50) + "\n"); + + // Build response + ChatResponse response = new ChatResponse(); + response.setSuccess(true); + if (responseMsg != null) { + response.setMessage(responseMsg.getTextContent()); + } + response.setAgentName(agent.getName()); + response.setTimestamp(System.currentTimeMillis()); + response.setProcessingTime(duration); + + return response; + + } catch (Exception e) { + System.err.println("โŒ Processing failed: " + e.getMessage()); + + ChatResponse errorResponse = new ChatResponse(); + errorResponse.setSuccess(false); + errorResponse.setMessage("Sorry, an issue occurred while processing your request."); + errorResponse.setError(e.getMessage()); + errorResponse.setTimestamp(System.currentTimeMillis()); + + return errorResponse; + } + } + + /** + * Get available tool list + */ + public List getAvailableTools() { + return toolkit.getToolSchemas().stream() + .map(schema -> { + Map schemaMap = new HashMap<>(); + if (schema != null) { + schema.forEach((k, v) -> schemaMap.put(String.valueOf(k), v)); + } + + ToolInfo info = new ToolInfo(); + Object function = schemaMap.get("function"); + if(function instanceof Map){ + info.setName(((Map) function).get("name").toString()); + info.setDescription(((Map) function).get("description").toString()); + if(((Map) function).get("parameters") instanceof Map paramsMap){ + Map paramMap = new HashMap<>(); + paramsMap.forEach((k, v) -> paramMap.put(String.valueOf(k), v)); + info.setParameters(paramMap); + } + } + return info; + }) + .collect(Collectors.toList()); + } + + /** + * Reset conversation memory + */ + public void resetMemory() { + // Each request creates a new Agent; memory is not shared by default + System.out.println("๐Ÿ”„ Reset request received (each request creates a new Agent by default)"); + } +} + diff --git a/examples/simple_sandbox_tool_example/src/main/java/com/example/agentscope/service/ResourceService.java b/examples/simple_sandbox_tool_example/src/main/java/com/example/agentscope/service/ResourceService.java new file mode 100644 index 00000000..655117bc --- /dev/null +++ b/examples/simple_sandbox_tool_example/src/main/java/com/example/agentscope/service/ResourceService.java @@ -0,0 +1,18 @@ +package com.example.agentscope.service; + +import io.agentscope.runtime.engine.services.sandbox.SandboxService; +import jakarta.annotation.PreDestroy; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class ResourceService { + @Autowired + public SandboxService sandboxService; + + @PreDestroy + public void cleanup() { + System.out.println("CloseOperation: Releasing resources..."); + sandboxService.stop(); + } +} diff --git a/examples/simple_sandbox_tool_example/src/main/java/com/example/agentscope/tools/CalculatorTool.java b/examples/simple_sandbox_tool_example/src/main/java/com/example/agentscope/tools/CalculatorTool.java new file mode 100644 index 00000000..2b216de7 --- /dev/null +++ b/examples/simple_sandbox_tool_example/src/main/java/com/example/agentscope/tools/CalculatorTool.java @@ -0,0 +1,93 @@ +package com.example.agentscope.tools; + +import io.agentscope.core.tool.Tool; +import io.agentscope.core.tool.ToolParam; +import org.springframework.stereotype.Component; + +/** + * Calculator tool + * Provide basic mathematical operations + */ +@Component +public class CalculatorTool { + + /** + * Add two numbers + */ + @Tool(description = "Calculate the sum of two numbers") + public double add( + @ToolParam(name = "a", description = "First number") double a, + @ToolParam(name = "b", description = "Second number") double b) { + + System.out.println("โž• Calculator invoked: " + a + " + " + b); + return a + b; + } + + /** + * Subtract two numbers + */ + @Tool(description = "Calculate the difference of two numbers") + public double subtract( + @ToolParam(name = "a", description = "Minuend") double a, + @ToolParam(name = "b", description = "Subtrahend") double b) { + + System.out.println("โž– Calculator invoked: " + a + " - " + b); + return a - b; + } + + /** + * Multiply two numbers + */ + @Tool(description = "Calculate the product of two numbers") + public double multiply( + @ToolParam(name = "a", description = "First number") double a, + @ToolParam(name = "b", description = "Second number") double b) { + + System.out.println("โœ–๏ธ Calculator invoked: " + a + " ร— " + b); + return a * b; + } + + /** + * Divide two numbers + */ + @Tool(description = "Calculate the quotient of two numbers") + public double divide( + @ToolParam(name = "a", description = "Dividend") double a, + @ToolParam(name = "b", description = "Divisor") double b) { + + System.out.println("โž— Calculator invoked: " + a + " รท " + b); + + if (b == 0) { + throw new IllegalArgumentException("Divisor cannot be zero"); + } + return a / b; + } + + /** + * Calculate exponentiation + */ + @Tool(description = "Calculate a number raised to a power") + public double power( + @ToolParam(name = "base", description = "Base") double base, + @ToolParam(name = "exponent", description = "Exponent") double exponent) { + + System.out.println("๐Ÿ”ข Calculator invoked: " + base + " ^ " + exponent); + return Math.pow(base, exponent); + } + + /** + * Calculate square root + */ + @Tool(description = "Calculate the square root of a number") + public double sqrt( + @ToolParam(name = "number", description = "Number to calculate the square root of") double number) { + + System.out.println("โˆš Calculator invoked: โˆš" + number); + + if (number < 0) { + throw new IllegalArgumentException("Cannot calculate the square root of a negative number"); + } + return Math.sqrt(number); + } +} + diff --git a/examples/simple_sandbox_tool_example/src/main/java/com/example/agentscope/tools/WeatherTool.java b/examples/simple_sandbox_tool_example/src/main/java/com/example/agentscope/tools/WeatherTool.java new file mode 100644 index 00000000..83ce238a --- /dev/null +++ b/examples/simple_sandbox_tool_example/src/main/java/com/example/agentscope/tools/WeatherTool.java @@ -0,0 +1,108 @@ +package com.example.agentscope.tools; + +import io.agentscope.core.tool.Tool; +import io.agentscope.core.tool.ToolParam; +import org.springframework.stereotype.Component; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.HashMap; +import java.util.Map; +import java.util.Random; + +/** + * Weather query tool + * Provide city weather lookup using mock data + */ +@Component +public class WeatherTool { + + private final Random random = new Random(); + + // Mock weather database + private final Map weatherDatabase = new HashMap<>() {{ + put("Beijing", new String[]{"Sunny", "Cloudy", "Overcast", "Light rain"}); + put("Shanghai", new String[]{"Cloudy", "Light rain", "Sunny", "Overcast"}); + put("Guangzhou", new String[]{"Sunny", "Cloudy", "Thunderstorm", "Sunny"}); + put("Shenzhen", new String[]{"Cloudy", "Sunny", "Light rain", "Sunny"}); + put("Hangzhou", new String[]{"Overcast", "Light rain", "Cloudy", "Sunny"}); + put("Chengdu", new String[]{"Cloudy", "Overcast", "Light rain", "Cloudy"}); + }}; + + /** + * Get current weather information for a specified city + * + * @param city City name (for example: Beijing, Shanghai, Guangzhou) + * @return Weather info string + */ + @Tool(description = "Get current weather for a specified city, including condition and temperature") + public String getWeather( + @ToolParam(name = "city", description = "City name, for example: Beijing, Shanghai, Guangzhou") + String city) { + + System.out.println("๐ŸŒค๏ธ Weather tool invoked: querying weather for " + city); + + // Simulate querying weather + String[] conditions = weatherDatabase.getOrDefault( + city, + new String[]{"Sunny", "Cloudy", "Overcast", "Light rain"} + ); + + String condition = conditions[random.nextInt(conditions.length)]; + int temperature = 15 + random.nextInt(20); // 15-35ยฐC + int humidity = 40 + random.nextInt(40); // 40-80% + String time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("HH:mm")); + + return String.format( + "Weather for %s\n" + + "๐Ÿ• Time: %s\n" + + "โ˜๏ธ Condition: %s\n" + + "๐ŸŒก๏ธ Temperature: %dยฐC\n" + + "๐Ÿ’ง Humidity: %d%%", + city, time, condition, temperature, humidity + ); + } + + /** + * Get the weather forecast for the coming days + * + * @param city City name + * @param days Number of forecast days (1-7 days) + * @return Weather forecast details + */ + @Tool(description = "Get the weather forecast for a specified city for the next few days") + public String getForecast( + @ToolParam(name = "city", description = "City name") + String city, + @ToolParam(name = "days", description = "Number of forecast days, range 1-7") + int days) { + + System.out.println("๐Ÿ“… Forecast tool invoked: querying weather for " + city + " for the next " + days + " days"); + + if (days < 1 || days > 7) { + return "Forecast days must be between 1 and 7"; + } + + StringBuilder forecast = new StringBuilder(); + forecast.append(String.format("Weather forecast for %s for the next %d days\n", city, days)); + + String[] conditions = weatherDatabase.getOrDefault( + city, + new String[]{"Sunny", "Cloudy", "Overcast", "Light rain"} + ); + + for (int i = 1; i <= days; i++) { + String condition = conditions[random.nextInt(conditions.length)]; + int tempHigh = 20 + random.nextInt(15); + int tempLow = 10 + random.nextInt(10); + + forecast.append(String.format( + "\nDay %d: %s, temperature %d~%dยฐC", + i, condition, tempLow, tempHigh + )); + } + + return forecast.toString(); + } +} + From 26094cdabf03e67aa69c1e2f62a1687b9b61c9df Mon Sep 17 00:00:00 2001 From: xuehuitian45 <13069167198@163.com> Date: Thu, 11 Dec 2025 20:50:48 +0800 Subject: [PATCH 2/2] fix: add licenses --- .../agentscope/AgentScopeApplication.java | 16 ++++++++++++++++ .../example/agentscope/config/AgentConfig.java | 16 ++++++++++++++++ .../agentscope/controller/ChatController.java | 16 ++++++++++++++++ .../example/agentscope/model/ChatRequest.java | 16 ++++++++++++++++ .../example/agentscope/model/ChatResponse.java | 16 ++++++++++++++++ .../com/example/agentscope/model/ToolInfo.java | 16 ++++++++++++++++ .../example/agentscope/service/ChatService.java | 16 ++++++++++++++++ .../agentscope/service/ResourceService.java | 16 ++++++++++++++++ .../example/agentscope/tools/CalculatorTool.java | 16 ++++++++++++++++ .../example/agentscope/tools/WeatherTool.java | 16 ++++++++++++++++ 10 files changed, 160 insertions(+) diff --git a/examples/simple_sandbox_tool_example/src/main/java/com/example/agentscope/AgentScopeApplication.java b/examples/simple_sandbox_tool_example/src/main/java/com/example/agentscope/AgentScopeApplication.java index 76cdd041..d0e5bdb5 100644 --- a/examples/simple_sandbox_tool_example/src/main/java/com/example/agentscope/AgentScopeApplication.java +++ b/examples/simple_sandbox_tool_example/src/main/java/com/example/agentscope/AgentScopeApplication.java @@ -1,3 +1,19 @@ +/* + * Copyright 2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package com.example.agentscope; import org.springframework.boot.SpringApplication; diff --git a/examples/simple_sandbox_tool_example/src/main/java/com/example/agentscope/config/AgentConfig.java b/examples/simple_sandbox_tool_example/src/main/java/com/example/agentscope/config/AgentConfig.java index 0da135e1..5875b880 100644 --- a/examples/simple_sandbox_tool_example/src/main/java/com/example/agentscope/config/AgentConfig.java +++ b/examples/simple_sandbox_tool_example/src/main/java/com/example/agentscope/config/AgentConfig.java @@ -1,3 +1,19 @@ +/* + * Copyright 2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package com.example.agentscope.config; import com.example.agentscope.tools.CalculatorTool; diff --git a/examples/simple_sandbox_tool_example/src/main/java/com/example/agentscope/controller/ChatController.java b/examples/simple_sandbox_tool_example/src/main/java/com/example/agentscope/controller/ChatController.java index d85badd8..09425fa2 100644 --- a/examples/simple_sandbox_tool_example/src/main/java/com/example/agentscope/controller/ChatController.java +++ b/examples/simple_sandbox_tool_example/src/main/java/com/example/agentscope/controller/ChatController.java @@ -1,3 +1,19 @@ +/* + * Copyright 2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package com.example.agentscope.controller; import com.example.agentscope.model.ChatRequest; diff --git a/examples/simple_sandbox_tool_example/src/main/java/com/example/agentscope/model/ChatRequest.java b/examples/simple_sandbox_tool_example/src/main/java/com/example/agentscope/model/ChatRequest.java index 24caeb87..b9a50b3e 100644 --- a/examples/simple_sandbox_tool_example/src/main/java/com/example/agentscope/model/ChatRequest.java +++ b/examples/simple_sandbox_tool_example/src/main/java/com/example/agentscope/model/ChatRequest.java @@ -1,3 +1,19 @@ +/* + * Copyright 2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package com.example.agentscope.model; /** diff --git a/examples/simple_sandbox_tool_example/src/main/java/com/example/agentscope/model/ChatResponse.java b/examples/simple_sandbox_tool_example/src/main/java/com/example/agentscope/model/ChatResponse.java index 56c47c0c..e665d765 100644 --- a/examples/simple_sandbox_tool_example/src/main/java/com/example/agentscope/model/ChatResponse.java +++ b/examples/simple_sandbox_tool_example/src/main/java/com/example/agentscope/model/ChatResponse.java @@ -1,3 +1,19 @@ +/* + * Copyright 2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package com.example.agentscope.model; /** diff --git a/examples/simple_sandbox_tool_example/src/main/java/com/example/agentscope/model/ToolInfo.java b/examples/simple_sandbox_tool_example/src/main/java/com/example/agentscope/model/ToolInfo.java index 53704c02..6918eb41 100644 --- a/examples/simple_sandbox_tool_example/src/main/java/com/example/agentscope/model/ToolInfo.java +++ b/examples/simple_sandbox_tool_example/src/main/java/com/example/agentscope/model/ToolInfo.java @@ -1,3 +1,19 @@ +/* + * Copyright 2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package com.example.agentscope.model; import java.util.Map; diff --git a/examples/simple_sandbox_tool_example/src/main/java/com/example/agentscope/service/ChatService.java b/examples/simple_sandbox_tool_example/src/main/java/com/example/agentscope/service/ChatService.java index 3e9f1cb5..ee2151e4 100644 --- a/examples/simple_sandbox_tool_example/src/main/java/com/example/agentscope/service/ChatService.java +++ b/examples/simple_sandbox_tool_example/src/main/java/com/example/agentscope/service/ChatService.java @@ -1,3 +1,19 @@ +/* + * Copyright 2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package com.example.agentscope.service; import com.example.agentscope.model.ChatRequest; diff --git a/examples/simple_sandbox_tool_example/src/main/java/com/example/agentscope/service/ResourceService.java b/examples/simple_sandbox_tool_example/src/main/java/com/example/agentscope/service/ResourceService.java index 655117bc..a59d8e8e 100644 --- a/examples/simple_sandbox_tool_example/src/main/java/com/example/agentscope/service/ResourceService.java +++ b/examples/simple_sandbox_tool_example/src/main/java/com/example/agentscope/service/ResourceService.java @@ -1,3 +1,19 @@ +/* + * Copyright 2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package com.example.agentscope.service; import io.agentscope.runtime.engine.services.sandbox.SandboxService; diff --git a/examples/simple_sandbox_tool_example/src/main/java/com/example/agentscope/tools/CalculatorTool.java b/examples/simple_sandbox_tool_example/src/main/java/com/example/agentscope/tools/CalculatorTool.java index 2b216de7..60d2e86e 100644 --- a/examples/simple_sandbox_tool_example/src/main/java/com/example/agentscope/tools/CalculatorTool.java +++ b/examples/simple_sandbox_tool_example/src/main/java/com/example/agentscope/tools/CalculatorTool.java @@ -1,3 +1,19 @@ +/* + * Copyright 2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package com.example.agentscope.tools; import io.agentscope.core.tool.Tool; diff --git a/examples/simple_sandbox_tool_example/src/main/java/com/example/agentscope/tools/WeatherTool.java b/examples/simple_sandbox_tool_example/src/main/java/com/example/agentscope/tools/WeatherTool.java index 83ce238a..9bb57d97 100644 --- a/examples/simple_sandbox_tool_example/src/main/java/com/example/agentscope/tools/WeatherTool.java +++ b/examples/simple_sandbox_tool_example/src/main/java/com/example/agentscope/tools/WeatherTool.java @@ -1,3 +1,19 @@ +/* + * Copyright 2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package com.example.agentscope.tools; import io.agentscope.core.tool.Tool;