ucm-core 0.1.18

Core types and traits for the Unified Content Model
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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
//! Block metadata for search, display, and LLM optimization.

use crate::content::Content;
use crate::id::ContentHash;
use crate::normalize::is_cjk_character;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::error::Error as StdError;
use std::fmt;
use std::str::FromStr;

/// Block metadata
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct BlockMetadata {
    /// Semantic role in document structure
    #[serde(skip_serializing_if = "Option::is_none")]
    pub semantic_role: Option<SemanticRole>,

    /// Human-readable label
    #[serde(skip_serializing_if = "Option::is_none")]
    pub label: Option<String>,

    /// Searchable tags
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub tags: Vec<String>,

    /// Pre-computed summary for folding/context management
    #[serde(skip_serializing_if = "Option::is_none")]
    pub summary: Option<String>,

    /// Estimated token count (computed lazily)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub token_estimate: Option<TokenEstimate>,

    /// Content hash for change detection
    pub content_hash: ContentHash,

    /// Creation timestamp
    pub created_at: DateTime<Utc>,

    /// Last modification timestamp
    pub modified_at: DateTime<Utc>,

    /// Custom key-value metadata
    #[serde(default, skip_serializing_if = "HashMap::is_empty")]
    pub custom: HashMap<String, serde_json::Value>,
}

impl BlockMetadata {
    /// Create new metadata with current timestamp
    pub fn new(content_hash: ContentHash) -> Self {
        let now = Utc::now();
        Self {
            semantic_role: None,
            label: None,
            tags: Vec::new(),
            summary: None,
            token_estimate: None,
            content_hash,
            created_at: now,
            modified_at: now,
            custom: HashMap::new(),
        }
    }

    /// Set semantic role
    pub fn with_role(mut self, role: SemanticRole) -> Self {
        self.semantic_role = Some(role);
        self
    }

    /// Set label
    pub fn with_label(mut self, label: impl Into<String>) -> Self {
        self.label = Some(label.into());
        self
    }

    /// Add a tag
    pub fn with_tag(mut self, tag: impl Into<String>) -> Self {
        self.tags.push(tag.into());
        self
    }

    /// Add multiple tags
    pub fn with_tags(mut self, tags: impl IntoIterator<Item = impl Into<String>>) -> Self {
        self.tags.extend(tags.into_iter().map(|t| t.into()));
        self
    }

    /// Set summary
    pub fn with_summary(mut self, summary: impl Into<String>) -> Self {
        self.summary = Some(summary.into());
        self
    }

    /// Set custom metadata
    pub fn with_custom(mut self, key: impl Into<String>, value: serde_json::Value) -> Self {
        self.custom.insert(key.into(), value);
        self
    }

    /// Update modification timestamp
    pub fn touch(&mut self) {
        self.modified_at = Utc::now();
    }

    /// Check if block has a specific tag
    pub fn has_tag(&self, tag: &str) -> bool {
        self.tags.iter().any(|t| t == tag)
    }
}

impl Default for BlockMetadata {
    fn default() -> Self {
        Self::new(ContentHash::from_bytes([0u8; 32]))
    }
}

/// Semantic role in document structure
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct SemanticRole {
    /// Primary category
    pub category: RoleCategory,
    /// Subcategory (optional)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub subcategory: Option<String>,
    /// Custom qualifier
    #[serde(skip_serializing_if = "Option::is_none")]
    pub qualifier: Option<String>,
}

impl SemanticRole {
    pub fn new(category: RoleCategory) -> Self {
        Self {
            category,
            subcategory: None,
            qualifier: None,
        }
    }

    pub fn with_subcategory(mut self, sub: impl Into<String>) -> Self {
        self.subcategory = Some(sub.into());
        self
    }

    pub fn with_qualifier(mut self, qual: impl Into<String>) -> Self {
        self.qualifier = Some(qual.into());
        self
    }

