diff --git a/pkg-r/NEWS.md b/pkg-r/NEWS.md index 2f07280..596231a 100644 --- a/pkg-r/NEWS.md +++ b/pkg-r/NEWS.md @@ -10,7 +10,7 @@ * Added `chat_append(icon=...)` and `chat_ui(icon_assistant=...)` for customizing the icon that appears next to assistant responses. (#88) -* `chat_mod_server()` now returns a list of reactives for `last_input` and `last_turn`, as well and functions to `update_user_input()` and `clear()` the chat. (#130) +* `chat_mod_server()` now returns a list of reactives for `last_input` and `last_turn`, as well functions to `update_user_input()`, `append()` and `clear()` the chat. (#130, #143, #145) ## Improvements diff --git a/pkg-r/R/chat_app.R b/pkg-r/R/chat_app.R index 552df10..e23d6b9 100644 --- a/pkg-r/R/chat_app.R +++ b/pkg-r/R/chat_app.R @@ -74,13 +74,16 @@ #' * `chat_app()` returns a [shiny::shinyApp()] object. #' * `chat_mod_ui()` returns the UI for a shinychat module. #' * `chat_mod_server()` includes the shinychat module server logic, and -#' and a list with: +#' returns a list containing: #' #' * `last_input`: A reactive value containing the last user input. #' * `last_turn`: A reactive value containing the last assistant turn. #' * `update_user_input()`: A function to update the chat input or submit a #' new user input. Takes the same arguments as [update_chat_user_input()], #' except for `id` and `session`, which are supplied automatically. +#' * `append()`: A function to append a new message to the chat UI. Takes +#' the same arguments as [chat_append()], except for `id` and `session`, +#' which are supplied automatically. #' * `clear()`: A function to clear the chat history and the chat UI. #' `clear()` takes an optional list of `messages` used to initialize the #' chat after clearing. `messages` should be a list of messages, where @@ -224,6 +227,10 @@ chat_mod_server <- function( ) } + chat_append_mod <- function(response, role = "assistant", icon = NULL) { + chat_append("chat", response, role = role, icon = icon, session = session) + } + client_clear <- function( messages = NULL, client_history = c("clear", "set", "append", "keep") @@ -271,6 +278,7 @@ chat_mod_server <- function( last_turn = shiny::reactive(last_turn()), last_input = shiny::reactive(last_input()), client = client, + append = chat_append_mod, update_user_input = chat_update_user_input, clear = client_clear ) diff --git a/pkg-r/man/chat_app.Rd b/pkg-r/man/chat_app.Rd index 923eb8a..468b522 100644 --- a/pkg-r/man/chat_app.Rd +++ b/pkg-r/man/chat_app.Rd @@ -48,13 +48,16 @@ in \code{\link[=chat_ui]{chat_ui()}}.} \item \code{chat_app()} returns a \code{\link[shiny:shinyApp]{shiny::shinyApp()}} object. \item \code{chat_mod_ui()} returns the UI for a shinychat module. \item \code{chat_mod_server()} includes the shinychat module server logic, and -and a list with: +returns a list containing: \itemize{ \item \code{last_input}: A reactive value containing the last user input. \item \code{last_turn}: A reactive value containing the last assistant turn. \item \code{update_user_input()}: A function to update the chat input or submit a new user input. Takes the same arguments as \code{\link[=update_chat_user_input]{update_chat_user_input()}}, except for \code{id} and \code{session}, which are supplied automatically. +\item \code{append()}: A function to append a new message to the chat UI. Takes +the same arguments as \code{\link[=chat_append]{chat_append()}}, except for \code{id} and \code{session}, +which are supplied automatically. \item \code{clear()}: A function to clear the chat history and the chat UI. \code{clear()} takes an optional list of \code{messages} used to initialize the chat after clearing. \code{messages} should be a list of messages, where