Package com.tnsai.roles
Class Role
java.lang.Object
com.tnsai.roles.Role
- Direct Known Subclasses:
ConfigurableRole
Abstract base class for organizational roles in the Gaia methodology.
A Role defines a set of capabilities (actions) that can be assigned to an
Agent. Roles follow the Gaia methodology for
Multi-Agent Systems, providing clear separation of concerns and reusability.
Key Concepts
- Identity - Name, goal, and domain of the role (
RoleIdentity) - Actions - Executable methods discovered via
@ActionSpecannotation, which carry their ownmustNever/mustAlwayssafety constraints
Quick Start
public class ResearchRole extends Role {
@Override
public RoleIdentity getIdentity() {
return new RoleIdentity(
"Researcher",
"Find and analyze information",
"Research"
);
}
@ActionSpec(
type = ActionType.LOCAL,
description = "Search for papers on a topic",
mustNever = {"fabricate references", "plagiarize content"},
mustAlways = {"cite sources properly"}
)
public List<Paper> searchPapers(String topic, int maxResults) {
// Implementation
}
}
Action Discovery
Public methods annotated with @ActionSpec are automatically discovered
and made available as tools for LLM-driven agents. Safety constraints declared
via mustNever / mustAlways are rendered into the system prompt
alongside the action they constrain.
- Author:
- TnsAI Team
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T extends Role>
TSafe factory method for creating Role instances.final Optional<ActionMetadata> final intfinal List<ActionMetadata> Gets all discovered actions.final Stringfinal StringgetGoal()abstract RoleIdentityTemplate method: Define the role's identity.Programmatic input-guardrail config for this role's actions (TNS-561) — the builder-path counterpart of a class-level@InputGuardrail.final LLMClientgetLLM()Gets the LLM client for this role.final StringGenerates a minimal prompt (identity only, without actions).final StringgetName()Programmatic output-guardrail config for this role's actions (TNS-561) — the builder-path counterpart of a class-level@OutputGuardrail.Programmatic resilience policy for this role's actions (TNS-567) — the builder-path counterpart of a class-level@Resilience.final DiscoveredRoleActionsgetSpec()Gets the role metadata (including discovered actions).final StringGenerates a complete system prompt for this role.final booleanfinal booleanhasLLM()final RoleIdentityidentity()Gets the role identity.final RoleSets the LLM client for this role.toString()
-
Method Details
-
create
Safe factory method for creating Role instances. Uses two-phase initialization to prevent issues with virtual method calls in constructor.- Type Parameters:
T- the Role type- Parameters:
roleClass- the Role subclass to instantiate- Returns:
- initialized Role instance
- Throws:
RoleInitializationException- if creation fails
-
getIdentity
Template method: Define the role's identity. Subclasses MUST override this method to return a RoleIdentity.- Returns:
- role identity (name, goal, domain)
-
identity
Gets the role identity.- Returns:
- role identity
- Throws:
IllegalStateException- if not initialized
-
getSpec
Gets the role metadata (including discovered actions). -
getName
-
getGoal
-
getDomain
-
getActions
Gets all discovered actions. -
getActionCount
public final int getActionCount() -
hasAction
-
getAction
-
getLLM
Gets the LLM client for this role.- Returns:
- the LLM client, or null if not set (uses agent's LLM)
-
getResilienceConfig
Programmatic resilience policy for this role's actions (TNS-567) — the builder-path counterpart of a class-level@Resilience. Default isResilienceConfig.NONE;ConfigurableRoleoverrides it with theRoleBuilder.resilience(...)value.ActionExecutorconsults this when no@Resilienceannotation is present.- Returns:
- the role's resilience config, never null
- Since:
- 0.12.0 (TNS-567)
-
getInputGuardrailConfig
Programmatic input-guardrail config for this role's actions (TNS-561) — the builder-path counterpart of a class-level@InputGuardrail. Defaultnull(no builder guardrail);ConfigurableRoleoverrides it with theRoleBuilder.inputGuardrail(...)value.InputGuardrailEnforcerconsults this (precedence: method annotation > this > class annotation).- Returns:
- the role's input-guardrail config, or
nullif none set via builder - Since:
- 0.12.0 (TNS-561)
-
getOutputGuardrailConfig
Programmatic output-guardrail config for this role's actions (TNS-561) — the builder-path counterpart of a class-level@OutputGuardrail. Defaultnull(no builder guardrail);ConfigurableRoleoverrides it with theRoleBuilder.outputGuardrail(...)value.OutputGuardrailEnforcerconsults this (precedence: method annotation > this > class annotation).- Returns:
- the role's output-guardrail config, or
nullif none set via builder - Since:
- 0.12.0 (TNS-561)
-
setLLM
Sets the LLM client for this role.- Parameters:
llm- the LLM client, or null to use agent's LLM- Returns:
- this role for method chaining
-
hasLLM
public final boolean hasLLM() -
getSystemPrompt
Generates a complete system prompt for this role. Includes identity and all actions (with their per-action constraints). -
getMinimalPrompt
Generates a minimal prompt (identity only, without actions). -
toString
-