Skip to content

Conversation

gadenbuie
Copy link
Collaborator

@gadenbuie gadenbuie commented Oct 2, 2025

For #140

Updates the $clear() function in the chat_mod_server() return value to take 2 arguments:

  • messages: An optional parameter used to initialize the chat after clearing.

    • Should be a list of messages, equivalent to messages of chat_ui() and chat_mod_ui().
    • For convenience, a single string is assumed to be an assistant message. Also cleanly handles a single message.
  • client_history: Controls how the chat client's history is updated after clearing. Can be one of four values:

    • "clear" - clears the chat history
    • "set" - sets the chat history to messages
    • "append" - appends messages to the existing chat history
    • "keep" - keeps the existing chat history

Example

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

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

server <- function(input, output, session) {
  client <- ellmer::chat("openai/gpt-4.1-nano")
  
  chat1 <- chat_mod_server("chat1", client)
  
  observeEvent(input$reset_chat, {
    chat1$clear(
      "🔄 Chat was reset. Ready for new dialog!",
      client_history = "clear"
    )
  })
}

shinyApp(ui, server)

@gadenbuie gadenbuie requested a review from Copilot October 2, 2025 20:15
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR enhances the $clear() function in chat_mod_server() to accept optional initial messages and control client history behavior after clearing. The update allows developers to programmatically reset chat sessions while optionally setting new initial messages and controlling how the chat client's history is managed.

  • Added support for initializing chat with messages after clearing
  • Introduced client_history parameter with four modes: "clear", "set", "append", "keep"
  • Added utility function as_ellmer_turns() to convert messages to ellmer Turn objects

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
pkg-r/R/utils-ellmer.R New utility function to convert messages to ellmer Turn objects
pkg-r/R/chat_app.R Enhanced $clear() function with message initialization and client history control

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@gadenbuie gadenbuie merged commit 92d70b5 into main Oct 2, 2025
@gadenbuie gadenbuie deleted the r/feat/mod-clear branch October 2, 2025 20:50
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