    /// Parse from string format (e.g., "intro.hook")
    pub fn parse(s: &str) -> Option<Self> {
        let parts: Vec<&str> = s.split('.').collect();
        if parts.is_empty() {
            return None;
        }

        let category = RoleCategory::from_str(parts[0]).ok()?;
        let subcategory = parts.get(1).map(|s| s.to_string());
        let qualifier = parts.get(2).map(|s| s.to_string());

        Some(Self {
            category,
            subcategory,
            qualifier,
        })
    }
}

impl std::fmt::Display for SemanticRole {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.category.as_str())?;
        if let Some(ref sub) = self.subcategory {
            write!(f, ".{}", sub)?;
        }
        if let Some(ref qual) = self.qualifier {
            write!(f, ".{}", qual)?;
        }
        Ok(())
    }
}

/// Semantic role categories
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum RoleCategory {
    // Document structure
    Title,
    Subtitle,
    Abstract,
    TableOfContents,

    // Headings (H1-H6)
    Heading1,
    Heading2,
    Heading3,
    Heading4,
    Heading5,
    Heading6,

    // Paragraphs and lists
    Paragraph,
    List,

    // Introduction
    Intro,
    IntroHook,
    IntroContext,
    IntroThesis,

    // Body
    Body,
    BodyArgument,
    BodyEvidence,
    BodyExample,
    BodyCounterargument,
    BodyTransition,

    // Conclusion
    Conclusion,
    ConclusionSummary,
    ConclusionImplication,
    ConclusionCallToAction,

    // Special sections
    Sidebar,
    Callout,
    Warning,
    Note,
    Quote,

    // Technical
    Definition,
    Theorem,
    Proof,
    Algorithm,
    Code,

    // Meta
    Metadata,
    Citation,
    Footnote,
    Appendix,
    Reference,

    // Custom
    Custom,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RoleCategoryParseError(pub String);

impl fmt::Display for RoleCategoryParseError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "unknown role category '{}'", self.0)
    }
}

impl StdError for RoleCategoryParseError {}

impl RoleCategory {
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Title => "title",
            Self::Subtitle => "subtitle",
            Self::Abstract => "abstract",
            Self::TableOfContents => "toc",
            Self::Heading1 => "heading1",
            Self::Heading2 => "heading2",
            Self::Heading3 => "heading3",
            Self::Heading4 => "heading4",
            Self::Heading5 => "heading5",
            Self::Heading6 => "heading6",
            Self::Paragraph => "paragraph",
            Self::List => "list",
            Self::Intro => "intro",
            Self::IntroHook => "intro_hook",
            Self::IntroContext => "intro_context",
            Self::IntroThesis => "intro_thesis",
            Self::Body => "body",
            Self::BodyArgument => "body_argument",
            Self::BodyEvidence => "body_evidence",
            Self::BodyExample => "body_example",
            Self::BodyCounterargument => "body_counterargument",
            Self::BodyTransition => "body_transition",
            Self::Conclusion => "conclusion",
            Self::ConclusionSummary => "conclusion_summary",
            Self::ConclusionImplication => "conclusion_implication",
            Self::ConclusionCallToAction => "conclusion_cta",
            Self::Sidebar => "sidebar",
            Self::Callout => "callout",
            Self::Warning => "warning",
            Self::Note => "note",
            Self::Quote => "quote",
            Self::Definition => "definition",
            Self::Theorem => "theorem",
            Self::Proof => "proof",
            Self::Algorithm => "algorithm",
            Self::Code => "code",
            Self::Metadata => "metadata",
            Self::Citation => "citation",
            Self::Footnote => "footnote",
            Self::Appendix => "appendix",
            Self::Reference => "reference",
            Self::Custom => "custom",
        }
    }
}

