Skip to main content

merry/
lib.rs

1//! Public Rust facade for Merry.
2//!
3//! This crate is the interface layer for applications embedding Merry. It
4//! exposes the high-level semantic agent API plus small components for runtime
5//! construction: providers, profiles, tools, lifecycle hooks, events, and
6//! stable errors. Lower-level crates still own their domains.
7//!
8//! # Context Windows And Lifecycle Hooks
9//!
10//! ```no_run
11//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
12//! use merry::{
13//!     AfterModelDecision, AgentBuilder, BeforeModelDecision, BeforeToolDecision,
14//!     CompactionConfig, HookRegistry, SessionId, ToolName, providers::openai_compatible,
15//! };
16//! use std::num::NonZeroU64;
17//!
18//! let provider = openai_compatible()
19//!     .api_key("sk-...")?
20//!     .model_name("gpt-4.1-mini")?
21//!     .context_window_tokens(NonZeroU64::new(128_000).expect("non-zero"))
22//!     .build()?;
23//!
24//! let hooks = HookRegistry::new()
25//!     .before_model_step(|ctx| async move {
26//!         if ctx.session_usage.is_some_and(|u| u.total.total_tokens >= 100_000) {
27//!             return BeforeModelDecision::stop_turn("token budget exceeded", None);
28//!         }
29//!         BeforeModelDecision::continue_normally()
30//!     })
31//!     .before_tool_call_for([ToolName::new("run_shell")?], |_ctx| async move {
32//!         BeforeToolDecision::deny("shell execution disabled")
33//!     })
34//!     .after_model_step(|_ctx| async move { AfterModelDecision::accept() });
35//!
36//! let agent = AgentBuilder::new(SessionId::new("demo")?)
37//!     .provider(provider)
38//!     .compaction(CompactionConfig::default())
39//!     .hooks(hooks)
40//!     .build()?;
41//!
42//! let _result = agent.run("Summarize the workspace.").await?;
43//! # Ok(())
44//! # }
45//! ```
46
47mod agent;
48mod agent_loop;
49pub mod binding;
50pub mod coding;
51mod errors;
52pub mod events;
53mod interactive;
54#[cfg(feature = "mcp")]
55pub mod mcp;
56pub mod observability;
57pub mod paths;
58pub mod permissions;
59pub mod process;
60mod profile;
61pub mod profiles;
62pub mod providers;
63#[doc(hidden)]
64pub mod runtime {
65    //! Explicit low-level runtime escape hatch for advanced integrations.
66
67    pub use merry_core::{RuntimeJournalEvent, RuntimeJournalPayload};
68    pub use merry_runtime::{
69        AgentLoopControl, AgentLoopInput, AgentLoopResult, AgentRun, AgentRunMessage,
70        CitationCompactionPolicy, DEFAULT_CONTEXT_WINDOW_FALLBACK_TOKENS,
71        DEFAULT_FOREGROUND_WAIT_BUDGET_SECS, DurableMemory, FinalOutputContract, InteractiveError,
72        InteractiveRunEventStream, InteractiveRunMessage, InteractiveSubagentSettings,
73        LoadedSession, MemoryLayout, ProjectRules, RegisteredTool, Runtime, RuntimeBuilder,
74        RuntimeError, RuntimeModelRole, RuntimeProfile, RuntimeProfileBuilder, SessionReservation,
75        SessionStoreError, StepContext, StepInput, SubagentConfig, SubagentLimitBehavior,
76        ToolExecutionContext, ToolExecutionError, ToolExecutionOutcome, ToolExecutor,
77        ToolExecutorFuture,
78    };
79}
80/// Rust-only lifetime-bearing adapter types for advanced hosts.
81///
82/// Foreign-language bindings must use [`binding`], whose owned protocol does
83/// not expose Rust lifetimes. This module is intentionally separate from that
84/// FFI contract and is hidden from generated API documentation.
85#[doc(hidden)]
86pub mod rust {
87    pub use super::agent::{
88        AgentRun, AgentRunMessage, ToolInvocation, ToolInvocationBatch, ToolInvocationContent,
89        ToolInvocationContentError, ToolInvocationResult, ToolInvocationSubmission,
90    };
91    pub use super::interactive::{
92        InteractiveMessage, InteractiveToolInvocation, InteractiveToolInvocationBatch,
93    };
94    pub use super::stream::StructuredAgentRun;
95}
96mod run_result;
97mod stream;
98pub mod tools;
99
100pub use agent::{Agent, AgentBuilder, RunResult};
101pub use agent_loop::{coding_agent_loop_config, generic_agent_loop_config};
102pub use errors::{AgentBuildError, AgentError, AgentProfileError};
103pub use interactive::{
104    InteractiveControl, InteractiveEventStream, InteractiveInput, InteractiveInputItem,
105    InteractiveInputSnapshot, InteractiveRun,
106};
107pub use merry_core::{
108    ContextWindowSource, RuntimeEvent, SessionId, ToolCallArguments, ToolName, ToolOutput,
109};
110pub use merry_llm::{
111    GenerationConfig, ModelName, ModelProvider, ModelRetryPolicy, ParallelToolCalls,
112    ReasoningEffort, ServiceTier,
113};
114pub use merry_runtime::{
115    AUTO_COMPACT_THRESHOLD_PERCENT, AfterModelDecision, AfterModelStepContext,
116    AfterToolCallContext, AfterToolDecision, AgentLoopBlockedReason, AgentLoopConfig,
117    AgentLoopConfigError, AgentLoopStatus, ArchiveConfig, BeforeModelDecision,
118    BeforeModelStepContext, BeforeToolCallContext, BeforeToolDecision, CitationCompactionPolicy,
119    CompactionConfig, CompactionError, CompactionOutcome, CompactionPrompt, FINAL_OUTPUT_TOOL_NAME,
120    FileSessionStore, FinalOutput, HistoryRetention, HookDecision, HookError, HookFuture,
121    HookInvocation, HookInvocationId, HookPoint, HookRegistry, HookRequest, HookSubscriptions,
122    InteractivePrimaryModel, InteractiveProcessExecutionUpdate, InteractiveSettingsUpdate,
123    InterruptReason, PromptBlock, PromptError, PromptProfile, QueuedInputId, RuntimeHookHandler,
124    RuntimeModelRole, StructuredOutputRetryPolicy, ToolHookFilter,
125};
126pub use profile::{AgentProfile, AgentProfileContext};
127pub use stream::{AgentEventStream, StructuredAgentEventStream, StructuredRunResult};
128pub use tools::{Tool, ToolBuildError, ToolSpec, tool};