# Decision Tree Implementation Notes ## Key Learnings from Implementation This document captures insights from implementing the decision tree render hook that go beyond the standard lessons. ## 1. Metadata Extraction Challenges **Problem**: Extracting metadata from nested YAML structures in Hugo templates is tricky because simple field matching returns nested occurrences. **Example**: When extracting `id:` from a decision tree YAML, you might get: - Top-level: `id: documents-tree` ✅ - Nested: `id: jsonOutcome` ❌ (from outcome objects) **Solution**: Use indentation detection to distinguish levels: ```html {{- if and (gt (len .) 0) (ne (index . 0) 32) (ne (index . 0) 9) -}} {{- /* Process only top-level lines (no leading space/tab) */ -}} {{- end -}} ``` **Key Insight**: Character codes matter - 32 = space, 9 = tab. This is more reliable than string operations. ## 2. String Manipulation in Hugo Templates **Problem**: `strings.TrimPrefix` doesn't always work as expected in Hugo templates. **Solution**: Use `strings.Replace` with a count parameter instead: ```html {{- $afterPrefix := strings.Replace $trimmed "id:" "" 1 -}} {{- $id = strings.TrimSpace $afterPrefix -}} ``` **Why**: `Replace` is more predictable and handles edge cases better than `TrimPrefix`. ## 3. Server-Side vs Client-Side Metadata **Key Principle**: Metadata for AI agents MUST be in static HTML, not JavaScript. **Why**: AI agents typically don't execute JavaScript. If metadata is only created by JavaScript, AI agents won't see it. **Implementation**: - Extract metadata in Hugo render hook - Embed as `