llm-transpile 0.4.1

High-performance LLM context bridge — token-optimized document transpiler
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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
//! renderer.rs — DocNode → bridge format renderer
//!
//! Final output format:
//! ```text
//! <D>                   ← SymbolDict global dictionary (omitted if empty)
//! SymA=TermA
//! </D>
//! <H>                   ← YAML header (title, summary, keywords)
//! t: document title
//! s: one-line summary
//! k: [kw1, kw2]
//! </H>
//! <B>                   ← body (compression + substitution applied)
//! ...
//! </B>
//! ```

use crate::ir::{DocNode, IRDocument};
use crate::symbol::SymbolDict;

// ────────────────────────────────────────────────
// 1. Individual node renderer
// ────────────────────────────────────────────────

/// Renders a single `DocNode` as bridge text.
///
/// If `dict` is provided, registered terms in the body are replaced with PUA symbols.
pub fn render_node(node: &DocNode, dict: &SymbolDict) -> String {
    match node {
        DocNode::Header { level, text } => {
            let prefix = "#".repeat(*level as usize);
            let encoded = dict.encode_str(text);
            format!("{} {}", prefix, encoded.trim())
        }

        DocNode::Para { text, .. } => {
            // Minimize consecutive whitespace and newlines
            let normalized = normalize_whitespace(text);
            dict.encode_str(&normalized)
        }

        DocNode::Table { headers, rows } => linearize_table(headers, rows),

        DocNode::Code { lang, body } => {
            let lang_tag = lang.as_deref().unwrap_or("");
            format!("```{}\n{}\n```", lang_tag, body.trim())
        }

        DocNode::List { ordered, items } => items
            .iter()
            .enumerate()
            .map(|(i, item)| {
                let encoded = dict.encode_str(item);
                if *ordered {
                    format!("{}. {}", i + 1, encoded.trim())
                } else {
                    format!("- {}", encoded.trim())
                }
            })
            .collect::<Vec<_>>()
            .join("\n"),

        DocNode::Metadata { key, value } => {
            // Metadata is not emitted directly at the renderer level.
            // The YAML header builder (`build_yaml_header`) handles it separately.
            let _ = (key, value);
            String::new()
        }
    }
}

// ────────────────────────────────────────────────
// 2. Table linearization
// ────────────────────────────────────────────────

/// Converts a table into token-efficient text.
///
/// | Row count | Output format                       |
/// |-----------|-------------------------------------|
/// | ≤ 5       | `Key:Val, Key:Val` sequence         |
/// | > 5       | JSON Lines (1 row = 1 JSON object)  |
pub fn linearize_table(headers: &[String], rows: &[Vec<String>]) -> String {
    if rows.is_empty() {
        return String::new();
    }
    if rows.len() <= 5 {
        rows.iter()
            .enumerate()
            .map(|(i, row)| {
                let pairs: Vec<String> = headers
                    .iter()
                    .zip(row.iter())
                    .map(|(h, v)| format!("{}:{}", h.trim(), v.trim()))
                    .collect();
                format!("[{}] {}", i + 1, pairs.join(", "))
            })
            .collect::<Vec<_>>()
            .join("\n")
    } else {
        // Compact pipe-separated format — significantly fewer tokens than JSON Lines.
        // Format: header row first, then one data row per line.
        // Example: `Name|Age\nAlice|30\nBob|25`
        let header_row = headers
            .iter()
            .map(|h| h.trim())
            .collect::<Vec<_>>()
            .join("|");
        let data_rows = rows
            .iter()
            .map(|row| row.iter().map(|v| v.trim()).collect::<Vec<_>>().join("|"))
            .collect::<Vec<_>>()
            .join("\n");
        format!("{}\n{}", header_row, data_rows)
    }
}

// ────────────────────────────────────────────────
// 3. YAML header builder
// ────────────────────────────────────────────────

