Skip to main content

agent_doc/
lib.rs

1//! # Module: lib (agent_doc)
2//!
3//! ## Spec
4//! - Exposes the public API surface consumed by the CLI binary, FFI layer, and editor plugins.
5//! - Re-exports: `component`, `crdt`, `debounce`, `ffi`, `frontmatter`, `merge`, `template`.
6//! - Provides two boundary-ID utilities used across all write paths:
7//!   - `new_boundary_id()` — 8-hex-char UUID v4 prefix (length controlled by `BOUNDARY_ID_LEN`).
8//!   - `new_boundary_id_with_summary(summary)` — same ID optionally suffixed with a 3-word,
9//!     20-char-max slug derived from `summary` (format: `a0cfeb34:boundary-fix`).
10//! - `format_boundary_marker(id)` — renders the canonical HTML comment form
11//!   `<!-- agent:boundary:<id> -->` used in document component boundaries.
12//! - `BOUNDARY_ID_LEN = 8` is a public constant; callers may read but should not override.
13//!
14//! ## Agentic Contracts
15//! - All public symbols are safe to call from FFI consumers (JNA, napi-rs) via `ffi` module.
16//! - `new_boundary_id` is non-deterministic (UUID v4); callers must not rely on ordering.
17//! - `new_boundary_id_with_summary(None)` and `new_boundary_id_with_summary(Some(""))` both
18//!   return a plain 8-char ID with no suffix.
19//! - Slug derivation: lowercase, non-alphanumeric chars → `-`, collapse runs, take first 3 words,
20//!   truncate to 20 chars.
21//!
22//! ## Evals
23//! - new_boundary_id_len: result is exactly `BOUNDARY_ID_LEN` chars of hex
24//! - new_boundary_id_with_summary_none: `None` summary → plain 8-char hex
25//! - new_boundary_id_with_summary_empty: `Some("")` → plain 8-char hex
26//! - new_boundary_id_with_summary_slug: `Some("Boundary Fix")` → `"<id>:boundary-fix"`
27//! - new_boundary_id_with_summary_truncate: long summary → slug capped at 20 chars
28//! - format_boundary_marker: `"abc123"` → `"<!-- agent:boundary:abc123 -->"`
29
30pub mod component;
31pub mod crdt;
32pub mod debounce;
33pub mod ffi;
34pub mod frontmatter;
35pub mod merge;
36pub mod template;
37
38/// Default number of hex characters for boundary IDs.
39pub const BOUNDARY_ID_LEN: usize = 8;
40
41/// Generate a new boundary ID (short hex string from UUID v4).
42pub fn new_boundary_id() -> String {
43    let full = uuid::Uuid::new_v4().to_string().replace('-', "");
44    full[..BOUNDARY_ID_LEN.min(full.len())].to_string()
45}
46
47/// Generate a boundary ID with an optional summary suffix.
48///
49/// Format: `a0cfeb34` or `a0cfeb34:boundary-fix` (with summary).
50/// The summary is slugified (lowercase, non-alphanumeric → `-`, max 20 chars).
51pub fn new_boundary_id_with_summary(summary: Option<&str>) -> String {
52    let id = new_boundary_id();
53    match summary {
54        Some(s) if !s.is_empty() => {
55            let slug: String = s.to_lowercase()
56                .chars()
57                .map(|c| if c.is_alphanumeric() { c } else { '-' })
58                .collect::<String>()
59                .split('-')
60                .filter(|s| !s.is_empty())
61                .take(3) // max 3 words
62                .collect::<Vec<&str>>()
63                .join("-");
64            let slug = &slug[..slug.len().min(20)];
65            format!("{}:{}", id, slug)
66        }
67        _ => id,
68    }
69}
70
71/// Format a boundary marker comment.
72pub fn format_boundary_marker(id: &str) -> String {
73    format!("<!-- agent:boundary:{} -->", id)
74}