Skip to content

Commit c504408

Browse files
authored
feat(pkg-r): Update module return value (#130)
* feat(pkg-r): Update module return value * docs: add NEWS item
1 parent 5c9c582 commit c504408

File tree

4 files changed

+68
-5
lines changed

4 files changed

+68
-5
lines changed

pkg-r/DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Imports:
3030
jsonlite,
3131
lifecycle,
3232
promises (>= 1.3.2),
33-
rlang,
33+
rlang (>= 1.1.0),
3434
S7,
3535
shiny (>= 1.10.0)
3636
Suggests:

pkg-r/NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
* Added `chat_append(icon=...)` and `chat_ui(icon_assistant=...)` for customizing the icon that appears next to assistant responses. (#88)
1212

13+
* `chat_mod_server()` now returns a list of reactives for `last_input` and `last_turn`, as well and functions to `update_user_input()` and `clear()` the chat. (#130)
14+
1315
## Improvements
1416

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

pkg-r/R/chat_app.R

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,17 @@
7474
#' * `chat_app()` returns a [shiny::shinyApp()] object.
7575
#' * `chat_mod_ui()` returns the UI for a shinychat module.
7676
#' * `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.
7888
#'
7989
#' @describeIn chat_app A simple Shiny app for live chatting. Note that this
8090
#' app is suitable for interactive use by a single user; do not use
@@ -173,18 +183,58 @@ chat_mod_server <- function(
173183
bookmark_on_response = bookmark_on_response
174184
)
175185

186+
last_turn <- shiny::reactiveVal(NULL)
187+
last_input <- shiny::reactiveVal(NULL)
188+
176189
shiny::observeEvent(input$chat_user_input, {
190+
last_input(input$chat_user_input)
177191
append_stream_task$invoke(
178192
client,
179193
"chat",
180194
input$chat_user_input
181195
)
182196
})
183197

184-
shiny::reactive({
198+
shiny::observe({
185199
if (append_stream_task$status() == "success") {
186-
client$last_turn()
200+
last_turn(client$last_turn())
187201
}
188202
})
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+
)
189239
})
190240
}

pkg-r/man/chat_app.Rd

Lines changed: 12 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)