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

Link the music video cast import to same-named catalog records (#11933)

[._.]/99096a877154
diff --git a/client/src/pages/CreativeCastImport.jsx b/client/src/pages/CreativeCastImport.jsx
index f854d6334a..c9cbcc2d01 100644
--- a/client/src/pages/CreativeCastImport.jsx
+++ b/client/src/pages/CreativeCastImport.jsx
@@ -13,6 +13,7 @@ import { Link } from 'react-router';
import { ArrowLeft, Import, Loader2, Sparkles } from 'lucide-react';
import PageHeader from '../components/PageHeader';
import MediaImage from '../components/MediaImage';
+import IngredientPicker from '../components/IngredientPicker';
import toast from '../components/ui/Toast';
import { previewMusicVideoCastImport, commitMusicVideoCastImport } from '../services/apiCatalog';
import { libraryForType } from '../lib/creativeLibraries';
@@ -33,7 +34,9 @@ async function commitInChunks(selections) {
return totals;
}
-function ImportRow({ project, item, action, onChange }) {
+// A row's choice: the server's proposal unless the director changed it. A
+// record picked by hand is a link carrying its own id and name.
+function ImportRow({ project, item, choice, onChange, onPick }) {
const id = `cast-import-${project.projectId}-${item.subjectId}`;
const kindLabel = libraryForType(item.kind)?.singular || item.kind;
return (
@@ -50,13 +53,20 @@ function ImportRow({ project, item, action, onChange }) {
<label htmlFor={id} className="sr-only">What to do with {item.name}</label>
<select
id={id}
- value={action}
- onChange={(e) => onChange(e.target.value)}
+ value={choice.action === 'link' && choice.ingredientId !== item.ingredientId ? 'picked' : choice.action}
+ onChange={(e) => {
+ const value = e.target.value;
+ if (value === 'pick') onPick();
+ else if (value === 'link') onChange({ action: 'link', ingredientId: item.ingredientId, ingredientName: item.ingredientName });
+ else if (value !== 'picked') onChange({ action: value });
+ }}
className="max-w-[11rem] sm:max-w-xs bg-port-bg border border-port-border rounded px-2 py-1.5 text-sm text-white"
>
{item.action === 'link' && <option value="link">Use {item.ingredientName}</option>}
+ {choice.action === 'link' && choice.ingredientId !== item.ingredientId && <option value="picked">Use {choice.ingredientName}</option>}
<option value="create">Add as new</option>
<option value="local">Keep in this video only</option>
+ <option value="pick">Use another record…</option>
</select>
</li>
);
@@ -66,6 +76,7 @@ export default function CreativeCastImport() {
const [projects, setProjects] = useState(null);
const [decisions, setDecisions] = useState({});
const [importing, setImporting] = useState(false);
+ const [picking, setPicking] = useState(null);
const load = useCallback(async (signal) => {
const data = await previewMusicVideoCastImport({ signal, silent: true }).catch((err) => {
@@ -83,10 +94,12 @@ export default function CreativeCastImport() {
return () => controller.abort();
}, [load]);
- const actionFor = (project, item) => decisions[decisionKey(project.projectId, item.subjectId)] || item.action;
+ const choiceFor = (project, item) => decisions[decisionKey(project.projectId, item.subjectId)]
+ || { action: item.action, ingredientId: item.ingredientId, ingredientName: item.ingredientName };
+ const setChoice = (project, item, choice) => setDecisions((d) => ({ ...d, [decisionKey(project.projectId, item.subjectId)]: choice }));
const selections = useMemo(() => (projects || []).flatMap((project) => project.items.map((item) => {
- const action = decisions[decisionKey(project.projectId, item.subjectId)] || item.action;
- return { projectId: project.projectId, subjectId: item.subjectId, action, ...(action === 'link' ? { ingredientId: item.ingredientId } : {}) };
+ const choice = decisions[decisionKey(project.projectId, item.subjectId)] || item;
+ return { projectId: project.projectId, subjectId: item.subjectId, action: choice.action, ...(choice.action === 'link' ? { ingredientId: choice.ingredientId } : {}) };
})), [projects, decisions]);
const toCatalog = selections.filter((s) => s.action !== 'local').length;
@@ -145,8 +158,9 @@ export default function CreativeCastImport() {
key={item.subjectId}
project={project}
item={item}
- action={actionFor(project, item)}
- onChange={(action) => setDecisions((d) => ({ ...d, [decisionKey(project.projectId, item.subjectId)]: action }))}
+ choice={choiceFor(project, item)}
+ onChange={(choice) => setChoice(project, item, choice)}
+ onPick={() => setPicking({ project, item })}
/>
))}
</ul>
@@ -154,6 +168,15 @@ export default function CreativeCastImport() {
))}
</>
)}
+ <IngredientPicker
+ open={!!picking}
+ type={picking?.item.kind}
+ onClose={() => setPicking(null)}
+ onSelect={(ingredient) => {
+ if (ingredient && picking) setChoice(picking.project, picking.item, { action: 'link', ingredientId: ingredient.id, ingredientName: ingredient.name });
+ setPicking(null);
+ }}
+ />
</div>
);
}
diff --git a/client/src/pages/CreativeCastImport.test.jsx b/client/src/pages/CreativeCastImport.test.jsx
index deb5330d2f..20a6c6da98 100644
--- a/client/src/pages/CreativeCastImport.test.jsx
+++ b/client/src/pages/CreativeCastImport.test.jsx
@@ -7,6 +7,13 @@ vi.mock('../services/apiCatalog', () => ({
commitMusicVideoCastImport: vi.fn(),
}));
vi.mock('../components/ui/Toast', () => ({ default: { success: vi.fn(), error: vi.fn() } }));
+vi.mock('../components/IngredientPicker', () => ({
+ default: ({ open, type, onSelect }) => (open ? (
+ <div role="dialog" aria-label={`Pick ${type}`}>
+ <button type="button" onClick={() => onSelect({ id: 'cat-plc-9', type: 'place', name: 'Corner cafe' })}>Corner cafe</button>
+ </div>
+ ) : null),
+}));
vi.mock('../components/MediaImage', () => ({
default: ({ src, alt, ...rest }) => <img src={src} alt={alt} {...rest} />,
}));
@@ -59,4 +66,18 @@ describe('CreativeCastImport', () => {
await waitFor(() => expect(toast.success).toHaveBeenCalledWith('Imported: 501 new, 0 linked, 0 kept in their videos'));
expect(commitMusicVideoCastImport.mock.calls.map(([rows]) => rows.length)).toEqual([500, 1]);
});
+
+ it('links a row to a record the director picks', async () => {
+ previewMusicVideoCastImport.mockResolvedValueOnce(PREVIEW).mockResolvedValueOnce({ projects: [] });
+ commitMusicVideoCastImport.mockResolvedValue({ projects: 1, created: 1, linked: 2, kept: 0 });
+ renderPage();
+
+ fireEvent.change(await screen.findByLabelText('What to do with Night diner'), { target: { value: 'pick' } });
+ fireEvent.click(screen.getByRole('button', { name: 'Corner cafe' }));
+ expect(screen.getByLabelText('What to do with Night diner')).toHaveDisplayValue('Use Corner cafe');
+ fireEvent.click(screen.getByRole('button', { name: 'Import 3' }));
+
+ await waitFor(() => expect(commitMusicVideoCastImport).toHaveBeenCalledTimes(1));
+ expect(commitMusicVideoCastImport.mock.calls[0][0][2]).toEqual({ projectId: 'mv-1', subjectId: 'cs-set-diner', action: 'link', ingredientId: 'cat-plc-9' });
+ });
});
diff --git a/server/services/catalogDB.facets.db.test.js b/server/services/catalogDB.facets.db.test.js
index b7f9eb1351..f22436b105 100644
--- a/server/services/catalogDB.facets.db.test.js
+++ b/server/services/catalogDB.facets.db.test.js
@@ -209,3 +209,23 @@ describe.skipIf(!runDb)('catalogDB facets + album filters (#1762)', () => {
expect(facets.total).toBeGreaterThanOrEqual(4);
});
});
+
+describe.skipIf(!runDb)('catalogDB.findIngredientsByName', () => {
+ it('matches a live record of the type by trimmed, case-insensitive name or alias, oldest first', async () => {
+ const make = async (type, name, payload = {}) => {
+ const row = await catalogDB.createIngredient({ type, name, payload });
+ createdIngredientIds.add(row.id);
+ return row;
+ };
+ const first = await make('character', ` Juno Example ${nonce} `);
+ const aliased = await make('character', `The Drummer ${nonce}`, { aliases: [`juno example ${nonce}`] });
+ const otherType = await make('place', `Juno Example ${nonce}`);
+ const gone = await make('character', `Juno Example ${nonce}`);
+ await catalogDB.deleteIngredient(gone.id);
+
+ const found = await catalogDB.findIngredientsByName('character', [`JUNO EXAMPLE ${nonce}`]);
+ expect(found.map((r) => r.id)).toEqual([first.id, aliased.id]);
+ expect(found.map((r) => r.id)).not.toContain(otherType.id);
+ expect(await catalogDB.findIngredientsByName('character', [' '])).toEqual([]);
+ });
+});
diff --git a/server/services/catalogDB/ingredients.js b/server/services/catalogDB/ingredients.js
index 7f308c2325..83464f2421 100644
--- a/server/services/catalogDB/ingredients.js
+++ b/server/services/catalogDB/ingredients.js
@@ -465,6 +465,26 @@ export async function searchIngredientsByEmbedding(vector, { type, limit = 20, t
return result.rows.map((row) => ({ ingredient: rowToIngredient(row), score: parseFloat(row.score) }));
}
+/**
+ * Live records of `type` whose name or one of whose aliases equals one of
+ * `names` (trimmed, case-insensitive), oldest first. Used to link to an
+ * existing record instead of creating a same-named duplicate.
+ */
+export async function findIngredientsByName(type, names) {
+ const keys = [...new Set((names || []).map((n) => String(n || '').trim().toLowerCase()).filter(Boolean))];
+ if (!type || !keys.length) return [];
+ const result = await query(
+ `SELECT * FROM catalog_ingredients
+ WHERE deleted = false AND type = $1
+ AND (lower(btrim(name)) = ANY($2)
+ OR (jsonb_typeof(payload->'aliases') = 'array'
+ AND EXISTS (SELECT 1 FROM jsonb_array_elements_text(payload->'aliases') a WHERE lower(btrim(a)) = ANY($2))))
+ ORDER BY created_at, id`,
+ [type, keys],
+ );
+ return result.rows.map(rowToIngredient);
+}
+
export async function searchIngredientsByText(q, { type, limit = 20 } = {}) {
if (!q) return [];
const conditions = ['deleted = false', `search_tsv @@ websearch_to_tsquery('english', $1)`];
diff --git a/server/services/musicVideo/catalogCast.js b/server/services/musicVideo/catalogCast.js
index 319593e6af..2caa267bf6 100644
--- a/server/services/musicVideo/catalogCast.js
+++ b/server/services/musicVideo/catalogCast.js
@@ -20,6 +20,8 @@ const PRIMARY_KEY = { character: 'physicalDescription', place: 'description', ob
const UNIVERSE_FIELD = { character: 'characters', place: 'places', object: 'objects' };
const REFERENCE_ROLE = { character: 'character', place: 'set', object: 'prop' };
+const isMusicVideoCastId = (id, kind) => new RegExp(`^cat-${TYPE_PREFIX[kind]}-mv-[0-9a-f]{32}$`).test(String(id || ''));
+
function musicVideoCastIngredientId(projectId, subject) {
const seed = `music-video:${projectId}:${subject.kind}:${subject.id}`;
return `cat-${TYPE_PREFIX[subject.kind]}-mv-${createHash('sha256').update(seed).digest('hex').slice(0, 32)}`;
@@ -65,28 +67,39 @@ function catalogCastCandidates(project, { checkinOnly = false } = {}) {
}));
}
+const nameKey = (kind, name) => `${kind}:${String(name || '').trim().toLowerCase()}`;
+
/**
- * Decide each candidate: link to the universe entry it was cast from, link to
- * a same-named record of the video's universe, link to the row a synced machine
- * already saved, or create. Reads only.
+ * Decide each candidate: link to the universe entry it was cast from, to a
+ * same-named record of the video's universe, to any other same-named catalog
+ * record, or to the row a synced machine already saved; otherwise create.
+ * `claimed` carries the creates already proposed in this run, so a name shared
+ * by two videos is created once and linked from the other. Reads only.
*/
-async function planCatalogCast(project, candidates, deps) {
+async function planCatalogCast(project, candidates, deps, claimed = new Map()) {
const { catalogDB, getUniverse } = deps;
const universeId = project.concept?.universeId || null;
const universe = universeId ? await getUniverse(universeId).catch(() => null) : null;
const universeRows = universeId ? await catalogDB.listIngredientsForRef('universe', universeId) : [];
const plan = [];
for (const candidate of candidates) {
+ const key = nameKey(candidate.kind, candidate.name);
const canon = candidate.canonId && universe
? (universe[UNIVERSE_FIELD[candidate.kind]] || []).find((e) => e.id === candidate.canonId && e.ingredientId)
: null;
- const named = universeRows.find(({ ingredient }) => ingredient.type === candidate.kind
- && ingredient.name.trim().toLowerCase() === candidate.name.trim().toLowerCase())?.ingredient;
+ const inUniverse = universeRows.find(({ ingredient }) => nameKey(ingredient.type, ingredient.name) === key)?.ingredient;
const ownId = musicVideoCastIngredientId(project.id, { id: candidate.subjectId, kind: candidate.kind });
- const target = canon ? { id: canon.ingredientId, name: canon.name } : named || await catalogDB.getIngredient(ownId);
- plan.push(target
- ? { ...candidate, action: 'link', ingredientId: target.id, ingredientName: target.name }
- : { ...candidate, action: 'create', ingredientId: ownId });
+ // A canon entry keeps its ingredientId after its record is deleted.
+ const canonLive = canon ? await catalogDB.getIngredient(canon.ingredientId) : null;
+ const target = canonLive ? { id: canonLive.id, name: canonLive.name }
+ : inUniverse || (await catalogDB.findIngredientsByName(candidate.kind, [candidate.name]))[0]
+ || await catalogDB.getIngredient(ownId) || claimed.get(key);
+ if (target) {
+ plan.push({ ...candidate, action: 'link', ingredientId: target.id, ingredientName: target.name });
+ } else {
+ claimed.set(key, { id: ownId, name: candidate.name });
+ plan.push({ ...candidate, action: 'create', ingredientId: ownId });
+ }
}
return plan;
}
@@ -127,20 +140,36 @@ async function commitCatalogCast(projectId, decisions, deps) {
if (d.action === 'local') {
stamps.set(subject.id, { localOnly: true });
stats.kept++;
- } else if (d.action === 'create') {
- const item = { kind: subject.kind, name: subject.name, description: subject.description || '', imageId: images.get(subject.id) || null, ingredientId: musicVideoCastIngredientId(projectId, subject) };
+ continue;
+ }
+ if (d.action === 'link' && d.ingredientId) {
+ const target = await catalogDB.getIngredient(d.ingredientId);
+ if (target) {
+ if (target.type !== subject.kind) continue;
+ stamps.set(subject.id, { ingredientId: target.id });
+ stats.linked++;
+ continue;
+ }
+ // Only the record another video's subject was to create in this import
+ // (kept local there) is made here, at that same id: a later video then
+ // links to it, and a tombstone at the id blocks it. Any other missing
+ // target was deleted by the director and stays unlinked.
+ if (!isMusicVideoCastId(d.ingredientId, subject.kind)) continue;
+ const item = { kind: subject.kind, name: subject.name, description: subject.description || '', imageId: images.get(subject.id) || null, ingredientId: d.ingredientId };
if (await createRecord(project, item, catalogDB)) {
stamps.set(subject.id, { ingredientId: item.ingredientId });
stats.created++;
- } else {
- stamps.set(subject.id, { localOnly: true });
- stats.kept++;
}
- } else if (d.action === 'link' && d.ingredientId) {
- const target = await catalogDB.getIngredient(d.ingredientId);
- if (!target || target.type !== subject.kind) continue;
- stamps.set(subject.id, { ingredientId: target.id });
- stats.linked++;
+ continue;
+ }
+ if (d.action !== 'create') continue;
+ const item = { kind: subject.kind, name: subject.name, description: subject.description || '', imageId: images.get(subject.id) || null, ingredientId: musicVideoCastIngredientId(projectId, subject) };
+ if (await createRecord(project, item, catalogDB)) {
+ stamps.set(subject.id, { ingredientId: item.ingredientId });
+ stats.created++;
+ } else {
+ stamps.set(subject.id, { localOnly: true });
+ stats.kept++;
}
}
if (!stamps.size) return { project, ...stats };
@@ -186,10 +215,11 @@ export async function saveCheckinCastToCatalog(projectId, deps = null) {
export async function previewCatalogCastImport(deps = null) {
const d = deps || await defaultDeps();
const out = [];
+ const claimed = new Map();
for (const project of await d.listProjects()) {
const candidates = catalogCastCandidates(project);
if (!candidates.length) continue;
- out.push({ projectId: project.id, name: project.name || 'Untitled', items: await planCatalogCast(project, candidates, d) });
+ out.push({ projectId: project.id, name: project.name || 'Untitled', items: await planCatalogCast(project, candidates, d, claimed) });
}
return out;
}
diff --git a/server/services/musicVideo/catalogCast.test.js b/server/services/musicVideo/catalogCast.test.js
index 4df2222670..d2a66816af 100644
--- a/server/services/musicVideo/catalogCast.test.js
+++ b/server/services/musicVideo/catalogCast.test.js
@@ -24,6 +24,10 @@ function makeDeps({ projects, universes = {}, rows = [], universeLinks = {} }) {
}),
attachMedia: vi.fn(async (id, key, kind, opts) => { media.push({ id, key, kind, caption: opts?.caption }); }),
listIngredientsForRef: vi.fn(async (kind, refId) => (universeLinks[refId] || []).map((id) => ({ ingredient: ingredients.get(id), role: 'cast' }))),
+ findIngredientsByName: vi.fn(async (type, names) => {
+ const keys = names.map((n) => n.trim().toLowerCase());
+ return [...ingredients.values()].filter((r) => !r.deleted && r.type === type && keys.includes(r.name.trim().toLowerCase()));
+ }),
};
const deps = {
catalogDB,
@@ -184,3 +188,72 @@ describe('re-approving Cast & Sets', () => {
]);
});
});
+
+describe('matching before creating', () => {
+ const twoVideos = () => [
+ { id: 'mv-a', name: 'First', concept: { subjects: [{ id: 'mvc-1', kind: 'place', name: 'Tattoo Parlor' }, { id: 'mvc-2', kind: 'place', name: 'night diner' }] } },
+ { id: 'mv-b', name: 'Second', concept: { subjects: [{ id: 'mvc-9', kind: 'place', name: 'tattoo parlor ' }] } },
+ ];
+
+ it('links to a same-named record anywhere in the catalog, and creates a name two videos share only once', async () => {
+ const projects = twoVideos();
+ const { deps, ingredients } = makeDeps({ projects, rows: [{ id: 'cat-plc-bible-1', type: 'place', name: 'Night Diner' }] });
+
+ const preview = await previewCatalogCastImport(deps);
+ const firstId = musicVideoCastIngredientId('mv-a', { id: 'mvc-1', kind: 'place' });
+ expect(preview[0].items.map((i) => [i.subjectId, i.action, i.ingredientId])).toEqual([
+ ['mvc-1', 'create', firstId],
+ ['mvc-2', 'link', 'cat-plc-bible-1'],
+ ]);
+ expect(preview[1].items[0]).toMatchObject({ action: 'link', ingredientId: firstId, ingredientName: 'Tattoo Parlor' });
+
+ const selections = preview.flatMap((p) => p.items.map((i) => ({ projectId: p.projectId, subjectId: i.subjectId, action: i.action, ingredientId: i.ingredientId })));
+ expect(await commitCatalogCastImport(selections, deps)).toEqual({ projects: 2, created: 1, linked: 2, kept: 0 });
+ expect([...ingredients.keys()].filter((id) => id.includes('-mv-'))).toEqual([firstId]);
+ expect(projects[1].concept.subjects[0].ingredientId).toBe(firstId);
+ });
+
+ it('creates a shared name once in a later video when the first one is kept local', async () => {
+ const projects = [...twoVideos(), { id: 'mv-c', name: 'Third', concept: { subjects: [{ id: 'mvc-5', kind: 'place', name: 'Tattoo parlor' }] } }];
+ const { deps, ingredients } = makeDeps({ projects });
+ const preview = await previewCatalogCastImport(deps);
+ const sharedId = preview[0].items[0].ingredientId;
+ expect(preview.slice(1).map((p) => p.items[0].ingredientId)).toEqual([sharedId, sharedId]);
+
+ const totals = await commitCatalogCastImport([
+ { projectId: 'mv-a', subjectId: 'mvc-1', action: 'local' },
+ { projectId: 'mv-b', subjectId: 'mvc-9', action: 'link', ingredientId: sharedId },
+ { projectId: 'mv-c', subjectId: 'mvc-5', action: 'link', ingredientId: sharedId },
+ ], deps);
+
+ expect(totals).toMatchObject({ created: 1, linked: 1, kept: 1 });
+ expect([...ingredients.keys()].filter((id) => id.includes('-mv-'))).toEqual([sharedId]);
+ expect([projects[1], projects[2]].map((p) => p.concept.subjects[0].ingredientId)).toEqual([sharedId, sharedId]);
+ });
+
+ it('never brings back a record the director deleted', async () => {
+ const projects = [{ id: 'mv-a', name: 'First', concept: { universeId: 'u-1', subjects: [
+ { id: 'mvc-1', kind: 'character', name: 'Example Lead', canonId: 'c1' },
+ { id: 'mvc-2', kind: 'character', name: 'Example Extra' },
+ ] } }];
+ const pickedId = musicVideoCastIngredientId('mv-z', { id: 'x', kind: 'character' });
+ const { deps, ingredients } = makeDeps({
+ projects,
+ universes: { 'u-1': { id: 'u-1', characters: [{ id: 'c1', name: 'Example Lead', ingredientId: 'cat-chr-canon' }] } },
+ rows: [{ id: 'cat-chr-canon', type: 'character', name: 'Example Lead', deleted: true }, { id: pickedId, type: 'character', name: 'Gone', deleted: true }],
+ });
+
+ // The deleted canon record is not proposed.
+ const [preview] = await previewCatalogCastImport(deps);
+ expect(preview.items[0]).toMatchObject({ action: 'create' });
+
+ const totals = await commitCatalogCastImport([
+ { projectId: 'mv-a', subjectId: 'mvc-1', action: 'link', ingredientId: 'cat-chr-canon' },
+ { projectId: 'mv-a', subjectId: 'mvc-2', action: 'link', ingredientId: pickedId },
+ ], deps);
+
+ expect(totals).toEqual({ projects: 0, created: 0, linked: 0, kept: 0 });
+ expect(ingredients.size).toBe(2);
+ expect(projects[0].concept.subjects.every((s) => !s.ingredientId)).toBe(true);
+ });
+});