Learn R Programming

aisdk (version 1.1.0)

console_chat: Start Console Chat

Description

Launch an interactive chat session in the R console. Supports streaming output, slash commands, and colorful display using the cli package.

The console UI has three presentation modes:

  • clean: compact default output with a stable status bar

  • inspect: keeps the compact transcript but adds a per-turn tool timeline and an overlay-backed inspector

  • debug: shows detailed tool logs and thinking output for troubleshooting

In agent mode, console_chat() can execute shell and R tools, summarize tool progress inline, and open an inspector overlay for the latest turn or a specific tool. The current implementation uses a shared frame builder for the status bar, tool timeline, and overlay surfaces, while preserving an append-only terminal fallback.

By default, the console operates in agent mode with tools for bash execution, file operations, R code execution, and more. Set agent = NULL for simple chat without tools.

Usage

console_chat(
  session = NULL,
  system_prompt = NULL,
  tools = NULL,
  hooks = NULL,
  stream = TRUE,
  verbose = FALSE,
  agent = "auto",
  working_dir = tempdir(),
  sandbox_mode = "permissive",
  show_thinking = verbose
)

Value

The ChatSession object (invisibly) when chat ends.

Arguments

session

A ChatSession object, a LanguageModelV1 object, or a model string ID to create a new session.

system_prompt

Optional system prompt (merged with agent prompt if agent is used).

tools

Optional list of additional Tool objects.

hooks

Optional HookHandler object.

stream

Whether to use streaming output. Default TRUE.

verbose

Logical. If TRUE, show detailed tool calls, tool results, and thinking output. Defaults to FALSE for a cleaner console UI.

agent

Agent configuration. Options:

  • "auto" (default): Use the built-in console agent with terminal tools

  • NULL: Simple chat mode without tools

  • An Agent object: Use the provided custom agent

working_dir

Working directory for the console agent. Defaults to tempdir().

sandbox_mode

Sandbox mode for the console agent: "strict", "permissive" (default), or "none".

show_thinking

Logical. Whether to show model thinking blocks when the provider exposes them. Defaults to verbose.

Examples

Run this code
# \donttest{
if (interactive()) {
  # Start with default agent (intelligent terminal mode)
  console_chat("openai:gpt-4o")

  # Start in debug mode with full tool logs
  console_chat("openai:gpt-4o", verbose = TRUE)

  # Simple chat mode without tools
  console_chat("openai:gpt-4o", agent = NULL)

  # Start with an existing session
  chat <- create_chat_session("anthropic:claude-3-5-sonnet-latest")
  console_chat(chat)

  # Start with a custom agent
  agent <- create_agent("MathAgent", "Does math", system_prompt = "You are a math wizard.")
  console_chat("openai:gpt-4o", agent = agent)

  # Available commands in the chat:
  # /quit or /exit - End the chat
  # /save [path]   - Save session to file
  # /load [path]   - Load session from file
  # /model         - Open the provider/model chooser
  # /model [id]    - Switch to a different model
  # /model current - Show the active model
  # /history       - Show conversation history
  # /stats         - Show token usage statistics
  # /clear         - Clear conversation history
  # /stream [on|off] - Toggle streaming mode
  # /inspect [on|off] - Toggle inspect mode
  # /inspect turn - Open overlay for the latest turn
  # /inspect tool  - Open overlay for a tool in the latest turn
  # /inspect next - Move inspector overlay to the next tool
  # /inspect prev - Move inspector overlay to the previous tool
  # /inspect close - Close the active inspect overlay
  # /debug [on|off]  - Toggle detailed tool/thinking output
  # /local [on|off]- Toggle local execution mode (Global Environment)
  # /help          - Show available commands
  # /agent [on|off] - Toggle agent mode
}
# }

Run the code above in your browser using DataLab