maproom 0.1.0

Semantic code search powered by embeddings and SQLite
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
//! React-specific context assembly strategy.
//!
//! This module provides a specialized assembler for React codebases that:
//! - Detects React components via naming conventions
//! - Includes route definitions for components
//! - Discovers and includes hooks (built-in and custom)
//! - Handles JSX parent/child relationships
//! - Applies React-specific budget allocation

use anyhow::{Context as AnyhowContext, Result};
use tracing::debug;

use crate::context::{
    assembler::{BasicContextAssembler, ChunkMetadata, ContextAssembler},
    detectors::{ComponentDetector, HookDetector, JsxRelationshipDetector},
    file_loader::FileLoader,
    token_counter::TokenCounter,
    types::{ContextBundle, ContextItem, ExpandOptions, LineRange},
};
use crate::db::traits::StoreChunks;
use crate::db::SqliteStore;
use std::sync::Arc;

/// Configuration for React assembly strategy.
#[derive(Debug, Clone)]
pub struct ReactConfig {
    /// Whether to include route definitions
    pub include_routes: bool,
    /// Whether to include hooks
    pub include_hooks: bool,
    /// Whether to include JSX parent components
    pub include_jsx_parents: bool,
    /// Whether to include JSX child components
    pub include_jsx_children: bool,
    /// Maximum number of hooks to include
    pub max_hooks: usize,
    /// Maximum number of JSX parents to include
    pub max_jsx_parents: usize,
    /// Maximum number of JSX children to include
    pub max_jsx_children: usize,
}

impl Default for ReactConfig {
    fn default() -> Self {
        Self {
            include_routes: true,
            include_hooks: true,
            include_jsx_parents: true,
            include_jsx_children: true,
            max_hooks: 5,
            max_jsx_parents: 2,
            max_jsx_children: 5,
        }
    }
}

impl ReactConfig {
    /// Create a configuration from ExpandOptions.
    pub fn from_expand_options(options: &ExpandOptions) -> Self {
        Self {
            include_routes: options.routes,
            include_hooks: options.hooks,
            include_jsx_parents: options.jsx_parents,
            include_jsx_children: options.jsx_children,
            ..Default::default()
        }
    }
}

/// React-specific context assembly strategy.
///
/// This assembler extends the basic assembler with React-specific enhancements:
/// - Component detection
/// - Route discovery
/// - Hook inclusion
/// - JSX relationship handling
pub struct ReactAssemblyStrategy {
    store: Arc<SqliteStore>,
    base_assembler: BasicContextAssembler,
    config: ReactConfig,
    component_detector: ComponentDetector,
    hook_detector: HookDetector,
    jsx_detector: JsxRelationshipDetector,
    token_counter: TokenCounter,
}

impl ReactAssemblyStrategy {
    /// Create a new React assembly strategy.
    pub fn new(store: Arc<SqliteStore>) -> Self {
        Self::with_config(store, ReactConfig::default())
    }

    /// Create a new React assembly strategy with custom configuration.
    pub fn with_config(store: Arc<SqliteStore>, config: ReactConfig) -> Self {
        Self {
            base_assembler: BasicContextAssembler::new_without_cache(Arc::clone(&store)),
            store,
            config,
            component_detector: ComponentDetector::new(),
            hook_detector: HookDetector::new(),
            jsx_detector: JsxRelationshipDetector::new(),
            token_counter: TokenCounter::new(),
        }
    }

    /// Check if a chunk is a React component.
    async fn is_component(&self, metadata: &ChunkMetadata) -> Result<bool> {
        // Quick check: file extension
        if !metadata.file_relpath.ends_with(".tsx") && !metadata.file_relpath.ends_with(".jsx") {
            return Ok(false);
        }

        // Use component detector for file path heuristics
        if !self
            .component_detector
            .is_component_file_path(&metadata.file_relpath)
        {
            return Ok(false);
        }

        // Load file content to verify JSX presence
        let file_loader = FileLoader::new(&metadata.worktree_path);
        let range = LineRange::new(metadata.start_line, metadata.end_line);

        match file_loader.load_range(&metadata.file_relpath, range).await {
            Ok(content) => Ok(self.component_detector.has_jsx_return(&content)),
            Err(_) => {
                // If we can't load content, rely on file path heuristics
                Ok(true)
            }
        }
    }

    /// Create a ContextItem from metadata.
    async fn create_context_item(
        &self,
        metadata: ChunkMetadata,
        role: &str,
        reason: &str,
    ) -> Result<ContextItem> {
        let file_loader = FileLoader::new(&metadata.worktree_path);
        let range = LineRange::new(metadata.start_line, metadata.end_line);

        let content = file_loader
            .load_range(&metadata.file_relpath, range)
            .await
            .with_context(|| {
                format!(
                    "Failed to load file content: {} (lines {}-{})",
                    metadata.file_relpath, metadata.start_line, metadata.end_line
                )
            })?;

        let tokens = self
            .token_counter
            .count(&content)
            .context("Failed to count tokens")?;

        Ok(ContextItem {
            relpath: metadata.file_relpath,
            range,
            role: role.to_string(),
            reason: reason.to_string(),
            content,
            tokens,
        })
    }

