agent-sdk 0.9.2

Rust Agent SDK for building LLM agents
Documentation
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
use crate::authority::EventAuthority;
use crate::context::{CompactionConfig, ContextCompactor};
use crate::hooks::{AgentHooks, DefaultHooks};
use crate::llm::LlmProvider;
#[cfg(feature = "skills")]
use crate::skills::Skill;
use crate::stores::{EventStore, 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>,
    event_store: Option<Arc<dyn EventStore>>,
    event_authority: Option<Arc<dyn EventAuthority>>,
    config: Option<AgentConfig>,
    compaction_config: Option<CompactionConfig>,
    compactor: Option<Arc<dyn ContextCompactor>>,
    execution_store: Option<Arc<dyn ToolExecutionStore>>,
    audit_sink: Option<Arc<dyn crate::hooks::ToolAuditSink>>,
    #[cfg(feature = "otel")]
    observability_store: Option<Arc<dyn crate::observability::ObservabilityStore>>,
}

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,
            event_store: None,
            event_authority: None,
            config: None,
            compaction_config: None,
            compactor: None,
            execution_store: None,
            audit_sink: None,
            #[cfg(feature = "otel")]
            observability_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,
            event_store: self.event_store,
            event_authority: self.event_authority,
            config: self.config,
            compaction_config: self.compaction_config,
            compactor: self.compactor,
            execution_store: self.execution_store,
            audit_sink: self.audit_sink,
            #[cfg(feature = "otel")]
            observability_store: self.observability_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,
            event_store: self.event_store,
            event_authority: self.event_authority,
            config: self.config,
            compaction_config: self.compaction_config,
            compactor: self.compactor,
            execution_store: self.execution_store,
            audit_sink: self.audit_sink,
            #[cfg(feature = "otel")]
            observability_store: self.observability_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,
            event_store: self.event_store,
            event_authority: self.event_authority,
            config: self.config,
            compaction_config: self.compaction_config,
            compactor: self.compactor,
            execution_store: self.execution_store,
            audit_sink: self.audit_sink,
            #[cfg(feature = "otel")]
            observability_store: self.observability_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),
            event_store: self.event_store,
            event_authority: self.event_authority,
            config: self.config,
            compaction_config: self.compaction_config,
            compactor: self.compactor,
            execution_store: self.execution_store,
            audit_sink: self.audit_sink,
            #[cfg(feature = "otel")]
            observability_store: self.observability_store,
        }
    }

    /// Set the authoritative event store for the loop lifecycle.
    #[must_use]
    pub fn event_store(mut self, store: Arc<dyn EventStore>) -> Self {
        self.event_store = Some(store);
        self
    }

    /// Set the event authority for envelope creation.
    ///
    /// When set, the authority governs how events are wrapped in envelopes
    /// (sequence numbers, event IDs, timestamps).  In server mode the
    /// authority seeds sequences from durable storage so ordering is
    /// continuous across turns within a thread.
    ///
    /// When not set, a fresh [`LocalEventAuthority`](crate::authority::LocalEventAuthority)
    /// starting at sequence 0 is created for each run.
    #[must_use]
    pub fn event_authority(mut self, authority: Arc<dyn EventAuthority>) -> Self {
        self.event_authority = Some(authority);
        self
    }

    /// 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 execution store from a shared `Arc`.
    ///
    /// Use this when the caller needs to retain a handle to the store
    /// (for inspection, pre-population, or sharing across loops). See
    /// [`Self::execution_store`] for the standard owned form.
    #[must_use]
    pub fn execution_store_shared(mut self, store: Arc<dyn ToolExecutionStore>) -> Self {
        self.execution_store = Some(store);
        self
    }

    /// Set the authoritative tool audit sink.
    ///
    /// When set, the agent loop emits a
    /// [`ToolAuditRecord`](crate::advanced::ToolAuditRecord) at every tool
    /// lifecycle transition — blocked, requires-confirmation, cached,
    /// replayed, invalidated, completed, and persistence-failed. This
    /// gives servers a complete audit trail without relying on the weaker
    /// `post_tool_use` hook.
    ///
    /// Defaults to [`NoopAuditSink`](crate::hooks::NoopAuditSink) when
    /// not set.
    #[must_use]
    pub fn audit_sink(mut self, sink: impl crate::hooks::ToolAuditSink + 'static) -> Self {
        self.audit_sink = Some(Arc::new(sink));
        self
    }

    /// Set the audit sink from a shared `Arc`.
    ///
    /// Use this when the caller needs to retain a handle to the sink
    /// (e.g. to inspect captured records from tests, or to share a
    /// single durable sink across multiple agent loops). Passing an
    /// `Arc<dyn ToolAuditSink>` here avoids the `Arc<Arc<S>>` double
    /// wrap that happens when callers `Arc::clone(&sink)` a sink they
    /// already wrapped and hand it to [`Self::audit_sink`].
    ///
    /// See [`Self::audit_sink`] for the standard owned form.
    #[must_use]
    pub fn audit_sink_shared(mut self, sink: Arc<dyn crate::hooks::ToolAuditSink>) -> Self {
        self.audit_sink = Some(sink);
        self
    }

    /// Set the observability store for `GenAI` payload capture.
    #[cfg(feature = "otel")]
    #[must_use]
    pub fn observability_store(
        mut self,
        store: impl crate::observability::ObservabilityStore + 'static,
    ) -> Self {
        self.observability_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.
    ///
    /// Available when the `skills` feature is enabled.
    ///
    /// # 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();
    /// ```
    #[cfg(feature = "skills")]
    #[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 message/state stores.
    ///
    /// This is a convenience method that uses:
    /// - `DefaultHooks` for hooks
    /// - `InMemoryStore` for message store
    /// - `InMemoryStore` for state store
    /// - `InMemoryEventStore` for the event store, when none was set
    /// - `AgentConfig::default()` if no config is set
    ///
    /// Supplying an [`event_store`](Self::event_store) is optional for this
    /// convenience build — a fresh [`InMemoryEventStore`](crate::InMemoryEventStore)
    /// is used by default so the 30-second path needs no `Arc` ceremony. Wire
    /// a durable store explicitly when you need persistence across process
    /// restarts.
    ///
    /// # Panics
    ///
    /// Panics if a provider has not been set.
    #[must_use]
    pub fn build(self) -> AgentLoop<Ctx, P, DefaultHooks, InMemoryStore, InMemoryStore> {
        let Some(provider) = self.provider else {
            panic!("provider is required");
        };
        let event_store = self
            .event_store
            .unwrap_or_else(|| Arc::new(crate::stores::InMemoryEventStore::new()));
        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()),
            event_store,
            event_authority: self.event_authority,
            config,
            compaction_config: self.compaction_config,
            compactor: self.compactor,
            execution_store: self.execution_store,
            audit_sink: self
                .audit_sink
                .unwrap_or_else(|| Arc::new(crate::hooks::NoopAuditSink)),
            #[cfg(feature = "otel")]
            observability_store: self.observability_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`
    /// - `event_store`
    #[must_use]
    pub fn build_with_stores(self) -> AgentLoop<Ctx, P, H, M, S> {
        let Some(provider) = self.provider else {
            panic!("provider is required");
        };
        let tools = self.tools.unwrap_or_default();
        let Some(hooks) = self.hooks else {
            panic!("hooks is required when using build_with_stores");
        };
        let Some(message_store) = self.message_store else {
            panic!("message_store is required when using build_with_stores");
        };
        let Some(state_store) = self.state_store else {
            panic!("state_store is required when using build_with_stores");
        };
        let Some(event_store) = self.event_store else {
            panic!("event_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),
            event_store,
            event_authority: self.event_authority,
            config,
            compaction_config: self.compaction_config,
            compactor: self.compactor,
            execution_store: self.execution_store,
            audit_sink: self
                .audit_sink
                .unwrap_or_else(|| Arc::new(crate::hooks::NoopAuditSink)),
            #[cfg(feature = "otel")]
            observability_store: self.observability_store,
        }
    }
}