@@ -656,3 +656,53 @@ class ModelToolCall:
656656 def call_func (self ) -> Any :
657657 """A helper function for calling the function/tool represented by this object."""
658658 return self .func (** self .args )
659+
660+
661+ class SimpleComponent (Component ):
662+ """A Component that is make up of named spans."""
663+
664+ def __init__ (self , ** kwargs ):
665+ """Initialized a simple component of the constructor's kwargs."""
666+ for key in kwargs .keys ():
667+ if type (kwargs [key ]) is str :
668+ kwargs [key ] = CBlock (value = kwargs [key ])
669+ self ._kwargs_type_check (kwargs )
670+ self ._kwargs = kwargs
671+
672+ def parts (self ):
673+ """Returns the values of the kwargs."""
674+ return list (self ._kwargs .values ())
675+
676+ def _kwargs_type_check (self , kwargs ):
677+ for key in kwargs .keys ():
678+ value = kwargs [key ]
679+ assert issubclass (type (value ), Component ) or issubclass (
680+ type (value ), CBlock
681+ ), f"Expected span but found { type (value )} of value: { value } "
682+ assert type (key ) is str
683+ return True
684+
685+ @staticmethod
686+ def make_simple_string (kwargs ):
687+ """Uses <|key|>value</|key|> to represent a simple component."""
688+ return "\n " .join (
689+ [f"<|{ key } |>{ value } </|{ key } |>" for (key , value ) in kwargs .items ()]
690+ )
691+
692+ @staticmethod
693+ def make_json_string (kwargs ):
694+ """Uses json."""
695+ str_args = dict ()
696+ for key in kwargs .keys ():
697+ match kwargs [key ]:
698+ case ModelOutputThunk () | CBlock ():
699+ str_args [key ] = kwargs [key ].value
700+ case Component ():
701+ str_args [key ] = kwargs [key ].format_for_llm ()
702+ import json
703+
704+ return json .dumps (str_args )
705+
706+ def format_for_llm (self ):
707+ """Uses a string rep."""
708+ return SimpleComponent .make_json_string (self ._kwargs )
0 commit comments