    /// Retrieve chunk metadata from the database by ID.
    async fn get_chunk_metadata(&self, chunk_id: i64) -> Result<ChunkMetadata> {
        // Use the base assembler's implementation which works with SQLite
        self.base_assembler.get_chunk_metadata(chunk_id).await
    }

    /// Get the worktree path for a chunk.
    async fn get_worktree_path(&self, chunk_id: i64) -> Result<String> {
        let metadata = self.get_chunk_metadata(chunk_id).await?;
        Ok(metadata.worktree_path)
    }

    /// Add route definitions to the context bundle.
    async fn add_routes(
        &self,
        _bundle: &mut ContextBundle,
        _chunk_id: i64,
        _budget: usize,
    ) -> Result<()> {
        // TODO: Implement route queries for SQLite backend using graph module
        // For now, this feature is disabled
        Ok(())
    }

    /// Add hooks to the context bundle.
    ///
    /// Uses the HookDetector to find hooks used by the component and
    /// adds their definitions to the context bundle.
    async fn add_hooks(
        &self,
        bundle: &mut ContextBundle,
        chunk_id: i64,
        budget: usize,
    ) -> Result<()> {
        if !self.config.include_hooks || bundle.total_tokens >= budget {
            return Ok(());
        }

        // Find hooks used by this component
        let hooks = self
            .hook_detector
            .find_used_hooks(&self.store, chunk_id)
            .await?;

        let mut added_count = 0;
        for hook in hooks {
            if added_count >= self.config.max_hooks || bundle.total_tokens >= budget {
                break;
            }

            // Skip built-in hooks (they don't have definitions in the codebase)
            if hook.is_builtin {
                continue;
            }

            // Get the hook's metadata and create a context item
            if let Some(chunk) = self.store.get_chunk_by_id(hook.id).await? {
                let metadata = ChunkMetadata {
                    id: chunk.id,
                    file_relpath: chunk.file_path.clone(),
                    worktree_path: self.get_worktree_path(chunk_id).await.unwrap_or_default(),
                    kind: chunk.kind.clone(),
                    symbol_name: chunk.symbol_name.clone(),
                    start_line: chunk.start_line,
                    end_line: chunk.end_line,
                    signature: None,
                    docstring: None,
                };

                if let Ok(item) = self
                    .create_context_item(
                        metadata,
                        "hook",
                        &format!("Custom hook: {} provides reusable logic", hook.symbol_name),
                    )
                    .await
                {
                    if !bundle.would_exceed_budget(item.tokens, budget) {
                        debug!("Adding hook {}: {} tokens", hook.symbol_name, item.tokens);
                        bundle.add_item(item);
                        added_count += 1;
                    }
                }
            }
        }

        Ok(())
    }

    /// Add JSX parent components to the context bundle.
    ///
    /// Uses the JsxRelationshipDetector to find components that render this component.
    async fn add_jsx_parents(
        &self,
        bundle: &mut ContextBundle,
        chunk_id: i64,
        symbol_name: &str,
        budget: usize,
    ) -> Result<()> {
        if !self.config.include_jsx_parents || bundle.total_tokens >= budget {
            return Ok(());
        }

        // Find parent components that render this component
        let parents = self
            .jsx_detector
            .find_parent_components(&self.store, chunk_id, symbol_name)
            .await?;

        let mut added_count = 0;
        for parent in parents {
            if added_count >= self.config.max_jsx_parents || bundle.total_tokens >= budget {
                break;
            }

            // Get the parent's metadata and create a context item
            if let Some(chunk) = self.store.get_chunk_by_id(parent.id).await? {
                let metadata = ChunkMetadata {
                    id: chunk.id,
                    file_relpath: chunk.file_path.clone(),
                    worktree_path: self.get_worktree_path(chunk_id).await.unwrap_or_default(),
                    kind: chunk.kind.clone(),
                    symbol_name: chunk.symbol_name.clone(),
                    start_line: chunk.start_line,
                    end_line: chunk.end_line,
                    signature: None,
                    docstring: None,
                };

                let parent_name = parent.symbol_name.as_deref().unwrap_or("ParentComponent");
                if let Ok(item) = self
                    .create_context_item(
                        metadata,
                        "jsx_parent",
                        &format!("JSX parent: {} renders this component", parent_name),
                    )
                    .await
                {
                    if !bundle.would_exceed_budget(item.tokens, budget) {
                        debug!("Adding JSX parent {}: {} tokens", parent_name, item.tokens);
                        bundle.add_item(item);
                        added_count += 1;
                    }
                }
            }
        }

        Ok(())
    }

