we only need one yaml lib

This commit is contained in:
Brian Madison
2025-12-13 18:35:07 +08:00
parent ce42d56fdd
commit 8642553bd7
27 changed files with 130 additions and 96 deletions

View File

@@ -1,4 +1,4 @@
const yaml = require('js-yaml');
const yaml = require('yaml');
const fs = require('fs-extra');
/**
@@ -91,7 +91,7 @@ class AgentAnalyzer {
*/
async analyzeAgentFile(filePath) {
const content = await fs.readFile(filePath, 'utf8');
const agentYaml = yaml.load(content);
const agentYaml = yaml.parse(content);
return this.analyzeAgentObject(agentYaml);
}

View File

@@ -313,7 +313,11 @@ async function compileAgent(yamlContent, answers = {}, agentName = '', targetPat
// Apply customization merges before template processing
// Handle metadata overrides (like name)
if (answers.metadata) {
agentYaml.agent.metadata = { ...agentYaml.agent.metadata, ...answers.metadata };
// Filter out empty values from metadata
const filteredMetadata = filterCustomizationData(answers.metadata);
if (Object.keys(filteredMetadata).length > 0) {
agentYaml.agent.metadata = { ...agentYaml.agent.metadata, ...filteredMetadata };
}
// Remove from answers so it doesn't get processed as template variables
const { metadata, ...templateAnswers } = answers;
answers = templateAnswers;
@@ -353,6 +357,36 @@ async function compileAgent(yamlContent, answers = {}, agentName = '', targetPat
};
}
/**
* Filter customization data to remove empty/null values
* @param {Object} data - Raw customization data
* @returns {Object} Filtered customization data
*/
function filterCustomizationData(data) {
const filtered = {};
for (const [key, value] of Object.entries(data)) {
if (value === null || value === undefined || value === '') {
continue; // Skip null/undefined/empty values
}
if (Array.isArray(value)) {
if (value.length > 0) {
filtered[key] = value;
}
} else if (typeof value === 'object') {
const nested = filterCustomizationData(value);
if (Object.keys(nested).length > 0) {
filtered[key] = nested;
}
} else {
filtered[key] = value;
}
}
return filtered;
}
/**
* Process TTS injection markers in content
* @param {string} content - Content to process
@@ -431,4 +465,5 @@ module.exports = {
buildPersonaXml,
buildPromptsXml,
buildMenuXml,
filterCustomizationData,
};

View File

@@ -1,5 +1,5 @@
const fs = require('fs-extra');
const yaml = require('js-yaml');
const yaml = require('yaml');
const path = require('node:path');
const packageJson = require('../../../package.json');
@@ -18,7 +18,7 @@ class Config {
}
const content = await fs.readFile(configPath, 'utf8');
return yaml.load(content);
return yaml.parse(content);
}
/**

View File

@@ -1,6 +1,6 @@
const fs = require('fs-extra');
const path = require('node:path');
const yaml = require('js-yaml');
const yaml = require('yaml');
const { getProjectRoot } = require('./project-root');
/**
@@ -20,7 +20,7 @@ class PlatformCodes {
try {
if (fs.existsSync(this.configPath)) {
const content = fs.readFileSync(this.configPath, 'utf8');
this.config = yaml.load(content);
this.config = yaml.parse(content);
} else {
console.warn(`Platform codes config not found at ${this.configPath}`);
this.config = { platforms: {} };

View File

@@ -1,6 +1,6 @@
const fs = require('node:fs');
const path = require('node:path');
const yaml = require('js-yaml');
const yaml = require('yaml');
const { execSync } = require('node:child_process');
// Dynamic import for ES module
@@ -57,11 +57,10 @@ async function formatYamlContent(content, filename) {
}
// Parse and re-dump YAML to format it
const parsed = yaml.load(fixedContent);
const formatted = yaml.dump(parsed, {
const parsed = yaml.parse(fixedContent);
const formatted = yaml.stringify(parsed, {
indent: 2,
lineWidth: -1, // Disable line wrapping
noRefs: true,
lineWidth: 0, // Disable line wrapping
sortKeys: false, // Preserve key order
});
// Ensure POSIX-compliant final newline
@@ -96,7 +95,6 @@ async function processMarkdownFile(filePath) {
const [fullMatch, yamlContent] = match;
const formatted = await formatYamlContent(yamlContent, filePath);
if (formatted !== null) {
// Remove trailing newline that js-yaml adds
const trimmedFormatted = formatted.replace(/\n$/, '');
if (trimmedFormatted !== yamlContent) {

View File

@@ -1,4 +1,4 @@
const yaml = require('js-yaml');
const yaml = require('yaml');
const fs = require('fs-extra');
const path = require('node:path');
const crypto = require('node:crypto');
@@ -64,13 +64,13 @@ class YamlXmlBuilder {
async loadAndMergeAgent(agentYamlPath, customizeYamlPath = null) {
// Load base agent
const agentContent = await fs.readFile(agentYamlPath, 'utf8');
const agentYaml = yaml.load(agentContent);
const agentYaml = yaml.parse(agentContent);
// Load customization if exists
let merged = agentYaml;
if (customizeYamlPath && (await fs.pathExists(customizeYamlPath))) {
const customizeContent = await fs.readFile(customizeYamlPath, 'utf8');
const customizeYaml = yaml.load(customizeContent);
const customizeYaml = yaml.parse(customizeContent);
if (customizeYaml) {
// Special handling: persona fields are merged, but only non-empty values override