{
  "id": "near-settlement-reconcile",
  "name": "Taifoon × NEAR AI Cloud — reconcile the day against the Taifoon sale run (digest)",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "hours",
              "hoursInterval": 24
            }
          ]
        }
      },
      "id": "every-day",
      "name": "Every day",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        0,
        300
      ]
    },
    {
      "parameters": {
        "options": {},
        "method": "GET",
        "url": "https://api.taifoon.dev/coordination/sale-run.json"
      },
      "id": "salerun",
      "name": "Sale run",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        220,
        300
      ],
      "notes": "The public summary of the one sale-run ledger: every sale Taifoon took as seller of record, its legs and whether they sum (sigma). Re-rendered every 5 minutes on box 116.",
      "notesInFlow": true
    },
    {
      "parameters": {
        "options": {},
        "method": "GET",
        "url": "https://coord.taifoon.dev/v1/access/key",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth"
      },
      "id": "standing",
      "name": "Key standing",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        220,
        500
      ],
      "notes": "The key's own standing: balance, calls metered today, the refusals. Header X-API-Key: the key behind TAIFOON_API_KEY.",
      "notesInFlow": true
    },
    {
      "parameters": {
        "jsCode": "const s = $('Sale run').first().json; let k = {}; try { k = $('Key standing').first().json; } catch {}\nconst inference = (s.cells ?? []).filter((c) => /inference/.test(c.class ?? ''));\nreturn [{ json: { date: new Date().toISOString().slice(0, 10), ledger: { sales: s.sales, events: s.events, sigma: s.sigma, invariant: s.invariant, reconcile: s.reconcile }, inference_pairs: inference.map((c) => ({ venue_in: c.venue_in, venue_out: c.venue_out, stage: c.stage, outcomes: c.outcomes })),\n  key: { balance: k.balance ?? k.standing?.balance ?? null, metered_today: k.metered_today ?? k.usage?.today ?? null }, settle_rule: 'one settlement per attempt, actual_cost_microusd from the usage the answer carried, evidence = the completion id (key door) or the sale id (x402 door)' } }];"
      },
      "id": "digest",
      "name": "Digest",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        440,
        400
      ],
      "notes": "What both sides read: Taifoon's ledger, the key's standing, the rule a settlement follows.",
      "notesInFlow": true
    },
    {
      "parameters": {
        "options": {},
        "method": "POST",
        "url": "=https://cloud-api.near.ai/v1/admin/provider-attempts/{{ $json.attempt_id }}/settlement",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ actual_cost_microusd: $json.actual_cost_microusd, evidence: $json.evidence }) }}",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth"
      },
      "id": "settle",
      "name": "POST provider-attempts/{id}/settlement — NEAR runs this",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        660,
        400
      ],
      "notes": "Disabled on purpose: NEAR's admin call, one per attempt, fed by its own attempt log joined on the evidence id.",
      "notesInFlow": true,
      "disabled": true
    }
  ],
  "connections": {
    "Every day": {
      "main": [
        [
          {
            "node": "Sale run",
            "type": "main",
            "index": 0
          },
          {
            "node": "Key standing",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Sale run": {
      "main": [
        [
          {
            "node": "Digest",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Key standing": {
      "main": [
        [
          {
            "node": "Digest",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Digest": {
      "main": [
        [
          {
            "node": "POST provider-attempts/{id}/settlement — NEAR runs this",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1",
    "saveDataSuccessExecution": "all",
    "saveManualExecutions": true
  },
  "meta": {
    "templateCredsSetupCompleted": false,
    "instanceId": "taifoon-coordination"
  }
}
