Implementation:1ay1 Agentty rag adapter
| Knowledge Sources | |
|---|---|
| Domains | RAG, Information_Retrieval |
| Last Updated | 2026-10-28 12:00 GMT |
Overview
Concrete adapter layer that bridges agentty's tool interface (search_code, search_docs) to the rag-cpp external submodule's hybrid retrieval pipeline — chunking, BM25+HNSW fusion, reranking, MMR diversification, and context injection.
Description
The rag adapter (in `src/rag/adapter.cpp`) is agentty's integration with the rag-cpp external submodule. The rag-cpp engine implements the retrieval pipeline (structure-aware chunking, hybrid BM25+HNSW retrieval, deterministic feature reranking, MMR diversification, near-duplicate removal, sentence-level compression). The adapter wraps this behind agentty's tool interface:
- search_code(query) — runs the full pipeline against the codebase index, returns ToolOutput with source-tagged snippets (file path, line numbers).
- search_docs(query) — same pipeline against documentation.
The adapter also handles the RagMode enum (On / FirstTurnOnly / Off) from `domain/rag_mode.hpp` for proactive pre-turn retrieval.
This is a Wrapper Doc: the retrieval engine (chunking, BM25, HNSW, reranker, MMR, compression) is implemented in the rag-cpp external submodule. This page documents the repo-specific adapter layer — how agentty configures and calls the engine through the search_code and search_docs tools.
Usage
- Model-initiated search: the model calls the `search_code` or `search_docs` tool with a query string.
- Proactive retrieval: before a turn, if RagMode is On (or FirstTurnOnly on the first turn), run retrieval and inject results.
Code Reference
Source Location
- Repository: agentty
- File: src/rag/adapter.cpp
- External Dependency: rag-cpp submodule (tree-sitter, BM25, HNSW, embedding model)
- Domain Type: include/agentty/domain/rag_mode.hpp
Signature
namespace agentty::rag {
// search_code tool: run the full retrieval pipeline against the
// codebase index. Returns source-tagged snippets for model consumption.
ToolOutput search_code(std::string_view query);
// search_docs tool: same pipeline against documentation.
ToolOutput search_docs(std::string_view query);
} // namespace agentty::rag
// Proactive retrieval mode (domain/rag_mode.hpp):
namespace agentty {
enum class RagMode : std::uint8_t {
On = 0, // proactive pre-turn retrieval on every turn
FirstTurnOnly, // retrieval only on a thread's first turn
Off, // no proactive injection (tools still work)
};
}
Import
// The search_code/search_docs tools are registered in the tool registry.
// RagMode is in:
#include "agentty/domain/rag_mode.hpp"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| query | string_view | Yes | Search query from the model's tool call |
| mode | RagMode | No | Proactive retrieval mode (On/FirstTurnOnly/Off) |
Outputs
| Name | Type | Description |
|---|---|---|
| return | ToolOutput | Source-tagged snippets (file path, line numbers) for model consumption |
Usage Examples
Model-Initiated Search
// The model calls the search_code tool:
// Tool call: search_code({"query": "ThreadLog append implementation"})
// The adapter runs:
// 1. Structure-aware chunking (tree-sitter, skip node_modules/.git)
// 2. Hybrid retrieval (BM25 + HNSW vector search, fused)
// 3. Deterministic feature reranking
// 4. MMR diversification + near-duplicate removal + chunk stitching
// 5. Sentence-level compression to byte budget
// 6. Format as ToolOutput with source tags
// Output to model:
// "src/io/thread_log.cpp:L140-160\n<code snippet>\n"
// "include/agentty/io/thread_log.hpp:L72-82\n<code snippet>\n"
Proactive Pre-Turn Retrieval
#include "agentty/domain/rag_mode.hpp"
using namespace agentty;
// Before dispatching the turn, check proactive mode:
if (thread.rag_mode == RagMode::On ||
(thread.rag_mode == RagMode::FirstTurnOnly && thread.is_first_turn)) {
// Run retrieval with the user's message as the query
auto results = rag::search_code(user_message.text);
// Inject results into the turn context
inject_context(results);
}
Related Pages
Implements Principle
- Principle:1ay1_Agentty_Hybrid_Retrieval
- Principle:1ay1_Agentty_Structure_Aware_Chunking
- Principle:1ay1_Agentty_Maximal_Marginal_Relevance
- Principle:1ay1_Agentty_Context_Injection