heartbeat

package
v1.2.2 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2026 License: Apache-2.0 Imports: 22 Imported by: 1 module, 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 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

View Source
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

func Clone

func Clone(heartbeat schema.Heartbeat) schema.Heartbeat

Clone returns a copy of the heartbeat, whose times are copies, so changing it doesn't change the heartbeat

func Sort

func Sort(heartbeats []schema.Heartbeat)

Sort sorts the heartbeats, those which fire soonest first, and those which won't fire again last, and then by when they were created

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

func (h *Heartbeats) Delete(ctx context.Context, id uuid.UUID) (_ schema.Heartbeat, err error)

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

func (h *Heartbeats) DeleteSession(ctx context.Context, session uuid.UUID) (_ int, err error)

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

func (h *Heartbeats) Get(ctx context.Context, id uuid.UUID) (_ schema.Heartbeat, err error)

Get returns the heartbeat with the identifier, with when it fires next, or ErrNotFound when there's none

func (*Heartbeats) List

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

func (h *Heartbeats) Run(ctx context.Context, logger *slog.Logger) error

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

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

func WithRetention(d time.Duration) Opt

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

func WithRun(fn RunFunc) Opt

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

func WithTracer(tracer trace.Tracer) Opt

WithTracer sets the tracer which traces the heartbeats' changes, or none, when it's nil

func WithZone

func WithZone(fn func() *time.Location) Opt

WithZone sets the function which returns the person's time zone, or nil, when it isn't known, which the schedules without a time zone of their own are in, so they follow the person, as it changes, such as the memory's

type RunFunc added in v1.1.0

type RunFunc func(context.Context, schema.FireEvent) error

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

type SessionFunc func(context.Context, uuid.UUID) error

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.

func NewStore

func NewStore() Store

NewStore returns a store which keeps the heartbeats in memory, so they aren't kept when the process ends, such as for tests, and examples

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

Jump to

Keyboard shortcuts

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