gid-core 0.1.0

Graph-Indexed Development core library — graph-based project management and code analysis for AI agents
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
509
510
511
512
513
514
515
516
517
//! Semantify module for upgrading file-level graphs to semantic graphs.
//!
//! Generates prompts and parses LLM responses. Does NOT call LLM directly.

use anyhow::{Context, Result, bail};
use serde::{Deserialize, Serialize};
use crate::graph::{Graph, Node, Edge};
use std::collections::HashMap;

/// A proposed semantic enhancement.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum SemanticProposal {
    /// Assign a layer to a node
    AssignLayer {
        node_id: String,
        layer: String,
        reason: String,
        #[serde(default)]
        confidence: f32,
    },
    /// Upgrade a file node to a component
    UpgradeToComponent {
        node_id: String,
        component_name: String,
        description: String,
        #[serde(default)]
        confidence: f32,
    },
    /// Add a feature node
    AddFeature {
        name: String,
        description: String,
        implementing_nodes: Vec<String>,
        #[serde(default)]
        confidence: f32,
    },
    /// Add description to a node
    AddDescription {
        node_id: String,
        description: String,
        #[serde(default)]
        confidence: f32,
    },
    /// Group nodes into a module
    GroupIntoModule {
        module_name: String,
        node_ids: Vec<String>,
        #[serde(default)]
        confidence: f32,
    },
}

/// Result from parsing LLM semantify response.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SemantifyResult {
    pub proposals: Vec<SemanticProposal>,
    /// Optional: the full transformed graph
    pub graph: Option<Graph>,
}

/// Generate a prompt to upgrade a file-level graph to a semantic graph.
pub fn generate_semantify_prompt(graph: &Graph) -> String {
    // Build context from graph
    let node_summary = build_node_summary(graph);
    let edge_summary = build_edge_summary(graph);
    
    format!(r#"You are a software architect. Analyze this code graph and suggest semantic enhancements.

CURRENT GRAPH:

Nodes ({} total):
{}

Edges ({} total):
{}

TASK:
1. Assign architectural layers to nodes (interface, application, domain, infrastructure)
2. Identify features that nodes implement
3. Add meaningful descriptions to important nodes
4. Group related files into logical components

LAYER DEFINITIONS:
- interface: User-facing (CLI commands, API routes, UI components, handlers)
- application: Use cases, services, orchestration
- domain: Core business logic, types, entities
- infrastructure: External integrations (DB, filesystem, parsers, adapters)

Respond with a JSON object:
```json
{{
  "proposals": [
    {{
      "type": "assign_layer",
      "node_id": "src/commands/init.ts",
      "layer": "interface",
      "reason": "CLI command handler",
      "confidence": 0.9
    }},
    {{
      "type": "add_feature",
      "name": "graph_visualization",
      "description": "Visualize the graph in various formats",
      "implementing_nodes": ["src/visual.ts", "src/render.ts"],
      "confidence": 0.85
    }},
    {{
      "type": "add_description",
      "node_id": "src/core/query.ts",
      "description": "Graph traversal and query engine",
      "confidence": 0.8
    }}
  ]
}}
```

Only output valid JSON. No explanation before or after."#,
        graph.nodes.len(),
        node_summary,
        graph.edges.len(),
        edge_summary
    )
}

