ChatSession: ChatSession Class

ChatSessionR Documentation

ChatSession Class

Description

R6 class representing a stateful chat session. Automatically manages conversation history, supports tool execution, and provides persistence.

Methods

Public methods


Method new()

Initialize a new ChatSession.

Usage
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
)
Arguments
model

A LanguageModelV1 object or model string ID (e.g., "openai:gpt-4o").

system_prompt

Optional system prompt for the conversation.

tools

Optional list of Tool objects for function calling.

hooks

Optional HookHandler object for event hooks.

history

Optional initial message history (list of message objects).

max_steps

Maximum steps for tool execution loops. Default 10.

registry

Optional ProviderRegistry for model resolution.

memory

Optional initial shared memory (list). For multi-agent state sharing.

metadata

Optional session metadata (list). Used for channel/runtime state.

envir

Optional shared R environment. For multi-agent data sharing.

agent

Optional Agent object. If provided, the session inherits the agent's tools and system prompt.


Method send()

Send a message and get a response.

Usage
ChatSession$send(prompt, ...)
Arguments
prompt

The user message to send.

...

Additional arguments passed to generate_text.

Returns

The GenerateResult object from the model.


Method send_stream()

Send a message with streaming output.

Usage
ChatSession$send_stream(prompt, callback, ...)
Arguments
prompt

The user message to send.

callback

Function called for each chunk: callback(text, done).

...

Additional arguments passed to stream_text.

Returns

The GenerateResult object invisibly (output is via callback).


Method continue_run()

Inject a manual continuation instruction into the current task.

Usage
ChatSession$continue_run(
  action = "continue",
  guidance = NULL,
  stream = TRUE,
  callback = NULL,
  ...
)
Arguments
action

One of "continue", "give_up", "avoid_tool", "explain", or "manual".

guidance

Optional operator guidance to include in the continuation.

stream

Whether to use streaming generation.

callback

Streaming callback when stream = TRUE.

...

Additional arguments passed to send/send_stream.

Returns

The GenerateResult object, or an invisible waiting-user result for manual action.


Method append_message()

Append a message to the history.

Usage
ChatSession$append_message(role, content, reasoning = NULL)
Arguments
role

Message role: "user", "assistant", "system", or "tool".

content

Message content.

reasoning

Optional reasoning text to attach to the message.


Method get_history()

Get the conversation history.

Usage
ChatSession$get_history()
Returns

A list of message objects.


Method get_last_response()

Get the last response from the assistant.

Usage
ChatSession$get_last_response()
Returns

The content of the last assistant message, or NULL.


Method clear_history()

Clear the conversation history.

Usage
ChatSession$clear_history(keep_system = TRUE)
Arguments
keep_system

If TRUE, keeps the system prompt. Default TRUE.


Method switch_model()

Switch to a different model.

Usage
ChatSession$switch_model(model)
Arguments
model

A LanguageModelV1 object or model string ID.


Method get_model_options()

Get model runtime options for this session.

Usage
ChatSession$get_model_options()
Returns

A list with context overrides and call options.


Method get_model_call_options()

Get generation options applied to every model call.

Usage
ChatSession$get_model_call_options()
Returns

A named list of call options.


Method set_model_options()

Set runtime options for this session's model.

Usage
ChatSession$set_model_options(
  context_window = NULL,
  max_output_tokens = NULL,
  max_tokens = NULL,
  thinking = NULL,
  thinking_budget = NULL,
  reasoning_effort = NULL,
  reset = FALSE
)
Arguments
context_window

Optional context-window override.

max_output_tokens

Optional maximum output-token metadata override.

max_tokens

Optional default generation token limit.

thinking

Optional default thinking-mode value.

thinking_budget

Optional default thinking budget.

reasoning_effort

Optional default reasoning effort.

reset

Logical. If TRUE, clears all model runtime options first.

Returns

Invisible self for chaining.


Method clear_model_options()

Clear model runtime options for this session.

Usage
ChatSession$clear_model_options(keys = NULL)
Arguments
keys

Optional option names to clear. If NULL, clears all.

