{
  "name": "Jev: безопасный ответ на вебхук заказа",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "jev-order-intent",
        "responseMode": "lastNode",
        "options": {}
      },
      "id": "db6c2d22-75ce-4dc9-8a4c-1203c1869401",
      "name": "Получить обращение",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        -720,
        0
      ],
      "webhookId": "73481656-0f94-44d7-a2ff-ec71d2bafd02"
    },
    {
      "parameters": {
        "jsCode": "const body = $json.body ?? $json;\nconst message = String(body.message ?? '').trim();\n\nif (!message) {\n  throw new Error('Нужно передать непустое поле message в теле запроса.');\n}\n\nreturn [{\n  json: {\n    state: {\n      order: {\n        id: String(body.orderId ?? ''),\n        status: String(body.orderStatus ?? 'unknown')\n      },\n      message\n    },\n    questions: {\n      intent: {\n        type: 'choice',\n        instructions: 'Какое действие описывает `message` с учётом `order.status`?',\n        criteria: {\n          status: 'Клиент хочет узнать состояние заказа.',\n          cancel: 'Клиент хочет отменить заказ.',\n          return: 'Клиент хочет оформить возврат или спрашивает о нём.',\n          other: 'Сообщение не относится к этим действиям или недостаточно ясно.'\n        }\n      },\n      explicit_cancel: {\n        type: 'noul',\n        instructions: 'В `message` есть явная просьба отменить заказ?'\n      }\n    }\n  }\n}];"
      },
      "id": "c4121443-1881-4a0b-a943-ea6ba5694302",
      "name": "Проверить вход и составить вопросы",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        -460,
        0
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.typesafe.ai/v1/systemone",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "Bearer REPLACE_WITH_YOUR_TYPESAFE_API_KEY"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({\n  state: $json.state,\n  model: 'jev-latest',\n  questions: $json.questions\n}) }}",
        "options": {}
      },
      "id": "0c4d5fed-3358-4bbf-829d-52daf88e2b03",
      "name": "Спросить Jev",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        -180,
        0
      ]
    },
    {
      "parameters": {
        "jsCode": "const answers = $json.answers ?? {};\nconst intent = answers.intent?.choice ?? 'other';\nconst confidence = Number(answers.intent?.confidence ?? 0);\nconst explicitCancel = Number(answers.explicit_cancel?.noul ?? 0);\n\nconst canShowStatus = intent === 'status' && confidence >= 0.85;\nconst decision = canShowStatus ? 'show_status' : 'confirm_or_review';\nconst reason = canShowStatus\n  ? 'Статус можно показать без изменения заказа.'\n  : 'Отмена и возврат требуют подтверждения, а не прямого действия из модели.';\n\nreturn [{\n  json: {\n    decision,\n    intent,\n    confidence,\n    explicitCancel,\n    reason\n  }\n}];"
      },
      "id": "b6b5adfc-7045-47c1-86e2-0dd3f3c25c04",
      "name": "Вернуть безопасное решение",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        100,
        0
      ]
    }
  ],
  "pinData": {},
  "connections": {
    "Получить обращение": {
      "main": [
        [
          {
            "node": "Проверить вход и составить вопросы",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Проверить вход и составить вопросы": {
      "main": [
        [
          {
            "node": "Спросить Jev",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Спросить Jev": {
      "main": [
        [
          {
            "node": "Вернуть безопасное решение",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {},
  "versionId": "5bf5bfaf-df2e-45cc-a5c5-3c21a47d5505",
  "meta": {
    "templateCredsSetupCompleted": false
  },
  "tags": []
}
