362 lines
18 KiB
HTML
362 lines
18 KiB
HTML
<!doctype html>
|
||
<html>
|
||
<head>
|
||
<meta charset="utf-8" />
|
||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||
<title>Environment</title>
|
||
<style>
|
||
[hidden] { display: none !important; }
|
||
html, body { margin: 0; background: hsl(var(--background, 0 0% 100%)); color: hsl(var(--foreground, 222 47% 11%)); }
|
||
.wrap { display: flex; flex-direction: column; gap: 24px; }
|
||
.filters { justify-content: flex-end; margin-bottom: 0; }
|
||
.grid3 { display: grid; grid-template-columns: 1fr; gap: 12px; }
|
||
.grid2 { display: grid; grid-template-columns: 1fr; gap: 16px; }
|
||
@media (min-width: 640px) { .grid3 { grid-template-columns: repeat(3, 1fr); } }
|
||
@media (min-width: 768px) { .grid2 { grid-template-columns: repeat(2, 1fr); } }
|
||
/* the three headline figures, each in its own colour */
|
||
.head { border-radius: var(--aia-r, 12px); border: 1px solid; padding: 16px; }
|
||
.head .lbl { display: flex; align-items: center; gap: 6px; font-size: 11px; font-weight: 500; text-transform: uppercase; letter-spacing: .04em; }
|
||
.head .val { margin-top: 6px; font: 700 24px/1.2 var(--aia-mono, ui-monospace, monospace); font-variant-numeric: tabular-nums; }
|
||
.head .sub { font-size: 11px; }
|
||
.c-carbon { color: hsl(160 84% 39%); } .b-carbon { border-color: hsl(160 84% 39% / .3); background: hsl(160 84% 39% / .05); }
|
||
.c-water { color: hsl(199 89% 48%); } .b-water { border-color: hsl(199 89% 48% / .3); background: hsl(199 89% 48% / .05); }
|
||
.c-energy { color: hsl(38 92% 50%); } .b-energy { border-color: hsl(38 92% 50% / .3); background: hsl(38 92% 50% / .05); }
|
||
section.aia-card { padding: 16px; }
|
||
section .aia-subtitle { margin: 2px 0 12px; font-size: 11px; }
|
||
.eq { display: flex; align-items: center; gap: 12px; border-radius: calc(var(--aia-r, 12px) - 4px); padding: 12px;
|
||
border: 1px solid hsl(var(--border, 214 32% 91%)); background: hsl(var(--muted, 210 40% 96%) / .2); }
|
||
.eq .ic { width: 40px; height: 40px; flex-shrink: 0; border-radius: 999px; display: flex; align-items: center; justify-content: center;
|
||
background: hsl(var(--card, 0 0% 100%)); }
|
||
.eq .v { font: 600 18px/1.2 var(--aia-mono, ui-monospace, monospace); font-variant-numeric: tabular-nums; }
|
||
.eq .l { font-size: 11px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
|
||
/* carbon over time */
|
||
.bars { display: flex; align-items: flex-end; gap: 3px; height: 128px; }
|
||
.bars div { flex: 1; min-width: 0; border-radius: 2px 2px 0 0; background: hsl(160 84% 39% / .7); }
|
||
.bars div:hover { background: hsl(160 84% 39%); }
|
||
.axis { margin-top: 4px; display: flex; justify-content: space-between; font-size: 9px; }
|
||
/* by model / by project */
|
||
.rows { display: flex; flex-direction: column; gap: 6px; }
|
||
.row { display: flex; align-items: center; gap: 8px; }
|
||
.row .n { width: 112px; flex-shrink: 0; font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
|
||
.row .track { flex: 1; height: 8px; border-radius: 999px; overflow: hidden; background: hsl(var(--muted, 210 40% 96%)); }
|
||
.row .fill { height: 100%; border-radius: 999px; }
|
||
.row .v { width: 96px; flex-shrink: 0; text-align: right; font: 12px var(--aia-mono, ui-monospace, monospace); font-variant-numeric: tabular-nums; }
|
||
#body { transition: opacity .15s; }
|
||
#body.stale { opacity: .5; }
|
||
.method { font-size: 11px; line-height: 1.6; margin: 0; }
|
||
a { color: hsl(var(--primary, 24 95% 53%)); text-decoration: none; }
|
||
a:hover { text-decoration: underline; }
|
||
svg.i { width: 16px; height: 16px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
|
||
</style>
|
||
</head>
|
||
<body>
|
||
<div class="aia-page wrap">
|
||
<div class="aia-toolbar filters">
|
||
<select id="range" class="aia-select" title="How far back the figures go">
|
||
<option value="24h">Last 24 hours</option>
|
||
<option value="7d">Last 7 days</option>
|
||
<option value="30d">Last 30 days</option>
|
||
<option value="90d">Last 90 days</option>
|
||
<option value="sub">Since subscription</option>
|
||
<option value="all">All time</option>
|
||
</select>
|
||
<div class="aia-seg" id="scopes"></div>
|
||
</div>
|
||
<div id="msg" hidden></div>
|
||
<div id="body" hidden style="display:flex;flex-direction:column;gap:24px">
|
||
<div class="grid3" id="heads"></div>
|
||
<section class="aia-card">
|
||
<h2 class="aia-title">In everyday terms</h2>
|
||
<p class="aia-subtitle">What that footprint roughly equals</p>
|
||
<div class="grid3" id="eqs"></div>
|
||
</section>
|
||
<section class="aia-card">
|
||
<h2 class="aia-title">Carbon over time</h2>
|
||
<p class="aia-subtitle" id="timeHint"></p>
|
||
<div class="bars" id="bars"></div>
|
||
<div class="axis aia-muted" id="axis"></div>
|
||
</section>
|
||
<div class="grid2">
|
||
<section class="aia-card">
|
||
<h2 class="aia-title">By model</h2>
|
||
<p class="aia-subtitle">Carbon split across model families</p>
|
||
<div class="rows" id="models"></div>
|
||
</section>
|
||
<section class="aia-card">
|
||
<h2 class="aia-title">Top projects</h2>
|
||
<p class="aia-subtitle">Carbon by project touched</p>
|
||
<div class="rows" id="projects"></div>
|
||
</section>
|
||
</div>
|
||
<p class="method aia-muted">
|
||
Order-of-magnitude estimate derived from each conversation's token usage — not a measurement.
|
||
Per-family carbon factors come from the open-source
|
||
<a href="https://github.com/gwittebolle/claude-carbon" target="_blank" rel="noreferrer">claude-carbon</a>
|
||
project, fitted to the peer-reviewed
|
||
<a href="https://arxiv.org/abs/2505.09598" target="_blank" rel="noreferrer">Jegham et al. 2025</a>
|
||
"How Hungry is AI?" study. Energy is backed out via the AWS grid carbon intensity (0.287 kgCO2e/kWh)
|
||
and water via the study's total WUE (3.32 L/kWh). Local (local) conversations are measured instead:
|
||
the box's wall power × the time it worked, at France's consumption mix (ADEME, 0.0461 kgCO2e/kWh)
|
||
and the study's off-site WUE (3.14 L/kWh). Other providers' conversations are excluded.
|
||
</p>
|
||
</div>
|
||
</div>
|
||
<script>
|
||
(function () {
|
||
"use strict";
|
||
var api = window.aiAgentPlugin || null;
|
||
var init = (api && api.init) || {};
|
||
var DAY = 86400000;
|
||
var KEY = "aia-environment";
|
||
var saved = {};
|
||
try { saved = JSON.parse(localStorage.getItem(KEY) || "{}") || {}; } catch (e) { saved = {}; }
|
||
var state = {
|
||
range: saved.range || "sub",
|
||
scope: typeof init.scope === "string" && init.scope ? init.scope : (saved.scope || ""),
|
||
};
|
||
var data = null, timer = null, handle = null, error = "", seq = 0, stale = false;
|
||
function $(id) { return document.getElementById(id); }
|
||
|
||
// Reads go through the host's cached query (paints at once from the last
|
||
// answer, again when the fresh one lands); an older host only has action().
|
||
function read(name, payload, cb) {
|
||
if (api.query) return api.query(name, payload, cb);
|
||
var live = true;
|
||
function run() {
|
||
return api.action(name, payload).then(
|
||
function (d) { if (live) cb(d, { fromCache: false, fetching: false, at: Date.now() }); },
|
||
function (e) { if (live) cb(null, { error: String((e && e.message) || e), fromCache: false, fetching: false, at: Date.now() }); });
|
||
}
|
||
run();
|
||
return { stop: function () { live = false; }, refresh: run };
|
||
}
|
||
function setHeader(h) { if (api && api.setHeader) api.setHeader(h); }
|
||
function el(tag, attrs, kids) {
|
||
var n = document.createElement(tag);
|
||
for (var k in attrs || {}) {
|
||
if (k === "text") n.textContent = attrs[k];
|
||
else if (k === "style") n.setAttribute("style", attrs[k]);
|
||
else n.setAttribute(k, attrs[k]);
|
||
}
|
||
(kids || []).forEach(function (c) { if (c) n.appendChild(typeof c === "string" ? document.createTextNode(c) : c); });
|
||
return n;
|
||
}
|
||
function svg(path) {
|
||
var s = document.createElementNS("http://www.w3.org/2000/svg", "svg");
|
||
s.setAttribute("viewBox", "0 0 24 24"); s.setAttribute("class", "i");
|
||
path.split("|").forEach(function (d) {
|
||
var p = document.createElementNS("http://www.w3.org/2000/svg", "path"); p.setAttribute("d", d); s.appendChild(p);
|
||
});
|
||
return s;
|
||
}
|
||
var ICONS = {
|
||
leaf: "M11 20A7 7 0 0 1 9.8 6.1C15.5 5 17 4.48 19 2c1 2 2 4.18 2 8 0 5.5-4.78 10-10 10Z|M2 21c0-3 1.85-5.36 5.08-6C9.5 14.52 12 13 13 12",
|
||
drop: "M12 22a7 7 0 0 0 7-7c0-2-1-3.9-3-5.5s-3.5-4-4-6.5c-.5 2.5-2 4.9-4 6.5C6 11.1 5 13 5 15a7 7 0 0 0 7 7z",
|
||
zap: "M4 14a1 1 0 0 1-.78-1.63l9.9-10.2a.5.5 0 0 1 .86.46l-1.92 6.02A1 1 0 0 0 13 10h7a1 1 0 0 1 .78 1.63l-9.9 10.2a.5.5 0 0 1-.86-.46l1.92-6.02A1 1 0 0 0 11 14z",
|
||
car: "M19 17h2c.6 0 1-.4 1-1v-3c0-.9-.7-1.7-1.5-1.9C18.7 10.6 16 10 16 10s-1.3-1.4-2.2-2.3c-.5-.4-1.1-.7-1.8-.7H5c-.6 0-1.1.4-1.4.9l-1.4 2.9A3.7 3.7 0 0 0 2 12v4c0 .6.4 1 1 1h2|M7 17h10|M9 17a2 2 0 1 1-4 0 2 2 0 0 1 4 0|M19 17a2 2 0 1 1-4 0 2 2 0 0 1 4 0",
|
||
phone: "M7 2h10a2 2 0 0 1 2 2v16a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2z|M12 18h.01",
|
||
glass: "M5.12 2.0 7 21h10l1.88-19z|M6 12a5 5 0 0 1 6 0 5 5 0 0 0 6 0"
|
||
};
|
||
var MODEL_HUE = { opus: "25 95% 53%", sonnet: "199 89% 48%", haiku: "160 84% 39%", fable: "258 90% 66%",
|
||
qwen: "330 81% 60%", inkling: "189 94% 43%", gpt: "84 81% 44%", gemini: "239 84% 67%" };
|
||
|
||
function sig(n) {
|
||
var s = n >= 100 ? n.toFixed(0) : n >= 10 ? n.toFixed(1) : n.toFixed(2);
|
||
return s.indexOf(".") >= 0 ? s.replace(/\.?0+$/, "") : s;
|
||
}
|
||
function fmtSig(n) { if (!n || n < 0) return "0"; if (n >= 1000) return Math.round(n).toLocaleString("en-US"); return sig(n); }
|
||
function fmtCo2e(kg) {
|
||
if (!kg || kg < 0) return "0 g eqCO2";
|
||
if (kg / 1000 >= 1) return sig(kg / 1000) + " t eqCO2";
|
||
if (kg >= 1) return sig(kg) + " kg eqCO2";
|
||
var g = kg * 1000;
|
||
if (g >= 1) return sig(g) + " g eqCO2";
|
||
return sig(g * 1000) + " mg eqCO2";
|
||
}
|
||
function fmtWater(l) {
|
||
if (!l || l < 0) return "0 mL";
|
||
if (l >= 1000) return sig(l / 1000) + " m³";
|
||
if (l >= 1) return sig(l) + " L";
|
||
return sig(l * 1000) + " mL";
|
||
}
|
||
function fmtEnergy(k) {
|
||
if (!k || k < 0) return "0 Wh";
|
||
if (k >= 1000) return sig(k / 1000) + " MWh";
|
||
if (k >= 1) return sig(k) + " kWh";
|
||
return sig(k * 1000) + " Wh";
|
||
}
|
||
var RANGE_LABEL = { "24h": "Last 24 hours", "7d": "Last 7 days", "30d": "Last 30 days", "90d": "Last 90 days",
|
||
sub: "Since subscription", all: "All time" };
|
||
|
||
var queued = false;
|
||
function resize() {
|
||
if (!api || typeof api.resize !== "function" || queued) return;
|
||
queued = true;
|
||
requestAnimationFrame(function () { queued = false; api.resize(Math.ceil(document.documentElement.scrollHeight) + 4); });
|
||
}
|
||
if (window.ResizeObserver) new ResizeObserver(resize).observe(document.body);
|
||
|
||
function persist() {
|
||
try { localStorage.setItem(KEY, JSON.stringify({ range: state.range, scope: state.scope })); } catch (e) {}
|
||
}
|
||
|
||
function renderScopes() {
|
||
var box = $("scopes");
|
||
box.textContent = "";
|
||
var segs = ((data && data.profiles) || []).map(function (p) { return { id: "profile:" + p.id, label: p.label }; });
|
||
segs.push({ id: "all", label: "All" });
|
||
var cur = state.scope || (data && data.scope);
|
||
segs.forEach(function (s) {
|
||
var b = el("button", { type: "button", text: s.label, "aria-pressed": s.id === cur ? "true" : "false" });
|
||
b.addEventListener("click", function () { state.scope = s.id; persist(); load(); });
|
||
box.appendChild(b);
|
||
});
|
||
}
|
||
|
||
function barRow(label, frac, value, color, sub) {
|
||
var w = Math.max(frac * 100, frac > 0 ? 2 : 0);
|
||
return el("div", { class: "row" }, [
|
||
el("div", { class: "n", text: label, title: sub || "" }),
|
||
el("div", { class: "track" }, [el("div", { class: "fill", style: "width:" + w + "%;background:hsl(" + color + " / .8)" })]),
|
||
el("div", { class: "v", text: value })
|
||
]);
|
||
}
|
||
|
||
function render() {
|
||
renderScopes();
|
||
$("range").value = state.range;
|
||
var rows = (data && data.conversations) || [];
|
||
var label = RANGE_LABEL[(data && data.range) || state.range] || "";
|
||
if (data) setHeader({ subtitle: rows.length + (rows.length === 1 ? " conversation" : " conversations") + " · " + label });
|
||
var msg = $("msg");
|
||
msg.textContent = "";
|
||
if (!data) { // no answer yet for this range / scope, or it failed
|
||
$("body").hidden = true;
|
||
msg.hidden = !error;
|
||
if (error) { msg.className = "aia-error"; msg.textContent = error; }
|
||
resize();
|
||
return;
|
||
}
|
||
// An error over an earlier answer: keep the figures, say so in a line.
|
||
if (error) { msg.hidden = false; msg.className = "aia-muted aia-small"; msg.textContent = "Showing the last figures — the refresh failed: " + error; }
|
||
else msg.hidden = true;
|
||
if (!rows.length) {
|
||
$("body").hidden = true;
|
||
msg.hidden = false; msg.className = "aia-empty";
|
||
msg.textContent = "No conversations with a footprint in range: " + label + ".";
|
||
resize();
|
||
return;
|
||
}
|
||
$("body").hidden = false;
|
||
$("body").classList.toggle("stale", stale);
|
||
var tot = { co2eKg: 0, waterL: 0, energyKwh: 0 };
|
||
rows.forEach(function (r) { tot.co2eKg += r.co2eKg; tot.waterL += r.waterL; tot.energyKwh += r.energyKwh; });
|
||
var n = rows.length;
|
||
|
||
var heads = $("heads"); heads.textContent = "";
|
||
[["carbon", "leaf", "Carbon", fmtCo2e(tot.co2eKg), fmtCo2e(tot.co2eKg / n)],
|
||
["water", "drop", "Water", fmtWater(tot.waterL), fmtWater(tot.waterL / n)],
|
||
["energy", "zap", "Energy", fmtEnergy(tot.energyKwh), fmtEnergy(tot.energyKwh / n)]].forEach(function (c) {
|
||
heads.appendChild(el("div", { class: "head b-" + c[0] }, [
|
||
el("div", { class: "lbl c-" + c[0] }, [svg(ICONS[c[1]]), c[2]]),
|
||
el("div", { class: "val", text: c[3] }),
|
||
el("div", { class: "sub muted", text: c[4] + " / convo" })
|
||
]));
|
||
});
|
||
|
||
var g = tot.co2eKg * 1000;
|
||
var eqs = $("eqs"); eqs.textContent = "";
|
||
[["car", "c-carbon", fmtSig(g / 120), "km by car"],
|
||
["phone", "c-carbon", fmtSig(g / 8), "phone charges"],
|
||
["glass", "c-water", fmtSig(tot.waterL / 0.25), "glasses of water"]].forEach(function (e) {
|
||
var ic = el("div", { class: "ic " + e[1] }, [svg(ICONS[e[0]])]);
|
||
eqs.appendChild(el("div", { class: "eq" }, [ic, el("div", { style: "min-width:0" }, [
|
||
el("div", { class: "v", text: e[2] }), el("div", { class: "l muted", text: e[3] })])]));
|
||
});
|
||
|
||
// carbon over time — per day, per week past 45 days (max 120 bars)
|
||
var now = Date.now();
|
||
var times = rows.map(function (r) { return r.t; }).filter(function (t) { return typeof t === "number"; });
|
||
var start = data.since != null ? data.since : Math.min.apply(null, times);
|
||
var weekly = (now - start) / DAY > 45;
|
||
var step = weekly ? 7 * DAY : DAY;
|
||
var nb = Math.min(120, Math.max(1, Math.ceil((now - start) / step) + 1));
|
||
var base = now - (nb - 1) * step;
|
||
var bins = []; for (var i = 0; i < nb; i++) bins.push(0);
|
||
rows.forEach(function (r) {
|
||
if (typeof r.t !== "number") return;
|
||
var idx = Math.floor((r.t - base) / step);
|
||
if (idx >= 0 && idx < nb) bins[idx] += r.co2eKg;
|
||
});
|
||
function lab(i) { var d = new Date(base + i * step); return weekly ? (d.getMonth() + 1) + "/" + d.getDate() : String(d.getDate()); }
|
||
var sum = bins.reduce(function (a, b) { return a + b; }, 0);
|
||
var max = Math.max.apply(null, [1e-9].concat(bins));
|
||
$("timeHint").textContent = "eqCO2 " + (weekly ? "per week" : "per day") + " · " + fmtCo2e(sum) + " total";
|
||
var bars = $("bars"); bars.textContent = "";
|
||
bins.forEach(function (kg, i) {
|
||
bars.appendChild(el("div", { title: lab(i) + ": " + fmtCo2e(kg),
|
||
style: "height:" + Math.max(kg / max * 100, kg > 0 ? 2 : 0) + "%" }));
|
||
});
|
||
$("axis").textContent = "";
|
||
$("axis").appendChild(el("span", { text: lab(0) }));
|
||
$("axis").appendChild(el("span", { text: lab(nb - 1) }));
|
||
|
||
function group(keyOf) {
|
||
var m = {};
|
||
rows.forEach(function (r) {
|
||
keyOf(r).forEach(function (k) { var e = m[k] || (m[k] = { kg: 0, n: 0 }); e.kg += r.co2eKg; e.n += 1; });
|
||
});
|
||
return Object.keys(m).map(function (k) { return [k, m[k]]; }).sort(function (a, b) { return b[1].kg - a[1].kg; });
|
||
}
|
||
var fams = group(function (r) { return [r.family || "opus"]; });
|
||
var famTot = fams.reduce(function (a, f) { return a + f[1].kg; }, 0) || 1;
|
||
var mbox = $("models"); mbox.textContent = "";
|
||
fams.forEach(function (f) {
|
||
mbox.appendChild(barRow(f[0], f[1].kg / famTot, fmtCo2e(f[1].kg), MODEL_HUE[f[0]] || "215 16% 47%", f[1].n + " convos"));
|
||
});
|
||
var projs = group(function (r) { return r.projects && r.projects.length ? r.projects : ["(none)"]; }).slice(0, 8);
|
||
var pmax = Math.max.apply(null, [1e-9].concat(projs.map(function (p) { return p[1].kg; })));
|
||
var pbox = $("projects"); pbox.textContent = "";
|
||
projs.forEach(function (p) {
|
||
pbox.appendChild(barRow(p[0], p[1].kg / pmax, fmtCo2e(p[1].kg), "160 84% 39%", p[1].n + " convos"));
|
||
});
|
||
resize();
|
||
}
|
||
|
||
// One query per range × scope: a filter change stops the old one and
|
||
// starts the next (which paints at once when this device has seen it).
|
||
function load() {
|
||
clearTimeout(timer);
|
||
if (handle) handle.stop();
|
||
// The figures on screen stay, dimmed, until the new range / scope answers.
|
||
stale = !!data; error = "";
|
||
render();
|
||
if (!api) { var m = $("msg"); m.hidden = false; m.className = "aia-empty"; m.textContent = "Open this page from the ai-agent."; return; }
|
||
var my = ++seq; // an older host's stop() can't cancel an answer on its way
|
||
handle = read("footprint", { scope: state.scope, range: state.range }, function (d, meta) {
|
||
if (my !== seq) return;
|
||
if (d) {
|
||
data = d;
|
||
if (d.scope && !state.scope) state.scope = d.scope;
|
||
} else if (meta.error && stale) data = null; // not the last range's figures under the new one
|
||
stale = false;
|
||
error = meta.error ? String(meta.error) : "";
|
||
render();
|
||
if (!meta.fetching) {
|
||
clearTimeout(timer);
|
||
timer = setTimeout(function tick() {
|
||
if (my !== seq) return;
|
||
if (document.hidden) timer = setTimeout(tick, 60000);
|
||
else handle.refresh();
|
||
}, 60000);
|
||
}
|
||
});
|
||
}
|
||
$("range").addEventListener("change", function () { state.range = this.value; persist(); load(); });
|
||
document.addEventListener("visibilitychange", function () { if (!document.hidden && data && handle) handle.refresh(); });
|
||
load();
|
||
})();
|
||
</script>
|
||
</body>
|
||
</html>
|