-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_util.py
More file actions
56 lines (42 loc) · 1.53 KB
/
Copy path_util.py
File metadata and controls
56 lines (42 loc) · 1.53 KB
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
56
import json
from typing import Any, Literal
from context_compiler import (
POLICY_PROHIBIT,
POLICY_USE,
get_clarify_prompt,
get_decision_state,
get_policy_items,
get_premise_value,
is_clarify,
is_update,
)
def canonical_json(obj: Any) -> str:
return json.dumps(obj, sort_keys=True, separators=(",", ":"))
def print_json(obj: Any) -> None:
print(canonical_json(obj))
def _format_policy_values(state: Any, value: Literal["use", "prohibit"]) -> str:
items = get_policy_items(state, value)
return ", ".join(items) if items else "(none)"
def print_state_summary(state: Any, label: str = "state") -> None:
premise = get_premise_value(state)
premise_text = premise if premise is not None else "(none)"
print(f"{label}:")
print(f"- premise: {premise_text}")
print(f"- use policies: {_format_policy_values(state, POLICY_USE)}")
print(f"- prohibit policies: {_format_policy_values(state, POLICY_PROHIBIT)}")
def print_decision_summary(decision: Any) -> None:
if is_update(decision):
print("result: updated")
state = get_decision_state(decision)
assert isinstance(state, dict)
print_state_summary(state, "compiled state")
return
if is_clarify(decision):
print("result: clarify")
prompt = get_clarify_prompt(decision)
if isinstance(prompt, str) and prompt:
print("clarify prompt:")
for line in prompt.splitlines():
print(f"- {line}")
return
print("result: passthrough")