themegen

package
v1.6.0 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2026 License: MIT Imports: 10 Imported by: 2 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package themegen is the AI theme generator module's domain layer: the brief, theme, and validation value types, the generation/store ports, and the envelope parser that validates the model's JSON output. Provider adapters (the OpenAI-compatible client) live in infrastructure; the filesystem store lives in store; neither is imported by the domain.

Index

Constants

View Source
const (
	// FindingSeverityError blocks install and activation.
	FindingSeverityError = "error"
	// FindingSeverityWarning surfaces in the UI without blocking.
	FindingSeverityWarning = "warning"
)

Variables

View Source
var (
	// ErrInvalidBrief reports an empty or over-long generation brief.
	ErrInvalidBrief = errors.New("invalid brief")
	// ErrInvalidThemeName reports a theme name outside the slug grammar.
	ErrInvalidThemeName = errors.New("invalid theme name")
	// ErrPathNotAllowed reports a theme file path outside the allowlist.
	ErrPathNotAllowed = errors.New("path not allowed")
	// ErrSizeLimitExceeded reports a theme file or total exceeding the caps.
	ErrSizeLimitExceeded = errors.New("size limit exceeded")
	// ErrGenerationFailed reports an AI generation or repair failure.
	ErrGenerationFailed = errors.New("generation failed")
	// ErrThemeExists reports an install colliding with an existing theme dir.
	ErrThemeExists = errors.New("theme exists")
	// ErrThemeNotFound reports a lookup for a missing theme.
	ErrThemeNotFound = errors.New("theme not found")
	// ErrThemeActive reports a delete while the theme is active.
	ErrThemeActive = errors.New("theme active")
	// ErrActivationFailed reports a validation or config-write activation failure.
	ErrActivationFailed = errors.New("activation failed")
	// ErrGenerationUnavailable reports generation without an AI key.
	ErrGenerationUnavailable = errors.New("generation unavailable")
)

Functions

func ValidateBrief

func ValidateBrief(brief Brief) error

ValidateBrief enforces the generation brief contract: non-empty and capped at 5,000 runes; style is a free-form preset hint.

func ValidateThemeFiles

func ValidateThemeFiles(files map[string]string) error

ValidateThemeFiles enforces the v1 theme contract: at least one of static/base.css or static/style.css, allowlisted paths only, per-file 512 KiB and 2 MiB total caps.

func ValidateThemeName

func ValidateThemeName(name string) error

ValidateThemeName reports whether name is an installable theme slug: lowercase alphanumerics and dashes, 2–50 runes, not reserved.

Types

type ActiveThemeState

type ActiveThemeState interface {
	ActiveTheme(ctx context.Context) string
}

ActiveThemeState resolves the currently active installed theme: the [theme] active value the edition composes over the embedded default.

type Brief

type Brief struct {
	Description       string
	Style             string
	TemplateOverrides bool
}

Brief is the natural-language theme request from the admin UI.

func NewBrief

func NewBrief(description, style string, templateOverrides bool) Brief

NewBrief assembles a generation brief from its parts.

type Edition

type Edition string

Edition selects which embedded theme the generator derives from.

const (
	// EditionBlog renders the blog content site with theme-default.
	EditionBlog Edition = "blog"
	// EditionStorefront renders the commerce content site with theme-storefront.
	EditionStorefront Edition = "storefront"
)

type Finding

type Finding struct {
	Rule     string
	Severity string
	Message  string
}

Finding is one validation result: a rule hit with severity and detail. Errors block install and activation; warnings surface in the UI only.

func NewFinding

func NewFinding(rule, severity, message string) Finding

NewFinding assembles a validation finding.

func ScanGeneratedThemeFiles

func ScanGeneratedThemeFiles(files map[string]string) []Finding

ScanGeneratedThemeFiles flags executable content a generated theme must not ship: script elements, inline event handlers, and javascript: URLs in renderable markup, plus any root/ file (served from the site origin). It runs only on the generation path — manual operator themes are trusted and pass through boot, reload, and activation with structural validation alone — so every finding means the model added something the repair loop must remove. Paths iterate sorted so findings (and repair prompts) are deterministic.

type InstalledTheme

type InstalledTheme struct {
	Name      string
	Edition   Edition
	CreatedAt time.Time
	Active    bool
	Source    string
}

InstalledTheme is the manifest projection the list endpoint serves: identity, edition, creation time, active marker, and provenance.

func NewInstalledTheme

func NewInstalledTheme(
	name string,
	edition Edition,
	createdAt time.Time,
	active bool,
	source string,
) InstalledTheme

NewInstalledTheme assembles an installed-theme projection.

type Service

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

Service orchestrates theme generation: brief validation, AI generation, validation gate with a bounded repair loop, and auto-install as inactive.

func NewService

