Spectre.Session (Spectre v0.3.0)

Copy Markdown View Source

Conversation-scoped GenServer for running a Spectre agent under supervision.

The session keeps the latest %Spectre.State{} in process memory and delegates each turn to Spectre.ask/3. Applications that prefer durable state can use a state MyApp.Store adapter in the agent DSL; sessions restore from that adapter on start when no explicit state is supplied.

Summary

Functions

Returns the Agent module owned by a supervised session.

Handles one turn through a supervised session.

Returns a child spec for a supervised session.

Executes the session's current pending effect and commits its terminal state.

Replaces the current in-memory state.

Resolves the session's currently open policy from a trusted host decision.

Starts a conversation-scoped session process.

Returns the current in-memory Spectre state.

Handles one turn through a supervised session and returns a %Spectre.Turn{}.

Types

option()

@type option() ::
  {:agent, module()}
  | {:state, Spectre.State.t() | map() | keyword()}
  | {:opts, keyword()}
  | {:name, GenServer.name()}
  | {:conversation_id, term()}
  | {:idle, timeout() | false | nil}
  | {:shutdown, timeout() | false | nil}

Functions

agent(server)

@spec agent(GenServer.server()) :: module()

Returns the Agent module owned by a supervised session.

ask(server, input, opts \\ [])

@spec ask(GenServer.server(), Spectre.Input.t() | String.t() | map(), keyword()) ::
  {:ok, Spectre.Result.t()} | {:error, term()}

Handles one turn through a supervised session.

{:ok, result} = Spectre.Session.ask(session, "hello")

child_spec(init_arg)

@spec child_spec(keyword()) :: Supervisor.child_spec()

Returns a child spec for a supervised session.

children = [
  {Spectre.Session,
   agent: MyApp.Agents.ProjectAgent,
   name: MyApp.ProjectAgentSession,
   shutdown: :timer.minutes(10)}
]

execute(server, result, opts \\ [])

@spec execute(GenServer.server(), Spectre.Result.t(), keyword()) ::
  {:ok, Spectre.Result.t()} | {:error, term()}

Executes the session's current pending effect and commits its terminal state.

The state embedded in the supplied result is treated as a reference only; the Session's current state remains authoritative.

reset(server, state \\ %State{})

@spec reset(GenServer.server(), Spectre.State.t() | map() | keyword()) ::
  :ok | {:error, :instance_busy}

Replaces the current in-memory state.

:ok = Spectre.Session.reset(session, %Spectre.State{})

resolve_policy(server, result, resolution, opts \\ [])

@spec resolve_policy(
  GenServer.server(),
  Spectre.Result.t(),
  Spectre.Policy.resolution(),
  keyword()
) :: {:ok, Spectre.Result.t()} | {:error, term()}

Resolves the session's currently open policy from a trusted host decision.

The session uses its current state rather than trusting a potentially stale state embedded in the supplied result.

start_link(opts)

@spec start_link([option()]) :: GenServer.on_start()

Starts a conversation-scoped session process.

{:ok, pid} = Spectre.Session.start_link(agent: MyApp.Agent)

state(server)

@spec state(GenServer.server()) :: Spectre.State.t()

Returns the current in-memory Spectre state.

%Spectre.State{} = Spectre.Session.state(session)

turn(server, input, opts \\ [])

@spec turn(GenServer.server(), Spectre.Input.t() | String.t() | map(), keyword()) ::
  {:ok, Spectre.Turn.t()} | {:error, term()}

Handles one turn through a supervised session and returns a %Spectre.Turn{}.