ai-librarian 0.2.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- ai_librarian-0.2.0/LICENSE +21 -0
- ai_librarian-0.2.0/PKG-INFO +339 -0
- ai_librarian-0.2.0/README.md +306 -0
- ai_librarian-0.2.0/pyproject.toml +113 -0
- ai_librarian-0.2.0/setup.cfg +4 -0
- ai_librarian-0.2.0/src/ai_librarian/__init__.py +3 -0
- ai_librarian-0.2.0/src/ai_librarian/cli.py +672 -0
- ai_librarian-0.2.0/src/ai_librarian/config.py +1195 -0
- ai_librarian-0.2.0/src/ai_librarian/indexer/graph.py +439 -0
- ai_librarian-0.2.0/src/ai_librarian/indexer/install_spec.py +553 -0
- ai_librarian-0.2.0/src/ai_librarian/indexer/pipeline.py +1688 -0
- ai_librarian-0.2.0/src/ai_librarian/indexer/source_loader.py +755 -0
- ai_librarian-0.2.0/src/ai_librarian/indexer/usage.py +129 -0
- ai_librarian-0.2.0/src/ai_librarian/llm/__init__.py +1 -0
- ai_librarian-0.2.0/src/ai_librarian/llm/gateway.py +484 -0
- ai_librarian-0.2.0/src/ai_librarian/llm/prompts.py +134 -0
- ai_librarian-0.2.0/src/ai_librarian/llm/transport.py +363 -0
- ai_librarian-0.2.0/src/ai_librarian/logging_setup.py +221 -0
- ai_librarian-0.2.0/src/ai_librarian/mcp/__init__.py +7 -0
- ai_librarian-0.2.0/src/ai_librarian/mcp/server.py +150 -0
- ai_librarian-0.2.0/src/ai_librarian/models.py +765 -0
- ai_librarian-0.2.0/src/ai_librarian/parsing/base.py +469 -0
- ai_librarian-0.2.0/src/ai_librarian/parsing/clang_ng_backend.py +510 -0
- ai_librarian-0.2.0/src/ai_librarian/parsing/clang_session.py +963 -0
- ai_librarian-0.2.0/src/ai_librarian/parsing/compile_db.py +721 -0
- ai_librarian-0.2.0/src/ai_librarian/parsing/cpp_adapter.py +1020 -0
- ai_librarian-0.2.0/src/ai_librarian/parsing/executor.py +186 -0
- ai_librarian-0.2.0/src/ai_librarian/parsing/make_db.py +303 -0
- ai_librarian-0.2.0/src/ai_librarian/parsing/pool.py +203 -0
- ai_librarian-0.2.0/src/ai_librarian/parsing/proto.py +84 -0
- ai_librarian-0.2.0/src/ai_librarian/parsing/python_adapter.py +583 -0
- ai_librarian-0.2.0/src/ai_librarian/parsing/registry.py +193 -0
- ai_librarian-0.2.0/src/ai_librarian/parsing/worker.py +126 -0
- ai_librarian-0.2.0/src/ai_librarian/query/__init__.py +13 -0
- ai_librarian-0.2.0/src/ai_librarian/query/engine.py +244 -0
- ai_librarian-0.2.0/src/ai_librarian/query/fetcher.py +196 -0
- ai_librarian-0.2.0/src/ai_librarian/service.py +151 -0
- ai_librarian-0.2.0/src/ai_librarian/storage/__init__.py +1 -0
- ai_librarian-0.2.0/src/ai_librarian/storage/base.py +146 -0
- ai_librarian-0.2.0/src/ai_librarian/storage/sqlite_store.py +478 -0
- ai_librarian-0.2.0/src/ai_librarian/websearch/__init__.py +7 -0
- ai_librarian-0.2.0/src/ai_librarian/websearch/fetch.py +198 -0
- ai_librarian-0.2.0/src/ai_librarian/websearch/guard.py +93 -0
- ai_librarian-0.2.0/src/ai_librarian/websearch/http.py +152 -0
- ai_librarian-0.2.0/src/ai_librarian/websearch/search.py +533 -0
- ai_librarian-0.2.0/src/ai_librarian.egg-info/PKG-INFO +339 -0
- ai_librarian-0.2.0/src/ai_librarian.egg-info/SOURCES.txt +49 -0
- ai_librarian-0.2.0/src/ai_librarian.egg-info/dependency_links.txt +1 -0
- ai_librarian-0.2.0/src/ai_librarian.egg-info/entry_points.txt +3 -0
- ai_librarian-0.2.0/src/ai_librarian.egg-info/requires.txt +27 -0
- ai_librarian-0.2.0/src/ai_librarian.egg-info/top_level.txt +1 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Davide Libenzi
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,339 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: ai-librarian
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: Two-phase library discovery index for AI coding agents: search_library + fetch_symbol_source
|
|
5
|
+
License-Expression: MIT
|
|
6
|
+
Requires-Python: >=3.10
|
|
7
|
+
Description-Content-Type: text/markdown
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Requires-Dist: openai>=1.30
|
|
10
|
+
Requires-Dist: tenacity>=8.2
|
|
11
|
+
Requires-Dist: pyyaml>=6.0
|
|
12
|
+
Requires-Dist: requests>=2.31
|
|
13
|
+
Requires-Dist: pypdf>=4.0
|
|
14
|
+
Requires-Dist: beautifulsoup4>=4.12
|
|
15
|
+
Requires-Dist: lxml>=5.0
|
|
16
|
+
Requires-Dist: sqlite-vec>=0.1.6
|
|
17
|
+
Requires-Dist: tree-sitter>=0.22
|
|
18
|
+
Requires-Dist: tree-sitter-python>=0.23
|
|
19
|
+
Requires-Dist: tree-sitter-cpp>=0.23
|
|
20
|
+
Provides-Extra: index
|
|
21
|
+
Requires-Dist: libclang-ng>=22.1; extra == "index"
|
|
22
|
+
Requires-Dist: compiledb>=0.10; extra == "index"
|
|
23
|
+
Provides-Extra: mcp
|
|
24
|
+
Requires-Dist: mcp>=2.3; extra == "mcp"
|
|
25
|
+
Provides-Extra: dev
|
|
26
|
+
Requires-Dist: pytest>=8.0; extra == "dev"
|
|
27
|
+
Requires-Dist: ruff>=0.4; extra == "dev"
|
|
28
|
+
Requires-Dist: yapf>=0.40; extra == "dev"
|
|
29
|
+
Requires-Dist: ai-librarian[index,mcp]; extra == "dev"
|
|
30
|
+
Provides-Extra: all
|
|
31
|
+
Requires-Dist: ai-librarian[dev,mcp]; extra == "all"
|
|
32
|
+
Dynamic: license-file
|
|
33
|
+
|
|
34
|
+
# ai_librarian
|
|
35
|
+
|
|
36
|
+
[](https://github.com/davidel/ai_librarian/actions/workflows/ci.yml)
|
|
37
|
+
[](https://opensource.org/licenses/MIT)
|
|
38
|
+
[](https://www.python.org/downloads/)
|
|
39
|
+
|
|
40
|
+
Give your AI coding agent a librarian: a durable, searchable index of
|
|
41
|
+
your private code so it stops reinventing wheels that already exist.
|
|
42
|
+
|
|
43
|
+
## The problem
|
|
44
|
+
|
|
45
|
+
LLMs know `numpy`, PyTorch, and the C++ standard library — that's in
|
|
46
|
+
their training data. They don't know your internal monorepo, your
|
|
47
|
+
in-house RPC client, or the weird log parser your team wrote three
|
|
48
|
+
years ago. Asked to work with proprietary code, the model guesses,
|
|
49
|
+
hallucinates, or reinvents the utility badly. You end up with a second,
|
|
50
|
+
divergent implementation you now own.
|
|
51
|
+
|
|
52
|
+
Pasting raw files into the chat doesn't fix it. The agent still has to
|
|
53
|
+
read hundreds of files to discover that `yourpkg.io.load_parquet`
|
|
54
|
+
exists — and it pays that reading cost on every single task.
|
|
55
|
+
|
|
56
|
+
What the agent needs is pre-digested knowledge: built once, queried
|
|
57
|
+
cheaply, available forever.
|
|
58
|
+
|
|
59
|
+
## What ai_librarian does
|
|
60
|
+
|
|
61
|
+
ai_librarian builds a searchable index of your codebase. It reads your
|
|
62
|
+
repositories, parses every file, and records each symbol — functions,
|
|
63
|
+
classes, methods, their signatures, where they live, and what they
|
|
64
|
+
call. An LLM then writes a plain-English summary for each symbol so
|
|
65
|
+
your agent can understand what something does without reading the
|
|
66
|
+
source.
|
|
67
|
+
|
|
68
|
+
Later, your agent asks questions in natural language and gets back a
|
|
69
|
+
small handful of compact metadata cards — no walls of source code.
|
|
70
|
+
Search is hybrid (full-text plus vector embeddings) with optional
|
|
71
|
+
LLM-assisted rewriting and reranking. If the LLM is down or missing,
|
|
72
|
+
search still works — it just falls back to plain retrieval.
|
|
73
|
+
|
|
74
|
+
The agent gets two tools:
|
|
75
|
+
|
|
76
|
+
- **`search_library`** — ask in plain English, get back ranked
|
|
77
|
+
metadata cards (symbol name, signature, summary, where it's defined).
|
|
78
|
+
A few dozen tokens per candidate — enough to decide if it's relevant.
|
|
79
|
+
- **`fetch_symbol_source`** — give it a symbol ID, get the actual source
|
|
80
|
+
code: local file text when a source root is configured, a GitHub or
|
|
81
|
+
GitLab viewer URL with line numbers, or an explicit "unavailable."
|
|
82
|
+
Your agent pays for source code only for the symbol it actually
|
|
83
|
+
chooses.
|
|
84
|
+
|
|
85
|
+
Everything lives in a single SQLite file (with full-text search and
|
|
86
|
+
vector embeddings). No services to run, no infrastructure to manage.
|
|
87
|
+
|
|
88
|
+
## Install
|
|
89
|
+
|
|
90
|
+
```Shell
|
|
91
|
+
pip install git+https://github.com/davidel/ai_librarian.git
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
If you want the MCP server (to connect to an AI coding agent):
|
|
95
|
+
|
|
96
|
+
```Shell
|
|
97
|
+
pip install 'ai-librarian[mcp] @ git+https://github.com/davidel/ai_librarian.git'
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
If you want to build indexes (includes C/C++ parsing tooling):
|
|
101
|
+
|
|
102
|
+
```Shell
|
|
103
|
+
pip install 'ai-librarian[index] @ git+https://github.com/davidel/ai_librarian.git'
|
|
104
|
+
sudo apt-get install -y gcc clang make cmake meson ninja-build
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
For Bazel workspaces you'll also need `bazelisk` (provides `bazel`)
|
|
108
|
+
and the `bazel-compile-commands` converter.
|
|
109
|
+
|
|
110
|
+
For a development install (everything plus test tooling):
|
|
111
|
+
|
|
112
|
+
```Shell
|
|
113
|
+
pip install -e '.[dev]'
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
## Configure
|
|
117
|
+
|
|
118
|
+
ai_librarian is configured with a YAML file. See
|
|
119
|
+
[`config.example.yaml`](config.example.yaml) for all the knobs with
|
|
120
|
+
comments. Setting precedence is: CLI flags, then `AI_LIBRARIAN_*`
|
|
121
|
+
environment variables, then the config file, then built-in defaults.
|
|
122
|
+
|
|
123
|
+
```Shell
|
|
124
|
+
export AI_LIBRARIAN_CONFIG=~/etc/ai_librarian.toml
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
At minimum you'll want to point it at an LLM. The config defines
|
|
128
|
+
providers (OpenAI-compatible endpoints) and model profiles. Two profiles
|
|
129
|
+
are used:
|
|
130
|
+
|
|
131
|
+
- **`indexer_llm`** — for writing symbol summaries at index time. You
|
|
132
|
+
want a strong model here (it writes the documentation your agent
|
|
133
|
+
will rely on).
|
|
134
|
+
- **`query_llm`** — for rewriting and reranking searches at query time.
|
|
135
|
+
A fast local model (like Ollama) is usually plenty.
|
|
136
|
+
|
|
137
|
+
Example snippet:
|
|
138
|
+
|
|
139
|
+
```yaml
|
|
140
|
+
providers:
|
|
141
|
+
openrouter:
|
|
142
|
+
base_url: "https://openrouter.ai/api/v1"
|
|
143
|
+
api_key_env: "OPENROUTER_API_KEY"
|
|
144
|
+
|
|
145
|
+
llm:
|
|
146
|
+
indexer_llm:
|
|
147
|
+
model: "openrouter:z-ai/glm-5.3-flash:floor,llama3.1:8b"
|
|
148
|
+
temperature: 0.2
|
|
149
|
+
max_tokens: 4096
|
|
150
|
+
query_llm:
|
|
151
|
+
model: "llama3.1:8b"
|
|
152
|
+
base_url: "http://localhost:11434/v1"
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
Models are specified as a comma-separated stack — entries are tried
|
|
156
|
+
left to right. If one fails it cools off and the primary is retried
|
|
157
|
+
later. So the example above tries OpenRouter first, falls back to a
|
|
158
|
+
local Ollama model if needed.
|
|
159
|
+
|
|
160
|
+
The index store lives at `index_store_uri` (default:
|
|
161
|
+
`~/.ai_librarian/index.db`) — a single SQLite file. Nothing else to
|
|
162
|
+
run.
|
|
163
|
+
|
|
164
|
+
## Index your code
|
|
165
|
+
|
|
166
|
+
Point ai_librarian at a library and let it build the index:
|
|
167
|
+
|
|
168
|
+
```Shell
|
|
169
|
+
ai-librarian auto-index ~/code/mylib
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
`auto-index` walks the package, parses everything, and — importantly —
|
|
173
|
+
follows external dependencies it discovers along the way, indexing
|
|
174
|
+
those too until it reaches a fixpoint (or a cap you set).
|
|
175
|
+
|
|
176
|
+
If you just want to index a single package without dependency
|
|
177
|
+
discovery:
|
|
178
|
+
|
|
179
|
+
```Shell
|
|
180
|
+
ai-librarian index ~/code/mylib
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
Indexing is resilient. If a symbol summary fails to generate, that
|
|
184
|
+
symbol is deferred and the run continues. Structural records (names,
|
|
185
|
+
signatures, call edges) are always saved, so full-text search keeps
|
|
186
|
+
working even if some LLM-authored summaries are missing. At the end
|
|
187
|
+
you get a tally of what succeeded and what was deferred.
|
|
188
|
+
|
|
189
|
+
### C++ compilation flags
|
|
190
|
+
|
|
191
|
+
For C/C++ code, ai_librarian needs real compiler flags to parse
|
|
192
|
+
accurately. It looks for a `compile_commands.json` (from CMake, Meson,
|
|
193
|
+
or Bazel) next to your package. If it can't find one, it can try to
|
|
194
|
+
generate one by running your project's build tools in a scratch
|
|
195
|
+
directory. You can control this with the `compile_commands` setting
|
|
196
|
+
in your config — `auto` (the default), a path to a specific database,
|
|
197
|
+
or `off` to skip compile-db generation and use heuristic flags only.
|
|
198
|
+
|
|
199
|
+
## Search
|
|
200
|
+
|
|
201
|
+
Once you have an index, search it from the shell:
|
|
202
|
+
|
|
203
|
+
```Shell
|
|
204
|
+
ai-librarian search "parse a binary log record with a checksum"
|
|
205
|
+
ai-librarian search "read parquet with column pruning" --limit 5 --json
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
Results are metadata cards — symbol name, qualified name, signature,
|
|
209
|
+
summary, and where it's defined. No source text, so the results stay
|
|
210
|
+
compact and cheap to read.
|
|
211
|
+
|
|
212
|
+
Exit codes: `0` = success, `1` = refused (empty index or unknown
|
|
213
|
+
symbol), `2` = invalid query.
|
|
214
|
+
|
|
215
|
+
To fetch the actual source for a symbol you found:
|
|
216
|
+
|
|
217
|
+
```Shell
|
|
218
|
+
ai-librarian fetch-source mypkg.io.load_parquet --with-deps
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
This returns the source text (from a configured local source root), a
|
|
222
|
+
viewer URL with line anchors (GitHub `#L5-L8`, GitLab `#L5-8`), or
|
|
223
|
+
`unavailable` if it can't be located.
|
|
224
|
+
|
|
225
|
+
## Connect to your AI coding agent
|
|
226
|
+
|
|
227
|
+
ai_librarian speaks MCP (Model Context Protocol) over stdio — the
|
|
228
|
+
transport every MCP client uses. Run the server:
|
|
229
|
+
|
|
230
|
+
```Shell
|
|
231
|
+
ai-librarian-mcp
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
Then register it with your client (example for Claude Code, Cursor,
|
|
235
|
+
etc.):
|
|
236
|
+
|
|
237
|
+
```JSON
|
|
238
|
+
{
|
|
239
|
+
"mcpServers": {
|
|
240
|
+
"ai_librarian": {
|
|
241
|
+
"command": "ai-librarian-mcp",
|
|
242
|
+
"env": { "AI_LIBRARIAN_CONFIG": "/home/you/etc/ai_librarian.toml" }
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
The agent then gets two tools:
|
|
249
|
+
|
|
250
|
+
- `search_library(query, limit, rewrite, rerank)` — natural language
|
|
251
|
+
search over your indexed code.
|
|
252
|
+
- `fetch_symbol_source(symbol_id, include_internal_deps)` — get the
|
|
253
|
+
source for a specific symbol.
|
|
254
|
+
|
|
255
|
+
The MCP server logs to stderr only, so the stdio stream stays clean
|
|
256
|
+
for the protocol.
|
|
257
|
+
|
|
258
|
+
## Handling private or external APIs
|
|
259
|
+
|
|
260
|
+
When indexing finds imports it can't resolve locally (external
|
|
261
|
+
libraries, internal packages not yet indexed), ai_librarian has a
|
|
262
|
+
websearch lane. It looks up the unknown APIs on the public web and
|
|
263
|
+
injects what it finds into the summarize prompts, so the index knows
|
|
264
|
+
something about external symbols too.
|
|
265
|
+
|
|
266
|
+
By default it uses keyless search engines (DuckDuckGo, Yahoo), so it
|
|
267
|
+
works out of the box. You can opt in to keyed backends (Tavily, Brave,
|
|
268
|
+
Google, Serper, SerpAPI, Brightdata) or a self-hosted SearXNG instance
|
|
269
|
+
by adding them to the `backends` list and setting the corresponding
|
|
270
|
+
API key environment variables. All lookups are cached per package, so
|
|
271
|
+
re-indexing doesn't repeat the network calls.
|
|
272
|
+
|
|
273
|
+
If you'd rather never do web I/O, set `policy = "never"` in the
|
|
274
|
+
`[websearch]` config section.
|
|
275
|
+
|
|
276
|
+
## Keeping keys out of sandboxed environments
|
|
277
|
+
|
|
278
|
+
If your indexer or agent runs somewhere you don't fully trust, use
|
|
279
|
+
[`tools/superproxy/`](tools/superproxy/README.md). It's a small proxy
|
|
280
|
+
that holds the API keys (OpenRouter, OpenAI, Serper, Tavily, Brave)
|
|
281
|
+
and injects them into requests. The sandbox talks to the proxy; the
|
|
282
|
+
proxy talks to the providers. Keys never enter the sandbox.
|
|
283
|
+
|
|
284
|
+
## How it works under the hood
|
|
285
|
+
|
|
286
|
+
You don't need to read this to use ai_librarian, but it helps if
|
|
287
|
+
something behaves unexpectedly.
|
|
288
|
+
|
|
289
|
+
**Index time:** walk sources → parse with tree-sitter → compute a
|
|
290
|
+
content hash → write symbol records → generate summaries with the LLM
|
|
291
|
+
→ store in SQLite. Failures are skipped and tallied, never fatal
|
|
292
|
+
mid-run. The exit code reflects the final tally.
|
|
293
|
+
|
|
294
|
+
**Query time:** your question → optional LLM rewrite → hybrid recall
|
|
295
|
+
(full-text + vector search) → reciprocal rank fusion → optional LLM
|
|
296
|
+
rerank → metadata cards back to you. Every LLM stage degrades
|
|
297
|
+
gracefully to plain retrieval.
|
|
298
|
+
|
|
299
|
+
**Fetch:** symbol ID → file coordinates → look up in configured
|
|
300
|
+
source roots → local text, viewer URL, or unavailable.
|
|
301
|
+
|
|
302
|
+
The `symbol_id` is the qualified name inside its package, prefixed
|
|
303
|
+
with `::` for package root — for example `mypkg.io.load_parquet` or
|
|
304
|
+
`::main`. C++ overloads that share coordinates get `#2`, `#3`, etc.
|
|
305
|
+
suffixes so call edges always point at the right symbol.
|
|
306
|
+
|
|
307
|
+
Observability: logging goes to stderr (required for MCP stdio) with
|
|
308
|
+
extended levels (`SPAM`, `VERBOSE`, `DEBUG0..3`) and per-module
|
|
309
|
+
overrides.
|
|
310
|
+
|
|
311
|
+
## Development
|
|
312
|
+
|
|
313
|
+
If you want to hack on ai_librarian itself:
|
|
314
|
+
|
|
315
|
+
- Python is formatted with `yapf` (Google style, 2-space indent — see
|
|
316
|
+
`.style.yapf`). C/C++ uses `clang-format` (Google style).
|
|
317
|
+
- `ruff` must stay clean. Lint-suppressing comments need a reason — no
|
|
318
|
+
blanket `# noqa`.
|
|
319
|
+
- Tests use only the public API, never `_`-prefixed internals.
|
|
320
|
+
- Design decisions live in commit messages, not just code comments.
|
|
321
|
+
|
|
322
|
+
```Shell
|
|
323
|
+
yapf --diff --recursive src tests # check formatting
|
|
324
|
+
ruff check src tests # lint
|
|
325
|
+
pytest tests/python/ # Python test suite
|
|
326
|
+
```
|
|
327
|
+
|
|
328
|
+
For C++ tooling (optional):
|
|
329
|
+
|
|
330
|
+
```Shell
|
|
331
|
+
cmake -S . -B build && cmake --build build && ctest --test-dir build
|
|
332
|
+
```
|
|
333
|
+
|
|
334
|
+
The code and this README are the source of truth for how the system
|
|
335
|
+
works. Remaining work is tracked in [`docs/roadmap.md`](docs/roadmap.md).
|
|
336
|
+
|
|
337
|
+
## License
|
|
338
|
+
|
|
339
|
+
MIT. See [LICENSE](LICENSE).
|
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+
# ai_librarian
|
|
2
|
+
|
|
3
|
+
[](https://github.com/davidel/ai_librarian/actions/workflows/ci.yml)
|
|
4
|
+
[](https://opensource.org/licenses/MIT)
|
|
5
|
+
[](https://www.python.org/downloads/)
|
|
6
|
+
|
|
7
|
+
Give your AI coding agent a librarian: a durable, searchable index of
|
|
8
|
+
your private code so it stops reinventing wheels that already exist.
|
|
9
|
+
|
|
10
|
+
## The problem
|
|
11
|
+
|
|
12
|
+
LLMs know `numpy`, PyTorch, and the C++ standard library — that's in
|
|
13
|
+
their training data. They don't know your internal monorepo, your
|
|
14
|
+
in-house RPC client, or the weird log parser your team wrote three
|
|
15
|
+
years ago. Asked to work with proprietary code, the model guesses,
|
|
16
|
+
hallucinates, or reinvents the utility badly. You end up with a second,
|
|
17
|
+
divergent implementation you now own.
|
|
18
|
+
|
|
19
|
+
Pasting raw files into the chat doesn't fix it. The agent still has to
|
|
20
|
+
read hundreds of files to discover that `yourpkg.io.load_parquet`
|
|
21
|
+
exists — and it pays that reading cost on every single task.
|
|
22
|
+
|
|
23
|
+
What the agent needs is pre-digested knowledge: built once, queried
|
|
24
|
+
cheaply, available forever.
|
|
25
|
+
|
|
26
|
+
## What ai_librarian does
|
|
27
|
+
|
|
28
|
+
ai_librarian builds a searchable index of your codebase. It reads your
|
|
29
|
+
repositories, parses every file, and records each symbol — functions,
|
|
30
|
+
classes, methods, their signatures, where they live, and what they
|
|
31
|
+
call. An LLM then writes a plain-English summary for each symbol so
|
|
32
|
+
your agent can understand what something does without reading the
|
|
33
|
+
source.
|
|
34
|
+
|
|
35
|
+
Later, your agent asks questions in natural language and gets back a
|
|
36
|
+
small handful of compact metadata cards — no walls of source code.
|
|
37
|
+
Search is hybrid (full-text plus vector embeddings) with optional
|
|
38
|
+
LLM-assisted rewriting and reranking. If the LLM is down or missing,
|
|
39
|
+
search still works — it just falls back to plain retrieval.
|
|
40
|
+
|
|
41
|
+
The agent gets two tools:
|
|
42
|
+
|
|
43
|
+
- **`search_library`** — ask in plain English, get back ranked
|
|
44
|
+
metadata cards (symbol name, signature, summary, where it's defined).
|
|
45
|
+
A few dozen tokens per candidate — enough to decide if it's relevant.
|
|
46
|
+
- **`fetch_symbol_source`** — give it a symbol ID, get the actual source
|
|
47
|
+
code: local file text when a source root is configured, a GitHub or
|
|
48
|
+
GitLab viewer URL with line numbers, or an explicit "unavailable."
|
|
49
|
+
Your agent pays for source code only for the symbol it actually
|
|
50
|
+
chooses.
|
|
51
|
+
|
|
52
|
+
Everything lives in a single SQLite file (with full-text search and
|
|
53
|
+
vector embeddings). No services to run, no infrastructure to manage.
|
|
54
|
+
|
|
55
|
+
## Install
|
|
56
|
+
|
|
57
|
+
```Shell
|
|
58
|
+
pip install git+https://github.com/davidel/ai_librarian.git
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
If you want the MCP server (to connect to an AI coding agent):
|
|
62
|
+
|
|
63
|
+
```Shell
|
|
64
|
+
pip install 'ai-librarian[mcp] @ git+https://github.com/davidel/ai_librarian.git'
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
If you want to build indexes (includes C/C++ parsing tooling):
|
|
68
|
+
|
|
69
|
+
```Shell
|
|
70
|
+
pip install 'ai-librarian[index] @ git+https://github.com/davidel/ai_librarian.git'
|
|
71
|
+
sudo apt-get install -y gcc clang make cmake meson ninja-build
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
For Bazel workspaces you'll also need `bazelisk` (provides `bazel`)
|
|
75
|
+
and the `bazel-compile-commands` converter.
|
|
76
|
+
|
|
77
|
+
For a development install (everything plus test tooling):
|
|
78
|
+
|
|
79
|
+
```Shell
|
|
80
|
+
pip install -e '.[dev]'
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## Configure
|
|
84
|
+
|
|
85
|
+
ai_librarian is configured with a YAML file. See
|
|
86
|
+
[`config.example.yaml`](config.example.yaml) for all the knobs with
|
|
87
|
+
comments. Setting precedence is: CLI flags, then `AI_LIBRARIAN_*`
|
|
88
|
+
environment variables, then the config file, then built-in defaults.
|
|
89
|
+
|
|
90
|
+
```Shell
|
|
91
|
+
export AI_LIBRARIAN_CONFIG=~/etc/ai_librarian.toml
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
At minimum you'll want to point it at an LLM. The config defines
|
|
95
|
+
providers (OpenAI-compatible endpoints) and model profiles. Two profiles
|
|
96
|
+
are used:
|
|
97
|
+
|
|
98
|
+
- **`indexer_llm`** — for writing symbol summaries at index time. You
|
|
99
|
+
want a strong model here (it writes the documentation your agent
|
|
100
|
+
will rely on).
|
|
101
|
+
- **`query_llm`** — for rewriting and reranking searches at query time.
|
|
102
|
+
A fast local model (like Ollama) is usually plenty.
|
|
103
|
+
|
|
104
|
+
Example snippet:
|
|
105
|
+
|
|
106
|
+
```yaml
|
|
107
|
+
providers:
|
|
108
|
+
openrouter:
|
|
109
|
+
base_url: "https://openrouter.ai/api/v1"
|
|
110
|
+
api_key_env: "OPENROUTER_API_KEY"
|
|
111
|
+
|
|
112
|
+
llm:
|
|
113
|
+
indexer_llm:
|
|
114
|
+
model: "openrouter:z-ai/glm-5.3-flash:floor,llama3.1:8b"
|
|
115
|
+
temperature: 0.2
|
|
116
|
+
max_tokens: 4096
|
|
117
|
+
query_llm:
|
|
118
|
+
model: "llama3.1:8b"
|
|
119
|
+
base_url: "http://localhost:11434/v1"
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
Models are specified as a comma-separated stack — entries are tried
|
|
123
|
+
left to right. If one fails it cools off and the primary is retried
|
|
124
|
+
later. So the example above tries OpenRouter first, falls back to a
|
|
125
|
+
local Ollama model if needed.
|
|
126
|
+
|
|
127
|
+
The index store lives at `index_store_uri` (default:
|
|
128
|
+
`~/.ai_librarian/index.db`) — a single SQLite file. Nothing else to
|
|
129
|
+
run.
|
|
130
|
+
|
|
131
|
+
## Index your code
|
|
132
|
+
|
|
133
|
+
Point ai_librarian at a library and let it build the index:
|
|
134
|
+
|
|
135
|
+
```Shell
|
|
136
|
+
ai-librarian auto-index ~/code/mylib
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
`auto-index` walks the package, parses everything, and — importantly —
|
|
140
|
+
follows external dependencies it discovers along the way, indexing
|
|
141
|
+
those too until it reaches a fixpoint (or a cap you set).
|
|
142
|
+
|
|
143
|
+
If you just want to index a single package without dependency
|
|
144
|
+
discovery:
|
|
145
|
+
|
|
146
|
+
```Shell
|
|
147
|
+
ai-librarian index ~/code/mylib
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
Indexing is resilient. If a symbol summary fails to generate, that
|
|
151
|
+
symbol is deferred and the run continues. Structural records (names,
|
|
152
|
+
signatures, call edges) are always saved, so full-text search keeps
|
|
153
|
+
working even if some LLM-authored summaries are missing. At the end
|
|
154
|
+
you get a tally of what succeeded and what was deferred.
|
|
155
|
+
|
|
156
|
+
### C++ compilation flags
|
|
157
|
+
|
|
158
|
+
For C/C++ code, ai_librarian needs real compiler flags to parse
|
|
159
|
+
accurately. It looks for a `compile_commands.json` (from CMake, Meson,
|
|
160
|
+
or Bazel) next to your package. If it can't find one, it can try to
|
|
161
|
+
generate one by running your project's build tools in a scratch
|
|
162
|
+
directory. You can control this with the `compile_commands` setting
|
|
163
|
+
in your config — `auto` (the default), a path to a specific database,
|
|
164
|
+
or `off` to skip compile-db generation and use heuristic flags only.
|
|
165
|
+
|
|
166
|
+
## Search
|
|
167
|
+
|
|
168
|
+
Once you have an index, search it from the shell:
|
|
169
|
+
|
|
170
|
+
```Shell
|
|
171
|
+
ai-librarian search "parse a binary log record with a checksum"
|
|
172
|
+
ai-librarian search "read parquet with column pruning" --limit 5 --json
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
Results are metadata cards — symbol name, qualified name, signature,
|
|
176
|
+
summary, and where it's defined. No source text, so the results stay
|
|
177
|
+
compact and cheap to read.
|
|
178
|
+
|
|
179
|
+
Exit codes: `0` = success, `1` = refused (empty index or unknown
|
|
180
|
+
symbol), `2` = invalid query.
|
|
181
|
+
|
|
182
|
+
To fetch the actual source for a symbol you found:
|
|
183
|
+
|
|
184
|
+
```Shell
|
|
185
|
+
ai-librarian fetch-source mypkg.io.load_parquet --with-deps
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
This returns the source text (from a configured local source root), a
|
|
189
|
+
viewer URL with line anchors (GitHub `#L5-L8`, GitLab `#L5-8`), or
|
|
190
|
+
`unavailable` if it can't be located.
|
|
191
|
+
|
|
192
|
+
## Connect to your AI coding agent
|
|
193
|
+
|
|
194
|
+
ai_librarian speaks MCP (Model Context Protocol) over stdio — the
|
|
195
|
+
transport every MCP client uses. Run the server:
|
|
196
|
+
|
|
197
|
+
```Shell
|
|
198
|
+
ai-librarian-mcp
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
Then register it with your client (example for Claude Code, Cursor,
|
|
202
|
+
etc.):
|
|
203
|
+
|
|
204
|
+
```JSON
|
|
205
|
+
{
|
|
206
|
+
"mcpServers": {
|
|
207
|
+
"ai_librarian": {
|
|
208
|
+
"command": "ai-librarian-mcp",
|
|
209
|
+
"env": { "AI_LIBRARIAN_CONFIG": "/home/you/etc/ai_librarian.toml" }
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
The agent then gets two tools:
|
|
216
|
+
|
|
217
|
+
- `search_library(query, limit, rewrite, rerank)` — natural language
|
|
218
|
+
search over your indexed code.
|
|
219
|
+
- `fetch_symbol_source(symbol_id, include_internal_deps)` — get the
|
|
220
|
+
source for a specific symbol.
|
|
221
|
+
|
|
222
|
+
The MCP server logs to stderr only, so the stdio stream stays clean
|
|
223
|
+
for the protocol.
|
|
224
|
+
|
|
225
|
+
## Handling private or external APIs
|
|
226
|
+
|
|
227
|
+
When indexing finds imports it can't resolve locally (external
|
|
228
|
+
libraries, internal packages not yet indexed), ai_librarian has a
|
|
229
|
+
websearch lane. It looks up the unknown APIs on the public web and
|
|
230
|
+
injects what it finds into the summarize prompts, so the index knows
|
|
231
|
+
something about external symbols too.
|
|
232
|
+
|
|
233
|
+
By default it uses keyless search engines (DuckDuckGo, Yahoo), so it
|
|
234
|
+
works out of the box. You can opt in to keyed backends (Tavily, Brave,
|
|
235
|
+
Google, Serper, SerpAPI, Brightdata) or a self-hosted SearXNG instance
|
|
236
|
+
by adding them to the `backends` list and setting the corresponding
|
|
237
|
+
API key environment variables. All lookups are cached per package, so
|
|
238
|
+
re-indexing doesn't repeat the network calls.
|
|
239
|
+
|
|
240
|
+
If you'd rather never do web I/O, set `policy = "never"` in the
|
|
241
|
+
`[websearch]` config section.
|
|
242
|
+
|
|
243
|
+
## Keeping keys out of sandboxed environments
|
|
244
|
+
|
|
245
|
+
If your indexer or agent runs somewhere you don't fully trust, use
|
|
246
|
+
[`tools/superproxy/`](tools/superproxy/README.md). It's a small proxy
|
|
247
|
+
that holds the API keys (OpenRouter, OpenAI, Serper, Tavily, Brave)
|
|
248
|
+
and injects them into requests. The sandbox talks to the proxy; the
|
|
249
|
+
proxy talks to the providers. Keys never enter the sandbox.
|
|
250
|
+
|
|
251
|
+
## How it works under the hood
|
|
252
|
+
|
|
253
|
+
You don't need to read this to use ai_librarian, but it helps if
|
|
254
|
+
something behaves unexpectedly.
|
|
255
|
+
|
|
256
|
+
**Index time:** walk sources → parse with tree-sitter → compute a
|
|
257
|
+
content hash → write symbol records → generate summaries with the LLM
|
|
258
|
+
→ store in SQLite. Failures are skipped and tallied, never fatal
|
|
259
|
+
mid-run. The exit code reflects the final tally.
|
|
260
|
+
|
|
261
|
+
**Query time:** your question → optional LLM rewrite → hybrid recall
|
|
262
|
+
(full-text + vector search) → reciprocal rank fusion → optional LLM
|
|
263
|
+
rerank → metadata cards back to you. Every LLM stage degrades
|
|
264
|
+
gracefully to plain retrieval.
|
|
265
|
+
|
|
266
|
+
**Fetch:** symbol ID → file coordinates → look up in configured
|
|
267
|
+
source roots → local text, viewer URL, or unavailable.
|
|
268
|
+
|
|
269
|
+
The `symbol_id` is the qualified name inside its package, prefixed
|
|
270
|
+
with `::` for package root — for example `mypkg.io.load_parquet` or
|
|
271
|
+
`::main`. C++ overloads that share coordinates get `#2`, `#3`, etc.
|
|
272
|
+
suffixes so call edges always point at the right symbol.
|
|
273
|
+
|
|
274
|
+
Observability: logging goes to stderr (required for MCP stdio) with
|
|
275
|
+
extended levels (`SPAM`, `VERBOSE`, `DEBUG0..3`) and per-module
|
|
276
|
+
overrides.
|
|
277
|
+
|
|
278
|
+
## Development
|
|
279
|
+
|
|
280
|
+
If you want to hack on ai_librarian itself:
|
|
281
|
+
|
|
282
|
+
- Python is formatted with `yapf` (Google style, 2-space indent — see
|
|
283
|
+
`.style.yapf`). C/C++ uses `clang-format` (Google style).
|
|
284
|
+
- `ruff` must stay clean. Lint-suppressing comments need a reason — no
|
|
285
|
+
blanket `# noqa`.
|
|
286
|
+
- Tests use only the public API, never `_`-prefixed internals.
|
|
287
|
+
- Design decisions live in commit messages, not just code comments.
|
|
288
|
+
|
|
289
|
+
```Shell
|
|
290
|
+
yapf --diff --recursive src tests # check formatting
|
|
291
|
+
ruff check src tests # lint
|
|
292
|
+
pytest tests/python/ # Python test suite
|
|
293
|
+
```
|
|
294
|
+
|
|
295
|
+
For C++ tooling (optional):
|
|
296
|
+
|
|
297
|
+
```Shell
|
|
298
|
+
cmake -S . -B build && cmake --build build && ctest --test-dir build
|
|
299
|
+
```
|
|
300
|
+
|
|
301
|
+
The code and this README are the source of truth for how the system
|
|
302
|
+
works. Remaining work is tracked in [`docs/roadmap.md`](docs/roadmap.md).
|
|
303
|
+
|
|
304
|
+
## License
|
|
305
|
+
|
|
306
|
+
MIT. See [LICENSE](LICENSE).
|