ConvergerAgent

Elixir agent SDK for Converger: a bot backend that answers the conversations of a Converger channel over one channel-scoped Protocol v1 WebSocket (Mint.WebSocket). It turns user messages into turns, runs your handler per turn, and takes care of acks, interrupts, reconnects and per-conversation watermarks.

def deps do
  # Not yet published on Hex; use a path or git dependency until then.
  [{:converger_agent, "~> 0.1"}]
end

Full documentation: converger.aimtune.dev/sdk/agent-elixir.

Quick start

defmodule MyBot do
  use ConvergerAgent
  alias ConvergerAgent.Reply

  @impl true
  def handle_turn(%{text: "refund"}, reply) do
    with {:ok, "yes"} <-
           Reply.ask(reply, %{"title" => "Refund 249.90 TRY?"},
             actions: [%{label: "Yes", value: "yes"}, %{label: "No", value: "no"}]
           ) do
      Reply.text(reply, "Refund started.")
    end
  end

  def handle_turn(turn, reply), do: Reply.text(reply, "You said: " <> turn.text)

  # optional
  @impl true
  def handle_error(error), do: IO.warn(Exception.message(error))
end

children = [
  {MyBot,
   url: ConvergerAgent.socket_url("https://chat.example.com"),
   token: fn -> MyApp.channel_token() end,
   group: "workers"}
]

Supervisor.start_link(children, strategy: :one_for_one)

The token is channel-scoped (POST /api/v1/converger/tokens/generate with {"scope": "channel"}). It may be a string, a 0-arity function or an {m, f, a} returning a token (or {:ok, token}); functions are called on every connect, so an expired token can be replaced.

Without a module:

ConvergerAgent.start_link(
  url: ConvergerAgent.socket_url("https://chat.example.com"),
  token: token,
  handler: fn turn, reply -> ConvergerAgent.Reply.text(reply, "Hi " <> turn.text) end,
  on_error: &IO.inspect/1
)

API

Semantics

Testing

mix deps.get
mix format --check-formatted
mix compile --warnings-as-errors --force
mix test --warnings-as-errors

The tests run the shared conformance fixture (packages/conformance/agent.json) against an in-process Bandit fake server and validate every frame the agent sends against the Protocol v1 JSON Schemas (priv/protocol/v1). Set CONVERGER_CONFORMANCE_DIR / CONVERGER_SCHEMA_DIR to use other copies.