refactor(media-use): ponytail cleanup — dedup cache, stdlib extname

- cache.mjs: replace duplicated readGlobalManifest/appendGlobalRecord
  with readManifest(globalMediaDir())/appendRecord(globalMediaDir(), ...)
- cache.mjs: extract validateCacheHit to deduplicate sentinel check
- resolve.mjs: replace local typeSubdir with import from manifest.mjs
- resolve.mjs: replace hand-rolled extFromCachedPath/extFromUrl with
  path.extname (stdlib)
- manifest.mjs: export typeSubdir, remove no-op voice ternary in nextId

net: -40 lines
This commit is contained in:
Miguel Ángel
2026-06-22 15:57:55 -04:00
parent 6b9950dfa8
commit 0fa49c57fe
3 changed files with 32 additions and 91 deletions
+17 -54
View File
@@ -8,7 +8,7 @@ import {
import { join, basename } from "node:path";
import { createHash } from "node:crypto";
import { homedir } from "node:os";
import { readManifest } from "./manifest.mjs";
import { readManifest, appendRecord } from "./manifest.mjs";
const SCHEMA_PREFIX = "mu-v1-";
const KEY_HEX_CHARS = 16;
@@ -36,69 +36,32 @@ function markComplete(entryDir) {
}
function readGlobalManifest() {
const dir = globalMediaDir();
const p = join(dir, "manifest.jsonl");
if (!existsSync(p)) return [];
const raw = readFileSync(p, "utf8");
const records = [];
for (const line of raw.split(/\r?\n/)) {
const trimmed = line.trim();
if (!trimmed) continue;
try {
records.push(JSON.parse(trimmed));
} catch {
// skip malformed
}
}
return records;
return readManifest(globalMediaDir());
}
function appendGlobalRecord(record) {
const dir = globalMediaDir();
mkdirSync(dir, { recursive: true });
const p = join(dir, "manifest.jsonl");
const line = JSON.stringify(record) + "\n";
if (existsSync(p)) {
const existing = readFileSync(p, "utf8");
const sep = existing.length > 0 && !existing.endsWith("\n") ? "\n" : "";
writeFileSync(p, existing + sep + line);
} else {
writeFileSync(p, line);
}
function validateCacheHit(match) {
if (!match?.sha) return null;
return isComplete(cacheEntryDir(globalMediaDir(), match.sha)) ? match : null;
}
export function cacheGet(prompt, type) {
const records = readGlobalManifest();
const match = records.find(
(r) =>
r.reusable &&
r.provenance?.prompt === prompt &&
(type == null || r.type === type),
return validateCacheHit(
readGlobalManifest().find(
(r) =>
r.reusable &&
r.provenance?.prompt === prompt &&
(type == null || r.type === type),
),
);
if (!match) return null;
const sha = match.sha;
if (!sha) return null;
const entryDir = cacheEntryDir(globalMediaDir(), sha);
if (!isComplete(entryDir)) return null;
return match;
}
export function cacheGetByEntity(entity) {
const lower = entity.toLowerCase();
const records = readGlobalManifest();
const match = records.find(
(r) => r.reusable && r.entity && r.entity.toLowerCase() === lower,
return validateCacheHit(
readGlobalManifest().find(
(r) => r.reusable && r.entity && r.entity.toLowerCase() === lower,
),
);
if (!match) return null;
const sha = match.sha;
if (!sha) return null;
const entryDir = cacheEntryDir(globalMediaDir(), sha);
if (!isComplete(entryDir)) return null;
return match;
}
export function cachePut(filePath, record) {
@@ -117,7 +80,7 @@ export function cachePut(filePath, record) {
reusable: true,
cached_path: dest,
};
appendGlobalRecord(globalRecord);
appendRecord(globalMediaDir(), globalRecord);
return { sha, cached_path: dest };
}
+7 -3
View File
@@ -25,10 +25,14 @@ export function indexPath(projectDir) {
return join(mediaDir(projectDir), INDEX_FILE);
}
export function typeDirPath(projectDir, type) {
export function typeSubdir(type) {
const sub = TYPE_DIRS[type];
if (!sub) throw new Error(`unknown media type: ${type}`);
return join(mediaDir(projectDir), sub);
return sub;
}
export function typeDirPath(projectDir, type) {
return join(mediaDir(projectDir), typeSubdir(type));
}
export function readManifest(projectDir) {
@@ -85,7 +89,7 @@ export function findByEntity(projectDir, entity) {
export function nextId(projectDir, type) {
const records = readManifest(projectDir);
const prefix = type === "voice" ? "voice" : type;
const prefix = type;
let max = 0;
for (const r of records) {
if (r.type !== type) continue;
+8 -34
View File
@@ -1,13 +1,14 @@
#!/usr/bin/env node
import { existsSync } from "node:fs";
import { resolve, join } from "node:path";
import { resolve, join, extname } from "node:path";
import { parseArgs } from "node:util";
import {
appendRecord,
findByPrompt,
findByEntity,
nextId,
typeSubdir,
} from "./lib/manifest.mjs";
import { regenerateIndex } from "./lib/index-gen.mjs";
import {
@@ -77,7 +78,7 @@ async function run() {
const cacheHit = cacheGet(intent, type);
if (cacheHit) {
const id = nextId(projectDir, type);
const ext = extFromCachedPath(cacheHit.cached_path);
const ext = extname(cacheHit.cached_path);
const localPath = `.media/${typeSubdir(type)}/${id}${ext}`;
const imported = importFromCache(cacheHit, projectDir, id, localPath);
if (imported) {
@@ -91,7 +92,7 @@ async function run() {
const entityCacheHit = cacheGetByEntity(entity);
if (entityCacheHit && entityCacheHit.type === type) {
const id = nextId(projectDir, type);
const ext = extFromCachedPath(entityCacheHit.cached_path);
const ext = extname(entityCacheHit.cached_path);
const localPath = `.media/${typeSubdir(type)}/${id}${ext}`;
const imported = importFromCache(entityCacheHit, projectDir, id, localPath);
if (imported) {
@@ -186,45 +187,18 @@ function formatMeta(record, source) {
return parts.join(", ");
}
function typeSubdir(type) {
const map = {
bgm: "audio/bgm",
sfx: "audio/sfx",
voice: "audio/voice",
image: "images",
icon: "images",
brand: "images",
};
return map[type] || type;
}
function extFromUrl(url) {
try {
const pathname = new URL(url).pathname;
const dot = pathname.lastIndexOf(".");
if (dot >= 0) return pathname.slice(dot);
return extname(new URL(url).pathname) || null;
} catch {
// not a valid URL
return null;
}
return null;
}
function extFromCachedPath(cachedPath) {
if (!cachedPath) return defaultExt("bgm");
const dot = cachedPath.lastIndexOf(".");
return dot >= 0 ? cachedPath.slice(dot) : "";
}
const DEFAULT_EXT = { bgm: ".wav", sfx: ".mp3", voice: ".wav", image: ".jpg", icon: ".svg", brand: ".png" };
function defaultExt(type) {
const map = {
bgm: ".wav",
sfx: ".mp3",
voice: ".wav",
image: ".jpg",
icon: ".svg",
brand: ".png",
};
return map[type] || ".bin";
return DEFAULT_EXT[type] || ".bin";
}
run().catch((err) => {