ai-agent-sdk 0.5.0

Idiomatic agent sdk inspired by the claude code source leak
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
//! AI.md loader implementation

use crate::ai_md::types::*;
use crate::ai_md::AI_MD_INSTRUCTION_PROMPT;
use crate::error::AgentError;
use std::collections::HashSet;
use std::fs;
use std::path::{Path, PathBuf};
use std::sync::OnceLock;

/// Filename for AI.md files
pub const AI_MD_FILENAME: &str = "AI.md";
/// Filename for local AI.md files
pub const AI_MD_LOCAL_FILENAME: &str = "AI.local.md";
/// Rules directory name (for project-level rules)
pub const PROJECT_RULES_DIR: &str = ".ai/rules";

/// Maximum depth for @include recursion
const MAX_INCLUDE_DEPTH: usize = 5;

/// Home directory cache
static HOME_DIR: OnceLock<Option<PathBuf>> = OnceLock::new();

/// Get home directory
fn get_home_dir() -> Option<&'static PathBuf> {
    HOME_DIR.get_or_init(|| dirs::home_dir()).as_ref()
}

/// Get config directory (~/.ai)
fn get_config_dir() -> Option<PathBuf> {
    get_home_dir().map(|h| h.join(".ai"))
}

/// Get managed rules directory (/etc/ai-code/.ai/rules)
fn get_managed_rules_dir() -> Option<PathBuf> {
    PathBuf::from("/etc/ai-code/.ai/rules")
        .exists()
        .then_some(PathBuf::from("/etc/ai-code/.ai/rules"))
}

/// Get user rules directory (~/.ai/rules)
fn get_user_rules_dir() -> Option<PathBuf> {
    get_config_dir()
        .map(|p| p.join("rules"))
        .filter(|p| p.exists())
}

/// Check if a file extension is text
#[allow(dead_code)]
fn is_text_file(path: &Path) -> bool {
    let ext = path.extension().and_then(|e| e.to_str()).unwrap_or("");
    is_allowed_extension(&format!(".{}", ext))
}

/// Parse frontmatter from markdown content
fn parse_frontmatter(content: &str) -> ParsedAiMd {
    if !content.starts_with("---") {
        return ParsedAiMd {
            frontmatter: AiMdFrontmatter::default(),
            content: content.to_string(),
        };
    }

    // Find closing ---
    if let Some(end_idx) = content[3..].find("---") {
        let frontmatter_str = &content[3..end_idx + 3];
        let body = &content[end_idx + 6..];

        // Parse YAML frontmatter (simple parsing)
        let mut frontmatter = AiMdFrontmatter::default();

        for line in frontmatter_str.lines() {
            let line = line.trim();
            if line.starts_with("paths:") {
                let value = line["paths:".len()..].trim();
                if value.starts_with('[') && value.ends_with(']') {
                    let inner = &value[1..value.len() - 1];
                    frontmatter.paths = Some(
                        inner
                            .split(',')
                            .map(|s| s.trim().trim_matches('"').to_string())
                            .filter(|s| !s.is_empty())
                            .collect(),
                    );
                }
            } else if line.starts_with("name:") {
                frontmatter.name = Some(line["name:".len()..].trim().to_string());
            } else if line.starts_with("description:") {
                frontmatter.description = Some(line["description:".len()..].trim().to_string());
            } else if line.starts_with("type:") {
                frontmatter.r#type = Some(line["type:".len()..].trim().to_string());
            }
        }

        ParsedAiMd {
            frontmatter,
            content: body.trim_start().to_string(),
        }
    } else {
        ParsedAiMd {
            frontmatter: AiMdFrontmatter::default(),
            content: content.to_string(),
        }
    }
}

/// Extract @include paths from content
fn extract_include_paths(content: &str, base_path: &Path) -> Vec<PathBuf> {
    let mut paths = Vec::new();
    let include_regex = regex::Regex::new(r"(?:^|\s)@((?:[^\s\\]|\\ )+)").ok();

    if let Some(re) = include_regex {
        for cap in re.captures_iter(content) {
            if let Some(path_match) = cap.get(1) {
                let mut path = path_match.as_str().to_string();

                // Remove fragment identifiers
                if let Some(idx) = path.find('#') {
                    path.truncate(idx);
                }
                if path.is_empty() {
                    continue;
                }

                // Unescape spaces
                path = path.replace("\\ ", " ");

                // Resolve path
                let resolved = resolve_include_path(&path, base_path);
                if let Some(resolved) = resolved {
                    paths.push(resolved);
                }
            }
        }
    }

    paths
}