    /// Add JSX child components to the context bundle.
    ///
    /// Uses the JsxRelationshipDetector to find components rendered by this component.
    async fn add_jsx_children(
        &self,
        bundle: &mut ContextBundle,
        chunk_id: i64,
        budget: usize,
    ) -> Result<()> {
        if !self.config.include_jsx_children || bundle.total_tokens >= budget {
            return Ok(());
        }

        // Find child components rendered by this component
        let children = self
            .jsx_detector
            .find_child_components(&self.store, chunk_id)
            .await?;

        let mut added_count = 0;
        for child in children {
            if added_count >= self.config.max_jsx_children || bundle.total_tokens >= budget {
                break;
            }

            // Get the child's metadata and create a context item
            if let Some(chunk) = self.store.get_chunk_by_id(child.id).await? {
                let metadata = ChunkMetadata {
                    id: chunk.id,
                    file_relpath: chunk.file_path.clone(),
                    worktree_path: self.get_worktree_path(chunk_id).await.unwrap_or_default(),
                    kind: chunk.kind.clone(),
                    symbol_name: chunk.symbol_name.clone(),
                    start_line: chunk.start_line,
                    end_line: chunk.end_line,
                    signature: None,
                    docstring: None,
                };

                let child_name = child.symbol_name.as_deref().unwrap_or("ChildComponent");
                if let Ok(item) = self
                    .create_context_item(
                        metadata,
                        "jsx_child",
                        &format!("JSX child: {} is rendered by this component", child_name),
                    )
                    .await
                {
                    if !bundle.would_exceed_budget(item.tokens, budget) {
                        debug!("Adding JSX child {}: {} tokens", child_name, item.tokens);
                        bundle.add_item(item);
                        added_count += 1;
                    }
                }
            }
        }

        Ok(())
    }
}

#[async_trait::async_trait]
impl ContextAssembler for ReactAssemblyStrategy {
    async fn assemble(
        &self,
        chunk_id: i64,
        budget: usize,
        options: ExpandOptions,
    ) -> Result<ContextBundle> {
        debug!(
            "React strategy assembling context for chunk {} with budget {} tokens",
            chunk_id, budget
        );

        // Update config from options
        let mut config = self.config.clone();
        config.include_routes = options.routes;
        config.include_hooks = options.hooks;
        config.include_jsx_parents = options.jsx_parents;
        config.include_jsx_children = options.jsx_children;

        // Get chunk metadata
        let metadata = self
            .get_chunk_metadata(chunk_id)
            .await
            .context("Failed to retrieve chunk metadata")?;

        // Check if this is a React component
        let is_component = self.is_component(&metadata).await?;

        debug!(
            "Chunk {} is {}a React component",
            chunk_id,
            if is_component { "" } else { "not " }
        );

        // Start with the basic assembly (primary chunk + tests if requested)
        let mut bundle = self
            .base_assembler
            .assemble(chunk_id, budget, options.clone())
            .await?;

        // If not a component, return the basic bundle
        if !is_component {
            return Ok(bundle);
        }

        // Add React-specific context items
        let symbol_name = metadata.symbol_name.as_deref().unwrap_or("Component");

        // Priority order: routes → hooks → jsx_parents → jsx_children
        if config.include_routes {
            self.add_routes(&mut bundle, chunk_id, budget).await?;
        }

        if config.include_hooks {
            self.add_hooks(&mut bundle, chunk_id, budget).await?;
        }

        if config.include_jsx_parents {
            self.add_jsx_parents(&mut bundle, chunk_id, symbol_name, budget)
                .await?;
        }

        if config.include_jsx_children {
            self.add_jsx_children(&mut bundle, chunk_id, budget).await?;
        }

        debug!(
            "React strategy assembled {} items, {} tokens total",
            bundle.items.len(),
            bundle.total_tokens
        );

        Ok(bundle)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_react_config_default() {
        let config = ReactConfig::default();
        assert!(config.include_routes);
        assert!(config.include_hooks);
        assert!(config.include_jsx_parents);
        assert!(config.include_jsx_children);
        assert_eq!(config.max_hooks, 5);
        assert_eq!(config.max_jsx_parents, 2);
        assert_eq!(config.max_jsx_children, 5);
    }

    #[test]
    fn test_react_config_from_expand_options() {
        let options = ExpandOptions {
            routes: true,
            hooks: true,
            jsx_parents: false,
            jsx_children: true,
            ..Default::default()
        };

        let config = ReactConfig::from_expand_options(&options);
        assert!(config.include_routes);
        assert!(config.include_hooks);
        assert!(!config.include_jsx_parents);
        assert!(config.include_jsx_children);
    }

    #[test]
    fn test_expand_options_for_react_component() {
        let options = ExpandOptions::for_react_component();
        assert!(!options.callers);
        assert!(!options.callees);
        assert!(options.tests);
        assert!(options.routes);
        assert!(options.hooks);
        assert!(options.jsx_parents);
        assert!(options.jsx_children);
    }

    // Integration tests with database are in tests/ directory
    #[tokio::test]
    #[ignore]
    async fn test_react_assembly_strategy() {
        // Integration test - requires database
    }
}