Returns

Invisible self for chaining.


Method set_capability_model()

Set a model route for a session capability.

Usage
ChatSession$set_capability_model(
  capability,
  model,
  type = "auto",
  required_model_capabilities = NULL
)
Arguments
capability

Capability route name, such as "vision.inspect".

model

Model ID string or model object. Passing NULL clears the route.

type

Model type: "auto", "language", "embedding", or "image".

required_model_capabilities

Optional required model capability flags.

Returns

Invisible self for chaining.


Method get_capability_model()

Get the configured model for a session capability.

Usage
ChatSession$get_capability_model(capability, default = NULL)
Arguments
capability

Capability route name.

default

Value returned when no route is configured.

Returns

A model ID string, model object, or default.


Method list_capability_models()

List session capability model routes.

Usage
ChatSession$list_capability_models()
Returns

A data frame of configured session routes.


Method clear_capability_model()

Clear one or all session capability model routes.

Usage
ChatSession$clear_capability_model(capability = NULL)
Arguments
capability

Optional route name. If NULL, clears all routes.

Returns

Invisible self for chaining.


Method get_model_id()

Get current model identifier.

Usage
ChatSession$get_model_id()
Returns

Model ID string.


Method get_model()

Get the resolved language model for this session.

Usage
ChatSession$get_model()
Returns

A LanguageModelV1 object.


Method get_tools()

Get tools configured on this session.

Usage
ChatSession$get_tools()
Returns

A list of Tool objects.


Method stats()

Get token usage statistics.

Usage
ChatSession$stats()
Returns

A list with token counts and message stats.


Method save()

Save session to a file.

Usage
ChatSession$save(path, format = NULL)
Arguments
path

File path (supports .rds or .json extension).

format

Optional format override: "rds" or "json". Auto-detected from path.


Method as_list()

Export session state as a list (for serialization).

Usage
ChatSession$as_list()
Returns

A list containing session state.


Method restore()

Restore session from a file.

Usage
ChatSession$restore(path, format = NULL)
Arguments
path

File path (supports .rds or .json extension).

format

Optional format override: "rds" or "json". Auto-detected from path.


Method restore_from_list()

Restore session state from a list.

Usage
ChatSession$restore_from_list(data)
Arguments
data

A list exported by as_list().


Method print()

Print method for ChatSession.

Usage
ChatSession$print()

Method get_memory()

Get a value from shared memory.

Usage
ChatSession$get_memory(key, default = NULL)
Arguments
key

The key to retrieve.

default

Default value if key not found. Default NULL.

Returns

The stored value or default.


Method get_run_state()

Return the most recent structured run state.

Usage
ChatSession$get_run_state()
Returns

A run state list.


Method set_run_state()

Store the current structured run state.

Usage
ChatSession$set_run_state(run_state)
Arguments
run_state

A run state list.

Returns

Invisible self.


Method set_memory()

Set a value in shared memory.

Usage
ChatSession$set_memory(key, value)
Arguments
key

The key to store.

value

The value to store.

Returns

Invisible self for chaining.


Method list_memory()

List all keys in shared memory.

Usage
ChatSession$list_memory()
Returns

Character vector of memory keys.


Method get_metadata()

Get a value from session metadata.

Usage
ChatSession$get_metadata(key, default = NULL)
Arguments
key

The metadata key to retrieve.

default

Default value if key is not present.

Returns

The stored metadata value or default.


Method set_metadata()

Set a value in session metadata.

Usage
ChatSession$set_metadata(key, value)
Arguments
key

The metadata key to set.

value

The value to store.

Returns

Invisible self for chaining.


Method merge_metadata()

Merge a named list into session metadata.

Usage
ChatSession$merge_metadata(values)
Arguments
values

Named list of metadata values.

Returns

Invisible self for chaining.


Method list_metadata()

List metadata keys.

Usage
ChatSession$list_metadata()
Returns

Character vector of metadata keys.


Method get_context_state()

Get the adaptive context state for this session.

Usage
ChatSession$get_context_state()
Returns

A normalized context state list.