/// Builds a YAML header block from the IRDocument's metadata.
///
/// Example output:
/// ```yaml
/// t: Contract Analysis Report
/// s: Summary of key clauses in the software license agreement signed in 2024
/// k: [license, contract, software]
/// ```
pub fn build_yaml_header(doc: &IRDocument) -> String {
    let title = doc.get_metadata("title").unwrap_or("");
    let summary = doc.get_metadata("summary").unwrap_or("");
    let keywords = doc.get_metadata("keywords").unwrap_or("");

    let mut lines = Vec::new();
    if !title.is_empty() {
        lines.push(format!("t: {}", title.trim()));
    }
    if !summary.is_empty() {
        lines.push(format!("s: {}", summary.trim()));
    }
    if !keywords.is_empty() {
        // "kw1, kw2, kw3" → "[kw1, kw2, kw3]"  (wrap in brackets)
        let kws: Vec<&str> = keywords.split(',').map(str::trim).collect();
        lines.push(format!("k: [{}]", kws.join(", ")));
    }
    lines.join("\n")
}

// ────────────────────────────────────────────────
// 4. Full document renderer
// ────────────────────────────────────────────────

/// Renders an entire IRDocument as a bridge-format string.
///
/// Output structure: `<D>?` + `<H>` + `<B>`
pub fn render_full(doc: &IRDocument, dict: &mut SymbolDict) -> String {
    let allows_compression = doc.fidelity.allows_compression();
    let mut last_header_text: Option<String> = None;

    // ① Render body first (the dictionary is populated during substitution)
    let body_lines: Vec<String> = doc
        .nodes
        .iter()
        .filter_map(|node| {
            // Metadata is handled by the header builder
            if matches!(node, crate::ir::DocNode::Metadata { .. }) {
                return None;
            }

            // Track header text for R5 dedup
            if let DocNode::Header { text, .. } = node {
                last_header_text = Some(text.clone());
            }

            // R6: Compress code blocks when fidelity allows it
            let rendered = match node {
                DocNode::Code { lang, body } if allows_compression => {
                    let compressed = compress_code_block(body, lang.as_deref());
                    let lang_tag = lang.as_deref().unwrap_or("");
                    format!("```{}\n{}\n```", lang_tag, compressed.trim())
                }
                _ => render_node(node, dict),
            };

            // R5: Strip duplicate header text from immediately following paragraph
            let rendered = if allows_compression {
                if let DocNode::Para { .. } = node {
                    if let Some(ref header_text) = last_header_text.take() {
                        strip_leading_header_duplicate(&rendered, header_text)
                    } else {
                        rendered
                    }
                } else {
                    rendered
                }
            } else {
                rendered
            };

            // Reset header tracking for non-header, non-para nodes
            if !matches!(node, DocNode::Header { .. } | DocNode::Para { .. }) {
                last_header_text = None;
            }

            if rendered.is_empty() {
                None
            } else {
                Some(rendered)
            }
        })
        .collect();
    let body = body_lines.join("\n");

    // ② Global dictionary block
    let dict_block = dict.render_dict_header();

    // ③ YAML header
    let yaml_header = build_yaml_header(doc);

    // ④ Assemble output
    let mut output = String::new();
    if !dict_block.is_empty() {
        output.push_str(&dict_block);
    }
    if !yaml_header.is_empty() {
        output.push_str("<H>\n");
        output.push_str(yaml_header.trim());
        output.push_str("\n</H>\n");
    }
    output.push_str("<B>\n");
    output.push_str(body.trim());
    output.push_str("\n</B>");

    output
}

// ────────────────────────────────────────────────
// 5. Internal utilities
// ────────────────────────────────────────────────

/// Normalizes consecutive whitespace and newlines to a single space.
fn normalize_whitespace(s: &str) -> String {
    let mut result = String::with_capacity(s.len());
    let mut prev_space = false;
    for c in s.chars() {
        if c.is_whitespace() {
            if !prev_space {
                result.push(' ');
            }
            prev_space = true;
        } else {
            result.push(c);
            prev_space = false;
        }
    }
    result.trim().to_string()
}

/// Strips leading duplicate of `header_text` from `para_text`.
///
/// Only strips if the paragraph starts with the header text followed by a space
/// (word boundary), or is exactly equal to the header text.
fn strip_leading_header_duplicate(para_text: &str, header_text: &str) -> String {
    let header = header_text.trim();
    let para = para_text.trim();

    if header.is_empty() || para.is_empty() {
        return para.to_string();
    }

    // Check if paragraph starts with the header text (using char-based comparison)
    if let Some(rest) = para.strip_prefix(header) {
        // Paragraph is longer -- only strip if followed by whitespace (word boundary)
        if !rest.is_empty() && rest.starts_with(char::is_whitespace) {
            return rest.trim().to_string();
        }
        // Exact match -- paragraph was just the header text repeated
        if rest.is_empty() {
            return String::new();
        }
    }

    para.to_string()
}

