4
4
# This source code is licensed under the terms described in the LICENSE file in
5
5
# the root directory of this source tree.
6
6
7
- from typing import Optional
7
+ from typing import Any , Iterator , Optional , Tuple
8
8
9
9
from termcolor import cprint
10
10
11
11
from llama_stack_client .types import InterleavedContent , ToolResponseMessage
12
12
13
13
14
14
def interleaved_content_as_str (content : InterleavedContent , sep : str = " " ) -> str :
15
- def _process (c ) -> str :
15
+ def _process (c : Any ) -> str :
16
16
if isinstance (c , str ):
17
17
return c
18
18
elif hasattr (c , "type" ):
@@ -36,36 +36,38 @@ def __init__(
36
36
self ,
37
37
role : Optional [str ] = None ,
38
38
content : str = "" ,
39
- end : str = "\n " ,
40
- color = "white" ,
41
- ):
39
+ end : Optional [ str ] = "\n " ,
40
+ color : str = "white" ,
41
+ ) -> None :
42
42
self .role = role
43
43
self .content = content
44
44
self .color = color
45
45
self .end = "\n " if end is None else end
46
46
47
- def __str__ (self ):
47
+ def __str__ (self ) -> str :
48
48
if self .role is not None :
49
49
return f"{ self .role } > { self .content } "
50
50
else :
51
51
return f"{ self .content } "
52
52
53
- def print (self , flush = True ):
53
+ def print (self , flush : bool = True ) -> None :
54
54
cprint (f"{ str (self )} " , color = self .color , end = self .end , flush = flush )
55
55
56
56
57
57
class TurnStreamEventPrinter :
58
- def __init__ (self ):
59
- self .previous_event_type = None
60
- self .previous_step_type = None
58
+ def __init__ (self ) -> None :
59
+ self .previous_event_type : Optional [ str ] = None
60
+ self .previous_step_type : Optional [ str ] = None
61
61
62
- def yield_printable_events (self , chunk ) :
62
+ def yield_printable_events (self , chunk : Any ) -> Iterator [ TurnStreamPrintableEvent ] :
63
63
for printable_event in self ._yield_printable_events (chunk , self .previous_event_type , self .previous_step_type ):
64
64
yield printable_event
65
65
66
66
self .previous_event_type , self .previous_step_type = self ._get_event_type_step_type (chunk )
67
67
68
- def _yield_printable_events (self , chunk , previous_event_type = None , previous_step_type = None ):
68
+ def _yield_printable_events (
69
+ self , chunk : Any , previous_event_type : Optional [str ] = None , previous_step_type : Optional [str ] = None
70
+ ) -> Iterator [TurnStreamPrintableEvent ]:
69
71
if hasattr (chunk , "error" ):
70
72
yield TurnStreamPrintableEvent (role = None , content = chunk .error ["message" ], color = "red" )
71
73
return
@@ -151,7 +153,7 @@ def _yield_printable_events(self, chunk, previous_event_type=None, previous_step
151
153
color = "green" ,
152
154
)
153
155
154
- def _get_event_type_step_type (self , chunk ) :
156
+ def _get_event_type_step_type (self , chunk : Any ) -> Tuple [ Optional [ str ], Optional [ str ]] :
155
157
if hasattr (chunk , "event" ):
156
158
previous_event_type = chunk .event .payload .event_type if hasattr (chunk , "event" ) else None
157
159
previous_step_type = (
@@ -162,7 +164,7 @@ def _get_event_type_step_type(self, chunk):
162
164
163
165
164
166
class EventLogger :
165
- def log (self , event_generator ) :
167
+ def log (self , event_generator : Iterator [ Any ]) -> Iterator [ TurnStreamPrintableEvent ] :
166
168
printer = TurnStreamEventPrinter ()
167
169
for chunk in event_generator :
168
170
yield from printer .yield_printable_events (chunk )
0 commit comments