{
  "name": "知识库自动更新工作流",
  "nodes": [
    {
      "parameters": {
        "url": "https://example.com/blog/feed.xml",
        "options": {}
      },
      "name": "RSS订阅源",
      "type": "n8n-nodes-base.rssFeedRead",
      "typeVersion": 1,
      "position": [0, 0]
    },
    {
      "parameters": {
        "url": "={{$json.link}}",
        "method": "GET",
        "responseFormat": "text",
        "options": {}
      },
      "name": "抓取正文",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [220, 0]
    },
    {
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const item = $input.first().json;\nconst title = (item.title || '').toString().trim();\nconst url = (item.link || item.url || '').toString().trim();\nconst body = (item.content || item.body || '').toString();\n// 按 URL+标题做 hash 去重键\nconst key = url + '|' + title;\nlet h = 0;\nfor (let i = 0; i < key.length; i++) h = ((h << 5) - h + key.charCodeAt(i)) | 0;\nconst hash = 'kb_' + Math.abs(h).toString(36);\n// 截断超长正文，避免超 LLM 上下文\nconst content = body.slice(0, 8000);\nreturn { json: { title, url, content, hash, source: 'rss' } };"
      },
      "name": "去重hash",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [440, 0]
    },
    {
      "parameters": {
        "url": "=YOUR_KB_LOOKUP_API?hash={{$json.hash}}",
        "method": "GET",
        "responseFormat": "json",
        "options": {}
      },
      "name": "查已入库",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [660, 0]
    },
    {
      "parameters": {
        "conditions": {
          "boolean": [
            { "value1": "={{$json.exists}}", "value2": true }
          ]
        }
      },
      "name": "IF命中跳过",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [880, 0]
    },
    {
      "parameters": {
        "url": "https://api.moonshot.cn/v1/chat/completions",
        "method": "POST",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            { "name": "Authorization", "value": "Bearer YOUR_LLM_API_KEY" },
            { "name": "Content-Type", "value": "application/json" }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\"model\":\"moonshot-v1-8k\",\"messages\":[{\"role\":\"system\",\"content\":\"你是知识库文档分析助手。读文档正文输出JSON：title(标题)、summary(150字内摘要)、keywords(3-5个关键词数组)、category(产品文档/技术文档/运维手册/FAQ/公告/其他 六选一)、tags(1-3个标签)、language(zh/en)。不编造正文之外的事实，仅基于原文判断。\"},{\"role\":\"user\",\"content\":\"={{$json.content}}\"}]}",
        "options": {}
      },
      "name": "LLM摘要分类",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [1100, 0]
    },
    {
      "parameters": {
        "url": "YOUR_VECTOR_DB_UPSERT_API",
        "method": "POST",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            { "name": "Authorization", "value": "Bearer YOUR_VECTOR_DB_API_KEY" },
            { "name": "Content-Type", "value": "application/json" }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\"id\":\"{{$node[\\\"去重hash\\\"].json.hash}}\",\"content\":\"{{$node[\\\"去重hash\\\"].json.content}}\",\"metadata\":{\"title\":\"{{$node[\\\"去重hash\\\"].json.title}}\",\"url\":\"{{$node[\\\"去重hash\\\"].json.url}}\",\"summary\":\"{{JSON.parse($json.choices[0].message.content).summary}}\",\"keywords\":\"{{JSON.parse($json.choices[0].message.content).keywords}}\",\"category\":\"{{JSON.parse($json.choices[0].message.content).category}}\",\"source\":\"{{$node[\\\"去重hash\\\"].json.source}}\",\"updated_at\":\"{{$now}}\"}}",
        "options": {}
      },
      "name": "向量化入库",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [1320, 0]
    },
    {
      "parameters": {
        "url": "YOUR_SLACK_OR_FEISHU_WEBHOOK",
        "method": "POST",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\"text\":\"知识库已更新 1 条\\n标题:{{$node[\\\"去重hash\\\"].json.title}}\\n分类:{{JSON.parse($node[\\\"LLM摘要分类\\\"].json.choices[0].message.content).category}}\\n来源:{{$node[\\\"去重hash\\\"].json.url}}\"}",
        "options": {}
      },
      "name": "通知",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [1540, 0]
    }
  ],
  "connections": {
    "RSS订阅源": { "main": [ [{ "node": "抓取正文", "type": "main", "index": 0 }] ] },
    "抓取正文": { "main": [ [{ "node": "去重hash", "type": "main", "index": 0 }] ] },
    "去重hash": { "main": [ [{ "node": "查已入库", "type": "main", "index": 0 }] ] },
    "查已入库": { "main": [ [{ "node": "IF命中跳过", "type": "main", "index": 0 }] ] },
    "IF命中跳过": { "main": [ [], [{ "node": "LLM摘要分类", "type": "main", "index": 0 }] ] },
    "LLM摘要分类": { "main": [ [{ "node": "向量化入库", "type": "main", "index": 0 }] ] },
    "向量化入库": { "main": [ [{ "node": "通知", "type": "main", "index": 0 }] ] }
  }
}
