feat(cli): index media-use assets for catalog search

This commit is contained in:
Miguel Angel Simon Sierra
2026-09-21 00:14:20 -04:00
parent a5f1f88546
commit fde415553d
7 changed files with 306 additions and 30 deletions
+67 -19
View File
@@ -32,6 +32,17 @@ import {
localVectorRevision,
} from "./catalog-artifact.js";
export interface MediaVectorRow {
id: string;
kind: string;
title: string;
description: string;
tags: string[];
file: string;
duration?: number;
dimensions?: { width: number; height: number };
}
/** Distinct from 1 so the pre-commit hook can tell "cannot" from "failed". */
const EXIT_NO_MODEL = 3;
@@ -40,6 +51,36 @@ function arg(name: string): string | undefined {
return index === -1 ? undefined : process.argv[index + 1];
}
function mediaRows(manifestPath: string): MediaVectorRow[] {
const parsed = JSON.parse(readFileSync(manifestPath, "utf8")) as
| MediaVectorRow[]
| Record<string, Omit<MediaVectorRow, "id" | "kind" | "title" | "tags"> & { tags?: string[] }>;
const bundledSfx = manifestPath.endsWith("skills/media-use/audio/assets/sfx/manifest.json");
const rootRelativeFile = (file: string): string =>
bundledSfx && !file.startsWith("skills/")
? `skills/media-use/audio/assets/sfx/${file}`
: file;
const rows = Array.isArray(parsed)
? parsed.map((row) => ({ ...row, file: rootRelativeFile(row.file) }))
: Object.entries(parsed).map(([id, entry]) => ({
id,
kind: "sfx",
title: id,
description: entry.description,
tags: entry.tags ?? ["sfx"],
file: rootRelativeFile(entry.file),
...(entry.duration === undefined ? {} : { duration: entry.duration }),
...(entry.dimensions === undefined ? {} : { dimensions: entry.dimensions }),
}));
if (rows.length === 0) throw new Error(`media manifest contains no rows: ${manifestPath}`);
for (const row of rows) {
if (!row.id || !row.file || !row.title || !row.description) {
throw new Error(`media manifest row is missing id, title, description, or file`);
}
}
return [...rows].sort((left, right) => left.id.localeCompare(right.id));
}
/**
* Embed in batches, in `names` order.
*
@@ -81,19 +122,24 @@ function packVectors(names: string[], vectors: number[][]): Float32Array {
async function main(): Promise<void> {
const dir = arg("artifact") ?? "registry/catalog-artifact";
const registryDir = arg("registry") ?? "registry";
const manifestPath = arg("manifest");
const basename = arg("output") ?? (manifestPath ? "media-vectors" : "local-vectors");
// Read the corpus straight from the registry rather than from a catalog.json
// built by the hosted-tier script. That file is not in the repo, so the
// documented regeneration command used to fail on a missing path, which is
// the whole reason the index was allowed to drift.
const catalogMap = catalogFromRegistry(
registryDir,
(path) => readFileSync(path, "utf-8"),
(path) =>
readdirSync(path, { withFileTypes: true })
.filter((e) => e.isDirectory())
.map((e) => e.name),
);
const rows = manifestPath ? mediaRows(manifestPath) : undefined;
const catalogMap = rows
? new Map(rows.map((row) => [row.id, `${row.title}\n${row.description}\n${row.tags.join(" ")}\n${row.kind}`]))
: catalogFromRegistry(
registryDir,
(path) => readFileSync(path, "utf-8"),
(path) =>
readdirSync(path, { withFileTypes: true })
.filter((e) => e.isDirectory())
.map((e) => e.name),
);
const catalog = Object.fromEntries(catalogMap);
const names = Object.keys(catalog).sort();
if (names.length === 0) throw new Error(`no registry items found under ${registryDir}`);
@@ -122,25 +168,27 @@ async function main(): Promise<void> {
const vectors = await embedInBatches(names, catalog, embedder);
const flat = packVectors(names, vectors);
writeFileSync(join(dir, "local-vectors.bin"), Buffer.from(flat.buffer));
writeFileSync(join(dir, `${basename}.bin`), Buffer.from(flat.buffer));
writeFileSync(
join(dir, "local-vectors.json"),
`${JSON.stringify({ model: LOCAL_MODEL_ID, modelRevision: LOCAL_MODEL_REVISION, dimensions: LOCAL_MODEL_DIMENSIONS, revision, names }, null, 2)}\n`,
);
const registryPath = join(registryDir, "registry.json");
const registry = JSON.parse(readFileSync(registryPath, "utf-8")) as RegistryManifest;
writeFileSync(
registryPath,
`${JSON.stringify({ ...registry, catalogArtifact: { revision } }, null, 2)}\n`,
join(dir, `${basename}.json`),
`${JSON.stringify({ model: LOCAL_MODEL_ID, modelRevision: LOCAL_MODEL_REVISION, dimensions: LOCAL_MODEL_DIMENSIONS, revision, names, ...(rows ? { rows } : {}) }, null, 2)}\n`,
);
if (!rows) {
const registryPath = join(registryDir, "registry.json");
const registry = JSON.parse(readFileSync(registryPath, "utf-8")) as RegistryManifest;
writeFileSync(
registryPath,
`${JSON.stringify({ ...registry, catalogArtifact: { revision } }, null, 2)}\n`,
);
}
const megabytes = (flat.byteLength / 1024 / 1024).toFixed(2);
console.log(`moves ${names.length}`);
console.log(`${rows ? "media" : "moves"} ${names.length}`);
console.log(`model ${LOCAL_MODEL_ID}`);
console.log(`dimensions ${LOCAL_MODEL_DIMENSIONS}`);
console.log(`revision ${revision}`);
console.log(`payload ${megabytes} MB`);
console.log(`written ${dir}`);
console.log(`written ${dir}/${basename}.{json,bin}`);
}
await main();
+32 -1
View File
@@ -23,9 +23,12 @@ import { catalogFromRegistry, localVectorRevision } from "./catalog-artifact.js"
type RegistryItem = { name: string; type?: string };
type Registry = { items: RegistryItem[]; catalogArtifact?: { revision?: string } };
type Artifact = { model?: string; dimensions?: number; revision?: string; names?: string[] };
type MediaArtifact = { rows?: Array<{ id?: string; file?: string }> };
const REGISTRY = "registry/registry.json";
const ARTIFACT = "registry/catalog-artifact/local-vectors.json";
const MEDIA_MANIFEST = "skills/media-use/audio/assets/sfx/manifest.json";
const MEDIA_ARTIFACT = "registry/catalog-artifact/media-vectors.json";
function read<T>(path: string): T {
try {
@@ -67,6 +70,23 @@ const expectedRevision = localVectorRevision(
);
const artifactRevisionMatches = artifact.revision === expectedRevision;
const registryRevisionMatches = registry.catalogArtifact?.revision === expectedRevision;
const mediaSource = read<Record<string, { file?: string }>>(MEDIA_MANIFEST);
const mediaArtifact = read<MediaArtifact>(MEDIA_ARTIFACT);
const mediaRows = new Map(
(mediaArtifact.rows ?? [])
.filter(
(row): row is { id: string; file: string } =>
typeof row.id === "string" && typeof row.file === "string",
)
.map((row) => [row.id, row.file]),
);
const missingMediaRows = Object.entries(mediaSource)
.filter(
([id, source]) =>
mediaRows.get(id) !== `skills/media-use/audio/assets/sfx/${source.file ?? ""}`,
)
.map(([id]) => id)
.sort();
const show = (names: string[]) =>
names
@@ -76,6 +96,12 @@ const show = (names: string[]) =>
console.log(`registry: ${registryNames.size} searchable items (blocks + components)`);
console.log(`artifact: ${artifactNames.size} vectors (${artifact.model ?? "unknown model"})`);
console.log(`media: ${mediaRows.size} rows for ${Object.keys(mediaSource).length} bundled SFX files`);
if (missingMediaRows.length > 0) {
console.error(`\n${missingMediaRows.length} bundled SFX file(s) have no matching media row:`);
console.error(show(missingMediaRows));
}
if (dropped.length > 0) {
// Not fatal: the CLI filters these before a user ever sees them.
@@ -96,7 +122,12 @@ if (!artifactRevisionMatches || !registryRevisionMatches) {
console.error(` registry: ${registry.catalogArtifact?.revision ?? "missing"}`);
}
if (unindexed.length > 0 || !artifactRevisionMatches || !registryRevisionMatches) {
if (
unindexed.length > 0 ||
!artifactRevisionMatches ||
!registryRevisionMatches ||
missingMediaRows.length > 0
) {
console.error(
"\nMeaning search is stale. Word search still uses the live registry.\n\n" +
"If you have the embedding model, regenerate and commit the artifact:\n" +