"""Style Loader — Load JSON style examples for few-shot prompting. Loads manual style examples from shared/project-docs/blog/style-examples/ """ import json import logging from pathlib import Path from typing import Dict, Any, Optional, List logger = logging.getLogger(__name__) # Path to style examples directory STYLE_EXAMPLES_DIR = Path("/home/hoffmann_admin/.openclaw/workspace/shared/project-docs/blog/style-examples") class StyleExample: """Represents a style example for few-shot prompting.""" def __init__(self, data: Dict[str, Any]): self.slug = data.get("slug", "") self.title = data.get("title", "") self.excerpt = data.get("excerpt", "") self.structure = data.get("structure", {}) self.voice_markers = data.get("voice_markers", {}) def to_prompt_context(self) -> str: """Convert to prompt-friendly context.""" sections = [ f"Example: {self.title}", f"Excerpt: {self.excerpt}", "", "Structure breakdown:", ] if self.structure: for key, value in self.structure.items(): if isinstance(value, dict) and "content" in value: sections.append(f" {key}: {value['content'][:100]}...") else: sections.append(f" {key}: {str(value)[:100]}") if self.voice_markers: sections.append("") sections.append("Voice markers:") for key, value in self.voice_markers.items(): if isinstance(value, list): sections.append(f" {key}: {', '.join(str(v) for v in value[:3])}") else: sections.append(f" {key}: {value}") return "\n".join(sections) class StyleLoader: """Loads and caches style examples.""" def __init__(self): self.examples: Dict[str, StyleExample] = {} self._load_examples() def _load_examples(self): """Load all JSON examples from style-examples directory.""" if not STYLE_EXAMPLES_DIR.exists(): logger.warning(f"Style examples directory not found: {STYLE_EXAMPLES_DIR}") return for json_file in STYLE_EXAMPLES_DIR.glob("*.json"): try: with open(json_file, 'r') as f: data = json.load(f) example = StyleExample(data) self.examples[example.slug] = example logger.info(f"Loaded style example: {example.slug}") except Exception as e: logger.error(f"Failed to load style example {json_file}: {e}") def get(self, slug: str) -> Optional[StyleExample]: """Get a style example by slug.""" return self.examples.get(slug) def list_available(self) -> List[Dict[str, str]]: """List available style examples.""" return [ {"slug": slug, "title": ex.title} for slug, ex in self.examples.items() ] def get_prompt_context(self, slug: str) -> str: """Get prompt context for a style example.""" example = self.get(slug) if example: return example.to_prompt_context() return "" # Global loader instance style_loader = StyleLoader() def get_style_example(slug: str) -> Optional[StyleExample]: """Convenience function to get a style example.""" return style_loader.get(slug) def list_style_examples() -> List[Dict[str, str]]: """Convenience function to list available examples.""" return style_loader.list_available() def get_style_prompt_context(slug: str) -> str: """Get style example as prompt context.""" return style_loader.get_prompt_context(slug)