In order to support custom rendering of tool results in shinychat's UI, I'd like to follow ellmer's pattern of using content_*() functions to render ContentToolResult in different contexts.
In order to provide a custom ContentToolResult renderer, tool functions need to be able to return objects that are stored in the chat turns as a custom class that inherits from ContentToolResult.
The solution I propose is that ellmer allow tool functions to return ContentToolResult objects directly. When provided in this way, ellmer would return the tool result, after updating the @id property. (id would need to be optional for users but would be filled in by the tool invocation function.)
In addition to making id an optional property when constructing the tool result, I'd also like to add a detail property that could be used by tool authors to store additional data or context that is stored alongside the tool result and that could be used by custom content rendering functions.
WeatherToolResult <- new_class(
"WeatherToolResult",
parent = ellmer::ContentToolResult
)
method(contents_html, WeatherToolResult) <- function(content, ...) {
render_weather_card(content@detail$data)
}
tool(
function(lat, lng) {
data <- call_weather_api(lat, lng)
current_conditions <- data$current_conditions
WeatherToolResult(
value = current_conditions,
detail = list(data = data)
)
},
.name = "get_current_weather_conditions",
.description = "Get the current weather conditions for a location.",
lat = type_number("Latitude"),
lng = type_number("Longitude")
)
In order to support custom rendering of tool results in shinychat's UI, I'd like to follow ellmer's pattern of using
content_*()functions to renderContentToolResultin different contexts.In order to provide a custom
ContentToolResultrenderer, tool functions need to be able to return objects that are stored in the chat turns as a custom class that inherits fromContentToolResult.The solution I propose is that ellmer allow tool functions to return
ContentToolResultobjects directly. When provided in this way, ellmer would return the tool result, after updating the@idproperty. (idwould need to be optional for users but would be filled in by the tool invocation function.)In addition to making
idan optional property when constructing the tool result, I'd also like to add adetailproperty that could be used by tool authors to store additional data or context that is stored alongside the tool result and that could be used by custom content rendering functions.