prompt

package
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 21, 2026 License: MIT Imports: 10 Imported by: 0

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

Constants

This section is empty.

Variables

View Source
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

func ScopeMatches(overrideScope Scope, requestedScope Scope) bool

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

func ScopePrecedence(scope Scope) int

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

func VersionFromTemplate(template string) string

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.

func (Ident) String

func (id Ident) String() string

String returns the raw string form of the prompt identifier.

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) History

func (s *InMemoryStore) History(ctx context.Context, promptID Ident) ([]*Override, error)

History returns override records for one prompt, newest-first.

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

func (*InMemoryStore) Set

func (s *InMemoryStore) Set(ctx context.Context, promptID Ident, scope Scope, template string, metadata map[string]string) error

Set persists one override record.

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

func NewRegistry(store Store) *Registry

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.

func (*Registry) SetStore

func (r *Registry) SetStore(store Store)

SetStore updates the override store used by Render.

type RenderEvent

type RenderEvent struct {
	PromptID Ident
	Version  string
	Scope    Scope
}

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

type Scope struct {
	SessionID string
	Labels    map[string]string
}

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".

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL