pub struct BasicAgent {Show 13 fields
pub system_prompt: String,
pub model_config: ModelConfig,
pub provider_override: Option<Arc<dyn StreamProvider>>,
pub thinking_level: ThinkingLevel,
pub max_tokens: Option<u32>,
pub temperature: Option<f32>,
pub context_config: Option<ContextConfig>,
pub execution_limits: Option<ExecutionLimits>,
pub cache_config: CacheConfig,
pub tool_execution: ToolExecutionStrategy,
pub tool_timeout: Option<Duration>,
pub response_format: ResponseFormat,
pub retry_config: RetryConfig,
/* private fields */
}Expand description
Reference implementation of the Agent trait.
Custom agents should implement the Agent trait directly. New generic agent
methods should be defined on the Agent trait first, then implemented here —
never add public methods to BasicAgent without the corresponding trait method.
Configuration is done via the builder pattern before any prompting. The runtime
interface (prompting, state access, control) is provided via the Agent trait.
Fields§
§system_prompt: String§model_config: ModelConfig§provider_override: Option<Arc<dyn StreamProvider>>Optional provider override. When set, bypasses ProviderRegistry dispatch.
Used primarily in tests to inject a MockProvider.
thinking_level: ThinkingLevel§max_tokens: Option<u32>§temperature: Option<f32>§context_config: Option<ContextConfig>§execution_limits: Option<ExecutionLimits>§cache_config: CacheConfig§tool_execution: ToolExecutionStrategy§tool_timeout: Option<Duration>§response_format: ResponseFormat§retry_config: RetryConfigImplementations§
Source§impl BasicAgent
impl BasicAgent
pub fn new(model_config: ModelConfig) -> Self
Sourcepub fn with_session(self, session: Session) -> Self
pub fn with_session(self, session: Session) -> Self
Set a session for block-based compaction.
Sourcepub fn take_session(&mut self) -> Option<Session>
pub fn take_session(&mut self) -> Option<Session>
Take the session out of the agent, returning ownership.
pub fn with_system_prompt(self, prompt: impl Into<String>) -> Self
pub fn with_thinking(self, level: ThinkingLevel) -> Self
pub fn with_tools(self, tools: Vec<Arc<dyn AgentTool>>) -> Self
pub fn with_model_config(self, config: ModelConfig) -> Self
Sourcepub fn with_provider_override(self, provider: Arc<dyn StreamProvider>) -> Self
pub fn with_provider_override(self, provider: Arc<dyn StreamProvider>) -> Self
Override the provider used by the agent loop, bypassing ProviderRegistry dispatch.
Primarily used in tests to inject a MockProvider.
pub fn with_max_tokens(self, max: u32) -> Self
pub fn with_context_config(self, config: ContextConfig) -> Self
pub fn with_cache_config(self, config: CacheConfig) -> Self
pub fn with_tool_execution(self, strategy: ToolExecutionStrategy) -> Self
Sourcepub fn with_tool_timeout(self, timeout: Duration) -> Self
pub fn with_tool_timeout(self, timeout: Duration) -> Self
Set the per-tool execution timeout. See AgentLoopConfig::tool_timeout.
Sourcepub fn with_response_format(self, format: ResponseFormat) -> Self
pub fn with_response_format(self, format: ResponseFormat) -> Self
Set the desired LLM output shape. See crate::provider::ResponseFormat.
pub fn with_retry_config(self, config: RetryConfig) -> Self
Sourcepub fn with_skills(self, skills: SkillSet) -> Self
pub fn with_skills(self, skills: SkillSet) -> Self
Load skills and append their index to the system prompt.
The skills index is appended as XML per the AgentSkills standard.
The agent can then read individual SKILL.md files using the read_file tool
when it decides a skill is relevant.
pub fn with_execution_limits(self, limits: ExecutionLimits) -> Self
pub fn with_messages(self, msgs: Vec<AgentMessage>) -> Self
pub fn on_before_turn( self, f: impl Fn(&[AgentMessage], usize) -> bool + Send + Sync + 'static, ) -> Self
pub fn on_after_turn( self, f: impl Fn(&[AgentMessage], &Usage) + Send + Sync + 'static, ) -> Self
pub fn on_error(self, f: impl Fn(&str) + Send + Sync + 'static) -> Self
Sourcepub fn with_input_filter(self, filter: impl InputFilter + 'static) -> Self
pub fn with_input_filter(self, filter: impl InputFilter + 'static) -> Self
Add an input filter. Filters run in order on user messages before the LLM call.
Sourcepub fn with_compaction_strategy(
self,
strategy: impl CompactionStrategy + 'static,
) -> Self
pub fn with_compaction_strategy( self, strategy: impl CompactionStrategy + 'static, ) -> Self
Set a custom in-memory compaction strategy on the context config.
When set, replaces DefaultCompaction during context compaction
for sessionless runs. (G5: stored on CompactionConfig, not BasicAgent.)
Sourcepub fn with_profile(self, profile: AgentProfile) -> Self
pub fn with_profile(self, profile: AgentProfile) -> Self
Set the agent profile blueprint. Also copies profile fields into this agent’s public fields for backward compatibility (profile values act as defaults; existing field values take precedence if already set).
Sourcepub fn with_temperature(self, temp: f32) -> Self
pub fn with_temperature(self, temp: f32) -> Self
Set the temperature for LLM calls.
Sourcepub fn with_config_id(self, id: impl Into<String>) -> Self
pub fn with_config_id(self, id: impl Into<String>) -> Self
Set the config identity, used as the middle segment of loop_id.
Sourcepub fn with_workspace(self, path: impl Into<PathBuf>) -> Self
pub fn with_workspace(self, path: impl Into<PathBuf>) -> Self
Set the agent workspace directory. File paths in system prompt blocks resolve relative to this directory.
Sourcepub fn on_before_loop(
self,
f: impl Fn(&[AgentMessage], usize) -> bool + Send + Sync + 'static,
) -> Self
pub fn on_before_loop( self, f: impl Fn(&[AgentMessage], usize) -> bool + Send + Sync + 'static, ) -> Self
Set the before-loop hook. Return false to abort the loop.
Sourcepub fn on_after_loop(
self,
f: impl Fn(&[AgentMessage], &Usage) + Send + Sync + 'static,
) -> Self
pub fn on_after_loop( self, f: impl Fn(&[AgentMessage], &Usage) + Send + Sync + 'static, ) -> Self
Set the after-loop hook.
Sourcepub fn on_before_tool_execution(
self,
f: impl Fn(&str, &str, &Value) -> bool + Send + Sync + 'static,
) -> Self
pub fn on_before_tool_execution( self, f: impl Fn(&str, &str, &Value) -> bool + Send + Sync + 'static, ) -> Self
Set the before-tool-execution hook. Return false to skip the tool call.
Sourcepub fn on_after_tool_execution(
self,
f: impl Fn(&str, &str, bool) + Send + Sync + 'static,
) -> Self
pub fn on_after_tool_execution( self, f: impl Fn(&str, &str, bool) + Send + Sync + 'static, ) -> Self
Set the after-tool-execution hook.
Sourcepub fn on_before_tool_execution_update(
self,
f: impl Fn(&str, &str, &str) -> bool + Send + Sync + 'static,
) -> Self
pub fn on_before_tool_execution_update( self, f: impl Fn(&str, &str, &str) -> bool + Send + Sync + 'static, ) -> Self
Set the before-tool-execution-update hook. Return false to suppress the event.
Sourcepub fn on_after_tool_execution_update(
self,
f: impl Fn(&str, &str, &str) + Send + Sync + 'static,
) -> Self
pub fn on_after_tool_execution_update( self, f: impl Fn(&str, &str, &str) + Send + Sync + 'static, ) -> Self
Set the after-tool-execution-update hook.
Sourcepub fn with_prun_tool(self) -> Self
pub fn with_prun_tool(self) -> Self
Enable the prun tool (both prun and prun_with_memo variants).
Adds both tool variants to the tool set and wires up the shared pending queue.
Sourcepub fn with_convert_to_llm(
self,
f: impl Fn(&[AgentMessage]) -> Vec<Message> + Send + Sync + 'static,
) -> Self
pub fn with_convert_to_llm( self, f: impl Fn(&[AgentMessage]) -> Vec<Message> + Send + Sync + 'static, ) -> Self
Set a custom convert-to-LLM function.
Sourcepub fn with_transform_context(
self,
f: impl Fn(Vec<AgentMessage>) -> Vec<AgentMessage> + Send + Sync + 'static,
) -> Self
pub fn with_transform_context( self, f: impl Fn(Vec<AgentMessage>) -> Vec<AgentMessage> + Send + Sync + 'static, ) -> Self
Set a custom transform-context function.
Sourcepub fn with_block_compaction_strategy(
self,
strategy: impl BlockCompactionStrategy + 'static,
) -> Self
pub fn with_block_compaction_strategy( self, strategy: impl BlockCompactionStrategy + 'static, ) -> Self
Set a custom block compaction strategy for Session-aware compaction. (G5: stored on CompactionConfig, not BasicAgent.)
Sourcepub fn on_before_compaction_start(
self,
f: impl Fn(usize, usize) -> bool + Send + Sync + 'static,
) -> Self
pub fn on_before_compaction_start( self, f: impl Fn(usize, usize) -> bool + Send + Sync + 'static, ) -> Self
Set the before-compaction-start hook (G1). Return false to skip compaction.
Sourcepub fn on_after_compaction_end(
self,
f: impl Fn(usize, usize, usize, usize) + Send + Sync + 'static,
) -> Self
pub fn on_after_compaction_end( self, f: impl Fn(usize, usize, usize, usize) + Send + Sync + 'static, ) -> Self
Set the after-compaction-end hook (G1).
Sourcepub fn with_context_translation(
self,
strategy: Arc<dyn ContextTranslationStrategy>,
) -> Self
pub fn with_context_translation( self, strategy: Arc<dyn ContextTranslationStrategy>, ) -> Self
Set the context translation strategy (G8) for cross-provider compatibility.
Sourcepub fn with_sub_agent(self, sub: SubAgentTool) -> Self
pub fn with_sub_agent(self, sub: SubAgentTool) -> Self
Add a sub-agent tool. The sub-agent runs its own agent_loop() when invoked.
Sourcepub fn without_context_management(self) -> Self
pub fn without_context_management(self) -> Self
Disable automatic context compaction
Sourcepub async fn with_mcp_server_stdio(
self,
command: &str,
args: &[&str],
env: Option<HashMap<String, String>>,
) -> Result<Self, McpError>
pub async fn with_mcp_server_stdio( self, command: &str, args: &[&str], env: Option<HashMap<String, String>>, ) -> Result<Self, McpError>
Connect to an MCP server via stdio and add its tools to the agent.
Sourcepub async fn with_mcp_server_http(self, url: &str) -> Result<Self, McpError>
pub async fn with_mcp_server_http(self, url: &str) -> Result<Self, McpError>
Connect to an MCP server via HTTP and add its tools to the agent.
Sourcepub async fn prompt(
&mut self,
text: impl Into<String>,
) -> UnboundedReceiver<AgentEvent>
pub async fn prompt( &mut self, text: impl Into<String>, ) -> UnboundedReceiver<AgentEvent>
Send a text prompt. Returns a stream of AgentEvents.
Accepts impl Into<String> (e.g. &str). The trait’s Agent::prompt default
requires an owned String; use this inherent method to pass &str without .to_string().
Sourcepub async fn prompt_with_sender(
&mut self,
text: impl Into<String>,
tx: UnboundedSender<AgentEvent>,
)
pub async fn prompt_with_sender( &mut self, text: impl Into<String>, tx: UnboundedSender<AgentEvent>, )
Send a text prompt, streaming events to a caller-provided sender.
Accepts impl Into<String> (e.g. &str).
Sourcepub fn compact_context_with_sender(&mut self, tx: &UnboundedSender<AgentEvent>)
pub fn compact_context_with_sender(&mut self, tx: &UnboundedSender<AgentEvent>)
Run block-based compaction on the agent’s session and emit the full event lifecycle.
Emits: AgentStart(Compaction) → CompactionStarted → CompactionEnded → AgentEnd.
Requires self.session to be Some and self.context_config to be Some.
Panics if either is missing.
No-op if self.session or self.context_config is None.
Sourcepub fn compact_context(&mut self) -> usize
pub fn compact_context(&mut self) -> usize
Fire-and-forget compaction. Returns the number of loops that received
new CompactionBlocks.
Requires self.session to be Some and self.context_config to be Some.
Returns 0 if self.session or self.context_config is None.
Sourcepub fn new_session(&mut self) -> String
pub fn new_session(&mut self) -> String
Immediately rotate to a new session_id.
All subsequent loops will belong to the new session. Loop counters are
reset so the new session’s loop ids start from .1.
Returns the newly assigned session_id.
Sourcepub fn check_and_rotate(&mut self, threshold: Duration) -> Option<String>
pub fn check_and_rotate(&mut self, threshold: Duration) -> Option<String>
Rotate to a new session if the agent has been idle for longer than threshold.
Idleness is measured from the last prompt_messages_with_sender
call. If no prompt has ever been issued, returns None (no rotation needed
— the session has never been used).
Returns Some(new_session_id) if rotation happened, None otherwise.
Trait Implementations§
Source§impl Agent for BasicAgent
impl Agent for BasicAgent
Source§fn prompt_messages_with_sender<'life0, 'async_trait>(
&'life0 mut self,
messages: Vec<AgentMessage>,
tx: UnboundedSender<AgentEvent>,
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn prompt_messages_with_sender<'life0, 'async_trait>(
&'life0 mut self,
messages: Vec<AgentMessage>,
tx: UnboundedSender<AgentEvent>,
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Send messages as a prompt, streaming events to a caller-provided sender.
Source§fn continue_loop_with_sender<'life0, 'async_trait>(
&'life0 mut self,
tx: UnboundedSender<AgentEvent>,
kind: ContinuationKind,
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn continue_loop_with_sender<'life0, 'async_trait>(
&'life0 mut self,
tx: UnboundedSender<AgentEvent>,
kind: ContinuationKind,
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Continue from current context, streaming events to a caller-provided sender.
kind describes how this continuation relates to prior loops:
Default— unspecified continuation (preserves current semantics; use when the Rerun/Branch distinction is not relevant to the caller)Rerun { tag }— retry from the same context state (auto-generates a UTC timestamp tag)Branch { tag }— explore a different path from the same starting point (same tag)
Source§fn messages(&self) -> &[AgentMessage]
fn messages(&self) -> &[AgentMessage]
Source§fn is_streaming(&self) -> bool
fn is_streaming(&self) -> bool
Source§fn agent_id(&self) -> &str
fn agent_id(&self) -> &str
AgentStart event.Source§fn session_id(&self) -> &str
fn session_id(&self) -> &str
Source§fn last_loop_id(&self) -> Option<&str>
fn last_loop_id(&self) -> Option<&str>
Source§fn clear_messages(&mut self)
fn clear_messages(&mut self)
Source§fn append_message(&mut self, msg: AgentMessage)
fn append_message(&mut self, msg: AgentMessage)
Source§fn replace_messages(&mut self, msgs: Vec<AgentMessage>)
fn replace_messages(&mut self, msgs: Vec<AgentMessage>)
Source§fn restore_messages(&mut self, json: &str) -> Result<(), Error>
fn restore_messages(&mut self, json: &str) -> Result<(), Error>
Source§fn steer(&self, msg: AgentMessage)
fn steer(&self, msg: AgentMessage)
Source§fn follow_up(&self, msg: AgentMessage)
fn follow_up(&self, msg: AgentMessage)
Source§fn clear_steering_queue(&self)
fn clear_steering_queue(&self)
Source§fn clear_follow_up_queue(&self)
fn clear_follow_up_queue(&self)
Source§fn set_steering_mode(&mut self, mode: QueueMode)
fn set_steering_mode(&mut self, mode: QueueMode)
Source§fn set_follow_up_mode(&mut self, mode: QueueMode)
fn set_follow_up_mode(&mut self, mode: QueueMode)
Source§fn profile(&self) -> Option<&AgentProfile>
fn profile(&self) -> Option<&AgentProfile>
None.Source§fn system_prompt(&self) -> &str
fn system_prompt(&self) -> &str
Source§fn model_config(&self) -> Option<&ModelConfig>
fn model_config(&self) -> Option<&ModelConfig>
None.Source§fn thinking_level(&self) -> ThinkingLevel
fn thinking_level(&self) -> ThinkingLevel
ThinkingLevel::Off.Source§fn temperature(&self) -> Option<f32>
fn temperature(&self) -> Option<f32>
None.Source§fn max_tokens(&self) -> Option<u32>
fn max_tokens(&self) -> Option<u32>
None.Source§fn context_config(&self) -> Option<&ContextConfig>
fn context_config(&self) -> Option<&ContextConfig>
None.Source§fn execution_limits(&self) -> Option<&ExecutionLimits>
fn execution_limits(&self) -> Option<&ExecutionLimits>
None.Source§fn cache_config(&self) -> CacheConfig
fn cache_config(&self) -> CacheConfig
CacheConfig::default().Source§fn tool_execution(&self) -> ToolExecutionStrategy
fn tool_execution(&self) -> ToolExecutionStrategy
ToolExecutionStrategy::default().Source§fn tool_timeout(&self) -> Option<Duration>
fn tool_timeout(&self) -> Option<Duration>
None (no per-tool timeout). Read moreSource§fn response_format(&self) -> ResponseFormat
fn response_format(&self) -> ResponseFormat
ResponseFormat::Text (free-form text). Read moreSource§fn retry_config(&self) -> RetryConfig
fn retry_config(&self) -> RetryConfig
RetryConfig::default().Source§fn workspace(&self) -> Option<&Path>
fn workspace(&self) -> Option<&Path>
None (uses current directory).Source§fn set_before_turn(&mut self, f: Option<BeforeTurnFn>)
fn set_before_turn(&mut self, f: Option<BeforeTurnFn>)
Source§fn set_after_turn(&mut self, f: Option<AfterTurnFn>)
fn set_after_turn(&mut self, f: Option<AfterTurnFn>)
Source§fn set_before_loop(&mut self, f: Option<BeforeLoopFn>)
fn set_before_loop(&mut self, f: Option<BeforeLoopFn>)
Source§fn set_after_loop(&mut self, f: Option<AfterLoopFn>)
fn set_after_loop(&mut self, f: Option<AfterLoopFn>)
Source§fn set_before_tool_execution(&mut self, f: Option<BeforeToolExecutionFn>)
fn set_before_tool_execution(&mut self, f: Option<BeforeToolExecutionFn>)
Source§fn set_after_tool_execution(&mut self, f: Option<AfterToolExecutionFn>)
fn set_after_tool_execution(&mut self, f: Option<AfterToolExecutionFn>)
Source§fn set_before_tool_execution_update(
&mut self,
f: Option<BeforeToolExecutionUpdateFn>,
)
fn set_before_tool_execution_update( &mut self, f: Option<BeforeToolExecutionUpdateFn>, )
Source§fn set_after_tool_execution_update(
&mut self,
f: Option<AfterToolExecutionUpdateFn>,
)
fn set_after_tool_execution_update( &mut self, f: Option<AfterToolExecutionUpdateFn>, )
Source§fn set_convert_to_llm(&mut self, f: Option<ConvertToLlmFn>)
fn set_convert_to_llm(&mut self, f: Option<ConvertToLlmFn>)
Source§fn set_transform_context(&mut self, f: Option<TransformContextFn>)
fn set_transform_context(&mut self, f: Option<TransformContextFn>)
Source§fn set_block_compaction_strategy(
&mut self,
s: Option<Arc<dyn BlockCompactionStrategy>>,
)
fn set_block_compaction_strategy( &mut self, s: Option<Arc<dyn BlockCompactionStrategy>>, )
Source§fn set_before_compaction_start(&mut self, f: Option<BeforeCompactionStartFn>)
fn set_before_compaction_start(&mut self, f: Option<BeforeCompactionStartFn>)
Source§fn set_after_compaction_end(&mut self, f: Option<AfterCompactionEndFn>)
fn set_after_compaction_end(&mut self, f: Option<AfterCompactionEndFn>)
Source§fn set_context_translation(
&mut self,
s: Option<Arc<dyn ContextTranslationStrategy>>,
)
fn set_context_translation( &mut self, s: Option<Arc<dyn ContextTranslationStrategy>>, )
Source§fn context_translation(&self) -> Option<Arc<dyn ContextTranslationStrategy>>
fn context_translation(&self) -> Option<Arc<dyn ContextTranslationStrategy>>
Source§fn prompt_with_sender<'life0, 'async_trait>(
&'life0 mut self,
text: String,
tx: UnboundedSender<AgentEvent>,
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn prompt_with_sender<'life0, 'async_trait>(
&'life0 mut self,
text: String,
tx: UnboundedSender<AgentEvent>,
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Source§fn prompt<'life0, 'async_trait>(
&'life0 mut self,
text: String,
) -> Pin<Box<dyn Future<Output = UnboundedReceiver<AgentEvent>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn prompt<'life0, 'async_trait>(
&'life0 mut self,
text: String,
) -> Pin<Box<dyn Future<Output = UnboundedReceiver<AgentEvent>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
AgentEvents. Read moreSource§fn prompt_messages<'life0, 'async_trait>(
&'life0 mut self,
messages: Vec<AgentMessage>,
) -> Pin<Box<dyn Future<Output = UnboundedReceiver<AgentEvent>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn prompt_messages<'life0, 'async_trait>(
&'life0 mut self,
messages: Vec<AgentMessage>,
) -> Pin<Box<dyn Future<Output = UnboundedReceiver<AgentEvent>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
AgentEvents. Read moreSource§fn continue_loop<'life0, 'async_trait>(
&'life0 mut self,
) -> Pin<Box<dyn Future<Output = UnboundedReceiver<AgentEvent>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn continue_loop<'life0, 'async_trait>(
&'life0 mut self,
) -> Pin<Box<dyn Future<Output = UnboundedReceiver<AgentEvent>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
AgentEvents. Read moreSource§fn clear_all_queues(&self)
fn clear_all_queues(&self)
Source§fn set_prun_enabled(&mut self, _enabled: bool)
fn set_prun_enabled(&mut self, _enabled: bool)
Source§fn build_config(&self) -> Result<AgentLoopConfig, AgentBuildError>
fn build_config(&self) -> Result<AgentLoopConfig, AgentBuildError>
AgentLoopConfig from this agent’s current settings. Read more