Package jakarta.ai.agent
This package provides the fundamental APIs for developing agents that can perceive, reason, decide, and act autonomously within a Jakarta EE environment. Agents are CDI-managed beans that encapsulate goal-driven behavior using a structured workflow model with well-defined lifecycle phases.
Core Components
Agent Annotations
@Agent- Marks a class as an agent@Trigger- Marks a workflow trigger method@Decision- Marks a decision point in the workflow@Action- Marks an action execution step@Outcome- Marks the workflow outcome phase@HandleException- Marks an exception handler
Core Interfaces
LargeLanguageModel- Facade for LLM operations
CDI Integration
@WorkflowScoped- Custom CDI scope for workflow lifecycle
Data Models
Result- Standardized decision outcome recordLLMException- Runtime exception for LLM operation failures
Workflow Execution Model
An agent workflow is flexible and begins with a required trigger, followed by any combination of optional decision and action phases. Workflows support simple linear execution currently, with potential for more complex dynamic conditional branching in future versions.
Workflow Phases
- Trigger Phase (required) - Exactly one
@Triggermethod is invoked when a CDI event matching its parameters is fired. This initiates the workflow. Triggers support two return patterns:- void - Performs initialization with side effects only
- Domain Object - Returns an object available for injection in subsequent phases
- Decision Phases (optional) - Zero or more
@Decisionmethods analyze the workflow state and determine whether to proceed. They support three return patterns:- Boolean -
trueto proceed,falseto terminate Result- Success flag with optional details- Object - Non-null to proceed (object available for injection), null to terminate
- Boolean -
- Action Phases (optional) - Zero or more
@Actionmethods execute operations as part of the workflow. Actions can execute independently or based on prior decisions. Actions support two return patterns:- void - Performs side effects only (e.g., alerts, database updates)
- Domain Object - Returns an object available for injection in subsequent workflow methods
- Outcome Phase (optional) - Zero or one
@Outcomemethod handles finalization, state persistence, and downstream notifications. Must return void. - Exception Handling (optional) - Zero or more
@HandleExceptionmethods handle exceptions throughout the workflow lifecycle. Handler returns normally to continue workflow (after recovery), or re-throws exception to stop workflow.
Flexible Composition
Decisions and actions can be intermixed in any sequence, allowing patterns such as:
- Trigger + Action (simple execution without decisions)
- Trigger + multiple Actions (sequential processing)
- Trigger + Decision + Action (conditional execution)
- Trigger + Decision + Action + Decision + Action (complex branching)
Methods execute in declaration order within the source file.
Usage Example
import jakarta.ai.agent.*;
import jakarta.enterprise.inject.Produces;
import jakarta.inject.Inject;
@Agent(name = "ExampleAgent", description = "An example autonomous agent")
public class ExampleAgent {
@Inject
private LargeLanguageModel llm;
@Trigger
public void onEvent(MyEvent event) {
// Triggered by CDI event
}
@Decision
public boolean shouldProceed(MyEvent event) {
String analysis = llm.query("Should we process this event?", event);
return analysis.contains("yes");
}
@Action
public void executeAction(MyEvent event) {
// Perform the action with side effects
notifySystem(event);
}
@Outcome
public void recordOutcome(MyEvent event) {
// Finalize and persist results
}
}
CDI Integration
Agents are CDI-managed beans that integrate seamlessly with Jakarta EE:
- Dependency injection of other CDI beans
- Custom
@WorkflowScopedscope for workflow-level lifecycle - CDI event observation
- Interceptor support for cross-cutting concerns
Large Language Model Integration
The LargeLanguageModel interface provides
a minimal, type-converting facade for LLM operations. Implementations
can support:
- Parameterized text prompts with structured context objects
- String and domain object returns
- Unwrapping for vendor-specific features
LLM operations may throw:
IllegalArgumentException- For null or invalid parameters, or serialization failures on input parametersLLMException- Runtime exception for LLM service failures (communication errors, rate limiting, timeouts, invalid responses) or response deserialization failures
These exceptions can be handled with try-catch blocks or
@HandleException
methods for workflow-level error handling.
Parameter Injection
Workflow methods automatically receive parameters based on type resolution:
- Trigger event objects - From the workflow trigger
- Decision results - Objects returned from
@Decisionmethods - Action results - Objects returned from
@Actionmethods LargeLanguageModel- Injected LLM facade- Any CDI injectable dependencies - Standard Jakarta EE beans and resources
- Since:
- 1.0
- See Also:
-
ClassDescriptionMarks a method as an action in an agent workflow.Declares a class as an AI agent.Marks a method as a decision point in an agent workflow.Marks a method as an exception handler within an agent workflow.Minimal facade for Large Language Model (LLM) operations.Runtime exception thrown when a Large Language Model (LLM) operation fails.Marks a method as the outcome of an agent workflow.Represents the result of a decision or workflow step in an agent.Marks a method as an external trigger that initiates an agent workflow.Defines a workflow-scoped CDI context.Supports inline instantiation of the WorkflowScoped annotation.