function truncateSummaryText(value: unknown, maxLength = 500) { if (typeof value !== "string") return null; return value.length > maxLength ? value.slice(0, maxLength) : value; } function readNumericField(record: Record, key: string) { return key in record ? record[key] ?? null : undefined; } export function summarizeHeartbeatRunResultJson( resultJson: Record | null | undefined, ): Record | null { if (!resultJson || typeof resultJson !== "object" || Array.isArray(resultJson)) { return null; } const summary: Record = {}; const textFields = ["summary", "result", "message", "error"] as const; for (const key of textFields) { const value = truncateSummaryText(resultJson[key]); if (value !== null) { summary[key] = value; } } const numericFieldAliases = ["total_cost_usd", "cost_usd", "costUsd"] as const; for (const key of numericFieldAliases) { const value = readNumericField(resultJson, key); if (value !== undefined && value !== null) { summary[key] = value; } } return Object.keys(summary).length > 0 ? summary : null; }