File size: 12,852 Bytes
df455fc
 
 
 
 
d425821
df455fc
 
 
 
c6bc2db
df455fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
import json
from types import SimpleNamespace

import pytest

from agent_action_tools import (
    build_openai_tools,
    parse_tool_invocation,
    tool_invocation_to_action,
)
from ppt_agent.inference import (
    SYSTEM_PROMPT,
    _planning_payload,
    _validate_tool_choice,
    choose_action,
)


def _observation(**overrides):
    payload = {
        "task_name": "sample_task",
        "difficulty": "easy",
        "task_prompt": "Create a 3-slide executive summary.",
        "source_context": "[Doc]\nRevenue grew 12% year over year.",
        "prompt_summary": "Create a 3-slide executive summary.",
        "slide_count": 1,
        "last_action_error": None,
        "last_action_result": None,
        "score": 0.0,
        "metadata": {
            "current_theme": {"accent": "#2563EB", "font": "Aptos"},
            "known_named_shapes_by_slide": {},
            "slide_constraints": {"min_slides": 3, "max_slides": 3},
            "default_save_path": "outputs/sample_task_episode.pptx",
            "max_steps": 8,
            "step_count": 0,
        },
    }
    payload.update(overrides)
    return SimpleNamespace(**payload)


def test_tool_invocation_to_action_builds_create_slide_payload_directly():
    invocation = parse_tool_invocation(
        "create_slide",
        {
            "background_color": "<surface>",
            "shapes": [
                {
                    "type": "text",
                    "name": "title",
                    "text": "Growth remains resilient",
                    "x": 0.8,
                    "y": 0.8,
                    "w": 5.0,
                    "h": 0.8,
                    "style": {
                        "font_name": "<font>",
                        "font_size_pt": "<title_size>",
                        "color_hex": "<primary>",
                        "bold": True,
                        "word_wrap": True,
                        "space_after_pt": 6,
                        "line_spacing": 1.2,
                    },
                }
            ],
        },
    )

    action = tool_invocation_to_action(invocation, default_save_path="outputs/out.pptx")

    assert action.action_type == "create_slide"
    assert action.slide_index is None
    assert action.payload["background_color"] == "<surface>"
    assert action.payload["shapes"][0]["type"] == "text"
    assert action.payload["shapes"][0]["style"]["font_size_pt"] == "<title_size>"
    assert action.payload["shapes"][0]["style"]["word_wrap"] is True
    assert action.payload["shapes"][0]["style"]["space_after_pt"] == 6


def test_parse_tool_invocation_rejects_invalid_text_style_alias_keys():
    with pytest.raises(Exception):
        parse_tool_invocation(
            "create_slide",
            {
                "shapes": [
                    {
                        "type": "text",
                        "text": "Growth remains resilient",
                        "x": 0.8,
                        "y": 0.8,
                        "w": 5.0,
                        "h": 0.8,
                        "style": {"color": "<primary>", "font_size": 24},
                    }
                ]
            },
        )


def test_parse_tool_invocation_accepts_compact_alias_fields():
    invocation = parse_tool_invocation(
        "update_slide",
        {
            "si": 2,
            "bg": "<surface>",
            "add": [
                {
                    "type": "chart",
                    "name": "revenue_chart",
                    "ct": "bar_clustered",
                    "cd": {"categories": ["Q1", "Q2"], "series": []},
                    "x": 0.7,
                    "y": 1.4,
                    "w": 5.4,
                    "h": 3.0,
                }
            ],
            "upd": [{"type": "text", "id": 11, "text": "Updated title"}],
            "del": [12],
        },
    )

    action = tool_invocation_to_action(invocation, default_save_path="outputs/out.pptx")

    assert action.action_type == "update_slide"
    assert action.slide_index == 2
    assert action.payload == {
        "background_color": "<surface>",
        "delete_shape_ids": [12],
        "add_shapes": [
            {
                "type": "chart",
                "name": "revenue_chart",
                "chart_type": "bar_clustered",
                "chart_data": {"categories": ["Q1", "Q2"], "series": []},
                "x": 0.7,
                "y": 1.4,
                "w": 5.4,
                "h": 3.0,
            }
        ],
        "update_shapes": [{"type": "text", "shape_id": 11, "text": "Updated title"}],
    }


def test_validate_tool_choice_blocks_early_save():
    observation = _observation(slide_count=2)
    invocation = parse_tool_invocation(
        "save_presentation", {"path": "outputs/out.pptx"}
    )

    with pytest.raises(ValueError, match="cannot save"):
        _validate_tool_choice(invocation, observation)


def test_tool_invocation_to_action_builds_set_theme_payload_directly():
    invocation = parse_tool_invocation(
        "set_theme",
        {
            "accent": "#112233",
            "font": "Inter",
            "title_size": 30,
        },
    )

    action = tool_invocation_to_action(invocation, default_save_path="outputs/out.pptx")

    assert action.action_type == "set_theme"
    assert action.payload == {
        "accent": "#112233",
        "font": "Inter",
        "title_size": 30,
    }


def test_parse_tool_invocation_rejects_removed_layout_index_field():
    with pytest.raises(Exception):
        parse_tool_invocation("create_slide", {"layout_index": 6, "shapes": []})


def test_parse_tool_invocation_rejects_removed_citation_shape_type():
    with pytest.raises(Exception):
        parse_tool_invocation(
            "create_slide",
            {
                "shapes": [
                    {
                        "type": "citation",
                        "text": "Source: quarterly report",
                    }
                ]
            },
        )