Method set_context_state()

Store adaptive context state for this session.

Usage
ChatSession$set_context_state(state)
Arguments
state

Context state list.

Returns

Invisible self for chaining.


Method clear_context_state()

Clear adaptive context state back to defaults.

Usage
ChatSession$clear_context_state()
Returns

Invisible self for chaining.


Method get_context_management_mode()

Get the context management mode for this session.

Usage
ChatSession$get_context_management_mode()
Returns

One of "off", "basic", or "adaptive".


Method get_context_management_config()

Get the full adaptive context-management configuration.

Usage
ChatSession$get_context_management_config()
Returns

A normalized context-management configuration list.


Method set_context_management_mode()

Override the context management mode for this session.

Usage
ChatSession$set_context_management_mode(mode)
Arguments
mode

One of "off", "basic", or "adaptive".

Returns

Invisible self for chaining.


Method set_context_management_config()

Apply adaptive context-management configuration to this session.

Usage
ChatSession$set_context_management_config(config = NULL, ...)
Arguments
config

Optional config list created by create_context_management_config().

...

Optional overrides forwarded to set_context_management_config().

Returns

Invisible self for chaining.


Method get_context_metrics()

Estimate current context metrics for this session.

Usage
ChatSession$get_context_metrics(turn_system_prompt = NULL)
Arguments
turn_system_prompt

Optional turn-specific system prompt to include in the estimate.

Returns

A list of context metrics, or NULL if no model metadata is available.


Method assemble_messages()

Build a budget-aware prompt payload from current session history.

Usage
ChatSession$assemble_messages(turn_system_prompt = NULL)
Arguments
turn_system_prompt

Optional turn-specific system prompt.

Returns

A list with messages, system, metrics, and state.


Method refresh_context_state()

Refresh the adaptive context state from current history.

Usage
ChatSession$refresh_context_state(
  generation_result = NULL,
  turn_system_prompt = NULL
)
Arguments
generation_result

Optional GenerateResult used to update tool/artifact digests.

turn_system_prompt

Optional turn-specific system prompt for the snapshot.

Returns

The normalized context state list.


Method list_context_handles()

List compact context handles available to this session.

Usage
ChatSession$list_context_handles()
Returns

A list of context handle records.


Method create_context_query_tools()

Create context query tools bound to this session.

Usage
ChatSession$create_context_query_tools()
Returns

A list of Tool objects.


Method sub_session_query()

Run a bounded child session for a focused query.

Usage
ChatSession$sub_session_query(...)
Arguments
...

Arguments passed to sub_session_query().

Returns

A compact sub-session result list.


Method clear_memory()

Clear shared memory.

Usage
ChatSession$clear_memory(keys = NULL)
Arguments
keys

Optional specific keys to clear. If NULL, clears all.

Returns

Invisible self for chaining.


Method get_envir()

Get the shared R environment.

Usage
ChatSession$get_envir()
Details

This environment is shared across all agents using this session. Agents can store and retrieve data frames, models, and other R objects.

Returns

An environment object.


Method eval_in_session()

Evaluate an expression in the session environment.

Usage
ChatSession$eval_in_session(expr)
Arguments
expr

An expression to evaluate.

Returns

The result of the evaluation.


Method list_envir()

List objects in the session environment.

Usage
ChatSession$list_envir()
Returns

Character vector of object names.


Method checkpoint()

Save a memory snapshot to a file (checkpoint for Mission resume).

Usage
ChatSession$checkpoint(path = NULL)
Arguments
path

File path (.rds). If NULL, uses a temp file and returns the path.

Returns

Invisible file path.


Method restore_checkpoint()

Restore memory and history from a checkpoint file.

Usage
ChatSession$restore_checkpoint(path)
Arguments
path

File path to a checkpoint created by checkpoint().

Returns

Invisible self for chaining.


Method clone()

The objects of this class are cloneable with this method.

Usage
ChatSession$clone(deep = FALSE)
Arguments
deep

Whether to make a deep clone.


aisdk documentation built on May 29, 2026, 9:07 a.m.