ailoy 0.3.0

Agents that drive a language model through tool-augmented turns, in a virtx console.
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
use std::sync::Arc;

use tokio::sync::Mutex;
use virtx::console::ConsoleClient;

use crate::{
    agent::{Agent, AgentSpec, AgentState, ContextManager},
    memory::Memory,
    message::Message,
    tool::{ToolDesc, WebSearchEngineKind},
};

/// Fluent builder over [`AgentSpec`] for [`Agent`].
///
/// For assembling an agent inline; with a fully-formed [`AgentSpec`], call
/// [`Agent::try_new`] / [`Agent::try_with_provider`] directly.
///
/// # Examples
///
/// ```rust,no_run
/// # use ailoy::{
/// #     agent::AgentBuilder,
/// #     tool::ToolDescBuilder,
/// #     to_value,
/// # };
/// # #[tokio::main]
/// # async fn main() -> anyhow::Result<()> {
/// // Uses the `"default"` agent-provider bundle (env-driven lang models + built-in
/// // tools). Register others via `get_lm_providers_mut()` / `get_tool_providers_mut()` /
/// // `get_agent_providers_mut()` and select them with [`AgentBuilder::agent_provider`].
/// let agent = AgentBuilder::new("openai/gpt-4o")
///     .tool(ToolDescBuilder::new("web_search")
///         .description("Search the web.")
///         .parameters(to_value!({ "type": "object", "properties": {} }))
///         .build()
///     )
///     .build()
///     .await?;
/// #   Ok(())
/// # }
/// ```
pub struct AgentBuilder {
    spec: AgentSpec,

    /// [`AgentProvider`](crate::agent::AgentProvider) name resolved at [`build`](Self::build).
    agent_provider: String,

    history: Vec<Message>,

    console: Option<Arc<Mutex<Option<ConsoleClient>>>>,

    memory: Option<Memory>,

    context_manager: Option<ContextManager>,
}

impl AgentBuilder {
    /// Create a builder for `model` (e.g. `"openai/gpt-4o"`), which the selected
    /// [`AgentProvider`](crate::agent::AgentProvider) must resolve at [`build`](Self::build) time.
    pub fn new(model: impl Into<String>) -> Self {
        let spec = AgentSpec::new(model);
        Self {
            spec,
            agent_provider: "default".to_string(),
            history: Vec::new(),
            console: None,
            memory: None,
            context_manager: None,
        }
    }

    /// Select the [`AgentProvider`](crate::agent::AgentProvider) bundle (default
    /// `"default"`). `name` must be registered in
    /// [`get_agent_providers`](crate::agent::get_agent_providers) by [`build`](Self::build) time.
    pub fn agent_provider(mut self, name: impl Into<String>) -> Self {
        self.agent_provider = name.into();
        self
    }

    /// Set the system instruction stored on the spec.
    pub fn instruction(mut self, inst: impl Into<String>) -> Self {
        self.spec = self.spec.instruction(inst);
        self
    }

    pub fn tool(mut self, desc: ToolDesc) -> Self {
        self.spec.tools.push(desc);
        self
    }

    pub fn tools(mut self, desc: impl IntoIterator<Item = ToolDesc>) -> Self {
        let mut desc = desc.into_iter().collect();
        self.spec.tools.append(&mut desc);
        self
    }

    /// Append the canonical local-execution toolset.
    /// See [`AgentSpec::system_tools`] for the per-family tool selection.
    pub fn system_tools(mut self) -> Self {
        self.spec = self.spec.system_tools();
        self
    }

    pub fn shell_tool(mut self) -> Self {
        self.spec = self.spec.shell_tool();
        self
    }

    pub fn web_search_tool(mut self, engines: Vec<WebSearchEngineKind>) -> Self {
        self.spec = self.spec.web_search_tool(engines);
        self
    }

    pub fn web_fetch_tool(mut self) -> Self {
        self.spec = self.spec.web_fetch_tool();
        self
    }

    /// Append a sub-agent spec, registered as a callable tool at [`build`](Self::build)
    /// time and sharing the parent's console. It must carry an
    /// [`AgentCard`](crate::agent::AgentCard).
    pub fn subagent(mut self, spec: AgentSpec) -> Self {
        self.spec.subagents.push(spec);
        self
    }

    /// Seed the agent's [`AgentState::history`] (e.g. for resuming a prior session).
    /// A leading system message here overrides the one the spec's instruction would
    /// produce; otherwise the instruction is still seeded, at the front of this history.
    pub fn history(mut self, history: impl IntoIterator<Item = Message>) -> Self {
        self.history = history.into_iter().collect();
        self
    }

