{
  "name": "Codex CLI Webhook Chat",
  "nodes": [
    {
      "parameters": {
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.chatTrigger",
      "typeVersion": 1.4,
      "position": [
        0,
        300
      ],
      "id": "chat-trigger-1",
      "name": "When chat message received"
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://example.com/codex",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "content-type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"prompt\": \"{{$json.chatInput}}\"\n}",
        "options": {
          "timeout": 600000
        }
      },
      "id": "http-1",
      "name": "Send Message to AI Endpoint",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.4,
      "position": [
        320,
        300
      ]
    },
    {
      "parameters": {
        "jsCode": "return items.map(({ json }) => {\n  const raw = String(json.data ?? json.ndjson ?? json.body ?? '');\n  const normalized = raw.replace(/\\\\n/g, '\\n');\n  let answer = '';\n\n  for (const line of normalized.split(/\\r?\\n/)) {\n    try {\n      const event = JSON.parse(line.trim());\n      if (event.type === 'item.completed' && event.item?.type === 'agent_message') {\n        answer = event.item.text;\n        break;\n      }\n    } catch {}\n  }\n\n  if (!answer) {\n    const matches = [...raw.matchAll(/\\\"type\\\":\\\"agent_message\\\",\\\"text\\\":\\\"((?:\\\\.|[^\\\"\\\\])*)\\\"/g)];\n    if (matches.length) answer = JSON.parse(`\\\"${matches.at(-1)[1]}\\\"`);\n  }\n\n  if (!answer) throw new Error('No agent answer found.');\n  return { json: { output: answer } };\n});"
      },
      "id": "code-1",
      "name": "Extract Agent Answer",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        640,
        300
      ]
    }
  ],
  "connections": {
    "When chat message received": {
      "main": [
        [
          {
            "node": "Send Message to AI Endpoint",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send Message to AI Endpoint": {
      "main": [
        [
          {
            "node": "Extract Agent Answer",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "meta": {
    "templateCredsSetupCompleted": false
  }
}