import json import random from typing import Any, Dict, List, Optional import numpy as np def set_seed(seed: int) -> None: random.seed(seed) np.random.seed(seed) def format_conversation(history: List[Dict], last_n: int = 5) -> str: lines = [] for msg in history[-last_n:]: role = msg.get("role", "unknown").upper() content = msg.get("content", msg.get("message", "")) action = msg.get("action", "") if action: lines.append(f"[{role}] ({action}) {content}") else: lines.append(f"[{role}] {content}") return "\n".join(lines) def validate_json(data: Any, schema: Dict) -> bool: if not isinstance(data, dict): return False for key, expected_type in schema.items(): if key not in data: return False if expected_type is not None and not isinstance(data[key], expected_type): return False return True POLICY_SCHEMA = { "version": str, "policies": dict, } def validate_policy_file(data: Dict) -> bool: return validate_json(data, POLICY_SCHEMA) def deep_merge(base: Dict, overlay: Dict) -> Dict: result = base.copy() for key, value in overlay.items(): if key in result and isinstance(result[key], dict) and isinstance(value, dict): result[key] = deep_merge(result[key], value) else: result[key] = value return result