improvements added to rules engine
This commit is contained in:
+55
-10
@@ -1,9 +1,9 @@
|
||||
"""
|
||||
Template management
|
||||
Template management with type support
|
||||
"""
|
||||
import json
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
from typing import Optional, Tuple
|
||||
|
||||
|
||||
class TemplateManager:
|
||||
@@ -11,7 +11,7 @@ class TemplateManager:
|
||||
self.templates_dir = templates_dir
|
||||
|
||||
def load_template(self, name: str) -> dict:
|
||||
"""Load template by name"""
|
||||
"""Load template by name and parse field types"""
|
||||
template_path = self.templates_dir / f'{name}.json'
|
||||
|
||||
if not template_path.exists():
|
||||
@@ -19,25 +19,70 @@ class TemplateManager:
|
||||
template_path = self.templates_dir / 'default.json'
|
||||
|
||||
with open(template_path, 'r', encoding='utf-8') as f:
|
||||
return json.load(f)
|
||||
raw_template = json.load(f)
|
||||
|
||||
# Parse template to separate strategies and types
|
||||
template = {}
|
||||
for field, value in raw_template.items():
|
||||
template[field] = value
|
||||
|
||||
return template
|
||||
|
||||
def parse_field(self, field_value: str) -> Tuple[str, str]:
|
||||
"""
|
||||
Parse field value to extract strategy and type
|
||||
Format: "strategy|strategy:type" or just "strategy|strategy"
|
||||
|
||||
Returns: (strategy, field_type)
|
||||
"""
|
||||
# Check if type is specified with colon
|
||||
if ':' in field_value:
|
||||
parts = field_value.rsplit(':', 1)
|
||||
strategy = parts[0]
|
||||
field_type = parts[1].strip()
|
||||
else:
|
||||
strategy = field_value
|
||||
field_type = 'text' # Default type
|
||||
|
||||
return strategy, field_type
|
||||
|
||||
def get_structured_output_schema(self, template: dict) -> dict:
|
||||
"""Generate JSON schema for structured output"""
|
||||
"""Generate JSON schema for structured output based on field types"""
|
||||
properties = {}
|
||||
required = []
|
||||
|
||||
for field in template.keys():
|
||||
if field == 'tags':
|
||||
for field, field_value in template.items():
|
||||
strategy, field_type = self.parse_field(str(field_value))
|
||||
|
||||
# Map field type to JSON schema type
|
||||
if field_type == 'list':
|
||||
properties[field] = {
|
||||
"type": "array",
|
||||
"items": {"type": "string"},
|
||||
"description": f"Field: {field}"
|
||||
"description": f"Field: {field} (list of text)"
|
||||
}
|
||||
else:
|
||||
elif field_type == 'number':
|
||||
properties[field] = {
|
||||
"type": "number",
|
||||
"description": f"Field: {field} (number)"
|
||||
}
|
||||
elif field_type == 'checkbox':
|
||||
properties[field] = {
|
||||
"type": "boolean",
|
||||
"description": f"Field: {field} (true/false)"
|
||||
}
|
||||
elif field_type in ('date', 'datetime'):
|
||||
properties[field] = {
|
||||
"type": "string",
|
||||
"description": f"Field: {field}"
|
||||
"format": "date-time" if field_type == 'datetime' else "date",
|
||||
"description": f"Field: {field} ({field_type})"
|
||||
}
|
||||
else: # text or unspecified
|
||||
properties[field] = {
|
||||
"type": "string",
|
||||
"description": f"Field: {field} (text)"
|
||||
}
|
||||
|
||||
required.append(field)
|
||||
|
||||
return {
|
||||
|
||||
Reference in New Issue
Block a user