@@ -60,24 +60,47 @@ collapse_tool_result <- function(tool_result) {
6060 return (as.character(tool_result @ error ))
6161 }
6262
63- contents <- purrr :: map_chr(tool_result @ value , tool_result_value_to_string )
64- paste0(contents , collapse = " \n " )
63+ content_list <- purrr :: map(tool_result @ value , tool_result_value_to_content )
64+
65+ all_text <- all(purrr :: map_lgl(content_list , function (x ) x $ type == " text" ))
66+
67+ if (all_text ) {
68+ return (paste0(purrr :: map_chr(content_list , function (x ) x $ text ), collapse = " \n " ))
69+ }
70+
71+ content_list
6572}
6673
67- tool_result_value_to_string <- function (x ) {
74+ tool_result_value_to_content <- function (x ) {
6875 if (is.atomic(x )) {
69- return (paste0(x , collapse = " \n " ))
76+ return (list ( type = " text " , text = paste0(x , collapse = " \n " ) ))
7077 }
7178
7279 switch (
7380 x $ type ,
74- text = x $ text ,
75- image = paste0(" [image:" , x $ source $ media_type , " ] " ,
76- " data:" , x $ source $ media_type , " ;base64," , x $ source $ data ),
77- audio = paste0(" [audio:" , x $ source $ media_type , " ] " ,
78- " data:" , x $ source $ media_type , " ;base64," , x $ source $ data ),
79- video = paste0(" [video:" , x $ source $ media_type , " ] " ,
80- " data:" , x $ source $ media_type , " ;base64," , x $ source $ data ),
81- input_string(tibble :: as_tibble(x $ source ))
81+ text = list (type = " text" , text = x $ text ),
82+ image = list (
83+ type = " image" ,
84+ image = paste0(" data:" , x $ source $ media_type , " ;base64," , x $ source $ data )
85+ ),
86+ audio = list (
87+ type = " audio" ,
88+ audio = paste0(" data:" , x $ source $ media_type , " ;base64," , x $ source $ data ),
89+ format = extract_format_from_media_type(x $ source $ media_type , " wav" )
90+ ),
91+ video = list (
92+ type = " video" ,
93+ video = paste0(" data:" , x $ source $ media_type , " ;base64," , x $ source $ data ),
94+ format = extract_format_from_media_type(x $ source $ media_type , " mp4" )
95+ ),
96+ list (type = " text" , text = input_string(tibble :: as_tibble(x $ source )))
8297 )
8398}
99+
100+ extract_format_from_media_type <- function (media_type , default ) {
101+ parts <- strsplit(media_type , " /" )[[1 ]]
102+ if (length(parts ) == 2 ) {
103+ return (parts [2 ])
104+ }
105+ default
106+ }
0 commit comments