core

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2026 License: MIT Imports: 9 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 core holds the parts shared by the flow, step and group packages: the wire types, the replay state of one call, and the error types. Users never import it; the public packages re-export what they need.

Index

Constants

View Source
const (
	HeaderSignature  = "X-Inngest-Signature"
	HeaderNoRetry    = "X-Inngest-No-Retry"
	HeaderRetryAfter = "Retry-After"
	// HeaderAnswerSignature signs the app's answers (see signAnswer in the root package).
	HeaderAnswerSignature = "X-Flow-Answer-Signature"
	// HeaderStream: on a POST, the engine takes a streamed answer; on the
	// answer, the answer is streamed (see stream.go in the root package).
	HeaderStream = "X-Flow-Stream"
)

Header names, as in packages/protocol/src/types.ts (HEADERS).

View Source
const (
	StatusDone = 200
	StatusOps  = 206
)

HTTP status codes of the app endpoint (STATUS in types.ts).

View Source
const (
	OpStepCompleted  = "StepCompleted"
	OpStepFailed     = "StepFailed"
	OpStepPlanned    = "StepPlanned"
	OpSleep          = "Sleep"
	OpWaitForEvent   = "WaitForEvent"
	OpSendEvent      = "SendEvent"
	OpInvokeFunction = "InvokeFunction"
)

Opcode names.

View Source
const MaxOutputBytes = 4_000_000

MaxOutputBytes is the largest step output the engine keeps (4 MB).

Variables

View Source
var ErrNotInFunction = errors.New("flow: step called outside a flow function or inside another step")

ErrNotInFunction is returned by step calls made outside a flow function, or inside another step's callback.

Functions

func Decode

func Decode[T any](data json.RawMessage) (T, error)

Decode reads stored step data into T. A JSON null leaves the zero value.

func Duration

func Duration(d time.Duration) string

Duration turns a Go duration into the engine's text form: "<n>s" when it is whole seconds, else "<n>ms". parseDuration in packages/protocol reads both. Anything under a millisecond is dropped.

func DurationPtr

func DurationPtr(d *time.Duration) *string

DurationPtr is Duration for an optional value: nil or zero gives nil.

func HashStepID

func HashStepID(id string, occurrence int) string

HashStepID gives the hashed step ID: lowercase hex SHA-1 of id for the first use (occurrence 0), and of "id:N" for the Nth repeat.

func IdempotencyKeyFrom

func IdempotencyKeyFrom(ctx context.Context) string

IdempotencyKeyFrom returns the key stored in a step callback's context.

func InParallel

func InParallel(ctx context.Context) bool

InParallel reports whether ctx is inside a group.Parallel branch.

func IsBranchStop

func IsBranchStop(r any) bool

IsBranchStop reports whether r stops only one parallel branch.

func IsControl

func IsControl(r any) bool

IsControl reports whether a recovered value is the SDK's control panic.

func IsNoRetry

func IsNoRetry(err error) bool

IsNoRetry reports whether err (or an error it wraps) is a NoRetry.

func NoRetryErr

func NoRetryErr(err error) error

NoRetryErr wraps err so it is not retried.

func RetryTime

func RetryTime(err error) (time.Time, bool)

RetryTime returns the retry time of a RetryAt in err, if any.

func StopBranch

func StopBranch()

StopBranch panics to stop one parallel branch.

func StopRun

func StopRun()

StopRun panics to stop the whole handler.

func WithParallel

func WithParallel(ctx context.Context) context.Context

WithParallel marks the context of one group.Parallel branch.

func WithRun

func WithRun(ctx context.Context, r *Run) context.Context

WithRun puts the replay state in the context given to a handler.

Types

type Control

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

Control is the value the SDK panics with. Never swallow it.

type Event

type Event struct {
	Name string `json:"name"`
	Data any    `json:"data,omitempty"`
	// ID makes the event idempotent: two events with the same ID are one event.
	ID *string `json:"id,omitempty"`
	// Timestamp is when the event happened, in ms since epoch. A future time
	// schedules the run. Zero means now.
	Timestamp int64 `json:"ts,omitempty"`
	User      any   `json:"user,omitempty"`
}

Event is an event to send: to POST /e, or from step.Send.

type InvokeRequest

type InvokeRequest struct {
	FnID         string               `json:"fnId"`
	RunID        string               `json:"runId"`
	Event        json.RawMessage      `json:"event"`
	Events       []json.RawMessage    `json:"events"`
	Memo         map[string]MemoEntry `json:"memo"`
	Attempt      int                  `json:"attempt"`
	MaxAttempts  int                  `json:"maxAttempts"`
	TargetStepID string               `json:"targetStepId,omitempty"`
	// UseAPI means the state was too big to send: Memo and Events are empty,
	// and the SDK loads them from GET {base}/runs/{runId}/state.
	UseAPI bool `json:"useApi,omitempty"`
}

InvokeRequest is the body of the signed POST from the engine.

type LoadedMemoEntry

type LoadedMemoEntry = MemoEntry

LoadedMemoEntry is a memo entry from the run state API. It has the same { data } or { error } shape as MemoEntry.

type MemoEntry

type MemoEntry struct {
	Data  json.RawMessage
	Error *SerializedError
}

MemoEntry is a finished step: either Data or Error is set.

