{
  "name": "AI HQ \u2014 Chat Trigger (web-\u0432\u0445\u043e\u0434)",
  "nodes": [
    {
      "parameters": {
        "public": true,
        "mode": "hostedChat",
        "initialMessages": "\ud83d\udc4b \u041f\u0440\u0438\u0432\u0435\u0442! \u042f \u2014 \u0418\u0418-\u0448\u0442\u0430\u0431-\u043a\u0432\u0430\u0440\u0442\u0438\u0440\u0430 AI HQ.\n\n\u041a\u043e\u043c\u0430\u043d\u0434\u044b:\n\u2022 /project owner/repo \u2014 \u043f\u0435\u0440\u0435\u043a\u043b\u044e\u0447\u0438\u0442\u044c\u0441\u044f \u043d\u0430 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439\n\u2022 /new \u0438\u043c\u044f \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u2014 \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u043f\u0440\u043e\u0435\u043a\u0442\n\u2022 \u041b\u044e\u0431\u043e\u0439 \u0442\u0435\u043a\u0441\u0442 \u2014 \u0437\u0430\u0434\u0430\u0447\u0430 \u0434\u043b\u044f \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u043f\u0440\u043e\u0435\u043a\u0442\u0430\n\n\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u043f\u0440\u0438\u0434\u0451\u0442 \u0432 Telegram.",
        "options": {},
        "webhookId": "ai-hq-chat"
      },
      "id": "aa1bb2cc-dd3e-4f56-7890-aabbccddeef0",
      "name": "Chat Trigger",
      "type": "@n8n/n8n-nodes-langchain.chatTrigger",
      "typeVersion": 1,
      "position": [
        240,
        300
      ]
    },
    {
      "parameters": {
        "jsCode": "const input = $input.first().json;\nconst userText = (input.chatInput || input.message || '').trim();\nconst sessionId = input.sessionId || 'anonymous';\n// Extract Telegram chat_id from URL query param (passed by bot)\nconst tgChatId = (input.query?.chatId || '').toString().trim();\nconst chatId = (tgChatId && /^\\d+$/.test(tgChatId)) ? tgChatId : ('web-' + sessionId);\n\nconst store = $getWorkflowStaticData('global');\nstore.sessions = store.sessions || {};\nconst session = store.sessions[sessionId] || {};\n\nlet shouldForward = true;\nlet localReply = null;\nlet payload = null;\n\nif (userText.startsWith('/project ')) {\n  const repo = userText.replace('/project', '').trim();\n  if (!/^[\\w.-]+\\/[\\w.-]+$/.test(repo)) {\n    localReply = '\u041d\u0435\u0432\u0435\u0440\u043d\u044b\u0439 \u0444\u043e\u0440\u043c\u0430\u0442. \u041f\u0440\u0438\u043c\u0435\u0440: /project owner/repo';\n  } else {\n    session.repo_path = repo;\n    store.sessions[sessionId] = session;\n    localReply = '\u0410\u043a\u0442\u0438\u0432\u043d\u044b\u0439 \u043f\u0440\u043e\u0435\u043a\u0442: ' + repo;\n  }\n  shouldForward = false;\n} else if (userText.startsWith('/new ')) {\n  const rest = userText.replace('/new', '').trim();\n  const parts = rest.split(/\\s+/);\n  const repo_name = parts.shift();\n  const description = parts.join(' ') || repo_name;\n  if (!repo_name) {\n    localReply = '\u041d\u0435\u0432\u0435\u0440\u043d\u044b\u0439 \u0444\u043e\u0440\u043c\u0430\u0442. \u041f\u0440\u0438\u043c\u0435\u0440: /new my_app \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0435';\n    shouldForward = false;\n  } else {\n    payload = { action: 'create_project', chat_id: chatId, repo_name: repo_name, description: description };\n  }\n} else {\n  const repo_path = session.repo_path;\n  if (!repo_path) {\n    localReply = '\u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u0432\u044b\u0431\u0435\u0440\u0438\u0442\u0435 \u043f\u0440\u043e\u0435\u043a\u0442: /project owner/repo';\n    shouldForward = false;\n  } else {\n    payload = { action: 'develop_feature', chat_id: chatId, repo_path: repo_path, task: userText, task_id: Date.now() };\n  }\n}\n\nreturn { json: { shouldForward: shouldForward, localReply: localReply, payload: payload } };"
      },
      "id": "cc3dd4ee-ff5a-4678-9012-ccddeeff0012",
      "name": "\u041f\u0430\u0440\u0441\u0438\u043d\u0433 \u0438 \u0441\u0435\u0441\u0441\u0438\u044f",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        460,
        300
      ]
    },
    {
      "parameters": {
        "value1": "={{ $json.shouldForward }}",
        "operation": "equal",
        "value2": "true"
      },
      "id": "dd4ee5ff-aa6b-4789-0123-ddeeff001123",
      "name": "\u041d\u0443\u0436\u0435\u043d \u0444\u043e\u0440\u0432\u0430\u0440\u0434?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        680,
        300
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "={{ $env.MAIN_WORKFLOW_WEBHOOK_URL || 'http://localhost:5678/webhook/ai-hq' }}",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ $json.payload }}",
        "options": {
          "timeout": 10000
        },
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        }
      },
      "id": "ee5ff6aa-bb7c-4890-1234-eeff00112234",
      "name": "\u0424\u043e\u0440\u0432\u0430\u0440\u0434 \u0432 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 workflow",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 3,
      "position": [
        900,
        220
      ],
      "continueOnFail": true
    },
    {
      "parameters": {
        "jsCode": "const origRow = $('\u041f\u0430\u0440\u0441\u0438\u043d\u0433 \u0438 \u0441\u0435\u0441\u0441\u0438\u044f').first().json;\nconst payload = origRow.payload || {};\nconst forwarded = $input.first().json;\nlet reply;\nif (forwarded.error) {\n  reply = '\u041d\u0435 \u0443\u0434\u0430\u043b\u043e\u0441\u044c \u0441\u0432\u044f\u0437\u0430\u0442\u044c\u0441\u044f \u0441 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u043c workflow. \u041f\u0440\u043e\u0432\u0435\u0440\u044c\u0442\u0435 \u0447\u0442\u043e \u043e\u043d \u0430\u043a\u0442\u0438\u0432\u0438\u0440\u043e\u0432\u0430\u043d.';\n} else if (payload.action === 'create_project') {\n  reply = '\u0417\u0430\u043f\u0443\u0441\u043a\u0430\u044e \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043f\u0440\u043e\u0435\u043a\u0442\u0430 ' + payload.repo_name + '. \u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u043f\u0440\u0438\u0434\u0451\u0442 \u0432 Telegram.';\n} else {\n  reply = '\u0417\u0430\u0434\u0430\u0447\u0430 \u043f\u0440\u0438\u043d\u044f\u0442\u0430 \u0434\u043b\u044f ' + payload.repo_path + '. \u041f\u0440\u0435\u0432\u044c\u044e \u043f\u0440\u0438\u0434\u0451\u0442 \u0432 Telegram.';\n}\nreturn { json: { output: reply } };"
      },
      "id": "ff6aa7bb-cc8d-4901-2345-ff0011223345",
      "name": "\u041e\u0442\u0432\u0435\u0442 \u043f\u043e\u0441\u043b\u0435 \u0444\u043e\u0440\u0432\u0430\u0440\u0434\u0430",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1120,
        220
      ]
    },
    {
      "parameters": {
        "jsCode": "const origRow = $('\u041f\u0430\u0440\u0441\u0438\u043d\u0433 \u0438 \u0441\u0435\u0441\u0441\u0438\u044f').first().json;\nreturn { json: { output: origRow.localReply || 'OK' } };"
      },
      "id": "aa7bb8cc-dd9e-4012-3456-001122334456",
      "name": "\u041b\u043e\u043a\u0430\u043b\u044c\u043d\u044b\u0439 \u043e\u0442\u0432\u0435\u0442",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        900,
        380
      ]
    }
  ],
  "connections": {
    "Chat Trigger": {
      "main": [
        [
          {
            "node": "\u041f\u0430\u0440\u0441\u0438\u043d\u0433 \u0438 \u0441\u0435\u0441\u0441\u0438\u044f",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "\u041f\u0430\u0440\u0441\u0438\u043d\u0433 \u0438 \u0441\u0435\u0441\u0441\u0438\u044f": {
      "main": [
        [
          {
            "node": "\u041d\u0443\u0436\u0435\u043d \u0444\u043e\u0440\u0432\u0430\u0440\u0434?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "\u041d\u0443\u0436\u0435\u043d \u0444\u043e\u0440\u0432\u0430\u0440\u0434?": {
      "main": [
        [
          {
            "node": "\u0424\u043e\u0440\u0432\u0430\u0440\u0434 \u0432 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 workflow",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "\u041b\u043e\u043a\u0430\u043b\u044c\u043d\u044b\u0439 \u043e\u0442\u0432\u0435\u0442",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "\u0424\u043e\u0440\u0432\u0430\u0440\u0434 \u0432 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 workflow": {
      "main": [
        [
          {
            "node": "\u041e\u0442\u0432\u0435\u0442 \u043f\u043e\u0441\u043b\u0435 \u0444\u043e\u0440\u0432\u0430\u0440\u0434\u0430",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "bb2cc3dd-ee4f-4568-9012-bbccddeeff02",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "tags": []
}