variables

package
v1.1.6 Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2025 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package variables provides dynamic variable resolution for prompt templates. Variable providers can inject context from external sources (databases, APIs, conversation state) before template rendering.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ChainProvider

type ChainProvider struct {
	// contains filtered or unexported fields
}

ChainProvider composes multiple providers into a single provider. Providers are called in order, with later providers overriding variables from earlier providers when keys conflict.

func Chain

func Chain(providers ...Provider) *ChainProvider

Chain creates a ChainProvider from multiple providers. Providers are called in the order given. Later providers override variables from earlier providers.

func (*ChainProvider) Add

Add appends a provider to the chain.

func (*ChainProvider) Name

func (c *ChainProvider) Name() string

Name returns the provider identifier.

func (*ChainProvider) Provide

func (c *ChainProvider) Provide(ctx context.Context) (map[string]string, error)

Provide calls all chained providers and merges their results. Returns an error if any provider fails.

func (*ChainProvider) Providers

func (c *ChainProvider) Providers() []Provider

Providers returns the list of providers in the chain.

type Provider

type Provider interface {
	// Name returns the provider identifier (for logging/debugging)
	Name() string

	// Provide returns variables to inject into template context.
	// Called before each template render.
	Provide(ctx context.Context) (map[string]string, error)
}

Provider resolves variables dynamically at runtime. Variables returned override static variables with the same key. Providers are called before template rendering to inject dynamic context.

Providers that need access to conversation state (like StateProvider) should receive it via constructor injection rather than through Provide().

type StateProvider

type StateProvider struct {

	// KeyPrefix filters metadata keys. Only keys with this prefix are included.
	// If empty, all metadata keys are included.
	KeyPrefix string

	// StripPrefix removes the KeyPrefix from variable names when true.
	// For example, if KeyPrefix="user_" and StripPrefix=true,
	// metadata key "user_name" becomes variable "name".
	StripPrefix bool
	// contains filtered or unexported fields
}

StateProvider resolves variables from conversation state metadata. It extracts key-value pairs from the state's Metadata field and converts them to string variables for template substitution.

The StateStore is injected via constructor, allowing the provider to look up state for the current conversation.

func NewStatePrefixProvider

func NewStatePrefixProvider(store statestore.Store, conversationID, prefix string, stripPrefix bool) *StateProvider

NewStatePrefixProvider creates a StateProvider that only extracts metadata keys with the given prefix. If stripPrefix is true, the prefix is removed from the resulting variable names.

func NewStateProvider

func NewStateProvider(store statestore.Store, conversationID string) *StateProvider

NewStateProvider creates a StateProvider that extracts all metadata as variables from the given conversation's state.

func (*StateProvider) Name

func (p *StateProvider) Name() string

Name returns the provider identifier.

func (*StateProvider) Provide

func (p *StateProvider) Provide(ctx context.Context) (map[string]string, error)

Provide extracts variables from conversation state metadata. Returns nil if store is nil, conversation not found, or has no metadata.

type TimeProvider

type TimeProvider struct {
	// Format is the time format string for current_time variable.
	// Defaults to time.RFC3339 if empty.
	Format string

	// Location specifies the timezone. Defaults to UTC if nil.
	Location *time.Location
	// contains filtered or unexported fields
}

TimeProvider provides current time and date variables. Useful for prompts that need temporal context like "What day is it?" or time-sensitive instructions.

func NewTimeProvider

func NewTimeProvider() *TimeProvider

NewTimeProvider creates a TimeProvider with default settings (UTC, RFC3339 format).

func NewTimeProviderWithFormat

func NewTimeProviderWithFormat(format string) *TimeProvider

NewTimeProviderWithFormat creates a TimeProvider with a custom time format.

func NewTimeProviderWithLocation

func NewTimeProviderWithLocation(loc *time.Location) *TimeProvider

NewTimeProviderWithLocation creates a TimeProvider for a specific timezone.

func (*TimeProvider) Name

func (p *TimeProvider) Name() string

Name returns the provider identifier.

func (*TimeProvider) Provide

func (p *TimeProvider) Provide(ctx context.Context) (map[string]string, error)

Provide returns time-related variables. Variables provided:

  • current_time: Full timestamp in configured format
  • current_date: Date in YYYY-MM-DD format
  • current_year: Four-digit year
  • current_month: Full month name (e.g., "January")
  • current_weekday: Full weekday name (e.g., "Monday")
  • current_hour: Hour in 24-hour format (00-23)

func (*TimeProvider) WithNowFunc

func (p *TimeProvider) WithNowFunc(fn func() time.Time) *TimeProvider

WithNowFunc sets a custom time source (primarily for testing).

Jump to

Keyboard shortcuts

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