-
Notifications
You must be signed in to change notification settings - Fork 804
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AI Extension: Enhance blocks parsing flickering with child blocks (#3…
…2635) * add block comparison function * add prompt tweaks and history with examples * fix streaming flickering * changelog * update changelog
- Loading branch information
Showing
3 changed files
with
62 additions
and
2 deletions.
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
projects/plugins/jetpack/changelog/update-ai-assistant-required-fields
This file contains 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,4 @@ | ||
Significance: patch | ||
Type: enhancement | ||
|
||
AI Extension: Enhance blocks parsing flickering with child blocks |
This file contains 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
35 changes: 35 additions & 0 deletions
35
projects/plugins/jetpack/extensions/blocks/ai-assistant/lib/utils/compare-blocks.ts
This file contains 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,35 @@ | ||
type Block = { | ||
attributes?: object; | ||
clientId?: string; | ||
innerBlocks?: Block[]; | ||
isValid?: boolean; | ||
name?: string; | ||
originalContent?: string; | ||
}; | ||
|
||
const omitClientId = ( block: Block ): Block => { | ||
delete block.clientId; | ||
|
||
for ( const child of block.innerBlocks ?? [] ) { | ||
omitClientId( child ); | ||
} | ||
|
||
return block; | ||
}; | ||
|
||
const copyBlock = ( block: Block ): Block => JSON.parse( JSON.stringify( block ) ); | ||
const copyBlockWithoutClientId = ( block: Block ) => omitClientId( copyBlock( block ) ); | ||
|
||
/** | ||
* Deeply compares two blocks, ignoring the clientId property. | ||
* | ||
* @param {Block} blockA - The first block to compare. | ||
* @param {Block} blockB - The second block to compare. | ||
* @returns {boolean} Whether the two blocks are equal. | ||
*/ | ||
export function compareBlocks( blockA: Block, blockB: Block ): boolean { | ||
const aCopy = copyBlockWithoutClientId( blockA ); | ||
const bCopy = copyBlockWithoutClientId( blockB ); | ||
|
||
return JSON.stringify( aCopy ) === JSON.stringify( bCopy ); | ||
} |