Class PromptTemplate


@ComponentId("akka-prompt-template") public final class PromptTemplate extends EventSourcedEntity<PromptTemplate.Prompt,PromptTemplate.Event>
A built-in Event Sourced Entity for managing dynamic prompt templates with change history.

PromptTemplate allows you to change agent prompts at runtime without restarting or redeploying the service. Since it's managed as an entity, you retain full change history and can subscribe to prompt changes.

Automatic Registration: The Akka runtime automatically registers this entity when it detects an Agent component in your service.

Template Parameters: Templates support Java String.formatted(java.lang.Object...) style parameters when using systemMessageFromTemplate(templateId, args...).

Change Monitoring: You can subscribe to prompt template changes using a Consumer to build views or react to prompt updates.

  • Constructor Details

    • PromptTemplate

      public PromptTemplate()
  • Method Details

    • init

      public EventSourcedEntity.Effect<akka.Done> init(String prompt)
      Initialize the prompt template. Call this method for existing prompt template will be ignored, so it's safe to use it e.g. in ServiceSetup to initialize the prompt template with default value.
    • update

      public EventSourcedEntity.Effect<akka.Done> update(String prompt)
      Update the prompt template. Updating the prompt template with the same value will be ignored.
    • delete

      public EventSourcedEntity.Effect<akka.Done> delete()
      Delete the prompt template. If the prompt template was already deleted or never set, the call will succeed.
    • get

      Get the prompt template. If the prompt template is not set or deleted, an error will be returned.
    • getOptional

      public EventSourcedEntity.Effect<Optional<String>> getOptional()
      Get the prompt template. If the prompt template is not set or deleted, an empty optional will be returned.
    • applyEvent

      public PromptTemplate.Prompt applyEvent(PromptTemplate.Event event)
      Description copied from class: EventSourcedEntity
      This is the main event handler method. Whenever an event is persisted, this handler will be called. It should return the new state of the entity.

      Note that this method is called in two situations:

      • when one or more events are persisted by the command handler, this method is called to produce the new state of the entity.
      • when instantiating an entity from the event journal, this method is called to restore the state of the entity.
      It's important to keep the event handler side effect free. This means that it should only apply the event on the current state and return the updated state. This is because the event handler is called during recovery.

      Events are required to inherit from a common sealed interface, and it's recommend to implement this method using a switch statement. As such, the compiler can check if all existing events are being handled.

      
       // example of sealed event interface with concrete events implementing it
       public sealed interface Event {
         @TypeName("created")
         public record UserCreated(String name, String email) implements Event {};
         @TypeName("email-updated")
         public record EmailUpdated(String newEmail) implements Event {};
       }
      
       // example of applyEvent implementation
       public User applyEvent(Event event) {
          return switch (event) {
            case UserCreated userCreated -> new User(userCreated.name, userCreated.email);
            case EmailUpdated emailUpdated -> this.copy(email = emailUpdated.newEmail);
          }
       }
       
      Specified by:
      applyEvent in class EventSourcedEntity<PromptTemplate.Prompt,PromptTemplate.Event>