magellan 3.3.1

Deterministic codebase mapping tool for local development
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
//! Web UI server for Magellan
//!
//! Provides a simple web interface for exploring code graphs.
//! Uses axum (like codemcp) for the HTTP server.

use axum::{
    extract::{Query, State},
    http::StatusCode,
    response::{Html, Json},
    routing::get,
    Router,
};
use serde::Deserialize;
use std::path::PathBuf;
use std::sync::Arc;
use tower_http::cors::{Any, CorsLayer};

use crate::context;
use crate::graph::CodeGraph;

/// Application state shared across handlers
pub struct AppState {
    pub db_path: PathBuf,
}

/// Build the axum router with the given state
pub fn create_app(state: Arc<AppState>) -> Router {
    // CORS for local development
    let cors = CorsLayer::new()
        .allow_origin(Any)
        .allow_methods(Any)
        .allow_headers(Any);

    Router::new()
        // HTML explorer page
        .route("/", get(|| async { get_index_html() }))
        // API routes
        .route("/api/summary", get(handle_summary))
        .route("/api/symbols", get(handle_list_symbols))
        .route("/api/symbol", get(handle_get_symbol))
        .route("/api/file", get(handle_get_file))
        .route("/api/callers", get(handle_get_callers))
        .route("/api/callees", get(handle_get_callees))
        .with_state(state)
        .layer(cors)
}

/// Start the web server
pub async fn run_web_server(db_path: PathBuf, host: String, port: u16) -> anyhow::Result<()> {
    let state = Arc::new(AppState {
        db_path: db_path.clone(),
    });
    let app = create_app(state);

    let addr = format!("{}:{}", host, port);
    println!("🌐 Magellan Web UI starting at http://{}", addr);
    println!("   API endpoints:");
    println!("   - GET /api/summary");
    println!("   - GET /api/symbols");
    println!("   - GET /api/symbol?name=<name>");
    println!("   - GET /api/file?path=...");
    println!("   - GET /api/callers?name=<name>&file=<path>");
    println!("   - GET /api/callees?name=<name>&file=<path>");
    println!("   - Static files served from ./web-ui/");

    let listener = tokio::net::TcpListener::bind(&addr).await?;
    axum::serve(listener, app).await?;

    Ok(())
}

#[derive(Debug, Deserialize)]
struct CallersQuery {
    name: String,
    #[serde(default)]
    file: Option<String>,
}

async fn handle_get_callers(
    State(state): State<Arc<AppState>>,
    Query(params): Query<CallersQuery>,
) -> Result<Json<Vec<context::query::SymbolListItem>>, StatusCode> {
    let mut graph = CodeGraph::open(&state.db_path).map_err(|_| StatusCode::SERVICE_UNAVAILABLE)?;
    match context::query::get_callers(&mut graph, &params.name, params.file.as_deref()) {
        Ok(callers) => Ok(Json(callers)),
        Err(e) => {
            eprintln!("Callers query failed: {}", e);
            Err(StatusCode::INTERNAL_SERVER_ERROR)
        }
    }
}

async fn handle_get_callees(
    State(state): State<Arc<AppState>>,
    Query(params): Query<CallersQuery>,
) -> Result<Json<Vec<context::query::SymbolListItem>>, StatusCode> {
    let mut graph = CodeGraph::open(&state.db_path).map_err(|_| StatusCode::SERVICE_UNAVAILABLE)?;
    match context::query::get_callees(&mut graph, &params.name, params.file.as_deref()) {
        Ok(callees) => Ok(Json(callees)),
        Err(e) => {
            eprintln!("Callees query failed: {}", e);
            Err(StatusCode::INTERNAL_SERVER_ERROR)
        }
    }
}

// ============================================================================
// API Handlers
// ============================================================================

/// GET /api/summary - Project overview
async fn handle_summary(
    State(state): State<Arc<AppState>>,
) -> Result<Json<SummaryResponse>, StatusCode> {
    let mut graph = CodeGraph::open(&state.db_path).map_err(|_| StatusCode::SERVICE_UNAVAILABLE)?;

    let total_calls = graph
        .count_calls()
        .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;

    match context::query::get_project_summary(&mut graph) {
        Ok(summary) => Ok(Json(SummaryResponse {
            total_files: summary.total_files,
            total_symbols: summary.total_symbols,
            total_calls,
        })),
        Err(e) => {
            eprintln!("Summary query failed: {}", e);
            Err(StatusCode::SERVICE_UNAVAILABLE)
        }
    }
}

