{
  "data": {
    "edges": [
      {
        "animated": false,
        "className": "",
        "data": {
          "sourceHandle": {
            "dataType": "msp_ticket_etl",
            "id": "msp_ticket_etl-vH1pD",
            "name": "tickets_clean",
            "output_types": [
              "DataFrame"
            ]
          },
          "targetHandle": {
            "fieldName": "tickets_df",
            "id": "msp_metrics_builder-QvFjh",
            "inputTypes": [
              "DataFrame"
            ],
            "type": "other"
          }
        },
        "id": "reactflow__edge-msp_ticket_etl-vH1pD{œdataTypeœ:œmsp_ticket_etlœ,œidœ:œmsp_ticket_etl-vH1pDœ,œnameœ:œtickets_cleanœ,œoutput_typesœ:[œDataFrameœ]}-msp_metrics_builder-QvFjh{œfieldNameœ:œtickets_dfœ,œidœ:œmsp_metrics_builder-QvFjhœ,œinputTypesœ:[œDataFrameœ],œtypeœ:œotherœ}",
        "selected": false,
        "source": "msp_ticket_etl-vH1pD",
        "sourceHandle": "{œdataTypeœ:œmsp_ticket_etlœ,œidœ:œmsp_ticket_etl-vH1pDœ,œnameœ:œtickets_cleanœ,œoutput_typesœ:[œDataFrameœ]}",
        "target": "msp_metrics_builder-QvFjh",
        "targetHandle": "{œfieldNameœ:œtickets_dfœ,œidœ:œmsp_metrics_builder-QvFjhœ,œinputTypesœ:[œDataFrameœ],œtypeœ:œotherœ}"
      },
      {
        "animated": false,
        "className": "",
        "data": {
          "sourceHandle": {
            "dataType": "msp_metrics_builder",
            "id": "msp_metrics_builder-QvFjh",
            "name": "insights_context",
            "output_types": [
              "Message"
            ]
          },
          "targetHandle": {
            "fieldName": "insights",
            "id": "Prompt Template-KjO8V",
            "inputTypes": [
              "Message"
            ],
            "type": "str"
          }
        },
        "id": "reactflow__edge-msp_metrics_builder-QvFjh{œdataTypeœ:œmsp_metrics_builderœ,œidœ:œmsp_metrics_builder-QvFjhœ,œnameœ:œinsights_contextœ,œoutput_typesœ:[œMessageœ]}-Prompt Template-KjO8V{œfieldNameœ:œinsightsœ,œidœ:œPrompt Template-KjO8Vœ,œinputTypesœ:[œMessageœ],œtypeœ:œstrœ}",
        "selected": false,
        "source": "msp_metrics_builder-QvFjh",
        "sourceHandle": "{œdataTypeœ:œmsp_metrics_builderœ,œidœ:œmsp_metrics_builder-QvFjhœ,œnameœ:œinsights_contextœ,œoutput_typesœ:[œMessageœ]}",
        "target": "Prompt Template-KjO8V",
        "targetHandle": "{œfieldNameœ:œinsightsœ,œidœ:œPrompt Template-KjO8Vœ,œinputTypesœ:[œMessageœ],œtypeœ:œstrœ}"
      },
      {
        "animated": false,
        "className": "",
        "data": {
          "sourceHandle": {
            "dataType": "Prompt Template",
            "id": "Prompt Template-KjO8V",
            "name": "prompt",
            "output_types": [
              "Message"
            ]
          },
          "targetHandle": {
            "fieldName": "input_value",
            "id": "Agent-0r7mv",
            "inputTypes": [
              "Message"
            ],
            "type": "str"
          }
        },
        "id": "reactflow__edge-Prompt Template-KjO8V{œdataTypeœ:œPrompt Templateœ,œidœ:œPrompt Template-KjO8Vœ,œnameœ:œpromptœ,œoutput_typesœ:[œMessageœ]}-Agent-0r7mv{œfieldNameœ:œinput_valueœ,œidœ:œAgent-0r7mvœ,œinputTypesœ:[œMessageœ],œtypeœ:œstrœ}",
        "selected": false,
        "source": "Prompt Template-KjO8V",
        "sourceHandle": "{œdataTypeœ:œPrompt Templateœ,œidœ:œPrompt Template-KjO8Vœ,œnameœ:œpromptœ,œoutput_typesœ:[œMessageœ]}",
        "target": "Agent-0r7mv",
        "targetHandle": "{œfieldNameœ:œinput_valueœ,œidœ:œAgent-0r7mvœ,œinputTypesœ:[œMessageœ],œtypeœ:œstrœ}"
      },
      {
        "animated": false,
        "className": "",
        "data": {
          "sourceHandle": {
            "dataType": "msp_invoice_metrics_builder",
            "id": "msp_invoice_metrics_builder-StB0j",
            "name": "insights_context",
            "output_types": [
              "Message"
            ]
          },
          "targetHandle": {
            "fieldName": "insights",
            "id": "Prompt Template-KySSd",
            "inputTypes": [
              "Message"
            ],
            "type": "str"
          }
        },
        "id": "reactflow__edge-msp_invoice_metrics_builder-StB0j{œdataTypeœ:œmsp_invoice_metrics_builderœ,œidœ:œmsp_invoice_metrics_builder-StB0jœ,œnameœ:œinsights_contextœ,œoutput_typesœ:[œMessageœ]}-Prompt Template-KySSd{œfieldNameœ:œinsightsœ,œidœ:œPrompt Template-KySSdœ,œinputTypesœ:[œMessageœ],œtypeœ:œstrœ}",
        "selected": false,
        "source": "msp_invoice_metrics_builder-StB0j",
        "sourceHandle": "{œdataTypeœ:œmsp_invoice_metrics_builderœ,œidœ:œmsp_invoice_metrics_builder-StB0jœ,œnameœ:œinsights_contextœ,œoutput_typesœ:[œMessageœ]}",
        "target": "Prompt Template-KySSd",
        "targetHandle": "{œfieldNameœ:œinsightsœ,œidœ:œPrompt Template-KySSdœ,œinputTypesœ:[œMessageœ],œtypeœ:œstrœ}"
      },
      {
        "animated": false,
        "className": "",
        "data": {
          "sourceHandle": {
            "dataType": "msp_invoice_metrics_builder",
            "id": "msp_invoice_metrics_builder-gxG08",
            "name": "revenue_by_month",
            "output_types": [
              "DataFrame"
            ]
          },
          "targetHandle": {
            "fieldName": "revenue_by_month",
            "id": "msp_invoice_llm_context_builder-5yIEm",
            "inputTypes": [
              "DataFrame"
            ],
            "type": "other"
          }
        },
        "id": "reactflow__edge-msp_invoice_metrics_builder-gxG08{œdataTypeœ:œmsp_invoice_metrics_builderœ,œidœ:œmsp_invoice_metrics_builder-gxG08œ,œnameœ:œrevenue_by_monthœ,œoutput_typesœ:[œDataFrameœ]}-msp_invoice_llm_context_builder-5yIEm{œfieldNameœ:œrevenue_by_monthœ,œidœ:œmsp_invoice_llm_context_builder-5yIEmœ,œinputTypesœ:[œDataFrameœ],œtypeœ:œotherœ}",
        "selected": false,
        "source": "msp_invoice_metrics_builder-gxG08",
        "sourceHandle": "{œdataTypeœ:œmsp_invoice_metrics_builderœ,œidœ:œmsp_invoice_metrics_builder-gxG08œ,œnameœ:œrevenue_by_monthœ,œoutput_typesœ:[œDataFrameœ]}",
        "target": "msp_invoice_llm_context_builder-5yIEm",
        "targetHandle": "{œfieldNameœ:œrevenue_by_monthœ,œidœ:œmsp_invoice_llm_context_builder-5yIEmœ,œinputTypesœ:[œDataFrameœ],œtypeœ:œotherœ}"
      },
      {
        "animated": false,
        "className": "",
        "data": {
          "sourceHandle": {
            "dataType": "msp_invoice_metrics_builder",
            "id": "msp_invoice_metrics_builder-66D4o",
            "name": "revenue_by_client",
            "output_types": [
              "DataFrame"
            ]
          },
          "targetHandle": {
            "fieldName": "revenue_by_client",
            "id": "msp_invoice_llm_context_builder-5yIEm",
            "inputTypes": [
              "DataFrame"
            ],
            "type": "other"
          }
        },
        "id": "reactflow__edge-msp_invoice_metrics_builder-66D4o{œdataTypeœ:œmsp_invoice_metrics_builderœ,œidœ:œmsp_invoice_metrics_builder-66D4oœ,œnameœ:œrevenue_by_clientœ,œoutput_typesœ:[œDataFrameœ]}-msp_invoice_llm_context_builder-5yIEm{œfieldNameœ:œrevenue_by_clientœ,œidœ:œmsp_invoice_llm_context_builder-5yIEmœ,œinputTypesœ:[œDataFrameœ],œtypeœ:œotherœ}",
        "selected": false,
        "source": "msp_invoice_metrics_builder-66D4o",
        "sourceHandle": "{œdataTypeœ:œmsp_invoice_metrics_builderœ,œidœ:œmsp_invoice_metrics_builder-66D4oœ,œnameœ:œrevenue_by_clientœ,œoutput_typesœ:[œDataFrameœ]}",
        "target": "msp_invoice_llm_context_builder-5yIEm",
        "targetHandle": "{œfieldNameœ:œrevenue_by_clientœ,œidœ:œmsp_invoice_llm_context_builder-5yIEmœ,œinputTypesœ:[œDataFrameœ],œtypeœ:œotherœ}"
      },
      {
        "animated": false,
        "className": "",
        "data": {
          "sourceHandle": {
            "dataType": "msp_invoice_metrics_builder",
            "id": "msp_invoice_metrics_builder-8zhSm",
            "name": "revenue_concentration",
            "output_types": [
              "DataFrame"
            ]
          },
          "targetHandle": {
            "fieldName": "revenue_concentration",
            "id": "msp_invoice_llm_context_builder-5yIEm",
            "inputTypes": [
              "DataFrame"
            ],
            "type": "other"
          }
        },
        "id": "reactflow__edge-msp_invoice_metrics_builder-8zhSm{œdataTypeœ:œmsp_invoice_metrics_builderœ,œidœ:œmsp_invoice_metrics_builder-8zhSmœ,œnameœ:œrevenue_concentrationœ,œoutput_typesœ:[œDataFrameœ]}-msp_invoice_llm_context_builder-5yIEm{œfieldNameœ:œrevenue_concentrationœ,œidœ:œmsp_invoice_llm_context_builder-5yIEmœ,œinputTypesœ:[œDataFrameœ],œtypeœ:œotherœ}",
        "selected": false,
        "source": "msp_invoice_metrics_builder-8zhSm",
        "sourceHandle": "{œdataTypeœ:œmsp_invoice_metrics_builderœ,œidœ:œmsp_invoice_metrics_builder-8zhSmœ,œnameœ:œrevenue_concentrationœ,œoutput_typesœ:[œDataFrameœ]}",
        "target": "msp_invoice_llm_context_builder-5yIEm",
        "targetHandle": "{œfieldNameœ:œrevenue_concentrationœ,œidœ:œmsp_invoice_llm_context_builder-5yIEmœ,œinputTypesœ:[œDataFrameœ],œtypeœ:œotherœ}"
      },
      {
        "animated": false,
        "className": "",
        "data": {
          "sourceHandle": {
            "dataType": "msp_invoice_metrics_builder",
            "id": "msp_invoice_metrics_builder-l3Hd0",
            "name": "category_breakdown",
            "output_types": [
              "DataFrame"
            ]
          },
          "targetHandle": {
            "fieldName": "category_breakdown",
            "id": "msp_invoice_llm_context_builder-5yIEm",
            "inputTypes": [
              "DataFrame"
            ],
            "type": "other"
          }
        },
        "id": "reactflow__edge-msp_invoice_metrics_builder-l3Hd0{œdataTypeœ:œmsp_invoice_metrics_builderœ,œidœ:œmsp_invoice_metrics_builder-l3Hd0œ,œnameœ:œcategory_breakdownœ,œoutput_typesœ:[œDataFrameœ]}-msp_invoice_llm_context_builder-5yIEm{œfieldNameœ:œcategory_breakdownœ,œidœ:œmsp_invoice_llm_context_builder-5yIEmœ,œinputTypesœ:[œDataFrameœ],œtypeœ:œotherœ}",
        "selected": false,
        "source": "msp_invoice_metrics_builder-l3Hd0",
        "sourceHandle": "{œdataTypeœ:œmsp_invoice_metrics_builderœ,œidœ:œmsp_invoice_metrics_builder-l3Hd0œ,œnameœ:œcategory_breakdownœ,œoutput_typesœ:[œDataFrameœ]}",
        "target": "msp_invoice_llm_context_builder-5yIEm",
        "targetHandle": "{œfieldNameœ:œcategory_breakdownœ,œidœ:œmsp_invoice_llm_context_builder-5yIEmœ,œinputTypesœ:[œDataFrameœ],œtypeœ:œotherœ}"
      },
      {
        "animated": false,
        "className": "",
        "data": {
          "sourceHandle": {
            "dataType": "msp_invoice_llm_context_builder",
            "id": "msp_invoice_llm_context_builder-5yIEm",
            "name": "llm_context",
            "output_types": [
              "Message"
            ]
          },
          "targetHandle": {
            "fieldName": "revenue_breakdown",
            "id": "Prompt Template-KySSd",
            "inputTypes": [
              "Message"
            ],
            "type": "str"
          }
        },
        "id": "reactflow__edge-msp_invoice_llm_context_builder-5yIEm{œdataTypeœ:œmsp_invoice_llm_context_builderœ,œidœ:œmsp_invoice_llm_context_builder-5yIEmœ,œnameœ:œllm_contextœ,œoutput_typesœ:[œMessageœ]}-Prompt Template-KySSd{œfieldNameœ:œrevenue_breakdownœ,œidœ:œPrompt Template-KySSdœ,œinputTypesœ:[œMessageœ],œtypeœ:œstrœ}",
        "selected": false,
        "source": "msp_invoice_llm_context_builder-5yIEm",
        "sourceHandle": "{œdataTypeœ:œmsp_invoice_llm_context_builderœ,œidœ:œmsp_invoice_llm_context_builder-5yIEmœ,œnameœ:œllm_contextœ,œoutput_typesœ:[œMessageœ]}",
        "target": "Prompt Template-KySSd",
        "targetHandle": "{œfieldNameœ:œrevenue_breakdownœ,œidœ:œPrompt Template-KySSdœ,œinputTypesœ:[œMessageœ],œtypeœ:œstrœ}"
      },
      {
        "animated": false,
        "className": "",
        "data": {
          "sourceHandle": {
            "dataType": "Prompt Template",
            "id": "Prompt Template-KySSd",
            "name": "prompt",
            "output_types": [
              "Message"
            ]
          },
          "targetHandle": {
            "fieldName": "input_value",
            "id": "Agent-CdUIZ",
            "inputTypes": [
              "Message"
            ],
            "type": "str"
          }
        },
        "id": "reactflow__edge-Prompt Template-KySSd{œdataTypeœ:œPrompt Templateœ,œidœ:œPrompt Template-KySSdœ,œnameœ:œpromptœ,œoutput_typesœ:[œMessageœ]}-Agent-CdUIZ{œfieldNameœ:œinput_valueœ,œidœ:œAgent-CdUIZœ,œinputTypesœ:[œMessageœ],œtypeœ:œstrœ}",
        "selected": false,
        "source": "Prompt Template-KySSd",
        "sourceHandle": "{œdataTypeœ:œPrompt Templateœ,œidœ:œPrompt Template-KySSdœ,œnameœ:œpromptœ,œoutput_typesœ:[œMessageœ]}",
        "target": "Agent-CdUIZ",
        "targetHandle": "{œfieldNameœ:œinput_valueœ,œidœ:œAgent-CdUIZœ,œinputTypesœ:[œMessageœ],œtypeœ:œstrœ}"
      },
      {
        "animated": false,
        "className": "",
        "data": {
          "sourceHandle": {
            "dataType": "msp_time_entry_etl",
            "id": "msp_time_entry_etl-hIlCX",
            "name": "time_clean",
            "output_types": [
              "DataFrame"
            ]
          },
          "targetHandle": {
            "fieldName": "time_df",
            "id": "msp_time_entry_metrics_builder-TTywd",
            "inputTypes": [
              "DataFrame"
            ],
            "type": "other"
          }
        },
        "id": "reactflow__edge-msp_time_entry_etl-hIlCX{œdataTypeœ:œmsp_time_entry_etlœ,œidœ:œmsp_time_entry_etl-hIlCXœ,œnameœ:œtime_cleanœ,œoutput_typesœ:[œDataFrameœ]}-msp_time_entry_metrics_builder-TTywd{œfieldNameœ:œtime_dfœ,œidœ:œmsp_time_entry_metrics_builder-TTywdœ,œinputTypesœ:[œDataFrameœ],œtypeœ:œotherœ}",
        "selected": false,
        "source": "msp_time_entry_etl-hIlCX",
        "sourceHandle": "{œdataTypeœ:œmsp_time_entry_etlœ,œidœ:œmsp_time_entry_etl-hIlCXœ,œnameœ:œtime_cleanœ,œoutput_typesœ:[œDataFrameœ]}",
        "target": "msp_time_entry_metrics_builder-TTywd",
        "targetHandle": "{œfieldNameœ:œtime_dfœ,œidœ:œmsp_time_entry_metrics_builder-TTywdœ,œinputTypesœ:[œDataFrameœ],œtypeœ:œotherœ}"
      },
      {
        "animated": false,
        "className": "",
        "data": {
          "sourceHandle": {
            "dataType": "msp_time_entry_metrics_builder",
            "id": "msp_time_entry_metrics_builder-Rw7a8",
            "name": "hours_by_member",
            "output_types": [
              "DataFrame"
            ]
          },
          "targetHandle": {
            "fieldName": "hours_by_member",
            "id": "msp_time_entry_llm_context_builder-Q2fyh",
            "inputTypes": [
              "DataFrame"
            ],
            "type": "other"
          }
        },
        "id": "reactflow__edge-msp_time_entry_metrics_builder-Rw7a8{œdataTypeœ:œmsp_time_entry_metrics_builderœ,œidœ:œmsp_time_entry_metrics_builder-Rw7a8œ,œnameœ:œhours_by_memberœ,œoutput_typesœ:[œDataFrameœ]}-msp_time_entry_llm_context_builder-Q2fyh{œfieldNameœ:œhours_by_memberœ,œidœ:œmsp_time_entry_llm_context_builder-Q2fyhœ,œinputTypesœ:[œDataFrameœ],œtypeœ:œotherœ}",
        "selected": false,
        "source": "msp_time_entry_metrics_builder-Rw7a8",
        "sourceHandle": "{œdataTypeœ:œmsp_time_entry_metrics_builderœ,œidœ:œmsp_time_entry_metrics_builder-Rw7a8œ,œnameœ:œhours_by_memberœ,œoutput_typesœ:[œDataFrameœ]}",
        "target": "msp_time_entry_llm_context_builder-Q2fyh",
        "targetHandle": "{œfieldNameœ:œhours_by_memberœ,œidœ:œmsp_time_entry_llm_context_builder-Q2fyhœ,œinputTypesœ:[œDataFrameœ],œtypeœ:œotherœ}"
      },
      {
        "animated": false,
        "className": "",
        "data": {
          "sourceHandle": {
            "dataType": "msp_time_entry_metrics_builder",
            "id": "msp_time_entry_metrics_builder-TTywd",
            "name": "kpi_overview",
            "output_types": [
              "DataFrame"
            ]
          },
          "targetHandle": {
            "fieldName": "kpi_overview",
            "id": "msp_time_entry_llm_context_builder-Q2fyh",
            "inputTypes": [
              "DataFrame"
            ],
            "type": "other"
          }
        },
        "id": "reactflow__edge-msp_time_entry_metrics_builder-TTywd{œdataTypeœ:œmsp_time_entry_metrics_builderœ,œidœ:œmsp_time_entry_metrics_builder-TTywdœ,œnameœ:œkpi_overviewœ,œoutput_typesœ:[œDataFrameœ]}-msp_time_entry_llm_context_builder-Q2fyh{œfieldNameœ:œkpi_overviewœ,œidœ:œmsp_time_entry_llm_context_builder-Q2fyhœ,œinputTypesœ:[œDataFrameœ],œtypeœ:œotherœ}",
        "selected": false,
        "source": "msp_time_entry_metrics_builder-TTywd",
        "sourceHandle": "{œdataTypeœ:œmsp_time_entry_metrics_builderœ,œidœ:œmsp_time_entry_metrics_builder-TTywdœ,œnameœ:œkpi_overviewœ,œoutput_typesœ:[œDataFrameœ]}",
        "target": "msp_time_entry_llm_context_builder-Q2fyh",
        "targetHandle": "{œfieldNameœ:œkpi_overviewœ,œidœ:œmsp_time_entry_llm_context_builder-Q2fyhœ,œinputTypesœ:[œDataFrameœ],œtypeœ:œotherœ}"
      },
      {
        "animated": false,
        "className": "",
        "data": {
          "sourceHandle": {
            "dataType": "msp_time_entry_metrics_builder",
            "id": "msp_time_entry_metrics_builder-7kGMn",
            "name": "hours_by_company",
            "output_types": [
              "DataFrame"
            ]
          },
          "targetHandle": {
            "fieldName": "hours_by_company",
            "id": "msp_time_entry_llm_context_builder-Q2fyh",
            "inputTypes": [
              "DataFrame"
            ],
            "type": "other"
          }
        },
        "id": "reactflow__edge-msp_time_entry_metrics_builder-7kGMn{œdataTypeœ:œmsp_time_entry_metrics_builderœ,œidœ:œmsp_time_entry_metrics_builder-7kGMnœ,œnameœ:œhours_by_companyœ,œoutput_typesœ:[œDataFrameœ]}-msp_time_entry_llm_context_builder-Q2fyh{œfieldNameœ:œhours_by_companyœ,œidœ:œmsp_time_entry_llm_context_builder-Q2fyhœ,œinputTypesœ:[œDataFrameœ],œtypeœ:œotherœ}",
        "selected": false,
        "source": "msp_time_entry_metrics_builder-7kGMn",
        "sourceHandle": "{œdataTypeœ:œmsp_time_entry_metrics_builderœ,œidœ:œmsp_time_entry_metrics_builder-7kGMnœ,œnameœ:œhours_by_companyœ,œoutput_typesœ:[œDataFrameœ]}",
        "target": "msp_time_entry_llm_context_builder-Q2fyh",
        "targetHandle": "{œfieldNameœ:œhours_by_companyœ,œidœ:œmsp_time_entry_llm_context_builder-Q2fyhœ,œinputTypesœ:[œDataFrameœ],œtypeœ:œotherœ}"
      },
      {
        "animated": false,
        "className": "",
        "data": {
          "sourceHandle": {
            "dataType": "msp_time_entry_metrics_builder",
            "id": "msp_time_entry_metrics_builder-aKsdI",
            "name": "hours_by_month",
            "output_types": [
              "DataFrame"
            ]
          },
          "targetHandle": {
            "fieldName": "hours_by_month",
            "id": "msp_time_entry_llm_context_builder-Q2fyh",
            "inputTypes": [
              "DataFrame"
            ],
            "type": "other"
          }
        },
        "id": "reactflow__edge-msp_time_entry_metrics_builder-aKsdI{œdataTypeœ:œmsp_time_entry_metrics_builderœ,œidœ:œmsp_time_entry_metrics_builder-aKsdIœ,œnameœ:œhours_by_monthœ,œoutput_typesœ:[œDataFrameœ]}-msp_time_entry_llm_context_builder-Q2fyh{œfieldNameœ:œhours_by_monthœ,œidœ:œmsp_time_entry_llm_context_builder-Q2fyhœ,œinputTypesœ:[œDataFrameœ],œtypeœ:œotherœ}",
        "selected": false,
        "source": "msp_time_entry_metrics_builder-aKsdI",
        "sourceHandle": "{œdataTypeœ:œmsp_time_entry_metrics_builderœ,œidœ:œmsp_time_entry_metrics_builder-aKsdIœ,œnameœ:œhours_by_monthœ,œoutput_typesœ:[œDataFrameœ]}",
        "target": "msp_time_entry_llm_context_builder-Q2fyh",
        "targetHandle": "{œfieldNameœ:œhours_by_monthœ,œidœ:œmsp_time_entry_llm_context_builder-Q2fyhœ,œinputTypesœ:[œDataFrameœ],œtypeœ:œotherœ}"
      },
      {
        "animated": false,
        "className": "",
        "data": {
          "sourceHandle": {
            "dataType": "msp_time_entry_etl",
            "id": "msp_time_entry_etl-hIlCX",
            "name": "time_clean",
            "output_types": [
              "DataFrame"
            ]
          },
          "targetHandle": {
            "fieldName": "time_df",
            "id": "msp_time_entry_metrics_builder-Rw7a8",
            "inputTypes": [
              "DataFrame"
            ],
            "type": "other"
          }
        },
        "id": "reactflow__edge-msp_time_entry_etl-hIlCX{œdataTypeœ:œmsp_time_entry_etlœ,œidœ:œmsp_time_entry_etl-hIlCXœ,œnameœ:œtime_cleanœ,œoutput_typesœ:[œDataFrameœ]}-msp_time_entry_metrics_builder-Rw7a8{œfieldNameœ:œtime_dfœ,œidœ:œmsp_time_entry_metrics_builder-Rw7a8œ,œinputTypesœ:[œDataFrameœ],œtypeœ:œotherœ}",
        "selected": false,
        "source": "msp_time_entry_etl-hIlCX",
        "sourceHandle": "{œdataTypeœ:œmsp_time_entry_etlœ,œidœ:œmsp_time_entry_etl-hIlCXœ,œnameœ:œtime_cleanœ,œoutput_typesœ:[œDataFrameœ]}",
        "target": "msp_time_entry_metrics_builder-Rw7a8",
        "targetHandle": "{œfieldNameœ:œtime_dfœ,œidœ:œmsp_time_entry_metrics_builder-Rw7a8œ,œinputTypesœ:[œDataFrameœ],œtypeœ:œotherœ}"
      },
      {
        "animated": false,
        "className": "",
        "data": {
          "sourceHandle": {
            "dataType": "msp_time_entry_etl",
            "id": "msp_time_entry_etl-hIlCX",
            "name": "time_clean",
            "output_types": [
              "DataFrame"
            ]
          },
          "targetHandle": {
            "fieldName": "time_df",
            "id": "msp_time_entry_metrics_builder-7kGMn",
            "inputTypes": [
              "DataFrame"
            ],
            "type": "other"
          }
        },
        "id": "reactflow__edge-msp_time_entry_etl-hIlCX{œdataTypeœ:œmsp_time_entry_etlœ,œidœ:œmsp_time_entry_etl-hIlCXœ,œnameœ:œtime_cleanœ,œoutput_typesœ:[œDataFrameœ]}-msp_time_entry_metrics_builder-7kGMn{œfieldNameœ:œtime_dfœ,œidœ:œmsp_time_entry_metrics_builder-7kGMnœ,œinputTypesœ:[œDataFrameœ],œtypeœ:œotherœ}",
        "selected": false,
        "source": "msp_time_entry_etl-hIlCX",
        "sourceHandle": "{œdataTypeœ:œmsp_time_entry_etlœ,œidœ:œmsp_time_entry_etl-hIlCXœ,œnameœ:œtime_cleanœ,œoutput_typesœ:[œDataFrameœ]}",
        "target": "msp_time_entry_metrics_builder-7kGMn",
        "targetHandle": "{œfieldNameœ:œtime_dfœ,œidœ:œmsp_time_entry_metrics_builder-7kGMnœ,œinputTypesœ:[œDataFrameœ],œtypeœ:œotherœ}"
      },
      {
        "animated": false,
        "className": "",
        "data": {
          "sourceHandle": {
            "dataType": "msp_time_entry_etl",
            "id": "msp_time_entry_etl-hIlCX",
            "name": "time_clean",
            "output_types": [
              "DataFrame"
            ]
          },
          "targetHandle": {
            "fieldName": "time_df",
            "id": "msp_time_entry_metrics_builder-aKsdI",
            "inputTypes": [
              "DataFrame"
            ],
            "type": "other"
          }
        },
        "id": "reactflow__edge-msp_time_entry_etl-hIlCX{œdataTypeœ:œmsp_time_entry_etlœ,œidœ:œmsp_time_entry_etl-hIlCXœ,œnameœ:œtime_cleanœ,œoutput_typesœ:[œDataFrameœ]}-msp_time_entry_metrics_builder-aKsdI{œfieldNameœ:œtime_dfœ,œidœ:œmsp_time_entry_metrics_builder-aKsdIœ,œinputTypesœ:[œDataFrameœ],œtypeœ:œotherœ}",
        "selected": false,
        "source": "msp_time_entry_etl-hIlCX",
        "sourceHandle": "{œdataTypeœ:œmsp_time_entry_etlœ,œidœ:œmsp_time_entry_etl-hIlCXœ,œnameœ:œtime_cleanœ,œoutput_typesœ:[œDataFrameœ]}",
        "target": "msp_time_entry_metrics_builder-aKsdI",
        "targetHandle": "{œfieldNameœ:œtime_dfœ,œidœ:œmsp_time_entry_metrics_builder-aKsdIœ,œinputTypesœ:[œDataFrameœ],œtypeœ:œotherœ}"
      },
      {
        "animated": false,
        "className": "",
        "data": {
          "sourceHandle": {
            "dataType": "msp_time_entry_etl",
            "id": "msp_time_entry_etl-hIlCX",
            "name": "time_clean",
            "output_types": [
              "DataFrame"
            ]
          },
          "targetHandle": {
            "fieldName": "time_df",
            "id": "msp_time_entry_metrics_builder-NKU5T",
            "inputTypes": [
              "DataFrame"
            ],
            "type": "other"
          }
        },
        "id": "reactflow__edge-msp_time_entry_etl-hIlCX{œdataTypeœ:œmsp_time_entry_etlœ,œidœ:œmsp_time_entry_etl-hIlCXœ,œnameœ:œtime_cleanœ,œoutput_typesœ:[œDataFrameœ]}-msp_time_entry_metrics_builder-NKU5T{œfieldNameœ:œtime_dfœ,œidœ:œmsp_time_entry_metrics_builder-NKU5Tœ,œinputTypesœ:[œDataFrameœ],œtypeœ:œotherœ}",
        "selected": false,
        "source": "msp_time_entry_etl-hIlCX",
        "sourceHandle": "{œdataTypeœ:œmsp_time_entry_etlœ,œidœ:œmsp_time_entry_etl-hIlCXœ,œnameœ:œtime_cleanœ,œoutput_typesœ:[œDataFrameœ]}",
        "target": "msp_time_entry_metrics_builder-NKU5T",
        "targetHandle": "{œfieldNameœ:œtime_dfœ,œidœ:œmsp_time_entry_metrics_builder-NKU5Tœ,œinputTypesœ:[œDataFrameœ],œtypeœ:œotherœ}"
      },
      {
        "animated": false,
        "className": "",
        "data": {
          "sourceHandle": {
            "dataType": "msp_time_entry_etl",
            "id": "msp_time_entry_etl-hIlCX",
            "name": "time_clean",
            "output_types": [
              "DataFrame"
            ]
          },
          "targetHandle": {
            "fieldName": "time_df",
            "id": "msp_time_entry_metrics_builder-1NFh1",
            "inputTypes": [
              "DataFrame"
            ],
            "type": "other"
          }
        },
        "id": "reactflow__edge-msp_time_entry_etl-hIlCX{œdataTypeœ:œmsp_time_entry_etlœ,œidœ:œmsp_time_entry_etl-hIlCXœ,œnameœ:œtime_cleanœ,œoutput_typesœ:[œDataFrameœ]}-msp_time_entry_metrics_builder-1NFh1{œfieldNameœ:œtime_dfœ,œidœ:œmsp_time_entry_metrics_builder-1NFh1œ,œinputTypesœ:[œDataFrameœ],œtypeœ:œotherœ}",
        "selected": false,
        "source": "msp_time_entry_etl-hIlCX",
        "sourceHandle": "{œdataTypeœ:œmsp_time_entry_etlœ,œidœ:œmsp_time_entry_etl-hIlCXœ,œnameœ:œtime_cleanœ,œoutput_typesœ:[œDataFrameœ]}",
        "target": "msp_time_entry_metrics_builder-1NFh1",
        "targetHandle": "{œfieldNameœ:œtime_dfœ,œidœ:œmsp_time_entry_metrics_builder-1NFh1œ,œinputTypesœ:[œDataFrameœ],œtypeœ:œotherœ}"
      },
      {
        "animated": false,
        "className": "",
        "data": {
          "sourceHandle": {
            "dataType": "msp_time_entry_metrics_builder",
            "id": "msp_time_entry_metrics_builder-NKU5T",
            "name": "internal_breakdown",
            "output_types": [
              "DataFrame"
            ]
          },
          "targetHandle": {
            "fieldName": "internal_breakdown",
            "id": "msp_time_entry_llm_context_builder-Q2fyh",
            "inputTypes": [
              "DataFrame"
            ],
            "type": "other"
          }
        },
        "id": "reactflow__edge-msp_time_entry_metrics_builder-NKU5T{œdataTypeœ:œmsp_time_entry_metrics_builderœ,œidœ:œmsp_time_entry_metrics_builder-NKU5Tœ,œnameœ:œinternal_breakdownœ,œoutput_typesœ:[œDataFrameœ]}-msp_time_entry_llm_context_builder-Q2fyh{œfieldNameœ:œinternal_breakdownœ,œidœ:œmsp_time_entry_llm_context_builder-Q2fyhœ,œinputTypesœ:[œDataFrameœ],œtypeœ:œotherœ}",
        "selected": false,
        "source": "msp_time_entry_metrics_builder-NKU5T",
        "sourceHandle": "{œdataTypeœ:œmsp_time_entry_metrics_builderœ,œidœ:œmsp_time_entry_metrics_builder-NKU5Tœ,œnameœ:œinternal_breakdownœ,œoutput_typesœ:[œDataFrameœ]}",
        "target": "msp_time_entry_llm_context_builder-Q2fyh",
        "targetHandle": "{œfieldNameœ:œinternal_breakdownœ,œidœ:œmsp_time_entry_llm_context_builder-Q2fyhœ,œinputTypesœ:[œDataFrameœ],œtypeœ:œotherœ}"
      },
      {
        "animated": false,
        "className": "",
        "data": {
          "sourceHandle": {
            "dataType": "msp_time_entry_metrics_builder",
            "id": "msp_time_entry_metrics_builder-1NFh1",
            "name": "weekend_vs_weekday",
            "output_types": [
              "DataFrame"
            ]
          },
          "targetHandle": {
            "fieldName": "weekend_vs_weekday",
            "id": "msp_time_entry_llm_context_builder-Q2fyh",
            "inputTypes": [
              "DataFrame"
            ],
            "type": "other"
          }
        },
        "id": "reactflow__edge-msp_time_entry_metrics_builder-1NFh1{œdataTypeœ:œmsp_time_entry_metrics_builderœ,œidœ:œmsp_time_entry_metrics_builder-1NFh1œ,œnameœ:œweekend_vs_weekdayœ,œoutput_typesœ:[œDataFrameœ]}-msp_time_entry_llm_context_builder-Q2fyh{œfieldNameœ:œweekend_vs_weekdayœ,œidœ:œmsp_time_entry_llm_context_builder-Q2fyhœ,œinputTypesœ:[œDataFrameœ],œtypeœ:œotherœ}",
        "selected": false,
        "source": "msp_time_entry_metrics_builder-1NFh1",
        "sourceHandle": "{œdataTypeœ:œmsp_time_entry_metrics_builderœ,œidœ:œmsp_time_entry_metrics_builder-1NFh1œ,œnameœ:œweekend_vs_weekdayœ,œoutput_typesœ:[œDataFrameœ]}",
        "target": "msp_time_entry_llm_context_builder-Q2fyh",
        "targetHandle": "{œfieldNameœ:œweekend_vs_weekdayœ,œidœ:œmsp_time_entry_llm_context_builder-Q2fyhœ,œinputTypesœ:[œDataFrameœ],œtypeœ:œotherœ}"
      },
      {
        "animated": false,
        "className": "",
        "data": {
          "sourceHandle": {
            "dataType": "msp_time_entry_llm_context_builder",
            "id": "msp_time_entry_llm_context_builder-Q2fyh",
            "name": "llm_context",
            "output_types": [
              "Message"
            ]
          },
          "targetHandle": {
            "fieldName": "input_value",
            "id": "Agent-6DFFZ",
            "inputTypes": [
              "Message"
            ],
            "type": "str"
          }
        },
        "id": "reactflow__edge-msp_time_entry_llm_context_builder-Q2fyh{œdataTypeœ:œmsp_time_entry_llm_context_builderœ,œidœ:œmsp_time_entry_llm_context_builder-Q2fyhœ,œnameœ:œllm_contextœ,œoutput_typesœ:[œMessageœ]}-Agent-6DFFZ{œfieldNameœ:œinput_valueœ,œidœ:œAgent-6DFFZœ,œinputTypesœ:[œMessageœ],œtypeœ:œstrœ}",
        "selected": false,
        "source": "msp_time_entry_llm_context_builder-Q2fyh",
        "sourceHandle": "{œdataTypeœ:œmsp_time_entry_llm_context_builderœ,œidœ:œmsp_time_entry_llm_context_builder-Q2fyhœ,œnameœ:œllm_contextœ,œoutput_typesœ:[œMessageœ]}",
        "target": "Agent-6DFFZ",
        "targetHandle": "{œfieldNameœ:œinput_valueœ,œidœ:œAgent-6DFFZœ,œinputTypesœ:[œMessageœ],œtypeœ:œstrœ}"
      },
      {
        "animated": false,
        "className": "",
        "data": {
          "sourceHandle": {
            "dataType": "Agent",
            "id": "Agent-CdUIZ",
            "name": "response",
            "output_types": [
              "Message"
            ]
          },
          "targetHandle": {
            "fieldName": "msg_a",
            "id": "combine_three_llm_outputs-78VjU",
            "inputTypes": [
              "Message"
            ],
            "type": "str"
          }
        },
        "id": "reactflow__edge-Agent-CdUIZ{œdataTypeœ:œAgentœ,œidœ:œAgent-CdUIZœ,œnameœ:œresponseœ,œoutput_typesœ:[œMessageœ]}-combine_three_llm_outputs-78VjU{œfieldNameœ:œmsg_aœ,œidœ:œcombine_three_llm_outputs-78VjUœ,œinputTypesœ:[œMessageœ],œtypeœ:œstrœ}",
        "selected": false,
        "source": "Agent-CdUIZ",
        "sourceHandle": "{œdataTypeœ:œAgentœ,œidœ:œAgent-CdUIZœ,œnameœ:œresponseœ,œoutput_typesœ:[œMessageœ]}",
        "target": "combine_three_llm_outputs-78VjU",
        "targetHandle": "{œfieldNameœ:œmsg_aœ,œidœ:œcombine_three_llm_outputs-78VjUœ,œinputTypesœ:[œMessageœ],œtypeœ:œstrœ}"
      },
      {
        "animated": false,
        "className": "",
        "data": {
          "sourceHandle": {
            "dataType": "Agent",
            "id": "Agent-0r7mv",
            "name": "response",
            "output_types": [
              "Message"
            ]
          },
          "targetHandle": {
            "fieldName": "msg_b",
            "id": "combine_three_llm_outputs-78VjU",
            "inputTypes": [
              "Message"
            ],
            "type": "str"
          }
        },
        "id": "reactflow__edge-Agent-0r7mv{œdataTypeœ:œAgentœ,œidœ:œAgent-0r7mvœ,œnameœ:œresponseœ,œoutput_typesœ:[œMessageœ]}-combine_three_llm_outputs-78VjU{œfieldNameœ:œmsg_bœ,œidœ:œcombine_three_llm_outputs-78VjUœ,œinputTypesœ:[œMessageœ],œtypeœ:œstrœ}",
        "selected": false,
        "source": "Agent-0r7mv",
        "sourceHandle": "{œdataTypeœ:œAgentœ,œidœ:œAgent-0r7mvœ,œnameœ:œresponseœ,œoutput_typesœ:[œMessageœ]}",
        "target": "combine_three_llm_outputs-78VjU",
        "targetHandle": "{œfieldNameœ:œmsg_bœ,œidœ:œcombine_three_llm_outputs-78VjUœ,œinputTypesœ:[œMessageœ],œtypeœ:œstrœ}"
      },
      {
        "animated": false,
        "className": "",
        "data": {
          "sourceHandle": {
            "dataType": "Agent",
            "id": "Agent-6DFFZ",
            "name": "response",
            "output_types": [
              "Message"
            ]
          },
          "targetHandle": {
            "fieldName": "msg_c",
            "id": "combine_three_llm_outputs-78VjU",
            "inputTypes": [
              "Message"
            ],
            "type": "str"
          }
        },
        "id": "reactflow__edge-Agent-6DFFZ{œdataTypeœ:œAgentœ,œidœ:œAgent-6DFFZœ,œnameœ:œresponseœ,œoutput_typesœ:[œMessageœ]}-combine_three_llm_outputs-78VjU{œfieldNameœ:œmsg_cœ,œidœ:œcombine_three_llm_outputs-78VjUœ,œinputTypesœ:[œMessageœ],œtypeœ:œstrœ}",
        "selected": false,
        "source": "Agent-6DFFZ",
        "sourceHandle": "{œdataTypeœ:œAgentœ,œidœ:œAgent-6DFFZœ,œnameœ:œresponseœ,œoutput_typesœ:[œMessageœ]}",
        "target": "combine_three_llm_outputs-78VjU",
        "targetHandle": "{œfieldNameœ:œmsg_cœ,œidœ:œcombine_three_llm_outputs-78VjUœ,œinputTypesœ:[œMessageœ],œtypeœ:œstrœ}"
      },
      {
        "animated": false,
        "className": "",
        "data": {
          "sourceHandle": {
            "dataType": "combine_three_llm_outputs",
            "id": "combine_three_llm_outputs-78VjU",
            "name": "combined",
            "output_types": [
              "Message"
            ]
          },
          "targetHandle": {
            "fieldName": "input_value",
            "id": "TextOutput-0km4F",
            "inputTypes": [
              "Message"
            ],
            "type": "str"
          }
        },
        "id": "reactflow__edge-combine_three_llm_outputs-78VjU{œdataTypeœ:œcombine_three_llm_outputsœ,œidœ:œcombine_three_llm_outputs-78VjUœ,œnameœ:œcombinedœ,œoutput_typesœ:[œMessageœ]}-TextOutput-0km4F{œfieldNameœ:œinput_valueœ,œidœ:œTextOutput-0km4Fœ,œinputTypesœ:[œMessageœ],œtypeœ:œstrœ}",
        "selected": false,
        "source": "combine_three_llm_outputs-78VjU",
        "sourceHandle": "{œdataTypeœ:œcombine_three_llm_outputsœ,œidœ:œcombine_three_llm_outputs-78VjUœ,œnameœ:œcombinedœ,œoutput_typesœ:[œMessageœ]}",
        "target": "TextOutput-0km4F",
        "targetHandle": "{œfieldNameœ:œinput_valueœ,œidœ:œTextOutput-0km4Fœ,œinputTypesœ:[œMessageœ],œtypeœ:œstrœ}"
      },
      {
        "animated": false,
        "className": "",
        "data": {
          "sourceHandle": {
            "dataType": "TextOutput",
            "id": "TextOutput-0km4F",
            "name": "text",
            "output_types": [
              "Message"
            ]
          },
          "targetHandle": {
            "fieldName": "input_value",
            "id": "ChatOutput-nhTcC",
            "inputTypes": [
              "Data",
              "DataFrame",
              "Message"
            ],
            "type": "other"
          }
        },
        "id": "reactflow__edge-TextOutput-0km4F{œdataTypeœ:œTextOutputœ,œidœ:œTextOutput-0km4Fœ,œnameœ:œtextœ,œoutput_typesœ:[œMessageœ]}-ChatOutput-nhTcC{œfieldNameœ:œinput_valueœ,œidœ:œChatOutput-nhTcCœ,œinputTypesœ:[œDataœ,œDataFrameœ,œMessageœ],œtypeœ:œotherœ}",
        "selected": false,
        "source": "TextOutput-0km4F",
        "sourceHandle": "{œdataTypeœ:œTextOutputœ,œidœ:œTextOutput-0km4Fœ,œnameœ:œtextœ,œoutput_typesœ:[œMessageœ]}",
        "target": "ChatOutput-nhTcC",
        "targetHandle": "{œfieldNameœ:œinput_valueœ,œidœ:œChatOutput-nhTcCœ,œinputTypesœ:[œDataœ,œDataFrameœ,œMessageœ],œtypeœ:œotherœ}"
      }
    ],
    "nodes": [
      {
        "data": {
          "id": "File-QOVjK",
          "node": {
            "base_classes": [
              "Message"
            ],
            "beta": false,
            "conditional_paths": [],
            "custom_fields": {},
            "description": "Loads content from one or more files.",
            "display_name": "File",
            "documentation": "https://docs.langflow.org/components-data#file",
            "edited": false,
            "field_order": [
              "path",
              "file_path",
              "separator",
              "silent_errors",
              "delete_server_file_after_processing",
              "ignore_unsupported_extensions",
              "ignore_unspecified_files",
              "advanced_mode",
              "pipeline",
              "ocr_engine",
              "md_image_placeholder",
              "md_page_break_placeholder",
              "doc_key",
              "use_multithreading",
              "concurrency_multithreading",
              "markdown"
            ],
            "frozen": false,
            "icon": "file-text",
            "last_updated": "2025-12-23T01:31:50.173Z",
            "legacy": false,
            "lf_version": "1.6.0",
            "metadata": {},
            "minimized": false,
            "output_types": [],
            "outputs": [
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Raw Content",
                "group_outputs": false,
                "method": "load_files_message",
                "name": "message",
                "options": null,
                "required_inputs": null,
                "selected": "Message",
                "tool_mode": true,
                "types": [
                  "Message"
                ],
                "value": "__UNDEFINED__"
              }
            ],
            "pinned": false,
            "template": {
              "_type": "Component",
              "advanced_mode": {
                "_input_type": "BoolInput",
                "advanced": false,
                "display_name": "Advanced Parser",
                "dynamic": false,
                "info": "Enable advanced document processing and export with Docling for PDFs, images, and office documents. Available only for single file processing.Note that advanced document processing can consume significant resources.",
                "list": false,
                "list_add_label": "Add More",
                "name": "advanced_mode",
                "placeholder": "",
                "real_time_refresh": true,
                "required": false,
                "show": false,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "bool",
                "value": false
              },
              "code": {
                "advanced": true,
                "dynamic": true,
                "fileTypes": [],
                "file_path": "",
                "info": "",
                "list": false,
                "load_from_db": false,
                "multiline": true,
                "name": "code",
                "password": false,
                "placeholder": "",
                "required": true,
                "show": true,
                "title_case": false,
                "type": "code",
                "value": "\"\"\"Enhanced file component with Docling support and process isolation.\n\nNotes:\n-----\n- ALL Docling parsing/export runs in a separate OS process to prevent memory\n  growth and native library state from impacting the main Langflow process.\n- Standard text/structured parsing continues to use existing BaseFileComponent\n  utilities (and optional threading via `parallel_load_data`).\n\"\"\"\n\nfrom __future__ import annotations\n\nimport json\nimport subprocess\nimport sys\nimport textwrap\nfrom copy import deepcopy\nfrom typing import TYPE_CHECKING, Any\n\nfrom langflow.base.data.base_file import BaseFileComponent\nfrom langflow.base.data.utils import TEXT_FILE_TYPES, parallel_load_data, parse_text_file_to_data\nfrom langflow.io import (\n    BoolInput,\n    DropdownInput,\n    FileInput,\n    IntInput,\n    MessageTextInput,\n    Output,\n    StrInput,\n)\nfrom langflow.schema.data import Data\nfrom langflow.schema.message import Message\n\nif TYPE_CHECKING:\n    from langflow.schema import DataFrame\n\n\nclass FileComponent(BaseFileComponent):\n    \"\"\"File component with optional Docling processing (isolated in a subprocess).\"\"\"\n\n    display_name = \"File\"\n    description = \"Loads content from one or more files.\"\n    documentation: str = \"https://docs.langflow.org/components-data#file\"\n    icon = \"file-text\"\n    name = \"File\"\n\n    # Docling-supported/compatible extensions; TEXT_FILE_TYPES are supported by the base loader.\n    VALID_EXTENSIONS = [\n        *TEXT_FILE_TYPES,\n        \"adoc\",\n        \"asciidoc\",\n        \"asc\",\n        \"bmp\",\n        \"dotx\",\n        \"dotm\",\n        \"docm\",\n        \"jpeg\",\n        \"png\",\n        \"potx\",\n        \"ppsx\",\n        \"pptm\",\n        \"potm\",\n        \"ppsm\",\n        \"pptx\",\n        \"tiff\",\n        \"xls\",\n        \"xlsx\",\n        \"xhtml\",\n        \"webp\",\n    ]\n\n    # Fixed export settings used when markdown export is requested.\n    EXPORT_FORMAT = \"Markdown\"\n    IMAGE_MODE = \"placeholder\"\n\n    # ---- Inputs / Outputs (kept as close to original as possible) -------------------\n    _base_inputs = deepcopy(BaseFileComponent._base_inputs)\n    for input_item in _base_inputs:\n        if isinstance(input_item, FileInput) and input_item.name == \"path\":\n            input_item.real_time_refresh = True\n            break\n\n    inputs = [\n        *_base_inputs,\n        BoolInput(\n            name=\"advanced_mode\",\n            display_name=\"Advanced Parser\",\n            value=False,\n            real_time_refresh=True,\n            info=(\n                \"Enable advanced document processing and export with Docling for PDFs, images, and office documents. \"\n                \"Available only for single file processing.\"\n                \"Note that advanced document processing can consume significant resources.\"\n            ),\n            show=False,\n        ),\n        DropdownInput(\n            name=\"pipeline\",\n            display_name=\"Pipeline\",\n            info=\"Docling pipeline to use\",\n            options=[\"standard\", \"vlm\"],\n            value=\"standard\",\n            advanced=True,\n            real_time_refresh=True,\n        ),\n        DropdownInput(\n            name=\"ocr_engine\",\n            display_name=\"OCR Engine\",\n            info=\"OCR engine to use. Only available when pipeline is set to 'standard'.\",\n            options=[\"None\", \"easyocr\"],\n            value=\"easyocr\",\n            show=False,\n            advanced=True,\n        ),\n        StrInput(\n            name=\"md_image_placeholder\",\n            display_name=\"Image placeholder\",\n            info=\"Specify the image placeholder for markdown exports.\",\n            value=\"<!-- image -->\",\n            advanced=True,\n            show=False,\n        ),\n        StrInput(\n            name=\"md_page_break_placeholder\",\n            display_name=\"Page break placeholder\",\n            info=\"Add this placeholder between pages in the markdown output.\",\n            value=\"\",\n            advanced=True,\n            show=False,\n        ),\n        MessageTextInput(\n            name=\"doc_key\",\n            display_name=\"Doc Key\",\n            info=\"The key to use for the DoclingDocument column.\",\n            value=\"doc\",\n            advanced=True,\n            show=False,\n        ),\n        # Deprecated input retained for backward-compatibility.\n        BoolInput(\n            name=\"use_multithreading\",\n            display_name=\"[Deprecated] Use Multithreading\",\n            advanced=True,\n            value=True,\n            info=\"Set 'Processing Concurrency' greater than 1 to enable multithreading.\",\n        ),\n        IntInput(\n            name=\"concurrency_multithreading\",\n            display_name=\"Processing Concurrency\",\n            advanced=True,\n            info=\"When multiple files are being processed, the number of files to process concurrently.\",\n            value=1,\n        ),\n        BoolInput(\n            name=\"markdown\",\n            display_name=\"Markdown Export\",\n            info=\"Export processed documents to Markdown format. Only available when advanced mode is enabled.\",\n            value=False,\n            show=False,\n        ),\n    ]\n\n    outputs = [\n        Output(display_name=\"Raw Content\", name=\"message\", method=\"load_files_message\"),\n    ]\n\n    # ------------------------------ UI helpers --------------------------------------\n\n    def _path_value(self, template: dict) -> list[str]:\n        \"\"\"Return the list of currently selected file paths from the template.\"\"\"\n        return template.get(\"path\", {}).get(\"file_path\", [])\n\n    def update_build_config(\n        self,\n        build_config: dict[str, Any],\n        field_value: Any,\n        field_name: str | None = None,\n    ) -> dict[str, Any]:\n        \"\"\"Show/hide Advanced Parser and related fields based on selection context.\"\"\"\n        if field_name == \"path\":\n            paths = self._path_value(build_config)\n            file_path = paths[0] if paths else \"\"\n            file_count = len(field_value) if field_value else 0\n\n            # Advanced mode only for single (non-tabular) file\n            allow_advanced = file_count == 1 and not file_path.endswith((\".csv\", \".xlsx\", \".parquet\"))\n            build_config[\"advanced_mode\"][\"show\"] = allow_advanced\n            if not allow_advanced:\n                build_config[\"advanced_mode\"][\"value\"] = False\n                for f in (\"pipeline\", \"ocr_engine\", \"doc_key\", \"md_image_placeholder\", \"md_page_break_placeholder\"):\n                    if f in build_config:\n                        build_config[f][\"show\"] = False\n\n        # Docling Processing\n        elif field_name == \"advanced_mode\":\n            for f in (\"pipeline\", \"ocr_engine\", \"doc_key\", \"md_image_placeholder\", \"md_page_break_placeholder\"):\n                if f in build_config:\n                    build_config[f][\"show\"] = bool(field_value)\n\n        elif field_name == \"pipeline\":\n            if field_value == \"standard\":\n                build_config[\"ocr_engine\"][\"show\"] = True\n                build_config[\"ocr_engine\"][\"value\"] = \"easyocr\"\n            else:\n                build_config[\"ocr_engine\"][\"show\"] = False\n                build_config[\"ocr_engine\"][\"value\"] = \"None\"\n\n        return build_config\n\n    def update_outputs(self, frontend_node: dict[str, Any], field_name: str, field_value: Any) -> dict[str, Any]:  # noqa: ARG002\n        \"\"\"Dynamically show outputs based on file count/type and advanced mode.\"\"\"\n        if field_name not in [\"path\", \"advanced_mode\", \"pipeline\"]:\n            return frontend_node\n\n        template = frontend_node.get(\"template\", {})\n        paths = self._path_value(template)\n        if not paths:\n            return frontend_node\n\n        frontend_node[\"outputs\"] = []\n        if len(paths) == 1:\n            file_path = paths[0] if field_name == \"path\" else frontend_node[\"template\"][\"path\"][\"file_path\"][0]\n            if file_path.endswith((\".csv\", \".xlsx\", \".parquet\")):\n                frontend_node[\"outputs\"].append(\n                    Output(display_name=\"Structured Content\", name=\"dataframe\", method=\"load_files_structured\"),\n                )\n            elif file_path.endswith(\".json\"):\n                frontend_node[\"outputs\"].append(\n                    Output(display_name=\"Structured Content\", name=\"json\", method=\"load_files_json\"),\n                )\n\n            advanced_mode = frontend_node.get(\"template\", {}).get(\"advanced_mode\", {}).get(\"value\", False)\n            if advanced_mode:\n                frontend_node[\"outputs\"].append(\n                    Output(display_name=\"Structured Output\", name=\"advanced_dataframe\", method=\"load_files_dataframe\"),\n                )\n                frontend_node[\"outputs\"].append(\n                    Output(display_name=\"Markdown\", name=\"advanced_markdown\", method=\"load_files_markdown\"),\n                )\n                frontend_node[\"outputs\"].append(\n                    Output(display_name=\"File Path\", name=\"path\", method=\"load_files_path\"),\n                )\n            else:\n                frontend_node[\"outputs\"].append(\n                    Output(display_name=\"Raw Content\", name=\"message\", method=\"load_files_message\"),\n                )\n                frontend_node[\"outputs\"].append(\n                    Output(display_name=\"File Path\", name=\"path\", method=\"load_files_path\"),\n                )\n        else:\n            # Multiple files => DataFrame output; advanced parser disabled\n            frontend_node[\"outputs\"].append(Output(display_name=\"Files\", name=\"dataframe\", method=\"load_files\"))\n\n        return frontend_node\n\n    # ------------------------------ Core processing ----------------------------------\n\n    def _is_docling_compatible(self, file_path: str) -> bool:\n        \"\"\"Lightweight extension gate for Docling-compatible types.\"\"\"\n        docling_exts = (\n            \".adoc\",\n            \".asciidoc\",\n            \".asc\",\n            \".bmp\",\n            \".csv\",\n            \".dotx\",\n            \".dotm\",\n            \".docm\",\n            \".docx\",\n            \".htm\",\n            \".html\",\n            \".jpeg\",\n            \".json\",\n            \".md\",\n            \".pdf\",\n            \".png\",\n            \".potx\",\n            \".ppsx\",\n            \".pptm\",\n            \".potm\",\n            \".ppsm\",\n            \".pptx\",\n            \".tiff\",\n            \".txt\",\n            \".xls\",\n            \".xlsx\",\n            \".xhtml\",\n            \".xml\",\n            \".webp\",\n        )\n        return file_path.lower().endswith(docling_exts)\n\n    def _process_docling_in_subprocess(self, file_path: str) -> Data | None:\n        \"\"\"Run Docling in a separate OS process and map the result to a Data object.\n\n        We avoid multiprocessing pickling by launching `python -c \"<script>\"` and\n        passing JSON config via stdin. The child prints a JSON result to stdout.\n        \"\"\"\n        if not file_path:\n            return None\n\n        args: dict[str, Any] = {\n            \"file_path\": file_path,\n            \"markdown\": bool(self.markdown),\n            \"image_mode\": str(self.IMAGE_MODE),\n            \"md_image_placeholder\": str(self.md_image_placeholder),\n            \"md_page_break_placeholder\": str(self.md_page_break_placeholder),\n            \"pipeline\": str(self.pipeline),\n            \"ocr_engine\": (\n                self.ocr_engine if self.ocr_engine and self.ocr_engine != \"None\" and self.pipeline != \"vlm\" else None\n            ),\n        }\n\n        self.log(f\"Starting Docling subprocess for file: {file_path}\")\n        self.log(args)\n\n        # Child script for isolating the docling processing\n        child_script = textwrap.dedent(\n            r\"\"\"\n            import json, sys\n\n            def try_imports():\n                # Strategy 1: latest layout\n                try:\n                    from docling.datamodel.base_models import ConversionStatus, InputFormat  # type: ignore\n                    from docling.document_converter import DocumentConverter  # type: ignore\n                    from docling_core.types.doc import ImageRefMode  # type: ignore\n                    return ConversionStatus, InputFormat, DocumentConverter, ImageRefMode, \"latest\"\n                except Exception:\n                    pass\n                # Strategy 2: alternative layout\n                try:\n                    from docling.document_converter import DocumentConverter  # type: ignore\n                    try:\n                        from docling_core.types import ConversionStatus, InputFormat  # type: ignore\n                    except Exception:\n                        try:\n                            from docling.datamodel import ConversionStatus, InputFormat  # type: ignore\n                        except Exception:\n                            class ConversionStatus: SUCCESS = \"success\"\n                            class InputFormat:\n                                PDF=\"pdf\"; IMAGE=\"image\"\n                    try:\n                        from docling_core.types.doc import ImageRefMode  # type: ignore\n                    except Exception:\n                        class ImageRefMode:\n                            PLACEHOLDER=\"placeholder\"; EMBEDDED=\"embedded\"\n                    return ConversionStatus, InputFormat, DocumentConverter, ImageRefMode, \"alternative\"\n                except Exception:\n                    pass\n                # Strategy 3: basic converter only\n                try:\n                    from docling.document_converter import DocumentConverter  # type: ignore\n                    class ConversionStatus: SUCCESS = \"success\"\n                    class InputFormat:\n                        PDF=\"pdf\"; IMAGE=\"image\"\n                    class ImageRefMode:\n                        PLACEHOLDER=\"placeholder\"; EMBEDDED=\"embedded\"\n                    return ConversionStatus, InputFormat, DocumentConverter, ImageRefMode, \"basic\"\n                except Exception as e:\n                    raise ImportError(f\"Docling imports failed: {e}\") from e\n\n            def create_converter(strategy, input_format, DocumentConverter, pipeline, ocr_engine):\n                # --- Standard PDF/IMAGE pipeline (your existing behavior), with optional OCR ---\n                if pipeline == \"standard\":\n                    try:\n                        from docling.datamodel.pipeline_options import PdfPipelineOptions  # type: ignore\n                        from docling.document_converter import PdfFormatOption  # type: ignore\n\n                        pipe = PdfPipelineOptions()\n                        pipe.do_ocr = False\n\n                        if ocr_engine:\n                            try:\n                                from docling.models.factories import get_ocr_factory  # type: ignore\n                                pipe.do_ocr = True\n                                fac = get_ocr_factory(allow_external_plugins=False)\n                                pipe.ocr_options = fac.create_options(kind=ocr_engine)\n                            except Exception:\n                                # If OCR setup fails, disable it\n                                pipe.do_ocr = False\n\n                        fmt = {}\n                        if hasattr(input_format, \"PDF\"):\n                            fmt[getattr(input_format, \"PDF\")] = PdfFormatOption(pipeline_options=pipe)\n                        if hasattr(input_format, \"IMAGE\"):\n                            fmt[getattr(input_format, \"IMAGE\")] = PdfFormatOption(pipeline_options=pipe)\n\n                        return DocumentConverter(format_options=fmt)\n                    except Exception:\n                        return DocumentConverter()\n\n                # --- Vision-Language Model (VLM) pipeline ---\n                if pipeline == \"vlm\":\n                    try:\n                        from docling.pipeline.vlm_pipeline import VlmPipeline\n                        from docling.document_converter import PdfFormatOption  # type: ignore\n\n                        vl_pipe = VlmPipelineOptions()\n\n                        # VLM paths generally don't need OCR; keep OCR off by default here.\n                        fmt = {}\n                        if hasattr(input_format, \"PDF\"):\n                            fmt[getattr(input_format, \"PDF\")] = PdfFormatOption(pipeline_cls=VlmPipeline)\n                        if hasattr(input_format, \"IMAGE\"):\n                            fmt[getattr(input_format, \"IMAGE\")] = PdfFormatOption(pipeline_cls=VlmPipeline)\n\n                        return DocumentConverter(format_options=fmt)\n                    except Exception:\n                        return DocumentConverter()\n\n                # --- Fallback: default converter with no special options ---\n                return DocumentConverter()\n\n            def export_markdown(document, ImageRefMode, image_mode, img_ph, pg_ph):\n                try:\n                    mode = getattr(ImageRefMode, image_mode.upper(), image_mode)\n                    return document.export_to_markdown(\n                        image_mode=mode,\n                        image_placeholder=img_ph,\n                        page_break_placeholder=pg_ph,\n                    )\n                except Exception:\n                    try:\n                        return document.export_to_text()\n                    except Exception:\n                        return str(document)\n\n            def to_rows(doc_dict):\n                rows = []\n                for t in doc_dict.get(\"texts\", []):\n                    prov = t.get(\"prov\") or []\n                    page_no = None\n                    if prov and isinstance(prov, list) and isinstance(prov[0], dict):\n                        page_no = prov[0].get(\"page_no\")\n                    rows.append({\n                        \"page_no\": page_no,\n                        \"label\": t.get(\"label\"),\n                        \"text\": t.get(\"text\"),\n                        \"level\": t.get(\"level\"),\n                    })\n                return rows\n\n            def main():\n                cfg = json.loads(sys.stdin.read())\n                file_path = cfg[\"file_path\"]\n                markdown = cfg[\"markdown\"]\n                image_mode = cfg[\"image_mode\"]\n                img_ph = cfg[\"md_image_placeholder\"]\n                pg_ph = cfg[\"md_page_break_placeholder\"]\n                pipeline = cfg[\"pipeline\"]\n                ocr_engine = cfg.get(\"ocr_engine\")\n                meta = {\"file_path\": file_path}\n\n                try:\n                    ConversionStatus, InputFormat, DocumentConverter, ImageRefMode, strategy = try_imports()\n                    converter = create_converter(strategy, InputFormat, DocumentConverter, pipeline, ocr_engine)\n                    try:\n                        res = converter.convert(file_path)\n                    except Exception as e:\n                        print(json.dumps({\"ok\": False, \"error\": f\"Docling conversion error: {e}\", \"meta\": meta}))\n                        return\n\n                    ok = False\n                    if hasattr(res, \"status\"):\n                        try:\n                            ok = (res.status == ConversionStatus.SUCCESS) or (str(res.status).lower() == \"success\")\n                        except Exception:\n                            ok = (str(res.status).lower() == \"success\")\n                    if not ok and hasattr(res, \"document\"):\n                        ok = getattr(res, \"document\", None) is not None\n                    if not ok:\n                        print(json.dumps({\"ok\": False, \"error\": \"Docling conversion failed\", \"meta\": meta}))\n                        return\n\n                    doc = getattr(res, \"document\", None)\n                    if doc is None:\n                        print(json.dumps({\"ok\": False, \"error\": \"Docling produced no document\", \"meta\": meta}))\n                        return\n\n                    if markdown:\n                        text = export_markdown(doc, ImageRefMode, image_mode, img_ph, pg_ph)\n                        print(json.dumps({\"ok\": True, \"mode\": \"markdown\", \"text\": text, \"meta\": meta}))\n                        return\n\n                    # structured\n                    try:\n                        doc_dict = doc.export_to_dict()\n                    except Exception as e:\n                        print(json.dumps({\"ok\": False, \"error\": f\"Docling export_to_dict failed: {e}\", \"meta\": meta}))\n                        return\n\n                    rows = to_rows(doc_dict)\n                    print(json.dumps({\"ok\": True, \"mode\": \"structured\", \"doc\": rows, \"meta\": meta}))\n                except Exception as e:\n                    print(\n                        json.dumps({\n                            \"ok\": False,\n                            \"error\": f\"Docling processing error: {e}\",\n                            \"meta\": {\"file_path\": file_path},\n                        })\n                    )\n\n            if __name__ == \"__main__\":\n                main()\n            \"\"\"\n        )\n\n        # Validate file_path to avoid command injection or unsafe input\n        if not isinstance(args[\"file_path\"], str) or any(c in args[\"file_path\"] for c in [\";\", \"|\", \"&\", \"$\", \"`\"]):\n            return Data(data={\"error\": \"Unsafe file path detected.\", \"file_path\": args[\"file_path\"]})\n\n        proc = subprocess.run(  # noqa: S603\n            [sys.executable, \"-u\", \"-c\", child_script],\n            input=json.dumps(args).encode(\"utf-8\"),\n            capture_output=True,\n            check=False,\n        )\n\n        if not proc.stdout:\n            err_msg = proc.stderr.decode(\"utf-8\", errors=\"replace\") or \"no output from child process\"\n            return Data(data={\"error\": f\"Docling subprocess error: {err_msg}\", \"file_path\": file_path})\n\n        try:\n            result = json.loads(proc.stdout.decode(\"utf-8\"))\n        except Exception as e:  # noqa: BLE001\n            err_msg = proc.stderr.decode(\"utf-8\", errors=\"replace\")\n            return Data(\n                data={\"error\": f\"Invalid JSON from Docling subprocess: {e}. stderr={err_msg}\", \"file_path\": file_path},\n            )\n\n        if not result.get(\"ok\"):\n            return Data(data={\"error\": result.get(\"error\", \"Unknown Docling error\"), **result.get(\"meta\", {})})\n\n        meta = result.get(\"meta\", {})\n        if result.get(\"mode\") == \"markdown\":\n            exported_content = str(result.get(\"text\", \"\"))\n            return Data(\n                text=exported_content,\n                data={\"exported_content\": exported_content, \"export_format\": self.EXPORT_FORMAT, **meta},\n            )\n\n        rows = list(result.get(\"doc\", []))\n        return Data(data={\"doc\": rows, \"export_format\": self.EXPORT_FORMAT, **meta})\n\n    def process_files(\n        self,\n        file_list: list[BaseFileComponent.BaseFile],\n    ) -> list[BaseFileComponent.BaseFile]:\n        \"\"\"Process input files.\n\n        - Single file + advanced_mode => Docling in a separate process.\n        - Otherwise => standard parsing in current process (optionally threaded).\n        \"\"\"\n        if not file_list:\n            msg = \"No files to process.\"\n            raise ValueError(msg)\n\n        def process_file_standard(file_path: str, *, silent_errors: bool = False) -> Data | None:\n            try:\n                return parse_text_file_to_data(file_path, silent_errors=silent_errors)\n            except FileNotFoundError as e:\n                self.log(f\"File not found: {file_path}. Error: {e}\")\n                if not silent_errors:\n                    raise\n                return None\n            except Exception as e:\n                self.log(f\"Unexpected error processing {file_path}: {e}\")\n                if not silent_errors:\n                    raise\n                return None\n\n        # Advanced path: only for a single Docling-compatible file\n        if len(file_list) == 1:\n            file_path = str(file_list[0].path)\n            if self.advanced_mode and self._is_docling_compatible(file_path):\n                advanced_data: Data | None = self._process_docling_in_subprocess(file_path)\n\n                # --- UNNEST: expand each element in `doc` to its own Data row\n                payload = getattr(advanced_data, \"data\", {}) or {}\n                doc_rows = payload.get(\"doc\")\n                if isinstance(doc_rows, list):\n                    rows: list[Data | None] = [\n                        Data(\n                            data={\n                                \"file_path\": file_path,\n                                **(item if isinstance(item, dict) else {\"value\": item}),\n                            },\n                        )\n                        for item in doc_rows\n                    ]\n                    return self.rollup_data(file_list, rows)\n\n                # If not structured, keep as-is (e.g., markdown export or error dict)\n                return self.rollup_data(file_list, [advanced_data])\n\n        # Standard multi-file (or single non-advanced) path\n        concurrency = 1 if not self.use_multithreading else max(1, self.concurrency_multithreading)\n        file_paths = [str(f.path) for f in file_list]\n        self.log(f\"Starting parallel processing of {len(file_paths)} files with concurrency: {concurrency}.\")\n        my_data = parallel_load_data(\n            file_paths,\n            silent_errors=self.silent_errors,\n            load_function=process_file_standard,\n            max_concurrency=concurrency,\n        )\n        return self.rollup_data(file_list, my_data)\n\n    # ------------------------------ Output helpers -----------------------------------\n\n    def load_files_helper(self) -> DataFrame:\n        result = self.load_files()\n\n        # Error condition - raise error if no text and an error is present\n        if not hasattr(result, \"text\"):\n            if hasattr(result, \"error\"):\n                raise ValueError(result.error[0])\n            msg = \"No content generated.\"\n            raise ValueError(msg)\n\n        return result\n\n    def load_files_dataframe(self) -> DataFrame:\n        \"\"\"Load files using advanced Docling processing and export to DataFrame format.\"\"\"\n        self.markdown = False\n        return self.load_files_helper()\n\n    def load_files_markdown(self) -> Message:\n        \"\"\"Load files using advanced Docling processing and export to Markdown format.\"\"\"\n        self.markdown = True\n        result = self.load_files_helper()\n        return Message(text=str(result.text[0]))\n"
              },
              "concurrency_multithreading": {
                "_input_type": "IntInput",
                "advanced": true,
                "display_name": "Processing Concurrency",
                "dynamic": false,
                "info": "When multiple files are being processed, the number of files to process concurrently.",
                "list": false,
                "list_add_label": "Add More",
                "name": "concurrency_multithreading",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "int",
                "value": 1
              },
              "delete_server_file_after_processing": {
                "_input_type": "BoolInput",
                "advanced": true,
                "display_name": "Delete Server File After Processing",
                "dynamic": false,
                "info": "If true, the Server File Path will be deleted after processing.",
                "list": false,
                "list_add_label": "Add More",
                "name": "delete_server_file_after_processing",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "bool",
                "value": true
              },
              "doc_key": {
                "_input_type": "MessageTextInput",
                "advanced": true,
                "display_name": "Doc Key",
                "dynamic": false,
                "info": "The key to use for the DoclingDocument column.",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "doc_key",
                "placeholder": "",
                "required": false,
                "show": false,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "doc"
              },
              "file_path": {
                "_input_type": "HandleInput",
                "advanced": true,
                "display_name": "Server File Path",
                "dynamic": false,
                "info": "Data object with a 'file_path' property pointing to server file or a Message object with a path to the file. Supercedes 'Path' but supports same file types.",
                "input_types": [
                  "Data",
                  "Message"
                ],
                "list": true,
                "list_add_label": "Add More",
                "name": "file_path",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "trace_as_metadata": true,
                "type": "other",
                "value": ""
              },
              "ignore_unspecified_files": {
                "_input_type": "BoolInput",
                "advanced": true,
                "display_name": "Ignore Unspecified Files",
                "dynamic": false,
                "info": "If true, Data with no 'file_path' property will be ignored.",
                "list": false,
                "list_add_label": "Add More",
                "name": "ignore_unspecified_files",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "bool",
                "value": false
              },
              "ignore_unsupported_extensions": {
                "_input_type": "BoolInput",
                "advanced": true,
                "display_name": "Ignore Unsupported Extensions",
                "dynamic": false,
                "info": "If true, files with unsupported extensions will not be processed.",
                "list": false,
                "list_add_label": "Add More",
                "name": "ignore_unsupported_extensions",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "bool",
                "value": true
              },
              "markdown": {
                "_input_type": "BoolInput",
                "advanced": false,
                "display_name": "Markdown Export",
                "dynamic": false,
                "info": "Export processed documents to Markdown format. Only available when advanced mode is enabled.",
                "list": false,
                "list_add_label": "Add More",
                "name": "markdown",
                "placeholder": "",
                "required": false,
                "show": false,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "bool",
                "value": false
              },
              "md_image_placeholder": {
                "_input_type": "StrInput",
                "advanced": true,
                "display_name": "Image placeholder",
                "dynamic": false,
                "info": "Specify the image placeholder for markdown exports.",
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "md_image_placeholder",
                "placeholder": "",
                "required": false,
                "show": false,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "str",
                "value": "<!-- image -->"
              },
              "md_page_break_placeholder": {
                "_input_type": "StrInput",
                "advanced": true,
                "display_name": "Page break placeholder",
                "dynamic": false,
                "info": "Add this placeholder between pages in the markdown output.",
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "md_page_break_placeholder",
                "placeholder": "",
                "required": false,
                "show": false,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "str",
                "value": ""
              },
              "ocr_engine": {
                "_input_type": "DropdownInput",
                "advanced": true,
                "combobox": false,
                "dialog_inputs": {},
                "display_name": "OCR Engine",
                "dynamic": false,
                "external_options": {},
                "info": "OCR engine to use. Only available when pipeline is set to 'standard'.",
                "name": "ocr_engine",
                "options": [
                  "None",
                  "easyocr"
                ],
                "options_metadata": [],
                "placeholder": "",
                "required": false,
                "show": false,
                "title_case": false,
                "toggle": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "str",
                "value": "easyocr"
              },
              "path": {
                "_input_type": "FileInput",
                "advanced": false,
                "display_name": "Files",
                "dynamic": false,
                "fileTypes": [
                  "csv",
                  "json",
                  "pdf",
                  "txt",
                  "md",
                  "mdx",
                  "yaml",
                  "yml",
                  "xml",
                  "html",
                  "htm",
                  "docx",
                  "py",
                  "sh",
                  "sql",
                  "js",
                  "ts",
                  "tsx",
                  "adoc",
                  "asciidoc",
                  "asc",
                  "bmp",
                  "dotx",
                  "dotm",
                  "docm",
                  "jpeg",
                  "png",
                  "potx",
                  "ppsx",
                  "pptm",
                  "potm",
                  "ppsm",
                  "pptx",
                  "tiff",
                  "xls",
                  "xlsx",
                  "xhtml",
                  "webp",
                  "zip",
                  "tar",
                  "tgz",
                  "bz2",
                  "gz"
                ],
                "file_path": [],
                "info": "Supported file extensions: csv, json, pdf, txt, md, mdx, yaml, yml, xml, html, htm, docx, py, sh, sql, js, ts, tsx, adoc, asciidoc, asc, bmp, dotx, dotm, docm, jpeg, png, potx, ppsx, pptm, potm, ppsm, pptx, tiff, xls, xlsx, xhtml, webp; optionally bundled in file extensions: zip, tar, tgz, bz2, gz",
                "list": true,
                "list_add_label": "Add More",
                "name": "path",
                "placeholder": "",
                "real_time_refresh": true,
                "required": false,
                "show": true,
                "temp_file": false,
                "title_case": false,
                "trace_as_metadata": true,
                "type": "file",
                "value": ""
              },
              "pipeline": {
                "_input_type": "DropdownInput",
                "advanced": true,
                "combobox": false,
                "dialog_inputs": {},
                "display_name": "Pipeline",
                "dynamic": false,
                "external_options": {},
                "info": "Docling pipeline to use",
                "name": "pipeline",
                "options": [
                  "standard",
                  "vlm"
                ],
                "options_metadata": [],
                "placeholder": "",
                "real_time_refresh": true,
                "required": false,
                "show": false,
                "title_case": false,
                "toggle": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "str",
                "value": "standard"
              },
              "separator": {
                "_input_type": "StrInput",
                "advanced": true,
                "display_name": "Separator",
                "dynamic": false,
                "info": "Specify the separator to use between multiple outputs in Message format.",
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "separator",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "str",
                "value": "\n\n"
              },
              "silent_errors": {
                "_input_type": "BoolInput",
                "advanced": true,
                "display_name": "Silent Errors",
                "dynamic": false,
                "info": "If true, errors will not raise an exception.",
                "list": false,
                "list_add_label": "Add More",
                "name": "silent_errors",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "bool",
                "value": false
              },
              "use_multithreading": {
                "_input_type": "BoolInput",
                "advanced": true,
                "display_name": "[Deprecated] Use Multithreading",
                "dynamic": false,
                "info": "Set 'Processing Concurrency' greater than 1 to enable multithreading.",
                "list": false,
                "list_add_label": "Add More",
                "name": "use_multithreading",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "bool",
                "value": true
              }
            },
            "tool_mode": false
          },
          "selected_output": "dataframe",
          "showNode": true,
          "type": "File"
        },
        "dragging": false,
        "id": "File-QOVjK",
        "measured": {
          "height": 213,
          "width": 320
        },
        "position": {
          "x": 610.1592011789115,
          "y": 755.2052263469712
        },
        "selected": false,
        "type": "genericNode"
      },
      {
        "data": {
          "id": "Agent-0r7mv",
          "node": {
            "base_classes": [
              "Message"
            ],
            "beta": false,
            "conditional_paths": [],
            "custom_fields": {},
            "description": "Define the agent's instructions, then enter a task to complete using tools.",
            "display_name": "Agent",
            "documentation": "https://docs.langflow.org/agents",
            "edited": false,
            "field_order": [
              "agent_llm",
              "max_tokens",
              "model_kwargs",
              "model_name",
              "openai_api_base",
              "api_key",
              "temperature",
              "seed",
              "max_retries",
              "timeout",
              "system_prompt",
              "n_messages",
              "format_instructions",
              "output_schema",
              "tools",
              "input_value",
              "handle_parsing_errors",
              "verbose",
              "max_iterations",
              "agent_description",
              "add_current_date_tool"
            ],
            "frozen": false,
            "icon": "bot",
            "last_updated": "2025-12-23T01:31:42.741Z",
            "legacy": false,
            "lf_version": "1.6.0",
            "metadata": {},
            "minimized": false,
            "output_types": [],
            "outputs": [
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Response",
                "group_outputs": false,
                "method": "message_response",
                "name": "response",
                "options": null,
                "required_inputs": null,
                "selected": "Message",
                "tool_mode": true,
                "types": [
                  "Message"
                ],
                "value": "__UNDEFINED__"
              }
            ],
            "pinned": false,
            "template": {
              "_type": "Component",
              "add_current_date_tool": {
                "_input_type": "BoolInput",
                "advanced": true,
                "display_name": "Current Date",
                "dynamic": false,
                "info": "If true, will add a tool to the agent that returns the current date.",
                "list": false,
                "list_add_label": "Add More",
                "name": "add_current_date_tool",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "bool",
                "value": true
              },
              "agent_description": {
                "_input_type": "MultilineInput",
                "advanced": true,
                "copy_field": false,
                "display_name": "Agent Description [Deprecated]",
                "dynamic": false,
                "info": "The description of the agent. This is only used when in Tool Mode. Defaults to 'A helpful assistant with access to the following tools:' and tools are added dynamically. This feature is deprecated and will be removed in future versions.",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "multiline": true,
                "name": "agent_description",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "A helpful assistant with access to the following tools:"
              },
              "agent_llm": {
                "_input_type": "DropdownInput",
                "advanced": false,
                "combobox": false,
                "dialog_inputs": {},
                "display_name": "Model Provider",
                "dynamic": false,
                "external_options": {
                  "fields": {
                    "data": {
                      "node": {
                        "display_name": "Connect other models",
                        "icon": "CornerDownLeft",
                        "name": "connect_other_models"
                      }
                    }
                  }
                },
                "info": "The provider of the language model that the agent will use to generate responses.",
                "input_types": [],
                "name": "agent_llm",
                "options": [
                  "Anthropic",
                  "Google Generative AI",
                  "OpenAI"
                ],
                "options_metadata": [
                  {
                    "icon": "Anthropic"
                  },
                  {
                    "icon": "GoogleGenerativeAI"
                  },
                  {
                    "icon": "OpenAI"
                  }
                ],
                "placeholder": "",
                "real_time_refresh": true,
                "refresh_button": false,
                "required": false,
                "show": true,
                "title_case": false,
                "toggle": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "str",
                "value": "OpenAI"
              },
              "api_key": {
                "_input_type": "SecretStrInput",
                "advanced": false,
                "display_name": "OpenAI API Key",
                "dynamic": false,
                "info": "The OpenAI API Key to use for the OpenAI model.",
                "input_types": [],
                "load_from_db": false,
                "name": "api_key",
                "password": true,
                "placeholder": "",
                "real_time_refresh": true,
                "required": false,
                "show": true,
                "title_case": false,
                "type": "str",
                "value": ""
              },
              "code": {
                "advanced": true,
                "dynamic": true,
                "fileTypes": [],
                "file_path": "",
                "info": "",
                "list": false,
                "load_from_db": false,
                "multiline": true,
                "name": "code",
                "password": false,
                "placeholder": "",
                "required": true,
                "show": true,
                "title_case": false,
                "type": "code",
                "value": "import json\nimport re\n\nfrom langchain_core.tools import StructuredTool\nfrom pydantic import ValidationError\n\nfrom langflow.base.agents.agent import LCToolsAgentComponent\nfrom langflow.base.agents.events import ExceptionWithMessageError\nfrom langflow.base.models.model_input_constants import (\n    ALL_PROVIDER_FIELDS,\n    MODEL_DYNAMIC_UPDATE_FIELDS,\n    MODEL_PROVIDERS_DICT,\n    MODELS_METADATA,\n)\nfrom langflow.base.models.model_utils import get_model_name\nfrom langflow.components.helpers.current_date import CurrentDateComponent\nfrom langflow.components.helpers.memory import MemoryComponent\nfrom langflow.components.langchain_utilities.tool_calling import (\n    ToolCallingAgentComponent,\n)\nfrom langflow.custom.custom_component.component import _get_component_toolkit\nfrom langflow.custom.utils import update_component_build_config\nfrom langflow.field_typing import Tool\nfrom langflow.helpers.base_model import build_model_from_schema\nfrom langflow.io import (\n    BoolInput,\n    DropdownInput,\n    IntInput,\n    MultilineInput,\n    Output,\n    TableInput,\n)\nfrom langflow.logging import logger\nfrom langflow.schema.data import Data\nfrom langflow.schema.dotdict import dotdict\nfrom langflow.schema.message import Message\nfrom langflow.schema.table import EditMode\n\n\ndef set_advanced_true(component_input):\n    component_input.advanced = True\n    return component_input\n\n\nMODEL_PROVIDERS_LIST = [\"Anthropic\", \"Google Generative AI\", \"OpenAI\"]\n\n\nclass AgentComponent(ToolCallingAgentComponent):\n    display_name: str = \"Agent\"\n    description: str = \"Define the agent's instructions, then enter a task to complete using tools.\"\n    documentation: str = \"https://docs.langflow.org/agents\"\n    icon = \"bot\"\n    beta = False\n    name = \"Agent\"\n\n    memory_inputs = [set_advanced_true(component_input) for component_input in MemoryComponent().inputs]\n\n    # Filter out json_mode from OpenAI inputs since we handle structured output differently\n    openai_inputs_filtered = [\n        input_field\n        for input_field in MODEL_PROVIDERS_DICT[\"OpenAI\"][\"inputs\"]\n        if not (hasattr(input_field, \"name\") and input_field.name == \"json_mode\")\n    ]\n\n    inputs = [\n        DropdownInput(\n            name=\"agent_llm\",\n            display_name=\"Model Provider\",\n            info=\"The provider of the language model that the agent will use to generate responses.\",\n            options=[*MODEL_PROVIDERS_LIST],\n            value=\"OpenAI\",\n            real_time_refresh=True,\n            refresh_button=False,\n            input_types=[],\n            options_metadata=[MODELS_METADATA[key] for key in MODEL_PROVIDERS_LIST],\n            external_options={\n                \"fields\": {\n                    \"data\": {\n                        \"node\": {\n                            \"name\": \"connect_other_models\",\n                            \"display_name\": \"Connect other models\",\n                            \"icon\": \"CornerDownLeft\",\n                        }\n                    }\n                },\n            },\n        ),\n        *openai_inputs_filtered,\n        MultilineInput(\n            name=\"system_prompt\",\n            display_name=\"Agent Instructions\",\n            info=\"System Prompt: Initial instructions and context provided to guide the agent's behavior.\",\n            value=\"You are a helpful assistant that can use tools to answer questions and perform tasks.\",\n            advanced=False,\n        ),\n        IntInput(\n            name=\"n_messages\",\n            display_name=\"Number of Chat History Messages\",\n            value=100,\n            info=\"Number of chat history messages to retrieve.\",\n            advanced=True,\n            show=True,\n        ),\n        MultilineInput(\n            name=\"format_instructions\",\n            display_name=\"Output Format Instructions\",\n            info=\"Generic Template for structured output formatting. Valid only with Structured response.\",\n            value=(\n                \"You are an AI that extracts structured JSON objects from unstructured text. \"\n                \"Use a predefined schema with expected types (str, int, float, bool, dict). \"\n                \"Extract ALL relevant instances that match the schema - if multiple patterns exist, capture them all. \"\n                \"Fill missing or ambiguous values with defaults: null for missing values. \"\n                \"Remove exact duplicates but keep variations that have different field values. \"\n                \"Always return valid JSON in the expected format, never throw errors. \"\n                \"If multiple objects can be extracted, return them all in the structured format.\"\n            ),\n            advanced=True,\n        ),\n        TableInput(\n            name=\"output_schema\",\n            display_name=\"Output Schema\",\n            info=(\n                \"Schema Validation: Define the structure and data types for structured output. \"\n                \"No validation if no output schema.\"\n            ),\n            advanced=True,\n            required=False,\n            value=[],\n            table_schema=[\n                {\n                    \"name\": \"name\",\n                    \"display_name\": \"Name\",\n                    \"type\": \"str\",\n                    \"description\": \"Specify the name of the output field.\",\n                    \"default\": \"field\",\n                    \"edit_mode\": EditMode.INLINE,\n                },\n                {\n                    \"name\": \"description\",\n                    \"display_name\": \"Description\",\n                    \"type\": \"str\",\n                    \"description\": \"Describe the purpose of the output field.\",\n                    \"default\": \"description of field\",\n                    \"edit_mode\": EditMode.POPOVER,\n                },\n                {\n                    \"name\": \"type\",\n                    \"display_name\": \"Type\",\n                    \"type\": \"str\",\n                    \"edit_mode\": EditMode.INLINE,\n                    \"description\": (\"Indicate the data type of the output field (e.g., str, int, float, bool, dict).\"),\n                    \"options\": [\"str\", \"int\", \"float\", \"bool\", \"dict\"],\n                    \"default\": \"str\",\n                },\n                {\n                    \"name\": \"multiple\",\n                    \"display_name\": \"As List\",\n                    \"type\": \"boolean\",\n                    \"description\": \"Set to True if this output field should be a list of the specified type.\",\n                    \"default\": \"False\",\n                    \"edit_mode\": EditMode.INLINE,\n                },\n            ],\n        ),\n        *LCToolsAgentComponent._base_inputs,\n        # removed memory inputs from agent component\n        # *memory_inputs,\n        BoolInput(\n            name=\"add_current_date_tool\",\n            display_name=\"Current Date\",\n            advanced=True,\n            info=\"If true, will add a tool to the agent that returns the current date.\",\n            value=True,\n        ),\n    ]\n    outputs = [\n        Output(name=\"response\", display_name=\"Response\", method=\"message_response\"),\n    ]\n\n    async def get_agent_requirements(self):\n        \"\"\"Get the agent requirements for the agent.\"\"\"\n        llm_model, display_name = await self.get_llm()\n        if llm_model is None:\n            msg = \"No language model selected. Please choose a model to proceed.\"\n            raise ValueError(msg)\n        self.model_name = get_model_name(llm_model, display_name=display_name)\n\n        # Get memory data\n        self.chat_history = await self.get_memory_data()\n        if isinstance(self.chat_history, Message):\n            self.chat_history = [self.chat_history]\n\n        # Add current date tool if enabled\n        if self.add_current_date_tool:\n            if not isinstance(self.tools, list):  # type: ignore[has-type]\n                self.tools = []\n            current_date_tool = (await CurrentDateComponent(**self.get_base_args()).to_toolkit()).pop(0)\n            if not isinstance(current_date_tool, StructuredTool):\n                msg = \"CurrentDateComponent must be converted to a StructuredTool\"\n                raise TypeError(msg)\n            self.tools.append(current_date_tool)\n        return llm_model, self.chat_history, self.tools\n\n    async def message_response(self) -> Message:\n        try:\n            llm_model, self.chat_history, self.tools = await self.get_agent_requirements()\n            # Set up and run agent\n            self.set(\n                llm=llm_model,\n                tools=self.tools or [],\n                chat_history=self.chat_history,\n                input_value=self.input_value,\n                system_prompt=self.system_prompt,\n            )\n            agent = self.create_agent_runnable()\n            result = await self.run_agent(agent)\n\n            # Store result for potential JSON output\n            self._agent_result = result\n\n        except (ValueError, TypeError, KeyError) as e:\n            await logger.aerror(f\"{type(e).__name__}: {e!s}\")\n            raise\n        except ExceptionWithMessageError as e:\n            await logger.aerror(f\"ExceptionWithMessageError occurred: {e}\")\n            raise\n        # Avoid catching blind Exception; let truly unexpected exceptions propagate\n        except Exception as e:\n            await logger.aerror(f\"Unexpected error: {e!s}\")\n            raise\n        else:\n            return result\n\n    def _preprocess_schema(self, schema):\n        \"\"\"Preprocess schema to ensure correct data types for build_model_from_schema.\"\"\"\n        processed_schema = []\n        for field in schema:\n            processed_field = {\n                \"name\": str(field.get(\"name\", \"field\")),\n                \"type\": str(field.get(\"type\", \"str\")),\n                \"description\": str(field.get(\"description\", \"\")),\n                \"multiple\": field.get(\"multiple\", False),\n            }\n            # Ensure multiple is handled correctly\n            if isinstance(processed_field[\"multiple\"], str):\n                processed_field[\"multiple\"] = processed_field[\"multiple\"].lower() in [\n                    \"true\",\n                    \"1\",\n                    \"t\",\n                    \"y\",\n                    \"yes\",\n                ]\n            processed_schema.append(processed_field)\n        return processed_schema\n\n    async def build_structured_output_base(self, content: str):\n        \"\"\"Build structured output with optional BaseModel validation.\"\"\"\n        json_pattern = r\"\\{.*\\}\"\n        schema_error_msg = \"Try setting an output schema\"\n\n        # Try to parse content as JSON first\n        json_data = None\n        try:\n            json_data = json.loads(content)\n        except json.JSONDecodeError:\n            json_match = re.search(json_pattern, content, re.DOTALL)\n            if json_match:\n                try:\n                    json_data = json.loads(json_match.group())\n                except json.JSONDecodeError:\n                    return {\"content\": content, \"error\": schema_error_msg}\n            else:\n                return {\"content\": content, \"error\": schema_error_msg}\n\n        # If no output schema provided, return parsed JSON without validation\n        if not hasattr(self, \"output_schema\") or not self.output_schema or len(self.output_schema) == 0:\n            return json_data\n\n        # Use BaseModel validation with schema\n        try:\n            processed_schema = self._preprocess_schema(self.output_schema)\n            output_model = build_model_from_schema(processed_schema)\n\n            # Validate against the schema\n            if isinstance(json_data, list):\n                # Multiple objects\n                validated_objects = []\n                for item in json_data:\n                    try:\n                        validated_obj = output_model.model_validate(item)\n                        validated_objects.append(validated_obj.model_dump())\n                    except ValidationError as e:\n                        await logger.aerror(f\"Validation error for item: {e}\")\n                        # Include invalid items with error info\n                        validated_objects.append({\"data\": item, \"validation_error\": str(e)})\n                return validated_objects\n\n            # Single object\n            try:\n                validated_obj = output_model.model_validate(json_data)\n                return [validated_obj.model_dump()]  # Return as list for consistency\n            except ValidationError as e:\n                await logger.aerror(f\"Validation error: {e}\")\n                return [{\"data\": json_data, \"validation_error\": str(e)}]\n\n        except (TypeError, ValueError) as e:\n            await logger.aerror(f\"Error building structured output: {e}\")\n            # Fallback to parsed JSON without validation\n            return json_data\n\n    async def json_response(self) -> Data:\n        \"\"\"Convert agent response to structured JSON Data output with schema validation.\"\"\"\n        # Always use structured chat agent for JSON response mode for better JSON formatting\n        try:\n            system_components = []\n\n            # 1. Agent Instructions (system_prompt)\n            agent_instructions = getattr(self, \"system_prompt\", \"\") or \"\"\n            if agent_instructions:\n                system_components.append(f\"{agent_instructions}\")\n\n            # 2. Format Instructions\n            format_instructions = getattr(self, \"format_instructions\", \"\") or \"\"\n            if format_instructions:\n                system_components.append(f\"Format instructions: {format_instructions}\")\n\n            # 3. Schema Information from BaseModel\n            if hasattr(self, \"output_schema\") and self.output_schema and len(self.output_schema) > 0:\n                try:\n                    processed_schema = self._preprocess_schema(self.output_schema)\n                    output_model = build_model_from_schema(processed_schema)\n                    schema_dict = output_model.model_json_schema()\n                    schema_info = (\n                        \"You are given some text that may include format instructions, \"\n                        \"explanations, or other content alongside a JSON schema.\\n\\n\"\n                        \"Your task:\\n\"\n                        \"- Extract only the JSON schema.\\n\"\n                        \"- Return it as valid JSON.\\n\"\n                        \"- Do not include format instructions, explanations, or extra text.\\n\\n\"\n                        \"Input:\\n\"\n                        f\"{json.dumps(schema_dict, indent=2)}\\n\\n\"\n                        \"Output (only JSON schema):\"\n                    )\n                    system_components.append(schema_info)\n                except (ValidationError, ValueError, TypeError, KeyError) as e:\n                    await logger.aerror(f\"Could not build schema for prompt: {e}\", exc_info=True)\n\n            # Combine all components\n            combined_instructions = \"\\n\\n\".join(system_components) if system_components else \"\"\n            llm_model, self.chat_history, self.tools = await self.get_agent_requirements()\n            self.set(\n                llm=llm_model,\n                tools=self.tools or [],\n                chat_history=self.chat_history,\n                input_value=self.input_value,\n                system_prompt=combined_instructions,\n            )\n\n            # Create and run structured chat agent\n            try:\n                structured_agent = self.create_agent_runnable()\n            except (NotImplementedError, ValueError, TypeError) as e:\n                await logger.aerror(f\"Error with structured chat agent: {e}\")\n                raise\n            try:\n                result = await self.run_agent(structured_agent)\n            except (\n                ExceptionWithMessageError,\n                ValueError,\n                TypeError,\n                RuntimeError,\n            ) as e:\n                await logger.aerror(f\"Error with structured agent result: {e}\")\n                raise\n            # Extract content from structured agent result\n            if hasattr(result, \"content\"):\n                content = result.content\n            elif hasattr(result, \"text\"):\n                content = result.text\n            else:\n                content = str(result)\n\n        except (\n            ExceptionWithMessageError,\n            ValueError,\n            TypeError,\n            NotImplementedError,\n            AttributeError,\n        ) as e:\n            await logger.aerror(f\"Error with structured chat agent: {e}\")\n            # Fallback to regular agent\n            content_str = \"No content returned from agent\"\n            return Data(data={\"content\": content_str, \"error\": str(e)})\n\n        # Process with structured output validation\n        try:\n            structured_output = await self.build_structured_output_base(content)\n\n            # Handle different output formats\n            if isinstance(structured_output, list) and structured_output:\n                if len(structured_output) == 1:\n                    return Data(data=structured_output[0])\n                return Data(data={\"results\": structured_output})\n            if isinstance(structured_output, dict):\n                return Data(data=structured_output)\n            return Data(data={\"content\": content})\n\n        except (ValueError, TypeError) as e:\n            await logger.aerror(f\"Error in structured output processing: {e}\")\n            return Data(data={\"content\": content, \"error\": str(e)})\n\n    async def get_memory_data(self):\n        # TODO: This is a temporary fix to avoid message duplication. We should develop a function for this.\n        messages = (\n            await MemoryComponent(**self.get_base_args())\n            .set(\n                session_id=self.graph.session_id,\n                order=\"Ascending\",\n                n_messages=self.n_messages,\n            )\n            .retrieve_messages()\n        )\n        return [\n            message for message in messages if getattr(message, \"id\", None) != getattr(self.input_value, \"id\", None)\n        ]\n\n    async def get_llm(self):\n        if not isinstance(self.agent_llm, str):\n            return self.agent_llm, None\n\n        try:\n            provider_info = MODEL_PROVIDERS_DICT.get(self.agent_llm)\n            if not provider_info:\n                msg = f\"Invalid model provider: {self.agent_llm}\"\n                raise ValueError(msg)\n\n            component_class = provider_info.get(\"component_class\")\n            display_name = component_class.display_name\n            inputs = provider_info.get(\"inputs\")\n            prefix = provider_info.get(\"prefix\", \"\")\n\n            return self._build_llm_model(component_class, inputs, prefix), display_name\n\n        except (AttributeError, ValueError, TypeError, RuntimeError) as e:\n            await logger.aerror(f\"Error building {self.agent_llm} language model: {e!s}\")\n            msg = f\"Failed to initialize language model: {e!s}\"\n            raise ValueError(msg) from e\n\n    def _build_llm_model(self, component, inputs, prefix=\"\"):\n        model_kwargs = {}\n        for input_ in inputs:\n            if hasattr(self, f\"{prefix}{input_.name}\"):\n                model_kwargs[input_.name] = getattr(self, f\"{prefix}{input_.name}\")\n        return component.set(**model_kwargs).build_model()\n\n    def set_component_params(self, component):\n        provider_info = MODEL_PROVIDERS_DICT.get(self.agent_llm)\n        if provider_info:\n            inputs = provider_info.get(\"inputs\")\n            prefix = provider_info.get(\"prefix\")\n            # Filter out json_mode and only use attributes that exist on this component\n            model_kwargs = {}\n            for input_ in inputs:\n                if hasattr(self, f\"{prefix}{input_.name}\"):\n                    model_kwargs[input_.name] = getattr(self, f\"{prefix}{input_.name}\")\n\n            return component.set(**model_kwargs)\n        return component\n\n    def delete_fields(self, build_config: dotdict, fields: dict | list[str]) -> None:\n        \"\"\"Delete specified fields from build_config.\"\"\"\n        for field in fields:\n            build_config.pop(field, None)\n\n    def update_input_types(self, build_config: dotdict) -> dotdict:\n        \"\"\"Update input types for all fields in build_config.\"\"\"\n        for key, value in build_config.items():\n            if isinstance(value, dict):\n                if value.get(\"input_types\") is None:\n                    build_config[key][\"input_types\"] = []\n            elif hasattr(value, \"input_types\") and value.input_types is None:\n                value.input_types = []\n        return build_config\n\n    async def update_build_config(\n        self, build_config: dotdict, field_value: str, field_name: str | None = None\n    ) -> dotdict:\n        # Iterate over all providers in the MODEL_PROVIDERS_DICT\n        # Existing logic for updating build_config\n        if field_name in (\"agent_llm\",):\n            build_config[\"agent_llm\"][\"value\"] = field_value\n            provider_info = MODEL_PROVIDERS_DICT.get(field_value)\n            if provider_info:\n                component_class = provider_info.get(\"component_class\")\n                if component_class and hasattr(component_class, \"update_build_config\"):\n                    # Call the component class's update_build_config method\n                    build_config = await update_component_build_config(\n                        component_class, build_config, field_value, \"model_name\"\n                    )\n\n            provider_configs: dict[str, tuple[dict, list[dict]]] = {\n                provider: (\n                    MODEL_PROVIDERS_DICT[provider][\"fields\"],\n                    [\n                        MODEL_PROVIDERS_DICT[other_provider][\"fields\"]\n                        for other_provider in MODEL_PROVIDERS_DICT\n                        if other_provider != provider\n                    ],\n                )\n                for provider in MODEL_PROVIDERS_DICT\n            }\n            if field_value in provider_configs:\n                fields_to_add, fields_to_delete = provider_configs[field_value]\n\n                # Delete fields from other providers\n                for fields in fields_to_delete:\n                    self.delete_fields(build_config, fields)\n\n                # Add provider-specific fields\n                if field_value == \"OpenAI\" and not any(field in build_config for field in fields_to_add):\n                    build_config.update(fields_to_add)\n                else:\n                    build_config.update(fields_to_add)\n                # Reset input types for agent_llm\n                build_config[\"agent_llm\"][\"input_types\"] = []\n                build_config[\"agent_llm\"][\"display_name\"] = \"Model Provider\"\n            elif field_value == \"connect_other_models\":\n                # Delete all provider fields\n                self.delete_fields(build_config, ALL_PROVIDER_FIELDS)\n                # # Update with custom component\n                custom_component = DropdownInput(\n                    name=\"agent_llm\",\n                    display_name=\"Language Model\",\n                    info=\"The provider of the language model that the agent will use to generate responses.\",\n                    options=[*MODEL_PROVIDERS_LIST],\n                    real_time_refresh=True,\n                    refresh_button=False,\n                    input_types=[\"LanguageModel\"],\n                    placeholder=\"Awaiting model input.\",\n                    options_metadata=[MODELS_METADATA[key] for key in MODEL_PROVIDERS_LIST],\n                    external_options={\n                        \"fields\": {\n                            \"data\": {\n                                \"node\": {\n                                    \"name\": \"connect_other_models\",\n                                    \"display_name\": \"Connect other models\",\n                                    \"icon\": \"CornerDownLeft\",\n                                },\n                            }\n                        },\n                    },\n                )\n                build_config.update({\"agent_llm\": custom_component.to_dict()})\n            # Update input types for all fields\n            build_config = self.update_input_types(build_config)\n\n            # Validate required keys\n            default_keys = [\n                \"code\",\n                \"_type\",\n                \"agent_llm\",\n                \"tools\",\n                \"input_value\",\n                \"add_current_date_tool\",\n                \"system_prompt\",\n                \"agent_description\",\n                \"max_iterations\",\n                \"handle_parsing_errors\",\n                \"verbose\",\n            ]\n            missing_keys = [key for key in default_keys if key not in build_config]\n            if missing_keys:\n                msg = f\"Missing required keys in build_config: {missing_keys}\"\n                raise ValueError(msg)\n        if (\n            isinstance(self.agent_llm, str)\n            and self.agent_llm in MODEL_PROVIDERS_DICT\n            and field_name in MODEL_DYNAMIC_UPDATE_FIELDS\n        ):\n            provider_info = MODEL_PROVIDERS_DICT.get(self.agent_llm)\n            if provider_info:\n                component_class = provider_info.get(\"component_class\")\n                component_class = self.set_component_params(component_class)\n                prefix = provider_info.get(\"prefix\")\n                if component_class and hasattr(component_class, \"update_build_config\"):\n                    # Call each component class's update_build_config method\n                    # remove the prefix from the field_name\n                    if isinstance(field_name, str) and isinstance(prefix, str):\n                        field_name = field_name.replace(prefix, \"\")\n                    build_config = await update_component_build_config(\n                        component_class, build_config, field_value, \"model_name\"\n                    )\n        return dotdict({k: v.to_dict() if hasattr(v, \"to_dict\") else v for k, v in build_config.items()})\n\n    async def _get_tools(self) -> list[Tool]:\n        component_toolkit = _get_component_toolkit()\n        tools_names = self._build_tools_names()\n        agent_description = self.get_tool_description()\n        # TODO: Agent Description Depreciated Feature to be removed\n        description = f\"{agent_description}{tools_names}\"\n        tools = component_toolkit(component=self).get_tools(\n            tool_name=\"Call_Agent\",\n            tool_description=description,\n            callbacks=self.get_langchain_callbacks(),\n        )\n        if hasattr(self, \"tools_metadata\"):\n            tools = component_toolkit(component=self, metadata=self.tools_metadata).update_tools_metadata(tools=tools)\n        return tools\n"
              },
              "format_instructions": {
                "_input_type": "MultilineInput",
                "advanced": true,
                "copy_field": false,
                "display_name": "Output Format Instructions",
                "dynamic": false,
                "info": "Generic Template for structured output formatting. Valid only with Structured response.",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "multiline": true,
                "name": "format_instructions",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "You are an AI that extracts structured JSON objects from unstructured text. Use a predefined schema with expected types (str, int, float, bool, dict). Extract ALL relevant instances that match the schema - if multiple patterns exist, capture them all. Fill missing or ambiguous values with defaults: null for missing values. Remove exact duplicates but keep variations that have different field values. Always return valid JSON in the expected format, never throw errors. If multiple objects can be extracted, return them all in the structured format."
              },
              "handle_parsing_errors": {
                "_input_type": "BoolInput",
                "advanced": true,
                "display_name": "Handle Parse Errors",
                "dynamic": false,
                "info": "Should the Agent fix errors when reading user input for better processing?",
                "list": false,
                "list_add_label": "Add More",
                "name": "handle_parsing_errors",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "bool",
                "value": true
              },
              "input_value": {
                "_input_type": "MessageInput",
                "advanced": false,
                "display_name": "Input",
                "dynamic": false,
                "info": "The input provided by the user for the agent to process.",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "input_value",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": true,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": ""
              },
              "max_iterations": {
                "_input_type": "IntInput",
                "advanced": true,
                "display_name": "Max Iterations",
                "dynamic": false,
                "info": "The maximum number of attempts the agent can make to complete its task before it stops.",
                "list": false,
                "list_add_label": "Add More",
                "name": "max_iterations",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "int",
                "value": 15
              },
              "max_retries": {
                "_input_type": "IntInput",
                "advanced": true,
                "display_name": "Max Retries",
                "dynamic": false,
                "info": "The maximum number of retries to make when generating.",
                "list": false,
                "list_add_label": "Add More",
                "name": "max_retries",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "int",
                "value": 5
              },
              "max_tokens": {
                "_input_type": "IntInput",
                "advanced": true,
                "display_name": "Max Tokens",
                "dynamic": false,
                "info": "The maximum number of tokens to generate. Set to 0 for unlimited tokens.",
                "list": false,
                "list_add_label": "Add More",
                "name": "max_tokens",
                "placeholder": "",
                "range_spec": {
                  "max": 128000,
                  "min": 0,
                  "step": 0.1,
                  "step_type": "float"
                },
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "int",
                "value": ""
              },
              "model_kwargs": {
                "_input_type": "DictInput",
                "advanced": true,
                "display_name": "Model Kwargs",
                "dynamic": false,
                "info": "Additional keyword arguments to pass to the model.",
                "list": false,
                "list_add_label": "Add More",
                "name": "model_kwargs",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "type": "dict",
                "value": {}
              },
              "model_name": {
                "_input_type": "DropdownInput",
                "advanced": false,
                "combobox": true,
                "dialog_inputs": {},
                "display_name": "Model Name",
                "dynamic": false,
                "external_options": {},
                "info": "To see the model names, first choose a provider. Then, enter your API key and click the refresh button next to the model name.",
                "name": "model_name",
                "options": [
                  "gpt-4o-mini",
                  "gpt-4o",
                  "gpt-4.1",
                  "gpt-4.1-mini",
                  "gpt-4.1-nano",
                  "gpt-4-turbo",
                  "gpt-4-turbo-preview",
                  "gpt-4",
                  "gpt-3.5-turbo",
                  "gpt-5",
                  "gpt-5-mini",
                  "gpt-5-nano",
                  "gpt-5-chat-latest",
                  "o1",
                  "o3-mini",
                  "o3",
                  "o3-pro",
                  "o4-mini",
                  "o4-mini-high"
                ],
                "options_metadata": [],
                "placeholder": "",
                "real_time_refresh": false,
                "required": false,
                "show": true,
                "title_case": false,
                "toggle": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "str",
                "value": "gpt-5"
              },
              "n_messages": {
                "_input_type": "IntInput",
                "advanced": true,
                "display_name": "Number of Chat History Messages",
                "dynamic": false,
                "info": "Number of chat history messages to retrieve.",
                "list": false,
                "list_add_label": "Add More",
                "name": "n_messages",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "int",
                "value": 100
              },
              "openai_api_base": {
                "_input_type": "StrInput",
                "advanced": true,
                "display_name": "OpenAI API Base",
                "dynamic": false,
                "info": "The base URL of the OpenAI API. Defaults to https://api.openai.com/v1. You can change this to use other APIs like JinaChat, LocalAI and Prem.",
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "openai_api_base",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "str",
                "value": ""
              },
              "output_schema": {
                "_input_type": "TableInput",
                "advanced": true,
                "display_name": "Output Schema",
                "dynamic": false,
                "info": "Schema Validation: Define the structure and data types for structured output. No validation if no output schema.",
                "is_list": true,
                "list_add_label": "Add More",
                "name": "output_schema",
                "placeholder": "",
                "required": false,
                "show": true,
                "table_icon": "Table",
                "table_schema": {
                  "columns": [
                    {
                      "default": "field",
                      "description": "Specify the name of the output field.",
                      "disable_edit": false,
                      "display_name": "Name",
                      "edit_mode": "inline",
                      "filterable": true,
                      "formatter": "text",
                      "hidden": false,
                      "name": "name",
                      "sortable": true,
                      "type": "str"
                    },
                    {
                      "default": "description of field",
                      "description": "Describe the purpose of the output field.",
                      "disable_edit": false,
                      "display_name": "Description",
                      "edit_mode": "popover",
                      "filterable": true,
                      "formatter": "text",
                      "hidden": false,
                      "name": "description",
                      "sortable": true,
                      "type": "str"
                    },
                    {
                      "default": "str",
                      "description": "Indicate the data type of the output field (e.g., str, int, float, bool, dict).",
                      "disable_edit": false,
                      "display_name": "Type",
                      "edit_mode": "inline",
                      "filterable": true,
                      "formatter": "text",
                      "hidden": false,
                      "name": "type",
                      "options": [
                        "str",
                        "int",
                        "float",
                        "bool",
                        "dict"
                      ],
                      "sortable": true,
                      "type": "str"
                    },
                    {
                      "default": false,
                      "description": "Set to True if this output field should be a list of the specified type.",
                      "disable_edit": false,
                      "display_name": "As List",
                      "edit_mode": "inline",
                      "filterable": true,
                      "formatter": "boolean",
                      "hidden": false,
                      "name": "multiple",
                      "sortable": true,
                      "type": "boolean"
                    }
                  ]
                },
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "trigger_icon": "Table",
                "trigger_text": "Open table",
                "type": "table",
                "value": []
              },
              "seed": {
                "_input_type": "IntInput",
                "advanced": true,
                "display_name": "Seed",
                "dynamic": false,
                "info": "The seed controls the reproducibility of the job.",
                "list": false,
                "list_add_label": "Add More",
                "name": "seed",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "int",
                "value": 1
              },
              "system_prompt": {
                "_input_type": "MultilineInput",
                "advanced": false,
                "copy_field": false,
                "display_name": "Agent Instructions",
                "dynamic": false,
                "info": "System Prompt: Initial instructions and context provided to guide the agent's behavior.",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "multiline": true,
                "name": "system_prompt",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "You are an MSP helpdesk operations analyst and product specialist. You will be given a dataset of helpdesk tickets that has already been loaded from CSV and lightly cleaned. Your job is to produce quantitative insights and clear recommendations.\n\nCore objectives\n\t1.\tIdentify usage patterns in the ticket data, including volume trends over time, spikes, seasonality, distribution by day of week and time of day if timestamps exist, and breakdowns by board, type, subtype, source, status, and priority.\n\t2.\tEstimate which work is likely AI-addressable versus human-required, using rules of thumb based on ticket metadata. Always explain the rules and show counts and percentages.\n\t3.\tIdentify which client companies are the most “needy” and quantify this precisely with multiple metrics, not just raw ticket count.\n\nRules and constraints\n\t•\tUse only the provided ticket data. Do not assume facts not in the dataset.\n\t•\tIf a field needed for a metric is missing or too sparse, explicitly say so and propose the closest proxy.\n\t•\tQuantify everything with numbers and percentages, and include top 10 rankings where relevant.\n\t•\tCall out data quality issues (missing time, inconsistent categories, overly broad subtypes like “Default”, null closed dates, etc.).\n\t•\tPrefer medians and percentiles over averages. Highlight long-tail behavior.\n\t•\tKeep outputs concise and decision-oriented. Avoid jargon.\n\nDefinitions and heuristics\n\t•\tTicket volume: count of tickets created (Entered date).\n\t•\tClosed tickets: tickets with a valid Closed date or a closed status.\n\t•\tResolution time: Closed datetime minus Entered datetime, in hours and days. Report median, p75, p90, p95.\n\t•\t“Needy client” scoring: compute and present at least these per-company metrics:\na) total ticket volume\nb) tickets per active month\nc) tickets per weekday (if dates exist)\nd) share of all tickets (%)\ne) median resolution time for that client\nf) percentage of tickets arriving outside business hours (local time assumed America/Los_Angeles if time exists)\ng) concentration by category (board/type/subtype/source), to show what they struggle with\nThen provide a composite “Neediness Index” that you define clearly as a weighted score, and show the top 10 clients by this index.\n\nAI vs human task estimation\n\t•\tCreate a classification called “Automation Potential” with values: High, Medium, Low.\n\t•\tUse only ticket metadata such as board, type, subtype, source, summary/subject, and status.\n\t•\tBase the classification on the following principles:\nHigh: repetitive service requests, account/access, password/MFA, permissions, software install, device inventory, monitoring alerts, standard triage, routine ticket updates, documentation tasks, known-error fixes.\nMedium: troubleshooting that likely needs diagnostics but could be accelerated, or workflows requiring approvals and multi-step actions.\nLow: complex incidents, network changes, security incidents, vendor coordination, outages, high-risk changes, projects, anything requiring deep judgment.\n\t•\tOutput a table that shows counts and percent of tickets by Automation Potential overall, and by company, and by board/type/subtype/source (top 10 each).\n\t•\tProvide an estimate of “AI-addressable share” as High + a defined fraction of Medium. Default: High = 100% addressable, Medium = 40% addressable, Low = 0%. If you change these assumptions, explain why.\n\nRequired output format\nReturn results in this structure:\n\nA) Executive summary (5 bullets)\nB) Usage patterns\n\t•\tTime trends (monthly)\n\t•\tTime-of-day/day-of-week (if possible)\n\t•\tBreakdown by board/type/subtype/source/status/priority\nC) Neediest clients\n\t•\tTop 10 by volume\n\t•\tTop 10 by Neediness Index (show the formula and the score)\n\t•\tFor each top client: what categories drive their tickets and what to fix first\nD) AI vs human task split\n\t•\tOverall addressable share estimate\n\t•\tTop 10 categories most AI-addressable\n\t•\tTop 10 clients with highest AI-addressable volume\nE) Recommendations\n\t•\t10 specific automation or workflow opportunities, prioritized by impact and ease\nF) Data quality notes\n\t•\tTop issues and how to improve future exports\n\nTone\nBe crisp and analytical. Use plain language. When you make an assumption, state it explicitly and keep it conservative."
              },
              "temperature": {
                "_input_type": "SliderInput",
                "advanced": true,
                "display_name": "Temperature",
                "dynamic": false,
                "info": "",
                "max_label": "",
                "max_label_icon": "",
                "min_label": "",
                "min_label_icon": "",
                "name": "temperature",
                "placeholder": "",
                "range_spec": {
                  "max": 1,
                  "min": 0,
                  "step": 0.01,
                  "step_type": "float"
                },
                "required": false,
                "show": true,
                "slider_buttons": false,
                "slider_buttons_options": [],
                "slider_input": false,
                "title_case": false,
                "tool_mode": false,
                "type": "slider",
                "value": 0.1
              },
              "timeout": {
                "_input_type": "IntInput",
                "advanced": true,
                "display_name": "Timeout",
                "dynamic": false,
                "info": "The timeout for requests to OpenAI completion API.",
                "list": false,
                "list_add_label": "Add More",
                "name": "timeout",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "int",
                "value": 700
              },
              "tools": {
                "_input_type": "HandleInput",
                "advanced": false,
                "display_name": "Tools",
                "dynamic": false,
                "info": "These are the tools that the agent can use to help with tasks.",
                "input_types": [
                  "Tool"
                ],
                "list": true,
                "list_add_label": "Add More",
                "name": "tools",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "trace_as_metadata": true,
                "type": "other",
                "value": ""
              },
              "verbose": {
                "_input_type": "BoolInput",
                "advanced": true,
                "display_name": "Verbose",
                "dynamic": false,
                "info": "",
                "list": false,
                "list_add_label": "Add More",
                "name": "verbose",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "bool",
                "value": true
              }
            },
            "tool_mode": false
          },
          "showNode": true,
          "type": "Agent"
        },
        "dragging": false,
        "id": "Agent-0r7mv",
        "measured": {
          "height": 591,
          "width": 320
        },
        "position": {
          "x": 4908.241198232782,
          "y": 1011.9344589986795
        },
        "selected": false,
        "type": "genericNode"
      },
      {
        "data": {
          "id": "msp_ticket_etl-vH1pD",
          "node": {
            "base_classes": [
              "DataFrame",
              "Message"
            ],
            "beta": false,
            "conditional_paths": [],
            "custom_fields": {},
            "description": "Clean ticket CSVs, normalize columns, parse dates, and optionally drop tickets with no hours.",
            "display_name": "MSP Ticket ETL",
            "documentation": "",
            "edited": true,
            "field_order": [
              "tickets_df",
              "ticket_id_col",
              "ticket_total_hours_col",
              "entered_col",
              "closed_col",
              "drop_no_hours"
            ],
            "frozen": false,
            "icon": "FileSpreadsheet",
            "legacy": false,
            "lf_version": "1.6.0",
            "metadata": {},
            "minimized": false,
            "output_types": [],
            "outputs": [
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Tickets (Clean)",
                "group_outputs": false,
                "hidden": null,
                "method": "build_tickets_clean",
                "name": "tickets_clean",
                "options": null,
                "required_inputs": null,
                "selected": "DataFrame",
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "ETL Summary",
                "group_outputs": false,
                "hidden": null,
                "method": "build_etl_summary",
                "name": "etl_summary",
                "options": null,
                "required_inputs": null,
                "selected": "Message",
                "tool_mode": true,
                "types": [
                  "Message"
                ],
                "value": "__UNDEFINED__"
              }
            ],
            "pinned": false,
            "template": {
              "_type": "Component",
              "closed_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Ticket Closed date column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "closed_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Closed On"
              },
              "code": {
                "advanced": true,
                "dynamic": true,
                "fileTypes": [],
                "file_path": "",
                "info": "",
                "list": false,
                "load_from_db": false,
                "multiline": true,
                "name": "code",
                "password": false,
                "placeholder": "",
                "required": true,
                "show": true,
                "title_case": false,
                "type": "code",
                "value": "from __future__ import annotations\n\nfrom typing import Optional, Dict, Any\nimport pandas as pd\n\nfrom langflow.custom import Component\nfrom langflow.io import DataFrameInput, BoolInput, MessageTextInput, Output\nfrom langflow.schema import DataFrame\nfrom langflow.schema.message import Message\n\n\nclass MSPTicketETL(Component):\n    display_name = \"MSP Ticket ETL\"\n    description = \"Clean ticket CSVs, normalize columns, parse dates, and optionally drop tickets with no hours.\"\n    name = \"msp_ticket_etl\"\n    icon = \"FileSpreadsheet\"\n\n    inputs = [\n        DataFrameInput(name=\"tickets_df\", display_name=\"Tickets DataFrame\", required=True),\n\n        # Column mapping defaults (adjust to match your exports)\n        MessageTextInput(name=\"ticket_id_col\", display_name=\"Ticket ID column\", value=\"Ticket #\"),\n        MessageTextInput(name=\"ticket_total_hours_col\", display_name=\"Ticket Total Hours column (optional)\", value=\"Total Hours\"),\n\n        MessageTextInput(name=\"entered_col\", display_name=\"Ticket Entered date column\", value=\"Entered\"),\n        MessageTextInput(name=\"closed_col\", display_name=\"Ticket Closed date column\", value=\"Closed On\"),\n\n        BoolInput(name=\"drop_no_hours\", display_name=\"Drop tickets with no hours (uses Ticket Total Hours)\", value=False),\n    ]\n\n    outputs = [\n        Output(name=\"tickets_clean\", display_name=\"Tickets (Clean)\", method=\"build_tickets_clean\"),\n        Output(name=\"etl_summary\", display_name=\"ETL Summary\", method=\"build_etl_summary\"),\n    ]\n\n    # -------------------------\n    # Helpers\n    # -------------------------\n\n    def _normalize_cols(self, df: pd.DataFrame) -> pd.DataFrame:\n        df = df.copy()\n        df.columns = [str(c).strip() for c in df.columns]\n        return df\n\n    def _to_datetime(self, s: pd.Series) -> pd.Series:\n        return pd.to_datetime(s, errors=\"coerce\")\n\n    def _safe_df(self, maybe_df) -> Optional[pd.DataFrame]:\n        if maybe_df is None:\n            return None\n        # Langflow DataFrame ports are pandas-compatible; wrap safely\n        return self._normalize_cols(pd.DataFrame(maybe_df))\n\n    # -------------------------\n    # Outputs\n    # -------------------------\n\n    def build_tickets_clean(self) -> DataFrame:\n        df = self._safe_df(self.tickets_df)\n        if df is None:\n            return DataFrame(pd.DataFrame())\n\n        tid = self.ticket_id_col\n        thours = self.ticket_total_hours_col\n\n        # Parse dates\n        if self.entered_col in df.columns:\n            df[self.entered_col] = self._to_datetime(df[self.entered_col])\n        if self.closed_col in df.columns:\n            df[self.closed_col] = self._to_datetime(df[self.closed_col])\n\n        # Normalize ticket id to string (helps joins and sorting)\n        if tid in df.columns:\n            df[tid] = df[tid].astype(str).str.strip()\n\n        # Numeric hours (optional)\n        if thours in df.columns:\n            df[thours] = pd.to_numeric(df[thours], errors=\"coerce\")\n\n        # Optional filter: drop tickets without hours (based on ticket export total hours)\n        if self.drop_no_hours and (thours in df.columns):\n            df = df[pd.to_numeric(df[thours], errors=\"coerce\").fillna(0) > 0].copy()\n\n        return DataFrame(df)\n\n    def build_etl_summary(self) -> Message:\n        tickets_df = pd.DataFrame(self.build_tickets_clean())\n\n        summary: Dict[str, Any] = {\n            \"tickets_rows\": int(len(tickets_df)),\n            \"columns\": list(tickets_df.columns),\n        }\n\n        if self.entered_col in tickets_df.columns:\n            summary[\"entered_min\"] = str(tickets_df[self.entered_col].min())\n            summary[\"entered_max\"] = str(tickets_df[self.entered_col].max())\n\n        if \"Company\" in tickets_df.columns:\n            summary[\"unique_companies\"] = int(tickets_df[\"Company\"].nunique())\n\n        if self.ticket_total_hours_col in tickets_df.columns:\n            hrs = pd.to_numeric(tickets_df[self.ticket_total_hours_col], errors=\"coerce\")\n            summary[\"tickets_with_hours_nonzero\"] = int((hrs.fillna(0) > 0).sum())\n\n        return Message(text=str(summary))"
              },
              "drop_no_hours": {
                "_input_type": "BoolInput",
                "advanced": false,
                "display_name": "Drop tickets with no hours (uses Ticket Total Hours)",
                "dynamic": false,
                "info": "",
                "list": false,
                "list_add_label": "Add More",
                "name": "drop_no_hours",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "bool",
                "value": false
              },
              "entered_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Ticket Entered date column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "entered_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Entered"
              },
              "ticket_id_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Ticket ID column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "ticket_id_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Ticket #"
              },
              "ticket_total_hours_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Ticket Total Hours column (optional)",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "ticket_total_hours_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Total Hours"
              },
              "tickets_df": {
                "_input_type": "DataFrameInput",
                "advanced": false,
                "display_name": "Tickets DataFrame",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "DataFrame"
                ],
                "list": false,
                "list_add_label": "Add More",
                "name": "tickets_df",
                "placeholder": "",
                "required": true,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "other",
                "value": ""
              }
            },
            "tool_mode": false
          },
          "selected_output": "tickets_clean",
          "showNode": true,
          "type": "msp_ticket_etl"
        },
        "dragging": false,
        "id": "msp_ticket_etl-vH1pD",
        "measured": {
          "height": 551,
          "width": 320
        },
        "position": {
          "x": 1541.3783701198959,
          "y": 159.12941079633822
        },
        "selected": false,
        "type": "genericNode"
      },
      {
        "data": {
          "id": "msp_metrics_builder-QvFjh",
          "node": {
            "base_classes": [
              "DataFrame",
              "Message"
            ],
            "beta": false,
            "conditional_paths": [],
            "custom_fields": {},
            "description": "Ticket KPIs plus client-level insights, automation potential, and after-hours patterns.",
            "display_name": "MSP Metrics Builder (Tickets)",
            "documentation": "",
            "edited": true,
            "field_order": [
              "tickets_df",
              "company_col",
              "ticket_id_col",
              "entered_col",
              "closed_col",
              "board_col",
              "type_col",
              "subtype_col",
              "source_col",
              "priority_col",
              "summary_col",
              "timezone_name",
              "business_hour_start",
              "business_hour_end",
              "top_n",
              "ai_addressable_assumptions",
              "timestamps_are_local",
              "show_debug"
            ],
            "frozen": false,
            "icon": "BarChart3",
            "legacy": false,
            "lf_version": "1.6.0",
            "metadata": {},
            "minimized": false,
            "output_types": [],
            "outputs": [
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "KPI Overview",
                "group_outputs": false,
                "hidden": null,
                "method": "build_kpi_overview",
                "name": "kpi_overview",
                "options": null,
                "required_inputs": null,
                "selected": "DataFrame",
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Tickets by Month",
                "group_outputs": false,
                "hidden": null,
                "method": "build_tickets_by_month",
                "name": "tickets_by_month",
                "options": null,
                "required_inputs": null,
                "selected": "DataFrame",
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Tickets by Company",
                "group_outputs": false,
                "hidden": null,
                "method": "build_tickets_by_company",
                "name": "tickets_by_company",
                "options": null,
                "required_inputs": null,
                "selected": "DataFrame",
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Tickets by Board",
                "group_outputs": false,
                "hidden": null,
                "method": "build_tickets_by_board",
                "name": "tickets_by_board",
                "options": null,
                "required_inputs": null,
                "selected": "DataFrame",
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Tickets by Source",
                "group_outputs": false,
                "hidden": null,
                "method": "build_tickets_by_source",
                "name": "tickets_by_source",
                "options": null,
                "required_inputs": null,
                "selected": "DataFrame",
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Tickets by Subtype",
                "group_outputs": false,
                "hidden": null,
                "method": "build_tickets_by_subtype",
                "name": "tickets_by_subtype",
                "options": null,
                "required_inputs": null,
                "selected": "DataFrame",
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Client Metrics (All Clients)",
                "group_outputs": false,
                "hidden": null,
                "method": "build_client_metrics_all",
                "name": "client_metrics_all",
                "options": null,
                "required_inputs": null,
                "selected": "DataFrame",
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Neediness Index (Top Clients)",
                "group_outputs": false,
                "hidden": null,
                "method": "build_neediness_index",
                "name": "neediness_index",
                "options": null,
                "required_inputs": null,
                "selected": "DataFrame",
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Automation Potential Breakdown",
                "group_outputs": false,
                "hidden": null,
                "method": "build_automation_potential",
                "name": "automation_potential",
                "options": null,
                "required_inputs": null,
                "selected": "DataFrame",
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "AI Addressable by Client (Top)",
                "group_outputs": false,
                "hidden": null,
                "method": "build_ai_addressable_by_company",
                "name": "ai_addressable_by_company",
                "options": null,
                "required_inputs": null,
                "selected": "DataFrame",
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Insights Context (for LLM)",
                "group_outputs": false,
                "hidden": null,
                "method": "build_insights_context",
                "name": "insights_context",
                "options": null,
                "required_inputs": null,
                "selected": "Message",
                "tool_mode": true,
                "types": [
                  "Message"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Data Diagnostics",
                "group_outputs": false,
                "hidden": null,
                "method": "build_data_diagnostics",
                "name": "data_diagnostics",
                "options": null,
                "required_inputs": null,
                "selected": "Message",
                "tool_mode": true,
                "types": [
                  "Message"
                ],
                "value": "__UNDEFINED__"
              }
            ],
            "pinned": false,
            "template": {
              "_type": "Component",
              "ai_addressable_assumptions": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "AI addressable assumptions (High,Medium,Low)",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "ai_addressable_assumptions",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "High=1.0,Medium=0.4,Low=0.0"
              },
              "board_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Tickets: Board column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "board_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Board"
              },
              "business_hour_end": {
                "_input_type": "IntInput",
                "advanced": false,
                "display_name": "Business hour end (0-23)",
                "dynamic": false,
                "info": "",
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "business_hour_end",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "int",
                "value": 19
              },
              "business_hour_start": {
                "_input_type": "IntInput",
                "advanced": false,
                "display_name": "Business hour start (0-23)",
                "dynamic": false,
                "info": "",
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "business_hour_start",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "int",
                "value": 7
              },
              "closed_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Tickets: Closed datetime column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "closed_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Closed On"
              },
              "code": {
                "advanced": true,
                "dynamic": true,
                "fileTypes": [],
                "file_path": "",
                "info": "",
                "list": false,
                "load_from_db": false,
                "multiline": true,
                "name": "code",
                "password": false,
                "placeholder": "",
                "required": true,
                "show": true,
                "title_case": false,
                "type": "code",
                "value": "from __future__ import annotations\n\nfrom typing import List, Dict, Any\nimport pandas as pd\nimport numpy as np\n\nfrom langflow.custom import Component\nfrom langflow.io import DataFrameInput, MessageTextInput, BoolInput, IntInput, Output\nfrom langflow.schema import DataFrame\nfrom langflow.schema.message import Message\n\n\nclass MSPMetricsBuilder(Component):\n    display_name = \"MSP Metrics Builder (Tickets)\"\n    description = \"Ticket KPIs plus client-level insights, automation potential, and after-hours patterns.\"\n    name = \"msp_metrics_builder\"\n    icon = \"BarChart3\"\n\n    inputs = [\n        DataFrameInput(name=\"tickets_df\", display_name=\"Tickets (Clean) DataFrame\", required=True),\n\n        # Tickets mappings\n        MessageTextInput(name=\"company_col\", display_name=\"Tickets: Company column\", value=\"Company\"),\n        MessageTextInput(name=\"ticket_id_col\", display_name=\"Tickets: Ticket ID column\", value=\"Ticket #\"),\n        MessageTextInput(name=\"entered_col\", display_name=\"Tickets: Entered datetime column\", value=\"Entered\"),\n        MessageTextInput(name=\"closed_col\", display_name=\"Tickets: Closed datetime column\", value=\"Closed On\"),\n        MessageTextInput(name=\"board_col\", display_name=\"Tickets: Board column\", value=\"Board\"),\n        MessageTextInput(name=\"type_col\", display_name=\"Tickets: Type column\", value=\"Type\"),\n        MessageTextInput(name=\"subtype_col\", display_name=\"Tickets: SubType column\", value=\"SubType\"),\n        MessageTextInput(name=\"source_col\", display_name=\"Tickets: Source column\", value=\"Source\"),\n        MessageTextInput(name=\"priority_col\", display_name=\"Tickets: Priority column\", value=\"Priority\"),\n        MessageTextInput(name=\"summary_col\", display_name=\"Tickets: Summary/Subject column (optional)\", value=\"Summary\"),\n\n        # Business hours assumptions\n        MessageTextInput(name=\"timezone_name\", display_name=\"Timezone name\", value=\"America/Los_Angeles\"),\n        IntInput(name=\"business_hour_start\", display_name=\"Business hour start (0-23)\", value=9),\n        IntInput(name=\"business_hour_end\", display_name=\"Business hour end (0-23)\", value=17),\n\n        IntInput(name=\"top_n\", display_name=\"Top N rows for rankings\", value=15),\n\n        MessageTextInput(\n            name=\"ai_addressable_assumptions\",\n            display_name=\"AI addressable assumptions (High,Medium,Low)\",\n            value=\"High=1.0,Medium=0.4,Low=0.0\",\n        ),\n\n        BoolInput(name=\"timestamps_are_local\", display_name=\"Ticket timestamps are already local time\", value=True),\n        BoolInput(name=\"show_debug\", display_name=\"Include debug details in insights output\", value=False),\n    ]\n\n    outputs = [\n        Output(name=\"kpi_overview\", display_name=\"KPI Overview\", method=\"build_kpi_overview\"),\n        Output(name=\"tickets_by_month\", display_name=\"Tickets by Month\", method=\"build_tickets_by_month\"),\n        Output(name=\"tickets_by_company\", display_name=\"Tickets by Company\", method=\"build_tickets_by_company\"),\n        Output(name=\"tickets_by_board\", display_name=\"Tickets by Board\", method=\"build_tickets_by_board\"),\n        Output(name=\"tickets_by_source\", display_name=\"Tickets by Source\", method=\"build_tickets_by_source\"),\n        Output(name=\"tickets_by_subtype\", display_name=\"Tickets by Subtype\", method=\"build_tickets_by_subtype\"),\n\n        Output(name=\"client_metrics_all\", display_name=\"Client Metrics (All Clients)\", method=\"build_client_metrics_all\"),\n        Output(name=\"neediness_index\", display_name=\"Neediness Index (Top Clients)\", method=\"build_neediness_index\"),\n\n        Output(name=\"automation_potential\", display_name=\"Automation Potential Breakdown\", method=\"build_automation_potential\"),\n        Output(name=\"ai_addressable_by_company\", display_name=\"AI Addressable by Client (Top)\", method=\"build_ai_addressable_by_company\"),\n\n        Output(name=\"insights_context\", display_name=\"Insights Context (for LLM)\", method=\"build_insights_context\"),\n        Output(name=\"data_diagnostics\", display_name=\"Data Diagnostics\", method=\"build_data_diagnostics\"),\n    ]\n\n    # -------------------------\n    # Core helpers\n    # -------------------------\n\n    def _safe_col(self, df: pd.DataFrame, col: str) -> bool:\n        return col is not None and str(col).strip() != \"\" and col in df.columns\n\n    def _unwrap_to_df(self, obj: Any) -> pd.DataFrame:\n        \"\"\"Normalizes Langflow DataFrame-like inputs to pandas DataFrame.\"\"\"\n        if obj is None:\n            return pd.DataFrame()\n\n        if isinstance(obj, pd.DataFrame):\n            df = obj.copy()\n            df.columns = [str(c).replace(\"\\ufeff\", \"\").strip() for c in df.columns]\n            return df\n\n        if hasattr(obj, \"data\"):\n            d = getattr(obj, \"data\")\n            if isinstance(d, pd.DataFrame):\n                df = d.copy()\n                df.columns = [str(c).replace(\"\\ufeff\", \"\").strip() for c in df.columns]\n                return df\n            if isinstance(d, list):\n                df = pd.DataFrame(d)\n                df.columns = [str(c).replace(\"\\ufeff\", \"\").strip() for c in df.columns]\n                return df\n            if isinstance(d, dict):\n                for k in [\"data\", \"rows\", \"records\", \"items\"]:\n                    if k in d and isinstance(d[k], list):\n                        df = pd.DataFrame(d[k])\n                        df.columns = [str(c).replace(\"\\ufeff\", \"\").strip() for c in df.columns]\n                        return df\n\n        if isinstance(obj, dict):\n            for k in [\"data\", \"rows\", \"records\", \"items\"]:\n                if k in obj and isinstance(obj[k], list):\n                    df = pd.DataFrame(obj[k])\n                    df.columns = [str(c).replace(\"\\ufeff\", \"\").strip() for c in df.columns]\n                    return df\n            df = pd.DataFrame(obj)\n            df.columns = [str(c).replace(\"\\ufeff\", \"\").strip() for c in df.columns]\n            return df\n\n        if isinstance(obj, list):\n            df = pd.DataFrame(obj)\n            df.columns = [str(c).replace(\"\\ufeff\", \"\").strip() for c in df.columns]\n            return df\n\n        df = pd.DataFrame(obj)\n        df.columns = [str(c).replace(\"\\ufeff\", \"\").strip() for c in df.columns]\n        return df\n\n    def _to_dt(self, s: pd.Series) -> pd.Series:\n        return pd.to_datetime(s, errors=\"coerce\")\n\n    def _company_key(self, s: pd.Series) -> pd.Series:\n        x = s.fillna(\"\").astype(str).str.lower().str.strip()\n        x = x.str.replace(r\"[^\\w\\s]\", \" \", regex=True)\n        x = x.str.replace(r\"\\b(inc|incorporated|llc|l\\.l\\.c|ltd|corp|corporation|co|company)\\b\", \" \", regex=True)\n        x = x.str.replace(r\"\\s+\", \" \", regex=True).str.strip()\n        return x\n\n    def _parse_ai_assumptions(self) -> Dict[str, float]:\n        out = {\"High\": 1.0, \"Medium\": 0.4, \"Low\": 0.0}\n        try:\n            parts = [p.strip() for p in self.ai_addressable_assumptions.split(\",\")]\n            for p in parts:\n                k, v = [x.strip() for x in p.split(\"=\")]\n                out[k] = float(v)\n        except Exception:\n            pass\n        return out\n\n    def _minmax(self, s: pd.Series) -> pd.Series:\n        s = pd.to_numeric(s, errors=\"coerce\")\n        if s.notna().sum() == 0:\n            return pd.Series([0.0] * len(s), index=s.index)\n        mn = float(s.min())\n        mx = float(s.max())\n        if mn == mx:\n            return pd.Series([0.0] * len(s), index=s.index)\n        return (s - mn) / (mx - mn)\n\n    # -------------------------\n    # Data prep\n    # -------------------------\n\n    def _df_tickets(self) -> pd.DataFrame:\n        return self._unwrap_to_df(self.tickets_df)\n\n    def _prep_tickets(self) -> pd.DataFrame:\n        df = self._df_tickets().copy()\n        if df.empty:\n            return df\n\n        if self._safe_col(df, self.entered_col):\n            df[self.entered_col] = self._to_dt(df[self.entered_col])\n        if self._safe_col(df, self.closed_col):\n            df[self.closed_col] = self._to_dt(df[self.closed_col])\n\n        if self._safe_col(df, self.entered_col) and self._safe_col(df, self.closed_col):\n            df[\"resolution_hours\"] = (df[self.closed_col] - df[self.entered_col]).dt.total_seconds() / 3600.0\n        else:\n            df[\"resolution_hours\"] = np.nan\n\n        if self._safe_col(df, self.entered_col):\n            entered = df[self.entered_col]\n            df[\"entered_month\"] = entered.dt.to_period(\"M\").astype(str)\n            df[\"entered_dow\"] = entered.dt.day_name()\n            df[\"entered_hour\"] = entered.dt.hour\n\n            start_h = int(self.business_hour_start)\n            end_h = int(self.business_hour_end)\n            is_weekend = entered.dt.dayofweek >= 5\n            df[\"after_hours\"] = ((entered.dt.hour < start_h) | (entered.dt.hour >= end_h) | is_weekend).astype(int)\n        else:\n            df[\"entered_month\"] = None\n            df[\"entered_dow\"] = None\n            df[\"entered_hour\"] = None\n            df[\"after_hours\"] = np.nan\n\n        df[\"is_closed\"] = 0\n        if self._safe_col(df, self.closed_col):\n            df.loc[df[self.closed_col].notna(), \"is_closed\"] = 1\n\n        if self._safe_col(df, self.company_col):\n            df[\"company\"] = df[self.company_col].astype(str).str.strip()\n        else:\n            df[\"company\"] = \"(unknown)\"\n\n        df[\"company_key\"] = self._company_key(df[\"company\"])\n\n        if self._safe_col(df, self.ticket_id_col):\n            df[\"ticket_id\"] = df[self.ticket_id_col].astype(str).str.strip()\n        else:\n            df[\"ticket_id\"] = np.nan\n\n        return self._automation_label(df)\n\n    # -------------------------\n    # Automation label\n    # -------------------------\n\n    def _automation_label(self, df: pd.DataFrame) -> pd.DataFrame:\n        high_kw = [\n            \"password\", \"reset\", \"unlock\", \"mfa\", \"duo\", \"okta\",\n            \"permission\", \"permissions\", \"access\", \"group\", \"mailbox\", \"license\",\n            \"install\", \"uninstall\", \"software\", \"application\", \"printer\",\n            \"onboard\", \"offboard\", \"new user\", \"account\", \"user\",\n            \"alert\", \"monitor\", \"patch\", \"inventory\", \"script\", \"agent\",\n        ]\n        low_kw = [\n            \"outage\", \"incident\", \"ransomware\", \"breach\", \"compromise\",\n            \"firewall\", \"routing\", \"switch\", \"vlan\",\n            \"migration\", \"project\", \"disaster recovery\", \"failover\",\n            \"ediscovery\", \"legal hold\",\n        ]\n        medium_kw = [\n            \"troubleshoot\", \"intermittent\", \"performance\", \"slow\", \"crash\",\n            \"vpn\", \"wifi\", \"dns\", \"backup\", \"restore\",\n        ]\n\n        def classify(text: str) -> str:\n            t = (text or \"\").lower()\n            for k in low_kw:\n                if k in t:\n                    return \"Low\"\n            for k in high_kw:\n                if k in t:\n                    return \"High\"\n            for k in medium_kw:\n                if k in t:\n                    return \"Medium\"\n            return \"Medium\"\n\n        blob_cols = [\n            c for c in [self.board_col, self.type_col, self.subtype_col, self.source_col, self.summary_col]\n            if c in df.columns\n        ]\n        if not blob_cols:\n            df[\"automation_potential\"] = \"Medium\"\n            return df\n\n        blob = df[blob_cols].astype(str).replace(\"nan\", \"\", regex=False).agg(\" \".join, axis=1)\n        df[\"automation_potential\"] = blob.apply(classify)\n        return df\n\n    # -------------------------\n    # Small helpers for outputs\n    # -------------------------\n\n    def _top_counts(self, df: pd.DataFrame, col: str, top_n: int) -> pd.DataFrame:\n        if not self._safe_col(df, col):\n            return pd.DataFrame(columns=[col, \"tickets\", \"share\"])\n        vc = df[col].fillna(\"(blank)\").astype(str).value_counts(dropna=False).head(top_n)\n        out = vc.rename(\"tickets\").reset_index().rename(columns={\"index\": col})\n        out[\"share\"] = out[\"tickets\"] / max(len(df), 1)\n        return out\n\n    def _resolution_stats(self, df: pd.DataFrame) -> Dict[str, float]:\n        x = df.loc[df[\"resolution_hours\"].notna() & (df[\"resolution_hours\"] >= 0), \"resolution_hours\"]\n        if len(x) == 0:\n            return {\"median\": np.nan, \"p75\": np.nan, \"p90\": np.nan, \"p95\": np.nan}\n        return {\n            \"median\": float(np.nanmedian(x)),\n            \"p75\": float(np.nanpercentile(x, 75)),\n            \"p90\": float(np.nanpercentile(x, 90)),\n            \"p95\": float(np.nanpercentile(x, 95)),\n        }\n\n    # -------------------------\n    # KPI outputs\n    # -------------------------\n\n    def build_kpi_overview(self) -> DataFrame:\n        tickets = self._prep_tickets()\n        total = int(len(tickets))\n        closed = int(tickets[\"is_closed\"].sum()) if \"is_closed\" in tickets.columns else 0\n        closed_rate = closed / max(total, 1)\n\n        res = self._resolution_stats(tickets[tickets[\"is_closed\"] == 1]) if \"is_closed\" in tickets.columns else {\n            \"median\": np.nan, \"p75\": np.nan, \"p90\": np.nan, \"p95\": np.nan\n        }\n        after_hours_pct = float(tickets[\"after_hours\"].mean()) if \"after_hours\" in tickets.columns and tickets[\"after_hours\"].notna().any() else np.nan\n\n        out = pd.DataFrame([{\n            \"tickets_total\": total,\n            \"tickets_closed\": closed,\n            \"closed_rate\": closed_rate,\n            \"resolution_hours_median\": res[\"median\"],\n            \"resolution_hours_p75\": res[\"p75\"],\n            \"resolution_hours_p90\": res[\"p90\"],\n            \"resolution_hours_p95\": res[\"p95\"],\n            \"after_hours_pct\": after_hours_pct,\n            \"unique_companies\": int(tickets[\"company_key\"].nunique()) if \"company_key\" in tickets.columns else np.nan,\n        }])\n        return DataFrame(out)\n\n    def build_tickets_by_month(self) -> DataFrame:\n        tickets = self._prep_tickets()\n        if tickets.empty or \"entered_month\" not in tickets.columns or tickets[\"entered_month\"].isna().all():\n            return DataFrame(pd.DataFrame(columns=[\"entered_month\", \"tickets\"]))\n        out = tickets.groupby(\"entered_month\").size().rename(\"tickets\").reset_index().sort_values(\"entered_month\")\n        return DataFrame(out)\n\n    def build_tickets_by_company(self) -> DataFrame:\n        tickets = self._prep_tickets()\n        if tickets.empty:\n            return DataFrame(pd.DataFrame(columns=[\"company\", \"tickets\", \"share\"]))\n        g = tickets.groupby(\"company\", dropna=False).size().rename(\"tickets\").reset_index()\n        g[\"share\"] = g[\"tickets\"] / max(len(tickets), 1)\n        return DataFrame(g.sort_values(\"tickets\", ascending=False).head(int(self.top_n)))\n\n    def build_tickets_by_board(self) -> DataFrame:\n        tickets = self._prep_tickets()\n        return DataFrame(self._top_counts(tickets, self.board_col, int(self.top_n)))\n\n    def build_tickets_by_source(self) -> DataFrame:\n        tickets = self._prep_tickets()\n        return DataFrame(self._top_counts(tickets, self.source_col, int(self.top_n)))\n\n    def build_tickets_by_subtype(self) -> DataFrame:\n        tickets = self._prep_tickets()\n        return DataFrame(self._top_counts(tickets, self.subtype_col, int(self.top_n)))\n\n    # -------------------------\n    # Client metrics\n    # -------------------------\n\n    def _client_metrics_pandas(self) -> pd.DataFrame:\n        tickets = self._prep_tickets()\n        if tickets.empty:\n            return pd.DataFrame()\n\n        assumptions = self._parse_ai_assumptions()\n        tickets[\"addressable_fraction\"] = tickets[\"automation_potential\"].map(assumptions).fillna(0.0)\n\n        g = tickets.groupby(\"company_key\", dropna=False)\n        base = pd.DataFrame({\n            \"company_key\": g.size().index.astype(str),\n            \"tickets\": g.size().values,\n            \"after_hours_pct\": g[\"after_hours\"].mean(numeric_only=True).values,\n            \"resolution_hours_median\": g[\"resolution_hours\"].median(numeric_only=True).values,\n            \"closed_rate\": g[\"is_closed\"].mean(numeric_only=True).values,\n            \"ai_addressable_ticket_equiv\": g[\"addressable_fraction\"].sum().values,\n        })\n        base[\"ai_addressable_share_est\"] = base[\"ai_addressable_ticket_equiv\"] / base[\"tickets\"].replace(0, np.nan)\n\n        name_map = (\n            tickets.groupby(\"company_key\")[\"company\"]\n            .agg(lambda s: s.value_counts().index[0] if len(s.dropna()) else \"(unknown)\")\n            .rename(\"company\")\n            .reset_index()\n        )\n        base = base.merge(name_map, on=\"company_key\", how=\"left\")\n\n        if \"entered_month\" in tickets.columns and tickets[\"entered_month\"].notna().any():\n            months_active = tickets.groupby(\"company_key\")[\"entered_month\"].nunique().rename(\"months_active\").reset_index()\n            base = base.merge(months_active, on=\"company_key\", how=\"left\")\n            base[\"tickets_per_active_month\"] = base[\"tickets\"] / base[\"months_active\"].replace(0, np.nan)\n        else:\n            base[\"months_active\"] = np.nan\n            base[\"tickets_per_active_month\"] = np.nan\n\n        # Neediness index (ticket-only)\n        w_volume, w_intensity, w_after, w_res = 0.40, 0.30, 0.15, 0.15\n        base[\"n_tickets\"] = self._minmax(base[\"tickets\"].fillna(0))\n        base[\"n_intensity\"] = self._minmax(base[\"tickets_per_active_month\"].fillna(0))\n        base[\"n_after\"] = self._minmax(base[\"after_hours_pct\"].fillna(0))\n        base[\"n_res\"] = self._minmax(base[\"resolution_hours_median\"].fillna(0))\n\n        base[\"neediness_index\"] = (\n            w_volume * base[\"n_tickets\"]\n            + w_intensity * base[\"n_intensity\"]\n            + w_after * base[\"n_after\"]\n            + w_res * base[\"n_res\"]\n        )\n\n        return base\n\n    def build_client_metrics_all(self) -> DataFrame:\n        df = self._client_metrics_pandas()\n        if df.empty:\n            return DataFrame(pd.DataFrame())\n        return DataFrame(df.sort_values(\"tickets\", ascending=False))\n\n    def build_neediness_index(self) -> DataFrame:\n        df = self._client_metrics_pandas()\n        if df.empty:\n            return DataFrame(pd.DataFrame())\n        cols = [\n            \"company\", \"neediness_index\", \"tickets\",\n            \"after_hours_pct\", \"resolution_hours_median\", \"closed_rate\",\n            \"months_active\", \"tickets_per_active_month\",\n        ]\n        out = df[cols].sort_values(\"neediness_index\", ascending=False).head(int(self.top_n))\n        return DataFrame(out)\n\n    # -------------------------\n    # Automation and AI addressable outputs\n    # -------------------------\n\n    def build_automation_potential(self) -> DataFrame:\n        tickets = self._prep_tickets()\n        if tickets.empty or \"automation_potential\" not in tickets.columns:\n            return DataFrame(pd.DataFrame(columns=[\n                \"automation_potential\", \"tickets\", \"share\", \"addressable_fraction\", \"addressable_tickets_equiv\"\n            ]))\n\n        total = max(len(tickets), 1)\n        out = tickets[\"automation_potential\"].value_counts().rename(\"tickets\").reset_index().rename(\n            columns={\"index\": \"automation_potential\"}\n        )\n        out[\"share\"] = out[\"tickets\"] / total\n\n        assumptions = self._parse_ai_assumptions()\n        out[\"addressable_fraction\"] = out[\"automation_potential\"].map(assumptions).fillna(0.0)\n        out[\"addressable_tickets_equiv\"] = out[\"tickets\"] * out[\"addressable_fraction\"]\n        return DataFrame(out)\n\n    def build_ai_addressable_by_company(self) -> DataFrame:\n        df = self._client_metrics_pandas()\n        if df.empty:\n            return DataFrame(pd.DataFrame())\n        cols = [\"company\", \"tickets\", \"ai_addressable_ticket_equiv\", \"ai_addressable_share_est\"]\n        out = df[cols].sort_values(\"ai_addressable_ticket_equiv\", ascending=False).head(int(self.top_n))\n        return DataFrame(out)\n\n    # -------------------------\n    # Insights context (rich message)\n    # -------------------------\n\n    def build_insights_context(self) -> Message:\n        kpi = pd.DataFrame(self.build_kpi_overview())\n        auto = pd.DataFrame(self.build_automation_potential())\n        neediest = pd.DataFrame(self.build_neediness_index())\n        ai_top = pd.DataFrame(self.build_ai_addressable_by_company())\n\n        def fmt_pct(x) -> str:\n            return \"n/a\" if pd.isna(x) else f\"{float(x) * 100:.1f}%\"\n\n        def fmt_num(x) -> str:\n            return \"n/a\" if pd.isna(x) else f\"{float(x):,.2f}\"\n\n        def bullets(df: pd.DataFrame, cols: List[str], n: int = 8) -> str:\n            if df is None or df.empty:\n                return \"  (no data)\\n\"\n            lines = []\n            for _, r in df.head(n).iterrows():\n                parts = []\n                for c in cols:\n                    if c not in df.columns:\n                        continue\n                    v = r[c]\n                    if c.endswith(\"_pct\") or c.endswith(\"_rate\") or c.endswith(\"_share_est\") or c == \"share\":\n                        parts.append(f\"{c}={fmt_pct(v)}\")\n                    elif \"hours\" in c:\n                        parts.append(f\"{c}={fmt_num(v)}\")\n                    elif \"tickets\" in c or \"equiv\" in c or \"index\" in c:\n                        parts.append(f\"{c}={fmt_num(v)}\" if isinstance(v, (float, np.floating)) else f\"{c}={v}\")\n                    else:\n                        parts.append(f\"{c}={v}\")\n                lines.append(\"  - \" + \", \".join(parts))\n            return \"\\n\".join(lines) + \"\\n\"\n\n        lines: List[str] = []\n        lines.append(\"DATASET OVERVIEW\")\n        if not kpi.empty:\n            row = kpi.iloc[0].to_dict()\n            lines.append(f\"tickets_total={row.get('tickets_total')}\")\n            lines.append(f\"tickets_closed={row.get('tickets_closed')} (closed_rate={fmt_pct(row.get('closed_rate'))})\")\n            lines.append(\n                \"resolution_hours: \"\n                f\"median={fmt_num(row.get('resolution_hours_median'))}, \"\n                f\"p75={fmt_num(row.get('resolution_hours_p75'))}, \"\n                f\"p90={fmt_num(row.get('resolution_hours_p90'))}, \"\n                f\"p95={fmt_num(row.get('resolution_hours_p95'))}\"\n            )\n            lines.append(f\"after_hours_pct={fmt_pct(row.get('after_hours_pct'))}\")\n            lines.append(f\"unique_companies={row.get('unique_companies')}\")\n        lines.append(\"\")\n\n        lines.append(\"AUTOMATION POTENTIAL (overall)\")\n        lines.append(bullets(auto, [\"automation_potential\", \"tickets\", \"share\", \"addressable_fraction\", \"addressable_tickets_equiv\"], 3))\n\n        lines.append(\"NEEDIEST CLIENTS (top)\")\n        lines.append(bullets(neediest, [\"company\", \"neediness_index\", \"tickets\", \"after_hours_pct\", \"resolution_hours_median\", \"closed_rate\"], int(self.top_n)))\n\n        lines.append(\"AI ADDRESSABLE CLIENTS (top)\")\n        lines.append(bullets(ai_top, [\"company\", \"ai_addressable_ticket_equiv\", \"ai_addressable_share_est\", \"tickets\"], int(self.top_n)))\n\n        if bool(self.show_debug):\n            diag = self.build_data_diagnostics().text\n            lines.append(\"\")\n            lines.append(diag)\n\n        return Message(text=\"\\n\".join(lines))\n\n    # -------------------------\n    # Diagnostics\n    # -------------------------\n\n    def build_data_diagnostics(self) -> Message:\n        t = self._df_tickets()\n\n        lines: List[str] = []\n        lines.append(\"DATA DIAGNOSTICS\")\n        lines.append(\n            f\"tickets_rows={len(t)} cols={len(t.columns)} \"\n            f\"company_col_present={self.company_col in t.columns} \"\n            f\"entered_col_present={self.entered_col in t.columns} \"\n            f\"closed_col_present={self.closed_col in t.columns}\"\n        )\n        lines.append(\"tickets_columns_sample=\" + str(list(t.columns)[:40]))\n        return Message(text=\"\\n\".join(lines))"
              },
              "company_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Tickets: Company column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "company_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Company"
              },
              "entered_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Tickets: Entered datetime column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "entered_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Entered"
              },
              "priority_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Tickets: Priority column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "priority_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Priority"
              },
              "show_debug": {
                "_input_type": "BoolInput",
                "advanced": false,
                "display_name": "Include debug details in insights output",
                "dynamic": false,
                "info": "",
                "list": false,
                "list_add_label": "Add More",
                "name": "show_debug",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "bool",
                "value": false
              },
              "source_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Tickets: Source column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "source_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Source"
              },
              "subtype_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Tickets: SubType column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "subtype_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "SubType"
              },
              "summary_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Tickets: Summary/Subject column (optional)",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "summary_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Summary"
              },
              "ticket_id_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Tickets: Ticket ID column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "ticket_id_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Ticket #"
              },
              "tickets_df": {
                "_input_type": "DataFrameInput",
                "advanced": false,
                "display_name": "Tickets (Clean) DataFrame",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "DataFrame"
                ],
                "list": false,
                "list_add_label": "Add More",
                "name": "tickets_df",
                "placeholder": "",
                "required": true,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "other",
                "value": ""
              },
              "timestamps_are_local": {
                "_input_type": "BoolInput",
                "advanced": false,
                "display_name": "Ticket timestamps are already local time",
                "dynamic": false,
                "info": "",
                "list": false,
                "list_add_label": "Add More",
                "name": "timestamps_are_local",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "bool",
                "value": true
              },
              "timezone_name": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Timezone name",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "timezone_name",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "America/Los_Angeles"
              },
              "top_n": {
                "_input_type": "IntInput",
                "advanced": false,
                "display_name": "Top N rows for rankings",
                "dynamic": false,
                "info": "",
                "list": false,
                "list_add_label": "Add More",
                "name": "top_n",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "int",
                "value": 15
              },
              "type_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Tickets: Type column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "type_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Type"
              }
            },
            "tool_mode": false
          },
          "selected_output": "insights_context",
          "showNode": true,
          "type": "msp_metrics_builder"
        },
        "dragging": false,
        "id": "msp_metrics_builder-QvFjh",
        "measured": {
          "height": 1495,
          "width": 320
        },
        "position": {
          "x": 2263.70790373339,
          "y": 118.6085202865471
        },
        "selected": false,
        "type": "genericNode"
      },
      {
        "data": {
          "id": "Prompt Template-KjO8V",
          "node": {
            "base_classes": [
              "Message"
            ],
            "beta": false,
            "conditional_paths": [],
            "custom_fields": {
              "template": [
                "insights"
              ]
            },
            "description": "Create a prompt template with dynamic variables.",
            "display_name": "Prompt Template",
            "documentation": "https://docs.langflow.org/components-prompts",
            "edited": false,
            "error": null,
            "field_order": [
              "template",
              "tool_placeholder"
            ],
            "frozen": false,
            "full_path": null,
            "icon": "braces",
            "is_composition": null,
            "is_input": null,
            "is_output": null,
            "legacy": false,
            "lf_version": "1.6.0",
            "metadata": {},
            "minimized": false,
            "name": "",
            "output_types": [],
            "outputs": [
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Prompt",
                "group_outputs": false,
                "hidden": null,
                "method": "build_prompt",
                "name": "prompt",
                "options": null,
                "required_inputs": null,
                "selected": "Message",
                "tool_mode": true,
                "types": [
                  "Message"
                ],
                "value": "__UNDEFINED__"
              }
            ],
            "pinned": false,
            "priority": 0,
            "replacement": null,
            "template": {
              "_type": "Component",
              "code": {
                "advanced": true,
                "dynamic": true,
                "fileTypes": [],
                "file_path": "",
                "info": "",
                "list": false,
                "load_from_db": false,
                "multiline": true,
                "name": "code",
                "password": false,
                "placeholder": "",
                "required": true,
                "show": true,
                "title_case": false,
                "type": "code",
                "value": "from langflow.base.prompts.api_utils import process_prompt_template\nfrom langflow.custom.custom_component.component import Component\nfrom langflow.inputs.inputs import DefaultPromptField\nfrom langflow.io import MessageTextInput, Output, PromptInput\nfrom langflow.schema.message import Message\nfrom langflow.template.utils import update_template_values\n\n\nclass PromptComponent(Component):\n    display_name: str = \"Prompt Template\"\n    description: str = \"Create a prompt template with dynamic variables.\"\n    documentation: str = \"https://docs.langflow.org/components-prompts\"\n    icon = \"braces\"\n    trace_type = \"prompt\"\n    name = \"Prompt Template\"\n    priority = 0  # Set priority to 0 to make it appear first\n\n    inputs = [\n        PromptInput(name=\"template\", display_name=\"Template\"),\n        MessageTextInput(\n            name=\"tool_placeholder\",\n            display_name=\"Tool Placeholder\",\n            tool_mode=True,\n            advanced=True,\n            info=\"A placeholder input for tool mode.\",\n        ),\n    ]\n\n    outputs = [\n        Output(display_name=\"Prompt\", name=\"prompt\", method=\"build_prompt\"),\n    ]\n\n    async def build_prompt(self) -> Message:\n        prompt = Message.from_template(**self._attributes)\n        self.status = prompt.text\n        return prompt\n\n    def _update_template(self, frontend_node: dict):\n        prompt_template = frontend_node[\"template\"][\"template\"][\"value\"]\n        custom_fields = frontend_node[\"custom_fields\"]\n        frontend_node_template = frontend_node[\"template\"]\n        _ = process_prompt_template(\n            template=prompt_template,\n            name=\"template\",\n            custom_fields=custom_fields,\n            frontend_node_template=frontend_node_template,\n        )\n        return frontend_node\n\n    async def update_frontend_node(self, new_frontend_node: dict, current_frontend_node: dict):\n        \"\"\"This function is called after the code validation is done.\"\"\"\n        frontend_node = await super().update_frontend_node(new_frontend_node, current_frontend_node)\n        template = frontend_node[\"template\"][\"template\"][\"value\"]\n        # Kept it duplicated for backwards compatibility\n        _ = process_prompt_template(\n            template=template,\n            name=\"template\",\n            custom_fields=frontend_node[\"custom_fields\"],\n            frontend_node_template=frontend_node[\"template\"],\n        )\n        # Now that template is updated, we need to grab any values that were set in the current_frontend_node\n        # and update the frontend_node with those values\n        update_template_values(new_template=frontend_node, previous_template=current_frontend_node[\"template\"])\n        return frontend_node\n\n    def _get_fallback_input(self, **kwargs):\n        return DefaultPromptField(**kwargs)\n"
              },
              "insights": {
                "advanced": false,
                "display_name": "insights",
                "dynamic": false,
                "field_type": "str",
                "fileTypes": [],
                "file_path": "",
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "load_from_db": false,
                "multiline": true,
                "name": "insights",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "type": "str",
                "value": ""
              },
              "template": {
                "_input_type": "PromptInput",
                "advanced": false,
                "display_name": "Template",
                "dynamic": false,
                "info": "",
                "list": false,
                "list_add_label": "Add More",
                "name": "template",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "type": "prompt",
                "value": "Use the following ticket metrics to produce insights:\n{insights}"
              },
              "tool_placeholder": {
                "_input_type": "MessageTextInput",
                "advanced": true,
                "display_name": "Tool Placeholder",
                "dynamic": false,
                "info": "A placeholder input for tool mode.",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "tool_placeholder",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": true,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": ""
              }
            },
            "tool_mode": false
          },
          "showNode": true,
          "type": "Prompt Template"
        },
        "dragging": false,
        "id": "Prompt Template-KjO8V",
        "measured": {
          "height": 313,
          "width": 320
        },
        "position": {
          "x": 3209.4964637723865,
          "y": 1328.0934825828815
        },
        "selected": false,
        "type": "genericNode"
      },
      {
        "data": {
          "id": "File-Gq99J",
          "node": {
            "base_classes": [
              "Message"
            ],
            "beta": false,
            "category": "data",
            "conditional_paths": [],
            "custom_fields": {},
            "description": "Loads content from one or more files.",
            "display_name": "File",
            "documentation": "https://docs.langflow.org/components-data#file",
            "edited": false,
            "field_order": [
              "path",
              "file_path",
              "separator",
              "silent_errors",
              "delete_server_file_after_processing",
              "ignore_unsupported_extensions",
              "ignore_unspecified_files",
              "advanced_mode",
              "pipeline",
              "ocr_engine",
              "md_image_placeholder",
              "md_page_break_placeholder",
              "doc_key",
              "use_multithreading",
              "concurrency_multithreading",
              "markdown"
            ],
            "frozen": false,
            "icon": "file-text",
            "key": "File",
            "last_updated": "2025-12-23T01:31:53.698Z",
            "legacy": false,
            "lf_version": "1.6.0",
            "metadata": {},
            "minimized": false,
            "output_types": [],
            "outputs": [
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Raw Content",
                "group_outputs": false,
                "method": "load_files_message",
                "name": "message",
                "options": null,
                "required_inputs": null,
                "selected": "Message",
                "tool_mode": true,
                "types": [
                  "Message"
                ],
                "value": "__UNDEFINED__"
              }
            ],
            "pinned": false,
            "score": 2.220446049250313e-16,
            "template": {
              "_type": "Component",
              "advanced_mode": {
                "_input_type": "BoolInput",
                "advanced": false,
                "display_name": "Advanced Parser",
                "dynamic": false,
                "info": "Enable advanced document processing and export with Docling for PDFs, images, and office documents. Available only for single file processing.Note that advanced document processing can consume significant resources.",
                "list": false,
                "list_add_label": "Add More",
                "name": "advanced_mode",
                "placeholder": "",
                "real_time_refresh": true,
                "required": false,
                "show": false,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "bool",
                "value": false
              },
              "code": {
                "advanced": true,
                "dynamic": true,
                "fileTypes": [],
                "file_path": "",
                "info": "",
                "list": false,
                "load_from_db": false,
                "multiline": true,
                "name": "code",
                "password": false,
                "placeholder": "",
                "required": true,
                "show": true,
                "title_case": false,
                "type": "code",
                "value": "\"\"\"Enhanced file component with Docling support and process isolation.\n\nNotes:\n-----\n- ALL Docling parsing/export runs in a separate OS process to prevent memory\n  growth and native library state from impacting the main Langflow process.\n- Standard text/structured parsing continues to use existing BaseFileComponent\n  utilities (and optional threading via `parallel_load_data`).\n\"\"\"\n\nfrom __future__ import annotations\n\nimport json\nimport subprocess\nimport sys\nimport textwrap\nfrom copy import deepcopy\nfrom typing import TYPE_CHECKING, Any\n\nfrom langflow.base.data.base_file import BaseFileComponent\nfrom langflow.base.data.utils import TEXT_FILE_TYPES, parallel_load_data, parse_text_file_to_data\nfrom langflow.io import (\n    BoolInput,\n    DropdownInput,\n    FileInput,\n    IntInput,\n    MessageTextInput,\n    Output,\n    StrInput,\n)\nfrom langflow.schema.data import Data\nfrom langflow.schema.message import Message\n\nif TYPE_CHECKING:\n    from langflow.schema import DataFrame\n\n\nclass FileComponent(BaseFileComponent):\n    \"\"\"File component with optional Docling processing (isolated in a subprocess).\"\"\"\n\n    display_name = \"File\"\n    description = \"Loads content from one or more files.\"\n    documentation: str = \"https://docs.langflow.org/components-data#file\"\n    icon = \"file-text\"\n    name = \"File\"\n\n    # Docling-supported/compatible extensions; TEXT_FILE_TYPES are supported by the base loader.\n    VALID_EXTENSIONS = [\n        *TEXT_FILE_TYPES,\n        \"adoc\",\n        \"asciidoc\",\n        \"asc\",\n        \"bmp\",\n        \"dotx\",\n        \"dotm\",\n        \"docm\",\n        \"jpeg\",\n        \"png\",\n        \"potx\",\n        \"ppsx\",\n        \"pptm\",\n        \"potm\",\n        \"ppsm\",\n        \"pptx\",\n        \"tiff\",\n        \"xls\",\n        \"xlsx\",\n        \"xhtml\",\n        \"webp\",\n    ]\n\n    # Fixed export settings used when markdown export is requested.\n    EXPORT_FORMAT = \"Markdown\"\n    IMAGE_MODE = \"placeholder\"\n\n    # ---- Inputs / Outputs (kept as close to original as possible) -------------------\n    _base_inputs = deepcopy(BaseFileComponent._base_inputs)\n    for input_item in _base_inputs:\n        if isinstance(input_item, FileInput) and input_item.name == \"path\":\n            input_item.real_time_refresh = True\n            break\n\n    inputs = [\n        *_base_inputs,\n        BoolInput(\n            name=\"advanced_mode\",\n            display_name=\"Advanced Parser\",\n            value=False,\n            real_time_refresh=True,\n            info=(\n                \"Enable advanced document processing and export with Docling for PDFs, images, and office documents. \"\n                \"Available only for single file processing.\"\n                \"Note that advanced document processing can consume significant resources.\"\n            ),\n            show=False,\n        ),\n        DropdownInput(\n            name=\"pipeline\",\n            display_name=\"Pipeline\",\n            info=\"Docling pipeline to use\",\n            options=[\"standard\", \"vlm\"],\n            value=\"standard\",\n            advanced=True,\n            real_time_refresh=True,\n        ),\n        DropdownInput(\n            name=\"ocr_engine\",\n            display_name=\"OCR Engine\",\n            info=\"OCR engine to use. Only available when pipeline is set to 'standard'.\",\n            options=[\"None\", \"easyocr\"],\n            value=\"easyocr\",\n            show=False,\n            advanced=True,\n        ),\n        StrInput(\n            name=\"md_image_placeholder\",\n            display_name=\"Image placeholder\",\n            info=\"Specify the image placeholder for markdown exports.\",\n            value=\"<!-- image -->\",\n            advanced=True,\n            show=False,\n        ),\n        StrInput(\n            name=\"md_page_break_placeholder\",\n            display_name=\"Page break placeholder\",\n            info=\"Add this placeholder between pages in the markdown output.\",\n            value=\"\",\n            advanced=True,\n            show=False,\n        ),\n        MessageTextInput(\n            name=\"doc_key\",\n            display_name=\"Doc Key\",\n            info=\"The key to use for the DoclingDocument column.\",\n            value=\"doc\",\n            advanced=True,\n            show=False,\n        ),\n        # Deprecated input retained for backward-compatibility.\n        BoolInput(\n            name=\"use_multithreading\",\n            display_name=\"[Deprecated] Use Multithreading\",\n            advanced=True,\n            value=True,\n            info=\"Set 'Processing Concurrency' greater than 1 to enable multithreading.\",\n        ),\n        IntInput(\n            name=\"concurrency_multithreading\",\n            display_name=\"Processing Concurrency\",\n            advanced=True,\n            info=\"When multiple files are being processed, the number of files to process concurrently.\",\n            value=1,\n        ),\n        BoolInput(\n            name=\"markdown\",\n            display_name=\"Markdown Export\",\n            info=\"Export processed documents to Markdown format. Only available when advanced mode is enabled.\",\n            value=False,\n            show=False,\n        ),\n    ]\n\n    outputs = [\n        Output(display_name=\"Raw Content\", name=\"message\", method=\"load_files_message\"),\n    ]\n\n    # ------------------------------ UI helpers --------------------------------------\n\n    def _path_value(self, template: dict) -> list[str]:\n        \"\"\"Return the list of currently selected file paths from the template.\"\"\"\n        return template.get(\"path\", {}).get(\"file_path\", [])\n\n    def update_build_config(\n        self,\n        build_config: dict[str, Any],\n        field_value: Any,\n        field_name: str | None = None,\n    ) -> dict[str, Any]:\n        \"\"\"Show/hide Advanced Parser and related fields based on selection context.\"\"\"\n        if field_name == \"path\":\n            paths = self._path_value(build_config)\n            file_path = paths[0] if paths else \"\"\n            file_count = len(field_value) if field_value else 0\n\n            # Advanced mode only for single (non-tabular) file\n            allow_advanced = file_count == 1 and not file_path.endswith((\".csv\", \".xlsx\", \".parquet\"))\n            build_config[\"advanced_mode\"][\"show\"] = allow_advanced\n            if not allow_advanced:\n                build_config[\"advanced_mode\"][\"value\"] = False\n                for f in (\"pipeline\", \"ocr_engine\", \"doc_key\", \"md_image_placeholder\", \"md_page_break_placeholder\"):\n                    if f in build_config:\n                        build_config[f][\"show\"] = False\n\n        # Docling Processing\n        elif field_name == \"advanced_mode\":\n            for f in (\"pipeline\", \"ocr_engine\", \"doc_key\", \"md_image_placeholder\", \"md_page_break_placeholder\"):\n                if f in build_config:\n                    build_config[f][\"show\"] = bool(field_value)\n\n        elif field_name == \"pipeline\":\n            if field_value == \"standard\":\n                build_config[\"ocr_engine\"][\"show\"] = True\n                build_config[\"ocr_engine\"][\"value\"] = \"easyocr\"\n            else:\n                build_config[\"ocr_engine\"][\"show\"] = False\n                build_config[\"ocr_engine\"][\"value\"] = \"None\"\n\n        return build_config\n\n    def update_outputs(self, frontend_node: dict[str, Any], field_name: str, field_value: Any) -> dict[str, Any]:  # noqa: ARG002\n        \"\"\"Dynamically show outputs based on file count/type and advanced mode.\"\"\"\n        if field_name not in [\"path\", \"advanced_mode\", \"pipeline\"]:\n            return frontend_node\n\n        template = frontend_node.get(\"template\", {})\n        paths = self._path_value(template)\n        if not paths:\n            return frontend_node\n\n        frontend_node[\"outputs\"] = []\n        if len(paths) == 1:\n            file_path = paths[0] if field_name == \"path\" else frontend_node[\"template\"][\"path\"][\"file_path\"][0]\n            if file_path.endswith((\".csv\", \".xlsx\", \".parquet\")):\n                frontend_node[\"outputs\"].append(\n                    Output(display_name=\"Structured Content\", name=\"dataframe\", method=\"load_files_structured\"),\n                )\n            elif file_path.endswith(\".json\"):\n                frontend_node[\"outputs\"].append(\n                    Output(display_name=\"Structured Content\", name=\"json\", method=\"load_files_json\"),\n                )\n\n            advanced_mode = frontend_node.get(\"template\", {}).get(\"advanced_mode\", {}).get(\"value\", False)\n            if advanced_mode:\n                frontend_node[\"outputs\"].append(\n                    Output(display_name=\"Structured Output\", name=\"advanced_dataframe\", method=\"load_files_dataframe\"),\n                )\n                frontend_node[\"outputs\"].append(\n                    Output(display_name=\"Markdown\", name=\"advanced_markdown\", method=\"load_files_markdown\"),\n                )\n                frontend_node[\"outputs\"].append(\n                    Output(display_name=\"File Path\", name=\"path\", method=\"load_files_path\"),\n                )\n            else:\n                frontend_node[\"outputs\"].append(\n                    Output(display_name=\"Raw Content\", name=\"message\", method=\"load_files_message\"),\n                )\n                frontend_node[\"outputs\"].append(\n                    Output(display_name=\"File Path\", name=\"path\", method=\"load_files_path\"),\n                )\n        else:\n            # Multiple files => DataFrame output; advanced parser disabled\n            frontend_node[\"outputs\"].append(Output(display_name=\"Files\", name=\"dataframe\", method=\"load_files\"))\n\n        return frontend_node\n\n    # ------------------------------ Core processing ----------------------------------\n\n    def _is_docling_compatible(self, file_path: str) -> bool:\n        \"\"\"Lightweight extension gate for Docling-compatible types.\"\"\"\n        docling_exts = (\n            \".adoc\",\n            \".asciidoc\",\n            \".asc\",\n            \".bmp\",\n            \".csv\",\n            \".dotx\",\n            \".dotm\",\n            \".docm\",\n            \".docx\",\n            \".htm\",\n            \".html\",\n            \".jpeg\",\n            \".json\",\n            \".md\",\n            \".pdf\",\n            \".png\",\n            \".potx\",\n            \".ppsx\",\n            \".pptm\",\n            \".potm\",\n            \".ppsm\",\n            \".pptx\",\n            \".tiff\",\n            \".txt\",\n            \".xls\",\n            \".xlsx\",\n            \".xhtml\",\n            \".xml\",\n            \".webp\",\n        )\n        return file_path.lower().endswith(docling_exts)\n\n    def _process_docling_in_subprocess(self, file_path: str) -> Data | None:\n        \"\"\"Run Docling in a separate OS process and map the result to a Data object.\n\n        We avoid multiprocessing pickling by launching `python -c \"<script>\"` and\n        passing JSON config via stdin. The child prints a JSON result to stdout.\n        \"\"\"\n        if not file_path:\n            return None\n\n        args: dict[str, Any] = {\n            \"file_path\": file_path,\n            \"markdown\": bool(self.markdown),\n            \"image_mode\": str(self.IMAGE_MODE),\n            \"md_image_placeholder\": str(self.md_image_placeholder),\n            \"md_page_break_placeholder\": str(self.md_page_break_placeholder),\n            \"pipeline\": str(self.pipeline),\n            \"ocr_engine\": (\n                self.ocr_engine if self.ocr_engine and self.ocr_engine != \"None\" and self.pipeline != \"vlm\" else None\n            ),\n        }\n\n        self.log(f\"Starting Docling subprocess for file: {file_path}\")\n        self.log(args)\n\n        # Child script for isolating the docling processing\n        child_script = textwrap.dedent(\n            r\"\"\"\n            import json, sys\n\n            def try_imports():\n                # Strategy 1: latest layout\n                try:\n                    from docling.datamodel.base_models import ConversionStatus, InputFormat  # type: ignore\n                    from docling.document_converter import DocumentConverter  # type: ignore\n                    from docling_core.types.doc import ImageRefMode  # type: ignore\n                    return ConversionStatus, InputFormat, DocumentConverter, ImageRefMode, \"latest\"\n                except Exception:\n                    pass\n                # Strategy 2: alternative layout\n                try:\n                    from docling.document_converter import DocumentConverter  # type: ignore\n                    try:\n                        from docling_core.types import ConversionStatus, InputFormat  # type: ignore\n                    except Exception:\n                        try:\n                            from docling.datamodel import ConversionStatus, InputFormat  # type: ignore\n                        except Exception:\n                            class ConversionStatus: SUCCESS = \"success\"\n                            class InputFormat:\n                                PDF=\"pdf\"; IMAGE=\"image\"\n                    try:\n                        from docling_core.types.doc import ImageRefMode  # type: ignore\n                    except Exception:\n                        class ImageRefMode:\n                            PLACEHOLDER=\"placeholder\"; EMBEDDED=\"embedded\"\n                    return ConversionStatus, InputFormat, DocumentConverter, ImageRefMode, \"alternative\"\n                except Exception:\n                    pass\n                # Strategy 3: basic converter only\n                try:\n                    from docling.document_converter import DocumentConverter  # type: ignore\n                    class ConversionStatus: SUCCESS = \"success\"\n                    class InputFormat:\n                        PDF=\"pdf\"; IMAGE=\"image\"\n                    class ImageRefMode:\n                        PLACEHOLDER=\"placeholder\"; EMBEDDED=\"embedded\"\n                    return ConversionStatus, InputFormat, DocumentConverter, ImageRefMode, \"basic\"\n                except Exception as e:\n                    raise ImportError(f\"Docling imports failed: {e}\") from e\n\n            def create_converter(strategy, input_format, DocumentConverter, pipeline, ocr_engine):\n                # --- Standard PDF/IMAGE pipeline (your existing behavior), with optional OCR ---\n                if pipeline == \"standard\":\n                    try:\n                        from docling.datamodel.pipeline_options import PdfPipelineOptions  # type: ignore\n                        from docling.document_converter import PdfFormatOption  # type: ignore\n\n                        pipe = PdfPipelineOptions()\n                        pipe.do_ocr = False\n\n                        if ocr_engine:\n                            try:\n                                from docling.models.factories import get_ocr_factory  # type: ignore\n                                pipe.do_ocr = True\n                                fac = get_ocr_factory(allow_external_plugins=False)\n                                pipe.ocr_options = fac.create_options(kind=ocr_engine)\n                            except Exception:\n                                # If OCR setup fails, disable it\n                                pipe.do_ocr = False\n\n                        fmt = {}\n                        if hasattr(input_format, \"PDF\"):\n                            fmt[getattr(input_format, \"PDF\")] = PdfFormatOption(pipeline_options=pipe)\n                        if hasattr(input_format, \"IMAGE\"):\n                            fmt[getattr(input_format, \"IMAGE\")] = PdfFormatOption(pipeline_options=pipe)\n\n                        return DocumentConverter(format_options=fmt)\n                    except Exception:\n                        return DocumentConverter()\n\n                # --- Vision-Language Model (VLM) pipeline ---\n                if pipeline == \"vlm\":\n                    try:\n                        from docling.pipeline.vlm_pipeline import VlmPipeline\n                        from docling.document_converter import PdfFormatOption  # type: ignore\n\n                        vl_pipe = VlmPipelineOptions()\n\n                        # VLM paths generally don't need OCR; keep OCR off by default here.\n                        fmt = {}\n                        if hasattr(input_format, \"PDF\"):\n                            fmt[getattr(input_format, \"PDF\")] = PdfFormatOption(pipeline_cls=VlmPipeline)\n                        if hasattr(input_format, \"IMAGE\"):\n                            fmt[getattr(input_format, \"IMAGE\")] = PdfFormatOption(pipeline_cls=VlmPipeline)\n\n                        return DocumentConverter(format_options=fmt)\n                    except Exception:\n                        return DocumentConverter()\n\n                # --- Fallback: default converter with no special options ---\n                return DocumentConverter()\n\n            def export_markdown(document, ImageRefMode, image_mode, img_ph, pg_ph):\n                try:\n                    mode = getattr(ImageRefMode, image_mode.upper(), image_mode)\n                    return document.export_to_markdown(\n                        image_mode=mode,\n                        image_placeholder=img_ph,\n                        page_break_placeholder=pg_ph,\n                    )\n                except Exception:\n                    try:\n                        return document.export_to_text()\n                    except Exception:\n                        return str(document)\n\n            def to_rows(doc_dict):\n                rows = []\n                for t in doc_dict.get(\"texts\", []):\n                    prov = t.get(\"prov\") or []\n                    page_no = None\n                    if prov and isinstance(prov, list) and isinstance(prov[0], dict):\n                        page_no = prov[0].get(\"page_no\")\n                    rows.append({\n                        \"page_no\": page_no,\n                        \"label\": t.get(\"label\"),\n                        \"text\": t.get(\"text\"),\n                        \"level\": t.get(\"level\"),\n                    })\n                return rows\n\n            def main():\n                cfg = json.loads(sys.stdin.read())\n                file_path = cfg[\"file_path\"]\n                markdown = cfg[\"markdown\"]\n                image_mode = cfg[\"image_mode\"]\n                img_ph = cfg[\"md_image_placeholder\"]\n                pg_ph = cfg[\"md_page_break_placeholder\"]\n                pipeline = cfg[\"pipeline\"]\n                ocr_engine = cfg.get(\"ocr_engine\")\n                meta = {\"file_path\": file_path}\n\n                try:\n                    ConversionStatus, InputFormat, DocumentConverter, ImageRefMode, strategy = try_imports()\n                    converter = create_converter(strategy, InputFormat, DocumentConverter, pipeline, ocr_engine)\n                    try:\n                        res = converter.convert(file_path)\n                    except Exception as e:\n                        print(json.dumps({\"ok\": False, \"error\": f\"Docling conversion error: {e}\", \"meta\": meta}))\n                        return\n\n                    ok = False\n                    if hasattr(res, \"status\"):\n                        try:\n                            ok = (res.status == ConversionStatus.SUCCESS) or (str(res.status).lower() == \"success\")\n                        except Exception:\n                            ok = (str(res.status).lower() == \"success\")\n                    if not ok and hasattr(res, \"document\"):\n                        ok = getattr(res, \"document\", None) is not None\n                    if not ok:\n                        print(json.dumps({\"ok\": False, \"error\": \"Docling conversion failed\", \"meta\": meta}))\n                        return\n\n                    doc = getattr(res, \"document\", None)\n                    if doc is None:\n                        print(json.dumps({\"ok\": False, \"error\": \"Docling produced no document\", \"meta\": meta}))\n                        return\n\n                    if markdown:\n                        text = export_markdown(doc, ImageRefMode, image_mode, img_ph, pg_ph)\n                        print(json.dumps({\"ok\": True, \"mode\": \"markdown\", \"text\": text, \"meta\": meta}))\n                        return\n\n                    # structured\n                    try:\n                        doc_dict = doc.export_to_dict()\n                    except Exception as e:\n                        print(json.dumps({\"ok\": False, \"error\": f\"Docling export_to_dict failed: {e}\", \"meta\": meta}))\n                        return\n\n                    rows = to_rows(doc_dict)\n                    print(json.dumps({\"ok\": True, \"mode\": \"structured\", \"doc\": rows, \"meta\": meta}))\n                except Exception as e:\n                    print(\n                        json.dumps({\n                            \"ok\": False,\n                            \"error\": f\"Docling processing error: {e}\",\n                            \"meta\": {\"file_path\": file_path},\n                        })\n                    )\n\n            if __name__ == \"__main__\":\n                main()\n            \"\"\"\n        )\n\n        # Validate file_path to avoid command injection or unsafe input\n        if not isinstance(args[\"file_path\"], str) or any(c in args[\"file_path\"] for c in [\";\", \"|\", \"&\", \"$\", \"`\"]):\n            return Data(data={\"error\": \"Unsafe file path detected.\", \"file_path\": args[\"file_path\"]})\n\n        proc = subprocess.run(  # noqa: S603\n            [sys.executable, \"-u\", \"-c\", child_script],\n            input=json.dumps(args).encode(\"utf-8\"),\n            capture_output=True,\n            check=False,\n        )\n\n        if not proc.stdout:\n            err_msg = proc.stderr.decode(\"utf-8\", errors=\"replace\") or \"no output from child process\"\n            return Data(data={\"error\": f\"Docling subprocess error: {err_msg}\", \"file_path\": file_path})\n\n        try:\n            result = json.loads(proc.stdout.decode(\"utf-8\"))\n        except Exception as e:  # noqa: BLE001\n            err_msg = proc.stderr.decode(\"utf-8\", errors=\"replace\")\n            return Data(\n                data={\"error\": f\"Invalid JSON from Docling subprocess: {e}. stderr={err_msg}\", \"file_path\": file_path},\n            )\n\n        if not result.get(\"ok\"):\n            return Data(data={\"error\": result.get(\"error\", \"Unknown Docling error\"), **result.get(\"meta\", {})})\n\n        meta = result.get(\"meta\", {})\n        if result.get(\"mode\") == \"markdown\":\n            exported_content = str(result.get(\"text\", \"\"))\n            return Data(\n                text=exported_content,\n                data={\"exported_content\": exported_content, \"export_format\": self.EXPORT_FORMAT, **meta},\n            )\n\n        rows = list(result.get(\"doc\", []))\n        return Data(data={\"doc\": rows, \"export_format\": self.EXPORT_FORMAT, **meta})\n\n    def process_files(\n        self,\n        file_list: list[BaseFileComponent.BaseFile],\n    ) -> list[BaseFileComponent.BaseFile]:\n        \"\"\"Process input files.\n\n        - Single file + advanced_mode => Docling in a separate process.\n        - Otherwise => standard parsing in current process (optionally threaded).\n        \"\"\"\n        if not file_list:\n            msg = \"No files to process.\"\n            raise ValueError(msg)\n\n        def process_file_standard(file_path: str, *, silent_errors: bool = False) -> Data | None:\n            try:\n                return parse_text_file_to_data(file_path, silent_errors=silent_errors)\n            except FileNotFoundError as e:\n                self.log(f\"File not found: {file_path}. Error: {e}\")\n                if not silent_errors:\n                    raise\n                return None\n            except Exception as e:\n                self.log(f\"Unexpected error processing {file_path}: {e}\")\n                if not silent_errors:\n                    raise\n                return None\n\n        # Advanced path: only for a single Docling-compatible file\n        if len(file_list) == 1:\n            file_path = str(file_list[0].path)\n            if self.advanced_mode and self._is_docling_compatible(file_path):\n                advanced_data: Data | None = self._process_docling_in_subprocess(file_path)\n\n                # --- UNNEST: expand each element in `doc` to its own Data row\n                payload = getattr(advanced_data, \"data\", {}) or {}\n                doc_rows = payload.get(\"doc\")\n                if isinstance(doc_rows, list):\n                    rows: list[Data | None] = [\n                        Data(\n                            data={\n                                \"file_path\": file_path,\n                                **(item if isinstance(item, dict) else {\"value\": item}),\n                            },\n                        )\n                        for item in doc_rows\n                    ]\n                    return self.rollup_data(file_list, rows)\n\n                # If not structured, keep as-is (e.g., markdown export or error dict)\n                return self.rollup_data(file_list, [advanced_data])\n\n        # Standard multi-file (or single non-advanced) path\n        concurrency = 1 if not self.use_multithreading else max(1, self.concurrency_multithreading)\n        file_paths = [str(f.path) for f in file_list]\n        self.log(f\"Starting parallel processing of {len(file_paths)} files with concurrency: {concurrency}.\")\n        my_data = parallel_load_data(\n            file_paths,\n            silent_errors=self.silent_errors,\n            load_function=process_file_standard,\n            max_concurrency=concurrency,\n        )\n        return self.rollup_data(file_list, my_data)\n\n    # ------------------------------ Output helpers -----------------------------------\n\n    def load_files_helper(self) -> DataFrame:\n        result = self.load_files()\n\n        # Error condition - raise error if no text and an error is present\n        if not hasattr(result, \"text\"):\n            if hasattr(result, \"error\"):\n                raise ValueError(result.error[0])\n            msg = \"No content generated.\"\n            raise ValueError(msg)\n\n        return result\n\n    def load_files_dataframe(self) -> DataFrame:\n        \"\"\"Load files using advanced Docling processing and export to DataFrame format.\"\"\"\n        self.markdown = False\n        return self.load_files_helper()\n\n    def load_files_markdown(self) -> Message:\n        \"\"\"Load files using advanced Docling processing and export to Markdown format.\"\"\"\n        self.markdown = True\n        result = self.load_files_helper()\n        return Message(text=str(result.text[0]))\n"
              },
              "concurrency_multithreading": {
                "_input_type": "IntInput",
                "advanced": true,
                "display_name": "Processing Concurrency",
                "dynamic": false,
                "info": "When multiple files are being processed, the number of files to process concurrently.",
                "list": false,
                "list_add_label": "Add More",
                "name": "concurrency_multithreading",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "int",
                "value": 1
              },
              "delete_server_file_after_processing": {
                "_input_type": "BoolInput",
                "advanced": true,
                "display_name": "Delete Server File After Processing",
                "dynamic": false,
                "info": "If true, the Server File Path will be deleted after processing.",
                "list": false,
                "list_add_label": "Add More",
                "name": "delete_server_file_after_processing",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "bool",
                "value": true
              },
              "doc_key": {
                "_input_type": "MessageTextInput",
                "advanced": true,
                "display_name": "Doc Key",
                "dynamic": false,
                "info": "The key to use for the DoclingDocument column.",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "doc_key",
                "placeholder": "",
                "required": false,
                "show": false,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "doc"
              },
              "file_path": {
                "_input_type": "HandleInput",
                "advanced": true,
                "display_name": "Server File Path",
                "dynamic": false,
                "info": "Data object with a 'file_path' property pointing to server file or a Message object with a path to the file. Supercedes 'Path' but supports same file types.",
                "input_types": [
                  "Data",
                  "Message"
                ],
                "list": true,
                "list_add_label": "Add More",
                "name": "file_path",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "trace_as_metadata": true,
                "type": "other",
                "value": ""
              },
              "ignore_unspecified_files": {
                "_input_type": "BoolInput",
                "advanced": true,
                "display_name": "Ignore Unspecified Files",
                "dynamic": false,
                "info": "If true, Data with no 'file_path' property will be ignored.",
                "list": false,
                "list_add_label": "Add More",
                "name": "ignore_unspecified_files",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "bool",
                "value": false
              },
              "ignore_unsupported_extensions": {
                "_input_type": "BoolInput",
                "advanced": true,
                "display_name": "Ignore Unsupported Extensions",
                "dynamic": false,
                "info": "If true, files with unsupported extensions will not be processed.",
                "list": false,
                "list_add_label": "Add More",
                "name": "ignore_unsupported_extensions",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "bool",
                "value": true
              },
              "markdown": {
                "_input_type": "BoolInput",
                "advanced": false,
                "display_name": "Markdown Export",
                "dynamic": false,
                "info": "Export processed documents to Markdown format. Only available when advanced mode is enabled.",
                "list": false,
                "list_add_label": "Add More",
                "name": "markdown",
                "placeholder": "",
                "required": false,
                "show": false,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "bool",
                "value": false
              },
              "md_image_placeholder": {
                "_input_type": "StrInput",
                "advanced": true,
                "display_name": "Image placeholder",
                "dynamic": false,
                "info": "Specify the image placeholder for markdown exports.",
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "md_image_placeholder",
                "placeholder": "",
                "required": false,
                "show": false,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "str",
                "value": "<!-- image -->"
              },
              "md_page_break_placeholder": {
                "_input_type": "StrInput",
                "advanced": true,
                "display_name": "Page break placeholder",
                "dynamic": false,
                "info": "Add this placeholder between pages in the markdown output.",
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "md_page_break_placeholder",
                "placeholder": "",
                "required": false,
                "show": false,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "str",
                "value": ""
              },
              "ocr_engine": {
                "_input_type": "DropdownInput",
                "advanced": true,
                "combobox": false,
                "dialog_inputs": {},
                "display_name": "OCR Engine",
                "dynamic": false,
                "external_options": {},
                "info": "OCR engine to use. Only available when pipeline is set to 'standard'.",
                "name": "ocr_engine",
                "options": [
                  "None",
                  "easyocr"
                ],
                "options_metadata": [],
                "placeholder": "",
                "required": false,
                "show": false,
                "title_case": false,
                "toggle": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "str",
                "value": "easyocr"
              },
              "path": {
                "_input_type": "FileInput",
                "advanced": false,
                "display_name": "Files",
                "dynamic": false,
                "fileTypes": [
                  "csv",
                  "json",
                  "pdf",
                  "txt",
                  "md",
                  "mdx",
                  "yaml",
                  "yml",
                  "xml",
                  "html",
                  "htm",
                  "docx",
                  "py",
                  "sh",
                  "sql",
                  "js",
                  "ts",
                  "tsx",
                  "adoc",
                  "asciidoc",
                  "asc",
                  "bmp",
                  "dotx",
                  "dotm",
                  "docm",
                  "jpeg",
                  "png",
                  "potx",
                  "ppsx",
                  "pptm",
                  "potm",
                  "ppsm",
                  "pptx",
                  "tiff",
                  "xls",
                  "xlsx",
                  "xhtml",
                  "webp",
                  "zip",
                  "tar",
                  "tgz",
                  "bz2",
                  "gz"
                ],
                "file_path": [],
                "info": "Supported file extensions: csv, json, pdf, txt, md, mdx, yaml, yml, xml, html, htm, docx, py, sh, sql, js, ts, tsx, adoc, asciidoc, asc, bmp, dotx, dotm, docm, jpeg, png, potx, ppsx, pptm, potm, ppsm, pptx, tiff, xls, xlsx, xhtml, webp; optionally bundled in file extensions: zip, tar, tgz, bz2, gz",
                "list": true,
                "list_add_label": "Add More",
                "name": "path",
                "placeholder": "",
                "real_time_refresh": true,
                "required": false,
                "show": true,
                "temp_file": false,
                "title_case": false,
                "trace_as_metadata": true,
                "type": "file",
                "value": ""
              },
              "pipeline": {
                "_input_type": "DropdownInput",
                "advanced": true,
                "combobox": false,
                "dialog_inputs": {},
                "display_name": "Pipeline",
                "dynamic": false,
                "external_options": {},
                "info": "Docling pipeline to use",
                "name": "pipeline",
                "options": [
                  "standard",
                  "vlm"
                ],
                "options_metadata": [],
                "placeholder": "",
                "real_time_refresh": true,
                "required": false,
                "show": false,
                "title_case": false,
                "toggle": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "str",
                "value": "standard"
              },
              "separator": {
                "_input_type": "StrInput",
                "advanced": true,
                "display_name": "Separator",
                "dynamic": false,
                "info": "Specify the separator to use between multiple outputs in Message format.",
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "separator",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "str",
                "value": "\n\n"
              },
              "silent_errors": {
                "_input_type": "BoolInput",
                "advanced": true,
                "display_name": "Silent Errors",
                "dynamic": false,
                "info": "If true, errors will not raise an exception.",
                "list": false,
                "list_add_label": "Add More",
                "name": "silent_errors",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "bool",
                "value": false
              },
              "use_multithreading": {
                "_input_type": "BoolInput",
                "advanced": true,
                "display_name": "[Deprecated] Use Multithreading",
                "dynamic": false,
                "info": "Set 'Processing Concurrency' greater than 1 to enable multithreading.",
                "list": false,
                "list_add_label": "Add More",
                "name": "use_multithreading",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "bool",
                "value": true
              }
            },
            "tool_mode": false
          },
          "selected_output": "dataframe",
          "showNode": true,
          "type": "File"
        },
        "id": "File-Gq99J",
        "measured": {
          "height": 213,
          "width": 320
        },
        "position": {
          "x": 408.5618519297534,
          "y": -1321.555387508855
        },
        "selected": false,
        "type": "genericNode"
      },
      {
        "data": {
          "id": "msp_invoice_metrics_builder-gxG08",
          "node": {
            "base_classes": [
              "DataFrame",
              "Message"
            ],
            "beta": false,
            "conditional_paths": [],
            "custom_fields": {},
            "description": "Build invoice KPIs and rollups (month, client, type, optional category) and a compact insights context for an LLM.",
            "display_name": "MSP Invoice Metrics Builder",
            "documentation": "",
            "edited": true,
            "field_order": [
              "invoices_df",
              "date_col",
              "company_col",
              "invoice_total_col",
              "product_total_col",
              "service_total_col",
              "tax_col",
              "invoice_type_col",
              "status_col",
              "reference_col",
              "category_col",
              "exclude_non_posted",
              "posted_status_values",
              "credit_type_values",
              "top_n",
              "anomaly_top_n",
              "show_debug"
            ],
            "frozen": false,
            "icon": "BarChart3",
            "legacy": false,
            "lf_version": "1.6.0",
            "metadata": {},
            "minimized": false,
            "output_types": [],
            "outputs": [
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "KPI Overview",
                "group_outputs": false,
                "hidden": null,
                "method": "build_kpi_overview",
                "name": "kpi_overview",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Revenue by Month",
                "group_outputs": false,
                "hidden": null,
                "method": "build_revenue_by_month",
                "name": "revenue_by_month",
                "options": null,
                "required_inputs": null,
                "selected": "DataFrame",
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Revenue by Client",
                "group_outputs": false,
                "hidden": null,
                "method": "build_revenue_by_client",
                "name": "revenue_by_client",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Revenue by Invoice Type",
                "group_outputs": false,
                "hidden": null,
                "method": "build_revenue_by_type",
                "name": "revenue_by_type",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Revenue Concentration",
                "group_outputs": false,
                "hidden": null,
                "method": "build_revenue_concentration",
                "name": "revenue_concentration",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Category Breakdown (Optional)",
                "group_outputs": false,
                "hidden": null,
                "method": "build_category_breakdown",
                "name": "category_breakdown",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Anomalies (Large Invoices / Spikes)",
                "group_outputs": false,
                "hidden": null,
                "method": "build_anomalies",
                "name": "anomalies",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Insights Context (for LLM)",
                "group_outputs": false,
                "hidden": null,
                "method": "build_insights_context",
                "name": "insights_context",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "Message"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Data Diagnostics",
                "group_outputs": false,
                "hidden": null,
                "method": "build_data_diagnostics",
                "name": "data_diagnostics",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "Message"
                ],
                "value": "__UNDEFINED__"
              }
            ],
            "pinned": false,
            "template": {
              "_type": "Component",
              "anomaly_top_n": {
                "_input_type": "IntInput",
                "advanced": false,
                "display_name": "Top N anomalies",
                "dynamic": false,
                "info": "",
                "list": false,
                "list_add_label": "Add More",
                "name": "anomaly_top_n",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "int",
                "value": 8
              },
              "category_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Category column (optional)",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "category_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Category"
              },
              "code": {
                "advanced": true,
                "dynamic": true,
                "fileTypes": [],
                "file_path": "",
                "info": "",
                "list": false,
                "load_from_db": false,
                "multiline": true,
                "name": "code",
                "password": false,
                "placeholder": "",
                "required": true,
                "show": true,
                "title_case": false,
                "type": "code",
                "value": "from __future__ import annotations\n\nfrom typing import List, Dict, Any, Optional\nimport pandas as pd\nimport numpy as np\n\nfrom langflow.custom import Component\nfrom langflow.io import DataFrameInput, MessageTextInput, BoolInput, IntInput, Output\nfrom langflow.schema import DataFrame\nfrom langflow.schema.message import Message\n\n\nclass MSPInvoiceMetricsBuilder(Component):\n    display_name = \"MSP Invoice Metrics Builder\"\n    description = \"Build invoice KPIs and rollups (month, client, type, optional category) and a compact insights context for an LLM.\"\n    name = \"msp_invoice_metrics_builder\"\n    icon = \"BarChart3\"\n\n    inputs = [\n        DataFrameInput(name=\"invoices_df\", display_name=\"Invoices (Clean) DataFrame\", required=True),\n\n        # Column mappings (adjust to match your export)\n        MessageTextInput(name=\"date_col\", display_name=\"Invoice date column\", value=\"Date\"),\n        MessageTextInput(name=\"company_col\", display_name=\"Company column\", value=\"Company Name\"),\n        MessageTextInput(name=\"invoice_total_col\", display_name=\"Invoice total column\", value=\"Invoice Total\"),\n        MessageTextInput(name=\"product_total_col\", display_name=\"Product total column (optional)\", value=\"Product Total\"),\n        MessageTextInput(name=\"service_total_col\", display_name=\"Service total column (optional)\", value=\"Service Total\"),\n        MessageTextInput(name=\"tax_col\", display_name=\"Sales tax column (optional)\", value=\"Sales Tax\"),\n        MessageTextInput(name=\"invoice_type_col\", display_name=\"Invoice type column (optional)\", value=\"Invoice Type\"),\n        MessageTextInput(name=\"status_col\", display_name=\"Status column (optional)\", value=\"Status\"),\n        MessageTextInput(name=\"reference_col\", display_name=\"Invoice reference/summary column (optional)\", value=\"Invoice Reference\"),\n\n        # Optional categorization column if your ETL adds one\n        MessageTextInput(name=\"category_col\", display_name=\"Category column (optional)\", value=\"Category\"),\n\n        # Filters and semantics\n        BoolInput(name=\"exclude_non_posted\", display_name=\"Exclude non-posted invoices\", value=True),\n        MessageTextInput(\n            name=\"posted_status_values\",\n            display_name=\"Posted status values (comma-separated)\",\n            value=\"Posted,Paid,Closed\",\n        ),\n        MessageTextInput(\n            name=\"credit_type_values\",\n            display_name=\"Credit memo type values (comma-separated)\",\n            value=\"Credit Memo,Credit,CM\",\n        ),\n\n        IntInput(name=\"top_n\", display_name=\"Top N rows\", value=15),\n        IntInput(name=\"anomaly_top_n\", display_name=\"Top N anomalies\", value=8),\n        BoolInput(name=\"show_debug\", display_name=\"Include debug details in insights output\", value=False),\n    ]\n\n    outputs = [\n        Output(name=\"kpi_overview\", display_name=\"KPI Overview\", method=\"build_kpi_overview\"),\n        Output(name=\"revenue_by_month\", display_name=\"Revenue by Month\", method=\"build_revenue_by_month\"),\n        Output(name=\"revenue_by_client\", display_name=\"Revenue by Client\", method=\"build_revenue_by_client\"),\n        Output(name=\"revenue_by_type\", display_name=\"Revenue by Invoice Type\", method=\"build_revenue_by_type\"),\n        Output(name=\"revenue_concentration\", display_name=\"Revenue Concentration\", method=\"build_revenue_concentration\"),\n        Output(name=\"category_breakdown\", display_name=\"Category Breakdown (Optional)\", method=\"build_category_breakdown\"),\n        Output(name=\"anomalies\", display_name=\"Anomalies (Large Invoices / Spikes)\", method=\"build_anomalies\"),\n        Output(name=\"insights_context\", display_name=\"Insights Context (for LLM)\", method=\"build_insights_context\"),\n        Output(name=\"data_diagnostics\", display_name=\"Data Diagnostics\", method=\"build_data_diagnostics\"),\n    ]\n\n    # -------------------------\n    # Helpers\n    # -------------------------\n\n    def _safe_col(self, df: pd.DataFrame, col: str) -> bool:\n        return col is not None and str(col).strip() != \"\" and col in df.columns\n\n    def _unwrap_to_df(self, obj: Any) -> pd.DataFrame:\n        if obj is None:\n            return pd.DataFrame()\n\n        if isinstance(obj, pd.DataFrame):\n            df = obj.copy()\n            df.columns = [str(c).replace(\"\\ufeff\", \"\").strip() for c in df.columns]\n            return df\n\n        if hasattr(obj, \"data\"):\n            d = getattr(obj, \"data\")\n            if isinstance(d, pd.DataFrame):\n                df = d.copy()\n                df.columns = [str(c).replace(\"\\ufeff\", \"\").strip() for c in df.columns]\n                return df\n            if isinstance(d, list):\n                df = pd.DataFrame(d)\n                df.columns = [str(c).replace(\"\\ufeff\", \"\").strip() for c in df.columns]\n                return df\n            if isinstance(d, dict):\n                for k in [\"data\", \"rows\", \"records\", \"items\"]:\n                    if k in d and isinstance(d[k], list):\n                        df = pd.DataFrame(d[k])\n                        df.columns = [str(c).replace(\"\\ufeff\", \"\").strip() for c in df.columns]\n                        return df\n\n        if isinstance(obj, dict):\n            for k in [\"data\", \"rows\", \"records\", \"items\"]:\n                if k in obj and isinstance(obj[k], list):\n                    df = pd.DataFrame(obj[k])\n                    df.columns = [str(c).replace(\"\\ufeff\", \"\").strip() for c in df.columns]\n                    return df\n            df = pd.DataFrame(obj)\n            df.columns = [str(c).replace(\"\\ufeff\", \"\").strip() for c in df.columns]\n            return df\n\n        if isinstance(obj, list):\n            df = pd.DataFrame(obj)\n            df.columns = [str(c).replace(\"\\ufeff\", \"\").strip() for c in df.columns]\n            return df\n\n        df = pd.DataFrame(obj)\n        df.columns = [str(c).replace(\"\\ufeff\", \"\").strip() for c in df.columns]\n        return df\n\n    def _to_dt(self, s: pd.Series) -> pd.Series:\n        return pd.to_datetime(s, errors=\"coerce\")\n\n    def _to_money(self, s: pd.Series) -> pd.Series:\n        x = s.astype(str).str.strip()\n        x = x.str.replace(\"$\", \"\", regex=False).str.replace(\",\", \"\", regex=False)\n        x = x.str.replace(\"(\", \"-\", regex=False).str.replace(\")\", \"\", regex=False)\n        return pd.to_numeric(x, errors=\"coerce\")\n\n    def _company_key(self, s: pd.Series) -> pd.Series:\n        x = s.fillna(\"\").astype(str).str.lower().str.strip()\n        x = x.str.replace(r\"[^\\w\\s]\", \" \", regex=True)\n        x = x.str.replace(r\"\\b(inc|incorporated|llc|l\\.l\\.c|ltd|corp|corporation|co|company)\\b\", \" \", regex=True)\n        x = x.str.replace(r\"\\s+\", \" \", regex=True).str.strip()\n        return x\n\n    def _parse_csv_list(self, s: str) -> List[str]:\n        if s is None:\n            return []\n        return [p.strip() for p in str(s).split(\",\") if p.strip()]\n\n    def _prep_invoices(self) -> pd.DataFrame:\n        df = self._unwrap_to_df(self.invoices_df)\n        if df.empty:\n            return df\n\n        # Date\n        if self._safe_col(df, self.date_col):\n            df[self.date_col] = self._to_dt(df[self.date_col])\n\n        # Core money fields\n        if self._safe_col(df, self.invoice_total_col):\n            df[\"_invoice_total\"] = self._to_money(df[self.invoice_total_col]).fillna(0.0)\n        else:\n            df[\"_invoice_total\"] = np.nan\n\n        if self._safe_col(df, self.product_total_col):\n            df[\"_product_total\"] = self._to_money(df[self.product_total_col]).fillna(0.0)\n        else:\n            df[\"_product_total\"] = np.nan\n\n        if self._safe_col(df, self.service_total_col):\n            df[\"_service_total\"] = self._to_money(df[self.service_total_col]).fillna(0.0)\n        else:\n            df[\"_service_total\"] = np.nan\n\n        if self._safe_col(df, self.tax_col):\n            df[\"_tax_total\"] = self._to_money(df[self.tax_col]).fillna(0.0)\n        else:\n            df[\"_tax_total\"] = np.nan\n\n        # Company normalization\n        if self._safe_col(df, self.company_col):\n            df[\"_company\"] = df[self.company_col].astype(str).str.strip()\n        else:\n            df[\"_company\"] = \"(unknown)\"\n        df[\"_company_key\"] = self._company_key(df[\"_company\"])\n\n        # Type, status, reference\n        df[\"_type\"] = df[self.invoice_type_col].astype(str).str.strip() if self._safe_col(df, self.invoice_type_col) else \"(unknown)\"\n        df[\"_status\"] = df[self.status_col].astype(str).str.strip() if self._safe_col(df, self.status_col) else \"(unknown)\"\n        df[\"_reference\"] = df[self.reference_col].astype(str).str.strip() if self._safe_col(df, self.reference_col) else \"\"\n\n        # Posted filter\n        if bool(self.exclude_non_posted) and self._safe_col(df, self.status_col):\n            allowed = set([x.lower() for x in self._parse_csv_list(self.posted_status_values)])\n            df = df[df[\"_status\"].astype(str).str.lower().isin(allowed)].copy()\n\n        # Credit memo sign\n        credit_vals = set([x.lower() for x in self._parse_csv_list(self.credit_type_values)])\n        is_credit = df[\"_type\"].astype(str).str.lower().isin(credit_vals)\n        df[\"_sign\"] = np.where(is_credit, -1.0, 1.0)\n\n        # Net revenue fields\n        df[\"revenue_net\"] = df[\"_invoice_total\"] * df[\"_sign\"]\n        df[\"product_net\"] = df[\"_product_total\"] * df[\"_sign\"] if df[\"_product_total\"].notna().any() else np.nan\n        df[\"service_net\"] = df[\"_service_total\"] * df[\"_sign\"] if df[\"_service_total\"].notna().any() else np.nan\n\n        # Month\n        if self._safe_col(df, self.date_col) and df[self.date_col].notna().any():\n            df[\"invoice_month\"] = df[self.date_col].dt.to_period(\"M\").astype(str)\n        else:\n            df[\"invoice_month\"] = None\n\n        return df\n\n    # -------------------------\n    # Outputs\n    # -------------------------\n\n    def build_kpi_overview(self) -> DataFrame:\n        df = self._prep_invoices()\n        if df.empty:\n            return DataFrame(pd.DataFrame())\n\n        total_invoices = int(len(df))\n        total_revenue = float(df[\"revenue_net\"].sum()) if \"revenue_net\" in df.columns else np.nan\n        credits_count = int((df[\"_sign\"] < 0).sum()) if \"_sign\" in df.columns else 0\n        avg_invoice = float(df[\"revenue_net\"].mean()) if total_invoices else np.nan\n        median_invoice = float(df[\"revenue_net\"].median()) if total_invoices else np.nan\n\n        out = pd.DataFrame([{\n            \"invoices_count\": total_invoices,\n            \"credits_count\": credits_count,\n            \"revenue_total_net\": total_revenue,\n            \"avg_invoice_net\": avg_invoice,\n            \"median_invoice_net\": median_invoice,\n            \"unique_clients\": int(df[\"_company_key\"].nunique()) if \"_company_key\" in df.columns else np.nan,\n        }])\n        return DataFrame(out)\n\n    def build_revenue_by_month(self) -> DataFrame:\n        df = self._prep_invoices()\n        if df.empty or \"invoice_month\" not in df.columns or df[\"invoice_month\"].isna().all():\n            return DataFrame(pd.DataFrame(columns=[\"invoice_month\", \"revenue_total_net\", \"invoices_count\"]))\n\n        g = df.groupby(\"invoice_month\", dropna=False)\n        out = pd.DataFrame({\n            \"invoice_month\": g.size().index.astype(str),\n            \"invoices_count\": g.size().values,\n            \"revenue_total_net\": g[\"revenue_net\"].sum().values,\n        })\n\n        if \"product_net\" in df.columns and df[\"product_net\"].notna().any():\n            out[\"product_total_net\"] = g[\"product_net\"].sum().values\n        if \"service_net\" in df.columns and df[\"service_net\"].notna().any():\n            out[\"service_total_net\"] = g[\"service_net\"].sum().values\n\n        out = out.sort_values(\"invoice_month\")\n        return DataFrame(out)\n\n    def build_revenue_by_client(self) -> DataFrame:\n        df = self._prep_invoices()\n        if df.empty:\n            return DataFrame(pd.DataFrame(columns=[\"company\", \"invoices_count\", \"revenue_total_net\", \"avg_invoice_net\"]))\n\n        g = df.groupby(\"_company_key\", dropna=False)\n        out = pd.DataFrame({\n            \"_company_key\": g.size().index.astype(str),\n            \"invoices_count\": g.size().values,\n            \"revenue_total_net\": g[\"revenue_net\"].sum().values,\n            \"avg_invoice_net\": g[\"revenue_net\"].mean().values,\n        })\n\n        # Representative display name\n        name_map = (\n            df.groupby(\"_company_key\")[\"_company\"]\n            .agg(lambda s: s.value_counts().index[0] if len(s.dropna()) else \"(unknown)\")\n            .rename(\"company\")\n            .reset_index()\n        )\n        out = out.merge(name_map, on=\"_company_key\", how=\"left\")\n\n        out = out.sort_values(\"revenue_total_net\", ascending=False).head(int(self.top_n))\n        out = out[[\"company\", \"invoices_count\", \"revenue_total_net\", \"avg_invoice_net\"]]\n        return DataFrame(out)\n\n    def build_revenue_by_type(self) -> DataFrame:\n        df = self._prep_invoices()\n        if df.empty:\n            return DataFrame(pd.DataFrame(columns=[\"invoice_type\", \"invoices_count\", \"revenue_total_net\", \"share\"]))\n\n        g = df.groupby(\"_type\", dropna=False)\n        out = pd.DataFrame({\n            \"invoice_type\": g.size().index.astype(str),\n            \"invoices_count\": g.size().values,\n            \"revenue_total_net\": g[\"revenue_net\"].sum().values,\n        })\n        total = float(out[\"revenue_total_net\"].abs().sum()) if len(out) else 0.0\n        out[\"share\"] = out[\"revenue_total_net\"].abs() / (total if total else np.nan)\n        out = out.sort_values(\"revenue_total_net\", ascending=False)\n        return DataFrame(out)\n\n    def build_revenue_concentration(self) -> DataFrame:\n        df = self._prep_invoices()\n        if df.empty:\n            return DataFrame(pd.DataFrame())\n\n        by = df.groupby(\"_company_key\", dropna=False)[\"revenue_net\"].sum().sort_values(ascending=False)\n        total = float(by.sum()) if len(by) else 0.0\n\n        def share_top(n: int) -> float:\n            if total == 0:\n                return np.nan\n            return float(by.head(n).sum() / total)\n\n        out = pd.DataFrame([{\n            \"clients_total\": int(by.shape[0]),\n            \"revenue_total_net\": total,\n            \"top_1_share\": share_top(1),\n            \"top_3_share\": share_top(3),\n            \"top_5_share\": share_top(5),\n            \"top_10_share\": share_top(10),\n        }])\n        return DataFrame(out)\n\n    def build_category_breakdown(self) -> DataFrame:\n        df = self._prep_invoices()\n        if df.empty or not self._safe_col(df, self.category_col):\n            return DataFrame(pd.DataFrame(columns=[\"category\", \"invoices_count\", \"revenue_total_net\", \"share\"]))\n\n        cat = df[self.category_col].fillna(\"(blank)\").astype(str).str.strip()\n        tmp = df.copy()\n        tmp[\"_category\"] = cat\n\n        g = tmp.groupby(\"_category\", dropna=False)\n        out = pd.DataFrame({\n            \"category\": g.size().index.astype(str),\n            \"invoices_count\": g.size().values,\n            \"revenue_total_net\": g[\"revenue_net\"].sum().values,\n        })\n        total = float(out[\"revenue_total_net\"].abs().sum()) if len(out) else 0.0\n        out[\"share\"] = out[\"revenue_total_net\"].abs() / (total if total else np.nan)\n        out = out.sort_values(\"revenue_total_net\", ascending=False)\n        return DataFrame(out)\n\n    def build_anomalies(self) -> DataFrame:\n        df = self._prep_invoices()\n        if df.empty:\n            return DataFrame(pd.DataFrame(columns=[\"date\", \"company\", \"invoice_type\", \"revenue_net\", \"reference\"]))\n\n        # Large invoices\n        x = df[\"revenue_net\"].replace([np.inf, -np.inf], np.nan).dropna()\n        if len(x) < 10:\n            thresh = x.abs().quantile(0.90) if len(x) else np.nan\n        else:\n            thresh = x.abs().quantile(0.95)\n\n        an = df[df[\"revenue_net\"].abs() >= float(thresh) if pd.notna(thresh) else False].copy()\n        an = an.sort_values(\"revenue_net\", ascending=False).head(int(self.anomaly_top_n))\n\n        # Friendly columns\n        out = pd.DataFrame({\n            \"date\": an[self.date_col] if self._safe_col(an, self.date_col) else pd.NaT,\n            \"company\": an[\"_company\"],\n            \"invoice_type\": an[\"_type\"],\n            \"revenue_net\": an[\"revenue_net\"],\n            \"reference\": an[\"_reference\"],\n        })\n        return DataFrame(out)\n\n    # -------------------------\n    # Insights context\n    # -------------------------\n\n    def build_insights_context(self) -> Message:\n        kpi = pd.DataFrame(self.build_kpi_overview())\n        by_month = pd.DataFrame(self.build_revenue_by_month())\n        by_client = pd.DataFrame(self.build_revenue_by_client())\n        by_type = pd.DataFrame(self.build_revenue_by_type())\n        conc = pd.DataFrame(self.build_revenue_concentration())\n        cats = pd.DataFrame(self.build_category_breakdown())\n        anom = pd.DataFrame(self.build_anomalies())\n\n        def fmt_money(x) -> str:\n            return \"n/a\" if pd.isna(x) else f\"${float(x):,.2f}\"\n\n        def fmt_pct(x) -> str:\n            return \"n/a\" if pd.isna(x) else f\"{float(x) * 100:.1f}%\"\n\n        lines: List[str] = []\n        lines.append(\"INVOICE DATASET OVERVIEW\")\n        if not kpi.empty:\n            r = kpi.iloc[0].to_dict()\n            lines.append(f\"invoices_count={r.get('invoices_count')}\")\n            lines.append(f\"unique_clients={r.get('unique_clients')}\")\n            lines.append(f\"credits_count={r.get('credits_count')}\")\n            lines.append(f\"revenue_total_net={fmt_money(r.get('revenue_total_net'))}\")\n            lines.append(f\"avg_invoice_net={fmt_money(r.get('avg_invoice_net'))}\")\n            lines.append(f\"median_invoice_net={fmt_money(r.get('median_invoice_net'))}\")\n        lines.append(\"\")\n\n        lines.append(\"REVENUE CONCENTRATION\")\n        if not conc.empty:\n            r = conc.iloc[0].to_dict()\n            lines.append(f\"top_1_share={fmt_pct(r.get('top_1_share'))}\")\n            lines.append(f\"top_3_share={fmt_pct(r.get('top_3_share'))}\")\n            lines.append(f\"top_10_share={fmt_pct(r.get('top_10_share'))}\")\n        lines.append(\"\")\n\n        lines.append(\"TOP CLIENTS BY REVENUE (net)\")\n        if by_client.empty:\n            lines.append(\"  (no data)\\n\")\n        else:\n            for _, row in by_client.head(min(len(by_client), int(self.top_n))).iterrows():\n                lines.append(f\"  - {row.get('company')}: revenue_total_net={fmt_money(row.get('revenue_total_net'))}, invoices={int(row.get('invoices_count'))}\")\n\n        lines.append(\"\")\n        lines.append(\"REVENUE BY INVOICE TYPE\")\n        if by_type.empty:\n            lines.append(\"  (no data)\\n\")\n        else:\n            for _, row in by_type.iterrows():\n                lines.append(f\"  - {row.get('invoice_type')}: revenue_total_net={fmt_money(row.get('revenue_total_net'))}, share={fmt_pct(row.get('share'))}\")\n\n        if not cats.empty and (cats.shape[0] > 0) and (cats[\"category\"].notna().any()):\n            lines.append(\"\")\n            lines.append(\"CATEGORY BREAKDOWN\")\n            for _, row in cats.iterrows():\n                lines.append(f\"  - {row.get('category')}: revenue_total_net={fmt_money(row.get('revenue_total_net'))}, share={fmt_pct(row.get('share'))}\")\n\n        # Seasonality note\n        if not by_month.empty and \"revenue_total_net\" in by_month.columns:\n            lines.append(\"\")\n            peak = by_month.sort_values(\"revenue_total_net\", ascending=False).head(1)\n            trough = by_month.sort_values(\"revenue_total_net\", ascending=True).head(1)\n            if len(peak) == 1:\n                lines.append(f\"PEAK MONTH: {peak.iloc[0].get('invoice_month')} revenue_total_net={fmt_money(peak.iloc[0].get('revenue_total_net'))}\")\n            if len(trough) == 1:\n                lines.append(f\"LOW MONTH: {trough.iloc[0].get('invoice_month')} revenue_total_net={fmt_money(trough.iloc[0].get('revenue_total_net'))}\")\n\n        # Anomalies\n        lines.append(\"\")\n        lines.append(\"NOTABLE LARGE INVOICES / CREDIT EVENTS\")\n        if anom.empty:\n            lines.append(\"  (no large anomalies detected)\\n\")\n        else:\n            for _, row in anom.iterrows():\n                dt = row.get(\"date\")\n                dt_s = \"n/a\" if pd.isna(dt) else str(pd.to_datetime(dt).date())\n                ref = str(row.get(\"reference\") or \"\").strip()\n                ref = (ref[:90] + \"…\") if len(ref) > 90 else ref\n                lines.append(f\"  - {dt_s} {row.get('company')} {row.get('invoice_type')}: revenue_net={fmt_money(row.get('revenue_net'))} ref={ref}\")\n\n        if bool(self.show_debug):\n            lines.append(\"\")\n            lines.append(self.build_data_diagnostics().text)\n\n        return Message(text=\"\\n\".join(lines))\n\n    # -------------------------\n    # Diagnostics\n    # -------------------------\n\n    def build_data_diagnostics(self) -> Message:\n        df = self._unwrap_to_df(self.invoices_df)\n        lines: List[str] = []\n        lines.append(\"DATA DIAGNOSTICS\")\n        lines.append(f\"invoices_rows={len(df)} cols={len(df.columns)}\")\n        lines.append(\"columns_sample=\" + str(list(df.columns)[:40]))\n        for c in [self.date_col, self.company_col, self.invoice_total_col, self.invoice_type_col, self.status_col, self.reference_col, self.category_col]:\n            if c and c in df.columns:\n                lines.append(f\"col_present: {c}=true\")\n            else:\n                lines.append(f\"col_present: {c}=false\")\n        return Message(text=\"\\n\".join(lines))"
              },
              "company_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Company column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "company_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Company Name"
              },
              "credit_type_values": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Credit memo type values (comma-separated)",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "credit_type_values",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Credit Memo,Credit,CM"
              },
              "date_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Invoice date column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "date_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Date"
              },
              "exclude_non_posted": {
                "_input_type": "BoolInput",
                "advanced": false,
                "display_name": "Exclude non-posted invoices",
                "dynamic": false,
                "info": "",
                "list": false,
                "list_add_label": "Add More",
                "name": "exclude_non_posted",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "bool",
                "value": false
              },
              "invoice_total_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Invoice total column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "invoice_total_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Invoice Total"
              },
              "invoice_type_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Invoice type column (optional)",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "invoice_type_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Invoice Type"
              },
              "invoices_df": {
                "_input_type": "DataFrameInput",
                "advanced": false,
                "display_name": "Invoices (Clean) DataFrame",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "DataFrame"
                ],
                "list": false,
                "list_add_label": "Add More",
                "name": "invoices_df",
                "placeholder": "",
                "required": true,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "other",
                "value": ""
              },
              "posted_status_values": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Posted status values (comma-separated)",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "posted_status_values",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Posted,Paid,Closed"
              },
              "product_total_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Product total column (optional)",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "product_total_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Product Total"
              },
              "reference_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Invoice reference/summary column (optional)",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "reference_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Invoice Reference"
              },
              "service_total_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Service total column (optional)",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "service_total_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Service Total"
              },
              "show_debug": {
                "_input_type": "BoolInput",
                "advanced": false,
                "display_name": "Include debug details in insights output",
                "dynamic": false,
                "info": "",
                "list": false,
                "list_add_label": "Add More",
                "name": "show_debug",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "bool",
                "value": false
              },
              "status_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Status column (optional)",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "status_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Status"
              },
              "tax_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Sales tax column (optional)",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "tax_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Sales Tax"
              },
              "top_n": {
                "_input_type": "IntInput",
                "advanced": false,
                "display_name": "Top N rows",
                "dynamic": false,
                "info": "",
                "list": false,
                "list_add_label": "Add More",
                "name": "top_n",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "int",
                "value": 15
              }
            },
            "tool_mode": false
          },
          "selected_output": "revenue_by_month",
          "showNode": true,
          "type": "msp_invoice_metrics_builder"
        },
        "dragging": false,
        "id": "msp_invoice_metrics_builder-gxG08",
        "measured": {
          "height": 1429,
          "width": 320
        },
        "position": {
          "x": 1408.2748343957846,
          "y": -2254.6709924326224
        },
        "selected": false,
        "type": "genericNode"
      },
      {
        "data": {
          "id": "msp_invoice_metrics_builder-4eg0q",
          "node": {
            "base_classes": [
              "DataFrame",
              "Message"
            ],
            "beta": false,
            "conditional_paths": [],
            "custom_fields": {},
            "description": "Build invoice KPIs and rollups (month, client, type, optional category) and a compact insights context for an LLM.",
            "display_name": "MSP Invoice Metrics Builder",
            "documentation": "",
            "edited": true,
            "field_order": [
              "invoices_df",
              "date_col",
              "company_col",
              "invoice_total_col",
              "product_total_col",
              "service_total_col",
              "tax_col",
              "invoice_type_col",
              "status_col",
              "reference_col",
              "category_col",
              "exclude_non_posted",
              "posted_status_values",
              "credit_type_values",
              "top_n",
              "anomaly_top_n",
              "show_debug"
            ],
            "frozen": false,
            "icon": "BarChart3",
            "legacy": false,
            "lf_version": "1.6.0",
            "metadata": {},
            "minimized": false,
            "output_types": [],
            "outputs": [
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "KPI Overview",
                "group_outputs": false,
                "hidden": null,
                "method": "build_kpi_overview",
                "name": "kpi_overview",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Revenue by Month",
                "group_outputs": false,
                "hidden": null,
                "method": "build_revenue_by_month",
                "name": "revenue_by_month",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Revenue by Client",
                "group_outputs": false,
                "hidden": null,
                "method": "build_revenue_by_client",
                "name": "revenue_by_client",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Revenue by Invoice Type",
                "group_outputs": false,
                "hidden": null,
                "method": "build_revenue_by_type",
                "name": "revenue_by_type",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Revenue Concentration",
                "group_outputs": false,
                "hidden": null,
                "method": "build_revenue_concentration",
                "name": "revenue_concentration",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Category Breakdown (Optional)",
                "group_outputs": false,
                "hidden": null,
                "method": "build_category_breakdown",
                "name": "category_breakdown",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Anomalies (Large Invoices / Spikes)",
                "group_outputs": false,
                "hidden": null,
                "method": "build_anomalies",
                "name": "anomalies",
                "options": null,
                "required_inputs": null,
                "selected": "DataFrame",
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Insights Context (for LLM)",
                "group_outputs": false,
                "hidden": null,
                "method": "build_insights_context",
                "name": "insights_context",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "Message"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Data Diagnostics",
                "group_outputs": false,
                "hidden": null,
                "method": "build_data_diagnostics",
                "name": "data_diagnostics",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "Message"
                ],
                "value": "__UNDEFINED__"
              }
            ],
            "pinned": false,
            "template": {
              "_type": "Component",
              "anomaly_top_n": {
                "_input_type": "IntInput",
                "advanced": false,
                "display_name": "Top N anomalies",
                "dynamic": false,
                "info": "",
                "list": false,
                "list_add_label": "Add More",
                "name": "anomaly_top_n",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "int",
                "value": 8
              },
              "category_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Category column (optional)",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "category_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Category"
              },
              "code": {
                "advanced": true,
                "dynamic": true,
                "fileTypes": [],
                "file_path": "",
                "info": "",
                "list": false,
                "load_from_db": false,
                "multiline": true,
                "name": "code",
                "password": false,
                "placeholder": "",
                "required": true,
                "show": true,
                "title_case": false,
                "type": "code",
                "value": "from __future__ import annotations\n\nfrom typing import List, Dict, Any, Optional\nimport pandas as pd\nimport numpy as np\n\nfrom langflow.custom import Component\nfrom langflow.io import DataFrameInput, MessageTextInput, BoolInput, IntInput, Output\nfrom langflow.schema import DataFrame\nfrom langflow.schema.message import Message\n\n\nclass MSPInvoiceMetricsBuilder(Component):\n    display_name = \"MSP Invoice Metrics Builder\"\n    description = \"Build invoice KPIs and rollups (month, client, type, optional category) and a compact insights context for an LLM.\"\n    name = \"msp_invoice_metrics_builder\"\n    icon = \"BarChart3\"\n\n    inputs = [\n        DataFrameInput(name=\"invoices_df\", display_name=\"Invoices (Clean) DataFrame\", required=True),\n\n        # Column mappings (adjust to match your export)\n        MessageTextInput(name=\"date_col\", display_name=\"Invoice date column\", value=\"Date\"),\n        MessageTextInput(name=\"company_col\", display_name=\"Company column\", value=\"Company Name\"),\n        MessageTextInput(name=\"invoice_total_col\", display_name=\"Invoice total column\", value=\"Invoice Total\"),\n        MessageTextInput(name=\"product_total_col\", display_name=\"Product total column (optional)\", value=\"Product Total\"),\n        MessageTextInput(name=\"service_total_col\", display_name=\"Service total column (optional)\", value=\"Service Total\"),\n        MessageTextInput(name=\"tax_col\", display_name=\"Sales tax column (optional)\", value=\"Sales Tax\"),\n        MessageTextInput(name=\"invoice_type_col\", display_name=\"Invoice type column (optional)\", value=\"Invoice Type\"),\n        MessageTextInput(name=\"status_col\", display_name=\"Status column (optional)\", value=\"Status\"),\n        MessageTextInput(name=\"reference_col\", display_name=\"Invoice reference/summary column (optional)\", value=\"Invoice Reference\"),\n\n        # Optional categorization column if your ETL adds one\n        MessageTextInput(name=\"category_col\", display_name=\"Category column (optional)\", value=\"Category\"),\n\n        # Filters and semantics\n        BoolInput(name=\"exclude_non_posted\", display_name=\"Exclude non-posted invoices\", value=True),\n        MessageTextInput(\n            name=\"posted_status_values\",\n            display_name=\"Posted status values (comma-separated)\",\n            value=\"Posted,Paid,Closed\",\n        ),\n        MessageTextInput(\n            name=\"credit_type_values\",\n            display_name=\"Credit memo type values (comma-separated)\",\n            value=\"Credit Memo,Credit,CM\",\n        ),\n\n        IntInput(name=\"top_n\", display_name=\"Top N rows\", value=15),\n        IntInput(name=\"anomaly_top_n\", display_name=\"Top N anomalies\", value=8),\n        BoolInput(name=\"show_debug\", display_name=\"Include debug details in insights output\", value=False),\n    ]\n\n    outputs = [\n        Output(name=\"kpi_overview\", display_name=\"KPI Overview\", method=\"build_kpi_overview\"),\n        Output(name=\"revenue_by_month\", display_name=\"Revenue by Month\", method=\"build_revenue_by_month\"),\n        Output(name=\"revenue_by_client\", display_name=\"Revenue by Client\", method=\"build_revenue_by_client\"),\n        Output(name=\"revenue_by_type\", display_name=\"Revenue by Invoice Type\", method=\"build_revenue_by_type\"),\n        Output(name=\"revenue_concentration\", display_name=\"Revenue Concentration\", method=\"build_revenue_concentration\"),\n        Output(name=\"category_breakdown\", display_name=\"Category Breakdown (Optional)\", method=\"build_category_breakdown\"),\n        Output(name=\"anomalies\", display_name=\"Anomalies (Large Invoices / Spikes)\", method=\"build_anomalies\"),\n        Output(name=\"insights_context\", display_name=\"Insights Context (for LLM)\", method=\"build_insights_context\"),\n        Output(name=\"data_diagnostics\", display_name=\"Data Diagnostics\", method=\"build_data_diagnostics\"),\n    ]\n\n    # -------------------------\n    # Helpers\n    # -------------------------\n\n    def _safe_col(self, df: pd.DataFrame, col: str) -> bool:\n        return col is not None and str(col).strip() != \"\" and col in df.columns\n\n    def _unwrap_to_df(self, obj: Any) -> pd.DataFrame:\n        if obj is None:\n            return pd.DataFrame()\n\n        if isinstance(obj, pd.DataFrame):\n            df = obj.copy()\n            df.columns = [str(c).replace(\"\\ufeff\", \"\").strip() for c in df.columns]\n            return df\n\n        if hasattr(obj, \"data\"):\n            d = getattr(obj, \"data\")\n            if isinstance(d, pd.DataFrame):\n                df = d.copy()\n                df.columns = [str(c).replace(\"\\ufeff\", \"\").strip() for c in df.columns]\n                return df\n            if isinstance(d, list):\n                df = pd.DataFrame(d)\n                df.columns = [str(c).replace(\"\\ufeff\", \"\").strip() for c in df.columns]\n                return df\n            if isinstance(d, dict):\n                for k in [\"data\", \"rows\", \"records\", \"items\"]:\n                    if k in d and isinstance(d[k], list):\n                        df = pd.DataFrame(d[k])\n                        df.columns = [str(c).replace(\"\\ufeff\", \"\").strip() for c in df.columns]\n                        return df\n\n        if isinstance(obj, dict):\n            for k in [\"data\", \"rows\", \"records\", \"items\"]:\n                if k in obj and isinstance(obj[k], list):\n                    df = pd.DataFrame(obj[k])\n                    df.columns = [str(c).replace(\"\\ufeff\", \"\").strip() for c in df.columns]\n                    return df\n            df = pd.DataFrame(obj)\n            df.columns = [str(c).replace(\"\\ufeff\", \"\").strip() for c in df.columns]\n            return df\n\n        if isinstance(obj, list):\n            df = pd.DataFrame(obj)\n            df.columns = [str(c).replace(\"\\ufeff\", \"\").strip() for c in df.columns]\n            return df\n\n        df = pd.DataFrame(obj)\n        df.columns = [str(c).replace(\"\\ufeff\", \"\").strip() for c in df.columns]\n        return df\n\n    def _to_dt(self, s: pd.Series) -> pd.Series:\n        return pd.to_datetime(s, errors=\"coerce\")\n\n    def _to_money(self, s: pd.Series) -> pd.Series:\n        x = s.astype(str).str.strip()\n        x = x.str.replace(\"$\", \"\", regex=False).str.replace(\",\", \"\", regex=False)\n        x = x.str.replace(\"(\", \"-\", regex=False).str.replace(\")\", \"\", regex=False)\n        return pd.to_numeric(x, errors=\"coerce\")\n\n    def _company_key(self, s: pd.Series) -> pd.Series:\n        x = s.fillna(\"\").astype(str).str.lower().str.strip()\n        x = x.str.replace(r\"[^\\w\\s]\", \" \", regex=True)\n        x = x.str.replace(r\"\\b(inc|incorporated|llc|l\\.l\\.c|ltd|corp|corporation|co|company)\\b\", \" \", regex=True)\n        x = x.str.replace(r\"\\s+\", \" \", regex=True).str.strip()\n        return x\n\n    def _parse_csv_list(self, s: str) -> List[str]:\n        if s is None:\n            return []\n        return [p.strip() for p in str(s).split(\",\") if p.strip()]\n\n    def _prep_invoices(self) -> pd.DataFrame:\n        df = self._unwrap_to_df(self.invoices_df)\n        if df.empty:\n            return df\n\n        # Date\n        if self._safe_col(df, self.date_col):\n            df[self.date_col] = self._to_dt(df[self.date_col])\n\n        # Core money fields\n        if self._safe_col(df, self.invoice_total_col):\n            df[\"_invoice_total\"] = self._to_money(df[self.invoice_total_col]).fillna(0.0)\n        else:\n            df[\"_invoice_total\"] = np.nan\n\n        if self._safe_col(df, self.product_total_col):\n            df[\"_product_total\"] = self._to_money(df[self.product_total_col]).fillna(0.0)\n        else:\n            df[\"_product_total\"] = np.nan\n\n        if self._safe_col(df, self.service_total_col):\n            df[\"_service_total\"] = self._to_money(df[self.service_total_col]).fillna(0.0)\n        else:\n            df[\"_service_total\"] = np.nan\n\n        if self._safe_col(df, self.tax_col):\n            df[\"_tax_total\"] = self._to_money(df[self.tax_col]).fillna(0.0)\n        else:\n            df[\"_tax_total\"] = np.nan\n\n        # Company normalization\n        if self._safe_col(df, self.company_col):\n            df[\"_company\"] = df[self.company_col].astype(str).str.strip()\n        else:\n            df[\"_company\"] = \"(unknown)\"\n        df[\"_company_key\"] = self._company_key(df[\"_company\"])\n\n        # Type, status, reference\n        df[\"_type\"] = df[self.invoice_type_col].astype(str).str.strip() if self._safe_col(df, self.invoice_type_col) else \"(unknown)\"\n        df[\"_status\"] = df[self.status_col].astype(str).str.strip() if self._safe_col(df, self.status_col) else \"(unknown)\"\n        df[\"_reference\"] = df[self.reference_col].astype(str).str.strip() if self._safe_col(df, self.reference_col) else \"\"\n\n        # Posted filter\n        if bool(self.exclude_non_posted) and self._safe_col(df, self.status_col):\n            allowed = set([x.lower() for x in self._parse_csv_list(self.posted_status_values)])\n            df = df[df[\"_status\"].astype(str).str.lower().isin(allowed)].copy()\n\n        # Credit memo sign\n        credit_vals = set([x.lower() for x in self._parse_csv_list(self.credit_type_values)])\n        is_credit = df[\"_type\"].astype(str).str.lower().isin(credit_vals)\n        df[\"_sign\"] = np.where(is_credit, -1.0, 1.0)\n\n        # Net revenue fields\n        df[\"revenue_net\"] = df[\"_invoice_total\"] * df[\"_sign\"]\n        df[\"product_net\"] = df[\"_product_total\"] * df[\"_sign\"] if df[\"_product_total\"].notna().any() else np.nan\n        df[\"service_net\"] = df[\"_service_total\"] * df[\"_sign\"] if df[\"_service_total\"].notna().any() else np.nan\n\n        # Month\n        if self._safe_col(df, self.date_col) and df[self.date_col].notna().any():\n            df[\"invoice_month\"] = df[self.date_col].dt.to_period(\"M\").astype(str)\n        else:\n            df[\"invoice_month\"] = None\n\n        return df\n\n    # -------------------------\n    # Outputs\n    # -------------------------\n\n    def build_kpi_overview(self) -> DataFrame:\n        df = self._prep_invoices()\n        if df.empty:\n            return DataFrame(pd.DataFrame())\n\n        total_invoices = int(len(df))\n        total_revenue = float(df[\"revenue_net\"].sum()) if \"revenue_net\" in df.columns else np.nan\n        credits_count = int((df[\"_sign\"] < 0).sum()) if \"_sign\" in df.columns else 0\n        avg_invoice = float(df[\"revenue_net\"].mean()) if total_invoices else np.nan\n        median_invoice = float(df[\"revenue_net\"].median()) if total_invoices else np.nan\n\n        out = pd.DataFrame([{\n            \"invoices_count\": total_invoices,\n            \"credits_count\": credits_count,\n            \"revenue_total_net\": total_revenue,\n            \"avg_invoice_net\": avg_invoice,\n            \"median_invoice_net\": median_invoice,\n            \"unique_clients\": int(df[\"_company_key\"].nunique()) if \"_company_key\" in df.columns else np.nan,\n        }])\n        return DataFrame(out)\n\n    def build_revenue_by_month(self) -> DataFrame:\n        df = self._prep_invoices()\n        if df.empty or \"invoice_month\" not in df.columns or df[\"invoice_month\"].isna().all():\n            return DataFrame(pd.DataFrame(columns=[\"invoice_month\", \"revenue_total_net\", \"invoices_count\"]))\n\n        g = df.groupby(\"invoice_month\", dropna=False)\n        out = pd.DataFrame({\n            \"invoice_month\": g.size().index.astype(str),\n            \"invoices_count\": g.size().values,\n            \"revenue_total_net\": g[\"revenue_net\"].sum().values,\n        })\n\n        if \"product_net\" in df.columns and df[\"product_net\"].notna().any():\n            out[\"product_total_net\"] = g[\"product_net\"].sum().values\n        if \"service_net\" in df.columns and df[\"service_net\"].notna().any():\n            out[\"service_total_net\"] = g[\"service_net\"].sum().values\n\n        out = out.sort_values(\"invoice_month\")\n        return DataFrame(out)\n\n    def build_revenue_by_client(self) -> DataFrame:\n        df = self._prep_invoices()\n        if df.empty:\n            return DataFrame(pd.DataFrame(columns=[\"company\", \"invoices_count\", \"revenue_total_net\", \"avg_invoice_net\"]))\n\n        g = df.groupby(\"_company_key\", dropna=False)\n        out = pd.DataFrame({\n            \"_company_key\": g.size().index.astype(str),\n            \"invoices_count\": g.size().values,\n            \"revenue_total_net\": g[\"revenue_net\"].sum().values,\n            \"avg_invoice_net\": g[\"revenue_net\"].mean().values,\n        })\n\n        # Representative display name\n        name_map = (\n            df.groupby(\"_company_key\")[\"_company\"]\n            .agg(lambda s: s.value_counts().index[0] if len(s.dropna()) else \"(unknown)\")\n            .rename(\"company\")\n            .reset_index()\n        )\n        out = out.merge(name_map, on=\"_company_key\", how=\"left\")\n\n        out = out.sort_values(\"revenue_total_net\", ascending=False).head(int(self.top_n))\n        out = out[[\"company\", \"invoices_count\", \"revenue_total_net\", \"avg_invoice_net\"]]\n        return DataFrame(out)\n\n    def build_revenue_by_type(self) -> DataFrame:\n        df = self._prep_invoices()\n        if df.empty:\n            return DataFrame(pd.DataFrame(columns=[\"invoice_type\", \"invoices_count\", \"revenue_total_net\", \"share\"]))\n\n        g = df.groupby(\"_type\", dropna=False)\n        out = pd.DataFrame({\n            \"invoice_type\": g.size().index.astype(str),\n            \"invoices_count\": g.size().values,\n            \"revenue_total_net\": g[\"revenue_net\"].sum().values,\n        })\n        total = float(out[\"revenue_total_net\"].abs().sum()) if len(out) else 0.0\n        out[\"share\"] = out[\"revenue_total_net\"].abs() / (total if total else np.nan)\n        out = out.sort_values(\"revenue_total_net\", ascending=False)\n        return DataFrame(out)\n\n    def build_revenue_concentration(self) -> DataFrame:\n        df = self._prep_invoices()\n        if df.empty:\n            return DataFrame(pd.DataFrame())\n\n        by = df.groupby(\"_company_key\", dropna=False)[\"revenue_net\"].sum().sort_values(ascending=False)\n        total = float(by.sum()) if len(by) else 0.0\n\n        def share_top(n: int) -> float:\n            if total == 0:\n                return np.nan\n            return float(by.head(n).sum() / total)\n\n        out = pd.DataFrame([{\n            \"clients_total\": int(by.shape[0]),\n            \"revenue_total_net\": total,\n            \"top_1_share\": share_top(1),\n            \"top_3_share\": share_top(3),\n            \"top_5_share\": share_top(5),\n            \"top_10_share\": share_top(10),\n        }])\n        return DataFrame(out)\n\n    def build_category_breakdown(self) -> DataFrame:\n        df = self._prep_invoices()\n        if df.empty or not self._safe_col(df, self.category_col):\n            return DataFrame(pd.DataFrame(columns=[\"category\", \"invoices_count\", \"revenue_total_net\", \"share\"]))\n\n        cat = df[self.category_col].fillna(\"(blank)\").astype(str).str.strip()\n        tmp = df.copy()\n        tmp[\"_category\"] = cat\n\n        g = tmp.groupby(\"_category\", dropna=False)\n        out = pd.DataFrame({\n            \"category\": g.size().index.astype(str),\n            \"invoices_count\": g.size().values,\n            \"revenue_total_net\": g[\"revenue_net\"].sum().values,\n        })\n        total = float(out[\"revenue_total_net\"].abs().sum()) if len(out) else 0.0\n        out[\"share\"] = out[\"revenue_total_net\"].abs() / (total if total else np.nan)\n        out = out.sort_values(\"revenue_total_net\", ascending=False)\n        return DataFrame(out)\n\n    def build_anomalies(self) -> DataFrame:\n        df = self._prep_invoices()\n        if df.empty:\n            return DataFrame(pd.DataFrame(columns=[\"date\", \"company\", \"invoice_type\", \"revenue_net\", \"reference\"]))\n\n        # Large invoices\n        x = df[\"revenue_net\"].replace([np.inf, -np.inf], np.nan).dropna()\n        if len(x) < 10:\n            thresh = x.abs().quantile(0.90) if len(x) else np.nan\n        else:\n            thresh = x.abs().quantile(0.95)\n\n        an = df[df[\"revenue_net\"].abs() >= float(thresh) if pd.notna(thresh) else False].copy()\n        an = an.sort_values(\"revenue_net\", ascending=False).head(int(self.anomaly_top_n))\n\n        # Friendly columns\n        out = pd.DataFrame({\n            \"date\": an[self.date_col] if self._safe_col(an, self.date_col) else pd.NaT,\n            \"company\": an[\"_company\"],\n            \"invoice_type\": an[\"_type\"],\n            \"revenue_net\": an[\"revenue_net\"],\n            \"reference\": an[\"_reference\"],\n        })\n        return DataFrame(out)\n\n    # -------------------------\n    # Insights context\n    # -------------------------\n\n    def build_insights_context(self) -> Message:\n        kpi = pd.DataFrame(self.build_kpi_overview())\n        by_month = pd.DataFrame(self.build_revenue_by_month())\n        by_client = pd.DataFrame(self.build_revenue_by_client())\n        by_type = pd.DataFrame(self.build_revenue_by_type())\n        conc = pd.DataFrame(self.build_revenue_concentration())\n        cats = pd.DataFrame(self.build_category_breakdown())\n        anom = pd.DataFrame(self.build_anomalies())\n\n        def fmt_money(x) -> str:\n            return \"n/a\" if pd.isna(x) else f\"${float(x):,.2f}\"\n\n        def fmt_pct(x) -> str:\n            return \"n/a\" if pd.isna(x) else f\"{float(x) * 100:.1f}%\"\n\n        lines: List[str] = []\n        lines.append(\"INVOICE DATASET OVERVIEW\")\n        if not kpi.empty:\n            r = kpi.iloc[0].to_dict()\n            lines.append(f\"invoices_count={r.get('invoices_count')}\")\n            lines.append(f\"unique_clients={r.get('unique_clients')}\")\n            lines.append(f\"credits_count={r.get('credits_count')}\")\n            lines.append(f\"revenue_total_net={fmt_money(r.get('revenue_total_net'))}\")\n            lines.append(f\"avg_invoice_net={fmt_money(r.get('avg_invoice_net'))}\")\n            lines.append(f\"median_invoice_net={fmt_money(r.get('median_invoice_net'))}\")\n        lines.append(\"\")\n\n        lines.append(\"REVENUE CONCENTRATION\")\n        if not conc.empty:\n            r = conc.iloc[0].to_dict()\n            lines.append(f\"top_1_share={fmt_pct(r.get('top_1_share'))}\")\n            lines.append(f\"top_3_share={fmt_pct(r.get('top_3_share'))}\")\n            lines.append(f\"top_10_share={fmt_pct(r.get('top_10_share'))}\")\n        lines.append(\"\")\n\n        lines.append(\"TOP CLIENTS BY REVENUE (net)\")\n        if by_client.empty:\n            lines.append(\"  (no data)\\n\")\n        else:\n            for _, row in by_client.head(min(len(by_client), int(self.top_n))).iterrows():\n                lines.append(f\"  - {row.get('company')}: revenue_total_net={fmt_money(row.get('revenue_total_net'))}, invoices={int(row.get('invoices_count'))}\")\n\n        lines.append(\"\")\n        lines.append(\"REVENUE BY INVOICE TYPE\")\n        if by_type.empty:\n            lines.append(\"  (no data)\\n\")\n        else:\n            for _, row in by_type.iterrows():\n                lines.append(f\"  - {row.get('invoice_type')}: revenue_total_net={fmt_money(row.get('revenue_total_net'))}, share={fmt_pct(row.get('share'))}\")\n\n        if not cats.empty and (cats.shape[0] > 0) and (cats[\"category\"].notna().any()):\n            lines.append(\"\")\n            lines.append(\"CATEGORY BREAKDOWN\")\n            for _, row in cats.iterrows():\n                lines.append(f\"  - {row.get('category')}: revenue_total_net={fmt_money(row.get('revenue_total_net'))}, share={fmt_pct(row.get('share'))}\")\n\n        # Seasonality note\n        if not by_month.empty and \"revenue_total_net\" in by_month.columns:\n            lines.append(\"\")\n            peak = by_month.sort_values(\"revenue_total_net\", ascending=False).head(1)\n            trough = by_month.sort_values(\"revenue_total_net\", ascending=True).head(1)\n            if len(peak) == 1:\n                lines.append(f\"PEAK MONTH: {peak.iloc[0].get('invoice_month')} revenue_total_net={fmt_money(peak.iloc[0].get('revenue_total_net'))}\")\n            if len(trough) == 1:\n                lines.append(f\"LOW MONTH: {trough.iloc[0].get('invoice_month')} revenue_total_net={fmt_money(trough.iloc[0].get('revenue_total_net'))}\")\n\n        # Anomalies\n        lines.append(\"\")\n        lines.append(\"NOTABLE LARGE INVOICES / CREDIT EVENTS\")\n        if anom.empty:\n            lines.append(\"  (no large anomalies detected)\\n\")\n        else:\n            for _, row in anom.iterrows():\n                dt = row.get(\"date\")\n                dt_s = \"n/a\" if pd.isna(dt) else str(pd.to_datetime(dt).date())\n                ref = str(row.get(\"reference\") or \"\").strip()\n                ref = (ref[:90] + \"…\") if len(ref) > 90 else ref\n                lines.append(f\"  - {dt_s} {row.get('company')} {row.get('invoice_type')}: revenue_net={fmt_money(row.get('revenue_net'))} ref={ref}\")\n\n        if bool(self.show_debug):\n            lines.append(\"\")\n            lines.append(self.build_data_diagnostics().text)\n\n        return Message(text=\"\\n\".join(lines))\n\n    # -------------------------\n    # Diagnostics\n    # -------------------------\n\n    def build_data_diagnostics(self) -> Message:\n        df = self._unwrap_to_df(self.invoices_df)\n        lines: List[str] = []\n        lines.append(\"DATA DIAGNOSTICS\")\n        lines.append(f\"invoices_rows={len(df)} cols={len(df.columns)}\")\n        lines.append(\"columns_sample=\" + str(list(df.columns)[:40]))\n        for c in [self.date_col, self.company_col, self.invoice_total_col, self.invoice_type_col, self.status_col, self.reference_col, self.category_col]:\n            if c and c in df.columns:\n                lines.append(f\"col_present: {c}=true\")\n            else:\n                lines.append(f\"col_present: {c}=false\")\n        return Message(text=\"\\n\".join(lines))"
              },
              "company_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Company column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "company_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Company Name"
              },
              "credit_type_values": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Credit memo type values (comma-separated)",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "credit_type_values",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Credit Memo,Credit,CM"
              },
              "date_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Invoice date column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "date_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Date"
              },
              "exclude_non_posted": {
                "_input_type": "BoolInput",
                "advanced": false,
                "display_name": "Exclude non-posted invoices",
                "dynamic": false,
                "info": "",
                "list": false,
                "list_add_label": "Add More",
                "name": "exclude_non_posted",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "bool",
                "value": false
              },
              "invoice_total_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Invoice total column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "invoice_total_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Invoice Total"
              },
              "invoice_type_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Invoice type column (optional)",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "invoice_type_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Invoice Type"
              },
              "invoices_df": {
                "_input_type": "DataFrameInput",
                "advanced": false,
                "display_name": "Invoices (Clean) DataFrame",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "DataFrame"
                ],
                "list": false,
                "list_add_label": "Add More",
                "name": "invoices_df",
                "placeholder": "",
                "required": true,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "other",
                "value": ""
              },
              "posted_status_values": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Posted status values (comma-separated)",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "posted_status_values",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Posted,Paid,Closed"
              },
              "product_total_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Product total column (optional)",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "product_total_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Product Total"
              },
              "reference_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Invoice reference/summary column (optional)",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "reference_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Invoice Reference"
              },
              "service_total_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Service total column (optional)",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "service_total_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Service Total"
              },
              "show_debug": {
                "_input_type": "BoolInput",
                "advanced": false,
                "display_name": "Include debug details in insights output",
                "dynamic": false,
                "info": "",
                "list": false,
                "list_add_label": "Add More",
                "name": "show_debug",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "bool",
                "value": false
              },
              "status_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Status column (optional)",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "status_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Status"
              },
              "tax_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Sales tax column (optional)",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "tax_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Sales Tax"
              },
              "top_n": {
                "_input_type": "IntInput",
                "advanced": false,
                "display_name": "Top N rows",
                "dynamic": false,
                "info": "",
                "list": false,
                "list_add_label": "Add More",
                "name": "top_n",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "int",
                "value": 15
              }
            },
            "tool_mode": false
          },
          "selected_output": "anomalies",
          "showNode": true,
          "type": "msp_invoice_metrics_builder"
        },
        "dragging": false,
        "id": "msp_invoice_metrics_builder-4eg0q",
        "measured": {
          "height": 1429,
          "width": 320
        },
        "position": {
          "x": 1091.837420718309,
          "y": -2127.187241482592
        },
        "selected": false,
        "type": "genericNode"
      },
      {
        "data": {
          "id": "msp_invoice_metrics_builder-66D4o",
          "node": {
            "base_classes": [
              "DataFrame",
              "Message"
            ],
            "beta": false,
            "conditional_paths": [],
            "custom_fields": {},
            "description": "Build invoice KPIs and rollups (month, client, type, optional category) and a compact insights context for an LLM.",
            "display_name": "MSP Invoice Metrics Builder",
            "documentation": "",
            "edited": true,
            "field_order": [
              "invoices_df",
              "date_col",
              "company_col",
              "invoice_total_col",
              "product_total_col",
              "service_total_col",
              "tax_col",
              "invoice_type_col",
              "status_col",
              "reference_col",
              "category_col",
              "exclude_non_posted",
              "posted_status_values",
              "credit_type_values",
              "top_n",
              "anomaly_top_n",
              "show_debug"
            ],
            "frozen": false,
            "icon": "BarChart3",
            "legacy": false,
            "lf_version": "1.6.0",
            "metadata": {},
            "minimized": false,
            "output_types": [],
            "outputs": [
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "KPI Overview",
                "group_outputs": false,
                "hidden": null,
                "method": "build_kpi_overview",
                "name": "kpi_overview",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Revenue by Month",
                "group_outputs": false,
                "hidden": null,
                "method": "build_revenue_by_month",
                "name": "revenue_by_month",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Revenue by Client",
                "group_outputs": false,
                "hidden": null,
                "method": "build_revenue_by_client",
                "name": "revenue_by_client",
                "options": null,
                "required_inputs": null,
                "selected": "DataFrame",
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Revenue by Invoice Type",
                "group_outputs": false,
                "hidden": null,
                "method": "build_revenue_by_type",
                "name": "revenue_by_type",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Revenue Concentration",
                "group_outputs": false,
                "hidden": null,
                "method": "build_revenue_concentration",
                "name": "revenue_concentration",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Category Breakdown (Optional)",
                "group_outputs": false,
                "hidden": null,
                "method": "build_category_breakdown",
                "name": "category_breakdown",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Anomalies (Large Invoices / Spikes)",
                "group_outputs": false,
                "hidden": null,
                "method": "build_anomalies",
                "name": "anomalies",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Insights Context (for LLM)",
                "group_outputs": false,
                "hidden": null,
                "method": "build_insights_context",
                "name": "insights_context",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "Message"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Data Diagnostics",
                "group_outputs": false,
                "hidden": null,
                "method": "build_data_diagnostics",
                "name": "data_diagnostics",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "Message"
                ],
                "value": "__UNDEFINED__"
              }
            ],
            "pinned": false,
            "template": {
              "_type": "Component",
              "anomaly_top_n": {
                "_input_type": "IntInput",
                "advanced": false,
                "display_name": "Top N anomalies",
                "dynamic": false,
                "info": "",
                "list": false,
                "list_add_label": "Add More",
                "name": "anomaly_top_n",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "int",
                "value": 8
              },
              "category_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Category column (optional)",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "category_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Category"
              },
              "code": {
                "advanced": true,
                "dynamic": true,
                "fileTypes": [],
                "file_path": "",
                "info": "",
                "list": false,
                "load_from_db": false,
                "multiline": true,
                "name": "code",
                "password": false,
                "placeholder": "",
                "required": true,
                "show": true,
                "title_case": false,
                "type": "code",
                "value": "from __future__ import annotations\n\nfrom typing import List, Dict, Any, Optional\nimport pandas as pd\nimport numpy as np\n\nfrom langflow.custom import Component\nfrom langflow.io import DataFrameInput, MessageTextInput, BoolInput, IntInput, Output\nfrom langflow.schema import DataFrame\nfrom langflow.schema.message import Message\n\n\nclass MSPInvoiceMetricsBuilder(Component):\n    display_name = \"MSP Invoice Metrics Builder\"\n    description = \"Build invoice KPIs and rollups (month, client, type, optional category) and a compact insights context for an LLM.\"\n    name = \"msp_invoice_metrics_builder\"\n    icon = \"BarChart3\"\n\n    inputs = [\n        DataFrameInput(name=\"invoices_df\", display_name=\"Invoices (Clean) DataFrame\", required=True),\n\n        # Column mappings (adjust to match your export)\n        MessageTextInput(name=\"date_col\", display_name=\"Invoice date column\", value=\"Date\"),\n        MessageTextInput(name=\"company_col\", display_name=\"Company column\", value=\"Company Name\"),\n        MessageTextInput(name=\"invoice_total_col\", display_name=\"Invoice total column\", value=\"Invoice Total\"),\n        MessageTextInput(name=\"product_total_col\", display_name=\"Product total column (optional)\", value=\"Product Total\"),\n        MessageTextInput(name=\"service_total_col\", display_name=\"Service total column (optional)\", value=\"Service Total\"),\n        MessageTextInput(name=\"tax_col\", display_name=\"Sales tax column (optional)\", value=\"Sales Tax\"),\n        MessageTextInput(name=\"invoice_type_col\", display_name=\"Invoice type column (optional)\", value=\"Invoice Type\"),\n        MessageTextInput(name=\"status_col\", display_name=\"Status column (optional)\", value=\"Status\"),\n        MessageTextInput(name=\"reference_col\", display_name=\"Invoice reference/summary column (optional)\", value=\"Invoice Reference\"),\n\n        # Optional categorization column if your ETL adds one\n        MessageTextInput(name=\"category_col\", display_name=\"Category column (optional)\", value=\"Category\"),\n\n        # Filters and semantics\n        BoolInput(name=\"exclude_non_posted\", display_name=\"Exclude non-posted invoices\", value=True),\n        MessageTextInput(\n            name=\"posted_status_values\",\n            display_name=\"Posted status values (comma-separated)\",\n            value=\"Posted,Paid,Closed\",\n        ),\n        MessageTextInput(\n            name=\"credit_type_values\",\n            display_name=\"Credit memo type values (comma-separated)\",\n            value=\"Credit Memo,Credit,CM\",\n        ),\n\n        IntInput(name=\"top_n\", display_name=\"Top N rows\", value=15),\n        IntInput(name=\"anomaly_top_n\", display_name=\"Top N anomalies\", value=8),\n        BoolInput(name=\"show_debug\", display_name=\"Include debug details in insights output\", value=False),\n    ]\n\n    outputs = [\n        Output(name=\"kpi_overview\", display_name=\"KPI Overview\", method=\"build_kpi_overview\"),\n        Output(name=\"revenue_by_month\", display_name=\"Revenue by Month\", method=\"build_revenue_by_month\"),\n        Output(name=\"revenue_by_client\", display_name=\"Revenue by Client\", method=\"build_revenue_by_client\"),\n        Output(name=\"revenue_by_type\", display_name=\"Revenue by Invoice Type\", method=\"build_revenue_by_type\"),\n        Output(name=\"revenue_concentration\", display_name=\"Revenue Concentration\", method=\"build_revenue_concentration\"),\n        Output(name=\"category_breakdown\", display_name=\"Category Breakdown (Optional)\", method=\"build_category_breakdown\"),\n        Output(name=\"anomalies\", display_name=\"Anomalies (Large Invoices / Spikes)\", method=\"build_anomalies\"),\n        Output(name=\"insights_context\", display_name=\"Insights Context (for LLM)\", method=\"build_insights_context\"),\n        Output(name=\"data_diagnostics\", display_name=\"Data Diagnostics\", method=\"build_data_diagnostics\"),\n    ]\n\n    # -------------------------\n    # Helpers\n    # -------------------------\n\n    def _safe_col(self, df: pd.DataFrame, col: str) -> bool:\n        return col is not None and str(col).strip() != \"\" and col in df.columns\n\n    def _unwrap_to_df(self, obj: Any) -> pd.DataFrame:\n        if obj is None:\n            return pd.DataFrame()\n\n        if isinstance(obj, pd.DataFrame):\n            df = obj.copy()\n            df.columns = [str(c).replace(\"\\ufeff\", \"\").strip() for c in df.columns]\n            return df\n\n        if hasattr(obj, \"data\"):\n            d = getattr(obj, \"data\")\n            if isinstance(d, pd.DataFrame):\n                df = d.copy()\n                df.columns = [str(c).replace(\"\\ufeff\", \"\").strip() for c in df.columns]\n                return df\n            if isinstance(d, list):\n                df = pd.DataFrame(d)\n                df.columns = [str(c).replace(\"\\ufeff\", \"\").strip() for c in df.columns]\n                return df\n            if isinstance(d, dict):\n                for k in [\"data\", \"rows\", \"records\", \"items\"]:\n                    if k in d and isinstance(d[k], list):\n                        df = pd.DataFrame(d[k])\n                        df.columns = [str(c).replace(\"\\ufeff\", \"\").strip() for c in df.columns]\n                        return df\n\n        if isinstance(obj, dict):\n            for k in [\"data\", \"rows\", \"records\", \"items\"]:\n                if k in obj and isinstance(obj[k], list):\n                    df = pd.DataFrame(obj[k])\n                    df.columns = [str(c).replace(\"\\ufeff\", \"\").strip() for c in df.columns]\n                    return df\n            df = pd.DataFrame(obj)\n            df.columns = [str(c).replace(\"\\ufeff\", \"\").strip() for c in df.columns]\n            return df\n\n        if isinstance(obj, list):\n            df = pd.DataFrame(obj)\n            df.columns = [str(c).replace(\"\\ufeff\", \"\").strip() for c in df.columns]\n            return df\n\n        df = pd.DataFrame(obj)\n        df.columns = [str(c).replace(\"\\ufeff\", \"\").strip() for c in df.columns]\n        return df\n\n    def _to_dt(self, s: pd.Series) -> pd.Series:\n        return pd.to_datetime(s, errors=\"coerce\")\n\n    def _to_money(self, s: pd.Series) -> pd.Series:\n        x = s.astype(str).str.strip()\n        x = x.str.replace(\"$\", \"\", regex=False).str.replace(\",\", \"\", regex=False)\n        x = x.str.replace(\"(\", \"-\", regex=False).str.replace(\")\", \"\", regex=False)\n        return pd.to_numeric(x, errors=\"coerce\")\n\n    def _company_key(self, s: pd.Series) -> pd.Series:\n        x = s.fillna(\"\").astype(str).str.lower().str.strip()\n        x = x.str.replace(r\"[^\\w\\s]\", \" \", regex=True)\n        x = x.str.replace(r\"\\b(inc|incorporated|llc|l\\.l\\.c|ltd|corp|corporation|co|company)\\b\", \" \", regex=True)\n        x = x.str.replace(r\"\\s+\", \" \", regex=True).str.strip()\n        return x\n\n    def _parse_csv_list(self, s: str) -> List[str]:\n        if s is None:\n            return []\n        return [p.strip() for p in str(s).split(\",\") if p.strip()]\n\n    def _prep_invoices(self) -> pd.DataFrame:\n        df = self._unwrap_to_df(self.invoices_df)\n        if df.empty:\n            return df\n\n        # Date\n        if self._safe_col(df, self.date_col):\n            df[self.date_col] = self._to_dt(df[self.date_col])\n\n        # Core money fields\n        if self._safe_col(df, self.invoice_total_col):\n            df[\"_invoice_total\"] = self._to_money(df[self.invoice_total_col]).fillna(0.0)\n        else:\n            df[\"_invoice_total\"] = np.nan\n\n        if self._safe_col(df, self.product_total_col):\n            df[\"_product_total\"] = self._to_money(df[self.product_total_col]).fillna(0.0)\n        else:\n            df[\"_product_total\"] = np.nan\n\n        if self._safe_col(df, self.service_total_col):\n            df[\"_service_total\"] = self._to_money(df[self.service_total_col]).fillna(0.0)\n        else:\n            df[\"_service_total\"] = np.nan\n\n        if self._safe_col(df, self.tax_col):\n            df[\"_tax_total\"] = self._to_money(df[self.tax_col]).fillna(0.0)\n        else:\n            df[\"_tax_total\"] = np.nan\n\n        # Company normalization\n        if self._safe_col(df, self.company_col):\n            df[\"_company\"] = df[self.company_col].astype(str).str.strip()\n        else:\n            df[\"_company\"] = \"(unknown)\"\n        df[\"_company_key\"] = self._company_key(df[\"_company\"])\n\n        # Type, status, reference\n        df[\"_type\"] = df[self.invoice_type_col].astype(str).str.strip() if self._safe_col(df, self.invoice_type_col) else \"(unknown)\"\n        df[\"_status\"] = df[self.status_col].astype(str).str.strip() if self._safe_col(df, self.status_col) else \"(unknown)\"\n        df[\"_reference\"] = df[self.reference_col].astype(str).str.strip() if self._safe_col(df, self.reference_col) else \"\"\n\n        # Posted filter\n        if bool(self.exclude_non_posted) and self._safe_col(df, self.status_col):\n            allowed = set([x.lower() for x in self._parse_csv_list(self.posted_status_values)])\n            df = df[df[\"_status\"].astype(str).str.lower().isin(allowed)].copy()\n\n        # Credit memo sign\n        credit_vals = set([x.lower() for x in self._parse_csv_list(self.credit_type_values)])\n        is_credit = df[\"_type\"].astype(str).str.lower().isin(credit_vals)\n        df[\"_sign\"] = np.where(is_credit, -1.0, 1.0)\n\n        # Net revenue fields\n        df[\"revenue_net\"] = df[\"_invoice_total\"] * df[\"_sign\"]\n        df[\"product_net\"] = df[\"_product_total\"] * df[\"_sign\"] if df[\"_product_total\"].notna().any() else np.nan\n        df[\"service_net\"] = df[\"_service_total\"] * df[\"_sign\"] if df[\"_service_total\"].notna().any() else np.nan\n\n        # Month\n        if self._safe_col(df, self.date_col) and df[self.date_col].notna().any():\n            df[\"invoice_month\"] = df[self.date_col].dt.to_period(\"M\").astype(str)\n        else:\n            df[\"invoice_month\"] = None\n\n        return df\n\n    # -------------------------\n    # Outputs\n    # -------------------------\n\n    def build_kpi_overview(self) -> DataFrame:\n        df = self._prep_invoices()\n        if df.empty:\n            return DataFrame(pd.DataFrame())\n\n        total_invoices = int(len(df))\n        total_revenue = float(df[\"revenue_net\"].sum()) if \"revenue_net\" in df.columns else np.nan\n        credits_count = int((df[\"_sign\"] < 0).sum()) if \"_sign\" in df.columns else 0\n        avg_invoice = float(df[\"revenue_net\"].mean()) if total_invoices else np.nan\n        median_invoice = float(df[\"revenue_net\"].median()) if total_invoices else np.nan\n\n        out = pd.DataFrame([{\n            \"invoices_count\": total_invoices,\n            \"credits_count\": credits_count,\n            \"revenue_total_net\": total_revenue,\n            \"avg_invoice_net\": avg_invoice,\n            \"median_invoice_net\": median_invoice,\n            \"unique_clients\": int(df[\"_company_key\"].nunique()) if \"_company_key\" in df.columns else np.nan,\n        }])\n        return DataFrame(out)\n\n    def build_revenue_by_month(self) -> DataFrame:\n        df = self._prep_invoices()\n        if df.empty or \"invoice_month\" not in df.columns or df[\"invoice_month\"].isna().all():\n            return DataFrame(pd.DataFrame(columns=[\"invoice_month\", \"revenue_total_net\", \"invoices_count\"]))\n\n        g = df.groupby(\"invoice_month\", dropna=False)\n        out = pd.DataFrame({\n            \"invoice_month\": g.size().index.astype(str),\n            \"invoices_count\": g.size().values,\n            \"revenue_total_net\": g[\"revenue_net\"].sum().values,\n        })\n\n        if \"product_net\" in df.columns and df[\"product_net\"].notna().any():\n            out[\"product_total_net\"] = g[\"product_net\"].sum().values\n        if \"service_net\" in df.columns and df[\"service_net\"].notna().any():\n            out[\"service_total_net\"] = g[\"service_net\"].sum().values\n\n        out = out.sort_values(\"invoice_month\")\n        return DataFrame(out)\n\n    def build_revenue_by_client(self) -> DataFrame:\n        df = self._prep_invoices()\n        if df.empty:\n            return DataFrame(pd.DataFrame(columns=[\"company\", \"invoices_count\", \"revenue_total_net\", \"avg_invoice_net\"]))\n\n        g = df.groupby(\"_company_key\", dropna=False)\n        out = pd.DataFrame({\n            \"_company_key\": g.size().index.astype(str),\n            \"invoices_count\": g.size().values,\n            \"revenue_total_net\": g[\"revenue_net\"].sum().values,\n            \"avg_invoice_net\": g[\"revenue_net\"].mean().values,\n        })\n\n        # Representative display name\n        name_map = (\n            df.groupby(\"_company_key\")[\"_company\"]\n            .agg(lambda s: s.value_counts().index[0] if len(s.dropna()) else \"(unknown)\")\n            .rename(\"company\")\n            .reset_index()\n        )\n        out = out.merge(name_map, on=\"_company_key\", how=\"left\")\n\n        out = out.sort_values(\"revenue_total_net\", ascending=False).head(int(self.top_n))\n        out = out[[\"company\", \"invoices_count\", \"revenue_total_net\", \"avg_invoice_net\"]]\n        return DataFrame(out)\n\n    def build_revenue_by_type(self) -> DataFrame:\n        df = self._prep_invoices()\n        if df.empty:\n            return DataFrame(pd.DataFrame(columns=[\"invoice_type\", \"invoices_count\", \"revenue_total_net\", \"share\"]))\n\n        g = df.groupby(\"_type\", dropna=False)\n        out = pd.DataFrame({\n            \"invoice_type\": g.size().index.astype(str),\n            \"invoices_count\": g.size().values,\n            \"revenue_total_net\": g[\"revenue_net\"].sum().values,\n        })\n        total = float(out[\"revenue_total_net\"].abs().sum()) if len(out) else 0.0\n        out[\"share\"] = out[\"revenue_total_net\"].abs() / (total if total else np.nan)\n        out = out.sort_values(\"revenue_total_net\", ascending=False)\n        return DataFrame(out)\n\n    def build_revenue_concentration(self) -> DataFrame:\n        df = self._prep_invoices()\n        if df.empty:\n            return DataFrame(pd.DataFrame())\n\n        by = df.groupby(\"_company_key\", dropna=False)[\"revenue_net\"].sum().sort_values(ascending=False)\n        total = float(by.sum()) if len(by) else 0.0\n\n        def share_top(n: int) -> float:\n            if total == 0:\n                return np.nan\n            return float(by.head(n).sum() / total)\n\n        out = pd.DataFrame([{\n            \"clients_total\": int(by.shape[0]),\n            \"revenue_total_net\": total,\n            \"top_1_share\": share_top(1),\n            \"top_3_share\": share_top(3),\n            \"top_5_share\": share_top(5),\n            \"top_10_share\": share_top(10),\n        }])\n        return DataFrame(out)\n\n    def build_category_breakdown(self) -> DataFrame:\n        df = self._prep_invoices()\n        if df.empty or not self._safe_col(df, self.category_col):\n            return DataFrame(pd.DataFrame(columns=[\"category\", \"invoices_count\", \"revenue_total_net\", \"share\"]))\n\n        cat = df[self.category_col].fillna(\"(blank)\").astype(str).str.strip()\n        tmp = df.copy()\n        tmp[\"_category\"] = cat\n\n        g = tmp.groupby(\"_category\", dropna=False)\n        out = pd.DataFrame({\n            \"category\": g.size().index.astype(str),\n            \"invoices_count\": g.size().values,\n            \"revenue_total_net\": g[\"revenue_net\"].sum().values,\n        })\n        total = float(out[\"revenue_total_net\"].abs().sum()) if len(out) else 0.0\n        out[\"share\"] = out[\"revenue_total_net\"].abs() / (total if total else np.nan)\n        out = out.sort_values(\"revenue_total_net\", ascending=False)\n        return DataFrame(out)\n\n    def build_anomalies(self) -> DataFrame:\n        df = self._prep_invoices()\n        if df.empty:\n            return DataFrame(pd.DataFrame(columns=[\"date\", \"company\", \"invoice_type\", \"revenue_net\", \"reference\"]))\n\n        # Large invoices\n        x = df[\"revenue_net\"].replace([np.inf, -np.inf], np.nan).dropna()\n        if len(x) < 10:\n            thresh = x.abs().quantile(0.90) if len(x) else np.nan\n        else:\n            thresh = x.abs().quantile(0.95)\n\n        an = df[df[\"revenue_net\"].abs() >= float(thresh) if pd.notna(thresh) else False].copy()\n        an = an.sort_values(\"revenue_net\", ascending=False).head(int(self.anomaly_top_n))\n\n        # Friendly columns\n        out = pd.DataFrame({\n            \"date\": an[self.date_col] if self._safe_col(an, self.date_col) else pd.NaT,\n            \"company\": an[\"_company\"],\n            \"invoice_type\": an[\"_type\"],\n            \"revenue_net\": an[\"revenue_net\"],\n            \"reference\": an[\"_reference\"],\n        })\n        return DataFrame(out)\n\n    # -------------------------\n    # Insights context\n    # -------------------------\n\n    def build_insights_context(self) -> Message:\n        kpi = pd.DataFrame(self.build_kpi_overview())\n        by_month = pd.DataFrame(self.build_revenue_by_month())\n        by_client = pd.DataFrame(self.build_revenue_by_client())\n        by_type = pd.DataFrame(self.build_revenue_by_type())\n        conc = pd.DataFrame(self.build_revenue_concentration())\n        cats = pd.DataFrame(self.build_category_breakdown())\n        anom = pd.DataFrame(self.build_anomalies())\n\n        def fmt_money(x) -> str:\n            return \"n/a\" if pd.isna(x) else f\"${float(x):,.2f}\"\n\n        def fmt_pct(x) -> str:\n            return \"n/a\" if pd.isna(x) else f\"{float(x) * 100:.1f}%\"\n\n        lines: List[str] = []\n        lines.append(\"INVOICE DATASET OVERVIEW\")\n        if not kpi.empty:\n            r = kpi.iloc[0].to_dict()\n            lines.append(f\"invoices_count={r.get('invoices_count')}\")\n            lines.append(f\"unique_clients={r.get('unique_clients')}\")\n            lines.append(f\"credits_count={r.get('credits_count')}\")\n            lines.append(f\"revenue_total_net={fmt_money(r.get('revenue_total_net'))}\")\n            lines.append(f\"avg_invoice_net={fmt_money(r.get('avg_invoice_net'))}\")\n            lines.append(f\"median_invoice_net={fmt_money(r.get('median_invoice_net'))}\")\n        lines.append(\"\")\n\n        lines.append(\"REVENUE CONCENTRATION\")\n        if not conc.empty:\n            r = conc.iloc[0].to_dict()\n            lines.append(f\"top_1_share={fmt_pct(r.get('top_1_share'))}\")\n            lines.append(f\"top_3_share={fmt_pct(r.get('top_3_share'))}\")\n            lines.append(f\"top_10_share={fmt_pct(r.get('top_10_share'))}\")\n        lines.append(\"\")\n\n        lines.append(\"TOP CLIENTS BY REVENUE (net)\")\n        if by_client.empty:\n            lines.append(\"  (no data)\\n\")\n        else:\n            for _, row in by_client.head(min(len(by_client), int(self.top_n))).iterrows():\n                lines.append(f\"  - {row.get('company')}: revenue_total_net={fmt_money(row.get('revenue_total_net'))}, invoices={int(row.get('invoices_count'))}\")\n\n        lines.append(\"\")\n        lines.append(\"REVENUE BY INVOICE TYPE\")\n        if by_type.empty:\n            lines.append(\"  (no data)\\n\")\n        else:\n            for _, row in by_type.iterrows():\n                lines.append(f\"  - {row.get('invoice_type')}: revenue_total_net={fmt_money(row.get('revenue_total_net'))}, share={fmt_pct(row.get('share'))}\")\n\n        if not cats.empty and (cats.shape[0] > 0) and (cats[\"category\"].notna().any()):\n            lines.append(\"\")\n            lines.append(\"CATEGORY BREAKDOWN\")\n            for _, row in cats.iterrows():\n                lines.append(f\"  - {row.get('category')}: revenue_total_net={fmt_money(row.get('revenue_total_net'))}, share={fmt_pct(row.get('share'))}\")\n\n        # Seasonality note\n        if not by_month.empty and \"revenue_total_net\" in by_month.columns:\n            lines.append(\"\")\n            peak = by_month.sort_values(\"revenue_total_net\", ascending=False).head(1)\n            trough = by_month.sort_values(\"revenue_total_net\", ascending=True).head(1)\n            if len(peak) == 1:\n                lines.append(f\"PEAK MONTH: {peak.iloc[0].get('invoice_month')} revenue_total_net={fmt_money(peak.iloc[0].get('revenue_total_net'))}\")\n            if len(trough) == 1:\n                lines.append(f\"LOW MONTH: {trough.iloc[0].get('invoice_month')} revenue_total_net={fmt_money(trough.iloc[0].get('revenue_total_net'))}\")\n\n        # Anomalies\n        lines.append(\"\")\n        lines.append(\"NOTABLE LARGE INVOICES / CREDIT EVENTS\")\n        if anom.empty:\n            lines.append(\"  (no large anomalies detected)\\n\")\n        else:\n            for _, row in anom.iterrows():\n                dt = row.get(\"date\")\n                dt_s = \"n/a\" if pd.isna(dt) else str(pd.to_datetime(dt).date())\n                ref = str(row.get(\"reference\") or \"\").strip()\n                ref = (ref[:90] + \"…\") if len(ref) > 90 else ref\n                lines.append(f\"  - {dt_s} {row.get('company')} {row.get('invoice_type')}: revenue_net={fmt_money(row.get('revenue_net'))} ref={ref}\")\n\n        if bool(self.show_debug):\n            lines.append(\"\")\n            lines.append(self.build_data_diagnostics().text)\n\n        return Message(text=\"\\n\".join(lines))\n\n    # -------------------------\n    # Diagnostics\n    # -------------------------\n\n    def build_data_diagnostics(self) -> Message:\n        df = self._unwrap_to_df(self.invoices_df)\n        lines: List[str] = []\n        lines.append(\"DATA DIAGNOSTICS\")\n        lines.append(f\"invoices_rows={len(df)} cols={len(df.columns)}\")\n        lines.append(\"columns_sample=\" + str(list(df.columns)[:40]))\n        for c in [self.date_col, self.company_col, self.invoice_total_col, self.invoice_type_col, self.status_col, self.reference_col, self.category_col]:\n            if c and c in df.columns:\n                lines.append(f\"col_present: {c}=true\")\n            else:\n                lines.append(f\"col_present: {c}=false\")\n        return Message(text=\"\\n\".join(lines))"
              },
              "company_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Company column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "company_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Company Name"
              },
              "credit_type_values": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Credit memo type values (comma-separated)",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "credit_type_values",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Credit Memo,Credit,CM"
              },
              "date_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Invoice date column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "date_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Date"
              },
              "exclude_non_posted": {
                "_input_type": "BoolInput",
                "advanced": false,
                "display_name": "Exclude non-posted invoices",
                "dynamic": false,
                "info": "",
                "list": false,
                "list_add_label": "Add More",
                "name": "exclude_non_posted",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "bool",
                "value": false
              },
              "invoice_total_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Invoice total column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "invoice_total_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Invoice Total"
              },
              "invoice_type_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Invoice type column (optional)",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "invoice_type_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Invoice Type"
              },
              "invoices_df": {
                "_input_type": "DataFrameInput",
                "advanced": false,
                "display_name": "Invoices (Clean) DataFrame",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "DataFrame"
                ],
                "list": false,
                "list_add_label": "Add More",
                "name": "invoices_df",
                "placeholder": "",
                "required": true,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "other",
                "value": ""
              },
              "posted_status_values": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Posted status values (comma-separated)",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "posted_status_values",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Posted,Paid,Closed"
              },
              "product_total_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Product total column (optional)",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "product_total_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Product Total"
              },
              "reference_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Invoice reference/summary column (optional)",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "reference_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Invoice Reference"
              },
              "service_total_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Service total column (optional)",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "service_total_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Service Total"
              },
              "show_debug": {
                "_input_type": "BoolInput",
                "advanced": false,
                "display_name": "Include debug details in insights output",
                "dynamic": false,
                "info": "",
                "list": false,
                "list_add_label": "Add More",
                "name": "show_debug",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "bool",
                "value": false
              },
              "status_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Status column (optional)",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "status_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Status"
              },
              "tax_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Sales tax column (optional)",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "tax_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Sales Tax"
              },
              "top_n": {
                "_input_type": "IntInput",
                "advanced": false,
                "display_name": "Top N rows",
                "dynamic": false,
                "info": "",
                "list": false,
                "list_add_label": "Add More",
                "name": "top_n",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "int",
                "value": 15
              }
            },
            "tool_mode": false
          },
          "selected_output": "revenue_by_client",
          "showNode": true,
          "type": "msp_invoice_metrics_builder"
        },
        "dragging": false,
        "id": "msp_invoice_metrics_builder-66D4o",
        "measured": {
          "height": 1429,
          "width": 320
        },
        "position": {
          "x": 1728.2425440718816,
          "y": -2352.20050391378
        },
        "selected": false,
        "type": "genericNode"
      },
      {
        "data": {
          "id": "msp_invoice_metrics_builder-8zhSm",
          "node": {
            "base_classes": [
              "DataFrame",
              "Message"
            ],
            "beta": false,
            "conditional_paths": [],
            "custom_fields": {},
            "description": "Build invoice KPIs and rollups (month, client, type, optional category) and a compact insights context for an LLM.",
            "display_name": "MSP Invoice Metrics Builder",
            "documentation": "",
            "edited": true,
            "field_order": [
              "invoices_df",
              "date_col",
              "company_col",
              "invoice_total_col",
              "product_total_col",
              "service_total_col",
              "tax_col",
              "invoice_type_col",
              "status_col",
              "reference_col",
              "category_col",
              "exclude_non_posted",
              "posted_status_values",
              "credit_type_values",
              "top_n",
              "anomaly_top_n",
              "show_debug"
            ],
            "frozen": false,
            "icon": "BarChart3",
            "legacy": false,
            "lf_version": "1.6.0",
            "metadata": {},
            "minimized": false,
            "output_types": [],
            "outputs": [
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "KPI Overview",
                "group_outputs": false,
                "hidden": null,
                "method": "build_kpi_overview",
                "name": "kpi_overview",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Revenue by Month",
                "group_outputs": false,
                "hidden": null,
                "method": "build_revenue_by_month",
                "name": "revenue_by_month",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Revenue by Client",
                "group_outputs": false,
                "hidden": null,
                "method": "build_revenue_by_client",
                "name": "revenue_by_client",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Revenue by Invoice Type",
                "group_outputs": false,
                "hidden": null,
                "method": "build_revenue_by_type",
                "name": "revenue_by_type",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Revenue Concentration",
                "group_outputs": false,
                "hidden": null,
                "method": "build_revenue_concentration",
                "name": "revenue_concentration",
                "options": null,
                "required_inputs": null,
                "selected": "DataFrame",
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Category Breakdown (Optional)",
                "group_outputs": false,
                "hidden": null,
                "method": "build_category_breakdown",
                "name": "category_breakdown",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Anomalies (Large Invoices / Spikes)",
                "group_outputs": false,
                "hidden": null,
                "method": "build_anomalies",
                "name": "anomalies",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Insights Context (for LLM)",
                "group_outputs": false,
                "hidden": null,
                "method": "build_insights_context",
                "name": "insights_context",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "Message"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Data Diagnostics",
                "group_outputs": false,
                "hidden": null,
                "method": "build_data_diagnostics",
                "name": "data_diagnostics",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "Message"
                ],
                "value": "__UNDEFINED__"
              }
            ],
            "pinned": false,
            "template": {
              "_type": "Component",
              "anomaly_top_n": {
                "_input_type": "IntInput",
                "advanced": false,
                "display_name": "Top N anomalies",
                "dynamic": false,
                "info": "",
                "list": false,
                "list_add_label": "Add More",
                "name": "anomaly_top_n",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "int",
                "value": 8
              },
              "category_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Category column (optional)",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "category_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Category"
              },
              "code": {
                "advanced": true,
                "dynamic": true,
                "fileTypes": [],
                "file_path": "",
                "info": "",
                "list": false,
                "load_from_db": false,
                "multiline": true,
                "name": "code",
                "password": false,
                "placeholder": "",
                "required": true,
                "show": true,
                "title_case": false,
                "type": "code",
                "value": "from __future__ import annotations\n\nfrom typing import List, Dict, Any, Optional\nimport pandas as pd\nimport numpy as np\n\nfrom langflow.custom import Component\nfrom langflow.io import DataFrameInput, MessageTextInput, BoolInput, IntInput, Output\nfrom langflow.schema import DataFrame\nfrom langflow.schema.message import Message\n\n\nclass MSPInvoiceMetricsBuilder(Component):\n    display_name = \"MSP Invoice Metrics Builder\"\n    description = \"Build invoice KPIs and rollups (month, client, type, optional category) and a compact insights context for an LLM.\"\n    name = \"msp_invoice_metrics_builder\"\n    icon = \"BarChart3\"\n\n    inputs = [\n        DataFrameInput(name=\"invoices_df\", display_name=\"Invoices (Clean) DataFrame\", required=True),\n\n        # Column mappings (adjust to match your export)\n        MessageTextInput(name=\"date_col\", display_name=\"Invoice date column\", value=\"Date\"),\n        MessageTextInput(name=\"company_col\", display_name=\"Company column\", value=\"Company Name\"),\n        MessageTextInput(name=\"invoice_total_col\", display_name=\"Invoice total column\", value=\"Invoice Total\"),\n        MessageTextInput(name=\"product_total_col\", display_name=\"Product total column (optional)\", value=\"Product Total\"),\n        MessageTextInput(name=\"service_total_col\", display_name=\"Service total column (optional)\", value=\"Service Total\"),\n        MessageTextInput(name=\"tax_col\", display_name=\"Sales tax column (optional)\", value=\"Sales Tax\"),\n        MessageTextInput(name=\"invoice_type_col\", display_name=\"Invoice type column (optional)\", value=\"Invoice Type\"),\n        MessageTextInput(name=\"status_col\", display_name=\"Status column (optional)\", value=\"Status\"),\n        MessageTextInput(name=\"reference_col\", display_name=\"Invoice reference/summary column (optional)\", value=\"Invoice Reference\"),\n\n        # Optional categorization column if your ETL adds one\n        MessageTextInput(name=\"category_col\", display_name=\"Category column (optional)\", value=\"Category\"),\n\n        # Filters and semantics\n        BoolInput(name=\"exclude_non_posted\", display_name=\"Exclude non-posted invoices\", value=True),\n        MessageTextInput(\n            name=\"posted_status_values\",\n            display_name=\"Posted status values (comma-separated)\",\n            value=\"Posted,Paid,Closed\",\n        ),\n        MessageTextInput(\n            name=\"credit_type_values\",\n            display_name=\"Credit memo type values (comma-separated)\",\n            value=\"Credit Memo,Credit,CM\",\n        ),\n\n        IntInput(name=\"top_n\", display_name=\"Top N rows\", value=15),\n        IntInput(name=\"anomaly_top_n\", display_name=\"Top N anomalies\", value=8),\n        BoolInput(name=\"show_debug\", display_name=\"Include debug details in insights output\", value=False),\n    ]\n\n    outputs = [\n        Output(name=\"kpi_overview\", display_name=\"KPI Overview\", method=\"build_kpi_overview\"),\n        Output(name=\"revenue_by_month\", display_name=\"Revenue by Month\", method=\"build_revenue_by_month\"),\n        Output(name=\"revenue_by_client\", display_name=\"Revenue by Client\", method=\"build_revenue_by_client\"),\n        Output(name=\"revenue_by_type\", display_name=\"Revenue by Invoice Type\", method=\"build_revenue_by_type\"),\n        Output(name=\"revenue_concentration\", display_name=\"Revenue Concentration\", method=\"build_revenue_concentration\"),\n        Output(name=\"category_breakdown\", display_name=\"Category Breakdown (Optional)\", method=\"build_category_breakdown\"),\n        Output(name=\"anomalies\", display_name=\"Anomalies (Large Invoices / Spikes)\", method=\"build_anomalies\"),\n        Output(name=\"insights_context\", display_name=\"Insights Context (for LLM)\", method=\"build_insights_context\"),\n        Output(name=\"data_diagnostics\", display_name=\"Data Diagnostics\", method=\"build_data_diagnostics\"),\n    ]\n\n    # -------------------------\n    # Helpers\n    # -------------------------\n\n    def _safe_col(self, df: pd.DataFrame, col: str) -> bool:\n        return col is not None and str(col).strip() != \"\" and col in df.columns\n\n    def _unwrap_to_df(self, obj: Any) -> pd.DataFrame:\n        if obj is None:\n            return pd.DataFrame()\n\n        if isinstance(obj, pd.DataFrame):\n            df = obj.copy()\n            df.columns = [str(c).replace(\"\\ufeff\", \"\").strip() for c in df.columns]\n            return df\n\n        if hasattr(obj, \"data\"):\n            d = getattr(obj, \"data\")\n            if isinstance(d, pd.DataFrame):\n                df = d.copy()\n                df.columns = [str(c).replace(\"\\ufeff\", \"\").strip() for c in df.columns]\n                return df\n            if isinstance(d, list):\n                df = pd.DataFrame(d)\n                df.columns = [str(c).replace(\"\\ufeff\", \"\").strip() for c in df.columns]\n                return df\n            if isinstance(d, dict):\n                for k in [\"data\", \"rows\", \"records\", \"items\"]:\n                    if k in d and isinstance(d[k], list):\n                        df = pd.DataFrame(d[k])\n                        df.columns = [str(c).replace(\"\\ufeff\", \"\").strip() for c in df.columns]\n                        return df\n\n        if isinstance(obj, dict):\n            for k in [\"data\", \"rows\", \"records\", \"items\"]:\n                if k in obj and isinstance(obj[k], list):\n                    df = pd.DataFrame(obj[k])\n                    df.columns = [str(c).replace(\"\\ufeff\", \"\").strip() for c in df.columns]\n                    return df\n            df = pd.DataFrame(obj)\n            df.columns = [str(c).replace(\"\\ufeff\", \"\").strip() for c in df.columns]\n            return df\n\n        if isinstance(obj, list):\n            df = pd.DataFrame(obj)\n            df.columns = [str(c).replace(\"\\ufeff\", \"\").strip() for c in df.columns]\n            return df\n\n        df = pd.DataFrame(obj)\n        df.columns = [str(c).replace(\"\\ufeff\", \"\").strip() for c in df.columns]\n        return df\n\n    def _to_dt(self, s: pd.Series) -> pd.Series:\n        return pd.to_datetime(s, errors=\"coerce\")\n\n    def _to_money(self, s: pd.Series) -> pd.Series:\n        x = s.astype(str).str.strip()\n        x = x.str.replace(\"$\", \"\", regex=False).str.replace(\",\", \"\", regex=False)\n        x = x.str.replace(\"(\", \"-\", regex=False).str.replace(\")\", \"\", regex=False)\n        return pd.to_numeric(x, errors=\"coerce\")\n\n    def _company_key(self, s: pd.Series) -> pd.Series:\n        x = s.fillna(\"\").astype(str).str.lower().str.strip()\n        x = x.str.replace(r\"[^\\w\\s]\", \" \", regex=True)\n        x = x.str.replace(r\"\\b(inc|incorporated|llc|l\\.l\\.c|ltd|corp|corporation|co|company)\\b\", \" \", regex=True)\n        x = x.str.replace(r\"\\s+\", \" \", regex=True).str.strip()\n        return x\n\n    def _parse_csv_list(self, s: str) -> List[str]:\n        if s is None:\n            return []\n        return [p.strip() for p in str(s).split(\",\") if p.strip()]\n\n    def _prep_invoices(self) -> pd.DataFrame:\n        df = self._unwrap_to_df(self.invoices_df)\n        if df.empty:\n            return df\n\n        # Date\n        if self._safe_col(df, self.date_col):\n            df[self.date_col] = self._to_dt(df[self.date_col])\n\n        # Core money fields\n        if self._safe_col(df, self.invoice_total_col):\n            df[\"_invoice_total\"] = self._to_money(df[self.invoice_total_col]).fillna(0.0)\n        else:\n            df[\"_invoice_total\"] = np.nan\n\n        if self._safe_col(df, self.product_total_col):\n            df[\"_product_total\"] = self._to_money(df[self.product_total_col]).fillna(0.0)\n        else:\n            df[\"_product_total\"] = np.nan\n\n        if self._safe_col(df, self.service_total_col):\n            df[\"_service_total\"] = self._to_money(df[self.service_total_col]).fillna(0.0)\n        else:\n            df[\"_service_total\"] = np.nan\n\n        if self._safe_col(df, self.tax_col):\n            df[\"_tax_total\"] = self._to_money(df[self.tax_col]).fillna(0.0)\n        else:\n            df[\"_tax_total\"] = np.nan\n\n        # Company normalization\n        if self._safe_col(df, self.company_col):\n            df[\"_company\"] = df[self.company_col].astype(str).str.strip()\n        else:\n            df[\"_company\"] = \"(unknown)\"\n        df[\"_company_key\"] = self._company_key(df[\"_company\"])\n\n        # Type, status, reference\n        df[\"_type\"] = df[self.invoice_type_col].astype(str).str.strip() if self._safe_col(df, self.invoice_type_col) else \"(unknown)\"\n        df[\"_status\"] = df[self.status_col].astype(str).str.strip() if self._safe_col(df, self.status_col) else \"(unknown)\"\n        df[\"_reference\"] = df[self.reference_col].astype(str).str.strip() if self._safe_col(df, self.reference_col) else \"\"\n\n        # Posted filter\n        if bool(self.exclude_non_posted) and self._safe_col(df, self.status_col):\n            allowed = set([x.lower() for x in self._parse_csv_list(self.posted_status_values)])\n            df = df[df[\"_status\"].astype(str).str.lower().isin(allowed)].copy()\n\n        # Credit memo sign\n        credit_vals = set([x.lower() for x in self._parse_csv_list(self.credit_type_values)])\n        is_credit = df[\"_type\"].astype(str).str.lower().isin(credit_vals)\n        df[\"_sign\"] = np.where(is_credit, -1.0, 1.0)\n\n        # Net revenue fields\n        df[\"revenue_net\"] = df[\"_invoice_total\"] * df[\"_sign\"]\n        df[\"product_net\"] = df[\"_product_total\"] * df[\"_sign\"] if df[\"_product_total\"].notna().any() else np.nan\n        df[\"service_net\"] = df[\"_service_total\"] * df[\"_sign\"] if df[\"_service_total\"].notna().any() else np.nan\n\n        # Month\n        if self._safe_col(df, self.date_col) and df[self.date_col].notna().any():\n            df[\"invoice_month\"] = df[self.date_col].dt.to_period(\"M\").astype(str)\n        else:\n            df[\"invoice_month\"] = None\n\n        return df\n\n    # -------------------------\n    # Outputs\n    # -------------------------\n\n    def build_kpi_overview(self) -> DataFrame:\n        df = self._prep_invoices()\n        if df.empty:\n            return DataFrame(pd.DataFrame())\n\n        total_invoices = int(len(df))\n        total_revenue = float(df[\"revenue_net\"].sum()) if \"revenue_net\" in df.columns else np.nan\n        credits_count = int((df[\"_sign\"] < 0).sum()) if \"_sign\" in df.columns else 0\n        avg_invoice = float(df[\"revenue_net\"].mean()) if total_invoices else np.nan\n        median_invoice = float(df[\"revenue_net\"].median()) if total_invoices else np.nan\n\n        out = pd.DataFrame([{\n            \"invoices_count\": total_invoices,\n            \"credits_count\": credits_count,\n            \"revenue_total_net\": total_revenue,\n            \"avg_invoice_net\": avg_invoice,\n            \"median_invoice_net\": median_invoice,\n            \"unique_clients\": int(df[\"_company_key\"].nunique()) if \"_company_key\" in df.columns else np.nan,\n        }])\n        return DataFrame(out)\n\n    def build_revenue_by_month(self) -> DataFrame:\n        df = self._prep_invoices()\n        if df.empty or \"invoice_month\" not in df.columns or df[\"invoice_month\"].isna().all():\n            return DataFrame(pd.DataFrame(columns=[\"invoice_month\", \"revenue_total_net\", \"invoices_count\"]))\n\n        g = df.groupby(\"invoice_month\", dropna=False)\n        out = pd.DataFrame({\n            \"invoice_month\": g.size().index.astype(str),\n            \"invoices_count\": g.size().values,\n            \"revenue_total_net\": g[\"revenue_net\"].sum().values,\n        })\n\n        if \"product_net\" in df.columns and df[\"product_net\"].notna().any():\n            out[\"product_total_net\"] = g[\"product_net\"].sum().values\n        if \"service_net\" in df.columns and df[\"service_net\"].notna().any():\n            out[\"service_total_net\"] = g[\"service_net\"].sum().values\n\n        out = out.sort_values(\"invoice_month\")\n        return DataFrame(out)\n\n    def build_revenue_by_client(self) -> DataFrame:\n        df = self._prep_invoices()\n        if df.empty:\n            return DataFrame(pd.DataFrame(columns=[\"company\", \"invoices_count\", \"revenue_total_net\", \"avg_invoice_net\"]))\n\n        g = df.groupby(\"_company_key\", dropna=False)\n        out = pd.DataFrame({\n            \"_company_key\": g.size().index.astype(str),\n            \"invoices_count\": g.size().values,\n            \"revenue_total_net\": g[\"revenue_net\"].sum().values,\n            \"avg_invoice_net\": g[\"revenue_net\"].mean().values,\n        })\n\n        # Representative display name\n        name_map = (\n            df.groupby(\"_company_key\")[\"_company\"]\n            .agg(lambda s: s.value_counts().index[0] if len(s.dropna()) else \"(unknown)\")\n            .rename(\"company\")\n            .reset_index()\n        )\n        out = out.merge(name_map, on=\"_company_key\", how=\"left\")\n\n        out = out.sort_values(\"revenue_total_net\", ascending=False).head(int(self.top_n))\n        out = out[[\"company\", \"invoices_count\", \"revenue_total_net\", \"avg_invoice_net\"]]\n        return DataFrame(out)\n\n    def build_revenue_by_type(self) -> DataFrame:\n        df = self._prep_invoices()\n        if df.empty:\n            return DataFrame(pd.DataFrame(columns=[\"invoice_type\", \"invoices_count\", \"revenue_total_net\", \"share\"]))\n\n        g = df.groupby(\"_type\", dropna=False)\n        out = pd.DataFrame({\n            \"invoice_type\": g.size().index.astype(str),\n            \"invoices_count\": g.size().values,\n            \"revenue_total_net\": g[\"revenue_net\"].sum().values,\n        })\n        total = float(out[\"revenue_total_net\"].abs().sum()) if len(out) else 0.0\n        out[\"share\"] = out[\"revenue_total_net\"].abs() / (total if total else np.nan)\n        out = out.sort_values(\"revenue_total_net\", ascending=False)\n        return DataFrame(out)\n\n    def build_revenue_concentration(self) -> DataFrame:\n        df = self._prep_invoices()\n        if df.empty:\n            return DataFrame(pd.DataFrame())\n\n        by = df.groupby(\"_company_key\", dropna=False)[\"revenue_net\"].sum().sort_values(ascending=False)\n        total = float(by.sum()) if len(by) else 0.0\n\n        def share_top(n: int) -> float:\n            if total == 0:\n                return np.nan\n            return float(by.head(n).sum() / total)\n\n        out = pd.DataFrame([{\n            \"clients_total\": int(by.shape[0]),\n            \"revenue_total_net\": total,\n            \"top_1_share\": share_top(1),\n            \"top_3_share\": share_top(3),\n            \"top_5_share\": share_top(5),\n            \"top_10_share\": share_top(10),\n        }])\n        return DataFrame(out)\n\n    def build_category_breakdown(self) -> DataFrame:\n        df = self._prep_invoices()\n        if df.empty or not self._safe_col(df, self.category_col):\n            return DataFrame(pd.DataFrame(columns=[\"category\", \"invoices_count\", \"revenue_total_net\", \"share\"]))\n\n        cat = df[self.category_col].fillna(\"(blank)\").astype(str).str.strip()\n        tmp = df.copy()\n        tmp[\"_category\"] = cat\n\n        g = tmp.groupby(\"_category\", dropna=False)\n        out = pd.DataFrame({\n            \"category\": g.size().index.astype(str),\n            \"invoices_count\": g.size().values,\n            \"revenue_total_net\": g[\"revenue_net\"].sum().values,\n        })\n        total = float(out[\"revenue_total_net\"].abs().sum()) if len(out) else 0.0\n        out[\"share\"] = out[\"revenue_total_net\"].abs() / (total if total else np.nan)\n        out = out.sort_values(\"revenue_total_net\", ascending=False)\n        return DataFrame(out)\n\n    def build_anomalies(self) -> DataFrame:\n        df = self._prep_invoices()\n        if df.empty:\n            return DataFrame(pd.DataFrame(columns=[\"date\", \"company\", \"invoice_type\", \"revenue_net\", \"reference\"]))\n\n        # Large invoices\n        x = df[\"revenue_net\"].replace([np.inf, -np.inf], np.nan).dropna()\n        if len(x) < 10:\n            thresh = x.abs().quantile(0.90) if len(x) else np.nan\n        else:\n            thresh = x.abs().quantile(0.95)\n\n        an = df[df[\"revenue_net\"].abs() >= float(thresh) if pd.notna(thresh) else False].copy()\n        an = an.sort_values(\"revenue_net\", ascending=False).head(int(self.anomaly_top_n))\n\n        # Friendly columns\n        out = pd.DataFrame({\n            \"date\": an[self.date_col] if self._safe_col(an, self.date_col) else pd.NaT,\n            \"company\": an[\"_company\"],\n            \"invoice_type\": an[\"_type\"],\n            \"revenue_net\": an[\"revenue_net\"],\n            \"reference\": an[\"_reference\"],\n        })\n        return DataFrame(out)\n\n    # -------------------------\n    # Insights context\n    # -------------------------\n\n    def build_insights_context(self) -> Message:\n        kpi = pd.DataFrame(self.build_kpi_overview())\n        by_month = pd.DataFrame(self.build_revenue_by_month())\n        by_client = pd.DataFrame(self.build_revenue_by_client())\n        by_type = pd.DataFrame(self.build_revenue_by_type())\n        conc = pd.DataFrame(self.build_revenue_concentration())\n        cats = pd.DataFrame(self.build_category_breakdown())\n        anom = pd.DataFrame(self.build_anomalies())\n\n        def fmt_money(x) -> str:\n            return \"n/a\" if pd.isna(x) else f\"${float(x):,.2f}\"\n\n        def fmt_pct(x) -> str:\n            return \"n/a\" if pd.isna(x) else f\"{float(x) * 100:.1f}%\"\n\n        lines: List[str] = []\n        lines.append(\"INVOICE DATASET OVERVIEW\")\n        if not kpi.empty:\n            r = kpi.iloc[0].to_dict()\n            lines.append(f\"invoices_count={r.get('invoices_count')}\")\n            lines.append(f\"unique_clients={r.get('unique_clients')}\")\n            lines.append(f\"credits_count={r.get('credits_count')}\")\n            lines.append(f\"revenue_total_net={fmt_money(r.get('revenue_total_net'))}\")\n            lines.append(f\"avg_invoice_net={fmt_money(r.get('avg_invoice_net'))}\")\n            lines.append(f\"median_invoice_net={fmt_money(r.get('median_invoice_net'))}\")\n        lines.append(\"\")\n\n        lines.append(\"REVENUE CONCENTRATION\")\n        if not conc.empty:\n            r = conc.iloc[0].to_dict()\n            lines.append(f\"top_1_share={fmt_pct(r.get('top_1_share'))}\")\n            lines.append(f\"top_3_share={fmt_pct(r.get('top_3_share'))}\")\n            lines.append(f\"top_10_share={fmt_pct(r.get('top_10_share'))}\")\n        lines.append(\"\")\n\n        lines.append(\"TOP CLIENTS BY REVENUE (net)\")\n        if by_client.empty:\n            lines.append(\"  (no data)\\n\")\n        else:\n            for _, row in by_client.head(min(len(by_client), int(self.top_n))).iterrows():\n                lines.append(f\"  - {row.get('company')}: revenue_total_net={fmt_money(row.get('revenue_total_net'))}, invoices={int(row.get('invoices_count'))}\")\n\n        lines.append(\"\")\n        lines.append(\"REVENUE BY INVOICE TYPE\")\n        if by_type.empty:\n            lines.append(\"  (no data)\\n\")\n        else:\n            for _, row in by_type.iterrows():\n                lines.append(f\"  - {row.get('invoice_type')}: revenue_total_net={fmt_money(row.get('revenue_total_net'))}, share={fmt_pct(row.get('share'))}\")\n\n        if not cats.empty and (cats.shape[0] > 0) and (cats[\"category\"].notna().any()):\n            lines.append(\"\")\n            lines.append(\"CATEGORY BREAKDOWN\")\n            for _, row in cats.iterrows():\n                lines.append(f\"  - {row.get('category')}: revenue_total_net={fmt_money(row.get('revenue_total_net'))}, share={fmt_pct(row.get('share'))}\")\n\n        # Seasonality note\n        if not by_month.empty and \"revenue_total_net\" in by_month.columns:\n            lines.append(\"\")\n            peak = by_month.sort_values(\"revenue_total_net\", ascending=False).head(1)\n            trough = by_month.sort_values(\"revenue_total_net\", ascending=True).head(1)\n            if len(peak) == 1:\n                lines.append(f\"PEAK MONTH: {peak.iloc[0].get('invoice_month')} revenue_total_net={fmt_money(peak.iloc[0].get('revenue_total_net'))}\")\n            if len(trough) == 1:\n                lines.append(f\"LOW MONTH: {trough.iloc[0].get('invoice_month')} revenue_total_net={fmt_money(trough.iloc[0].get('revenue_total_net'))}\")\n\n        # Anomalies\n        lines.append(\"\")\n        lines.append(\"NOTABLE LARGE INVOICES / CREDIT EVENTS\")\n        if anom.empty:\n            lines.append(\"  (no large anomalies detected)\\n\")\n        else:\n            for _, row in anom.iterrows():\n                dt = row.get(\"date\")\n                dt_s = \"n/a\" if pd.isna(dt) else str(pd.to_datetime(dt).date())\n                ref = str(row.get(\"reference\") or \"\").strip()\n                ref = (ref[:90] + \"…\") if len(ref) > 90 else ref\n                lines.append(f\"  - {dt_s} {row.get('company')} {row.get('invoice_type')}: revenue_net={fmt_money(row.get('revenue_net'))} ref={ref}\")\n\n        if bool(self.show_debug):\n            lines.append(\"\")\n            lines.append(self.build_data_diagnostics().text)\n\n        return Message(text=\"\\n\".join(lines))\n\n    # -------------------------\n    # Diagnostics\n    # -------------------------\n\n    def build_data_diagnostics(self) -> Message:\n        df = self._unwrap_to_df(self.invoices_df)\n        lines: List[str] = []\n        lines.append(\"DATA DIAGNOSTICS\")\n        lines.append(f\"invoices_rows={len(df)} cols={len(df.columns)}\")\n        lines.append(\"columns_sample=\" + str(list(df.columns)[:40]))\n        for c in [self.date_col, self.company_col, self.invoice_total_col, self.invoice_type_col, self.status_col, self.reference_col, self.category_col]:\n            if c and c in df.columns:\n                lines.append(f\"col_present: {c}=true\")\n            else:\n                lines.append(f\"col_present: {c}=false\")\n        return Message(text=\"\\n\".join(lines))"
              },
              "company_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Company column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "company_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Company Name"
              },
              "credit_type_values": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Credit memo type values (comma-separated)",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "credit_type_values",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Credit Memo,Credit,CM"
              },
              "date_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Invoice date column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "date_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Date"
              },
              "exclude_non_posted": {
                "_input_type": "BoolInput",
                "advanced": false,
                "display_name": "Exclude non-posted invoices",
                "dynamic": false,
                "info": "",
                "list": false,
                "list_add_label": "Add More",
                "name": "exclude_non_posted",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "bool",
                "value": false
              },
              "invoice_total_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Invoice total column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "invoice_total_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Invoice Total"
              },
              "invoice_type_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Invoice type column (optional)",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "invoice_type_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Invoice Type"
              },
              "invoices_df": {
                "_input_type": "DataFrameInput",
                "advanced": false,
                "display_name": "Invoices (Clean) DataFrame",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "DataFrame"
                ],
                "list": false,
                "list_add_label": "Add More",
                "name": "invoices_df",
                "placeholder": "",
                "required": true,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "other",
                "value": ""
              },
              "posted_status_values": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Posted status values (comma-separated)",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "posted_status_values",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Posted,Paid,Closed"
              },
              "product_total_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Product total column (optional)",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "product_total_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Product Total"
              },
              "reference_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Invoice reference/summary column (optional)",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "reference_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Invoice Reference"
              },
              "service_total_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Service total column (optional)",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "service_total_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Service Total"
              },
              "show_debug": {
                "_input_type": "BoolInput",
                "advanced": false,
                "display_name": "Include debug details in insights output",
                "dynamic": false,
                "info": "",
                "list": false,
                "list_add_label": "Add More",
                "name": "show_debug",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "bool",
                "value": false
              },
              "status_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Status column (optional)",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "status_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Status"
              },
              "tax_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Sales tax column (optional)",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "tax_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Sales Tax"
              },
              "top_n": {
                "_input_type": "IntInput",
                "advanced": false,
                "display_name": "Top N rows",
                "dynamic": false,
                "info": "",
                "list": false,
                "list_add_label": "Add More",
                "name": "top_n",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "int",
                "value": 15
              }
            },
            "tool_mode": false
          },
          "selected_output": "revenue_concentration",
          "showNode": true,
          "type": "msp_invoice_metrics_builder"
        },
        "dragging": false,
        "id": "msp_invoice_metrics_builder-8zhSm",
        "measured": {
          "height": 1429,
          "width": 320
        },
        "position": {
          "x": 2048.2102537479786,
          "y": -2462.5992388434797
        },
        "selected": false,
        "type": "genericNode"
      },
      {
        "data": {
          "id": "msp_invoice_metrics_builder-l3Hd0",
          "node": {
            "base_classes": [
              "DataFrame",
              "Message"
            ],
            "beta": false,
            "conditional_paths": [],
            "custom_fields": {},
            "description": "Build invoice KPIs and rollups (month, client, type, optional category) and a compact insights context for an LLM.",
            "display_name": "MSP Invoice Metrics Builder",
            "documentation": "",
            "edited": true,
            "field_order": [
              "invoices_df",
              "date_col",
              "company_col",
              "invoice_total_col",
              "product_total_col",
              "service_total_col",
              "tax_col",
              "invoice_type_col",
              "status_col",
              "reference_col",
              "category_col",
              "exclude_non_posted",
              "posted_status_values",
              "credit_type_values",
              "top_n",
              "anomaly_top_n",
              "show_debug"
            ],
            "frozen": false,
            "icon": "BarChart3",
            "legacy": false,
            "lf_version": "1.6.0",
            "metadata": {},
            "minimized": false,
            "output_types": [],
            "outputs": [
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "KPI Overview",
                "group_outputs": false,
                "hidden": null,
                "method": "build_kpi_overview",
                "name": "kpi_overview",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Revenue by Month",
                "group_outputs": false,
                "hidden": null,
                "method": "build_revenue_by_month",
                "name": "revenue_by_month",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Revenue by Client",
                "group_outputs": false,
                "hidden": null,
                "method": "build_revenue_by_client",
                "name": "revenue_by_client",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Revenue by Invoice Type",
                "group_outputs": false,
                "hidden": null,
                "method": "build_revenue_by_type",
                "name": "revenue_by_type",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Revenue Concentration",
                "group_outputs": false,
                "hidden": null,
                "method": "build_revenue_concentration",
                "name": "revenue_concentration",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Category Breakdown (Optional)",
                "group_outputs": false,
                "hidden": null,
                "method": "build_category_breakdown",
                "name": "category_breakdown",
                "options": null,
                "required_inputs": null,
                "selected": "DataFrame",
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Anomalies (Large Invoices / Spikes)",
                "group_outputs": false,
                "hidden": null,
                "method": "build_anomalies",
                "name": "anomalies",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Insights Context (for LLM)",
                "group_outputs": false,
                "hidden": null,
                "method": "build_insights_context",
                "name": "insights_context",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "Message"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Data Diagnostics",
                "group_outputs": false,
                "hidden": null,
                "method": "build_data_diagnostics",
                "name": "data_diagnostics",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "Message"
                ],
                "value": "__UNDEFINED__"
              }
            ],
            "pinned": false,
            "template": {
              "_type": "Component",
              "anomaly_top_n": {
                "_input_type": "IntInput",
                "advanced": false,
                "display_name": "Top N anomalies",
                "dynamic": false,
                "info": "",
                "list": false,
                "list_add_label": "Add More",
                "name": "anomaly_top_n",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "int",
                "value": 8
              },
              "category_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Category column (optional)",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "category_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Department"
              },
              "code": {
                "advanced": true,
                "dynamic": true,
                "fileTypes": [],
                "file_path": "",
                "info": "",
                "list": false,
                "load_from_db": false,
                "multiline": true,
                "name": "code",
                "password": false,
                "placeholder": "",
                "required": true,
                "show": true,
                "title_case": false,
                "type": "code",
                "value": "from __future__ import annotations\n\nfrom typing import List, Dict, Any, Optional\nimport pandas as pd\nimport numpy as np\n\nfrom langflow.custom import Component\nfrom langflow.io import DataFrameInput, MessageTextInput, BoolInput, IntInput, Output\nfrom langflow.schema import DataFrame\nfrom langflow.schema.message import Message\n\n\nclass MSPInvoiceMetricsBuilder(Component):\n    display_name = \"MSP Invoice Metrics Builder\"\n    description = \"Build invoice KPIs and rollups (month, client, type, optional category) and a compact insights context for an LLM.\"\n    name = \"msp_invoice_metrics_builder\"\n    icon = \"BarChart3\"\n\n    inputs = [\n        DataFrameInput(name=\"invoices_df\", display_name=\"Invoices (Clean) DataFrame\", required=True),\n\n        # Column mappings (adjust to match your export)\n        MessageTextInput(name=\"date_col\", display_name=\"Invoice date column\", value=\"Date\"),\n        MessageTextInput(name=\"company_col\", display_name=\"Company column\", value=\"Company Name\"),\n        MessageTextInput(name=\"invoice_total_col\", display_name=\"Invoice total column\", value=\"Invoice Total\"),\n        MessageTextInput(name=\"product_total_col\", display_name=\"Product total column (optional)\", value=\"Product Total\"),\n        MessageTextInput(name=\"service_total_col\", display_name=\"Service total column (optional)\", value=\"Service Total\"),\n        MessageTextInput(name=\"tax_col\", display_name=\"Sales tax column (optional)\", value=\"Sales Tax\"),\n        MessageTextInput(name=\"invoice_type_col\", display_name=\"Invoice type column (optional)\", value=\"Invoice Type\"),\n        MessageTextInput(name=\"status_col\", display_name=\"Status column (optional)\", value=\"Status\"),\n        MessageTextInput(name=\"reference_col\", display_name=\"Invoice reference/summary column (optional)\", value=\"Invoice Reference\"),\n\n        # Optional categorization column if your ETL adds one\n        MessageTextInput(name=\"category_col\", display_name=\"Category column (optional)\", value=\"Category\"),\n\n        # Filters and semantics\n        BoolInput(name=\"exclude_non_posted\", display_name=\"Exclude non-posted invoices\", value=True),\n        MessageTextInput(\n            name=\"posted_status_values\",\n            display_name=\"Posted status values (comma-separated)\",\n            value=\"Posted,Paid,Closed\",\n        ),\n        MessageTextInput(\n            name=\"credit_type_values\",\n            display_name=\"Credit memo type values (comma-separated)\",\n            value=\"Credit Memo,Credit,CM\",\n        ),\n\n        IntInput(name=\"top_n\", display_name=\"Top N rows\", value=15),\n        IntInput(name=\"anomaly_top_n\", display_name=\"Top N anomalies\", value=8),\n        BoolInput(name=\"show_debug\", display_name=\"Include debug details in insights output\", value=False),\n    ]\n\n    outputs = [\n        Output(name=\"kpi_overview\", display_name=\"KPI Overview\", method=\"build_kpi_overview\"),\n        Output(name=\"revenue_by_month\", display_name=\"Revenue by Month\", method=\"build_revenue_by_month\"),\n        Output(name=\"revenue_by_client\", display_name=\"Revenue by Client\", method=\"build_revenue_by_client\"),\n        Output(name=\"revenue_by_type\", display_name=\"Revenue by Invoice Type\", method=\"build_revenue_by_type\"),\n        Output(name=\"revenue_concentration\", display_name=\"Revenue Concentration\", method=\"build_revenue_concentration\"),\n        Output(name=\"category_breakdown\", display_name=\"Category Breakdown (Optional)\", method=\"build_category_breakdown\"),\n        Output(name=\"anomalies\", display_name=\"Anomalies (Large Invoices / Spikes)\", method=\"build_anomalies\"),\n        Output(name=\"insights_context\", display_name=\"Insights Context (for LLM)\", method=\"build_insights_context\"),\n        Output(name=\"data_diagnostics\", display_name=\"Data Diagnostics\", method=\"build_data_diagnostics\"),\n    ]\n\n    # -------------------------\n    # Helpers\n    # -------------------------\n\n    def _safe_col(self, df: pd.DataFrame, col: str) -> bool:\n        return col is not None and str(col).strip() != \"\" and col in df.columns\n\n    def _unwrap_to_df(self, obj: Any) -> pd.DataFrame:\n        if obj is None:\n            return pd.DataFrame()\n\n        if isinstance(obj, pd.DataFrame):\n            df = obj.copy()\n            df.columns = [str(c).replace(\"\\ufeff\", \"\").strip() for c in df.columns]\n            return df\n\n        if hasattr(obj, \"data\"):\n            d = getattr(obj, \"data\")\n            if isinstance(d, pd.DataFrame):\n                df = d.copy()\n                df.columns = [str(c).replace(\"\\ufeff\", \"\").strip() for c in df.columns]\n                return df\n            if isinstance(d, list):\n                df = pd.DataFrame(d)\n                df.columns = [str(c).replace(\"\\ufeff\", \"\").strip() for c in df.columns]\n                return df\n            if isinstance(d, dict):\n                for k in [\"data\", \"rows\", \"records\", \"items\"]:\n                    if k in d and isinstance(d[k], list):\n                        df = pd.DataFrame(d[k])\n                        df.columns = [str(c).replace(\"\\ufeff\", \"\").strip() for c in df.columns]\n                        return df\n\n        if isinstance(obj, dict):\n            for k in [\"data\", \"rows\", \"records\", \"items\"]:\n                if k in obj and isinstance(obj[k], list):\n                    df = pd.DataFrame(obj[k])\n                    df.columns = [str(c).replace(\"\\ufeff\", \"\").strip() for c in df.columns]\n                    return df\n            df = pd.DataFrame(obj)\n            df.columns = [str(c).replace(\"\\ufeff\", \"\").strip() for c in df.columns]\n            return df\n\n        if isinstance(obj, list):\n            df = pd.DataFrame(obj)\n            df.columns = [str(c).replace(\"\\ufeff\", \"\").strip() for c in df.columns]\n            return df\n\n        df = pd.DataFrame(obj)\n        df.columns = [str(c).replace(\"\\ufeff\", \"\").strip() for c in df.columns]\n        return df\n\n    def _to_dt(self, s: pd.Series) -> pd.Series:\n        return pd.to_datetime(s, errors=\"coerce\")\n\n    def _to_money(self, s: pd.Series) -> pd.Series:\n        x = s.astype(str).str.strip()\n        x = x.str.replace(\"$\", \"\", regex=False).str.replace(\",\", \"\", regex=False)\n        x = x.str.replace(\"(\", \"-\", regex=False).str.replace(\")\", \"\", regex=False)\n        return pd.to_numeric(x, errors=\"coerce\")\n\n    def _company_key(self, s: pd.Series) -> pd.Series:\n        x = s.fillna(\"\").astype(str).str.lower().str.strip()\n        x = x.str.replace(r\"[^\\w\\s]\", \" \", regex=True)\n        x = x.str.replace(r\"\\b(inc|incorporated|llc|l\\.l\\.c|ltd|corp|corporation|co|company)\\b\", \" \", regex=True)\n        x = x.str.replace(r\"\\s+\", \" \", regex=True).str.strip()\n        return x\n\n    def _parse_csv_list(self, s: str) -> List[str]:\n        if s is None:\n            return []\n        return [p.strip() for p in str(s).split(\",\") if p.strip()]\n\n    def _prep_invoices(self) -> pd.DataFrame:\n        df = self._unwrap_to_df(self.invoices_df)\n        if df.empty:\n            return df\n\n        # Date\n        if self._safe_col(df, self.date_col):\n            df[self.date_col] = self._to_dt(df[self.date_col])\n\n        # Core money fields\n        if self._safe_col(df, self.invoice_total_col):\n            df[\"_invoice_total\"] = self._to_money(df[self.invoice_total_col]).fillna(0.0)\n        else:\n            df[\"_invoice_total\"] = np.nan\n\n        if self._safe_col(df, self.product_total_col):\n            df[\"_product_total\"] = self._to_money(df[self.product_total_col]).fillna(0.0)\n        else:\n            df[\"_product_total\"] = np.nan\n\n        if self._safe_col(df, self.service_total_col):\n            df[\"_service_total\"] = self._to_money(df[self.service_total_col]).fillna(0.0)\n        else:\n            df[\"_service_total\"] = np.nan\n\n        if self._safe_col(df, self.tax_col):\n            df[\"_tax_total\"] = self._to_money(df[self.tax_col]).fillna(0.0)\n        else:\n            df[\"_tax_total\"] = np.nan\n\n        # Company normalization\n        if self._safe_col(df, self.company_col):\n            df[\"_company\"] = df[self.company_col].astype(str).str.strip()\n        else:\n            df[\"_company\"] = \"(unknown)\"\n        df[\"_company_key\"] = self._company_key(df[\"_company\"])\n\n        # Type, status, reference\n        df[\"_type\"] = df[self.invoice_type_col].astype(str).str.strip() if self._safe_col(df, self.invoice_type_col) else \"(unknown)\"\n        df[\"_status\"] = df[self.status_col].astype(str).str.strip() if self._safe_col(df, self.status_col) else \"(unknown)\"\n        df[\"_reference\"] = df[self.reference_col].astype(str).str.strip() if self._safe_col(df, self.reference_col) else \"\"\n\n        # Posted filter\n        if bool(self.exclude_non_posted) and self._safe_col(df, self.status_col):\n            allowed = set([x.lower() for x in self._parse_csv_list(self.posted_status_values)])\n            df = df[df[\"_status\"].astype(str).str.lower().isin(allowed)].copy()\n\n        # Credit memo sign\n        credit_vals = set([x.lower() for x in self._parse_csv_list(self.credit_type_values)])\n        is_credit = df[\"_type\"].astype(str).str.lower().isin(credit_vals)\n        df[\"_sign\"] = np.where(is_credit, -1.0, 1.0)\n\n        # Net revenue fields\n        df[\"revenue_net\"] = df[\"_invoice_total\"] * df[\"_sign\"]\n        df[\"product_net\"] = df[\"_product_total\"] * df[\"_sign\"] if df[\"_product_total\"].notna().any() else np.nan\n        df[\"service_net\"] = df[\"_service_total\"] * df[\"_sign\"] if df[\"_service_total\"].notna().any() else np.nan\n\n        # Month\n        if self._safe_col(df, self.date_col) and df[self.date_col].notna().any():\n            df[\"invoice_month\"] = df[self.date_col].dt.to_period(\"M\").astype(str)\n        else:\n            df[\"invoice_month\"] = None\n\n        return df\n\n    # -------------------------\n    # Outputs\n    # -------------------------\n\n    def build_kpi_overview(self) -> DataFrame:\n        df = self._prep_invoices()\n        if df.empty:\n            return DataFrame(pd.DataFrame())\n\n        total_invoices = int(len(df))\n        total_revenue = float(df[\"revenue_net\"].sum()) if \"revenue_net\" in df.columns else np.nan\n        credits_count = int((df[\"_sign\"] < 0).sum()) if \"_sign\" in df.columns else 0\n        avg_invoice = float(df[\"revenue_net\"].mean()) if total_invoices else np.nan\n        median_invoice = float(df[\"revenue_net\"].median()) if total_invoices else np.nan\n\n        out = pd.DataFrame([{\n            \"invoices_count\": total_invoices,\n            \"credits_count\": credits_count,\n            \"revenue_total_net\": total_revenue,\n            \"avg_invoice_net\": avg_invoice,\n            \"median_invoice_net\": median_invoice,\n            \"unique_clients\": int(df[\"_company_key\"].nunique()) if \"_company_key\" in df.columns else np.nan,\n        }])\n        return DataFrame(out)\n\n    def build_revenue_by_month(self) -> DataFrame:\n        df = self._prep_invoices()\n        if df.empty or \"invoice_month\" not in df.columns or df[\"invoice_month\"].isna().all():\n            return DataFrame(pd.DataFrame(columns=[\"invoice_month\", \"revenue_total_net\", \"invoices_count\"]))\n\n        g = df.groupby(\"invoice_month\", dropna=False)\n        out = pd.DataFrame({\n            \"invoice_month\": g.size().index.astype(str),\n            \"invoices_count\": g.size().values,\n            \"revenue_total_net\": g[\"revenue_net\"].sum().values,\n        })\n\n        if \"product_net\" in df.columns and df[\"product_net\"].notna().any():\n            out[\"product_total_net\"] = g[\"product_net\"].sum().values\n        if \"service_net\" in df.columns and df[\"service_net\"].notna().any():\n            out[\"service_total_net\"] = g[\"service_net\"].sum().values\n\n        out = out.sort_values(\"invoice_month\")\n        return DataFrame(out)\n\n    def build_revenue_by_client(self) -> DataFrame:\n        df = self._prep_invoices()\n        if df.empty:\n            return DataFrame(pd.DataFrame(columns=[\"company\", \"invoices_count\", \"revenue_total_net\", \"avg_invoice_net\"]))\n\n        g = df.groupby(\"_company_key\", dropna=False)\n        out = pd.DataFrame({\n            \"_company_key\": g.size().index.astype(str),\n            \"invoices_count\": g.size().values,\n            \"revenue_total_net\": g[\"revenue_net\"].sum().values,\n            \"avg_invoice_net\": g[\"revenue_net\"].mean().values,\n        })\n\n        # Representative display name\n        name_map = (\n            df.groupby(\"_company_key\")[\"_company\"]\n            .agg(lambda s: s.value_counts().index[0] if len(s.dropna()) else \"(unknown)\")\n            .rename(\"company\")\n            .reset_index()\n        )\n        out = out.merge(name_map, on=\"_company_key\", how=\"left\")\n\n        out = out.sort_values(\"revenue_total_net\", ascending=False).head(int(self.top_n))\n        out = out[[\"company\", \"invoices_count\", \"revenue_total_net\", \"avg_invoice_net\"]]\n        return DataFrame(out)\n\n    def build_revenue_by_type(self) -> DataFrame:\n        df = self._prep_invoices()\n        if df.empty:\n            return DataFrame(pd.DataFrame(columns=[\"invoice_type\", \"invoices_count\", \"revenue_total_net\", \"share\"]))\n\n        g = df.groupby(\"_type\", dropna=False)\n        out = pd.DataFrame({\n            \"invoice_type\": g.size().index.astype(str),\n            \"invoices_count\": g.size().values,\n            \"revenue_total_net\": g[\"revenue_net\"].sum().values,\n        })\n        total = float(out[\"revenue_total_net\"].abs().sum()) if len(out) else 0.0\n        out[\"share\"] = out[\"revenue_total_net\"].abs() / (total if total else np.nan)\n        out = out.sort_values(\"revenue_total_net\", ascending=False)\n        return DataFrame(out)\n\n    def build_revenue_concentration(self) -> DataFrame:\n        df = self._prep_invoices()\n        if df.empty:\n            return DataFrame(pd.DataFrame())\n\n        by = df.groupby(\"_company_key\", dropna=False)[\"revenue_net\"].sum().sort_values(ascending=False)\n        total = float(by.sum()) if len(by) else 0.0\n\n        def share_top(n: int) -> float:\n            if total == 0:\n                return np.nan\n            return float(by.head(n).sum() / total)\n\n        out = pd.DataFrame([{\n            \"clients_total\": int(by.shape[0]),\n            \"revenue_total_net\": total,\n            \"top_1_share\": share_top(1),\n            \"top_3_share\": share_top(3),\n            \"top_5_share\": share_top(5),\n            \"top_10_share\": share_top(10),\n        }])\n        return DataFrame(out)\n\n    def build_category_breakdown(self) -> DataFrame:\n        df = self._prep_invoices()\n        if df.empty or not self._safe_col(df, self.category_col):\n            return DataFrame(pd.DataFrame(columns=[\"category\", \"invoices_count\", \"revenue_total_net\", \"share\"]))\n\n        cat = df[self.category_col].fillna(\"(blank)\").astype(str).str.strip()\n        tmp = df.copy()\n        tmp[\"_category\"] = cat\n\n        g = tmp.groupby(\"_category\", dropna=False)\n        out = pd.DataFrame({\n            \"category\": g.size().index.astype(str),\n            \"invoices_count\": g.size().values,\n            \"revenue_total_net\": g[\"revenue_net\"].sum().values,\n        })\n        total = float(out[\"revenue_total_net\"].abs().sum()) if len(out) else 0.0\n        out[\"share\"] = out[\"revenue_total_net\"].abs() / (total if total else np.nan)\n        out = out.sort_values(\"revenue_total_net\", ascending=False)\n        return DataFrame(out)\n\n    def build_anomalies(self) -> DataFrame:\n        df = self._prep_invoices()\n        if df.empty:\n            return DataFrame(pd.DataFrame(columns=[\"date\", \"company\", \"invoice_type\", \"revenue_net\", \"reference\"]))\n\n        # Large invoices\n        x = df[\"revenue_net\"].replace([np.inf, -np.inf], np.nan).dropna()\n        if len(x) < 10:\n            thresh = x.abs().quantile(0.90) if len(x) else np.nan\n        else:\n            thresh = x.abs().quantile(0.95)\n\n        an = df[df[\"revenue_net\"].abs() >= float(thresh) if pd.notna(thresh) else False].copy()\n        an = an.sort_values(\"revenue_net\", ascending=False).head(int(self.anomaly_top_n))\n\n        # Friendly columns\n        out = pd.DataFrame({\n            \"date\": an[self.date_col] if self._safe_col(an, self.date_col) else pd.NaT,\n            \"company\": an[\"_company\"],\n            \"invoice_type\": an[\"_type\"],\n            \"revenue_net\": an[\"revenue_net\"],\n            \"reference\": an[\"_reference\"],\n        })\n        return DataFrame(out)\n\n    # -------------------------\n    # Insights context\n    # -------------------------\n\n    def build_insights_context(self) -> Message:\n        kpi = pd.DataFrame(self.build_kpi_overview())\n        by_month = pd.DataFrame(self.build_revenue_by_month())\n        by_client = pd.DataFrame(self.build_revenue_by_client())\n        by_type = pd.DataFrame(self.build_revenue_by_type())\n        conc = pd.DataFrame(self.build_revenue_concentration())\n        cats = pd.DataFrame(self.build_category_breakdown())\n        anom = pd.DataFrame(self.build_anomalies())\n\n        def fmt_money(x) -> str:\n            return \"n/a\" if pd.isna(x) else f\"${float(x):,.2f}\"\n\n        def fmt_pct(x) -> str:\n            return \"n/a\" if pd.isna(x) else f\"{float(x) * 100:.1f}%\"\n\n        lines: List[str] = []\n        lines.append(\"INVOICE DATASET OVERVIEW\")\n        if not kpi.empty:\n            r = kpi.iloc[0].to_dict()\n            lines.append(f\"invoices_count={r.get('invoices_count')}\")\n            lines.append(f\"unique_clients={r.get('unique_clients')}\")\n            lines.append(f\"credits_count={r.get('credits_count')}\")\n            lines.append(f\"revenue_total_net={fmt_money(r.get('revenue_total_net'))}\")\n            lines.append(f\"avg_invoice_net={fmt_money(r.get('avg_invoice_net'))}\")\n            lines.append(f\"median_invoice_net={fmt_money(r.get('median_invoice_net'))}\")\n        lines.append(\"\")\n\n        lines.append(\"REVENUE CONCENTRATION\")\n        if not conc.empty:\n            r = conc.iloc[0].to_dict()\n            lines.append(f\"top_1_share={fmt_pct(r.get('top_1_share'))}\")\n            lines.append(f\"top_3_share={fmt_pct(r.get('top_3_share'))}\")\n            lines.append(f\"top_10_share={fmt_pct(r.get('top_10_share'))}\")\n        lines.append(\"\")\n\n        lines.append(\"TOP CLIENTS BY REVENUE (net)\")\n        if by_client.empty:\n            lines.append(\"  (no data)\\n\")\n        else:\n            for _, row in by_client.head(min(len(by_client), int(self.top_n))).iterrows():\n                lines.append(f\"  - {row.get('company')}: revenue_total_net={fmt_money(row.get('revenue_total_net'))}, invoices={int(row.get('invoices_count'))}\")\n\n        lines.append(\"\")\n        lines.append(\"REVENUE BY INVOICE TYPE\")\n        if by_type.empty:\n            lines.append(\"  (no data)\\n\")\n        else:\n            for _, row in by_type.iterrows():\n                lines.append(f\"  - {row.get('invoice_type')}: revenue_total_net={fmt_money(row.get('revenue_total_net'))}, share={fmt_pct(row.get('share'))}\")\n\n        if not cats.empty and (cats.shape[0] > 0) and (cats[\"category\"].notna().any()):\n            lines.append(\"\")\n            lines.append(\"CATEGORY BREAKDOWN\")\n            for _, row in cats.iterrows():\n                lines.append(f\"  - {row.get('category')}: revenue_total_net={fmt_money(row.get('revenue_total_net'))}, share={fmt_pct(row.get('share'))}\")\n\n        # Seasonality note\n        if not by_month.empty and \"revenue_total_net\" in by_month.columns:\n            lines.append(\"\")\n            peak = by_month.sort_values(\"revenue_total_net\", ascending=False).head(1)\n            trough = by_month.sort_values(\"revenue_total_net\", ascending=True).head(1)\n            if len(peak) == 1:\n                lines.append(f\"PEAK MONTH: {peak.iloc[0].get('invoice_month')} revenue_total_net={fmt_money(peak.iloc[0].get('revenue_total_net'))}\")\n            if len(trough) == 1:\n                lines.append(f\"LOW MONTH: {trough.iloc[0].get('invoice_month')} revenue_total_net={fmt_money(trough.iloc[0].get('revenue_total_net'))}\")\n\n        # Anomalies\n        lines.append(\"\")\n        lines.append(\"NOTABLE LARGE INVOICES / CREDIT EVENTS\")\n        if anom.empty:\n            lines.append(\"  (no large anomalies detected)\\n\")\n        else:\n            for _, row in anom.iterrows():\n                dt = row.get(\"date\")\n                dt_s = \"n/a\" if pd.isna(dt) else str(pd.to_datetime(dt).date())\n                ref = str(row.get(\"reference\") or \"\").strip()\n                ref = (ref[:90] + \"…\") if len(ref) > 90 else ref\n                lines.append(f\"  - {dt_s} {row.get('company')} {row.get('invoice_type')}: revenue_net={fmt_money(row.get('revenue_net'))} ref={ref}\")\n\n        if bool(self.show_debug):\n            lines.append(\"\")\n            lines.append(self.build_data_diagnostics().text)\n\n        return Message(text=\"\\n\".join(lines))\n\n    # -------------------------\n    # Diagnostics\n    # -------------------------\n\n    def build_data_diagnostics(self) -> Message:\n        df = self._unwrap_to_df(self.invoices_df)\n        lines: List[str] = []\n        lines.append(\"DATA DIAGNOSTICS\")\n        lines.append(f\"invoices_rows={len(df)} cols={len(df.columns)}\")\n        lines.append(\"columns_sample=\" + str(list(df.columns)[:40]))\n        for c in [self.date_col, self.company_col, self.invoice_total_col, self.invoice_type_col, self.status_col, self.reference_col, self.category_col]:\n            if c and c in df.columns:\n                lines.append(f\"col_present: {c}=true\")\n            else:\n                lines.append(f\"col_present: {c}=false\")\n        return Message(text=\"\\n\".join(lines))"
              },
              "company_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Company column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "company_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Company Name"
              },
              "credit_type_values": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Credit memo type values (comma-separated)",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "credit_type_values",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Credit Memo,Credit,CM"
              },
              "date_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Invoice date column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "date_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Date"
              },
              "exclude_non_posted": {
                "_input_type": "BoolInput",
                "advanced": false,
                "display_name": "Exclude non-posted invoices",
                "dynamic": false,
                "info": "",
                "list": false,
                "list_add_label": "Add More",
                "name": "exclude_non_posted",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "bool",
                "value": false
              },
              "invoice_total_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Invoice total column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "invoice_total_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Invoice Total"
              },
              "invoice_type_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Invoice type column (optional)",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "invoice_type_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Invoice Type"
              },
              "invoices_df": {
                "_input_type": "DataFrameInput",
                "advanced": false,
                "display_name": "Invoices (Clean) DataFrame",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "DataFrame"
                ],
                "list": false,
                "list_add_label": "Add More",
                "name": "invoices_df",
                "placeholder": "",
                "required": true,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "other",
                "value": ""
              },
              "posted_status_values": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Posted status values (comma-separated)",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "posted_status_values",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Posted,Paid,Closed"
              },
              "product_total_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Product total column (optional)",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "product_total_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Product Total"
              },
              "reference_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Invoice reference/summary column (optional)",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "reference_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Invoice Reference"
              },
              "service_total_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Service total column (optional)",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "service_total_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Service Total"
              },
              "show_debug": {
                "_input_type": "BoolInput",
                "advanced": false,
                "display_name": "Include debug details in insights output",
                "dynamic": false,
                "info": "",
                "list": false,
                "list_add_label": "Add More",
                "name": "show_debug",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "bool",
                "value": false
              },
              "status_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Status column (optional)",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "status_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Status"
              },
              "tax_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Sales tax column (optional)",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "tax_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Sales Tax"
              },
              "top_n": {
                "_input_type": "IntInput",
                "advanced": false,
                "display_name": "Top N rows",
                "dynamic": false,
                "info": "",
                "list": false,
                "list_add_label": "Add More",
                "name": "top_n",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "int",
                "value": 15
              }
            },
            "tool_mode": false
          },
          "selected_output": "category_breakdown",
          "showNode": true,
          "type": "msp_invoice_metrics_builder"
        },
        "dragging": false,
        "id": "msp_invoice_metrics_builder-l3Hd0",
        "measured": {
          "height": 1429,
          "width": 320
        },
        "position": {
          "x": 2369.6267729022484,
          "y": -2550.0308098219352
        },
        "selected": false,
        "type": "genericNode"
      },
      {
        "data": {
          "id": "msp_invoice_metrics_builder-StB0j",
          "node": {
            "base_classes": [
              "DataFrame",
              "Message"
            ],
            "beta": false,
            "conditional_paths": [],
            "custom_fields": {},
            "description": "Build invoice KPIs and rollups (month, client, type, optional category) and a compact insights context for an LLM.",
            "display_name": "MSP Invoice Metrics Builder",
            "documentation": "",
            "edited": true,
            "field_order": [
              "invoices_df",
              "date_col",
              "company_col",
              "invoice_total_col",
              "product_total_col",
              "service_total_col",
              "tax_col",
              "invoice_type_col",
              "status_col",
              "reference_col",
              "category_col",
              "exclude_non_posted",
              "posted_status_values",
              "credit_type_values",
              "top_n",
              "anomaly_top_n",
              "show_debug"
            ],
            "frozen": false,
            "icon": "BarChart3",
            "legacy": false,
            "lf_version": "1.6.0",
            "metadata": {},
            "minimized": false,
            "output_types": [],
            "outputs": [
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "KPI Overview",
                "group_outputs": false,
                "hidden": null,
                "method": "build_kpi_overview",
                "name": "kpi_overview",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Revenue by Month",
                "group_outputs": false,
                "hidden": null,
                "method": "build_revenue_by_month",
                "name": "revenue_by_month",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Revenue by Client",
                "group_outputs": false,
                "hidden": null,
                "method": "build_revenue_by_client",
                "name": "revenue_by_client",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Revenue by Invoice Type",
                "group_outputs": false,
                "hidden": null,
                "method": "build_revenue_by_type",
                "name": "revenue_by_type",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Revenue Concentration",
                "group_outputs": false,
                "hidden": null,
                "method": "build_revenue_concentration",
                "name": "revenue_concentration",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Category Breakdown (Optional)",
                "group_outputs": false,
                "hidden": null,
                "method": "build_category_breakdown",
                "name": "category_breakdown",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Anomalies (Large Invoices / Spikes)",
                "group_outputs": false,
                "hidden": null,
                "method": "build_anomalies",
                "name": "anomalies",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Insights Context (for LLM)",
                "group_outputs": false,
                "hidden": null,
                "method": "build_insights_context",
                "name": "insights_context",
                "options": null,
                "required_inputs": null,
                "selected": "Message",
                "tool_mode": true,
                "types": [
                  "Message"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Data Diagnostics",
                "group_outputs": false,
                "hidden": null,
                "method": "build_data_diagnostics",
                "name": "data_diagnostics",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "Message"
                ],
                "value": "__UNDEFINED__"
              }
            ],
            "pinned": false,
            "template": {
              "_type": "Component",
              "anomaly_top_n": {
                "_input_type": "IntInput",
                "advanced": false,
                "display_name": "Top N anomalies",
                "dynamic": false,
                "info": "",
                "list": false,
                "list_add_label": "Add More",
                "name": "anomaly_top_n",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "int",
                "value": 8
              },
              "category_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Category column (optional)",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "category_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Category"
              },
              "code": {
                "advanced": true,
                "dynamic": true,
                "fileTypes": [],
                "file_path": "",
                "info": "",
                "list": false,
                "load_from_db": false,
                "multiline": true,
                "name": "code",
                "password": false,
                "placeholder": "",
                "required": true,
                "show": true,
                "title_case": false,
                "type": "code",
                "value": "from __future__ import annotations\n\nfrom typing import List, Dict, Any, Optional\nimport pandas as pd\nimport numpy as np\n\nfrom langflow.custom import Component\nfrom langflow.io import DataFrameInput, MessageTextInput, BoolInput, IntInput, Output\nfrom langflow.schema import DataFrame\nfrom langflow.schema.message import Message\n\n\nclass MSPInvoiceMetricsBuilder(Component):\n    display_name = \"MSP Invoice Metrics Builder\"\n    description = \"Build invoice KPIs and rollups (month, client, type, optional category) and a compact insights context for an LLM.\"\n    name = \"msp_invoice_metrics_builder\"\n    icon = \"BarChart3\"\n\n    inputs = [\n        DataFrameInput(name=\"invoices_df\", display_name=\"Invoices (Clean) DataFrame\", required=True),\n\n        # Column mappings (adjust to match your export)\n        MessageTextInput(name=\"date_col\", display_name=\"Invoice date column\", value=\"Date\"),\n        MessageTextInput(name=\"company_col\", display_name=\"Company column\", value=\"Company Name\"),\n        MessageTextInput(name=\"invoice_total_col\", display_name=\"Invoice total column\", value=\"Invoice Total\"),\n        MessageTextInput(name=\"product_total_col\", display_name=\"Product total column (optional)\", value=\"Product Total\"),\n        MessageTextInput(name=\"service_total_col\", display_name=\"Service total column (optional)\", value=\"Service Total\"),\n        MessageTextInput(name=\"tax_col\", display_name=\"Sales tax column (optional)\", value=\"Sales Tax\"),\n        MessageTextInput(name=\"invoice_type_col\", display_name=\"Invoice type column (optional)\", value=\"Invoice Type\"),\n        MessageTextInput(name=\"status_col\", display_name=\"Status column (optional)\", value=\"Status\"),\n        MessageTextInput(name=\"reference_col\", display_name=\"Invoice reference/summary column (optional)\", value=\"Invoice Reference\"),\n\n        # Optional categorization column if your ETL adds one\n        MessageTextInput(name=\"category_col\", display_name=\"Category column (optional)\", value=\"Category\"),\n\n        # Filters and semantics\n        BoolInput(name=\"exclude_non_posted\", display_name=\"Exclude non-posted invoices\", value=True),\n        MessageTextInput(\n            name=\"posted_status_values\",\n            display_name=\"Posted status values (comma-separated)\",\n            value=\"Posted,Paid,Closed\",\n        ),\n        MessageTextInput(\n            name=\"credit_type_values\",\n            display_name=\"Credit memo type values (comma-separated)\",\n            value=\"Credit Memo,Credit,CM\",\n        ),\n\n        IntInput(name=\"top_n\", display_name=\"Top N rows\", value=15),\n        IntInput(name=\"anomaly_top_n\", display_name=\"Top N anomalies\", value=8),\n        BoolInput(name=\"show_debug\", display_name=\"Include debug details in insights output\", value=False),\n    ]\n\n    outputs = [\n        Output(name=\"kpi_overview\", display_name=\"KPI Overview\", method=\"build_kpi_overview\"),\n        Output(name=\"revenue_by_month\", display_name=\"Revenue by Month\", method=\"build_revenue_by_month\"),\n        Output(name=\"revenue_by_client\", display_name=\"Revenue by Client\", method=\"build_revenue_by_client\"),\n        Output(name=\"revenue_by_type\", display_name=\"Revenue by Invoice Type\", method=\"build_revenue_by_type\"),\n        Output(name=\"revenue_concentration\", display_name=\"Revenue Concentration\", method=\"build_revenue_concentration\"),\n        Output(name=\"category_breakdown\", display_name=\"Category Breakdown (Optional)\", method=\"build_category_breakdown\"),\n        Output(name=\"anomalies\", display_name=\"Anomalies (Large Invoices / Spikes)\", method=\"build_anomalies\"),\n        Output(name=\"insights_context\", display_name=\"Insights Context (for LLM)\", method=\"build_insights_context\"),\n        Output(name=\"data_diagnostics\", display_name=\"Data Diagnostics\", method=\"build_data_diagnostics\"),\n    ]\n\n    # -------------------------\n    # Helpers\n    # -------------------------\n\n    def _safe_col(self, df: pd.DataFrame, col: str) -> bool:\n        return col is not None and str(col).strip() != \"\" and col in df.columns\n\n    def _unwrap_to_df(self, obj: Any) -> pd.DataFrame:\n        if obj is None:\n            return pd.DataFrame()\n\n        if isinstance(obj, pd.DataFrame):\n            df = obj.copy()\n            df.columns = [str(c).replace(\"\\ufeff\", \"\").strip() for c in df.columns]\n            return df\n\n        if hasattr(obj, \"data\"):\n            d = getattr(obj, \"data\")\n            if isinstance(d, pd.DataFrame):\n                df = d.copy()\n                df.columns = [str(c).replace(\"\\ufeff\", \"\").strip() for c in df.columns]\n                return df\n            if isinstance(d, list):\n                df = pd.DataFrame(d)\n                df.columns = [str(c).replace(\"\\ufeff\", \"\").strip() for c in df.columns]\n                return df\n            if isinstance(d, dict):\n                for k in [\"data\", \"rows\", \"records\", \"items\"]:\n                    if k in d and isinstance(d[k], list):\n                        df = pd.DataFrame(d[k])\n                        df.columns = [str(c).replace(\"\\ufeff\", \"\").strip() for c in df.columns]\n                        return df\n\n        if isinstance(obj, dict):\n            for k in [\"data\", \"rows\", \"records\", \"items\"]:\n                if k in obj and isinstance(obj[k], list):\n                    df = pd.DataFrame(obj[k])\n                    df.columns = [str(c).replace(\"\\ufeff\", \"\").strip() for c in df.columns]\n                    return df\n            df = pd.DataFrame(obj)\n            df.columns = [str(c).replace(\"\\ufeff\", \"\").strip() for c in df.columns]\n            return df\n\n        if isinstance(obj, list):\n            df = pd.DataFrame(obj)\n            df.columns = [str(c).replace(\"\\ufeff\", \"\").strip() for c in df.columns]\n            return df\n\n        df = pd.DataFrame(obj)\n        df.columns = [str(c).replace(\"\\ufeff\", \"\").strip() for c in df.columns]\n        return df\n\n    def _to_dt(self, s: pd.Series) -> pd.Series:\n        return pd.to_datetime(s, errors=\"coerce\")\n\n    def _to_money(self, s: pd.Series) -> pd.Series:\n        x = s.astype(str).str.strip()\n        x = x.str.replace(\"$\", \"\", regex=False).str.replace(\",\", \"\", regex=False)\n        x = x.str.replace(\"(\", \"-\", regex=False).str.replace(\")\", \"\", regex=False)\n        return pd.to_numeric(x, errors=\"coerce\")\n\n    def _company_key(self, s: pd.Series) -> pd.Series:\n        x = s.fillna(\"\").astype(str).str.lower().str.strip()\n        x = x.str.replace(r\"[^\\w\\s]\", \" \", regex=True)\n        x = x.str.replace(r\"\\b(inc|incorporated|llc|l\\.l\\.c|ltd|corp|corporation|co|company)\\b\", \" \", regex=True)\n        x = x.str.replace(r\"\\s+\", \" \", regex=True).str.strip()\n        return x\n\n    def _parse_csv_list(self, s: str) -> List[str]:\n        if s is None:\n            return []\n        return [p.strip() for p in str(s).split(\",\") if p.strip()]\n\n    def _prep_invoices(self) -> pd.DataFrame:\n        df = self._unwrap_to_df(self.invoices_df)\n        if df.empty:\n            return df\n\n        # Date\n        if self._safe_col(df, self.date_col):\n            df[self.date_col] = self._to_dt(df[self.date_col])\n\n        # Core money fields\n        if self._safe_col(df, self.invoice_total_col):\n            df[\"_invoice_total\"] = self._to_money(df[self.invoice_total_col]).fillna(0.0)\n        else:\n            df[\"_invoice_total\"] = np.nan\n\n        if self._safe_col(df, self.product_total_col):\n            df[\"_product_total\"] = self._to_money(df[self.product_total_col]).fillna(0.0)\n        else:\n            df[\"_product_total\"] = np.nan\n\n        if self._safe_col(df, self.service_total_col):\n            df[\"_service_total\"] = self._to_money(df[self.service_total_col]).fillna(0.0)\n        else:\n            df[\"_service_total\"] = np.nan\n\n        if self._safe_col(df, self.tax_col):\n            df[\"_tax_total\"] = self._to_money(df[self.tax_col]).fillna(0.0)\n        else:\n            df[\"_tax_total\"] = np.nan\n\n        # Company normalization\n        if self._safe_col(df, self.company_col):\n            df[\"_company\"] = df[self.company_col].astype(str).str.strip()\n        else:\n            df[\"_company\"] = \"(unknown)\"\n        df[\"_company_key\"] = self._company_key(df[\"_company\"])\n\n        # Type, status, reference\n        df[\"_type\"] = df[self.invoice_type_col].astype(str).str.strip() if self._safe_col(df, self.invoice_type_col) else \"(unknown)\"\n        df[\"_status\"] = df[self.status_col].astype(str).str.strip() if self._safe_col(df, self.status_col) else \"(unknown)\"\n        df[\"_reference\"] = df[self.reference_col].astype(str).str.strip() if self._safe_col(df, self.reference_col) else \"\"\n\n        # Posted filter\n        if bool(self.exclude_non_posted) and self._safe_col(df, self.status_col):\n            allowed = set([x.lower() for x in self._parse_csv_list(self.posted_status_values)])\n            df = df[df[\"_status\"].astype(str).str.lower().isin(allowed)].copy()\n\n        # Credit memo sign\n        credit_vals = set([x.lower() for x in self._parse_csv_list(self.credit_type_values)])\n        is_credit = df[\"_type\"].astype(str).str.lower().isin(credit_vals)\n        df[\"_sign\"] = np.where(is_credit, -1.0, 1.0)\n\n        # Net revenue fields\n        df[\"revenue_net\"] = df[\"_invoice_total\"] * df[\"_sign\"]\n        df[\"product_net\"] = df[\"_product_total\"] * df[\"_sign\"] if df[\"_product_total\"].notna().any() else np.nan\n        df[\"service_net\"] = df[\"_service_total\"] * df[\"_sign\"] if df[\"_service_total\"].notna().any() else np.nan\n\n        # Month\n        if self._safe_col(df, self.date_col) and df[self.date_col].notna().any():\n            df[\"invoice_month\"] = df[self.date_col].dt.to_period(\"M\").astype(str)\n        else:\n            df[\"invoice_month\"] = None\n\n        return df\n\n    # -------------------------\n    # Outputs\n    # -------------------------\n\n    def build_kpi_overview(self) -> DataFrame:\n        df = self._prep_invoices()\n        if df.empty:\n            return DataFrame(pd.DataFrame())\n\n        total_invoices = int(len(df))\n        total_revenue = float(df[\"revenue_net\"].sum()) if \"revenue_net\" in df.columns else np.nan\n        credits_count = int((df[\"_sign\"] < 0).sum()) if \"_sign\" in df.columns else 0\n        avg_invoice = float(df[\"revenue_net\"].mean()) if total_invoices else np.nan\n        median_invoice = float(df[\"revenue_net\"].median()) if total_invoices else np.nan\n\n        out = pd.DataFrame([{\n            \"invoices_count\": total_invoices,\n            \"credits_count\": credits_count,\n            \"revenue_total_net\": total_revenue,\n            \"avg_invoice_net\": avg_invoice,\n            \"median_invoice_net\": median_invoice,\n            \"unique_clients\": int(df[\"_company_key\"].nunique()) if \"_company_key\" in df.columns else np.nan,\n        }])\n        return DataFrame(out)\n\n    def build_revenue_by_month(self) -> DataFrame:\n        df = self._prep_invoices()\n        if df.empty or \"invoice_month\" not in df.columns or df[\"invoice_month\"].isna().all():\n            return DataFrame(pd.DataFrame(columns=[\"invoice_month\", \"revenue_total_net\", \"invoices_count\"]))\n\n        g = df.groupby(\"invoice_month\", dropna=False)\n        out = pd.DataFrame({\n            \"invoice_month\": g.size().index.astype(str),\n            \"invoices_count\": g.size().values,\n            \"revenue_total_net\": g[\"revenue_net\"].sum().values,\n        })\n\n        if \"product_net\" in df.columns and df[\"product_net\"].notna().any():\n            out[\"product_total_net\"] = g[\"product_net\"].sum().values\n        if \"service_net\" in df.columns and df[\"service_net\"].notna().any():\n            out[\"service_total_net\"] = g[\"service_net\"].sum().values\n\n        out = out.sort_values(\"invoice_month\")\n        return DataFrame(out)\n\n    def build_revenue_by_client(self) -> DataFrame:\n        df = self._prep_invoices()\n        if df.empty:\n            return DataFrame(pd.DataFrame(columns=[\"company\", \"invoices_count\", \"revenue_total_net\", \"avg_invoice_net\"]))\n\n        g = df.groupby(\"_company_key\", dropna=False)\n        out = pd.DataFrame({\n            \"_company_key\": g.size().index.astype(str),\n            \"invoices_count\": g.size().values,\n            \"revenue_total_net\": g[\"revenue_net\"].sum().values,\n            \"avg_invoice_net\": g[\"revenue_net\"].mean().values,\n        })\n\n        # Representative display name\n        name_map = (\n            df.groupby(\"_company_key\")[\"_company\"]\n            .agg(lambda s: s.value_counts().index[0] if len(s.dropna()) else \"(unknown)\")\n            .rename(\"company\")\n            .reset_index()\n        )\n        out = out.merge(name_map, on=\"_company_key\", how=\"left\")\n\n        out = out.sort_values(\"revenue_total_net\", ascending=False).head(int(self.top_n))\n        out = out[[\"company\", \"invoices_count\", \"revenue_total_net\", \"avg_invoice_net\"]]\n        return DataFrame(out)\n\n    def build_revenue_by_type(self) -> DataFrame:\n        df = self._prep_invoices()\n        if df.empty:\n            return DataFrame(pd.DataFrame(columns=[\"invoice_type\", \"invoices_count\", \"revenue_total_net\", \"share\"]))\n\n        g = df.groupby(\"_type\", dropna=False)\n        out = pd.DataFrame({\n            \"invoice_type\": g.size().index.astype(str),\n            \"invoices_count\": g.size().values,\n            \"revenue_total_net\": g[\"revenue_net\"].sum().values,\n        })\n        total = float(out[\"revenue_total_net\"].abs().sum()) if len(out) else 0.0\n        out[\"share\"] = out[\"revenue_total_net\"].abs() / (total if total else np.nan)\n        out = out.sort_values(\"revenue_total_net\", ascending=False)\n        return DataFrame(out)\n\n    def build_revenue_concentration(self) -> DataFrame:\n        df = self._prep_invoices()\n        if df.empty:\n            return DataFrame(pd.DataFrame())\n\n        by = df.groupby(\"_company_key\", dropna=False)[\"revenue_net\"].sum().sort_values(ascending=False)\n        total = float(by.sum()) if len(by) else 0.0\n\n        def share_top(n: int) -> float:\n            if total == 0:\n                return np.nan\n            return float(by.head(n).sum() / total)\n\n        out = pd.DataFrame([{\n            \"clients_total\": int(by.shape[0]),\n            \"revenue_total_net\": total,\n            \"top_1_share\": share_top(1),\n            \"top_3_share\": share_top(3),\n            \"top_5_share\": share_top(5),\n            \"top_10_share\": share_top(10),\n        }])\n        return DataFrame(out)\n\n    def build_category_breakdown(self) -> DataFrame:\n        df = self._prep_invoices()\n        if df.empty or not self._safe_col(df, self.category_col):\n            return DataFrame(pd.DataFrame(columns=[\"category\", \"invoices_count\", \"revenue_total_net\", \"share\"]))\n\n        cat = df[self.category_col].fillna(\"(blank)\").astype(str).str.strip()\n        tmp = df.copy()\n        tmp[\"_category\"] = cat\n\n        g = tmp.groupby(\"_category\", dropna=False)\n        out = pd.DataFrame({\n            \"category\": g.size().index.astype(str),\n            \"invoices_count\": g.size().values,\n            \"revenue_total_net\": g[\"revenue_net\"].sum().values,\n        })\n        total = float(out[\"revenue_total_net\"].abs().sum()) if len(out) else 0.0\n        out[\"share\"] = out[\"revenue_total_net\"].abs() / (total if total else np.nan)\n        out = out.sort_values(\"revenue_total_net\", ascending=False)\n        return DataFrame(out)\n\n    def build_anomalies(self) -> DataFrame:\n        df = self._prep_invoices()\n        if df.empty:\n            return DataFrame(pd.DataFrame(columns=[\"date\", \"company\", \"invoice_type\", \"revenue_net\", \"reference\"]))\n\n        # Large invoices\n        x = df[\"revenue_net\"].replace([np.inf, -np.inf], np.nan).dropna()\n        if len(x) < 10:\n            thresh = x.abs().quantile(0.90) if len(x) else np.nan\n        else:\n            thresh = x.abs().quantile(0.95)\n\n        an = df[df[\"revenue_net\"].abs() >= float(thresh) if pd.notna(thresh) else False].copy()\n        an = an.sort_values(\"revenue_net\", ascending=False).head(int(self.anomaly_top_n))\n\n        # Friendly columns\n        out = pd.DataFrame({\n            \"date\": an[self.date_col] if self._safe_col(an, self.date_col) else pd.NaT,\n            \"company\": an[\"_company\"],\n            \"invoice_type\": an[\"_type\"],\n            \"revenue_net\": an[\"revenue_net\"],\n            \"reference\": an[\"_reference\"],\n        })\n        return DataFrame(out)\n\n    # -------------------------\n    # Insights context\n    # -------------------------\n\n    def build_insights_context(self) -> Message:\n        kpi = pd.DataFrame(self.build_kpi_overview())\n        by_month = pd.DataFrame(self.build_revenue_by_month())\n        by_client = pd.DataFrame(self.build_revenue_by_client())\n        by_type = pd.DataFrame(self.build_revenue_by_type())\n        conc = pd.DataFrame(self.build_revenue_concentration())\n        cats = pd.DataFrame(self.build_category_breakdown())\n        anom = pd.DataFrame(self.build_anomalies())\n\n        def fmt_money(x) -> str:\n            return \"n/a\" if pd.isna(x) else f\"${float(x):,.2f}\"\n\n        def fmt_pct(x) -> str:\n            return \"n/a\" if pd.isna(x) else f\"{float(x) * 100:.1f}%\"\n\n        lines: List[str] = []\n        lines.append(\"INVOICE DATASET OVERVIEW\")\n        if not kpi.empty:\n            r = kpi.iloc[0].to_dict()\n            lines.append(f\"invoices_count={r.get('invoices_count')}\")\n            lines.append(f\"unique_clients={r.get('unique_clients')}\")\n            lines.append(f\"credits_count={r.get('credits_count')}\")\n            lines.append(f\"revenue_total_net={fmt_money(r.get('revenue_total_net'))}\")\n            lines.append(f\"avg_invoice_net={fmt_money(r.get('avg_invoice_net'))}\")\n            lines.append(f\"median_invoice_net={fmt_money(r.get('median_invoice_net'))}\")\n        lines.append(\"\")\n\n        lines.append(\"REVENUE CONCENTRATION\")\n        if not conc.empty:\n            r = conc.iloc[0].to_dict()\n            lines.append(f\"top_1_share={fmt_pct(r.get('top_1_share'))}\")\n            lines.append(f\"top_3_share={fmt_pct(r.get('top_3_share'))}\")\n            lines.append(f\"top_10_share={fmt_pct(r.get('top_10_share'))}\")\n        lines.append(\"\")\n\n        lines.append(\"TOP CLIENTS BY REVENUE (net)\")\n        if by_client.empty:\n            lines.append(\"  (no data)\\n\")\n        else:\n            for _, row in by_client.head(min(len(by_client), int(self.top_n))).iterrows():\n                lines.append(f\"  - {row.get('company')}: revenue_total_net={fmt_money(row.get('revenue_total_net'))}, invoices={int(row.get('invoices_count'))}\")\n\n        lines.append(\"\")\n        lines.append(\"REVENUE BY INVOICE TYPE\")\n        if by_type.empty:\n            lines.append(\"  (no data)\\n\")\n        else:\n            for _, row in by_type.iterrows():\n                lines.append(f\"  - {row.get('invoice_type')}: revenue_total_net={fmt_money(row.get('revenue_total_net'))}, share={fmt_pct(row.get('share'))}\")\n\n        if not cats.empty and (cats.shape[0] > 0) and (cats[\"category\"].notna().any()):\n            lines.append(\"\")\n            lines.append(\"CATEGORY BREAKDOWN\")\n            for _, row in cats.iterrows():\n                lines.append(f\"  - {row.get('category')}: revenue_total_net={fmt_money(row.get('revenue_total_net'))}, share={fmt_pct(row.get('share'))}\")\n\n        # Seasonality note\n        if not by_month.empty and \"revenue_total_net\" in by_month.columns:\n            lines.append(\"\")\n            peak = by_month.sort_values(\"revenue_total_net\", ascending=False).head(1)\n            trough = by_month.sort_values(\"revenue_total_net\", ascending=True).head(1)\n            if len(peak) == 1:\n                lines.append(f\"PEAK MONTH: {peak.iloc[0].get('invoice_month')} revenue_total_net={fmt_money(peak.iloc[0].get('revenue_total_net'))}\")\n            if len(trough) == 1:\n                lines.append(f\"LOW MONTH: {trough.iloc[0].get('invoice_month')} revenue_total_net={fmt_money(trough.iloc[0].get('revenue_total_net'))}\")\n\n        # Anomalies\n        lines.append(\"\")\n        lines.append(\"NOTABLE LARGE INVOICES / CREDIT EVENTS\")\n        if anom.empty:\n            lines.append(\"  (no large anomalies detected)\\n\")\n        else:\n            for _, row in anom.iterrows():\n                dt = row.get(\"date\")\n                dt_s = \"n/a\" if pd.isna(dt) else str(pd.to_datetime(dt).date())\n                ref = str(row.get(\"reference\") or \"\").strip()\n                ref = (ref[:90] + \"…\") if len(ref) > 90 else ref\n                lines.append(f\"  - {dt_s} {row.get('company')} {row.get('invoice_type')}: revenue_net={fmt_money(row.get('revenue_net'))} ref={ref}\")\n\n        if bool(self.show_debug):\n            lines.append(\"\")\n            lines.append(self.build_data_diagnostics().text)\n\n        return Message(text=\"\\n\".join(lines))\n\n    # -------------------------\n    # Diagnostics\n    # -------------------------\n\n    def build_data_diagnostics(self) -> Message:\n        df = self._unwrap_to_df(self.invoices_df)\n        lines: List[str] = []\n        lines.append(\"DATA DIAGNOSTICS\")\n        lines.append(f\"invoices_rows={len(df)} cols={len(df.columns)}\")\n        lines.append(\"columns_sample=\" + str(list(df.columns)[:40]))\n        for c in [self.date_col, self.company_col, self.invoice_total_col, self.invoice_type_col, self.status_col, self.reference_col, self.category_col]:\n            if c and c in df.columns:\n                lines.append(f\"col_present: {c}=true\")\n            else:\n                lines.append(f\"col_present: {c}=false\")\n        return Message(text=\"\\n\".join(lines))"
              },
              "company_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Company column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "company_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Company Name"
              },
              "credit_type_values": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Credit memo type values (comma-separated)",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "credit_type_values",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Credit Memo,Credit,CM"
              },
              "date_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Invoice date column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "date_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Date"
              },
              "exclude_non_posted": {
                "_input_type": "BoolInput",
                "advanced": false,
                "display_name": "Exclude non-posted invoices",
                "dynamic": false,
                "info": "",
                "list": false,
                "list_add_label": "Add More",
                "name": "exclude_non_posted",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "bool",
                "value": false
              },
              "invoice_total_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Invoice total column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "invoice_total_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Invoice Total"
              },
              "invoice_type_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Invoice type column (optional)",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "invoice_type_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Invoice Type"
              },
              "invoices_df": {
                "_input_type": "DataFrameInput",
                "advanced": false,
                "display_name": "Invoices (Clean) DataFrame",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "DataFrame"
                ],
                "list": false,
                "list_add_label": "Add More",
                "name": "invoices_df",
                "placeholder": "",
                "required": true,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "other",
                "value": ""
              },
              "posted_status_values": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Posted status values (comma-separated)",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "posted_status_values",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Posted,Paid,Closed"
              },
              "product_total_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Product total column (optional)",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "product_total_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Product Total"
              },
              "reference_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Invoice reference/summary column (optional)",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "reference_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Invoice Reference"
              },
              "service_total_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Service total column (optional)",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "service_total_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Service Total"
              },
              "show_debug": {
                "_input_type": "BoolInput",
                "advanced": false,
                "display_name": "Include debug details in insights output",
                "dynamic": false,
                "info": "",
                "list": false,
                "list_add_label": "Add More",
                "name": "show_debug",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "bool",
                "value": false
              },
              "status_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Status column (optional)",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "status_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Status"
              },
              "tax_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Sales tax column (optional)",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "tax_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Sales Tax"
              },
              "top_n": {
                "_input_type": "IntInput",
                "advanced": false,
                "display_name": "Top N rows",
                "dynamic": false,
                "info": "",
                "list": false,
                "list_add_label": "Add More",
                "name": "top_n",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "int",
                "value": 15
              }
            },
            "tool_mode": false
          },
          "selected_output": "insights_context",
          "showNode": true,
          "type": "msp_invoice_metrics_builder"
        },
        "dragging": false,
        "id": "msp_invoice_metrics_builder-StB0j",
        "measured": {
          "height": 1429,
          "width": 320
        },
        "position": {
          "x": 2688.7234927475074,
          "y": -2678.058868708081
        },
        "selected": false,
        "type": "genericNode"
      },
      {
        "data": {
          "id": "Prompt Template-KySSd",
          "node": {
            "base_classes": [
              "Message"
            ],
            "beta": false,
            "conditional_paths": [],
            "custom_fields": {
              "template": [
                "revenue_breakdown",
                "insights"
              ]
            },
            "description": "Create a prompt template with dynamic variables.",
            "display_name": "Prompt Template",
            "documentation": "https://docs.langflow.org/components-prompts",
            "edited": false,
            "error": null,
            "field_order": [
              "template",
              "tool_placeholder"
            ],
            "frozen": false,
            "full_path": null,
            "icon": "braces",
            "is_composition": null,
            "is_input": null,
            "is_output": null,
            "legacy": false,
            "lf_version": "1.6.0",
            "metadata": {},
            "minimized": false,
            "name": "",
            "output_types": [],
            "outputs": [
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Prompt",
                "group_outputs": false,
                "hidden": null,
                "method": "build_prompt",
                "name": "prompt",
                "options": null,
                "required_inputs": null,
                "selected": "Message",
                "tool_mode": true,
                "types": [
                  "Message"
                ],
                "value": "__UNDEFINED__"
              }
            ],
            "pinned": false,
            "priority": 0,
            "replacement": null,
            "template": {
              "_type": "Component",
              "code": {
                "advanced": true,
                "dynamic": true,
                "fileTypes": [],
                "file_path": "",
                "info": "",
                "list": false,
                "load_from_db": false,
                "multiline": true,
                "name": "code",
                "password": false,
                "placeholder": "",
                "required": true,
                "show": true,
                "title_case": false,
                "type": "code",
                "value": "from langflow.base.prompts.api_utils import process_prompt_template\nfrom langflow.custom.custom_component.component import Component\nfrom langflow.inputs.inputs import DefaultPromptField\nfrom langflow.io import MessageTextInput, Output, PromptInput\nfrom langflow.schema.message import Message\nfrom langflow.template.utils import update_template_values\n\n\nclass PromptComponent(Component):\n    display_name: str = \"Prompt Template\"\n    description: str = \"Create a prompt template with dynamic variables.\"\n    documentation: str = \"https://docs.langflow.org/components-prompts\"\n    icon = \"braces\"\n    trace_type = \"prompt\"\n    name = \"Prompt Template\"\n    priority = 0  # Set priority to 0 to make it appear first\n\n    inputs = [\n        PromptInput(name=\"template\", display_name=\"Template\"),\n        MessageTextInput(\n            name=\"tool_placeholder\",\n            display_name=\"Tool Placeholder\",\n            tool_mode=True,\n            advanced=True,\n            info=\"A placeholder input for tool mode.\",\n        ),\n    ]\n\n    outputs = [\n        Output(display_name=\"Prompt\", name=\"prompt\", method=\"build_prompt\"),\n    ]\n\n    async def build_prompt(self) -> Message:\n        prompt = Message.from_template(**self._attributes)\n        self.status = prompt.text\n        return prompt\n\n    def _update_template(self, frontend_node: dict):\n        prompt_template = frontend_node[\"template\"][\"template\"][\"value\"]\n        custom_fields = frontend_node[\"custom_fields\"]\n        frontend_node_template = frontend_node[\"template\"]\n        _ = process_prompt_template(\n            template=prompt_template,\n            name=\"template\",\n            custom_fields=custom_fields,\n            frontend_node_template=frontend_node_template,\n        )\n        return frontend_node\n\n    async def update_frontend_node(self, new_frontend_node: dict, current_frontend_node: dict):\n        \"\"\"This function is called after the code validation is done.\"\"\"\n        frontend_node = await super().update_frontend_node(new_frontend_node, current_frontend_node)\n        template = frontend_node[\"template\"][\"template\"][\"value\"]\n        # Kept it duplicated for backwards compatibility\n        _ = process_prompt_template(\n            template=template,\n            name=\"template\",\n            custom_fields=frontend_node[\"custom_fields\"],\n            frontend_node_template=frontend_node[\"template\"],\n        )\n        # Now that template is updated, we need to grab any values that were set in the current_frontend_node\n        # and update the frontend_node with those values\n        update_template_values(new_template=frontend_node, previous_template=current_frontend_node[\"template\"])\n        return frontend_node\n\n    def _get_fallback_input(self, **kwargs):\n        return DefaultPromptField(**kwargs)\n"
              },
              "insights": {
                "advanced": false,
                "display_name": "insights",
                "dynamic": false,
                "field_type": "str",
                "fileTypes": [],
                "file_path": "",
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "load_from_db": false,
                "multiline": true,
                "name": "insights",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "type": "str",
                "value": ""
              },
              "revenue_breakdown": {
                "advanced": false,
                "display_name": "revenue_breakdown",
                "dynamic": false,
                "field_type": "str",
                "fileTypes": [],
                "file_path": "",
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "load_from_db": false,
                "multiline": true,
                "name": "revenue_breakdown",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "type": "str",
                "value": ""
              },
              "template": {
                "_input_type": "PromptInput",
                "advanced": false,
                "display_name": "Template",
                "dynamic": false,
                "info": "",
                "list": false,
                "list_add_label": "Add More",
                "name": "template",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "type": "prompt",
                "value": "{revenue_breakdown}\n{insights}"
              },
              "tool_placeholder": {
                "_input_type": "MessageTextInput",
                "advanced": true,
                "display_name": "Tool Placeholder",
                "dynamic": false,
                "info": "A placeholder input for tool mode.",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "tool_placeholder",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": true,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": ""
              }
            },
            "tool_mode": false
          },
          "showNode": true,
          "type": "Prompt Template"
        },
        "dragging": false,
        "id": "Prompt Template-KySSd",
        "measured": {
          "height": 379,
          "width": 320
        },
        "position": {
          "x": 3395.5372968179836,
          "y": -1431.834080936179
        },
        "selected": false,
        "type": "genericNode"
      },
      {
        "data": {
          "id": "msp_invoice_llm_context_builder-5yIEm",
          "node": {
            "base_classes": [
              "Message"
            ],
            "beta": false,
            "conditional_paths": [],
            "custom_fields": {},
            "description": "Converts invoice rollup tables into a compact LLM-friendly context summary.",
            "display_name": "MSP Invoice LLM Context Builder",
            "documentation": "",
            "edited": true,
            "field_order": [
              "revenue_by_month",
              "revenue_by_client",
              "revenue_by_type",
              "revenue_concentration",
              "category_breakdown",
              "top_n_clients",
              "top_n_categories",
              "recent_months",
              "anomaly_top_n",
              "include_debug"
            ],
            "frozen": false,
            "icon": "MessageSquareText",
            "legacy": false,
            "lf_version": "1.6.0",
            "metadata": {},
            "minimized": false,
            "output_types": [],
            "outputs": [
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "LLM Context",
                "group_outputs": false,
                "hidden": null,
                "method": "build_llm_context",
                "name": "llm_context",
                "options": null,
                "required_inputs": null,
                "selected": "Message",
                "tool_mode": true,
                "types": [
                  "Message"
                ],
                "value": "__UNDEFINED__"
              }
            ],
            "pinned": false,
            "template": {
              "_type": "Component",
              "anomaly_top_n": {
                "_input_type": "IntInput",
                "advanced": false,
                "display_name": "Top N anomalies",
                "dynamic": false,
                "info": "",
                "list": false,
                "list_add_label": "Add More",
                "name": "anomaly_top_n",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "int",
                "value": 5
              },
              "category_breakdown": {
                "_input_type": "DataFrameInput",
                "advanced": false,
                "display_name": "Category Breakdown",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "DataFrame"
                ],
                "list": false,
                "list_add_label": "Add More",
                "name": "category_breakdown",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "other",
                "value": ""
              },
              "code": {
                "advanced": true,
                "dynamic": true,
                "fileTypes": [],
                "file_path": "",
                "info": "",
                "list": false,
                "load_from_db": false,
                "multiline": true,
                "name": "code",
                "password": false,
                "placeholder": "",
                "required": true,
                "show": true,
                "title_case": false,
                "type": "code",
                "value": "from __future__ import annotations\n\nfrom typing import Any, List\nimport pandas as pd\nimport numpy as np\n\nfrom langflow.custom import Component\nfrom langflow.io import DataFrameInput, IntInput, BoolInput, Output\nfrom langflow.schema.message import Message\n\n\nclass MSPInvoiceLLMContextBuilder(Component):\n    display_name = \"MSP Invoice LLM Context Builder\"\n    description = \"Converts invoice rollup tables into a compact LLM-friendly context summary.\"\n    name = \"msp_invoice_llm_context_builder\"\n    icon = \"MessageSquareText\"\n\n    inputs = [\n        DataFrameInput(name=\"revenue_by_month\", display_name=\"Revenue by Month\", required=False),\n        DataFrameInput(name=\"revenue_by_client\", display_name=\"Revenue by Client\", required=False),\n        DataFrameInput(name=\"revenue_by_type\", display_name=\"Revenue by Type\", required=False),\n        DataFrameInput(name=\"revenue_concentration\", display_name=\"Revenue Concentration\", required=False),\n        DataFrameInput(name=\"category_breakdown\", display_name=\"Category Breakdown\", required=False),\n\n        IntInput(name=\"top_n_clients\", display_name=\"Top N clients to list\", value=10),\n        IntInput(name=\"top_n_categories\", display_name=\"Top N categories to list\", value=10),\n        IntInput(name=\"recent_months\", display_name=\"Recent months to list\", value=6),\n\n        # NEW: anomaly controls\n        IntInput(name=\"anomaly_top_n\", display_name=\"Top N anomalies\", value=5),\n        BoolInput(name=\"include_debug\", display_name=\"Include debug diagnostics\", value=False),\n    ]\n\n    outputs = [\n        Output(name=\"llm_context\", display_name=\"LLM Context\", method=\"build_llm_context\"),\n    ]\n\n    # -------------------------\n    # Helpers\n    # -------------------------\n\n    def _unwrap_to_df(self, obj: Any) -> pd.DataFrame:\n        if obj is None:\n            return pd.DataFrame()\n        if isinstance(obj, pd.DataFrame):\n            return obj.copy()\n        if hasattr(obj, \"data\"):\n            d = getattr(obj, \"data\")\n            if isinstance(d, pd.DataFrame):\n                return d.copy()\n            if isinstance(d, list):\n                return pd.DataFrame(d)\n        return pd.DataFrame(obj)\n\n    def _fmt_money(self, x) -> str:\n        try:\n            if pd.isna(x):\n                return \"n/a\"\n            return f\"${float(x):,.2f}\"\n        except Exception:\n            return \"n/a\"\n\n    def _fmt_pct(self, x) -> str:\n        try:\n            if pd.isna(x):\n                return \"n/a\"\n            return f\"{float(x)*100:.1f}%\"\n        except Exception:\n            return \"n/a\"\n\n    def _pick_col(self, df: pd.DataFrame, candidates: List[str]) -> str:\n        for c in candidates:\n            if c in df.columns:\n                return c\n        return \"\"\n\n    def _robust_z(self, x: pd.Series) -> pd.Series:\n        \"\"\"\n        Robust z-score using MAD. Returns approx std-normal z values.\n        If MAD is 0, returns zeros.\n        \"\"\"\n        x = pd.to_numeric(x, errors=\"coerce\")\n        med = x.median()\n        mad = (x - med).abs().median()\n        if pd.isna(mad) or mad == 0:\n            return pd.Series([0.0] * len(x), index=x.index)\n        return 0.6745 * (x - med) / mad\n\n    # -------------------------\n    # Output\n    # -------------------------\n\n    def build_llm_context(self) -> Message:\n        by_month = self._unwrap_to_df(self.revenue_by_month)\n        by_client = self._unwrap_to_df(self.revenue_by_client)\n        by_type = self._unwrap_to_df(self.revenue_by_type)\n        conc = self._unwrap_to_df(self.revenue_concentration)\n        cats = self._unwrap_to_df(self.category_breakdown)\n\n        lines: List[str] = []\n        lines.append(\"INVOICE ROLLUP CONTEXT\")\n\n        # Concentration\n        if not conc.empty:\n            top3 = conc.iloc[0].get(\"top_3_share\", np.nan)\n            top10 = conc.iloc[0].get(\"top_10_share\", np.nan)\n            total = conc.iloc[0].get(\"revenue_total_net\", conc.iloc[0].get(\"revenue_total\", np.nan))\n            lines.append(\"\")\n            lines.append(\"CONCENTRATION\")\n            lines.append(f\"- total_revenue_net: {self._fmt_money(total)}\")\n            lines.append(f\"- top_3_share: {self._fmt_pct(top3)}\")\n            lines.append(f\"- top_10_share: {self._fmt_pct(top10)}\")\n\n        # Categories\n        if not cats.empty:\n            cat_col = self._pick_col(cats, [\"category\", \"Category\"])\n            rev_col = self._pick_col(cats, [\"revenue_net\", \"revenue_total_net\", \"revenue_total\"])\n            share_col = self._pick_col(cats, [\"share\", \"Share\"])\n            count_col = self._pick_col(cats, [\"invoice_count\", \"invoices_count\", \"count\"])\n\n            lines.append(\"\")\n            lines.append(\"CATEGORY BREAKDOWN (top)\")\n            tmp = cats.copy()\n            if rev_col:\n                tmp = tmp.sort_values(rev_col, ascending=False)\n            tmp = tmp.head(int(self.top_n_categories))\n\n            for _, r in tmp.iterrows():\n                cat = r.get(cat_col, \"(unknown)\") if cat_col else \"(unknown)\"\n                rev = r.get(rev_col, np.nan) if rev_col else np.nan\n                share = r.get(share_col, np.nan) if share_col else np.nan\n                cnt = r.get(count_col, np.nan) if count_col else np.nan\n                extra = []\n                if pd.notna(cnt):\n                    try:\n                        extra.append(f\"invoices={int(cnt)}\")\n                    except Exception:\n                        pass\n                if share_col:\n                    extra.append(f\"share={self._fmt_pct(share)}\")\n                extra_s = (\", \" + \", \".join(extra)) if extra else \"\"\n                lines.append(f\"- {cat}: revenue_net={self._fmt_money(rev)}{extra_s}\")\n\n        # Types\n        if not by_type.empty:\n            t_col = self._pick_col(by_type, [\"invoice_type\", \"Invoice Type\", \"_type\"])\n            rev_col = self._pick_col(by_type, [\"revenue_net\", \"revenue_total_net\", \"revenue_total\"])\n            share_col = self._pick_col(by_type, [\"share\"])\n            count_col = self._pick_col(by_type, [\"invoice_count\", \"invoices_count\"])\n\n            lines.append(\"\")\n            lines.append(\"INVOICE TYPES (sorted by revenue)\")\n            tmp = by_type.copy()\n            if rev_col:\n                tmp = tmp.sort_values(rev_col, ascending=False)\n            for _, r in tmp.iterrows():\n                t = r.get(t_col, \"(unknown)\") if t_col else \"(unknown)\"\n                rev = r.get(rev_col, np.nan) if rev_col else np.nan\n                share = r.get(share_col, np.nan) if share_col else np.nan\n                cnt = r.get(count_col, np.nan) if count_col else np.nan\n                bits = [f\"revenue_net={self._fmt_money(rev)}\"]\n                if pd.notna(cnt):\n                    try:\n                        bits.append(f\"invoices={int(cnt)}\")\n                    except Exception:\n                        pass\n                if share_col:\n                    bits.append(f\"share={self._fmt_pct(share)}\")\n                lines.append(f\"- {t}: \" + \", \".join(bits))\n\n        # Recent months + anomalies (NEW)\n        if not by_month.empty:\n            m_col = self._pick_col(by_month, [\"invoice_month\", \"month\"])\n            rev_col = self._pick_col(by_month, [\"revenue_total_net\", \"revenue_net\", \"revenue_total\"])\n            p_col = self._pick_col(by_month, [\"product_total_net\", \"product_net\", \"product_total\"])\n            s_col = self._pick_col(by_month, [\"service_total_net\", \"service_net\", \"service_total\"])\n            c_col = self._pick_col(by_month, [\"invoices_count\", \"invoice_count\", \"count\"])\n\n            tmp = by_month.copy()\n            if m_col:\n                tmp[m_col] = tmp[m_col].astype(str)\n                tmp = tmp.sort_values(m_col)\n\n            # Recent months section\n            lines.append(\"\")\n            lines.append(\"RECENT MONTHS\")\n            recent = tmp.tail(int(self.recent_months))\n            for _, r in recent.iterrows():\n                m = r.get(m_col, \"(unknown)\") if m_col else \"(unknown)\"\n                rev = r.get(rev_col, np.nan) if rev_col else np.nan\n                parts = [f\"revenue_net={self._fmt_money(rev)}\"]\n                if p_col:\n                    parts.append(f\"product_net={self._fmt_money(r.get(p_col, np.nan))}\")\n                if s_col:\n                    parts.append(f\"service_net={self._fmt_money(r.get(s_col, np.nan))}\")\n                if c_col and pd.notna(r.get(c_col, np.nan)):\n                    try:\n                        parts.append(f\"invoices={int(r.get(c_col))}\")\n                    except Exception:\n                        pass\n                lines.append(f\"- {m}: \" + \", \".join(parts))\n\n            # Revenue anomalies section\n            if rev_col and m_col and tmp[rev_col].notna().sum() >= 4:\n                series = pd.to_numeric(tmp[rev_col], errors=\"coerce\")\n                rz = self._robust_z(series)\n                tmp[\"_rev\"] = series\n                tmp[\"_rz\"] = rz\n\n                # Big outlier months (absolute)\n                outliers = tmp.loc[tmp[\"_rev\"].notna()].copy()\n                outliers[\"abs_rz\"] = outliers[\"_rz\"].abs()\n                outliers = outliers.sort_values(\"abs_rz\", ascending=False).head(int(self.anomaly_top_n))\n\n                # MoM deltas (spikes)\n                deltas = tmp.loc[tmp[\"_rev\"].notna(), [m_col, \"_rev\"]].copy()\n                deltas[\"_delta\"] = deltas[\"_rev\"].diff()\n                # largest positive and negative\n                pos = deltas.sort_values(\"_delta\", ascending=False).head(max(1, int(self.anomaly_top_n // 2)))\n                neg = deltas.sort_values(\"_delta\", ascending=True).head(max(1, int(self.anomaly_top_n // 2)))\n\n                lines.append(\"\")\n                lines.append(\"REVENUE ANOMALIES\")\n\n                if len(outliers) > 0:\n                    lines.append(\"- Outlier months (unusual vs typical):\")\n                    for _, r in outliers.iterrows():\n                        lines.append(\n                            f\"  - {r.get(m_col)}: revenue_net={self._fmt_money(r.get('_rev'))}, \"\n                            f\"robust_z={float(r.get('_rz')):.2f}\"\n                        )\n\n                # Only show deltas if we have at least 2 rows\n                if deltas[\"_delta\"].notna().sum() >= 1:\n                    lines.append(\"- Largest month-over-month increases:\")\n                    for _, r in pos.iterrows():\n                        if pd.isna(r.get(\"_delta\")):\n                            continue\n                        lines.append(\n                            f\"  - {r.get(m_col)}: Δ={self._fmt_money(r.get('_delta'))} \"\n                            f\"(to {self._fmt_money(r.get('_rev'))})\"\n                        )\n\n                    lines.append(\"- Largest month-over-month decreases:\")\n                    for _, r in neg.iterrows():\n                        if pd.isna(r.get(\"_delta\")):\n                            continue\n                        lines.append(\n                            f\"  - {r.get(m_col)}: Δ={self._fmt_money(r.get('_delta'))} \"\n                            f\"(to {self._fmt_money(r.get('_rev'))})\"\n                        )\n\n        # Top clients\n        if not by_client.empty:\n            c_col = self._pick_col(by_client, [\"company\", \"Company Name\", \"_company\"])\n            rev_col = self._pick_col(by_client, [\"revenue_total_net\", \"revenue_net\", \"revenue_total\"])\n            count_col = self._pick_col(by_client, [\"invoices_count\", \"invoice_count\"])\n            avg_col = self._pick_col(by_client, [\"avg_invoice_net\", \"avg_invoice\"])\n\n            lines.append(\"\")\n            lines.append(\"TOP CLIENTS (by revenue)\")\n            tmp = by_client.copy()\n            if rev_col:\n                tmp = tmp.sort_values(rev_col, ascending=False)\n            tmp = tmp.head(int(self.top_n_clients))\n\n            for _, r in tmp.iterrows():\n                name = r.get(c_col, \"(unknown)\") if c_col else \"(unknown)\"\n                rev = r.get(rev_col, np.nan) if rev_col else np.nan\n                cnt = r.get(count_col, np.nan) if count_col else np.nan\n                avg = r.get(avg_col, np.nan) if avg_col else np.nan\n                bits = [f\"revenue_net={self._fmt_money(rev)}\"]\n                if pd.notna(cnt):\n                    try:\n                        bits.append(f\"invoices={int(cnt)}\")\n                    except Exception:\n                        pass\n                if avg_col:\n                    bits.append(f\"avg_invoice={self._fmt_money(avg)}\")\n                lines.append(f\"- {name}: \" + \", \".join(bits))\n\n        if bool(self.include_debug):\n            lines.append(\"\")\n            lines.append(\"DEBUG\")\n            lines.append(f\"- by_month_rows={len(by_month)} cols={list(by_month.columns)}\")\n            lines.append(f\"- by_client_rows={len(by_client)} cols={list(by_client.columns)}\")\n            lines.append(f\"- by_type_rows={len(by_type)} cols={list(by_type.columns)}\")\n            lines.append(f\"- conc_rows={len(conc)} cols={list(conc.columns)}\")\n            lines.append(f\"- cats_rows={len(cats)} cols={list(cats.columns)}\")\n\n        return Message(text=\"\\n\".join(lines))"
              },
              "include_debug": {
                "_input_type": "BoolInput",
                "advanced": false,
                "display_name": "Include debug diagnostics",
                "dynamic": false,
                "info": "",
                "list": false,
                "list_add_label": "Add More",
                "name": "include_debug",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "bool",
                "value": false
              },
              "recent_months": {
                "_input_type": "IntInput",
                "advanced": false,
                "display_name": "Recent months to list",
                "dynamic": false,
                "info": "",
                "list": false,
                "list_add_label": "Add More",
                "name": "recent_months",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "int",
                "value": 6
              },
              "revenue_by_client": {
                "_input_type": "DataFrameInput",
                "advanced": false,
                "display_name": "Revenue by Client",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "DataFrame"
                ],
                "list": false,
                "list_add_label": "Add More",
                "name": "revenue_by_client",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "other",
                "value": ""
              },
              "revenue_by_month": {
                "_input_type": "DataFrameInput",
                "advanced": false,
                "display_name": "Revenue by Month",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "DataFrame"
                ],
                "list": false,
                "list_add_label": "Add More",
                "name": "revenue_by_month",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "other",
                "value": ""
              },
              "revenue_by_type": {
                "_input_type": "DataFrameInput",
                "advanced": false,
                "display_name": "Revenue by Type",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "DataFrame"
                ],
                "list": false,
                "list_add_label": "Add More",
                "name": "revenue_by_type",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "other",
                "value": ""
              },
              "revenue_concentration": {
                "_input_type": "DataFrameInput",
                "advanced": false,
                "display_name": "Revenue Concentration",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "DataFrame"
                ],
                "list": false,
                "list_add_label": "Add More",
                "name": "revenue_concentration",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "other",
                "value": ""
              },
              "top_n_categories": {
                "_input_type": "IntInput",
                "advanced": false,
                "display_name": "Top N categories to list",
                "dynamic": false,
                "info": "",
                "list": false,
                "list_add_label": "Add More",
                "name": "top_n_categories",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "int",
                "value": 10
              },
              "top_n_clients": {
                "_input_type": "IntInput",
                "advanced": false,
                "display_name": "Top N clients to list",
                "dynamic": false,
                "info": "",
                "list": false,
                "list_add_label": "Add More",
                "name": "top_n_clients",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "int",
                "value": 10
              }
            },
            "tool_mode": false
          },
          "showNode": true,
          "type": "msp_invoice_llm_context_builder"
        },
        "dragging": false,
        "id": "msp_invoice_llm_context_builder-5yIEm",
        "measured": {
          "height": 727,
          "width": 320
        },
        "position": {
          "x": 2781.3066868989877,
          "y": -931.5336990420248
        },
        "selected": false,
        "type": "genericNode"
      },
      {
        "data": {
          "id": "Agent-CdUIZ",
          "node": {
            "base_classes": [
              "Message"
            ],
            "beta": false,
            "conditional_paths": [],
            "custom_fields": {},
            "description": "Define the agent's instructions, then enter a task to complete using tools.",
            "display_name": "Agent",
            "documentation": "https://docs.langflow.org/agents",
            "edited": false,
            "field_order": [
              "agent_llm",
              "max_tokens",
              "model_kwargs",
              "model_name",
              "openai_api_base",
              "api_key",
              "temperature",
              "seed",
              "max_retries",
              "timeout",
              "system_prompt",
              "n_messages",
              "format_instructions",
              "output_schema",
              "tools",
              "input_value",
              "handle_parsing_errors",
              "verbose",
              "max_iterations",
              "agent_description",
              "add_current_date_tool"
            ],
            "frozen": false,
            "icon": "bot",
            "last_updated": "2025-12-23T01:31:42.744Z",
            "legacy": false,
            "lf_version": "1.6.0",
            "metadata": {},
            "minimized": false,
            "output_types": [],
            "outputs": [
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Response",
                "group_outputs": false,
                "method": "message_response",
                "name": "response",
                "options": null,
                "required_inputs": null,
                "selected": "Message",
                "tool_mode": true,
                "types": [
                  "Message"
                ],
                "value": "__UNDEFINED__"
              }
            ],
            "pinned": false,
            "template": {
              "_type": "Component",
              "add_current_date_tool": {
                "_input_type": "BoolInput",
                "advanced": true,
                "display_name": "Current Date",
                "dynamic": false,
                "info": "If true, will add a tool to the agent that returns the current date.",
                "list": false,
                "list_add_label": "Add More",
                "name": "add_current_date_tool",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "bool",
                "value": true
              },
              "agent_description": {
                "_input_type": "MultilineInput",
                "advanced": true,
                "copy_field": false,
                "display_name": "Agent Description [Deprecated]",
                "dynamic": false,
                "info": "The description of the agent. This is only used when in Tool Mode. Defaults to 'A helpful assistant with access to the following tools:' and tools are added dynamically. This feature is deprecated and will be removed in future versions.",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "multiline": true,
                "name": "agent_description",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "A helpful assistant with access to the following tools:"
              },
              "agent_llm": {
                "_input_type": "DropdownInput",
                "advanced": false,
                "combobox": false,
                "dialog_inputs": {},
                "display_name": "Model Provider",
                "dynamic": false,
                "external_options": {
                  "fields": {
                    "data": {
                      "node": {
                        "display_name": "Connect other models",
                        "icon": "CornerDownLeft",
                        "name": "connect_other_models"
                      }
                    }
                  }
                },
                "info": "The provider of the language model that the agent will use to generate responses.",
                "input_types": [],
                "name": "agent_llm",
                "options": [
                  "Anthropic",
                  "Google Generative AI",
                  "OpenAI"
                ],
                "options_metadata": [
                  {
                    "icon": "Anthropic"
                  },
                  {
                    "icon": "GoogleGenerativeAI"
                  },
                  {
                    "icon": "OpenAI"
                  }
                ],
                "placeholder": "",
                "real_time_refresh": true,
                "refresh_button": false,
                "required": false,
                "show": true,
                "title_case": false,
                "toggle": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "str",
                "value": "OpenAI"
              },
              "api_key": {
                "_input_type": "SecretStrInput",
                "advanced": false,
                "display_name": "OpenAI API Key",
                "dynamic": false,
                "info": "The OpenAI API Key to use for the OpenAI model.",
                "input_types": [],
                "load_from_db": false,
                "name": "api_key",
                "password": true,
                "placeholder": "",
                "real_time_refresh": true,
                "required": false,
                "show": true,
                "title_case": false,
                "type": "str",
                "value": ""
              },
              "code": {
                "advanced": true,
                "dynamic": true,
                "fileTypes": [],
                "file_path": "",
                "info": "",
                "list": false,
                "load_from_db": false,
                "multiline": true,
                "name": "code",
                "password": false,
                "placeholder": "",
                "required": true,
                "show": true,
                "title_case": false,
                "type": "code",
                "value": "import json\nimport re\n\nfrom langchain_core.tools import StructuredTool\nfrom pydantic import ValidationError\n\nfrom langflow.base.agents.agent import LCToolsAgentComponent\nfrom langflow.base.agents.events import ExceptionWithMessageError\nfrom langflow.base.models.model_input_constants import (\n    ALL_PROVIDER_FIELDS,\n    MODEL_DYNAMIC_UPDATE_FIELDS,\n    MODEL_PROVIDERS_DICT,\n    MODELS_METADATA,\n)\nfrom langflow.base.models.model_utils import get_model_name\nfrom langflow.components.helpers.current_date import CurrentDateComponent\nfrom langflow.components.helpers.memory import MemoryComponent\nfrom langflow.components.langchain_utilities.tool_calling import (\n    ToolCallingAgentComponent,\n)\nfrom langflow.custom.custom_component.component import _get_component_toolkit\nfrom langflow.custom.utils import update_component_build_config\nfrom langflow.field_typing import Tool\nfrom langflow.helpers.base_model import build_model_from_schema\nfrom langflow.io import (\n    BoolInput,\n    DropdownInput,\n    IntInput,\n    MultilineInput,\n    Output,\n    TableInput,\n)\nfrom langflow.logging import logger\nfrom langflow.schema.data import Data\nfrom langflow.schema.dotdict import dotdict\nfrom langflow.schema.message import Message\nfrom langflow.schema.table import EditMode\n\n\ndef set_advanced_true(component_input):\n    component_input.advanced = True\n    return component_input\n\n\nMODEL_PROVIDERS_LIST = [\"Anthropic\", \"Google Generative AI\", \"OpenAI\"]\n\n\nclass AgentComponent(ToolCallingAgentComponent):\n    display_name: str = \"Agent\"\n    description: str = \"Define the agent's instructions, then enter a task to complete using tools.\"\n    documentation: str = \"https://docs.langflow.org/agents\"\n    icon = \"bot\"\n    beta = False\n    name = \"Agent\"\n\n    memory_inputs = [set_advanced_true(component_input) for component_input in MemoryComponent().inputs]\n\n    # Filter out json_mode from OpenAI inputs since we handle structured output differently\n    openai_inputs_filtered = [\n        input_field\n        for input_field in MODEL_PROVIDERS_DICT[\"OpenAI\"][\"inputs\"]\n        if not (hasattr(input_field, \"name\") and input_field.name == \"json_mode\")\n    ]\n\n    inputs = [\n        DropdownInput(\n            name=\"agent_llm\",\n            display_name=\"Model Provider\",\n            info=\"The provider of the language model that the agent will use to generate responses.\",\n            options=[*MODEL_PROVIDERS_LIST],\n            value=\"OpenAI\",\n            real_time_refresh=True,\n            refresh_button=False,\n            input_types=[],\n            options_metadata=[MODELS_METADATA[key] for key in MODEL_PROVIDERS_LIST],\n            external_options={\n                \"fields\": {\n                    \"data\": {\n                        \"node\": {\n                            \"name\": \"connect_other_models\",\n                            \"display_name\": \"Connect other models\",\n                            \"icon\": \"CornerDownLeft\",\n                        }\n                    }\n                },\n            },\n        ),\n        *openai_inputs_filtered,\n        MultilineInput(\n            name=\"system_prompt\",\n            display_name=\"Agent Instructions\",\n            info=\"System Prompt: Initial instructions and context provided to guide the agent's behavior.\",\n            value=\"You are a helpful assistant that can use tools to answer questions and perform tasks.\",\n            advanced=False,\n        ),\n        IntInput(\n            name=\"n_messages\",\n            display_name=\"Number of Chat History Messages\",\n            value=100,\n            info=\"Number of chat history messages to retrieve.\",\n            advanced=True,\n            show=True,\n        ),\n        MultilineInput(\n            name=\"format_instructions\",\n            display_name=\"Output Format Instructions\",\n            info=\"Generic Template for structured output formatting. Valid only with Structured response.\",\n            value=(\n                \"You are an AI that extracts structured JSON objects from unstructured text. \"\n                \"Use a predefined schema with expected types (str, int, float, bool, dict). \"\n                \"Extract ALL relevant instances that match the schema - if multiple patterns exist, capture them all. \"\n                \"Fill missing or ambiguous values with defaults: null for missing values. \"\n                \"Remove exact duplicates but keep variations that have different field values. \"\n                \"Always return valid JSON in the expected format, never throw errors. \"\n                \"If multiple objects can be extracted, return them all in the structured format.\"\n            ),\n            advanced=True,\n        ),\n        TableInput(\n            name=\"output_schema\",\n            display_name=\"Output Schema\",\n            info=(\n                \"Schema Validation: Define the structure and data types for structured output. \"\n                \"No validation if no output schema.\"\n            ),\n            advanced=True,\n            required=False,\n            value=[],\n            table_schema=[\n                {\n                    \"name\": \"name\",\n                    \"display_name\": \"Name\",\n                    \"type\": \"str\",\n                    \"description\": \"Specify the name of the output field.\",\n                    \"default\": \"field\",\n                    \"edit_mode\": EditMode.INLINE,\n                },\n                {\n                    \"name\": \"description\",\n                    \"display_name\": \"Description\",\n                    \"type\": \"str\",\n                    \"description\": \"Describe the purpose of the output field.\",\n                    \"default\": \"description of field\",\n                    \"edit_mode\": EditMode.POPOVER,\n                },\n                {\n                    \"name\": \"type\",\n                    \"display_name\": \"Type\",\n                    \"type\": \"str\",\n                    \"edit_mode\": EditMode.INLINE,\n                    \"description\": (\"Indicate the data type of the output field (e.g., str, int, float, bool, dict).\"),\n                    \"options\": [\"str\", \"int\", \"float\", \"bool\", \"dict\"],\n                    \"default\": \"str\",\n                },\n                {\n                    \"name\": \"multiple\",\n                    \"display_name\": \"As List\",\n                    \"type\": \"boolean\",\n                    \"description\": \"Set to True if this output field should be a list of the specified type.\",\n                    \"default\": \"False\",\n                    \"edit_mode\": EditMode.INLINE,\n                },\n            ],\n        ),\n        *LCToolsAgentComponent._base_inputs,\n        # removed memory inputs from agent component\n        # *memory_inputs,\n        BoolInput(\n            name=\"add_current_date_tool\",\n            display_name=\"Current Date\",\n            advanced=True,\n            info=\"If true, will add a tool to the agent that returns the current date.\",\n            value=True,\n        ),\n    ]\n    outputs = [\n        Output(name=\"response\", display_name=\"Response\", method=\"message_response\"),\n    ]\n\n    async def get_agent_requirements(self):\n        \"\"\"Get the agent requirements for the agent.\"\"\"\n        llm_model, display_name = await self.get_llm()\n        if llm_model is None:\n            msg = \"No language model selected. Please choose a model to proceed.\"\n            raise ValueError(msg)\n        self.model_name = get_model_name(llm_model, display_name=display_name)\n\n        # Get memory data\n        self.chat_history = await self.get_memory_data()\n        if isinstance(self.chat_history, Message):\n            self.chat_history = [self.chat_history]\n\n        # Add current date tool if enabled\n        if self.add_current_date_tool:\n            if not isinstance(self.tools, list):  # type: ignore[has-type]\n                self.tools = []\n            current_date_tool = (await CurrentDateComponent(**self.get_base_args()).to_toolkit()).pop(0)\n            if not isinstance(current_date_tool, StructuredTool):\n                msg = \"CurrentDateComponent must be converted to a StructuredTool\"\n                raise TypeError(msg)\n            self.tools.append(current_date_tool)\n        return llm_model, self.chat_history, self.tools\n\n    async def message_response(self) -> Message:\n        try:\n            llm_model, self.chat_history, self.tools = await self.get_agent_requirements()\n            # Set up and run agent\n            self.set(\n                llm=llm_model,\n                tools=self.tools or [],\n                chat_history=self.chat_history,\n                input_value=self.input_value,\n                system_prompt=self.system_prompt,\n            )\n            agent = self.create_agent_runnable()\n            result = await self.run_agent(agent)\n\n            # Store result for potential JSON output\n            self._agent_result = result\n\n        except (ValueError, TypeError, KeyError) as e:\n            await logger.aerror(f\"{type(e).__name__}: {e!s}\")\n            raise\n        except ExceptionWithMessageError as e:\n            await logger.aerror(f\"ExceptionWithMessageError occurred: {e}\")\n            raise\n        # Avoid catching blind Exception; let truly unexpected exceptions propagate\n        except Exception as e:\n            await logger.aerror(f\"Unexpected error: {e!s}\")\n            raise\n        else:\n            return result\n\n    def _preprocess_schema(self, schema):\n        \"\"\"Preprocess schema to ensure correct data types for build_model_from_schema.\"\"\"\n        processed_schema = []\n        for field in schema:\n            processed_field = {\n                \"name\": str(field.get(\"name\", \"field\")),\n                \"type\": str(field.get(\"type\", \"str\")),\n                \"description\": str(field.get(\"description\", \"\")),\n                \"multiple\": field.get(\"multiple\", False),\n            }\n            # Ensure multiple is handled correctly\n            if isinstance(processed_field[\"multiple\"], str):\n                processed_field[\"multiple\"] = processed_field[\"multiple\"].lower() in [\n                    \"true\",\n                    \"1\",\n                    \"t\",\n                    \"y\",\n                    \"yes\",\n                ]\n            processed_schema.append(processed_field)\n        return processed_schema\n\n    async def build_structured_output_base(self, content: str):\n        \"\"\"Build structured output with optional BaseModel validation.\"\"\"\n        json_pattern = r\"\\{.*\\}\"\n        schema_error_msg = \"Try setting an output schema\"\n\n        # Try to parse content as JSON first\n        json_data = None\n        try:\n            json_data = json.loads(content)\n        except json.JSONDecodeError:\n            json_match = re.search(json_pattern, content, re.DOTALL)\n            if json_match:\n                try:\n                    json_data = json.loads(json_match.group())\n                except json.JSONDecodeError:\n                    return {\"content\": content, \"error\": schema_error_msg}\n            else:\n                return {\"content\": content, \"error\": schema_error_msg}\n\n        # If no output schema provided, return parsed JSON without validation\n        if not hasattr(self, \"output_schema\") or not self.output_schema or len(self.output_schema) == 0:\n            return json_data\n\n        # Use BaseModel validation with schema\n        try:\n            processed_schema = self._preprocess_schema(self.output_schema)\n            output_model = build_model_from_schema(processed_schema)\n\n            # Validate against the schema\n            if isinstance(json_data, list):\n                # Multiple objects\n                validated_objects = []\n                for item in json_data:\n                    try:\n                        validated_obj = output_model.model_validate(item)\n                        validated_objects.append(validated_obj.model_dump())\n                    except ValidationError as e:\n                        await logger.aerror(f\"Validation error for item: {e}\")\n                        # Include invalid items with error info\n                        validated_objects.append({\"data\": item, \"validation_error\": str(e)})\n                return validated_objects\n\n            # Single object\n            try:\n                validated_obj = output_model.model_validate(json_data)\n                return [validated_obj.model_dump()]  # Return as list for consistency\n            except ValidationError as e:\n                await logger.aerror(f\"Validation error: {e}\")\n                return [{\"data\": json_data, \"validation_error\": str(e)}]\n\n        except (TypeError, ValueError) as e:\n            await logger.aerror(f\"Error building structured output: {e}\")\n            # Fallback to parsed JSON without validation\n            return json_data\n\n    async def json_response(self) -> Data:\n        \"\"\"Convert agent response to structured JSON Data output with schema validation.\"\"\"\n        # Always use structured chat agent for JSON response mode for better JSON formatting\n        try:\n            system_components = []\n\n            # 1. Agent Instructions (system_prompt)\n            agent_instructions = getattr(self, \"system_prompt\", \"\") or \"\"\n            if agent_instructions:\n                system_components.append(f\"{agent_instructions}\")\n\n            # 2. Format Instructions\n            format_instructions = getattr(self, \"format_instructions\", \"\") or \"\"\n            if format_instructions:\n                system_components.append(f\"Format instructions: {format_instructions}\")\n\n            # 3. Schema Information from BaseModel\n            if hasattr(self, \"output_schema\") and self.output_schema and len(self.output_schema) > 0:\n                try:\n                    processed_schema = self._preprocess_schema(self.output_schema)\n                    output_model = build_model_from_schema(processed_schema)\n                    schema_dict = output_model.model_json_schema()\n                    schema_info = (\n                        \"You are given some text that may include format instructions, \"\n                        \"explanations, or other content alongside a JSON schema.\\n\\n\"\n                        \"Your task:\\n\"\n                        \"- Extract only the JSON schema.\\n\"\n                        \"- Return it as valid JSON.\\n\"\n                        \"- Do not include format instructions, explanations, or extra text.\\n\\n\"\n                        \"Input:\\n\"\n                        f\"{json.dumps(schema_dict, indent=2)}\\n\\n\"\n                        \"Output (only JSON schema):\"\n                    )\n                    system_components.append(schema_info)\n                except (ValidationError, ValueError, TypeError, KeyError) as e:\n                    await logger.aerror(f\"Could not build schema for prompt: {e}\", exc_info=True)\n\n            # Combine all components\n            combined_instructions = \"\\n\\n\".join(system_components) if system_components else \"\"\n            llm_model, self.chat_history, self.tools = await self.get_agent_requirements()\n            self.set(\n                llm=llm_model,\n                tools=self.tools or [],\n                chat_history=self.chat_history,\n                input_value=self.input_value,\n                system_prompt=combined_instructions,\n            )\n\n            # Create and run structured chat agent\n            try:\n                structured_agent = self.create_agent_runnable()\n            except (NotImplementedError, ValueError, TypeError) as e:\n                await logger.aerror(f\"Error with structured chat agent: {e}\")\n                raise\n            try:\n                result = await self.run_agent(structured_agent)\n            except (\n                ExceptionWithMessageError,\n                ValueError,\n                TypeError,\n                RuntimeError,\n            ) as e:\n                await logger.aerror(f\"Error with structured agent result: {e}\")\n                raise\n            # Extract content from structured agent result\n            if hasattr(result, \"content\"):\n                content = result.content\n            elif hasattr(result, \"text\"):\n                content = result.text\n            else:\n                content = str(result)\n\n        except (\n            ExceptionWithMessageError,\n            ValueError,\n            TypeError,\n            NotImplementedError,\n            AttributeError,\n        ) as e:\n            await logger.aerror(f\"Error with structured chat agent: {e}\")\n            # Fallback to regular agent\n            content_str = \"No content returned from agent\"\n            return Data(data={\"content\": content_str, \"error\": str(e)})\n\n        # Process with structured output validation\n        try:\n            structured_output = await self.build_structured_output_base(content)\n\n            # Handle different output formats\n            if isinstance(structured_output, list) and structured_output:\n                if len(structured_output) == 1:\n                    return Data(data=structured_output[0])\n                return Data(data={\"results\": structured_output})\n            if isinstance(structured_output, dict):\n                return Data(data=structured_output)\n            return Data(data={\"content\": content})\n\n        except (ValueError, TypeError) as e:\n            await logger.aerror(f\"Error in structured output processing: {e}\")\n            return Data(data={\"content\": content, \"error\": str(e)})\n\n    async def get_memory_data(self):\n        # TODO: This is a temporary fix to avoid message duplication. We should develop a function for this.\n        messages = (\n            await MemoryComponent(**self.get_base_args())\n            .set(\n                session_id=self.graph.session_id,\n                order=\"Ascending\",\n                n_messages=self.n_messages,\n            )\n            .retrieve_messages()\n        )\n        return [\n            message for message in messages if getattr(message, \"id\", None) != getattr(self.input_value, \"id\", None)\n        ]\n\n    async def get_llm(self):\n        if not isinstance(self.agent_llm, str):\n            return self.agent_llm, None\n\n        try:\n            provider_info = MODEL_PROVIDERS_DICT.get(self.agent_llm)\n            if not provider_info:\n                msg = f\"Invalid model provider: {self.agent_llm}\"\n                raise ValueError(msg)\n\n            component_class = provider_info.get(\"component_class\")\n            display_name = component_class.display_name\n            inputs = provider_info.get(\"inputs\")\n            prefix = provider_info.get(\"prefix\", \"\")\n\n            return self._build_llm_model(component_class, inputs, prefix), display_name\n\n        except (AttributeError, ValueError, TypeError, RuntimeError) as e:\n            await logger.aerror(f\"Error building {self.agent_llm} language model: {e!s}\")\n            msg = f\"Failed to initialize language model: {e!s}\"\n            raise ValueError(msg) from e\n\n    def _build_llm_model(self, component, inputs, prefix=\"\"):\n        model_kwargs = {}\n        for input_ in inputs:\n            if hasattr(self, f\"{prefix}{input_.name}\"):\n                model_kwargs[input_.name] = getattr(self, f\"{prefix}{input_.name}\")\n        return component.set(**model_kwargs).build_model()\n\n    def set_component_params(self, component):\n        provider_info = MODEL_PROVIDERS_DICT.get(self.agent_llm)\n        if provider_info:\n            inputs = provider_info.get(\"inputs\")\n            prefix = provider_info.get(\"prefix\")\n            # Filter out json_mode and only use attributes that exist on this component\n            model_kwargs = {}\n            for input_ in inputs:\n                if hasattr(self, f\"{prefix}{input_.name}\"):\n                    model_kwargs[input_.name] = getattr(self, f\"{prefix}{input_.name}\")\n\n            return component.set(**model_kwargs)\n        return component\n\n    def delete_fields(self, build_config: dotdict, fields: dict | list[str]) -> None:\n        \"\"\"Delete specified fields from build_config.\"\"\"\n        for field in fields:\n            build_config.pop(field, None)\n\n    def update_input_types(self, build_config: dotdict) -> dotdict:\n        \"\"\"Update input types for all fields in build_config.\"\"\"\n        for key, value in build_config.items():\n            if isinstance(value, dict):\n                if value.get(\"input_types\") is None:\n                    build_config[key][\"input_types\"] = []\n            elif hasattr(value, \"input_types\") and value.input_types is None:\n                value.input_types = []\n        return build_config\n\n    async def update_build_config(\n        self, build_config: dotdict, field_value: str, field_name: str | None = None\n    ) -> dotdict:\n        # Iterate over all providers in the MODEL_PROVIDERS_DICT\n        # Existing logic for updating build_config\n        if field_name in (\"agent_llm\",):\n            build_config[\"agent_llm\"][\"value\"] = field_value\n            provider_info = MODEL_PROVIDERS_DICT.get(field_value)\n            if provider_info:\n                component_class = provider_info.get(\"component_class\")\n                if component_class and hasattr(component_class, \"update_build_config\"):\n                    # Call the component class's update_build_config method\n                    build_config = await update_component_build_config(\n                        component_class, build_config, field_value, \"model_name\"\n                    )\n\n            provider_configs: dict[str, tuple[dict, list[dict]]] = {\n                provider: (\n                    MODEL_PROVIDERS_DICT[provider][\"fields\"],\n                    [\n                        MODEL_PROVIDERS_DICT[other_provider][\"fields\"]\n                        for other_provider in MODEL_PROVIDERS_DICT\n                        if other_provider != provider\n                    ],\n                )\n                for provider in MODEL_PROVIDERS_DICT\n            }\n            if field_value in provider_configs:\n                fields_to_add, fields_to_delete = provider_configs[field_value]\n\n                # Delete fields from other providers\n                for fields in fields_to_delete:\n                    self.delete_fields(build_config, fields)\n\n                # Add provider-specific fields\n                if field_value == \"OpenAI\" and not any(field in build_config for field in fields_to_add):\n                    build_config.update(fields_to_add)\n                else:\n                    build_config.update(fields_to_add)\n                # Reset input types for agent_llm\n                build_config[\"agent_llm\"][\"input_types\"] = []\n                build_config[\"agent_llm\"][\"display_name\"] = \"Model Provider\"\n            elif field_value == \"connect_other_models\":\n                # Delete all provider fields\n                self.delete_fields(build_config, ALL_PROVIDER_FIELDS)\n                # # Update with custom component\n                custom_component = DropdownInput(\n                    name=\"agent_llm\",\n                    display_name=\"Language Model\",\n                    info=\"The provider of the language model that the agent will use to generate responses.\",\n                    options=[*MODEL_PROVIDERS_LIST],\n                    real_time_refresh=True,\n                    refresh_button=False,\n                    input_types=[\"LanguageModel\"],\n                    placeholder=\"Awaiting model input.\",\n                    options_metadata=[MODELS_METADATA[key] for key in MODEL_PROVIDERS_LIST],\n                    external_options={\n                        \"fields\": {\n                            \"data\": {\n                                \"node\": {\n                                    \"name\": \"connect_other_models\",\n                                    \"display_name\": \"Connect other models\",\n                                    \"icon\": \"CornerDownLeft\",\n                                },\n                            }\n                        },\n                    },\n                )\n                build_config.update({\"agent_llm\": custom_component.to_dict()})\n            # Update input types for all fields\n            build_config = self.update_input_types(build_config)\n\n            # Validate required keys\n            default_keys = [\n                \"code\",\n                \"_type\",\n                \"agent_llm\",\n                \"tools\",\n                \"input_value\",\n                \"add_current_date_tool\",\n                \"system_prompt\",\n                \"agent_description\",\n                \"max_iterations\",\n                \"handle_parsing_errors\",\n                \"verbose\",\n            ]\n            missing_keys = [key for key in default_keys if key not in build_config]\n            if missing_keys:\n                msg = f\"Missing required keys in build_config: {missing_keys}\"\n                raise ValueError(msg)\n        if (\n            isinstance(self.agent_llm, str)\n            and self.agent_llm in MODEL_PROVIDERS_DICT\n            and field_name in MODEL_DYNAMIC_UPDATE_FIELDS\n        ):\n            provider_info = MODEL_PROVIDERS_DICT.get(self.agent_llm)\n            if provider_info:\n                component_class = provider_info.get(\"component_class\")\n                component_class = self.set_component_params(component_class)\n                prefix = provider_info.get(\"prefix\")\n                if component_class and hasattr(component_class, \"update_build_config\"):\n                    # Call each component class's update_build_config method\n                    # remove the prefix from the field_name\n                    if isinstance(field_name, str) and isinstance(prefix, str):\n                        field_name = field_name.replace(prefix, \"\")\n                    build_config = await update_component_build_config(\n                        component_class, build_config, field_value, \"model_name\"\n                    )\n        return dotdict({k: v.to_dict() if hasattr(v, \"to_dict\") else v for k, v in build_config.items()})\n\n    async def _get_tools(self) -> list[Tool]:\n        component_toolkit = _get_component_toolkit()\n        tools_names = self._build_tools_names()\n        agent_description = self.get_tool_description()\n        # TODO: Agent Description Depreciated Feature to be removed\n        description = f\"{agent_description}{tools_names}\"\n        tools = component_toolkit(component=self).get_tools(\n            tool_name=\"Call_Agent\",\n            tool_description=description,\n            callbacks=self.get_langchain_callbacks(),\n        )\n        if hasattr(self, \"tools_metadata\"):\n            tools = component_toolkit(component=self, metadata=self.tools_metadata).update_tools_metadata(tools=tools)\n        return tools\n"
              },
              "format_instructions": {
                "_input_type": "MultilineInput",
                "advanced": true,
                "copy_field": false,
                "display_name": "Output Format Instructions",
                "dynamic": false,
                "info": "Generic Template for structured output formatting. Valid only with Structured response.",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "multiline": true,
                "name": "format_instructions",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "You are an AI that extracts structured JSON objects from unstructured text. Use a predefined schema with expected types (str, int, float, bool, dict). Extract ALL relevant instances that match the schema - if multiple patterns exist, capture them all. Fill missing or ambiguous values with defaults: null for missing values. Remove exact duplicates but keep variations that have different field values. Always return valid JSON in the expected format, never throw errors. If multiple objects can be extracted, return them all in the structured format."
              },
              "handle_parsing_errors": {
                "_input_type": "BoolInput",
                "advanced": true,
                "display_name": "Handle Parse Errors",
                "dynamic": false,
                "info": "Should the Agent fix errors when reading user input for better processing?",
                "list": false,
                "list_add_label": "Add More",
                "name": "handle_parsing_errors",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "bool",
                "value": true
              },
              "input_value": {
                "_input_type": "MessageInput",
                "advanced": false,
                "display_name": "Input",
                "dynamic": false,
                "info": "The input provided by the user for the agent to process.",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "input_value",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": true,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": ""
              },
              "max_iterations": {
                "_input_type": "IntInput",
                "advanced": true,
                "display_name": "Max Iterations",
                "dynamic": false,
                "info": "The maximum number of attempts the agent can make to complete its task before it stops.",
                "list": false,
                "list_add_label": "Add More",
                "name": "max_iterations",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "int",
                "value": 15
              },
              "max_retries": {
                "_input_type": "IntInput",
                "advanced": true,
                "display_name": "Max Retries",
                "dynamic": false,
                "info": "The maximum number of retries to make when generating.",
                "list": false,
                "list_add_label": "Add More",
                "name": "max_retries",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "int",
                "value": 5
              },
              "max_tokens": {
                "_input_type": "IntInput",
                "advanced": true,
                "display_name": "Max Tokens",
                "dynamic": false,
                "info": "The maximum number of tokens to generate. Set to 0 for unlimited tokens.",
                "list": false,
                "list_add_label": "Add More",
                "name": "max_tokens",
                "placeholder": "",
                "range_spec": {
                  "max": 128000,
                  "min": 0,
                  "step": 0.1,
                  "step_type": "float"
                },
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "int",
                "value": ""
              },
              "model_kwargs": {
                "_input_type": "DictInput",
                "advanced": true,
                "display_name": "Model Kwargs",
                "dynamic": false,
                "info": "Additional keyword arguments to pass to the model.",
                "list": false,
                "list_add_label": "Add More",
                "name": "model_kwargs",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "type": "dict",
                "value": {}
              },
              "model_name": {
                "_input_type": "DropdownInput",
                "advanced": false,
                "combobox": true,
                "dialog_inputs": {},
                "display_name": "Model Name",
                "dynamic": false,
                "external_options": {},
                "info": "To see the model names, first choose a provider. Then, enter your API key and click the refresh button next to the model name.",
                "name": "model_name",
                "options": [
                  "gpt-4o-mini",
                  "gpt-4o",
                  "gpt-4.1",
                  "gpt-4.1-mini",
                  "gpt-4.1-nano",
                  "gpt-4-turbo",
                  "gpt-4-turbo-preview",
                  "gpt-4",
                  "gpt-3.5-turbo",
                  "gpt-5",
                  "gpt-5-mini",
                  "gpt-5-nano",
                  "gpt-5-chat-latest",
                  "o1",
                  "o3-mini",
                  "o3",
                  "o3-pro",
                  "o4-mini",
                  "o4-mini-high"
                ],
                "options_metadata": [],
                "placeholder": "",
                "real_time_refresh": false,
                "required": false,
                "show": true,
                "title_case": false,
                "toggle": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "str",
                "value": "gpt-5"
              },
              "n_messages": {
                "_input_type": "IntInput",
                "advanced": true,
                "display_name": "Number of Chat History Messages",
                "dynamic": false,
                "info": "Number of chat history messages to retrieve.",
                "list": false,
                "list_add_label": "Add More",
                "name": "n_messages",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "int",
                "value": 100
              },
              "openai_api_base": {
                "_input_type": "StrInput",
                "advanced": true,
                "display_name": "OpenAI API Base",
                "dynamic": false,
                "info": "The base URL of the OpenAI API. Defaults to https://api.openai.com/v1. You can change this to use other APIs like JinaChat, LocalAI and Prem.",
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "openai_api_base",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "str",
                "value": ""
              },
              "output_schema": {
                "_input_type": "TableInput",
                "advanced": true,
                "display_name": "Output Schema",
                "dynamic": false,
                "info": "Schema Validation: Define the structure and data types for structured output. No validation if no output schema.",
                "is_list": true,
                "list_add_label": "Add More",
                "name": "output_schema",
                "placeholder": "",
                "required": false,
                "show": true,
                "table_icon": "Table",
                "table_schema": {
                  "columns": [
                    {
                      "default": "field",
                      "description": "Specify the name of the output field.",
                      "disable_edit": false,
                      "display_name": "Name",
                      "edit_mode": "inline",
                      "filterable": true,
                      "formatter": "text",
                      "hidden": false,
                      "name": "name",
                      "sortable": true,
                      "type": "str"
                    },
                    {
                      "default": "description of field",
                      "description": "Describe the purpose of the output field.",
                      "disable_edit": false,
                      "display_name": "Description",
                      "edit_mode": "popover",
                      "filterable": true,
                      "formatter": "text",
                      "hidden": false,
                      "name": "description",
                      "sortable": true,
                      "type": "str"
                    },
                    {
                      "default": "str",
                      "description": "Indicate the data type of the output field (e.g., str, int, float, bool, dict).",
                      "disable_edit": false,
                      "display_name": "Type",
                      "edit_mode": "inline",
                      "filterable": true,
                      "formatter": "text",
                      "hidden": false,
                      "name": "type",
                      "options": [
                        "str",
                        "int",
                        "float",
                        "bool",
                        "dict"
                      ],
                      "sortable": true,
                      "type": "str"
                    },
                    {
                      "default": false,
                      "description": "Set to True if this output field should be a list of the specified type.",
                      "disable_edit": false,
                      "display_name": "As List",
                      "edit_mode": "inline",
                      "filterable": true,
                      "formatter": "boolean",
                      "hidden": false,
                      "name": "multiple",
                      "sortable": true,
                      "type": "boolean"
                    }
                  ]
                },
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "trigger_icon": "Table",
                "trigger_text": "Open table",
                "type": "table",
                "value": []
              },
              "seed": {
                "_input_type": "IntInput",
                "advanced": true,
                "display_name": "Seed",
                "dynamic": false,
                "info": "The seed controls the reproducibility of the job.",
                "list": false,
                "list_add_label": "Add More",
                "name": "seed",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "int",
                "value": 1
              },
              "system_prompt": {
                "_input_type": "MultilineInput",
                "advanced": false,
                "copy_field": false,
                "display_name": "Agent Instructions",
                "dynamic": false,
                "info": "System Prompt: Initial instructions and context provided to guide the agent's behavior.",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "multiline": true,
                "name": "system_prompt",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "You are an expert MSP finance analyst and revenue-operations partner.\n\nYou will be given an \"Invoice Rollup Context\" plus optional tables (by month, by client, by type, by category) derived from ConnectWise invoices. Your job is to produce actionable insights, not a data dump.\n\nRules:\n- Use only the provided context and tables. Do not assume missing information.\n- When you state a number, include the exact figure from the context. If not present, say \"not available\".\n- Separate findings into: Revenue Mix, Concentration Risk, Trends/Seasonality, Category Insights, Anomalies, and Next Actions.\n- Prioritize the top 3–7 insights that would change a decision.\n- Call out data quality issues (filters, missing columns, unexpected zeros, status filtering).\n- If categories exist: identify which categories are recurring and which are one-off.\n- Suggest 3–5 follow-up questions that would improve accuracy (but do not ask the user; just list them).\nOutput format:\n- 1–2 sentence executive summary\n- Bulleted insights by section\n- A short “What I would do next” checklist\nTone: clear, direct, CFO-friendly."
              },
              "temperature": {
                "_input_type": "SliderInput",
                "advanced": true,
                "display_name": "Temperature",
                "dynamic": false,
                "info": "",
                "max_label": "",
                "max_label_icon": "",
                "min_label": "",
                "min_label_icon": "",
                "name": "temperature",
                "placeholder": "",
                "range_spec": {
                  "max": 1,
                  "min": 0,
                  "step": 0.01,
                  "step_type": "float"
                },
                "required": false,
                "show": true,
                "slider_buttons": false,
                "slider_buttons_options": [],
                "slider_input": false,
                "title_case": false,
                "tool_mode": false,
                "type": "slider",
                "value": 0.1
              },
              "timeout": {
                "_input_type": "IntInput",
                "advanced": true,
                "display_name": "Timeout",
                "dynamic": false,
                "info": "The timeout for requests to OpenAI completion API.",
                "list": false,
                "list_add_label": "Add More",
                "name": "timeout",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "int",
                "value": 700
              },
              "tools": {
                "_input_type": "HandleInput",
                "advanced": false,
                "display_name": "Tools",
                "dynamic": false,
                "info": "These are the tools that the agent can use to help with tasks.",
                "input_types": [
                  "Tool"
                ],
                "list": true,
                "list_add_label": "Add More",
                "name": "tools",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "trace_as_metadata": true,
                "type": "other",
                "value": ""
              },
              "verbose": {
                "_input_type": "BoolInput",
                "advanced": true,
                "display_name": "Verbose",
                "dynamic": false,
                "info": "",
                "list": false,
                "list_add_label": "Add More",
                "name": "verbose",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "bool",
                "value": true
              }
            },
            "tool_mode": false
          },
          "showNode": true,
          "type": "Agent"
        },
        "dragging": false,
        "id": "Agent-CdUIZ",
        "measured": {
          "height": 591,
          "width": 320
        },
        "position": {
          "x": 4934.5422709319155,
          "y": 321.4142271703529
        },
        "selected": false,
        "type": "genericNode"
      },
      {
        "data": {
          "id": "File-5iHSR",
          "node": {
            "base_classes": [
              "Message"
            ],
            "beta": false,
            "category": "data",
            "conditional_paths": [],
            "custom_fields": {},
            "description": "Loads content from one or more files.",
            "display_name": "File",
            "documentation": "https://docs.langflow.org/components-data#file",
            "edited": false,
            "field_order": [
              "path",
              "file_path",
              "separator",
              "silent_errors",
              "delete_server_file_after_processing",
              "ignore_unsupported_extensions",
              "ignore_unspecified_files",
              "advanced_mode",
              "pipeline",
              "ocr_engine",
              "md_image_placeholder",
              "md_page_break_placeholder",
              "doc_key",
              "use_multithreading",
              "concurrency_multithreading",
              "markdown"
            ],
            "frozen": false,
            "icon": "file-text",
            "key": "File",
            "last_updated": "2025-12-23T01:32:00.581Z",
            "legacy": false,
            "lf_version": "1.6.0",
            "metadata": {},
            "minimized": false,
            "output_types": [],
            "outputs": [
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Raw Content",
                "group_outputs": false,
                "method": "load_files_message",
                "name": "message",
                "options": null,
                "required_inputs": null,
                "selected": "Message",
                "tool_mode": true,
                "types": [
                  "Message"
                ],
                "value": "__UNDEFINED__"
              }
            ],
            "pinned": false,
            "score": 2.220446049250313e-16,
            "template": {
              "_type": "Component",
              "advanced_mode": {
                "_input_type": "BoolInput",
                "advanced": false,
                "display_name": "Advanced Parser",
                "dynamic": false,
                "info": "Enable advanced document processing and export with Docling for PDFs, images, and office documents. Available only for single file processing.Note that advanced document processing can consume significant resources.",
                "list": false,
                "list_add_label": "Add More",
                "name": "advanced_mode",
                "placeholder": "",
                "real_time_refresh": true,
                "required": false,
                "show": false,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "bool",
                "value": false
              },
              "code": {
                "advanced": true,
                "dynamic": true,
                "fileTypes": [],
                "file_path": "",
                "info": "",
                "list": false,
                "load_from_db": false,
                "multiline": true,
                "name": "code",
                "password": false,
                "placeholder": "",
                "required": true,
                "show": true,
                "title_case": false,
                "type": "code",
                "value": "\"\"\"Enhanced file component with Docling support and process isolation.\n\nNotes:\n-----\n- ALL Docling parsing/export runs in a separate OS process to prevent memory\n  growth and native library state from impacting the main Langflow process.\n- Standard text/structured parsing continues to use existing BaseFileComponent\n  utilities (and optional threading via `parallel_load_data`).\n\"\"\"\n\nfrom __future__ import annotations\n\nimport json\nimport subprocess\nimport sys\nimport textwrap\nfrom copy import deepcopy\nfrom typing import TYPE_CHECKING, Any\n\nfrom langflow.base.data.base_file import BaseFileComponent\nfrom langflow.base.data.utils import TEXT_FILE_TYPES, parallel_load_data, parse_text_file_to_data\nfrom langflow.io import (\n    BoolInput,\n    DropdownInput,\n    FileInput,\n    IntInput,\n    MessageTextInput,\n    Output,\n    StrInput,\n)\nfrom langflow.schema.data import Data\nfrom langflow.schema.message import Message\n\nif TYPE_CHECKING:\n    from langflow.schema import DataFrame\n\n\nclass FileComponent(BaseFileComponent):\n    \"\"\"File component with optional Docling processing (isolated in a subprocess).\"\"\"\n\n    display_name = \"File\"\n    description = \"Loads content from one or more files.\"\n    documentation: str = \"https://docs.langflow.org/components-data#file\"\n    icon = \"file-text\"\n    name = \"File\"\n\n    # Docling-supported/compatible extensions; TEXT_FILE_TYPES are supported by the base loader.\n    VALID_EXTENSIONS = [\n        *TEXT_FILE_TYPES,\n        \"adoc\",\n        \"asciidoc\",\n        \"asc\",\n        \"bmp\",\n        \"dotx\",\n        \"dotm\",\n        \"docm\",\n        \"jpeg\",\n        \"png\",\n        \"potx\",\n        \"ppsx\",\n        \"pptm\",\n        \"potm\",\n        \"ppsm\",\n        \"pptx\",\n        \"tiff\",\n        \"xls\",\n        \"xlsx\",\n        \"xhtml\",\n        \"webp\",\n    ]\n\n    # Fixed export settings used when markdown export is requested.\n    EXPORT_FORMAT = \"Markdown\"\n    IMAGE_MODE = \"placeholder\"\n\n    # ---- Inputs / Outputs (kept as close to original as possible) -------------------\n    _base_inputs = deepcopy(BaseFileComponent._base_inputs)\n    for input_item in _base_inputs:\n        if isinstance(input_item, FileInput) and input_item.name == \"path\":\n            input_item.real_time_refresh = True\n            break\n\n    inputs = [\n        *_base_inputs,\n        BoolInput(\n            name=\"advanced_mode\",\n            display_name=\"Advanced Parser\",\n            value=False,\n            real_time_refresh=True,\n            info=(\n                \"Enable advanced document processing and export with Docling for PDFs, images, and office documents. \"\n                \"Available only for single file processing.\"\n                \"Note that advanced document processing can consume significant resources.\"\n            ),\n            show=False,\n        ),\n        DropdownInput(\n            name=\"pipeline\",\n            display_name=\"Pipeline\",\n            info=\"Docling pipeline to use\",\n            options=[\"standard\", \"vlm\"],\n            value=\"standard\",\n            advanced=True,\n            real_time_refresh=True,\n        ),\n        DropdownInput(\n            name=\"ocr_engine\",\n            display_name=\"OCR Engine\",\n            info=\"OCR engine to use. Only available when pipeline is set to 'standard'.\",\n            options=[\"None\", \"easyocr\"],\n            value=\"easyocr\",\n            show=False,\n            advanced=True,\n        ),\n        StrInput(\n            name=\"md_image_placeholder\",\n            display_name=\"Image placeholder\",\n            info=\"Specify the image placeholder for markdown exports.\",\n            value=\"<!-- image -->\",\n            advanced=True,\n            show=False,\n        ),\n        StrInput(\n            name=\"md_page_break_placeholder\",\n            display_name=\"Page break placeholder\",\n            info=\"Add this placeholder between pages in the markdown output.\",\n            value=\"\",\n            advanced=True,\n            show=False,\n        ),\n        MessageTextInput(\n            name=\"doc_key\",\n            display_name=\"Doc Key\",\n            info=\"The key to use for the DoclingDocument column.\",\n            value=\"doc\",\n            advanced=True,\n            show=False,\n        ),\n        # Deprecated input retained for backward-compatibility.\n        BoolInput(\n            name=\"use_multithreading\",\n            display_name=\"[Deprecated] Use Multithreading\",\n            advanced=True,\n            value=True,\n            info=\"Set 'Processing Concurrency' greater than 1 to enable multithreading.\",\n        ),\n        IntInput(\n            name=\"concurrency_multithreading\",\n            display_name=\"Processing Concurrency\",\n            advanced=True,\n            info=\"When multiple files are being processed, the number of files to process concurrently.\",\n            value=1,\n        ),\n        BoolInput(\n            name=\"markdown\",\n            display_name=\"Markdown Export\",\n            info=\"Export processed documents to Markdown format. Only available when advanced mode is enabled.\",\n            value=False,\n            show=False,\n        ),\n    ]\n\n    outputs = [\n        Output(display_name=\"Raw Content\", name=\"message\", method=\"load_files_message\"),\n    ]\n\n    # ------------------------------ UI helpers --------------------------------------\n\n    def _path_value(self, template: dict) -> list[str]:\n        \"\"\"Return the list of currently selected file paths from the template.\"\"\"\n        return template.get(\"path\", {}).get(\"file_path\", [])\n\n    def update_build_config(\n        self,\n        build_config: dict[str, Any],\n        field_value: Any,\n        field_name: str | None = None,\n    ) -> dict[str, Any]:\n        \"\"\"Show/hide Advanced Parser and related fields based on selection context.\"\"\"\n        if field_name == \"path\":\n            paths = self._path_value(build_config)\n            file_path = paths[0] if paths else \"\"\n            file_count = len(field_value) if field_value else 0\n\n            # Advanced mode only for single (non-tabular) file\n            allow_advanced = file_count == 1 and not file_path.endswith((\".csv\", \".xlsx\", \".parquet\"))\n            build_config[\"advanced_mode\"][\"show\"] = allow_advanced\n            if not allow_advanced:\n                build_config[\"advanced_mode\"][\"value\"] = False\n                for f in (\"pipeline\", \"ocr_engine\", \"doc_key\", \"md_image_placeholder\", \"md_page_break_placeholder\"):\n                    if f in build_config:\n                        build_config[f][\"show\"] = False\n\n        # Docling Processing\n        elif field_name == \"advanced_mode\":\n            for f in (\"pipeline\", \"ocr_engine\", \"doc_key\", \"md_image_placeholder\", \"md_page_break_placeholder\"):\n                if f in build_config:\n                    build_config[f][\"show\"] = bool(field_value)\n\n        elif field_name == \"pipeline\":\n            if field_value == \"standard\":\n                build_config[\"ocr_engine\"][\"show\"] = True\n                build_config[\"ocr_engine\"][\"value\"] = \"easyocr\"\n            else:\n                build_config[\"ocr_engine\"][\"show\"] = False\n                build_config[\"ocr_engine\"][\"value\"] = \"None\"\n\n        return build_config\n\n    def update_outputs(self, frontend_node: dict[str, Any], field_name: str, field_value: Any) -> dict[str, Any]:  # noqa: ARG002\n        \"\"\"Dynamically show outputs based on file count/type and advanced mode.\"\"\"\n        if field_name not in [\"path\", \"advanced_mode\", \"pipeline\"]:\n            return frontend_node\n\n        template = frontend_node.get(\"template\", {})\n        paths = self._path_value(template)\n        if not paths:\n            return frontend_node\n\n        frontend_node[\"outputs\"] = []\n        if len(paths) == 1:\n            file_path = paths[0] if field_name == \"path\" else frontend_node[\"template\"][\"path\"][\"file_path\"][0]\n            if file_path.endswith((\".csv\", \".xlsx\", \".parquet\")):\n                frontend_node[\"outputs\"].append(\n                    Output(display_name=\"Structured Content\", name=\"dataframe\", method=\"load_files_structured\"),\n                )\n            elif file_path.endswith(\".json\"):\n                frontend_node[\"outputs\"].append(\n                    Output(display_name=\"Structured Content\", name=\"json\", method=\"load_files_json\"),\n                )\n\n            advanced_mode = frontend_node.get(\"template\", {}).get(\"advanced_mode\", {}).get(\"value\", False)\n            if advanced_mode:\n                frontend_node[\"outputs\"].append(\n                    Output(display_name=\"Structured Output\", name=\"advanced_dataframe\", method=\"load_files_dataframe\"),\n                )\n                frontend_node[\"outputs\"].append(\n                    Output(display_name=\"Markdown\", name=\"advanced_markdown\", method=\"load_files_markdown\"),\n                )\n                frontend_node[\"outputs\"].append(\n                    Output(display_name=\"File Path\", name=\"path\", method=\"load_files_path\"),\n                )\n            else:\n                frontend_node[\"outputs\"].append(\n                    Output(display_name=\"Raw Content\", name=\"message\", method=\"load_files_message\"),\n                )\n                frontend_node[\"outputs\"].append(\n                    Output(display_name=\"File Path\", name=\"path\", method=\"load_files_path\"),\n                )\n        else:\n            # Multiple files => DataFrame output; advanced parser disabled\n            frontend_node[\"outputs\"].append(Output(display_name=\"Files\", name=\"dataframe\", method=\"load_files\"))\n\n        return frontend_node\n\n    # ------------------------------ Core processing ----------------------------------\n\n    def _is_docling_compatible(self, file_path: str) -> bool:\n        \"\"\"Lightweight extension gate for Docling-compatible types.\"\"\"\n        docling_exts = (\n            \".adoc\",\n            \".asciidoc\",\n            \".asc\",\n            \".bmp\",\n            \".csv\",\n            \".dotx\",\n            \".dotm\",\n            \".docm\",\n            \".docx\",\n            \".htm\",\n            \".html\",\n            \".jpeg\",\n            \".json\",\n            \".md\",\n            \".pdf\",\n            \".png\",\n            \".potx\",\n            \".ppsx\",\n            \".pptm\",\n            \".potm\",\n            \".ppsm\",\n            \".pptx\",\n            \".tiff\",\n            \".txt\",\n            \".xls\",\n            \".xlsx\",\n            \".xhtml\",\n            \".xml\",\n            \".webp\",\n        )\n        return file_path.lower().endswith(docling_exts)\n\n    def _process_docling_in_subprocess(self, file_path: str) -> Data | None:\n        \"\"\"Run Docling in a separate OS process and map the result to a Data object.\n\n        We avoid multiprocessing pickling by launching `python -c \"<script>\"` and\n        passing JSON config via stdin. The child prints a JSON result to stdout.\n        \"\"\"\n        if not file_path:\n            return None\n\n        args: dict[str, Any] = {\n            \"file_path\": file_path,\n            \"markdown\": bool(self.markdown),\n            \"image_mode\": str(self.IMAGE_MODE),\n            \"md_image_placeholder\": str(self.md_image_placeholder),\n            \"md_page_break_placeholder\": str(self.md_page_break_placeholder),\n            \"pipeline\": str(self.pipeline),\n            \"ocr_engine\": (\n                self.ocr_engine if self.ocr_engine and self.ocr_engine != \"None\" and self.pipeline != \"vlm\" else None\n            ),\n        }\n\n        self.log(f\"Starting Docling subprocess for file: {file_path}\")\n        self.log(args)\n\n        # Child script for isolating the docling processing\n        child_script = textwrap.dedent(\n            r\"\"\"\n            import json, sys\n\n            def try_imports():\n                # Strategy 1: latest layout\n                try:\n                    from docling.datamodel.base_models import ConversionStatus, InputFormat  # type: ignore\n                    from docling.document_converter import DocumentConverter  # type: ignore\n                    from docling_core.types.doc import ImageRefMode  # type: ignore\n                    return ConversionStatus, InputFormat, DocumentConverter, ImageRefMode, \"latest\"\n                except Exception:\n                    pass\n                # Strategy 2: alternative layout\n                try:\n                    from docling.document_converter import DocumentConverter  # type: ignore\n                    try:\n                        from docling_core.types import ConversionStatus, InputFormat  # type: ignore\n                    except Exception:\n                        try:\n                            from docling.datamodel import ConversionStatus, InputFormat  # type: ignore\n                        except Exception:\n                            class ConversionStatus: SUCCESS = \"success\"\n                            class InputFormat:\n                                PDF=\"pdf\"; IMAGE=\"image\"\n                    try:\n                        from docling_core.types.doc import ImageRefMode  # type: ignore\n                    except Exception:\n                        class ImageRefMode:\n                            PLACEHOLDER=\"placeholder\"; EMBEDDED=\"embedded\"\n                    return ConversionStatus, InputFormat, DocumentConverter, ImageRefMode, \"alternative\"\n                except Exception:\n                    pass\n                # Strategy 3: basic converter only\n                try:\n                    from docling.document_converter import DocumentConverter  # type: ignore\n                    class ConversionStatus: SUCCESS = \"success\"\n                    class InputFormat:\n                        PDF=\"pdf\"; IMAGE=\"image\"\n                    class ImageRefMode:\n                        PLACEHOLDER=\"placeholder\"; EMBEDDED=\"embedded\"\n                    return ConversionStatus, InputFormat, DocumentConverter, ImageRefMode, \"basic\"\n                except Exception as e:\n                    raise ImportError(f\"Docling imports failed: {e}\") from e\n\n            def create_converter(strategy, input_format, DocumentConverter, pipeline, ocr_engine):\n                # --- Standard PDF/IMAGE pipeline (your existing behavior), with optional OCR ---\n                if pipeline == \"standard\":\n                    try:\n                        from docling.datamodel.pipeline_options import PdfPipelineOptions  # type: ignore\n                        from docling.document_converter import PdfFormatOption  # type: ignore\n\n                        pipe = PdfPipelineOptions()\n                        pipe.do_ocr = False\n\n                        if ocr_engine:\n                            try:\n                                from docling.models.factories import get_ocr_factory  # type: ignore\n                                pipe.do_ocr = True\n                                fac = get_ocr_factory(allow_external_plugins=False)\n                                pipe.ocr_options = fac.create_options(kind=ocr_engine)\n                            except Exception:\n                                # If OCR setup fails, disable it\n                                pipe.do_ocr = False\n\n                        fmt = {}\n                        if hasattr(input_format, \"PDF\"):\n                            fmt[getattr(input_format, \"PDF\")] = PdfFormatOption(pipeline_options=pipe)\n                        if hasattr(input_format, \"IMAGE\"):\n                            fmt[getattr(input_format, \"IMAGE\")] = PdfFormatOption(pipeline_options=pipe)\n\n                        return DocumentConverter(format_options=fmt)\n                    except Exception:\n                        return DocumentConverter()\n\n                # --- Vision-Language Model (VLM) pipeline ---\n                if pipeline == \"vlm\":\n                    try:\n                        from docling.pipeline.vlm_pipeline import VlmPipeline\n                        from docling.document_converter import PdfFormatOption  # type: ignore\n\n                        vl_pipe = VlmPipelineOptions()\n\n                        # VLM paths generally don't need OCR; keep OCR off by default here.\n                        fmt = {}\n                        if hasattr(input_format, \"PDF\"):\n                            fmt[getattr(input_format, \"PDF\")] = PdfFormatOption(pipeline_cls=VlmPipeline)\n                        if hasattr(input_format, \"IMAGE\"):\n                            fmt[getattr(input_format, \"IMAGE\")] = PdfFormatOption(pipeline_cls=VlmPipeline)\n\n                        return DocumentConverter(format_options=fmt)\n                    except Exception:\n                        return DocumentConverter()\n\n                # --- Fallback: default converter with no special options ---\n                return DocumentConverter()\n\n            def export_markdown(document, ImageRefMode, image_mode, img_ph, pg_ph):\n                try:\n                    mode = getattr(ImageRefMode, image_mode.upper(), image_mode)\n                    return document.export_to_markdown(\n                        image_mode=mode,\n                        image_placeholder=img_ph,\n                        page_break_placeholder=pg_ph,\n                    )\n                except Exception:\n                    try:\n                        return document.export_to_text()\n                    except Exception:\n                        return str(document)\n\n            def to_rows(doc_dict):\n                rows = []\n                for t in doc_dict.get(\"texts\", []):\n                    prov = t.get(\"prov\") or []\n                    page_no = None\n                    if prov and isinstance(prov, list) and isinstance(prov[0], dict):\n                        page_no = prov[0].get(\"page_no\")\n                    rows.append({\n                        \"page_no\": page_no,\n                        \"label\": t.get(\"label\"),\n                        \"text\": t.get(\"text\"),\n                        \"level\": t.get(\"level\"),\n                    })\n                return rows\n\n            def main():\n                cfg = json.loads(sys.stdin.read())\n                file_path = cfg[\"file_path\"]\n                markdown = cfg[\"markdown\"]\n                image_mode = cfg[\"image_mode\"]\n                img_ph = cfg[\"md_image_placeholder\"]\n                pg_ph = cfg[\"md_page_break_placeholder\"]\n                pipeline = cfg[\"pipeline\"]\n                ocr_engine = cfg.get(\"ocr_engine\")\n                meta = {\"file_path\": file_path}\n\n                try:\n                    ConversionStatus, InputFormat, DocumentConverter, ImageRefMode, strategy = try_imports()\n                    converter = create_converter(strategy, InputFormat, DocumentConverter, pipeline, ocr_engine)\n                    try:\n                        res = converter.convert(file_path)\n                    except Exception as e:\n                        print(json.dumps({\"ok\": False, \"error\": f\"Docling conversion error: {e}\", \"meta\": meta}))\n                        return\n\n                    ok = False\n                    if hasattr(res, \"status\"):\n                        try:\n                            ok = (res.status == ConversionStatus.SUCCESS) or (str(res.status).lower() == \"success\")\n                        except Exception:\n                            ok = (str(res.status).lower() == \"success\")\n                    if not ok and hasattr(res, \"document\"):\n                        ok = getattr(res, \"document\", None) is not None\n                    if not ok:\n                        print(json.dumps({\"ok\": False, \"error\": \"Docling conversion failed\", \"meta\": meta}))\n                        return\n\n                    doc = getattr(res, \"document\", None)\n                    if doc is None:\n                        print(json.dumps({\"ok\": False, \"error\": \"Docling produced no document\", \"meta\": meta}))\n                        return\n\n                    if markdown:\n                        text = export_markdown(doc, ImageRefMode, image_mode, img_ph, pg_ph)\n                        print(json.dumps({\"ok\": True, \"mode\": \"markdown\", \"text\": text, \"meta\": meta}))\n                        return\n\n                    # structured\n                    try:\n                        doc_dict = doc.export_to_dict()\n                    except Exception as e:\n                        print(json.dumps({\"ok\": False, \"error\": f\"Docling export_to_dict failed: {e}\", \"meta\": meta}))\n                        return\n\n                    rows = to_rows(doc_dict)\n                    print(json.dumps({\"ok\": True, \"mode\": \"structured\", \"doc\": rows, \"meta\": meta}))\n                except Exception as e:\n                    print(\n                        json.dumps({\n                            \"ok\": False,\n                            \"error\": f\"Docling processing error: {e}\",\n                            \"meta\": {\"file_path\": file_path},\n                        })\n                    )\n\n            if __name__ == \"__main__\":\n                main()\n            \"\"\"\n        )\n\n        # Validate file_path to avoid command injection or unsafe input\n        if not isinstance(args[\"file_path\"], str) or any(c in args[\"file_path\"] for c in [\";\", \"|\", \"&\", \"$\", \"`\"]):\n            return Data(data={\"error\": \"Unsafe file path detected.\", \"file_path\": args[\"file_path\"]})\n\n        proc = subprocess.run(  # noqa: S603\n            [sys.executable, \"-u\", \"-c\", child_script],\n            input=json.dumps(args).encode(\"utf-8\"),\n            capture_output=True,\n            check=False,\n        )\n\n        if not proc.stdout:\n            err_msg = proc.stderr.decode(\"utf-8\", errors=\"replace\") or \"no output from child process\"\n            return Data(data={\"error\": f\"Docling subprocess error: {err_msg}\", \"file_path\": file_path})\n\n        try:\n            result = json.loads(proc.stdout.decode(\"utf-8\"))\n        except Exception as e:  # noqa: BLE001\n            err_msg = proc.stderr.decode(\"utf-8\", errors=\"replace\")\n            return Data(\n                data={\"error\": f\"Invalid JSON from Docling subprocess: {e}. stderr={err_msg}\", \"file_path\": file_path},\n            )\n\n        if not result.get(\"ok\"):\n            return Data(data={\"error\": result.get(\"error\", \"Unknown Docling error\"), **result.get(\"meta\", {})})\n\n        meta = result.get(\"meta\", {})\n        if result.get(\"mode\") == \"markdown\":\n            exported_content = str(result.get(\"text\", \"\"))\n            return Data(\n                text=exported_content,\n                data={\"exported_content\": exported_content, \"export_format\": self.EXPORT_FORMAT, **meta},\n            )\n\n        rows = list(result.get(\"doc\", []))\n        return Data(data={\"doc\": rows, \"export_format\": self.EXPORT_FORMAT, **meta})\n\n    def process_files(\n        self,\n        file_list: list[BaseFileComponent.BaseFile],\n    ) -> list[BaseFileComponent.BaseFile]:\n        \"\"\"Process input files.\n\n        - Single file + advanced_mode => Docling in a separate process.\n        - Otherwise => standard parsing in current process (optionally threaded).\n        \"\"\"\n        if not file_list:\n            msg = \"No files to process.\"\n            raise ValueError(msg)\n\n        def process_file_standard(file_path: str, *, silent_errors: bool = False) -> Data | None:\n            try:\n                return parse_text_file_to_data(file_path, silent_errors=silent_errors)\n            except FileNotFoundError as e:\n                self.log(f\"File not found: {file_path}. Error: {e}\")\n                if not silent_errors:\n                    raise\n                return None\n            except Exception as e:\n                self.log(f\"Unexpected error processing {file_path}: {e}\")\n                if not silent_errors:\n                    raise\n                return None\n\n        # Advanced path: only for a single Docling-compatible file\n        if len(file_list) == 1:\n            file_path = str(file_list[0].path)\n            if self.advanced_mode and self._is_docling_compatible(file_path):\n                advanced_data: Data | None = self._process_docling_in_subprocess(file_path)\n\n                # --- UNNEST: expand each element in `doc` to its own Data row\n                payload = getattr(advanced_data, \"data\", {}) or {}\n                doc_rows = payload.get(\"doc\")\n                if isinstance(doc_rows, list):\n                    rows: list[Data | None] = [\n                        Data(\n                            data={\n                                \"file_path\": file_path,\n                                **(item if isinstance(item, dict) else {\"value\": item}),\n                            },\n                        )\n                        for item in doc_rows\n                    ]\n                    return self.rollup_data(file_list, rows)\n\n                # If not structured, keep as-is (e.g., markdown export or error dict)\n                return self.rollup_data(file_list, [advanced_data])\n\n        # Standard multi-file (or single non-advanced) path\n        concurrency = 1 if not self.use_multithreading else max(1, self.concurrency_multithreading)\n        file_paths = [str(f.path) for f in file_list]\n        self.log(f\"Starting parallel processing of {len(file_paths)} files with concurrency: {concurrency}.\")\n        my_data = parallel_load_data(\n            file_paths,\n            silent_errors=self.silent_errors,\n            load_function=process_file_standard,\n            max_concurrency=concurrency,\n        )\n        return self.rollup_data(file_list, my_data)\n\n    # ------------------------------ Output helpers -----------------------------------\n\n    def load_files_helper(self) -> DataFrame:\n        result = self.load_files()\n\n        # Error condition - raise error if no text and an error is present\n        if not hasattr(result, \"text\"):\n            if hasattr(result, \"error\"):\n                raise ValueError(result.error[0])\n            msg = \"No content generated.\"\n            raise ValueError(msg)\n\n        return result\n\n    def load_files_dataframe(self) -> DataFrame:\n        \"\"\"Load files using advanced Docling processing and export to DataFrame format.\"\"\"\n        self.markdown = False\n        return self.load_files_helper()\n\n    def load_files_markdown(self) -> Message:\n        \"\"\"Load files using advanced Docling processing and export to Markdown format.\"\"\"\n        self.markdown = True\n        result = self.load_files_helper()\n        return Message(text=str(result.text[0]))\n"
              },
              "concurrency_multithreading": {
                "_input_type": "IntInput",
                "advanced": true,
                "display_name": "Processing Concurrency",
                "dynamic": false,
                "info": "When multiple files are being processed, the number of files to process concurrently.",
                "list": false,
                "list_add_label": "Add More",
                "name": "concurrency_multithreading",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "int",
                "value": 1
              },
              "delete_server_file_after_processing": {
                "_input_type": "BoolInput",
                "advanced": true,
                "display_name": "Delete Server File After Processing",
                "dynamic": false,
                "info": "If true, the Server File Path will be deleted after processing.",
                "list": false,
                "list_add_label": "Add More",
                "name": "delete_server_file_after_processing",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "bool",
                "value": true
              },
              "doc_key": {
                "_input_type": "MessageTextInput",
                "advanced": true,
                "display_name": "Doc Key",
                "dynamic": false,
                "info": "The key to use for the DoclingDocument column.",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "doc_key",
                "placeholder": "",
                "required": false,
                "show": false,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "doc"
              },
              "file_path": {
                "_input_type": "HandleInput",
                "advanced": true,
                "display_name": "Server File Path",
                "dynamic": false,
                "info": "Data object with a 'file_path' property pointing to server file or a Message object with a path to the file. Supercedes 'Path' but supports same file types.",
                "input_types": [
                  "Data",
                  "Message"
                ],
                "list": true,
                "list_add_label": "Add More",
                "name": "file_path",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "trace_as_metadata": true,
                "type": "other",
                "value": ""
              },
              "ignore_unspecified_files": {
                "_input_type": "BoolInput",
                "advanced": true,
                "display_name": "Ignore Unspecified Files",
                "dynamic": false,
                "info": "If true, Data with no 'file_path' property will be ignored.",
                "list": false,
                "list_add_label": "Add More",
                "name": "ignore_unspecified_files",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "bool",
                "value": false
              },
              "ignore_unsupported_extensions": {
                "_input_type": "BoolInput",
                "advanced": true,
                "display_name": "Ignore Unsupported Extensions",
                "dynamic": false,
                "info": "If true, files with unsupported extensions will not be processed.",
                "list": false,
                "list_add_label": "Add More",
                "name": "ignore_unsupported_extensions",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "bool",
                "value": true
              },
              "markdown": {
                "_input_type": "BoolInput",
                "advanced": false,
                "display_name": "Markdown Export",
                "dynamic": false,
                "info": "Export processed documents to Markdown format. Only available when advanced mode is enabled.",
                "list": false,
                "list_add_label": "Add More",
                "name": "markdown",
                "placeholder": "",
                "required": false,
                "show": false,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "bool",
                "value": false
              },
              "md_image_placeholder": {
                "_input_type": "StrInput",
                "advanced": true,
                "display_name": "Image placeholder",
                "dynamic": false,
                "info": "Specify the image placeholder for markdown exports.",
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "md_image_placeholder",
                "placeholder": "",
                "required": false,
                "show": false,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "str",
                "value": "<!-- image -->"
              },
              "md_page_break_placeholder": {
                "_input_type": "StrInput",
                "advanced": true,
                "display_name": "Page break placeholder",
                "dynamic": false,
                "info": "Add this placeholder between pages in the markdown output.",
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "md_page_break_placeholder",
                "placeholder": "",
                "required": false,
                "show": false,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "str",
                "value": ""
              },
              "ocr_engine": {
                "_input_type": "DropdownInput",
                "advanced": true,
                "combobox": false,
                "dialog_inputs": {},
                "display_name": "OCR Engine",
                "dynamic": false,
                "external_options": {},
                "info": "OCR engine to use. Only available when pipeline is set to 'standard'.",
                "name": "ocr_engine",
                "options": [
                  "None",
                  "easyocr"
                ],
                "options_metadata": [],
                "placeholder": "",
                "required": false,
                "show": false,
                "title_case": false,
                "toggle": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "str",
                "value": "easyocr"
              },
              "path": {
                "_input_type": "FileInput",
                "advanced": false,
                "display_name": "Files",
                "dynamic": false,
                "fileTypes": [
                  "csv",
                  "json",
                  "pdf",
                  "txt",
                  "md",
                  "mdx",
                  "yaml",
                  "yml",
                  "xml",
                  "html",
                  "htm",
                  "docx",
                  "py",
                  "sh",
                  "sql",
                  "js",
                  "ts",
                  "tsx",
                  "adoc",
                  "asciidoc",
                  "asc",
                  "bmp",
                  "dotx",
                  "dotm",
                  "docm",
                  "jpeg",
                  "png",
                  "potx",
                  "ppsx",
                  "pptm",
                  "potm",
                  "ppsm",
                  "pptx",
                  "tiff",
                  "xls",
                  "xlsx",
                  "xhtml",
                  "webp",
                  "zip",
                  "tar",
                  "tgz",
                  "bz2",
                  "gz"
                ],
                "file_path": [],
                "info": "Supported file extensions: csv, json, pdf, txt, md, mdx, yaml, yml, xml, html, htm, docx, py, sh, sql, js, ts, tsx, adoc, asciidoc, asc, bmp, dotx, dotm, docm, jpeg, png, potx, ppsx, pptm, potm, ppsm, pptx, tiff, xls, xlsx, xhtml, webp; optionally bundled in file extensions: zip, tar, tgz, bz2, gz",
                "list": true,
                "list_add_label": "Add More",
                "name": "path",
                "placeholder": "",
                "real_time_refresh": true,
                "required": false,
                "show": true,
                "temp_file": false,
                "title_case": false,
                "trace_as_metadata": true,
                "type": "file",
                "value": ""
              },
              "pipeline": {
                "_input_type": "DropdownInput",
                "advanced": true,
                "combobox": false,
                "dialog_inputs": {},
                "display_name": "Pipeline",
                "dynamic": false,
                "external_options": {},
                "info": "Docling pipeline to use",
                "name": "pipeline",
                "options": [
                  "standard",
                  "vlm"
                ],
                "options_metadata": [],
                "placeholder": "",
                "real_time_refresh": true,
                "required": false,
                "show": false,
                "title_case": false,
                "toggle": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "str",
                "value": "standard"
              },
              "separator": {
                "_input_type": "StrInput",
                "advanced": true,
                "display_name": "Separator",
                "dynamic": false,
                "info": "Specify the separator to use between multiple outputs in Message format.",
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "separator",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "str",
                "value": "\n\n"
              },
              "silent_errors": {
                "_input_type": "BoolInput",
                "advanced": true,
                "display_name": "Silent Errors",
                "dynamic": false,
                "info": "If true, errors will not raise an exception.",
                "list": false,
                "list_add_label": "Add More",
                "name": "silent_errors",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "bool",
                "value": false
              },
              "use_multithreading": {
                "_input_type": "BoolInput",
                "advanced": true,
                "display_name": "[Deprecated] Use Multithreading",
                "dynamic": false,
                "info": "Set 'Processing Concurrency' greater than 1 to enable multithreading.",
                "list": false,
                "list_add_label": "Add More",
                "name": "use_multithreading",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "bool",
                "value": true
              }
            },
            "tool_mode": false
          },
          "selected_output": "dataframe",
          "showNode": true,
          "type": "File"
        },
        "dragging": false,
        "id": "File-5iHSR",
        "measured": {
          "height": 213,
          "width": 320
        },
        "position": {
          "x": 441.01130151116365,
          "y": 3106.495551675048
        },
        "selected": false,
        "type": "genericNode"
      },
      {
        "data": {
          "id": "msp_time_entry_etl-hIlCX",
          "node": {
            "base_classes": [
              "DataFrame",
              "Message"
            ],
            "beta": false,
            "conditional_paths": [],
            "custom_fields": {},
            "description": "Clean and normalize ConnectWise time entries. Adds internal flags, parsed dates, and numeric hours.",
            "display_name": "MSP Time Entry ETL",
            "documentation": "",
            "edited": true,
            "field_order": [
              "time_df",
              "date_col",
              "member_col",
              "company_col",
              "department_col",
              "project_col",
              "ticket_col",
              "hours_col",
              "billable_col",
              "status_col",
              "notes_col",
              "internal_departments",
              "internal_companies",
              "drop_zero_hours"
            ],
            "frozen": false,
            "icon": "FileSpreadsheet",
            "legacy": false,
            "lf_version": "1.6.0",
            "metadata": {},
            "minimized": false,
            "output_types": [],
            "outputs": [
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Time Entries (Clean)",
                "group_outputs": false,
                "hidden": null,
                "method": "build_time_clean",
                "name": "time_clean",
                "options": null,
                "required_inputs": null,
                "selected": "DataFrame",
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "ETL Summary",
                "group_outputs": false,
                "hidden": null,
                "method": "build_etl_summary",
                "name": "etl_summary",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "Message"
                ],
                "value": "__UNDEFINED__"
              }
            ],
            "pinned": false,
            "template": {
              "_type": "Component",
              "billable_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Billable column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "billable_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Billable"
              },
              "code": {
                "advanced": true,
                "dynamic": true,
                "fileTypes": [],
                "file_path": "",
                "info": "",
                "list": false,
                "load_from_db": false,
                "multiline": true,
                "name": "code",
                "password": false,
                "placeholder": "",
                "required": true,
                "show": true,
                "title_case": false,
                "type": "code",
                "value": "from __future__ import annotations\n\nfrom typing import Any, Dict, List\nimport pandas as pd\nimport numpy as np\n\nfrom langflow.custom import Component\nfrom langflow.io import DataFrameInput, MessageTextInput, BoolInput, Output\nfrom langflow.schema import DataFrame\nfrom langflow.schema.message import Message\n\n\nclass MSPTimeEntryETL(Component):\n    display_name = \"MSP Time Entry ETL\"\n    description = \"Clean and normalize ConnectWise time entries. Adds internal flags, parsed dates, and numeric hours.\"\n    name = \"msp_time_entry_etl\"\n    icon = \"FileSpreadsheet\"\n\n    inputs = [\n        DataFrameInput(name=\"time_df\", display_name=\"Time Entries DataFrame\", required=True),\n\n        MessageTextInput(name=\"date_col\", display_name=\"Date column\", value=\"Date\"),\n        MessageTextInput(name=\"member_col\", display_name=\"Member column\", value=\"Member\"),\n        MessageTextInput(name=\"company_col\", display_name=\"Company column\", value=\"Company\"),\n        MessageTextInput(name=\"department_col\", display_name=\"Department column\", value=\"Department\"),\n        MessageTextInput(name=\"project_col\", display_name=\"Project column (optional)\", value=\"Project\"),\n        MessageTextInput(name=\"ticket_col\", display_name=\"Ticket/Service column\", value=\"Service #\"),\n        MessageTextInput(name=\"hours_col\", display_name=\"Hours column\", value=\"Hours\"),\n        MessageTextInput(name=\"billable_col\", display_name=\"Billable column\", value=\"Billable\"),\n        MessageTextInput(name=\"status_col\", display_name=\"Status column (optional)\", value=\"Status\"),\n        MessageTextInput(name=\"notes_col\", display_name=\"Notes column (optional)\", value=\"Notes\"),\n\n        # Internal classification rules\n        MessageTextInput(\n            name=\"internal_departments\",\n            display_name=\"Internal departments (comma-separated)\",\n            value=\"Office/Admin (INTERNAL)\",\n        ),\n        MessageTextInput(\n            name=\"internal_companies\",\n            display_name=\"Internal companies (comma-separated, optional)\",\n            value=\"Generation IX\",\n        ),\n\n        BoolInput(name=\"drop_zero_hours\", display_name=\"Drop rows with 0 hours\", value=True),\n    ]\n\n    outputs = [\n        Output(name=\"time_clean\", display_name=\"Time Entries (Clean)\", method=\"build_time_clean\"),\n        Output(name=\"etl_summary\", display_name=\"ETL Summary\", method=\"build_etl_summary\"),\n    ]\n\n    def _unwrap_to_df(self, obj: Any) -> pd.DataFrame:\n        if obj is None:\n            return pd.DataFrame()\n        if isinstance(obj, pd.DataFrame):\n            df = obj.copy()\n        elif hasattr(obj, \"data\"):\n            d = getattr(obj, \"data\")\n            df = d.copy() if isinstance(d, pd.DataFrame) else pd.DataFrame(d)\n        else:\n            df = pd.DataFrame(obj)\n        df.columns = [str(c).replace(\"\\ufeff\", \"\").strip() for c in df.columns]\n        return df\n\n    def _safe_col(self, df: pd.DataFrame, col: str) -> bool:\n        return col is not None and str(col).strip() != \"\" and col in df.columns\n\n    def _to_dt(self, s: pd.Series) -> pd.Series:\n        return pd.to_datetime(s, errors=\"coerce\")\n\n    def _parse_list(self, s: str) -> List[str]:\n        return [x.strip().lower() for x in str(s or \"\").split(\",\") if x.strip()]\n\n    def build_time_clean(self) -> DataFrame:\n        df = self._unwrap_to_df(self.time_df)\n        if df.empty:\n            return DataFrame(pd.DataFrame())\n\n        # Parse date\n        if self._safe_col(df, self.date_col):\n            df[self.date_col] = self._to_dt(df[self.date_col])\n\n        # Normalize key text fields\n        for c in [self.member_col, self.company_col, self.department_col, self.project_col, self.billable_col, self.status_col]:\n            if self._safe_col(df, c):\n                df[c] = df[c].astype(str).str.strip()\n\n        # Hours numeric\n        if self._safe_col(df, self.hours_col):\n            df[self.hours_col] = pd.to_numeric(df[self.hours_col], errors=\"coerce\").fillna(0.0)\n        else:\n            df[self.hours_col] = 0.0\n\n        if bool(self.drop_zero_hours):\n            df = df[df[self.hours_col] > 0].copy()\n\n        # Internal flags\n        internal_depts = set(self._parse_list(self.internal_departments))\n        internal_cos = set(self._parse_list(self.internal_companies))\n\n        dept_norm = df[self.department_col].fillna(\"\").astype(str).str.lower().str.strip() if self._safe_col(df, self.department_col) else \"\"\n        co_norm = df[self.company_col].fillna(\"\").astype(str).str.lower().str.strip() if self._safe_col(df, self.company_col) else \"\"\n\n        df[\"is_internal\"] = 0\n        if self._safe_col(df, self.department_col):\n            df.loc[dept_norm.isin(internal_depts), \"is_internal\"] = 1\n        if self._safe_col(df, self.company_col) and len(internal_cos) > 0:\n            df.loc[co_norm.isin(internal_cos), \"is_internal\"] = 1\n\n        # Billable normalization\n        df[\"billable_class\"] = df[self.billable_col].fillna(\"\").astype(str).str.strip() if self._safe_col(df, self.billable_col) else \"\"\n        df[\"is_billable\"] = df[\"billable_class\"].astype(str).str.lower().isin([\"billable\", \"yes\", \"true\", \"1\"]).astype(int)\n\n        # Month\n        if self._safe_col(df, self.date_col) and df[self.date_col].notna().any():\n            df[\"month\"] = df[self.date_col].dt.to_period(\"M\").astype(str)\n            df[\"dow\"] = df[self.date_col].dt.day_name()\n            df[\"is_weekend\"] = (df[self.date_col].dt.dayofweek >= 5).astype(int)\n        else:\n            df[\"month\"] = None\n            df[\"dow\"] = None\n            df[\"is_weekend\"] = np.nan\n\n        # Standardized columns\n        df[\"member\"] = df[self.member_col] if self._safe_col(df, self.member_col) else \"(unknown)\"\n        df[\"company\"] = df[self.company_col] if self._safe_col(df, self.company_col) else \"(unknown)\"\n        df[\"department\"] = df[self.department_col] if self._safe_col(df, self.department_col) else \"(unknown)\"\n        df[\"project\"] = df[self.project_col] if self._safe_col(df, self.project_col) else \"\"\n        df[\"ticket_id\"] = df[self.ticket_col].astype(str).str.strip() if self._safe_col(df, self.ticket_col) else \"\"\n\n        return DataFrame(df)\n\n    def build_etl_summary(self) -> Message:\n        df = pd.DataFrame(self.build_time_clean())\n        out: Dict[str, Any] = {\n            \"rows\": int(len(df)),\n            \"columns\": list(df.columns),\n            \"members\": int(df[\"member\"].nunique()) if \"member\" in df.columns else 0,\n            \"companies\": int(df[\"company\"].nunique()) if \"company\" in df.columns else 0,\n            \"internal_rows\": int(df[\"is_internal\"].sum()) if \"is_internal\" in df.columns else 0,\n            \"billable_rows\": int(df[\"is_billable\"].sum()) if \"is_billable\" in df.columns else 0,\n            \"total_hours\": float(df[self.hours_col].sum()) if self.hours_col in df.columns else 0.0,\n        }\n        return Message(text=str(out))"
              },
              "company_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Company column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "company_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Company"
              },
              "date_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Date column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "date_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Date"
              },
              "department_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Department column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "department_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Department"
              },
              "drop_zero_hours": {
                "_input_type": "BoolInput",
                "advanced": false,
                "display_name": "Drop rows with 0 hours",
                "dynamic": false,
                "info": "",
                "list": false,
                "list_add_label": "Add More",
                "name": "drop_zero_hours",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "bool",
                "value": true
              },
              "hours_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Hours column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "hours_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Hours"
              },
              "internal_companies": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Internal companies (comma-separated, optional)",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "internal_companies",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "GENERATION IX"
              },
              "internal_departments": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Internal departments (comma-separated)",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "internal_departments",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Office/Admin (INTERNAL)"
              },
              "member_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Member column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "member_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Member"
              },
              "notes_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Notes column (optional)",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "notes_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Notes"
              },
              "project_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Project column (optional)",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "project_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Project"
              },
              "status_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Status column (optional)",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "status_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Status"
              },
              "ticket_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Ticket/Service column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "ticket_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Service #"
              },
              "time_df": {
                "_input_type": "DataFrameInput",
                "advanced": false,
                "display_name": "Time Entries DataFrame",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "DataFrame"
                ],
                "list": false,
                "list_add_label": "Add More",
                "name": "time_df",
                "placeholder": "",
                "required": true,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "other",
                "value": ""
              }
            },
            "tool_mode": false
          },
          "selected_output": "time_clean",
          "showNode": true,
          "type": "msp_time_entry_etl"
        },
        "dragging": false,
        "id": "msp_time_entry_etl-hIlCX",
        "measured": {
          "height": 1223,
          "width": 320
        },
        "position": {
          "x": 1379.3850822650616,
          "y": 2703.432736234141
        },
        "selected": false,
        "type": "genericNode"
      },
      {
        "data": {
          "id": "msp_time_entry_metrics_builder-TTywd",
          "node": {
            "base_classes": [
              "DataFrame",
              "Message"
            ],
            "beta": false,
            "conditional_paths": [],
            "custom_fields": {},
            "description": "Build utilization, internal vs client mix, and fully-loaded labor cost rollups for time entries.",
            "display_name": "MSP Time Entry Metrics Builder",
            "documentation": "",
            "edited": true,
            "field_order": [
              "time_df",
              "hours_col",
              "month_col",
              "member_col",
              "company_col",
              "department_col",
              "billable_flag_col",
              "internal_flag_col",
              "weekend_flag_col",
              "annual_salary",
              "annual_work_hours",
              "benefits_multiplier_x100",
              "top_n"
            ],
            "frozen": false,
            "icon": "BarChart3",
            "legacy": false,
            "lf_version": "1.6.0",
            "metadata": {},
            "minimized": false,
            "output_types": [],
            "outputs": [
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "KPI Overview",
                "group_outputs": false,
                "hidden": null,
                "method": "build_kpi_overview",
                "name": "kpi_overview",
                "options": null,
                "required_inputs": null,
                "selected": "DataFrame",
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Hours by Member",
                "group_outputs": false,
                "hidden": null,
                "method": "build_hours_by_member",
                "name": "hours_by_member",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Hours by Company",
                "group_outputs": false,
                "hidden": null,
                "method": "build_hours_by_company",
                "name": "hours_by_company",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Hours by Month",
                "group_outputs": false,
                "hidden": null,
                "method": "build_hours_by_month",
                "name": "hours_by_month",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Internal Breakdown",
                "group_outputs": false,
                "hidden": null,
                "method": "build_internal_breakdown",
                "name": "internal_breakdown",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Weekend vs Weekday",
                "group_outputs": false,
                "hidden": null,
                "method": "build_weekend_vs_weekday",
                "name": "weekend_vs_weekday",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "LLM Context",
                "group_outputs": false,
                "hidden": null,
                "method": "build_llm_context",
                "name": "llm_context",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "Message"
                ],
                "value": "__UNDEFINED__"
              }
            ],
            "pinned": false,
            "template": {
              "_type": "Component",
              "annual_salary": {
                "_input_type": "IntInput",
                "advanced": false,
                "display_name": "Annual salary per tech (USD)",
                "dynamic": false,
                "info": "",
                "list": false,
                "list_add_label": "Add More",
                "name": "annual_salary",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "int",
                "value": 55000
              },
              "annual_work_hours": {
                "_input_type": "IntInput",
                "advanced": false,
                "display_name": "Annual work hours",
                "dynamic": false,
                "info": "",
                "list": false,
                "list_add_label": "Add More",
                "name": "annual_work_hours",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "int",
                "value": 2080
              },
              "benefits_multiplier_x100": {
                "_input_type": "IntInput",
                "advanced": false,
                "display_name": "Benefits multiplier x100 (125 = 1.25)",
                "dynamic": false,
                "info": "",
                "list": false,
                "list_add_label": "Add More",
                "name": "benefits_multiplier_x100",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "int",
                "value": 125
              },
              "billable_flag_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Billable flag column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "billable_flag_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "is_billable"
              },
              "code": {
                "advanced": true,
                "dynamic": true,
                "fileTypes": [],
                "file_path": "",
                "info": "",
                "list": false,
                "load_from_db": false,
                "multiline": true,
                "name": "code",
                "password": false,
                "placeholder": "",
                "required": true,
                "show": true,
                "title_case": false,
                "type": "code",
                "value": "from __future__ import annotations\n\nfrom typing import Any, List, Dict\nimport pandas as pd\nimport numpy as np\n\nfrom langflow.custom import Component\nfrom langflow.io import DataFrameInput, IntInput, MessageTextInput, Output\nfrom langflow.schema import DataFrame\nfrom langflow.schema.message import Message\n\n\nclass MSPTimeEntryMetricsBuilder(Component):\n    display_name = \"MSP Time Entry Metrics Builder\"\n    description = \"Build utilization, internal vs client mix, and fully-loaded labor cost rollups for time entries.\"\n    name = \"msp_time_entry_metrics_builder\"\n    icon = \"BarChart3\"\n\n    inputs = [\n        DataFrameInput(name=\"time_df\", display_name=\"Time Entries (Clean) DataFrame\", required=True),\n\n        MessageTextInput(name=\"hours_col\", display_name=\"Hours column\", value=\"Hours\"),\n        MessageTextInput(name=\"month_col\", display_name=\"Month column\", value=\"month\"),\n        MessageTextInput(name=\"member_col\", display_name=\"Member column\", value=\"member\"),\n        MessageTextInput(name=\"company_col\", display_name=\"Company column\", value=\"company\"),\n        MessageTextInput(name=\"department_col\", display_name=\"Department column\", value=\"department\"),\n        MessageTextInput(name=\"billable_flag_col\", display_name=\"Billable flag column\", value=\"is_billable\"),\n        MessageTextInput(name=\"internal_flag_col\", display_name=\"Internal flag column\", value=\"is_internal\"),\n        MessageTextInput(name=\"weekend_flag_col\", display_name=\"Weekend flag column\", value=\"is_weekend\"),\n\n        IntInput(name=\"annual_salary\", display_name=\"Annual salary per tech (USD)\", value=55000),\n        IntInput(name=\"annual_work_hours\", display_name=\"Annual work hours\", value=2080),\n        IntInput(name=\"benefits_multiplier_x100\", display_name=\"Benefits multiplier x100 (125 = 1.25)\", value=125),\n        IntInput(name=\"top_n\", display_name=\"Top N rows\", value=15),\n    ]\n\n    outputs = [\n        Output(name=\"kpi_overview\", display_name=\"KPI Overview\", method=\"build_kpi_overview\"),\n        Output(name=\"hours_by_member\", display_name=\"Hours by Member\", method=\"build_hours_by_member\"),\n        Output(name=\"hours_by_company\", display_name=\"Hours by Company\", method=\"build_hours_by_company\"),\n        Output(name=\"hours_by_month\", display_name=\"Hours by Month\", method=\"build_hours_by_month\"),\n        Output(name=\"internal_breakdown\", display_name=\"Internal Breakdown\", method=\"build_internal_breakdown\"),\n        Output(name=\"weekend_vs_weekday\", display_name=\"Weekend vs Weekday\", method=\"build_weekend_vs_weekday\"),\n        Output(name=\"llm_context\", display_name=\"LLM Context\", method=\"build_llm_context\"),\n    ]\n\n    def _unwrap_to_df(self, obj: Any) -> pd.DataFrame:\n        if obj is None:\n            return pd.DataFrame()\n        if isinstance(obj, pd.DataFrame):\n            return obj.copy()\n        if hasattr(obj, \"data\"):\n            d = getattr(obj, \"data\")\n            if isinstance(d, pd.DataFrame):\n                return d.copy()\n            if isinstance(d, list):\n                return pd.DataFrame(d)\n        return pd.DataFrame(obj)\n\n    def _hourly_cost(self) -> float:\n        mult = float(self.benefits_multiplier_x100) / 100.0\n        return (float(self.annual_salary) * mult) / max(float(self.annual_work_hours), 1.0)\n\n    def _prep(self) -> pd.DataFrame:\n        df = self._unwrap_to_df(self.time_df)\n        if df.empty:\n            return df\n\n        df = df.copy()\n        df[self.hours_col] = pd.to_numeric(df.get(self.hours_col, 0.0), errors=\"coerce\").fillna(0.0)\n        df[self.billable_flag_col] = pd.to_numeric(df.get(self.billable_flag_col, 0), errors=\"coerce\").fillna(0).astype(int)\n        df[self.internal_flag_col] = pd.to_numeric(df.get(self.internal_flag_col, 0), errors=\"coerce\").fillna(0).astype(int)\n\n        df[\"hourly_cost\"] = self._hourly_cost()\n        df[\"labor_cost\"] = df[self.hours_col] * df[\"hourly_cost\"]\n\n        return df\n\n    def build_kpi_overview(self) -> DataFrame:\n        df = self._prep()\n        if df.empty:\n            return DataFrame(pd.DataFrame())\n\n        total_hours = float(df[self.hours_col].sum())\n        billable_hours = float(df.loc[df[self.billable_flag_col] == 1, self.hours_col].sum())\n        internal_hours = float(df.loc[df[self.internal_flag_col] == 1, self.hours_col].sum())\n        total_cost = float(df[\"labor_cost\"].sum())\n\n        out = pd.DataFrame([{\n            \"hourly_cost_fully_loaded\": float(df[\"hourly_cost\"].iloc[0]),\n            \"total_hours\": total_hours,\n            \"billable_hours\": billable_hours,\n            \"billable_utilization\": billable_hours / max(total_hours, 1e-9),\n            \"internal_hours\": internal_hours,\n            \"internal_share\": internal_hours / max(total_hours, 1e-9),\n            \"labor_cost_total\": total_cost,\n        }])\n        return DataFrame(out)\n\n    def build_hours_by_member(self) -> DataFrame:\n        df = self._prep()\n        if df.empty:\n            return DataFrame(pd.DataFrame())\n\n        g = df.groupby(self.member_col, dropna=False)\n        out = pd.DataFrame({\n            \"member\": g.size().index.astype(str),\n            \"entries\": g.size().values,\n            \"hours_total\": g[self.hours_col].sum().values,\n            \"hours_billable\": g.apply(lambda x: float(x.loc[x[self.billable_flag_col] == 1, self.hours_col].sum())).values,\n            \"hours_internal\": g.apply(lambda x: float(x.loc[x[self.internal_flag_col] == 1, self.hours_col].sum())).values,\n            \"labor_cost\": g[\"labor_cost\"].sum().values,\n        })\n        out[\"billable_utilization\"] = out[\"hours_billable\"] / out[\"hours_total\"].replace(0, np.nan)\n        return DataFrame(out.sort_values(\"hours_total\", ascending=False).head(int(self.top_n)))\n\n    def build_hours_by_company(self) -> DataFrame:\n        df = self._prep()\n        if df.empty:\n            return DataFrame(pd.DataFrame())\n        g = df.groupby(self.company_col, dropna=False)\n        out = pd.DataFrame({\n            \"company\": g.size().index.astype(str),\n            \"hours_total\": g[self.hours_col].sum().values,\n            \"labor_cost\": g[\"labor_cost\"].sum().values,\n        })\n        return DataFrame(out.sort_values(\"hours_total\", ascending=False).head(int(self.top_n)))\n\n    def build_hours_by_month(self) -> DataFrame:\n        df = self._prep()\n        if df.empty or self.month_col not in df.columns:\n            return DataFrame(pd.DataFrame(columns=[\"month\", \"hours_total\", \"hours_billable\", \"hours_internal\", \"labor_cost\"]))\n        g = df.groupby(self.month_col, dropna=False)\n        out = pd.DataFrame({\n            \"month\": g.size().index.astype(str),\n            \"hours_total\": g[self.hours_col].sum().values,\n            \"hours_billable\": g.apply(lambda x: float(x.loc[x[self.billable_flag_col] == 1, self.hours_col].sum())).values,\n            \"hours_internal\": g.apply(lambda x: float(x.loc[x[self.internal_flag_col] == 1, self.hours_col].sum())).values,\n            \"labor_cost\": g[\"labor_cost\"].sum().values,\n        })\n        out[\"billable_utilization\"] = out[\"hours_billable\"] / out[\"hours_total\"].replace(0, np.nan)\n        return DataFrame(out.sort_values(\"month\"))\n\n    def build_internal_breakdown(self) -> DataFrame:\n        df = self._prep()\n        if df.empty:\n            return DataFrame(pd.DataFrame())\n        internal = df[df[self.internal_flag_col] == 1].copy()\n        if internal.empty:\n            return DataFrame(pd.DataFrame(columns=[\"department\", \"hours_total\", \"labor_cost\"]))\n        g = internal.groupby(self.department_col, dropna=False)\n        out = pd.DataFrame({\n            \"department\": g.size().index.astype(str),\n            \"hours_total\": g[self.hours_col].sum().values,\n            \"labor_cost\": g[\"labor_cost\"].sum().values,\n        })\n        return DataFrame(out.sort_values(\"hours_total\", ascending=False))\n\n    def build_weekend_vs_weekday(self) -> DataFrame:\n        df = self._prep()\n        if df.empty or self.weekend_flag_col not in df.columns:\n            return DataFrame(pd.DataFrame(columns=[\"is_weekend\", \"hours_total\", \"labor_cost\"]))\n        g = df.groupby(self.weekend_flag_col, dropna=False)\n        out = pd.DataFrame({\n            \"is_weekend\": g.size().index.astype(int),\n            \"hours_total\": g[self.hours_col].sum().values,\n            \"labor_cost\": g[\"labor_cost\"].sum().values,\n        })\n        return DataFrame(out.sort_values(\"is_weekend\"))\n\n    def build_llm_context(self) -> Message:\n        kpi = self._unwrap_to_df(self.build_kpi_overview())\n        by_member = self._unwrap_to_df(self.build_hours_by_member())\n        by_company = self._unwrap_to_df(self.build_hours_by_company())\n        by_month = self._unwrap_to_df(self.build_hours_by_month())\n\n        def fmt_money(x) -> str:\n            return \"n/a\" if pd.isna(x) else f\"${float(x):,.2f}\"\n\n        def fmt_pct(x) -> str:\n            return \"n/a\" if pd.isna(x) else f\"{float(x)*100:.1f}%\"\n\n        lines: List[str] = []\n        lines.append(\"TIME ENTRY ANALYSIS CONTEXT (Generation IX)\")\n        if not kpi.empty:\n            r = kpi.iloc[0].to_dict()\n            lines.append(f\"- hourly_cost_fully_loaded: {fmt_money(r.get('hourly_cost_fully_loaded'))}\")\n            lines.append(f\"- total_hours: {r.get('total_hours')}\")\n            lines.append(f\"- billable_utilization: {fmt_pct(r.get('billable_utilization'))}\")\n            lines.append(f\"- internal_share: {fmt_pct(r.get('internal_share'))}\")\n            lines.append(f\"- labor_cost_total: {fmt_money(r.get('labor_cost_total'))}\")\n\n        lines.append(\"\")\n        lines.append(\"TOP TECHS (hours)\")\n        for _, row in by_member.head(10).iterrows():\n            lines.append(\n                f\"- {row.get('member')}: hours={float(row.get('hours_total')):.2f}, \"\n                f\"billable_util={fmt_pct(row.get('billable_utilization'))}, cost={fmt_money(row.get('labor_cost'))}\"\n            )\n\n        lines.append(\"\")\n        lines.append(\"TOP CLIENTS (hours)\")\n        for _, row in by_company.head(10).iterrows():\n            lines.append(f\"- {row.get('company')}: hours={float(row.get('hours_total')):.2f}, cost={fmt_money(row.get('labor_cost'))}\")\n\n        if not by_month.empty:\n            lines.append(\"\")\n            lines.append(\"MONTHLY TREND (hours, utilization)\")\n            for _, row in by_month.tail(6).iterrows():\n                lines.append(\n                    f\"- {row.get('month')}: hours={float(row.get('hours_total')):.2f}, \"\n                    f\"billable_util={fmt_pct(row.get('billable_utilization'))}, cost={fmt_money(row.get('labor_cost'))}\"\n                )\n\n        return Message(text=\"\\n\".join(lines))"
              },
              "company_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Company column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "company_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "company"
              },
              "department_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Department column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "department_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "department"
              },
              "hours_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Hours column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "hours_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Hours"
              },
              "internal_flag_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Internal flag column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "internal_flag_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "is_internal"
              },
              "member_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Member column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "member_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Member"
              },
              "month_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Month column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "month_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "month"
              },
              "time_df": {
                "_input_type": "DataFrameInput",
                "advanced": false,
                "display_name": "Time Entries (Clean) DataFrame",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "DataFrame"
                ],
                "list": false,
                "list_add_label": "Add More",
                "name": "time_df",
                "placeholder": "",
                "required": true,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "other",
                "value": ""
              },
              "top_n": {
                "_input_type": "IntInput",
                "advanced": false,
                "display_name": "Top N rows",
                "dynamic": false,
                "info": "",
                "list": false,
                "list_add_label": "Add More",
                "name": "top_n",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "int",
                "value": 15
              },
              "weekend_flag_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Weekend flag column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "weekend_flag_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "is_weekend"
              }
            },
            "tool_mode": false
          },
          "selected_output": "kpi_overview",
          "showNode": true,
          "type": "msp_time_entry_metrics_builder"
        },
        "dragging": false,
        "id": "msp_time_entry_metrics_builder-TTywd",
        "measured": {
          "height": 1165,
          "width": 320
        },
        "position": {
          "x": 2115.57106577338,
          "y": 2705.410539186223
        },
        "selected": false,
        "type": "genericNode"
      },
      {
        "data": {
          "id": "msp_time_entry_metrics_builder-Rw7a8",
          "node": {
            "base_classes": [
              "DataFrame",
              "Message"
            ],
            "beta": false,
            "conditional_paths": [],
            "custom_fields": {},
            "description": "Build utilization, internal vs client mix, and fully-loaded labor cost rollups for time entries.",
            "display_name": "MSP Time Entry Metrics Builder",
            "documentation": "",
            "edited": true,
            "field_order": [
              "time_df",
              "hours_col",
              "month_col",
              "member_col",
              "company_col",
              "department_col",
              "billable_flag_col",
              "internal_flag_col",
              "weekend_flag_col",
              "annual_salary",
              "annual_work_hours",
              "benefits_multiplier_x100",
              "top_n"
            ],
            "frozen": false,
            "icon": "BarChart3",
            "legacy": false,
            "lf_version": "1.6.0",
            "metadata": {},
            "minimized": false,
            "output_types": [],
            "outputs": [
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "KPI Overview",
                "group_outputs": false,
                "hidden": null,
                "method": "build_kpi_overview",
                "name": "kpi_overview",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Hours by Member",
                "group_outputs": false,
                "hidden": null,
                "method": "build_hours_by_member",
                "name": "hours_by_member",
                "options": null,
                "required_inputs": null,
                "selected": "DataFrame",
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Hours by Company",
                "group_outputs": false,
                "hidden": null,
                "method": "build_hours_by_company",
                "name": "hours_by_company",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Hours by Month",
                "group_outputs": false,
                "hidden": null,
                "method": "build_hours_by_month",
                "name": "hours_by_month",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Internal Breakdown",
                "group_outputs": false,
                "hidden": null,
                "method": "build_internal_breakdown",
                "name": "internal_breakdown",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Weekend vs Weekday",
                "group_outputs": false,
                "hidden": null,
                "method": "build_weekend_vs_weekday",
                "name": "weekend_vs_weekday",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "LLM Context",
                "group_outputs": false,
                "hidden": null,
                "method": "build_llm_context",
                "name": "llm_context",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "Message"
                ],
                "value": "__UNDEFINED__"
              }
            ],
            "pinned": false,
            "template": {
              "_type": "Component",
              "annual_salary": {
                "_input_type": "IntInput",
                "advanced": false,
                "display_name": "Annual salary per tech (USD)",
                "dynamic": false,
                "info": "",
                "list": false,
                "list_add_label": "Add More",
                "name": "annual_salary",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "int",
                "value": 55000
              },
              "annual_work_hours": {
                "_input_type": "IntInput",
                "advanced": false,
                "display_name": "Annual work hours",
                "dynamic": false,
                "info": "",
                "list": false,
                "list_add_label": "Add More",
                "name": "annual_work_hours",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "int",
                "value": 2080
              },
              "benefits_multiplier_x100": {
                "_input_type": "IntInput",
                "advanced": false,
                "display_name": "Benefits multiplier x100 (125 = 1.25)",
                "dynamic": false,
                "info": "",
                "list": false,
                "list_add_label": "Add More",
                "name": "benefits_multiplier_x100",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "int",
                "value": 125
              },
              "billable_flag_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Billable flag column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "billable_flag_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "is_billable"
              },
              "code": {
                "advanced": true,
                "dynamic": true,
                "fileTypes": [],
                "file_path": "",
                "info": "",
                "list": false,
                "load_from_db": false,
                "multiline": true,
                "name": "code",
                "password": false,
                "placeholder": "",
                "required": true,
                "show": true,
                "title_case": false,
                "type": "code",
                "value": "from __future__ import annotations\n\nfrom typing import Any, List, Dict\nimport pandas as pd\nimport numpy as np\n\nfrom langflow.custom import Component\nfrom langflow.io import DataFrameInput, IntInput, MessageTextInput, Output\nfrom langflow.schema import DataFrame\nfrom langflow.schema.message import Message\n\n\nclass MSPTimeEntryMetricsBuilder(Component):\n    display_name = \"MSP Time Entry Metrics Builder\"\n    description = \"Build utilization, internal vs client mix, and fully-loaded labor cost rollups for time entries.\"\n    name = \"msp_time_entry_metrics_builder\"\n    icon = \"BarChart3\"\n\n    inputs = [\n        DataFrameInput(name=\"time_df\", display_name=\"Time Entries (Clean) DataFrame\", required=True),\n\n        MessageTextInput(name=\"hours_col\", display_name=\"Hours column\", value=\"Hours\"),\n        MessageTextInput(name=\"month_col\", display_name=\"Month column\", value=\"month\"),\n        MessageTextInput(name=\"member_col\", display_name=\"Member column\", value=\"member\"),\n        MessageTextInput(name=\"company_col\", display_name=\"Company column\", value=\"company\"),\n        MessageTextInput(name=\"department_col\", display_name=\"Department column\", value=\"department\"),\n        MessageTextInput(name=\"billable_flag_col\", display_name=\"Billable flag column\", value=\"is_billable\"),\n        MessageTextInput(name=\"internal_flag_col\", display_name=\"Internal flag column\", value=\"is_internal\"),\n        MessageTextInput(name=\"weekend_flag_col\", display_name=\"Weekend flag column\", value=\"is_weekend\"),\n\n        IntInput(name=\"annual_salary\", display_name=\"Annual salary per tech (USD)\", value=55000),\n        IntInput(name=\"annual_work_hours\", display_name=\"Annual work hours\", value=2080),\n        IntInput(name=\"benefits_multiplier_x100\", display_name=\"Benefits multiplier x100 (125 = 1.25)\", value=125),\n        IntInput(name=\"top_n\", display_name=\"Top N rows\", value=15),\n    ]\n\n    outputs = [\n        Output(name=\"kpi_overview\", display_name=\"KPI Overview\", method=\"build_kpi_overview\"),\n        Output(name=\"hours_by_member\", display_name=\"Hours by Member\", method=\"build_hours_by_member\"),\n        Output(name=\"hours_by_company\", display_name=\"Hours by Company\", method=\"build_hours_by_company\"),\n        Output(name=\"hours_by_month\", display_name=\"Hours by Month\", method=\"build_hours_by_month\"),\n        Output(name=\"internal_breakdown\", display_name=\"Internal Breakdown\", method=\"build_internal_breakdown\"),\n        Output(name=\"weekend_vs_weekday\", display_name=\"Weekend vs Weekday\", method=\"build_weekend_vs_weekday\"),\n        Output(name=\"llm_context\", display_name=\"LLM Context\", method=\"build_llm_context\"),\n    ]\n\n    def _unwrap_to_df(self, obj: Any) -> pd.DataFrame:\n        if obj is None:\n            return pd.DataFrame()\n        if isinstance(obj, pd.DataFrame):\n            return obj.copy()\n        if hasattr(obj, \"data\"):\n            d = getattr(obj, \"data\")\n            if isinstance(d, pd.DataFrame):\n                return d.copy()\n            if isinstance(d, list):\n                return pd.DataFrame(d)\n        return pd.DataFrame(obj)\n\n    def _hourly_cost(self) -> float:\n        mult = float(self.benefits_multiplier_x100) / 100.0\n        return (float(self.annual_salary) * mult) / max(float(self.annual_work_hours), 1.0)\n\n    def _prep(self) -> pd.DataFrame:\n        df = self._unwrap_to_df(self.time_df)\n        if df.empty:\n            return df\n\n        df = df.copy()\n        df[self.hours_col] = pd.to_numeric(df.get(self.hours_col, 0.0), errors=\"coerce\").fillna(0.0)\n        df[self.billable_flag_col] = pd.to_numeric(df.get(self.billable_flag_col, 0), errors=\"coerce\").fillna(0).astype(int)\n        df[self.internal_flag_col] = pd.to_numeric(df.get(self.internal_flag_col, 0), errors=\"coerce\").fillna(0).astype(int)\n\n        df[\"hourly_cost\"] = self._hourly_cost()\n        df[\"labor_cost\"] = df[self.hours_col] * df[\"hourly_cost\"]\n\n        return df\n\n    def build_kpi_overview(self) -> DataFrame:\n        df = self._prep()\n        if df.empty:\n            return DataFrame(pd.DataFrame())\n\n        total_hours = float(df[self.hours_col].sum())\n        billable_hours = float(df.loc[df[self.billable_flag_col] == 1, self.hours_col].sum())\n        internal_hours = float(df.loc[df[self.internal_flag_col] == 1, self.hours_col].sum())\n        total_cost = float(df[\"labor_cost\"].sum())\n\n        out = pd.DataFrame([{\n            \"hourly_cost_fully_loaded\": float(df[\"hourly_cost\"].iloc[0]),\n            \"total_hours\": total_hours,\n            \"billable_hours\": billable_hours,\n            \"billable_utilization\": billable_hours / max(total_hours, 1e-9),\n            \"internal_hours\": internal_hours,\n            \"internal_share\": internal_hours / max(total_hours, 1e-9),\n            \"labor_cost_total\": total_cost,\n        }])\n        return DataFrame(out)\n\n    def build_hours_by_member(self) -> DataFrame:\n        df = self._prep()\n        if df.empty:\n            return DataFrame(pd.DataFrame())\n\n        g = df.groupby(self.member_col, dropna=False)\n        out = pd.DataFrame({\n            \"member\": g.size().index.astype(str),\n            \"entries\": g.size().values,\n            \"hours_total\": g[self.hours_col].sum().values,\n            \"hours_billable\": g.apply(lambda x: float(x.loc[x[self.billable_flag_col] == 1, self.hours_col].sum())).values,\n            \"hours_internal\": g.apply(lambda x: float(x.loc[x[self.internal_flag_col] == 1, self.hours_col].sum())).values,\n            \"labor_cost\": g[\"labor_cost\"].sum().values,\n        })\n        out[\"billable_utilization\"] = out[\"hours_billable\"] / out[\"hours_total\"].replace(0, np.nan)\n        return DataFrame(out.sort_values(\"hours_total\", ascending=False).head(int(self.top_n)))\n\n    def build_hours_by_company(self) -> DataFrame:\n        df = self._prep()\n        if df.empty:\n            return DataFrame(pd.DataFrame())\n        g = df.groupby(self.company_col, dropna=False)\n        out = pd.DataFrame({\n            \"company\": g.size().index.astype(str),\n            \"hours_total\": g[self.hours_col].sum().values,\n            \"labor_cost\": g[\"labor_cost\"].sum().values,\n        })\n        return DataFrame(out.sort_values(\"hours_total\", ascending=False).head(int(self.top_n)))\n\n    def build_hours_by_month(self) -> DataFrame:\n        df = self._prep()\n        if df.empty or self.month_col not in df.columns:\n            return DataFrame(pd.DataFrame(columns=[\"month\", \"hours_total\", \"hours_billable\", \"hours_internal\", \"labor_cost\"]))\n        g = df.groupby(self.month_col, dropna=False)\n        out = pd.DataFrame({\n            \"month\": g.size().index.astype(str),\n            \"hours_total\": g[self.hours_col].sum().values,\n            \"hours_billable\": g.apply(lambda x: float(x.loc[x[self.billable_flag_col] == 1, self.hours_col].sum())).values,\n            \"hours_internal\": g.apply(lambda x: float(x.loc[x[self.internal_flag_col] == 1, self.hours_col].sum())).values,\n            \"labor_cost\": g[\"labor_cost\"].sum().values,\n        })\n        out[\"billable_utilization\"] = out[\"hours_billable\"] / out[\"hours_total\"].replace(0, np.nan)\n        return DataFrame(out.sort_values(\"month\"))\n\n    def build_internal_breakdown(self) -> DataFrame:\n        df = self._prep()\n        if df.empty:\n            return DataFrame(pd.DataFrame())\n        internal = df[df[self.internal_flag_col] == 1].copy()\n        if internal.empty:\n            return DataFrame(pd.DataFrame(columns=[\"department\", \"hours_total\", \"labor_cost\"]))\n        g = internal.groupby(self.department_col, dropna=False)\n        out = pd.DataFrame({\n            \"department\": g.size().index.astype(str),\n            \"hours_total\": g[self.hours_col].sum().values,\n            \"labor_cost\": g[\"labor_cost\"].sum().values,\n        })\n        return DataFrame(out.sort_values(\"hours_total\", ascending=False))\n\n    def build_weekend_vs_weekday(self) -> DataFrame:\n        df = self._prep()\n        if df.empty or self.weekend_flag_col not in df.columns:\n            return DataFrame(pd.DataFrame(columns=[\"is_weekend\", \"hours_total\", \"labor_cost\"]))\n        g = df.groupby(self.weekend_flag_col, dropna=False)\n        out = pd.DataFrame({\n            \"is_weekend\": g.size().index.astype(int),\n            \"hours_total\": g[self.hours_col].sum().values,\n            \"labor_cost\": g[\"labor_cost\"].sum().values,\n        })\n        return DataFrame(out.sort_values(\"is_weekend\"))\n\n    def build_llm_context(self) -> Message:\n        kpi = self._unwrap_to_df(self.build_kpi_overview())\n        by_member = self._unwrap_to_df(self.build_hours_by_member())\n        by_company = self._unwrap_to_df(self.build_hours_by_company())\n        by_month = self._unwrap_to_df(self.build_hours_by_month())\n\n        def fmt_money(x) -> str:\n            return \"n/a\" if pd.isna(x) else f\"${float(x):,.2f}\"\n\n        def fmt_pct(x) -> str:\n            return \"n/a\" if pd.isna(x) else f\"{float(x)*100:.1f}%\"\n\n        lines: List[str] = []\n        lines.append(\"TIME ENTRY ANALYSIS CONTEXT (Generation IX)\")\n        if not kpi.empty:\n            r = kpi.iloc[0].to_dict()\n            lines.append(f\"- hourly_cost_fully_loaded: {fmt_money(r.get('hourly_cost_fully_loaded'))}\")\n            lines.append(f\"- total_hours: {r.get('total_hours')}\")\n            lines.append(f\"- billable_utilization: {fmt_pct(r.get('billable_utilization'))}\")\n            lines.append(f\"- internal_share: {fmt_pct(r.get('internal_share'))}\")\n            lines.append(f\"- labor_cost_total: {fmt_money(r.get('labor_cost_total'))}\")\n\n        lines.append(\"\")\n        lines.append(\"TOP TECHS (hours)\")\n        for _, row in by_member.head(10).iterrows():\n            lines.append(\n                f\"- {row.get('member')}: hours={float(row.get('hours_total')):.2f}, \"\n                f\"billable_util={fmt_pct(row.get('billable_utilization'))}, cost={fmt_money(row.get('labor_cost'))}\"\n            )\n\n        lines.append(\"\")\n        lines.append(\"TOP CLIENTS (hours)\")\n        for _, row in by_company.head(10).iterrows():\n            lines.append(f\"- {row.get('company')}: hours={float(row.get('hours_total')):.2f}, cost={fmt_money(row.get('labor_cost'))}\")\n\n        if not by_month.empty:\n            lines.append(\"\")\n            lines.append(\"MONTHLY TREND (hours, utilization)\")\n            for _, row in by_month.tail(6).iterrows():\n                lines.append(\n                    f\"- {row.get('month')}: hours={float(row.get('hours_total')):.2f}, \"\n                    f\"billable_util={fmt_pct(row.get('billable_utilization'))}, cost={fmt_money(row.get('labor_cost'))}\"\n                )\n\n        return Message(text=\"\\n\".join(lines))"
              },
              "company_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Company column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "company_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "company"
              },
              "department_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Department column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "department_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "department"
              },
              "hours_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Hours column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "hours_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Hours"
              },
              "internal_flag_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Internal flag column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "internal_flag_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "is_internal"
              },
              "member_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Member column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "member_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Member"
              },
              "month_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Month column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "month_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "month"
              },
              "time_df": {
                "_input_type": "DataFrameInput",
                "advanced": false,
                "display_name": "Time Entries (Clean) DataFrame",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "DataFrame"
                ],
                "list": false,
                "list_add_label": "Add More",
                "name": "time_df",
                "placeholder": "",
                "required": true,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "other",
                "value": ""
              },
              "top_n": {
                "_input_type": "IntInput",
                "advanced": false,
                "display_name": "Top N rows",
                "dynamic": false,
                "info": "",
                "list": false,
                "list_add_label": "Add More",
                "name": "top_n",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "int",
                "value": 15
              },
              "weekend_flag_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Weekend flag column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "weekend_flag_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "is_weekend"
              }
            },
            "tool_mode": false
          },
          "selected_output": "hours_by_member",
          "showNode": true,
          "type": "msp_time_entry_metrics_builder"
        },
        "dragging": false,
        "id": "msp_time_entry_metrics_builder-Rw7a8",
        "measured": {
          "height": 1165,
          "width": 320
        },
        "position": {
          "x": 2426.9981410744404,
          "y": 2554.678485621763
        },
        "selected": false,
        "type": "genericNode"
      },
      {
        "data": {
          "id": "msp_time_entry_metrics_builder-7kGMn",
          "node": {
            "base_classes": [
              "DataFrame",
              "Message"
            ],
            "beta": false,
            "conditional_paths": [],
            "custom_fields": {},
            "description": "Build utilization, internal vs client mix, and fully-loaded labor cost rollups for time entries.",
            "display_name": "MSP Time Entry Metrics Builder",
            "documentation": "",
            "edited": true,
            "field_order": [
              "time_df",
              "hours_col",
              "month_col",
              "member_col",
              "company_col",
              "department_col",
              "billable_flag_col",
              "internal_flag_col",
              "weekend_flag_col",
              "annual_salary",
              "annual_work_hours",
              "benefits_multiplier_x100",
              "top_n"
            ],
            "frozen": false,
            "icon": "BarChart3",
            "legacy": false,
            "lf_version": "1.6.0",
            "metadata": {},
            "minimized": false,
            "output_types": [],
            "outputs": [
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "KPI Overview",
                "group_outputs": false,
                "hidden": null,
                "method": "build_kpi_overview",
                "name": "kpi_overview",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Hours by Member",
                "group_outputs": false,
                "hidden": null,
                "method": "build_hours_by_member",
                "name": "hours_by_member",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Hours by Company",
                "group_outputs": false,
                "hidden": null,
                "method": "build_hours_by_company",
                "name": "hours_by_company",
                "options": null,
                "required_inputs": null,
                "selected": "DataFrame",
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Hours by Month",
                "group_outputs": false,
                "hidden": null,
                "method": "build_hours_by_month",
                "name": "hours_by_month",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Internal Breakdown",
                "group_outputs": false,
                "hidden": null,
                "method": "build_internal_breakdown",
                "name": "internal_breakdown",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Weekend vs Weekday",
                "group_outputs": false,
                "hidden": null,
                "method": "build_weekend_vs_weekday",
                "name": "weekend_vs_weekday",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "LLM Context",
                "group_outputs": false,
                "hidden": null,
                "method": "build_llm_context",
                "name": "llm_context",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "Message"
                ],
                "value": "__UNDEFINED__"
              }
            ],
            "pinned": false,
            "template": {
              "_type": "Component",
              "annual_salary": {
                "_input_type": "IntInput",
                "advanced": false,
                "display_name": "Annual salary per tech (USD)",
                "dynamic": false,
                "info": "",
                "list": false,
                "list_add_label": "Add More",
                "name": "annual_salary",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "int",
                "value": 55000
              },
              "annual_work_hours": {
                "_input_type": "IntInput",
                "advanced": false,
                "display_name": "Annual work hours",
                "dynamic": false,
                "info": "",
                "list": false,
                "list_add_label": "Add More",
                "name": "annual_work_hours",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "int",
                "value": 2080
              },
              "benefits_multiplier_x100": {
                "_input_type": "IntInput",
                "advanced": false,
                "display_name": "Benefits multiplier x100 (125 = 1.25)",
                "dynamic": false,
                "info": "",
                "list": false,
                "list_add_label": "Add More",
                "name": "benefits_multiplier_x100",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "int",
                "value": 125
              },
              "billable_flag_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Billable flag column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "billable_flag_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "is_billable"
              },
              "code": {
                "advanced": true,
                "dynamic": true,
                "fileTypes": [],
                "file_path": "",
                "info": "",
                "list": false,
                "load_from_db": false,
                "multiline": true,
                "name": "code",
                "password": false,
                "placeholder": "",
                "required": true,
                "show": true,
                "title_case": false,
                "type": "code",
                "value": "from __future__ import annotations\n\nfrom typing import Any, List, Dict\nimport pandas as pd\nimport numpy as np\n\nfrom langflow.custom import Component\nfrom langflow.io import DataFrameInput, IntInput, MessageTextInput, Output\nfrom langflow.schema import DataFrame\nfrom langflow.schema.message import Message\n\n\nclass MSPTimeEntryMetricsBuilder(Component):\n    display_name = \"MSP Time Entry Metrics Builder\"\n    description = \"Build utilization, internal vs client mix, and fully-loaded labor cost rollups for time entries.\"\n    name = \"msp_time_entry_metrics_builder\"\n    icon = \"BarChart3\"\n\n    inputs = [\n        DataFrameInput(name=\"time_df\", display_name=\"Time Entries (Clean) DataFrame\", required=True),\n\n        MessageTextInput(name=\"hours_col\", display_name=\"Hours column\", value=\"Hours\"),\n        MessageTextInput(name=\"month_col\", display_name=\"Month column\", value=\"month\"),\n        MessageTextInput(name=\"member_col\", display_name=\"Member column\", value=\"member\"),\n        MessageTextInput(name=\"company_col\", display_name=\"Company column\", value=\"company\"),\n        MessageTextInput(name=\"department_col\", display_name=\"Department column\", value=\"department\"),\n        MessageTextInput(name=\"billable_flag_col\", display_name=\"Billable flag column\", value=\"is_billable\"),\n        MessageTextInput(name=\"internal_flag_col\", display_name=\"Internal flag column\", value=\"is_internal\"),\n        MessageTextInput(name=\"weekend_flag_col\", display_name=\"Weekend flag column\", value=\"is_weekend\"),\n\n        IntInput(name=\"annual_salary\", display_name=\"Annual salary per tech (USD)\", value=55000),\n        IntInput(name=\"annual_work_hours\", display_name=\"Annual work hours\", value=2080),\n        IntInput(name=\"benefits_multiplier_x100\", display_name=\"Benefits multiplier x100 (125 = 1.25)\", value=125),\n        IntInput(name=\"top_n\", display_name=\"Top N rows\", value=15),\n    ]\n\n    outputs = [\n        Output(name=\"kpi_overview\", display_name=\"KPI Overview\", method=\"build_kpi_overview\"),\n        Output(name=\"hours_by_member\", display_name=\"Hours by Member\", method=\"build_hours_by_member\"),\n        Output(name=\"hours_by_company\", display_name=\"Hours by Company\", method=\"build_hours_by_company\"),\n        Output(name=\"hours_by_month\", display_name=\"Hours by Month\", method=\"build_hours_by_month\"),\n        Output(name=\"internal_breakdown\", display_name=\"Internal Breakdown\", method=\"build_internal_breakdown\"),\n        Output(name=\"weekend_vs_weekday\", display_name=\"Weekend vs Weekday\", method=\"build_weekend_vs_weekday\"),\n        Output(name=\"llm_context\", display_name=\"LLM Context\", method=\"build_llm_context\"),\n    ]\n\n    def _unwrap_to_df(self, obj: Any) -> pd.DataFrame:\n        if obj is None:\n            return pd.DataFrame()\n        if isinstance(obj, pd.DataFrame):\n            return obj.copy()\n        if hasattr(obj, \"data\"):\n            d = getattr(obj, \"data\")\n            if isinstance(d, pd.DataFrame):\n                return d.copy()\n            if isinstance(d, list):\n                return pd.DataFrame(d)\n        return pd.DataFrame(obj)\n\n    def _hourly_cost(self) -> float:\n        mult = float(self.benefits_multiplier_x100) / 100.0\n        return (float(self.annual_salary) * mult) / max(float(self.annual_work_hours), 1.0)\n\n    def _prep(self) -> pd.DataFrame:\n        df = self._unwrap_to_df(self.time_df)\n        if df.empty:\n            return df\n\n        df = df.copy()\n        df[self.hours_col] = pd.to_numeric(df.get(self.hours_col, 0.0), errors=\"coerce\").fillna(0.0)\n        df[self.billable_flag_col] = pd.to_numeric(df.get(self.billable_flag_col, 0), errors=\"coerce\").fillna(0).astype(int)\n        df[self.internal_flag_col] = pd.to_numeric(df.get(self.internal_flag_col, 0), errors=\"coerce\").fillna(0).astype(int)\n\n        df[\"hourly_cost\"] = self._hourly_cost()\n        df[\"labor_cost\"] = df[self.hours_col] * df[\"hourly_cost\"]\n\n        return df\n\n    def build_kpi_overview(self) -> DataFrame:\n        df = self._prep()\n        if df.empty:\n            return DataFrame(pd.DataFrame())\n\n        total_hours = float(df[self.hours_col].sum())\n        billable_hours = float(df.loc[df[self.billable_flag_col] == 1, self.hours_col].sum())\n        internal_hours = float(df.loc[df[self.internal_flag_col] == 1, self.hours_col].sum())\n        total_cost = float(df[\"labor_cost\"].sum())\n\n        out = pd.DataFrame([{\n            \"hourly_cost_fully_loaded\": float(df[\"hourly_cost\"].iloc[0]),\n            \"total_hours\": total_hours,\n            \"billable_hours\": billable_hours,\n            \"billable_utilization\": billable_hours / max(total_hours, 1e-9),\n            \"internal_hours\": internal_hours,\n            \"internal_share\": internal_hours / max(total_hours, 1e-9),\n            \"labor_cost_total\": total_cost,\n        }])\n        return DataFrame(out)\n\n    def build_hours_by_member(self) -> DataFrame:\n        df = self._prep()\n        if df.empty:\n            return DataFrame(pd.DataFrame())\n\n        g = df.groupby(self.member_col, dropna=False)\n        out = pd.DataFrame({\n            \"member\": g.size().index.astype(str),\n            \"entries\": g.size().values,\n            \"hours_total\": g[self.hours_col].sum().values,\n            \"hours_billable\": g.apply(lambda x: float(x.loc[x[self.billable_flag_col] == 1, self.hours_col].sum())).values,\n            \"hours_internal\": g.apply(lambda x: float(x.loc[x[self.internal_flag_col] == 1, self.hours_col].sum())).values,\n            \"labor_cost\": g[\"labor_cost\"].sum().values,\n        })\n        out[\"billable_utilization\"] = out[\"hours_billable\"] / out[\"hours_total\"].replace(0, np.nan)\n        return DataFrame(out.sort_values(\"hours_total\", ascending=False).head(int(self.top_n)))\n\n    def build_hours_by_company(self) -> DataFrame:\n        df = self._prep()\n        if df.empty:\n            return DataFrame(pd.DataFrame())\n        g = df.groupby(self.company_col, dropna=False)\n        out = pd.DataFrame({\n            \"company\": g.size().index.astype(str),\n            \"hours_total\": g[self.hours_col].sum().values,\n            \"labor_cost\": g[\"labor_cost\"].sum().values,\n        })\n        return DataFrame(out.sort_values(\"hours_total\", ascending=False).head(int(self.top_n)))\n\n    def build_hours_by_month(self) -> DataFrame:\n        df = self._prep()\n        if df.empty or self.month_col not in df.columns:\n            return DataFrame(pd.DataFrame(columns=[\"month\", \"hours_total\", \"hours_billable\", \"hours_internal\", \"labor_cost\"]))\n        g = df.groupby(self.month_col, dropna=False)\n        out = pd.DataFrame({\n            \"month\": g.size().index.astype(str),\n            \"hours_total\": g[self.hours_col].sum().values,\n            \"hours_billable\": g.apply(lambda x: float(x.loc[x[self.billable_flag_col] == 1, self.hours_col].sum())).values,\n            \"hours_internal\": g.apply(lambda x: float(x.loc[x[self.internal_flag_col] == 1, self.hours_col].sum())).values,\n            \"labor_cost\": g[\"labor_cost\"].sum().values,\n        })\n        out[\"billable_utilization\"] = out[\"hours_billable\"] / out[\"hours_total\"].replace(0, np.nan)\n        return DataFrame(out.sort_values(\"month\"))\n\n    def build_internal_breakdown(self) -> DataFrame:\n        df = self._prep()\n        if df.empty:\n            return DataFrame(pd.DataFrame())\n        internal = df[df[self.internal_flag_col] == 1].copy()\n        if internal.empty:\n            return DataFrame(pd.DataFrame(columns=[\"department\", \"hours_total\", \"labor_cost\"]))\n        g = internal.groupby(self.department_col, dropna=False)\n        out = pd.DataFrame({\n            \"department\": g.size().index.astype(str),\n            \"hours_total\": g[self.hours_col].sum().values,\n            \"labor_cost\": g[\"labor_cost\"].sum().values,\n        })\n        return DataFrame(out.sort_values(\"hours_total\", ascending=False))\n\n    def build_weekend_vs_weekday(self) -> DataFrame:\n        df = self._prep()\n        if df.empty or self.weekend_flag_col not in df.columns:\n            return DataFrame(pd.DataFrame(columns=[\"is_weekend\", \"hours_total\", \"labor_cost\"]))\n        g = df.groupby(self.weekend_flag_col, dropna=False)\n        out = pd.DataFrame({\n            \"is_weekend\": g.size().index.astype(int),\n            \"hours_total\": g[self.hours_col].sum().values,\n            \"labor_cost\": g[\"labor_cost\"].sum().values,\n        })\n        return DataFrame(out.sort_values(\"is_weekend\"))\n\n    def build_llm_context(self) -> Message:\n        kpi = self._unwrap_to_df(self.build_kpi_overview())\n        by_member = self._unwrap_to_df(self.build_hours_by_member())\n        by_company = self._unwrap_to_df(self.build_hours_by_company())\n        by_month = self._unwrap_to_df(self.build_hours_by_month())\n\n        def fmt_money(x) -> str:\n            return \"n/a\" if pd.isna(x) else f\"${float(x):,.2f}\"\n\n        def fmt_pct(x) -> str:\n            return \"n/a\" if pd.isna(x) else f\"{float(x)*100:.1f}%\"\n\n        lines: List[str] = []\n        lines.append(\"TIME ENTRY ANALYSIS CONTEXT (Generation IX)\")\n        if not kpi.empty:\n            r = kpi.iloc[0].to_dict()\n            lines.append(f\"- hourly_cost_fully_loaded: {fmt_money(r.get('hourly_cost_fully_loaded'))}\")\n            lines.append(f\"- total_hours: {r.get('total_hours')}\")\n            lines.append(f\"- billable_utilization: {fmt_pct(r.get('billable_utilization'))}\")\n            lines.append(f\"- internal_share: {fmt_pct(r.get('internal_share'))}\")\n            lines.append(f\"- labor_cost_total: {fmt_money(r.get('labor_cost_total'))}\")\n\n        lines.append(\"\")\n        lines.append(\"TOP TECHS (hours)\")\n        for _, row in by_member.head(10).iterrows():\n            lines.append(\n                f\"- {row.get('member')}: hours={float(row.get('hours_total')):.2f}, \"\n                f\"billable_util={fmt_pct(row.get('billable_utilization'))}, cost={fmt_money(row.get('labor_cost'))}\"\n            )\n\n        lines.append(\"\")\n        lines.append(\"TOP CLIENTS (hours)\")\n        for _, row in by_company.head(10).iterrows():\n            lines.append(f\"- {row.get('company')}: hours={float(row.get('hours_total')):.2f}, cost={fmt_money(row.get('labor_cost'))}\")\n\n        if not by_month.empty:\n            lines.append(\"\")\n            lines.append(\"MONTHLY TREND (hours, utilization)\")\n            for _, row in by_month.tail(6).iterrows():\n                lines.append(\n                    f\"- {row.get('month')}: hours={float(row.get('hours_total')):.2f}, \"\n                    f\"billable_util={fmt_pct(row.get('billable_utilization'))}, cost={fmt_money(row.get('labor_cost'))}\"\n                )\n\n        return Message(text=\"\\n\".join(lines))"
              },
              "company_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Company column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "company_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "company"
              },
              "department_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Department column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "department_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "department"
              },
              "hours_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Hours column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "hours_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Hours"
              },
              "internal_flag_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Internal flag column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "internal_flag_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "is_internal"
              },
              "member_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Member column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "member_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Member"
              },
              "month_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Month column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "month_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "month"
              },
              "time_df": {
                "_input_type": "DataFrameInput",
                "advanced": false,
                "display_name": "Time Entries (Clean) DataFrame",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "DataFrame"
                ],
                "list": false,
                "list_add_label": "Add More",
                "name": "time_df",
                "placeholder": "",
                "required": true,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "other",
                "value": ""
              },
              "top_n": {
                "_input_type": "IntInput",
                "advanced": false,
                "display_name": "Top N rows",
                "dynamic": false,
                "info": "",
                "list": false,
                "list_add_label": "Add More",
                "name": "top_n",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "int",
                "value": 15
              },
              "weekend_flag_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Weekend flag column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "weekend_flag_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "is_weekend"
              }
            },
            "tool_mode": false
          },
          "selected_output": "hours_by_company",
          "showNode": true,
          "type": "msp_time_entry_metrics_builder"
        },
        "dragging": false,
        "id": "msp_time_entry_metrics_builder-7kGMn",
        "measured": {
          "height": 1165,
          "width": 320
        },
        "position": {
          "x": 2750.584615226713,
          "y": 2443.0844971096412
        },
        "selected": false,
        "type": "genericNode"
      },
      {
        "data": {
          "id": "msp_time_entry_metrics_builder-aKsdI",
          "node": {
            "base_classes": [
              "DataFrame",
              "Message"
            ],
            "beta": false,
            "conditional_paths": [],
            "custom_fields": {},
            "description": "Build utilization, internal vs client mix, and fully-loaded labor cost rollups for time entries.",
            "display_name": "MSP Time Entry Metrics Builder",
            "documentation": "",
            "edited": true,
            "field_order": [
              "time_df",
              "hours_col",
              "month_col",
              "member_col",
              "company_col",
              "department_col",
              "billable_flag_col",
              "internal_flag_col",
              "weekend_flag_col",
              "annual_salary",
              "annual_work_hours",
              "benefits_multiplier_x100",
              "top_n"
            ],
            "frozen": false,
            "icon": "BarChart3",
            "legacy": false,
            "lf_version": "1.6.0",
            "metadata": {},
            "minimized": false,
            "output_types": [],
            "outputs": [
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "KPI Overview",
                "group_outputs": false,
                "hidden": null,
                "method": "build_kpi_overview",
                "name": "kpi_overview",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Hours by Member",
                "group_outputs": false,
                "hidden": null,
                "method": "build_hours_by_member",
                "name": "hours_by_member",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Hours by Company",
                "group_outputs": false,
                "hidden": null,
                "method": "build_hours_by_company",
                "name": "hours_by_company",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Hours by Month",
                "group_outputs": false,
                "hidden": null,
                "method": "build_hours_by_month",
                "name": "hours_by_month",
                "options": null,
                "required_inputs": null,
                "selected": "DataFrame",
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Internal Breakdown",
                "group_outputs": false,
                "hidden": null,
                "method": "build_internal_breakdown",
                "name": "internal_breakdown",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Weekend vs Weekday",
                "group_outputs": false,
                "hidden": null,
                "method": "build_weekend_vs_weekday",
                "name": "weekend_vs_weekday",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "LLM Context",
                "group_outputs": false,
                "hidden": null,
                "method": "build_llm_context",
                "name": "llm_context",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "Message"
                ],
                "value": "__UNDEFINED__"
              }
            ],
            "pinned": false,
            "template": {
              "_type": "Component",
              "annual_salary": {
                "_input_type": "IntInput",
                "advanced": false,
                "display_name": "Annual salary per tech (USD)",
                "dynamic": false,
                "info": "",
                "list": false,
                "list_add_label": "Add More",
                "name": "annual_salary",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "int",
                "value": 55000
              },
              "annual_work_hours": {
                "_input_type": "IntInput",
                "advanced": false,
                "display_name": "Annual work hours",
                "dynamic": false,
                "info": "",
                "list": false,
                "list_add_label": "Add More",
                "name": "annual_work_hours",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "int",
                "value": 2080
              },
              "benefits_multiplier_x100": {
                "_input_type": "IntInput",
                "advanced": false,
                "display_name": "Benefits multiplier x100 (125 = 1.25)",
                "dynamic": false,
                "info": "",
                "list": false,
                "list_add_label": "Add More",
                "name": "benefits_multiplier_x100",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "int",
                "value": 125
              },
              "billable_flag_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Billable flag column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "billable_flag_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "is_billable"
              },
              "code": {
                "advanced": true,
                "dynamic": true,
                "fileTypes": [],
                "file_path": "",
                "info": "",
                "list": false,
                "load_from_db": false,
                "multiline": true,
                "name": "code",
                "password": false,
                "placeholder": "",
                "required": true,
                "show": true,
                "title_case": false,
                "type": "code",
                "value": "from __future__ import annotations\n\nfrom typing import Any, List, Dict\nimport pandas as pd\nimport numpy as np\n\nfrom langflow.custom import Component\nfrom langflow.io import DataFrameInput, IntInput, MessageTextInput, Output\nfrom langflow.schema import DataFrame\nfrom langflow.schema.message import Message\n\n\nclass MSPTimeEntryMetricsBuilder(Component):\n    display_name = \"MSP Time Entry Metrics Builder\"\n    description = \"Build utilization, internal vs client mix, and fully-loaded labor cost rollups for time entries.\"\n    name = \"msp_time_entry_metrics_builder\"\n    icon = \"BarChart3\"\n\n    inputs = [\n        DataFrameInput(name=\"time_df\", display_name=\"Time Entries (Clean) DataFrame\", required=True),\n\n        MessageTextInput(name=\"hours_col\", display_name=\"Hours column\", value=\"Hours\"),\n        MessageTextInput(name=\"month_col\", display_name=\"Month column\", value=\"month\"),\n        MessageTextInput(name=\"member_col\", display_name=\"Member column\", value=\"member\"),\n        MessageTextInput(name=\"company_col\", display_name=\"Company column\", value=\"company\"),\n        MessageTextInput(name=\"department_col\", display_name=\"Department column\", value=\"department\"),\n        MessageTextInput(name=\"billable_flag_col\", display_name=\"Billable flag column\", value=\"is_billable\"),\n        MessageTextInput(name=\"internal_flag_col\", display_name=\"Internal flag column\", value=\"is_internal\"),\n        MessageTextInput(name=\"weekend_flag_col\", display_name=\"Weekend flag column\", value=\"is_weekend\"),\n\n        IntInput(name=\"annual_salary\", display_name=\"Annual salary per tech (USD)\", value=55000),\n        IntInput(name=\"annual_work_hours\", display_name=\"Annual work hours\", value=2080),\n        IntInput(name=\"benefits_multiplier_x100\", display_name=\"Benefits multiplier x100 (125 = 1.25)\", value=125),\n        IntInput(name=\"top_n\", display_name=\"Top N rows\", value=15),\n    ]\n\n    outputs = [\n        Output(name=\"kpi_overview\", display_name=\"KPI Overview\", method=\"build_kpi_overview\"),\n        Output(name=\"hours_by_member\", display_name=\"Hours by Member\", method=\"build_hours_by_member\"),\n        Output(name=\"hours_by_company\", display_name=\"Hours by Company\", method=\"build_hours_by_company\"),\n        Output(name=\"hours_by_month\", display_name=\"Hours by Month\", method=\"build_hours_by_month\"),\n        Output(name=\"internal_breakdown\", display_name=\"Internal Breakdown\", method=\"build_internal_breakdown\"),\n        Output(name=\"weekend_vs_weekday\", display_name=\"Weekend vs Weekday\", method=\"build_weekend_vs_weekday\"),\n        Output(name=\"llm_context\", display_name=\"LLM Context\", method=\"build_llm_context\"),\n    ]\n\n    def _unwrap_to_df(self, obj: Any) -> pd.DataFrame:\n        if obj is None:\n            return pd.DataFrame()\n        if isinstance(obj, pd.DataFrame):\n            return obj.copy()\n        if hasattr(obj, \"data\"):\n            d = getattr(obj, \"data\")\n            if isinstance(d, pd.DataFrame):\n                return d.copy()\n            if isinstance(d, list):\n                return pd.DataFrame(d)\n        return pd.DataFrame(obj)\n\n    def _hourly_cost(self) -> float:\n        mult = float(self.benefits_multiplier_x100) / 100.0\n        return (float(self.annual_salary) * mult) / max(float(self.annual_work_hours), 1.0)\n\n    def _prep(self) -> pd.DataFrame:\n        df = self._unwrap_to_df(self.time_df)\n        if df.empty:\n            return df\n\n        df = df.copy()\n        df[self.hours_col] = pd.to_numeric(df.get(self.hours_col, 0.0), errors=\"coerce\").fillna(0.0)\n        df[self.billable_flag_col] = pd.to_numeric(df.get(self.billable_flag_col, 0), errors=\"coerce\").fillna(0).astype(int)\n        df[self.internal_flag_col] = pd.to_numeric(df.get(self.internal_flag_col, 0), errors=\"coerce\").fillna(0).astype(int)\n\n        df[\"hourly_cost\"] = self._hourly_cost()\n        df[\"labor_cost\"] = df[self.hours_col] * df[\"hourly_cost\"]\n\n        return df\n\n    def build_kpi_overview(self) -> DataFrame:\n        df = self._prep()\n        if df.empty:\n            return DataFrame(pd.DataFrame())\n\n        total_hours = float(df[self.hours_col].sum())\n        billable_hours = float(df.loc[df[self.billable_flag_col] == 1, self.hours_col].sum())\n        internal_hours = float(df.loc[df[self.internal_flag_col] == 1, self.hours_col].sum())\n        total_cost = float(df[\"labor_cost\"].sum())\n\n        out = pd.DataFrame([{\n            \"hourly_cost_fully_loaded\": float(df[\"hourly_cost\"].iloc[0]),\n            \"total_hours\": total_hours,\n            \"billable_hours\": billable_hours,\n            \"billable_utilization\": billable_hours / max(total_hours, 1e-9),\n            \"internal_hours\": internal_hours,\n            \"internal_share\": internal_hours / max(total_hours, 1e-9),\n            \"labor_cost_total\": total_cost,\n        }])\n        return DataFrame(out)\n\n    def build_hours_by_member(self) -> DataFrame:\n        df = self._prep()\n        if df.empty:\n            return DataFrame(pd.DataFrame())\n\n        g = df.groupby(self.member_col, dropna=False)\n        out = pd.DataFrame({\n            \"member\": g.size().index.astype(str),\n            \"entries\": g.size().values,\n            \"hours_total\": g[self.hours_col].sum().values,\n            \"hours_billable\": g.apply(lambda x: float(x.loc[x[self.billable_flag_col] == 1, self.hours_col].sum())).values,\n            \"hours_internal\": g.apply(lambda x: float(x.loc[x[self.internal_flag_col] == 1, self.hours_col].sum())).values,\n            \"labor_cost\": g[\"labor_cost\"].sum().values,\n        })\n        out[\"billable_utilization\"] = out[\"hours_billable\"] / out[\"hours_total\"].replace(0, np.nan)\n        return DataFrame(out.sort_values(\"hours_total\", ascending=False).head(int(self.top_n)))\n\n    def build_hours_by_company(self) -> DataFrame:\n        df = self._prep()\n        if df.empty:\n            return DataFrame(pd.DataFrame())\n        g = df.groupby(self.company_col, dropna=False)\n        out = pd.DataFrame({\n            \"company\": g.size().index.astype(str),\n            \"hours_total\": g[self.hours_col].sum().values,\n            \"labor_cost\": g[\"labor_cost\"].sum().values,\n        })\n        return DataFrame(out.sort_values(\"hours_total\", ascending=False).head(int(self.top_n)))\n\n    def build_hours_by_month(self) -> DataFrame:\n        df = self._prep()\n        if df.empty or self.month_col not in df.columns:\n            return DataFrame(pd.DataFrame(columns=[\"month\", \"hours_total\", \"hours_billable\", \"hours_internal\", \"labor_cost\"]))\n        g = df.groupby(self.month_col, dropna=False)\n        out = pd.DataFrame({\n            \"month\": g.size().index.astype(str),\n            \"hours_total\": g[self.hours_col].sum().values,\n            \"hours_billable\": g.apply(lambda x: float(x.loc[x[self.billable_flag_col] == 1, self.hours_col].sum())).values,\n            \"hours_internal\": g.apply(lambda x: float(x.loc[x[self.internal_flag_col] == 1, self.hours_col].sum())).values,\n            \"labor_cost\": g[\"labor_cost\"].sum().values,\n        })\n        out[\"billable_utilization\"] = out[\"hours_billable\"] / out[\"hours_total\"].replace(0, np.nan)\n        return DataFrame(out.sort_values(\"month\"))\n\n    def build_internal_breakdown(self) -> DataFrame:\n        df = self._prep()\n        if df.empty:\n            return DataFrame(pd.DataFrame())\n        internal = df[df[self.internal_flag_col] == 1].copy()\n        if internal.empty:\n            return DataFrame(pd.DataFrame(columns=[\"department\", \"hours_total\", \"labor_cost\"]))\n        g = internal.groupby(self.department_col, dropna=False)\n        out = pd.DataFrame({\n            \"department\": g.size().index.astype(str),\n            \"hours_total\": g[self.hours_col].sum().values,\n            \"labor_cost\": g[\"labor_cost\"].sum().values,\n        })\n        return DataFrame(out.sort_values(\"hours_total\", ascending=False))\n\n    def build_weekend_vs_weekday(self) -> DataFrame:\n        df = self._prep()\n        if df.empty or self.weekend_flag_col not in df.columns:\n            return DataFrame(pd.DataFrame(columns=[\"is_weekend\", \"hours_total\", \"labor_cost\"]))\n        g = df.groupby(self.weekend_flag_col, dropna=False)\n        out = pd.DataFrame({\n            \"is_weekend\": g.size().index.astype(int),\n            \"hours_total\": g[self.hours_col].sum().values,\n            \"labor_cost\": g[\"labor_cost\"].sum().values,\n        })\n        return DataFrame(out.sort_values(\"is_weekend\"))\n\n    def build_llm_context(self) -> Message:\n        kpi = self._unwrap_to_df(self.build_kpi_overview())\n        by_member = self._unwrap_to_df(self.build_hours_by_member())\n        by_company = self._unwrap_to_df(self.build_hours_by_company())\n        by_month = self._unwrap_to_df(self.build_hours_by_month())\n\n        def fmt_money(x) -> str:\n            return \"n/a\" if pd.isna(x) else f\"${float(x):,.2f}\"\n\n        def fmt_pct(x) -> str:\n            return \"n/a\" if pd.isna(x) else f\"{float(x)*100:.1f}%\"\n\n        lines: List[str] = []\n        lines.append(\"TIME ENTRY ANALYSIS CONTEXT (Generation IX)\")\n        if not kpi.empty:\n            r = kpi.iloc[0].to_dict()\n            lines.append(f\"- hourly_cost_fully_loaded: {fmt_money(r.get('hourly_cost_fully_loaded'))}\")\n            lines.append(f\"- total_hours: {r.get('total_hours')}\")\n            lines.append(f\"- billable_utilization: {fmt_pct(r.get('billable_utilization'))}\")\n            lines.append(f\"- internal_share: {fmt_pct(r.get('internal_share'))}\")\n            lines.append(f\"- labor_cost_total: {fmt_money(r.get('labor_cost_total'))}\")\n\n        lines.append(\"\")\n        lines.append(\"TOP TECHS (hours)\")\n        for _, row in by_member.head(10).iterrows():\n            lines.append(\n                f\"- {row.get('member')}: hours={float(row.get('hours_total')):.2f}, \"\n                f\"billable_util={fmt_pct(row.get('billable_utilization'))}, cost={fmt_money(row.get('labor_cost'))}\"\n            )\n\n        lines.append(\"\")\n        lines.append(\"TOP CLIENTS (hours)\")\n        for _, row in by_company.head(10).iterrows():\n            lines.append(f\"- {row.get('company')}: hours={float(row.get('hours_total')):.2f}, cost={fmt_money(row.get('labor_cost'))}\")\n\n        if not by_month.empty:\n            lines.append(\"\")\n            lines.append(\"MONTHLY TREND (hours, utilization)\")\n            for _, row in by_month.tail(6).iterrows():\n                lines.append(\n                    f\"- {row.get('month')}: hours={float(row.get('hours_total')):.2f}, \"\n                    f\"billable_util={fmt_pct(row.get('billable_utilization'))}, cost={fmt_money(row.get('labor_cost'))}\"\n                )\n\n        return Message(text=\"\\n\".join(lines))"
              },
              "company_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Company column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "company_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "company"
              },
              "department_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Department column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "department_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "department"
              },
              "hours_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Hours column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "hours_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Hours"
              },
              "internal_flag_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Internal flag column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "internal_flag_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "is_internal"
              },
              "member_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Member column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "member_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Member"
              },
              "month_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Month column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "month_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "month"
              },
              "time_df": {
                "_input_type": "DataFrameInput",
                "advanced": false,
                "display_name": "Time Entries (Clean) DataFrame",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "DataFrame"
                ],
                "list": false,
                "list_add_label": "Add More",
                "name": "time_df",
                "placeholder": "",
                "required": true,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "other",
                "value": ""
              },
              "top_n": {
                "_input_type": "IntInput",
                "advanced": false,
                "display_name": "Top N rows",
                "dynamic": false,
                "info": "",
                "list": false,
                "list_add_label": "Add More",
                "name": "top_n",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "int",
                "value": 15
              },
              "weekend_flag_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Weekend flag column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "weekend_flag_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "is_weekend"
              }
            },
            "tool_mode": false
          },
          "selected_output": "hours_by_month",
          "showNode": true,
          "type": "msp_time_entry_metrics_builder"
        },
        "dragging": false,
        "id": "msp_time_entry_metrics_builder-aKsdI",
        "measured": {
          "height": 1165,
          "width": 320
        },
        "position": {
          "x": 3062.011690527774,
          "y": 2362.648968153751
        },
        "selected": false,
        "type": "genericNode"
      },
      {
        "data": {
          "id": "msp_time_entry_llm_context_builder-Q2fyh",
          "node": {
            "base_classes": [
              "Message"
            ],
            "beta": false,
            "conditional_paths": [],
            "custom_fields": {},
            "description": "Convert time entry KPI + rollup DataFrames into a compact LLM-friendly context block.",
            "display_name": "MSP Time Entry LLM Context Builder",
            "documentation": "",
            "edited": true,
            "field_order": [
              "kpi_overview",
              "hours_by_member",
              "hours_by_company",
              "hours_by_month",
              "internal_breakdown",
              "weekend_vs_weekday",
              "top_n_members",
              "top_n_companies",
              "recent_months",
              "top_n_internal",
              "include_debug"
            ],
            "frozen": false,
            "icon": "MessageSquareText",
            "legacy": false,
            "lf_version": "1.6.0",
            "metadata": {},
            "minimized": false,
            "output_types": [],
            "outputs": [
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "LLM Context",
                "group_outputs": false,
                "hidden": null,
                "method": "build_llm_context",
                "name": "llm_context",
                "options": null,
                "required_inputs": null,
                "selected": "Message",
                "tool_mode": true,
                "types": [
                  "Message"
                ],
                "value": "__UNDEFINED__"
              }
            ],
            "pinned": false,
            "template": {
              "_type": "Component",
              "code": {
                "advanced": true,
                "dynamic": true,
                "fileTypes": [],
                "file_path": "",
                "info": "",
                "list": false,
                "load_from_db": false,
                "multiline": true,
                "name": "code",
                "password": false,
                "placeholder": "",
                "required": true,
                "show": true,
                "title_case": false,
                "type": "code",
                "value": "from __future__ import annotations\n\nfrom typing import Any, List, Optional\nimport pandas as pd\nimport numpy as np\n\nfrom langflow.custom import Component\nfrom langflow.io import DataFrameInput, IntInput, BoolInput, Output\nfrom langflow.schema.message import Message\n\n\nclass MSPTimeEntryLLMContextBuilder(Component):\n    display_name = \"MSP Time Entry LLM Context Builder\"\n    description = \"Convert time entry KPI + rollup DataFrames into a compact LLM-friendly context block.\"\n    name = \"msp_time_entry_llm_context_builder\"\n    icon = \"MessageSquareText\"\n\n    inputs = [\n        DataFrameInput(name=\"kpi_overview\", display_name=\"KPI Overview\", required=False),\n        DataFrameInput(name=\"hours_by_member\", display_name=\"Hours by Member\", required=False),\n        DataFrameInput(name=\"hours_by_company\", display_name=\"Hours by Company\", required=False),\n        DataFrameInput(name=\"hours_by_month\", display_name=\"Hours by Month\", required=False),\n        DataFrameInput(name=\"internal_breakdown\", display_name=\"Internal Breakdown\", required=False),\n        DataFrameInput(name=\"weekend_vs_weekday\", display_name=\"Weekend vs Weekday\", required=False),\n\n        IntInput(name=\"top_n_members\", display_name=\"Top N members\", value=10),\n        IntInput(name=\"top_n_companies\", display_name=\"Top N companies\", value=10),\n        IntInput(name=\"recent_months\", display_name=\"Recent months\", value=6),\n        IntInput(name=\"top_n_internal\", display_name=\"Top N internal depts\", value=8),\n        BoolInput(name=\"include_debug\", display_name=\"Include debug info\", value=False),\n    ]\n\n    outputs = [\n        Output(name=\"llm_context\", display_name=\"LLM Context\", method=\"build_llm_context\"),\n    ]\n\n    # -------------------------\n    # Helpers\n    # -------------------------\n\n    def _unwrap_to_df(self, obj: Any) -> pd.DataFrame:\n        if obj is None:\n            return pd.DataFrame()\n        if isinstance(obj, pd.DataFrame):\n            return obj.copy()\n        if hasattr(obj, \"data\"):\n            d = getattr(obj, \"data\")\n            if isinstance(d, pd.DataFrame):\n                return d.copy()\n            if isinstance(d, list):\n                return pd.DataFrame(d)\n        return pd.DataFrame(obj)\n\n    def _pick_col(self, df: pd.DataFrame, candidates: List[str]) -> str:\n        for c in candidates:\n            if c in df.columns:\n                return c\n        return \"\"\n\n    def _fmt_money(self, x) -> str:\n        try:\n            if pd.isna(x):\n                return \"n/a\"\n            return f\"${float(x):,.2f}\"\n        except Exception:\n            return \"n/a\"\n\n    def _fmt_pct(self, x) -> str:\n        try:\n            if pd.isna(x):\n                return \"n/a\"\n            return f\"{float(x) * 100:.1f}%\"\n        except Exception:\n            return \"n/a\"\n\n    def _fmt_hours(self, x) -> str:\n        try:\n            if pd.isna(x):\n                return \"n/a\"\n            return f\"{float(x):,.2f}\"\n        except Exception:\n            return \"n/a\"\n\n    def _tail_sorted(self, df: pd.DataFrame, sort_col: str, n: int) -> pd.DataFrame:\n        if df.empty or not sort_col or sort_col not in df.columns:\n            return df.head(0)\n        tmp = df.copy()\n        tmp[sort_col] = tmp[sort_col].astype(str)\n        tmp = tmp.sort_values(sort_col)\n        return tmp.tail(n)\n\n    # -------------------------\n    # Output\n    # -------------------------\n\n    def build_llm_context(self) -> Message:\n        kpi = self._unwrap_to_df(self.kpi_overview)\n        by_member = self._unwrap_to_df(self.hours_by_member)\n        by_company = self._unwrap_to_df(self.hours_by_company)\n        by_month = self._unwrap_to_df(self.hours_by_month)\n        internal = self._unwrap_to_df(self.internal_breakdown)\n        weekend = self._unwrap_to_df(self.weekend_vs_weekday)\n\n        lines: List[str] = []\n        lines.append(\"TIME ENTRY CONTEXT (LLM)\")\n        lines.append(\"Scope: time entries, utilization, internal vs client work, and fully-loaded labor cost (if provided).\")\n\n        # KPI Overview\n        if not kpi.empty:\n            r = kpi.iloc[0].to_dict()\n            lines.append(\"\")\n            lines.append(\"KPI OVERVIEW\")\n            # Support multiple naming schemes\n            total_hours = r.get(\"total_hours\", r.get(\"hours_total\"))\n            billable_hours = r.get(\"billable_hours\", r.get(\"hours_billable\"))\n            billable_util = r.get(\"billable_utilization\", r.get(\"utilization\"))\n            internal_hours = r.get(\"internal_hours\", r.get(\"hours_internal\"))\n            internal_share = r.get(\"internal_share\")\n            hourly_cost = r.get(\"hourly_cost_fully_loaded\", r.get(\"hourly_cost\"))\n            labor_cost_total = r.get(\"labor_cost_total\", r.get(\"total_cost\"))\n\n            if hourly_cost is not None:\n                lines.append(f\"- hourly_cost_fully_loaded: {self._fmt_money(hourly_cost)}\")\n            if total_hours is not None:\n                lines.append(f\"- total_hours: {self._fmt_hours(total_hours)}\")\n            if billable_hours is not None:\n                lines.append(f\"- billable_hours: {self._fmt_hours(billable_hours)}\")\n            if billable_util is not None:\n                lines.append(f\"- billable_utilization: {self._fmt_pct(billable_util)}\")\n            if internal_hours is not None:\n                lines.append(f\"- internal_hours: {self._fmt_hours(internal_hours)}\")\n            if internal_share is not None:\n                lines.append(f\"- internal_share: {self._fmt_pct(internal_share)}\")\n            if labor_cost_total is not None:\n                lines.append(f\"- labor_cost_total: {self._fmt_money(labor_cost_total)}\")\n\n        # Weekend vs weekday\n        if not weekend.empty:\n            isw_col = self._pick_col(weekend, [\"is_weekend\", \"weekend\", \"Is Weekend\"])\n            h_col = self._pick_col(weekend, [\"hours_total\", \"total_hours\", \"hours\"])\n            c_col = self._pick_col(weekend, [\"labor_cost\", \"cost\", \"labor_cost_total\"])\n            if isw_col and h_col:\n                lines.append(\"\")\n                lines.append(\"WEEKEND VS WEEKDAY\")\n                tmp = weekend.copy()\n                tmp[isw_col] = pd.to_numeric(tmp[isw_col], errors=\"coerce\").fillna(0).astype(int)\n                tmp = tmp.sort_values(isw_col)\n                for _, row in tmp.iterrows():\n                    label = \"Weekend\" if int(row.get(isw_col, 0)) == 1 else \"Weekday\"\n                    bits = [f\"hours={self._fmt_hours(row.get(h_col))}\"]\n                    if c_col:\n                        bits.append(f\"cost={self._fmt_money(row.get(c_col))}\")\n                    lines.append(f\"- {label}: \" + \", \".join(bits))\n\n        # Top members\n        if not by_member.empty:\n            name_col = self._pick_col(by_member, [\"member\", \"Member\", \"engineer\"])\n            hours_col = self._pick_col(by_member, [\"hours_total\", \"total_hours\", \"hours\"])\n            util_col = self._pick_col(by_member, [\"billable_utilization\", \"utilization\"])\n            cost_col = self._pick_col(by_member, [\"labor_cost\", \"cost\"])\n\n            if name_col and hours_col:\n                lines.append(\"\")\n                lines.append(\"TOP TECHS (by hours)\")\n                tmp = by_member.copy()\n                tmp[hours_col] = pd.to_numeric(tmp[hours_col], errors=\"coerce\")\n                tmp = tmp.sort_values(hours_col, ascending=False).head(int(self.top_n_members))\n                for _, row in tmp.iterrows():\n                    bits = [f\"hours={self._fmt_hours(row.get(hours_col))}\"]\n                    if util_col:\n                        bits.append(f\"billable_util={self._fmt_pct(row.get(util_col))}\")\n                    if cost_col:\n                        bits.append(f\"cost={self._fmt_money(row.get(cost_col))}\")\n                    lines.append(f\"- {row.get(name_col)}: \" + \", \".join(bits))\n\n        # Top companies\n        if not by_company.empty:\n            co_col = self._pick_col(by_company, [\"company\", \"Company\", \"Company Name\"])\n            hours_col = self._pick_col(by_company, [\"hours_total\", \"total_hours\", \"hours\"])\n            cost_col = self._pick_col(by_company, [\"labor_cost\", \"cost\"])\n\n            if co_col and hours_col:\n                lines.append(\"\")\n                lines.append(\"TOP COMPANIES (by hours)\")\n                tmp = by_company.copy()\n                tmp[hours_col] = pd.to_numeric(tmp[hours_col], errors=\"coerce\")\n                tmp = tmp.sort_values(hours_col, ascending=False).head(int(self.top_n_companies))\n                for _, row in tmp.iterrows():\n                    bits = [f\"hours={self._fmt_hours(row.get(hours_col))}\"]\n                    if cost_col:\n                        bits.append(f\"cost={self._fmt_money(row.get(cost_col))}\")\n                    lines.append(f\"- {row.get(co_col)}: \" + \", \".join(bits))\n\n        # Monthly trend\n        if not by_month.empty:\n            m_col = self._pick_col(by_month, [\"month\", \"Month\"])\n            h_col = self._pick_col(by_month, [\"hours_total\", \"total_hours\", \"hours\"])\n            util_col = self._pick_col(by_month, [\"billable_utilization\", \"utilization\"])\n            cost_col = self._pick_col(by_month, [\"labor_cost\", \"cost\"])\n\n            if m_col and h_col:\n                lines.append(\"\")\n                lines.append(\"RECENT MONTHS (trend)\")\n                tmp = by_month.copy()\n                # Sort month lexicographically works for YYYY-MM\n                tmp[m_col] = tmp[m_col].astype(str)\n                tmp = tmp.sort_values(m_col).tail(int(self.recent_months))\n                for _, row in tmp.iterrows():\n                    bits = [f\"hours={self._fmt_hours(row.get(h_col))}\"]\n                    if util_col:\n                        bits.append(f\"billable_util={self._fmt_pct(row.get(util_col))}\")\n                    if cost_col:\n                        bits.append(f\"cost={self._fmt_money(row.get(cost_col))}\")\n                    lines.append(f\"- {row.get(m_col)}: \" + \", \".join(bits))\n\n        # Internal breakdown\n        if not internal.empty:\n            d_col = self._pick_col(internal, [\"department\", \"Department\"])\n            h_col = self._pick_col(internal, [\"hours_total\", \"total_hours\", \"hours\"])\n            c_col = self._pick_col(internal, [\"labor_cost\", \"cost\"])\n\n            if d_col and h_col:\n                lines.append(\"\")\n                lines.append(\"INTERNAL WORK (breakdown)\")\n                tmp = internal.copy()\n                tmp[h_col] = pd.to_numeric(tmp[h_col], errors=\"coerce\")\n                tmp = tmp.sort_values(h_col, ascending=False).head(int(self.top_n_internal))\n                for _, row in tmp.iterrows():\n                    bits = [f\"hours={self._fmt_hours(row.get(h_col))}\"]\n                    if c_col:\n                        bits.append(f\"cost={self._fmt_money(row.get(c_col))}\")\n                    lines.append(f\"- {row.get(d_col)}: \" + \", \".join(bits))\n\n        if bool(self.include_debug):\n            lines.append(\"\")\n            lines.append(\"DEBUG\")\n            lines.append(f\"- kpi_rows={len(kpi)} cols={list(kpi.columns)}\")\n            lines.append(f\"- by_member_rows={len(by_member)} cols={list(by_member.columns)}\")\n            lines.append(f\"- by_company_rows={len(by_company)} cols={list(by_company.columns)}\")\n            lines.append(f\"- by_month_rows={len(by_month)} cols={list(by_month.columns)}\")\n            lines.append(f\"- internal_rows={len(internal)} cols={list(internal.columns)}\")\n            lines.append(f\"- weekend_rows={len(weekend)} cols={list(weekend.columns)}\")\n\n        return Message(text=\"\\n\".join(lines))\n    "
              },
              "hours_by_company": {
                "_input_type": "DataFrameInput",
                "advanced": false,
                "display_name": "Hours by Company",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "DataFrame"
                ],
                "list": false,
                "list_add_label": "Add More",
                "name": "hours_by_company",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "other",
                "value": ""
              },
              "hours_by_member": {
                "_input_type": "DataFrameInput",
                "advanced": false,
                "display_name": "Hours by Member",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "DataFrame"
                ],
                "list": false,
                "list_add_label": "Add More",
                "name": "hours_by_member",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "other",
                "value": ""
              },
              "hours_by_month": {
                "_input_type": "DataFrameInput",
                "advanced": false,
                "display_name": "Hours by Month",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "DataFrame"
                ],
                "list": false,
                "list_add_label": "Add More",
                "name": "hours_by_month",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "other",
                "value": ""
              },
              "include_debug": {
                "_input_type": "BoolInput",
                "advanced": false,
                "display_name": "Include debug info",
                "dynamic": false,
                "info": "",
                "list": false,
                "list_add_label": "Add More",
                "name": "include_debug",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "bool",
                "value": false
              },
              "internal_breakdown": {
                "_input_type": "DataFrameInput",
                "advanced": false,
                "display_name": "Internal Breakdown",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "DataFrame"
                ],
                "list": false,
                "list_add_label": "Add More",
                "name": "internal_breakdown",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "other",
                "value": ""
              },
              "kpi_overview": {
                "_input_type": "DataFrameInput",
                "advanced": false,
                "display_name": "KPI Overview",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "DataFrame"
                ],
                "list": false,
                "list_add_label": "Add More",
                "name": "kpi_overview",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "other",
                "value": ""
              },
              "recent_months": {
                "_input_type": "IntInput",
                "advanced": false,
                "display_name": "Recent months",
                "dynamic": false,
                "info": "",
                "list": false,
                "list_add_label": "Add More",
                "name": "recent_months",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "int",
                "value": 6
              },
              "top_n_companies": {
                "_input_type": "IntInput",
                "advanced": false,
                "display_name": "Top N companies",
                "dynamic": false,
                "info": "",
                "list": false,
                "list_add_label": "Add More",
                "name": "top_n_companies",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "int",
                "value": 10
              },
              "top_n_internal": {
                "_input_type": "IntInput",
                "advanced": false,
                "display_name": "Top N internal depts",
                "dynamic": false,
                "info": "",
                "list": false,
                "list_add_label": "Add More",
                "name": "top_n_internal",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "int",
                "value": 8
              },
              "top_n_members": {
                "_input_type": "IntInput",
                "advanced": false,
                "display_name": "Top N members",
                "dynamic": false,
                "info": "",
                "list": false,
                "list_add_label": "Add More",
                "name": "top_n_members",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "int",
                "value": 10
              },
              "weekend_vs_weekday": {
                "_input_type": "DataFrameInput",
                "advanced": false,
                "display_name": "Weekend vs Weekday",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "DataFrame"
                ],
                "list": false,
                "list_add_label": "Add More",
                "name": "weekend_vs_weekday",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "other",
                "value": ""
              }
            },
            "tool_mode": false
          },
          "showNode": true,
          "type": "msp_time_entry_llm_context_builder"
        },
        "dragging": false,
        "id": "msp_time_entry_llm_context_builder-Q2fyh",
        "measured": {
          "height": 771,
          "width": 320
        },
        "position": {
          "x": 4082.3144687833765,
          "y": 3579.6157261725193
        },
        "selected": false,
        "type": "genericNode"
      },
      {
        "data": {
          "id": "msp_time_entry_metrics_builder-NKU5T",
          "node": {
            "base_classes": [
              "DataFrame",
              "Message"
            ],
            "beta": false,
            "conditional_paths": [],
            "custom_fields": {},
            "description": "Build utilization, internal vs client mix, and fully-loaded labor cost rollups for time entries.",
            "display_name": "MSP Time Entry Metrics Builder",
            "documentation": "",
            "edited": true,
            "field_order": [
              "time_df",
              "hours_col",
              "month_col",
              "member_col",
              "company_col",
              "department_col",
              "billable_flag_col",
              "internal_flag_col",
              "weekend_flag_col",
              "annual_salary",
              "annual_work_hours",
              "benefits_multiplier_x100",
              "top_n"
            ],
            "frozen": false,
            "icon": "BarChart3",
            "legacy": false,
            "lf_version": "1.6.0",
            "metadata": {},
            "minimized": false,
            "output_types": [],
            "outputs": [
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "KPI Overview",
                "group_outputs": false,
                "hidden": null,
                "method": "build_kpi_overview",
                "name": "kpi_overview",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Hours by Member",
                "group_outputs": false,
                "hidden": null,
                "method": "build_hours_by_member",
                "name": "hours_by_member",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Hours by Company",
                "group_outputs": false,
                "hidden": null,
                "method": "build_hours_by_company",
                "name": "hours_by_company",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Hours by Month",
                "group_outputs": false,
                "hidden": null,
                "method": "build_hours_by_month",
                "name": "hours_by_month",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Internal Breakdown",
                "group_outputs": false,
                "hidden": null,
                "method": "build_internal_breakdown",
                "name": "internal_breakdown",
                "options": null,
                "required_inputs": null,
                "selected": "DataFrame",
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Weekend vs Weekday",
                "group_outputs": false,
                "hidden": null,
                "method": "build_weekend_vs_weekday",
                "name": "weekend_vs_weekday",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "LLM Context",
                "group_outputs": false,
                "hidden": null,
                "method": "build_llm_context",
                "name": "llm_context",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "Message"
                ],
                "value": "__UNDEFINED__"
              }
            ],
            "pinned": false,
            "template": {
              "_type": "Component",
              "annual_salary": {
                "_input_type": "IntInput",
                "advanced": false,
                "display_name": "Annual salary per tech (USD)",
                "dynamic": false,
                "info": "",
                "list": false,
                "list_add_label": "Add More",
                "name": "annual_salary",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "int",
                "value": 55000
              },
              "annual_work_hours": {
                "_input_type": "IntInput",
                "advanced": false,
                "display_name": "Annual work hours",
                "dynamic": false,
                "info": "",
                "list": false,
                "list_add_label": "Add More",
                "name": "annual_work_hours",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "int",
                "value": 2080
              },
              "benefits_multiplier_x100": {
                "_input_type": "IntInput",
                "advanced": false,
                "display_name": "Benefits multiplier x100 (125 = 1.25)",
                "dynamic": false,
                "info": "",
                "list": false,
                "list_add_label": "Add More",
                "name": "benefits_multiplier_x100",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "int",
                "value": 125
              },
              "billable_flag_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Billable flag column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "billable_flag_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "is_billable"
              },
              "code": {
                "advanced": true,
                "dynamic": true,
                "fileTypes": [],
                "file_path": "",
                "info": "",
                "list": false,
                "load_from_db": false,
                "multiline": true,
                "name": "code",
                "password": false,
                "placeholder": "",
                "required": true,
                "show": true,
                "title_case": false,
                "type": "code",
                "value": "from __future__ import annotations\n\nfrom typing import Any, List, Dict\nimport pandas as pd\nimport numpy as np\n\nfrom langflow.custom import Component\nfrom langflow.io import DataFrameInput, IntInput, MessageTextInput, Output\nfrom langflow.schema import DataFrame\nfrom langflow.schema.message import Message\n\n\nclass MSPTimeEntryMetricsBuilder(Component):\n    display_name = \"MSP Time Entry Metrics Builder\"\n    description = \"Build utilization, internal vs client mix, and fully-loaded labor cost rollups for time entries.\"\n    name = \"msp_time_entry_metrics_builder\"\n    icon = \"BarChart3\"\n\n    inputs = [\n        DataFrameInput(name=\"time_df\", display_name=\"Time Entries (Clean) DataFrame\", required=True),\n\n        MessageTextInput(name=\"hours_col\", display_name=\"Hours column\", value=\"Hours\"),\n        MessageTextInput(name=\"month_col\", display_name=\"Month column\", value=\"month\"),\n        MessageTextInput(name=\"member_col\", display_name=\"Member column\", value=\"member\"),\n        MessageTextInput(name=\"company_col\", display_name=\"Company column\", value=\"company\"),\n        MessageTextInput(name=\"department_col\", display_name=\"Department column\", value=\"department\"),\n        MessageTextInput(name=\"billable_flag_col\", display_name=\"Billable flag column\", value=\"is_billable\"),\n        MessageTextInput(name=\"internal_flag_col\", display_name=\"Internal flag column\", value=\"is_internal\"),\n        MessageTextInput(name=\"weekend_flag_col\", display_name=\"Weekend flag column\", value=\"is_weekend\"),\n\n        IntInput(name=\"annual_salary\", display_name=\"Annual salary per tech (USD)\", value=55000),\n        IntInput(name=\"annual_work_hours\", display_name=\"Annual work hours\", value=2080),\n        IntInput(name=\"benefits_multiplier_x100\", display_name=\"Benefits multiplier x100 (125 = 1.25)\", value=125),\n        IntInput(name=\"top_n\", display_name=\"Top N rows\", value=15),\n    ]\n\n    outputs = [\n        Output(name=\"kpi_overview\", display_name=\"KPI Overview\", method=\"build_kpi_overview\"),\n        Output(name=\"hours_by_member\", display_name=\"Hours by Member\", method=\"build_hours_by_member\"),\n        Output(name=\"hours_by_company\", display_name=\"Hours by Company\", method=\"build_hours_by_company\"),\n        Output(name=\"hours_by_month\", display_name=\"Hours by Month\", method=\"build_hours_by_month\"),\n        Output(name=\"internal_breakdown\", display_name=\"Internal Breakdown\", method=\"build_internal_breakdown\"),\n        Output(name=\"weekend_vs_weekday\", display_name=\"Weekend vs Weekday\", method=\"build_weekend_vs_weekday\"),\n        Output(name=\"llm_context\", display_name=\"LLM Context\", method=\"build_llm_context\"),\n    ]\n\n    def _unwrap_to_df(self, obj: Any) -> pd.DataFrame:\n        if obj is None:\n            return pd.DataFrame()\n        if isinstance(obj, pd.DataFrame):\n            return obj.copy()\n        if hasattr(obj, \"data\"):\n            d = getattr(obj, \"data\")\n            if isinstance(d, pd.DataFrame):\n                return d.copy()\n            if isinstance(d, list):\n                return pd.DataFrame(d)\n        return pd.DataFrame(obj)\n\n    def _hourly_cost(self) -> float:\n        mult = float(self.benefits_multiplier_x100) / 100.0\n        return (float(self.annual_salary) * mult) / max(float(self.annual_work_hours), 1.0)\n\n    def _prep(self) -> pd.DataFrame:\n        df = self._unwrap_to_df(self.time_df)\n        if df.empty:\n            return df\n\n        df = df.copy()\n        df[self.hours_col] = pd.to_numeric(df.get(self.hours_col, 0.0), errors=\"coerce\").fillna(0.0)\n        df[self.billable_flag_col] = pd.to_numeric(df.get(self.billable_flag_col, 0), errors=\"coerce\").fillna(0).astype(int)\n        df[self.internal_flag_col] = pd.to_numeric(df.get(self.internal_flag_col, 0), errors=\"coerce\").fillna(0).astype(int)\n\n        df[\"hourly_cost\"] = self._hourly_cost()\n        df[\"labor_cost\"] = df[self.hours_col] * df[\"hourly_cost\"]\n\n        return df\n\n    def build_kpi_overview(self) -> DataFrame:\n        df = self._prep()\n        if df.empty:\n            return DataFrame(pd.DataFrame())\n\n        total_hours = float(df[self.hours_col].sum())\n        billable_hours = float(df.loc[df[self.billable_flag_col] == 1, self.hours_col].sum())\n        internal_hours = float(df.loc[df[self.internal_flag_col] == 1, self.hours_col].sum())\n        total_cost = float(df[\"labor_cost\"].sum())\n\n        out = pd.DataFrame([{\n            \"hourly_cost_fully_loaded\": float(df[\"hourly_cost\"].iloc[0]),\n            \"total_hours\": total_hours,\n            \"billable_hours\": billable_hours,\n            \"billable_utilization\": billable_hours / max(total_hours, 1e-9),\n            \"internal_hours\": internal_hours,\n            \"internal_share\": internal_hours / max(total_hours, 1e-9),\n            \"labor_cost_total\": total_cost,\n        }])\n        return DataFrame(out)\n\n    def build_hours_by_member(self) -> DataFrame:\n        df = self._prep()\n        if df.empty:\n            return DataFrame(pd.DataFrame())\n\n        g = df.groupby(self.member_col, dropna=False)\n        out = pd.DataFrame({\n            \"member\": g.size().index.astype(str),\n            \"entries\": g.size().values,\n            \"hours_total\": g[self.hours_col].sum().values,\n            \"hours_billable\": g.apply(lambda x: float(x.loc[x[self.billable_flag_col] == 1, self.hours_col].sum())).values,\n            \"hours_internal\": g.apply(lambda x: float(x.loc[x[self.internal_flag_col] == 1, self.hours_col].sum())).values,\n            \"labor_cost\": g[\"labor_cost\"].sum().values,\n        })\n        out[\"billable_utilization\"] = out[\"hours_billable\"] / out[\"hours_total\"].replace(0, np.nan)\n        return DataFrame(out.sort_values(\"hours_total\", ascending=False).head(int(self.top_n)))\n\n    def build_hours_by_company(self) -> DataFrame:\n        df = self._prep()\n        if df.empty:\n            return DataFrame(pd.DataFrame())\n        g = df.groupby(self.company_col, dropna=False)\n        out = pd.DataFrame({\n            \"company\": g.size().index.astype(str),\n            \"hours_total\": g[self.hours_col].sum().values,\n            \"labor_cost\": g[\"labor_cost\"].sum().values,\n        })\n        return DataFrame(out.sort_values(\"hours_total\", ascending=False).head(int(self.top_n)))\n\n    def build_hours_by_month(self) -> DataFrame:\n        df = self._prep()\n        if df.empty or self.month_col not in df.columns:\n            return DataFrame(pd.DataFrame(columns=[\"month\", \"hours_total\", \"hours_billable\", \"hours_internal\", \"labor_cost\"]))\n        g = df.groupby(self.month_col, dropna=False)\n        out = pd.DataFrame({\n            \"month\": g.size().index.astype(str),\n            \"hours_total\": g[self.hours_col].sum().values,\n            \"hours_billable\": g.apply(lambda x: float(x.loc[x[self.billable_flag_col] == 1, self.hours_col].sum())).values,\n            \"hours_internal\": g.apply(lambda x: float(x.loc[x[self.internal_flag_col] == 1, self.hours_col].sum())).values,\n            \"labor_cost\": g[\"labor_cost\"].sum().values,\n        })\n        out[\"billable_utilization\"] = out[\"hours_billable\"] / out[\"hours_total\"].replace(0, np.nan)\n        return DataFrame(out.sort_values(\"month\"))\n\n    def build_internal_breakdown(self) -> DataFrame:\n        df = self._prep()\n        if df.empty:\n            return DataFrame(pd.DataFrame())\n        internal = df[df[self.internal_flag_col] == 1].copy()\n        if internal.empty:\n            return DataFrame(pd.DataFrame(columns=[\"department\", \"hours_total\", \"labor_cost\"]))\n        g = internal.groupby(self.department_col, dropna=False)\n        out = pd.DataFrame({\n            \"department\": g.size().index.astype(str),\n            \"hours_total\": g[self.hours_col].sum().values,\n            \"labor_cost\": g[\"labor_cost\"].sum().values,\n        })\n        return DataFrame(out.sort_values(\"hours_total\", ascending=False))\n\n    def build_weekend_vs_weekday(self) -> DataFrame:\n        df = self._prep()\n        if df.empty or self.weekend_flag_col not in df.columns:\n            return DataFrame(pd.DataFrame(columns=[\"is_weekend\", \"hours_total\", \"labor_cost\"]))\n        g = df.groupby(self.weekend_flag_col, dropna=False)\n        out = pd.DataFrame({\n            \"is_weekend\": g.size().index.astype(int),\n            \"hours_total\": g[self.hours_col].sum().values,\n            \"labor_cost\": g[\"labor_cost\"].sum().values,\n        })\n        return DataFrame(out.sort_values(\"is_weekend\"))\n\n    def build_llm_context(self) -> Message:\n        kpi = self._unwrap_to_df(self.build_kpi_overview())\n        by_member = self._unwrap_to_df(self.build_hours_by_member())\n        by_company = self._unwrap_to_df(self.build_hours_by_company())\n        by_month = self._unwrap_to_df(self.build_hours_by_month())\n\n        def fmt_money(x) -> str:\n            return \"n/a\" if pd.isna(x) else f\"${float(x):,.2f}\"\n\n        def fmt_pct(x) -> str:\n            return \"n/a\" if pd.isna(x) else f\"{float(x)*100:.1f}%\"\n\n        lines: List[str] = []\n        lines.append(\"TIME ENTRY ANALYSIS CONTEXT (Generation IX)\")\n        if not kpi.empty:\n            r = kpi.iloc[0].to_dict()\n            lines.append(f\"- hourly_cost_fully_loaded: {fmt_money(r.get('hourly_cost_fully_loaded'))}\")\n            lines.append(f\"- total_hours: {r.get('total_hours')}\")\n            lines.append(f\"- billable_utilization: {fmt_pct(r.get('billable_utilization'))}\")\n            lines.append(f\"- internal_share: {fmt_pct(r.get('internal_share'))}\")\n            lines.append(f\"- labor_cost_total: {fmt_money(r.get('labor_cost_total'))}\")\n\n        lines.append(\"\")\n        lines.append(\"TOP TECHS (hours)\")\n        for _, row in by_member.head(10).iterrows():\n            lines.append(\n                f\"- {row.get('member')}: hours={float(row.get('hours_total')):.2f}, \"\n                f\"billable_util={fmt_pct(row.get('billable_utilization'))}, cost={fmt_money(row.get('labor_cost'))}\"\n            )\n\n        lines.append(\"\")\n        lines.append(\"TOP CLIENTS (hours)\")\n        for _, row in by_company.head(10).iterrows():\n            lines.append(f\"- {row.get('company')}: hours={float(row.get('hours_total')):.2f}, cost={fmt_money(row.get('labor_cost'))}\")\n\n        if not by_month.empty:\n            lines.append(\"\")\n            lines.append(\"MONTHLY TREND (hours, utilization)\")\n            for _, row in by_month.tail(6).iterrows():\n                lines.append(\n                    f\"- {row.get('month')}: hours={float(row.get('hours_total')):.2f}, \"\n                    f\"billable_util={fmt_pct(row.get('billable_utilization'))}, cost={fmt_money(row.get('labor_cost'))}\"\n                )\n\n        return Message(text=\"\\n\".join(lines))"
              },
              "company_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Company column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "company_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "company"
              },
              "department_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Department column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "department_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "department"
              },
              "hours_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Hours column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "hours_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Hours"
              },
              "internal_flag_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Internal flag column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "internal_flag_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "is_internal"
              },
              "member_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Member column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "member_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Member"
              },
              "month_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Month column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "month_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "month"
              },
              "time_df": {
                "_input_type": "DataFrameInput",
                "advanced": false,
                "display_name": "Time Entries (Clean) DataFrame",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "DataFrame"
                ],
                "list": false,
                "list_add_label": "Add More",
                "name": "time_df",
                "placeholder": "",
                "required": true,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "other",
                "value": ""
              },
              "top_n": {
                "_input_type": "IntInput",
                "advanced": false,
                "display_name": "Top N rows",
                "dynamic": false,
                "info": "",
                "list": false,
                "list_add_label": "Add More",
                "name": "top_n",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "int",
                "value": 15
              },
              "weekend_flag_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Weekend flag column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "weekend_flag_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "is_weekend"
              }
            },
            "tool_mode": false
          },
          "selected_output": "internal_breakdown",
          "showNode": true,
          "type": "msp_time_entry_metrics_builder"
        },
        "dragging": false,
        "id": "msp_time_entry_metrics_builder-NKU5T",
        "measured": {
          "height": 1165,
          "width": 320
        },
        "position": {
          "x": 3377.842538487858,
          "y": 2277.5065172278664
        },
        "selected": false,
        "type": "genericNode"
      },
      {
        "data": {
          "id": "msp_time_entry_metrics_builder-1NFh1",
          "node": {
            "base_classes": [
              "DataFrame",
              "Message"
            ],
            "beta": false,
            "conditional_paths": [],
            "custom_fields": {},
            "description": "Build utilization, internal vs client mix, and fully-loaded labor cost rollups for time entries.",
            "display_name": "MSP Time Entry Metrics Builder",
            "documentation": "",
            "edited": true,
            "field_order": [
              "time_df",
              "hours_col",
              "month_col",
              "member_col",
              "company_col",
              "department_col",
              "billable_flag_col",
              "internal_flag_col",
              "weekend_flag_col",
              "annual_salary",
              "annual_work_hours",
              "benefits_multiplier_x100",
              "top_n"
            ],
            "frozen": false,
            "icon": "BarChart3",
            "legacy": false,
            "lf_version": "1.6.0",
            "metadata": {},
            "minimized": false,
            "output_types": [],
            "outputs": [
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "KPI Overview",
                "group_outputs": false,
                "hidden": null,
                "method": "build_kpi_overview",
                "name": "kpi_overview",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Hours by Member",
                "group_outputs": false,
                "hidden": null,
                "method": "build_hours_by_member",
                "name": "hours_by_member",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Hours by Company",
                "group_outputs": false,
                "hidden": null,
                "method": "build_hours_by_company",
                "name": "hours_by_company",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Hours by Month",
                "group_outputs": false,
                "hidden": null,
                "method": "build_hours_by_month",
                "name": "hours_by_month",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Internal Breakdown",
                "group_outputs": false,
                "hidden": null,
                "method": "build_internal_breakdown",
                "name": "internal_breakdown",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Weekend vs Weekday",
                "group_outputs": false,
                "hidden": null,
                "method": "build_weekend_vs_weekday",
                "name": "weekend_vs_weekday",
                "options": null,
                "required_inputs": null,
                "selected": "DataFrame",
                "tool_mode": true,
                "types": [
                  "DataFrame"
                ],
                "value": "__UNDEFINED__"
              },
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "LLM Context",
                "group_outputs": false,
                "hidden": null,
                "method": "build_llm_context",
                "name": "llm_context",
                "options": null,
                "required_inputs": null,
                "tool_mode": true,
                "types": [
                  "Message"
                ],
                "value": "__UNDEFINED__"
              }
            ],
            "pinned": false,
            "template": {
              "_type": "Component",
              "annual_salary": {
                "_input_type": "IntInput",
                "advanced": false,
                "display_name": "Annual salary per tech (USD)",
                "dynamic": false,
                "info": "",
                "list": false,
                "list_add_label": "Add More",
                "name": "annual_salary",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "int",
                "value": 55000
              },
              "annual_work_hours": {
                "_input_type": "IntInput",
                "advanced": false,
                "display_name": "Annual work hours",
                "dynamic": false,
                "info": "",
                "list": false,
                "list_add_label": "Add More",
                "name": "annual_work_hours",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "int",
                "value": 2080
              },
              "benefits_multiplier_x100": {
                "_input_type": "IntInput",
                "advanced": false,
                "display_name": "Benefits multiplier x100 (125 = 1.25)",
                "dynamic": false,
                "info": "",
                "list": false,
                "list_add_label": "Add More",
                "name": "benefits_multiplier_x100",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "int",
                "value": 125
              },
              "billable_flag_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Billable flag column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "billable_flag_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "is_billable"
              },
              "code": {
                "advanced": true,
                "dynamic": true,
                "fileTypes": [],
                "file_path": "",
                "info": "",
                "list": false,
                "load_from_db": false,
                "multiline": true,
                "name": "code",
                "password": false,
                "placeholder": "",
                "required": true,
                "show": true,
                "title_case": false,
                "type": "code",
                "value": "from __future__ import annotations\n\nfrom typing import Any, List, Dict\nimport pandas as pd\nimport numpy as np\n\nfrom langflow.custom import Component\nfrom langflow.io import DataFrameInput, IntInput, MessageTextInput, Output\nfrom langflow.schema import DataFrame\nfrom langflow.schema.message import Message\n\n\nclass MSPTimeEntryMetricsBuilder(Component):\n    display_name = \"MSP Time Entry Metrics Builder\"\n    description = \"Build utilization, internal vs client mix, and fully-loaded labor cost rollups for time entries.\"\n    name = \"msp_time_entry_metrics_builder\"\n    icon = \"BarChart3\"\n\n    inputs = [\n        DataFrameInput(name=\"time_df\", display_name=\"Time Entries (Clean) DataFrame\", required=True),\n\n        MessageTextInput(name=\"hours_col\", display_name=\"Hours column\", value=\"Hours\"),\n        MessageTextInput(name=\"month_col\", display_name=\"Month column\", value=\"month\"),\n        MessageTextInput(name=\"member_col\", display_name=\"Member column\", value=\"member\"),\n        MessageTextInput(name=\"company_col\", display_name=\"Company column\", value=\"company\"),\n        MessageTextInput(name=\"department_col\", display_name=\"Department column\", value=\"department\"),\n        MessageTextInput(name=\"billable_flag_col\", display_name=\"Billable flag column\", value=\"is_billable\"),\n        MessageTextInput(name=\"internal_flag_col\", display_name=\"Internal flag column\", value=\"is_internal\"),\n        MessageTextInput(name=\"weekend_flag_col\", display_name=\"Weekend flag column\", value=\"is_weekend\"),\n\n        IntInput(name=\"annual_salary\", display_name=\"Annual salary per tech (USD)\", value=55000),\n        IntInput(name=\"annual_work_hours\", display_name=\"Annual work hours\", value=2080),\n        IntInput(name=\"benefits_multiplier_x100\", display_name=\"Benefits multiplier x100 (125 = 1.25)\", value=125),\n        IntInput(name=\"top_n\", display_name=\"Top N rows\", value=15),\n    ]\n\n    outputs = [\n        Output(name=\"kpi_overview\", display_name=\"KPI Overview\", method=\"build_kpi_overview\"),\n        Output(name=\"hours_by_member\", display_name=\"Hours by Member\", method=\"build_hours_by_member\"),\n        Output(name=\"hours_by_company\", display_name=\"Hours by Company\", method=\"build_hours_by_company\"),\n        Output(name=\"hours_by_month\", display_name=\"Hours by Month\", method=\"build_hours_by_month\"),\n        Output(name=\"internal_breakdown\", display_name=\"Internal Breakdown\", method=\"build_internal_breakdown\"),\n        Output(name=\"weekend_vs_weekday\", display_name=\"Weekend vs Weekday\", method=\"build_weekend_vs_weekday\"),\n        Output(name=\"llm_context\", display_name=\"LLM Context\", method=\"build_llm_context\"),\n    ]\n\n    def _unwrap_to_df(self, obj: Any) -> pd.DataFrame:\n        if obj is None:\n            return pd.DataFrame()\n        if isinstance(obj, pd.DataFrame):\n            return obj.copy()\n        if hasattr(obj, \"data\"):\n            d = getattr(obj, \"data\")\n            if isinstance(d, pd.DataFrame):\n                return d.copy()\n            if isinstance(d, list):\n                return pd.DataFrame(d)\n        return pd.DataFrame(obj)\n\n    def _hourly_cost(self) -> float:\n        mult = float(self.benefits_multiplier_x100) / 100.0\n        return (float(self.annual_salary) * mult) / max(float(self.annual_work_hours), 1.0)\n\n    def _prep(self) -> pd.DataFrame:\n        df = self._unwrap_to_df(self.time_df)\n        if df.empty:\n            return df\n\n        df = df.copy()\n        df[self.hours_col] = pd.to_numeric(df.get(self.hours_col, 0.0), errors=\"coerce\").fillna(0.0)\n        df[self.billable_flag_col] = pd.to_numeric(df.get(self.billable_flag_col, 0), errors=\"coerce\").fillna(0).astype(int)\n        df[self.internal_flag_col] = pd.to_numeric(df.get(self.internal_flag_col, 0), errors=\"coerce\").fillna(0).astype(int)\n\n        df[\"hourly_cost\"] = self._hourly_cost()\n        df[\"labor_cost\"] = df[self.hours_col] * df[\"hourly_cost\"]\n\n        return df\n\n    def build_kpi_overview(self) -> DataFrame:\n        df = self._prep()\n        if df.empty:\n            return DataFrame(pd.DataFrame())\n\n        total_hours = float(df[self.hours_col].sum())\n        billable_hours = float(df.loc[df[self.billable_flag_col] == 1, self.hours_col].sum())\n        internal_hours = float(df.loc[df[self.internal_flag_col] == 1, self.hours_col].sum())\n        total_cost = float(df[\"labor_cost\"].sum())\n\n        out = pd.DataFrame([{\n            \"hourly_cost_fully_loaded\": float(df[\"hourly_cost\"].iloc[0]),\n            \"total_hours\": total_hours,\n            \"billable_hours\": billable_hours,\n            \"billable_utilization\": billable_hours / max(total_hours, 1e-9),\n            \"internal_hours\": internal_hours,\n            \"internal_share\": internal_hours / max(total_hours, 1e-9),\n            \"labor_cost_total\": total_cost,\n        }])\n        return DataFrame(out)\n\n    def build_hours_by_member(self) -> DataFrame:\n        df = self._prep()\n        if df.empty:\n            return DataFrame(pd.DataFrame())\n\n        g = df.groupby(self.member_col, dropna=False)\n        out = pd.DataFrame({\n            \"member\": g.size().index.astype(str),\n            \"entries\": g.size().values,\n            \"hours_total\": g[self.hours_col].sum().values,\n            \"hours_billable\": g.apply(lambda x: float(x.loc[x[self.billable_flag_col] == 1, self.hours_col].sum())).values,\n            \"hours_internal\": g.apply(lambda x: float(x.loc[x[self.internal_flag_col] == 1, self.hours_col].sum())).values,\n            \"labor_cost\": g[\"labor_cost\"].sum().values,\n        })\n        out[\"billable_utilization\"] = out[\"hours_billable\"] / out[\"hours_total\"].replace(0, np.nan)\n        return DataFrame(out.sort_values(\"hours_total\", ascending=False).head(int(self.top_n)))\n\n    def build_hours_by_company(self) -> DataFrame:\n        df = self._prep()\n        if df.empty:\n            return DataFrame(pd.DataFrame())\n        g = df.groupby(self.company_col, dropna=False)\n        out = pd.DataFrame({\n            \"company\": g.size().index.astype(str),\n            \"hours_total\": g[self.hours_col].sum().values,\n            \"labor_cost\": g[\"labor_cost\"].sum().values,\n        })\n        return DataFrame(out.sort_values(\"hours_total\", ascending=False).head(int(self.top_n)))\n\n    def build_hours_by_month(self) -> DataFrame:\n        df = self._prep()\n        if df.empty or self.month_col not in df.columns:\n            return DataFrame(pd.DataFrame(columns=[\"month\", \"hours_total\", \"hours_billable\", \"hours_internal\", \"labor_cost\"]))\n        g = df.groupby(self.month_col, dropna=False)\n        out = pd.DataFrame({\n            \"month\": g.size().index.astype(str),\n            \"hours_total\": g[self.hours_col].sum().values,\n            \"hours_billable\": g.apply(lambda x: float(x.loc[x[self.billable_flag_col] == 1, self.hours_col].sum())).values,\n            \"hours_internal\": g.apply(lambda x: float(x.loc[x[self.internal_flag_col] == 1, self.hours_col].sum())).values,\n            \"labor_cost\": g[\"labor_cost\"].sum().values,\n        })\n        out[\"billable_utilization\"] = out[\"hours_billable\"] / out[\"hours_total\"].replace(0, np.nan)\n        return DataFrame(out.sort_values(\"month\"))\n\n    def build_internal_breakdown(self) -> DataFrame:\n        df = self._prep()\n        if df.empty:\n            return DataFrame(pd.DataFrame())\n        internal = df[df[self.internal_flag_col] == 1].copy()\n        if internal.empty:\n            return DataFrame(pd.DataFrame(columns=[\"department\", \"hours_total\", \"labor_cost\"]))\n        g = internal.groupby(self.department_col, dropna=False)\n        out = pd.DataFrame({\n            \"department\": g.size().index.astype(str),\n            \"hours_total\": g[self.hours_col].sum().values,\n            \"labor_cost\": g[\"labor_cost\"].sum().values,\n        })\n        return DataFrame(out.sort_values(\"hours_total\", ascending=False))\n\n    def build_weekend_vs_weekday(self) -> DataFrame:\n        df = self._prep()\n        if df.empty or self.weekend_flag_col not in df.columns:\n            return DataFrame(pd.DataFrame(columns=[\"is_weekend\", \"hours_total\", \"labor_cost\"]))\n        g = df.groupby(self.weekend_flag_col, dropna=False)\n        out = pd.DataFrame({\n            \"is_weekend\": g.size().index.astype(int),\n            \"hours_total\": g[self.hours_col].sum().values,\n            \"labor_cost\": g[\"labor_cost\"].sum().values,\n        })\n        return DataFrame(out.sort_values(\"is_weekend\"))\n\n    def build_llm_context(self) -> Message:\n        kpi = self._unwrap_to_df(self.build_kpi_overview())\n        by_member = self._unwrap_to_df(self.build_hours_by_member())\n        by_company = self._unwrap_to_df(self.build_hours_by_company())\n        by_month = self._unwrap_to_df(self.build_hours_by_month())\n\n        def fmt_money(x) -> str:\n            return \"n/a\" if pd.isna(x) else f\"${float(x):,.2f}\"\n\n        def fmt_pct(x) -> str:\n            return \"n/a\" if pd.isna(x) else f\"{float(x)*100:.1f}%\"\n\n        lines: List[str] = []\n        lines.append(\"TIME ENTRY ANALYSIS CONTEXT (Generation IX)\")\n        if not kpi.empty:\n            r = kpi.iloc[0].to_dict()\n            lines.append(f\"- hourly_cost_fully_loaded: {fmt_money(r.get('hourly_cost_fully_loaded'))}\")\n            lines.append(f\"- total_hours: {r.get('total_hours')}\")\n            lines.append(f\"- billable_utilization: {fmt_pct(r.get('billable_utilization'))}\")\n            lines.append(f\"- internal_share: {fmt_pct(r.get('internal_share'))}\")\n            lines.append(f\"- labor_cost_total: {fmt_money(r.get('labor_cost_total'))}\")\n\n        lines.append(\"\")\n        lines.append(\"TOP TECHS (hours)\")\n        for _, row in by_member.head(10).iterrows():\n            lines.append(\n                f\"- {row.get('member')}: hours={float(row.get('hours_total')):.2f}, \"\n                f\"billable_util={fmt_pct(row.get('billable_utilization'))}, cost={fmt_money(row.get('labor_cost'))}\"\n            )\n\n        lines.append(\"\")\n        lines.append(\"TOP CLIENTS (hours)\")\n        for _, row in by_company.head(10).iterrows():\n            lines.append(f\"- {row.get('company')}: hours={float(row.get('hours_total')):.2f}, cost={fmt_money(row.get('labor_cost'))}\")\n\n        if not by_month.empty:\n            lines.append(\"\")\n            lines.append(\"MONTHLY TREND (hours, utilization)\")\n            for _, row in by_month.tail(6).iterrows():\n                lines.append(\n                    f\"- {row.get('month')}: hours={float(row.get('hours_total')):.2f}, \"\n                    f\"billable_util={fmt_pct(row.get('billable_utilization'))}, cost={fmt_money(row.get('labor_cost'))}\"\n                )\n\n        return Message(text=\"\\n\".join(lines))"
              },
              "company_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Company column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "company_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "company"
              },
              "department_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Department column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "department_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "department"
              },
              "hours_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Hours column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "hours_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Hours"
              },
              "internal_flag_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Internal flag column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "internal_flag_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "is_internal"
              },
              "member_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Member column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "member_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Member"
              },
              "month_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Month column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "month_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "month"
              },
              "time_df": {
                "_input_type": "DataFrameInput",
                "advanced": false,
                "display_name": "Time Entries (Clean) DataFrame",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "DataFrame"
                ],
                "list": false,
                "list_add_label": "Add More",
                "name": "time_df",
                "placeholder": "",
                "required": true,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "other",
                "value": ""
              },
              "top_n": {
                "_input_type": "IntInput",
                "advanced": false,
                "display_name": "Top N rows",
                "dynamic": false,
                "info": "",
                "list": false,
                "list_add_label": "Add More",
                "name": "top_n",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "int",
                "value": 15
              },
              "weekend_flag_col": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Weekend flag column",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "weekend_flag_col",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "is_weekend"
              }
            },
            "tool_mode": false
          },
          "selected_output": "weekend_vs_weekday",
          "showNode": true,
          "type": "msp_time_entry_metrics_builder"
        },
        "dragging": false,
        "id": "msp_time_entry_metrics_builder-1NFh1",
        "measured": {
          "height": 1165,
          "width": 320
        },
        "position": {
          "x": 3686.8572108592216,
          "y": 2152.6030420344478
        },
        "selected": false,
        "type": "genericNode"
      },
      {
        "data": {
          "id": "Agent-6DFFZ",
          "node": {
            "base_classes": [
              "Message"
            ],
            "beta": false,
            "conditional_paths": [],
            "custom_fields": {},
            "description": "Define the agent's instructions, then enter a task to complete using tools.",
            "display_name": "Agent",
            "documentation": "https://docs.langflow.org/agents",
            "edited": false,
            "field_order": [
              "agent_llm",
              "max_tokens",
              "model_kwargs",
              "model_name",
              "openai_api_base",
              "api_key",
              "temperature",
              "seed",
              "max_retries",
              "timeout",
              "system_prompt",
              "n_messages",
              "format_instructions",
              "output_schema",
              "tools",
              "input_value",
              "handle_parsing_errors",
              "verbose",
              "max_iterations",
              "agent_description",
              "add_current_date_tool"
            ],
            "frozen": false,
            "icon": "bot",
            "last_updated": "2025-12-23T01:31:42.745Z",
            "legacy": false,
            "lf_version": "1.6.0",
            "metadata": {},
            "minimized": false,
            "output_types": [],
            "outputs": [
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Response",
                "group_outputs": false,
                "method": "message_response",
                "name": "response",
                "options": null,
                "required_inputs": null,
                "selected": "Message",
                "tool_mode": true,
                "types": [
                  "Message"
                ],
                "value": "__UNDEFINED__"
              }
            ],
            "pinned": false,
            "template": {
              "_type": "Component",
              "add_current_date_tool": {
                "_input_type": "BoolInput",
                "advanced": true,
                "display_name": "Current Date",
                "dynamic": false,
                "info": "If true, will add a tool to the agent that returns the current date.",
                "list": false,
                "list_add_label": "Add More",
                "name": "add_current_date_tool",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "bool",
                "value": true
              },
              "agent_description": {
                "_input_type": "MultilineInput",
                "advanced": true,
                "copy_field": false,
                "display_name": "Agent Description [Deprecated]",
                "dynamic": false,
                "info": "The description of the agent. This is only used when in Tool Mode. Defaults to 'A helpful assistant with access to the following tools:' and tools are added dynamically. This feature is deprecated and will be removed in future versions.",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "multiline": true,
                "name": "agent_description",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "A helpful assistant with access to the following tools:"
              },
              "agent_llm": {
                "_input_type": "DropdownInput",
                "advanced": false,
                "combobox": false,
                "dialog_inputs": {},
                "display_name": "Model Provider",
                "dynamic": false,
                "external_options": {
                  "fields": {
                    "data": {
                      "node": {
                        "display_name": "Connect other models",
                        "icon": "CornerDownLeft",
                        "name": "connect_other_models"
                      }
                    }
                  }
                },
                "info": "The provider of the language model that the agent will use to generate responses.",
                "input_types": [],
                "name": "agent_llm",
                "options": [
                  "Anthropic",
                  "Google Generative AI",
                  "OpenAI"
                ],
                "options_metadata": [
                  {
                    "icon": "Anthropic"
                  },
                  {
                    "icon": "GoogleGenerativeAI"
                  },
                  {
                    "icon": "OpenAI"
                  }
                ],
                "placeholder": "",
                "real_time_refresh": true,
                "refresh_button": false,
                "required": false,
                "show": true,
                "title_case": false,
                "toggle": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "str",
                "value": "OpenAI"
              },
              "api_key": {
                "_input_type": "SecretStrInput",
                "advanced": false,
                "display_name": "OpenAI API Key",
                "dynamic": false,
                "info": "The OpenAI API Key to use for the OpenAI model.",
                "input_types": [],
                "load_from_db": false,
                "name": "api_key",
                "password": true,
                "placeholder": "",
                "real_time_refresh": true,
                "required": false,
                "show": true,
                "title_case": false,
                "type": "str",
                "value": ""
              },
              "code": {
                "advanced": true,
                "dynamic": true,
                "fileTypes": [],
                "file_path": "",
                "info": "",
                "list": false,
                "load_from_db": false,
                "multiline": true,
                "name": "code",
                "password": false,
                "placeholder": "",
                "required": true,
                "show": true,
                "title_case": false,
                "type": "code",
                "value": "import json\nimport re\n\nfrom langchain_core.tools import StructuredTool\nfrom pydantic import ValidationError\n\nfrom langflow.base.agents.agent import LCToolsAgentComponent\nfrom langflow.base.agents.events import ExceptionWithMessageError\nfrom langflow.base.models.model_input_constants import (\n    ALL_PROVIDER_FIELDS,\n    MODEL_DYNAMIC_UPDATE_FIELDS,\n    MODEL_PROVIDERS_DICT,\n    MODELS_METADATA,\n)\nfrom langflow.base.models.model_utils import get_model_name\nfrom langflow.components.helpers.current_date import CurrentDateComponent\nfrom langflow.components.helpers.memory import MemoryComponent\nfrom langflow.components.langchain_utilities.tool_calling import (\n    ToolCallingAgentComponent,\n)\nfrom langflow.custom.custom_component.component import _get_component_toolkit\nfrom langflow.custom.utils import update_component_build_config\nfrom langflow.field_typing import Tool\nfrom langflow.helpers.base_model import build_model_from_schema\nfrom langflow.io import (\n    BoolInput,\n    DropdownInput,\n    IntInput,\n    MultilineInput,\n    Output,\n    TableInput,\n)\nfrom langflow.logging import logger\nfrom langflow.schema.data import Data\nfrom langflow.schema.dotdict import dotdict\nfrom langflow.schema.message import Message\nfrom langflow.schema.table import EditMode\n\n\ndef set_advanced_true(component_input):\n    component_input.advanced = True\n    return component_input\n\n\nMODEL_PROVIDERS_LIST = [\"Anthropic\", \"Google Generative AI\", \"OpenAI\"]\n\n\nclass AgentComponent(ToolCallingAgentComponent):\n    display_name: str = \"Agent\"\n    description: str = \"Define the agent's instructions, then enter a task to complete using tools.\"\n    documentation: str = \"https://docs.langflow.org/agents\"\n    icon = \"bot\"\n    beta = False\n    name = \"Agent\"\n\n    memory_inputs = [set_advanced_true(component_input) for component_input in MemoryComponent().inputs]\n\n    # Filter out json_mode from OpenAI inputs since we handle structured output differently\n    openai_inputs_filtered = [\n        input_field\n        for input_field in MODEL_PROVIDERS_DICT[\"OpenAI\"][\"inputs\"]\n        if not (hasattr(input_field, \"name\") and input_field.name == \"json_mode\")\n    ]\n\n    inputs = [\n        DropdownInput(\n            name=\"agent_llm\",\n            display_name=\"Model Provider\",\n            info=\"The provider of the language model that the agent will use to generate responses.\",\n            options=[*MODEL_PROVIDERS_LIST],\n            value=\"OpenAI\",\n            real_time_refresh=True,\n            refresh_button=False,\n            input_types=[],\n            options_metadata=[MODELS_METADATA[key] for key in MODEL_PROVIDERS_LIST],\n            external_options={\n                \"fields\": {\n                    \"data\": {\n                        \"node\": {\n                            \"name\": \"connect_other_models\",\n                            \"display_name\": \"Connect other models\",\n                            \"icon\": \"CornerDownLeft\",\n                        }\n                    }\n                },\n            },\n        ),\n        *openai_inputs_filtered,\n        MultilineInput(\n            name=\"system_prompt\",\n            display_name=\"Agent Instructions\",\n            info=\"System Prompt: Initial instructions and context provided to guide the agent's behavior.\",\n            value=\"You are a helpful assistant that can use tools to answer questions and perform tasks.\",\n            advanced=False,\n        ),\n        IntInput(\n            name=\"n_messages\",\n            display_name=\"Number of Chat History Messages\",\n            value=100,\n            info=\"Number of chat history messages to retrieve.\",\n            advanced=True,\n            show=True,\n        ),\n        MultilineInput(\n            name=\"format_instructions\",\n            display_name=\"Output Format Instructions\",\n            info=\"Generic Template for structured output formatting. Valid only with Structured response.\",\n            value=(\n                \"You are an AI that extracts structured JSON objects from unstructured text. \"\n                \"Use a predefined schema with expected types (str, int, float, bool, dict). \"\n                \"Extract ALL relevant instances that match the schema - if multiple patterns exist, capture them all. \"\n                \"Fill missing or ambiguous values with defaults: null for missing values. \"\n                \"Remove exact duplicates but keep variations that have different field values. \"\n                \"Always return valid JSON in the expected format, never throw errors. \"\n                \"If multiple objects can be extracted, return them all in the structured format.\"\n            ),\n            advanced=True,\n        ),\n        TableInput(\n            name=\"output_schema\",\n            display_name=\"Output Schema\",\n            info=(\n                \"Schema Validation: Define the structure and data types for structured output. \"\n                \"No validation if no output schema.\"\n            ),\n            advanced=True,\n            required=False,\n            value=[],\n            table_schema=[\n                {\n                    \"name\": \"name\",\n                    \"display_name\": \"Name\",\n                    \"type\": \"str\",\n                    \"description\": \"Specify the name of the output field.\",\n                    \"default\": \"field\",\n                    \"edit_mode\": EditMode.INLINE,\n                },\n                {\n                    \"name\": \"description\",\n                    \"display_name\": \"Description\",\n                    \"type\": \"str\",\n                    \"description\": \"Describe the purpose of the output field.\",\n                    \"default\": \"description of field\",\n                    \"edit_mode\": EditMode.POPOVER,\n                },\n                {\n                    \"name\": \"type\",\n                    \"display_name\": \"Type\",\n                    \"type\": \"str\",\n                    \"edit_mode\": EditMode.INLINE,\n                    \"description\": (\"Indicate the data type of the output field (e.g., str, int, float, bool, dict).\"),\n                    \"options\": [\"str\", \"int\", \"float\", \"bool\", \"dict\"],\n                    \"default\": \"str\",\n                },\n                {\n                    \"name\": \"multiple\",\n                    \"display_name\": \"As List\",\n                    \"type\": \"boolean\",\n                    \"description\": \"Set to True if this output field should be a list of the specified type.\",\n                    \"default\": \"False\",\n                    \"edit_mode\": EditMode.INLINE,\n                },\n            ],\n        ),\n        *LCToolsAgentComponent._base_inputs,\n        # removed memory inputs from agent component\n        # *memory_inputs,\n        BoolInput(\n            name=\"add_current_date_tool\",\n            display_name=\"Current Date\",\n            advanced=True,\n            info=\"If true, will add a tool to the agent that returns the current date.\",\n            value=True,\n        ),\n    ]\n    outputs = [\n        Output(name=\"response\", display_name=\"Response\", method=\"message_response\"),\n    ]\n\n    async def get_agent_requirements(self):\n        \"\"\"Get the agent requirements for the agent.\"\"\"\n        llm_model, display_name = await self.get_llm()\n        if llm_model is None:\n            msg = \"No language model selected. Please choose a model to proceed.\"\n            raise ValueError(msg)\n        self.model_name = get_model_name(llm_model, display_name=display_name)\n\n        # Get memory data\n        self.chat_history = await self.get_memory_data()\n        if isinstance(self.chat_history, Message):\n            self.chat_history = [self.chat_history]\n\n        # Add current date tool if enabled\n        if self.add_current_date_tool:\n            if not isinstance(self.tools, list):  # type: ignore[has-type]\n                self.tools = []\n            current_date_tool = (await CurrentDateComponent(**self.get_base_args()).to_toolkit()).pop(0)\n            if not isinstance(current_date_tool, StructuredTool):\n                msg = \"CurrentDateComponent must be converted to a StructuredTool\"\n                raise TypeError(msg)\n            self.tools.append(current_date_tool)\n        return llm_model, self.chat_history, self.tools\n\n    async def message_response(self) -> Message:\n        try:\n            llm_model, self.chat_history, self.tools = await self.get_agent_requirements()\n            # Set up and run agent\n            self.set(\n                llm=llm_model,\n                tools=self.tools or [],\n                chat_history=self.chat_history,\n                input_value=self.input_value,\n                system_prompt=self.system_prompt,\n            )\n            agent = self.create_agent_runnable()\n            result = await self.run_agent(agent)\n\n            # Store result for potential JSON output\n            self._agent_result = result\n\n        except (ValueError, TypeError, KeyError) as e:\n            await logger.aerror(f\"{type(e).__name__}: {e!s}\")\n            raise\n        except ExceptionWithMessageError as e:\n            await logger.aerror(f\"ExceptionWithMessageError occurred: {e}\")\n            raise\n        # Avoid catching blind Exception; let truly unexpected exceptions propagate\n        except Exception as e:\n            await logger.aerror(f\"Unexpected error: {e!s}\")\n            raise\n        else:\n            return result\n\n    def _preprocess_schema(self, schema):\n        \"\"\"Preprocess schema to ensure correct data types for build_model_from_schema.\"\"\"\n        processed_schema = []\n        for field in schema:\n            processed_field = {\n                \"name\": str(field.get(\"name\", \"field\")),\n                \"type\": str(field.get(\"type\", \"str\")),\n                \"description\": str(field.get(\"description\", \"\")),\n                \"multiple\": field.get(\"multiple\", False),\n            }\n            # Ensure multiple is handled correctly\n            if isinstance(processed_field[\"multiple\"], str):\n                processed_field[\"multiple\"] = processed_field[\"multiple\"].lower() in [\n                    \"true\",\n                    \"1\",\n                    \"t\",\n                    \"y\",\n                    \"yes\",\n                ]\n            processed_schema.append(processed_field)\n        return processed_schema\n\n    async def build_structured_output_base(self, content: str):\n        \"\"\"Build structured output with optional BaseModel validation.\"\"\"\n        json_pattern = r\"\\{.*\\}\"\n        schema_error_msg = \"Try setting an output schema\"\n\n        # Try to parse content as JSON first\n        json_data = None\n        try:\n            json_data = json.loads(content)\n        except json.JSONDecodeError:\n            json_match = re.search(json_pattern, content, re.DOTALL)\n            if json_match:\n                try:\n                    json_data = json.loads(json_match.group())\n                except json.JSONDecodeError:\n                    return {\"content\": content, \"error\": schema_error_msg}\n            else:\n                return {\"content\": content, \"error\": schema_error_msg}\n\n        # If no output schema provided, return parsed JSON without validation\n        if not hasattr(self, \"output_schema\") or not self.output_schema or len(self.output_schema) == 0:\n            return json_data\n\n        # Use BaseModel validation with schema\n        try:\n            processed_schema = self._preprocess_schema(self.output_schema)\n            output_model = build_model_from_schema(processed_schema)\n\n            # Validate against the schema\n            if isinstance(json_data, list):\n                # Multiple objects\n                validated_objects = []\n                for item in json_data:\n                    try:\n                        validated_obj = output_model.model_validate(item)\n                        validated_objects.append(validated_obj.model_dump())\n                    except ValidationError as e:\n                        await logger.aerror(f\"Validation error for item: {e}\")\n                        # Include invalid items with error info\n                        validated_objects.append({\"data\": item, \"validation_error\": str(e)})\n                return validated_objects\n\n            # Single object\n            try:\n                validated_obj = output_model.model_validate(json_data)\n                return [validated_obj.model_dump()]  # Return as list for consistency\n            except ValidationError as e:\n                await logger.aerror(f\"Validation error: {e}\")\n                return [{\"data\": json_data, \"validation_error\": str(e)}]\n\n        except (TypeError, ValueError) as e:\n            await logger.aerror(f\"Error building structured output: {e}\")\n            # Fallback to parsed JSON without validation\n            return json_data\n\n    async def json_response(self) -> Data:\n        \"\"\"Convert agent response to structured JSON Data output with schema validation.\"\"\"\n        # Always use structured chat agent for JSON response mode for better JSON formatting\n        try:\n            system_components = []\n\n            # 1. Agent Instructions (system_prompt)\n            agent_instructions = getattr(self, \"system_prompt\", \"\") or \"\"\n            if agent_instructions:\n                system_components.append(f\"{agent_instructions}\")\n\n            # 2. Format Instructions\n            format_instructions = getattr(self, \"format_instructions\", \"\") or \"\"\n            if format_instructions:\n                system_components.append(f\"Format instructions: {format_instructions}\")\n\n            # 3. Schema Information from BaseModel\n            if hasattr(self, \"output_schema\") and self.output_schema and len(self.output_schema) > 0:\n                try:\n                    processed_schema = self._preprocess_schema(self.output_schema)\n                    output_model = build_model_from_schema(processed_schema)\n                    schema_dict = output_model.model_json_schema()\n                    schema_info = (\n                        \"You are given some text that may include format instructions, \"\n                        \"explanations, or other content alongside a JSON schema.\\n\\n\"\n                        \"Your task:\\n\"\n                        \"- Extract only the JSON schema.\\n\"\n                        \"- Return it as valid JSON.\\n\"\n                        \"- Do not include format instructions, explanations, or extra text.\\n\\n\"\n                        \"Input:\\n\"\n                        f\"{json.dumps(schema_dict, indent=2)}\\n\\n\"\n                        \"Output (only JSON schema):\"\n                    )\n                    system_components.append(schema_info)\n                except (ValidationError, ValueError, TypeError, KeyError) as e:\n                    await logger.aerror(f\"Could not build schema for prompt: {e}\", exc_info=True)\n\n            # Combine all components\n            combined_instructions = \"\\n\\n\".join(system_components) if system_components else \"\"\n            llm_model, self.chat_history, self.tools = await self.get_agent_requirements()\n            self.set(\n                llm=llm_model,\n                tools=self.tools or [],\n                chat_history=self.chat_history,\n                input_value=self.input_value,\n                system_prompt=combined_instructions,\n            )\n\n            # Create and run structured chat agent\n            try:\n                structured_agent = self.create_agent_runnable()\n            except (NotImplementedError, ValueError, TypeError) as e:\n                await logger.aerror(f\"Error with structured chat agent: {e}\")\n                raise\n            try:\n                result = await self.run_agent(structured_agent)\n            except (\n                ExceptionWithMessageError,\n                ValueError,\n                TypeError,\n                RuntimeError,\n            ) as e:\n                await logger.aerror(f\"Error with structured agent result: {e}\")\n                raise\n            # Extract content from structured agent result\n            if hasattr(result, \"content\"):\n                content = result.content\n            elif hasattr(result, \"text\"):\n                content = result.text\n            else:\n                content = str(result)\n\n        except (\n            ExceptionWithMessageError,\n            ValueError,\n            TypeError,\n            NotImplementedError,\n            AttributeError,\n        ) as e:\n            await logger.aerror(f\"Error with structured chat agent: {e}\")\n            # Fallback to regular agent\n            content_str = \"No content returned from agent\"\n            return Data(data={\"content\": content_str, \"error\": str(e)})\n\n        # Process with structured output validation\n        try:\n            structured_output = await self.build_structured_output_base(content)\n\n            # Handle different output formats\n            if isinstance(structured_output, list) and structured_output:\n                if len(structured_output) == 1:\n                    return Data(data=structured_output[0])\n                return Data(data={\"results\": structured_output})\n            if isinstance(structured_output, dict):\n                return Data(data=structured_output)\n            return Data(data={\"content\": content})\n\n        except (ValueError, TypeError) as e:\n            await logger.aerror(f\"Error in structured output processing: {e}\")\n            return Data(data={\"content\": content, \"error\": str(e)})\n\n    async def get_memory_data(self):\n        # TODO: This is a temporary fix to avoid message duplication. We should develop a function for this.\n        messages = (\n            await MemoryComponent(**self.get_base_args())\n            .set(\n                session_id=self.graph.session_id,\n                order=\"Ascending\",\n                n_messages=self.n_messages,\n            )\n            .retrieve_messages()\n        )\n        return [\n            message for message in messages if getattr(message, \"id\", None) != getattr(self.input_value, \"id\", None)\n        ]\n\n    async def get_llm(self):\n        if not isinstance(self.agent_llm, str):\n            return self.agent_llm, None\n\n        try:\n            provider_info = MODEL_PROVIDERS_DICT.get(self.agent_llm)\n            if not provider_info:\n                msg = f\"Invalid model provider: {self.agent_llm}\"\n                raise ValueError(msg)\n\n            component_class = provider_info.get(\"component_class\")\n            display_name = component_class.display_name\n            inputs = provider_info.get(\"inputs\")\n            prefix = provider_info.get(\"prefix\", \"\")\n\n            return self._build_llm_model(component_class, inputs, prefix), display_name\n\n        except (AttributeError, ValueError, TypeError, RuntimeError) as e:\n            await logger.aerror(f\"Error building {self.agent_llm} language model: {e!s}\")\n            msg = f\"Failed to initialize language model: {e!s}\"\n            raise ValueError(msg) from e\n\n    def _build_llm_model(self, component, inputs, prefix=\"\"):\n        model_kwargs = {}\n        for input_ in inputs:\n            if hasattr(self, f\"{prefix}{input_.name}\"):\n                model_kwargs[input_.name] = getattr(self, f\"{prefix}{input_.name}\")\n        return component.set(**model_kwargs).build_model()\n\n    def set_component_params(self, component):\n        provider_info = MODEL_PROVIDERS_DICT.get(self.agent_llm)\n        if provider_info:\n            inputs = provider_info.get(\"inputs\")\n            prefix = provider_info.get(\"prefix\")\n            # Filter out json_mode and only use attributes that exist on this component\n            model_kwargs = {}\n            for input_ in inputs:\n                if hasattr(self, f\"{prefix}{input_.name}\"):\n                    model_kwargs[input_.name] = getattr(self, f\"{prefix}{input_.name}\")\n\n            return component.set(**model_kwargs)\n        return component\n\n    def delete_fields(self, build_config: dotdict, fields: dict | list[str]) -> None:\n        \"\"\"Delete specified fields from build_config.\"\"\"\n        for field in fields:\n            build_config.pop(field, None)\n\n    def update_input_types(self, build_config: dotdict) -> dotdict:\n        \"\"\"Update input types for all fields in build_config.\"\"\"\n        for key, value in build_config.items():\n            if isinstance(value, dict):\n                if value.get(\"input_types\") is None:\n                    build_config[key][\"input_types\"] = []\n            elif hasattr(value, \"input_types\") and value.input_types is None:\n                value.input_types = []\n        return build_config\n\n    async def update_build_config(\n        self, build_config: dotdict, field_value: str, field_name: str | None = None\n    ) -> dotdict:\n        # Iterate over all providers in the MODEL_PROVIDERS_DICT\n        # Existing logic for updating build_config\n        if field_name in (\"agent_llm\",):\n            build_config[\"agent_llm\"][\"value\"] = field_value\n            provider_info = MODEL_PROVIDERS_DICT.get(field_value)\n            if provider_info:\n                component_class = provider_info.get(\"component_class\")\n                if component_class and hasattr(component_class, \"update_build_config\"):\n                    # Call the component class's update_build_config method\n                    build_config = await update_component_build_config(\n                        component_class, build_config, field_value, \"model_name\"\n                    )\n\n            provider_configs: dict[str, tuple[dict, list[dict]]] = {\n                provider: (\n                    MODEL_PROVIDERS_DICT[provider][\"fields\"],\n                    [\n                        MODEL_PROVIDERS_DICT[other_provider][\"fields\"]\n                        for other_provider in MODEL_PROVIDERS_DICT\n                        if other_provider != provider\n                    ],\n                )\n                for provider in MODEL_PROVIDERS_DICT\n            }\n            if field_value in provider_configs:\n                fields_to_add, fields_to_delete = provider_configs[field_value]\n\n                # Delete fields from other providers\n                for fields in fields_to_delete:\n                    self.delete_fields(build_config, fields)\n\n                # Add provider-specific fields\n                if field_value == \"OpenAI\" and not any(field in build_config for field in fields_to_add):\n                    build_config.update(fields_to_add)\n                else:\n                    build_config.update(fields_to_add)\n                # Reset input types for agent_llm\n                build_config[\"agent_llm\"][\"input_types\"] = []\n                build_config[\"agent_llm\"][\"display_name\"] = \"Model Provider\"\n            elif field_value == \"connect_other_models\":\n                # Delete all provider fields\n                self.delete_fields(build_config, ALL_PROVIDER_FIELDS)\n                # # Update with custom component\n                custom_component = DropdownInput(\n                    name=\"agent_llm\",\n                    display_name=\"Language Model\",\n                    info=\"The provider of the language model that the agent will use to generate responses.\",\n                    options=[*MODEL_PROVIDERS_LIST],\n                    real_time_refresh=True,\n                    refresh_button=False,\n                    input_types=[\"LanguageModel\"],\n                    placeholder=\"Awaiting model input.\",\n                    options_metadata=[MODELS_METADATA[key] for key in MODEL_PROVIDERS_LIST],\n                    external_options={\n                        \"fields\": {\n                            \"data\": {\n                                \"node\": {\n                                    \"name\": \"connect_other_models\",\n                                    \"display_name\": \"Connect other models\",\n                                    \"icon\": \"CornerDownLeft\",\n                                },\n                            }\n                        },\n                    },\n                )\n                build_config.update({\"agent_llm\": custom_component.to_dict()})\n            # Update input types for all fields\n            build_config = self.update_input_types(build_config)\n\n            # Validate required keys\n            default_keys = [\n                \"code\",\n                \"_type\",\n                \"agent_llm\",\n                \"tools\",\n                \"input_value\",\n                \"add_current_date_tool\",\n                \"system_prompt\",\n                \"agent_description\",\n                \"max_iterations\",\n                \"handle_parsing_errors\",\n                \"verbose\",\n            ]\n            missing_keys = [key for key in default_keys if key not in build_config]\n            if missing_keys:\n                msg = f\"Missing required keys in build_config: {missing_keys}\"\n                raise ValueError(msg)\n        if (\n            isinstance(self.agent_llm, str)\n            and self.agent_llm in MODEL_PROVIDERS_DICT\n            and field_name in MODEL_DYNAMIC_UPDATE_FIELDS\n        ):\n            provider_info = MODEL_PROVIDERS_DICT.get(self.agent_llm)\n            if provider_info:\n                component_class = provider_info.get(\"component_class\")\n                component_class = self.set_component_params(component_class)\n                prefix = provider_info.get(\"prefix\")\n                if component_class and hasattr(component_class, \"update_build_config\"):\n                    # Call each component class's update_build_config method\n                    # remove the prefix from the field_name\n                    if isinstance(field_name, str) and isinstance(prefix, str):\n                        field_name = field_name.replace(prefix, \"\")\n                    build_config = await update_component_build_config(\n                        component_class, build_config, field_value, \"model_name\"\n                    )\n        return dotdict({k: v.to_dict() if hasattr(v, \"to_dict\") else v for k, v in build_config.items()})\n\n    async def _get_tools(self) -> list[Tool]:\n        component_toolkit = _get_component_toolkit()\n        tools_names = self._build_tools_names()\n        agent_description = self.get_tool_description()\n        # TODO: Agent Description Depreciated Feature to be removed\n        description = f\"{agent_description}{tools_names}\"\n        tools = component_toolkit(component=self).get_tools(\n            tool_name=\"Call_Agent\",\n            tool_description=description,\n            callbacks=self.get_langchain_callbacks(),\n        )\n        if hasattr(self, \"tools_metadata\"):\n            tools = component_toolkit(component=self, metadata=self.tools_metadata).update_tools_metadata(tools=tools)\n        return tools\n"
              },
              "format_instructions": {
                "_input_type": "MultilineInput",
                "advanced": true,
                "copy_field": false,
                "display_name": "Output Format Instructions",
                "dynamic": false,
                "info": "Generic Template for structured output formatting. Valid only with Structured response.",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "multiline": true,
                "name": "format_instructions",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "You are an AI that extracts structured JSON objects from unstructured text. Use a predefined schema with expected types (str, int, float, bool, dict). Extract ALL relevant instances that match the schema - if multiple patterns exist, capture them all. Fill missing or ambiguous values with defaults: null for missing values. Remove exact duplicates but keep variations that have different field values. Always return valid JSON in the expected format, never throw errors. If multiple objects can be extracted, return them all in the structured format."
              },
              "handle_parsing_errors": {
                "_input_type": "BoolInput",
                "advanced": true,
                "display_name": "Handle Parse Errors",
                "dynamic": false,
                "info": "Should the Agent fix errors when reading user input for better processing?",
                "list": false,
                "list_add_label": "Add More",
                "name": "handle_parsing_errors",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "bool",
                "value": true
              },
              "input_value": {
                "_input_type": "MessageInput",
                "advanced": false,
                "display_name": "Input",
                "dynamic": false,
                "info": "The input provided by the user for the agent to process.",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "input_value",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": true,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": ""
              },
              "max_iterations": {
                "_input_type": "IntInput",
                "advanced": true,
                "display_name": "Max Iterations",
                "dynamic": false,
                "info": "The maximum number of attempts the agent can make to complete its task before it stops.",
                "list": false,
                "list_add_label": "Add More",
                "name": "max_iterations",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "int",
                "value": 15
              },
              "max_retries": {
                "_input_type": "IntInput",
                "advanced": true,
                "display_name": "Max Retries",
                "dynamic": false,
                "info": "The maximum number of retries to make when generating.",
                "list": false,
                "list_add_label": "Add More",
                "name": "max_retries",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "int",
                "value": 5
              },
              "max_tokens": {
                "_input_type": "IntInput",
                "advanced": true,
                "display_name": "Max Tokens",
                "dynamic": false,
                "info": "The maximum number of tokens to generate. Set to 0 for unlimited tokens.",
                "list": false,
                "list_add_label": "Add More",
                "name": "max_tokens",
                "placeholder": "",
                "range_spec": {
                  "max": 128000,
                  "min": 0,
                  "step": 0.1,
                  "step_type": "float"
                },
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "int",
                "value": ""
              },
              "model_kwargs": {
                "_input_type": "DictInput",
                "advanced": true,
                "display_name": "Model Kwargs",
                "dynamic": false,
                "info": "Additional keyword arguments to pass to the model.",
                "list": false,
                "list_add_label": "Add More",
                "name": "model_kwargs",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "type": "dict",
                "value": {}
              },
              "model_name": {
                "_input_type": "DropdownInput",
                "advanced": false,
                "combobox": true,
                "dialog_inputs": {},
                "display_name": "Model Name",
                "dynamic": false,
                "external_options": {},
                "info": "To see the model names, first choose a provider. Then, enter your API key and click the refresh button next to the model name.",
                "name": "model_name",
                "options": [
                  "gpt-4o-mini",
                  "gpt-4o",
                  "gpt-4.1",
                  "gpt-4.1-mini",
                  "gpt-4.1-nano",
                  "gpt-4-turbo",
                  "gpt-4-turbo-preview",
                  "gpt-4",
                  "gpt-3.5-turbo",
                  "gpt-5",
                  "gpt-5-mini",
                  "gpt-5-nano",
                  "gpt-5-chat-latest",
                  "o1",
                  "o3-mini",
                  "o3",
                  "o3-pro",
                  "o4-mini",
                  "o4-mini-high"
                ],
                "options_metadata": [],
                "placeholder": "",
                "real_time_refresh": false,
                "required": false,
                "show": true,
                "title_case": false,
                "toggle": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "str",
                "value": "gpt-5"
              },
              "n_messages": {
                "_input_type": "IntInput",
                "advanced": true,
                "display_name": "Number of Chat History Messages",
                "dynamic": false,
                "info": "Number of chat history messages to retrieve.",
                "list": false,
                "list_add_label": "Add More",
                "name": "n_messages",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "int",
                "value": 100
              },
              "openai_api_base": {
                "_input_type": "StrInput",
                "advanced": true,
                "display_name": "OpenAI API Base",
                "dynamic": false,
                "info": "The base URL of the OpenAI API. Defaults to https://api.openai.com/v1. You can change this to use other APIs like JinaChat, LocalAI and Prem.",
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "openai_api_base",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "str",
                "value": ""
              },
              "output_schema": {
                "_input_type": "TableInput",
                "advanced": true,
                "display_name": "Output Schema",
                "dynamic": false,
                "info": "Schema Validation: Define the structure and data types for structured output. No validation if no output schema.",
                "is_list": true,
                "list_add_label": "Add More",
                "name": "output_schema",
                "placeholder": "",
                "required": false,
                "show": true,
                "table_icon": "Table",
                "table_schema": {
                  "columns": [
                    {
                      "default": "field",
                      "description": "Specify the name of the output field.",
                      "disable_edit": false,
                      "display_name": "Name",
                      "edit_mode": "inline",
                      "filterable": true,
                      "formatter": "text",
                      "hidden": false,
                      "name": "name",
                      "sortable": true,
                      "type": "str"
                    },
                    {
                      "default": "description of field",
                      "description": "Describe the purpose of the output field.",
                      "disable_edit": false,
                      "display_name": "Description",
                      "edit_mode": "popover",
                      "filterable": true,
                      "formatter": "text",
                      "hidden": false,
                      "name": "description",
                      "sortable": true,
                      "type": "str"
                    },
                    {
                      "default": "str",
                      "description": "Indicate the data type of the output field (e.g., str, int, float, bool, dict).",
                      "disable_edit": false,
                      "display_name": "Type",
                      "edit_mode": "inline",
                      "filterable": true,
                      "formatter": "text",
                      "hidden": false,
                      "name": "type",
                      "options": [
                        "str",
                        "int",
                        "float",
                        "bool",
                        "dict"
                      ],
                      "sortable": true,
                      "type": "str"
                    },
                    {
                      "default": false,
                      "description": "Set to True if this output field should be a list of the specified type.",
                      "disable_edit": false,
                      "display_name": "As List",
                      "edit_mode": "inline",
                      "filterable": true,
                      "formatter": "boolean",
                      "hidden": false,
                      "name": "multiple",
                      "sortable": true,
                      "type": "boolean"
                    }
                  ]
                },
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "trigger_icon": "Table",
                "trigger_text": "Open table",
                "type": "table",
                "value": []
              },
              "seed": {
                "_input_type": "IntInput",
                "advanced": true,
                "display_name": "Seed",
                "dynamic": false,
                "info": "The seed controls the reproducibility of the job.",
                "list": false,
                "list_add_label": "Add More",
                "name": "seed",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "int",
                "value": 1
              },
              "system_prompt": {
                "_input_type": "MultilineInput",
                "advanced": false,
                "copy_field": false,
                "display_name": "Agent Instructions",
                "dynamic": false,
                "info": "System Prompt: Initial instructions and context provided to guide the agent's behavior.",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "multiline": true,
                "name": "system_prompt",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Analyze the time entry rollup context below and produce:\n1) Executive summary (2 sentences)\n2) Key metrics and what they imply (utilization, internal share, cost)\n3) Workload balance: who is over/under loaded (top techs by hours, utilization)\n4) Where time is going: top clients and internal departments\n5) Trend notes from recent months (if present)\n6) 5 recommended actions (specific operational changes)\n7) 5 follow-up data asks to improve accuracy\n\nContext:\n{time}"
              },
              "temperature": {
                "_input_type": "SliderInput",
                "advanced": true,
                "display_name": "Temperature",
                "dynamic": false,
                "info": "",
                "max_label": "",
                "max_label_icon": "",
                "min_label": "",
                "min_label_icon": "",
                "name": "temperature",
                "placeholder": "",
                "range_spec": {
                  "max": 1,
                  "min": 0,
                  "step": 0.01,
                  "step_type": "float"
                },
                "required": false,
                "show": true,
                "slider_buttons": false,
                "slider_buttons_options": [],
                "slider_input": false,
                "title_case": false,
                "tool_mode": false,
                "type": "slider",
                "value": 0.1
              },
              "timeout": {
                "_input_type": "IntInput",
                "advanced": true,
                "display_name": "Timeout",
                "dynamic": false,
                "info": "The timeout for requests to OpenAI completion API.",
                "list": false,
                "list_add_label": "Add More",
                "name": "timeout",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "int",
                "value": 700
              },
              "tools": {
                "_input_type": "HandleInput",
                "advanced": false,
                "display_name": "Tools",
                "dynamic": false,
                "info": "These are the tools that the agent can use to help with tasks.",
                "input_types": [
                  "Tool"
                ],
                "list": true,
                "list_add_label": "Add More",
                "name": "tools",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "trace_as_metadata": true,
                "type": "other",
                "value": ""
              },
              "verbose": {
                "_input_type": "BoolInput",
                "advanced": true,
                "display_name": "Verbose",
                "dynamic": false,
                "info": "",
                "list": false,
                "list_add_label": "Add More",
                "name": "verbose",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "bool",
                "value": true
              }
            },
            "tool_mode": false
          },
          "showNode": true,
          "type": "Agent"
        },
        "dragging": false,
        "id": "Agent-6DFFZ",
        "measured": {
          "height": 591,
          "width": 320
        },
        "position": {
          "x": 4902.946154538767,
          "y": 1753.3190213761363
        },
        "selected": false,
        "type": "genericNode"
      },
      {
        "data": {
          "id": "combine_three_llm_outputs-78VjU",
          "node": {
            "base_classes": [
              "Message"
            ],
            "beta": false,
            "conditional_paths": [],
            "custom_fields": {},
            "description": "Combine three LLM Message outputs into one Message, with optional headers and separators.",
            "display_name": "Combine 3 LLM Outputs",
            "documentation": "",
            "edited": true,
            "field_order": [
              "msg_a",
              "msg_b",
              "msg_c",
              "include_headers",
              "header_a",
              "header_b",
              "header_c",
              "skip_empty",
              "dedupe_exact_blocks",
              "separator"
            ],
            "frozen": false,
            "icon": "Combine",
            "legacy": false,
            "lf_version": "1.6.0",
            "metadata": {},
            "minimized": false,
            "output_types": [],
            "outputs": [
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Combined Output",
                "group_outputs": false,
                "hidden": null,
                "method": "build_combined",
                "name": "combined",
                "options": null,
                "required_inputs": null,
                "selected": "Message",
                "tool_mode": true,
                "types": [
                  "Message"
                ],
                "value": "__UNDEFINED__"
              }
            ],
            "pinned": false,
            "template": {
              "_type": "Component",
              "code": {
                "advanced": true,
                "dynamic": true,
                "fileTypes": [],
                "file_path": "",
                "info": "",
                "list": false,
                "load_from_db": false,
                "multiline": true,
                "name": "code",
                "password": false,
                "placeholder": "",
                "required": true,
                "show": true,
                "title_case": false,
                "type": "code",
                "value": "from __future__ import annotations\n\nfrom typing import Any, List\nimport pandas as pd\n\nfrom langflow.custom import Component\nfrom langflow.io import MessageInput, MessageTextInput, BoolInput, Output\nfrom langflow.schema.message import Message\n\n\nclass CombineThreeLLMOutputs(Component):\n    display_name = \"Combine 3 LLM Outputs\"\n    description = \"Combine three LLM Message outputs into one Message, with optional headers and separators.\"\n    name = \"combine_three_llm_outputs\"\n    icon = \"Combine\"\n\n    inputs = [\n        MessageInput(name=\"msg_a\", display_name=\"LLM Output A\", required=False),\n        MessageInput(name=\"msg_b\", display_name=\"LLM Output B\", required=False),\n        MessageInput(name=\"msg_c\", display_name=\"LLM Output C\", required=False),\n\n        BoolInput(name=\"include_headers\", display_name=\"Include headers\", value=True),\n        MessageTextInput(name=\"header_a\", display_name=\"Header A\", value=\"Analysis A\"),\n        MessageTextInput(name=\"header_b\", display_name=\"Header B\", value=\"Analysis B\"),\n        MessageTextInput(name=\"header_c\", display_name=\"Header C\", value=\"Analysis C\"),\n\n        BoolInput(name=\"skip_empty\", display_name=\"Skip empty sections\", value=True),\n        BoolInput(name=\"dedupe_exact_blocks\", display_name=\"Remove duplicate blocks\", value=True),\n        MessageTextInput(name=\"separator\", display_name=\"Separator\", value=\"\\n\\n---\\n\\n\"),\n    ]\n\n    outputs = [\n        Output(name=\"combined\", display_name=\"Combined Output\", method=\"build_combined\"),\n    ]\n\n    def _to_text(self, x: Any) -> str:\n        if x is None:\n            return \"\"\n        if isinstance(x, Message):\n            return (x.text or \"\").strip()\n        if isinstance(x, str):\n            return x.strip()\n        if hasattr(x, \"text\"):\n            try:\n                return str(getattr(x, \"text\") or \"\").strip()\n            except Exception:\n                return \"\"\n        if isinstance(x, dict) and \"text\" in x:\n            return str(x.get(\"text\") or \"\").strip()\n        return str(x).strip()\n\n    def build_combined(self) -> Message:\n        parts: List[str] = []\n\n        blocks = [\n            (\"A\", self.header_a, self.msg_a),\n            (\"B\", self.header_b, self.msg_b),\n            (\"C\", self.header_c, self.msg_c),\n        ]\n\n        for _, header, msg in blocks:\n            txt = self._to_text(msg)\n            if self.skip_empty and not txt:\n                continue\n\n            if self.include_headers:\n                parts.append(f\"{header}\\n{txt}\".strip())\n            else:\n                parts.append(txt.strip())\n\n        if self.dedupe_exact_blocks:\n            seen = set()\n            deduped: List[str] = []\n            for p in parts:\n                if p not in seen:\n                    deduped.append(p)\n                    seen.add(p)\n            parts = deduped\n\n        combined = (self.separator or \"\\n\\n---\\n\\n\").join([p for p in parts if p.strip()])\n\n        return Message(text=combined)"
              },
              "dedupe_exact_blocks": {
                "_input_type": "BoolInput",
                "advanced": false,
                "display_name": "Remove duplicate blocks",
                "dynamic": false,
                "info": "",
                "list": false,
                "list_add_label": "Add More",
                "name": "dedupe_exact_blocks",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "bool",
                "value": true
              },
              "header_a": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Header A",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "header_a",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Invoice Summary"
              },
              "header_b": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Header B",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "header_b",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Ticket Summary"
              },
              "header_c": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Header C",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "header_c",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Time Entry Smmary"
              },
              "include_headers": {
                "_input_type": "BoolInput",
                "advanced": false,
                "display_name": "Include headers",
                "dynamic": false,
                "info": "",
                "list": false,
                "list_add_label": "Add More",
                "name": "include_headers",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "bool",
                "value": true
              },
              "msg_a": {
                "_input_type": "MessageInput",
                "advanced": false,
                "display_name": "LLM Output A",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "msg_a",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": ""
              },
              "msg_b": {
                "_input_type": "MessageInput",
                "advanced": false,
                "display_name": "LLM Output B",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "msg_b",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": ""
              },
              "msg_c": {
                "_input_type": "MessageInput",
                "advanced": false,
                "display_name": "LLM Output C",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "msg_c",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": ""
              },
              "separator": {
                "_input_type": "MessageTextInput",
                "advanced": false,
                "display_name": "Separator",
                "dynamic": false,
                "info": "",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "separator",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "\n\n---\n\n"
              },
              "skip_empty": {
                "_input_type": "BoolInput",
                "advanced": false,
                "display_name": "Skip empty sections",
                "dynamic": false,
                "info": "",
                "list": false,
                "list_add_label": "Add More",
                "name": "skip_empty",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "bool",
                "value": true
              }
            },
            "tool_mode": false
          },
          "showNode": true,
          "type": "combine_three_llm_outputs"
        },
        "dragging": false,
        "id": "combine_three_llm_outputs-78VjU",
        "measured": {
          "height": 837,
          "width": 320
        },
        "position": {
          "x": 5853.517164049982,
          "y": 1402.3151247888857
        },
        "selected": false,
        "type": "genericNode"
      },
      {
        "data": {
          "id": "TextOutput-0km4F",
          "node": {
            "base_classes": [
              "Message"
            ],
            "beta": false,
            "category": "input_output",
            "conditional_paths": [],
            "custom_fields": {},
            "description": "Sends text output via API.",
            "display_name": "Text Output",
            "documentation": "https://docs.langflow.org/components-io#text-output",
            "edited": false,
            "field_order": [
              "input_value"
            ],
            "frozen": false,
            "icon": "type",
            "key": "TextOutput",
            "legacy": false,
            "lf_version": "1.6.0",
            "metadata": {},
            "minimized": false,
            "output_types": [],
            "outputs": [
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Output Text",
                "group_outputs": false,
                "method": "text_response",
                "name": "text",
                "selected": "Message",
                "tool_mode": true,
                "types": [
                  "Message"
                ],
                "value": "__UNDEFINED__"
              }
            ],
            "pinned": false,
            "score": 0.0026904540161865127,
            "template": {
              "_type": "Component",
              "code": {
                "advanced": true,
                "dynamic": true,
                "fileTypes": [],
                "file_path": "",
                "info": "",
                "list": false,
                "load_from_db": false,
                "multiline": true,
                "name": "code",
                "password": false,
                "placeholder": "",
                "required": true,
                "show": true,
                "title_case": false,
                "type": "code",
                "value": "from langflow.base.io.text import TextComponent\nfrom langflow.io import MultilineInput, Output\nfrom langflow.schema.message import Message\n\n\nclass TextOutputComponent(TextComponent):\n    display_name = \"Text Output\"\n    description = \"Sends text output via API.\"\n    documentation: str = \"https://docs.langflow.org/components-io#text-output\"\n    icon = \"type\"\n    name = \"TextOutput\"\n\n    inputs = [\n        MultilineInput(\n            name=\"input_value\",\n            display_name=\"Inputs\",\n            info=\"Text to be passed as output.\",\n        ),\n    ]\n    outputs = [\n        Output(display_name=\"Output Text\", name=\"text\", method=\"text_response\"),\n    ]\n\n    def text_response(self) -> Message:\n        message = Message(\n            text=self.input_value,\n        )\n        self.status = self.input_value\n        return message\n"
              },
              "input_value": {
                "_input_type": "MultilineInput",
                "advanced": false,
                "copy_field": false,
                "display_name": "Inputs",
                "dynamic": false,
                "info": "Text to be passed as output.",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "multiline": true,
                "name": "input_value",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": ""
              }
            },
            "tool_mode": false
          },
          "showNode": true,
          "type": "TextOutput"
        },
        "id": "TextOutput-0km4F",
        "measured": {
          "height": 203,
          "width": 320
        },
        "position": {
          "x": 6957.2991162613425,
          "y": 1660.5725806885716
        },
        "selected": false,
        "type": "genericNode"
      },
      {
        "data": {
          "id": "ChatOutput-nhTcC",
          "node": {
            "base_classes": [
              "Message"
            ],
            "beta": false,
            "conditional_paths": [],
            "custom_fields": {},
            "description": "Display a chat message in the Playground.",
            "display_name": "Chat Output",
            "documentation": "https://docs.langflow.org/components-io#chat-output",
            "edited": false,
            "field_order": [
              "input_value",
              "should_store_message",
              "sender",
              "sender_name",
              "session_id",
              "data_template"
            ],
            "frozen": false,
            "icon": "MessagesSquare",
            "legacy": false,
            "lf_version": "1.6.0",
            "metadata": {},
            "minimized": true,
            "output_types": [],
            "outputs": [
              {
                "allows_loop": false,
                "cache": true,
                "display_name": "Output Message",
                "group_outputs": false,
                "method": "message_response",
                "name": "message",
                "selected": "Message",
                "tool_mode": true,
                "types": [
                  "Message"
                ],
                "value": "__UNDEFINED__"
              }
            ],
            "pinned": false,
            "template": {
              "_type": "Component",
              "code": {
                "advanced": true,
                "dynamic": true,
                "fileTypes": [],
                "file_path": "",
                "info": "",
                "list": false,
                "load_from_db": false,
                "multiline": true,
                "name": "code",
                "password": false,
                "placeholder": "",
                "required": true,
                "show": true,
                "title_case": false,
                "type": "code",
                "value": "from collections.abc import Generator\nfrom typing import Any\n\nimport orjson\nfrom fastapi.encoders import jsonable_encoder\n\nfrom langflow.base.io.chat import ChatComponent\nfrom langflow.helpers.data import safe_convert\nfrom langflow.inputs.inputs import BoolInput, DropdownInput, HandleInput, MessageTextInput\nfrom langflow.schema.data import Data\nfrom langflow.schema.dataframe import DataFrame\nfrom langflow.schema.message import Message\nfrom langflow.schema.properties import Source\nfrom langflow.template.field.base import Output\nfrom langflow.utils.constants import (\n    MESSAGE_SENDER_AI,\n    MESSAGE_SENDER_NAME_AI,\n    MESSAGE_SENDER_USER,\n)\n\n\nclass ChatOutput(ChatComponent):\n    display_name = \"Chat Output\"\n    description = \"Display a chat message in the Playground.\"\n    documentation: str = \"https://docs.langflow.org/components-io#chat-output\"\n    icon = \"MessagesSquare\"\n    name = \"ChatOutput\"\n    minimized = True\n\n    inputs = [\n        HandleInput(\n            name=\"input_value\",\n            display_name=\"Inputs\",\n            info=\"Message to be passed as output.\",\n            input_types=[\"Data\", \"DataFrame\", \"Message\"],\n            required=True,\n        ),\n        BoolInput(\n            name=\"should_store_message\",\n            display_name=\"Store Messages\",\n            info=\"Store the message in the history.\",\n            value=True,\n            advanced=True,\n        ),\n        DropdownInput(\n            name=\"sender\",\n            display_name=\"Sender Type\",\n            options=[MESSAGE_SENDER_AI, MESSAGE_SENDER_USER],\n            value=MESSAGE_SENDER_AI,\n            advanced=True,\n            info=\"Type of sender.\",\n        ),\n        MessageTextInput(\n            name=\"sender_name\",\n            display_name=\"Sender Name\",\n            info=\"Name of the sender.\",\n            value=MESSAGE_SENDER_NAME_AI,\n            advanced=True,\n        ),\n        MessageTextInput(\n            name=\"session_id\",\n            display_name=\"Session ID\",\n            info=\"The session ID of the chat. If empty, the current session ID parameter will be used.\",\n            advanced=True,\n        ),\n        MessageTextInput(\n            name=\"data_template\",\n            display_name=\"Data Template\",\n            value=\"{text}\",\n            advanced=True,\n            info=\"Template to convert Data to Text. If left empty, it will be dynamically set to the Data's text key.\",\n        ),\n    ]\n    outputs = [\n        Output(\n            display_name=\"Output Message\",\n            name=\"message\",\n            method=\"message_response\",\n        ),\n    ]\n\n    def _build_source(self, id_: str | None, display_name: str | None, source: str | None) -> Source:\n        source_dict = {}\n        if id_:\n            source_dict[\"id\"] = id_\n        if display_name:\n            source_dict[\"display_name\"] = display_name\n        if source:\n            # Handle case where source is a ChatOpenAI object\n            if hasattr(source, \"model_name\"):\n                source_dict[\"source\"] = source.model_name\n            elif hasattr(source, \"model\"):\n                source_dict[\"source\"] = str(source.model)\n            else:\n                source_dict[\"source\"] = str(source)\n        return Source(**source_dict)\n\n    async def message_response(self) -> Message:\n        # First convert the input to string if needed\n        text = self.convert_to_string()\n\n        # Get source properties\n        source, _icon, display_name, source_id = self.get_properties_from_source_component()\n\n        # Create or use existing Message object\n        if isinstance(self.input_value, Message):\n            message = self.input_value\n            # Update message properties\n            message.text = text\n        else:\n            message = Message(text=text)\n\n        # Set message properties\n        message.sender = self.sender\n        message.sender_name = self.sender_name\n        message.session_id = self.session_id\n        message.flow_id = self.graph.flow_id if hasattr(self, \"graph\") else None\n        message.properties.source = self._build_source(source_id, display_name, source)\n\n        # Store message if needed\n        if self.session_id and self.should_store_message:\n            stored_message = await self.send_message(message)\n            self.message.value = stored_message\n            message = stored_message\n\n        self.status = message\n        return message\n\n    def _serialize_data(self, data: Data) -> str:\n        \"\"\"Serialize Data object to JSON string.\"\"\"\n        # Convert data.data to JSON-serializable format\n        serializable_data = jsonable_encoder(data.data)\n        # Serialize with orjson, enabling pretty printing with indentation\n        json_bytes = orjson.dumps(serializable_data, option=orjson.OPT_INDENT_2)\n        # Convert bytes to string and wrap in Markdown code blocks\n        return \"```json\\n\" + json_bytes.decode(\"utf-8\") + \"\\n```\"\n\n    def _validate_input(self) -> None:\n        \"\"\"Validate the input data and raise ValueError if invalid.\"\"\"\n        if self.input_value is None:\n            msg = \"Input data cannot be None\"\n            raise ValueError(msg)\n        if isinstance(self.input_value, list) and not all(\n            isinstance(item, Message | Data | DataFrame | str) for item in self.input_value\n        ):\n            invalid_types = [\n                type(item).__name__\n                for item in self.input_value\n                if not isinstance(item, Message | Data | DataFrame | str)\n            ]\n            msg = f\"Expected Data or DataFrame or Message or str, got {invalid_types}\"\n            raise TypeError(msg)\n        if not isinstance(\n            self.input_value,\n            Message | Data | DataFrame | str | list | Generator | type(None),\n        ):\n            type_name = type(self.input_value).__name__\n            msg = f\"Expected Data or DataFrame or Message or str, Generator or None, got {type_name}\"\n            raise TypeError(msg)\n\n    def convert_to_string(self) -> str | Generator[Any, None, None]:\n        \"\"\"Convert input data to string with proper error handling.\"\"\"\n        self._validate_input()\n        if isinstance(self.input_value, list):\n            clean_data: bool = getattr(self, \"clean_data\", False)\n            return \"\\n\".join([safe_convert(item, clean_data=clean_data) for item in self.input_value])\n        if isinstance(self.input_value, Generator):\n            return self.input_value\n        return safe_convert(self.input_value)\n"
              },
              "data_template": {
                "_input_type": "MessageTextInput",
                "advanced": true,
                "display_name": "Data Template",
                "dynamic": false,
                "info": "Template to convert Data to Text. If left empty, it will be dynamically set to the Data's text key.",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "data_template",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "{text}"
              },
              "input_value": {
                "_input_type": "HandleInput",
                "advanced": false,
                "display_name": "Inputs",
                "dynamic": false,
                "info": "Message to be passed as output.",
                "input_types": [
                  "Data",
                  "DataFrame",
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "name": "input_value",
                "placeholder": "",
                "required": true,
                "show": true,
                "title_case": false,
                "trace_as_metadata": true,
                "type": "other",
                "value": ""
              },
              "sender": {
                "_input_type": "DropdownInput",
                "advanced": true,
                "combobox": false,
                "dialog_inputs": {},
                "display_name": "Sender Type",
                "dynamic": false,
                "external_options": {},
                "info": "Type of sender.",
                "name": "sender",
                "options": [
                  "Machine",
                  "User"
                ],
                "options_metadata": [],
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "toggle": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "str",
                "value": "Machine"
              },
              "sender_name": {
                "_input_type": "MessageTextInput",
                "advanced": true,
                "display_name": "Sender Name",
                "dynamic": false,
                "info": "Name of the sender.",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "sender_name",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": "AI"
              },
              "session_id": {
                "_input_type": "MessageTextInput",
                "advanced": true,
                "display_name": "Session ID",
                "dynamic": false,
                "info": "The session ID of the chat. If empty, the current session ID parameter will be used.",
                "input_types": [
                  "Message"
                ],
                "list": false,
                "list_add_label": "Add More",
                "load_from_db": false,
                "name": "session_id",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_input": true,
                "trace_as_metadata": true,
                "type": "str",
                "value": ""
              },
              "should_store_message": {
                "_input_type": "BoolInput",
                "advanced": true,
                "display_name": "Store Messages",
                "dynamic": false,
                "info": "Store the message in the history.",
                "list": false,
                "list_add_label": "Add More",
                "name": "should_store_message",
                "placeholder": "",
                "required": false,
                "show": true,
                "title_case": false,
                "tool_mode": false,
                "trace_as_metadata": true,
                "type": "bool",
                "value": true
              }
            },
            "tool_mode": false
          },
          "showNode": false,
          "type": "ChatOutput"
        },
        "id": "ChatOutput-nhTcC",
        "measured": {
          "height": 48,
          "width": 192
        },
        "position": {
          "x": 7556.881500554695,
          "y": 1797.1995743264222
        },
        "selected": false,
        "type": "genericNode"
      }
    ],
    "viewport": {
      "x": 174.72918100490375,
      "y": -386.9197739419236,
      "zoom": 0.25
    }
  },
  "description": "Connectwise Manage Insights",
  "endpoint_name": null,
  "id": "b84b0d34-4257-4de5-acfc-86a59d0134bd",
  "is_component": false,
  "last_tested_version": "1.6.0",
  "name": "Invoive, Ticket, and Time Insights.v2",
  "tags": []
}