Skip to main content

lib/mix/tasks/apple_intents.gen.jido.ex

defmodule Mix.Tasks.AppleIntents.Gen.Jido do
  @shortdoc "Prints Jido-integrated intent router and handler snippets"

  @moduledoc """
  Generates copy-paste snippets for Jido-backed App Intents fulfillment.

  Requires `apple_intents_jido` in your Phoenix app.

  ## Examples

      mix apple_intents.gen.jido
      mix apple_intents.gen.jido --router MyApp.IntentRouter --orchestrator MyApp.Orchestrator

  """

  use Mix.Task

  @switches [router: :string, orchestrator: :string, intent: :string, task: :string]

  @impl Mix.Task
  def run(args) do
    {opts, _argv, _} = OptionParser.parse(args, strict: @switches)

    router = Keyword.get(opts, :router, "MyApp.IntentRouter")
    orchestrator = Keyword.get(opts, :orchestrator, "MyApp.Orchestrator")
    intent = Keyword.get(opts, :intent, "MyApp.PhotoIntent")
    task = Keyword.get(opts, :task, "organize_photos")

    Mix.shell().info("""

    # ── Jido + App Intents (generated by mix apple_intents.gen.jido) ──
    # Add to mix.exs: {:apple_intents_jido, "~> 0.1.0"}

    config :apple_intents_jido,
      orchestrator: #{orchestrator},
      tasks: %{"#{task}" => #{intent}Action}

    defmodule #{intent} do
      use AppleIntents.Intent, intent: "#{Macro.camelize(task)}", domain: :photos
      use AppleIntents.Jido, task: "#{task}"
    end

    defmodule #{router} do
      use AppleIntents.Router
      use AppleIntents.Jido, orchestrator: #{orchestrator}

      handlers do
        intent #{intent}
      end
    end
    """)
  end
end