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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
# -*- coding: utf-8 -*-
"""Comprehensive formatter unit tests for XAIChatFormatter and

XAIMultiAgentFormatter (xAI), following the reference test style.



Because these formatters return xai_sdk protobuf Message objects (not plain

dicts), a lightweight xai_sdk stub is built at module load so that tests run

without the real package.  The stub objects support __eq__ and __repr__ so

full assertListEqual comparisons work.

"""
import sys
from typing import Any
from types import ModuleType
from unittest import IsolatedAsyncioTestCase
from unittest.mock import MagicMock

from agentscope.formatter import XAIChatFormatter, XAIMultiAgentFormatter
from agentscope.message import (
    UserMsg,
    AssistantMsg,
    SystemMsg,
    TextBlock,
    DataBlock,
    Base64Source,
    ToolCallBlock,
    ToolResultBlock,
    ThinkingBlock,
    ToolResultState,
    HintBlock,
)


# ---------------------------------------------------------------------------
# Comparable stub objects for xai_sdk protobuf messages.
# ---------------------------------------------------------------------------


class _StubMessage:
    """Comparable stub for xai_sdk.chat.{user,assistant,system,tool_result}."""

    def __init__(

        self,

        role: str,

        args: tuple = (),

        kwargs: dict | None = None,

    ) -> None:
        self.role = role
        self.args = args
        self.kwargs = kwargs or {}

    def __eq__(self, other: object) -> bool:
        if not isinstance(other, _StubMessage):
            return NotImplemented
        return (
            self.role == other.role
            and self.args == other.args
            and self.kwargs == other.kwargs
        )

    def __repr__(self) -> str:
        parts = [f"role={self.role!r}", f"args={self.args!r}"]
        if self.kwargs:
            parts.append(f"kwargs={self.kwargs!r}")
        return f"_StubMessage({', '.join(parts)})"


class _StubImage:
    """Comparable stub for xai_sdk.chat.image()."""

    def __init__(self, url: str) -> None:
        self.type = "image"
        self.url = url

    def __eq__(self, other: object) -> bool:
        if not isinstance(other, _StubImage):
            return NotImplemented
        return self.url == other.url

    def __repr__(self) -> str:
        return f"_StubImage(url={self.url!r})"


def user(*args: Any) -> _StubMessage:
    """Create a comparable stub user message."""
    return _StubMessage(role="user", args=args)


def assistant(*args: Any) -> _StubMessage:
    """Create a comparable stub assistant message."""
    return _StubMessage(role="assistant", args=args)


def system(*args: Any) -> _StubMessage:
    """Create a comparable stub system message."""
    return _StubMessage(role="system", args=args)


def tool_result(*args: Any, **kwargs: Any) -> _StubMessage:
    """Create a comparable stub tool_result message."""
    return _StubMessage(role="tool", args=args, kwargs=kwargs)


def image(url: str) -> _StubImage:
    """Create a comparable stub image object."""
    return _StubImage(url=url)


# ---------------------------------------------------------------------------
# Build a lightweight xai_sdk stub so tests run without the real package.
# ---------------------------------------------------------------------------


def _build_xai_sdk_stub() -> None:
    if "xai_sdk" in sys.modules:
        return

    chat_pb2 = ModuleType("xai_sdk.chat.chat_pb2")

    class _EnumHelper:
        _mapping = {
            "ROLE_ASSISTANT": 2,
            "TOOL_CALL_TYPE_CLIENT_SIDE_TOOL": 1,
        }

        def Value(self, name: str) -> int:
            """Return the integer value for the given enum name."""
            return self._mapping.get(name, 0)

    chat_pb2.MessageRole = _EnumHelper()
    chat_pb2.ToolCallType = _EnumHelper()

    class _RepeatedField(list):
        def __init__(self, factory: Any) -> None:
            super().__init__()
            self._factory = factory

        def add(self) -> Any:
            """Add a new item using the factory and return it."""
            item = self._factory()
            self.append(item)
            return item

    class _FunctionSpec:
        def __init__(self) -> None:
            self.name = ""
            self.arguments = ""

    class _ToolCallProto:
        def __init__(self) -> None:
            self.id = ""
            self.type = 0
            self.function = _FunctionSpec()

    class _ContentPart:
        text: str = ""

    class _MessageProto:
        def __init__(self) -> None:
            self.role = 0
            self.content = _RepeatedField(_ContentPart)
            self.tool_calls = _RepeatedField(_ToolCallProto)

    chat_pb2.Message = _MessageProto

    xai_chat = ModuleType("xai_sdk.chat")
    xai_chat.chat_pb2 = chat_pb2
    xai_chat.user = user
    xai_chat.assistant = assistant
    xai_chat.system = system
    xai_chat.tool_result = tool_result
    xai_chat.image = image

    xai_sdk = ModuleType("xai_sdk")
    xai_sdk.chat = xai_chat
    xai_sdk.AsyncClient = MagicMock()

    sys.modules["xai_sdk"] = xai_sdk
    sys.modules["xai_sdk.chat"] = xai_chat
    sys.modules["xai_sdk.chat.chat_pb2"] = chat_pb2


