ConversationContext class
Manages multi-turn conversation history for LLM models.
ConversationContext stores conversation turns with proper message roles (System, User, Assistant). It supports:
- System prompts that persist across clear calls
- Automatic FIFO pruning when history exceeds max length
- Chat template formatting for different LLM formats
Example
final context = ConversationContext();
context.setSystem('You are a helpful assistant.');
context.pushText('Hello!', MessageRole.user);
final result = await model.runWithContext(
XybridEnvelope.text('Hello!'),
context,
);
context.pushText(result.text ?? '', MessageRole.assistant);
Constructors
- ConversationContext()
- Create a new conversation context with a generated UUID.
- ConversationContext.withId(String id)
- Create a new conversation context with a specific ID.
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- hasSystem → bool
-
Check if a system prompt is set.
no setter
- historyLength → int
-
Get the current history length (excluding system prompt).
no setter
- id → String
-
Get the conversation ID.
no setter
- inner → FfiConversationContext
-
The underlying FRB conversation context.
final
- maxHistoryLength → int
-
Get the maximum history length before FIFO pruning.
no setter
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
clear(
) → void - Clear the conversation history but preserve the system prompt and ID.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
push(
XybridEnvelope envelope) → void - Add a message to the conversation history.
-
pushText(
String text, MessageRole role) → void - Add a text message with a specific role.
-
setMaxHistoryLength(
int length) → void - Set the maximum history length before FIFO pruning.
-
setSystem(
String text) → void - Set the system prompt for this conversation.
-
toString(
) → String -
A string representation of this object.
override
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited