1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// src/agent/core/builder.rs
use super::{Agent, ExtensionsMap};
use crate::agent::processor::{PassThroughFormatter, StandardStreamFormatter};
use crate::agent::tool::DefaultToolParser;
use crate::config::{AgentConfig, EvictionStrategy};
use crate::error::{AmbiError, Result};
use crate::llm::{LLMEngine, LLMEngineConfig, LLMEngineTrait};
use crate::runtime::spawn_blocking;
use crate::types::{ChatTemplate, Message, StreamFormatter, Tool, ToolCallParser, ToolDefinition};
use crate::agent::core::history::ChatHistory;
use crate::AgentState;
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::RwLock;
impl AgentState {
/// Creates a fresh, empty conversation state.
pub fn new(session_id: impl Into<String>) -> Self {
Self {
session_id: session_id.into(),
dynamic_context: String::new(),
chat_history: ChatHistory::new(),
extensions: ExtensionsMap::new(),
}
}
/// Convenience: returns a thread-shared, lock-protected AgentState,
/// ready to be passed directly into Pipeline::execute / execute_stream.
#[cfg_attr(target_arch = "wasm32", allow(clippy::arc_with_non_send_sync))]
pub fn new_shared(session_id: impl Into<String>) -> Arc<RwLock<Self>> {
Arc::new(RwLock::new(Self::new(session_id)))
}
/// Get a mutable reference to the expanded mapping.
pub fn extensions_mut(&mut self) -> &mut ExtensionsMap {
&mut self.extensions
}
/// Get an immutable reference to the extended mapping.
pub fn extensions(&self) -> &ExtensionsMap {
&self.extensions
}
/// Forks the current conversational state into an independent parallel universe.
/// This is a zero-cost operation for message contents, as the underlying `ChatHistory`
/// only clones `Arc` pointers.
/// Useful for advanced branching architectures like Tree of Thoughts (ToT) or CoT.
pub fn fork(&self) -> Self {
Self {
session_id: self.session_id.clone(),
dynamic_context: self.dynamic_context.clone(),
chat_history: self.chat_history.clone(),
extensions: ExtensionsMap::new(), // Note: Extensions are intentionally not cloned
}
}
/// Forks and wraps the state in a thread-safe atomic lock.
#[cfg_attr(target_arch = "wasm32", allow(clippy::arc_with_non_send_sync))]
pub fn fork_shared(&self) -> Arc<RwLock<Self>> {
Arc::new(RwLock::new(self.fork()))
}
}
impl Agent {
/// # Constructors
/// Creates a new Agent using a standard, framework-supported `LLMEngineConfig`.
pub async fn make(engine_cfg: LLMEngineConfig) -> Result<Self> {
let engine = spawn_blocking(move || LLMEngine::load(engine_cfg))
.await
.map_err(|e| {
AmbiError::EngineError(format!("Failed to spawn blocking task: {}", e))
})??;
Ok(Self::init_agent(engine))
}
/// Creates an Agent using a custom LLM backend via the `LLMEngineConfig::Custom` variant.
///
/// # Deprecation
/// This method is deprecated. Use `Agent::make(LLMEngineConfig::Custom(backend)).await` instead.
///
/// # Migration
///
/// ```rust,ignore
/// // Old (deprecated):
/// let agent = Agent::with_custom_engine(Box::new(MyEngine))?;
///
/// // New (recommended):
/// let agent = Agent::make(LLMEngineConfig::Custom(Box::new(MyEngine))).await?;
/// ```
#[deprecated(
since = "0.3.3",
note = "use `Agent::make(LLMEngineConfig::Custom(backend)).await` instead"
)]
#[allow(deprecated)]
pub fn with_custom_engine(custom_backend: Box<dyn LLMEngineTrait>) -> Result<Self> {
let engine = LLMEngine::from_custom(custom_backend)?;
Ok(Self::init_agent(engine))
}
/// Creates an Agent from an already-initialized Engine (Arc).
///
/// This is the primitive constructor; `init_agent` and `make` both go through this.
#[cfg_attr(target_arch = "wasm32", allow(clippy::arc_with_non_send_sync))]
pub fn from_engine(llm_engine: Arc<LLMEngine>) -> Self {
Self {
llm_engine,
config: Arc::new(AgentConfig::default()),
tools_def: Arc::new(Vec::new()),
tool_map: Arc::new(HashMap::new()),
tool_parser: Arc::new(DefaultToolParser::make()),
on_evict_handler: None,
formatter_factory: Arc::new(|| Box::new(PassThroughFormatter)),
cached_tool_prompt: Arc::new(String::new()),
}
}
#[cfg_attr(target_arch = "wasm32", allow(clippy::arc_with_non_send_sync))]
pub(super) fn init_agent(engine: LLMEngine) -> Self {
Self::from_engine(Arc::new(engine))
}
/// # Core Configuration
/// Sets the persistent global system prompt (instructions) for the Agent.
pub fn preamble(mut self, system_prompt: &str) -> Self {
Arc::make_mut(&mut self.config).system_prompt = system_prompt.to_string();
self
}
/// Configures the chat template format (e.g., ChatML, Llama3) used to compile prompts.
pub fn template<T: Into<ChatTemplate>>(mut self, template_source: T) -> Self {
Arc::make_mut(&mut self.config).template = template_source.into();
self
}
/// Customizes the contextual memory limits and eviction behavior.
pub fn with_eviction_strategy(mut self, strategy: EvictionStrategy) -> Self {
Arc::make_mut(&mut self.config).eviction_strategy = strategy;
self
}
/// Sets the maximum number of internal ReAct iterations (LLM -> Tool -> LLM)
/// allowed per single user request before forcefully halting.
pub fn max_iterations(mut self, n: usize) -> Self {
Arc::make_mut(&mut self.config).max_iterations = n;
self
}
/// # Tooling Assembly
/// Registers a custom tool to the Agent.
/// Fails fast and returns an Error if a tool with the same name already exists.
pub fn tool<T: Tool + 'static>(mut self, tool: T) -> Result<Self> {
let def = tool.definition();
let defs = Arc::make_mut(&mut self.tools_def);
let map = Arc::make_mut(&mut self.tool_map);
if defs.iter().any(|t| t.name == def.name) {
return Err(AmbiError::AgentError(format!(
"Tool registration conflict: A tool named '{}' is already registered. \
Please rename your tool or handle the conflict in your setup logic.",
def.name
)));
}
defs.push(ToolDefinition {
name: def.name.clone(),
description: def.description,
parameters: def.parameters,
timeout_secs: def.timeout_secs,
max_retries: def.max_retries,
is_idempotent: def.is_idempotent,
});
map.insert(def.name, Arc::new(tool));
self.update_cached_tool_prompt();
Ok(self)
}
/// Register multiple custom tools with the agent at the same time.
pub fn with_dyn_tools<T: Tool + 'static>(mut self, tools: Vec<Arc<T>>) -> Result<Self> {
for tool in tools {
let def = tool.definition();
Arc::make_mut(&mut self.tools_def).push(def.clone());
Arc::make_mut(&mut self.tool_map).insert(def.name, tool);
}
self.update_cached_tool_prompt();
Ok(self)
}
/// Injects a custom parser to define how the LLM outputs tool-call requests.
pub fn with_tool_parser<P: ToolCallParser + 'static>(mut self, parser: P) -> Self {
self.tool_parser = Arc::new(parser);
self.update_cached_tool_prompt();
self
}
/// Syntactic sugar: configures a simple tag-based tool call parser.
/// Replaces the default `[TOOL_CALL]` / `[/TOOL_CALL]` tags with custom ones.
pub fn with_tool_tags(mut self, start_tag: &str, end_tag: &str) -> Self {
use crate::agent::tool::TagToolParser;
self.tool_parser = Arc::new(TagToolParser::new(start_tag, end_tag));
self.update_cached_tool_prompt();
self
}
fn update_cached_tool_prompt(&mut self) {
if self.tools_def.is_empty() {
self.cached_tool_prompt = Arc::new(String::new());
} else {
let tools_json = serde_json::to_string(&*self.tools_def).unwrap_or_default();
self.cached_tool_prompt = Arc::new(self.tool_parser.format_instruction(&tools_json));
}
}
// --- Processors & Lifecycle Hooks ---
/// Injects a custom stream formatter factory to manipulate raw LLM output text on the fly.
#[cfg(not(target_arch = "wasm32"))]
pub fn with_stream_formatter<F>(mut self, factory: F) -> Self
where
F: Fn() -> Box<dyn StreamFormatter + Send + Sync> + Send + Sync + 'static,
{
self.formatter_factory = Arc::new(factory);
self
}
/// Injects a custom stream formatter factory to manipulate raw LLM output text on the fly.
#[cfg(target_arch = "wasm32")]
pub fn with_stream_formatter<F>(mut self, factory: F) -> Self
where
F: Fn() -> Box<dyn StreamFormatter> + 'static,
{
self.formatter_factory = Arc::new(factory);
self
}
/// Syntactic sugar: applies the framework's `StandardStreamFormatter`.
/// Automatically intercepts tool call syntaxes and formats reasoning (think) blocks cleanly.
pub fn with_standard_formatting(mut self) -> Self {
let (tool_start, tool_end) = self.tool_parser.get_tags();
let think_start = self.config.template.think_prefix.clone();
let think_end = self.config.template.think_suffix.clone();
self.formatter_factory = Arc::new(move || {
Box::new(StandardStreamFormatter::new(
&tool_start,
&tool_end,
&think_start,
&think_end,
))
});
self
}
/// Registers a callback that triggers whenever conversation history is evicted
/// due to token capacity limits.
///
/// **⚠️ Performance Warning:**
/// This callback executes synchronously while holding the `AgentState` write lock.
/// Do NOT perform blocking I/O (like writing to a database or file) directly inside this closure.
/// Instead, extract the required handles (e.g., from `state.extensions()`) and `tokio::spawn`
/// an asynchronous task to handle the evicted messages.
#[cfg(not(target_arch = "wasm32"))]
pub fn on_evict<F>(mut self, handler: F) -> Self
where
F: Fn(&AgentState, Vec<Arc<Message>>) + Send + Sync + 'static,
{
self.on_evict_handler = Some(Arc::new(handler));
self
}
/// Registers a callback that triggers whenever conversation history is evicted.
/// (See native documentation for performance warnings regarding blocking operations).
#[cfg(target_arch = "wasm32")]
pub fn on_evict<F>(mut self, handler: F) -> Self
where
F: Fn(&AgentState, Vec<Arc<Message>>) + 'static,
{
self.on_evict_handler = Some(Arc::new(handler));
self
}
}