    /// Run this agent's console tools in `console`. It need not be started: the first
    /// command that needs a booted session boots it.
    ///
    /// Nothing builds a console implicitly, since that means choosing a console server.
    /// Without one, pure tools still run and console tools fail with an error.
    pub fn console(mut self, console: ConsoleClient) -> Self {
        self.console = Some(Arc::new(Mutex::new(Some(console))));
        self
    }

    /// Share a console slot with another `Agent` built elsewhere.
    pub fn shared_console(mut self, console: Arc<Mutex<Option<ConsoleClient>>>) -> Self {
        self.console = Some(console);
        self
    }

    /// Let this agent remember into `memory`.
    ///
    /// Adds the `mem_search` and `mem_insert` tools; they are not spec tools because the
    /// store is a per-agent value, not a name in the [`ToolProvider`](crate::tool::ToolProvider).
    ///
    /// The store must already exist (`mem init`); a missing file is reported by the first
    /// memory tool call, not by [`build`](Self::build). Memory tools run on the
    /// [`console`](Self::console), so one is required too.
    pub fn memory(mut self, memory: impl Into<Memory>) -> Self {
        self.memory = Some(memory.into());
        self
    }

    /// Give this agent the skill in `dir`. See [`AgentSpec::skill`].
    pub fn skill(mut self, dir: impl Into<String>) -> Self {
        self.spec = self.spec.skill(dir);
        self
    }

    /// Set the context window management spec.
    pub fn context_manager(mut self, spec: ContextManager) -> Self {
        self.context_manager = Some(spec);
        self
    }

    /// The most tokens one reply may have, forwarded to the language model on every call.
    pub fn max_tokens(mut self, max_tokens: u64) -> Self {
        self.spec = self.spec.max_tokens(max_tokens);
        self
    }

    /// Sampling temperature forwarded to the language model on every call.
    pub fn temperature(mut self, temperature: f64) -> Self {
        self.spec = self.spec.temperature(temperature);
        self
    }

    pub fn top_p(mut self, top_p: f64) -> Self {
        self.spec = self.spec.top_p(top_p);
        self
    }

    pub fn top_k(mut self, top_k: u64) -> Self {
        self.spec = self.spec.top_k(top_k);
        self
    }

    pub fn response_format(mut self, fmt: crate::lang_model::ResponseFormat) -> Self {
        self.spec = self.spec.response_format(fmt);
        self
    }

    /// Turn on the model's thinking at `effort`, forwarded to the language model on every call.
    pub fn reasoning(mut self, effort: crate::lang_model::ReasoningEffort) -> Self {
        self.spec = self.spec.reasoning(effort);
        self
    }

    /// Materialise the agent via [`Agent::try_with_provider_and_state`], which is async
    /// because it reads the skills through the console.
    pub async fn build(self) -> anyhow::Result<Agent> {
        let Self {
            spec,
            agent_provider,
            history,
            console,
            memory,
            context_manager,
        } = self;

        let mut state = AgentState::new();
        if let Some(c) = console {
            state = state.with_console_slot(c);
        }
        if let Some(m) = memory {
            state = state.with_memory(m);
        }
        if !history.is_empty() {
            state = state.with_history(history);
        }

        let mut agent = Agent::try_with_provider_and_state(spec, &agent_provider, state).await?;
        if context_manager.is_some() {
            agent.set_context_manager(context_manager);
        }
        Ok(agent)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        agent::{AgentCard, AgentProvider, get_agent_providers_mut},
        lang_model::{LangModelProvider, get_lm_providers_mut},
        message::Role,
        test_console,
    };

    const TEST_MODEL: &str = "openai/gpt-4o-mini";
    const TEST_PROVIDER_NAME: &str = "agent_builder_tests";

    /// Register a dummy lang-model provider and an `AgentProvider` pointing at it. Idempotent.
    fn ensure_dummy_provider() {
        let mut lmps = get_lm_providers_mut();
        if !lmps.contains_key(TEST_PROVIDER_NAME) {
            let mut lmp = LangModelProvider::new();
            lmp.insert(TEST_MODEL.into(), LangModelProvider::openai("dummy".into()));
            lmps.insert(TEST_PROVIDER_NAME.to_string(), lmp);
        }
        drop(lmps);
        let mut aps = get_agent_providers_mut();
        aps.entry(TEST_PROVIDER_NAME.to_string())
            .or_insert_with(|| AgentProvider::new(TEST_PROVIDER_NAME, "default"));
    }

    fn system_text(agent: &Agent) -> Option<String> {
        let history = agent.get_history();
        let m = history.first()?;
        if m.role != Role::System {
            return None;
        }
        m.contents
            .iter()
            .find_map(|p| p.as_text())
            .map(str::to_string)
    }