/// GET /api/symbols - List symbols with pagination
async fn handle_list_symbols(
    State(state): State<Arc<AppState>>,
    Query(params): Query<ListSymbolsParams>,
) -> Result<Json<context::query::PaginatedResult<context::query::SymbolListItem>>, StatusCode> {
    let mut graph = CodeGraph::open(&state.db_path).map_err(|_| StatusCode::SERVICE_UNAVAILABLE)?;
    let query = context::query::ListQuery {
        kind: params.kind,
        file_pattern: None,
        page: params.page,
        page_size: Some(params.page_size),
        cursor: params.cursor,
    };
    match context::query::list_symbols(&mut graph, &query) {
        Ok(result) => Ok(Json(result)),
        Err(e) => {
            eprintln!("List symbols query failed: {}", e);
            Err(StatusCode::INTERNAL_SERVER_ERROR)
        }
    }
}

#[derive(Debug, Deserialize)]
struct SymbolQuery {
    name: String,
    #[serde(default)]
    file: Option<String>,
}

/// GET /api/symbol - Get symbol detail
async fn handle_get_symbol(
    State(state): State<Arc<AppState>>,
    Query(params): Query<SymbolQuery>,
) -> Result<Json<context::query::SymbolDetail>, StatusCode> {
    let mut graph = CodeGraph::open(&state.db_path).map_err(|_| StatusCode::SERVICE_UNAVAILABLE)?;
    match context::query::get_symbol_detail(&mut graph, &params.name, params.file.as_deref()) {
        Ok(detail) => Ok(Json(detail)),
        Err(e) => {
            let err_str = format!("{}", e);
            if err_str.contains("not found") {
                Err(StatusCode::NOT_FOUND)
            } else {
                eprintln!("Symbol detail query failed: {}", e);
                Err(StatusCode::INTERNAL_SERVER_ERROR)
            }
        }
    }
}

#[derive(Debug, Deserialize)]
struct FileQuery {
    path: String,
}

/// GET /api/file?path=... - Get file context
async fn handle_get_file(
    State(state): State<Arc<AppState>>,
    Query(params): Query<FileQuery>,
) -> Result<Json<context::query::FileContext>, StatusCode> {
    let mut graph = CodeGraph::open(&state.db_path).map_err(|_| StatusCode::SERVICE_UNAVAILABLE)?;
    match context::query::get_file_context(&mut graph, &params.path) {
        Ok(context) => Ok(Json(context)),
        Err(e) => {
            let err_str = format!("{}", e);
            if err_str.contains("not found") {
                Err(StatusCode::NOT_FOUND)
            } else {
                eprintln!("File context query failed: {}", e);
                Err(StatusCode::INTERNAL_SERVER_ERROR)
            }
        }
    }
}

// ============================================================================
// Helper Functions
// ============================================================================

// ============================================================================
// Query Parameters
// ============================================================================

#[derive(Debug, Deserialize)]
struct ListSymbolsParams {
    #[serde(default)]
    kind: Option<String>,
    #[serde(default)]
    page: Option<usize>,
    #[serde(default = "default_page_size")]
    page_size: usize,
    #[serde(default)]
    cursor: Option<String>,
}

fn default_page_size() -> usize {
    50
}

// ============================================================================
// Response Types
// ============================================================================

#[derive(Debug, serde::Serialize)]
struct SummaryResponse {
    total_files: usize,
    total_symbols: usize,
    total_calls: usize,
}

// ============================================================================
// Embedded HTML UI
// ============================================================================

/// Get the index HTML page
pub fn get_index_html() -> Html<&'static str> {
    Html(INDEX_HTML)
}

