Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pkg-r/NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export(chat_mod_ui)
export(chat_ui)
export(markdown_stream)
export(output_markdown_stream)
export(update_chat_user_input)
if (getRversion() < "4.3.0") importFrom("S7", "@")
import(S7)
import(rlang)
Expand Down
5 changes: 4 additions & 1 deletion pkg-r/NEWS.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# shinychat (development version)

## New features and improvements
## New features

* Added `chat_enable_bookmarking()` which adds Shiny bookmarking hooks to save and restore the `{ellmer}` chat client. (#28)
* Added `update_chat_user_input()` for programmatically updating the user input of a chat UI element. (#78)

## Improvements

* `chat_app()` now correctly restores the chat client state when refreshing the app, e.g. by reloading the page. (#71)

Expand Down
80 changes: 80 additions & 0 deletions pkg-r/R/chat.R
Original file line number Diff line number Diff line change
Expand Up @@ -541,3 +541,83 @@ chat_clear <- function(id, session = getDefaultReactiveDomain()) {
)
)
}


#' Update the user input of a chat control
#'
#' @param id The ID of the chat element
#' @param ... Currently unused, but reserved for future use.
#' @param value The value to set the user input to. If `NULL`, the input will not be updated.
#' @param placeholder The placeholder text for the user input
#' @param submit Whether to automatically submit the text for the user. Requires `value`.
#' @param focus Whether to move focus to the input element. Requires `value`.
#' @param session The Shiny session object
#'
#' @export
#' @examplesIf interactive()
#' library(shiny)
#' library(bslib)
#' library(shinychat)
#'
#' ui <- page_fillable(
#' chat_ui("chat"),
#' layout_columns(
#' fill = FALSE,
#' actionButton("update_placeholder", "Update placeholder"),
#' actionButton("update_value", "Update user input")
#' )
#' )
#'
#' server <- function(input, output, session) {
#' observeEvent(input$update_placeholder, {
#' update_chat_user_input("chat", placeholder = "New placeholder text")
#' })
#'
#' observeEvent(input$update_value, {
#' update_chat_user_input("chat", value = "New user input", focus = TRUE)
#' })
#'
#' observeEvent(input$chat_user_input, {
#' response <- paste0("You said: ", input$chat_user_input)
#' chat_append("chat", response)
#' })
#' }
#'
#' shinyApp(ui, server)

update_chat_user_input <- function(
id,
...,
value = NULL,
placeholder = NULL,
submit = FALSE,
focus = FALSE,
session = getDefaultReactiveDomain()
) {
rlang::check_dots_empty()
check_active_session(session)

if (is.null(value) && (submit || focus)) {
rlang::abort(
"An input `value` must be provided when `submit` or `focus` are `TRUE`."
)
}

vals <- drop_nulls(
list(
value = value,
placeholder = placeholder,
submit = submit,
focus = focus
)
)

session$sendCustomMessage(
"shinyChatMessage",
list(
id = resolve_id(id, session),
handler = "shiny-chat-update-user-input",
obj = vals
)
)
}
5 changes: 5 additions & 0 deletions pkg-r/R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,8 @@ strip_ansi <- function(text) {
ansi_pattern <- "(\x1B|\x033)\\[[0-9;?=<>]*[@-~]"
gsub(ansi_pattern, "", text)
}


drop_nulls <- function(x) {
x[!vapply(x, is.null, FUN.VALUE = logical(1))]
}
67 changes: 67 additions & 0 deletions pkg-r/man/update_chat_user_input.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading