Skip to content

Commit de01137

Browse files
authored
fix: Namespace id in chat_append() and friends (#37)
* fix: Namespace `id` in `chat_append()` and friends * Allows `chat_append()` to work in modules * We also now check for an active Shiny session and throw early * docs: Add news item
1 parent 7b1f60b commit de01137

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
# shinychat (development version)
22

33
* 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)
4+
45
* Both `chat_ui()` and `output_markdown_stream()` now support arbirary Shiny UI elements inside of messages. This allows for gathering input from the user (e.g., `selectInput()`), displaying of rich output (e.g., `{htmlwidgets}` like `{plotly}`), and more. (#1868)
6+
57
* Added a new `chat_clear()` function to clear the chat of all messages. (#25)
68

9+
* `chat_append()`, `chat_append_message()` and `chat_clear()` now all work in Shiny modules without needing to namespace the `id` of the Chat component. (#37)
10+
711
# shinychat 0.1.1
812

913
* Initial CRAN submission.

R/chat.R

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,9 @@ chat_ui <- function(
237237
#'
238238
#' @export
239239
chat_append <- function(id, response, role = c("assistant", "user"), session = getDefaultReactiveDomain()) {
240+
check_active_session(session)
240241
role <- match.arg(role)
242+
241243
stream <- as_generator(response)
242244
chat_append_stream(id, stream, role = role, session = session)
243245
}
@@ -312,6 +314,8 @@ chat_append <- function(id, response, role = c("assistant", "user"), session = g
312314
#'
313315
#' @export
314316
chat_append_message <- function(id, msg, chunk = TRUE, operation = c("append", "replace"), session = getDefaultReactiveDomain()) {
317+
check_active_session(session)
318+
315319
if (!is.list(msg)) {
316320
rlang::abort("msg must be a named list with 'role' and 'content' fields")
317321
}
@@ -366,7 +370,7 @@ chat_append_message <- function(id, msg, chunk = TRUE, operation = c("append", "
366370
)
367371

368372
session$sendCustomMessage("shinyChatMessage", list(
369-
id = id,
373+
id = resolve_id(id, session),
370374
handler = msg_type,
371375
obj = msg
372376
))
@@ -444,10 +448,12 @@ rlang::on_load(chat_append_stream_impl <- coro::async(function(id, stream, role
444448
#'
445449
#' shinyApp(ui, server)
446450
chat_clear <- function(id, session = getDefaultReactiveDomain()) {
451+
check_active_session(session)
452+
447453
session$sendCustomMessage(
448454
"shinyChatMessage",
449455
list(
450-
id = id,
456+
id = resolve_id(id, session),
451457
handler = "shiny-chat-clear-messages",
452458
obj = NULL
453459
)

R/utils-shiny.R

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
check_active_session <- function(session = shiny::getDefaultReactiveDomain()) {
2+
rlang::abort(
3+
"An active Shiny session is required.",
4+
call = rlang::caller_env()
5+
)
6+
}
7+
8+
resolve_id <- function(id, session = shiny::getDefaultReactiveDomain()) {
9+
if (is.null(session)) return(id)
10+
session$ns(id)
11+
}

0 commit comments

Comments
 (0)