# 1) Create the initial response request with the tool enabled
response = client.responses.create(
    model="codex-mini-latest",
    tools=[{"type": "local_shell"}],
    inputs=[
        {
            "type": "message",
            "role": "user",
            "content": [{"type": "text", "text": "List files in the current directory"}],
        }
    ],
)

while True:
    # 2) Look for a local_shell_call in the model's output items
    shell_calls = [item for item in response.output if item["type"] == "local_shell_call"]
    if not shell_calls:
        # No more commands â the assistant is done.
        break

    call = shell_calls[0]
    args = call["action"]

    # 3) Execute the command locally (here we just trust the command!)
    #    The command is already split into argv tokens.
    completed = subprocess.run(
        args["command"],
        cwd=args.get("working_directory") or os.getcwd(),
        env={**os.environ, **args.get("env", {})},
        capture_output=True,
        text=True,
        timeout=(args["timeout_ms"] / 1000) if args["timeout_ms"] else None,
    )

    output_item = {
        "type": "local_shell_call_output",
        "call_id": call["call_id"],
        "output": completed.stdout + completed.stderr,
    }

    # 4) Send the output back to the model to continue the conversation
    response = client.responses.create(
        model="codex-mini-latest",
        tools=[{"type": "local_shell"}],
        previous_response_id=response.id,
        inputs=[output_item],
    )