[
  {
    "name": "support_escalation",
    "description": "Fired by agent-runner when triage confidence falls below threshold. Marks the thread as pending human review and notifies the support team.",
    "trigger_type": "event",
    "config": {
      "event": "agent.inference.low_confidence",
      "scope": "account"
    },
    "stages": [
      {
        "stage_type": "send_notification",
        "config": {
          "message": "A support ticket has been escalated to human review — confidence too low for AI response.",
          "entity_type": "threads",
          "entity_id_field": "thread_id",
          "recipients": ["role:support_admin", "role:support"]
        },
        "continue_on_error": true
      }
    ],
    "metadata": {
      "note": "Thread escalation_status is already set to 'pending' by agent-runner before this pipeline fires. Ticket status is updated by the calling function (custom_support-triage) from agentMsg.data.escalated."
    },
    "ownership": "platform",
    "is_system": true,
    "is_active": true
  },
  {
    "name": "auto_embed_on_publish",
    "description": "Triggered when a KB article or course lesson is published. Generates embeddings via the core embedding engine.",
    "trigger_type": "event",
    "config": { "event": "item_updated", "scope": "account" },
    "stages": [
      {
        "stage_type": "generate_embeddings",
        "config": { "item_id": "{{trigger.entity_id}}", "force_regenerate": false },
        "continue_on_error": true
      }
    ],
    "metadata": { "note": "The generate_embeddings stage type calls embeddings.generate internally. item_id is resolved from trigger data." },
    "ownership": "platform",
    "is_system": true,
    "is_active": true
  },
  {
    "name": "auto_revoke_on_unpublish",
    "description": "Triggered when a KB article or course lesson is unpublished. Revokes embeddings so they are excluded from retrieval.",
    "trigger_type": "event",
    "config": { "event": "item_updated", "scope": "account" },
    "stages": [
      {
        "stage_type": "http_request",
        "config": {
          "method": "POST",
          "url": "/.netlify/functions/embeddings",
          "body": { "action": "revoke", "document_id": "{{trigger.entity_id}}" }
        },
        "continue_on_error": true
      }
    ],
    "metadata": { "note": "Calls embeddings.revoke to set revoked_at on all embeddings for the item." },
    "ownership": "platform",
    "is_system": true,
    "is_active": true
  },
  {
    "name": "capture_ai_interaction",
    "description": "Triggered after each AI agent message. Creates an ai_feedback_record item for feedback capture and training data.",
    "trigger_type": "event",
    "config": { "event": "message_created", "scope": "account" },
    "stages": [
      {
        "stage_type": "http_request",
        "config": {
          "method": "POST",
          "url": "/.netlify/functions/ai-interactions",
          "body": { "action": "create_interaction", "ai_message_id": "{{trigger.entity_id}}", "thread_id": "{{trigger.thread_id}}" }
        },
        "continue_on_error": true
      }
    ],
    "metadata": { "note": "Only fires for agent messages (trigger filter: data->>'sender_type' = 'agent')." },
    "ownership": "platform",
    "is_system": true,
    "is_active": true
  },
  {
    "name": "kb_candidate_review",
    "description": "Triggered when a support ticket is flagged as a KB candidate after case analysis. Notifies support_admin to review.",
    "trigger_type": "event",
    "config": { "event": "item_updated", "scope": "account" },
    "stages": [
      {
        "stage_type": "send_notification",
        "config": {
          "message": "A support ticket has been flagged as a KB candidate. Review and approve the draft KB article.",
          "entity_type": "items",
          "entity_id_field": "entity_id",
          "recipients": ["role:support_admin"]
        },
        "continue_on_error": true
      }
    ],
    "metadata": { "note": "Trigger filter: type_slug = 'support_ticket', data->>'ca_kb_candidate' = 'true'." },
    "ownership": "platform",
    "is_system": true,
    "is_active": true
  }
]
