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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
use crate::context::{CompactionConfig, ContextCompactor};
use crate::hooks::{AgentHooks, DefaultHooks};
use crate::llm::LlmProvider;
use crate::skills::Skill;
use crate::stores::{InMemoryStore, MessageStore, StateStore, ToolExecutionStore};
use crate::tools::ToolRegistry;
use crate::types::AgentConfig;
use std::sync::Arc;
use super::AgentLoop;
/// Builder for constructing an `AgentLoop`.
///
/// # Example
///
/// ```ignore
/// let agent = AgentLoop::builder()
/// .provider(my_provider)
/// .tools(my_tools)
/// .config(AgentConfig::default())
/// .build();
/// ```
pub struct AgentLoopBuilder<Ctx, P, H, M, S> {
provider: Option<P>,
tools: Option<ToolRegistry<Ctx>>,
hooks: Option<H>,
message_store: Option<M>,
state_store: Option<S>,
config: Option<AgentConfig>,
compaction_config: Option<CompactionConfig>,
compactor: Option<Arc<dyn ContextCompactor>>,
execution_store: Option<Arc<dyn ToolExecutionStore>>,
}
impl<Ctx> AgentLoopBuilder<Ctx, (), (), (), ()> {
/// Create a new builder with no components set.
#[must_use]
pub fn new() -> Self {
Self {
provider: None,
tools: None,
hooks: None,
message_store: None,
state_store: None,
config: None,
compaction_config: None,
compactor: None,
execution_store: None,
}
}
}
impl<Ctx> Default for AgentLoopBuilder<Ctx, (), (), (), ()> {
fn default() -> Self {
Self::new()
}
}
impl<Ctx, P, H, M, S> AgentLoopBuilder<Ctx, P, H, M, S> {
/// Set the LLM provider.
#[must_use]
pub fn provider<P2: LlmProvider>(self, provider: P2) -> AgentLoopBuilder<Ctx, P2, H, M, S> {
AgentLoopBuilder {
provider: Some(provider),
tools: self.tools,
hooks: self.hooks,
message_store: self.message_store,
state_store: self.state_store,
config: self.config,
compaction_config: self.compaction_config,
compactor: self.compactor,
execution_store: self.execution_store,
}
}
/// Set the tool registry.
#[must_use]
pub fn tools(mut self, tools: ToolRegistry<Ctx>) -> Self {
self.tools = Some(tools);
self
}
/// Set the agent hooks.
#[must_use]
pub fn hooks<H2: AgentHooks>(self, hooks: H2) -> AgentLoopBuilder<Ctx, P, H2, M, S> {
AgentLoopBuilder {
provider: self.provider,
tools: self.tools,
hooks: Some(hooks),
message_store: self.message_store,
state_store: self.state_store,
config: self.config,
compaction_config: self.compaction_config,
compactor: self.compactor,
execution_store: self.execution_store,
}
}
/// Set the message store.
#[must_use]
pub fn message_store<M2: MessageStore>(
self,
message_store: M2,
) -> AgentLoopBuilder<Ctx, P, H, M2, S> {
AgentLoopBuilder {
provider: self.provider,
tools: self.tools,
hooks: self.hooks,
message_store: Some(message_store),
state_store: self.state_store,
config: self.config,
compaction_config: self.compaction_config,
compactor: self.compactor,
execution_store: self.execution_store,
}
}
/// Set the state store.
#[must_use]
pub fn state_store<S2: StateStore>(
self,
state_store: S2,
) -> AgentLoopBuilder<Ctx, P, H, M, S2> {
AgentLoopBuilder {
provider: self.provider,
tools: self.tools,
hooks: self.hooks,
message_store: self.message_store,
state_store: Some(state_store),
config: self.config,
compaction_config: self.compaction_config,
compactor: self.compactor,
execution_store: self.execution_store,
}
}
/// Set the execution store for tool idempotency.
///
/// When set, tool executions will be tracked using a write-ahead pattern:
/// 1. Record execution intent BEFORE calling the tool
/// 2. Update with result AFTER completion
/// 3. On retry, return cached result if execution already completed
///
/// # Example
///
/// ```ignore
/// use agent_sdk::{builder, stores::InMemoryExecutionStore};
///
/// let agent = builder()
/// .provider(my_provider)
/// .execution_store(InMemoryExecutionStore::new())
/// .build();
/// ```
#[must_use]
pub fn execution_store(mut self, store: impl ToolExecutionStore + 'static) -> Self {
self.execution_store = Some(Arc::new(store));
self
}
/// Set the agent configuration.
#[must_use]
pub fn config(mut self, config: AgentConfig) -> Self {
self.config = Some(config);
self
}
/// Enable context compaction with the given configuration.
///
/// When enabled, the agent will automatically compact conversation history
/// when it exceeds the configured token threshold.
///
/// # Example
///
/// ```ignore
/// use agent_sdk::{builder, context::CompactionConfig};
///
/// let agent = builder()
/// .provider(my_provider)
/// .with_compaction(CompactionConfig::default())
/// .build();
/// ```
#[must_use]
pub const fn with_compaction(mut self, config: CompactionConfig) -> Self {
self.compaction_config = Some(config);
self
}
/// Enable context compaction with default settings.
///
/// This is a convenience method equivalent to:
/// ```ignore
/// builder.with_compaction(CompactionConfig::default())
/// ```
#[must_use]
pub fn with_auto_compaction(self) -> Self {
self.with_compaction(CompactionConfig::default())
}
/// Override the default compactor with a custom implementation.
#[must_use]
pub fn with_custom_compactor(mut self, compactor: impl ContextCompactor + 'static) -> Self {
self.compactor = Some(Arc::new(compactor));
self
}
/// Apply a skill configuration.
///
/// This merges the skill's system prompt with the existing configuration
/// and filters tools based on the skill's allowed/denied lists.
///
/// # Example
///
/// ```ignore
/// let skill = Skill::new("code-review", "You are a code reviewer...")
/// .with_denied_tools(vec!["bash".into()]);
///
/// let agent = builder()
/// .provider(provider)
/// .tools(tools)
/// .with_skill(skill)
/// .build();
/// ```
#[must_use]
pub fn with_skill(mut self, skill: Skill) -> Self
where
Ctx: Send + Sync + 'static,
{
// Filter tools based on skill configuration first (before moving skill)
if let Some(ref mut tools) = self.tools {
tools.filter(|name| skill.is_tool_allowed(name));
}
// Merge system prompt
let mut config = self.config.take().unwrap_or_default();
if config.system_prompt.is_empty() {
config.system_prompt = skill.system_prompt;
} else {
config.system_prompt = format!("{}\n\n{}", config.system_prompt, skill.system_prompt);
}
self.config = Some(config);
self
}
}
impl<Ctx, P> AgentLoopBuilder<Ctx, P, (), (), ()>
where
Ctx: Send + Sync + 'static,
P: LlmProvider + 'static,
{
/// Build the agent loop with default hooks and in-memory stores.
///
/// This is a convenience method that uses:
/// - `DefaultHooks` for hooks
/// - `InMemoryStore` for message store
/// - `InMemoryStore` for state store
/// - `AgentConfig::default()` if no config is set
///
/// # Panics
///
/// Panics if a provider has not been set.
#[must_use]
pub fn build(self) -> AgentLoop<Ctx, P, DefaultHooks, InMemoryStore, InMemoryStore> {
let provider = self.provider.expect("provider is required");
let tools = self.tools.unwrap_or_default();
let config = self.config.unwrap_or_default();
AgentLoop {
provider: Arc::new(provider),
tools: Arc::new(tools),
hooks: Arc::new(DefaultHooks),
message_store: Arc::new(InMemoryStore::new()),
state_store: Arc::new(InMemoryStore::new()),
config,
compaction_config: self.compaction_config,
compactor: self.compactor,
execution_store: self.execution_store,
}
}
}
impl<Ctx, P, H, M, S> AgentLoopBuilder<Ctx, P, H, M, S>
where
Ctx: Send + Sync + 'static,
P: LlmProvider + 'static,
H: AgentHooks + 'static,
M: MessageStore + 'static,
S: StateStore + 'static,
{
/// Build the agent loop with all custom components.
///
/// # Panics
///
/// Panics if any of the following have not been set:
/// - `provider`
/// - `hooks`
/// - `message_store`
/// - `state_store`
#[must_use]
pub fn build_with_stores(self) -> AgentLoop<Ctx, P, H, M, S> {
let provider = self.provider.expect("provider is required");
let tools = self.tools.unwrap_or_default();
let hooks = self
.hooks
.expect("hooks is required when using build_with_stores");
let message_store = self
.message_store
.expect("message_store is required when using build_with_stores");
let state_store = self
.state_store
.expect("state_store is required when using build_with_stores");
let config = self.config.unwrap_or_default();
AgentLoop {
provider: Arc::new(provider),
tools: Arc::new(tools),
hooks: Arc::new(hooks),
message_store: Arc::new(message_store),
state_store: Arc::new(state_store),
config,
compaction_config: self.compaction_config,
compactor: self.compactor,
execution_store: self.execution_store,
}
}
}