ReplyBrain Docs LLMs

Capabilities

Every call ReplyBrain makes to your app is a signed POST under your mount (/replybrain). The gem routes them; this page lists what each one is sent and must answer.

Operation Path Required?
message.publish capabilities/message.publish Yes, to receive drafts.
message.withdraw capabilities/message.withdraw Yes: without it your app is sent no assistant draft.
support.open_tickets capabilities/support.open_tickets No. Lists open tickets, so a backlog can be drafted.
identity.lookup capabilities/identity.lookup No. Says which customer wrote a mail.
conversation.close capabilities/conversation.close No. Without it a case of your app is never wrapped up by ReplyBrain.
facts facts/<topic> For drafts that use the customer's facts. See [[guides/facts
manifest manifest Served by the engine.

Except message.publish and facts, each operation is used only after ReplyBrain registers it for your connection: tell us which ones you serve.

message.publish

Defined with a ReplyBrain::Client::Source subclass. Sent (with Idempotency-Key: <effect_key>):

{
  "capability": "message.publish", "version": 1,
  "publication_id": "c0ffee00-0000-4000-8000-000000000003", "effect_key": "publish:c0ffee00",
  "mode": "draft",
  "arguments": {
    "body": "Hi Ada, your export finished at 10:02; you can download it from Settings → Exports.",
    "channel": "in_app", "conversation_ref": "ticket:SUP-1042",
    "recipients": [], "expected_source_revision": null,
    "origin": "assistant", "disclosure_mode": "none", "approval_id": null, "policy_id": null
  }
}

Answer drafted(<your draft id>), accepted(message_id: …) (sent; only in publish mode) or refused(<reason>). A valid_until argument comes only if your connection declared it enforces it: store it, and refuse to send the draft after it. origin is assistant or member (a teammate's words, disclosure_mode null).

The gem's Source::Message exposes body, recipients, channel, conversation_ref, expected_source_revision, disclosure_mode, approval_id, policy_id, publication_id, effect_key, mode, draft? and publish?. For origin or valid_until, read the raw arguments: payload["arguments"]["valid_until"].

Its lookup (at capabilities/message.publish/reconcile) answers whether something exists for an effect_key: a Hash ({ draft_id:, state:, message_id: }) or nil. state is one of pending, sent, withdrawn, rejected, superseded, expired; sent counts only with its message_id.

message.withdraw

{ "capability": "message.withdraw", "version": 1, "effect_key": "withdraw:c0ffee00",
  "arguments": { "publication_id": "c0ffee00-0000-4000-8000-000000000003", "draft_id": "981", "reason": "evidence_withdrawn" } }

draft_id is the id you answered drafted with (absent if the withdrawal arrives before the draft). reason is a code such as evidence_withdrawn, evidence_superseded, grant_revoked. Answer succeeded(outcome: …):

outcome When
withdrawn The draft was removed.
prevented No draft yet: remember the publication so its later placement is refused.
not_pending Nothing left to take back.
already_sent A person sent it first; add message_id. A person is then asked to correct what the customer was told.

Take the same lock a person's send takes, so a withdrawal and a send never both win. A failed(…) answer or a 4xx (other than 408, 425, 429) is treated as a bug and never retried. See the quickstart for a full class.

support.open_tickets

Read-only (effect :read), always answered fresh:

class SupportOpenTickets < ReplyBrain::Client::Capability
  manifest "support.open_tickets", version: 1, effect: :read

  perform do
    tickets = Ticket.open.order(:created_at).limit(500).map do |t|
      { conversation_ref: "ticket:#{t.reference}", latest_message_id: t.messages.last&.id&.to_s,
        opened_at: t.created_at.utc.iso8601, awaiting: t.awaiting, requester: { ref: "user:#{t.user_id}", account_scope: t.user_id.to_s } }
    end
    succeeded(tickets:, truncated: Ticket.open.count > 500)
  end
end

conversation_ref must be exactly "<source_object.type>:<source_object.id>" of your events.

identity.lookup

Read-only. Sent { "arguments": { "email": "[email protected]", "phones": ["+34600000000"], "name": "Ada" } } (phones and name only when the mail mentions them). Answer the one customer who holds that confirmed address, or nobody:

class IdentityLookup < ReplyBrain::Client::Capability
  manifest "identity.lookup", version: 1, effect: :read
  argument :email

  perform do |email:|
    next failed("invalid_email") unless email.to_s.include?("@")
    user = User.where(email: email.downcase).where.not(confirmed_at: nil).first
    user ? succeeded(ref: "user:#{user.id}", account_scope: user.id.to_s, name: user.first_name, matched_by: "email")
         : succeeded(ref: nil, account_scope: nil)
  end
end

ref must be the same requester.ref your events use. The answer is a candidate a teammate confirms, never proof.

conversation.close

Sent { "arguments": { "conversation_ref", "expected_last_message_id", "closing_publication_id", "silent": "true" | "false" } }. Close only if the conversation's last message is still expected_last_message_id (under the same lock an inbound reply takes), and answer succeeded(outcome: "closed"), or failed("<reason>").

Your own actions

Anything else your app can do for a customer (extend a trial, resend an invoice) is a capability ReplyBrain may propose in a plan, for a person to approve. Describe it so it is proposed at the right time:

class ExtendTrial < ReplyBrain::Client::Capability
  manifest "trial.extend", version: 1, effect: :billing
  describe "Extend a customer's trial by a few days",
           use_when: "the customer could not try the product during the trial for a reason on our side",
           never_when: "the trial was already extended",
           risk: :low,
           outcome: { en: "I've extended your trial by %{days} days." }

  argument :user, resource: :user
  argument :days, type: :integer, description: "How many days"

  perform   { |user:, days:, effect_key:| Trials.extend!(user, days:, idempotency_key: effect_key) ? succeeded : failed("not_eligible") }
  reconcile { |user:, effect_key:| Trials.extension(user, key: effect_key) ? effected : not_effected }
  verifier do |user:, effect_key:|
    ext = Trials.extension(user, key: effect_key) or next pending
    observe(source: "trial_extension:#{ext.id}", revision: ext.updated_at) { ext.applied? }
  end
end
WARNING

A capability that changes something must declare reconcile and verifier, or it does not load. perform must pass effect_key to your own idempotency, and must check again that the action is still allowed when it runs: an approval may be hours old.

Changing a capability's arguments or effect needs a new version; then ask for your manifest to be read again.