Documentation Index
Fetch the complete documentation index at: https://docs.magi-mda.org/llms.txt
Use this file to discover all available pages before exploring further.
MAGI Developer Guide: Encoding & Decoding
This guide provides examples of how to programmatically encode (create) and decode (parse) MAGI (Markdown for Agent Guidance & Instruction) documents in different environments. MAGI files typically use the.mda extension.
Core Concepts
- Encoding: Combining separate pieces of information (metadata from Front Matter, human-readable Markdown content, AI instructions within
ai-scriptblocks, and document relationships defined in Footnotes) into the MAGI string format (.mda). - Decoding: Parsing a MAGI string (from an
.mdafile or source) to extract its distinct structured components: YAML Front Matter, the main Markdown content,ai-scriptJSON blocks, and Footnote relationship JSON payloads.
- YAML Parsers: For handling the Front Matter (e.g.,
js-yamlorgray-matterin Node.js,PyYAMLorpython-frontmatterin Python). - Markdown Parsers: While standard Markdown parsers can render the core content, specialized logic or regular expressions are often needed to correctly identify and extract the
ai-scriptblocks and JSON-based footnotes without treating them as plain text or standard code/footnotes. Libraries likemarkedormarkdown-it(Node.js) andmarkdownormistune(Python) can be adapted. - JSON Parsers: Built-in functionality in most languages (
JSON.parse/JSON.stringifyin JS,jsonmodule in Python).
TypeScript / JavaScript Examples
Libraries needed:gray-matter (recommended for robust front matter parsing), marked (or similar, optional for final Markdown rendering).
Encoding MAGI
Python Examples
Libraries needed:python-frontmatter (handles YAML front matter effectively), PyYAML (usually a dependency of python-frontmatter), json (built-in).
Encoding MAGI
Decoding MAGI
Expected output structure is similar to the TypeScript example
Decoding MAGI via Command Line (Conceptual)
Directly and reliably decoding MAGI (.mda) files using only standard Unix command-line tools (grep, sed, awk) is complex due to the nested structures (YAML, JSON within Markdown).
A more robust command-line approach typically involves a dedicated script:
- Script Input: The script (e.g., Python, Node.js using the libraries above) accepts a
.mdafile path or piped input. - Parsing Logic: It uses libraries like
python-frontmatterorgray-matter, combined with regular expressions or custom parsing logic, to extract the Front Matter,ai-scriptblocks, footnote relationships, and main content. - Structured Output: The script outputs the extracted components in a machine-readable format, such as a single JSON object containing all parts, or separate files for each component (e.g.,
metadata.yaml,content.md,scripts.json,relationships.json).