From 3073391d796f9095a06f9d8fe993e53063b3699c Mon Sep 17 00:00:00 2001 From: SepehrYahyaee <7heycallmegray@gmail.com> Date: Mon, 9 Nov 2026 15:13:51 +0330 Subject: [PATCH] fix: keep AI provider and billing metadata out of case results --- docs/ai-damage-companion.md | 7 +++++-- src/ai/ai-result.ts | 26 ++++++++++++++++++++++++++ src/ai/ai.service.spec.ts | 24 +++++++++++++++++++++++- src/ai/ai.service.ts | 4 ++-- 4 files changed, 56 insertions(+), 5 deletions(-) diff --git a/docs/ai-damage-companion.md b/docs/ai-damage-companion.md index 3170fad..a741eda 100644 --- a/docs/ai-damage-companion.md +++ b/docs/ai-damage-companion.md @@ -128,7 +128,9 @@ Provider monetary fields in tomans are converted to rials **once** by the respon normalizer: overall/genuine/aftermarket totals, component min/median/max, repair estimates and individual listing prices. Missing/invalid/unsafe numbers remain null, not zero. Unsupported currencies do not acquire a guessed conversion. Provider -processing cost remains in USD and duration remains milliseconds. The UI explicitly +processing cost and duration remain server-side diagnostics. They are excluded from +expert-facing API results, together with request IDs, pipeline versions, model names +and hashes, including when reading previously stored jobs. The UI explicitly labels monetary values as rials and identifies an LLM repair guess separately. Each job uses one photo, so the provider's `pricing` summary is **that photo's** @@ -140,7 +142,8 @@ The expert selects the matching catalogue part and enters the final valuation. Sources show used/unused status; only absolute HTTP(S) links without credentials are clickable. Price ranges, sample count, spread warning, original/aftermarket -alternatives, notes, cache status, model, date, cost and duration remain available. +alternatives, notes and the result date remain available. Model identity, billing, +processing timings and cache diagnostics are not displayed. Legacy detection-only results still render normally. Opening the AI tab does not start paid pricing. No real claim images were sent during implementation tests; transport and UI tests use local fixtures. Validate actual market quality with the diff --git a/src/ai/ai-result.ts b/src/ai/ai-result.ts index f387e61..f36f476 100644 --- a/src/ai/ai-result.ts +++ b/src/ai/ai-result.ts @@ -235,3 +235,29 @@ export function normalizeAiResult(value: unknown): AiResult { caseReport: text(raw.case_report) ?? "", }; } + +/** Expert-facing view: keep provider diagnostics and billing metadata server-side. */ +export function publicAiResult(result: AiResult | null | undefined) { + if (!result) return result; + return { + parts: result.parts, + report: result.report, + caseReport: result.caseReport, + vehicle: result.vehicle, + internalDamages: result.internalDamages, + ...(result.pricing + ? { + pricing: { + car: result.pricing.car, + minSeverity: result.pricing.minSeverity, + currency: result.pricing.currency, + originalCurrency: result.pricing.originalCurrency, + totals: result.pricing.totals, + complete: result.pricing.complete, + pricedParts: result.pricing.pricedParts, + unpricedParts: result.pricing.unpricedParts, + }, + } + : {}), + }; +} diff --git a/src/ai/ai.service.spec.ts b/src/ai/ai.service.spec.ts index c4733ff..8d5f253 100644 --- a/src/ai/ai.service.spec.ts +++ b/src/ai/ai.service.spec.ts @@ -187,7 +187,19 @@ describe("durable AI jobs", () => { { imageKey: aiImageKey(image.path), status: "completed", - result, + result: { + ...result, + requestId: "PRIVATE_REQUEST", + pipeline: { price_model: "PRIVATE_MODEL" }, + pricing: { + car: "پژو", + totals: { overall: 1200000 }, + costUsd: 0.04, + model: "PRIVATE_MODEL", + durationMs: 1000, + }, + }, + errorRequestId: "PRIVATE_ERROR_REQUEST", path: "private", attempts: 1, }, @@ -204,6 +216,16 @@ describe("durable AI jobs", () => { result, }); expect(view.images[0]).not.toHaveProperty("path"); + expect(view.images[0]).not.toHaveProperty("errorRequestId"); + expect(view.images[0].result).not.toHaveProperty("requestId"); + expect(view.images[0].result).not.toHaveProperty("pipeline"); + expect(view.images[0].result.pricing).toMatchObject({ + car: "پژو", + totals: { overall: 1200000 }, + }); + for (const key of ["costUsd", "model", "durationMs"]) + expect(view.images[0].result.pricing).not.toHaveProperty(key); + expect(JSON.stringify(view)).not.toContain("PRIVATE_"); }); it("manual retry affects only failed jobs for current claim/photos", async () => { const { service, jobs } = setup(); diff --git a/src/ai/ai.service.ts b/src/ai/ai.service.ts index dd788ee..d14df65 100644 --- a/src/ai/ai.service.ts +++ b/src/ai/ai.service.ts @@ -1,3 +1,4 @@ +import { publicAiResult } from "./ai-result"; import { createHash, randomUUID } from "node:crypto"; import { createReadStream } from "node:fs"; import { realpath, stat } from "node:fs/promises"; @@ -127,8 +128,7 @@ export class AiService implements OnModuleInit, OnModuleDestroy { completedAt: job?.completedAt, error: job?.error, errorCode: job?.errorCode, - errorRequestId: job?.errorRequestId, - result: job?.status === "completed" ? job.result : undefined, + result: job?.status === "completed" ? publicAiResult(job.result) : undefined, }; }), };