impl FromStr for RoleCategory {
    type Err = RoleCategoryParseError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "title" => Ok(Self::Title),
            "subtitle" => Ok(Self::Subtitle),
            "abstract" => Ok(Self::Abstract),
            "toc" | "table_of_contents" => Ok(Self::TableOfContents),
            "heading1" | "h1" => Ok(Self::Heading1),
            "heading2" | "h2" => Ok(Self::Heading2),
            "heading3" | "h3" => Ok(Self::Heading3),
            "heading4" | "h4" => Ok(Self::Heading4),
            "heading5" | "h5" => Ok(Self::Heading5),
            "heading6" | "h6" => Ok(Self::Heading6),
            "paragraph" | "para" | "p" => Ok(Self::Paragraph),
            "list" | "ul" | "ol" => Ok(Self::List),
            "intro" | "introduction" => Ok(Self::Intro),
            "intro_hook" | "hook" => Ok(Self::IntroHook),
            "intro_context" | "context" => Ok(Self::IntroContext),
            "intro_thesis" | "thesis" => Ok(Self::IntroThesis),
            "body" => Ok(Self::Body),
            "body_argument" | "argument" => Ok(Self::BodyArgument),
            "body_evidence" | "evidence" => Ok(Self::BodyEvidence),
            "body_example" | "example" => Ok(Self::BodyExample),
            "body_counterargument" | "counterargument" => Ok(Self::BodyCounterargument),
            "body_transition" | "transition" => Ok(Self::BodyTransition),
            "conclusion" => Ok(Self::Conclusion),
            "conclusion_summary" | "summary" => Ok(Self::ConclusionSummary),
            "conclusion_implication" | "implication" => Ok(Self::ConclusionImplication),
            "conclusion_cta" | "cta" | "call_to_action" => Ok(Self::ConclusionCallToAction),
            "sidebar" => Ok(Self::Sidebar),
            "callout" => Ok(Self::Callout),
            "warning" => Ok(Self::Warning),
            "note" => Ok(Self::Note),
            "quote" | "blockquote" => Ok(Self::Quote),
            "definition" => Ok(Self::Definition),
            "theorem" => Ok(Self::Theorem),
            "proof" => Ok(Self::Proof),
            "algorithm" => Ok(Self::Algorithm),
            "code" => Ok(Self::Code),
            "metadata" | "meta" => Ok(Self::Metadata),
            "citation" | "cite" => Ok(Self::Citation),
            "footnote" => Ok(Self::Footnote),
            "appendix" => Ok(Self::Appendix),
            "reference" | "ref" => Ok(Self::Reference),
            "custom" => Ok(Self::Custom),
            _ => Err(RoleCategoryParseError(s.to_string())),
        }
    }
}

/// Token estimation with model awareness
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct TokenEstimate {
    /// Estimated tokens for GPT-4 tokenizer
    pub gpt4: u32,
    /// Estimated tokens for Claude tokenizer
    pub claude: u32,
    /// Estimated tokens for Llama tokenizer
    pub llama: u32,
    /// Generic estimate (average)
    pub generic: u32,
}

impl TokenEstimate {
    /// Create a new token estimate with all models
    pub fn new(gpt4: u32, claude: u32, llama: u32) -> Self {
        let generic = (gpt4 + claude + llama) / 3;
        Self {
            gpt4,
            claude,
            llama,
            generic,
        }
    }

    /// Compute token estimate from content
    pub fn compute(content: &Content) -> Self {
        match content {
            Content::Text(text) => Self::estimate_text(&text.text),
            Content::Code(code) => Self::estimate_code(&code.source, &code.language),
            Content::Table(table) => Self::estimate_table(&table.columns, &table.rows),
            Content::Json { value, .. } => Self::estimate_json(value),
            Content::Math(math) => Self::estimate_text(&math.expression),
            _ => Self::default_estimate(),
        }
    }

    /// Get estimate for a specific model
    pub fn for_model(&self, model: TokenModel) -> u32 {
        match model {
            TokenModel::GPT4 => self.gpt4,
            TokenModel::Claude => self.claude,
            TokenModel::Llama => self.llama,
            TokenModel::Generic => self.generic,
        }
    }

