{
  "name": "Review analysis: configure before use",
  "active": false,
  "nodes": [
    {
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2.2,
      "position": [
        0,
        0
      ],
      "parameters": {
        "httpMethod": "POST",
        "path": "CONFIGURE_REVIEW_WEBHOOK_PATH",
        "authentication": "headerAuth",
        "responseMode": "onReceived",
        "options": {
          "responseCode": {
            "values": {
              "responseCode": "customCode",
              "customCode": 202
            }
          },
          "responseData": "accepted"
        }
      }
    },
    {
      "name": "Claim",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.5,
      "position": [
        240,
        0
      ],
      "parameters": {
        "method": "POST",
        "url": "={{ $('Webhook').first(0, 0).json.body.private_base_url + '/internal/runs/' + $('Webhook').first(0, 0).json.body.run_id + '/claim' }}",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "X-Run-Capability",
              "value": "={{ $('Webhook').first(0, 0).json.body.capability }}"
            }
          ]
        },
        "sendBody": true,
        "contentType": "json",
        "specifyBody": "json",
        "jsonBody": "={{ { run_id: $('Webhook').first(0, 0).json.body.run_id, attempt_id: $('Webhook').first(0, 0).json.body.attempt_id, epoch: $('Webhook').first(0, 0).json.body.epoch, execution_id: $execution.id,  } }}",
        "options": {
          "timeout": 20000,
          "response": {
            "response": {
              "responseFormat": "json",
              "neverError": false
            }
          }
        }
      }
    },
    {
      "name": "Stop?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.4,
      "position": [
        480,
        0
      ],
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 4
          },
          "conditions": [
            {
              "id": "stop",
              "leftValue": "={{ $json.stop }}",
              "rightValue": true,
              "operator": {
                "type": "boolean",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      }
    },
    {
      "name": "Подготовить запрос",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.5,
      "position": [
        720,
        0
      ],
      "parameters": {
        "mode": "raw",
        "jsonOutput": "={{ { ...$json, model_request: { system: $json.analysis_instruction, messages: [{role: \"user\", content: JSON.stringify($json.reviews.map(r => ({rid: r.rid, product_key: r.product_key, rating: r.rating, text_fields: r.text_fields})))}], model: \"deepseek/deepseek-v4.1-flash-1017\", reasoning_effort: \"none\", max_output_tokens: 4096, tools: [], tool_defs: {} } } }}",
        "includeOtherFields": true,
        "include": "all",
        "options": {}
      }
    },
    {
      "name": "AI",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.5,
      "position": [
        960,
        0
      ],
      "parameters": {
        "method": "POST",
        "url": "={{ $('Webhook').first(0, 0).json.body.private_base_url + '/internal/ai/complete' }}",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "X-Run-Capability",
              "value": "={{ $('Webhook').first(0, 0).json.body.capability }}"
            }
          ]
        },
        "sendBody": true,
        "contentType": "json",
        "specifyBody": "json",
        "jsonBody": "={{ { run_id: $('Webhook').first(0, 0).json.body.run_id, attempt_id: $('Webhook').first(0, 0).json.body.attempt_id, epoch: $('Webhook').first(0, 0).json.body.epoch, execution_id: $execution.id, batch_id: $json.batch_id, call_id: $json.call_id, model_request: $json.model_request } }}",
        "options": {
          "timeout": 130000,
          "response": {
            "response": {
              "responseFormat": "json",
              "neverError": false
            }
          }
        }
      }
    },
    {
      "name": "Validate",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.5,
      "position": [
        1200,
        0
      ],
      "parameters": {
        "method": "POST",
        "url": "={{ $('Webhook').first(0, 0).json.body.private_base_url + '/internal/runs/' + $('Webhook').first(0, 0).json.body.run_id + '/validate' }}",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "X-Run-Capability",
              "value": "={{ $('Webhook').first(0, 0).json.body.capability }}"
            }
          ]
        },
        "sendBody": true,
        "contentType": "json",
        "specifyBody": "json",
        "jsonBody": "={{ { run_id: $('Webhook').first(0, 0).json.body.run_id, attempt_id: $('Webhook').first(0, 0).json.body.attempt_id, epoch: $('Webhook').first(0, 0).json.body.epoch, execution_id: $execution.id, batch_id: $json.batch_id, content: $json.content } }}",
        "options": {
          "timeout": 20000,
          "response": {
            "response": {
              "responseFormat": "json",
              "neverError": false
            }
          }
        }
      }
    },
    {
      "name": "Valid or partial?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.4,
      "position": [
        1440,
        0
      ],
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 4
          },
          "conditions": [
            {
              "id": "has_valid",
              "leftValue": "={{ $json.has_valid }}",
              "rightValue": true,
              "operator": {
                "type": "boolean",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      }
    },
    {
      "name": "Commit",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.5,
      "position": [
        1680,
        0
      ],
      "parameters": {
        "method": "POST",
        "url": "={{ $('Webhook').first(0, 0).json.body.private_base_url + '/internal/runs/' + $('Webhook').first(0, 0).json.body.run_id + '/commit' }}",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "X-Run-Capability",
              "value": "={{ $('Webhook').first(0, 0).json.body.capability }}"
            }
          ]
        },
        "sendBody": true,
        "contentType": "json",
        "specifyBody": "json",
        "jsonBody": "={{ { run_id: $('Webhook').first(0, 0).json.body.run_id, attempt_id: $('Webhook').first(0, 0).json.body.attempt_id, epoch: $('Webhook').first(0, 0).json.body.epoch, execution_id: $execution.id, batch_id: $json.batch_id, record_revisions: $json.record_revisions, completed_rids: $json.completed_rids, labels: $json.labels } }}",
        "options": {
          "timeout": 20000,
          "response": {
            "response": {
              "responseFormat": "json",
              "neverError": false
            }
          }
        }
      }
    },
    {
      "name": "Finish",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.5,
      "position": [
        720,
        -260
      ],
      "parameters": {
        "method": "POST",
        "url": "={{ $('Webhook').first(0, 0).json.body.private_base_url + '/internal/runs/' + $('Webhook').first(0, 0).json.body.run_id + '/finish' }}",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "X-Run-Capability",
              "value": "={{ $('Webhook').first(0, 0).json.body.capability }}"
            }
          ]
        },
        "sendBody": true,
        "contentType": "json",
        "specifyBody": "json",
        "jsonBody": "={{ { run_id: $('Webhook').first(0, 0).json.body.run_id, attempt_id: $('Webhook').first(0, 0).json.body.attempt_id, epoch: $('Webhook').first(0, 0).json.body.epoch, execution_id: $execution.id, partial: false } }}",
        "options": {
          "timeout": 20000,
          "response": {
            "response": {
              "responseFormat": "json",
              "neverError": false
            }
          }
        }
      }
    },
    {
      "name": "Finish partial",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.5,
      "position": [
        1200,
        320
      ],
      "parameters": {
        "method": "POST",
        "url": "={{ $('Webhook').first(0, 0).json.body.private_base_url + '/internal/runs/' + $('Webhook').first(0, 0).json.body.run_id + '/finish' }}",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "X-Run-Capability",
              "value": "={{ $('Webhook').first(0, 0).json.body.capability }}"
            }
          ]
        },
        "sendBody": true,
        "contentType": "json",
        "specifyBody": "json",
        "jsonBody": "={{ { run_id: $('Webhook').first(0, 0).json.body.run_id, attempt_id: $('Webhook').first(0, 0).json.body.attempt_id, epoch: $('Webhook').first(0, 0).json.body.epoch, execution_id: $execution.id, partial: true } }}",
        "options": {
          "timeout": 20000,
          "response": {
            "response": {
              "responseFormat": "json",
              "neverError": false
            }
          }
        }
      }
    }
  ],
  "connections": {
    "Webhook": {
      "main": [
        [
          {
            "node": "Claim",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Claim": {
      "main": [
        [
          {
            "node": "Stop?",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Finish partial",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Stop?": {
      "main": [
        [
          {
            "node": "Finish",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Подготовить запрос",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Подготовить запрос": {
      "main": [
        [
          {
            "node": "AI",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Finish partial",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "AI": {
      "main": [
        [
          {
            "node": "Validate",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Finish partial",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Validate": {
      "main": [
        [
          {
            "node": "Valid or partial?",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Finish partial",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Valid or partial?": {
      "main": [
        [
          {
            "node": "Commit",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Finish partial",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Commit": {
      "main": [
        [
          {
            "node": "Claim",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Finish partial",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  }
}
