basemind 0.19.0

Full AI context layer over MCP — tree-sitter code-map, document RAG (PDF/Office/HTML/email + OCR + reranker), shared agent memory, on-demand web crawl, git history + blame + per-symbol diff. 300+ languages, 10+ coding-agent harnesses, content-addressed Fjall + LanceDB.
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
//! Code-map query subcommands: 1:1 with the MCP code-map tools.
//!
//! Each handler builds the matching `*Params` struct from clap args, calls the
//! identical `#[tool]` method on the in-process [`BasemindServer`], and renders
//! the result. No query logic lives here — parity is by construction.

use std::io::Write;

use anyhow::Result;
use clap::Subcommand;

use crate::mcp::BasemindServer;
use crate::mcp::params::*;
use crate::path::{RelPath, normalize_query_path};

use super::render::emit;
use super::run_tool;

/// Resolve a user-supplied CLI path into the repo-relative `RelPath` key the
/// index is keyed by (scanner-produced: no leading `./`, never absolute).
///
/// `query outline /abs/repo/src/foo.rs` and `query outline ./src/foo.rs` both
/// resolve to `src/foo.rs`. Paths that escape or fall outside the repository
/// can't match an indexed file, so we fall back to the raw input and let the
/// downstream tool report "file not indexed" rather than silently mangling it.
fn resolve_path(server: &BasemindServer, path: &str) -> RelPath {
    match normalize_query_path(path, &server.state.root) {
        Some(rel) => RelPath::from(rel),
        None => RelPath::from(path),
    }
}

#[derive(Subcommand, Debug)]
pub enum QueryCmd {
    /// File outline: symbols + imports, optionally calls + docs (L2).
    Outline {
        /// Repository-relative path.
        path: String,
        /// Also include calls + doc comments (L2).
        #[arg(long)]
        l2: bool,
    },
    /// Search symbols by name substring (alias of `search`).
    Symbol {
        needle: String,
        #[arg(long)]
        kind: Option<String>,
        #[arg(long)]
        limit: Option<u32>,
    },
    /// Search symbols by name substring.
    Search {
        needle: String,
        #[arg(long)]
        kind: Option<String>,
        #[arg(long)]
        limit: Option<u32>,
    },
    /// Call sites of any callee whose identifier matches `name`.
    References {
        name: String,
        #[arg(long)]
        limit: Option<u32>,
    },
    /// Callers of a specific definition (path + name + optional kind).
    Callers {
        path: String,
        name: String,
        #[arg(long)]
        kind: Option<String>,
        #[arg(long)]
        limit: Option<u32>,
    },
    /// Resolve the reference at a position to its scope-resolved definition.
    GotoDefinition {
        /// Repository-relative path of the file holding the reference.
        path: String,
        /// 1-based line of the reference identifier.
        line: u32,
        /// 0-based byte column of the reference within the line (default 0).
        #[arg(long, default_value_t = 0)]
        column: u32,
    },
    /// Types implementing / extending / inheriting from a trait or base class.
    Implementations {
        trait_name: String,
        #[arg(long)]
        language: Option<String>,
        #[arg(long)]
        limit: Option<u32>,
    },
    /// Transitive call-graph walk from a root function.
    CallGraph {
        name: String,
        #[arg(long, default_value = "callers")]
        direction: String,
        #[arg(long)]
        path: Option<String>,
        #[arg(long)]
        max_depth: Option<u32>,
        #[arg(long)]
        max_nodes: Option<u32>,
    },
    /// Architecture map ranked by graph centrality + git churn.
    ArchitectureMap {
        #[arg(long, default_value = "module")]
        granularity: String,
        #[arg(long)]
        focus: Option<String>,
        #[arg(long)]
        depth: Option<u32>,
        #[arg(long, default_value = "calls")]
        edges: String,
        #[arg(long, default_value_t = true)]
        include_churn: bool,
        #[arg(long)]
        churn_window: Option<u32>,
        #[arg(long)]
        max_nodes: Option<u32>,
        #[arg(long)]
        max_edges: Option<u32>,
        #[arg(long)]
        max_tokens: Option<u32>,
    },
    /// Regex content search across indexed files.
    Grep {
        pattern: String,
        #[arg(long)]
        language: Option<String>,
        #[arg(long)]
        path_contains: Option<String>,
        #[arg(long)]
        limit: Option<u32>,
        /// Suppress the 1-line before/after context for each match.
        #[arg(long = "no-context")]
        no_context: bool,
    },
    /// List indexed files, optionally filtered.
    ListFiles {
        #[arg(long)]
        path_contains: Option<String>,
        #[arg(long)]
        language: Option<String>,
        #[arg(long)]
        limit: Option<u32>,
    },
    /// High-level repo + cache state.
    Status,
    /// Workdir + branch + HEAD sha.
    RepoInfo,
    /// Files whose imports mention the given module (heuristic).
    Dependents { module: String },
    /// Search indexed code chunks — `hybrid` (RRF fusion, default), `semantic` (vector), or
    /// `keyword` (BM25). Returns pointers; fetch bodies with `get-chunk`. Needs `--features
    /// code-search`.
    SearchCode {
        query: String,
        #[arg(long)]
        limit: Option<u32>,
        /// Retrieval lane: `hybrid` (default), `semantic`, or `keyword`.
        #[arg(long)]
        mode: Option<String>,
        /// Run the cross-encoder rerank pass over the fused hits (first call downloads a model).
        #[arg(long)]
        rerank: bool,
        /// Reranker preset name (default `bge-reranker-base`).
        #[arg(long)]
        rerank_preset: Option<String>,
        #[arg(long)]
        format: Option<String>,
    },
    /// Fetch one code chunk's body by path (from a `search-code` hit). Needs `--features
    /// code-search`.
    GetChunk {
        /// Repository-relative path of the source file.
        path: String,
        #[arg(long)]
        chunk_id: Option<String>,
        #[arg(long)]
        byte_start: Option<u32>,
    },
    /// Expand a symbol to its raw source body (the inverse of an outline entry).
    Expand {
        /// Repository-relative path of the indexed file.
        path: String,
        /// Symbol name (matched exactly, case-sensitive).
        name: String,
        /// Kind filter to disambiguate (e.g. `function`, `struct`, `method`).
        #[arg(long)]
        kind: Option<String>,
    },
}

