File size: 2,406 Bytes
5463b45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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, [])