dx_forge/context/
mod.rs

1pub mod ai_context;
2pub mod annotations;
3pub mod discussions;
4
5use anyhow::Result;
6use std::path::Path;
7
8pub use annotations::Annotation;
9
10use crate::crdt::{Anchor, Position};
11use crate::storage::Database;
12
13pub async fn create_anchor(
14    file: &Path,
15    line: usize,
16    column: usize,
17    message: Option<String>,
18) -> Result<Anchor> {
19    let db = Database::open(".dx/forge")?;
20
21    // Load config to get actor_id
22    let config: serde_json::Value =
23        serde_json::from_str(&tokio::fs::read_to_string(".dx/forge/config.json").await?)?;
24    let actor_id = config["actor_id"].as_str().unwrap().to_string();
25
26    let position = Position::new(line, column, 0, actor_id, 0);
27    let anchor = Anchor::new(file.display().to_string(), position, message);
28
29    db.store_anchor(&anchor)?;
30
31    Ok(anchor)
32}
33
34pub async fn annotate(file: &Path, line: usize, message: &str, is_ai: bool) -> Result<()> {
35    let annotation = Annotation::new(file.display().to_string(), line, message.to_string(), is_ai);
36
37    // Store annotation
38    let db = Database::open(".dx/forge")?;
39    annotations::store_annotation(&db, &annotation)?;
40
41    Ok(())
42}
43
44pub async fn show_context(file: &Path, line: Option<usize>) -> Result<()> {
45    use colored::*;
46
47    let db = Database::open(".dx/forge")?;
48    let annotations = annotations::get_annotations(&db, file, line)?;
49
50    println!(
51        "{}",
52        format!("Context for: {}", file.display()).cyan().bold()
53    );
54    println!("{}", "═".repeat(80).bright_black());
55
56    for ann in annotations {
57        let icon = if ann.is_ai { "🤖" } else { "👤" };
58        let author = if ann.is_ai {
59            "AI Agent".bright_magenta()
60        } else {
61            ann.author.bright_cyan()
62        };
63
64        println!(
65            "\n{} {} {} {}",
66            icon,
67            author,
68            format!("(line {})", ann.line).bright_black(),
69            ann.created_at
70                .format("%Y-%m-%d %H:%M")
71                .to_string()
72                .bright_black()
73        );
74        println!("   {}", ann.content.bright_white());
75    }
76
77    Ok(())
78}