settingsctl

package
v0.16.397 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2026 License: AGPL-3.0 Imports: 16 Imported by: 0 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 settingsctl lets an agent change harnessly's settings when a person asks it to. It has three parts:

  • the catalog: every setting an agent may read and propose a change of, with a title, a description, its kind, the values it takes and the value it has now;
  • the proposals: an agent of the Settings window's own chat changes a setting at once; an agent of any other chat only proposes, and the change is made when the person says yes on the card the chat shows;
  • the journal: every change (by a person or by an agent) with the old and the new value, who made it and when, so that the Settings window can show what changed.

A value is a string everywhere: an enum's value, "on"/"off" for a switch, digits for a number. The catalog holds no secret: a key of a provider is no setting.

Index

Constants

View Source
const (
	On  = "on"
	Off = "off"
)

The values of a switch.

View Source
const (
	ReasonUnknownKey  = "unknown-setting" // no setting of that key
	ReasonBadValue    = "bad-value"       // the setting does not take the value
	ReasonSame        = "same"            // the setting already has the value
	ReasonNotFound    = "not-found"       // no proposal of that id
	ReasonClosed      = "closed"          // the proposal is answered already
	ReasonTooMany     = "too-many"        // too many proposals wait for an answer
	ReasonRateLimited = "rate-limited"    // too many proposals from one chat
	ReasonFailed      = "failed"
)

Reasons of a refusal.

View Source
const (
	ByPerson = "person"
	ByAgent  = "agent"
)

Who made a change.

View Source
const (
	// ViaPage: the person changed it on the Settings screen.
	ViaPage = "page"
	// ViaSettingsChat: the agent of the Settings window's chat changed it at once.
	ViaSettingsChat = "settings-chat"
	// ViaConfirmed: an agent of another chat proposed it and the person said yes.
	ViaConfirmed = "confirmed"
)

How a change was made.

View Source
const (
	StateWaiting  = "waiting"
	StateApplied  = "applied"
	StateDeclined = "declined"
	StateExpired  = "expired"
)

The states of a proposal.

View Source
const JournalKept = 500

JournalKept is how many changes the journal keeps; the oldest go first.

View Source
const (

	// MaxWhy is the longest reason an agent gives.
	MaxWhy = 300
)

Variables

This section is empty.

Functions

func CleanWhy

func CleanWhy(why string) string

CleanWhy is an agent's reason as one line.

Types

type Actor

type Actor struct {
	Kind string `json:"kind"`
	// Conv and Title are the agent's chat (its title as it was then); empty for a person.
	Conv  string `json:"conv,omitempty"`
	Title string `json:"title,omitempty"`
}

Actor is who made a change: a person, or the agent of a chat.

type Change

type Change struct {
	ID    string `json:"id"`
	At    int64  `json:"at"`
	Key   string `json:"key"`
	Title string `json:"title"`
	// Old and New are the values; OldLabel and NewLabel the same in words.
	Old      string `json:"old"`
	New      string `json:"new"`
	OldLabel string `json:"oldLabel"`
	NewLabel string `json:"newLabel"`
	By       Actor  `json:"by"`
	Via      string `json:"via"`
	// Why is the agent's one line of why it changed the setting.
	Why string `json:"why,omitempty"`
	// Note, when not empty, says that the value was not set here but passed on, and what it waits for.
	Note string `json:"note,omitempty"`
}

Change is one line of the journal.

type Item

type Item struct {
	Key, Section, Title, Description string
	Kind                             Kind
	// Options are the values of an enum, asked each time (the providers and their models change).
	Options  func() []Option
	Min, Max int
	Get      func() (string, error)
	// Set is given a value that passed the check of its kind.
	Set func(value string) error
	// SetBy, when set, is called in place of Set and is also told who asks (a change that is made elsewhere, in the
	// hub, says the chat it comes from). Its note, when not empty, says what became of the value in place of a
	// change made here: it was passed on and waits for someone else's yes.
	SetBy func(value string, by Actor) (note string, err error)
	// Free lets an enum take a value that is none of Options when it says so (a model the provider does not list);
	// for a text it is the whole check.
	Free func(value string) bool
	// Why says in words why a value was not taken, when the setting can tell; nil or "": the common words.
	Why func(value string) string
	// Label says a text's value in words for the person (the card of a change, the journal); nil: the value itself.
	Label func(value string) string
	// Replaces says what a value that changes a part of a text takes the place of, in words ("" when it only adds);
	// part is false when the value is the whole text. nil: every value is the whole.
	Replaces func(value string) (label string, part bool)
}

Item is a setting with the way to read and to change it.

type Journal

type Journal struct {
	Path string
	// contains filtered or unexported fields
}

Journal is the changes of the settings in a file, a JSON line each, the newest last.

func (*Journal) Append

func (j *Journal) Append(c Change) error

Append adds a change. A journal past JournalKept is written anew without its oldest lines.

func (*Journal) List

func (j *Journal) List(limit int, key string) []Change

List is the last changes, the newest first: at most limit of them (all the kept ones when limit is 0), of one setting when key is given.

type Kind

type Kind string

Kind is what values a setting takes.

const (
	// KindEnum: one of Options.
	KindEnum Kind = "enum"
	// KindBool: "on" or "off".
	KindBool Kind = "bool"
	// KindNumber: a whole number from Min to Max.
	KindNumber Kind = "number"
	// KindText: a text the setting itself checks (Free), such as a description in JSON.
	KindText Kind = "text"
)

