Skip to main content

ai_memory/
lib.rs

1// Copyright 2026 AlphaOne LLC
2// SPDX-License-Identifier: Apache-2.0
3
4#![recursion_limit = "256"]
5// The library target was added by the proptest infra (Agent G) to expose
6// production modules to the integration test crate. The bin target's
7// clippy run already gates CI — re-running pedantic against the same
8// modules through the lib target would re-flag the same pre-existing
9// lint backlog the bin target already passes. Allow at the lib level;
10// the bin target is the authoritative gate for production-code linting.
11#![allow(clippy::pedantic, clippy::all)]
12
13// Library interface for ai-memory. Exposes public modules for testing and external use.
14
15pub mod autonomy;
16pub mod bench;
17pub mod cli;
18pub mod color;
19pub mod config;
20pub mod curator;
21pub mod daemon_runtime;
22pub mod db;
23pub mod embeddings;
24pub mod errors;
25pub mod federation;
26pub mod handlers;
27pub mod hnsw;
28pub mod identity;
29pub mod llm;
30pub mod mcp;
31pub mod metrics;
32pub mod mine;
33pub mod models;
34pub mod replication;
35pub mod reranker;
36pub mod subscriptions;
37pub mod tls;
38pub mod toon;
39pub mod validate;
40
41#[cfg(feature = "sal")]
42pub mod migrate;
43
44#[cfg(feature = "sal")]
45pub mod store;
46
47// ---------------------------------------------------------------------------
48// Router construction
49// ---------------------------------------------------------------------------
50//
51// `build_router` is the single source of truth for the daemon's HTTP route
52// table. It is exposed through the lib crate so the integration test suite
53// can construct an in-process `axum::Router` and exercise endpoints via
54// `Router::oneshot()` instead of spawning a subprocess + curl, which:
55//   1. eliminates the OS-level daemon-spawn overhead per test (~200-500ms),
56//   2. exposes the routes' line coverage to `cargo llvm-cov` (subprocess
57//      coverage attribution requires extra `LLVM_PROFILE_FILE` plumbing
58//      that the test harness doesn't provide), and
59//   3. lets test failures surface assertion-level diagnostics instead of
60//      "curl returned 000" black holes.
61//
62// The function takes the same two state values that `serve()` constructs
63// inline (the API key middleware state and the composite app state) so
64// the production binary and the test harness share a single route map.
65pub fn build_router(
66    api_key_state: handlers::ApiKeyState,
67    app_state: handlers::AppState,
68) -> axum::Router {
69    use axum::{
70        extract::DefaultBodyLimit,
71        routing::{delete, get, post, put},
72    };
73    use tower_http::{cors::CorsLayer, trace::TraceLayer};
74
75    axum::Router::new()
76        .route("/api/v1/health", get(handlers::health))
77        // v0.6.0.0: Prometheus scrape endpoint. Exposed at both /metrics
78        // (the community convention) and /api/v1/metrics (consistent with
79        // the rest of the REST surface).
80        .route("/metrics", get(handlers::prometheus_metrics))
81        .route("/api/v1/metrics", get(handlers::prometheus_metrics))
82        .route("/api/v1/memories", get(handlers::list_memories))
83        .route("/api/v1/memories", post(handlers::create_memory))
84        .route("/api/v1/memories/bulk", post(handlers::bulk_create))
85        .route("/api/v1/memories/{id}", get(handlers::get_memory))
86        .route("/api/v1/memories/{id}", put(handlers::update_memory))
87        .route("/api/v1/memories/{id}", delete(handlers::delete_memory))
88        .route(
89            "/api/v1/memories/{id}/promote",
90            post(handlers::promote_memory),
91        )
92        .route("/api/v1/search", get(handlers::search_memories))
93        .route("/api/v1/recall", get(handlers::recall_memories_get))
94        .route("/api/v1/recall", post(handlers::recall_memories_post))
95        .route("/api/v1/forget", post(handlers::forget_memories))
96        .route("/api/v1/consolidate", post(handlers::consolidate_memories))
97        .route(
98            "/api/v1/contradictions",
99            get(handlers::detect_contradictions),
100        )
101        .route("/api/v1/links", post(handlers::create_link))
102        .route("/api/v1/links", delete(handlers::delete_link))
103        .route("/api/v1/links/{id}", get(handlers::get_links))
104        // HTTP parity for MCP-only tools. The `/api/v1/namespaces` surface
105        // serves three verbs: GET lists namespaces OR (when ?namespace=…)
106        // fetches the namespace standard, POST sets a standard, DELETE
107        // clears one. S34/S35 use the query-string form; the path form
108        // (`/api/v1/namespaces/{ns}/standard`) is kept for MCP-tool parity.
109        .route(
110            "/api/v1/namespaces",
111            get(handlers::get_namespace_standard_qs),
112        )
113        .route(
114            "/api/v1/namespaces",
115            post(handlers::set_namespace_standard_qs),
116        )
117        .route(
118            "/api/v1/namespaces",
119            delete(handlers::clear_namespace_standard_qs),
120        )
121        .route(
122            "/api/v1/namespaces/{ns}/standard",
123            post(handlers::set_namespace_standard),
124        )
125        .route(
126            "/api/v1/namespaces/{ns}/standard",
127            get(handlers::get_namespace_standard),
128        )
129        .route(
130            "/api/v1/namespaces/{ns}/standard",
131            delete(handlers::clear_namespace_standard),
132        )
133        // Pillar 1 / Stream A — hierarchical namespace taxonomy.
134        .route("/api/v1/taxonomy", get(handlers::get_taxonomy))
135        // Pillar 2 / Stream D — pre-write near-duplicate check.
136        .route("/api/v1/check_duplicate", post(handlers::check_duplicate))
137        // Pillar 2 / Stream B — entity registry.
138        .route("/api/v1/entities", post(handlers::entity_register))
139        .route(
140            "/api/v1/entities/by_alias",
141            get(handlers::entity_get_by_alias),
142        )
143        // Pillar 2 / Stream C — KG timeline.
144        .route("/api/v1/kg/timeline", get(handlers::kg_timeline))
145        // Pillar 2 / Stream C — KG link supersession.
146        .route("/api/v1/kg/invalidate", post(handlers::kg_invalidate))
147        // Pillar 2 / Stream C — KG outbound traversal.
148        .route("/api/v1/kg/query", post(handlers::kg_query))
149        .route("/api/v1/stats", get(handlers::get_stats))
150        .route("/api/v1/gc", post(handlers::run_gc))
151        .route("/api/v1/export", get(handlers::export_memories))
152        .route("/api/v1/import", post(handlers::import_memories))
153        .route("/api/v1/archive", get(handlers::list_archive))
154        .route("/api/v1/archive", post(handlers::archive_by_ids))
155        .route("/api/v1/archive", delete(handlers::purge_archive))
156        .route(
157            "/api/v1/archive/{id}/restore",
158            post(handlers::restore_archive),
159        )
160        .route("/api/v1/archive/stats", get(handlers::archive_stats))
161        .route("/api/v1/agents", get(handlers::list_agents))
162        .route("/api/v1/agents", post(handlers::register_agent))
163        .route("/api/v1/pending", get(handlers::list_pending))
164        .route(
165            "/api/v1/pending/{id}/approve",
166            post(handlers::approve_pending),
167        )
168        .route(
169            "/api/v1/pending/{id}/reject",
170            post(handlers::reject_pending),
171        )
172        // Phase 3 foundation (issue #224) — peer-to-peer sync endpoints.
173        .route("/api/v1/sync/push", post(handlers::sync_push))
174        .route("/api/v1/sync/since", get(handlers::sync_since))
175        // HTTP parity for MCP-only tools.
176        .route("/api/v1/capabilities", get(handlers::get_capabilities))
177        .route("/api/v1/notify", post(handlers::notify))
178        .route("/api/v1/inbox", get(handlers::get_inbox))
179        .route("/api/v1/subscriptions", post(handlers::subscribe))
180        .route("/api/v1/subscriptions", delete(handlers::unsubscribe))
181        .route("/api/v1/subscriptions", get(handlers::list_subscriptions))
182        .route("/api/v1/session/start", post(handlers::session_start))
183        .layer(axum::middleware::from_fn_with_state(
184            api_key_state,
185            handlers::api_key_auth,
186        ))
187        .layer(TraceLayer::new_for_http())
188        .layer(DefaultBodyLimit::max(2 * 1024 * 1024))
189        .layer(CorsLayer::new())
190        .with_state(app_state)
191}