This example demonstrates how to build a Google ADK agent that generates and verifies code using Daytona sandboxes. The agent uses the DaytonaPlugin to execute code in an isolated environment, enabling automated code generation workflows with built-in testing.
In this example, the agent is tasked with writing a TypeScript groupBy function that takes an array and a key function, then groups array elements by the key. The agent generates the implementation, creates test cases, executes them in the sandbox, and iterates until all tests pass before returning the verified code.
- Secure sandbox execution: All code runs in isolated Daytona sandboxes
- Multi-language support: Generate code in Python, JavaScript, or TypeScript
- Automatic testing: Agent creates and runs tests to verify implementations
- Iterative refinement: Agent fixes code until tests pass before responding
- Natural language interface: Describe your function in plain English
- Python: Version 3.10 or higher is required
Tip
It's recommended to use a virtual environment (venv or poetry) to isolate project dependencies.
To run this example, you need to set the following environment variables:
DAYTONA_API_KEY: Required for access to Daytona sandboxes. Get it from Daytona DashboardGOOGLE_API_KEY: Required for Gemini model access. Get it from Google AI Studio
See the .env.example file for the exact structure. Copy .env.example to .env and fill in your API keys before running.
Before proceeding, complete the following steps:
- Ensure Python 3.10 or higher is installed
- Copy
.env.exampleto.envand add your API keys
-
Create and activate a virtual environment:
python3.10 -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install dependencies:
pip install -U google-adk daytona-adk python-dotenv
-
Run the example:
python main.py
- Agent Instruction: The
AGENT_INSTRUCTIONconstant inmain.pydefines the agent's behavior. You can customize this to change how the agent approaches code generation and testing.
When you run the example, the agent follows this workflow:
- Receive Request: The agent receives your natural language description of a function
- Generate Code: Agent writes the function implementation in the specified language
- Create Tests: Agent generates test cases to verify the implementation
- Execute in Sandbox: Code and tests run in an isolated Daytona sandbox
- Iterate: If tests fail, agent fixes the code and re-executes until tests pass
- Return Result: Once verified, the agent returns only the working function code
- Cleanup: Sandbox resources are automatically cleaned up
When the agent completes the task, you'll see output like:
AGENT RESPONSE:
------------------------------------------------------------
```typescript
function groupBy<T, K extends keyof any>(
array: T[],
keyFn: (item: T) => K
): Record<K, T[]> {
return array.reduce((result, item) => {
const key = keyFn(item);
if (!result[key]) {
result[key] = [];
}
result[key].push(item);
return result;
}, {} as Record<K, T[]>);
}
```
============================================================
App closed, sandbox cleaned up. Done!
The agent has already tested this code in the sandbox before returning it, so you can trust that the implementation works correctly.
For the complete API reference, see the daytona-adk documentation.
See the main project LICENSE file for details.