1010ROLE_REPLACE_NAME = {"assistant" : "agent" }
1111
1212
13- def pack_part_line (role : str , part : Part , truncate_chars : int = None ) -> str :
13+ def pack_part_line (
14+ role : str ,
15+ part : Part ,
16+ tool_mapping : dict [str , ToolCallMeta ],
17+ truncate_chars : int = None ,
18+ ) -> str :
1419 role = ROLE_REPLACE_NAME .get (role , role )
1520 header = f"<{ role } >({ part .type } )"
1621 if part .type not in STRING_TYPES :
@@ -25,15 +30,25 @@ def pack_part_line(role: str, part: Part, truncate_chars: int = None) -> str:
2530 "arguments" : tool_call_meta .arguments ,
2631 }
2732 )
33+ if tool_call_meta .id is not None :
34+ tool_mapping [tool_call_meta .id ] = tool_call_meta
2835 r = f"{ header } { tool_data } "
2936 elif part .type == "tool-result" :
3037 tool_result_meta = ToolResultMeta (** part .meta )
31- tool_data = json .dumps (
32- {
33- "tool_name" : tool_result_meta .name ,
34- "result" : tool_result_meta .result ,
35- }
36- )
38+ if tool_result_meta .tool_call_id not in tool_mapping :
39+ tool_data = json .dumps (
40+ {
41+ "result" : part .text ,
42+ }
43+ )
44+ else :
45+ tool_data = tool_mapping [tool_result_meta .tool_call_id ]
46+ tool_data = json .dumps (
47+ {
48+ "tool_name" : tool_data .name ,
49+ "result" : part .text ,
50+ }
51+ )
3752 r = f"{ header } { tool_data } "
3853 else :
3954 LOG .warning (f"Unknown message part type: { part .type } " )
@@ -49,9 +64,16 @@ class MessageBlob(BaseModel):
4964 parts : List [Part ]
5065 task_id : Optional [asUUID ] = None
5166
52- def to_string (self , truncate_chars : int = None , ** kwargs ) -> str :
67+ def to_string (
68+ self ,
69+ tool_mapping : dict [str , ToolCallMeta ],
70+ truncate_chars : int = None ,
71+ ** kwargs ,
72+ ) -> str :
5373 lines = [
54- pack_part_line (self .role , p , truncate_chars = truncate_chars , ** kwargs )
74+ pack_part_line (
75+ self .role , p , tool_mapping , truncate_chars = truncate_chars , ** kwargs
76+ )
5577 for p in self .parts
5678 ]
5779 return "\n " .join (lines )
0 commit comments