{
  "name": "Vorlux AI | Agent Task Router",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "agent-task-route",
        "options": {
          "responseMode": "lastNode"
        }
      },
      "id": "webhook-trigger",
      "name": "Task Router Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        220,
        300
      ]
    },
    {
      "parameters": {
        "url": "http://host.docker.internal:3010/api/agents/stats",
        "method": "GET",
        "options": {
          "timeout": 10000
        }
      },
      "id": "fetch-agents",
      "name": "Fetch Agent Stats",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        460,
        200
      ]
    },
    {
      "parameters": {
        "url": "http://host.docker.internal:3010/api/agents/skills",
        "method": "GET",
        "options": {
          "timeout": 10000
        }
      },
      "id": "fetch-skills",
      "name": "Fetch Agent Skills",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        460,
        400
      ]
    },
    {
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const task = $('Task Router Webhook').first().json.body || $('Task Router Webhook').first().json;\nconst agents = $('Fetch Agent Stats').first().json.agents || $('Fetch Agent Stats').first().json.data || [];\nconst skills = $('Fetch Agent Skills').first().json.skills || $('Fetch Agent Skills').first().json.data || [];\n\nconst taskType = task.type || task.category || 'general';\nconst taskPriority = task.priority || 'medium';\nconst requiredSkills = task.skills || [];\n\n// Build skill map\nconst skillMap = {};\nskills.forEach(s => {\n  if (!skillMap[s.agent_id]) skillMap[s.agent_id] = [];\n  skillMap[s.agent_id].push(s.skill_name || s.name);\n});\n\n// Score agents\nconst scored = agents.map(a => {\n  let score = 0;\n  const agentSkills = skillMap[a.agent_id] || [];\n  requiredSkills.forEach(rs => { if (agentSkills.some(as => as.toLowerCase().includes(rs.toLowerCase()))) score += 10; });\n  if (a.status === 'active') score += 5;\n  if (a.current_load < 3) score += 3;\n  score -= (a.error_rate || 0) * 2;\n  return { agent_id: a.agent_id, display_name: a.display_name, score, skills: agentSkills, load: a.current_load || 0 };\n}).sort((a, b) => b.score - a.score);\n\nconst bestAgent = scored[0] || { agent_id: 'main', display_name: 'Paco' };\nreturn [{ json: { task, bestAgent, allCandidates: scored.slice(0, 5), routed: true } }];"
      },
      "id": "route-task",
      "name": "Score & Route Task",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        720,
        300
      ]
    },
    {
      "parameters": {
        "url": "http://host.docker.internal:3010/api/orchestrator/dispatch",
        "method": "POST",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ agentId: $json.bestAgent.agent_id, task: $json.task, priority: $json.task.priority || 'medium' }) }}",
        "options": {
          "timeout": 15000
        }
      },
      "id": "dispatch-task",
      "name": "Dispatch to Agent",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        960,
        300
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "={{$env.DISCORD_OPS_WEBHOOK}}",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ content: '\ud83d\udccb Task routed: **' + ($json.task.title || $json.task.type || 'Task') + '** \u2192 Agent: **' + $json.bestAgent.display_name + '** (score: ' + $json.bestAgent.score + ')' }) }}"
      },
      "id": "discord-notify",
      "name": "Discord Notify",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1200,
        300
      ]
    }
  ],
  "connections": {
    "Task Router Webhook": {
      "main": [
        [
          {
            "node": "Fetch Agent Stats",
            "type": "main",
            "index": 0
          },
          {
            "node": "Fetch Agent Skills",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch Agent Stats": {
      "main": [
        [
          {
            "node": "Score & Route Task",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch Agent Skills": {
      "main": [
        [
          {
            "node": "Score & Route Task",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Score & Route Task": {
      "main": [
        [
          {
            "node": "Dispatch to Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Dispatch to Agent": {
      "main": [
        [
          {
            "node": "Discord Notify",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  }
}