|
| 1 | +import { z } from 'zod' |
| 2 | +import Tool from './tool.js' |
| 3 | +import { designTokens, type DesignToken } from '../optics-data.js' |
| 4 | +import { extractAllValues, findMatchingToken } from '../utils/css-parser.js' |
| 5 | + |
| 6 | +interface ReplacementSuggestion { |
| 7 | + original: string |
| 8 | + replacement: string |
| 9 | + tokenName: string |
| 10 | + property?: string |
| 11 | +} |
| 12 | + |
| 13 | +interface ReplacementResult { |
| 14 | + originalCode: string |
| 15 | + fixedCode: string |
| 16 | + replacements: ReplacementSuggestion[] |
| 17 | + replacementCount: number |
| 18 | +} |
| 19 | + |
| 20 | +class ReplaceHardCodedValuesTool extends Tool { |
| 21 | + name = 'replace_hard_coded_values' |
| 22 | + title = 'Replace Hard-Coded Values' |
| 23 | + description = 'Replace hard-coded values with design tokens' |
| 24 | + |
| 25 | + inputSchema = { |
| 26 | + code: z.string().describe('Code containing hard-coded values'), |
| 27 | + autofix: z.boolean().optional().describe('Whether to automatically fix the code (default: false)'), |
| 28 | + } |
| 29 | + |
| 30 | + async handler(args: any): Promise<string> { |
| 31 | + const { code, autofix } = args |
| 32 | + const result = this.replaceHardCodedValues(code, designTokens, autofix ?? false) |
| 33 | + return this.formatReplacementSuggestions(result) |
| 34 | + } |
| 35 | + |
| 36 | + private replaceHardCodedValues( |
| 37 | + code: string, |
| 38 | + tokens: DesignToken[], |
| 39 | + autofix: boolean = false |
| 40 | + ): ReplacementResult { |
| 41 | + const extractedValues = extractAllValues(code) |
| 42 | + const replacements: ReplacementSuggestion[] = [] |
| 43 | + let fixedCode = code |
| 44 | + |
| 45 | + for (const value of extractedValues) { |
| 46 | + const matchingToken = findMatchingToken(value.value, tokens) |
| 47 | + |
| 48 | + if (matchingToken) { |
| 49 | + const replacement = `var(--${matchingToken})` |
| 50 | + replacements.push({ |
| 51 | + original: value.value, |
| 52 | + replacement, |
| 53 | + tokenName: matchingToken, |
| 54 | + property: value.property, |
| 55 | + }) |
| 56 | + |
| 57 | + if (autofix) { |
| 58 | + fixedCode = fixedCode.replace( |
| 59 | + new RegExp(this.escapeRegex(value.value), 'g'), |
| 60 | + replacement |
| 61 | + ) |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + return { |
| 67 | + originalCode: code, |
| 68 | + fixedCode: autofix ? fixedCode : code, |
| 69 | + replacements, |
| 70 | + replacementCount: replacements.length, |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + private escapeRegex(str: string): string { |
| 75 | + return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') |
| 76 | + } |
| 77 | + |
| 78 | + private formatReplacementSuggestions(result: ReplacementResult): string { |
| 79 | + const lines: string[] = [ |
| 80 | + '# Token Replacement Suggestions', |
| 81 | + '', |
| 82 | + `**Replacements Found**: ${result.replacementCount}`, |
| 83 | + '', |
| 84 | + ] |
| 85 | + |
| 86 | + if (result.replacements.length > 0) { |
| 87 | + lines.push('## Suggested Replacements') |
| 88 | + lines.push('') |
| 89 | + |
| 90 | + for (const rep of result.replacements) { |
| 91 | + lines.push(`- Replace \`${rep.original}\` with \`${rep.replacement}\``) |
| 92 | + lines.push(` Token: ${rep.tokenName}`) |
| 93 | + if (rep.property) lines.push(` Property: ${rep.property}`) |
| 94 | + lines.push('') |
| 95 | + } |
| 96 | + |
| 97 | + if (result.fixedCode !== result.originalCode) { |
| 98 | + lines.push('## Fixed Code') |
| 99 | + lines.push('```css') |
| 100 | + lines.push(result.fixedCode) |
| 101 | + lines.push('```') |
| 102 | + } |
| 103 | + } else { |
| 104 | + lines.push('✓ No replacements needed!') |
| 105 | + } |
| 106 | + |
| 107 | + return lines.join('\n') |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +export default ReplaceHardCodedValuesTool |
0 commit comments