cache

package
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Aug 13, 2026 License: AGPL-3.0 Imports: 10 Imported by: 0

Documentation

Overview

Package cache owns response-cache eligibility, semantic identity, versioned canonical records, and stream replay.

Index

Constants

View Source
const RecordSchemaVersion = 1

RecordSchemaVersion identifies the canonical response-cache record schema.

View Source
const SemanticKeyVersion = 2

SemanticKeyVersion identifies the canonical cache-identity encoding.

Variables

View Source
var (
	// ErrIneligible reports a request whose identity is not cache-safe.
	ErrIneligible = errors.New("request is not eligible for response caching")
	// ErrTenantRequired reports a request without tenant identity.
	ErrTenantRequired = errors.New("cache tenant ID is required")
	// ErrGenerationRequired reports a request without catalog identity.
	ErrGenerationRequired = errors.New("catalog generation ID is required")
	// ErrMutableImage reports a remote image that can change without a new key.
	ErrMutableImage = errors.New("remote image input is mutable")
	// ErrUnknownExtension reports provider semantics outside the canonical identity.
	ErrUnknownExtension = errors.New("provider extension semantics are not cache-safe")
	// ErrInvalidJSONContract reports an invalid tool or output schema.
	ErrInvalidJSONContract = errors.New("request JSON contract is invalid")
)
View Source
var (
	// ErrStoreRequired reports an absent response-cache byte store.
	ErrStoreRequired = errors.New("response-cache store is required")
	// ErrCorruptRecord reports invalid durable response-cache data.
	ErrCorruptRecord = errors.New("response-cache record is invalid")
	// ErrKindMismatch reports a record payload that does not match its kind.
	ErrKindMismatch = errors.New("response-cache record kind does not match")
)
View Source
var (
	// ErrNoStreamEvents reports an empty canonical event sequence.
	ErrNoStreamEvents = errors.New("stream has no canonical events")
	// ErrUnsupportedContent reports content that canonical replay cannot represent.
	ErrUnsupportedContent = errors.New("cached stream content is not text")
)

Functions

func ChatKey

func ChatKey(identity ChatIdentity) (string, error)

ChatKey returns the versioned semantic key for an eligible chat request.

func CompleteStream

func CompleteStream(events []inference.StreamEvent) (inference.ChatResponse, error)

CompleteStream builds one canonical completed result from stream events.

func EmbeddingKey

func EmbeddingKey(identity EmbeddingIdentity) (string, error)

EmbeddingKey returns the versioned semantic key for an eligible embedding request.

func StreamEvents

func StreamEvents(response inference.ChatResponse, options inference.StreamOptions) ([]inference.StreamEvent, error)

StreamEvents reconstructs canonical stream events from a completed result.

Types

type ChatIdentity

type ChatIdentity struct {
	TenantID          string
	CatalogGeneration string
	Request           inference.ChatRequest
	Policy            Policy
}

ChatIdentity contains all semantic inputs for one chat cache entry.

type Clock

type Clock interface {
	Now() time.Time
}

Clock supplies deterministic record time.

type EmbeddingIdentity

type EmbeddingIdentity struct {
	TenantID          string
	CatalogGeneration string
	Request           inference.EmbeddingRequest
	Policy            Policy
}

EmbeddingIdentity contains all semantic inputs for one embedding cache entry.

type Policy

type Policy struct {
	Provider ProviderPolicy `json:"provider"`
	Tenant   TenantPolicy   `json:"tenant"`
}

Policy contains all routing policy that can change a response.

type ProviderPolicy

type ProviderPolicy struct {
	Order          []string          `json:"order,omitempty"`
	Only           []string          `json:"only,omitempty"`
	Ignore         []string          `json:"ignore,omitempty"`
	AllowFallbacks bool              `json:"allow_fallbacks"`
	Route          string            `json:"route,omitempty"`
	ModelOverrides map[string]string `json:"model_overrides,omitempty"`
}

ProviderPolicy contains request-scoped provider routing semantics.

type Repository

Repository stores versioned canonical inference results.

func Open

func Open(store Store, clock Clock) (Repository, error)

Open creates a canonical response-cache repository.

type Store

type Store interface {
	GetResponse(context.Context, string) ([]byte, bool, error)
	SetResponse(context.Context, string, []byte) error
}

Store is the byte-cache contract required by the repository.

type TenantPolicy

type TenantPolicy struct {
	AllowedModels      []string `json:"allowed_models,omitempty"`
	AllowedProviders   []string `json:"allowed_providers,omitempty"`
	RateLimitTier      string   `json:"rate_limit_tier,omitempty"`
	CredentialStrategy string   `json:"credential_strategy,omitempty"`
}

TenantPolicy contains tenant-scoped route restrictions.

Jump to

Keyboard shortcuts

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