ctx/lib.rs
1//! ctx - Code intelligence library for AI-assisted development.
2//!
3//! This library powers the `ctx` CLI and can be embedded in your own tools.
4//! It provides:
5//!
6//! - **Code Indexing**: Parse and index source code with symbol extraction
7//! - **Semantic Search**: Find relevant code using embeddings
8//! - **Call Graph Analysis**: Understand code relationships and impact
9//! - **Smart Context Selection**: Intelligently select files for LLM context
10//! - **Diff-Aware Context**: Generate context focused on code changes
11//! - **Token Management**: Count and budget tokens for LLM context windows
12//!
13//! # Installation
14//!
15//! The package is published on crates.io as **`agentis-ctx`**, but the
16//! library target is named `ctx`, so code imports use `ctx::`:
17//!
18//! ```toml
19//! [dependencies]
20//! agentis-ctx = "0.2"
21//! ```
22//!
23//! Most of the commonly used types are re-exported through [`prelude`]:
24//!
25//! ```
26//! use ctx::prelude::*;
27//! ```
28//!
29//! # Quick Start: index a codebase and search it
30//!
31//! Indexing creates `.ctx/codebase.sqlite` under the project root. Subsequent
32//! runs are incremental — only changed files are reparsed.
33//!
34//! ```no_run
35//! use ctx::prelude::*;
36//! use std::path::Path;
37//!
38//! fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
39//! let root = Path::new("./my-project");
40//!
41//! // Build (or incrementally update) the index
42//! let mut indexer = Indexer::with_config(root, false, WalkerConfig::default())?;
43//! let result = indexer.index()?;
44//! println!(
45//! "Indexed {} files: {} symbols, {} edges",
46//! result.files_indexed, result.symbols_extracted, result.edges_extracted
47//! );
48//!
49//! // Reopen the database later without reindexing
50//! let db = open_database(root)?;
51//!
52//! // Keyword search over symbols (FTS5)
53//! for symbol in db.find_symbols("authenticate", 10)? {
54//! println!("{} ({}:{})", symbol.name, symbol.file_path, symbol.line_start);
55//! }
56//! Ok(())
57//! }
58//! ```
59//!
60//! # Smart context selection
61//!
62//! Select the files most relevant to a task description, within a token
63//! budget — the same engine behind `ctx smart`:
64//!
65//! ```no_run
66//! use ctx::prelude::*;
67//! use std::path::Path;
68//!
69//! fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
70//! let root = Path::new("./my-project");
71//! let db = open_database(root)?;
72//! let analytics = Analytics::open(root)?;
73//!
74//! // Local embedding model (downloaded on first use, ~90 MB).
75//! // OpenAIProvider is available as an alternative.
76//! let provider = LocalProvider::new()?;
77//!
78//! let context = smart_context(
79//! &db,
80//! &analytics,
81//! &provider,
82//! "add rate limiting to the API",
83//! SmartConfig::default(),
84//! )?;
85//!
86//! for file in &context.selected_files {
87//! println!("{} (relevance {:.2})", file.path, file.relevance_score);
88//! }
89//! println!("~{} tokens selected", context.total_tokens);
90//! Ok(())
91//! }
92//! ```
93//!
94//! # Token counting
95//!
96//! ```
97//! use ctx::tokens::{count_tokens, count_tokens_with_encoding, Encoding};
98//!
99//! let n = count_tokens("fn main() { println!(\"hello\"); }").unwrap();
100//! assert!(n > 0);
101//!
102//! let n = count_tokens_with_encoding("hello world", Encoding::O200kBase).unwrap();
103//! assert!(n > 0);
104//! ```
105//!
106//! # Module overview
107//!
108//! | Module | Purpose |
109//! |--------|---------|
110//! | [`index`] | Build and update the code intelligence index |
111//! | [`db`] | SQLite storage: symbols, edges, files, FTS and vector search |
112//! | [`parser`] | Tree-sitter based symbol/relationship extraction |
113//! | [`embeddings`] | Embedding providers (local fastembed or OpenAI) |
114//! | [`analytics`] | Call graph queries: callers, dependencies, impact (DuckDB) |
115//! | [`smart`] | Task-driven file selection for LLM context |
116//! | [`diff`] | Context generation focused on git changes |
117//! | [`walker`] | File discovery with glob patterns and ignore rules |
118//! | [`tokens`] | Token counting and budgeting (tiktoken) |
119//! | [`formatter`], [`output`], [`tree`] | Rendering context as XML/Markdown/JSON/plain |
120//! | [`audit`] | Code quality scoring for CI gates |
121//!
122//! # Feature Flags
123//!
124//! - `duckdb` *(enabled by default)* - DuckDB-backed analytics (call graphs,
125//! impact analysis, complexity). When disabled, [`analytics`] falls back to
126//! a stub that compiles everywhere (use this on Windows MSVC): add the
127//! dependency with `default-features = false`.
128//! - `mcp` - Model Context Protocol server support for editor/agent
129//! integrations (see the `mcp` module).
130
131// Core modules
132pub mod analytics;
133pub mod db;
134pub mod embeddings;
135pub mod error;
136pub mod exit;
137pub mod fingerprint;
138pub mod index;
139pub mod json;
140pub mod parser;
141pub mod rank;
142pub mod tokens;
143pub mod walker;
144
145// Context generation
146pub mod diff;
147pub mod smart;
148
149// Output formatting
150pub mod formatter;
151pub mod output;
152pub mod tree;
153
154// Project configuration (.ctx/config.toml)
155pub mod config;
156
157// Architecture rules (ctx check)
158pub mod check;
159pub mod rules;
160
161// Quality scorecard (ctx score)
162pub mod score;
163
164// Gate-evaluation logging (CTX_GATE_LOG JSONL records from ctx score)
165pub mod gatelog;
166
167// Per-commit Parquet metric snapshots (ctx snapshot)
168pub mod snapshot;
169
170// Harness packaging (ctx harness): Claude Code hooks, plugin scaffolding,
171// version compatibility guard, and integration diagnostics
172pub mod harness;
173
174// Release & update mechanism (ctx self-update, passive update notices)
175pub mod update;
176
177// Utilities
178pub mod audit;
179pub mod gitutil;
180pub mod utils;
181
182// Test helpers (public so the bin crate's tests can use them; not part of the
183// supported API surface)
184#[doc(hidden)]
185pub mod testutil;
186
187// Deterministic synthetic-repo generator for performance fixtures and the
188// external ctx-bench suite (ships in the crate; not part of the supported API)
189#[doc(hidden)]
190pub mod fixture;
191
192// Internal modules (not part of public API)
193pub(crate) mod default_ignores;
194
195// MCP server support (feature-gated)
196#[cfg(feature = "mcp")]
197pub mod mcp;
198
199// Re-export commonly used types at the crate root
200pub use error::{CtxError, Result};
201
202/// Prelude module for convenient imports.
203///
204/// ```ignore
205/// use ctx::prelude::*;
206/// ```
207pub mod prelude {
208 pub use crate::analytics::Analytics;
209 pub use crate::db::{Database, Edge, EdgeKind, FileRecord, ParseResult, Symbol, SymbolKind};
210 pub use crate::diff::{diff_context, DiffConfig, DiffContext};
211 pub use crate::embeddings::{Embedding, EmbeddingProvider, LocalProvider};
212 pub use crate::error::{CtxError, Result};
213 pub use crate::index::{open_database, IndexResult, Indexer};
214 pub use crate::parser::{CodeParser, Language};
215 pub use crate::smart::{smart_context, FileSelection, SmartConfig, SmartContext};
216 pub use crate::tokens::{count_tokens, Encoding, HasTokenCount, TokenCount};
217 pub use crate::walker::{discover_files, FileEntry, WalkerConfig};
218}