69 lines
2.1 KiB
Python
69 lines
2.1 KiB
Python
"""
|
|
Template management
|
|
"""
|
|
import json
|
|
from pathlib import Path
|
|
from typing import Optional
|
|
|
|
|
|
class TemplateManager:
|
|
def __init__(self, templates_dir: Path):
|
|
self.templates_dir = templates_dir
|
|
|
|
if not templates_dir.exists():
|
|
templates_dir.mkdir(parents=True)
|
|
self._create_default_template()
|
|
|
|
def _create_default_template(self):
|
|
"""Create default template"""
|
|
default_template = {
|
|
"title": "heur|orig",
|
|
"created": "heur|orig",
|
|
"changed": "heur|orig",
|
|
"authors": "heur|orig",
|
|
"version": "heur|orig",
|
|
"tags": "orig|ai default"
|
|
}
|
|
|
|
with open(self.templates_dir / 'default.json', 'w', encoding='utf-8') as f:
|
|
json.dump(default_template, f, indent=2)
|
|
|
|
def load_template(self, name: str) -> dict:
|
|
"""Load template by name"""
|
|
template_path = self.templates_dir / f'{name}.json'
|
|
|
|
if not template_path.exists():
|
|
# Fallback to default
|
|
template_path = self.templates_dir / 'default.json'
|
|
if not template_path.exists():
|
|
self._create_default_template()
|
|
|
|
with open(template_path, 'r', encoding='utf-8') as f:
|
|
return json.load(f)
|
|
|
|
def get_structured_output_schema(self, template: dict) -> dict:
|
|
"""Generate JSON schema for structured output"""
|
|
properties = {}
|
|
required = []
|
|
|
|
for field in template.keys():
|
|
if field == 'tags':
|
|
properties[field] = {
|
|
"type": "array",
|
|
"items": {"type": "string"},
|
|
"description": f"Field: {field}"
|
|
}
|
|
else:
|
|
properties[field] = {
|
|
"type": "string",
|
|
"description": f"Field: {field}"
|
|
}
|
|
required.append(field)
|
|
|
|
return {
|
|
"type": "object",
|
|
"properties": properties,
|
|
"required": required,
|
|
"additionalProperties": False
|
|
}
|