Skip to content

Commit 0ffc72e

Browse files
authored
Add chat_clear() (#25)
* Add chat_clear() function * Add NEWS
1 parent 8d41729 commit 0ffc72e

File tree

4 files changed

+83
-0
lines changed

4 files changed

+83
-0
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
export(chat_append)
44
export(chat_append_message)
5+
export(chat_clear)
56
export(chat_ui)
67
export(markdown_stream)
78
export(output_markdown_stream)

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# shinychat (development version)
22

3+
* Added a new `chat_clear()` function to clear the chat of all messages. (#25)
34
* Added new `output_markdown_stream()` and `markdown_stream()` functions to allow for streaming markdown content to the client. This is useful for showing Generative AI responses in real-time in a Shiny app, outside of a chat interface. (#23)
45

56
# shinychat 0.1.1

R/chat.R

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,3 +387,43 @@ rlang::on_load(chat_append_stream_impl <- coro::async(function(id, stream, role
387387
}
388388
chat_append_message(id, list(role = role, content = ""), chunk = "end", operation = "append", session = session)
389389
}))
390+
391+
392+
#' Clear all messages from a chat control
393+
#'
394+
#' @param id The ID of the chat element
395+
#' @param session The Shiny session object
396+
#'
397+
#' @export
398+
#' @examplesIf interactive()
399+
#'
400+
#' library(shiny)
401+
#' library(bslib)
402+
#'
403+
#' ui <- page_fillable(
404+
#' chat_ui("chat", fill = TRUE),
405+
#' actionButton("clear", "Clear chat")
406+
#' )
407+
#'
408+
#' server <- function(input, output, session) {
409+
#' observeEvent(input$clear, {
410+
#' chat_clear("chat")
411+
#' })
412+
#'
413+
#' observeEvent(input$chat_user_input, {
414+
#' response <- paste0("You said: ", input$chat_user_input)
415+
#' chat_append("chat", response)
416+
#' })
417+
#' }
418+
#'
419+
#' shinyApp(ui, server)
420+
chat_clear <- function(id, session = getDefaultReactiveDomain()) {
421+
session$sendCustomMessage(
422+
"shinyChatMessage",
423+
list(
424+
id = id,
425+
handler = "shiny-chat-clear-messages",
426+
obj = NULL
427+
)
428+
)
429+
}

man/chat_clear.Rd

Lines changed: 41 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)