export interface ClawclawLocalizedResourceText { readonly name: string; readonly description: string; } export type ClawclawResourceLocalizations = Readonly< Record >; export interface ClawclawResourceMetadata { readonly id: string; readonly name?: string; readonly description: string; /** Host-owned human-facing text; never replaces the script's Agent description. */ readonly display?: ClawclawLocalizedResourceText; readonly localizations?: ClawclawResourceLocalizations; } export interface ClawclawResolvedResourceMetadata { readonly id: string; readonly name: string; readonly description: string; } function normalizedLocale(value: string): string { return value.trim().replaceAll('_', '-').toLowerCase(); } export function hasValidClawclawResourceLocalizations( localizations: ClawclawResourceLocalizations | undefined, ): boolean { if (localizations === undefined) return true; if (!localizations || typeof localizations !== 'object' || Array.isArray(localizations)) return false; return Object.entries(localizations).every(([locale, text]) => ( normalizedLocale(locale).length > 0 && !!text && typeof text === 'object' && !Array.isArray(text) && typeof text.name === 'string' && text.name.trim().length > 0 && typeof text.description === 'string' && text.description.trim().length > 0 )); } /** Resolves localized display text while preserving manifest text as the fallback. */ export function resolveClawclawResourceMetadata( metadata: ClawclawResourceMetadata, locale: string, ): ClawclawResolvedResourceMetadata { if (metadata.display) return { id: metadata.id, ...metadata.display }; const requested = normalizedLocale(locale); const entries = Object.entries(metadata.localizations ?? {}); const exact = entries.find(([key]) => normalizedLocale(key) === requested)?.[1]; const language = requested.split('-')[0]; const languageMatch = entries.find(([key]) => normalizedLocale(key) === language)?.[1]; const regionalMatch = entries.find(([key]) => ( normalizedLocale(key).startsWith(`${language}-`) ))?.[1]; const localized = exact ?? languageMatch ?? regionalMatch; return { id: metadata.id, name: localized?.name ?? metadata.name ?? metadata.id, description: localized?.description ?? metadata.description, }; }