# `ADK.Oban.AgentWorker`
[🔗](https://github.com/zeroasterisk/adk-elixir/blob/main/lib/adk/oban/agent_worker.ex#L2)

Oban worker for durable agent execution.

Runs ADK agents as background jobs with retries, scheduling, and persistence.

## Setup

1. Add `oban` to your deps
2. Configure Oban in your application (see Oban docs)
3. Enqueue jobs using `new/2` or the helper `enqueue/4`

## Usage

    # Direct Oban usage
    %{
      agent_module: "MyApp.Agents.Helper",
      user_id: "user1",
      session_id: "sess1",
      message: "Hello!",
      app_name: "my_app"
    }
    |> ADK.Oban.AgentWorker.new(queue: :agents, max_attempts: 5)
    |> Oban.insert()

    # Helper function
    ADK.Oban.AgentWorker.enqueue(
      MyApp.Agents.Helper,
      "user1",
      "Hello!",
      app_name: "my_app",
      session_id: "custom-session",
      queue: :agents,
      priority: 1
    )

## Agent Resolution

The worker resolves agents via `agent_module` — a module that implements
a `agent/0` callback returning an `ADK.Agent.t()` struct:

    defmodule MyApp.Agents.Helper do
      def agent do
        ADK.Agent.LlmAgent.new(
          name: "helper",
          model: "gemini-flash-latest",
          instruction: "You are a helpful assistant."
        )
      end
    end

Alternatively, pass `agent_config` as a map with `type` and agent params
for inline agent construction (useful for simple cases):

    %{
      agent_config: %{
        "type" => "llm",
        "name" => "helper",
        "model" => "gemini-flash-latest",
        "instruction" => "Be helpful"
      },
      user_id: "user1",
      message: "Hello!"
    }
    |> ADK.Oban.AgentWorker.new()
    |> Oban.insert()

## Result Storage

By default, results are broadcast via `ADK.Telemetry`:

    :telemetry.execute(
      [:adk, :oban, :job, :complete],
      %{duration: duration_ms},
      %{job_id: id, events: events, args: args}
    )

Attach a telemetry handler to persist results as needed.

## Configuration

    config :adk, ADK.Oban.AgentWorker,
      default_queue: :agents,
      default_max_attempts: 3,
      default_priority: 2

# `enqueue`

```elixir
@spec enqueue(module(), String.t(), String.t(), keyword()) ::
  {:ok, Oban.Job.t()} | {:error, term()}
```

Enqueue an agent job with a convenient API.

## Options

  * `:app_name` - application name (default: "adk_oban")
  * `:session_id` - session ID (default: auto-generated)
  * `:queue` - Oban queue (default: :agents)
  * `:max_attempts` - max retry attempts (default: 3)
  * `:priority` - job priority, 0-9 (default: 2)
  * `:scheduled_at` - schedule for later execution
  * `:runner_opts` - keyword opts passed to `ADK.Runner.run/5`

---

*Consult [api-reference.md](api-reference.md) for complete listing*
