|
74 | 74 | #' * `chat_app()` returns a [shiny::shinyApp()] object. |
75 | 75 | #' * `chat_mod_ui()` returns the UI for a shinychat module. |
76 | 76 | #' * `chat_mod_server()` includes the shinychat module server logic, and |
77 | | -#' and returns the last turn upon successful chat completion. |
| 77 | +#' and a list with: |
| 78 | +#' |
| 79 | +#' * `last_input`: A reactive value containing the last user input. |
| 80 | +#' * `last_turn`: A reactive value containing the last assistant turn. |
| 81 | +#' * `update_user_input()`: A function to update the chat input or submit a |
| 82 | +#' new user input. Takes the same arguments as [update_chat_user_input()], |
| 83 | +#' except for `id` and `session`, which are supplied automatically. |
| 84 | +#' * `clear()`: A function to clear the chat history and the chat UI. |
| 85 | +#' Takes a single argument, `clear_history`, which indicates whether to |
| 86 | +#' also clear the chat history in the `client` object. Defaults to `TRUE`. |
| 87 | +#' * `client`: The chat client object, which is mutated as you chat. |
78 | 88 | #' |
79 | 89 | #' @describeIn chat_app A simple Shiny app for live chatting. Note that this |
80 | 90 | #' app is suitable for interactive use by a single user; do not use |
@@ -173,18 +183,58 @@ chat_mod_server <- function( |
173 | 183 | bookmark_on_response = bookmark_on_response |
174 | 184 | ) |
175 | 185 |
|
| 186 | + last_turn <- shiny::reactiveVal(NULL) |
| 187 | + last_input <- shiny::reactiveVal(NULL) |
| 188 | + |
176 | 189 | shiny::observeEvent(input$chat_user_input, { |
| 190 | + last_input(input$chat_user_input) |
177 | 191 | append_stream_task$invoke( |
178 | 192 | client, |
179 | 193 | "chat", |
180 | 194 | input$chat_user_input |
181 | 195 | ) |
182 | 196 | }) |
183 | 197 |
|
184 | | - shiny::reactive({ |
| 198 | + shiny::observe({ |
185 | 199 | if (append_stream_task$status() == "success") { |
186 | | - client$last_turn() |
| 200 | + last_turn(client$last_turn()) |
187 | 201 | } |
188 | 202 | }) |
| 203 | + |
| 204 | + list( |
| 205 | + last_turn = shiny::reactive(last_turn()), |
| 206 | + last_input = shiny::reactive(last_input()), |
| 207 | + client = client, |
| 208 | + update_user_input = function( |
| 209 | + value = NULL, |
| 210 | + ..., |
| 211 | + placeholder = NULL, |
| 212 | + submit = FALSE, |
| 213 | + focus = FALSE |
| 214 | + ) { |
| 215 | + update_chat_user_input( |
| 216 | + "chat", |
| 217 | + value = value, |
| 218 | + placeholder = placeholder, |
| 219 | + submit = submit, |
| 220 | + focus = focus, |
| 221 | + ..., |
| 222 | + session = session |
| 223 | + ) |
| 224 | + }, |
| 225 | + clear = function(clear_history = TRUE) { |
| 226 | + if (!rlang::is_bool(clear_history)) { |
| 227 | + cli::cli_abort( |
| 228 | + "{.var clear_history} must be {.or {.val {c(TRUE, FALSE)}}}." |
| 229 | + ) |
| 230 | + } |
| 231 | + chat_clear("chat", session = session) |
| 232 | + if (isTRUE(clear_history)) { |
| 233 | + client$set_turns(list()) |
| 234 | + } |
| 235 | + last_turn(NULL) |
| 236 | + last_input(NULL) |
| 237 | + } |
| 238 | + ) |
189 | 239 | }) |
190 | 240 | } |
0 commit comments