pub async fn run(server: &BasemindServer, cmd: QueryCmd, json: bool, out: &mut impl Write) -> Result<()> {
    match cmd {
        QueryCmd::Outline { path, l2 } => {
            let p = OutlineParams {
                path: resolve_path(server, &path),
                l2,
                max_tokens: None,
                format: None,
            };
            let r = run_tool("outline", server.outline(Parameters(Lenient(p))).await)?;
            emit("outline", &r, json, out)
        }
        QueryCmd::Symbol { needle, kind, limit } | QueryCmd::Search { needle, kind, limit } => {
            let p = SearchSymbolsParams {
                needle,
                kind,
                limit,
                max_tokens: None,
                format: None,
                cursor: None,
            };
            let r = run_tool("search_symbols", server.search_symbols(Parameters(Lenient(p))).await)?;
            emit("search_symbols", &r, json, out)
        }
        QueryCmd::References { name, limit } => {
            let p = FindReferencesParams {
                name,
                limit,
                max_tokens: None,
                format: None,
                cursor: None,
            };
            let r = run_tool("find_references", server.find_references(Parameters(Lenient(p))).await)?;
            emit("find_references", &r, json, out)
        }
        QueryCmd::Callers {
            path,
            name,
            kind,
            limit,
        } => {
            let p = FindCallersParams {
                path: resolve_path(server, &path),
                name,
                kind,
                limit,
                max_tokens: None,
                cursor: None,
            };
            let r = run_tool("find_callers", server.find_callers(Parameters(Lenient(p))).await)?;
            emit("find_callers", &r, json, out)
        }
        QueryCmd::GotoDefinition { path, line, column } => {
            let p = GotoDefinitionParams {
                path: resolve_path(server, &path),
                line,
                column,
            };
            let r = run_tool("goto_definition", server.goto_definition(Parameters(Lenient(p))).await)?;
            emit("goto_definition", &r, json, out)
        }
        QueryCmd::Implementations {
            trait_name,
            language,
            limit,
        } => {
            let p = FindImplementationsParams {
                trait_name,
                language,
                limit,
                max_tokens: None,
                cursor: None,
            };
            let r = run_tool(
                "find_implementations",
                server.find_implementations(Parameters(Lenient(p))).await,
            )?;
            emit("find_implementations", &r, json, out)
        }
        QueryCmd::CallGraph {
            name,
            direction,
            path,
            max_depth,
            max_nodes,
        } => {
            let p = CallGraphParams {
                name,
                direction,
                path: path.map(|s| resolve_path(server, &s)),
                max_depth,
                max_nodes,
            };
            let r = run_tool("call_graph", server.call_graph(Parameters(p)).await)?;
            emit("call_graph", &r, json, out)
        }
        QueryCmd::ArchitectureMap {
            granularity,
            focus,
            depth,
            edges,
            include_churn,
            churn_window,
            max_nodes,
            max_edges,
            max_tokens,
        } => {
            let p = ArchitectureMapParams {
                granularity,
                focus,
                depth,
                edges,
                include_churn,
                churn_window,
                max_nodes,
                max_edges,
                max_tokens,
            };
            let r = run_tool("architecture_map", server.architecture_map(Parameters(p)).await)?;
            emit("architecture_map", &r, json, out)
        }
        QueryCmd::Grep {
            pattern,
            language,
            path_contains,
            limit,
            no_context,
        } => {
            let p = WorkspaceGrepParams {
                pattern,
                language,
                path_contains,
                limit,
                max_tokens: None,
                format: None,
                include_context: !no_context,
                cursor: None,
            };
            let r = run_tool("workspace_grep", server.workspace_grep(Parameters(Lenient(p))).await)?;
            emit("workspace_grep", &r, json, out)
        }
        QueryCmd::ListFiles {
            path_contains,
            language,
            limit,
        } => {
            let p = ListFilesParams {
                path_contains,
                language,
                limit,
                max_tokens: None,
                format: None,
                cursor: None,
            };
            let r = run_tool("list_files", server.list_files(Parameters(p)).await)?;
            emit("list_files", &r, json, out)
        }
        QueryCmd::Status => {
            let r = run_tool("status", server.status(Parameters(StatusParams {})).await)?;
            emit("status", &r, json, out)
        }
        QueryCmd::RepoInfo => {
            let r = run_tool("repo_info", server.repo_info(Parameters(RepoInfoParams {})).await)?;
            emit("repo_info", &r, json, out)
        }
        QueryCmd::Dependents { module } => {
            let p = DependentsParams { module };
            let r = run_tool("dependents", server.dependents(Parameters(Lenient(p))).await)?;
            emit("dependents", &r, json, out)
        }
        QueryCmd::SearchCode {
            query,
            limit,
            mode,
            rerank,
            rerank_preset,
            format,
        } => {
            let p = SearchCodeParams {
                query,
                limit,
                max_tokens: None,
                mode,
                reranker_enabled: rerank.then_some(true),
                reranker_preset: rerank_preset,
                reranker_top_k: None,
                format,
            };
            let r = run_tool("search_code", server.search_code(Parameters(Lenient(p))).await)?;
            emit("search_code", &r, json, out)
        }
        QueryCmd::GetChunk {
            path,
            chunk_id,
            byte_start,
        } => {
            let p = GetChunkParams {
                path: resolve_path(server, &path),
                chunk_id,
                byte_start,
            };
            let r = run_tool("get_chunk", server.get_chunk(Parameters(Lenient(p))).await)?;
            emit("get_chunk", &r, json, out)
        }
        QueryCmd::Expand { path, name, kind } => {
            let p = ExpandParams {
                path: resolve_path(server, &path),
                name,
                kind,
            };
            let r = run_tool("expand", server.expand(Parameters(p)).await)?;
            emit("expand", &r, json, out)
        }
    }
}