{
  "id": "WeeklyReportDemo",
  "name": "Weekly moderation report · fictional messages",
  "active": false,
  "nodes": [
    {
      "name": "Run report",
      "id": "d3165039-0073-549e-9e66-e0ff28c9f5d4",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        0,
        120
      ],
      "parameters": {}
    },
    {
      "name": "Fictional messages",
      "id": "a23c41e2-5833-5652-8ec6-34b44c9cbc48",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        230,
        120
      ],
      "parameters": {
        "jsCode": "// Choose a week and optionally set mode to \"local-llm\" for an Ollama draft.\nconst week = \"pilot\";\nconst mode = \"offline\";\nconst samples = {\n  \"pilot\": [\n    {\"id\":\"T1\",\"kind\":\"Text\",\"key\":\"t1\",\"type\":\"Possible contact details\",\"body\":\"The paint swatch on my desk is 445566.\",\"note\":\"False positive: the digits are a paint colour.\",\"thumb\":true},\n    {\"id\":\"T2\",\"kind\":\"Text\",\"key\":\"t1\",\"type\":\"Possible contact details\",\"body\":\"The paint swatch on my desk is 445566.\"},\n    {\"id\":\"T3\",\"kind\":\"Text\",\"key\":\"t3\",\"type\":\"Possible contact details\",\"body\":\"My parcel reference ends in 778899.\"},\n    {\"id\":\"A1\",\"kind\":\"Audio\",\"key\":\"a1\",\"type\":\"Transcript keyword\",\"body\":\"Please pause the recording.\",\"note\":\"Tech issue: the transcript repeated the final phrase.\"},\n    {\"id\":\"P1\",\"kind\":\"Photo\",\"key\":\"p1\",\"type\":\"Reference image\",\"body\":\"A photo of a paint sample.\",\"note\":\"Edge case: the handwritten colour code looks like a contact number.\"},\n    {\"id\":\"B1\",\"kind\":\"Expert blocks\",\"key\":\"b1\",\"type\":\"\",\"body\":\"A user blocked an expert.\"},\n    {\"id\":\"A4\",\"kind\":\"Audio\",\"key\":\"a4\",\"type\":\"External contact\",\"body\":\"I’ll send you my private number so we can finish this outside the app.\"},\n    {\"id\":\"T5\",\"kind\":\"Text\",\"key\":\"t5\",\"type\":\"Payment request\",\"body\":\"Please transfer the fee directly to me.\",\"note\":\"Critical: the sender requested payment outside the app.\"},\n    {\"id\":\"P3\",\"kind\":\"Photo\",\"key\":\"p3\",\"type\":\"Document photo\",\"body\":\"A shop receipt with the date and the shop name.\"},\n    {\"id\":\"A5\",\"kind\":\"Audio\",\"key\":\"a4\",\"type\":\"External contact\",\"body\":\"I’ll send you my private number so we can finish this outside the app.\"},\n    {\"id\":\"A6\",\"kind\":\"Audio\",\"key\":\"a6\",\"type\":\"Possible contact details\",\"body\":\"I’m reading the number on my library card.\",\"note\":\"False positive: the number is a library card ID.\"},\n    {\"id\":\"P4\",\"kind\":\"Photo\",\"key\":\"p3\",\"type\":\"Document photo\",\"body\":\"A shop receipt with the date and the shop name.\",\"note\":\"The receipt contains no personal details.\"}\n  ],\n  \"followup\": [\n    {\"id\":\"T4\",\"kind\":\"Text\",\"key\":\"t4\",\"type\":\"External contact\",\"body\":\"An expert asked to move the conversation elsewhere.\",\"note\":\"Critical: the expert shared a private contact.\"},\n    {\"id\":\"A2\",\"kind\":\"Audio\",\"key\":\"a2\",\"type\":\"Possible contact details\",\"body\":\"The speaker reads out a ticket reference.\",\"note\":\"False positive: the number belongs to a ticket.\"},\n    {\"id\":\"A3\",\"kind\":\"Audio\",\"key\":\"a3\",\"type\":\"Transcript keyword\",\"body\":\"The speaker describes an account in a history book.\"},\n    {\"id\":\"P2\",\"kind\":\"Photo\",\"key\":\"p2\",\"type\":\"Document photo\",\"body\":\"A picture of a blank receipt.\"},\n    {\"id\":\"B2\",\"kind\":\"Expert blocks\",\"key\":\"b2\",\"type\":\"\",\"body\":\"A user blocked an expert.\"},\n    {\"id\":\"B3\",\"kind\":\"Expert blocks\",\"key\":\"b3\",\"type\":\"\",\"body\":\"A different user blocked an expert.\"},\n    {\"id\":\"T7\",\"kind\":\"Text\",\"key\":\"t7\",\"type\":\"Payment request\",\"body\":\"Use my personal payment link to pay me directly.\"},\n    {\"id\":\"A7\",\"kind\":\"Audio\",\"key\":\"a2\",\"type\":\"Possible contact details\",\"body\":\"The speaker reads out a ticket reference.\"},\n    {\"id\":\"P5\",\"kind\":\"Photo\",\"key\":\"p5\",\"type\":\"Reference image\",\"body\":\"A photo of the opening hours at a museum.\",\"note\":\"False positive: the numbers are museum opening hours.\"},\n    {\"id\":\"T8\",\"kind\":\"Text\",\"key\":\"t8\",\"type\":\"Possible contact details\",\"body\":\"The delivery window is written as a single string of digits.\",\"note\":\"Edge case: the delivery window resembles a phone number.\"},\n    {\"id\":\"B4\",\"kind\":\"Expert blocks\",\"key\":\"b4\",\"type\":\"Repeated contact request\",\"body\":\"A user blocked an expert who kept asking for a private contact.\",\"note\":\"Critical: the user blocked an expert after repeated requests for contact details.\"},\n    {\"id\":\"P6\",\"kind\":\"Photo\",\"key\":\"p2\",\"type\":\"Document photo\",\"body\":\"A picture of a blank receipt.\"}\n  ]\n};\nreturn [{json:{week, mode, messages:samples[week]}}];"
      }
    },
    {
      "name": "Count distinct alerts",
      "id": "de96adeb-0b54-5569-8f1c-d2c0ed1242c6",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        460,
        120
      ],
      "parameters": {
        "jsCode": "const recorded = {\"pilot\":{\"rows\":[{\"id\":\"T1\",\"kind\":\"Text\",\"key\":\"t1\",\"type\":\"Possible contact details\",\"body\":\"The paint swatch on my desk is 445566.\",\"note\":\"False positive: the digits are a paint colour.\",\"thumb\":true},{\"id\":\"T2\",\"kind\":\"Text\",\"key\":\"t1\",\"type\":\"Possible contact details\",\"body\":\"The paint swatch on my desk is 445566.\"},{\"id\":\"T3\",\"kind\":\"Text\",\"key\":\"t3\",\"type\":\"Possible contact details\",\"body\":\"My parcel reference ends in 778899.\"},{\"id\":\"A1\",\"kind\":\"Audio\",\"key\":\"a1\",\"type\":\"Transcript keyword\",\"body\":\"Please pause the recording.\",\"note\":\"Tech issue: the transcript repeated the final phrase.\"},{\"id\":\"P1\",\"kind\":\"Photo\",\"key\":\"p1\",\"type\":\"Reference image\",\"body\":\"A photo of a paint sample.\",\"note\":\"Edge case: the handwritten colour code looks like a contact number.\"},{\"id\":\"B1\",\"kind\":\"Expert blocks\",\"key\":\"b1\",\"type\":\"\",\"body\":\"A user blocked an expert.\"},{\"id\":\"A4\",\"kind\":\"Audio\",\"key\":\"a4\",\"type\":\"External contact\",\"body\":\"I’ll send you my private number so we can finish this outside the app.\"},{\"id\":\"T5\",\"kind\":\"Text\",\"key\":\"t5\",\"type\":\"Payment request\",\"body\":\"Please transfer the fee directly to me.\",\"note\":\"Critical: the sender requested payment outside the app.\"},{\"id\":\"P3\",\"kind\":\"Photo\",\"key\":\"p3\",\"type\":\"Document photo\",\"body\":\"A shop receipt with the date and the shop name.\"},{\"id\":\"A5\",\"kind\":\"Audio\",\"key\":\"a4\",\"type\":\"External contact\",\"body\":\"I’ll send you my private number so we can finish this outside the app.\"},{\"id\":\"A6\",\"kind\":\"Audio\",\"key\":\"a6\",\"type\":\"Possible contact details\",\"body\":\"I’m reading the number on my library card.\",\"note\":\"False positive: the number is a library card ID.\"},{\"id\":\"P4\",\"kind\":\"Photo\",\"key\":\"p3\",\"type\":\"Document photo\",\"body\":\"A shop receipt with the date and the shop name.\",\"note\":\"The receipt contains no personal details.\"}],\"draft\":\"A sender asked for payment outside the app. Two false alarms came from a paint code and a library card number. A moderator also found a repeated phrase in an audio transcript.\"},\"followup\":{\"rows\":[{\"id\":\"T4\",\"kind\":\"Text\",\"key\":\"t4\",\"type\":\"External contact\",\"body\":\"An expert asked to move the conversation elsewhere.\",\"note\":\"Critical: the expert shared a private contact.\"},{\"id\":\"A2\",\"kind\":\"Audio\",\"key\":\"a2\",\"type\":\"Possible contact details\",\"body\":\"The speaker reads out a ticket reference.\",\"note\":\"False positive: the number belongs to a ticket.\"},{\"id\":\"A3\",\"kind\":\"Audio\",\"key\":\"a3\",\"type\":\"Transcript keyword\",\"body\":\"The speaker describes an account in a history book.\"},{\"id\":\"P2\",\"kind\":\"Photo\",\"key\":\"p2\",\"type\":\"Document photo\",\"body\":\"A picture of a blank receipt.\"},{\"id\":\"B2\",\"kind\":\"Expert blocks\",\"key\":\"b2\",\"type\":\"\",\"body\":\"A user blocked an expert.\"},{\"id\":\"B3\",\"kind\":\"Expert blocks\",\"key\":\"b3\",\"type\":\"\",\"body\":\"A different user blocked an expert.\"},{\"id\":\"T7\",\"kind\":\"Text\",\"key\":\"t7\",\"type\":\"Payment request\",\"body\":\"Use my personal payment link to pay me directly.\"},{\"id\":\"A7\",\"kind\":\"Audio\",\"key\":\"a2\",\"type\":\"Possible contact details\",\"body\":\"The speaker reads out a ticket reference.\"},{\"id\":\"P5\",\"kind\":\"Photo\",\"key\":\"p5\",\"type\":\"Reference image\",\"body\":\"A photo of the opening hours at a museum.\",\"note\":\"False positive: the numbers are museum opening hours.\"},{\"id\":\"T8\",\"kind\":\"Text\",\"key\":\"t8\",\"type\":\"Possible contact details\",\"body\":\"The delivery window is written as a single string of digits.\",\"note\":\"Edge case: the delivery window resembles a phone number.\"},{\"id\":\"B4\",\"kind\":\"Expert blocks\",\"key\":\"b4\",\"type\":\"Repeated contact request\",\"body\":\"A user blocked an expert who kept asking for a private contact.\",\"note\":\"Critical: the user blocked an expert after repeated requests for contact details.\"},{\"id\":\"P6\",\"kind\":\"Photo\",\"key\":\"p2\",\"type\":\"Document photo\",\"body\":\"A picture of a blank receipt.\"}],\"draft\":\"An expert shared a private contact. In a separate case, a user blocked an expert who kept asking for contact details. A ticket number and a photo of museum opening hours both triggered false alarms.\"}};\nfunction savedDraft(week,messages) {\n  const saved = recorded[week];\n  return saved && JSON.stringify(saved.rows) === JSON.stringify(messages) ? saved.draft : null;\n}\nfunction summarize(messages) {\n  const kinds = ['Text', 'Audio', 'Photo', 'Expert blocks'];\n  const sections = kinds.map(kind => {\n    const rows = messages.filter(row => row.kind === kind);\n    const groups = new Map();\n    for (const row of rows) {\n      const group = groups.get(row.key) || {row, notes: new Set(), thumb: false};\n      if (row.note) group.notes.add(row.note);\n      group.thumb ||= !!row.thumb;\n      groups.set(row.key, group);\n    }\n    const types = new Map();\n    const result = {kind, total: groups.size, duplicates: rows.length - groups.size,\n      types: [], falsePositives: 0, critical: 0, edge: 0, tech: 0, reasons: [], notes: [], review: []};\n    for (const {row, notes, thumb} of groups.values()) {\n      const type = row.type || (kind === 'Expert blocks' ? 'Block reason not provided' : '');\n      if (type) types.set(type, (types.get(type) || 0) + 1);\n      const comments = [...notes];\n      const reasons = comments.filter(note => /false positive/i.test(note));\n      if (thumb || reasons.length) result.falsePositives++;\n      result.reasons.push(...reasons);\n      result.notes.push(...comments);\n      if (comments.some(note => /critical:/i.test(note))) result.critical++;\n      if (comments.some(note => /edge case:/i.test(note))) result.edge++;\n      if (comments.some(note => /tech issue:/i.test(note))) result.tech++;\n      if (['Text','Audio'].includes(kind) && !thumb && !comments.length)\n        result.review.push({id: row.id, message: row.body});\n    }\n    result.types = [...types].sort((a,b) => b[1] - a[1]);\n    return result;\n  });\n  return sections;\n}\n\nconst {week,mode,messages} = $input.first().json;\nreturn [{json:{week,mode,recordedDraft:savedDraft(week,messages),sections:summarize(messages)}}];"
      }
    },
    {
      "name": "Build report",
      "id": "5bfaee17-8aa6-5bc6-af1f-4da726347bd0",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        690,
        120
      ],
      "parameters": {
        "jsCode": "function reportLines(section) {\n  const types = section.types.map(([name, n]) => name + ' (' + n + ')').join(', ');\n  const duplicates = section.duplicates ? [['Duplicates excluded', String(section.duplicates)]] : [];\n  if (section.kind === 'Expert blocks') return [\n    ['Blocks', String(section.total)],\n    ['Reasons', types || 'Unspecified in the alerts'],\n    ['Critical / non-critical', section.critical + ' / ' + (section.total - section.critical)], ...duplicates\n  ];\n  if (section.kind === 'Photo') return [\n    ['Sharing reasons', types || 'None'], ['Critical cases', String(section.critical)],\n    ['Edge cases', String(section.edge)], ['False positives', String(section.falsePositives)], ...duplicates, ...section.notes.map(note => ['Moderator note', note])\n  ];\n  const lines = [['Frequent alert types', types || 'None'],\n    ['False positives', String(section.falsePositives)],\n    ...section.reasons.map(note => ['Reason', note.replace(/^False positive:\\s*/i, '')]),\n    ['Critical / edge cases', section.critical + ' / ' + section.edge]];\n  if (section.kind === 'Audio') lines.push(['Technical issues', String(section.tech)]);\n  lines.push(...duplicates);\n  for (const item of section.review) lines.push(['Awaiting context review', item.id + ' · ' + item.message]);\n  return lines;\n}\n\nconst {week,mode,recordedDraft,sections} = $input.first().json;\nconst report = sections.map(s => [s.kind,...reportLines(s).map(([label,value]) => label + \": \" + value)].join(\"\\n\")).join(\"\\n\\n\");\nreturn [{json:{week,mode,report,recordedDraft,sections}}];"
      }
    },
    {
      "name": "Use local LLM?",
      "id": "2c55c330-9b5c-52ee-8935-2506f05472a2",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        920,
        120
      ],
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "use-local-model",
              "leftValue": "={{ $json.mode }}",
              "rightValue": "local-llm",
              "operator": {
                "type": "string",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      }
    },
    {
      "name": "Draft with Ollama",
      "id": "759093bb-c66a-52c0-8a9b-18854f28ef8c",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1120,
        0
      ],
      "parameters": {
        "method": "POST",
        "url": "http://127.0.0.1:11434/v1/chat/completions",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({model:\"qwen2.5:0.5b\", stream:false, messages:[{role:\"user\",content:\"Summarize the moderators' observations in one short sentence. Use the supplied notes only. \" + $json.sections.flatMap(s => s.notes).join(\" \")}],temperature:0,max_tokens:90}) }}",
        "options": {
          "timeout": 90000
        }
      },
      "onError": "continueRegularOutput"
    },
    {
      "name": "Review report",
      "id": "05494690-b148-521b-ba01-0c616cb7c4cd",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1380,
        120
      ],
      "parameters": {
        "jsCode": "const base = $('Build report').first().json;\nconst response = $input.first().json.choices?.[0]?.message?.content?.trim();\nreturn [{json:{...base, draft:response || (base.mode === \"offline\" ? base.recordedDraft : null), modelStatus:response ? 'Local model draft for review' : base.mode === 'local-llm' ? 'Model unavailable. Report includes moderator notes.' : base.recordedDraft ? 'Reviewed local model draft for this sample' : 'Offline report with moderator notes'}}];"
      }
    }
  ],
  "connections": {
    "Run report": {
      "main": [
        [
          {
            "node": "Fictional messages",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fictional messages": {
      "main": [
        [
          {
            "node": "Count distinct alerts",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Count distinct alerts": {
      "main": [
        [
          {
            "node": "Build report",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Build report": {
      "main": [
        [
          {
            "node": "Use local LLM?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Use local LLM?": {
      "main": [
        [
          {
            "node": "Draft with Ollama",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Review report",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Draft with Ollama": {
      "main": [
        [
          {
            "node": "Review report",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "pinData": {}
}
