vtcode_core/lib.rs
1//! # vtcode-core - Runtime for VT Code
2//!
3//! `vtcode-core` powers the VT Code terminal coding agent. It provides the
4//! reusable building blocks for multi-provider LLM orchestration, tool
5//! execution, semantic code analysis, and configurable safety policies.
6//!
7//! ## Highlights
8//!
9//! - **Provider Abstraction**: unified LLM interface with adapters for OpenAI,
10//! Anthropic, xAI, DeepSeek, Gemini, and OpenRouter, including automatic
11//! failover and spend controls.
12//! - **Prompt Caching**: cross-provider prompt caching system that leverages
13//! provider-specific caching capabilities (OpenAI's automatic caching, Anthropic's
14//! cache_control blocks, Gemini's implicit/explicit caching) to reduce costs and
15//! latency, with configurable settings per provider.
16//! - **Semantic Workspace Model**: incremental tree-sitter parsing for Rust,
17//! Python, JavaScript, TypeScript, Go, and Java augmented by ast-grep based
18//! structural search and refactoring.
19//! - **Tool System**: trait-driven registry for shell execution, file IO,
20//! search, and custom commands, with Tokio-powered concurrency and PTY
21//! streaming.
22//! - **Configuration-First**: everything is driven by `vtcode.toml`, with
23//! model, safety, and automation constants centralized in
24//! `config::constants` and curated metadata in `docs/models.json`.
25//! - **Safety & Observability**: workspace boundary enforcement, command
26//! allow/deny lists, human-in-the-loop confirmation, and structured event
27//! logging.
28//!
29//! ## Architecture Overview
30//!
31//! The crate is organized into several key modules:
32//!
33//! - `config/`: configuration loader, defaults, and schema validation.
34//! - `llm/`: provider clients, request shaping, and response handling.
35//! - `tools/`: built-in tool implementations plus registration utilities.
36//! - `context/`: conversation management, summarization, and memory.
37//! - `executor/`: async orchestration for tool invocations and streaming output.
38//! - `tree_sitter/`: language-specific parsers, syntax tree caching, and
39//! semantic extraction helpers.
40//! - `core/prompt_caching`: cross-provider prompt caching system that leverages
41//! provider-specific caching mechanisms for cost optimization and reduced latency.
42//!
43//! ## Quickstart
44//!
45//! ```rust,ignore
46//! use vtcode_core::{Agent, VTCodeConfig};
47//!
48//! #[tokio::main]
49//! async fn main() -> Result<(), anyhow::Error> {
50//! // Load configuration from vtcode.toml or environment overrides
51//! let config = VTCodeConfig::load()?;
52//!
53//! // Construct the agent runtime
54//! let agent = Agent::new(config).await?;
55//!
56//! // Execute an interactive session
57//! agent.run().await?;
58//!
59//! Ok(())
60//! }
61//! ```
62//!
63//! ## Extending VT Code
64//!
65//! Register custom tools or providers by composing the existing traits:
66//!
67//! ```rust,ignore
68//! use vtcode_core::tools::{ToolRegistry, ToolRegistration};
69//!
70//! #[tokio::main]
71//! async fn main() -> Result<(), anyhow::Error> {
72//! let workspace = std::env::current_dir()?;
73//! let mut registry = ToolRegistry::new(workspace);
74//!
75//! let custom_tool = ToolRegistration {
76//! name: "my_custom_tool".into(),
77//! description: "A custom tool for specific tasks".into(),
78//! parameters: serde_json::json!({
79//! "type": "object",
80//! "properties": { "input": { "type": "string" } }
81//! }),
82//! handler: |_args| async move {
83//! // Implement your tool behavior here
84//! Ok(serde_json::json!({ "result": "success" }))
85//! },
86//! };
87//!
88//! registry.register_tool(custom_tool).await?;
89//! Ok(())
90//! }
91//! ```
92//!
93//! For a complete tour of modules and extension points, read
94//! `docs/ARCHITECTURE.md` and the guides in `docs/project/`.
95//!
96//! ## Agent Client Protocol (ACP)
97//!
98//! VT Code's binary exposes an ACP bridge for Zed. Enable it via the `[acp]` section in
99//! `vtcode.toml`, launch the `vtcode acp` subcommand, and register the binary under
100//! `agent_servers` in Zed's `settings.json`. Detailed instructions and troubleshooting live in the
101//! [Zed ACP integration guide](https://github.com/vinhnx/vtcode/blob/main/docs/guides/zed-acp.md),
102//! with a rendered summary on
103//! [docs.rs](https://docs.rs/vtcode/latest/vtcode/#agent-client-protocol-acp).
104
105//! ### Bridge guarantees
106//!
107//! - Tool exposure follows capability negotiation: `read_file` stays disabled unless Zed
108//! advertises `fs.read_text_file`.
109//! - Each filesystem request invokes `session/request_permission`, ensuring explicit approval
110//! within the editor before data flows.
111//! - Cancellation signals propagate into VT Code, cancelling active tool calls and ending the
112//! turn with `StopReason::Cancelled`.
113//! - ACP `plan` entries track analysis, context gathering, and response drafting for timeline
114//! parity with Zed.
115//! - Absolute-path checks guard every `read_file` argument before forwarding it to the client.
116//! - Non-tool-capable models trigger reasoning notices and an automatic downgrade to plain
117//! completions without losing plan consistency.
118
119//! VTCode Core Library
120//!
121//! This crate provides the core functionality for the VTCode agent,
122//! including tool implementations, LLM integration, and utility functions.
123
124// Public modules
125pub mod bash_runner;
126pub mod cli;
127pub mod code;
128pub mod commands;
129pub mod config;
130pub mod constants;
131pub mod core;
132pub mod gemini;
133pub mod llm;
134pub mod markdown_storage;
135pub mod mcp_client;
136pub mod models;
137pub mod project;
138pub mod project_doc;
139pub mod prompts;
140pub mod safety;
141pub mod simple_indexer;
142pub mod tool_policy;
143pub mod tools;
144pub mod types;
145pub mod ui;
146pub mod utils;
147
148// Re-exports for convenience
149pub use bash_runner::BashRunner;
150pub use cli::args::{Cli, Commands};
151pub use code::code_completion::{CompletionEngine, CompletionSuggestion};
152pub use commands::stats::handle_stats_command;
153pub use config::types::{
154 AnalysisDepth, CapabilityLevel, CommandResult, CompressionLevel, ContextConfig, LoggingConfig,
155 OutputFormat, PerformanceMetrics, ReasoningEffortLevel, SessionInfo, ToolConfig,
156};
157pub use config::{
158 AgentClientProtocolConfig, AgentClientProtocolTransport, AgentClientProtocolZedConfig,
159 AgentClientProtocolZedToolsConfig, AgentConfig, VTCodeConfig,
160};
161pub use core::agent::core::Agent;
162pub use core::context_compression::{
163 CompressedContext, ContextCompressionConfig, ContextCompressor,
164};
165pub use core::conversation_summarizer::ConversationSummarizer;
166pub use core::performance_profiler::PerformanceProfiler;
167pub use core::prompt_caching::{CacheStats, PromptCache, PromptCacheConfig, PromptOptimizer};
168pub use core::timeout_detector::TimeoutDetector;
169pub use gemini::{Content, FunctionDeclaration, Part};
170pub use llm::{AnyClient, make_client};
171pub use markdown_storage::{MarkdownStorage, ProjectData, ProjectStorage, SimpleKVStorage};
172pub use project::{SimpleCache, SimpleProjectManager};
173pub use prompts::{
174 generate_lightweight_instruction, generate_specialized_instruction, generate_system_instruction,
175};
176pub use simple_indexer::SimpleIndexer;
177pub use tool_policy::{ToolPolicy, ToolPolicyManager};
178pub use tools::advanced_search::{AdvancedSearchTool, SearchOptions};
179pub use tools::grep_search::GrepSearchManager;
180pub use tools::tree_sitter::TreeSitterAnalyzer;
181pub use tools::{
182 ToolRegistration, ToolRegistry, build_function_declarations,
183 build_function_declarations_for_level, build_function_declarations_with_mode,
184};
185pub use ui::diff_renderer::DiffRenderer;
186pub use utils::dot_config::{
187 CacheConfig, DotConfig, DotManager, ProviderConfigs, UiConfig, UserPreferences,
188 WorkspaceTrustLevel, WorkspaceTrustRecord, WorkspaceTrustStore, initialize_dot_folder,
189 load_user_config, save_user_config, update_theme_preference,
190};
191pub use utils::vtcodegitignore::initialize_vtcode_gitignore;
192
193#[cfg(test)]
194mod tests {
195 use super::*;
196
197 use tempfile::TempDir;
198
199 #[test]
200 fn test_library_exports() {
201 // Test that all public exports are accessible
202 let _cache = PromptCache::new();
203 }
204
205 #[test]
206 fn test_module_structure() {
207 // Test that all modules can be imported
208 // This is a compile-time test that ensures module structure is correct
209 }
210
211 #[test]
212 fn test_version_consistency() {
213 // Test that version information is consistent across modules
214 // This would be more meaningful with actual version checking
215 }
216
217 #[tokio::test]
218 async fn test_tool_registry_integration() {
219 use crate::config::constants::tools;
220
221 let temp_dir = TempDir::new().unwrap();
222 std::env::set_current_dir(&temp_dir).unwrap();
223
224 let mut registry = ToolRegistry::new(temp_dir.path().to_path_buf());
225
226 // Test that we can execute basic tools
227 let list_args = serde_json::json!({
228 "path": "."
229 });
230
231 let result = registry.execute_tool(tools::LIST_FILES, list_args).await;
232 assert!(result.is_ok());
233
234 let response: serde_json::Value = result.unwrap();
235 assert!(response["files"].is_array());
236 }
237
238 #[tokio::test]
239 async fn test_pty_basic_command() {
240 let temp_dir = TempDir::new().unwrap();
241 let workspace = temp_dir.path().to_path_buf();
242 let mut registry = ToolRegistry::new(workspace.clone());
243
244 // Test a simple PTY command
245 let args = serde_json::json!({
246 "command": "echo",
247 "args": ["Hello, PTY!"]
248 });
249
250 let result = registry.execute_tool("run_pty_cmd", args).await;
251 assert!(result.is_ok());
252 let response: serde_json::Value = result.unwrap();
253 assert_eq!(response["success"], true);
254 assert_eq!(response["code"], 0);
255 assert!(response["output"].as_str().unwrap().contains("Hello, PTY!"));
256 }
257
258 #[tokio::test]
259 async fn test_pty_session_management() {
260 let temp_dir = TempDir::new().unwrap();
261 let workspace = temp_dir.path().to_path_buf();
262 let mut registry = ToolRegistry::new(workspace.clone());
263
264 // Test creating a PTY session
265 let args = serde_json::json!({
266 "session_id": "test_session",
267 "command": "bash"
268 });
269
270 let result = registry.execute_tool("create_pty_session", args).await;
271 assert!(result.is_ok());
272 let response: serde_json::Value = result.unwrap();
273 assert_eq!(response["success"], true);
274 assert_eq!(response["session_id"], "test_session");
275
276 // Test listing PTY sessions
277 let args = serde_json::json!({});
278 let result = registry.execute_tool("list_pty_sessions", args).await;
279 assert!(result.is_ok());
280 let response: serde_json::Value = result.unwrap();
281 assert!(
282 response["sessions"]
283 .as_array()
284 .unwrap()
285 .contains(&"test_session".into())
286 );
287
288 // Test closing a PTY session
289 let args = serde_json::json!({
290 "session_id": "test_session"
291 });
292
293 let result = registry.execute_tool("close_pty_session", args).await;
294 assert!(result.is_ok());
295 let response: serde_json::Value = result.unwrap();
296 assert_eq!(response["success"], true);
297 assert_eq!(response["session_id"], "test_session");
298 }
299}