{
  "name": "Primex: first chat request",
  "active": false,
  "nodes": [
    {
      "id": "e02a84ca-851a-4cf8-9d73-9ee88c65a9d7",
      "name": "Run manually",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [0, 0],
      "parameters": {}
    },
    {
      "id": "cd282a56-93d7-4462-850c-a0e7ad6472f6",
      "name": "Request inputs",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [240, 0],
      "parameters": {
        "assignments": {
          "assignments": [
            {"id": "model", "name": "model", "value": "YOUR_LIVE_CHAT_MODEL", "type": "string"},
            {"id": "prompt", "name": "prompt", "value": "Hello from Primex", "type": "string"}
          ]
        },
        "options": {}
      }
    },
    {
      "id": "9065ce95-5cb7-4a6b-bd1a-27736ba8ec6d",
      "name": "Primex Chat",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [480, 0],
      "retryOnFail": false,
      "onError": "stopWorkflow",
      "parameters": {
        "method": "POST",
        "url": "https://gateway.primexos.com/v1/chat/completions",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ model: $json.model, messages: [{ role: 'user', content: $json.prompt }], stream: false }) }}",
        "options": {
          "timeout": 60000,
          "redirect": {"redirect": {"followRedirects": false}},
          "response": {"response": {"fullResponse": true, "neverError": false, "responseFormat": "json"}}
        }
      }
    },
    {
      "id": "4029b12a-e301-4a25-a5da-a662a1e08640",
      "name": "Has a text answer",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [720, 0],
      "parameters": {
        "conditions": {
          "options": {"caseSensitive": true, "leftValue": "", "typeValidation": "strict", "version": 2},
          "conditions": [
            {
              "id": "text-answer",
              "leftValue": "={{ typeof $json.body?.choices?.[0]?.message?.content === 'string' && $json.body.choices[0].message.content.trim().length > 0 }}",
              "rightValue": "",
              "operator": {"type": "boolean", "operation": "true", "singleValue": true}
            }
          ],
          "combinator": "and"
        },
        "options": {}
      }
    },
    {
      "id": "e5301ee0-36ec-47e7-83c0-38c8ee77dbb4",
      "name": "Read result",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [960, -100],
      "parameters": {
        "assignments": {
          "assignments": [
            {"id": "answer", "name": "answer", "value": "={{ $json.body.choices[0].message.content }}", "type": "string"},
            {"id": "requestId", "name": "requestId", "value": "={{ $json.headers['x-primex-request-id'] || '' }}", "type": "string"},
            {"id": "model-result", "name": "model", "value": "={{ $json.body.model || '' }}", "type": "string"},
            {"id": "usage", "name": "usage", "value": "={{ $json.body.usage || {} }}", "type": "object"}
          ]
        },
        "options": {}
      }
    },
    {
      "id": "7396a76f-8e76-4d4b-a14e-f4c04b7910de",
      "name": "Stop: no text answer",
      "type": "n8n-nodes-base.stopAndError",
      "typeVersion": 1,
      "position": [960, 100],
      "parameters": {"errorMessage": "Primex returned no text answer. Inspect the HTTP response and Usage before retrying."}
    }
  ],
  "connections": {
    "Run manually": {"main": [[{"node": "Request inputs", "type": "main", "index": 0}]]},
    "Request inputs": {"main": [[{"node": "Primex Chat", "type": "main", "index": 0}]]},
    "Primex Chat": {"main": [[{"node": "Has a text answer", "type": "main", "index": 0}]]},
    "Has a text answer": {"main": [[{"node": "Read result", "type": "main", "index": 0}], [{"node": "Stop: no text answer", "type": "main", "index": 0}]]}
  },
  "settings": {"executionOrder": "v1"},
  "pinData": {}
}
