UNPKG

@noanswer/context-compose

Version:

Orchestrate complex AI interactions with Context Compose. A powerful CLI and server for building, validating, and managing context for large language models using the Model Context Protocol (MCP).

50 lines 1.56 kB
import { readFile } from 'node:fs/promises'; import { parse as parseYaml } from 'yaml'; import { FileNotFoundError, YamlParseError } from '../errors.js'; import { yamlCache } from './cache.js'; import { fileExists } from './index.js'; /** * Read and parse a YAML file asynchronously with caching. */ export async function readYamlFile(filePath) { // Check cache first const cacheKey = `yaml:${filePath}`; const cached = await yamlCache.get(cacheKey, filePath); if (cached !== undefined) { return cached; } if (!(await fileExists(filePath))) { throw new FileNotFoundError(filePath); } try { const content = await readFile(filePath, 'utf8'); const parsed = parseYaml(content); // Cache the parsed result await yamlCache.set(cacheKey, parsed, filePath); return parsed; } catch (error) { throw new YamlParseError(filePath, error instanceof Error ? error : new Error(String(error))); } } /** * Read and parse multiple YAML files in parallel with caching. */ export async function readYamlFiles(filePaths) { const promises = filePaths.map((filePath) => readYamlFile(filePath)); return Promise.all(promises); } /** * Preload YAML files into cache */ export async function preloadYamlFiles(filePaths) { await Promise.all(filePaths.map(async (filePath) => { try { await readYamlFile(filePath); } catch { // Ignore errors during preloading } })); } //# sourceMappingURL=yaml.js.map