{
"event": "PreToolUse",
"tool_name": "Write",
"tool_input": {
"file_path": "\/var\/www\/dev.campus.systemische-tools.de\/crawler\/src\/infrastructure\/fetchers\/web_vitals_collector.py",
"content": "\"\"\"Collects Core Web Vitals via PerformanceObserver injected into the page.\"\"\"\n\nfrom __future__ import annotations\n\nfrom typing import Any\n\nINIT_SCRIPT = \"\"\"\nwindow.__webVitals = { lcp: null, cls: 0, tbt: 0 };\ntry {\n new PerformanceObserver((list) => {\n const last = list.getEntries().at(-1);\n if (last) window.__webVitals.lcp = Math.round(last.startTime);\n }).observe({ type: 'largest-contentful-paint', buffered: true });\n} catch (_) {}\ntry {\n new PerformanceObserver((list) => {\n for (const e of list.getEntries()) {\n if (!e.hadRecentInput) window.__webVitals.cls += e.value;\n }\n }).observe({ type: 'layout-shift', buffered: true });\n} catch (_) {}\ntry {\n new PerformanceObserver((list) => {\n for (const e of list.getEntries()) {\n window.__webVitals.tbt += Math.max(0, e.duration - 50);\n }\n }).observe({ type: 'longtask', buffered: true });\n} catch (_) {}\n\"\"\"\n\nREAD_SCRIPT = \"window.__webVitals || { lcp: null, cls: null, tbt: null }\"\n\n\ndef parse_vitals(raw: Any) -> dict[str, float | int | None]:\n if not isinstance(raw, dict):\n return {\"lcp_ms\": None, \"cls\": None, \"tbt_ms\": None}\n return {\n \"lcp_ms\": _opt_int(raw.get(\"lcp\")),\n \"cls\": _opt_float(raw.get(\"cls\")),\n \"tbt_ms\": _opt_int(raw.get(\"tbt\")),\n }\n\n\ndef _opt_int(value: object) -> int | None:\n if value is None:\n return None\n try:\n return int(value)\n except (TypeError, ValueError):\n return None\n\n\ndef _opt_float(value: object) -> float | None:\n if value is None:\n return None\n try:\n return round(float(value), 4)\n except (TypeError, ValueError):\n return None\n"
}
}
{
"tool_response": {
"type": "create",
"filePath": "\/var\/www\/dev.campus.systemische-tools.de\/crawler\/src\/infrastructure\/fetchers\/web_vitals_collector.py",
"content": "\"\"\"Collects Core Web Vitals via PerformanceObserver injected into the page.\"\"\"\n\nfrom __future__ import annotations\n\nfrom typing import Any\n\nINIT_SCRIPT = \"\"\"\nwindow.__webVitals = { lcp: null, cls: 0, tbt: 0 };\ntry {\n new PerformanceObserver((list) => {\n const last = list.getEntries().at(-1);\n if (last) window.__webVitals.lcp = Math.round(last.startTime);\n }).observe({ type: 'largest-contentful-paint', buffered: true });\n} catch (_) {}\ntry {\n new PerformanceObserver((list) => {\n for (const e of list.getEntries()) {\n if (!e.hadRecentInput) window.__webVitals.cls += e.value;\n }\n }).observe({ type: 'layout-shift', buffered: true });\n} catch (_) {}\ntry {\n new PerformanceObserver((list) => {\n for (const e of list.getEntries()) {\n window.__webVitals.tbt += Math.max(0, e.duration - 50);\n }\n }).observe({ type: 'longtask', buffered: true });\n} catch (_) {}\n\"\"\"\n\nREAD_SCRIPT = \"window.__webVitals || { lcp: null, cls: null, tbt: null }\"\n\n\ndef parse_vitals(raw: Any) -> dict[str, float | int | None]:\n if not isinstance(raw, dict):\n return {\"lcp_ms\": None, \"cls\": None, \"tbt_ms\": None}\n return {\n \"lcp_ms\": _opt_int(raw.get(\"lcp\")),\n \"cls\": _opt_float(raw.get(\"cls\")),\n \"tbt_ms\": _opt_int(raw.get(\"tbt\")),\n }\n\n\ndef _opt_int(value: object) -> int | None:\n if value is None:\n return None\n try:\n return int(value)\n except (TypeError, ValueError):\n return None\n\n\ndef _opt_float(value: object) -> float | None:\n if value is None:\n return None\n try:\n return round(float(value), 4)\n except (TypeError, ValueError):\n return None\n",
"structuredPatch": [],
"originalFile": null,
"userModified": false
}
}