Skip to main content

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//! ### Git Commands API (for executing git operations)
30//!
31//! - `POST /api/git/cmd/add` - Stage files
32//! - `POST /api/git/cmd/commit` - Create commit
33//! - `POST /api/git/cmd/reset` - Unstage files
34//! - `GET /api/git/cmd/branches` - List branches
35//! - `POST /api/git/cmd/checkout` - Switch branch
36//! - `GET /api/git/cmd/log` - Commit history
37//! - `GET /api/git/cmd/diff` - Get diff
38//!
39//! ## Usage
40//!
41//! ```rust,no_run
42//! use vibe_graph_api::{create_api_router, create_api_state, create_ops_router};
43//! use vibe_graph_ops::{Config, OpsContext};
44//!
45//! // For visualization server with pre-loaded graph
46//! let graph = vibe_graph_core::SourceCodeGraph::default();
47//! let state = create_api_state(graph);
48//! let router = create_api_router(state);
49//!
50//! // For operations API
51//! let config = Config::load().unwrap();
52//! let ctx = OpsContext::new(config);
53//! let ops_router = create_ops_router(ctx);
54//! ```
55
56mod routes;
57mod types;
58mod ws;
59
60pub use routes::{
61    create_api_router, create_file_router, create_full_api_router,
62    create_full_api_router_with_git, create_full_api_router_with_git_multi,
63    create_git_commands_router, create_git_commands_router_multi, create_ops_router,
64};
65pub use types::{ApiResponse, ApiState, WsClientMessage, WsServerMessage};
66
67use std::sync::Arc;
68use tokio::sync::{broadcast, RwLock};
69use vibe_graph_core::{GitChangeSnapshot, SourceCodeGraph};
70
71/// Create a new API state with the given graph.
72pub fn create_api_state(graph: SourceCodeGraph) -> Arc<ApiState> {
73    let (tx, _) = broadcast::channel(100);
74    Arc::new(ApiState {
75        graph: Arc::new(RwLock::new(graph)),
76        git_changes: Arc::new(RwLock::new(GitChangeSnapshot::default())),
77        tx,
78    })
79}
80
81/// Create a new API state with the given graph and git changes.
82pub fn create_api_state_with_changes(
83    graph: SourceCodeGraph,
84    git_changes: GitChangeSnapshot,
85) -> Arc<ApiState> {
86    let (tx, _) = broadcast::channel(100);
87    Arc::new(ApiState {
88        graph: Arc::new(RwLock::new(graph)),
89        git_changes: Arc::new(RwLock::new(git_changes)),
90        tx,
91    })
92}