|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2026 Google LLC |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +import type {TextSnapshot, TextSnapshotNode} from '../McpContext.js'; |
| 8 | +import {TreeDiff, type DiffNode} from '../utils/TreeDiff.js'; |
| 9 | + |
| 10 | +import {SnapshotFormatter} from './SnapshotFormatter.js'; |
| 11 | + |
| 12 | +export class SnapshotDiffFormatter { |
| 13 | + #root: DiffNode<TextSnapshotNode>; |
| 14 | + #oldFormatter: SnapshotFormatter; |
| 15 | + #newFormatter: SnapshotFormatter; |
| 16 | + |
| 17 | + constructor( |
| 18 | + root: DiffNode<TextSnapshotNode>, |
| 19 | + oldFormatter: SnapshotFormatter, |
| 20 | + newFormatter: SnapshotFormatter, |
| 21 | + ) { |
| 22 | + this.#root = root; |
| 23 | + this.#oldFormatter = oldFormatter; |
| 24 | + this.#newFormatter = newFormatter; |
| 25 | + } |
| 26 | + |
| 27 | + toString(): string { |
| 28 | + const lines = this.#formatDiffNode(this.#root, 0); |
| 29 | + const hasChanges = lines.some(l => l.startsWith('+') || l.startsWith('-')); |
| 30 | + if (!hasChanges) { |
| 31 | + return ''; |
| 32 | + } |
| 33 | + return lines.join('').trimEnd(); |
| 34 | + } |
| 35 | + |
| 36 | + #formatDiffNode( |
| 37 | + diffNode: DiffNode<TextSnapshotNode>, |
| 38 | + depth: number, |
| 39 | + ): string[] { |
| 40 | + const chunks: string[] = []; |
| 41 | + |
| 42 | + if (diffNode.type === 'same') { |
| 43 | + const oldLine = this.#oldFormatter.formatNodeSelf( |
| 44 | + diffNode.oldNode!, |
| 45 | + depth, |
| 46 | + ); |
| 47 | + const newLine = this.#newFormatter.formatNodeSelf(diffNode.node, depth); |
| 48 | + if (oldLine === newLine) { |
| 49 | + chunks.push(' ' + newLine); |
| 50 | + } else { |
| 51 | + chunks.push('- ' + oldLine); |
| 52 | + chunks.push('+ ' + newLine); |
| 53 | + } |
| 54 | + // Children |
| 55 | + for (const child of diffNode.children) { |
| 56 | + chunks.push(...this.#formatDiffNode(child, depth + 1)); |
| 57 | + } |
| 58 | + } else if (diffNode.type === 'added') { |
| 59 | + chunks.push( |
| 60 | + '+ ' + this.#newFormatter.formatNodeSelf(diffNode.node, depth), |
| 61 | + ); |
| 62 | + // Recursively add children (they are also 'added' in the tree) |
| 63 | + for (const child of diffNode.children) { |
| 64 | + chunks.push(...this.#formatDiffNode(child, depth + 1)); |
| 65 | + } |
| 66 | + } else if (diffNode.type === 'removed') { |
| 67 | + chunks.push( |
| 68 | + '- ' + this.#oldFormatter.formatNodeSelf(diffNode.node, depth), |
| 69 | + ); |
| 70 | + // Recursively remove children (they are also 'removed' in the tree) |
| 71 | + for (const child of diffNode.children) { |
| 72 | + chunks.push(...this.#formatDiffNode(child, depth + 1)); |
| 73 | + } |
| 74 | + } else if (diffNode.type === 'modified') { |
| 75 | + chunks.push( |
| 76 | + '- ' + this.#oldFormatter.formatNodeSelf(diffNode.oldNode!, depth), |
| 77 | + ); |
| 78 | + chunks.push( |
| 79 | + '+ ' + this.#newFormatter.formatNodeSelf(diffNode.node, depth), |
| 80 | + ); |
| 81 | + } |
| 82 | + |
| 83 | + return chunks; |
| 84 | + } |
| 85 | + |
| 86 | + toJSON(): object { |
| 87 | + return this.#nodeToJSON(this.#root) ?? {}; |
| 88 | + } |
| 89 | + |
| 90 | + #nodeToJSON(diffNode: DiffNode<TextSnapshotNode>): object | null { |
| 91 | + if (diffNode.type === 'same') { |
| 92 | + const oldJson = this.#oldFormatter.nodeToJSON(diffNode.oldNode!); |
| 93 | + const newJson = this.#newFormatter.nodeToJSON(diffNode.node); |
| 94 | + const childrenDiff = diffNode.children |
| 95 | + .map(child => this.#nodeToJSON(child)) |
| 96 | + .filter(x => x !== null); |
| 97 | + |
| 98 | + const contentChanged = |
| 99 | + JSON.stringify(oldJson) !== JSON.stringify(newJson); |
| 100 | + |
| 101 | + if (!contentChanged && childrenDiff.length === 0) { |
| 102 | + return null; |
| 103 | + } |
| 104 | + |
| 105 | + const result: Record<string, unknown> = {}; |
| 106 | + if (contentChanged) { |
| 107 | + result.type = 'modified'; |
| 108 | + result.oldAttributes = oldJson; |
| 109 | + result.newAttributes = newJson; |
| 110 | + } else { |
| 111 | + result.type = 'unchanged'; |
| 112 | + result.id = diffNode.node.id; |
| 113 | + } |
| 114 | + |
| 115 | + if (childrenDiff.length > 0) { |
| 116 | + result.children = childrenDiff; |
| 117 | + } |
| 118 | + return result; |
| 119 | + } else if (diffNode.type === 'added') { |
| 120 | + return { |
| 121 | + type: 'added', |
| 122 | + node: this.#newFormatter.nodeToJSON(diffNode.node), |
| 123 | + }; |
| 124 | + } else if (diffNode.type === 'removed') { |
| 125 | + return { |
| 126 | + type: 'removed', |
| 127 | + node: this.#oldFormatter.nodeToJSON(diffNode.node), |
| 128 | + }; |
| 129 | + } else if (diffNode.type === 'modified') { |
| 130 | + return { |
| 131 | + type: 'modified', |
| 132 | + oldNode: this.#oldFormatter.nodeToJSON(diffNode.oldNode!), |
| 133 | + newNode: this.#newFormatter.nodeToJSON(diffNode.node), |
| 134 | + }; |
| 135 | + } |
| 136 | + return null; |
| 137 | + } |
| 138 | + |
| 139 | + static diff( |
| 140 | + oldSnapshot: TextSnapshot, |
| 141 | + newSnapshot: TextSnapshot, |
| 142 | + ): SnapshotDiffFormatter { |
| 143 | + const diffRoot = TreeDiff.compute(oldSnapshot.root, newSnapshot.root); |
| 144 | + const oldFormatter = new SnapshotFormatter(oldSnapshot); |
| 145 | + const newFormatter = new SnapshotFormatter(newSnapshot); |
| 146 | + return new SnapshotDiffFormatter(diffRoot, oldFormatter, newFormatter); |
| 147 | + } |
| 148 | +} |
0 commit comments