{
  "name": "Stage6 Webhook Summary",
  "active": false,
  "nodes": [
    {
      "id": "Webhook",
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        0,
        0
      ],
      "parameters": {
        "httpMethod": "POST",
        "path": "stage6-summary",
        "responseMode": "responseNode",
        "options": {}
      },
      "webhookId": "stage6-summary"
    },
    {
      "id": "Model",
      "name": "Model",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        240,
        0
      ],
      "parameters": {
        "method": "POST",
        "url": "https://api.openai.com/v1/responses",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({model: \"gpt-5.4-mini-2026-03-17\", input: \"Return JSON only with one string field summary. Summarize: \" + String($json.body.text || \"\"), max_output_tokens: 256, store: false}) }}",
        "options": {
          "timeout": 30000
        }
      },
      "onError": "continueRegularOutput"
    },
    {
      "id": "Validate",
      "name": "Validate",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        480,
        0
      ],
      "parameters": {
        "jsCode": "return $input.all().map(item => {\n  let raw = item.json.modelText;\n  if (raw === undefined) raw = (item.json.output || []).flatMap(x => x.content || []).filter(x => x.type === 'output_text').map(x => x.text).join('');\n  try {\n    const value = JSON.parse(raw);\n    if (!value || typeof value.summary !== 'string' || !value.summary.trim() || value.summary.length > 1000) throw new Error('summary must be a nonempty string <= 1000 chars');\n    return {json: {case: item.json.case || 'webhook', ok: true, summary: value.summary}};\n  } catch (_) {\n    return {json: {case: item.json.case || 'webhook', ok: false, summary: '生成结果格式错误，请稍后重试。'}};\n  }\n});"
      }
    },
    {
      "id": "Valid?",
      "name": "Valid?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        720,
        0
      ],
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "valid",
              "leftValue": "={{ $json.ok }}",
              "rightValue": true,
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      }
    },
    {
      "id": "Success",
      "name": "Success",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        960,
        -100
      ],
      "parameters": {
        "jsCode": "return $input.all().map(x => ({json: {...x.json, route: 'success'}}));"
      }
    },
    {
      "id": "Fallback",
      "name": "Fallback",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        960,
        100
      ],
      "parameters": {
        "jsCode": "return $input.all().map(x => ({json: {...x.json, route: 'fallback'}}));"
      }
    },
    {
      "id": "Reply",
      "name": "Reply",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.4,
      "position": [
        1200,
        0
      ],
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ JSON.stringify($json) }}",
        "options": {}
      }
    }
  ],
  "connections": {
    "Validate": {
      "main": [
        [
          {
            "node": "Valid?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Valid?": {
      "main": [
        [
          {
            "node": "Success",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Fallback",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Webhook": {
      "main": [
        [
          {
            "node": "Model",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Model": {
      "main": [
        [
          {
            "node": "Validate",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Success": {
      "main": [
        [
          {
            "node": "Reply",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fallback": {
      "main": [
        [
          {
            "node": "Reply",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  }
}
