const INSTRUCTION_DOC_EXTENSIONS: &[&str] = &["md", "mdc", "markdown", "txt", "rst", "adoc"];
pub fn is_instruction_file(path: &str) -> bool {
is_builtin_instruction_file(path) || is_user_excluded(path)
}
fn is_builtin_instruction_file(path: &str) -> bool {
let lower = path.to_lowercase().replace('\\', "/");
let file = std::path::Path::new(&lower);
let filename = file.file_name().and_then(|f| f.to_str()).unwrap_or("");
if matches!(
filename,
"skill.md"
| "agents.md"
| "claude.md"
| "claude.local.md"
| "gemini.md"
| "copilot-instructions.md"
| "rules.md"
| ".cursorrules"
| ".clinerules"
| ".windsurfrules"
| "lean-ctx.md"
| "lean-ctx.mdc"
) {
return true;
}
let in_instruction_dir = lower.contains("/skills/")
|| lower.contains("/.cursor/rules/")
|| lower.contains("/.claude/rules/")
|| lower.contains("/.claude/agents/")
|| lower.contains("/.claude/commands/");
in_instruction_dir
&& file
.extension()
.and_then(|e| e.to_str())
.is_none_or(|ext| INSTRUCTION_DOC_EXTENSIONS.contains(&ext))
}
pub fn is_user_excluded(path: &str) -> bool {
let env = std::env::var("LEAN_CTX_HOOK_EXCLUDE").ok();
excluded_by(path, env.as_deref(), || {
crate::core::config::Config::load_arc()
.redirect_exclude
.clone()
})
}
pub(super) fn excluded_by(
path: &str,
env: Option<&str>,
configured: impl FnOnce() -> Vec<String>,
) -> bool {
match env {
Some(list) if !list.trim().is_empty() => {
let patterns: Vec<&str> = list.split(',').collect();
path_matches_any(path, &patterns)
}
_ => {
let configured = configured();
let patterns: Vec<&str> = configured.iter().map(String::as_str).collect();
path_matches_any(path, &patterns)
}
}
}
fn path_matches_any(path: &str, patterns: &[&str]) -> bool {
let normalized = path.replace('\\', "/");
let components: Vec<&str> = normalized.split('/').filter(|c| !c.is_empty()).collect();
let options = glob::MatchOptions {
case_sensitive: !cfg!(any(windows, target_os = "macos")),
require_literal_separator: true,
require_literal_leading_dot: false,
};
patterns
.iter()
.map(|p| p.trim().trim_start_matches("./"))
.filter(|p| !p.is_empty())
.filter_map(|p| glob::Pattern::new(p).ok())
.any(|pattern| {
(0..components.len())
.any(|start| pattern.matches_with(&components[start..].join("/"), options))
})
}
pub(super) fn find_similar_and_update_semantic_index(path: &str, content: &str) -> Option<String> {
const MAX_CONTENT_BYTES_FOR_SEMANTIC: usize = 32_768;
if content.len() > MAX_CONTENT_BYTES_FOR_SEMANTIC {
return None;
}
let cfg = crate::core::config::Config::load();
let profile = crate::core::config::MemoryProfile::effective(&cfg);
if !profile.semantic_cache_enabled() {
return None;
}
let project_root = detect_project_root(path);
let session_id = format!("{}", std::process::id());
let mut index = crate::core::semantic_cache::SemanticCacheIndex::load_or_create(&project_root);
let similar = index.find_similar(content, 0.7);
let relevant: Vec<_> = similar
.into_iter()
.filter(|(p, _)| p != path)
.take(3)
.collect();
index.add_file(path, content, &session_id);
if let Err(e) = index.save(&project_root) {
tracing::warn!("lean-ctx: failed to persist semantic index: {e}");
}
if relevant.is_empty() {
return None;
}
let hints: Vec<String> = relevant
.iter()
.map(|(p, score)| format!(" {p} ({:.0}% similar)", score * 100.0))
.collect();
Some(format!(
"[semantic: {} similar file(s) in cache]\n{}",
relevant.len(),
hints.join("\n")
))
}
pub(super) fn detect_project_root(path: &str) -> String {
crate::core::protocol::detect_project_root_or_cwd(path)
}
pub fn graph_related_hint(path: &str) -> Option<String> {
let project_root = detect_project_root(path);
crate::core::graph_context::build_related_hint(path, &project_root, 5)
}
#[allow(dead_code)]
pub(crate) fn read_image_file(
path: &str,
) -> Result<crate::server::tool_trait::ToolOutput, rmcp::ErrorData> {
use crate::core::binary_detect::{IMAGE_MAX_BYTES, image_mime_type};
use base64::Engine;
use rmcp::model::ContentBlock;
let metadata = std::fs::metadata(path)
.map_err(|e| rmcp::ErrorData::invalid_params(format!("Cannot read image: {e}"), None))?;
if metadata.len() > IMAGE_MAX_BYTES {
return Err(rmcp::ErrorData::invalid_params(
format!(
"Image too large ({:.1} MB, limit {:.0} MB). Resize or use a smaller image.",
metadata.len() as f64 / 1024.0 / 1024.0,
IMAGE_MAX_BYTES as f64 / 1024.0 / 1024.0,
),
None,
));
}
let mime_type = image_mime_type(path).ok_or_else(|| {
rmcp::ErrorData::invalid_params("Unsupported image format".to_string(), None)
})?;
let bytes = std::fs::read(path)
.map_err(|e| rmcp::ErrorData::invalid_params(format!("Cannot read image: {e}"), None))?;
let base64_data = base64::prelude::BASE64_STANDARD.encode(&bytes);
let short_name = std::path::Path::new(path)
.file_name()
.and_then(|n| n.to_str())
.unwrap_or(path);
let text_block = ContentBlock::text(format!(
"[Image: {} ({} KB, {})]",
short_name,
bytes.len() / 1024,
mime_type
));
let image_block = ContentBlock::image(base64_data, mime_type);
Ok(crate::server::tool_trait::ToolOutput::image(
vec![text_block, image_block],
path.to_string(),
))
}