func NewService(
	generation ThemeGenerationService,
	validator ThemeValidator,
	store ThemeStore,
	sources func(ctx context.Context, edition Edition) ThemeSources,
	edition Edition,
) *Service

NewService builds the orchestration service. A nil store disables persistence (generation still validates); a nil sources func generates from the distilled contract alone.

func (*Service) Delete

func (s *Service) Delete(ctx context.Context, name string) error

Delete removes an installed theme.

func (*Service) Generate

func (s *Service) Generate(
	ctx context.Context,
	brief Brief,
	edition Edition,
) (Theme, ValidationReport, error)

Generate validates the brief, asks the model, validates the result, and installs it as inactive. A failing result is sent back to the model up to maxRepairAttempts times with its findings; a still-failing theme is refused. The final report is returned alongside the theme even on success so the UI can surface warnings.

func (*Service) Get

func (s *Service) Get(ctx context.Context, name string) (Theme, error)

Get returns one installed theme by name.

func (*Service) List

func (s *Service) List(ctx context.Context) ([]InstalledTheme, error)

List returns the installed-theme projections.

func (*Service) Validate

func (s *Service) Validate(ctx context.Context, theme Theme) ValidationReport

Validate runs the validation gate without persisting.

func (*Service) Zip

func (s *Service) Zip(ctx context.Context, name string) ([]byte, error)

Zip builds the download archive for one installed theme.

type Theme

type Theme struct {
	Name      string
	Edition   Edition
	Files     map[string]string
	Rationale string
}

Theme is a validated generated theme: an installable slug plus the relative-path → content file map the store persists.

func NewTheme

func NewTheme(
	name string,
	edition Edition,
	files map[string]string,
	rationale string,
) Theme

NewTheme assembles a theme from its validated parts.

func ParseThemeEnvelope

func ParseThemeEnvelope(responseText string, edition Edition) (Theme, error)

ParseThemeEnvelope validates the model's JSON envelope and splits it into a Theme. The name and files run the slug and allowlist validators; markdown fences and surrounding commentary are rejected.

type ThemeGenerationService

type ThemeGenerationService interface {
	GenerateTheme(ctx context.Context, brief Brief, edition Edition, sources ThemeSources) (Theme, error)
	RepairTheme(ctx context.Context, brief Brief, sources ThemeSources, theme Theme, problems []string) (Theme, error)
}

ThemeGenerationService generates theme files from a brief. The edition selects the source theme; sources carries the readable base material (stylesheets and, for template overrides, page templates) the prompts ground on. RepairTheme feeds validation problems back to the model and receives the same brief and sources so the contract stays identical.

type ThemeManifest

type ThemeManifest struct {
	Name      string    `json:"name"`
	Edition   string    `json:"edition"`
	Generator string    `json:"generator"`
	CreatedAt time.Time `json:"createdAt"`
	Source    string    `json:"source"`
}

ThemeManifest is the on-disk theme.json metadata: identity and provenance. Active state lives in config, never in the manifest.

func NewThemeManifest

func NewThemeManifest(
	name, edition, generator string,
	createdAt time.Time,
	source string,
) ThemeManifest

NewThemeManifest assembles a manifest for an installed theme.

type ThemeSources

type ThemeSources struct {
	CSS       string
	Templates string
}

ThemeSources is the edition's readable prompt grounding: the stylesheet sources always, plus the template page sources when the brief asks for template overrides.

func NewThemeSources

func NewThemeSources(css, templates string) ThemeSources

NewThemeSources assembles the edition's readable prompt grounding.

type ThemeStore

type ThemeStore interface {
	Install(ctx context.Context, theme Theme) error
	List(ctx context.Context) ([]InstalledTheme, error)
	Get(ctx context.Context, name string) (Theme, error)
	Delete(ctx context.Context, name string) error
	Zip(ctx context.Context, name string) ([]byte, error)
}

ThemeStore persists installed themes on disk and builds download zips.

type ThemeValidator

type ThemeValidator interface {
	Validate(ctx context.Context, theme Theme) ValidationReport
}

ThemeValidator checks a generated theme before install and again before activation: size caps, CSS parse, and url() policy.

func NewValidator

func NewValidator() ThemeValidator

NewValidator builds the default theme validator.

type ValidationReport

type ValidationReport struct {
	Valid    bool
	Findings []Finding
}

ValidationReport is the outcome of validating a theme before install or activation. Valid is false when any error-severity finding exists.

func NewFindingsReport

func NewFindingsReport(findings []Finding) ValidationReport

NewFindingsReport assembles a report whose Valid is derived from the findings: false when any error-severity finding exists. Decorators and whole-pipeline validators use it so severity handling stays in one place.

func NewValidationReport

func NewValidationReport(valid bool, findings []Finding) ValidationReport

NewValidationReport assembles a validation report.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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