zeph_core/lib.rs
1// SPDX-FileCopyrightText: 2026 Andrei G <bug-ops>
2// SPDX-License-Identifier: MIT OR Apache-2.0
3
4// tracing::instrument on large async fns in zeph-agent-context increases the
5// Future type depth beyond the default limit of 128.
6#![recursion_limit = "256"]
7
8//! Zeph core agent: multi-model inference, semantic memory, skills orchestration, and tool execution.
9//!
10//! This crate provides the [`Agent`] struct — the autonomous AI system at the heart of Zeph.
11//! It integrates LLM providers (Claude, `OpenAI`, Ollama, Candle), semantic memory (Qdrant),
12//! skill registry and matching, tool execution (shell, web, custom), MCP client support, and
13//! security/compliance subsystems into a single composable agent framework.
14//!
15//! # Usage
16//!
17//! The main entry point is [`Agent::new`] or [`Agent::new_with_registry_arc`]. After creating
18//! an agent, call [`Agent::run`] to execute the main loop.
19//! Always call [`Agent::shutdown`] before dropping to persist state.
20//!
21//! See the `bootstrap` module in the `zeph` binary crate for config loading and provider setup examples.
22//!
23//! # Key Components
24//!
25//! - [`Agent`] — Main struct that runs the agent loop
26//! - [`Channel`] — Abstraction for user interaction (send/receive messages and events)
27//! - [`channel::ChannelMessage`] — Structured messages flowing to/from the user
28//! - `config` — Configuration schema (LLM providers, memory, skills, etc.)
29//! - `agent::session_config` — Per-session configuration (budget, timeouts, etc.)
30//! - `agent::context` — Context assembly and token budgeting utilities
31//! - [`pipeline`] — Structured execution pipelines for complex workflows
32//! - [`project`] — Project indexing and semantic retrieval
33//! - [`memory_tools`] — Memory search and management utilities
34//!
35//! Note: The `bootstrap` module (`AppBuilder`, provider setup, etc.) lives in the `zeph` binary crate.
36//!
37//! # Architecture
38//!
39//! The agent operates as a **single-turn finite state machine** that processes each user
40//! message through a series of stages:
41//!
42//! 1. **Input** — Receive user message via channel
43//! 2. **Context assembly** — Build prompt from conversation history, memory, and skills
44//! 3. **LLM inference** — Call the model with multi-tool calling support
45//! 4. **Tool execution** — Run tool calls concurrently with streaming output
46//! 5. **Feedback loop** — Feed tool results back to LLM for synthesis
47//! 6. **Output** — Send agent response via channel
48//! 7. **Persistence** — Save messages and state (async, deferred)
49//!
50//! All async operations (`await` points) are bounded with timeouts to prevent stalls.
51//!
52//! # Channel Contract
53//!
54//! Implementing the [`Channel`] trait allows the agent to integrate with any I/O system:
55//!
56//! - **CLI** — `cargo run -- --config config.toml`
57//! - **Telegram** — Bot interface with streaming updates
58//! - **TUI** — Multi-panel dashboard with real-time metrics
59//! - **HTTP gateway** — Webhook ingestion and agent event streaming
60//! - **Custom** — Implement [`Channel`] for domain-specific systems
61//!
62//! # Feature Flags
63//!
64//! - `candle` — Local inference via Candle (default off, requires CUDA/Metal)
65//! - `classifiers` — ML-based content classification and trust scoring
66//! - `metal` — Candle with Metal acceleration (macOS)
67//! - `cuda` — Candle with CUDA acceleration (Linux/Windows)
68//! - `scheduler` — Cron-based periodic task scheduler
69
70pub mod agent;
71#[cfg(feature = "profiling-alloc")]
72pub mod alloc_layer;
73pub mod channel;
74pub mod config;
75pub mod config_watcher;
76pub mod context;
77pub mod cost;
78pub mod daemon;
79pub mod debug_dump;
80pub mod file_watcher;
81pub mod goal;
82pub mod instructions;
83pub mod instrumented_channel;
84pub mod metrics;
85#[cfg(feature = "profiling")]
86pub mod metrics_bridge;
87pub mod notifications;
88pub mod pipeline;
89pub mod project;
90pub mod provider_factory;
91pub mod quality;
92pub mod redact;
93#[cfg(feature = "sysinfo")]
94pub mod system_metrics;
95
96pub mod http;
97pub mod json_event_layer;
98pub mod json_event_sink;
99pub mod lsp_hooks;
100pub mod memory_tools;
101pub mod overflow_tools;
102pub mod runtime_context;
103pub mod runtime_layer;
104pub mod skill_invoker;
105pub mod skill_loader;
106pub use zeph_common::text;
107
108#[cfg(test)]
109pub mod testing;
110
111pub use agent::Agent;
112pub use agent::error::AgentError;
113pub use agent::session_config::{AgentSessionConfig, CONTEXT_BUDGET_RESERVE_RATIO};
114pub use agent::state::AdversarialPolicyInfo;
115pub use agent::state::ProviderConfigSnapshot;
116pub use agent::state::ShellOverlaySnapshot;
117pub use channel::{
118 Attachment, AttachmentKind, Channel, ChannelError, ChannelMessage, LoopbackChannel,
119 LoopbackEvent, LoopbackHandle, StopHint, ToolOutputData, ToolOutputEvent, ToolStartData,
120 ToolStartEvent,
121};
122pub use config::{Config, ConfigError};
123pub use runtime_context::RuntimeContext;
124pub use skill_invoker::{InvokeSkillParams, SkillInvokeExecutor, SkillTrustSnapshot};
125pub use skill_loader::SkillLoaderExecutor;
126pub use zeph_common::hash::blake3_hex as content_hash;
127pub use zeph_sanitizer::exfiltration::{
128 ExfiltrationEvent, ExfiltrationGuard, ExfiltrationGuardConfig, extract_flagged_urls,
129};
130pub use zeph_sanitizer::{
131 ContentIsolationConfig, ContentSanitizer, ContentSource, ContentSourceKind, ContentTrustLevel,
132 InjectionFlag, QuarantineConfig, SanitizedContent,
133};
134pub use zeph_tools::executor::DiffData;
135
136// Re-export vault module to preserve internal import paths (e.g., `crate::vault::VaultProvider`).
137pub mod vault {
138 pub use zeph_vault::{
139 AgeVaultError, AgeVaultProvider, ArcAgeVaultProvider, Secret, VaultError, VaultProvider,
140 default_vault_dir,
141 };
142
143 /// Environment-variable backed vault provider.
144 ///
145 /// # Security
146 ///
147 /// This provider reads secrets from process environment variables and is
148 /// intended **exclusively for development and testing**. Never use in
149 /// production builds. Use [`AgeVaultProvider`] instead.
150 pub use zeph_vault::EnvVaultProvider;
151
152 #[cfg(any(test, feature = "mock"))]
153 pub use zeph_vault::MockVaultProvider;
154}