pub enum AgentMessage {
System {
content: String,
},
User {
content: String,
},
Assistant {
content: AssistantContent,
},
Tool {
tool_call_id: String,
content: String,
},
}Expand description
A single message in the agent conversation.
The closed enum prevents invalid states that a flat struct with role: String
would allow (e.g. a User message carrying tool_calls, or a Tool message
without a tool_call_id).
§Wire format
#[serde(tag = "role", rename_all = "lowercase")] produces JSON identical to
the TypeScript ChatMessage interface in the frontend:
{ "role": "user", "content": "What files are in the project?" }
{ "role": "assistant", "content": null, "tool_calls": [...] }
{ "role": "tool", "tool_call_id": "call_abc", "content": "src/\nlib/" }Variants§
System
A system-level instruction that sets the model’s persona and constraints.
User
A message from the human user.
Assistant
A response from the assistant model.
content always carries either text, tool calls, or both — the
vacuous all-None state of the previous Option<String> +
Option<Vec<ToolCall>> representation is impossible to construct.
Fields
content: AssistantContentContent of the assistant turn.
Tool
The result of a tool call, to be sent back to the model.
Fields
tool_call_id: StringMust match the ToolCall::id from the preceding Assistant message.
Implementations§
Source§impl AgentMessage
impl AgentMessage
Sourcepub fn char_count(&self) -> usize
pub fn char_count(&self) -> usize
Estimate the Unicode scalar-value count of this message.
Uses [str::chars().count()] rather than str::len (byte count) so
that multi-byte characters are counted as one unit, matching how LLMs
typically measure context length.
§Performance
This is an O(n) scan — it iterates over every Unicode scalar value
in every str field of the message. Avoid calling it inside tight or
nested loops. For repeated measurements over the same message set,
accumulate the total once and update it incrementally (the agent loop
does exactly this via its running_chars counter).
Trait Implementations§
Source§impl Clone for AgentMessage
impl Clone for AgentMessage
Source§fn clone(&self) -> AgentMessage
fn clone(&self) -> AgentMessage
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more