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