File size: 3,175 Bytes
11ba2d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from aworld.output.utils import consume_content

from aworld.output.base import MessageOutput, ToolResultOutput, StepOutput, Output


class AworldUI:
    """"""

    async def title(self, output):
        """
        Title
        """
        pass

    async def message_output(self, __output__: MessageOutput):
        """
        message_output
        """
        pass

    async def tool_result(self, output: ToolResultOutput):
        """
            loading
        """
        pass


    async def step(self, output: StepOutput):
        """
            loading
        """
        pass

    async def custom_output(self, output: Output) -> str:
        """
            custom
        """
        pass

    @classmethod
    async def parse_output(cls, output, ui: "AworldUI"):
        """
            parse_output
        """
        if isinstance(output, MessageOutput):
            return await ui.message_output(output)
        elif isinstance(output, ToolResultOutput):
            return await ui.tool_result(output)
        elif isinstance(output, StepOutput):
            return await ui.step(output)
        else:
            return await ui.custom_output(output)

class PrinterAworldUI(AworldUI):
    """"""

    async def title(self, output) -> str:
        """
        Title
        """
        pass

    async def message_output(self, __output__: MessageOutput) -> str:
        """
        message_output
        """
        result=[]

        async def __log_item(item):
            result.append(item)
            print(item, end="", flush=True)

        if __output__.reason_generator or __output__.response_generator:
            if __output__.reason_generator:
                await consume_content(__output__.reason_generator, __log_item)
            if __output__.response_generator:
                await consume_content(__output__.response_generator, __log_item)
        else:
            await consume_content(__output__.reasoning, __log_item)
            await consume_content(__output__.response, __log_item)
        # if __output__.tool_calls:
        #     await consume_content(__output__.tool_calls, __log_item)

        print("")
        return "".join(result)

    async def tool_result(self, output: ToolResultOutput) -> str:
        """
            loading
        """
        return f"call tool {output.origin_tool_call.id}#{output.origin_tool_call.function.name} \n" \
               f"with params {output.origin_tool_call.function.arguments} \n" \
               f"with result {output.data}\n"


    async def step(self, output: StepOutput) -> str:
        """
            loading
        """
        if output.status == "START":
            return f"=============✈️START {output.name}======================"
        elif output.status == "FINISHED":
            return f"=============🛬FINISHED {output.name}======================"
        elif output.status == "FAILED":
            return f"=============🛬💥FAILED {output.name}======================"
        return f"=============?UNKNOWN#{output.status} {output.name}======================"

    async def custom_output(self, output: Output) -> str:
        pass