fix: centralized yamlExtraction function and all now fix character issues for windows
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
const fs = require("node:fs").promises;
|
||||
const path = require("node:path");
|
||||
const DependencyResolver = require("../lib/dependency-resolver");
|
||||
const yamlUtils = require("../lib/yaml-utils");
|
||||
|
||||
class WebBuilder {
|
||||
constructor(options = {}) {
|
||||
@@ -111,10 +112,12 @@ class WebBuilder {
|
||||
|
||||
processAgentContent(content) {
|
||||
// First, replace content before YAML with the template
|
||||
const yamlContent = yamlUtils.extractYamlFromAgent(content);
|
||||
if (!yamlContent) return content;
|
||||
|
||||
const yamlMatch = content.match(/```ya?ml\n([\s\S]*?)\n```/);
|
||||
if (!yamlMatch) return content;
|
||||
|
||||
const yamlContent = yamlMatch[1];
|
||||
|
||||
const yamlStartIndex = content.indexOf(yamlMatch[0]);
|
||||
const yamlEndIndex = yamlStartIndex + yamlMatch[0].length;
|
||||
|
||||
@@ -294,11 +297,11 @@ class WebBuilder {
|
||||
sections.push(this.formatSection(`agents#${agentName}`, agentContent));
|
||||
|
||||
// Resolve and add agent dependencies
|
||||
const agentYaml = agentContent.match(/```yaml\n([\s\S]*?)\n```/);
|
||||
if (agentYaml) {
|
||||
const yamlContent = yamlUtils.extractYamlFromAgent(agentContent);
|
||||
if (yamlContent) {
|
||||
try {
|
||||
const yaml = require("js-yaml");
|
||||
const agentConfig = yaml.load(agentYaml[1]);
|
||||
const agentConfig = yaml.load(yamlContent);
|
||||
|
||||
if (agentConfig.dependencies) {
|
||||
// Add resources, first try expansion pack, then core
|
||||
@@ -474,13 +477,9 @@ class WebBuilder {
|
||||
sections.push(this.formatSection(`agents#${agentId}`, coreAgentContent));
|
||||
|
||||
// Parse and collect dependencies from core agent
|
||||
const agentYaml = coreAgentContent.match(/```yaml\n([\s\S]*?)\n```/);
|
||||
if (agentYaml) {
|
||||
const yamlContent = yamlUtils.extractYamlFromAgent(coreAgentContent, true);
|
||||
if (yamlContent) {
|
||||
try {
|
||||
// Clean up the YAML to handle command descriptions after dashes
|
||||
let yamlContent = agentYaml[1];
|
||||
yamlContent = yamlContent.replace(/^(\s*-)(\s*"[^"]+")(\s*-\s*.*)$/gm, "$1$2");
|
||||
|
||||
const agentConfig = this.parseYaml(yamlContent);
|
||||
if (agentConfig.dependencies) {
|
||||
for (const [resourceType, resources] of Object.entries(agentConfig.dependencies)) {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
const fs = require('fs-extra');
|
||||
const path = require('path');
|
||||
const yaml = require('js-yaml');
|
||||
const { extractYamlFromAgent } = require('../../lib/yaml-utils');
|
||||
|
||||
class ConfigLoader {
|
||||
constructor() {
|
||||
@@ -41,9 +42,9 @@ class ConfigLoader {
|
||||
const agentContent = await fs.readFile(agentPath, 'utf8');
|
||||
|
||||
// Extract YAML block from agent file
|
||||
const yamlMatch = agentContent.match(/```yaml\n([\s\S]*?)\n```/);
|
||||
if (yamlMatch) {
|
||||
const yamlContent = yaml.load(yamlMatch[1]);
|
||||
const yamlContentText = extractYamlFromAgent(agentContent);
|
||||
if (yamlContentText) {
|
||||
const yamlContent = yaml.load(yamlContentText);
|
||||
const agentConfig = yamlContent.agent || {};
|
||||
|
||||
agents.push({
|
||||
|
||||
@@ -3,6 +3,7 @@ const fs = require("fs-extra");
|
||||
const yaml = require("js-yaml");
|
||||
const fileManager = require("./file-manager");
|
||||
const configLoader = require("./config-loader");
|
||||
const { extractYamlFromAgent } = require("../../lib/yaml-utils");
|
||||
|
||||
// Dynamic import for ES module
|
||||
let chalk;
|
||||
@@ -100,9 +101,9 @@ class IdeSetup {
|
||||
"CRITICAL: Read the full YML, start activation to alter your state of being, follow startup section instructions, stay in this being until told to exit this mode:\n\n";
|
||||
mdcContent += "```yaml\n";
|
||||
// Extract just the YAML content from the agent file
|
||||
const yamlMatch = agentContent.match(/```ya?ml\n([\s\S]*?)```/);
|
||||
if (yamlMatch) {
|
||||
mdcContent += yamlMatch[1].trim();
|
||||
const yamlContent = extractYamlFromAgent(agentContent);
|
||||
if (yamlContent) {
|
||||
mdcContent += yamlContent;
|
||||
} else {
|
||||
// If no YAML found, include the whole content minus the header
|
||||
mdcContent += agentContent.replace(/^#.*$/m, "").trim();
|
||||
@@ -182,9 +183,9 @@ class IdeSetup {
|
||||
"CRITICAL: Read the full YML, start activation to alter your state of being, follow startup section instructions, stay in this being until told to exit this mode:\n\n";
|
||||
mdContent += "```yaml\n";
|
||||
// Extract just the YAML content from the agent file
|
||||
const yamlMatch = agentContent.match(/```ya?ml\n([\s\S]*?)```/);
|
||||
if (yamlMatch) {
|
||||
mdContent += yamlMatch[1].trim();
|
||||
const yamlContent = extractYamlFromAgent(agentContent);
|
||||
if (yamlContent) {
|
||||
mdContent += yamlContent;
|
||||
} else {
|
||||
// If no YAML found, include the whole content minus the header
|
||||
mdContent += agentContent.replace(/^#.*$/m, "").trim();
|
||||
@@ -430,9 +431,9 @@ class IdeSetup {
|
||||
"When the user types `@" + agentId + "`, adopt this persona and follow these guidelines:\n\n";
|
||||
mdContent += "```yaml\n";
|
||||
// Extract just the YAML content from the agent file
|
||||
const yamlMatch = agentContent.match(/```ya?ml\n([\s\S]*?)```/);
|
||||
if (yamlMatch) {
|
||||
mdContent += yamlMatch[1].trim();
|
||||
const yamlContent = extractYamlFromAgent(agentContent);
|
||||
if (yamlContent) {
|
||||
mdContent += yamlContent;
|
||||
} else {
|
||||
// If no YAML found, include the whole content minus the header
|
||||
mdContent += agentContent.replace(/^#.*$/m, "").trim();
|
||||
|
||||
@@ -2,6 +2,7 @@ const path = require("node:path");
|
||||
const fileManager = require("./file-manager");
|
||||
const configLoader = require("./config-loader");
|
||||
const ideSetup = require("./ide-setup");
|
||||
const { extractYamlFromAgent } = require("../../lib/yaml-utils");
|
||||
|
||||
// Dynamic imports for ES modules
|
||||
let chalk, ora, inquirer;
|
||||
@@ -1222,10 +1223,10 @@ class Installer {
|
||||
const agentContent = await fs.readFile(agentPath, 'utf8');
|
||||
|
||||
// Extract YAML frontmatter to check dependencies
|
||||
const yamlMatch = agentContent.match(/```yaml\n([\s\S]*?)```/);
|
||||
if (yamlMatch) {
|
||||
const yamlContent = extractYamlFromAgent(agentContent);
|
||||
if (yamlContent) {
|
||||
try {
|
||||
const agentConfig = yaml.parse(yamlMatch[1]);
|
||||
const agentConfig = yaml.parse(yamlContent);
|
||||
const dependencies = agentConfig.dependencies || {};
|
||||
|
||||
// Check for core dependencies (those that don't exist in the expansion pack)
|
||||
@@ -1314,13 +1315,10 @@ class Installer {
|
||||
|
||||
// Now resolve this agent's dependencies too
|
||||
const agentContent = await fs.readFile(coreAgentPath, 'utf8');
|
||||
const yamlMatch = agentContent.match(/```ya?ml\n([\s\S]*?)```/);
|
||||
const yamlContent = extractYamlFromAgent(agentContent, true);
|
||||
|
||||
if (yamlMatch) {
|
||||
if (yamlContent) {
|
||||
try {
|
||||
// Clean up the YAML to handle command descriptions
|
||||
let yamlContent = yamlMatch[1];
|
||||
yamlContent = yamlContent.replace(/^(\s*-)(\s*"[^"]+")(\s*-\s*.*)$/gm, '$1$2');
|
||||
|
||||
const agentConfig = yaml.parse(yamlContent);
|
||||
const dependencies = agentConfig.dependencies || {};
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
const fs = require('fs').promises;
|
||||
const path = require('path');
|
||||
const yaml = require('js-yaml');
|
||||
const { extractYamlFromAgent } = require('./yaml-utils');
|
||||
|
||||
class DependencyResolver {
|
||||
constructor(rootDir) {
|
||||
@@ -14,17 +15,12 @@ class DependencyResolver {
|
||||
const agentPath = path.join(this.bmadCore, 'agents', `${agentId}.md`);
|
||||
const agentContent = await fs.readFile(agentPath, 'utf8');
|
||||
|
||||
// Extract YAML from markdown content
|
||||
const yamlMatch = agentContent.replace(/\r/g, "").match(/```ya?ml\n([\s\S]*?)\n```/);
|
||||
if (!yamlMatch) {
|
||||
// Extract YAML from markdown content with command cleaning
|
||||
const yamlContent = extractYamlFromAgent(agentContent, true);
|
||||
if (!yamlContent) {
|
||||
throw new Error(`No YAML configuration found in agent ${agentId}`);
|
||||
}
|
||||
|
||||
// Clean up the YAML to handle command descriptions after dashes
|
||||
let yamlContent = yamlMatch[1];
|
||||
// Fix commands section: convert "- command - description" to just "- command"
|
||||
yamlContent = yamlContent.replace(/^(\s*-)(\s*"[^"]+")(\s*-\s*.*)$/gm, '$1$2');
|
||||
|
||||
const agentConfig = yaml.load(yamlContent);
|
||||
|
||||
const dependencies = {
|
||||
|
||||
29
tools/lib/yaml-utils.js
Normal file
29
tools/lib/yaml-utils.js
Normal file
@@ -0,0 +1,29 @@
|
||||
/**
|
||||
* Utility functions for YAML extraction from agent files
|
||||
*/
|
||||
|
||||
/**
|
||||
* Extract YAML content from agent markdown files
|
||||
* @param {string} agentContent - The full content of the agent file
|
||||
* @param {boolean} cleanCommands - Whether to clean command descriptions (default: false)
|
||||
* @returns {string|null} - The extracted YAML content or null if not found
|
||||
*/
|
||||
function extractYamlFromAgent(agentContent, cleanCommands = false) {
|
||||
// Remove carriage returns and match YAML block
|
||||
const yamlMatch = agentContent.replace(/\r/g, "").match(/```ya?ml\n([\s\S]*?)\n```/);
|
||||
if (!yamlMatch) return null;
|
||||
|
||||
let yamlContent = yamlMatch[1].trim();
|
||||
|
||||
// Clean up command descriptions if requested
|
||||
// Converts "- command - description" to just "- command"
|
||||
if (cleanCommands) {
|
||||
yamlContent = yamlContent.replace(/^(\s*-)(\s*"[^"]+")(\s*-\s*.*)$/gm, '$1$2');
|
||||
}
|
||||
|
||||
return yamlContent;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
extractYamlFromAgent
|
||||
};
|
||||
Reference in New Issue
Block a user