/// Strips comments and collapses consecutive blank lines in code blocks.
///
/// Only called when fidelity allows compression.
fn compress_code_block(body: &str, lang: Option<&str>) -> String {
    let lines: Vec<&str> = body.lines().collect();
    let mut result = Vec::with_capacity(lines.len());
    let mut blank_count = 0usize;

    for line in lines {
        let trimmed = line.trim();

        // Skip single-line comments based on language
        let is_comment = match lang {
            Some(l)
                if [
                    "rust",
                    "go",
                    "java",
                    "javascript",
                    "js",
                    "typescript",
                    "ts",
                    "c",
                    "cpp",
                    "csharp",
                    "cs",
                    "swift",
                    "kotlin",
                    "scala",
                ]
                .contains(&l) =>
            {
                trimmed.starts_with("//")
            }
            Some(l)
                if [
                    "python", "py", "ruby", "rb", "perl", "pl", "r", "shell", "bash", "sh", "yaml",
                    "yml", "toml",
                ]
                .contains(&l) =>
            {
                trimmed.starts_with('#') && !trimmed.starts_with("#!")
            }
            Some(l) if ["sql"].contains(&l) => trimmed.starts_with("--"),
            _ => false, // Unknown language: don't strip comments
        };

        if is_comment {
            continue;
        }

        // Collapse consecutive blank lines to max 1
        if trimmed.is_empty() {
            blank_count += 1;
            if blank_count <= 1 {
                result.push(line);
            }
            continue;
        }

        blank_count = 0;
        result.push(line);
    }

    result.join("\n")
}

