% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/chat.R
\name{chat_set_greeting}
\alias{chat_set_greeting}
\title{Set the greeting for a chat UI}
\usage{
chat_set_greeting(id, greeting, session = getDefaultReactiveDomain())
}
\arguments{
\item{id}{The ID of the chat element}

\item{greeting}{The greeting to display. Can be:
\itemize{
\item \code{NULL} to clear the current greeting.
\item A plain string or \code{\link[htmltools:HTML]{htmltools::HTML()}} object (auto-wrapped with default
options).
\item A \code{\link[=chat_greeting]{chat_greeting()}} object.
}}

\item{session}{The Shiny session object}
}
\value{
Returns \code{invisible(NULL)} for static and NULL greetings, or a
promise for streaming greetings (resolves when streaming is complete).
}
\description{
Sets or clears the greeting displayed in an existing \code{\link[=chat_ui]{chat_ui()}}. The
greeting is shown when the chat first loads and is dismissed when the user
sends their first message.

Call \code{chat_set_greeting()} from the server to display a dynamic or streaming
greeting. This is typically used inside an \code{\link[shiny:observeEvent]{shiny::observeEvent()}} watching
\verb{input$<id>_greeting_requested} -- an event that fires when the chat is
visible, has no messages, and has no greeting set. See the \strong{Greeting}
section in \code{\link[=chat_ui]{chat_ui()}} for details on \code{greeting_requested}.

If the greeting has already been dismissed, calling this function updates
the greeting content but does not make it visible again. To show a new
greeting after dismissal, first clear the chat with
\code{chat_clear(id, greeting = TRUE)}.

Pass \code{NULL} to clear the current greeting entirely.
}
\examples{
\dontshow{if (interactive()) withAutoprint(\{ # examplesIf}
library(shiny)
library(bslib)
library(shinychat)

# Static greeting set from the server
ui <- page_fillable(chat_ui("chat"))

server <- function(input, output, session) {
  chat_set_greeting("chat", "## Welcome!\n\nHow can I help you today?")

  observeEvent(input$chat_user_input, {
    chat_append("chat", paste0("You said: ", input$chat_user_input))
  })
}

shinyApp(ui, server)
\dontshow{\}) # examplesIf}
\dontshow{if (interactive()) withAutoprint(\{ # examplesIf}
library(shiny)
library(bslib)
library(shinychat)
library(coro)

# Streaming greeting generated by a function
greeting_generator <- async_generator(function() {
  for (chunk in strsplit("## Hello!\n\nHow can I help you?", "")[[1]]) {
    yield(chunk)
    await(async_sleep(0.02))
  }
})

ui <- page_fillable(chat_ui("chat"))

server <- function(input, output, session) {
  chat_set_greeting("chat", chat_greeting(greeting_generator()))

  observeEvent(input$chat_user_input, {
    chat_append("chat", paste0("You said: ", input$chat_user_input))
  })
}

shinyApp(ui, server)
\dontshow{\}) # examplesIf}
\dontshow{if (interactive()) withAutoprint(\{ # examplesIf}
library(shiny)
library(bslib)
library(shinychat)

# LLM-generated greeting using greeting_requested
ui <- page_fillable(chat_ui("chat"))

server <- function(input, output, session) {
  chat_client <- ellmer::chat_openai(model = "gpt-4o")

  observeEvent(input$chat_greeting_requested, {
    stream <- chat_client$stream_async(
      "Generate a short, friendly welcome message."
    )
    chat_set_greeting("chat", chat_greeting(stream))
  })

  observeEvent(input$chat_user_input, {
    stream <- chat_client$stream_async(input$chat_user_input)
    chat_append("chat", stream)
  })
}

shinyApp(ui, server)
\dontshow{\}) # examplesIf}
\dontshow{if (interactive()) withAutoprint(\{ # examplesIf}
library(shiny)
library(bslib)
library(shinychat)

# Regenerate pattern: chat_clear(greeting = TRUE) triggers greeting_requested
ui <- page_fillable(
  chat_ui("chat"),
  actionButton("regenerate", "New greeting")
)

server <- function(input, output, session) {
  observeEvent(input$chat_greeting_requested, {
    chat_set_greeting(
      "chat",
      paste("## Welcome!\n\nGenerated at", Sys.time())
    )
  })

  observeEvent(input$regenerate, {
    chat_clear("chat", greeting = TRUE)
  })

  observeEvent(input$chat_user_input, {
    chat_append("chat", paste0("You said: ", input$chat_user_input))
  })
}

shinyApp(ui, server)
\dontshow{\}) # examplesIf}
}
