| ChatSession | R Documentation |
R6 class representing a stateful chat session. Automatically manages conversation history, supports tool execution, and provides persistence.
new()Initialize a new ChatSession.
ChatSession$new( model = NULL, system_prompt = NULL, tools = NULL, hooks = NULL, history = NULL, max_steps = 10, registry = NULL, memory = NULL, metadata = NULL, envir = NULL, agent = NULL )
modelA LanguageModelV1 object or model string ID (e.g., "openai:gpt-4o").
system_promptOptional system prompt for the conversation.
toolsOptional list of Tool objects for function calling.
hooksOptional HookHandler object for event hooks.
historyOptional initial message history (list of message objects).
max_stepsMaximum steps for tool execution loops. Default 10.
registryOptional ProviderRegistry for model resolution.
memoryOptional initial shared memory (list). For multi-agent state sharing.
metadataOptional session metadata (list). Used for channel/runtime state.
envirOptional shared R environment. For multi-agent data sharing.
agentOptional Agent object. If provided, the session inherits the agent's tools and system prompt.
send()Send a message and get a response.
ChatSession$send(prompt, ...)
promptThe user message to send.
...Additional arguments passed to generate_text.
The GenerateResult object from the model.
send_stream()Send a message with streaming output.
ChatSession$send_stream(prompt, callback, ...)
promptThe user message to send.
callbackFunction called for each chunk: callback(text, done).
...Additional arguments passed to stream_text.
The GenerateResult object invisibly (output is via callback).
continue_run()Inject a manual continuation instruction into the current task.
ChatSession$continue_run( action = "continue", guidance = NULL, stream = TRUE, callback = NULL, ... )
actionOne of "continue", "give_up", "avoid_tool", "explain", or "manual".
guidanceOptional operator guidance to include in the continuation.
streamWhether to use streaming generation.
callbackStreaming callback when stream = TRUE.
...Additional arguments passed to send/send_stream.
The GenerateResult object, or an invisible waiting-user result for manual action.
append_message()Append a message to the history.
ChatSession$append_message(role, content, reasoning = NULL)
roleMessage role: "user", "assistant", "system", or "tool".
contentMessage content.
reasoningOptional reasoning text to attach to the message.
get_history()Get the conversation history.
ChatSession$get_history()
A list of message objects.
get_last_response()Get the last response from the assistant.
ChatSession$get_last_response()
The content of the last assistant message, or NULL.
clear_history()Clear the conversation history.
ChatSession$clear_history(keep_system = TRUE)
keep_systemIf TRUE, keeps the system prompt. Default TRUE.
switch_model()Switch to a different model.
ChatSession$switch_model(model)
modelA LanguageModelV1 object or model string ID.
get_model_options()Get model runtime options for this session.
ChatSession$get_model_options()
A list with context overrides and call options.
get_model_call_options()Get generation options applied to every model call.
ChatSession$get_model_call_options()
A named list of call options.
set_model_options()Set runtime options for this session's model.
ChatSession$set_model_options( context_window = NULL, max_output_tokens = NULL, max_tokens = NULL, thinking = NULL, thinking_budget = NULL, reasoning_effort = NULL, reset = FALSE )
context_windowOptional context-window override.
max_output_tokensOptional maximum output-token metadata override.
max_tokensOptional default generation token limit.
thinkingOptional default thinking-mode value.
thinking_budgetOptional default thinking budget.
reasoning_effortOptional default reasoning effort.
resetLogical. If TRUE, clears all model runtime options first.
Invisible self for chaining.
clear_model_options()Clear model runtime options for this session.
ChatSession$clear_model_options(keys = NULL)
keysOptional option names to clear. If NULL, clears all.
Invisible self for chaining.
set_capability_model()Set a model route for a session capability.
ChatSession$set_capability_model( capability, model, type = "auto", required_model_capabilities = NULL )
capabilityCapability route name, such as "vision.inspect".
modelModel ID string or model object. Passing NULL clears the route.
typeModel type: "auto", "language", "embedding", or "image".
required_model_capabilitiesOptional required model capability flags.
Invisible self for chaining.
get_capability_model()Get the configured model for a session capability.
ChatSession$get_capability_model(capability, default = NULL)
capabilityCapability route name.
defaultValue returned when no route is configured.
A model ID string, model object, or default.
list_capability_models()List session capability model routes.
ChatSession$list_capability_models()
A data frame of configured session routes.
clear_capability_model()Clear one or all session capability model routes.
ChatSession$clear_capability_model(capability = NULL)
capabilityOptional route name. If NULL, clears all routes.
Invisible self for chaining.
get_model_id()Get current model identifier.
ChatSession$get_model_id()
Model ID string.
get_model()Get the resolved language model for this session.
ChatSession$get_model()
A LanguageModelV1 object.
get_tools()Get tools configured on this session.
ChatSession$get_tools()
A list of Tool objects.
stats()Get token usage statistics.
ChatSession$stats()
A list with token counts and message stats.
save()Save session to a file.
ChatSession$save(path, format = NULL)
pathFile path (supports .rds or .json extension).
formatOptional format override: "rds" or "json". Auto-detected from path.
as_list()Export session state as a list (for serialization).
ChatSession$as_list()
A list containing session state.
restore()Restore session from a file.
ChatSession$restore(path, format = NULL)
pathFile path (supports .rds or .json extension).
formatOptional format override: "rds" or "json". Auto-detected from path.
restore_from_list()Restore session state from a list.
ChatSession$restore_from_list(data)
dataA list exported by as_list().
print()Print method for ChatSession.
ChatSession$print()
get_memory()Get a value from shared memory.
ChatSession$get_memory(key, default = NULL)
keyThe key to retrieve.
defaultDefault value if key not found. Default NULL.
The stored value or default.
get_run_state()Return the most recent structured run state.
ChatSession$get_run_state()
A run state list.
set_run_state()Store the current structured run state.
ChatSession$set_run_state(run_state)
run_stateA run state list.
Invisible self.
set_memory()Set a value in shared memory.
ChatSession$set_memory(key, value)
keyThe key to store.
valueThe value to store.
Invisible self for chaining.
list_memory()List all keys in shared memory.
ChatSession$list_memory()
Character vector of memory keys.
get_metadata()Get a value from session metadata.
ChatSession$get_metadata(key, default = NULL)
keyThe metadata key to retrieve.
defaultDefault value if key is not present.
The stored metadata value or default.
set_metadata()Set a value in session metadata.
ChatSession$set_metadata(key, value)
keyThe metadata key to set.
valueThe value to store.
Invisible self for chaining.
merge_metadata()Merge a named list into session metadata.
ChatSession$merge_metadata(values)
valuesNamed list of metadata values.
Invisible self for chaining.
list_metadata()List metadata keys.
ChatSession$list_metadata()
Character vector of metadata keys.
get_context_state()Get the adaptive context state for this session.
ChatSession$get_context_state()
A normalized context state list.
set_context_state()Store adaptive context state for this session.
ChatSession$set_context_state(state)
stateContext state list.
Invisible self for chaining.
clear_context_state()Clear adaptive context state back to defaults.
ChatSession$clear_context_state()
Invisible self for chaining.
get_context_management_mode()Get the context management mode for this session.
ChatSession$get_context_management_mode()
One of "off", "basic", or "adaptive".
get_context_management_config()Get the full adaptive context-management configuration.
ChatSession$get_context_management_config()
A normalized context-management configuration list.
set_context_management_mode()Override the context management mode for this session.
ChatSession$set_context_management_mode(mode)
modeOne of "off", "basic", or "adaptive".
Invisible self for chaining.
set_context_management_config()Apply adaptive context-management configuration to this session.
ChatSession$set_context_management_config(config = NULL, ...)
configOptional config list created by create_context_management_config().
...Optional overrides forwarded to set_context_management_config().
Invisible self for chaining.
get_context_metrics()Estimate current context metrics for this session.
ChatSession$get_context_metrics(turn_system_prompt = NULL)
turn_system_promptOptional turn-specific system prompt to include in the estimate.
A list of context metrics, or NULL if no model metadata is available.
assemble_messages()Build a budget-aware prompt payload from current session history.
ChatSession$assemble_messages(turn_system_prompt = NULL)
turn_system_promptOptional turn-specific system prompt.
A list with messages, system, metrics, and state.
refresh_context_state()Refresh the adaptive context state from current history.
ChatSession$refresh_context_state( generation_result = NULL, turn_system_prompt = NULL )
generation_resultOptional GenerateResult used to update tool/artifact digests.
turn_system_promptOptional turn-specific system prompt for the snapshot.
The normalized context state list.
list_context_handles()List compact context handles available to this session.
ChatSession$list_context_handles()
A list of context handle records.
create_context_query_tools()Create context query tools bound to this session.
ChatSession$create_context_query_tools()
A list of Tool objects.
sub_session_query()Run a bounded child session for a focused query.
ChatSession$sub_session_query(...)
...Arguments passed to sub_session_query().
A compact sub-session result list.
clear_memory()Clear shared memory.
ChatSession$clear_memory(keys = NULL)
keysOptional specific keys to clear. If NULL, clears all.
Invisible self for chaining.
get_envir()Get the shared R environment.
ChatSession$get_envir()
This environment is shared across all agents using this session. Agents can store and retrieve data frames, models, and other R objects.
An environment object.
eval_in_session()Evaluate an expression in the session environment.
ChatSession$eval_in_session(expr)
exprAn expression to evaluate.
The result of the evaluation.
list_envir()List objects in the session environment.
ChatSession$list_envir()
Character vector of object names.
checkpoint()Save a memory snapshot to a file (checkpoint for Mission resume).
ChatSession$checkpoint(path = NULL)
pathFile path (.rds). If NULL, uses a temp file and returns the path.
Invisible file path.
restore_checkpoint()Restore memory and history from a checkpoint file.
ChatSession$restore_checkpoint(path)
pathFile path to a checkpoint created by checkpoint().
Invisible self for chaining.
clone()The objects of this class are cloneable with this method.
ChatSession$clone(deep = FALSE)
deepWhether to make a deep clone.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.