Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

feat(catalog): batched GET /ingredients/:id/details endpoint

[._.]/ Adam Eivy30d73b724718
diff --git a/client/src/pages/CatalogIngredient.jsx b/client/src/pages/CatalogIngredient.jsx
index d24c4a6779..80c79b8f1e 100644
--- a/client/src/pages/CatalogIngredient.jsx
+++ b/client/src/pages/CatalogIngredient.jsx
@@ -10,10 +10,9 @@ import { useNavigate, useParams, Link } from 'react-router-dom';
import { Sparkles, Save, Trash2, ArrowLeft, Loader2, ExternalLink, Plus, X, History, RotateCcw, Image as ImageIcon, Star, ChevronDown } from 'lucide-react';
import toast from '../components/ui/Toast';
import {
- getCatalogIngredient,
+ getCatalogIngredientDetails,
updateCatalogIngredient,
deleteCatalogIngredient,
- listCatalogIngredientRelations,
linkCatalogIngredientRelation,
unlinkCatalogIngredientRelation,
listCatalogIngredientRevisions,
@@ -85,23 +84,34 @@ export default function CatalogIngredient() {
.catch(() => { /* history is non-critical — leave the panel empty */ });
}, [id]);
+ // One batched request hydrates the whole page on mount: ingredient + refs +
+ // sources + relations + revisions + media + missing-media. Post-mutation
+ // updates still use the granular refreshRevisions / refreshMedia callbacks +
+ // optimistic relation state, so a single edit doesn't re-pull everything.
useEffect(() => {
let cancelled = false;
setLoading(true);
- getCatalogIngredient(id, { silent: true })
- .then((r) => {
+ getCatalogIngredientDetails(id, { silent: true })
+ .then((d) => {
if (cancelled) return;
- if (!r) {
+ if (!d?.ingredient) {
toast.error('Ingredient not found');
navigate('/catalog');
return;
}
- setRecord(r);
+ const r = d.ingredient;
+ setRecord({ ...r, refs: d.refs, sources: d.sources });
setName(r.name || '');
setTags(Array.isArray(r.tags) ? r.tags : []);
setPayload(r.payload && typeof r.payload === 'object' ? { ...r.payload } : {});
+ setRelations({
+ outbound: Array.isArray(d.relations?.outbound) ? d.relations.outbound : [],
+ inbound: Array.isArray(d.relations?.inbound) ? d.relations.inbound : [],
+ });
+ setRevisions(Array.isArray(d.revisions) ? d.revisions : []);
+ setMedia(Array.isArray(d.media) ? d.media : []);
+ setMissingMedia(new Set(Array.isArray(d.missingMedia) ? d.missingMedia.map((m) => m.mediaKey) : []));
setLoading(false);
- refreshRevisions();
})
.catch((err) => {
if (cancelled) return;
@@ -109,21 +119,7 @@ export default function CatalogIngredient() {
navigate('/catalog');
});
return () => { cancelled = true; };
- }, [id, navigate, refreshRevisions]);
-
- useEffect(() => {
- let cancelled = false;
- listCatalogIngredientRelations(id, { silent: true })
- .then((data) => {
- if (cancelled) return;
- setRelations({
- outbound: Array.isArray(data?.outbound) ? data.outbound : [],
- inbound: Array.isArray(data?.inbound) ? data.inbound : [],
- });
- })
- .catch(() => { if (!cancelled) setRelations({ outbound: [], inbound: [] }); });
- return () => { cancelled = true; };
- }, [id]);
+ }, [id, navigate]);
// Add an outbound edge (this ingredient → picked target). Optimistically
// appends to local state so the panel updates without a refetch.
@@ -175,8 +171,8 @@ export default function CatalogIngredient() {
.then((r) => setMissingMedia(new Set(Array.isArray(r?.missing) ? r.missing.map((m) => m.mediaKey) : [])))
.catch(() => { /* integrity overlay is best-effort */ });
}, [id]);
-
- useEffect(() => { refreshMedia(); }, [refreshMedia]);
+ // Initial media/relations/revisions are seeded by the batched details load
+ // above; refreshMedia / refreshRevisions only run after a mutation.
// Attach a media key (gallery filename) as a typed attachment. `kind` defaults
// to 'reference' for drag-drop / picker; the "set portrait" path routes
diff --git a/client/src/services/apiCatalog.js b/client/src/services/apiCatalog.js
index 02a8675029..d6453d9ccd 100644
--- a/client/src/services/apiCatalog.js
+++ b/client/src/services/apiCatalog.js
@@ -66,6 +66,12 @@ export const listCatalogIngredients = ({ type, tag, q, limit, offset, ...options
export const getCatalogIngredient = (id, options) =>
request(`/catalog/ingredients/${enc(id)}`, options);
+// Batched detail hydration — one request for ingredient + refs + sources +
+// relations + revisions + media + missingMedia, used by the detail page's
+// initial load in place of five separate calls.
+export const getCatalogIngredientDetails = (id, options) =>
+ request(`/catalog/ingredients/${enc(id)}/details`, options);
+
// --- Tags (canonical taxonomy) ------------------------------------------
// Autocomplete over the canonical catalog_tags table. `q` is an optional
diff --git a/server/routes/catalog.js b/server/routes/catalog.js
index 0c5aaabcec..29a42f34bf 100644
--- a/server/routes/catalog.js
+++ b/server/routes/catalog.js
@@ -198,6 +198,36 @@ router.get('/ingredients/:id', asyncHandler(async (req, res) => {
});
}));
+// Batched detail hydration: ONE round-trip for everything the detail page loads
+// on mount — refs + sources + relations + revisions + media + missing-media —
+// replacing five separate client requests. Post-mutation refreshes still hit the
+// granular endpoints (revisions / media) so a single attach or restore doesn't
+// re-pull the whole record. Embedding is stripped like the single-ingredient
+// route (opt in with `?includeEmbedding=true`).
+router.get('/ingredients/:id/details', asyncHandler(async (req, res) => {
+ const ing = await catalogDB.getIngredient(req.params.id);
+ if (!ing) throw new ServerError('Ingredient not found', { status: 404 });
+ const [refs, sources, relations, revisions, media, missingMedia] = await Promise.all([
+ catalogDB.listRefsForIngredient(req.params.id),
+ catalogDB.listSourcesForIngredient(req.params.id),
+ catalogDB.listRelationsForIngredient(req.params.id),
+ catalogDB.listIngredientRevisions(req.params.id, { limit: 50 }),
+ catalogDB.listMediaForIngredient(req.params.id),
+ catalogDB.getMissingMediaForIngredient(req.params.id),
+ ]);
+ const includeEmbedding = req.query.includeEmbedding === 'true';
+ const { embedding, ...rest } = ing;
+ res.json({
+ ingredient: includeEmbedding ? { ...rest, embedding } : rest,
+ refs,
+ sources,
+ relations, // { outbound, inbound }
+ revisions: Array.isArray(revisions?.items) ? revisions.items : [],
+ media, // array of attachments
+ missingMedia, // array of { mediaKey, ... }
+ });
+}));
+
router.post('/ingredients', asyncHandler(async (req, res) => {
validateRequest(catalogIngredientCreateSchema, req.body);
const ing = await catalogDB.createIngredient({
diff --git a/server/routes/catalog.test.js b/server/routes/catalog.test.js
index 47cd1ec32d..17391d7e71 100644
--- a/server/routes/catalog.test.js
+++ b/server/routes/catalog.test.js
@@ -193,3 +193,32 @@ describe.skipIf(!dbReady)('POST /api/catalog/ingredients/:id/revisions/:revision
expect(r.status).toBe(404);
});
});
+
+describe.skipIf(!dbReady)('GET /api/catalog/ingredients/:id/details — batched hydration', () => {
+ it('returns ingredient + refs + sources + relations + revisions + media + missingMedia in one response', async () => {
+ const a = await catalogDB.createIngredient({ type: 'character', name: `Details A ${NONCE}`, payload: { physicalDescription: 'lead' } });
+ const b = await catalogDB.createIngredient({ type: 'place', name: `Details B ${NONCE}`, payload: { description: 'a place' } });
+ createdIngredientIds.add(a.id);
+ createdIngredientIds.add(b.id);
+ await catalogDB.linkIngredientRelation(a.id, b.id, 'lives-in');
+ await catalogDB.updateIngredient(a.id, { payload: { physicalDescription: 'lead, edited' } }); // → a revision
+
+ const r = await request(makeApp()).get(`/api/catalog/ingredients/${a.id}/details`);
+ expect(r.status).toBe(200);
+ expect(r.body.ingredient.id).toBe(a.id);
+ expect(r.body.ingredient).not.toHaveProperty('embedding'); // stripped by default
+ expect(Array.isArray(r.body.refs)).toBe(true);
+ expect(Array.isArray(r.body.sources)).toBe(true);
+ expect(Array.isArray(r.body.media)).toBe(true);
+ expect(Array.isArray(r.body.missingMedia)).toBe(true);
+ expect(Array.isArray(r.body.revisions)).toBe(true);
+ expect(r.body.revisions.length).toBeGreaterThan(0); // the edit above
+ // The relation A→B shows up as an outbound edge to B.
+ expect(r.body.relations.outbound.some((e) => e.toId === b.id && e.kind === 'lives-in')).toBe(true);
+ });
+
+ it('404s for an unknown ingredient id', async () => {
+ const r = await request(makeApp()).get(`/api/catalog/ingredients/cat-chr-does-not-exist-${NONCE}/details`);
+ expect(r.status).toBe(404);
+ });
+});