Package com.tnsai.prompt
Class PromptRegistry
java.lang.Object
com.tnsai.prompt.PromptRegistry
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddReloadListener(Consumer<com.tnsai.prompt.PromptTemplate> listener) Adds a listener for template reloads.voidclear()Clears all templates.booleanChecks if a template exists.List<com.tnsai.prompt.PromptTemplate> findByAuthor(String author) Finds templates by author.List<com.tnsai.prompt.PromptTemplate> Finds templates by tag.com.tnsai.prompt.PromptTemplateGets a template by name.Collection<com.tnsai.prompt.PromptTemplate> getAll()Gets all registered templates.getNames()Gets all registered template names.com.tnsai.prompt.PromptTemplategetOrDefault(String name, com.tnsai.prompt.PromptTemplate defaultTemplate) Gets a template with a default fallback.com.tnsai.prompt.PromptTemplateGets a template by name, or null if not found.static PromptRegistryglobal()Gets the global registry instance.loadDirectory(Path directory) Loads templates from a directory.loadDirectory(Path directory, boolean enableHotReload) Loads templates from a directory with optional hot-reload.loadFromResource(String resourcePath) Loads templates from a resource path.register(com.tnsai.prompt.PromptTemplate template) Registers a template.registerAll(com.tnsai.prompt.PromptTemplate... templates) Registers multiple templates.registerAll(Map<String, com.tnsai.prompt.PromptTemplate> templates) Registers multiple templates from a map.com.tnsai.prompt.PromptTemplateRemoves a template.voidremoveReloadListener(Consumer<com.tnsai.prompt.PromptTemplate> listener) Removes a reload listener.intsize()Gets the number of registered templates.voidStops watching directories and releases resources.voidwatchDirectory(Path directory) Starts watching a directory for changes.
-
Constructor Details
-
PromptRegistry
public PromptRegistry()Creates a new registry.
-
-
Method Details
-
global
Gets the global registry instance.- Returns:
- global registry
-
register
Registers a template.- Parameters:
template- the template- Returns:
- this registry
-
registerAll
Registers multiple templates.- Parameters:
templates- the templates- Returns:
- this registry
-
registerAll
Registers multiple templates from a map.- Parameters:
templates- map of name to template- Returns:
- this registry
-
get
Gets a template by name.- Parameters:
name- the template name- Returns:
- the template
- Throws:
com.tnsai.prompt.PromptException- if not found
-
getOrNull
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 namedefaultTemplate- fallback template- Returns:
- the template or default
-
contains
Checks if a template exists.- Parameters:
name- the template name- Returns:
- true if exists
-
getNames
Gets all registered template names.- Returns:
- set of names
-
getAll
Gets all registered templates.- Returns:
- collection of templates
-
findByTag
Finds templates by tag.- Parameters:
tag- the tag to search for- Returns:
- list of matching templates
-
findByAuthor
Finds templates by author.- Parameters:
author- the author- Returns:
- list of matching templates
-
remove
Removes a template.- Parameters:
name- the template name- Returns:
- removed template or null
-
clear
public void clear()Clears all templates. -
loadFromResource
Loads templates from a resource path.- Parameters:
resourcePath- the resource path- Returns:
- this registry
-
loadDirectory
Loads templates from a directory.- Parameters:
directory- the directory path- Returns:
- this registry
-
loadDirectory
Loads templates from a directory with optional hot-reload.- Parameters:
directory- the directory pathenableHotReload- whether to watch for changes- Returns:
- this registry
-
watchDirectory
Starts watching a directory for changes.- Parameters:
directory- the directory to watch
-
addReloadListener
Adds a listener for template reloads.- Parameters:
listener- the listener
-
removeReloadListener
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
-