feat(add-item): photo → Claude vision suggests name + category
In 'Tilføj vare', taking/choosing a photo now sends it (downscaled client- side) to Claude vision, which identifies the product and returns a Danish name, German name, best-fit catalog section and a short note. The form is prefilled and the suggested category shown — you just confirm and type the price. Full photo is still uploaded on submit. Degrades gracefully: no credential or a failed call just falls back to manual entry. - /api/item/analyze (vision via _claude_json, now accepts image content) - VISION_MODEL (Haiku by default; bump to sonnet via env for harder photos)
This commit is contained in:
@@ -157,6 +157,8 @@
|
||||
.btn-submit { width: 100%; background: var(--accent); color: white; border: none; border-radius: 10px; padding: 14px; font-size: 16px; font-weight: 600; font-family: var(--sans); cursor: pointer; margin-top: 0.5rem; }
|
||||
.btn-submit:active { opacity: 0.85; }
|
||||
.modal-context { background: var(--accent-light); border-radius: 8px; padding: 8px 12px; font-size: 13px; color: var(--accent); font-weight: 500; margin-bottom: 1rem; }
|
||||
.ai-suggest { background: var(--green-light); border: 1px solid #a8d5bc; border-radius: 8px; padding: 9px 12px; font-size: 13px; color: var(--green); margin-bottom: 1rem; line-height: 1.4; }
|
||||
.ai-suggest.loading { background: var(--bg); color: var(--muted); border-color: var(--border); }
|
||||
|
||||
.saving-indicator { position: fixed; top: 0.75rem; right: 0.75rem; background: var(--green); color: white; border-radius: 6px; padding: 4px 10px; font-size: 12px; font-family: var(--mono); opacity: 0; transition: opacity 0.3s; z-index: 50; pointer-events: none; }
|
||||
.saving-indicator.show { opacity: 1; }
|
||||
@@ -482,6 +484,7 @@
|
||||
<button class="modal-close" onclick="closeItemModal()">×</button>
|
||||
</div>
|
||||
<div class="modal-context" id="item-modal-ctx" style="display:none"></div>
|
||||
<div id="ai-suggest" class="ai-suggest" style="display:none"></div>
|
||||
<form id="add-form" onsubmit="submitItem(event)">
|
||||
<input type="hidden" name="section_key" id="item-section-key">
|
||||
<div class="form-group">
|
||||
@@ -750,11 +753,69 @@ function closeItemModal() {
|
||||
document.getElementById('item-modal').classList.remove('open');
|
||||
document.getElementById('add-form').reset();
|
||||
document.getElementById('photo-preview').style.display = 'none';
|
||||
document.getElementById('ai-suggest').style.display = 'none';
|
||||
}
|
||||
|
||||
function previewPhoto(input) {
|
||||
const p = document.getElementById('photo-preview');
|
||||
if (input.files && input.files[0]) { p.src = URL.createObjectURL(input.files[0]); p.style.display = 'block'; }
|
||||
if (input.files && input.files[0]) {
|
||||
p.src = URL.createObjectURL(input.files[0]); p.style.display = 'block';
|
||||
analyzePhoto(input.files[0]);
|
||||
}
|
||||
}
|
||||
|
||||
const SEC_NAMES = {koed:'🥩 Kød & fisk', frost:'🧊 Frostgrønt', mejeri:'🧀 Mejeri & ost',
|
||||
pasta:'🍝 Pasta', groent:'🥗 Grønt & tørvarer', konserves:'🥫 Konserves',
|
||||
indisk:'🌶️ Indisk & karry', mex:'🌮 Mexicansk & asiatisk', drikke:'🍺 Drikkevarer', hus:'🏠 Husholdning'};
|
||||
|
||||
// Downscale for the AI call only (fast/cheap, avoids size limits); the full
|
||||
// photo is still uploaded on submit.
|
||||
function downscaleImage(file, maxDim) {
|
||||
return new Promise((resolve) => {
|
||||
const img = new Image();
|
||||
img.onload = () => {
|
||||
const scale = Math.min(1, maxDim / Math.max(img.width, img.height));
|
||||
const w = Math.round(img.width * scale), h = Math.round(img.height * scale);
|
||||
const c = document.createElement('canvas');
|
||||
c.width = w; c.height = h;
|
||||
c.getContext('2d').drawImage(img, 0, 0, w, h);
|
||||
c.toBlob((b) => resolve(b || file), 'image/jpeg', 0.85);
|
||||
};
|
||||
img.onerror = () => resolve(file);
|
||||
img.src = URL.createObjectURL(file);
|
||||
});
|
||||
}
|
||||
|
||||
async function analyzePhoto(file) {
|
||||
const box = document.getElementById('ai-suggest');
|
||||
box.style.display = 'block';
|
||||
box.className = 'ai-suggest loading';
|
||||
box.textContent = '🪄 Analyserer billede…';
|
||||
try {
|
||||
const small = await downscaleImage(file, 1024);
|
||||
const fd = new FormData(); fd.append('photo', small, 'photo.jpg');
|
||||
const res = await fetch('/api/item/analyze', {method: 'POST', body: fd});
|
||||
if (!res.ok) throw new Error('fail');
|
||||
const d = await res.json();
|
||||
const form = document.getElementById('add-form');
|
||||
const skInput = document.getElementById('item-section-key');
|
||||
if (d.name && !form.name.value.trim()) form.name.value = d.name;
|
||||
if (!form.note.value.trim()) {
|
||||
const parts = [];
|
||||
if (d.note) parts.push(d.note);
|
||||
if (d.de) parts.push('🇩🇪 ' + d.de);
|
||||
if (parts.length) form.note.value = parts.join(' · ');
|
||||
}
|
||||
if (d.section && !skInput.value) skInput.value = d.section; // keep an explicitly chosen section
|
||||
const eff = skInput.value;
|
||||
const cat = eff ? (SEC_NAMES[eff] || eff) : 'Diverse (ingen kategori)';
|
||||
box.className = 'ai-suggest done';
|
||||
box.innerHTML = '🪄 Forslag: <b>' + (d.name || '?') + '</b> · ' + cat
|
||||
+ ' <span style="color:var(--muted)">— ret hvis forkert, og tast prisen</span>';
|
||||
} catch (e) {
|
||||
box.className = 'ai-suggest';
|
||||
box.innerHTML = '<span style="color:var(--muted)">Kunne ikke analysere billedet — udfyld manuelt.</span>';
|
||||
}
|
||||
}
|
||||
|
||||
async function submitItem(e) {
|
||||
|
||||
Reference in New Issue
Block a user