{
 "schema_version": "1.0",
 "nodes": [
  {
   "id": "document",
   "type": "input/text",
   "label": "Document text",
   "parameters": {
    "required": true,
    "multiline": true,
    "text": "FATTURA N. 2026/0412 — Data: 3 ottobre 2026. Fornitore: Tipografia Lario Srl, Via Volta 8, Como. Intestata a: Studio Verdi Srl, Via Roma 12, Bologna. 500 brochure A5, carta patinata 170 g: 380,00 EUR; IVA 22% 83,60 EUR; Totale da pagare 463,60 EUR. Pagamento con bonifico entro il 2 novembre 2026."
   }
  },
  {
   "id": "sort",
   "type": "ai/decide",
   "label": "What document is it?",
   "parameters": {
    "questions": {
     "schema": "madoo.decision-questions/v1",
     "questions": [
      {
       "name": "kind",
       "type": "choice",
       "instructions": "What kind of document is this?",
       "options": [
        {
         "value": "invoice",
         "description": "A bill asking for or recording payment"
        },
        {
         "value": "quote",
         "description": "An offer or estimate, not yet accepted"
        },
        {
         "value": "contract",
         "description": "An agreement with terms between parties"
        },
        {
         "value": "other",
         "description": "Anything else: a price list, a letter, minutes, a CV"
        }
       ],
       "threshold": 0.7
      },
      {
       "name": "has_amount",
       "type": "yes_no",
       "instructions": "The document states an amount of money to pay or to be paid.",
       "threshold": 0.8
      }
     ]
    }
   }
  },
  {
   "id": "is_invoice",
   "type": "utility/predicate",
   "label": "Is it a invoice?",
   "parameters": {
    "operation": "equals",
    "compare_text": "invoice"
   }
  },
  {
   "id": "only_invoice",
   "type": "utility/filter",
   "label": "Only a invoice",
   "parameters": {
    "invert": false
   }
  },
  {
   "id": "read_invoice",
   "type": "ai/text_generation",
   "label": "Read the invoice",
   "parameters": {
    "system_prompt": "From the invoice below, answer with one JSON object with exactly these keys: supplier (text), customer (text), number (text), date (YYYY-MM-DD), total (number, the amount due, in the invoice's currency), currency (ISO code), due_date (YYYY-MM-DD or null). Use only what the document says; null for anything it does not state.",
    "response_format": "json_object",
    "max_tokens": 600
   }
  },
  {
   "id": "o_invoice",
   "type": "output/json",
   "label": "Invoice data",
   "parameters": {
    "output_name": "invoice_data"
   }
  },
  {
   "id": "is_quote",
   "type": "utility/predicate",
   "label": "Is it a quote?",
   "parameters": {
    "operation": "equals",
    "compare_text": "quote"
   }
  },
  {
   "id": "only_quote",
   "type": "utility/filter",
   "label": "Only a quote",
   "parameters": {
    "invert": false
   }
  },
  {
   "id": "read_quote",
   "type": "ai/text_generation",
   "label": "Read the quote",
   "parameters": {
    "system_prompt": "From the quote below, answer with one JSON object with exactly these keys: issuer (text), customer (text), total (number), currency (ISO code), valid_until (YYYY-MM-DD or null), what (one sentence). Use only what the document says; null for anything it does not state.",
    "response_format": "json_object",
    "max_tokens": 600
   }
  },
  {
   "id": "o_quote",
   "type": "output/json",
   "label": "Quote data",
   "parameters": {
    "output_name": "quote_data"
   }
  },
  {
   "id": "is_contract",
   "type": "utility/predicate",
   "label": "Is it a contract?",
   "parameters": {
    "operation": "equals",
    "compare_text": "contract"
   }
  },
  {
   "id": "only_contract",
   "type": "utility/filter",
   "label": "Only a contract",
   "parameters": {
    "invert": false
   }
  },
  {
   "id": "read_contract",
   "type": "ai/text_generation",
   "label": "Read the contract",
   "parameters": {
    "system_prompt": "From the contract below, answer with one JSON object with exactly these keys: kind (text), parties (list of names), term (text), notice (text or null), governing_law (text or null). Use only what the document says; null for anything it does not state.",
    "response_format": "json_object",
    "max_tokens": 600
   }
  },
  {
   "id": "o_contract",
   "type": "output/json",
   "label": "Contract data",
   "parameters": {
    "output_name": "contract_data"
   }
  },
  {
   "id": "o_kind",
   "type": "output/text",
   "label": "Kind",
   "parameters": {
    "output_name": "kind"
   }
  },
  {
   "id": "o_kind_p",
   "type": "output/json",
   "label": "Kind — probability",
   "parameters": {
    "output_name": "kind_probability"
   }
  },
  {
   "id": "o_amount",
   "type": "output/json",
   "label": "States an amount",
   "parameters": {
    "output_name": "has_amount"
   }
  }
 ],
 "connections": [
  {
   "from_node": "document",
   "from_output": "text",
   "to_node": "sort",
   "to_input": "content"
  },
  {
   "from_node": "sort",
   "from_output": "kind",
   "to_node": "is_invoice",
   "to_input": "a"
  },
  {
   "from_node": "is_invoice",
   "from_output": "result",
   "to_node": "only_invoice",
   "to_input": "condition"
  },
  {
   "from_node": "document",
   "from_output": "text",
   "to_node": "only_invoice",
   "to_input": "value"
  },
  {
   "from_node": "only_invoice",
   "from_output": "value",
   "to_node": "read_invoice",
   "to_input": "prompt"
  },
  {
   "from_node": "read_invoice",
   "from_output": "json",
   "to_node": "o_invoice",
   "to_input": "json"
  },
  {
   "from_node": "sort",
   "from_output": "kind",
   "to_node": "is_quote",
   "to_input": "a"
  },
  {
   "from_node": "is_quote",
   "from_output": "result",
   "to_node": "only_quote",
   "to_input": "condition"
  },
  {
   "from_node": "document",
   "from_output": "text",
   "to_node": "only_quote",
   "to_input": "value"
  },
  {
   "from_node": "only_quote",
   "from_output": "value",
   "to_node": "read_quote",
   "to_input": "prompt"
  },
  {
   "from_node": "read_quote",
   "from_output": "json",
   "to_node": "o_quote",
   "to_input": "json"
  },
  {
   "from_node": "sort",
   "from_output": "kind",
   "to_node": "is_contract",
   "to_input": "a"
  },
  {
   "from_node": "is_contract",
   "from_output": "result",
   "to_node": "only_contract",
   "to_input": "condition"
  },
  {
   "from_node": "document",
   "from_output": "text",
   "to_node": "only_contract",
   "to_input": "value"
  },
  {
   "from_node": "only_contract",
   "from_output": "value",
   "to_node": "read_contract",
   "to_input": "prompt"
  },
  {
   "from_node": "read_contract",
   "from_output": "json",
   "to_node": "o_contract",
   "to_input": "json"
  },
  {
   "from_node": "sort",
   "from_output": "kind",
   "to_node": "o_kind",
   "to_input": "text"
  },
  {
   "from_node": "sort",
   "from_output": "kind_probability",
   "to_node": "o_kind_p",
   "to_input": "json"
  },
  {
   "from_node": "sort",
   "from_output": "has_amount",
   "to_node": "o_amount",
   "to_input": "json"
  }
 ]
}