type Option

type Option struct {
	Value string `json:"value"`
	Label string `json:"label"`
}

Option is one value of an enum, with its words for the person.

type Outcome

type Outcome struct {
	// State: applied (the setting is changed), waiting (the person has not answered yet), declined, expired.
	State string `json:"status"`
	// Change is the journal line of an applied change.
	Change *Change `json:"change,omitempty"`
	// Proposal is the card the person answers; absent for a change of the Settings window's chat, made at once.
	Proposal *Proposal `json:"proposal,omitempty"`
}

Outcome is what an agent's proposal came to.

type Proposal

type Proposal struct {
	ID       string `json:"id"`
	At       int64  `json:"at"`
	Key      string `json:"key"`
	Title    string `json:"title"`
	Old      string `json:"old"`
	New      string `json:"new"`
	OldLabel string `json:"oldLabel"`
	NewLabel string `json:"newLabel"`
	Why      string `json:"why,omitempty"`
	// Conv and ConvTitle are the chat whose agent asks; the card is shown there.
	Conv      string `json:"conv"`
	ConvTitle string `json:"convTitle"`
	State     string `json:"state"`
	// Failed says why a change the person agreed to was not made, in the setting's words; "" for one that was not
	// tried or was made. Such a proposal is declined: nothing changed.
	Failed string `json:"failed,omitempty"`
}

Proposal is a change an agent asks the person to confirm: the card of the chat.

type Refusal

type Refusal struct {
	Reason  string
	Setting *Setting
	Err     error
	// Message is the setting's own words for the refusal, when it has some.
	Message string
}

Refusal is a reason the caller is told; Setting is the setting it is about when there is one, so that the caller can be shown what values it takes.

func RefusalOf

func RefusalOf(err error) *Refusal

RefusalOf is the refusal of any error of the service.

func (*Refusal) Error

func (r *Refusal) Error() string

func (*Refusal) Unwrap

func (r *Refusal) Unwrap() error

type Said

type Said struct{ Message string }

Said is an error of a setting's Set whose words are for the person and the agent: a change that was refused where it is made (the hub said the person has no right to it).

func (*Said) Error

func (e *Said) Error() string

type Service

type Service struct {
	// Items are the settings, in the order the catalog lists them.
	Items   []Item
	Journal *Journal
	// SettingsChat says whether a chat is the Settings window's own: its agent changes a setting at once.
	SettingsChat func(conv string) bool
	Now          func() time.Time
	// contains filtered or unexported fields
}

Service is the catalog with its proposals and its journal.

func (*Service) Apply

func (s *Service) Apply(key, value string, by Actor, via, why string) (Change, error)

Apply changes a setting now and writes the change into the journal.

func (*Service) Approve

func (s *Service) Approve(id string) (Outcome, error)

Approve is the person's yes: the setting is changed as the agent of the proposal's chat, and the proposal is applied. A change the setting no longer takes (it has the value already, the value is gone) closes the proposal as declined and is told.

func (*Service) Decline

func (s *Service) Decline(id string) (Outcome, error)

Decline is the person's no.

func (*Service) Get

func (s *Service) Get(key string) (Setting, error)

Get is one setting of the catalog.

func (*Service) List

func (s *Service) List() []Setting

List is the catalog with the values of now. A setting that cannot be read is left out.

func (*Service) Pending

func (s *Service) Pending() []Proposal

Pending are the proposals that wait for the person, the oldest first.

func (*Service) Propose

func (s *Service) Propose(key, value, why string, by Actor) (Outcome, error)

Propose is an agent's change of a setting. The agent of the Settings window's chat changes it at once. Any other agent leaves a proposal for the person: the one that waits already for the same change of the same chat, else a new one.

func (*Service) Record

func (s *Service) Record(key, old, value string, by Actor, via string)

Record writes into the journal a change that was made past the catalog (a person saved the Settings screen): the values are told, nothing is set.

func (*Service) Rev

func (s *Service) Rev() uint64

Rev is the revision of the journal and the proposals: it moves with every change of either.

func (*Service) Touch

func (s *Service) Touch()

Touch moves the revision without a change of a setting: a page that watches reads what else it watches (the agents' looks, cmd/runtime routes_looks.go).

func (*Service) Wait

func (s *Service) Wait(ctx context.Context, id string, d time.Duration) Outcome

Wait is what the proposal came to after at most d (or ctx's end).

func (*Service) WaitRev

func (s *Service) WaitRev(ctx context.Context, after uint64, d time.Duration) uint64

WaitRev answers the revision as soon as it is not `after`, or after d when nothing moved.

type Setting

type Setting struct {
	Key string `json:"key"`
	// Section is the part of Settings it belongs to, in words.
	Section     string   `json:"section"`
	Title       string   `json:"title"`
	Description string   `json:"description"`
	Kind        Kind     `json:"kind"`
	Options     []Option `json:"options,omitempty"`
	Min         *int     `json:"min,omitempty"`
	Max         *int     `json:"max,omitempty"`
	// Value is what the setting is now; ValueLabel the same in words.
	Value      string `json:"value"`
	ValueLabel string `json:"valueLabel"`
}

Setting is a setting as the catalog lists it.

Jump to

Keyboard shortcuts

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