Added AI damage helper

This commit is contained in:
SepehrYahyaee
2026-10-03 16:38:48 +03:30
parent 18ccfa81e4
commit 2f92766689
22 changed files with 1085 additions and 442 deletions
+63
View File
@@ -0,0 +1,63 @@
export interface AiPart {
name: string;
severity: string | null;
confidence: number | null;
side: string | null;
carSide: string | null;
bbox: { x1: number; y1: number; x2: number; y2: number } | null;
}
export interface AiResult {
parts: AiPart[];
report: string;
caseReport: string;
}
const text = (v: unknown): string | null =>
typeof v === "string" ? v.trim().slice(0, 20000) : null;
/** Preserve detections independently: repeated names may describe different boxes. */
export function normalizeAiResult(value: unknown): AiResult {
const raw = value as any;
const report = raw?.distinct_damaged_parts_report;
if (
!report ||
!Array.isArray(report.parts) ||
typeof report.report !== "string" ||
typeof raw.case_report !== "string"
) {
throw new Error("Invalid AI response");
}
if (report.parts.length > 500) throw new Error("Too many AI detections");
const parts = report.parts.map((part: any): AiPart => {
const name = text(part?.part_name);
if (!name) throw new Error("Invalid AI part");
const b = part.part_bbox;
const bbox =
b &&
[b.x1, b.y1, b.x2, b.y2].every(
(v) => typeof v === "number" && Number.isFinite(v),
) &&
b.x2 > b.x1 &&
b.y2 > b.y1
? { x1: b.x1, y1: b.y1, x2: b.x2, y2: b.y2 }
: null;
return {
name,
bbox,
severity: text(part.damage_severity),
confidence:
typeof part.confidence === "number" &&
Number.isFinite(part.confidence) &&
part.confidence >= 0 &&
part.confidence <= 1
? part.confidence
: null,
side: text(part.side),
carSide: text(part.car_side),
};
});
return {
parts,
report: text(report.report) ?? "",
caseReport: text(raw.case_report) ?? "",
};
}