Skip to content

Conversation

gadenbuie
Copy link
Collaborator

Updates chat_mod_server() to return a list of values including:

  • last_input: A reactive value containing the last user input.
  • last_turn: A reactive value containing the last assistant turn.
  • update_user_input(): A function to update the chat input or submit a new user input. Takes the same arguments as [update_chat_user_input()], except for id and session, which are supplied automatically.
  • clear(): A function to clear the chat history and the chat UI. Takes a single argument, clear_history, which indicates whether to also clear the chat history in the client object. Defaults to TRUE.
  • client: The chat client object, which is mutated as you chat.
Example app
library(shiny)
library(bslib)
pkgload::load_all(here::here("pkg-r"))
library(shinychat)

ui <- page_fillable(
  layout_columns(
    fill = FALSE,
    actionButton("update", "Tell me a joke!"),
    actionButton("clear", "Clear chat")
  ),
  layout_columns(
    card(
      card_header("Chat"),
      chat_mod_ui("chat")
    ),
    card(
      card_header("Debug info"),
      verbatimTextOutput("debug")
    )
  )
)

server <- function(input, output, session) {
  chat <- chat_mod_server("chat", ellmer::chat("openai/gpt-4.1-nano"))

  observeEvent(input$update, {
    chat$update_user_input("Tell me a joke!", submit = TRUE)
  })

  observeEvent(input$clear, {
    chat$clear()
  })

  output$debug <- renderPrint({
    list(
      last_turn = chat$last_turn(),
      last_input = chat$last_input()
    )
  })
}

shinyApp(ui, server)

@gadenbuie gadenbuie requested a review from cpsievert September 9, 2025 18:18
@gadenbuie gadenbuie merged commit c504408 into main Sep 9, 2025
11 checks passed
@gadenbuie gadenbuie deleted the pkg-r/mod-update branch September 9, 2025 18:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant