import asyncio from pathlib import Path import sys from types import SimpleNamespace import unittest sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "scripts")) from stateless_openai import StatelessResponses class Item: def __init__(self, value): self.value = value def model_dump(self, **kwargs): return self.value class StatelessTests(unittest.IsolatedAsyncioTestCase): async def test_replays_reasoning_phase_and_tool_batch_without_response_id(self): calls = [] output = [ {"type": "reasoning", "encrypted_content": "opaque"}, {"type": "message", "role": "assistant", "phase": "commentary", "content": []}, {"type": "computer_call", "call_id": "c1", "actions": [{"type": "screenshot"}, {"type": "move", "x": 1, "y": 2}]}, ] class API: async def create(self, **kwargs): calls.append(kwargs) return SimpleNamespace(status="completed", id="r" + str(len(calls)), output=[Item(x) for x in output]) transport = StatelessResponses(API()) await transport.create(input="Inspect desktop", model="gpt-6-astra", prompt_cache_key="trial-1") result = {"type": "computer_call_output", "call_id": "c1", "output": {"type": "computer_screenshot", "image_url": "data:image/png;base64,test"}} await transport.create(previous_response_id="r1", input=[result], model="gpt-6-astra", prompt_cache_key="trial-1") self.assertEqual(calls[1]["input"][1:4], output) self.assertEqual(calls[1]["input"][-1], result) for call in calls: self.assertNotIn("previous_response_id", call) self.assertFalse(call["store"]) self.assertEqual(call["prompt_cache_key"], "trial-1") with self.assertRaises(ValueError): await transport.create(previous_response_id="other-trial", input=[result]) async def test_incomplete_response_cannot_be_reported_as_completion(self): class API: async def create(self, **kwargs): return SimpleNamespace(status="incomplete", id="r1", output=[]) transport = StatelessResponses(API()) with self.assertRaises(RuntimeError): await transport.create(input="test", prompt_cache_key="trial-1") self.assertEqual(transport.history, [])