Skip to content

Conversation

gadenbuie
Copy link
Collaborator

@gadenbuie gadenbuie commented Oct 2, 2025

Adds $append() to the functions returned by chat_mod_server() to make it easier to extend a chat UI module. The implementation simply wraps chat_append() with id and session provided.

Closes #140

Example

library(shiny)
library(bslib)
# pak::pak("posit-dev/shinychat/pkg-r#145")
pkgload::load_all(here::here("pkg-r"))
library(shinychat)

ui <- page_fillable(
  chat_mod_ui(
    "chat",
    messages = list(
      list(role = "assistant", content = "👋 Hello and welcome to the chat!")
    )
  ),
  layout_columns(
    actionButton("reset_chat", "Reset chat"),
    actionButton("append_chat", "Append to chat")
  )
)

server <- function(input, output, session) {
  client <- ellmer::chat("openai/gpt-4.1-nano")
  
  chat <- chat_mod_server("chat", client)
  
  observeEvent(input$reset_chat, {
    chat$clear(
      "🔄 Chat was reset. Ready for new dialog!",
      client_history = "clear"
    )
  })
  
  observeEvent(input$append_chat, {
    chat$append("🫡 This is an appended message. Woot!")
  })
}

shinyApp(ui, server)

@gadenbuie gadenbuie merged commit b80b8ba into main Oct 2, 2025
11 checks passed
@gadenbuie gadenbuie deleted the r/feat/mod-append branch October 2, 2025 21:22
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.

[R] chat_append() doesn’t work inside a module server

1 participant