    #[tokio::test]
    async fn test_simple_builder() {
        ensure_dummy_provider();
        let agent = AgentBuilder::new(TEST_MODEL)
            .agent_provider(TEST_PROVIDER_NAME)
            .instruction("You are a test agent.")
            .build()
            .await
            .unwrap();

        let history = agent.get_history();
        assert_eq!(history.len(), 1);
        assert_eq!(history[0].role, Role::System);
    }

    #[tokio::test]
    async fn test_builder_no_instruction() {
        ensure_dummy_provider();
        let agent = AgentBuilder::new(TEST_MODEL)
            .agent_provider(TEST_PROVIDER_NAME)
            .build()
            .await
            .unwrap();
        assert!(agent.get_history().is_empty());
    }

    /// `console()` puts the supplied console into `state.console`.
    #[tokio::test]
    async fn test_builder_console_is_applied() {
        ensure_dummy_provider();
        let agent = AgentBuilder::new(TEST_MODEL)
            .agent_provider(TEST_PROVIDER_NAME)
            .console(test_console().await)
            .build()
            .await
            .unwrap();

        let mut guard = agent.state.console.lock().await;
        let console = guard.as_mut().expect("the supplied console is in the slot");
        let result = console
            .exec(["sh", "-c", "echo ok"], None)
            .await
            .expect("exec failed");
        assert_eq!(result.stdout, b"ok\n");
    }

    #[tokio::test]
    async fn test_builder_subagent_in_spec() {
        use crate::agent::AgentSpec;

        ensure_dummy_provider();
        let sub_spec = AgentSpec::new(TEST_MODEL).card(AgentCard {
            name: "child".into(),
            description: "child agent".into(),
            skills: vec![],
        });

        AgentBuilder::new(TEST_MODEL)
            .agent_provider(TEST_PROVIDER_NAME)
            .subagent(sub_spec)
            .build()
            .await
            .unwrap();
    }

    #[tokio::test]
    async fn test_builder_context_manager_is_applied() {
        ensure_dummy_provider();
        let cm = ContextManager {
            max_input_tokens: 10_000,
            preserve_recent_turns: 2,
        };
        let agent = AgentBuilder::new(TEST_MODEL)
            .agent_provider(TEST_PROVIDER_NAME)
            .context_manager(cm)
            .build()
            .await
            .unwrap();
        assert!(agent.get_context_manager().is_some());
    }

    fn msg(role: Role, text: &str) -> Message {
        Message::new(role).with_contents([crate::message::Part::text(text)])
    }

    #[tokio::test]
    async fn test_instruction_seeded_into_history_without_system_message() {
        ensure_dummy_provider();
        let agent = AgentBuilder::new(TEST_MODEL)
            .agent_provider(TEST_PROVIDER_NAME)
            .instruction("You are a test agent.")
            .history([msg(Role::User, "hello"), msg(Role::Assistant, "hi")])
            .build()
            .await
            .unwrap();

        let history = agent.get_history();
        assert_eq!(history.len(), 3);
        assert_eq!(
            system_text(&agent).as_deref(),
            Some("You are a test agent.")
        );
        assert_eq!(history[1].role, Role::User);
        assert_eq!(history[2].role, Role::Assistant);
    }

    #[tokio::test]
    async fn test_existing_system_message_is_not_replaced() {
        ensure_dummy_provider();
        let agent = AgentBuilder::new(TEST_MODEL)
            .agent_provider(TEST_PROVIDER_NAME)
            .instruction("spec instruction")
            .history([
                msg(Role::System, "stored instruction"),
                msg(Role::User, "hello"),
            ])
            .build()
            .await
            .unwrap();

        let history = agent.get_history();
        assert_eq!(history.len(), 2);
        assert_eq!(system_text(&agent).as_deref(), Some("stored instruction"));
    }

    /// `memory()` lands on the state, which decides whether the agent gets memory tools.
    #[tokio::test]
    async fn test_builder_memory_is_applied() {
        use crate::memory::Memory;

        ensure_dummy_provider();
        let agent = AgentBuilder::new(TEST_MODEL)
            .agent_provider(TEST_PROVIDER_NAME)
            .memory(Memory::new("/work/notes.sqlite"))
            .build()
            .await
            .unwrap();

        assert_eq!(
            agent.state.memory,
            Some(Memory::new("/work/notes.sqlite")),
            "the memory the builder was given is the one on the state"
        );
    }

    /// A path converts into the equivalent `Memory`.
    #[tokio::test]
    async fn test_builder_memory_takes_a_path() {
        use crate::memory::Memory;

        ensure_dummy_provider();
        let agent = AgentBuilder::new(TEST_MODEL)
            .agent_provider(TEST_PROVIDER_NAME)
            .memory(std::path::Path::new("/work/notes.sqlite"))
            .build()
            .await
            .unwrap();

        assert_eq!(agent.state.memory, Some(Memory::new("/work/notes.sqlite")));
    }
}