|
| 1 | +# 355 - YAML Config Diff V2 |
| 2 | + |
| 3 | +```rig |
| 4 | +import { agent, p, s, defineTool, repair } from "rig"; |
| 5 | +
|
| 6 | +const extractYamlKeys = defineTool("extractYamlKeys", { |
| 7 | + description: "Extract top-level keys from a YAML string.", |
| 8 | + parameters: { content: s.string }, |
| 9 | + handler: ({ content }: { content: string }) => { |
| 10 | + const keys: string[] = []; |
| 11 | + for (const line of content.split("\n")) { |
| 12 | + const m = line.match(/^([a-zA-Z_][a-zA-Z0-9_-]*):/); |
| 13 | + if (m) keys.push(m[1]); |
| 14 | + } |
| 15 | + return keys; |
| 16 | + }, |
| 17 | +}); |
| 18 | +
|
| 19 | +const diffKeys = defineTool("diffKeys", { |
| 20 | + description: "Compute added and removed keys between two key arrays.", |
| 21 | + parameters: { baseKeys: s.array(s.string), targetKeys: s.array(s.string) }, |
| 22 | + handler: ({ baseKeys, targetKeys }: { baseKeys: string[]; targetKeys: string[] }) => { |
| 23 | + const addedKeys = targetKeys.filter((k: string) => !baseKeys.includes(k)); |
| 24 | + const removedKeys = baseKeys.filter((k: string) => !targetKeys.includes(k)); |
| 25 | + return { addedKeys, removedKeys }; |
| 26 | + }, |
| 27 | +}); |
| 28 | +
|
| 29 | +// Agent role: diff top-level YAML keys between two config files and detect breaking changes. |
| 30 | +const yamlConfigDiff = agent({ |
| 31 | + model: "small", |
| 32 | + input: s.object({ baseFile: s.path, targetFile: s.path }), |
| 33 | + instructions: p`Diff top-level YAML keys between two config files. |
| 34 | +
|
| 35 | +Base file content: ${p.readInput("baseFile")} |
| 36 | +Target file content: ${p.readInput("targetFile")} |
| 37 | +
|
| 38 | +Steps: |
| 39 | +1. Call extractYamlKeys on the base file content to get baseKeys. |
| 40 | +2. Call extractYamlKeys on the target file content to get targetKeys. |
| 41 | +3. Call diffKeys with baseKeys and targetKeys to get addedKeys and removedKeys. |
| 42 | +4. changedKeys: keys present in both but with differing values — estimate from content or leave empty. |
| 43 | +5. totalChanges = addedKeys.length + removedKeys.length + changedKeys.length. |
| 44 | +6. hasBreakingChanges = removedKeys.length > 0.`, |
| 45 | + output: s.object({ |
| 46 | + addedKeys: s.array(s.string), |
| 47 | + removedKeys: s.array(s.string), |
| 48 | + changedKeys: s.array(s.string), |
| 49 | + totalChanges: s.int, |
| 50 | + hasBreakingChanges: s.boolean, |
| 51 | + }), |
| 52 | + tools: [extractYamlKeys, diffKeys], |
| 53 | + maxTurns: 6, |
| 54 | + addons: [repair()], |
| 55 | +}); |
| 56 | +
|
| 57 | +export default yamlConfigDiff; |
| 58 | +
|
| 59 | +``` |
0 commit comments