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
mod builder;
pub(crate) mod control;
mod error;
pub(crate) mod handle;
mod hybrid_store;
mod intrinsic;
mod skill;
mod store;
pub(crate) mod task;
use std::{any::Any, path::Path, sync::Arc};
use crate::{
agent::{Agent, AgentConfig, AgentSpawnOptions, AgentStatus},
provider::{Provider, ProviderRegistry},
runtime::{builder::RuntimeBuilder, skill::SkillLoadError},
tool::ExecutableTool,
};
use mentra_provider::{BuiltinProvider, ModelInfo, ModelSelector, ProviderDescriptor, ProviderId};
pub use control::{
AuditHook, AuditLogHook, CancellationFlag, CancellationToken, CommandOutput, CommandRequest,
CommandSpec, ExecOutput, RunOptions, RuntimeExecutor, RuntimeHook, RuntimeHookEvent,
RuntimeHooks, RuntimePolicy, is_transient_provider_error, is_transient_runtime_error,
};
pub use error::RuntimeError;
pub(crate) use handle::RuntimeHandle;
pub use hybrid_store::HybridRuntimeStore;
pub(crate) use intrinsic::RuntimeIntrinsicTool;
pub use store::{
AgentStore, AuditStore, LeaseStore, RunStore, RuntimeStore, SqliteRuntimeStore, TaskStore,
};
pub(crate) use store::{LoadedAgentState, PersistedAgentRecord, TaskStateSnapshot};
pub(crate) use task::TaskIntrinsicTool;
pub use task::{TaskItem, TaskStatus};
/// Entry point for configuring providers, tools, and agent lifecycles.
///
/// A runtime composes four main subsystems:
/// - execution: providers, policies, hooks, and command execution
/// - persistence: agent state, runs, tasks, leases, and memory
/// - tooling: registered tools, skills, and app context
/// - collaboration: persistent teams and background task coordination
pub struct Runtime {
handle: RuntimeHandle,
provider_registry: ProviderRegistry,
}
/// Read-only summary of a persisted agent record for a runtime identifier.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PersistedAgentSummary {
pub id: String,
pub runtime_identifier: String,
pub name: String,
pub is_teammate: bool,
pub status: AgentStatus,
pub history_len: usize,
}
impl Runtime {
/// Returns a builder with Mentra's builtin tools enabled.
pub fn builder() -> RuntimeBuilder {
RuntimeBuilder::new(true)
}
/// Returns a builder with no builtin tools registered.
pub fn empty_builder() -> RuntimeBuilder {
RuntimeBuilder::new(false)
}
/// Registers a custom tool on the runtime after construction.
pub fn register_tool<T>(&self, tool: T)
where
T: ExecutableTool + 'static,
{
self.handle.register_tool(tool);
}
/// Registers typed application state that tools can retrieve from their context.
pub fn register_context(&self, context: Arc<dyn Any + Send + Sync>) {
self.handle.register_app_context(context);
}
/// Returns typed application state previously registered on this runtime.
pub fn app_context<T>(&self) -> Result<Arc<T>, String>
where
T: Any + Send + Sync + 'static,
{
self.handle.app_context::<T>()
}
/// Registers a skills directory and enables the builtin `load_skill` tool.
pub fn register_skills_dir(&self, path: impl AsRef<Path>) -> Result<(), SkillLoadError> {
self.handle
.register_skill_loader(skill::SkillLoader::from_dir(path)?);
Ok(())
}
/// Spawns a new agent with the default [`AgentConfig`].
pub fn spawn(&self, name: impl Into<String>, model: ModelInfo) -> Result<Agent, RuntimeError> {
self.spawn_with_config(name, model, AgentConfig::default())
}
/// Spawns a new agent with an explicit configuration.
pub fn spawn_with_config(
&self,
name: impl Into<String>,
model: ModelInfo,
config: AgentConfig,
) -> Result<Agent, RuntimeError> {
Agent::new(
self.handle.clone(),
model.id,
name.into(),
config,
self.provider_registry
.get_provider(Some(&model.provider))
.ok_or_else(|| RuntimeError::ProviderNotFound(Some(model.provider.clone())))?,
AgentSpawnOptions::default(),
)
}
/// Restores a previously persisted agent by identifier.
pub fn resume_agent(&self, agent_id: &str) -> Result<Agent, RuntimeError> {
let Some(state) = self.handle.store().load_agent(agent_id)? else {
return Err(RuntimeError::Store(format!(
"No persisted agent with id '{agent_id}'"
)));
};
let provider = self
.provider_registry
.get_provider(Some(&state.record.provider_id))
.ok_or_else(|| {
RuntimeError::ProviderNotFound(Some(state.record.provider_id.clone()))
})?;
Agent::from_loaded(self.handle.clone(), state, provider)
}
/// Restores every persisted agent that belongs to the provided runtime identifier.
pub fn resume(&self, runtime_identifier: &str) -> Result<Vec<Agent>, RuntimeError> {
let states = self
.handle
.store()
.list_agents_by_runtime(runtime_identifier)?;
let mut agents = Vec::new();
for state in states {
let provider = self
.provider_registry
.get_provider(Some(&state.record.provider_id))
.ok_or_else(|| {
RuntimeError::ProviderNotFound(Some(state.record.provider_id.clone()))
})?;
let agent = Agent::from_loaded(self.handle.clone(), state, provider)?;
if agent.is_teammate() {
agent.revive_teammate_actor()?;
} else {
agents.push(agent);
}
}
Ok(agents)
}
/// Lists persisted agents for a runtime identifier without reviving them.
pub fn list_persisted_agents(
&self,
runtime_identifier: &str,
) -> Result<Vec<PersistedAgentSummary>, RuntimeError> {
self.handle
.store()
.list_agents_by_runtime(runtime_identifier)
.map(|states| {
states
.into_iter()
.map(|state| PersistedAgentSummary {
id: state.record.id,
runtime_identifier: state.record.runtime_identifier,
name: state.record.name,
is_teammate: state.record.teammate_identity.is_some(),
status: state.record.status,
history_len: state.memory.transcript.len(),
})
.collect()
})
}
/// Restores every persisted agent known to the runtime store.
pub fn resume_all(&self) -> Result<Vec<Agent>, RuntimeError> {
let states = self.handle.store().list_agents()?;
let mut agents = Vec::new();
for state in states {
let provider = self
.provider_registry
.get_provider(Some(&state.record.provider_id))
.ok_or_else(|| {
RuntimeError::ProviderNotFound(Some(state.record.provider_id.clone()))
})?;
agents.push(Agent::from_loaded(self.handle.clone(), state, provider)?);
}
Ok(agents)
}
}
impl Runtime {
/// Returns descriptors for registered providers.
pub fn providers(&self) -> Vec<ProviderDescriptor> {
self.provider_registry.descriptors()
}
/// Registers a builtin provider from an API key.
pub fn register_provider(
&mut self,
id: BuiltinProvider,
api_key: impl Into<String>,
) -> Result<(), String> {
self.provider_registry
.register_builtin_provider(id, api_key)
}
/// Registers the local Ollama provider using its default OpenAI-compatible endpoint.
pub fn register_ollama(&mut self) {
self.provider_registry.register_ollama();
}
/// Registers the local LM Studio provider using its default OpenAI-compatible endpoint.
pub fn register_lmstudio(&mut self) {
self.provider_registry.register_lmstudio();
}
/// Registers a custom provider implementation.
///
/// This is the supported seam for injecting a scripted provider in tests or
/// embedding Mentra on top of a custom transport.
///
/// ```rust,no_run
/// use async_trait::async_trait;
/// use mentra::{BuiltinProvider, ModelInfo, ProviderDescriptor, Runtime};
/// use mentra::error::{ProviderError, RuntimeError};
/// use mentra::provider::{Provider, ProviderEventStream, Request};
/// use tokio::sync::mpsc;
///
/// struct TestProvider;
///
/// #[async_trait]
/// impl Provider for TestProvider {
/// fn descriptor(&self) -> ProviderDescriptor {
/// ProviderDescriptor::new(BuiltinProvider::Anthropic)
/// }
///
/// async fn list_models(&self) -> Result<Vec<ModelInfo>, ProviderError> {
/// Ok(vec![ModelInfo::new("test-model", BuiltinProvider::Anthropic)])
/// }
///
/// async fn stream(
/// &self,
/// _request: Request<'_>,
/// ) -> Result<ProviderEventStream, ProviderError> {
/// let (_tx, rx) = mpsc::unbounded_channel();
/// Ok(rx)
/// }
/// }
///
/// let mut runtime = Runtime::empty_builder()
/// .with_provider(BuiltinProvider::Anthropic, "placeholder")
/// .build()?;
/// runtime.register_provider_instance(TestProvider);
/// # Ok::<(), RuntimeError>(())
/// ```
pub fn register_provider_instance<P>(&mut self, provider: P)
where
P: Provider + 'static,
{
self.provider_registry.register_provider_instance(provider);
}
/// Lists models for a specific provider, or the default provider when omitted.
pub async fn list_models(
&self,
provider: Option<&ProviderId>,
) -> Result<Vec<ModelInfo>, RuntimeError> {
self.provider_registry
.get_provider(provider)
.ok_or_else(|| RuntimeError::ProviderNotFound(provider.cloned()))?
.list_models()
.await
.map_err(RuntimeError::FailedToListModels)
}
/// Resolves a model for a registered provider using a deterministic selection strategy.
pub async fn resolve_model(
&self,
provider: impl Into<ProviderId>,
selector: ModelSelector,
) -> Result<ModelInfo, RuntimeError> {
let provider = provider.into();
if self
.provider_registry
.get_provider(Some(&provider))
.is_none()
{
return Err(RuntimeError::ProviderNotFound(Some(provider)));
}
match selector {
ModelSelector::Id(id) => Ok(ModelInfo::new(id, provider)),
ModelSelector::NewestAvailable => {
let mut models = self.list_models(Some(&provider)).await?;
models.sort_by(|left, right| {
right
.created_at
.cmp(&left.created_at)
.then_with(|| left.id.cmp(&right.id))
});
models
.into_iter()
.next()
.ok_or(RuntimeError::NoModelsAvailable(provider))
}
}
}
}