state

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Dec 29, 2024 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultPrompt = `` /* 680-byte string literal not displayed */
View Source
const GenerateThemesPrompt = `` /* 573-byte string literal not displayed */

Variables

This section is empty.

Functions

func CreateConfigIfNotExists

func CreateConfigIfNotExists(path string) error

CreateConfigIfNotExists makes sure that a config file exists. If it doesn't, it is created and populated with the default config.

func CreatePlaylistIfNotExists

func CreatePlaylistIfNotExists(path string) error

CreatePlaylistIfNotExists makes sure that a playlist file exists. If it doesn't, it is created.

Types

type Config

type Config struct {
	DiscordBotToken string `yaml:"discordBotToken,omitempty" env:"DISCORD_BOT_TOKEN"`
	OpenAIKey       string `yaml:"openAiApiKey,omitempty" env:"OPENAI_API_KEY"`

	ExtrapolateWhenEmpty  bool `yaml:"extrapolateWhenEmpty"`
	ExtrapolationLookback int  `yaml:"extrapolationLookback"`

	Prometheus *PrometheusConfig `yaml:"prometheus,omitempty"`

	Prompt       string `yaml:"-"`
	ThemesPrompt string `yaml:"-"`

	LogLevel slog.Level `yaml:"logLevel"`
}

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig returns the default config.

func ReadConfig

func ReadConfig(path string) (*Config, error)

ReadConfig reads a config file from the specified path.

func (*Config) PopulateFromEnvironment

func (c *Config) PopulateFromEnvironment() error

PopulateFromEnvironment populates the config with values from environment variables.

func (*Config) Store

func (c *Config) Store(path string) error

Store stores the config in the specified path. Writes are atomic.

type Entity

type Entity struct {
	// Role is the role of the entity.
	Role Role `json:"role"`
	// ID is a role-specific id that uniquely identifies the entity.
	ID string `json:"id,omitempty"`
	// Name is a human-readable name of the entity.
	Name string `json:"name,omitempty"`
}

type Metrics

type Metrics struct {
	SongsPlayed    prometheus.Counter
	DurationPlayed prometheus.Counter
	ActiveStreams  prometheus.Gauge

	TokensConsumed prometheus.Counter
}

func NewMetrics

func NewMetrics() *Metrics

func (*Metrics) Collect

func (m *Metrics) Collect(c chan<- prometheus.Metric)

Collect implements prometheus.Collector.

func (*Metrics) Describe

func (m *Metrics) Describe(d chan<- *prometheus.Desc)

Describe implements prometheus.Collector.

type Playlist

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

func NewPlaylist

func NewPlaylist() *Playlist

func ReadPlaylist

func ReadPlaylist(path string) (*Playlist, error)

ReadPlaylist reads a playlist file from the specified path.

func (*Playlist) AddEntry

func (p *Playlist) AddEntry(entry PlaylistEntry)

AddEntry adds an entry to the playlist.

func (*Playlist) Clear

func (p *Playlist) Clear()

Clear clears the playlist.

func (*Playlist) Format

func (p *Playlist) Format(format string, n int, reversed bool) (string, error)

Format formats the first n entries of the playlist using the specified format template.

The template has the following values exposed:

  • Index: index of the entry
  • EntityName: name of the entity that added the entry
  • Title: title of the entry
  • RelativeTime: duration since the entry was added

func (*Playlist) MarshalJSON

func (p *Playlist) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (*Playlist) PeakBackN

func (p *Playlist) PeakBackN(n int) []PlaylistEntry

PeakBackN returns at most n entries from the back of the playlist.

func (*Playlist) Pop

func (p *Playlist) Pop() (PlaylistEntry, bool)

Pop removes and returns the top entry.

func (*Playlist) PopN

func (p *Playlist) PopN(n int) []PlaylistEntry

PopN returns at most n entries from the front of the playlist, removing them in the process.

func (*Playlist) Push

func (p *Playlist) Push(entry PlaylistEntry)

Push the entry to the back of the playlist.

func (*Playlist) PushFront

func (p *Playlist) PushFront(entry PlaylistEntry)

PushFront pushes the entry to the front of the playlist.

func (*Playlist) Store

func (p *Playlist) Store(path string) error

Store stores the playlist in the specified path. Writes are atomic.

func (*Playlist) UnmarshalJSON

func (p *Playlist) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler.

type PlaylistEntry

type PlaylistEntry struct {
	// Time is the time the entry was added to the playlist.
	Time time.Time `json:"time"`
	// Title is the title of the playlist.
	Title string `json:"title"`
	// AddedBy holds information on who added the entry to the playlist.
	AddedBy Entity `json:"addedBy"`
	// Source defines the source from which the entry can be found.
	Source Source `json:"source"`
	// URI is a source-specific URI that uniquely refers to the entry.
	URI string `json:"uri"`
}

type PrometheusConfig

type PrometheusConfig struct {
	Enabled bool   `yaml:"enabled"`
	Port    uint16 `yaml:"port"`
}

type Role

type Role string
const (
	RoleUser   Role = "user"
	RoleSystem Role = "system"
)

type Source

type Source string
const (
	SourceYouTube Source = "youtube"
)

type State

type State struct {
	Config *Config

	Queue *Playlist

	Suggestions *Playlist

	History *Playlist

	Metrics *Metrics
	// contains filtered or unexported fields
}

State holds application state.

func LoadOrInit

func LoadOrInit(basePath string) (*State, error)

LoadOrInit loads the state from the specified base path. If the state is not initialized (i.e. config files etc. not created), the state is initialized.

func (*State) Store

func (s *State) Store() error

Store stores the state to the same location it was read from.

Jump to

Keyboard shortcuts

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