Skip to contents

These are the building blocks for deploying a commons chat as a Shiny app; for local development, use commons_app(). Pair commons_server() with shinychat::page_chat() or shinychat::chat_ui(), passing theme = commons_theme() so the commons chat assets are on the page.

Usage

commons_server(id, client, ...)

commons_theme(..., preset = "shiny")

Arguments

id

The ID of the chat element; must match the id of the shinychat::page_chat() or shinychat::chat_ui() on the page.

client

An agent(). In a deployed app, create the agent inside the server function and pass it to commons_server() so each Shiny session gets its own agent state.

...

In commons_server(), arguments passed to shinychat::chat_server(). In commons_theme(), named Sass variables forwarded to shinychat::page_chat_theme().

preset

A bslib or Bootswatch preset name.

Value

commons_server() returns the shinychat::chat_server() result. commons_theme() returns a bslib::bs_theme() object.

Details

The agent writes R code at runtime, so deployment tools can't detect the packages it uses. Include them in the deployment, e.g. by listing the tidyverse in the app's DESCRIPTION; see vignette("commons", package = "commons").

Documents the agent writes open in the chat's drawer, which shinychat::page_chat() and shinychat::chat_ui() enable by default. An app that passes drawer = FALSE gets no documents in view, though the agent still writes them. Documents read best in a wider drawer than the default, e.g. drawer = shinychat::chat_drawer(width = 640, open = FALSE).

commons_theme() bundles the commons chat CSS and JavaScript into an ordinary bslib::bs_theme() (via shinychat::page_chat_theme()), so it works anywhere a bslib theme does.

Sharing on Posit Connect

On Posit Connect, viewers can publish a read-only copy of a conversation, document, plot, or table. Sharing is available only when the app runs on Posit Connect. Shared items are published as the viewer who shares it, so only viewers whose Connect account can publish content can share. Items can only be shared with people who can view the commons apps. Sharing works on a default Connect installation, but administrators may have turned it off.

Examples

if (FALSE) { # \dontrun{
library(shiny)
library(shinychat)

ui <- page_chat("Assistant", id = "chat", theme = commons_theme())

server <- function(input, output, session) {
  # One agent per session, so each user gets their own agent state
  agent <- agent(
    ellmer::chat_anthropic(),
    commons = commons(data_sources = data_source(sales = sales))
  )
  commons_server("chat", agent)
}

shinyApp(ui, server)
} # }