Documentation
¶
Overview ¶
Package heartbeat has prompts for sessions, which fire once, at a time, or again and again, on a schedule, such as a briefing every morning, or a reminder. A heartbeat which fires is run, with the function WithRun sets, once, on the server which fires it, with its prompt, and the session it's for. A schedule is in a time zone, or the person's, which it follows, as it changes.
Index ¶
- Constants
- func Clone(heartbeat schema.Heartbeat) schema.Heartbeat
- func Sort(heartbeats []schema.Heartbeat)
- type Heartbeats
- func (h *Heartbeats) Create(ctx context.Context, meta schema.HeartbeatMeta) (_ schema.Heartbeat, err error)
- func (h *Heartbeats) Delete(ctx context.Context, id uuid.UUID) (_ schema.Heartbeat, err error)
- func (h *Heartbeats) DeleteSession(ctx context.Context, session uuid.UUID) (_ int, err error)
- func (*Heartbeats) Description() string
- func (h *Heartbeats) Get(ctx context.Context, id uuid.UUID) (_ schema.Heartbeat, err error)
- func (h *Heartbeats) List(ctx context.Context, req schema.HeartbeatListRequest) (_ schema.HeartbeatList, err error)
- func (*Heartbeats) Name() string
- func (*Heartbeats) Notify(func(eventschema.EventAction, string))
- func (h *Heartbeats) Run(ctx context.Context, logger *slog.Logger) error
- func (h *Heartbeats) SystemPrompt() string
- func (h *Heartbeats) Tools() []llm.Tool
- func (h *Heartbeats) Update(ctx context.Context, id uuid.UUID, req schema.HeartbeatRequest) (_ schema.Heartbeat, err error)
- type Opt
- type RunFunc
- type SessionFunc
- type Store
Constants ¶
const ( // Name is the schema of the heartbeats' events, and the namespace of // their tools Name = "heartbeat" // HeartbeatTable is the table of the events of the heartbeats, whose // rows are their identifiers HeartbeatTable = "heartbeat" // DefaultRetention is how long a heartbeat which won't fire again is // kept, after it last fired, or was changed, before it's deleted DefaultRetention = 7 * 24 * time.Hour )
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Heartbeats ¶
type Heartbeats struct {
// contains filtered or unexported fields
}
Heartbeats are the heartbeats, which are kept in a store, and which fire, as Run runs. It's safe to use concurrently, and by more than one server, which share a store, so a heartbeat fires on only one of them.
func New ¶
func New(store Store, opts ...Opt) (*Heartbeats, error)
New returns the heartbeats in the store, with the options, such as the broadcaster they publish their changes to, and the person's time zone. Returns ErrBadParameter when there's no store, or an option isn't valid.
func (*Heartbeats) Create ¶
func (h *Heartbeats) Create(ctx context.Context, meta schema.HeartbeatMeta) (_ schema.Heartbeat, err error)
Create creates the heartbeat, publishes that it was, and returns it, with when it fires next. Returns ErrBadParameter when it isn't valid, or its time has passed, and the store's errors.
func (*Heartbeats) Delete ¶
Delete deletes the heartbeat with the identifier, publishes that it was, and returns it, as it was, or ErrNotFound when there's none
func (*Heartbeats) DeleteSession ¶
DeleteSession deletes the heartbeats for the session, such as when it's deleted, publishing that they were, and returns how many there were
func (*Heartbeats) Description ¶
func (*Heartbeats) Description() string
Description returns what the heartbeats are
func (*Heartbeats) Get ¶
Get returns the heartbeat with the identifier, with when it fires next, or ErrNotFound when there's none
func (*Heartbeats) List ¶
func (h *Heartbeats) List(ctx context.Context, req schema.HeartbeatListRequest) (_ schema.HeartbeatList, err error)
List returns the heartbeats, or those for the request's session, with when they fire next, the soonest first, and those which won't fire again last, of the request's page, and how many there are. Returns ErrBadParameter when the session isn't a UUID.
func (*Heartbeats) Name ¶
func (*Heartbeats) Name() string
func (*Heartbeats) Notify ¶
func (*Heartbeats) Notify(func(eventschema.EventAction, string))
Notify does nothing, as the tools don't change
func (*Heartbeats) Run ¶
Run fires the heartbeats which are due, when it starts, and every ten seconds, from the start of each minute, until the context is cancelled, when it returns nil. A heartbeat which was due more than once, such as when the server wasn't running, fires once, when it was first due. With a broadcaster, it deletes the heartbeats for each session which is deleted, and, with WithSessions, a heartbeat whose session doesn't exist when it's due, as the event may have been missed, is deleted, rather than fired. A heartbeat which won't fire again, such as a one-off which fired, is deleted once it's been kept for a week, or the time WithRetention sets, after it last fired, or was changed, which is looked for when it starts, and daily. What fails, such as a store which can't be read, is logged to the logger, when there is one.
func (*Heartbeats) SystemPrompt ¶
func (h *Heartbeats) SystemPrompt() string
SystemPrompt returns when to schedule heartbeats, and how, and the time zone their schedules are in, unless they have their own, without the time, which the tool's results have, so it doesn't change, and is cached
func (*Heartbeats) Tools ¶
func (h *Heartbeats) Tools() []llm.Tool
Tools returns the heartbeats' tool
func (*Heartbeats) Update ¶
func (h *Heartbeats) Update(ctx context.Context, id uuid.UUID, req schema.HeartbeatRequest) (_ schema.Heartbeat, err error)
Update changes the heartbeat with the identifier, as the request says, publishes that it was, and returns it, with when it fires next. A heartbeat whose time is changed fires again. Returns ErrBadParameter when it isn't valid, or its new time has passed, ErrNotFound when there's none, and the store's errors.
type Opt ¶
type Opt func(*opt) error
Opt is an option of the heartbeats
func WithBroadcaster ¶
func WithBroadcaster(b *broadcaster.Broadcaster) Opt
WithBroadcaster sets the broadcaster which the heartbeats publish their changes to, including when one fires, as when it fired is changed, whose schema is Name, whose table is HeartbeatTable, and whose rows are their identifiers. Run watches it for the sessions which are deleted, whose heartbeats are deleted with them.
func WithRetention ¶ added in v1.1.0
WithRetention sets how long a heartbeat which won't fire again, such as a one-off which fired, is kept, after it last fired, or was changed, before it's deleted, which is a week, unless it's set. Returns ErrBadParameter when it isn't positive.
func WithRun ¶ added in v1.1.0
WithRun sets the function which runs each heartbeat which fires, once, on the server which fires it, or none, so heartbeats fire, and nothing runs
func WithSessions ¶
func WithSessions(fn SessionFunc) Opt
WithSessions sets the function which says whether a session exists, so a heartbeat is only created for one which does, and one whose session was deleted is deleted, rather than fired, such as when the event that it was deleted was missed
func WithTracer ¶
WithTracer sets the tracer which traces the heartbeats' changes, or none, when it's nil
type RunFunc ¶ added in v1.1.0
RunFunc runs a heartbeat which fired, such as by asking a model its prompt, in its session, which is called in a goroutine of its own, so a run which takes a while doesn't hold up the others. What it returns is logged.
type SessionFunc ¶
SessionFunc returns nil when the session with the identifier exists, or ErrNotFound when it doesn't
type Store ¶
type Store interface {
// Create adds the heartbeat, or returns ErrConflict when there's one
// with its identifier
Create(context.Context, schema.Heartbeat) error
// Get returns the heartbeat with the identifier, or ErrNotFound when
// there's none
Get(context.Context, uuid.UUID) (schema.Heartbeat, error)
// List returns the heartbeats, in any order
List(context.Context) ([]schema.Heartbeat, error)
// Update calls the function with the heartbeat with the identifier, which
// it changes, and replaces it with what the function changed it to, and
// returns it, so no other change, such as by another server, is made
// between them, or returns the function's error, without changing it, or
// ErrNotFound when there's none
Update(context.Context, uuid.UUID, func(*schema.Heartbeat) error) (schema.Heartbeat, error)
// Delete deletes the heartbeat with the identifier, and returns it, as it
// was, or ErrNotFound when there's none. With a check, it's called with
// the heartbeat, as it is, and it's deleted only when the check returns
// nil, so no other change, such as by another server, is made between
// them, or the check's error is returned, without deleting it.
Delete(ctx context.Context, id uuid.UUID, check func(schema.Heartbeat) error) (schema.Heartbeat, error)
}
Store keeps the heartbeats, such as in files, or a database, so they're kept when the server restarts. A store which servers share changes a heartbeat at once, with Update, so only one of them fires it.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package cmd has the heartbeats' commands and flags
|
Package cmd has the heartbeats' commands and flags |
|
Package httpclient is a client of the heartbeats' HTTP API
|
Package httpclient is a client of the heartbeats' HTTP API |
|
Package httphandler has the heartbeats' HTTP API, which lists, creates, gets, changes and deletes them
|
Package httphandler has the heartbeats' HTTP API, which lists, creates, gets, changes and deletes them |
|
Package schema has the heartbeats' schedules, and what they are: a prompt for a session, at a time, or on a schedule
|
Package schema has the heartbeats' schedules, and what they are: a prompt for a session, at a time, or on a schedule |