vibe_graph_api/lib.rs
1//! REST + WebSocket API service for Vibe-Graph.
2//!
3//! This crate provides a clean API layer that can be consumed by any frontend.
4//! It separates data serving from visualization concerns.
5//!
6//! ## Endpoints
7//!
8//! ### Graph & WebSocket API (stateful, for visualization)
9//!
10//! - `GET /api/health` - Health check with node/edge counts
11//! - `GET /api/graph` - Full SourceCodeGraph JSON
12//! - `GET /api/graph/nodes` - Nodes only
13//! - `GET /api/graph/edges` - Edges only
14//! - `GET /api/graph/metadata` - Graph metadata
15//! - `GET /api/git/changes` - Current git change snapshot
16//! - `GET /api/ws` - WebSocket for real-time updates
17//!
18//! ### Operations API (stateless, for CLI-like operations)
19//!
20//! - `POST /api/ops/sync` - Sync a codebase
21//! - `GET /api/ops/sync?source=...` - Sync with query params
22//! - `POST /api/ops/graph` - Build source code graph
23//! - `GET /api/ops/graph?path=...` - Build graph with query params
24//! - `GET /api/ops/status?path=...` - Get workspace status
25//! - `GET /api/ops/load?path=...` - Load project from .self
26//! - `DELETE /api/ops/clean?path=...` - Clean .self folder
27//! - `GET /api/ops/git-changes?path=...` - Get git changes
28//!
29//! ## Usage
30//!
31//! ```rust,no_run
32//! use vibe_graph_api::{create_api_router, create_api_state, create_ops_router};
33//! use vibe_graph_ops::{Config, OpsContext};
34//!
35//! // For visualization server with pre-loaded graph
36//! let graph = vibe_graph_core::SourceCodeGraph::default();
37//! let state = create_api_state(graph);
38//! let router = create_api_router(state);
39//!
40//! // For operations API
41//! let config = Config::load().unwrap();
42//! let ctx = OpsContext::new(config);
43//! let ops_router = create_ops_router(ctx);
44//! ```
45
46mod routes;
47mod types;
48mod ws;
49
50pub use routes::{create_api_router, create_full_api_router, create_ops_router};
51pub use types::{ApiResponse, ApiState, WsClientMessage, WsServerMessage};
52
53use std::sync::Arc;
54use tokio::sync::{broadcast, RwLock};
55use vibe_graph_core::{GitChangeSnapshot, SourceCodeGraph};
56
57/// Create a new API state with the given graph.
58pub fn create_api_state(graph: SourceCodeGraph) -> Arc<ApiState> {
59 let (tx, _) = broadcast::channel(100);
60 Arc::new(ApiState {
61 graph: Arc::new(RwLock::new(graph)),
62 git_changes: Arc::new(RwLock::new(GitChangeSnapshot::default())),
63 tx,
64 })
65}
66
67/// Create a new API state with the given graph and git changes.
68pub fn create_api_state_with_changes(
69 graph: SourceCodeGraph,
70 git_changes: GitChangeSnapshot,
71) -> Arc<ApiState> {
72 let (tx, _) = broadcast::channel(100);
73 Arc::new(ApiState {
74 graph: Arc::new(RwLock::new(graph)),
75 git_changes: Arc::new(RwLock::new(git_changes)),
76 tx,
77 })
78}