File size: 1,429 Bytes
0b6a889
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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