GenAgentEnsemble.IEx (gen_agent_ensemble v0.2.0)

Copy Markdown View Source

Ergonomic iex-flavoured frontend to GenAgentEnsemble.

This module is meant to be aliased in .iex.exs:

alias GenAgentEnsemble.IEx, as: E

It delegates every core ensemble operation (list/0, tell/2, ask/2, etc.) to GenAgentEnsemble and adds a handful of helpers that make casual REPL work nicer: unwrapping the most useful field of a %GenAgent.Response{} (the text), blocking on a tell token, draining a pool to plain {token, text} pairs.

For programmatic use from library code, call GenAgentEnsemble directly -- this module is a humans-at-the-prompt convenience.

Example

iex> E.list()
["echo", "solo"]

iex> E.ask!("solo", "one-sentence summary of GenServer")
"A GenServer is a generic server process..."

iex> E.ask("solo", "give me markdown") |> E.puts()
# Markdown content
# ...
:ok

iex> {:ok, tok} = E.tell("qa-pool", "ask one")
iex> E.await("qa-pool", tok).text
"..."

Summary

Functions

Blocking ask that returns the response text directly, or raises.

Poll name/token until the token completes or timeout elapses.

Drain inbox/1 and unwrap each entry to {token, text} (or {token, {:error, reason}} for failed tokens).

Print the response text to stdout. Accepts a %Response{} or an {:ok, response} tuple. Handy for multi-line markdown output.

Extract the text field from a %GenAgent.Response{} or an {:ok, response} tuple. Raises on {:error, reason}.

Functions

ask(name, prompt)

See GenAgentEnsemble.ask/2.

ask(name, prompt, opts)

See GenAgentEnsemble.ask/3.

ask!(name, prompt, opts \\ [])

@spec ask!(String.t(), String.t(), keyword()) :: String.t()

Blocking ask that returns the response text directly, or raises.

The iex equivalent of "just give me the answer." Raises on error so mistakes don't silently become empty strings. Accepts the same opts as ask/3 (e.g. timeout: 60_000, agent: "alice").

await(name, token, timeout \\ 30000)

@spec await(String.t(), String.t(), timeout()) :: GenAgent.Response.t()

Poll name/token until the token completes or timeout elapses.

Returns the %Response{} on success; raises on error or timeout. The iex counterpart to GenAgentEnsemble.ask/3 for cases where the prompt was fired with tell/2 and the caller now wants a blocking wait.

drain(name)

@spec drain(String.t()) :: [{String.t(), String.t() | {:error, term()}}]

Drain inbox/1 and unwrap each entry to {token, text} (or {token, {:error, reason}} for failed tokens).

The quickest way to see what a Pool session has produced.

inbox(name)

See GenAgentEnsemble.inbox/1.

list()

See GenAgentEnsemble.list/0.

notify(name, event)

See GenAgentEnsemble.notify/2.

poll(name, token)

See GenAgentEnsemble.poll/2.

puts(arg)

@spec puts(GenAgent.Response.t() | {:ok, GenAgent.Response.t()} | {:error, term()}) ::
  :ok

Print the response text to stdout. Accepts a %Response{} or an {:ok, response} tuple. Handy for multi-line markdown output.

start_link(opts)

See GenAgentEnsemble.start_link/1.

status(name)

See GenAgentEnsemble.status/1.

stop(name)

See GenAgentEnsemble.stop/1.

tell(name, prompt)

See GenAgentEnsemble.tell/2.

tell(name, prompt, opts)

See GenAgentEnsemble.tell/3.

text(arg1)

@spec text(GenAgent.Response.t() | {:ok, GenAgent.Response.t()} | {:error, term()}) ::
  String.t()

Extract the text field from a %GenAgent.Response{} or an {:ok, response} tuple. Raises on {:error, reason}.

Useful in pipes: E.ask("solo", q) |> E.text() |> String.length().