// Stack → Agent → Server: installa capacità, combina regex, embedding e classificazione con modelli, poi gestisci l’intento nelle callback. MyApp.Embeddings e MyApp.IntentModel sono provider applicativi; l’esempio email richiede spectre_kinetic opzionale.
# 1. Stack: optional Kinetic registers the typed mail tool and effect.
defmodule MyApp.AI do
use Spectre.Stack
install Spectre.Kinetic, mode: :closed_moves,
actions: MyApp.MailActions, modes: [send_email: :write]
end
# 2. Agent: recognize intent with patterns, similarity or a model.
defmodule MyApp.SupportAgent do
use Spectre.Agent, stack: MyApp.AI, server: MyApp.SupportServer
embedding MyApp.Embeddings
classifier :llm, MyApp.IntentModel
router strategies: [:regex, :embedding, :llm_classifier]
flow :support do
on :pricing, regex: ~r/\b(price|pricing)\b/i,
semantic_examples: ["How much does a subscription cost?"]
on :send_email, regex: ~r/^send email$/i,
semantic_examples: ["Send the prepared message"]
end
end
# 3. Server: return responses or concrete governed proposals.
defmodule MyApp.SupportServer do
use Spectre.Server, tools: MyApp.AI
@impl true
def handle_flow({:support, :pricing}, _input, ctx),
do: respond(ctx, "Plans start at EUR 20 per month.")
def handle_flow({:support, :send_email}, _input, ctx),
do: propose(ctx, {:kinetic, :send_email},
%{"to" => "team@example.com", "body" => "The report is ready."},
on_result: :sent)
def handle_flow(:unmatched, _input, ctx),
do: respond(ctx, "Ask about pricing or send the prepared email.")
@impl true
def handle_result(:sent, {:governed, %{outcome: %{status: :succeeded}}}, ctx),
do: respond(ctx, "The registered mail operation completed.")
def handle_result(tag, result, ctx), do: super(tag, result, ctx)
end
{:ok, server} = Spectre.Server.start_link(
agent: MyApp.SupportAgent, consumer: self(),
identity: %{subject: "customer-42"},
gam: [operations: %{{:kinetic, :send_email} => [confirm: true]}])
{:ok, interaction} = Spectre.Server.input(server, "What is the price?")
receive do
{Spectre.Server, ^server, {:response, ^interaction, text, _meta}} ->
IO.puts(text)
after
5_000 -> raise "No response"
end
Definisci il catalogo tipizzato prima di compilare lo Stack. La fixture simula l’invio: sostituiscila con il tuo trasporto email. Il consumer configurato mostra la richiesta esatta e invia la scelta distinta della persona.
# Host-owned catalog; define before compiling MyApp.AI.
# This function simulates delivery and sends no real email.
defmodule MyApp.MailActions do
use SpectreKinetic
@al ~s(SEND EMAIL TO="team@example.com" BODY="The report is ready.")
@doc "Simulate delivery; replace with the application's mail transport."
@spec send_email(String.t(), String.t()) :: {:ok, map()}
def send_email(to, body), do: {:ok, %{to: to, body: body, simulated: true}}
end
# Continue in the configured consumer, using server from the first example.
{:ok, email_interaction} = Spectre.Server.input(server, "send email")
receive do
{Spectre.Server.GAM.Managed, _owner, {:presentation, material}} ->
IO.puts(material["rendered_payload"])
ref = material["presentation_ref"]
:ok = Spectre.Server.presented(server, ref)
receive do
{Spectre.Server, ^server, {:pending, ^email_interaction, _id}} -> :ok
after
5_000 -> raise "Consent wait unavailable"
end
choice = if IO.gets("Confirm? Type yes: ") == "yes\n", do: :confirm, else: :reject
Spectre.Server.decide(server, nil, ref, choice)
after
5_000 -> raise "No presentation"
end