Documentation
¶
Overview ¶
Package prompt contains runtime prompt registry and override resolution logic.
Package prompt defines runtime contracts for prompt identity, versioning, and scoped overrides.
The package intentionally separates prompt metadata (PromptSpec/PromptRef) from rendering output (PromptContent) and override targeting (Scope). This allows runtimes and applications to reason about prompt provenance without coupling to any specific model provider implementation.
Index ¶
- Variables
- func ScopeMatches(overrideScope Scope, requestedScope Scope) bool
- func ScopePrecedence(scope Scope) int
- func VersionFromTemplate(template string) string
- type Ident
- type InMemoryStore
- func (s *InMemoryStore) History(ctx context.Context, promptID Ident) ([]*Override, error)
- func (s *InMemoryStore) List(ctx context.Context) ([]*Override, error)
- func (s *InMemoryStore) Resolve(ctx context.Context, promptID Ident, scope Scope) (*Override, error)
- func (s *InMemoryStore) Set(ctx context.Context, promptID Ident, scope Scope, template string, ...) error
- type Override
- type PromptContent
- type PromptRef
- type PromptRole
- type PromptSpec
- type Registry
- type RenderEvent
- type RenderObserver
- type Scope
- type Store
Constants ¶
This section is empty.
Variables ¶
var ( // ErrPromptNotFound reports that no baseline prompt exists for the requested ID. ErrPromptNotFound = errors.New("prompt not found") // ErrDuplicatePromptSpec reports that a prompt ID has already been registered. ErrDuplicatePromptSpec = errors.New("duplicate prompt spec") // ErrTemplateParse reports that a resolved prompt template failed to parse. ErrTemplateParse = errors.New("prompt template parse failed") // ErrTemplateExecute reports that a resolved prompt template failed to execute. ErrTemplateExecute = errors.New("prompt template execute failed") // ErrInvalidPromptSpec reports that a prompt spec violates required contracts. ErrInvalidPromptSpec = errors.New("invalid prompt spec") )
Functions ¶
func ScopeMatches ¶
ScopeMatches reports whether overrideScope applies to requestedScope.
Contract:
- SessionID must match exactly when overrideScope.SessionID is set.
- Every label in overrideScope.Labels must exist with the same value in requestedScope.Labels.
func ScopePrecedence ¶
ScopePrecedence returns override precedence for conflict resolution.
Higher values are more specific:
- Session-scoped overrides outrank non-session overrides.
- For the same session dimension, more constrained label sets outrank less constrained ones.
func VersionFromTemplate ¶
VersionFromTemplate derives a deterministic prompt version identifier from template source content.
Types ¶
type Ident ¶
type Ident string
Ident is the strong type for globally unique prompt identifiers.
Prompt IDs are canonical strings (for example, "example.agent.system") used across prompt registration, rendering, override storage, and observability. Using Ident avoids accidental mixing with unrelated string IDs such as run IDs or session IDs.
type InMemoryStore ¶
type InMemoryStore struct {
// contains filtered or unexported fields
}
InMemoryStore stores prompt overrides in process memory.
This implementation is primarily intended for local development and tests. Overrides are not persisted across process restarts.
func NewInMemoryStore ¶
func NewInMemoryStore() *InMemoryStore
NewInMemoryStore returns an empty in-memory prompt store.
func (*InMemoryStore) List ¶
func (s *InMemoryStore) List(ctx context.Context) ([]*Override, error)
List returns all overrides, newest-first.
func (*InMemoryStore) Resolve ¶
func (s *InMemoryStore) Resolve(ctx context.Context, promptID Ident, scope Scope) (*Override, error)
Resolve returns the highest-precedence override for promptID and scope.
Precedence order is:
- session-scoped overrides before non-session overrides
- then most constrained label set (more labels = more specific)
- then newest override
type Override ¶
type Override struct {
// PromptID identifies which baseline prompt this override targets.
PromptID Ident
// Scope constrains where the override applies.
Scope Scope
// Template is the override template source.
Template string
// Version is the version assigned to the override.
Version string
// CreatedAt records when this override was persisted.
CreatedAt time.Time
// Metadata carries optional caller-defined attributes (for example,
// experiment identifiers).
Metadata map[string]string
}
Override is one stored prompt override record.
type PromptContent ¶
type PromptContent struct {
// Text is the rendered prompt text.
Text string
// Ref identifies which prompt/version produced Text.
Ref PromptRef
}
PromptContent is the output of rendering a resolved prompt.
type PromptRef ¶
type PromptRef struct {
// ID is the prompt identifier.
ID Ident
// Version is the resolved version used for rendering.
Version string
}
PromptRef is a lightweight prompt identity attached to rendered outputs and model requests.
type PromptRole ¶
type PromptRole string
PromptRole classifies the role a prompt plays in a model request.
const ( // PromptRoleSystem identifies system prompts. PromptRoleSystem PromptRole = "system" // PromptRoleUser identifies user prompts. PromptRoleUser PromptRole = "user" // PromptRoleTool identifies tool-level prompts. PromptRoleTool PromptRole = "tool" // PromptRoleSynthesis identifies synthesis/finalization prompts. PromptRoleSynthesis PromptRole = "synthesis" )
type PromptSpec ¶
type PromptSpec struct {
// ID is the globally unique prompt identifier.
ID Ident
// AgentID identifies the owning agent.
AgentID string
// Role identifies how the prompt is used (system/user/tool/synthesis).
Role PromptRole
// Description is optional human-readable metadata.
Description string
// Template is the baseline template source.
Template string
// Version identifies the baseline prompt version. If empty, registries may
// derive a deterministic version from Template content.
Version string
// Funcs is the template function map used when rendering this prompt.
Funcs template.FuncMap
}
PromptSpec is the baseline registration record for a prompt template.
Prompt specs are immutable once registered in a Registry. ID must be unique within a registry instance.
type Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
Registry keeps immutable baseline prompt specs and resolves runtime content by layering scoped store overrides on top.
func NewRegistry ¶
NewRegistry returns an empty prompt registry.
func (*Registry) List ¶
func (r *Registry) List() []PromptSpec
List returns all registered prompt specs sorted by ID.
func (*Registry) Register ¶
func (r *Registry) Register(spec PromptSpec) error
Register adds one baseline prompt spec.
func (*Registry) Render ¶
func (r *Registry) Render(ctx context.Context, id Ident, scope Scope, data any) (*PromptContent, error)
Render resolves and renders one prompt using baseline+override composition.
func (*Registry) SetObserver ¶
func (r *Registry) SetObserver(observer RenderObserver)
SetObserver configures the observer for successful render events.
type RenderEvent ¶
RenderEvent is emitted by Registry after successful prompt rendering.
type RenderObserver ¶
type RenderObserver func(ctx context.Context, event RenderEvent)
RenderObserver receives prompt render events.
type Scope ¶
Scope constrains which prompt overrides apply to a render call.
SessionID is an optional first-class session dimension. Labels carries caller-defined scope dimensions (for example, account/region/environment). Empty fields mean "no scope constraint" for that dimension.
type Store ¶
type Store interface {
// Resolve returns the highest-precedence override for promptID within scope.
// Returns (nil, nil) when no override exists.
Resolve(ctx context.Context, promptID Ident, scope Scope) (*Override, error)
// Set persists a new override record.
Set(ctx context.Context, promptID Ident, scope Scope, template string, metadata map[string]string) error
// History returns override records for promptID ordered newest-first.
History(ctx context.Context, promptID Ident) ([]*Override, error)
// List returns all override records ordered newest-first.
List(ctx context.Context) ([]*Override, error)
}
Store persists prompt overrides and resolves the active override for a prompt/scope pair.
Resolve must return (nil, nil) when no override matches. Callers treat nil as "use baseline spec".