def test_planning_payload_uses_observation_planning_metadata():
    history = [
        {
            "tool_name": "create_slide",
            "arguments": {"background_color": "<surface>", "shapes": []},
            "slide_index": 1,
            "tool_result": {"named_shapes": {"title": 11, "accent": 12}},
            "error": None,
        },
        {
            "tool_name": "update_slide",
            "arguments": {"slide_index": 1, "update_shapes": []},
            "slide_index": 1,
            "tool_result": {
                "named_shapes": {"body": 13},
                "deleted_shape_ids": [12],
            },
            "error": None,
        },
    ]

    observation = _observation(
        metadata={
            "current_theme": {"accent": "#2563EB", "font": "Aptos"},
            "known_named_shapes_by_slide": {1: {"title": 11, "body": 13}},
            "slide_constraints": {"min_slides": 3, "max_slides": 3},
            "default_save_path": "outputs/sample_task_episode.pptx",
            "max_steps": 8,
            "step_count": 2,
        }
    )

    prompt = _planning_payload(observation, history)

    assert prompt["recent_actions"][0]["tool_name"] == "create_slide"
    assert prompt["known_named_shapes_by_slide"][1] == {"title": 11, "body": 13}
    assert prompt["current_theme"] == {
        "accent": "#2563EB",
        "font": "Aptos",
    }
    assert prompt["slide_constraints"] == {
        "min_slides": 3,
        "max_slides": 3,
    }
    assert prompt["default_save_path"] == "outputs/sample_task_episode.pptx"
    assert prompt["remaining_steps"] == 6
    assert "task_name" not in prompt
    assert "difficulty" not in prompt
    assert "prompt_summary" not in prompt
    assert "requirements" not in prompt


def test_system_prompt_warns_against_old_macro_dsl_fields():
    assert "background -> use the schema field, not legacy DSL keys" in SYSTEM_PROMPT
    assert "type: title/body -> use type: text" in SYSTEM_PROMPT
    assert '"font_size_pt"' in SYSTEM_PROMPT
    assert "set_theme" in SYSTEM_PROMPT


def test_build_openai_tools_exposes_compact_tool_schema():
    tools = {
        tool["function"]["name"]: tool["function"] for tool in build_openai_tools()
    }

    create_slide = tools["create_slide"]
    update_slide = tools["update_slide"]

    assert "title" not in create_slide["parameters"]
    assert "default" not in json.dumps(create_slide["parameters"])
    assert create_slide["parameters"]["properties"]["bg"]["type"] == "string"
    assert update_slide["parameters"]["required"] == ["si"]


def test_choose_action_uses_tool_call_and_returns_agent_action():
    class FakeCompletions:
        def __init__(self):
            self.calls = []

        def create(self, **kwargs):
            self.calls.append(kwargs)
            return SimpleNamespace(
                choices=[
                    SimpleNamespace(
                        message=SimpleNamespace(
                            tool_calls=[
                                SimpleNamespace(
                                    function=SimpleNamespace(
                                        name="create_slide",
                                        arguments=json.dumps(
                                            {
                                                "background_color": "<surface>",
                                                "shapes": [
                                                    {
                                                        "type": "text",
                                                        "name": "title",
                                                        "text": "Growth outlook",
                                                        "x": 0.8,
                                                        "y": 0.8,
                                                        "w": 4.5,
                                                        "h": 0.7,
                                                    }
                                                ],
                                            }
                                        ),
                                    )
                                )
                            ],
                            content=None,
                        )
                    )
                ]
            )

    completions = FakeCompletions()
    client = SimpleNamespace(chat=SimpleNamespace(completions=completions))

    action, action_str, invocation = choose_action(
        client, _observation(slide_count=0), []
    )

    assert invocation.tool_name == "create_slide"
    assert action.action_type == "create_slide"
    assert action.payload["shapes"][0]["text"] == "Growth outlook"
    assert json.loads(action_str)["action_type"] == "create_slide"
    assert completions.calls[0]["tool_choice"] == "required"
    assert completions.calls[0]["tools"]


def test_choose_action_does_not_retry_after_error():
    class FakeCompletions:
        def __init__(self):
            self.calls = []

        def create(self, **kwargs):
            self.calls.append(kwargs)
            return SimpleNamespace(
                choices=[
                    SimpleNamespace(
                        message=SimpleNamespace(
                            tool_calls=[
                                SimpleNamespace(
                                    function=SimpleNamespace(
                                        name="save_presentation",
                                        arguments=json.dumps(
                                            {"path": "outputs/out.pptx"}
                                        ),
                                    )
                                )
                            ],
                            content=None,
                        )
                    )
                ]
            )

    completions = FakeCompletions()
    client = SimpleNamespace(chat=SimpleNamespace(completions=completions))

    with pytest.raises(ValueError, match="cannot save"):
        choose_action(client, _observation(slide_count=0), [])

    assert len(completions.calls) == 1


def test_choose_action_requires_structured_tool_calls():
    class FakeCompletions:
        def create(self, **kwargs):
            return SimpleNamespace(
                choices=[
                    SimpleNamespace(
                        message=SimpleNamespace(
                            tool_calls=[], content='{"tool_name":"create_slide"}'
                        )
                    )
                ]
            )

    client = SimpleNamespace(chat=SimpleNamespace(completions=FakeCompletions()))

    with pytest.raises(ValueError, match="exactly one tool call"):
        choose_action(client, _observation(slide_count=0), [])