reflex

package
v0.22.0 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2026 License: MIT Imports: 14 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 reflex holds the agent's reflexes: standing rules that watch the conversation and the app, and act when their trigger fires.

A reflex is a trigger and an action. Triggers:

text       {"pattern": "(?i)triage"}       the owner's message matches a regular expression
predicate  {...}                          a libs agent/core/reflexes predicate over the conversation
event      {"topic": "notes.saved"}        an event on the app's bus (prefix match)
focus      {"view": "notes"}               a window of that view comes in front

Actions:

remind       {"body": "..."}                       told to the agent with the next message
halt         {"message": "..."}                    the message is answered with this; the agent is not called
run_command  {"command": "...", "args": {...}}     an app command is run
call_tool    {"tool": "...", "input": {...}}       a tool is called (read and act levels only)

Reflexes live in the core database, so the owner and the Interaction Builder can add, change and switch them on without a restart. A reflex is on when Enabled is set, or while a layout whose Setup["reflexes"] lists its name is in use.

Index

Constants

View Source
const (
	TriggerText      = "text"
	TriggerPredicate = "predicate"
	TriggerEvent     = "event"
	TriggerFocus     = "focus"
)

The trigger kinds.

View Source
const (
	ActionRemind     = "remind"
	ActionHalt       = "halt"
	ActionRunCommand = "run_command"
	ActionCallTool   = "call_tool"
)

The action kinds.

Variables

This section is empty.

Functions

This section is empty.

Types

type Doer

type Doer interface {
	RunCommand(ctx context.Context, id string, args map[string]any) (any, error)
	// CallTool calls a tool by name. It refuses tools above the act level: a reflex does
	// not get to skip the owner's say on a change.
	CallTool(ctx context.Context, name string, input json.RawMessage) (any, error)
}

Doer carries out the actions that reach into the app. The agent service supplies it.

type Message

type Message struct {
	Text string
	// Prior is what came before, most recent first.
	Prior []libreflex.MessageSignal
	Sit   module.Situation
}

Message is the owner's message and the conversation before it, for the triggers.

type Outcome

type Outcome struct {
	// Reminders are told to the agent with the message.
	Reminders []string
	// Halt, when set, answers the message instead of the agent.
	Halt string
	// Fired names the reflexes that fired.
	Fired []string
}

Outcome is what the reflexes did to one message.

type Reflex

type Reflex struct {
	Name        string          `json:"name"`
	About       string          `json:"about,omitempty"`
	TriggerKind string          `json:"trigger_kind"`
	Trigger     json.RawMessage `json:"trigger"`
	ActionKind  string          `json:"action_kind"`
	Action      json.RawMessage `json:"action"`
	// Cooldown is how long, in seconds, it stays quiet after firing. 0: no wait.
	Cooldown int `json:"cooldown_seconds,omitempty"`
	// Enabled turns it on everywhere. Off, it is on only in a layout that lists it.
	Enabled   bool   `json:"enabled"`
	LastFired string `json:"last_fired,omitempty"`
	Fired     int    `json:"fired,omitempty"`
}

Reflex is one reflex.

func (Reflex) Validate

func (r Reflex) Validate() error

Validate checks a reflex's shape.

type Set

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

Set is the app's reflexes.

func Open

func Open(ctx context.Context, db *sql.DB, doer Doer, log func(msg string, args ...any)) (*Set, error)

Open migrates the reflex table into db.

func (*Set) Active

func (s *Set) Active(ctx context.Context, inLayout []string) []Reflex

Active returns the reflexes that are on now: the enabled ones, and those the layout in use lists (inLayout).

func (*Set) ApplyAction added in v0.4.0

func (s *Set) ApplyAction(ctx context.Context, kind string, spec json.RawMessage)

ApplyAction executes a runtime-selected host action with the same level checks as local reflexes.

func (*Set) Delete

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

Delete removes a reflex.

func (*Set) Get

func (s *Set) Get(ctx context.Context, name string) (Reflex, bool, error)

Get returns one reflex.

func (*Set) List

func (s *Set) List(ctx context.Context) ([]Reflex, error)

List returns every reflex, by name.

func (*Set) OnEvent

func (s *Set) OnEvent(ctx context.Context, ev module.Event, focusView string, inLayout []string)

OnEvent puts an app event to the active event and focus reflexes. focusView is the view that came in front when the event is a focus change, else "".

func (*Set) OnMessage

func (s *Set) OnMessage(ctx context.Context, m Message, inLayout []string) Outcome

OnMessage puts a message to the active reflexes and carries out what fires. Reminders left by event and focus reflexes since the last message are given too.

func (*Set) Put

func (s *Set) Put(ctx context.Context, r Reflex) error

Put adds a reflex or replaces the one of the same name (keeping its firing record).

func (*Set) RecordFired added in v0.4.0

func (s *Set) RecordFired(ctx context.Context, names []string)

RecordFired updates the UI's firing record after the runtime matches reflexes.

func (*Set) SetEnabled

func (s *Set) SetEnabled(ctx context.Context, name string, on bool) error

SetEnabled switches a reflex on or off everywhere.

Jump to

Keyboard shortcuts

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