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 durable;
81pub mod file_watcher;
82pub mod goal;
83pub mod instructions;
84pub mod instrumented_channel;
85pub mod metrics;
86#[cfg(feature = "profiling")]
87pub mod metrics_bridge;
88pub mod notifications;
89pub mod pipeline;
90pub mod project;
91pub mod provider_factory;
92pub mod quality;
93pub mod redact;
94pub mod serve;
95#[cfg(feature = "sysinfo")]
96pub mod system_metrics;
97
98pub mod http;
99pub mod json_event_layer;
100pub mod json_event_sink;
101pub mod lsp_hooks;
102pub mod memory_tools;
103pub mod overflow_tools;
104pub mod runtime_context;
105pub mod runtime_layer;
106pub mod skill_invoker;
107pub mod skill_loader;
108mod skill_trust_gate;
109pub use zeph_common::text;
110
111#[cfg(test)]
112pub mod testing;
113
114pub use agent::Agent;
115pub use agent::SkillConfigParams;
116pub use agent::error::AgentError;
117pub use agent::session_config::{AgentSessionConfig, CONTEXT_BUDGET_RESERVE_RATIO};
118pub use agent::state::AdversarialPolicyInfo;
119pub use agent::state::ProviderConfigSnapshot;
120pub use agent::state::ShellOverlaySnapshot;
121pub use channel::{
122 Attachment, AttachmentKind, Channel, ChannelError, ChannelMessage, LoopbackChannel,
123 LoopbackEvent, LoopbackHandle, StopHint, ToolOutputData, ToolOutputEvent, ToolStartData,
124 ToolStartEvent,
125};
126pub use config::{Config, ConfigError};
127pub use runtime_context::RuntimeContext;
128pub use skill_invoker::{InvokeSkillParams, SkillInvokeExecutor, SkillTrustSnapshot};
129pub use skill_loader::SkillLoaderExecutor;
130pub use skill_trust_gate::{SkillBodyResolution, SkillTrustGate, resolve_require_check};
131pub use zeph_common::hash::blake3_hex as content_hash;
132pub use zeph_sanitizer::exfiltration::{
133 ExfiltrationEvent, ExfiltrationGuard, ExfiltrationGuardConfig, extract_flagged_urls,
134};
135pub use zeph_sanitizer::{
136 ContentIsolationConfig, ContentSanitizer, ContentSource, ContentSourceKind, ContentTrustLevel,
137 InjectionFlag, QuarantineConfig, SanitizedContent,
138};
139pub use zeph_tools::executor::DiffData;
140
141// Re-export vault module to preserve internal import paths (e.g., `crate::vault::VaultProvider`).
142pub mod vault {
143 pub use zeph_vault::{
144 AgeVaultError, AgeVaultProvider, ArcAgeVaultProvider, Secret, VaultError, VaultProvider,
145 default_vault_dir,
146 };
147
148 /// Environment-variable backed vault provider.
149 ///
150 /// # Security
151 ///
152 /// This provider reads secrets from process environment variables and is
153 /// intended **exclusively for development and testing**. Never use in
154 /// production builds. Use [`AgeVaultProvider`] instead.
155 pub use zeph_vault::EnvVaultProvider;
156
157 #[cfg(any(test, feature = "mock"))]
158 pub use zeph_vault::MockVaultProvider;
159}