Liking cljdoc? Tell your friends :D

re-frame and re-frame2

To an app, an agent is a second user. It writes where a human writes, by dispatching events (domino 1), and it reads where a human's view reads, from subscriptions (domino 4). So the adapter is not one artifact. Each concern uses the artifact re-frame already has for its domino, the way re-frame-http-fx is just reg-fx :http-xhrio.

concerndominore-frame 1 (autonomous.re-frame)re-frame2 (autonomous.re-frame2)
the app tells the agent something3 effect[:autonomous/journal entry], a reg-fxthe same effect, re-frame2's binary reg-fx
who caused this event2 coeffect(rf/inject-cofx :autonomous/actor){:rf.cofx/requires [:autonomous/actor]}
observe every event, keep provenance2-3global interceptor :autonomous.re-frame/journalthe same id, named in the frame's :interceptors
answer after the cascade, dry run1-3:settle, :stub-fx, :re-frame/probe (dispatch-and-settle, dispatch-with):settle (dispatch-sync drains), :stub-fx (:fx-overrides); no probe
declare a tool1 and 4:autonomous/tool in registration metadata, through autonomous.re-frame.core:autonomous/tool in re-frame2's own registration metadata

Load autonomous.re-frame or autonomous.re-frame2, never both: each needs its own re-frame on the classpath. Loading either one registers the effect and the coeffect, as requiring http-fx registers :http-xhrio.

The journal effect

A handler tells the agent what happened by returning data:

(rf/reg-event-fx :todo/remove
  (fn [{:keys [db]} [_ id]]
    {:db (update db :todos dissoc id)
     :fx [[:autonomous/journal {:kind :todo/removed :id id}]]}))

The value is one entry (a map with a keyword :kind) or a vector of them. Anything else journals nothing and logs a warning (re-frame's console on re-frame 1, js/console.warn on re-frame2). On re-frame2 the handler is registered with rf/reg-event and is otherwise the same.

The actor coeffect

re-frame 1:

(rf/reg-event-fx :todo/add
  [(rf/inject-cofx :autonomous/actor)]
  (fn [{:keys [db autonomous/actor]} [_ text]]
    {:db (assoc-in db [:todos text] {:text text :by actor})}))

re-frame2:

(rf/reg-event :todo/add
  {:rf.cofx/requires [:autonomous/actor]}
  (fn [{:keys [db autonomous/actor]} [_ text]]
    {:db (assoc-in db [:todos text] {:text text :by actor})}))

The value is the calling agent's actor, or nil when the app dispatched the event itself. On re-frame 1, the events this event dispatches carry the actor too once observe-events! is installed. On re-frame2 the coeffect is not handed on to child events (each child is a new causal token), and under the :strict mint policy a human dispatch of a handler that requires it fails.

The journal interceptor

re-frame 1, one call:

(arf/observe-events! {:payloads? false})

re-frame2: register the interceptor first, then name it in the frame:

(adapter/observe-events! {:payloads? false})
(rf/make-frame {:id :app/main :interceptors [:autonomous.re-frame/journal]})

Each event is journaled with :origin :agent or :app. On re-frame 1 a child event an agent's event dispatches is journaled with the same actor and the parent's id as :cause. On re-frame2 the whole cascade of an agent's dispatch shares a :trace-id.

Settle and dry run

(arf/reg-event-tool :cart/checkout
  {:description "Check out the cart"
   :settle {:timeout-ms 3000}   ; answer once the events it dispatches ran
   :stub-fx #{:http-xhrio}      ; never hit the network from this tool
   :result-path [:cart]})

A :settle call is Pending until the cascade has settled. A cascade that does not settle in time is refused with :re-frame/settle-timeout. On re-frame2, dispatch-sync already drains the synchronous cascade; timers, HTTP replies and :dispatch-later are not awaited. (arf/install-dev-tools!) adds the :admin tool re-frame_probe, which dry-runs an event on re-frame 1 and answers the effects it asked for. On re-frame2 the probe is refused, because there :db is not an fx and cannot be stubbed.

Declaring tools

There are two styles, and both produce the same tools.

Explicit, in one namespace. Every tool an agent may use is listed in one file, which a security review reads alone. This is the default:

(arf/reg-event-tool :todo/add
  {:description "Add a todo item. Answers the visible todos."
   :input [:map [:text [:string {:min 1 :max 200}]]]
   :event-args [:text]                ; {:text "milk"} -> [:todo/add "milk"]
   :result-sub [:todos/visible]})
(arf/reg-sub-tool :todos/stats {:description "How many todos exist"})

At the registration. The tool is declared next to its handler, under :autonomous/tool in the registration's metadata map. Choose this when the handler and its tool should change together. On re-frame 1, swap one alias: autonomous.re-frame.core mirrors re-frame.core (and builds on re-frame.core-instrumented), and its reg-event-db, reg-event-fx, reg-event-ctx and reg-sub take the optional map after the id:

(ns todo.model
  (:require [autonomous.re-frame.core :as rf]))   ; was re-frame.core

(rf/reg-event-db :todo/add
  {:autonomous/tool {:description "Add a todo item. Answers the visible todos."
                     :input [:map [:text [:string {:min 1 :max 200}]]]
                     :event-args [:text]
                     :result-sub [:todos/visible]}}
  (fn [db [_ text]] ...))

(rf/reg-sub :todos/stats
  {:autonomous/tool {:description "How many todos exist"}}
  :<- [:todos/all]
  (fn [todos _] ...))

On re-frame2 the app keeps re-frame.core, whose registrations already take the map:

(rf/reg-event :todo/add
  {:autonomous/tool {:description "Add a todo item" :event-args [:text]
                     :frame :app/main :result-sub [:todos/visible]}}
  (fn [{:keys [db]} [_ text]] ...))

Then, once all handlers are registered, one call installs every declared tool. It is (arf/install-declared-tools!) on re-frame 1 and (adapter/install-declared-tools!) on re-frame2. The call checks every declaration first, with the checks reg-event-tool and reg-sub-tool make. It answers {:ok [tool-names]}, or the first refusal as a value with nothing installed. Calling it again (hot reload) replaces each tool in place. Without :autonomous/tool, a mirrored registration is exactly re-frame's own.

A declaration takes the keys of a tool spec (:description, :input, :output, :scopes, :read-only?, :event-args or :args->event, :result, :result-sub, :result-path, :settle, :stub-fx, :async?, :tool-id, :frame). It is protocol-neutral: it never names MCP's inputSchema, outputSchema or annotations. Behaviour hints are :destructive?, :idempotent? and :open-world?, and the MCP projection turns them into destructiveHint and the rest. So the same declaration can later be projected to other agent protocols. The todo example (example/src/todo/model.cljs) declares its tools this way. todo.agentic keeps what has no registration site: the app description, the app-db resource, the observer, tracing and the dev tools.

Can you improve this documentation?Edit on GitHub

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
←Move to previous article
→Move to next article
Ctrl+/Jump to the search field
× close