aKode 0.8.0: grafo del codice locale (tree-sitter + git) e strumento code_graph, memorie ancorate ai simboli con stato stale, client MCP akode-mcp-client
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
122
akode-rs/cli/src/code_graph_cmd.rs
Normal file
122
akode-rs/cli/src/code_graph_cmd.rs
Normal file
@@ -0,0 +1,122 @@
|
||||
//! `akode code-graph`: builds and queries the local code graph of a repository (tree-sitter and
|
||||
//! git, no model). The same index is used by the `code_graph` tool.
|
||||
|
||||
use std::path::PathBuf;
|
||||
|
||||
use akode_code_graph::CodeIndex;
|
||||
use akode_code_graph::anchor::Citation;
|
||||
use akode_code_graph::anchor::anchors_for_memory;
|
||||
use akode_core::config::Config;
|
||||
use clap::Parser;
|
||||
use clap::Subcommand;
|
||||
|
||||
#[derive(Debug, Parser)]
|
||||
pub struct CodeGraphCommand {
|
||||
/// Directory inside the repository (default: current directory).
|
||||
#[arg(long, global = true)]
|
||||
cwd: Option<PathBuf>,
|
||||
|
||||
/// Answer budget in tokens.
|
||||
#[arg(long, global = true, default_value_t = 1500)]
|
||||
budget: usize,
|
||||
|
||||
#[command(subcommand)]
|
||||
action: CodeGraphAction,
|
||||
}
|
||||
|
||||
#[derive(Debug, Subcommand)]
|
||||
enum CodeGraphAction {
|
||||
/// Update the index (incremental from git) and print what was done.
|
||||
Index,
|
||||
/// Delete the index of this repository and build it again.
|
||||
Rebuild,
|
||||
/// Symbols whose name matches.
|
||||
Search { query: String },
|
||||
/// Where a symbol is defined.
|
||||
Definition { symbol: String },
|
||||
/// Who calls (or uses) a symbol.
|
||||
Callers { symbol: String },
|
||||
/// What a symbol calls.
|
||||
Callees { symbol: String },
|
||||
/// Files linked to a file or symbol.
|
||||
Related { target: String },
|
||||
/// Outline of a file or folder.
|
||||
Map { path: Option<String> },
|
||||
/// Short map of the repository.
|
||||
Summary,
|
||||
/// Anchors for a citation `path[:lines]`, as sent to the memory server (no source code).
|
||||
Anchors {
|
||||
citation: String,
|
||||
#[arg(long)]
|
||||
quote: Option<String>,
|
||||
},
|
||||
}
|
||||
|
||||
pub async fn run(cmd: CodeGraphCommand, config: &Config) -> anyhow::Result<()> {
|
||||
let cwd = match cmd.cwd {
|
||||
Some(cwd) => cwd,
|
||||
None => std::env::current_dir()?,
|
||||
};
|
||||
let home = config.codex_home.to_path_buf();
|
||||
let budget = cmd.budget;
|
||||
let action = cmd.action;
|
||||
let text = tokio::task::spawn_blocking(move || -> anyhow::Result<String> {
|
||||
let mut index = CodeIndex::open_for_dir(&home, &cwd).map_err(anyhow::Error::msg)?;
|
||||
if matches!(action, CodeGraphAction::Rebuild) {
|
||||
let path = CodeIndex::default_path(&home, index.root());
|
||||
let root = index.root().to_path_buf();
|
||||
drop(index);
|
||||
for suffix in ["", "-wal", "-shm"] {
|
||||
let _ = std::fs::remove_file(format!("{}{suffix}", path.display()));
|
||||
}
|
||||
index = CodeIndex::open(&path, &root).map_err(anyhow::Error::msg)?;
|
||||
}
|
||||
let report = index.update().map_err(anyhow::Error::msg)?;
|
||||
let prefix = cwd
|
||||
.canonicalize()
|
||||
.ok()
|
||||
.and_then(|cwd| cwd.strip_prefix(index.root()).ok().map(|p| p.to_string_lossy().to_string()))
|
||||
.unwrap_or_default();
|
||||
let answer: Result<String, String> = match action {
|
||||
CodeGraphAction::Index | CodeGraphAction::Rebuild => Ok(format!(
|
||||
"{}: mode {}, {} path(s) checked, {} parsed, {} removed in {} ms; index: {} files, {} symbols, {} references ({})",
|
||||
index.root().display(),
|
||||
report.mode,
|
||||
report.checked,
|
||||
report.parsed,
|
||||
report.removed,
|
||||
report.millis,
|
||||
report.files,
|
||||
report.symbols,
|
||||
report.references,
|
||||
CodeIndex::default_path(&home, index.root()).display()
|
||||
)),
|
||||
CodeGraphAction::Search { query } => index.search(&query, None, None, 30, budget),
|
||||
CodeGraphAction::Definition { symbol } => index.definition(&symbol, 12, budget),
|
||||
CodeGraphAction::Callers { symbol } => index.callers(&symbol, budget),
|
||||
CodeGraphAction::Callees { symbol } => index.callees(&symbol, budget),
|
||||
CodeGraphAction::Related { target } => index.related(&target, &prefix, budget),
|
||||
CodeGraphAction::Map { path } => index.module_map(path.as_deref().unwrap_or(&prefix), &prefix, budget),
|
||||
CodeGraphAction::Summary => index.repo_summary(budget),
|
||||
CodeGraphAction::Anchors { citation, quote } => {
|
||||
let (path, lines) = match citation.rsplit_once(':') {
|
||||
Some((path, lines)) if lines.chars().all(|c| c.is_ascii_digit() || c == '-') => {
|
||||
(path.to_string(), Some(lines.to_string()))
|
||||
}
|
||||
_ => (citation.clone(), None),
|
||||
};
|
||||
let path = index.normalize_path(&path, &prefix);
|
||||
let anchors = anchors_for_memory(
|
||||
index.root(),
|
||||
&[Citation { path, lines, quote }],
|
||||
"",
|
||||
);
|
||||
Ok(serde_json::to_string_pretty(&anchors).unwrap_or_default())
|
||||
}
|
||||
};
|
||||
answer.map_err(anyhow::Error::msg)
|
||||
})
|
||||
.await??;
|
||||
println!("{text}");
|
||||
Ok(())
|
||||
}
|
||||
@@ -43,6 +43,7 @@ use supports_color::Stream;
|
||||
#[global_allocator]
|
||||
static ALLOCATOR: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc;
|
||||
|
||||
mod code_graph_cmd;
|
||||
mod config_override_args;
|
||||
mod daemon_install;
|
||||
mod doctor;
|
||||
@@ -159,6 +160,9 @@ enum Subcommand {
|
||||
/// Update aKode to the latest release published on the configured repository.
|
||||
Update(update_cmd::UpdateCommand),
|
||||
|
||||
/// Build and query the local code graph of a repository (tree-sitter and git, no model).
|
||||
CodeGraph(code_graph_cmd::CodeGraphCommand),
|
||||
|
||||
/// Diagnose local aKode installation, config, auth, and runtime health.
|
||||
Doctor(DoctorCommand),
|
||||
|
||||
@@ -1515,6 +1519,17 @@ async fn dispatch_cli(
|
||||
.await?;
|
||||
update_cmd::run(update_cli, &config).await?;
|
||||
}
|
||||
Some(Subcommand::CodeGraph(code_graph_cli)) => {
|
||||
reject_remote_mode_for_subcommand(
|
||||
root_remote.as_deref(),
|
||||
root_remote_auth_token_env.as_deref(),
|
||||
"code-graph",
|
||||
)?;
|
||||
let config =
|
||||
local_config::load_config(&root_config_overrides, LoaderOverrides::default())
|
||||
.await?;
|
||||
code_graph_cmd::run(code_graph_cli, &config).await?;
|
||||
}
|
||||
Some(Subcommand::Doctor(doctor_cli)) => {
|
||||
reject_remote_mode_for_subcommand(
|
||||
root_remote.as_deref(),
|
||||
@@ -2081,6 +2096,7 @@ fn unsupported_subcommand_name_for_strict_config(
|
||||
Some(Subcommand::Logout(_)) => Some("logout"),
|
||||
Some(Subcommand::Completion(_)) => Some("completion"),
|
||||
Some(Subcommand::Update(_)) => Some("update"),
|
||||
Some(Subcommand::CodeGraph(_)) => Some("code-graph"),
|
||||
Some(Subcommand::Sandbox(_)) => Some("sandbox"),
|
||||
Some(Subcommand::Debug(_)) => Some("debug"),
|
||||
Some(Subcommand::Execpolicy(_)) => Some("execpolicy"),
|
||||
|
||||
Reference in New Issue
Block a user