Files
juli a18f992fc1 Bulk delete selected items (admins), with confirmation
Also fix the demo seed script for event-stream MCP answers.
2026-11-14 21:04:48 +01:00

44 lines
4.8 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Fills a running server with a few demo items: node scripts/seed-demo.js http://localhost:8090 <admin password>
import sharp from 'sharp';
const [base = 'http://localhost:8080', pw = 'demo1234'] = process.argv.slice(2);
const login = await fetch(`${base}/api/login`, { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ username: 'admin', password: pw }) });
const cookie = login.headers.get('set-cookie').split(';')[0];
const A = (p, o = {}) => fetch(base + p, { ...o, headers: { cookie, ...(o.headers || {}) } });
const { url: mcp } = await (await A('/api/aikeys', { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ label: 'Demo seed' }) })).json();
const call = (name, args) => fetch(mcp, { method: 'POST', headers: { 'content-type': 'application/json', accept: 'application/json, text/event-stream' },
body: JSON.stringify({ jsonrpc: '2.0', id: 1, method: 'tools/call', params: { name, arguments: args } }) }).then(async (r) => {
const t = await r.text(); // plain JSON or an event stream
if (/^\s*[[{]/.test(t)) return JSON.parse(t);
const data = t.split('\n').filter((l) => l.startsWith('data:')).map((l) => l.slice(5).trim());
return JSON.parse(data[data.length - 1]);
});
const demo = [
['#7b4a2a', 'Big wooden cabinet in the hall', 'Hall', { title: 'Dutch oak "kussenkast" cabinet', category: 'Furniture', year: 'c. 1750', tier: 'notable', value_low: 2500, value_high: 6000, condition: 'good', attributes: { wood_or_material: 'Oak, ebonised mouldings', dimensions_cm: '210 × 190 × 70', style: 'Dutch Baroque' } }],
['#d9c9a3', 'stack of old books', 'Study shelf 2', { title: 'Tolkien – The Hobbit, 1st ed. (1937)', category: 'Books', manufacturer: 'J.R.R. Tolkien', year: '1937', tier: 'notable', value_low: 8000, value_high: 20000, attributes: { publisher: 'George Allen & Unwin', edition: 'First edition, first impression', dust_jacket: 'no' } }],
['#3d5a80', 'stamp album, blue', 'Study shelf 2', { title: 'Netherlands stamp album 1900–1960', category: 'Stamps', tier: 'sell', value_low: 120, value_high: 300, quantity: 1, attributes: { country: 'Netherlands', catalogue: 'NVPH', mint_or_used: 'mostly used' } }],
['#c0c0c0', 'silver spoons in a box', 'Kitchen drawer', null],
['#2a9d8f', 'blue and white plates x6', 'Kitchen', { title: 'Delft blue plates (set of 6)', category: 'Ceramics & Glass', tier: 'lot', quantity: 6, value_low: 40, value_high: 90, attributes: { maker_marks: 'De Porceleyne Fles', pattern: 'Floral' } }],
['#555555', 'old radio', 'Attic', null],
['#1d1d1d', 'crate of LPs, this one on top', 'Living room', { title: 'Pink Floyd – The Dark Side of the Moon (LP, UK 1973)', category: 'Vinyl Records', manufacturer: 'Pink Floyd', year: '1973', tier: 'notable', attributes: { label: 'Harvest', catalogue_number: 'SHVL 804', format: 'LP', country: 'UK', matrix_runout: 'not visible yet', media_grade: 'VG+ (from photo)', sleeve_grade: 'VG' } }],
['#e9c46a', 'photo albums', 'Attic box 1', { title: 'Photo albums (1950s–70s)', category: 'Photographs & Cameras', tier: 'keep', quantity: 4 }],
];
const fd0 = await (await A('/api/join')).json();
const pairAs = (who) => fetch(`${base}/api/pair`, { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ code: fd0.code, password: 'demo1234', kind: 'android', ...who }) }).then((r) => r.json());
let pair = await pairAs({ name: 'Sam' });
if (!pair.token) pair = await pairAs({ username: 'sam' }); // seeding a second time
for (const [color, note, location, ai] of demo) {
const img = await sharp({ create: { width: 1200, height: 900, channels: 3, background: color } }).jpeg().toBuffer();
const fd = new FormData();
fd.append('photos', new Blob([img], { type: 'image/jpeg' }), 'p.jpg');
fd.append('note', note); fd.append('location', location);
const it = await (await fetch(`${base}/api/items`, { method: 'POST', body: fd, headers: { authorization: `Bearer ${pair.token}` } })).json();
if (ai) await call('update_item', { id: it.id, ...ai });
}
await call('ask_humans', { id: 4, item_id: 4, question: 'Please photograph the small marks stamped on the back of one spoon.' });
await call('submit_run_report', {
report: 'Processed 7 items.\n\nConfident: #1 is an 18th-century Dutch oak cushion cabinet (kussenkast) — the ebonised mouldings and proportions match museum examples. #5 are Delft plates with the De Porceleyne Fles mark.\n\nUncertain: #2 looks like a 1937 first edition of The Hobbit, but I could not see the copyright page, so it could be a later printing.\n\nNeeds info: #4 — asked the team to photograph the hallmarks on a spoon. #6 — the radio has no visible model plate.\n\nNext: once #2 has a photo of the copyright page I can confirm the printing.',
processed: [1, 2, 3, 4, 5, 6, 7], confident: [1, 3, 5, 7], uncertain: [2], needs_info: [4, 6],
});
console.log('seeded');