Class PromptRegistry

java.lang.Object
com.tnsai.prompt.PromptRegistry

public class PromptRegistry extends Object
Central registry for prompt templates with hot-reload support.

The registry provides:

  • Template registration and lookup
  • Hot-reloading when files change
  • Template versioning
  • Search by tags

Usage


 // Get global registry
 PromptRegistry registry = PromptRegistry.global();

 // Register templates
 registry.register(SimplePromptTemplate.of("greeting", "Hello {{name}}!"));

 // Load from directory with hot-reload
 registry.loadDirectory(Path.of("prompts"), true);

 // Get template
 PromptTemplate template = registry.get("greeting");
 String prompt = template.render(Map.of("name", "World"));

 // Search by tag
 List<PromptTemplate> agentPrompts = registry.findByTag("agent");
 
  • Constructor Details

    • PromptRegistry

      public PromptRegistry()
      Creates a new registry.
  • Method Details

    • global

      public static PromptRegistry global()
      Gets the global registry instance.
      Returns:
      global registry
    • register

      public PromptRegistry register(com.tnsai.prompt.PromptTemplate template)
      Registers a template.
      Parameters:
      template - the template
      Returns:
      this registry
    • registerAll

      public PromptRegistry registerAll(com.tnsai.prompt.PromptTemplate... templates)
      Registers multiple templates.
      Parameters:
      templates - the templates
      Returns:
      this registry
    • registerAll

      public PromptRegistry registerAll(Map<String,com.tnsai.prompt.PromptTemplate> templates)
      Registers multiple templates from a map.
      Parameters:
      templates - map of name to template
      Returns:
      this registry
    • get

      public com.tnsai.prompt.PromptTemplate get(String name)
      Gets a template by name.
      Parameters:
      name - the template name
      Returns:
      the template
      Throws:
      com.tnsai.prompt.PromptException - if not found
    • getOrNull

      public com.tnsai.prompt.PromptTemplate getOrNull(String name)
      Gets a template by name, or null if not found.
      Parameters:
      name - the template name
      Returns:
      the template or null
    • getOrDefault

      public com.tnsai.prompt.PromptTemplate getOrDefault(String name, com.tnsai.prompt.PromptTemplate defaultTemplate)
      Gets a template with a default fallback.
      Parameters:
      name - the template name
      defaultTemplate - fallback template
      Returns:
      the template or default
    • contains

      public boolean contains(String name)
      Checks if a template exists.
      Parameters:
      name - the template name
      Returns:
      true if exists
    • getNames

      public Set<String> getNames()
      Gets all registered template names.
      Returns:
      set of names
    • getAll

      public Collection<com.tnsai.prompt.PromptTemplate> getAll()
      Gets all registered templates.
      Returns:
      collection of templates
    • findByTag

      public List<com.tnsai.prompt.PromptTemplate> findByTag(String tag)
      Finds templates by tag.
      Parameters:
      tag - the tag to search for
      Returns:
      list of matching templates
    • findByAuthor

      public List<com.tnsai.prompt.PromptTemplate> findByAuthor(String author)
      Finds templates by author.
      Parameters:
      author - the author
      Returns:
      list of matching templates
    • remove

      public com.tnsai.prompt.PromptTemplate remove(String name)
      Removes a template.
      Parameters:
      name - the template name
      Returns:
      removed template or null
    • clear

      public void clear()
      Clears all templates.
    • loadFromResource

      public PromptRegistry loadFromResource(String resourcePath)
      Loads templates from a resource path.
      Parameters:
      resourcePath - the resource path
      Returns:
      this registry
    • loadDirectory

      public PromptRegistry loadDirectory(Path directory)
      Loads templates from a directory.
      Parameters:
      directory - the directory path
      Returns:
      this registry
    • loadDirectory

      public PromptRegistry loadDirectory(Path directory, boolean enableHotReload)
      Loads templates from a directory with optional hot-reload.
      Parameters:
      directory - the directory path
      enableHotReload - whether to watch for changes
      Returns:
      this registry
    • watchDirectory

      public void watchDirectory(Path directory)
      Starts watching a directory for changes.
      Parameters:
      directory - the directory to watch
    • addReloadListener

      public void addReloadListener(Consumer<com.tnsai.prompt.PromptTemplate> listener)
      Adds a listener for template reloads.
      Parameters:
      listener - the listener
    • removeReloadListener

      public void removeReloadListener(Consumer<com.tnsai.prompt.PromptTemplate> listener)
      Removes a reload listener.
      Parameters:
      listener - the listener
    • stopWatching

      public void stopWatching()
      Stops watching directories and releases resources.
    • size

      public int size()
      Gets the number of registered templates.
      Returns:
      template count