webhook

package
v0.1.0-beta.1 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2026 License: Apache-2.0 Imports: 22 Imported by: ? 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 webhook sends a workspace's events to its HTTP endpoints: a task waiting on someone, an answer, a run that finished or failed, an agent going online or offline.

Events come from the activity log, so nothing is missed while a receiver is down and several replicas don't send twice: a cursor (locked while it moves) turns new log entries into delivery rows, and senders claim due rows with SKIP LOCKED. Each request is signed (see Sign and Verify) and retried with backoff.

Index

Constants

View Source
const (
	TaskWaiting    = "task.waiting"
	TaskAnswered   = "task.answered"
	RunCompleted   = "run.completed"
	RunFailed      = "run.failed"
	AddressOnline  = "address.online"
	AddressOffline = "address.offline"
	// Ping is sent by "Send test"; endpoints always get it.
	Ping = "ping"
)

Event types.

View Source
const (
	SignatureHeader = "Lagrange-Signature"
	EventHeader     = "Lagrange-Event"
	DeliveryHeader  = "Lagrange-Delivery"
)

Headers on every delivery.

Variables

View Source
var ErrBadSignature = errors.New("webhook: bad signature")

ErrBadSignature means a delivery's signature doesn't match, or is too old.

Types lists the event types an endpoint can subscribe to.

Functions

func NewClient

func NewClient(allowPrivate bool) *http.Client

NewClient returns the client deliveries use. Unless allowPrivate, it refuses to connect to loopback, private, link-local and unspecified addresses, checked on the resolved IP of every connection (so DNS tricks and redirects can't reach internal services), as the A2A spec asks of push notifications (13.2).

func Sign

func Sign(secret string, t time.Time, body []byte) string

Sign returns the Lagrange-Signature header for body at time t: "t=<unix seconds>,v1=<hex HMAC-SHA256 of "<t>.<body>" with secret>".

func ValidType

func ValidType(t string) bool

ValidType reports whether t is a type one can subscribe to.

func Verify

func Verify(secret, header string, body []byte, tolerance time.Duration) error

Verify checks a Lagrange-Signature header against body, and that it was made within tolerance of now (zero: five minutes), which stops replays.

Types

type Dispatcher

type Dispatcher struct {
	DB       *pg.DB
	Activity *activity.Log
	// Handles returns the addresses of a workspace: its events are those from or to them.
	Handles func(ctx context.Context, workspaceID int64) ([]string, error)
	// PublicURL makes links back to the web app.
	PublicURL string
	// Client sends the requests; nil means an SSRF-guarded client (see NewClient).
	Client *http.Client
	// RetryBase is the first retry delay (default 10s); each next one is three times
	// longer, up to six hours.
	RetryBase time.Duration
	// MaxAttempts is how often a delivery is tried before it fails (default 8).
	MaxAttempts int
	Logger      *slog.Logger
}

Dispatcher fans activity out to webhooks and sends the deliveries.

func (*Dispatcher) Fanout

func (d *Dispatcher) Fanout(ctx context.Context) error

Fanout turns new activity entries into deliveries for the webhooks that want them. Only one replica moves the cursor at a time; the others skip.

func (*Dispatcher) Ping

func (d *Dispatcher) Ping(ctx context.Context, hookID int64, workspace string) error

Ping queues a test event for one webhook.

func (*Dispatcher) Run

func (d *Dispatcher) Run(ctx context.Context, tick time.Duration)

Run fans out and sends every tick until ctx ends.

func (*Dispatcher) SendDue

func (d *Dispatcher) SendDue(ctx context.Context) error

SendDue sends the deliveries that are due, a batch at a time.

type Event

type Event struct {
	ID        string         `json:"id"`
	Type      string         `json:"type"`
	CreatedAt time.Time      `json:"createdAt"`
	Workspace string         `json:"workspace"`
	Data      map[string]any `json:"data"`
}

Event is the JSON body of a delivery.

Jump to

Keyboard shortcuts

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