/// Generate a prompt for full graph transformation.
pub fn generate_full_transform_prompt(graph: &Graph) -> String {
    let yaml = serde_yaml::to_string(graph).unwrap_or_default();
    
    format!(r#"You are a software architect. Transform this file-level graph into a semantic architecture graph.

CURRENT GRAPH (YAML):
```yaml
{}
```

Transform the graph by:
1. Adding a `layer` field to each node (interface, application, domain, infrastructure)
2. Adding meaningful `description` fields
3. Creating Feature nodes for logical feature groupings
4. Adding `implements` edges from components to features
5. Keeping all existing `depends_on` edges

Output the complete transformed graph as YAML:
```yaml
project:
  name: project-name
  description: Semantic architecture graph

nodes:
  - id: feature_visualization
    title: Graph Visualization
    type: feature
    description: Visualize graphs in multiple formats
    
  - id: src/visual.ts
    title: Visual Renderer
    type: component
    layer: interface
    description: Renders graph in ASCII, DOT, and Mermaid formats
    
edges:
  - from: src/visual.ts
    to: feature_visualization
    relation: implements
    
  - from: src/visual.ts
    to: src/core/graph.ts
    relation: depends_on
```

Only output valid YAML. Start with "```yaml" and end with "```"."#, yaml)
}

/// Parse an LLM response containing semantic proposals.
pub fn parse_semantify_response(response: &str) -> Result<SemantifyResult> {
    let json_str = extract_json(response)?;
    
    #[derive(Deserialize)]
    struct ProposalsResponse {
        proposals: Vec<SemanticProposal>,
    }
    
    let parsed: ProposalsResponse = serde_json::from_str(&json_str)
        .context("Failed to parse proposals JSON")?;
    
    Ok(SemantifyResult {
        proposals: parsed.proposals,
        graph: None,
    })
}

/// Parse an LLM response containing a full transformed graph.
pub fn parse_full_transform_response(response: &str) -> Result<Graph> {
    let yaml_str = extract_yaml(response)?;
    
    let graph: Graph = serde_yaml::from_str(&yaml_str)
        .context("Failed to parse graph YAML")?;
    
    Ok(graph)
}

/// Apply semantic proposals to a graph.
pub fn apply_proposals(graph: &mut Graph, proposals: &[SemanticProposal]) -> usize {
    let mut applied_count = 0;
    
    for proposal in proposals {
        match proposal {
            SemanticProposal::AssignLayer { node_id, layer, .. } => {
                if let Some(node) = graph.get_node_mut(node_id) {
                    node.metadata.insert("layer".to_string(), serde_json::json!(layer));
                    applied_count += 1;
                }
            }
            
            SemanticProposal::UpgradeToComponent { node_id, component_name, description, .. } => {
                if let Some(node) = graph.get_node_mut(node_id) {
                    node.node_type = Some("component".to_string());
                    node.title = component_name.clone();
                    node.description = Some(description.clone());
                    applied_count += 1;
                }
            }
            
            SemanticProposal::AddFeature { name, description, implementing_nodes, .. } => {
                // Create feature node
                let feature_id = format!("feature_{}", name.to_lowercase().replace(' ', "_"));
                let mut feature_node = Node::new(&feature_id, name);
                feature_node.node_type = Some("feature".to_string());
                feature_node.description = Some(description.clone());
                graph.add_node(feature_node);
                applied_count += 1;
                
                // Add implements edges
                for impl_node in implementing_nodes {
                    if graph.get_node(impl_node).is_some() {
                        graph.add_edge(Edge::new(impl_node, &feature_id, "implements"));
                        applied_count += 1;
                    }
                }
            }
            
            SemanticProposal::AddDescription { node_id, description, .. } => {
                if let Some(node) = graph.get_node_mut(node_id) {
                    if node.description.is_none() {
                        node.description = Some(description.clone());
                        applied_count += 1;
                    }
                }
            }
            
            SemanticProposal::GroupIntoModule { module_name, node_ids, .. } => {
                // Create module node
                let module_id = format!("module_{}", module_name.to_lowercase().replace(' ', "_"));
                let mut module_node = Node::new(&module_id, module_name);
                module_node.node_type = Some("module".to_string());
                graph.add_node(module_node);
                applied_count += 1;
                
                // Add contains edges
                for node_id in node_ids {
                    if graph.get_node(node_id).is_some() {
                        graph.add_edge(Edge::new(&module_id, node_id, "contains"));
                        applied_count += 1;
                    }
                }
            }
        }
    }
    
    applied_count
}

/// Build a summary of nodes for the prompt.
fn build_node_summary(graph: &Graph) -> String {
    let mut lines = Vec::new();
    
    for node in &graph.nodes {
        let node_type = node.node_type.as_deref().unwrap_or("unknown");
        let desc = node.description.as_deref().unwrap_or("");
        let layer = node.metadata.get("layer")
            .and_then(|v| v.as_str())
            .unwrap_or("none");
        
        lines.push(format!(
            "  - {} (type: {}, layer: {}) {}",
            node.id,
            node_type,
            layer,
            if desc.is_empty() { String::new() } else { format!("// {}", desc) }
        ));
    }
    
    lines.join("\n")
}

/// Build a summary of edges for the prompt.
fn build_edge_summary(graph: &Graph) -> String {
    // Group edges by relation
    let mut by_relation: HashMap<&str, Vec<(&str, &str)>> = HashMap::new();
    
    for edge in &graph.edges {
        by_relation.entry(&edge.relation)
            .or_default()
            .push((&edge.from, &edge.to));
    }
    
    let mut lines = Vec::new();
    
    for (relation, edges) in &by_relation {
        lines.push(format!("  {} edges ({}):", relation, edges.len()));
        for (from, to) in edges.iter().take(10) {
            lines.push(format!("    {} -> {}", from, to));
        }
        if edges.len() > 10 {
            lines.push(format!("    ... and {} more", edges.len() - 10));
        }
    }
    
    lines.join("\n")
}

/// Extract JSON from response with markdown code blocks.
fn extract_json(response: &str) -> Result<String> {
    // Try to find JSON in code block
    if let Some(start) = response.find("```json") {
        let content = &response[start + 7..];
        if let Some(end) = content.find("```") {
            return Ok(content[..end].trim().to_string());
        }
    }
    
    // Try plain code block
    if let Some(start) = response.find("```") {
        let content = &response[start + 3..];
        if let Some(end) = content.find("```") {
            let inner = content[..end].trim();
            if let Some(newline) = inner.find('\n') {
                let first_line = &inner[..newline];
                if !first_line.starts_with('{') && !first_line.starts_with('[') {
                    return Ok(inner[newline..].trim().to_string());
                }
            }
            return Ok(inner.to_string());
        }
    }
    
    // Try raw JSON
    let trimmed = response.trim();
    if trimmed.starts_with('{') || trimmed.starts_with('[') {
        return Ok(trimmed.to_string());
    }
    
    bail!("No JSON found in response")
}

/// Extract YAML from response.
fn extract_yaml(response: &str) -> Result<String> {
    if let Some(start) = response.find("```yaml") {
        let content = &response[start + 7..];
        if let Some(end) = content.find("```") {
            return Ok(content[..end].trim().to_string());
        }
    }
    
    if let Some(start) = response.find("```yml") {
        let content = &response[start + 6..];
        if let Some(end) = content.find("```") {
            return Ok(content[..end].trim().to_string());
        }
    }
    
    // Assume raw YAML
    let trimmed = response.trim();
    if trimmed.contains(':') {
        return Ok(trimmed.to_string());
    }
    
    bail!("No YAML found in response")
}

/// Heuristic layer assignment based on file paths.
pub fn heuristic_assign_layer(file_path: &str) -> Option<&'static str> {
    let path_lower = file_path.to_lowercase();
    
    // Interface layer patterns
    if path_lower.contains("/commands/") 
        || path_lower.contains("/cmd/")
        || path_lower.contains("/api/")
        || path_lower.contains("/routes/")
        || path_lower.contains("/controllers/")
        || path_lower.contains("/handlers/")
        || path_lower.contains("/web/")
        || path_lower.contains("/ui/")
        || path_lower.contains("/views/")
        || path_lower.contains("/pages/")
        || path_lower.contains("/components/")
    {
        return Some("interface");
    }
    
    // Application layer patterns
    if path_lower.contains("/services/")
        || path_lower.contains("/usecases/")
        || path_lower.contains("/use_cases/")
        || path_lower.contains("/orchestrators/")
        || path_lower.contains("/workflows/")
        || path_lower.contains("/ai/")
        || path_lower.contains("/llm/")
    {
        return Some("application");
    }
    
    // Domain layer patterns
    if path_lower.contains("/core/")
        || path_lower.contains("/domain/")
        || path_lower.contains("/entities/")
        || path_lower.contains("/models/")
        || path_lower.contains("/types/")
        || path_lower.contains("/lib/")
        || path_lower.ends_with("types.ts")
        || path_lower.ends_with("types.rs")
    {
        return Some("domain");
    }
    
    // Infrastructure layer patterns
    if path_lower.contains("/infrastructure/")
        || path_lower.contains("/db/")
        || path_lower.contains("/database/")
        || path_lower.contains("/repositories/")
        || path_lower.contains("/adapters/")
        || path_lower.contains("/clients/")
        || path_lower.contains("/extractors/")
        || path_lower.contains("/parsers/")
        || path_lower.contains("/config/")
    {
        return Some("infrastructure");
    }
    
    None
}

