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
//! molo — an AI agent framework.
//!
//! molo = **Mo**del **Lo**op: a lightweight Rust agent framework.
//!
//! # Quick Start
//!
//! A minimal agent needs only three things: a [`Provider`] to talk to the
//! LLM, a [`Memory`] to manage context, and a [`Tool`] for external
//! capabilities; the reasoning loop is built into [`ReActAgent`], and the
//! [`react_agent!`] macro assembles everything in one go. To self-test the
//! loop, use [`FakeProvider`] to inject scripted replies without depending
//! on a real API:
//!
//! ```rust
//! # #[tokio::main]
//! # async fn main() -> Result<(), molo::AgentError> {
//! use molo::{react_agent, Agent, FakeProvider, FakeReply};
//!
//! let mut agent = react_agent!(
//! FakeProvider::new([FakeReply::Text("Hello".into())]),
//! "You are a helpful assistant",
//! );
//! let answer = agent.run("Are you there?").await?;
//! assert_eq!(answer, "Hello");
//! # Ok(())
//! # }
//! ```
//!
//! For a real LLM, swap [`FakeProvider`] for [`OpenAiProvider`]; for retry
//! and timeout protection, wrap it in [`RetryProvider`]; for interaction
//! with the outside world (human approval, agent-to-agent conversation),
//! attach a [`MessageChannel`]; to observe the reasoning process, attach an
//! [`EventChannel`].
//!
//! # Component Overview
//!
//! Code is organized into domain modules, one concept per module; the top
//! level re-exports each module's core items, so `use molo::...` covers
//! most cases without digging into module paths:
//!
//! - [`agent`] — the agent interface and reasoning loop — [`Agent`] /
//! [`CancellableAgent`] / [`AgentError`], typed output via [`TypedAgent`]
//! with the validator [`StructuredValidator`], the [`ReActAgent`]
//! assembly with the [`react_agent!`] macro, sub-agent parts
//! ([`SubAgentTool`](crate::agent::SubAgentTool) /
//! [`SubAgentPool`](crate::agent::SubAgentPool)), streaming output chunks
//! and run summaries ([`MessageChunk`] / [`RunSummary`]);
//! - [`provider`] — LLM communication — the [`Provider`] interface,
//! request / response / usage models ([`ChatRequest`] / [`ChatResponse`] /
//! [`Usage`]), and implementations [`OpenAiProvider`] / [`RetryProvider`] /
//! [`FakeProvider`];
//! - [`tool`](mod@crate::tool): external capabilities — the [`Tool`]
//! interface and tool definitions ([`ToolSchema`]), registration and
//! execution ([`ToolRegistry`]), cross-tool shared state
//! ([`SharedState`]), and the procedural macro for one-shot tool
//! definitions [`tool`](macro@molo::tool);
//! - [`skill`] — skills — capability packages following the Agent Skills
//! open protocol ([`Skill`] parsing / validation, [`SkillRegistry`]
//! discovery and hot-swapping, progressive disclosure loading via
//! [`LoadSkillTool`]);
//! - [`mcp`] — MCP client adapter — wiring tools exposed by external MCP
//! servers into molo ([`McpClient`] connects and pulls, [`McpTool`]
//! adapts tools, [`McpError`]);
//! - [`memory`] — context management — the [`Memory`] interface and
//! implementations [`InMemoryMemory`] / [`WindowMemory`] (trims the
//! oldest turns by token budget), summary compression
//! [`SummarizeStrategy`] — old messages over budget are compressed into a
//! single summary;
//! - [`message`] — the conversation message model — [`Message`] /
//! [`ContentBlock`] / [`ToolCall`];
//! - [`message_channel`] — channels for external conversation —
//! [`CliMessageChannel`] and three more implementations (request-reply /
//! one-way notification);
//! - [`event_channel`] — observation channels — subscribe to the event
//! stream of an agent run ([`AgentEvent`] payloads).
//!
//! # Choosing Between Implementations
//!
//! When several implementations share a responsibility, pick per scenario:
//!
//! - **Context**: short sessions or unbounded needs use
//! [`InMemoryMemory`]; long sessions that must stay within budget use
//! [`WindowMemory`];
//! - **Talking to the LLM**: for development, inject scripted replies with
//! [`FakeProvider`], no real API needed; for production use
//! [`OpenAiProvider`], wrapped in [`RetryProvider`] for retry / timeout
//! protection;
//! - **External conversation**: use [`CliMessageChannel`] for
//! human-terminal interaction, [`MpscChannel`] for one-to-one in-process
//! agent conversation, [`BroadcastChannel`] / [`WatchChannel`] for
//! one-to-many broadcast / latest-value change notifications;
//! - **Observing the reasoning process**: subscribe to agent events via
//! [`BroadcastEventChannel`] (multiple subscribers; slow ones drop the
//! oldest events) or [`MpscEventChannel`] (single subscriber; nothing is
//! dropped within capacity).
//!
//! # Notes
//!
//! - molo targets the tokio ecosystem: all async APIs require a tokio
//! runtime; the library ships no runtime of its own, so callers bring
//! their own (examples uniformly use `#[tokio::main]`);
//! - cancellation is cooperative and opt-in: agents that support it
//! implement [`CancellableAgent`]; each run carries a
//! [`CancellationToken`] that any holder may request, and the loop
//! responds at safe points;
//! - tool execution failure does not abort the reasoning loop: the error
//! text is passed back to the model, which decides what to do next.
// `extern crate self`: lets macro-expanded code (which hardcodes `::molo::`
// paths) and in-crate tests refer to this crate as `molo::`.
extern crate self as molo;
// Re-export the procedural macro and helper trait: code expanded by
// `#[molo::tool]` references `::molo::tool` and `::molo::async_trait`,
// which transitive dependencies cannot resolve from user crates, so they
// are routed through this crate's root.
pub use async_trait;
pub use tool;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
// Cooperative cancellation primitive (a standard tokio-util component):
// `CancellableAgent::run_cancellable` / `run_stream_cancellable` use it as
// the cancellation source for each run; also re-exported for transitive
// dependencies (tokio-util, licensed MIT OR Apache-2.0).
pub use CancellationToken;