func (*MemoEntry) UnmarshalJSON

func (m *MemoEntry) UnmarshalJSON(b []byte) error

UnmarshalJSON reads { data } or { error }. A missing data means null.

type NoRetry

type NoRetry struct{ Err error }

NoRetry marks an error so it is not retried.

func (*NoRetry) Error

func (e *NoRetry) Error() string

func (*NoRetry) Unwrap

func (e *NoRetry) Unwrap() error

type Opcode

type Opcode struct {
	Op   string `json:"op"`
	ID   string `json:"id"`
	Name string `json:"name"`

	// StepCompleted. Always sent for StepCompleted (null when empty).
	Data json.RawMessage `json:"data,omitempty"`

	// StepFailed.
	Error   *SerializedError `json:"error,omitempty"`
	NoRetry bool             `json:"noRetry,omitempty"`
	RetryAt *int64           `json:"retryAt,omitempty"`

	// Sleep: ms since epoch.
	Until *int64 `json:"until,omitempty"`

	// WaitForEvent.
	Event   string  `json:"event,omitempty"`
	Timeout any     `json:"timeout,omitempty"`
	Match   *string `json:"match,omitempty"`
	If      *string `json:"if,omitempty"`

	// SendEvent.
	Events []Event `json:"events,omitempty"`

	// InvokeFunction. Function is the full "<appId>-<functionId>". Timeout is shared
	// with WaitForEvent above.
	Function string          `json:"function,omitempty"`
	User     json.RawMessage `json:"user,omitempty"`
}

Opcode is one step the SDK reports back. Op says which fields are used.

func RunStep

func RunStep(hashed, name string, value any, err error) Opcode

RunStep builds the StepCompleted or StepFailed for a step.Run result.

type PanicError

type PanicError struct {
	Value any
	Stack string
}

PanicError is a panic recovered from a step.Run callback. It fails that step's attempt (the step is retried), the same as an error thrown in a TypeScript step.

func (*PanicError) Error

func (e *PanicError) Error() string

type RetryAt

type RetryAt struct {
	Err error
	At  time.Time
}

RetryAt marks an error so the next attempt runs at At.

func (*RetryAt) Error

func (e *RetryAt) Error() string

func (*RetryAt) Unwrap

func (e *RetryAt) Unwrap() error

type Run

type Run struct {
	RunID  string
	Target string
	Now    func() time.Time
	// contains filtered or unexported fields
}

Run is the replay state of one call to a function.

func FromContext

func FromContext(ctx context.Context) *Run

FromContext returns the replay state, or nil outside a flow function (and inside a step callback).

func NewRun

func NewRun(runID, target string, memo map[string]MemoEntry, now func() time.Time) *Run

NewRun starts the replay state for one call.

func (*Run) HasPending

func (r *Run) HasPending() bool

HasPending reports whether new steps were found.

func (*Run) IdempotencyKey

func (r *Run) IdempotencyKey(hashed string) string

IdempotencyKey is "<runId>:<hashed step id>", the same on every attempt.

func (*Run) New

func (r *Run) New(ctx context.Context, op Opcode, exec func() Opcode)

New is called when a step is not in the memo. op is the step's own opcode (for step.Run, a StepPlanned); exec runs a step.Run callback and returns its StepCompleted or StepFailed (nil for other steps). New never returns.

func (*Run) Next

func (r *Run) Next(userID string) (string, *MemoEntry)

Next counts one use of the user's step ID and returns its hashed ID and its memo entry, if any.

func (*Run) Ops

func (r *Run) Ops() []Opcode

Ops returns the opcodes to answer with. In target mode it is the target's result (or nothing if it was not reached). Planned step.Run calls that did not run become StepPlanned.

func (*Run) RunIfAlone

func (r *Run) RunIfAlone()

RunIfAlone runs the found step now when it is the only new step and it is a step.Run. Called at the end of the outer group.Parallel.

func (*Run) StepContext

func (r *Run) StepContext(ctx context.Context, hashed string) context.Context

StepContext is the context a step.Run callback gets: step calls inside it fail with ErrNotInFunction, and step.IdempotencyKey works.

type RunStatePage

type RunStatePage struct {
	// Events is the run's batch. Only on the first page.
	Events []json.RawMessage          `json:"events,omitempty"`
	Memo   map[string]LoadedMemoEntry `json:"memo"`
	// Next is the cursor for the next page; nil on the last page.
	Next *string `json:"next"`
}

RunStatePage is one page of GET {base}/runs/{runId}/state.

type SerializedError

type SerializedError struct {
	Name    string `json:"name"`
	Message string `json:"message"`
	Stack   string `json:"stack,omitempty"`
}

SerializedError is an error as it travels on the wire.

func Serialize

func Serialize(err error) SerializedError

Serialize turns an error into its wire form. The name is the StepError's name when there is one, else "Error".

type StepError

type StepError struct {
	Name    string `json:"name"`
	Message string `json:"message"`
	Stack   string `json:"stack,omitempty"`
}

StepError is what a step returns on replay when the memo holds { error }: the step ran out of retries (or a child function failed). The function can check for it with errors.As and run compensation steps.

func (*StepError) Error

func (e *StepError) Error() string

Jump to

Keyboard shortcuts

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