// ────────────────────────────────────────────────
// 6. Unit tests
// ────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ir::{DocNode, FidelityLevel, IRDocument};

    fn empty_dict() -> SymbolDict {
        SymbolDict::new()
    }

    #[test]
    fn header_renders_with_hashes() {
        let node = DocNode::Header {
            level: 2,
            text: "제목".into(),
        };
        let out = render_node(&node, &empty_dict());
        assert_eq!(out, "## 제목");
    }

    #[test]
    fn para_whitespace_normalized() {
        let node = DocNode::Para {
            text: "  공백이   많은   문장  ".into(),
            importance: 1.0,
        };
        let out = render_node(&node, &empty_dict());
        assert_eq!(out, "공백이 많은 문장");
    }

    #[test]
    fn table_small_key_val_format() {
        let headers = vec!["이름".into(), "나이".into()];
        let rows = vec![
            vec!["홍길동".into(), "30".into()],
            vec!["이순신".into(), "45".into()],
        ];
        let out = linearize_table(&headers, &rows);
        assert!(out.contains("이름:홍길동"));
        assert!(out.contains("나이:30"));
        assert!(out.contains("[1]"));
        assert!(out.contains("[2]"));
    }

    #[test]
    fn table_large_pipe_format() {
        let headers = vec!["id".into(), "val".into()];
        let rows: Vec<Vec<String>> = (0..6)
            .map(|i| vec![i.to_string(), format!("v{}", i)])
            .collect();
        let out = linearize_table(&headers, &rows);
        // Compact pipe format: header row first, then one data row per line
        let mut lines = out.lines();
        let header_line = lines.next().expect("header row");
        assert_eq!(header_line, "id|val");
        for (i, line) in lines.enumerate() {
            assert_eq!(line, format!("{}|v{}", i, i));
        }
    }

    #[test]
    fn ordered_list_renders_numbers() {
        let node = DocNode::List {
            ordered: true,
            items: vec!["첫째".into(), "둘째".into()],
        };
        let out = render_node(&node, &empty_dict());
        assert!(out.contains("1. 첫째"));
        assert!(out.contains("2. 둘째"));
    }

    #[test]
    fn render_full_structure() {
        let mut doc = IRDocument::new(FidelityLevel::Semantic, None);
        doc.push(DocNode::Metadata {
            key: "title".into(),
            value: "테스트".into(),
        });
        doc.push(DocNode::Metadata {
            key: "summary".into(),
            value: "요약".into(),
        });
        doc.push(DocNode::Para {
            text: "본문 내용".into(),
            importance: 1.0,
        });

        let mut dict = SymbolDict::new();
        let output = render_full(&doc, &mut dict);

        assert!(output.contains("<H>"));
        assert!(output.contains("t: 테스트"));
        assert!(output.contains("<B>"));
        assert!(output.contains("본문 내용"));
    }

    // ── R5: Header-body duplicate text removal ──

    #[test]
    fn header_body_dedup_removes_duplicate() {
        let mut doc = IRDocument::new(FidelityLevel::Semantic, None);
        doc.push(DocNode::Header {
            level: 2,
            text: "API Endpoints".into(),
        });
        doc.push(DocNode::Para {
            text: "API Endpoints are used for communication.".into(),
            importance: 1.0,
        });

        let mut dict = SymbolDict::new();
        let output = render_full(&doc, &mut dict);

        let body_section = output
            .split("<B>")
            .nth(1)
            .unwrap()
            .split("</B>")
            .next()
            .unwrap();
        // The paragraph line (after the header line) should not start with "API Endpoints"
        // because it duplicates the header text.
        // Body lines: ["", "## API Endpoints", "API Endpoints are used for communication.", ""]
        let para_line = body_section.lines().nth(2).unwrap_or("");
        assert!(
            !para_line.starts_with("API Endpoints"),
            "paragraph should not repeat header text: {para_line} (body: {body_section})"
        );
        assert!(
            body_section.contains("are used for communication"),
            "rest of paragraph must be preserved: {body_section}"
        );
    }

    #[test]
    fn header_body_dedup_lossless_preserves() {
        let mut doc = IRDocument::new(FidelityLevel::Lossless, None);
        doc.push(DocNode::Header {
            level: 2,
            text: "API Endpoints".into(),
        });
        doc.push(DocNode::Para {
            text: "API Endpoints are used for communication.".into(),
            importance: 1.0,
        });

        let mut dict = SymbolDict::new();
        let output = render_full(&doc, &mut dict);

        let body_section = output
            .split("<B>")
            .nth(1)
            .unwrap()
            .split("</B>")
            .next()
            .unwrap();
        assert!(
            body_section.contains("API Endpoints"),
            "Lossless mode must preserve all text: {body_section}"
        );
    }

    // ── R6: Code block compression ──

    #[test]
    fn code_block_strips_comments_in_semantic() {
        let mut doc = IRDocument::new(FidelityLevel::Semantic, None);
        doc.push(DocNode::Code {
            lang: Some("rust".into()),
            body: "// This is a comment\nfn main() {}\n// Another comment\n".into(),
        });

        let mut dict = SymbolDict::new();
        let output = render_full(&doc, &mut dict);

        assert!(
            !output.contains("This is a comment"),
            "comments should be stripped in Semantic mode: {output}"
        );
        assert!(
            output.contains("fn main()"),
            "code must be preserved: {output}"
        );
    }

    #[test]
    fn code_block_preserves_comments_in_lossless() {
        let mut doc = IRDocument::new(FidelityLevel::Lossless, None);
        doc.push(DocNode::Code {
            lang: Some("rust".into()),
            body: "// This is a comment\nfn main() {}".into(),
        });

        let mut dict = SymbolDict::new();
        let output = render_full(&doc, &mut dict);

        assert!(
            output.contains("This is a comment"),
            "Lossless mode must preserve comments: {output}"
        );
    }

    #[test]
    fn code_block_collapse_blank_lines() {
        let mut doc = IRDocument::new(FidelityLevel::Semantic, None);
        doc.push(DocNode::Code {
            lang: Some("python".into()),
            body: "def foo():\n\n\n    pass\n\n\ndef bar():\n    pass".into(),
        });

        let mut dict = SymbolDict::new();
        let output = render_full(&doc, &mut dict);

        assert!(
            !output.contains("\n\n\n"),
            "consecutive blank lines should be collapsed: {output}"
        );
    }
}