-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
192515c
commit 94b3d23
Showing
21 changed files
with
109 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,22 @@ | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING, Optional | ||
|
||
from attrs import Attribute, define, field | ||
from attrs import define, field | ||
|
||
from griptape.rules import BaseRule, Ruleset | ||
|
||
if TYPE_CHECKING: | ||
from griptape.structures import Structure | ||
|
||
|
||
@define(slots=False) | ||
class RuleMixin: | ||
DEFAULT_RULESET_NAME = "Default Ruleset" | ||
ADDITIONAL_RULESET_NAME = "Additional Ruleset" | ||
|
||
rulesets: list[Ruleset] = field(factory=list, kw_only=True) | ||
_rulesets: list[Ruleset] = field(factory=list, kw_only=True, alias="rulesets") | ||
rules: list[BaseRule] = field(factory=list, kw_only=True) | ||
structure: Optional[Structure] = field(default=None, kw_only=True) | ||
|
||
@rulesets.validator # pyright: ignore[reportAttributeAccessIssue] | ||
def validate_rulesets(self, _: Attribute, rulesets: list[Ruleset]) -> None: | ||
if not rulesets: | ||
return | ||
|
||
if self.rules: | ||
raise ValueError("Can't have both rulesets and rules specified.") | ||
|
||
@rules.validator # pyright: ignore[reportAttributeAccessIssue] | ||
def validate_rules(self, _: Attribute, rules: list[BaseRule]) -> None: | ||
if not rules: | ||
return | ||
|
||
if self.rulesets: | ||
raise ValueError("Can't have both rules and rulesets specified.") | ||
|
||
@property | ||
def all_rulesets(self) -> list[Ruleset]: | ||
structure_rulesets = [] | ||
|
||
if self.structure: | ||
if self.structure.rulesets: | ||
structure_rulesets = self.structure.rulesets | ||
elif self.structure.rules: | ||
structure_rulesets = [Ruleset(name=self.DEFAULT_RULESET_NAME, rules=self.structure.rules)] | ||
def rulesets(self) -> list[Ruleset]: | ||
rulesets = self._rulesets | ||
|
||
task_rulesets = [] | ||
if self.rulesets: | ||
task_rulesets = self.rulesets | ||
elif self.rules: | ||
task_ruleset_name = self.ADDITIONAL_RULESET_NAME if structure_rulesets else self.DEFAULT_RULESET_NAME | ||
|
||
task_rulesets = [Ruleset(name=task_ruleset_name, rules=self.rules)] | ||
if self.rules: | ||
rulesets.append(Ruleset(name=self.DEFAULT_RULESET_NAME, rules=self.rules)) | ||
|
||
return structure_rulesets + task_rulesets | ||
return rulesets |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.