-
Notifications
You must be signed in to change notification settings - Fork 194
feat(word-diff): implement word-level diffing for granular text changes #2817
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
harbournick
merged 1 commit into
main
from
andrii/sd-2569-granular-tracked-changes-for-llm-text-replacements
Apr 15, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
122 changes: 122 additions & 0 deletions
122
packages/ai/src/ai-actions/__tests__/editor/word-diff.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| import { describe, it, expect } from 'vitest'; | ||
| import { tokenizeWords, computeWordDiff, getWordChanges } from '../../editor/word-diff'; | ||
|
|
||
| describe('tokenizeWords', () => { | ||
| it('should tokenize a basic sentence', () => { | ||
| const tokens = tokenizeWords('The quick fox'); | ||
| expect(tokens).toEqual([ | ||
| { text: 'The', offset: 0 }, | ||
| { text: ' ', offset: 3 }, | ||
| { text: 'quick', offset: 4 }, | ||
| { text: ' ', offset: 9 }, | ||
| { text: 'fox', offset: 10 }, | ||
| ]); | ||
| }); | ||
|
|
||
| it('should handle multiple spaces between words', () => { | ||
| const tokens = tokenizeWords('hello world'); | ||
| expect(tokens).toEqual([ | ||
| { text: 'hello', offset: 0 }, | ||
| { text: ' ', offset: 5 }, | ||
| { text: 'world', offset: 7 }, | ||
| ]); | ||
| }); | ||
|
|
||
| it('should handle leading and trailing whitespace', () => { | ||
| const tokens = tokenizeWords(' hello '); | ||
| expect(tokens).toEqual([ | ||
| { text: ' ', offset: 0 }, | ||
| { text: 'hello', offset: 2 }, | ||
| { text: ' ', offset: 7 }, | ||
| ]); | ||
| }); | ||
|
|
||
| it('should return empty array for empty string', () => { | ||
| expect(tokenizeWords('')).toEqual([]); | ||
| }); | ||
|
|
||
| it('should handle a single word', () => { | ||
| expect(tokenizeWords('hello')).toEqual([{ text: 'hello', offset: 0 }]); | ||
| }); | ||
|
|
||
| it('should handle punctuation attached to words', () => { | ||
| const tokens = tokenizeWords('Hello, world!'); | ||
| expect(tokens).toEqual([ | ||
| { text: 'Hello,', offset: 0 }, | ||
| { text: ' ', offset: 6 }, | ||
| { text: 'world!', offset: 7 }, | ||
| ]); | ||
| }); | ||
| }); | ||
|
|
||
| describe('computeWordDiff', () => { | ||
| it('should return empty array for identical strings', () => { | ||
| expect(computeWordDiff('hello world', 'hello world')).toEqual([]); | ||
| }); | ||
|
|
||
| it('should detect a single word replacement', () => { | ||
| const changes = getWordChanges('The quick fox', 'The fast fox'); | ||
| expect(changes).toEqual([{ type: 'replace', oldFrom: 4, oldTo: 9, newText: 'fast' }]); | ||
| }); | ||
|
|
||
| it('should detect multiple word replacements', () => { | ||
| const changes = getWordChanges( | ||
| 'The quick brown fox jumps over the lazy dog', | ||
| 'The fast brown fox leaps over the lazy cat', | ||
| ); | ||
| expect(changes).toEqual([ | ||
| { type: 'replace', oldFrom: 4, oldTo: 9, newText: 'fast' }, | ||
| { type: 'replace', oldFrom: 20, oldTo: 25, newText: 'leaps' }, | ||
| { type: 'replace', oldFrom: 40, oldTo: 43, newText: 'cat' }, | ||
| ]); | ||
| }); | ||
|
|
||
| it('should detect word insertion', () => { | ||
| const changes = getWordChanges('The fox', 'The quick fox'); | ||
| expect(changes).toHaveLength(1); | ||
| expect(changes[0].type).toBe('insert'); | ||
| // "quick " is inserted (word + trailing space before "fox") | ||
| expect(changes[0]).toHaveProperty('newText', 'quick '); | ||
| }); | ||
|
|
||
| it('should detect word deletion', () => { | ||
| const changes = getWordChanges('The quick fox', 'The fox'); | ||
| expect(changes).toHaveLength(1); | ||
| expect(changes[0].type).toBe('delete'); | ||
| // "quick " (word + space) is removed as a contiguous block | ||
| expect(changes[0]).toHaveProperty('oldFrom', 4); | ||
| expect(changes[0]).toHaveProperty('oldTo', 10); | ||
| }); | ||
|
|
||
| it('should handle complete rewrite', () => { | ||
| const changes = getWordChanges('hello world', 'goodbye earth'); | ||
| // Each word is replaced separately since the space is a shared separator | ||
| expect(changes.length).toBeGreaterThanOrEqual(1); | ||
| expect(changes.every((c) => c.type === 'replace')).toBe(true); | ||
| }); | ||
|
|
||
| it('should handle empty old text', () => { | ||
| const diff = computeWordDiff('', 'hello'); | ||
| expect(diff).toEqual([{ type: 'insert', insertAt: 0, newText: 'hello' }]); | ||
| }); | ||
|
|
||
| it('should handle empty new text', () => { | ||
| const diff = computeWordDiff('hello', ''); | ||
| expect(diff).toEqual([{ type: 'delete', oldFrom: 0, oldTo: 5 }]); | ||
| }); | ||
|
|
||
| it('should handle both empty', () => { | ||
| expect(computeWordDiff('', '')).toEqual([]); | ||
| }); | ||
|
|
||
| it('should preserve whitespace tokens as equal', () => { | ||
| const diff = computeWordDiff('a b c', 'a x c'); | ||
| const changes = diff.filter((op) => op.type !== 'equal'); | ||
| expect(changes).toEqual([{ type: 'replace', oldFrom: 2, oldTo: 3, newText: 'x' }]); | ||
| }); | ||
|
|
||
| it('should handle sentence with punctuation changes', () => { | ||
| const changes = getWordChanges('The company shall provide services.', 'The company must provide services.'); | ||
| expect(changes).toEqual([{ type: 'replace', oldFrom: 12, oldTo: 17, newText: 'must' }]); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
applyGranularChangesremaps later change positions through priortr.steps, but then callscollectTextSegments(from, to)onthis.editor.state.doc(the original document). After an earlier insert/delete changes length, those remapped positions no longer point to the same content in the original doc, so subsequent replacements can inherit marks from the wrong region (or none), causing formatting loss/corruption in multi-change tracked edits.Useful? React with 👍 / 👎.