    fn estimate_text(text: &str) -> Self {
        let char_count = text.chars().count();
        let word_count = text.split_whitespace().count();

        // Detect script type for better estimation
        let cjk_count = text.chars().filter(|c| is_cjk_character(*c)).count();
        let cjk_ratio = cjk_count as f32 / char_count.max(1) as f32;

        // CJK characters are ~1-2 tokens each, Latin ~4 chars per token
        let base_estimate = if cjk_ratio > 0.5 {
            (char_count as f32 * 1.5) as u32
        } else {
            (word_count as f32 * 1.3 + char_count as f32 / 4.0) as u32 / 2
        };

        Self {
            gpt4: base_estimate,
            claude: (base_estimate as f32 * 1.1) as u32,
            llama: (base_estimate as f32 * 0.95) as u32,
            generic: base_estimate,
        }
    }

    fn estimate_code(source: &str, language: &str) -> Self {
        let line_count = source.lines().count();
        let char_count = source.len();

        // Code typically has more tokens due to punctuation
        let base = (char_count / 3 + line_count * 2) as u32;

        // Language-specific adjustments
        let factor = match language.to_lowercase().as_str() {
            "rust" | "cpp" | "c" | "c++" => 1.2,
            "python" => 0.9,
            "javascript" | "typescript" | "js" | "ts" => 1.1,
            "go" | "golang" => 1.0,
            "java" => 1.15,
            _ => 1.0,
        };

        let adjusted = (base as f32 * factor) as u32;

        Self {
            gpt4: adjusted,
            claude: (adjusted as f32 * 1.05) as u32,
            llama: (adjusted as f32 * 0.95) as u32,
            generic: adjusted,
        }
    }

    fn estimate_table(columns: &[crate::content::Column], rows: &[crate::content::Row]) -> Self {
        let cell_count = columns.len() * rows.len();
        let header_tokens = columns.len() * 5; // ~5 tokens per header
        let cell_tokens = cell_count * 3; // ~3 tokens per cell average
        let structure_tokens = rows.len() * 2; // Row separators

        let total = (header_tokens + cell_tokens + structure_tokens) as u32;

        Self {
            gpt4: total,
            claude: (total as f32 * 1.1) as u32,
            llama: total,
            generic: total,
        }
    }

    fn estimate_json(value: &serde_json::Value) -> Self {
        let serialized = serde_json::to_string(value).unwrap_or_default();
        Self::estimate_text(&serialized)
    }

    fn default_estimate() -> Self {
        Self {
            gpt4: 100,
            claude: 110,
            llama: 95,
            generic: 100,
        }
    }
}

impl Default for TokenEstimate {
    fn default() -> Self {
        Self::default_estimate()
    }
}

/// Token model selector
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TokenModel {
    GPT4,
    Claude,
    Llama,
    Generic,
}

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

    #[test]
    fn test_semantic_role_parse() {
        let role = SemanticRole::parse("intro.hook").unwrap();
        assert_eq!(role.category, RoleCategory::Intro);
        assert_eq!(role.subcategory, Some("hook".to_string()));
    }

    #[test]
    fn test_semantic_role_display() {
        let role = SemanticRole::new(RoleCategory::Intro)
            .with_subcategory("hook")
            .with_qualifier("v2");
        assert_eq!(role.to_string(), "intro.hook.v2");
    }

    #[test]
    fn test_role_category_roundtrip() {
        let category = RoleCategory::BodyEvidence;
        let s = category.as_str();
        let parsed = RoleCategory::from_str(s).unwrap();
        assert_eq!(parsed, category);
    }

    #[test]
    fn test_token_estimate_text() {
        let estimate = TokenEstimate::estimate_text("Hello, world! This is a test.");
        assert!(estimate.gpt4 > 0);
        assert!(estimate.claude > 0);
    }

    #[test]
    fn test_token_estimate_cjk() {
        let estimate = TokenEstimate::estimate_text("你好世界");
        // CJK should have higher token count per character
        assert!(estimate.gpt4 > 0);
    }

    #[test]
    fn test_metadata_builder() {
        let hash = ContentHash::from_bytes([1u8; 32]);
        let metadata = BlockMetadata::new(hash)
            .with_label("Test Block")
            .with_tags(["important", "draft"])
            .with_role(SemanticRole::new(RoleCategory::Intro));

        assert_eq!(metadata.label, Some("Test Block".to_string()));
        assert!(metadata.has_tag("important"));
        assert!(metadata.has_tag("draft"));
    }
}