UNPKG

@nanocollective/nanocoder

Version:

A local-first CLI coding agent that brings the power of agentic coding tools like Claude Code and Gemini CLI to local models or controlled APIs like OpenRouter

51 lines 1.89 kB
import { diffWordsWithSpace } from 'diff'; /** * Compute inline diff segments between two strings. * Uses word-level diffing for more readable results. */ export function computeInlineDiff(oldText, newText) { const changes = diffWordsWithSpace(oldText, newText); const segments = []; for (const change of changes) { if (change.added) { segments.push({ text: change.value, type: 'added' }); } else if (change.removed) { segments.push({ text: change.value, type: 'removed' }); } else { segments.push({ text: change.value, type: 'unchanged' }); } } return segments; } /** * Check if two lines are similar enough to show as an inline diff. * Returns true if the lines share significant common content. */ export function areLinesSimlar(oldLine, newLine) { // If either is empty, they're not similar for inline display if (!oldLine.trim() && !newLine.trim()) return true; // Both empty/whitespace if (!oldLine.trim() || !newLine.trim()) return false; // Use LCS-like heuristic: count common characters const oldTrimmed = oldLine.trim(); const newTrimmed = newLine.trim(); // Calculate similarity using word overlap const oldWords = new Set(oldTrimmed.split(/\s+/).filter(Boolean)); const newWords = new Set(newTrimmed.split(/\s+/).filter(Boolean)); if (oldWords.size === 0 && newWords.size === 0) return true; if (oldWords.size === 0 || newWords.size === 0) return false; let commonWords = 0; for (const word of oldWords) { if (newWords.has(word)) commonWords++; } const similarity = commonWords / Math.max(oldWords.size, newWords.size); // Consider lines similar if they share at least 30% of words return similarity >= 0.3; } //# sourceMappingURL=inline-diff.js.map