repositories / pi-ext
pi-ext
bugabingas pi extensions
owned by admin
extensions/autopsy/metrics.ts
Rawimport { getImageDimensions } from "@earendil-works/pi-tui";
import type {
CatalogPrices,
InspectorCost,
InspectorEntry,
InspectorMetrics,
InspectorUsage,
} from "./src/types.ts";
const CONTENT_LIMIT = 2_000;
const TEXT_LIMIT = 1024 * 1024;
const IMAGE_BUDGET = 4 * 1024 * 1024;
const IMAGE_TYPES = new Set([
"image/png",
"image/jpeg",
"image/gif",
"image/webp",
]);
function record(value: unknown): Record<string, unknown> | null {
return value !== null && typeof value === "object" && !Array.isArray(value)
? (value as Record<string, unknown>)
: null;
}
function number(value: unknown): number | null {
return typeof value === "number" && Number.isFinite(value) && value >= 0
? value
: null;
}
function sum(values: (number | null)[]): number | null {
let total = 0;
for (const value of values) {
if (value === null) return null;
total += value;
}
return number(total);
}
/** Prices are USD per million tokens; missing counts remain unknown, not zero. */
export function usageOf(
value: unknown,
prices?: CatalogPrices | null,
): InspectorUsage | null {
const usage = record(value);
if (!usage) return null;
const input = number(usage["input"]);
const output = number(usage["output"]);
const cacheRead = number(usage["cacheRead"]);
const cacheWrite = number(usage["cacheWrite"]);
const stored = record(usage["cost"]);
const costBreakdown: InspectorCost = {
input: number(stored?.["input"]),
output: number(stored?.["output"]),
cacheRead: number(stored?.["cacheRead"]),
cacheWrite: number(stored?.["cacheWrite"]),
total: number(stored ? stored["total"] : usage["cost"]),
};
let catalogCost: InspectorCost | null = null;
if (prices) {
// Match Pi's request-wide tier selection; unknown input cannot select a tier.
let rates: CatalogPrices | null = prices;
if (prices.tiers?.length) {
const inputTokens = sum([input, cacheRead, cacheWrite]);
if (inputTokens === null || prices.tiers.length > 256) rates = null;
else {
let threshold = -1;
for (const tier of prices.tiers) {
if (
number(tier.inputTokensAbove) !== null &&
inputTokens > tier.inputTokensAbove &&
tier.inputTokensAbove > threshold
) {
rates = tier;
threshold = tier.inputTokensAbove;
}
}
}
}
const project = (tokens: number | null, rate: unknown): number | null => {
const validRate = number(rate);
return tokens === null || validRate === null
? null
: number((tokens / 1_000_000) * validRate);
};
// Pi treats absent retention detail as ordinary writes; invalid detail stays unknown.
const longWrite =
usage["cacheWrite1h"] === undefined ? 0 : number(usage["cacheWrite1h"]);
const writeCost =
cacheWrite === null || longWrite === null || longWrite > cacheWrite
? null
: sum([
project(cacheWrite - longWrite, rates?.cacheWrite),
project(longWrite, rates ? rates.input * 2 : null),
]);
catalogCost = {
input: project(input, rates?.input),
output: project(output, rates?.output),
cacheRead: project(cacheRead, rates?.cacheRead),
cacheWrite: writeCost,
total: null,
};
catalogCost.total = sum([
catalogCost.input,
catalogCost.output,
catalogCost.cacheRead,
catalogCost.cacheWrite,
]);
}
// Older recordings have no provenance marker. Never call those provider quotes.
const costSource =
stored?.["totalFromProvider"] === true
? "provider"
: stored?.["totalFromProvider"] === false
? "catalog"
: "recorded";
return {
input,
output,
cacheRead,
cacheWrite,
totalTokens: number(usage["totalTokens"]),
cost: costBreakdown.total,
costSource,
costBreakdown,
catalogCost,
};
}
/**
* Inspect the original entry, before image data or text is sanitized.
* Text size counts UTF-8 content, thinking, summaries and bash
* command/output, not session metadata, signatures, details or wire payloads.
* An exceeded work budget makes the affected total null, never a partial total.
*/
export function metricsOf(
entry: unknown,
budget: { text: number; images: number; visits: number } = {
text: TEXT_LIMIT,
images: IMAGE_BUDGET,
visits: 100_000,
},
): {
metrics: InspectorMetrics;
images: InspectorEntry["images"];
} {
const source = record(entry);
const message = record(source?.["message"]) ?? source;
const images: InspectorEntry["images"] = [];
let remainingText = Math.min(TEXT_LIMIT, budget.text);
let textBytes: number | null = 0;
let imageBytes: number | null = 0;
let imageCount: number | null = 0;
let remainingImages = budget.images;
const addText = (value: unknown): void => {
if (typeof value !== "string" || textBytes === null) return;
if (value.length > remainingText) {
textBytes = null;
return;
}
remainingText -= value.length;
budget.text -= value.length;
textBytes += Buffer.byteLength(value, "utf8");
};
const content = message?.["content"];
let hasContent = typeof content === "string" || Array.isArray(content);
if (typeof content === "string") addText(content);
else if (Array.isArray(content)) {
const limit = Math.min(content.length, CONTENT_LIMIT, budget.visits);
budget.visits -= limit;
if (content.length > limit) {
textBytes = null;
imageBytes = null;
imageCount = null;
}
for (let index = 0; index < limit; index++) {
const part = record(content[index]);
if (!part) continue;
if (part["type"] === "text" || part["type"] === "thinking") {
const value = part[part["type"] === "text" ? "text" : "thinking"];
if (typeof value !== "string") textBytes = null;
else addText(value);
} else if (part["type"] === "image") {
if (imageCount !== null) imageCount++;
const mimeType =
typeof part["mimeType"] === "string"
? part["mimeType"].slice(0, 256)
: "unknown";
let bytes: number | null = null;
let width: number | null = null;
let height: number | null = null;
const data = part["data"];
// A shared entry budget also bounds repeated image decoding/header scans.
if (
typeof data === "string" &&
data.length > 0 &&
data.length <= 4 * Math.ceil(remainingImages / 3)
) {
const decoded = Buffer.from(data, "base64");
const withinBudget = decoded.length <= remainingImages;
remainingImages = Math.max(
0,
remainingImages - Math.ceil(data.length / 4) * 3,
);
budget.images = remainingImages;
if (withinBudget && decoded.toString("base64") === data) {
bytes = decoded.length;
if (IMAGE_TYPES.has(mimeType)) {
const dimensions = getImageDimensions(
data.slice(0, 64 * 1024),
mimeType,
);
if (dimensions) {
width = number(dimensions.widthPx);
height = number(dimensions.heightPx);
if (!width || !height) width = height = null;
}
}
}
}
images.push({ index, mimeType, bytes, width, height });
imageBytes = sum([imageBytes, bytes]);
}
}
}
const sections = record(message?.["sections"]);
if (sections) {
hasContent = true;
let count = 0;
for (const key in sections) {
if (!Object.hasOwn(sections, key)) continue;
if (++count > CONTENT_LIMIT || budget.visits <= 0) {
textBytes = null;
break;
}
budget.visits--;
addText(sections[key]);
}
}
for (const value of [
message?.["command"],
message?.["output"],
message?.["summary"],
...(source !== message ? [source?.["summary"]] : []),
]) {
if (typeof value === "string") hasContent = true;
addText(value);
}
if (!hasContent) textBytes = imageBytes = imageCount = null;
const durationMs =
(message?.["role"] === "assistant" || message?.["role"] === "toolResult"
? number(message["durationMs"])
: null) ?? number(source?.["durationMs"]);
const timestamp = number(message?.["timestamp"]);
const start = message?.["role"] === "assistant" ? timestamp : null;
const startedAt =
start !== null && start >= 0 && start <= 8_640_000_000_000_000
? new Date(start).toISOString()
: null;
return {
metrics: {
durationMs,
startedAt,
textBytes,
imageBytes,
imageCount,
totalBytes: sum([textBytes, imageBytes]),
},
images,
};
}