Package com.tnsai.roles
Class RoleBuilder
java.lang.Object
com.tnsai.roles.RoleBuilder
Fluent builder for creating Role instances without subclassing.
This builder provides a fluent API for constructing roles with custom identity and LLM configuration, ideal for:
- Creating roles programmatically at runtime
- Dynamic role configuration from external sources (JSON, YAML)
- Testing with mock configurations
Safety constraints (mustNever / mustAlways) are now declared on
@ActionSpec methods of the role class. Builder-built roles do not
carry constraints — for safety-bound behavior, define a Role subclass with
@ActionSpec-annotated methods.
Example usage:
Role role = RoleBuilder.create()
.name("Analyst")
.goal("Analyze data and provide insights")
.domain("data_science")
.llm(new OpenAIClient("gpt-4o"))
.build();
- Author:
- TnsAI Team
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionactions(List<ActionConfig> actions) Registers multiple programmatic actions (TNS-551).addAction(ActionConfig action) Registers a programmatic action on this role (TNS-551) — the builder counterpart of an@ActionSpec-annotated method.build()Builds and returns the configured Role instance.static RoleBuildercreate()inputGuardrail(InputGuardrailConfig inputGuardrail) Sets this role's class-level input guardrail (TNS-561) — the programmatic equivalent of a class-level@InputGuardrail.Sets the LLM client for this role.outputGuardrail(OutputGuardrailConfig outputGuardrail) Sets this role's class-level output guardrail (TNS-561) — the programmatic equivalent of a class-level@OutputGuardrail.resilience(ResilienceConfig resilience) Sets this role's resilience policy (TNS-567) — the programmatic equivalent of a class-level@Resilience.
-
Method Details
-
create
-
name
-
goal
-
domain
-
llm
Sets the LLM client for this role.- Parameters:
llm- the LLM client, or null to use agent's LLM- Returns:
- this builder
-
resilience
Sets this role's resilience policy (TNS-567) — the programmatic equivalent of a class-level@Resilience. Applies to the role's actions viaActionExecutorwhen they have no method-level@Resilience. Models the runtime-enforced subset (retry + timeout); seeResilienceConfig.RoleBuilder.create().name("payment").goal("Process payments") .resilience(ResilienceConfig.builder() .retry(RetryPolicy.builder().maxAttempts(3).build()) .timeoutMs(5000) .build());- Parameters:
resilience- the resilience config (non-null; useResilienceConfig.NONEfor none)- Returns:
- this builder
- Since:
- 0.12.0 (TNS-567)
-
inputGuardrail
Sets this role's class-level input guardrail (TNS-561) — the programmatic equivalent of a class-level@InputGuardrail. Applies to every action in the role;InputGuardrailEnforcerenforces it (a method-level@InputGuardrailannotation still overrides it).RoleBuilder.create().name("support").goal("...") .inputGuardrail(InputGuardrailConfig.builder() .maxLength(10_000) .blockPatterns(List.of("ignore previous")) .build());- Parameters:
inputGuardrail- the input-guardrail config;nullclears it- Returns:
- this builder
- Since:
- 0.12.0 (TNS-561)
-
outputGuardrail
Sets this role's class-level output guardrail (TNS-561) — the programmatic equivalent of a class-level@OutputGuardrail. Applies to every action in the role;OutputGuardrailEnforcerenforces it (a method-level@OutputGuardrailannotation still overrides it).RoleBuilder.create().name("support").goal("...") .outputGuardrail(OutputGuardrailConfig.builder() .maxChars(2000) .onFailure(OutputGuardrail.FailureAction.TRUNCATE) .build());- Parameters:
outputGuardrail- the output-guardrail config;nullclears it- Returns:
- this builder
- Since:
- 0.12.0 (TNS-561)
-
addAction
Registers a programmatic action on this role (TNS-551) — the builder counterpart of an@ActionSpec-annotated method. The resultingRoledispatches it exactly like an annotation-discovered action.- Parameters:
action- the action config (seeActionConfig.local(java.lang.String, java.lang.String, com.tnsai.metadata.ActionHandler))- Returns:
- this builder
- Since:
- 0.11.0
-
actions
Registers multiple programmatic actions (TNS-551).- Parameters:
actions- the action configs (appended to any already added)- Returns:
- this builder
- Since:
- 0.11.0
-
build
Builds and returns the configured Role instance.- Returns:
- the configured Role
- Throws:
IllegalStateException- if required configuration is missing
-