dsh-brain/lib/skills/shared.js

74 lines
3.2 KiB
JavaScript

// Skill bundle member — the shared on-disk skill format helpers.
// Both sides of the seam speak this vocabulary: the repository and admin
// validate folder layouts with it, and the sync engine reuses the same
// constants when it prepares uploads. Moved verbatim from skill-sync.ts.
import { createHash } from 'node:crypto';
export const CLOUD_SKILL_NAME = /^[a-z0-9][a-z0-9-]{0,63}$/;
/** Layout contract of the cloud skill center (mirrors its store rules). */
export const TEXT_EXT = new Set(['.md', '.txt', '.json', '.yaml', '.yml', '.py', '.sh', '.ps1', '.mjs', '.js', '.ts', '.tsx', '.css', '.html', '.csv']);
export const BINARY_EXT = new Set(['.docx', '.xlsx', '.pptx', '.pdf', '.png', '.jpg', '.jpeg', '.gif', '.svg', '.zip']);
export const SCRIPT_EXT = new Set(['.py', '.sh', '.ps1', '.mjs']);
const SKILL_DIRS = new Set(['references', 'scripts', 'assets']);
const SEGMENT = /^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$/;
export const MAX_FILES = 100;
export const MAX_FILE_BYTES = 512 * 1024;
export const MAX_SKILL_BYTES = 2 * 1024 * 1024;
export const MAX_DEPTH = 3;
/** SHA-256 of a buffer or UTF-8 string, hex-encoded. */
export function sha256Hex(data) {
return createHash('sha256').update(data).digest('hex');
}
/**
* The center's linked bundle manifest: one `path\0sha256` line per file,
* sorted, under a version header. Byte-identical to the platform's own
* computation, so locally computed manifests compare directly.
*/
export function linkedSkillManifest(files) {
const lines = files.map((file) => `${file.path}\0${file.sha256}`).sort();
return sha256Hex(Buffer.from(`cloud-memory-skill-v1\n${lines.join('\n')}\n`, 'utf8'));
}
/** True when the cloud center would accept this relative file path. */
export function isCloudSkillPath(rel) {
if (rel === '' || rel.length > 200 || rel.startsWith('/') || rel.includes('\\') || rel.includes('//'))
return false;
const segments = rel.split('/');
if (segments.length > MAX_DEPTH)
return false;
for (const segment of segments) {
if (!SEGMENT.test(segment) || segment === '.' || segment === '..')
return false;
}
const dot = rel.lastIndexOf('.');
const ext = dot === -1 ? '' : rel.slice(dot).toLowerCase();
if (!TEXT_EXT.has(ext) && !BINARY_EXT.has(ext))
return false;
const first = segments[0];
if (segments.length === 1)
return rel === 'SKILL.md';
if (first === undefined || !SKILL_DIRS.has(first))
return false;
if (first === 'scripts' && !SCRIPT_EXT.has(ext))
return false;
return true;
}
/** Minimal frontmatter reader for `name` / `description` (plain scalars). */
export function parseSkillFrontmatter(markdown) {
if (!markdown.startsWith('---'))
return {};
const end = markdown.indexOf('\n---', 3);
if (end === -1)
return {};
const out = {};
for (const line of markdown.slice(3, end).split('\n')) {
const match = /^\s*(name|description)\s*:\s*(.+?)\s*$/.exec(line);
if (match === null)
continue;
const value = (match[2] ?? '').replace(/^["']|["']$/g, '');
if (match[1] === 'name' && value !== '')
out.name = value;
if (match[1] === 'description' && value !== '')
out.description = value;
}
return out;
}