{
  "name": "List Match + Outreach Email",
  "goal": "Score a request against a KG list of candidates, pick the top match, send a personalized outreach email on approval, and store the outcome.",
  "description": "Pattern 08 (composed email with approval) + pattern 09 (KG outcome storage). Works for any domain where you match one input against a KG list (mentors, suppliers, partners, investors, properties). Requires an outreachProfile input page and a connected email account (Gmail or Outlook).",
  "status": "draft",
  "context": {
    "executionInputConfig": {
      "title": "Match + Email",
      "description": "Request to match against a KG list.",
      "runCTALabel": "Find match",
      "fields": [
        { "name": "requester_name", "label": "Requester name", "type": "text", "required": true },
        { "name": "requester_email", "label": "Requester email", "type": "text", "required": true },
        { "name": "request", "label": "Request / need", "type": "textarea", "required": true },
        { "name": "candidate_list_key", "label": "KG list key (candidate pool)", "type": "text", "required": true }
      ]
    },
    "inputPages": [
      {
        "title": "Outreach Profile",
        "pathname": "outreach-profile",
        "configuration": {
          "contextKey": "outreachProfile",
          "shortDescriptionFields": ["name", "fromEmail"],
          "fields": [
            { "name": "name", "label": "Sender name", "type": "text", "required": true },
            { "name": "fromEmailLabel", "label": "From name", "type": "text", "required": true },
            { "name": "fromEmail", "label": "From email", "type": "connected_emails_selector_multiple", "required": true },
            { "name": "replyToEmail", "label": "Reply-to (optional)", "type": "text" }
          ]
        }
      }
    ]
  },
  "steps": [
    {
      "id": "start",
      "type": "trigger",
      "name": "Manual Start",
      "pipelineStepStartConditions": { "trigger": { "type": "manual" } },
      "next": { "stepId": "read-candidates" }
    },
    {
      "id": "read-candidates",
      "type": "appAction",
      "name": "Read Candidate Pool",
      "app": { "id": "kg", "actionId": "kg.read-list", "source": "native" },
      "stepInputData": {
        "listKey": "{{input.candidate_list_key}}",
        "limit": "500"
      },
      "next": { "stepId": "rank-top-3" }
    },
    {
      "id": "rank-top-3",
      "type": "aiAction",
      "name": "Rank Top 3 Candidates",
      "pipelineStepPrompt": {
        "template": "Score each candidate's fit for this request on expertise, relevance, and past outcomes. Return the top 3 with the best as top_match.\n\nRequest: {{input.request}}\n\nCandidate pool: {{steps.read-candidates.listEntries}}",
        "responseStructure": {
          "matches": "array of { candidate_id, candidate_name, candidate_email, fit_score (0-100), reasoning }",
          "top_match": "object matching the best candidate"
        }
      },
      "creditCost": 12,
      "next": { "stepId": "send-outreach-email" }
    },
    {
      "id": "send-outreach-email",
      "type": "aiAction",
      "name": "Send Outreach Email",
      "pipelineStepPrompt": {
        "type": "email",
        "template": "Draft a personalized outreach email to {{steps.rank-top-3.top_match.candidate_name}}, introducing {{input.requester_name}} and their request: {{input.request}}.\n\nKeep it warm, concise, and professional.",
        "responseStructure": {
          "email": {
            "from": "{{context.outreachProfile.fromEmail}}",
            "to": "{{steps.rank-top-3.top_match.candidate_email}}",
            "subject": "",
            "body": "",
            "bodyType": "html"
          }
        }
      },
      "renderer": {
        "type": "Email",
        "config": { "fromContextKey": "outreachProfile" }
      },
      "integrations": [
        {
          "type": "oneOf",
          "label": "Email",
          "connectorType": "email",
          "options": [
            { "name": "Gmail", "url": "https://gmail.com", "isUserAccountConnectionRequired": true },
            { "name": "Outlook", "url": "https://outlook.com", "isUserAccountConnectionRequired": true }
          ],
          "selectionHint": "preferConnected"
        }
      ],
      "onApproval": {
        "executedText": "Email sent by {{name}} at {{date}}",
        "failedText": "Email failed to send.",
        "action": "schedule-email"
      },
      "creditCost": 5,
      "next": { "stepId": "store-outcome", "conditions": { "approvalRequired": true } }
    },
    {
      "id": "store-outcome",
      "type": "knowledgeSync",
      "name": "Store Match Outcome",
      "knowledgeSync": {
        "source": { "stepId": "rank-top-3", "resultsPath": "matches" },
        "listKey": "match_outcomes",
        "fieldMapping": {
          "candidate_id": "candidate_id",
          "candidate_name": "candidate_name",
          "fit_score": "fit_score",
          "reasoning": "reasoning"
        }
      },
      "next": { "stepId": "done" }
    },
    {
      "id": "done",
      "type": "milestone",
      "name": "Done"
    }
  ]
}
