feat: replace OpenAI pricing with AI assessment estimates

Extend durable image assessments with explicit Persian vehicle names, deployment severity settings and advisory market prices normalized to rials. Preserve case access and retry boundaries, and remove the retired OpenAI pricing module and routes.
This commit is contained in:
SepehrYahyaee
2026-11-08 10:52:45 +03:30
parent a8bb0d8e35
commit 755e921c08
32 changed files with 638 additions and 2702 deletions
+151
View File
@@ -0,0 +1,151 @@
/** Market prices are normalized from the provider's tomans to the panel's rials. */
export interface AiPriceTotals {
overall: number | null;
genuine: number | null;
aftermarket: number | null;
}
export interface AiPriceStats {
count: number | null;
min: number | null;
median: number | null;
max: number | null;
wideSpread: boolean;
}
export interface AiPartPrice {
status: string;
message: string | null;
query: { car: string | null; part: string | null };
currency: "rial";
originalCurrency: string | null;
totals: AiPriceTotals;
components: Array<{
name: string | null;
overall: AiPriceStats;
genuine: AiPriceStats;
aftermarket: AiPriceStats;
}>;
repairEstimate: { amount: number | null; isLlmGuess: boolean } | null;
confidence: string | null;
notes: string | null;
sources: Array<{
component: string | null;
kind: string | null;
price: number | null;
title: string | null;
url: string | null;
used: boolean;
unusedReason: string | null;
}>;
cached: boolean;
}
export interface AiPricing {
car: string | null;
minSeverity: string | null;
currency: "rial";
originalCurrency: string | null;
totals: AiPriceTotals;
complete: boolean;
pricedParts: number | null;
unpricedParts: number | null;
model: string | null;
costUsd: number | null;
durationMs: number | null;
}
const text = (value: unknown) =>
typeof value === "string" ? value.trim().slice(0, 2000) : null;
const nonnegative = (value: unknown): number | null =>
typeof value === "number" && Number.isFinite(value) && value >= 0
? value
: null;
const count = (value: unknown) =>
Number.isSafeInteger(value) && (value as number) >= 0
? (value as number)
: null;
const money = (value: unknown, currency: unknown) => {
const n = nonnegative(value);
if (n === null || !["toman", "rial"].includes(String(currency))) return null;
const amount = currency === "toman" ? n * 10 : n;
return amount <= Number.MAX_SAFE_INTEGER ? Math.round(amount) : null;
};
const totals = (value: any, currency: unknown): AiPriceTotals => ({
overall: money(value?.overall, currency),
genuine: money(value?.genuine, currency),
aftermarket: money(value?.aftermarket, currency),
});
const stats = (value: any, currency: unknown): AiPriceStats => ({
count: count(value?.count),
min: money(value?.min, currency),
median: money(value?.median, currency),
max: money(value?.max, currency),
wideSpread: value?.wide_spread === true,
});
const safeUrl = (value: unknown): string | null => {
if (typeof value !== "string") return null;
try {
const u = new URL(value);
return ["http:", "https:"].includes(u.protocol) &&
!u.username &&
!u.password
? u.toString()
: null;
} catch {
return null;
}
};
export function normalizePartPrice(raw: any): AiPartPrice | undefined {
if (!raw || typeof raw !== "object" || typeof raw.status !== "string")
return undefined;
const currency = raw.currency;
return {
status: raw.status.slice(0, 100),
message: text(raw.message),
query: { car: text(raw.query?.car), part: text(raw.query?.part) },
currency: "rial",
originalCurrency: text(currency),
totals: totals(raw.totals, currency),
components: (Array.isArray(raw.components) ? raw.components : [])
.slice(0, 100)
.map((item: any) => ({
name: text(item?.name),
overall: stats(item?.overall, currency),
genuine: stats(item?.genuine, currency),
aftermarket: stats(item?.aftermarket, currency),
})),
repairEstimate: raw.repair_estimate
? {
amount: money(raw.repair_estimate.amount, currency),
isLlmGuess: raw.repair_estimate.is_llm_guess === true,
}
: null,
confidence: text(raw.confidence),
notes: text(raw.notes),
cached: raw.cached === true,
sources: (Array.isArray(raw.sources) ? raw.sources : [])
.slice(0, 200)
.map((item: any) => ({
component: text(item?.component),
kind: text(item?.kind),
price: money(item?.price, currency),
title: text(item?.title),
url: safeUrl(item?.url),
used: item?.used === true,
unusedReason: text(item?.unused_reason),
})),
};
}
export function normalizePricing(raw: any): AiPricing | undefined {
if (!raw || typeof raw !== "object") return undefined;
return {
car: text(raw.car),
minSeverity: text(raw.min_severity),
currency: "rial",
originalCurrency: text(raw.currency),
totals: totals(raw.totals, raw.currency),
complete: raw.complete === true,
pricedParts: count(raw.priced_parts),
unpricedParts: count(raw.unpriced_parts),
model: text(raw.model),
costUsd: nonnegative(raw.cost_usd),
durationMs: nonnegative(raw.duration_ms),
};
}