_build_xai_sdk_stub()


class TestXAIFormatter(IsolatedAsyncioTestCase):
    """Comprehensive tests for XAI Chat and MultiAgent formatters.



    The stub objects support __eq__, so full assertListEqual works for

    user/assistant/system/tool_result messages.  Tool-call messages use

    _MessageProto which is checked via attribute assertions.

    """

    async def asyncSetUp(self) -> None:
        self.msgs_system = [
            SystemMsg(
                name="system",
                content="You're a helpful assistant.",
            ),
        ]

        self.msgs_conversation = [
            UserMsg(
                name="user",
                content="What is the capital of France?",
            ),
            AssistantMsg(
                name="assistant",
                content="The capital of France is Paris.",
            ),
            UserMsg(
                name="user",
                content="What is the capital of Germany?",
            ),
            AssistantMsg(
                name="assistant",
                content="The capital of Germany is Berlin.",
            ),
            UserMsg(
                name="user",
                content="What is the capital of Japan?",
            ),
        ]

        self.msgs_tools = [
            AssistantMsg(
                name="assistant",
                content=[
                    ToolCallBlock(
                        id="call_1",
                        name="get_capital",
                        input='{"country": "Japan"}',
                    ),
                    ToolResultBlock(
                        id="call_1",
                        name="get_capital",
                        output=[
                            TextBlock(text="The capital of Japan is Tokyo."),
                        ],
                        state=ToolResultState.SUCCESS,
                    ),
                    TextBlock(text="The capital of Japan is Tokyo."),
                ],
            ),
        ]

        self._hist_prompt = (
            XAIMultiAgentFormatter().conversation_history_prompt
        )

    # -------------------------------------------------------------------
    # XAIChatFormatter tests
    # -------------------------------------------------------------------

    async def test_chat_formatter_system_message(self) -> None:
        """System message becomes a system() stub."""
        fmt = XAIChatFormatter()
        res = await fmt.format(self.msgs_system)
        self.assertListEqual(
            [system("You're a helpful assistant.")],
            res,
        )

    async def test_chat_formatter_user_assistant(self) -> None:
        """User and assistant text messages are passed through correctly."""
        fmt = XAIChatFormatter()
        res = await fmt.format(self.msgs_conversation)
        self.assertListEqual(
            [
                user("What is the capital of France?"),
                assistant("The capital of France is Paris."),
                user("What is the capital of Germany?"),
                assistant("The capital of Germany is Berlin."),
                user("What is the capital of Japan?"),
            ],
            res,
        )

    async def test_chat_formatter_tool_call(self) -> None:
        """Assistant tool call becomes a _MessageProto with tool_calls set."""
        fmt = XAIChatFormatter()
        res = await fmt.format(
            [*self.msgs_system, *self.msgs_conversation, *self.msgs_tools],
        )
        # Find the tool call proto (has tool_calls list, role=ROLE_ASSISTANT=2)
        tool_call_msgs = [
            m
            for m in res
            if hasattr(m, "tool_calls") and len(m.tool_calls) > 0
        ]
        self.assertListEqual(
            [len(tool_call_msgs)],
            [1],
        )
        tc = tool_call_msgs[0].tool_calls[0]
        self.assertListEqual(
            [tc.id, tc.function.name, tc.function.arguments],
            ["call_1", "get_capital", '{"country": "Japan"}'],
        )

    async def test_chat_formatter_tool_result(self) -> None:
        """Tool result becomes a tool_result() stub with the right id."""
        fmt = XAIChatFormatter()
        res = await fmt.format(self.msgs_tools)
        tool_msgs = [m for m in res if m.role == "tool"]
        self.assertListEqual(
            tool_msgs,
            [
                tool_result(
                    "The capital of Japan is Tokyo.",
                    tool_call_id="call_1",
                ),
            ],
        )

    async def test_chat_formatter_thinking_dropped(self) -> None:
        """ThinkingBlock is silently ignored in user/assistant xAI

        messages."""
        fmt = XAIChatFormatter()
        msgs = [
            AssistantMsg(
                name="assistant",
                content=[
                    ThinkingBlock(thinking="inner thoughts"),
                    TextBlock(text="reply"),
                ],
            ),
        ]
        res = await fmt.format(msgs)
        self.assertListEqual([assistant("reply")], res)

    async def test_chat_formatter_empty(self) -> None:
        """Empty input returns empty list."""
        fmt = XAIChatFormatter()
        res = await fmt.format([])
        self.assertListEqual([], res)

    # -------------------------------------------------------------------
    # XAIMultiAgentFormatter tests
    # -------------------------------------------------------------------

    async def test_multiagent_formatter_system_message(self) -> None:
        """System message is passed through as a system() stub."""
        fmt = XAIMultiAgentFormatter()
        res = await fmt.format(
            [*self.msgs_system, *self.msgs_conversation],
        )
        self.assertEqual(res[0], system("You're a helpful assistant."))

    async def test_multiagent_formatter_conversation_history(self) -> None:
        """Non-tool agent messages are collapsed into a user() history

        stub."""
        fmt = XAIMultiAgentFormatter()
        res = await fmt.format(self.msgs_conversation)
        self.assertListEqual(
            [
                user(
                    self._hist_prompt + "<history>\n"
                    "user: What is the capital of France?\n"
                    "assistant: The capital of France is Paris.\n"
                    "user: What is the capital of Germany?\n"
                    "assistant: The capital of Germany is Berlin.\n"
                    "user: What is the capital of Japan?\n"
                    "</history>",
                ),
            ],
            res,
        )

    async def test_multiagent_formatter_first_group_has_hist_prompt(

        self,

    ) -> None:
        """First agent message group includes the conversation history

        prompt."""
        fmt = XAIMultiAgentFormatter()
        res = await fmt.format(self.msgs_conversation)
        hist_text = res[0].args[0]
        self.assertListEqual(
            [hist_text.startswith(self._hist_prompt)],
            [True],
        )

    async def test_multiagent_formatter_full_history(self) -> None:
        """Full history produces system + conv_history + tool_call +

        tool_result + trailing assistant."""
        fmt = XAIMultiAgentFormatter()
        res = await fmt.format(
            [*self.msgs_system, *self.msgs_conversation, *self.msgs_tools],
        )
        roles = [m.role for m in res]
        self.assertListEqual(
            sorted(r for r in roles if isinstance(r, str)),
            sorted(["system", "user", "tool", "assistant"]),
        )

    async def test_multiagent_formatter_tools_only_is_first(self) -> None:
        """When only tools are given, trailing text is formatted as

        assistant."""
        fmt = XAIMultiAgentFormatter()
        res = await fmt.format(self.msgs_tools)
        trailing = [m for m in res if m.role == "assistant"]
        self.assertListEqual([len(trailing)], [1])

    async def test_multiagent_formatter_nonfirst_trailing_is_assistant(

        self,

    ) -> None:
        """Trailing text after a tool sequence is formatted as assistant."""
        fmt = XAIMultiAgentFormatter()
        res = await fmt.format(
            [*self.msgs_conversation, *self.msgs_tools],
        )
        trailing = [m for m in res if m.role == "assistant"]
        self.assertListEqual([len(trailing)], [1])

    async def test_multiagent_formatter_empty(self) -> None:
        """Empty input returns empty list."""
        fmt = XAIMultiAgentFormatter()
        res = await fmt.format([])
        self.assertListEqual([], res)

    async def test_chat_formatter_complex_multi_step(self) -> None:
        """Complex multi-step sequence with interleaved thinking, text,

        tool calls, and tool results."""
        fmt = XAIChatFormatter()
        msgs = [
            AssistantMsg(
                name="assistant",
                content=[
                    ThinkingBlock(thinking="thinking_1"),
                    TextBlock(text="text_1"),
                    ToolCallBlock(
                        id="call_1",
                        name="func_1",
                        input='{"arg": "value1"}',
                    ),
                    ToolCallBlock(
                        id="call_2",
                        name="func_2",
                        input='{"arg": "value2"}',
                    ),
                    ToolResultBlock(
                        id="call_1",
                        name="func_1",
                        output=[TextBlock(text="result_1")],
                        state=ToolResultState.SUCCESS,
                    ),
                    ToolResultBlock(
                        id="call_2",
                        name="func_2",
                        output=[TextBlock(text="result_2")],
                        state=ToolResultState.SUCCESS,
                    ),
                    ThinkingBlock(thinking="thinking_2"),
                    TextBlock(text="text_2"),
                    ToolCallBlock(
                        id="call_3",
                        name="func_3",
                        input='{"arg": "value3"}',
                    ),
                    ToolResultBlock(
                        id="call_3",
                        name="func_3",
                        output=[TextBlock(text="result_3")],
                        state=ToolResultState.SUCCESS,
                    ),
                    ToolCallBlock(
                        id="call_4",
                        name="func_4",
                        input='{"arg": "value4"}',
                    ),
                    ToolResultBlock(
                        id="call_4",
                        name="func_4",
                        output=[TextBlock(text="result_4")],
                        state=ToolResultState.SUCCESS,
                    ),
                    ThinkingBlock(thinking="thinking_3"),
                    TextBlock(text="text_3"),
                ],
            ),
        ]
        res = await fmt.format(msgs)

        # First message: proto with text_1 content + 2 tool_calls
        self.assertTrue(hasattr(res[0], "tool_calls"))
        self.assertEqual(len(res[0].tool_calls), 2)
        self.assertEqual(len(res[0].content), 1)
        self.assertEqual(res[0].tool_calls[0].id, "call_1")
        self.assertEqual(res[0].tool_calls[0].function.name, "func_1")
        self.assertEqual(
            res[0].tool_calls[0].function.arguments,
            '{"arg": "value1"}',
        )
        self.assertEqual(res[0].tool_calls[1].id, "call_2")
        self.assertEqual(res[0].tool_calls[1].function.name, "func_2")
        self.assertEqual(
            res[0].tool_calls[1].function.arguments,
            '{"arg": "value2"}',
        )

        # Tool results
        self.assertEqual(
            res[1],
            tool_result("result_1", tool_call_id="call_1"),
        )
        self.assertEqual(
            res[2],
            tool_result("result_2", tool_call_id="call_2"),
        )

        # Second proto: text_2 + 1 tool_call
        self.assertTrue(hasattr(res[3], "tool_calls"))
        self.assertEqual(len(res[3].tool_calls), 1)
        self.assertEqual(len(res[3].content), 1)
        self.assertEqual(res[3].tool_calls[0].id, "call_3")
        self.assertEqual(res[3].tool_calls[0].function.name, "func_3")

        # Tool result for call_3
        self.assertEqual(
            res[4],
            tool_result("result_3", tool_call_id="call_3"),
        )

        # Third proto: no content + 1 tool_call
        self.assertTrue(hasattr(res[5], "tool_calls"))
        self.assertEqual(len(res[5].tool_calls), 1)
        self.assertEqual(len(res[5].content), 0)
        self.assertEqual(res[5].tool_calls[0].id, "call_4")
        self.assertEqual(res[5].tool_calls[0].function.name, "func_4")

        # Tool result for call_4
        self.assertEqual(
            res[6],
            tool_result("result_4", tool_call_id="call_4"),
        )

        # Final assistant text
        self.assertEqual(res[7], assistant("text_3"))

        # Total 8 items
        self.assertEqual(len(res), 8)

    async def test_chat_formatter_hint_block(self) -> None:
        """HintBlock flushes preceding content and becomes a user message."""
        fmt = XAIChatFormatter()
        msgs = [
            AssistantMsg(
                name="assistant",
                content=[
                    TextBlock(text="Let me think about that."),
                    HintBlock(hint="Remember to be concise."),
                    TextBlock(text="Here is my answer."),
                ],
            ),
        ]
        res = await fmt.format(msgs)
        self.assertListEqual(
            [
                assistant("Let me think about that."),
                user("Remember to be concise."),
                assistant("Here is my answer."),
            ],
            res,
        )

    async def test_chat_formatter_hint_block_multimodal(self) -> None:
        """Multimodal HintBlock becomes one user() stub carrying text +

        image."""
        fmt = XAIChatFormatter()
        image_b64 = "ZmFrZSBpbWFnZSBkYXRh"
        msgs = [
            AssistantMsg(
                name="assistant",
                content=[
                    HintBlock(
                        hint=[
                            TextBlock(text="Inspect this screenshot:"),
                            DataBlock(
                                source=Base64Source(
                                    data=image_b64,
                                    media_type="image/png",
                                ),
                            ),
                        ],
                    ),
                ],
            ),
        ]
        res = await fmt.format(msgs)
        self.assertListEqual(
            [
                user(
                    "Inspect this screenshot:",
                    image(f"data:image/png;base64,{image_b64}"),
                ),
            ],
            res,
        )