From 25070bb38e00838ae559712e2ab977c72efe6f38 Mon Sep 17 00:00:00 2001 From: Joe Cheng Date: Fri, 27 Jun 2025 17:27:18 -0700 Subject: [PATCH 1/4] Make chat_app refresh its prepopulated UI messages on reload --- pkg-r/R/chat_app.R | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/pkg-r/R/chat_app.R b/pkg-r/R/chat_app.R index cc59ee18..2ab3d37e 100644 --- a/pkg-r/R/chat_app.R +++ b/pkg-r/R/chat_app.R @@ -69,15 +69,17 @@ chat_app <- function(client, ...) { check_ellmer_chat(client) - ui <- bslib::page_fillable( - chat_mod_ui("chat", client = client, height = "100%"), - shiny::actionButton( - "close_btn", - label = "", - class = "btn-close", - style = "position: fixed; top: 6px; right: 6px;" + ui <- function(req) { + bslib::page_fillable( + chat_mod_ui("chat", client = client, height = "100%"), + shiny::actionButton( + "close_btn", + label = "", + class = "btn-close", + style = "position: fixed; top: 6px; right: 6px;" + ) ) - ) + } server <- function(input, output, session) { chat_mod_server("chat", client) From 8ee006dfa0fe409e436e34c3a1f78a9d417a3241 Mon Sep 17 00:00:00 2001 From: Garrick Aden-Buie Date: Mon, 30 Jun 2025 09:28:46 -0400 Subject: [PATCH 2/4] docs: Note that `chat_app()` is single-user --- pkg-r/R/chat_app.R | 9 ++++++++- pkg-r/man/chat_app.Rd | 9 ++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/pkg-r/R/chat_app.R b/pkg-r/R/chat_app.R index 2ab3d37e..ab02d882 100644 --- a/pkg-r/R/chat_app.R +++ b/pkg-r/R/chat_app.R @@ -5,6 +5,11 @@ #' Note that these functions will mutate the input `client` object as #' you chat because your turns will be appended to the history. #' +#' The app created by `chat_app()` is suitable for interactive use by a single +#' user. For multi-user Shiny apps, use the Shiny module chat functions -- +#' `chat_mod_ui()` and `chat_mod_server()` -- and be sure to create a new chat +#' client for each user session. +#' #' @examples #' \dontrun{ #' # Interactive in the console ---- @@ -64,7 +69,9 @@ #' * `chat_mod_server()` includes the shinychat module server logic, and #' and returns the last turn upon successful chat completion. #' -#' @describeIn chat_app A simple Shiny app for live chatting. +#' @describeIn chat_app A simple Shiny app for live chatting. Note that this +#' app is suitable for interactive use by a single user; do not use +#' `chat_app()` in a multi-user Shiny app context. #' @export chat_app <- function(client, ...) { check_ellmer_chat(client) diff --git a/pkg-r/man/chat_app.Rd b/pkg-r/man/chat_app.Rd index 7a663038..5596a603 100644 --- a/pkg-r/man/chat_app.Rd +++ b/pkg-r/man/chat_app.Rd @@ -38,10 +38,17 @@ and returns the last turn upon successful chat completion. Create a simple Shiny app for live chatting using an \link[ellmer:Chat]{ellmer::Chat} object. Note that these functions will mutate the input \code{client} object as you chat because your turns will be appended to the history. + +The app created by \code{chat_app()} is suitable for interactive use by a single +user. For multi-user Shiny apps, use the Shiny module chat functions -- +\code{chat_mod_ui()} and \code{chat_mod_server()} -- and be sure to create a new chat +client for each user session. } \section{Functions}{ \itemize{ -\item \code{chat_app()}: A simple Shiny app for live chatting. +\item \code{chat_app()}: A simple Shiny app for live chatting. Note that this +app is suitable for interactive use by a single user; do not use +\code{chat_app()} in a multi-user Shiny app context. \item \code{chat_mod_ui()}: A simple chat app module UI. From 9da2f5373d6ba5ba4605c1a6a71ef6fbe6328c34 Mon Sep 17 00:00:00 2001 From: Garrick Aden-Buie Date: Mon, 30 Jun 2025 09:30:33 -0400 Subject: [PATCH 3/4] chore: `chat_anthropic()` not `chat_claude()` --- pkg-r/R/chat_app.R | 4 ++-- pkg-r/man/chat_app.Rd | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/pkg-r/R/chat_app.R b/pkg-r/R/chat_app.R index ab02d882..08cc9957 100644 --- a/pkg-r/R/chat_app.R +++ b/pkg-r/R/chat_app.R @@ -13,7 +13,7 @@ #' @examples #' \dontrun{ #' # Interactive in the console ---- -#' client <- ellmer::chat_claude() +#' client <- ellmer::chat_anthropic() #' chat_app(client) #' #' # Inside a Shiny app ---- @@ -47,7 +47,7 @@ #' ) #' #' server <- function(input, output, session) { -#' claude <- ellmer::chat_claude(model = "claude-3-5-sonnet-latest") # Requires ANTHROPIC_API_KEY +#' claude <- ellmer::chat_anthropic(model = "claude-3-5-sonnet-latest") # Requires ANTHROPIC_API_KEY #' openai <- ellmer::chat_openai(model = "gpt-4o") # Requires OPENAI_API_KEY #' #' chat_mod_server("claude", claude) diff --git a/pkg-r/man/chat_app.Rd b/pkg-r/man/chat_app.Rd index 5596a603..844e77eb 100644 --- a/pkg-r/man/chat_app.Rd +++ b/pkg-r/man/chat_app.Rd @@ -58,7 +58,7 @@ app is suitable for interactive use by a single user; do not use \examples{ \dontrun{ # Interactive in the console ---- -client <- ellmer::chat_claude() +client <- ellmer::chat_anthropic() chat_app(client) # Inside a Shiny app ---- @@ -92,7 +92,7 @@ ui <- page_fillable( ) server <- function(input, output, session) { - claude <- ellmer::chat_claude(model = "claude-3-5-sonnet-latest") # Requires ANTHROPIC_API_KEY + claude <- ellmer::chat_anthropic(model = "claude-3-5-sonnet-latest") # Requires ANTHROPIC_API_KEY openai <- ellmer::chat_openai(model = "gpt-4o") # Requires OPENAI_API_KEY chat_mod_server("claude", claude) From 60737ddcd58fd29f095108a75dfe75ed4062a1fb Mon Sep 17 00:00:00 2001 From: Garrick Aden-Buie Date: Mon, 30 Jun 2025 09:35:36 -0400 Subject: [PATCH 4/4] chore: Add NEWS entry --- pkg-r/NEWS.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pkg-r/NEWS.md b/pkg-r/NEWS.md index b37fa724..941e15b1 100644 --- a/pkg-r/NEWS.md +++ b/pkg-r/NEWS.md @@ -4,6 +4,8 @@ * Added `chat_enable_bookmarking()` which adds Shiny bookmarking hooks to save and restore the `{ellmer}` chat client. (#28) +* `chat_app()` now correctly restores the chat client state when refreshing the app, e.g. by reloading the page. (#71) + # shinychat 0.2.0 ## New features and improvements