Incremental engine for long horizon agents π Star if you like it!
Rust agentic-data-frameworkaiai-agentschange-data-capturecodebase-intelligencecontext-engineeringdata-engineeringdata-indexingdata-processingetlhelp-wantedindexingknowledge-graphllmlong-horizon-agentpythonragreal-timerustsemantic-search
//! Multi-Codebase Summarization β Rust equivalent of the Python example.
//!
//! Scans subdirectories of a root directory (each a project), extracts:
//! - Public classes/functions with summaries (via LLM)
//! - Mermaid graphs for function call relationships
//! - File-level summaries
//!
//! Aggregates per-file extractions into a project summary and outputs
//! markdown documentation.
//!
//! Demonstrates the mount macros and memoization layering:
//! - `mount_each!("project", projects, |project| process_project(ctx, β¦))` β one
//! component per project, processed concurrently and skipped wholesale on the
//! component-memo fast-path when unchanged
//! - `use_mount_each!(files, |file| extract_file_info(ctx, file))` β concurrent
//! per-file extraction, each its own component (unchanged files skip the LLM)
//! - `#[cocoindex::function(memo)]` β project aggregation cached by project
//! fingerprint (function-memo nested inside the project component)
//! - `DirTarget` β declarative output file sync
//!
//! ## Usage
//!
//! ```sh
//! export LLM_API_KEY="your-api-key"
//! cargo run -- ../../../../examples ./output
//! ```
use cocoindex::prelude::*;
use serde::Deserialize;
use std::path::Component;
use std::path::PathBuf;
use std::sync::OnceLock;
mod models;
use models::CodebaseInfo;
// ---------------------------------------------------------------------------
// LLM client (module-level, like Python's _instructor_client)
// ---------------------------------------------------------------------------
struct LlmClient {
api_key: String,
model: String,
http: reqwest::Client,
base_url: String,
}
/// Module-level LLM client, initialized once (same pattern as Python).
/// This avoids needing `ctx.get_or_err()` inside `#[function(memo)]` bodies.
static LLM: OnceLock<LlmClient> = OnceLock::new();
fn llm() -> &'static LlmClient {
LLM.get()
.expect("LLM client not initialized β call init_llm() first")
}
fn init_llm() {
dotenvy::dotenv().ok();
LLM.set(LlmClient {
api_key: std::env::var("LLM_API_KEY")
.or_else(|_| std::env::var("OPENAI_API_KEY"))
.expect("set LLM_API_KEY or OPENAI_API_KEY"),
model: std::env::var("LLM_MODEL").unwrap_or_else(|_| "gpt-4o-mini".into()),
base_url: std::env::var("LLM_BASE_URL")
.unwrap_or_else(|_| "https://api.openai.com/v1".into()),
http: reqwest::Client::new(),
})
.ok();
}
impl LlmClient {
async fn extract<T: for<'de> Deserialize<'de>>(
&self,
prompt: &str,
schema: &serde_json::Value,
) -> Result<T> {
let body = serde_json::json!({
"model": &self.model,
"messages": [{"role": "user", "content": prompt}],
"response_format": {
"type": "json_schema",
"json_schema": {
"name": "extraction",
"schema": schema,
"strict": true
}
}
});
let resp = self
.http
.post(format!("{}/chat/completions", self.base_url))
.bearer_auth(&self.api_key)
.json(&body)
.send()
.await
.map_err(|e| cocoindex::Error::engine(format!("LLM request failed: {e}")))?;
let resp_json: serde_json::Value = resp
.json()
.await
.map_err(|e| cocoindex::Error::engine(format!("LLM response parse: {e}")))?;
let content = resp_json["choices"][0]["message"]["content"]
.as_str()
.ok_or_else(|| cocoindex::Error::engine("no content in LLM response"))?;
serde_json::from_str(content)
.map_err(|e| cocoindex::Error::engine(format!("JSON decode: {e}")))
}
}
/// What the LLM steps depend on beyond their source: the model and the output
/// schema (its field descriptions guide the LLM). Both live outside the
/// functions' bodies, so `deps` folds them into the functions' logic and a
/// change re-runs the extraction, unchanged files included. Evaluated once per
/// process, after `init_llm()` has set the model.
fn llm_deps() -> (String, serde_json::Value) {
(llm().model.clone(), CodebaseInfo::json_schema())
}
fn should_skip_python_file(file: &FileEntry) -> bool {
file.relative_path()
.components()
.any(|component| match component {
Component::Normal(part) => {
let part = part.to_string_lossy();
part == "__pycache__" || part.starts_with('.')
}
_ => false,
})
}
// ---------------------------------------------------------------------------
// Per-file extraction β memoized (unchanged files skip the LLM call)
// ---------------------------------------------------------------------------
/// Extract structured info from a single Python file via LLM.
/// Mounted as one component per file: the component is skipped while the file
/// and this function's logic, `deps` included, are unchanged.
#[cocoindex::function(deps = llm_deps())]
async fn extract_file_info(_ctx: &Ctx, file: FileEntry) -> Result<CodebaseInfo> {
let content = file.content_str()?;
let file_path = file.key();
let prompt = format!(
"Analyze the following Python file and extract structured information.\n\n\
File path: {file_path}\n\n\
```python\n{content}\n```\n\n\
Instructions:\n\
1. Identify all PUBLIC classes (not starting with _) and summarize their purpose\n\
2. Identify all PUBLIC functions (not starting with _) and summarize their purpose\n\
3. If this file contains CocoIndex apps (coco.App), create Mermaid graphs showing the\n\
function call relationships (see the mermaid_graphs field description for format)\n\
4. Provide a brief summary of the file's purpose"
);
llm().extract(&prompt, &CodebaseInfo::json_schema()).await
}
// ---------------------------------------------------------------------------
// Aggregation
// ---------------------------------------------------------------------------
/// Aggregate per-file summaries into a project-level summary via LLM.
/// `memo` caches the aggregation result until any file-level summary changes.
#[cocoindex::function(memo, deps = llm_deps())]
async fn aggregate_project_info(
_ctx: &Ctx,
project_name: String,
file_infos: Vec<CodebaseInfo>,
) -> Result<CodebaseInfo> {
if file_infos.is_empty() {
return Ok(CodebaseInfo {
name: project_name,
summary: "Empty project with no Python files.".to_string(),
..Default::default()
});
}
if file_infos.len() == 1 {
let info = &file_infos[0];
return Ok(CodebaseInfo {
name: project_name,
summary: info.summary.clone(),
public_classes: info.public_classes.clone(),
public_functions: info.public_functions.clone(),
mermaid_graphs: info.mermaid_graphs.clone(),
});
}
let files_text: String = file_infos
.iter()
.map(|info| {
let classes: String = info
.public_classes
.iter()
.map(|c| c.name.as_str())
.collect::<Vec<_>>()
.join(", ");
let fns: String = info
.public_functions
.iter()
.map(|f| f.name.as_str())
.collect::<Vec<_>>()
.join(", ");
format!(
"### {}\nSummary: {}\nClasses: {}\nFunctions: {}",
info.name,
info.summary,
if classes.is_empty() { "None" } else { &classes },
if fns.is_empty() { "None" } else { &fns },
)
})
.collect::<Vec<_>>()
.join("\n\n");
let all_graphs: Vec<String> = file_infos
.iter()
.flat_map(|info| info.mermaid_graphs.iter().cloned())
.collect();
let prompt = format!(
"Aggregate the following Python files into a project-level summary.\n\n\
Project name: {project_name}\n\n\
Files:\n{files_text}\n\n\
Create a unified CodebaseInfo that:\n\
1. Summarizes the overall project purpose (not individual files)\n\
2. Lists the most important public classes across all files\n\
3. Lists the most important public functions across all files\n\
4. For mermaid_graphs: create a single unified graph showing how the CocoIndex\n\
components connect across the project (if applicable)"
);
let mut result: CodebaseInfo = llm().extract(&prompt, &CodebaseInfo::json_schema()).await?;
// Keep original file-level graphs if LLM didn't generate a unified one
if result.mermaid_graphs.is_empty() && !all_graphs.is_empty() {
result.mermaid_graphs = all_graphs;
}
Ok(result)
}
// ---------------------------------------------------------------------------
// Markdown generation
// ---------------------------------------------------------------------------
/// Generate markdown documentation from project info.
#[cocoindex::function]
async fn generate_markdown(
_ctx: &Ctx,
project_name: String,
info: CodebaseInfo,
file_infos: Vec<CodebaseInfo>,
) -> Result<String> {
let mut lines = vec![
format!("# {project_name}"),
String::new(),
"## Overview".into(),
String::new(),
info.summary.clone(),
String::new(),
];
if !info.public_classes.is_empty() || !info.public_functions.is_empty() {
lines.push("## Components".into());
lines.push(String::new());
if !info.public_classes.is_empty() {
lines.push("**Classes:**".into());
for cls in &info.public_classes {
lines.push(format!("- `{}`: {}", cls.name, cls.summary));
}
lines.push(String::new());
}
if !info.public_functions.is_empty() {
lines.push("**Functions:**".into());
for f in &info.public_functions {
let marker = if f.is_coco_function { " β
" } else { "" };
lines.push(format!("- `{}`{marker}: {}", f.signature, f.summary));
}
lines.push(String::new());
}
}
if !info.mermaid_graphs.is_empty() {
lines.push("## CocoIndex Pipeline".into());
lines.push(String::new());
for graph in &info.mermaid_graphs {
let content = graph.trim();
if content.starts_with("```") {
lines.push(content.to_string());
} else {
lines.push("```mermaid".into());
lines.push(content.to_string());
lines.push("```".into());
}
lines.push(String::new());
}
}
if file_infos.len() > 1 {
lines.push("## File Details".into());
lines.push(String::new());
for fi in &file_infos {
lines.push(format!("### {}", fi.name));
lines.push(String::new());
lines.push(fi.summary.clone());
lines.push(String::new());
}
}
lines.push("---".into());
lines.push(String::new());
lines.push("*β
= CocoIndex function*".into());
Ok(lines.join("\n"))
}
// ---------------------------------------------------------------------------
// Per-project processing component
// ---------------------------------------------------------------------------
/// Process one project: extract per-file info (one child component per file),
/// aggregate into a project summary, and write the markdown. Mounted as a
/// per-project processing component β the component-memo fast-path skips
/// projects whose files and this logic are unchanged.
#[cocoindex::function]
async fn process_project(
ctx: &Ctx,
project: (String, Vec<FileEntry>),
target: DirTarget,
) -> Result<()> {
let (project_name, files) = project;
// Extract per-file info β one child component per file (unchanged files skip).
let file_infos: Vec<CodebaseInfo> =
use_mount_each!(files.into_iter().map(|f| (f.key(), f)), |file| {
extract_file_info(ctx, file)
})
.await?;
// Aggregate into a project summary (memoized β unchanged projects skip the LLM).
let project_info =
aggregate_project_info(ctx, project_name.clone(), file_infos.clone()).await?;
// Generate and write the markdown.
let markdown = generate_markdown(ctx, project_name.clone(), project_info, file_infos).await?;
target.declare_file(ctx, &format!("{project_name}.md"), markdown.as_bytes())?;
Ok(())
}
// ---------------------------------------------------------------------------
// Main
// ---------------------------------------------------------------------------
#[tokio::main]
async fn main() -> Result<()> {
init_llm();
let args: Vec<String> = std::env::args().collect();
let root_dir = PathBuf::from(
args.get(1)
.map(|s| s.as_str())
.unwrap_or("../../../../examples"),
);
let output_dir = PathBuf::from(args.get(2).map(|s| s.as_str()).unwrap_or("./output"));
let app = cocoindex::App::open("multi_codebase_summarization", ".cocoindex_db").await?;
let stats = app
.run(move |ctx| async move {
// List subdirectories (each is a project)
let mut entries: Vec<_> = std::fs::read_dir(&root_dir)
.map_err(cocoindex::Error::Io)?
.filter_map(|e| e.ok())
.filter(|e| {
e.file_type().map(|t| t.is_dir()).unwrap_or(false)
&& !e.file_name().to_string_lossy().starts_with('.')
})
.collect();
entries.sort_by_key(|e| e.file_name());
let target = DirTarget::mount(&ctx, &output_dir)?;
let mut projects = Vec::new();
for entry in entries {
let project_name = entry.file_name().to_string_lossy().to_string();
let project_dir = entry.path();
// Match both root-level and nested Python files.
let files = cocoindex::resources::fs::walk(&project_dir, &["*.py", "**/*.py"])?;
let files: Vec<_> = files
.into_iter()
.filter(|f| !should_skip_python_file(f))
.collect();
let mut files = files;
files.sort_by_key(|f| f.key());
if files.is_empty() {
continue;
}
println!("Processing project: {project_name} ({} files)", files.len());
projects.push((project_name.clone(), (project_name, files)));
}
// One component per project (`project/<name>`), processed
// concurrently; unchanged projects are skipped wholesale.
mount_each!("project", projects, |project| {
process_project(ctx, project, target)
})
.await?;
println!("Wrote project summaries to {}", output_dir.display());
Ok(())
})
.await?;
println!("{stats}");
Ok(())
}