import asyncio
from agents import Agent, Runner, function_tool
from declaw.openai import SecurityPolicy, PIIConfig, InjectionDefenseConfig, SandboxNetworkOpts, VolumeAttachment
from declaw.sandbox_async.main import AsyncSandbox
async def analyze(question: str, volume_id: str) -> str:
sbx = await AsyncSandbox.create(
template="python",
network=SandboxNetworkOpts(allow_out=["api.openai.com"]),
security=SecurityPolicy(
pii=PIIConfig(enabled=True, action="redact"),
injection_defense=InjectionDefenseConfig(enabled=True, sensitivity="medium"),
),
volumes=[VolumeAttachment(volume_id=volume_id, mount_path="/data")],
)
@function_tool
async def run_shell(command: str) -> str:
r = await sbx.run_command(command, timeout=60)
return (r.stdout or "") + (r.stderr or "")
agent = Agent(
name="analyst",
model="gpt-4.1",
instructions="You are a data analyst. The dataset is at /data/sales.csv. "
"Use run_shell to inspect with awk/python. Answer with numbers.",
tools=[run_shell],
)
try:
result = await Runner.run(agent, question, max_turns=6)
return result.final_output or ""
finally:
await sbx.kill()
async def main():
vol = await AsyncVolumes.create(name="sales", data=open("sales.tar.gz","rb"))
try:
answers = await asyncio.gather(
analyze("Which product has the highest total revenue?", vol.volume_id),
analyze("Average total_usd per order for each region?", vol.volume_id),
analyze("Any orders with total_usd > 50× the median?", vol.volume_id),
)
for a in answers:
print(a, "\n---")
finally:
await AsyncVolumes.delete(vol.volume_id)
asyncio.run(main())