File size: 1,057 Bytes
2ce4ddf
 
 
 
 
 
 
 
 
 
 
 
 
 
1c484d0
 
 
 
 
2ce4ddf
 
1c484d0
 
 
 
 
2ce4ddf
 
 
1c484d0
2ce4ddf
 
 
 
 
 
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
import json
from pathlib import Path

import yaml


class StructuredDataAgent:
    SUPPORTED_EXTENSIONS = {".json", ".yaml", ".yml"}

    def process(self, file_path: Path) -> dict:
        raw = file_path.read_text(encoding="utf-8", errors="replace")
        ext = file_path.suffix.lower()

        if ext == ".json":
            try:
                pretty = json.dumps(json.loads(raw), indent=2)
            except json.JSONDecodeError:
                pretty = raw
            block = f"```json\n{pretty}\n```"
            file_type = "json"
        else:
            try:
                pretty = yaml.dump(yaml.safe_load(raw), default_flow_style=False)
            except yaml.YAMLError:
                pretty = raw
            block = f"```yaml\n{pretty}\n```"
            file_type = "yaml"

        return {
            "markdown": f"# {file_path.stem}\n\n{block}\n",
            "metadata": {
                "source_file": str(file_path),
                "file_type": file_type,
                "title": file_path.stem,
            },
        }