{
  "_scaffold": "funnel-orchestrator",
  "_description": "Scheduled KG funnel coordinator: read new rows, call child scoring workflow, read qualified rows, queue outreach workflow, compute KPIs, and finish with a digest.",
  "name": "Scheduled Funnel Orchestrator",
  "goal": "Run a recurring funnel pass across already-working child workflows and report what moved.",
  "description": "Use after sourcing, scoring, contact discovery, and outreach workflows have each passed manual validation.",
  "status": "draft",
  "context": {
    "executionInputConfig": {
      "title": "Run Funnel",
      "description": "Manual override for the scheduled orchestrator.",
      "runCTALabel": "Run funnel",
      "fields": [
        { "name": "source_list_key", "label": "Source list key", "type": "text", "required": true, "defaultValue": "leads", "description": "Knowledge list key (e.g. \"leads\", \"startups\")." },
        { "name": "new_status", "label": "New status", "type": "text", "required": true, "defaultValue": "new" },
        { "name": "qualified_status", "label": "Qualified status", "type": "text", "required": true, "defaultValue": "qualified" },
        { "name": "score_workflow_id", "label": "Scoring workflow ID", "type": "text", "required": true, "description": "Workflow ID for the child scoring workflow." },
        { "name": "outreach_workflow_id", "label": "Outreach workflow ID", "type": "text", "required": true, "description": "Workflow ID for the child outreach workflow." }
      ]
    }
  },
  "steps": [
    {
      "id": "start",
      "type": "trigger",
      "name": "Scheduled funnel run",
      "pipelineStepStartConditions": {
        "trigger": {
          "type": "schedule",
          "description": "Run weekly.",
          "config": { "frequency": "weekly", "dayOfWeek": "monday", "time": "09:00" }
        }
      },
      "next": { "stepId": "read-new" }
    },
    {
      "id": "read-new",
      "type": "appAction",
      "name": "Read new rows",
      "app": { "id": "kg", "actionId": "kg.read-list", "source": "native" },
      "stepInputData": {
        "listKey": "{{input.source_list_key}}",
        "status": "{{input.new_status || 'new'}}",
        "limit": "25"
      },
      "next": { "stepId": "score-rows" }
    },
    {
      "id": "score-rows",
      "type": "appAction",
      "name": "Run scoring workflow",
      "app": { "id": "agentled", "actionId": "agentled.call-workflow", "source": "native" },
      "stepInputData": {
        "workflowId": "{{input.score_workflow_id}}",
        "executionInputData": "{\"row\": {{newRow}}, \"listKey\": \"{{input.source_list_key}}\"}",
        "timeoutMinutes": "20"
      },
      "loopConfig": {
        "source": "executionContent",
        "config": { "stepId": "read-new", "field": "listEntries" },
        "ItemAlias": "newRow",
        "loopCompletion": { "criteria": "all_items_processed" }
      },
      "creditCost": 0,
      "next": { "stepId": "read-qualified" }
    },
    {
      "id": "read-qualified",
      "type": "appAction",
      "name": "Read qualified rows",
      "app": { "id": "kg", "actionId": "kg.read-list", "source": "native" },
      "stepInputData": {
        "listKey": "{{input.source_list_key}}",
        "status": "{{input.qualified_status || 'qualified'}}",
        "limit": "25"
      },
      "entryConditions": {
        "onCriteriaFail": "wait",
        "conditionText": "Wait for scoring child workflows to complete.",
        "criteria": [
          { "type": "loop_completion", "stepId": "score-rows", "operator": "==", "value": true }
        ]
      },
      "next": { "stepId": "queue-outreach" }
    },
    {
      "id": "queue-outreach",
      "type": "appAction",
      "name": "Queue outreach workflow",
      "app": { "id": "agentled", "actionId": "agentled.start-workflow", "source": "native" },
      "stepInputData": {
        "workflowId": "{{input.outreach_workflow_id}}",
        "executionInputData": "{\"row\": {{qualifiedRow}}, \"listKey\": \"{{input.source_list_key}}\"}"
      },
      "entryConditions": {
        "onCriteriaFail": "skip",
        "conditionText": "Skip outreach if no qualified rows are available.",
        "criteria": [
          { "variable": "{{steps.read-qualified.count}}", "operator": ">", "value": 0 }
        ]
      },
      "loopConfig": {
        "source": "executionContent",
        "config": { "stepId": "read-qualified", "field": "listEntries" },
        "ItemAlias": "qualifiedRow",
        "loopCompletion": { "criteria": "all_items_processed" }
      },
      "creditCost": 0,
      "next": { "stepId": "summarize-run" }
    },
    {
      "id": "summarize-run",
      "type": "aiAction",
      "name": "Summarize run",
      "pipelineStepPrompt": {
        "template": "Create a concise internal funnel digest.\n\nNew rows read: {{steps.read-new.count}}\nQualified rows read: {{steps.read-qualified.count}}\nScoring results: {{steps.score-rows}}\nOutreach starts: {{steps.queue-outreach}}\n\nReturn JSON only.",
        "responseStructure": {
          "summary": "string",
          "newCount": "number",
          "qualifiedCount": "number",
          "outreachQueuedCount": "number",
          "recommendations": ["string"]
        }
      },
      "creditCost": 3,
      "next": { "stepId": "done" }
    },
    {
      "id": "done",
      "type": "milestone",
      "name": "Funnel run complete"
    }
  ]
}
