diff --git a/skills/media-use/scripts/lib/cache.mjs b/skills/media-use/scripts/lib/cache.mjs index adde13b6e4..d2809b895e 100644 --- a/skills/media-use/scripts/lib/cache.mjs +++ b/skills/media-use/scripts/lib/cache.mjs @@ -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 }; } diff --git a/skills/media-use/scripts/lib/manifest.mjs b/skills/media-use/scripts/lib/manifest.mjs index fc60288582..ff35a4b638 100644 --- a/skills/media-use/scripts/lib/manifest.mjs +++ b/skills/media-use/scripts/lib/manifest.mjs @@ -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; diff --git a/skills/media-use/scripts/resolve.mjs b/skills/media-use/scripts/resolve.mjs index 0682c0920f..1a37e3b716 100644 --- a/skills/media-use/scripts/resolve.mjs +++ b/skills/media-use/scripts/resolve.mjs @@ -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) => {