Powered by AppSignal & Oban Pro

Jidoka: Ash Resources

livebook/14_ash_resources.livemd

Jidoka: Ash Resources

Run in Livebook

ash_resource expands an Ash resource’s AshJido actions into agent-callable tools and injects the resource domain into runtime context.

Setup

Mix.install(
  [
    {:jidoka, git: "https://github.com/agentjido/jidoka.git", branch: "main"},
    {:kino, "~> 0.19.0"}
  ],
  config: [
    jidoka: [
      model_aliases: %{fast: "anthropic:claude-haiku-4-5"}
    ]
  ]
)
Jidoka.Kino.setup()

Attach An Ash Resource

Jidoka ships a small example Ash resource used by the kitchen-sink demo.

defmodule LivebookDemo.AshResources.Agent do
  use Jidoka.Agent

  agent do
    id :livebook_ash_resource_agent
  end

  defaults do
    model :fast
    instructions "You can use Ash resource tools."
  end

  capabilities do
    ash_resource Jidoka.Examples.KitchenSink.Ash.User
  end
end
%{
  ash_resources: LivebookDemo.AshResources.Agent.ash_resources(),
  ash_domain: LivebookDemo.AshResources.Agent.ash_domain(),
  requires_actor?: LivebookDemo.AshResources.Agent.requires_actor?(),
  tool_names: LivebookDemo.AshResources.Agent.tool_names()
}

Actor Context Is Required

Ash-backed agents require an actor in runtime context before the provider is called.

ash_session =
  Jidoka.Session.new!(
    agent: LivebookDemo.AshResources.Agent,
    id: "livebook-ash-resource"
  )

{:ok, _pid} =
  Jidoka.Kino.start_or_reuse(ash_session.agent_id, fn ->
    Jidoka.Session.start_agent(ash_session)
  end)

{:error, error} = Jidoka.chat(ash_session, "List users.")

Jidoka.format_error(error)

When actor is present, Jidoka adds the Ash domain to the internal tool context.

{:ok, opts} =
  Jidoka.Agent.prepare_chat_opts(
    [context: %{actor: %{id: "user-1", name: "Livebook User"}}],
    %{
      domain: LivebookDemo.AshResources.Agent.ash_domain(),
      require_actor?: LivebookDemo.AshResources.Agent.requires_actor?()
    }
  )

Keyword.fetch!(opts, :tool_context)

Optional Provider Turn

actor_session =
  Jidoka.Session.new!(
    agent: LivebookDemo.AshResources.Agent,
    id: "livebook-ash-resource",
    context: %{actor: %{id: "user-1", name: "Livebook User"}}
  )

Jidoka.Kino.chat("Ash resource chat", fn ->
  Jidoka.chat(actor_session, "What Ash resource tools are available?")
end)