Crate code_mesh_core

Source
Expand description

§Code Mesh Core

Code Mesh Core is the foundational library for the Code Mesh AI coding assistant. It provides a comprehensive set of abstractions and implementations for building AI-powered development tools.

§Features

  • 🤖 Multi-LLM Support: Unified interface for Anthropic Claude, OpenAI GPT, Google Gemini, and more
  • 🛠️ Extensible Tool System: Built-in tools for file operations, code execution, web search, and custom extensions
  • 💾 Session Management: Persistent conversation history with intelligent context management
  • 🔐 Secure Authentication: OAuth and API key support with encrypted credential storage
  • 🌐 Cross-Platform: Native performance with WebAssembly compatibility
  • 🧠 Agent Orchestration: Multi-agent coordination for complex coding workflows

§Quick Start

use code_mesh_core::{Session, LanguageModel, ProviderRegistry, ToolRegistry};
use tokio;
 
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Initialize provider registry
    let mut providers = ProviderRegistry::new();
    providers.register_anthropic("your-api-key")?;
     
    // Create a new session
    let mut session = Session::new();
    session.add_user_message("Help me implement a binary search function");
     
    // Get a language model
    let model = providers.get_model("anthropic/claude-3-opus")?;
     
    // Generate response
    let response = model.complete(&session.build_prompt()).await?;
    session.add_assistant_message(response);
     
    println!("Assistant: {}", session.last_message().content);
    Ok(())
}

§Architecture Overview

Code Mesh Core is built around several key abstractions:

§Language Models (llm module)

The LanguageModel trait provides a unified interface for interacting with different AI providers. Implementations are available for major providers:

  • [AnthropicProvider] - Claude models via Anthropic API
  • [OpenAIProvider] - GPT models via OpenAI API
  • [MistralProvider] - Mistral models via Mistral AI API

§Tools (tool module)

The Tool trait enables AI agents to interact with external systems:

  • [FileTools] - Read, write, and search files
  • [BashTool] - Execute shell commands safely
  • [WebTool] - Search the web and fetch documentation
  • [GitTool] - Git operations and repository management

§Sessions (session module)

Sessions manage conversation state and context:

§Authentication (auth module)

Secure credential management for AI providers:

  • Auth - Authentication interface
  • [CredentialStore] - Encrypted credential storage
  • [OAuthFlow] - OAuth authentication flows

§Examples

§Multi-Provider Setup

use code_mesh_core::{ProviderRegistry, Provider};
 
let mut registry = ProviderRegistry::new();
 
// Add multiple providers
registry.register_anthropic("anthropic-key")?;
registry.register_openai("openai-key")?;
registry.register_mistral("mistral-key")?;
 
// Use different models for different tasks
let planning_model = registry.get_model("anthropic/claude-3-opus")?;
let coding_model = registry.get_model("openai/gpt-4")?;
let testing_model = registry.get_model("mistral/mistral-large")?;

§Tool Integration

use code_mesh_core::{ToolRegistry, FileTools, BashTool};
 
let mut tools = ToolRegistry::new();
tools.register(Box::new(FileTools::new()));
tools.register(Box::new(BashTool::new()));
 
// Tools can be used by AI agents
let context = ToolContext::new();
let result = tools.execute("read_file", &["src/main.rs"], &context).await?;

§Session Persistence

use code_mesh_core::{SessionManager, Storage};
 
let storage = Storage::new("./sessions")?;
let mut manager = SessionManager::new(storage);
 
// Save session
let session_id = manager.save_session(&session).await?;
 
// Load session later
let restored_session = manager.load_session(&session_id).await?;

§Feature Flags

Code Mesh Core supports conditional compilation based on target platform:

  • native (default): Full native functionality including file system access
  • wasm: WebAssembly-compatible subset with browser APIs
  • openai: OpenAI provider support
  • anthropic: Anthropic provider support
  • mistral: Mistral provider support

§Error Handling

All public APIs use the Result type with Error for consistent error handling. Errors are categorized by type and provide detailed context for debugging.

§Performance Considerations

  • Async/Await: All I/O operations are asynchronous for better performance
  • Connection Pooling: HTTP clients use connection pooling for efficiency
  • Caching: Intelligent caching of model responses and file contents
  • Memory Management: Bounded memory usage with configurable limits

Re-exports§

pub use llm::Provider;
pub use llm::Model;
pub use llm::LanguageModel;
pub use llm::ProviderRegistry;
pub use llm::Message;
pub use llm::MessageRole;
pub use llm::MessageContent;
pub use llm::GenerateOptions;
pub use llm::GenerateResult;
pub use llm::StreamChunk;
pub use llm::Usage;
pub use llm::FinishReason;
pub use tool::Tool;
pub use tool::ToolContext;
pub use tool::ToolResult;
pub use tool::ToolRegistry;
pub use tool::ToolError;
pub use tool::ToolDefinition;
pub use auth::Auth;
pub use auth::AuthCredentials;
pub use auth::AuthStorage;
pub use storage::Storage;
pub use storage::StorageError;
pub use session::Session;
pub use session::Message as SessionMessage;
pub use session::SessionManager;
pub use session::MessageRole as SessionMessageRole;
pub use session::SessionMetadata;
pub use config::Config;
pub use config::ConfigManager;
pub use config::ProviderConfig;
pub use config::ToolConfig;
pub use permission::PermissionManager;
pub use permission::PermissionContext;
pub use permission::PermissionLevel;
pub use error::Error;
pub use error::Result;
pub use events::Event;
pub use events::EventBus;
pub use events::EventHandler;
pub use sync::AsyncMutex;
pub use sync::AsyncRwLock;
pub use sync::Debouncer;

Modules§

agent
Agent orchestration for Code Mesh
auth
Authentication system for Code Mesh
config
Configuration management for Code Mesh Core
error
Error types for Code Mesh Core
events
Event system for Code Mesh Core
features
Feature flags for Code Mesh Core
llm
LLM provider abstractions and implementations
memory
Memory management for Code Mesh
permission
Permission system for Code Mesh Core
planner
Task planning for Code Mesh
prompts
System prompts for Code Mesh
runtime
Runtime compatibility layer
session
Session management for Code Mesh
storage
Storage abstractions for Code Mesh
sync
Synchronization primitives for Code Mesh Core
tool
Tool system for Code Mesh Comprehensive tool system with file operations, process execution, monitoring, and security
utils
Utility functions and helpers for Code Mesh Core

Macros§

event_handler
Convenience macro for creating simple event handlers

Constants§

VERSION