/// Apply heuristic layer assignments to a graph.
pub fn apply_heuristic_layers(graph: &mut Graph) -> usize {
    let mut assigned = 0;
    
    for node in &mut graph.nodes {
        // Skip if already has layer
        if node.metadata.contains_key("layer") {
            continue;
        }
        
        // Try to infer from path (stored in id for file nodes)
        if let Some(layer) = heuristic_assign_layer(&node.id) {
            node.metadata.insert("layer".to_string(), serde_json::json!(layer));
            assigned += 1;
        }
    }
    
    assigned
}

#[cfg(test)]
mod tests {
    use super::*;
    
    #[test]
    fn test_heuristic_layer_assignment() {
        assert_eq!(heuristic_assign_layer("src/commands/init.ts"), Some("interface"));
        assert_eq!(heuristic_assign_layer("src/services/auth.ts"), Some("application"));
        assert_eq!(heuristic_assign_layer("src/core/graph.ts"), Some("domain"));
        assert_eq!(heuristic_assign_layer("src/extractors/typescript.ts"), Some("infrastructure"));
        assert_eq!(heuristic_assign_layer("src/utils.ts"), None);
    }
    
    #[test]
    fn test_parse_proposals() {
        let response = r#"```json
{
  "proposals": [
    {
      "type": "assign_layer",
      "node_id": "src/cli.ts",
      "layer": "interface",
      "reason": "CLI entry point",
      "confidence": 0.9
    }
  ]
}
```"#;
        
        let result = parse_semantify_response(response).unwrap();
        assert_eq!(result.proposals.len(), 1);
    }
    
    #[test]
    fn test_apply_proposals() {
        let mut graph = Graph::new();
        graph.add_node(Node::new("src/cli.ts", "CLI"));
        
        let proposals = vec![
            SemanticProposal::AssignLayer {
                node_id: "src/cli.ts".to_string(),
                layer: "interface".to_string(),
                reason: "CLI".to_string(),
                confidence: 0.9,
            },
        ];
        
        let applied = apply_proposals(&mut graph, &proposals);
        assert_eq!(applied, 1);
        
        let node = graph.get_node("src/cli.ts").unwrap();
        assert_eq!(node.metadata.get("layer").and_then(|v| v.as_str()), Some("interface"));
    }
}