{
  "schemaVersion": "1.0.0",
  "workflowVersion": 1,
  "workflow": {
    "id": "wf-key-inventory-report-flow",
    "name": "Key manager — Key Inventory Report (PDF, branded)",
    "description": "End-to-end report pipeline. manual_trigger fans out two parallel branches. (A) Branding: text_input -> web_browse (headless Chromium loads a brand site) -> prompt_template + llm_call + output_parser turn the page's title, meta tags, image URLs and text into a JSON brand kit (brand, logo URL, primary and accent color, font, tagline). (B) Keys: mcp_tool calls the key service's `list_keys` tool -> code_node (Python, stdlib only) counts the keys by cloud, by algorithm and by age (<30d / 30-90d / 90-180d / 180-365d / >365d). The two branches meet at three chart nodes (Vega-Lite, server-side SVG): keys by cloud (bar), keys by algorithm (donut) and key aging (bar); the two bar charts take their colors from the brand kit. A final prompt_template places the brand values and the chart SVGs in one HTML page, pdf_output prints it with Chromium, and the Output returns the PDF as a data URL. Before it works for real: (1) point the mcp_tool connection at your key service's MCP server (add a secretRef if it needs one), (2) set the text_input to your brand URL (defaults to https://example.com), (3) give the llm_call a provider and key. A user_prompt sent with the run replaces the brand URL.",
    "schemaVersion": "1.0.0",
    "workflowVersion": 1,
    "nodes": [
      {
        "id": "trigger_1",
        "type": "manual_trigger",
        "name": "Run report",
        "position": { "x": 70, "y": 320 },
        "config": {}
      },

      {
        "id": "brand_url",
        "type": "text_input",
        "name": "Brand URL",
        "position": { "x": 360, "y": 120 },
        "config": {
          "text": "https://example.com"
        }
      },
      {
        "id": "web_browse_1",
        "type": "web_browse",
        "name": "Fetch Brand Site",
        "position": { "x": 670, "y": 120 },
        "config": {
          "urlTemplate": "{{user_prompt}}",
          "waitUntil": "networkidle",
          "timeoutMs": 45000,
          "viewportWidth": 1440,
          "viewportHeight": 900,
          "screenshot": true,
          "extractText": true,
          "extractLinks": false,
          "extractImages": true,
          "outputKey": "brandSite"
        }
      },
      {
        "id": "brand_prompt",
        "type": "prompt_template",
        "name": "Extract Branding Prompt",
        "position": { "x": 980, "y": 120 },
        "config": {
          "template": "You are a brand-asset extractor. Below is data extracted from one web page: its title, meta tags, image URLs and visible text. You cannot see the page's styles or a screenshot, so only report a color or a font when the data below states it (for example a theme-color meta tag or text that names it); otherwise use an empty string.\n\nPAGE TITLE: {{title}}\n\nMETA DESCRIPTION: {{ meta[\"description\"] }}\n\nMETA OG IMAGE: {{ meta[\"og:image\"] }}\n\nMETA THEME COLOR: {{ meta[\"theme-color\"] }}\n\nKEY IMAGES (first 10):\n{{ images.slice(0, 10) }}\n\nPAGE TEXT (first 4000 chars):\n{{ text.slice(0, 4000) }}\n\nReturn ONLY a JSON object on one line, no markdown:\n{\"brand\":\"<official brand name>\",\"primaryLogoUrl\":\"<URL of the main logo image, or empty>\",\"primaryColor\":\"<hex color stated in the data, or empty>\",\"accentColor\":\"<hex, or empty>\",\"fontFamily\":\"<typeface named in the data, or empty>\",\"tagline\":\"<short slogan if present>\"}",
          "outputKey": "brand_extraction_prompt"
        }
      },
      {
        "id": "brand_llm",
        "type": "llm_call",
        "name": "Distill Branding",
        "position": { "x": 1290, "y": 120 },
        "config": {
          "provider": {
            "providerId": "openai",
            "model": "gpt-4o-mini",
            "temperature": 0
          },
          "promptKey": "brand_extraction_prompt",
          "outputKey": "branding_raw"
        }
      },
      {
        "id": "brand_parse",
        "type": "output_parser",
        "name": "Parse Brand JSON",
        "position": { "x": 1600, "y": 120 },
        "config": {
          "mode": "json_schema",
          "inputKey": "branding_raw",
          "parsingMode": "lenient",
          "jsonSchema": "{\"type\":\"object\",\"properties\":{\"brand\":{\"type\":\"string\"},\"primaryLogoUrl\":{\"type\":\"string\"},\"primaryColor\":{\"type\":\"string\"},\"accentColor\":{\"type\":\"string\"},\"fontFamily\":{\"type\":\"string\"},\"tagline\":{\"type\":\"string\"}}}"
        }
      },

      {
        "id": "mcp_list_keys",
        "type": "mcp_tool",
        "name": "Key manager list_keys",
        "position": { "x": 360, "y": 520 },
        "config": {
          "serverId": "http_mcp",
          "toolName": "list_keys",
          "argsTemplate": "{\"limit\":1000}",
          "connection": {
            "transport": "http_streamable",
            "endpoint": "http://key-manager-mcp.internal/mcp"
          }
        }
      },
      {
        "id": "aggregate_keys",
        "type": "code_node",
        "name": "Aggregate Keys",
        "position": { "x": 670, "y": 520 },
        "config": {
          "language": "python",
          "timeout": 5000,
          "code": "from datetime import datetime, timezone\n\n# items[0] holds the MCP Tool node's output. tool_output is the tool's\n# text, usually JSON; structured is its structuredContent, when it sends one.\nsrc = items[0] if items else {}\npayload = src.get('structured') or src.get('tool_output') if isinstance(src, dict) else None\nif isinstance(payload, str):\n    try:\n        payload = json.loads(payload)\n    except ValueError:\n        payload = {}\nif isinstance(payload, list):\n    keys = payload\nelif isinstance(payload, dict):\n    keys = payload.get('keys') or payload.get('resources') or (payload.get('result') or {}).get('keys') or []\nelse:\n    keys = []\n\nby_cloud = {}\nby_algorithm = {}\nbuckets = ['<30d', '30-90d', '90-180d', '180-365d', '>365d']\nageing = {b: 0 for b in buckets}\nnow = datetime.now(timezone.utc)\n\nfor k in keys:\n    if not isinstance(k, dict):\n        continue\n    labels = k.get('labels') or k.get('metadata') or {}\n    cloud = (labels.get('cloud') if isinstance(labels, dict) else None) or k.get('cloud') or 'unknown'\n    algo = k.get('algorithm') or k.get('algo') or 'unknown'\n    by_cloud[cloud] = by_cloud.get(cloud, 0) + 1\n    by_algorithm[algo] = by_algorithm.get(algo, 0) + 1\n\n    created_raw = k.get('createdAt') or k.get('created_at') or k.get('creationDate') or ''\n    age_days = 0\n    try:\n        if isinstance(created_raw, str) and created_raw:\n            dt = datetime.fromisoformat(created_raw.replace('Z', '+00:00'))\n            age_days = (now - dt).days\n    except Exception:\n        age_days = 0\n    if age_days < 30:\n        ageing['<30d'] += 1\n    elif age_days < 90:\n        ageing['30-90d'] += 1\n    elif age_days < 180:\n        ageing['90-180d'] += 1\n    elif age_days < 365:\n        ageing['180-365d'] += 1\n    else:\n        ageing['>365d'] += 1\n\nresult = {\n    'byCloud': [{'category': c, 'count': n} for c, n in sorted(by_cloud.items(), key=lambda x: -x[1])],\n    'byAlgorithm': [{'algorithm': a, 'count': n} for a, n in sorted(by_algorithm.items(), key=lambda x: -x[1])],\n    'ageing': [{'bucket': b, 'count': ageing[b]} for b in buckets],\n    'totalKeys': len(keys),\n    'generatedAt': now.isoformat()\n}\n"
        }
      },

      {
        "id": "chart_cloud",
        "type": "chart",
        "name": "Keys by Cloud",
        "position": { "x": 980, "y": 440 },
        "config": {
          "specTemplate": "{\"mark\": {\"type\": \"bar\", \"cornerRadiusTopLeft\": 3, \"cornerRadiusTopRight\": 3}, \"encoding\": {\"x\": {\"field\": \"category\", \"type\": \"nominal\", \"title\": \"Cloud\", \"sort\": \"-y\"}, \"y\": {\"field\": \"count\", \"type\": \"quantitative\", \"title\": \"Keys\"}, \"color\": {\"value\": \"{{ parsed.primaryColor || '#4c78a8' }}\"}}}",
          "dataPath": "byCloud",
          "width": 380,
          "height": 220,
          "outputKey": "cloudChart"
        }
      },
      {
        "id": "chart_algo",
        "type": "chart",
        "name": "Keys by Algorithm",
        "position": { "x": 980, "y": 700 },
        "config": {
          "spec": {
            "mark": { "type": "arc", "innerRadius": 50 },
            "encoding": {
              "theta": { "field": "count", "type": "quantitative" },
              "color": { "field": "algorithm", "type": "nominal", "scale": { "scheme": "blues" } }
            }
          },
          "dataPath": "byAlgorithm",
          "width": 380,
          "height": 220,
          "outputKey": "algoChart"
        }
      },
      {
        "id": "chart_age",
        "type": "chart",
        "name": "Key Aging",
        "position": { "x": 980, "y": 960 },
        "config": {
          "specTemplate": "{\"mark\": {\"type\": \"bar\"}, \"encoding\": {\"x\": {\"field\": \"bucket\", \"type\": \"nominal\", \"sort\": null, \"title\": \"Age\"}, \"y\": {\"field\": \"count\", \"type\": \"quantitative\", \"title\": \"Keys\"}, \"color\": {\"value\": \"{{ parsed.accentColor || '#f58518' }}\"}}}",
          "dataPath": "ageing",
          "width": 380,
          "height": 220,
          "outputKey": "ageChart"
        }
      },

      {
        "id": "report_html",
        "type": "prompt_template",
        "name": "Compose Branded HTML",
        "position": { "x": 1290, "y": 700 },
        "config": {
          "template": "<!doctype html>\n<html><head><meta charset=\"utf-8\">\n<title>{{parsed.brand}} — Key Inventory Report</title>\n<style>\n  body { font-family: {{parsed.fontFamily}}, system-ui, sans-serif; margin: 32px; color: #0f172a; }\n  header { display: flex; align-items: center; gap: 16px; border-bottom: 3px solid {{parsed.primaryColor}}; padding-bottom: 12px; }\n  header img { height: 48px; }\n  h1 { color: {{parsed.primaryColor}}; margin: 0; font-size: 28px; }\n  h1 small { color: #64748b; font-weight: 400; font-size: 14px; display: block; margin-top: 4px; }\n  .tagline { color: #64748b; font-style: italic; margin: 4px 0 0; }\n  .summary { background: #f8fafc; border-left: 4px solid {{parsed.primaryColor}}; padding: 16px; margin: 24px 0; }\n  .summary .num { font-size: 32px; font-weight: 700; color: {{parsed.primaryColor}}; }\n  h2 { color: {{parsed.primaryColor}}; margin-top: 32px; border-bottom: 1px solid #e2e8f0; padding-bottom: 6px; }\n  .chart { padding: 12px 0; }\n  footer { margin-top: 48px; padding-top: 12px; border-top: 1px solid #e2e8f0; color: #94a3b8; font-size: 11px; }\n</style></head>\n<body>\n<header>\n  <img src=\"{{parsed.primaryLogoUrl}}\" alt=\"{{parsed.brand}}\">\n  <div>\n    <h1>Key Inventory Report<small>{{parsed.brand}} · Key manager</small></h1>\n    <p class=\"tagline\">{{parsed.tagline}}</p>\n  </div>\n</header>\n\n<div class=\"summary\">\n  <div class=\"num\">{{totalKeys}}</div>\n  <div>keys under management as of {{generatedAt}}</div>\n</div>\n\n<h2>Keys by Cloud Provider</h2>\n<div class=\"chart\">{{cloudChart.svg}}</div>\n\n<h2>Keys by Algorithm</h2>\n<div class=\"chart\">{{algoChart.svg}}</div>\n\n<h2>Key Aging Distribution</h2>\n<div class=\"chart\">{{ageChart.svg}}</div>\n\n<footer>Generated by L2M from live key manager data. Source: key manager list_keys MCP call. Branding extracted from {{user_prompt}}.</footer>\n</body></html>",
          "outputKey": "html"
        }
      },
      {
        "id": "pdf_1",
        "type": "pdf_output",
        "name": "Render PDF",
        "position": { "x": 1600, "y": 700 },
        "config": {
          "renderMode": "html",
          "inputKey": "html",
          "pageFormat": "A4",
          "printBackground": true,
          "filenameTemplate": "{{results.brand_parse.parsed.brand}}-key-inventory-{{session_id}}.pdf",
          "outputKey": "pdf"
        }
      },
      {
        "id": "output_1",
        "type": "output",
        "name": "PDF Report",
        "position": { "x": 1910, "y": 700 },
        "config": {
          "responseTemplate": "{{pdf.downloadUrl}}",
          "outputKey": "result"
        }
      }
    ],
    "edges": [
      { "id": "e_trigger_brand", "source": "trigger_1", "target": "brand_url" },
      { "id": "e_brand_url", "source": "brand_url", "target": "web_browse_1" },
      { "id": "e_browse_prompt", "source": "web_browse_1", "target": "brand_prompt" },
      { "id": "e_brand_prompt_llm", "source": "brand_prompt", "target": "brand_llm" },
      { "id": "e_brand_llm_parse", "source": "brand_llm", "target": "brand_parse" },

      { "id": "e_trigger_mcp", "source": "trigger_1", "target": "mcp_list_keys" },
      { "id": "e_mcp_agg", "source": "mcp_list_keys", "target": "aggregate_keys" },

      { "id": "e_agg_cloud", "source": "aggregate_keys", "target": "chart_cloud" },
      { "id": "e_agg_algo", "source": "aggregate_keys", "target": "chart_algo" },
      { "id": "e_agg_age", "source": "aggregate_keys", "target": "chart_age" },

      { "id": "e_parse_report", "source": "brand_parse", "target": "chart_cloud" },
      { "id": "e_parse_algo", "source": "brand_parse", "target": "chart_algo" },
      { "id": "e_parse_age", "source": "brand_parse", "target": "chart_age" },

      { "id": "e_cloud_report", "source": "chart_cloud", "target": "report_html" },
      { "id": "e_algo_report", "source": "chart_algo", "target": "report_html" },
      { "id": "e_age_report", "source": "chart_age", "target": "report_html" },
      { "id": "e_parse_html", "source": "brand_parse", "target": "report_html" },
      { "id": "e_agg_html", "source": "aggregate_keys", "target": "report_html" },
      { "id": "e_url_html", "source": "brand_url", "target": "report_html" },

      { "id": "e_report_pdf", "source": "report_html", "target": "pdf_1" },
      { "id": "e_pdf_out", "source": "pdf_1", "target": "output_1" }
    ]
  },
  "exportedAt": "2026-05-11T00:00:00.000Z"
}
