2026-03-11 17:23:33 -05:00
|
|
|
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<string, unknown>, key: string) {
|
|
|
|
|
return key in record ? record[key] ?? null : undefined;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-31 20:21:13 +01:00
|
|
|
function readCommentText(value: unknown) {
|
|
|
|
|
if (typeof value !== "string") return null;
|
|
|
|
|
const trimmed = value.trim();
|
|
|
|
|
return trimmed.length > 0 ? trimmed : null;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-11 17:23:33 -05:00
|
|
|
export function summarizeHeartbeatRunResultJson(
|
|
|
|
|
resultJson: Record<string, unknown> | null | undefined,
|
|
|
|
|
): Record<string, unknown> | null {
|
|
|
|
|
if (!resultJson || typeof resultJson !== "object" || Array.isArray(resultJson)) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const summary: Record<string, unknown> = {};
|
|
|
|
|
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;
|
|
|
|
|
}
|
2026-03-31 20:21:13 +01:00
|
|
|
|
|
|
|
|
export function buildHeartbeatRunIssueComment(
|
|
|
|
|
resultJson: Record<string, unknown> | null | undefined,
|
|
|
|
|
): string | null {
|
|
|
|
|
if (!resultJson || typeof resultJson !== "object" || Array.isArray(resultJson)) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
readCommentText(resultJson.summary)
|
|
|
|
|
?? readCommentText(resultJson.result)
|
|
|
|
|
?? readCommentText(resultJson.message)
|
|
|
|
|
?? null
|
|
|
|
|
);
|
|
|
|
|
}
|