Class Prompts
java.lang.Object
ai.redouble.nucleo.prompt.Prompts
Governor-style facade for the Prompt registry. Only public entry point for obtaining
Prompt instances.
Resolution order in produce(String):
- CACHE (for keys served by
StaticPromptSource) - returned immediately with fingerprint-aware guardrail re-validation if new guardrails were registered since cache insert. - OVERRIDES.get(key) - per-key override set via
replace(String, PromptSource). - GLOBAL_BACKEND - wholesale substitute set via
setGlobalBackend(PromptSource). - DEFAULTS.get(key) - code-declared default populated by the
@StaticPrompt/@DynamicPromptscanner.
PromptNotFoundException.
Invalidation ordering is reverse of intuitive put/remove so concurrent readers never observe the race "new override, stale cache":
replace(String, PromptSource):CACHE.remove(key); OVERRIDES.put(key, src);resetOverride(String):CACHE.remove(key); OVERRIDES.remove(key);setGlobalBackend(PromptSource):CACHE.clear(); GLOBAL_BACKEND = src;
Guardrails attach per-key via addGuardrail(String, Supplier). They are NOT attached to the
source, so source substitution cannot bypass validation. Baseline guardrails
(addBaselineGuardrail(Supplier)) apply to every Prompt produced regardless of key,
including inline of(String) calls; nothing is registered by default - a deployment
wires the shipped ones (size cap, exfiltration marker) itself.
- Since:
- 0.1 (2026-04-20)
- Author:
- Andrey Santrosyan
-
Method Summary
Modifier and TypeMethodDescriptionstatic voidaddBaselineGuardrail(Supplier<Guardrail<Prompt>> factory) Attach a framework-baseline guardrail factory that runs for every Prompt produced.static voidaddGuardrail(String key, Supplier<Guardrail<Prompt>> factory) Attach a guardrail factory to a specific key.static PromptbindStaticDefault(String key, String text) Lazy bind: if no default is registered for this key, install one wrappingtextin aStaticTextSource.static voidstatic PromptContextcomputeContextFor(String key, com.fasterxml.jackson.databind.JsonNode content) Return thePromptContextlineage for a given (key, content) pair.static PromptBuild a one-shot Prompt under a synthetic key derived from a content hash.static PromptRegister an inline text prompt as a code-level default under an explicit key and return it.static PromptResolve the registered source for this key, produce its content, validate via per-key and baseline guardrails, return aPrompt.static voidregisterAnnotated(AnnotatedElement element) Registers one@StaticPrompt/@DynamicPromptelement (a type, static field or static method) discovered at build time, and marks discovery done so the first-produce auto-scan never fires.static voidremoveBaselineGuardrail(Supplier<Guardrail<Prompt>> factory) Remove a previously-registered baseline guardrail factory.static voidreplace(String key, PromptSource source) Per-key override.static voidresetOverride(String key) Clear a per-key override, returning produce() to the global-backend / default layer.static voidscanPackage(String pkg) Eagerly scan a package for@StaticPrompt/@DynamicPromptdeclarations and register their default sources.static voidChange the package auto-scanned on firstproduce(String)orof(String)call.static voidsetGlobalBackend(PromptSource backend) Substitute a single source for every key.static voidEagerly produce every registeredStaticPromptSourcedefault, running its guardrails and populating the cache.
-
Method Details
-
produce
Resolve the registered source for this key, produce its content, validate via per-key and baseline guardrails, return aPrompt. If the source is aStaticPromptSource, subsequent calls return a cached Prompt.- Throws:
PromptNotFoundException- if no source resolves for the keyGuardrailException- if any guardrail rejects the produced Prompt
-
of
Build a one-shot Prompt under a synthetic key derived from a content hash. Not registered. Not substitutable. For inline throwaway content; registered@StaticPrompt/@DynamicPromptdeclarations are the preferred path for anything reusable. Baseline guardrails still run.- Throws:
GuardrailException
-
of
Register an inline text prompt as a code-level default under an explicit key and return it. If the key already has a default, the prior registration is replaced (this is the ergonomic entry point for "declare a prompt in code without the annotation scanner," so last-write-wins is the documented semantic). Does not populate the override layer -replace(String, PromptSource)andresetOverride(String)still function normally on top. -
bindStaticDefault
public static Prompt bindStaticDefault(String key, String text) throws PromptNotFoundException, GuardrailException Lazy bind: if no default is registered for this key, install one wrappingtextin aStaticTextSource. Subsequent calls reuse the existing registration and preserve the cached Prompt - no put, no cache invalidate. The intended caller is the runtime hook onSingleObjectiveThinkerthat resolves a@StaticPromptinstance-method declaration on first construction; the hook needs idempotent registration semantics to avoid invalidating the StaticPromptSource cache on every thinker construction. -
replace
Per-key override. Invalidates the cache entry for this key before publishing the new source. -
resetOverride
Clear a per-key override, returning produce() to the global-backend / default layer. -
setGlobalBackend
Substitute a single source for every key. Clears the cache so every key re-produces. -
clearGlobalBackend
public static void clearGlobalBackend() -
addGuardrail
Attach a guardrail factory to a specific key. Survives source replacement. Eachproduce(key)call that needs validation invokes the factory to get a fresh guardrailJobinstance and dispatches it viaJobDispatcher- Jobs are one-shot, so factories (not instances) are what survive in the registry. Invalidates the cached validation result. -
addBaselineGuardrail
Attach a framework-baseline guardrail factory that runs for every Prompt produced. Factory is invoked fresh per validation (seeaddGuardrail(String, Supplier)). Order of registration determines execution order. -
removeBaselineGuardrail
-
scanPackage
Eagerly scan a package for@StaticPrompt/@DynamicPromptdeclarations and register their default sources. Also disables the first-produce auto-scan since the caller has explicitly directed discovery. One scan per process: if any scan has already fired (this method or the auto-scan), the call is a silent no-op. -
registerAnnotated
Registers one@StaticPrompt/@DynamicPromptelement (a type, static field or static method) discovered at build time, and marks discovery done so the first-produce auto-scan never fires. A build that enumerates the annotated elements itself - the Quarkus extension does, from its class index - calls this per element instead of the classpath scan that a native image cannot run. The per-element validation and registration arePromptScanner's, so build-time and scan discovery share one contract. -
setDefaultScanPackage
Change the package auto-scanned on firstproduce(String)orof(String)call. Must be called before any produce/of, otherwise has no effect because the auto-scan has already fired. -
warmCache
Eagerly produce every registeredStaticPromptSourcedefault, running its guardrails and populating the cache. Call at bootstrap (outside any resource-holding region) so subsequent thinker-sideproduce(String)calls hit the cache. -
computeContextFor
public static PromptContext computeContextFor(String key, com.fasterxml.jackson.databind.JsonNode content) Return thePromptContextlineage for a given (key, content) pair. Called byTextPrompt.context(). Memoized on the Prompt identity via a weak map so repeatedcontext()calls on the same Prompt don't rehash.
-