const INDEX_HTML: &str = r#"<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Magellan Code Explorer</title>
    <style>
        body { font-family: system-ui, -apple-system, sans-serif; margin: 2rem; background: #f8f9fa; color: #212529; }
        .card { background: #fff; border: 1px solid #dee2e6; padding: 1rem; margin: 1rem 0; border-radius: 8px; box-shadow: 0 1px 3px rgba(0,0,0,0.04); }
        .stat { display: inline-block; margin-right: 2rem; }
        .stat-value { font-size: 2rem; font-weight: bold; color: #007bff; }
        table { width: 100%; border-collapse: collapse; }
        th, td { padding: 0.5rem; text-align: left; border-bottom: 1px solid #eee; }
        a { color: #007bff; text-decoration: none; }
        a:hover { text-decoration: underline; }
        .error { color: #dc3545; }
        #search { width: 100%; padding: 0.5rem; margin-bottom: 1rem; border: 1px solid #ced4da; border-radius: 4px; box-sizing: border-box; }
        .pagination { margin-top: 1rem; text-align: center; }
        .pagination button { margin: 0 0.25rem; padding: 0.35rem 0.7rem; border: 1px solid #007bff; background: #fff; color: #007bff; border-radius: 4px; cursor: pointer; }
        .pagination button:hover { background: #007bff; color: #fff; }
        .pagination button:disabled { opacity: 0.5; cursor: not-allowed; background: #fff; color: #007bff; }
        .pagination span { margin: 0 0.5rem; }
    </style>
</head>
<body>
    <h1>Magellan Code Explorer</h1>

    <div id="summary" class="card">
        <h2>Project Summary</h2>
        <div id="stats">Loading...</div>
    </div>

    <div class="card">
        <h2>Symbols</h2>
        <input type="text" id="search" placeholder="Search symbols...">
        <table>
            <thead>
                <tr>
                    <th>Name</th>
                    <th>Kind</th>
                    <th>File</th>
                    <th>Line</th>
                </tr>
            </thead>
            <tbody id="symbols"></tbody>
        </table>
        <div id="pagination" class="pagination"></div>
    </div>

    <script>
        (function() {
            function setError(element, message) {
                element.textContent = '';
                var err = document.createElement('div');
                err.className = 'error';
                err.textContent = message;
                element.appendChild(err);
            }

            function renderStats(data) {
                var stats = document.getElementById('stats');
                stats.textContent = '';
                var labels = ['Files', 'Symbols', 'Calls'];
                var keys = ['total_files', 'total_symbols', 'total_calls'];
                for (var i = 0; i < keys.length; i++) {
                    var s = document.createElement('div');
                    s.className = 'stat';
                    var v = document.createElement('div');
                    v.className = 'stat-value';
                    v.textContent = data[keys[i]];
                    s.appendChild(v);
                    s.appendChild(document.createTextNode(labels[i]));
                    stats.appendChild(s);
                }
            }

            function renderSymbols(data) {
                var tbody = document.getElementById('symbols');
                tbody.textContent = '';
                if (!data.items || data.items.length === 0) {
                    var tr = document.createElement('tr');
                    var td = document.createElement('td');
                    td.colSpan = 4;
                    td.textContent = 'No symbols found.';
                    tr.appendChild(td);
                    tbody.appendChild(tr);
                    return;
                }
                for (var i = 0; i < data.items.length; i++) {
                    var sym = data.items[i];
                    var tr = document.createElement('tr');

                    var tdName = document.createElement('td');
                    var a = document.createElement('a');
                    a.href = '/api/symbol?name=' + encodeURIComponent(sym.name);
                    a.textContent = sym.name;
                    tdName.appendChild(a);
                    tr.appendChild(tdName);

                    var tdKind = document.createElement('td');
                    tdKind.textContent = sym.kind || '';
                    tr.appendChild(tdKind);

                    var tdFile = document.createElement('td');
                    tdFile.textContent = sym.file || '';
                    tr.appendChild(tdFile);

                    var tdLine = document.createElement('td');
                    tdLine.textContent = sym.line != null ? String(sym.line) : '';
                    tr.appendChild(tdLine);

                    tbody.appendChild(tr);
                }
            }

            function renderPagination(data, query) {
                var pag = document.getElementById('pagination');
                pag.textContent = '';
                if (data.total_pages <= 1) return;
                var current = data.page || 1;
                var prev = document.createElement('button');
                prev.textContent = 'Prev';
                prev.disabled = current <= 1;
                prev.onclick = function() { loadSymbols(current - 1, query); };
                pag.appendChild(prev);

                var info = document.createElement('span');
                info.textContent = 'Page ' + current + ' of ' + data.total_pages;
                pag.appendChild(info);

                var next = document.createElement('button');
                next.textContent = 'Next';
                next.disabled = current >= data.total_pages;
                next.onclick = function() { loadSymbols(current + 1, query); };
                pag.appendChild(next);
            }

            function loadSymbols(page, query) {
                page = page || 1;
                var url = '/api/symbols?page=' + encodeURIComponent(page) + '&page_size=50';
                if (query) {
                    url += '&kind=' + encodeURIComponent(query);
                }
                var tbody = document.getElementById('symbols');
                tbody.textContent = 'Loading...';
                fetch(url)
                    .then(function(r) {
                        if (!r.ok) throw new Error('HTTP ' + r.status);
                        return r.json();
                    })
                    .then(function(data) {
                        renderSymbols(data);
                        renderPagination(data, query);
                    })
                    .catch(function(e) {
                        setError(tbody, 'Error loading symbols: ' + e.message);
                    });
            }

            fetch('/api/summary')
                .then(function(r) {
                    if (!r.ok) throw new Error('HTTP ' + r.status);
                    return r.json();
                })
                .then(function(data) {
                    renderStats(data);
                })
                .catch(function(e) {
                    setError(document.getElementById('stats'), 'Error loading summary: ' + e.message);
                });

            loadSymbols(1);

            var searchInput = document.getElementById('search');
            var debounceTimer;
            searchInput.addEventListener('input', function() {
                clearTimeout(debounceTimer);
                debounceTimer = setTimeout(function() {
                    loadSymbols(1, searchInput.value.trim());
                }, 300);
            });
            searchInput.addEventListener('keydown', function(e) {
                if (e.key === 'Enter') {
                    clearTimeout(debounceTimer);
                    loadSymbols(1, searchInput.value.trim());
                }
            });
        })();
    </script>
</body>
</html>"#;