/// Resolve include path (supports @path, @./path, @~/path, @/path)
fn resolve_include_path(path: &str, base_dir: &Path) -> Option<PathBuf> {
    let path = path.trim();

    if path.starts_with("@~/") || (path.starts_with("~/") && !path.starts_with("@")) {
        // Home directory path
        let rel = path.trim_start_matches("~/").trim_start_matches("@~/");
        get_home_dir().map(|h| h.join(rel))
    } else if path.starts_with("@/") {
        // Absolute path
        Some(PathBuf::from(path.trim_start_matches("@/")))
    } else if path.starts_with("@./") || path.starts_with("./") {
        // Relative to current file
        let rel = path.trim_start_matches("@./").trim_start_matches("./");
        Some(base_dir.join(rel))
    } else if path.starts_with('@') {
        // @path (relative)
        let rel = path.trim_start_matches('@');
        Some(base_dir.join(rel))
    } else if !path.starts_with('@')
        && !path.starts_with('#')
        && !path.starts_with('%')
        && !path.starts_with('^')
        && !path.starts_with('&')
        && !path.starts_with('*')
        && !path.starts_with('(')
        && !path.starts_with(')')
        && path
            .chars()
            .next()
            .map(|c| c.is_alphanumeric() || c == '.' || c == '_' || c == '-')
            .unwrap_or(false)
    {
        // Plain path (relative)
        Some(base_dir.join(path))
    } else {
        None
    }
}

/// Process a single AI.md file
pub fn process_ai_md_file(
    path: &Path,
    md_type: AiMdType,
    processed_paths: &mut HashSet<String>,
    depth: usize,
    parent: Option<&str>,
) -> Result<Vec<AiMdFile>, AgentError> {
    // Check depth limit
    if depth >= MAX_INCLUDE_DEPTH {
        return Ok(Vec::new());
    }

    // Normalize and check if already processed
    let canonical = path.canonicalize().unwrap_or_else(|_| path.to_path_buf());
    let normalized = canonical.to_string_lossy().to_lowercase();

    if processed_paths.contains(&normalized) {
        return Ok(Vec::new());
    }

    // Check if file exists
    if !path.exists() {
        return Ok(Vec::new());
    }

    // Read file
    let raw_content = fs::read_to_string(path).map_err(|e| AgentError::Io(e))?;

    if raw_content.trim().is_empty() {
        return Ok(Vec::new());
    }

    processed_paths.insert(normalized);

    let parent_str = parent.map(|s| s.to_string());
    let base_dir = path.parent().unwrap_or(Path::new("."));

    // Parse content
    let parsed = parse_frontmatter(&raw_content);

    // Extract include paths
    let include_paths = extract_include_paths(&parsed.content, base_dir);

    let content_differs = parsed.content != raw_content;

    // Create main file entry
    let mut result = vec![AiMdFile {
        path: path.to_string_lossy().to_string(),
        md_type,
        content: parsed.content.clone(),
        raw_content: if content_differs {
            Some(raw_content)
        } else {
            None
        },
        globs: parsed.frontmatter.paths,
        parent: parent_str.clone(),
        content_differs_from_disk: content_differs,
    }];

    // Process included files
    for include_path in include_paths {
        let included = process_ai_md_file(
            &include_path,
            md_type,
            processed_paths,
            depth + 1,
            Some(&path.to_string_lossy()),
        )?;
        result.extend(included);
    }

    Ok(result)
}

