|
| 1 | +import argparse |
| 2 | +import asyncio |
| 3 | +import base64 |
| 4 | +from pathlib import Path |
| 5 | +from tempfile import TemporaryDirectory |
| 6 | +from zipfile import ZIP_DEFLATED, ZipFile |
| 7 | + |
| 8 | +from openai.types.responses import ResponseFunctionShellToolCall |
| 9 | +from openai.types.responses.response_container_reference import ResponseContainerReference |
| 10 | + |
| 11 | +from agents import Agent, Runner, ShellTool, ShellToolInlineSkill, trace |
| 12 | +from agents.items import ModelResponse |
| 13 | + |
| 14 | +SKILL_NAME = "csv-workbench" |
| 15 | +SKILL_DIR = Path(__file__).resolve().parent / "skills" / SKILL_NAME |
| 16 | + |
| 17 | + |
| 18 | +def build_skill_zip_bundle() -> bytes: |
| 19 | + with TemporaryDirectory(prefix="agents-inline-skill-") as temp_dir: |
| 20 | + zip_path = Path(temp_dir) / f"{SKILL_NAME}.zip" |
| 21 | + with ZipFile(zip_path, "w", compression=ZIP_DEFLATED) as archive: |
| 22 | + for path in sorted(SKILL_DIR.rglob("*")): |
| 23 | + if path.is_file(): |
| 24 | + archive.write(path, f"{SKILL_NAME}/{path.relative_to(SKILL_DIR)}") |
| 25 | + return zip_path.read_bytes() |
| 26 | + |
| 27 | + |
| 28 | +def build_inline_skill() -> ShellToolInlineSkill: |
| 29 | + bundle = build_skill_zip_bundle() |
| 30 | + return { |
| 31 | + "type": "inline", |
| 32 | + "name": SKILL_NAME, |
| 33 | + "description": "Analyze CSV files in /mnt/data and return concise numeric summaries.", |
| 34 | + "source": { |
| 35 | + "type": "base64", |
| 36 | + "media_type": "application/zip", |
| 37 | + "data": base64.b64encode(bundle).decode("ascii"), |
| 38 | + }, |
| 39 | + } |
| 40 | + |
| 41 | + |
| 42 | +def extract_container_id(raw_responses: list[ModelResponse]) -> str | None: |
| 43 | + for response in raw_responses: |
| 44 | + for item in response.output: |
| 45 | + if isinstance(item, ResponseFunctionShellToolCall) and isinstance( |
| 46 | + item.environment, ResponseContainerReference |
| 47 | + ): |
| 48 | + return item.environment.container_id |
| 49 | + |
| 50 | + return None |
| 51 | + |
| 52 | + |
| 53 | +async def main(model: str) -> None: |
| 54 | + inline_skill = build_inline_skill() |
| 55 | + |
| 56 | + with trace("container_shell_inline_skill_example"): |
| 57 | + agent1 = Agent( |
| 58 | + name="Container Shell Agent (Inline Skill)", |
| 59 | + model=model, |
| 60 | + instructions="Use the available container skill to answer user requests.", |
| 61 | + tools=[ |
| 62 | + ShellTool( |
| 63 | + environment={ |
| 64 | + "type": "container_auto", |
| 65 | + "network_policy": {"type": "disabled"}, |
| 66 | + "skills": [inline_skill], |
| 67 | + } |
| 68 | + ) |
| 69 | + ], |
| 70 | + ) |
| 71 | + |
| 72 | + result1 = await Runner.run( |
| 73 | + agent1, |
| 74 | + ( |
| 75 | + "Use the csv-workbench skill. Create /mnt/data/orders.csv with columns " |
| 76 | + "id,region,amount,status and at least 6 rows. Then report total amount by " |
| 77 | + "region and count failed orders." |
| 78 | + ), |
| 79 | + ) |
| 80 | + print(f"Agent: {result1.final_output}") |
| 81 | + |
| 82 | + container_id = extract_container_id(result1.raw_responses) |
| 83 | + if not container_id: |
| 84 | + raise RuntimeError("Container ID was not returned in shell call output.") |
| 85 | + |
| 86 | + print(f"[info] Reusing container_id={container_id}") |
| 87 | + |
| 88 | + agent2 = Agent( |
| 89 | + name="Container Reference Shell Agent", |
| 90 | + model=model, |
| 91 | + instructions="Reuse the existing shell container and answer concisely.", |
| 92 | + tools=[ |
| 93 | + ShellTool( |
| 94 | + environment={ |
| 95 | + "type": "container_reference", |
| 96 | + "container_id": container_id, |
| 97 | + } |
| 98 | + ) |
| 99 | + ], |
| 100 | + ) |
| 101 | + |
| 102 | + result2 = await Runner.run( |
| 103 | + agent2, |
| 104 | + "Run `ls -la /mnt/data`, then summarize in one sentence.", |
| 105 | + ) |
| 106 | + print(f"Agent (container reuse): {result2.final_output}") |
| 107 | + |
| 108 | + |
| 109 | +if __name__ == "__main__": |
| 110 | + parser = argparse.ArgumentParser() |
| 111 | + parser.add_argument( |
| 112 | + "--model", |
| 113 | + default="gpt-5.2", |
| 114 | + help="Model name to use.", |
| 115 | + ) |
| 116 | + args = parser.parse_args() |
| 117 | + asyncio.run(main(args.model)) |
0 commit comments