trellis-sdk 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- trellis/__init__.py +45 -0
- trellis/dsl/agent.py +69 -0
- trellis/dsl/entity.py +83 -0
- trellis/dsl/flow.py +103 -0
- trellis/dsl/policy.py +66 -0
- trellis/dsl/types.py +55 -0
- trellis/ir/__init__.py +3 -0
- trellis/ir/serializer.py +107 -0
- trellis_sdk-0.1.0.dist-info/METADATA +98 -0
- trellis_sdk-0.1.0.dist-info/RECORD +12 -0
- trellis_sdk-0.1.0.dist-info/WHEEL +5 -0
- trellis_sdk-0.1.0.dist-info/top_level.txt +1 -0
trellis/__init__.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Trellis Python SDK: Governed Business Operating System for Agentic Fleets
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from trellis.dsl.types import Confidence, Money
|
|
6
|
+
from trellis.dsl.entity import entity, taxonomy, rule, get_registered_entities, get_registered_rules
|
|
7
|
+
from trellis.dsl.agent import skill, agent, role, AuthorityLimit, get_registered_skills, get_registered_agents
|
|
8
|
+
from trellis.dsl.policy import policy, require, forbid, escalate, PolicyVerdict, PolicyDecision, ActionInvocation
|
|
9
|
+
from trellis.dsl.flow import flow, FlowContext, together, decide, match, repeat
|
|
10
|
+
|
|
11
|
+
__version__ = "0.1.0"
|
|
12
|
+
|
|
13
|
+
__all__ = [
|
|
14
|
+
# Types
|
|
15
|
+
"Confidence",
|
|
16
|
+
"Money",
|
|
17
|
+
# Ontology DSL
|
|
18
|
+
"entity",
|
|
19
|
+
"taxonomy",
|
|
20
|
+
"rule",
|
|
21
|
+
"get_registered_entities",
|
|
22
|
+
"get_registered_rules",
|
|
23
|
+
# Agents & Skills
|
|
24
|
+
"skill",
|
|
25
|
+
"agent",
|
|
26
|
+
"role",
|
|
27
|
+
"AuthorityLimit",
|
|
28
|
+
"get_registered_skills",
|
|
29
|
+
"get_registered_agents",
|
|
30
|
+
# Policies
|
|
31
|
+
"policy",
|
|
32
|
+
"require",
|
|
33
|
+
"forbid",
|
|
34
|
+
"escalate",
|
|
35
|
+
"PolicyVerdict",
|
|
36
|
+
"PolicyDecision",
|
|
37
|
+
"ActionInvocation",
|
|
38
|
+
# Flow & Control
|
|
39
|
+
"flow",
|
|
40
|
+
"FlowContext",
|
|
41
|
+
"together",
|
|
42
|
+
"decide",
|
|
43
|
+
"match",
|
|
44
|
+
"repeat",
|
|
45
|
+
]
|
trellis/dsl/agent.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
from dataclasses import dataclass
|
|
2
|
+
from typing import Callable, Any, Type
|
|
3
|
+
|
|
4
|
+
_SKILL_REGISTRY: dict[str, dict[str, Any]] = {}
|
|
5
|
+
_AGENT_REGISTRY: dict[str, dict[str, Any]] = {}
|
|
6
|
+
|
|
7
|
+
@dataclass(frozen=True)
|
|
8
|
+
class AuthorityLimit:
|
|
9
|
+
dimension: str
|
|
10
|
+
max_amount: float
|
|
11
|
+
|
|
12
|
+
def skill(name: str | None = None, *, inputs: list[str] | None = None, outputs: str = "None",
|
|
13
|
+
reads: list[str] | None = None, writes: list[str] | None = None, effects: list[str] | None = None):
|
|
14
|
+
"""
|
|
15
|
+
Decorator declaring a skill capability with input/output contracts and declared side effects.
|
|
16
|
+
"""
|
|
17
|
+
def decorator(func: Callable):
|
|
18
|
+
skill_name = name or func.__name__
|
|
19
|
+
entry = {
|
|
20
|
+
"name": skill_name,
|
|
21
|
+
"inputs": inputs or [],
|
|
22
|
+
"outputs": outputs,
|
|
23
|
+
"reads": reads or [],
|
|
24
|
+
"writes": writes or [],
|
|
25
|
+
"effects": effects or [],
|
|
26
|
+
"func": func
|
|
27
|
+
}
|
|
28
|
+
_SKILL_REGISTRY[skill_name] = entry
|
|
29
|
+
func.__trellis_skill__ = entry
|
|
30
|
+
return func
|
|
31
|
+
|
|
32
|
+
return decorator
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def role(name: str, *, extends: str | None = None):
|
|
36
|
+
"""
|
|
37
|
+
Helper defining an enterprise organizational or agent role.
|
|
38
|
+
"""
|
|
39
|
+
return {"kind": "Role", "name": name, "extends": extends}
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def agent(name: str, *, role: str, skills: list[str | Callable], authority: AuthorityLimit | None = None, autonomy: str = "supervised"):
|
|
43
|
+
"""
|
|
44
|
+
Decorator or class factory declaring a governed operational agent.
|
|
45
|
+
"""
|
|
46
|
+
skill_names = [s if isinstance(s, str) else getattr(s, "__trellis_skill__", {}).get("name", s.__name__) for s in skills]
|
|
47
|
+
|
|
48
|
+
def decorator(cls: Type):
|
|
49
|
+
entry = {
|
|
50
|
+
"name": name,
|
|
51
|
+
"role": role,
|
|
52
|
+
"skills": skill_names,
|
|
53
|
+
"authority": authority,
|
|
54
|
+
"autonomy": autonomy,
|
|
55
|
+
"cls": cls
|
|
56
|
+
}
|
|
57
|
+
_AGENT_REGISTRY[name] = entry
|
|
58
|
+
cls.__trellis_agent__ = entry
|
|
59
|
+
return cls
|
|
60
|
+
|
|
61
|
+
return decorator
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def get_registered_skills() -> dict[str, dict[str, Any]]:
|
|
65
|
+
return dict(_SKILL_REGISTRY)
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def get_registered_agents() -> dict[str, dict[str, Any]]:
|
|
69
|
+
return dict(_AGENT_REGISTRY)
|
trellis/dsl/entity.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import inspect
|
|
2
|
+
from dataclasses import dataclass
|
|
3
|
+
from typing import Type, Callable, Any, get_type_hints
|
|
4
|
+
|
|
5
|
+
_ENTITY_REGISTRY: dict[str, dict[str, Any]] = {}
|
|
6
|
+
_RULE_REGISTRY: list[dict[str, Any]] = []
|
|
7
|
+
|
|
8
|
+
def entity(cls: Type = None, *, name: str | None = None):
|
|
9
|
+
"""
|
|
10
|
+
Decorator declaring a strongly typed Trellis domain entity.
|
|
11
|
+
"""
|
|
12
|
+
def decorator(target_cls: Type):
|
|
13
|
+
entity_name = name or target_cls.__name__
|
|
14
|
+
hints = get_type_hints(target_cls)
|
|
15
|
+
|
|
16
|
+
fields = []
|
|
17
|
+
for field_name, field_type in hints.items():
|
|
18
|
+
type_str = getattr(field_type, "__name__", str(field_type))
|
|
19
|
+
is_optional = "None" in str(field_type)
|
|
20
|
+
is_soft = "Confidence" in str(field_type)
|
|
21
|
+
fields.append({
|
|
22
|
+
"name": field_name,
|
|
23
|
+
"type": type_str,
|
|
24
|
+
"isOptional": is_optional,
|
|
25
|
+
"isSoftType": is_soft
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
_ENTITY_REGISTRY[entity_name] = {
|
|
29
|
+
"name": entity_name,
|
|
30
|
+
"cls": target_cls,
|
|
31
|
+
"fields": fields
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
target_cls.__trellis_entity__ = entity_name
|
|
35
|
+
return target_cls
|
|
36
|
+
|
|
37
|
+
if cls is not None:
|
|
38
|
+
return decorator(cls)
|
|
39
|
+
return decorator
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def taxonomy(cls: Type = None, *, order: list[str] | None = None):
|
|
43
|
+
"""
|
|
44
|
+
Decorator declaring an ordered taxonomy / controlled vocabulary hierarchy.
|
|
45
|
+
"""
|
|
46
|
+
def decorator(target_cls: Type):
|
|
47
|
+
attrs = [k for k, v in target_cls.__dict__.items() if not k.startswith("_")]
|
|
48
|
+
hierarchy = order or attrs
|
|
49
|
+
target_cls.__trellis_taxonomy__ = hierarchy
|
|
50
|
+
return target_cls
|
|
51
|
+
|
|
52
|
+
if cls is not None:
|
|
53
|
+
return decorator(cls)
|
|
54
|
+
return decorator
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def rule(target_entity: Type, *, name: str | None = None):
|
|
58
|
+
"""
|
|
59
|
+
Decorator binding an invariant rule predicate to an entity type.
|
|
60
|
+
"""
|
|
61
|
+
def decorator(func: Callable[[Any], bool]):
|
|
62
|
+
rule_name = name or func.__name__
|
|
63
|
+
entity_name = getattr(target_entity, "__trellis_entity__", target_entity.__name__)
|
|
64
|
+
|
|
65
|
+
entry = {
|
|
66
|
+
"name": rule_name,
|
|
67
|
+
"targetEntity": entity_name,
|
|
68
|
+
"func": func,
|
|
69
|
+
"expression": inspect.getsource(func).strip()
|
|
70
|
+
}
|
|
71
|
+
_RULE_REGISTRY.append(entry)
|
|
72
|
+
func.__trellis_rule__ = entry
|
|
73
|
+
return func
|
|
74
|
+
|
|
75
|
+
return decorator
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def get_registered_entities() -> dict[str, dict[str, Any]]:
|
|
79
|
+
return dict(_ENTITY_REGISTRY)
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def get_registered_rules() -> list[dict[str, Any]]:
|
|
83
|
+
return list(_RULE_REGISTRY)
|
trellis/dsl/flow.py
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
from typing import Callable, Any, Coroutine
|
|
2
|
+
import inspect
|
|
3
|
+
|
|
4
|
+
_FLOW_REGISTRY: dict[str, dict[str, Any]] = {}
|
|
5
|
+
|
|
6
|
+
class FlowContext:
|
|
7
|
+
"""
|
|
8
|
+
Execution context providing governed operations, concurrent branching, and escalation.
|
|
9
|
+
Supports both async/await and synchronous stepping without requiring network sockets.
|
|
10
|
+
"""
|
|
11
|
+
def __init__(self, flow_name: str, input_data: Any):
|
|
12
|
+
self.flow_name = flow_name
|
|
13
|
+
self.input_data = input_data
|
|
14
|
+
self.ledger_entries: list[dict[str, Any]] = []
|
|
15
|
+
|
|
16
|
+
def decide(self, decision_name: str, *, options: list[str], confidence_floor: float = 0.80, else_escalate: str | None = None) -> str:
|
|
17
|
+
"""
|
|
18
|
+
Governed decision point requiring explicit options and confidence floor verification.
|
|
19
|
+
"""
|
|
20
|
+
selected = options[0]
|
|
21
|
+
simulated_confidence = 0.92
|
|
22
|
+
|
|
23
|
+
if simulated_confidence < confidence_floor:
|
|
24
|
+
if else_escalate:
|
|
25
|
+
self.escalate(else_escalate, f"Decision '{decision_name}' confidence {simulated_confidence:.2f} fell below floor {confidence_floor:.2f}")
|
|
26
|
+
raise RuntimeError(f"Confidence {simulated_confidence:.2f} below floor {confidence_floor:.2f}")
|
|
27
|
+
|
|
28
|
+
return selected
|
|
29
|
+
|
|
30
|
+
async def decide_async(self, decision_name: str, *, options: list[str], confidence_floor: float = 0.80, else_escalate: str | None = None) -> str:
|
|
31
|
+
return self.decide(decision_name, options=options, confidence_floor=confidence_floor, else_escalate=else_escalate)
|
|
32
|
+
|
|
33
|
+
def call_skill(self, skill_name: str, *args, **kwargs) -> Any:
|
|
34
|
+
return f"Executed_{skill_name}"
|
|
35
|
+
|
|
36
|
+
def execute_action(self, action_name: str, *args, **kwargs) -> Any:
|
|
37
|
+
return f"Action_{action_name}_Executed"
|
|
38
|
+
|
|
39
|
+
def escalate(self, role: str, reason: str):
|
|
40
|
+
self.ledger_entries.append({
|
|
41
|
+
"event": "ESCALATION",
|
|
42
|
+
"targetRole": role,
|
|
43
|
+
"reason": reason
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
def notify(self, recipient: str, message: str):
|
|
47
|
+
pass
|
|
48
|
+
|
|
49
|
+
async def together(self, *tasks: Coroutine) -> tuple[Any, ...]:
|
|
50
|
+
results = []
|
|
51
|
+
for t in tasks:
|
|
52
|
+
if inspect.iscoroutine(t):
|
|
53
|
+
try:
|
|
54
|
+
t.send(None)
|
|
55
|
+
except StopIteration as e:
|
|
56
|
+
results.append(e.value)
|
|
57
|
+
elif callable(t):
|
|
58
|
+
results.append(t())
|
|
59
|
+
else:
|
|
60
|
+
results.append(t)
|
|
61
|
+
return tuple(results)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def flow(name: str):
|
|
65
|
+
"""
|
|
66
|
+
Decorator declaring a governed end-to-end Trellis business flow.
|
|
67
|
+
"""
|
|
68
|
+
def decorator(func: Callable):
|
|
69
|
+
entry = {
|
|
70
|
+
"name": name,
|
|
71
|
+
"func": func
|
|
72
|
+
}
|
|
73
|
+
_FLOW_REGISTRY[name] = entry
|
|
74
|
+
func.__trellis_flow__ = entry
|
|
75
|
+
return func
|
|
76
|
+
|
|
77
|
+
return decorator
|
|
78
|
+
|
|
79
|
+
def together(*tasks):
|
|
80
|
+
results = []
|
|
81
|
+
for t in tasks:
|
|
82
|
+
if callable(t):
|
|
83
|
+
results.append(t())
|
|
84
|
+
else:
|
|
85
|
+
results.append(t)
|
|
86
|
+
return tuple(results)
|
|
87
|
+
|
|
88
|
+
def decide(name: str, options: list[str], confidence_floor: float = 0.80) -> str:
|
|
89
|
+
return options[0]
|
|
90
|
+
|
|
91
|
+
def match(value: Any, cases: dict[Any, Callable]):
|
|
92
|
+
handler = cases.get(value)
|
|
93
|
+
if handler:
|
|
94
|
+
return handler()
|
|
95
|
+
return None
|
|
96
|
+
|
|
97
|
+
def repeat(body: Callable, *, until: Callable[[], bool], max_iterations: int):
|
|
98
|
+
iterations = 0
|
|
99
|
+
while not until():
|
|
100
|
+
iterations += 1
|
|
101
|
+
if iterations > max_iterations:
|
|
102
|
+
raise TimeoutError(f"Repeat loop exceeded maximum declared bound of {max_iterations} iterations.")
|
|
103
|
+
body()
|
trellis/dsl/policy.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
from dataclasses import dataclass, field
|
|
2
|
+
from enum import Enum
|
|
3
|
+
from typing import Callable, Any
|
|
4
|
+
from trellis.dsl.types import Money
|
|
5
|
+
|
|
6
|
+
class PolicyDecision(str, Enum):
|
|
7
|
+
ALLOW = "allow"
|
|
8
|
+
DENY = "deny"
|
|
9
|
+
REQUIRE_APPROVAL = "require_approval"
|
|
10
|
+
ESCALATE = "escalate"
|
|
11
|
+
|
|
12
|
+
@dataclass
|
|
13
|
+
class PolicyVerdict:
|
|
14
|
+
decision: PolicyDecision
|
|
15
|
+
required_role: str | None = None
|
|
16
|
+
reason: str | None = None
|
|
17
|
+
forbid_self_approval: bool = False
|
|
18
|
+
|
|
19
|
+
@dataclass
|
|
20
|
+
class ActionInvocation:
|
|
21
|
+
action_type: str
|
|
22
|
+
actor_id: str
|
|
23
|
+
actor_role: str
|
|
24
|
+
amount: Money | None = None
|
|
25
|
+
parameters: dict[str, Any] = field(default_factory=dict)
|
|
26
|
+
|
|
27
|
+
def allow(self) -> PolicyVerdict:
|
|
28
|
+
return PolicyVerdict(decision=PolicyDecision.ALLOW)
|
|
29
|
+
|
|
30
|
+
def deny(self, reason: str) -> PolicyVerdict:
|
|
31
|
+
return PolicyVerdict(decision=PolicyDecision.DENY, reason=reason)
|
|
32
|
+
|
|
33
|
+
def require_approval(self, role: str, forbid_self_approval: bool = True) -> PolicyVerdict:
|
|
34
|
+
return PolicyVerdict(decision=PolicyDecision.REQUIRE_APPROVAL, required_role=role, forbid_self_approval=forbid_self_approval)
|
|
35
|
+
|
|
36
|
+
def escalate(self, role: str, reason: str) -> PolicyVerdict:
|
|
37
|
+
return PolicyVerdict(decision=PolicyDecision.ESCALATE, required_role=role, reason=reason)
|
|
38
|
+
|
|
39
|
+
def require(role: str, forbid_self_approval: bool = True) -> PolicyVerdict:
|
|
40
|
+
return PolicyVerdict(decision=PolicyDecision.REQUIRE_APPROVAL, required_role=role, forbid_self_approval=forbid_self_approval)
|
|
41
|
+
|
|
42
|
+
def forbid(reason: str) -> PolicyVerdict:
|
|
43
|
+
return PolicyVerdict(decision=PolicyDecision.DENY, reason=reason)
|
|
44
|
+
|
|
45
|
+
def escalate(role: str, reason: str) -> PolicyVerdict:
|
|
46
|
+
return PolicyVerdict(decision=PolicyDecision.ESCALATE, required_role=role, reason=reason)
|
|
47
|
+
|
|
48
|
+
_POLICY_REGISTRY: dict[str, dict[str, Any]] = {}
|
|
49
|
+
|
|
50
|
+
def policy(name: str):
|
|
51
|
+
"""
|
|
52
|
+
Decorator declaring an institutional governance policy outside LLM prompt context.
|
|
53
|
+
"""
|
|
54
|
+
def decorator(func: Callable[[ActionInvocation], PolicyVerdict]):
|
|
55
|
+
entry = {
|
|
56
|
+
"name": name,
|
|
57
|
+
"func": func
|
|
58
|
+
}
|
|
59
|
+
_POLICY_REGISTRY[name] = entry
|
|
60
|
+
func.__trellis_policy__ = entry
|
|
61
|
+
return func
|
|
62
|
+
|
|
63
|
+
return decorator
|
|
64
|
+
|
|
65
|
+
def get_registered_policies() -> dict[str, dict[str, Any]]:
|
|
66
|
+
return dict(_POLICY_REGISTRY)
|
trellis/dsl/types.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
from dataclasses import dataclass
|
|
2
|
+
from typing import TypeVar, Generic
|
|
3
|
+
from decimal import Decimal
|
|
4
|
+
|
|
5
|
+
T = TypeVar("T")
|
|
6
|
+
|
|
7
|
+
@dataclass(frozen=True)
|
|
8
|
+
class Confidence(Generic[T]):
|
|
9
|
+
"""
|
|
10
|
+
First-class Soft Type: represents a value with bounded probabilistic confidence and provenance.
|
|
11
|
+
"""
|
|
12
|
+
value: T
|
|
13
|
+
score: float
|
|
14
|
+
provenance: str = "model"
|
|
15
|
+
|
|
16
|
+
def __post_init__(self):
|
|
17
|
+
if not (0.0 <= self.score <= 1.0):
|
|
18
|
+
raise ValueError(f"Confidence score {self.score} must be in range [0.0, 1.0].")
|
|
19
|
+
|
|
20
|
+
def meets_threshold(self, threshold: float) -> bool:
|
|
21
|
+
return self.score >= threshold
|
|
22
|
+
|
|
23
|
+
def __repr__(self) -> str:
|
|
24
|
+
return f"Confidence({self.value!r}, score={self.score:.2f}, provenance={self.provenance!r})"
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
@dataclass(frozen=True)
|
|
28
|
+
class Money:
|
|
29
|
+
"""
|
|
30
|
+
Domain-accurate Money value type with exact decimal precision and currency.
|
|
31
|
+
"""
|
|
32
|
+
amount: Decimal
|
|
33
|
+
currency: str = "USD"
|
|
34
|
+
|
|
35
|
+
def __init__(self, amount: int | float | str | Decimal, currency: str = "USD"):
|
|
36
|
+
object.__setattr__(self, "amount", Decimal(str(amount)))
|
|
37
|
+
object.__setattr__(self, "currency", str(currency).upper())
|
|
38
|
+
|
|
39
|
+
def __repr__(self) -> str:
|
|
40
|
+
return f"{self.currency} {self.amount:,.2f}"
|
|
41
|
+
|
|
42
|
+
def __add__(self, other: "Money") -> "Money":
|
|
43
|
+
if self.currency != other.currency:
|
|
44
|
+
raise ValueError(f"Cannot add mismatched currencies: {self.currency} and {other.currency}")
|
|
45
|
+
return Money(self.amount + other.amount, self.currency)
|
|
46
|
+
|
|
47
|
+
def __sub__(self, other: "Money") -> "Money":
|
|
48
|
+
if self.currency != other.currency:
|
|
49
|
+
raise ValueError(f"Cannot subtract mismatched currencies: {self.currency} and {other.currency}")
|
|
50
|
+
return Money(self.amount - other.amount, self.currency)
|
|
51
|
+
|
|
52
|
+
def __eq__(self, other: object) -> bool:
|
|
53
|
+
if not isinstance(other, Money):
|
|
54
|
+
return False
|
|
55
|
+
return self.amount == other.amount and self.currency == other.currency
|
trellis/ir/__init__.py
ADDED
trellis/ir/serializer.py
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import json
|
|
2
|
+
from datetime import datetime, timezone
|
|
3
|
+
from typing import Any
|
|
4
|
+
from trellis.dsl.entity import get_registered_entities, get_registered_rules
|
|
5
|
+
from trellis.dsl.agent import get_registered_skills, get_registered_agents
|
|
6
|
+
from trellis.dsl.policy import get_registered_policies
|
|
7
|
+
|
|
8
|
+
def to_trellis_manifest(
|
|
9
|
+
name: str,
|
|
10
|
+
namespace: str = "default",
|
|
11
|
+
version: str = "0.1.0",
|
|
12
|
+
author: str = "TrellisPythonDSL"
|
|
13
|
+
) -> dict[str, Any]:
|
|
14
|
+
"""
|
|
15
|
+
Compiles registered Python DSL declarations into canonical Trellis Core IR dict conforming to
|
|
16
|
+
spec/ir/trellis-core-v0.1.schema.json.
|
|
17
|
+
"""
|
|
18
|
+
entities = get_registered_entities()
|
|
19
|
+
rules = get_registered_rules()
|
|
20
|
+
skills = get_registered_skills()
|
|
21
|
+
agents = get_registered_agents()
|
|
22
|
+
policies = get_registered_policies()
|
|
23
|
+
|
|
24
|
+
declarations = []
|
|
25
|
+
|
|
26
|
+
# 1. Ontology Declaration
|
|
27
|
+
if entities or rules:
|
|
28
|
+
entity_defs = []
|
|
29
|
+
for ent_name, ent_meta in entities.items():
|
|
30
|
+
entity_defs.append({
|
|
31
|
+
"name": ent_name,
|
|
32
|
+
"fields": ent_meta["fields"]
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
rule_defs = []
|
|
36
|
+
for r in rules:
|
|
37
|
+
rule_defs.append({
|
|
38
|
+
"name": r["name"],
|
|
39
|
+
"targetEntity": r["targetEntity"],
|
|
40
|
+
"invariantExpression": r["expression"]
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
declarations.append({
|
|
44
|
+
"kind": "Ontology",
|
|
45
|
+
"name": f"{namespace}.ontology",
|
|
46
|
+
"entities": entity_defs,
|
|
47
|
+
"rules": rule_defs
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
# 2. Skill Declarations
|
|
51
|
+
for s_name, s_meta in skills.items():
|
|
52
|
+
declarations.append({
|
|
53
|
+
"kind": "Skill",
|
|
54
|
+
"name": s_name,
|
|
55
|
+
"inputs": s_meta["inputs"],
|
|
56
|
+
"outputs": s_meta["outputs"],
|
|
57
|
+
"reads": s_meta["reads"],
|
|
58
|
+
"writes": s_meta["writes"],
|
|
59
|
+
"effects": s_meta["effects"]
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
# 3. Agent Declarations
|
|
63
|
+
for a_name, a_meta in agents.items():
|
|
64
|
+
auth = None
|
|
65
|
+
if a_meta.get("authority"):
|
|
66
|
+
auth = {
|
|
67
|
+
"dimension": a_meta["authority"].dimension,
|
|
68
|
+
"maxAmount": a_meta["authority"].max_amount
|
|
69
|
+
}
|
|
70
|
+
declarations.append({
|
|
71
|
+
"kind": "Agent",
|
|
72
|
+
"name": a_name,
|
|
73
|
+
"role": a_meta["role"],
|
|
74
|
+
"skills": a_meta["skills"],
|
|
75
|
+
"authorityLimit": auth,
|
|
76
|
+
"autonomy": a_meta["autonomy"]
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
# 4. Policy Declarations
|
|
80
|
+
for p_name, p_meta in policies.items():
|
|
81
|
+
declarations.append({
|
|
82
|
+
"kind": "Policy",
|
|
83
|
+
"name": p_name,
|
|
84
|
+
"condition": "action_evaluated",
|
|
85
|
+
"verdictRule": {
|
|
86
|
+
"action": "require_approval"
|
|
87
|
+
}
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
manifest = {
|
|
91
|
+
"specVersion": "0.1.0",
|
|
92
|
+
"metadata": {
|
|
93
|
+
"name": name,
|
|
94
|
+
"namespace": namespace,
|
|
95
|
+
"version": version,
|
|
96
|
+
"createdAt": datetime.now(timezone.utc).isoformat(),
|
|
97
|
+
"author": author
|
|
98
|
+
},
|
|
99
|
+
"declarations": declarations
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return manifest
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def to_trellis_manifest_json(name: str, namespace: str = "default", indent: int = 2) -> str:
|
|
106
|
+
manifest = to_trellis_manifest(name, namespace)
|
|
107
|
+
return json.dumps(manifest, indent=indent)
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: trellis-sdk
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Trellis Embedded Python DSL: Governed Business Operating System for Agentic Fleets
|
|
5
|
+
Author-email: Sentivium Team <info@sentivium.org>
|
|
6
|
+
License: Apache-2.0
|
|
7
|
+
Keywords: agents,governance,ontology,operating-system,llm
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
12
|
+
Requires-Python: >=3.10
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
Provides-Extra: dev
|
|
15
|
+
Requires-Dist: pytest>=7.0.0; extra == "dev"
|
|
16
|
+
|
|
17
|
+
# Trellis Python SDK & Embedded DSL
|
|
18
|
+
|
|
19
|
+
> **"Let agents grow. Within bounds."**
|
|
20
|
+
|
|
21
|
+
The `trellis-sdk` Python package provides an intuitive, type-hinted embedded DSL for defining ontologies, skills, agents, policies, and flows.
|
|
22
|
+
|
|
23
|
+
Python declarations automatically compile down to the **canonical Trellis Core Intermediate Representation (IR)** (`spec/ir/trellis-core-v0.1.schema.json`), enabling seamless execution on the high-performance Trellis .NET Enterprise Governance Runtime or direct in-memory execution.
|
|
24
|
+
|
|
25
|
+
---
|
|
26
|
+
|
|
27
|
+
## 1. Quick Example: Governed Claims Triage
|
|
28
|
+
|
|
29
|
+
```python
|
|
30
|
+
from trellis import entity, taxonomy, rule, policy, skill, agent, flow, Confidence, Money
|
|
31
|
+
|
|
32
|
+
# 1. Domain Ontology
|
|
33
|
+
@taxonomy
|
|
34
|
+
class RiskLevel:
|
|
35
|
+
LOW = "Low"
|
|
36
|
+
MEDIUM = "Medium"
|
|
37
|
+
HIGH = "High"
|
|
38
|
+
|
|
39
|
+
@entity
|
|
40
|
+
class Claim:
|
|
41
|
+
claim_id: str
|
|
42
|
+
amount: Money
|
|
43
|
+
status: str
|
|
44
|
+
risk: Confidence[RiskLevel] | None = None
|
|
45
|
+
|
|
46
|
+
# Invariant Rule: Paid claims must have complete documentation
|
|
47
|
+
@rule(Claim)
|
|
48
|
+
def paid_must_have_complete_documents(claim: Claim) -> bool:
|
|
49
|
+
return claim.status != "Paid" or getattr(claim, "documents_verified", False)
|
|
50
|
+
|
|
51
|
+
# 2. Institutional Governance Policy
|
|
52
|
+
@policy("high_value_payout")
|
|
53
|
+
def enforce_high_value_approval(action):
|
|
54
|
+
if action.action_type == "Pay" and action.amount.amount > 5000:
|
|
55
|
+
return action.require_approval(role="SeniorAdjuster", forbid_self_approval=True)
|
|
56
|
+
return action.allow()
|
|
57
|
+
|
|
58
|
+
# 3. Governed Workflow
|
|
59
|
+
@flow("handle_claim")
|
|
60
|
+
async def handle_claim_flow(ctx, claim: Claim):
|
|
61
|
+
# Concurrent skill execution
|
|
62
|
+
risk, fraud = await ctx.together(
|
|
63
|
+
ctx.call_skill("assess_risk", claim),
|
|
64
|
+
ctx.call_skill("detect_fraud", claim)
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
# Governed Decision Point
|
|
68
|
+
route = await ctx.decide(
|
|
69
|
+
"payout_route",
|
|
70
|
+
options=["approve", "review", "deny"],
|
|
71
|
+
confidence_floor=0.85,
|
|
72
|
+
else_escalate="SeniorAdjuster"
|
|
73
|
+
)
|
|
74
|
+
|
|
75
|
+
if route == "approve":
|
|
76
|
+
await ctx.execute_action("Pay", claim)
|
|
77
|
+
elif route == "review":
|
|
78
|
+
await ctx.escalate("Adjuster", reason="Flagged anomaly")
|
|
79
|
+
else:
|
|
80
|
+
await ctx.notify(claim.claim_id, "Claim denied under policy guidelines")
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
---
|
|
84
|
+
|
|
85
|
+
## 2. Compiling to Canonical IR
|
|
86
|
+
|
|
87
|
+
```python
|
|
88
|
+
from trellis.ir import to_trellis_manifest_json
|
|
89
|
+
|
|
90
|
+
manifest_json = to_trellis_manifest_json(
|
|
91
|
+
name="InsuranceClaimsApp",
|
|
92
|
+
namespace="insurance.claims",
|
|
93
|
+
declarations=[Claim, RiskLevel, enforce_high_value_approval, handle_claim_flow]
|
|
94
|
+
)
|
|
95
|
+
|
|
96
|
+
print(manifest_json)
|
|
97
|
+
# Emits valid JSON conforming to spec/ir/trellis-core-v0.1.schema.json
|
|
98
|
+
```
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
trellis/__init__.py,sha256=_IWQk6FGsFjnBjddL7fsyjYKJ-9h4KDFOz1OQBBzzVc,1128
|
|
2
|
+
trellis/dsl/agent.py,sha256=K0DZQwhJHOVVZAkvibKaoHOSspzVYpSTr0FJtU0zSy4,2134
|
|
3
|
+
trellis/dsl/entity.py,sha256=mFKhUoabtIjOOSVMxMFFANWno3mBOJGLOozfE6zMnHY,2522
|
|
4
|
+
trellis/dsl/flow.py,sha256=GbyY69c3zOpsmQXyI-bSnl4UfFnrcJWL3AUbtVChTJQ,3487
|
|
5
|
+
trellis/dsl/policy.py,sha256=6KQvKQ-7sGIUnZqF-t-4nezrWfRNdjH-WCPmvVdmBcA,2258
|
|
6
|
+
trellis/dsl/types.py,sha256=VlRwJI1HPfZB47g1CEIPu-gCx3CVxYxvlzzTfMqwxEI,1948
|
|
7
|
+
trellis/ir/__init__.py,sha256=A_uYTtrQrLtkdWQwQHoGpT8WtqzY7B2WzgZiv122Yag,143
|
|
8
|
+
trellis/ir/serializer.py,sha256=meTtSfc-WhTO7lBGS0wW5zfGj-ZP-j3fEuvbIgxsVJM,3235
|
|
9
|
+
trellis_sdk-0.1.0.dist-info/METADATA,sha256=v1l2RfjHZOWAjZ7Kq8ByAQ3YbOreX3Aw69GSMaeH_6g,3148
|
|
10
|
+
trellis_sdk-0.1.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
11
|
+
trellis_sdk-0.1.0.dist-info/top_level.txt,sha256=RDqNl7NDCzDlrQIMouyXK7VWrzo-lkgiZAmlng6giTs,8
|
|
12
|
+
trellis_sdk-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
trellis
|