All files / src/config writer.ts

77.63% Statements 59/76
80.3% Branches 53/66
80% Functions 4/5
76.71% Lines 56/73

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152              16x 16x   16x 1x       16x 4x 4x     16x 1x 1x     16x 1x     16x 1x     16x 1x     16x 1x     16x       16x 2x     16x       16x       16x         16x 14x 15x 15x 15x   15x 11x 10x       15x 1x     15x 1x     15x 1x     15x 1x     15x 1x     15x       15x       15x       15x       15x 2x 2x 2x 1x   2x 2x   2x 1x           16x                                                  
import { readFileSync, writeFileSync } from 'node:fs';
import type { SkillConfig } from './schema.js';
 
/**
 * Generate TOML representation of a skill.
 */
export function generateSkillToml(skill: SkillConfig): string {
  const lines: string[] = ['[[skills]]'];
  lines.push(`name = "${skill.name}"`);
 
  if (skill.remote) {
    lines.push(`remote = "${skill.remote}"`);
  }
 
  // Skill-level fields
  if (skill.paths && skill.paths.length > 0) {
    const pathsStr = skill.paths.map((p) => `"${p}"`).join(', ');
    lines.push(`paths = [${pathsStr}]`);
  }
 
  if (skill.ignorePaths && skill.ignorePaths.length > 0) {
    const ignoreStr = skill.ignorePaths.map((p) => `"${p}"`).join(', ');
    lines.push(`ignorePaths = [${ignoreStr}]`);
  }
 
  if (skill.model) {
    lines.push(`model = "${skill.model}"`);
  }
 
  if (skill.failOn) {
    lines.push(`failOn = "${skill.failOn}"`);
  }
 
  if (skill.reportOn) {
    lines.push(`reportOn = "${skill.reportOn}"`);
  }
 
  if (skill.maxFindings) {
    lines.push(`maxFindings = ${skill.maxFindings}`);
  }
 
  Iif (skill.maxTurns) {
    lines.push(`maxTurns = ${skill.maxTurns}`);
  }
 
  if (skill.reportOnSuccess !== undefined) {
    lines.push(`reportOnSuccess = ${skill.reportOnSuccess}`);
  }
 
  Iif (skill.requestChanges !== undefined) {
    lines.push(`requestChanges = ${skill.requestChanges}`);
  }
 
  Iif (skill.failCheck !== undefined) {
    lines.push(`failCheck = ${skill.failCheck}`);
  }
 
  Iif (skill.minConfidence !== undefined) {
    lines.push(`minConfidence = "${skill.minConfidence}"`);
  }
 
  // Nested triggers
  if (skill.triggers) {
    for (const trigger of skill.triggers) {
      lines.push('');
      lines.push('[[skills.triggers]]');
      lines.push(`type = "${trigger.type}"`);
 
      if (trigger.actions && trigger.actions.length > 0) {
        const actionsStr = trigger.actions.map((a) => `"${a}"`).join(', ');
        lines.push(`actions = [${actionsStr}]`);
      }
 
      // Trigger-level overrides
      if (trigger.model) {
        lines.push(`model = "${trigger.model}"`);
      }
 
      if (trigger.failOn) {
        lines.push(`failOn = "${trigger.failOn}"`);
      }
 
      if (trigger.reportOn) {
        lines.push(`reportOn = "${trigger.reportOn}"`);
      }
 
      if (trigger.maxFindings) {
        lines.push(`maxFindings = ${trigger.maxFindings}`);
      }
 
      if (trigger.maxTurns) {
        lines.push(`maxTurns = ${trigger.maxTurns}`);
      }
 
      Iif (trigger.reportOnSuccess !== undefined) {
        lines.push(`reportOnSuccess = ${trigger.reportOnSuccess}`);
      }
 
      Iif (trigger.requestChanges !== undefined) {
        lines.push(`requestChanges = ${trigger.requestChanges}`);
      }
 
      Iif (trigger.failCheck !== undefined) {
        lines.push(`failCheck = ${trigger.failCheck}`);
      }
 
      Iif (trigger.minConfidence !== undefined) {
        lines.push(`minConfidence = "${trigger.minConfidence}"`);
      }
 
      if (trigger.schedule) {
        lines.push('');
        lines.push('[skills.triggers.schedule]');
        if (trigger.schedule.issueTitle) {
          lines.push(`issueTitle = "${trigger.schedule.issueTitle}"`);
        }
        Eif (trigger.schedule.createFixPR !== undefined) {
          lines.push(`createFixPR = ${trigger.schedule.createFixPR}`);
        }
        if (trigger.schedule.fixBranchPrefix && trigger.schedule.fixBranchPrefix !== 'warden-fix') {
          lines.push(`fixBranchPrefix = "${trigger.schedule.fixBranchPrefix}"`);
        }
      }
    }
  }
 
  return lines.join('\n');
}
 
/**
 * Append a skill to the warden.toml configuration file.
 * Preserves existing content and formatting by appending to the end.
 */
export function appendSkill(configPath: string, skill: SkillConfig): void {
  const existingContent = readFileSync(configPath, 'utf-8');
 
  // Ensure proper spacing before the new skill
  let separator: string;
  if (existingContent.endsWith('\n\n')) {
    separator = '';
  } else if (existingContent.endsWith('\n')) {
    separator = '\n';
  } else {
    separator = '\n\n';
  }
 
  const skillToml = generateSkillToml(skill);
  const newContent = existingContent + separator + skillToml + '\n';
 
  writeFileSync(configPath, newContent, 'utf-8');
}