/// Get all AI.md files for a given working directory
pub fn get_ai_md_files(cwd: &Path) -> Result<Vec<AiMdFile>, AgentError> {
    let mut result = Vec::new();
    let mut processed_paths = HashSet::new();

    // 1. Managed memory (/etc/ai-code/AI.md and /etc/ai-code/.ai/rules/*.md)
    if let Some(managed_dir) = PathBuf::from("/etc/ai-code")
        .exists()
        .then_some(PathBuf::from("/etc/ai-code"))
    {
        let managed_md = managed_dir.join(AI_MD_FILENAME);
        if managed_md.exists() {
            let files = process_ai_md_file(
                &managed_md,
                AiMdType::Managed,
                &mut processed_paths,
                0,
                None,
            )?;
            result.extend(files);
        }

        // Managed rules
        if let Some(rules_dir) = get_managed_rules_dir() {
            result.extend(load_rules_from_dir(
                &rules_dir,
                AiMdType::Managed,
                &mut processed_paths,
                0,
            )?);
        }
    }

    // 2. User memory (~/.ai/AI.md and ~/.ai/rules/*.md)
    if let Some(config_dir) = get_config_dir() {
        let user_md = config_dir.join(AI_MD_FILENAME);
        if user_md.exists() {
            let files =
                process_ai_md_file(&user_md, AiMdType::User, &mut processed_paths, 0, None)?;
            result.extend(files);
        }

        // User rules
        if let Some(rules_dir) = get_user_rules_dir() {
            result.extend(load_rules_from_dir(
                &rules_dir,
                AiMdType::User,
                &mut processed_paths,
                0,
            )?);
        }
    }

    // 3. Project memory (traverse from CWD up to root)
    let mut current_dir = cwd.to_path_buf();
    let root = PathBuf::from("/");

    while current_dir != root {
        // AI.md in current directory
        let project_md = current_dir.join(AI_MD_FILENAME);
        if project_md.exists() {
            let files = process_ai_md_file(
                &project_md,
                AiMdType::Project,
                &mut processed_paths,
                0,
                None,
            )?;
            result.extend(files);
        }

        // .ai/rules/*.md
        let rules_dir = current_dir.join(PROJECT_RULES_DIR);
        if rules_dir.exists() {
            result.extend(load_rules_from_dir(
                &rules_dir,
                AiMdType::Project,
                &mut processed_paths,
                0,
            )?);
        }

        // Move to parent
        if !current_dir.pop() {
            break;
        }
    }

    // 4. Local memory (AI.local.md - traverse from CWD up to root)
    let mut local_dir = cwd.to_path_buf();
    while local_dir != root {
        let local_md = local_dir.join(AI_MD_LOCAL_FILENAME);
        if local_md.exists() {
            let files =
                process_ai_md_file(&local_md, AiMdType::Local, &mut processed_paths, 0, None)?;
            result.extend(files);
        }

        if !local_dir.pop() {
            break;
        }
    }

    Ok(result)
}

/// Load .ai/rules/*.md files from a directory
fn load_rules_from_dir(
    dir: &Path,
    md_type: AiMdType,
    processed_paths: &mut HashSet<String>,
    depth: usize,
) -> Result<Vec<AiMdFile>, AgentError> {
    let mut result = Vec::new();

    if !dir.exists() || !dir.is_dir() {
        return Ok(result);
    }

    let entries = fs::read_dir(dir).map_err(|e| AgentError::Io(e))?;

    for entry in entries.flatten() {
        let path = entry.path();

        if path.is_dir() {
            // Recursively process subdirectories
            result.extend(load_rules_from_dir(&path, md_type, processed_paths, depth)?);
        } else if path.extension().and_then(|e| e.to_str()) == Some("md") {
            // Process .md files
            let files = process_ai_md_file(&path, md_type, processed_paths, depth, None)?;
            result.extend(files);
        }
    }

    Ok(result)
}

/// Format AI.md files for inclusion in system prompt
pub fn load_ai_md(cwd: &Path) -> Result<Option<String>, AgentError> {
    let files = get_ai_md_files(cwd)?;

    if files.is_empty() {
        return Ok(None);
    }

    let contents: Vec<AiMdContent> = files
        .into_iter()
        .filter(|f| !f.content.trim().is_empty())
        .map(|f| AiMdContent::new(f.path, f.content, f.md_type))
        .collect();

    if contents.is_empty() {
        return Ok(None);
    }

    let mut result = AI_MD_INSTRUCTION_PROMPT.to_string();
    result.push_str("\n\n");

    for c in contents {
        result.push_str(&format!(
            "Contents of {} {}:\n\n{}\n\n",
            c.path, c.type_description, c.content
        ));
    }

    Ok(Some(result))
}

#[cfg(test)]
#[allow(unused_imports)]
mod tests {
    use super::*;
    use tempfile::TempDir;

    #[test]
    fn test_parse_frontmatter() {
        let content = r#"---
name: test
paths: ["*.rs", "*.toml"]
---
# Hello World

This is content.
"#;
        let parsed = parse_frontmatter(content);
        assert!(parsed.frontmatter.name.is_some());
        assert_eq!(parsed.frontmatter.name.unwrap(), "test");
        assert!(parsed.frontmatter.paths.is_some());
    }

    #[test]
    fn test_process_file() {
        let temp_dir = TempDir::new().unwrap();
        let md_path = temp_dir.path().join("AI.md");
        fs::write(&md_path, "# Test\n\nContent here.").unwrap();

        let mut processed = HashSet::new();
        let result =
            process_ai_md_file(&md_path, AiMdType::Project, &mut processed, 0, None).unwrap();

        assert!(!result.is_empty());
        assert_eq!(result[0].md_type, AiMdType::Project);
    }

    #[test]
    fn test_get_ai_md_files() {
        let temp_dir = TempDir::new().unwrap();
        let md_path = temp_dir.path().join("AI.md");
        fs::write(&md_path, "# Project AI.md\n\nSome instructions.").unwrap();

        let files = get_ai_md_files(temp_dir.path()).unwrap();
        assert!(!files.is_empty());
    }
}