Spectre.Context (Spectre v0.3.0)

Copy Markdown View Source

Per-turn runtime context.

Context is passed between runtime, router, prompt rendering, handlers, and action execution. It keeps dependencies explicit without pushing the entire host application into each function signature.

Summary

Functions

Marks the context as halted.

Returns the Run that owns lifecycle values staged through this context.

Adds an error entry to the context.

Adds a trace entry to the context.

Types

t()

@type t() :: %Spectre.Context{
  agent: module(),
  assigns: map(),
  errors: [term()],
  halted?: boolean(),
  input: Spectre.Input.t(),
  memory: term(),
  opts: keyword(),
  route: Spectre.Route.t() | nil,
  state: Spectre.State.t(),
  traces: [term()]
}

Functions

halt(ctx)

@spec halt(t()) :: t()

Marks the context as halted.

ctx = Spectre.Context.halt(ctx)

lifecycle_run_id(arg1)

@spec lifecycle_run_id(t() | map()) :: String.t() | nil

Returns the Run that owns lifecycle values staged through this context.

Subject-scoped Spectre.Instance calls opt into per-Run lifecycle ownership. Stateless Runtime calls and conversation Sessions return nil and keep the legacy single-lifecycle contract. Extension-owned Effect builders should bind this value to every staged Effect and use the scoped Spectre.State lifecycle queries with the same value.

put_error(ctx, error)

@spec put_error(t(), term()) :: t()

Adds an error entry to the context.

ctx = Spectre.Context.put_error(ctx, {:adapter_failed, reason})

put_trace(ctx, trace)

@spec put_trace(t(), term()) :: t()

Adds a trace entry to the context.

ctx = Spectre.Context.put_trace(ctx, {:router, :accepted})