Home/Tools/@tpmjs/tools-diff-text-unified

diffTextUnified

@tpmjs/tools-diff-text-unified

Creates a unified diff between two text strings, showing additions, deletions, and context lines. Returns the diff in unified format along with statistics about changes. Useful for comparing versions of code, documents, or any text content.

Official
engineering
v0.2.0
MIT

Interactive Playground

Test @tpmjs/tools-diff-text-unified (diffTextUnified) with AI-powered execution

0/2000 characters

Installation & Usage

Install this tool and use it with the AI SDK

1. Install the package

npm install @tpmjs/tools-diff-text-unified
pnpm add @tpmjs/tools-diff-text-unified
yarn add @tpmjs/tools-diff-text-unified
bun add @tpmjs/tools-diff-text-unified
deno add npm:@tpmjs/tools-diff-text-unified

2. Import the tool

import { diffTextUnified } from '@tpmjs/tools-diff-text-unified';

3. Use with AI SDK

import { generateText } from 'ai';
import { openai } from '@ai-sdk/openai';
import { diffTextUnified } from '@tpmjs/tools-diff-text-unified';

const result = await generateText({
  model: openai('gpt-4o'),
  tools: { diffTextUnified },
  prompt: 'Your prompt here...',
});

console.log(result.text);

Parameters

Available configuration options

Auto-extracted
original
Required
Type: string

The original text to compare from

modified
Required
Type: string

The modified text to compare to

contextLines
Optional
Type: number

Number of context lines to show around changes (default: 3)

Schema extracted: 1/1/2026, 8:18:17 AM

README

@tpmjs/tools-diff-text-unified

Creates unified diff between two text strings with context lines.

Installation

npm install @tpmjs/tools-diff-text-unified

Usage

import { diffTextUnified } from '@tpmjs/tools-diff-text-unified';
import { generateText } from 'ai';

const result = await generateText({
  model: yourModel,
  tools: {
    diffTextUnified,
  },
  prompt: 'Compare these two versions and show me what changed',
});

Tool Details

diffTextUnified

Creates a unified diff between two text strings, showing additions, deletions, and context lines.

Parameters:

  • original (string, required) - The original text to compare from
  • modified (string, required) - The modified text to compare to
  • contextLines (number, optional) - Number of context lines to show around changes (default: 3)

Returns:

{
  diff: string;          // The unified diff string
  additions: number;     // Number of lines added
  deletions: number;     // Number of lines deleted
  changes: number;       // Total changes (additions + deletions)
  hasChanges: boolean;   // Whether any changes were detected
  summary: string;       // Human-readable summary
}

Example Output

{
  diff: `--- original
+++ modified
@@ -1,3 +1,3 @@
 function hello() {
-  console.log('Hello');
+  console.log('Hello, World!');
 }`,
  additions: 1,
  deletions: 1,
  changes: 2,
  hasChanges: true,
  summary: "1 addition, 1 deletion"
}

Features

  • Creates standard unified diff format
  • Configurable context lines
  • Counts additions and deletions
  • Provides human-readable summary
  • Works with any text content (code, documents, etc.)

License

MIT

Statistics

Downloads/month

0

Quality Score

0%

Bundle Size

NPM Keywords

tpmjs
engineering
diff
text
unified

Maintainers

thomasdavis(thomasalwyndavis@gmail.com)

Frameworks

vercel-ai
diffTextUnified | TPMJS | TPMJS