# provider_openai.py
import json
import os
from openai import AsyncOpenAI
from box_tool import SANDBOX_EXEC_SCHEMA, SANDBOX_EXEC_DESCRIPTION, sandbox_exec
TOOLS = [{
"type": "function",
"name": "sandbox_exec",
"description": SANDBOX_EXEC_DESCRIPTION,
"parameters": SANDBOX_EXEC_SCHEMA, # <-- the shared schema, unchanged
}]
def build_client():
# base_url is what selects the endpoint. Omit it for OpenAI itself.
return AsyncOpenAI(
api_key=os.environ["<YOUR_API_KEY_ENV_VAR>"], # e.g. OPENAI_API_KEY
base_url=os.getenv("<YOUR_BASE_URL_ENV_VAR>"), # e.g. https://api.minimax.io/v1
)
async def run_turn(client, box, goal, model, max_rounds=12):
response = await client.responses.create(
model=model, # e.g. "<YOUR_MODEL>"
instructions="Use sandbox_exec to interact with the environment. Summarize when done.",
input=[{"role": "user", "content": goal}],
tools=TOOLS,
tool_choice="auto",
)
for _ in range(max_rounds):
calls = [item for item in response.output if item.type == "function_call"]
if not calls:
return response
outputs = []
for call in calls:
args = json.loads(call.arguments or "{}")
result = await sandbox_exec(box, args.get("argv", []))
outputs.append({
"type": "function_call_output",
"call_id": call.call_id,
"output": json.dumps(result),
})
response = await client.responses.create(
model=model,
previous_response_id=response.id,
input=outputs,
tools=TOOLS,
tool_choice="auto",
)
return response