File size: 2,312 Bytes
116524e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""ExportSkillbookMarkdownStep — writes the skillbook as a human-readable markdown file."""

from __future__ import annotations

from collections import defaultdict
from pathlib import Path

from ..core.context import ACEStepContext
from ..core.skillbook import Skillbook


class ExportSkillbookMarkdownStep:
    """Export the skillbook as a markdown file after each learning cycle.



    Rewrites the file from scratch on every invocation so the markdown

    always reflects the current state of the skillbook.

    """

    requires: frozenset[str] = frozenset({"skillbook"})
    provides: frozenset[str] = frozenset()

    def __init__(self, path: str | Path, skillbook: Skillbook) -> None:
        self.path = Path(path)
        self.skillbook = skillbook

    def __call__(self, ctx: ACEStepContext) -> ACEStepContext:
        skills = self.skillbook.skills()
        if not skills:
            return ctx

        by_section: dict[str, list] = defaultdict(list)
        for skill in skills:
            by_section[skill.section].append(skill)

        lines: list[str] = ["# ACE Skillbook", ""]

        for section in sorted(by_section):
            lines.append(f"## {section}")
            lines.append("")
            for skill in by_section[section]:
                tags = (
                    f"helpful={skill.helpful_count}, "
                    f"harmful={skill.harmful_count}, "
                    f"neutral={skill.neutral_count}"
                )
                lines.append(f"### `{skill.id}`")
                lines.append("")
                lines.append(f"**Keywords:** {', '.join(skill.keywords)}")
                lines.append("")
                lines.append(f"**Issue:** {skill.issue}")
                lines.append("")
                if skill.insight:
                    lines.append(f"**Insight:** {skill.insight}")
                    lines.append("")
                if skill.occurrences:
                    lines.append(f"**Occurrences:** {len(skill.occurrences)}")
                    lines.append("")
                lines.append(f"*Tags: {tags}*")
                lines.append("")

        self.path.parent.mkdir(parents=True, exist_ok=True)
        self.path.write_text("\n".join(lines))
        return ctx