The Sandbox Service provides isolated tool execution environments (sandboxes) for agents to use tools like browsers or code executors securely. It manages the lifecycle of these environments, including:
- Creating Execution Environments: Generating new instances (e.g., a browser sandbox) for a specific user or session.
- Connecting to Existing Environments: Allowing agents to reconnect to previous sandboxes during multi-turn conversations.
- Tool Invocation: Providing callable methods (like
navigate or takeScreenshot) that can be registered as tools. - Releasing Environments: Cleaning up resources when sessions end.
Best Practice: Do not write underlying management logic for SandboxService or SandboxManager directly in your business code. Instead, use the encapsulated sandbox methods provided by ToolkitInit and register them in the Agent's Toolkit. This shields your business logic from changes in runtime modes or sandbox types.
import io.agentscope.core.tool.Toolkit;
import io.agentscope.runtime.engine.agents.agentscope.tools.ToolkitInit;
import io.agentscope.runtime.sandbox.box.BrowserSandbox;
import io.agentscope.runtime.sandbox.box.Sandbox;
import io.agentscope.runtime.sandbox.manager.ManagerConfig;
import io.agentscope.runtime.sandbox.manager.SandboxService;
// ... setup sandboxService ...
Toolkit toolkit = new Toolkit();
// Use ToolkitInit to wrap sandbox methods into tools
toolkit.registerTool(ToolkitInit.BrowserNavigateTool(sandbox));
toolkit.registerTool(ToolkitInit.BrowserTakeScreenshotTool(sandbox));