feat: AR-VINchecker v1.0 — VIN report generator with AI analysis
FastAPI server querying NHTSA/EPA APIs, generating PDF reports with risk scoring, and local Ollama AI analysis via DealAnalyzer. Security hardened: XSS fix, SSRF protection, CORS restricted. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,284 @@
|
||||
/* ── Reset & Variables ─────────────────────────────────────────────────────── */
|
||||
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
|
||||
|
||||
:root {
|
||||
--bg: #0d1117;
|
||||
--bg2: #161b22;
|
||||
--bg3: #1c2128;
|
||||
--border: #30363d;
|
||||
--text: #e6edf3;
|
||||
--muted: #8b949e;
|
||||
--accent: #58a6ff;
|
||||
--green: #3fb950;
|
||||
--yellow: #d29922;
|
||||
--red: #f85149;
|
||||
--navy: #1a3a5c;
|
||||
--radius: 8px;
|
||||
--shadow: 0 4px 16px rgba(0,0,0,.4);
|
||||
}
|
||||
|
||||
body {
|
||||
background: var(--bg);
|
||||
color: var(--text);
|
||||
font-family: 'Segoe UI', system-ui, sans-serif;
|
||||
font-size: 14px;
|
||||
min-height: 100vh;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
/* ── Topbar ────────────────────────────────────────────────────────────────── */
|
||||
.topbar {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
padding: 12px 24px;
|
||||
background: var(--bg2);
|
||||
border-bottom: 1px solid var(--border);
|
||||
position: sticky;
|
||||
top: 0;
|
||||
z-index: 100;
|
||||
}
|
||||
.topbar-logo {
|
||||
font-size: 18px;
|
||||
font-weight: 700;
|
||||
color: var(--accent);
|
||||
letter-spacing: -.3px;
|
||||
}
|
||||
.topbar-version {
|
||||
font-size: 11px;
|
||||
color: var(--muted);
|
||||
}
|
||||
|
||||
/* ── Layout ────────────────────────────────────────────────────────────────── */
|
||||
.layout {
|
||||
display: grid;
|
||||
grid-template-columns: 300px 1fr;
|
||||
gap: 0;
|
||||
flex: 1;
|
||||
height: calc(100vh - 49px);
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
/* ── Left panel ────────────────────────────────────────────────────────────── */
|
||||
.panel-form {
|
||||
background: var(--bg2);
|
||||
border-right: 1px solid var(--border);
|
||||
padding: 20px 18px;
|
||||
overflow-y: auto;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 12px;
|
||||
}
|
||||
.panel-title {
|
||||
font-size: 13px;
|
||||
font-weight: 600;
|
||||
color: var(--muted);
|
||||
text-transform: uppercase;
|
||||
letter-spacing: .6px;
|
||||
margin-bottom: 4px;
|
||||
}
|
||||
|
||||
/* ── Form ──────────────────────────────────────────────────────────────────── */
|
||||
form { display: flex; flex-direction: column; gap: 10px; }
|
||||
|
||||
.field { display: flex; flex-direction: column; gap: 4px; }
|
||||
|
||||
label {
|
||||
font-size: 11px;
|
||||
font-weight: 600;
|
||||
color: var(--muted);
|
||||
text-transform: uppercase;
|
||||
letter-spacing: .4px;
|
||||
}
|
||||
.req { color: var(--red); }
|
||||
|
||||
input[type="text"],
|
||||
input[type="url"] {
|
||||
background: var(--bg3);
|
||||
border: 1px solid var(--border);
|
||||
border-radius: 6px;
|
||||
color: var(--text);
|
||||
font-size: 13px;
|
||||
padding: 7px 10px;
|
||||
outline: none;
|
||||
transition: border-color .15s;
|
||||
width: 100%;
|
||||
}
|
||||
input:focus { border-color: var(--accent); }
|
||||
input::placeholder { color: var(--muted); opacity: .6; }
|
||||
|
||||
.btn {
|
||||
padding: 9px 14px;
|
||||
border: none;
|
||||
border-radius: 6px;
|
||||
font-size: 13px;
|
||||
font-weight: 600;
|
||||
cursor: pointer;
|
||||
transition: opacity .15s, transform .1s;
|
||||
}
|
||||
.btn:active { transform: scale(.98); }
|
||||
.btn:disabled { opacity: .45; cursor: not-allowed; }
|
||||
|
||||
.btn-primary {
|
||||
background: var(--accent);
|
||||
color: #fff;
|
||||
margin-top: 4px;
|
||||
}
|
||||
.btn-primary:hover:not(:disabled) { opacity: .85; }
|
||||
|
||||
.btn-gen {
|
||||
background: var(--green);
|
||||
color: #fff;
|
||||
font-size: 12px;
|
||||
padding: 7px 12px;
|
||||
}
|
||||
.btn-open {
|
||||
background: var(--bg3);
|
||||
border: 1px solid var(--border);
|
||||
color: var(--text);
|
||||
font-size: 12px;
|
||||
padding: 7px 12px;
|
||||
}
|
||||
|
||||
.divider { border: none; border-top: 1px solid var(--border); margin: 4px 0; }
|
||||
.hint { font-size: 11px; color: var(--muted); line-height: 1.6; }
|
||||
.mono { font-family: 'Cascadia Code', 'Consolas', monospace; font-size: 11px;
|
||||
color: var(--accent); }
|
||||
|
||||
/* ── Right panel ────────────────────────────────────────────────────────────── */
|
||||
.panel-cards {
|
||||
padding: 20px;
|
||||
overflow-y: auto;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 12px;
|
||||
}
|
||||
|
||||
.cards-container {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 14px;
|
||||
}
|
||||
|
||||
/* ── Empty state ────────────────────────────────────────────────────────────── */
|
||||
.empty-state {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
gap: 12px;
|
||||
padding: 60px 20px;
|
||||
color: var(--muted);
|
||||
text-align: center;
|
||||
}
|
||||
.empty-icon { font-size: 48px; opacity: .4; }
|
||||
|
||||
/* ── VIN Card ───────────────────────────────────────────────────────────────── */
|
||||
.vin-card {
|
||||
background: var(--bg2);
|
||||
border: 1px solid var(--border);
|
||||
border-radius: var(--radius);
|
||||
box-shadow: var(--shadow);
|
||||
overflow: hidden;
|
||||
animation: fadeIn .25s ease;
|
||||
}
|
||||
|
||||
@keyframes fadeIn { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; } }
|
||||
|
||||
.card-header {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
padding: 12px 16px;
|
||||
background: var(--bg3);
|
||||
border-bottom: 1px solid var(--border);
|
||||
}
|
||||
.card-vin {
|
||||
font-family: 'Cascadia Code', 'Consolas', monospace;
|
||||
font-size: 13px;
|
||||
font-weight: 700;
|
||||
color: var(--accent);
|
||||
}
|
||||
.card-vehicle { font-size: 12px; color: var(--muted); margin-top: 2px; }
|
||||
.card-dismiss {
|
||||
background: none;
|
||||
border: none;
|
||||
color: var(--muted);
|
||||
cursor: pointer;
|
||||
font-size: 16px;
|
||||
line-height: 1;
|
||||
padding: 2px 6px;
|
||||
border-radius: 4px;
|
||||
}
|
||||
.card-dismiss:hover { background: var(--border); color: var(--text); }
|
||||
|
||||
.card-body { padding: 12px 16px; display: flex; flex-direction: column; gap: 8px; }
|
||||
|
||||
/* ── Progress log ───────────────────────────────────────────────────────────── */
|
||||
.progress-log { display: flex; flex-direction: column; gap: 4px; }
|
||||
|
||||
.log-item {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
font-size: 12px;
|
||||
padding: 3px 0;
|
||||
color: var(--muted);
|
||||
transition: color .2s;
|
||||
}
|
||||
.log-item.success { color: var(--green); }
|
||||
.log-item.error { color: var(--red); }
|
||||
.log-item.warning { color: var(--yellow); }
|
||||
.log-item.progress { color: var(--text); }
|
||||
|
||||
.spinner {
|
||||
width: 12px; height: 12px;
|
||||
border: 2px solid var(--border);
|
||||
border-top-color: var(--accent);
|
||||
border-radius: 50%;
|
||||
animation: spin .6s linear infinite;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
@keyframes spin { to { transform: rotate(360deg); } }
|
||||
|
||||
/* ── Risk badge (in card) ────────────────────────────────────────────────────── */
|
||||
.risk-badge {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
gap: 6px;
|
||||
padding: 6px 12px;
|
||||
border-radius: 20px;
|
||||
font-size: 12px;
|
||||
font-weight: 700;
|
||||
margin-top: 4px;
|
||||
align-self: flex-start;
|
||||
}
|
||||
.risk-badge.green { background: rgba(63,185,80,.15); color: var(--green); border: 1px solid rgba(63,185,80,.3); }
|
||||
.risk-badge.yellow { background: rgba(210,153,34,.15); color: var(--yellow); border: 1px solid rgba(210,153,34,.3); }
|
||||
.risk-badge.red { background: rgba(248,81,73,.15); color: var(--red); border: 1px solid rgba(248,81,73,.3); }
|
||||
|
||||
/* ── Card actions ────────────────────────────────────────────────────────────── */
|
||||
.card-actions {
|
||||
display: flex;
|
||||
gap: 8px;
|
||||
flex-wrap: wrap;
|
||||
margin-top: 4px;
|
||||
}
|
||||
|
||||
.pdf-link {
|
||||
font-size: 12px;
|
||||
color: var(--accent);
|
||||
text-decoration: none;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 4px;
|
||||
margin-top: 4px;
|
||||
}
|
||||
.pdf-link:hover { text-decoration: underline; }
|
||||
|
||||
/* ── Scrollbar ───────────────────────────────────────────────────────────────── */
|
||||
::-webkit-scrollbar { width: 6px; }
|
||||
::-webkit-scrollbar-track { background: var(--bg); }
|
||||
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
|
||||
::-webkit-scrollbar-thumb:hover { background: var(--muted); }
|
||||
@@ -0,0 +1,259 @@
|
||||
"use strict";
|
||||
|
||||
const form = document.getElementById("fetchForm");
|
||||
const fetchBtn = document.getElementById("fetchBtn");
|
||||
const cardsContainer = document.getElementById("cardsContainer");
|
||||
|
||||
// Remove empty state on first card
|
||||
function clearEmpty() {
|
||||
const empty = cardsContainer.querySelector(".empty-state");
|
||||
if (empty) empty.remove();
|
||||
}
|
||||
|
||||
// ── Create a new VIN card ──────────────────────────────────────────────────────
|
||||
function createCard(vin) {
|
||||
clearEmpty();
|
||||
|
||||
const safeVin = vin.replace(/[^A-Z0-9]/g, "");
|
||||
|
||||
const card = document.createElement("div");
|
||||
card.className = "vin-card";
|
||||
card.id = `card-${safeVin}`;
|
||||
|
||||
// Build DOM without innerHTML interpolation to prevent XSS
|
||||
const header = document.createElement("div");
|
||||
header.className = "card-header";
|
||||
|
||||
const headerLeft = document.createElement("div");
|
||||
|
||||
const vinEl = document.createElement("div");
|
||||
vinEl.className = "card-vin";
|
||||
vinEl.textContent = safeVin;
|
||||
|
||||
const vehicleEl = document.createElement("div");
|
||||
vehicleEl.className = "card-vehicle";
|
||||
vehicleEl.id = `vehicle-${safeVin}`;
|
||||
vehicleEl.textContent = "Consultando...";
|
||||
|
||||
headerLeft.appendChild(vinEl);
|
||||
headerLeft.appendChild(vehicleEl);
|
||||
|
||||
const dismissBtn = document.createElement("button");
|
||||
dismissBtn.className = "card-dismiss";
|
||||
dismissBtn.title = "Cerrar";
|
||||
dismissBtn.textContent = "✕";
|
||||
dismissBtn.addEventListener("click", () => card.remove());
|
||||
|
||||
header.appendChild(headerLeft);
|
||||
header.appendChild(dismissBtn);
|
||||
|
||||
const body = document.createElement("div");
|
||||
body.className = "card-body";
|
||||
|
||||
const logEl = document.createElement("div");
|
||||
logEl.className = "progress-log";
|
||||
logEl.id = `log-${safeVin}`;
|
||||
|
||||
const actionsEl = document.createElement("div");
|
||||
actionsEl.id = `actions-${safeVin}`;
|
||||
|
||||
body.appendChild(logEl);
|
||||
body.appendChild(actionsEl);
|
||||
|
||||
card.appendChild(header);
|
||||
card.appendChild(body);
|
||||
|
||||
cardsContainer.prepend(card);
|
||||
return card;
|
||||
}
|
||||
|
||||
// ── Add a log line to a card ───────────────────────────────────────────────────
|
||||
function addLog(vin, text, status) {
|
||||
const log = document.getElementById(`log-${vin}`);
|
||||
if (!log) return;
|
||||
|
||||
// Remove spinner from any previous progress item
|
||||
if (status !== "progress") {
|
||||
const prev = log.querySelector(".log-item.progress");
|
||||
if (prev) prev.classList.replace("progress", "done");
|
||||
}
|
||||
|
||||
const item = document.createElement("div");
|
||||
item.className = `log-item ${status}`;
|
||||
|
||||
if (status === "progress") {
|
||||
item.innerHTML = `<span class="spinner"></span><span>${text}</span>`;
|
||||
} else {
|
||||
item.textContent = text;
|
||||
}
|
||||
|
||||
log.appendChild(item);
|
||||
item.scrollIntoView({ behavior: "smooth", block: "nearest" });
|
||||
}
|
||||
|
||||
// ── Show card actions after fetch completes ───────────────────────────────────
|
||||
function showActions(vin, risk) {
|
||||
const actionsDiv = document.getElementById(`actions-${vin}`);
|
||||
if (!actionsDiv) return;
|
||||
|
||||
// Risk badge
|
||||
const badge = document.createElement("div");
|
||||
badge.className = `risk-badge ${risk.color}`;
|
||||
badge.textContent = `${risk.emoji} ${risk.score}/100 — ${risk.level}`;
|
||||
actionsDiv.appendChild(badge);
|
||||
|
||||
// Buttons row
|
||||
const row = document.createElement("div");
|
||||
row.className = "card-actions";
|
||||
|
||||
const genBtn = document.createElement("button");
|
||||
genBtn.className = "btn btn-gen";
|
||||
genBtn.textContent = "📄 Generar PDF";
|
||||
genBtn.addEventListener("click", () => generatePDF(vin, genBtn));
|
||||
|
||||
row.appendChild(genBtn);
|
||||
actionsDiv.appendChild(row);
|
||||
}
|
||||
|
||||
// ── Generate PDF ──────────────────────────────────────────────────────────────
|
||||
async function generatePDF(vin, btn) {
|
||||
btn.disabled = true;
|
||||
btn.textContent = "⏳ Generando...";
|
||||
|
||||
try {
|
||||
const res = await fetch("/api/generate", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ vin }),
|
||||
});
|
||||
const data = await res.json();
|
||||
|
||||
if (data.error) {
|
||||
btn.disabled = false;
|
||||
btn.textContent = "📄 Generar PDF";
|
||||
addLog(vin, `❌ ${data.error}`, "error");
|
||||
return;
|
||||
}
|
||||
|
||||
btn.textContent = "✅ PDF listo";
|
||||
|
||||
const actionsDiv = document.getElementById(`actions-${vin}`);
|
||||
|
||||
// Download link
|
||||
const link = document.createElement("a");
|
||||
link.href = data.url;
|
||||
link.download = data.filename;
|
||||
link.className = "pdf-link";
|
||||
link.textContent = `⬇️ ${data.filename}`;
|
||||
actionsDiv.appendChild(link);
|
||||
|
||||
// Open locally button (Windows only via server)
|
||||
const openBtn = document.createElement("button");
|
||||
openBtn.className = "btn btn-open";
|
||||
openBtn.textContent = "🗂️ Abrir localmente";
|
||||
openBtn.addEventListener("click", () => openLocally(data.filename, openBtn));
|
||||
actionsDiv.querySelector(".card-actions").appendChild(openBtn);
|
||||
|
||||
addLog(vin, `✅ PDF generado: ${data.filename}`, "success");
|
||||
|
||||
} catch (e) {
|
||||
btn.disabled = false;
|
||||
btn.textContent = "📄 Generar PDF";
|
||||
addLog(vin, `❌ Error al generar PDF: ${e.message}`, "error");
|
||||
}
|
||||
}
|
||||
|
||||
// ── Open file locally via server ──────────────────────────────────────────────
|
||||
async function openLocally(filename, btn) {
|
||||
btn.disabled = true;
|
||||
try {
|
||||
const res = await fetch("/api/open", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ filename }),
|
||||
});
|
||||
const data = await res.json();
|
||||
if (data.error) {
|
||||
alert(data.error);
|
||||
}
|
||||
} catch (e) {
|
||||
alert("Error al abrir: " + e.message);
|
||||
} finally {
|
||||
btn.disabled = false;
|
||||
}
|
||||
}
|
||||
|
||||
// ── Form submission → SSE ──────────────────────────────────────────────────────
|
||||
form.addEventListener("submit", async (e) => {
|
||||
e.preventDefault();
|
||||
|
||||
const fd = new FormData(form);
|
||||
const vin = (fd.get("vin") || "").trim().toUpperCase();
|
||||
if (!vin) return;
|
||||
|
||||
// Prevent duplicate active cards
|
||||
const existing = document.getElementById(`card-${vin}`);
|
||||
if (existing) {
|
||||
existing.scrollIntoView({ behavior: "smooth" });
|
||||
existing.style.outline = "2px solid var(--accent)";
|
||||
setTimeout(() => (existing.style.outline = ""), 1500);
|
||||
return;
|
||||
}
|
||||
|
||||
fetchBtn.disabled = true;
|
||||
fetchBtn.textContent = "⏳ Consultando...";
|
||||
|
||||
createCard(vin);
|
||||
|
||||
const params = new URLSearchParams({
|
||||
vin,
|
||||
odometer: fd.get("odometer") || "",
|
||||
primary_damage: fd.get("primary_damage") || "",
|
||||
secondary_damage: fd.get("secondary_damage") || "",
|
||||
title: fd.get("title") || "",
|
||||
bid: fd.get("bid") || "",
|
||||
auction: fd.get("auction") || "",
|
||||
photo_url: fd.get("photo_url") || "",
|
||||
});
|
||||
|
||||
const es = new EventSource(`/api/fetch?${params.toString()}`);
|
||||
|
||||
es.onmessage = (event) => {
|
||||
let msg;
|
||||
try { msg = JSON.parse(event.data); } catch { return; }
|
||||
|
||||
const status = msg.status || "progress";
|
||||
|
||||
if (status === "done") {
|
||||
const vehicleEl = document.getElementById(`vehicle-${vin}`);
|
||||
if (vehicleEl && msg.vehicle) vehicleEl.textContent = msg.vehicle;
|
||||
showActions(vin, msg.risk);
|
||||
es.close();
|
||||
fetchBtn.disabled = false;
|
||||
fetchBtn.textContent = "⚡ FETCH DATA";
|
||||
return;
|
||||
}
|
||||
|
||||
if (status === "error") {
|
||||
addLog(vin, msg.step, "error");
|
||||
es.close();
|
||||
fetchBtn.disabled = false;
|
||||
fetchBtn.textContent = "⚡ FETCH DATA";
|
||||
return;
|
||||
}
|
||||
|
||||
if (status === "success" && msg.vehicle) {
|
||||
const vehicleEl = document.getElementById(`vehicle-${vin}`);
|
||||
if (vehicleEl) vehicleEl.textContent = msg.vehicle;
|
||||
}
|
||||
|
||||
addLog(vin, msg.step, status);
|
||||
};
|
||||
|
||||
es.onerror = () => {
|
||||
addLog(vin, "❌ Conexión interrumpida con el servidor", "error");
|
||||
es.close();
|
||||
fetchBtn.disabled = false;
|
||||
fetchBtn.textContent = "⚡ FETCH DATA";
|
||||
};
|
||||
});
|
||||
Reference in New Issue
Block a user