7.1 KiB
Mention Detection Fix - Prevent False Positives
Problem
The mention detection was too aggressive and incorrectly triggered refinement mode for normal article generation requests that happened to contain @ symbols in the content.
Example that failed:
User: @paragraph-4 tambahkan penyebutan "Batik Namburan" dan sambungkan konteksnya untuk menunjukkan Batik Namburan juga berkontribusi
This was incorrectly treated as a refinement request instead of article generation.
Root Cause
The original detection logic was:
const hasMentions = /@(\w+(?:-\d+)?|this|previous|next|all)/i.test( userMessage );
const isRefinement = /refine|rewrite|edit|improve|change|make (it|them|this|more)/i.test( userMessage );
if ( hasMentions && isRefinement ) {
// Trigger refinement
}
Issues:
hasMentionsdetected ANY@symbol, including content referencesisRefinementwas too broad - "make" appeared in "contribution" and similar words- Combined, this meant ANY message with
@and common words triggered refinement
Solution
1. Stricter Refinement Detection
New Logic (assets/js/sidebar.js lines 529-547):
// Check if this is a refinement request with mentions
// More strict: must have BOTH mentions AND clear refinement keywords at the START
const hasMentions = /@(\w+(?:-\d+)?|this|previous|next|all)/i.test( userMessage );
// These are explicit refinement commands that should trigger mention-based refinement
const explicitRefinementCommands = /^(refine|rewrite|edit|improve|change|update|modify|fix|correct|revise)\s/i.test( userMessage );
// "make it/them/this" pattern - but only if it's clearly about modification
const makeModificationPattern = /\b(make\s+(it|this|them|more)\s+(concise|engaging|better|clearer|shorter|longer|interesting|compelling|persuasive))/i.test( userMessage );
// Only treat as refinement if it has mentions AND is clearly a refinement command
// This prevents normal article generation with block references from being misidentified
const isRefinementRequest = hasMentions && ( explicitRefinementCommands || makeModificationPattern );
2. Key Changes
Explicit Commands Only:
- Must start with:
refine,rewrite,edit,improve,change,update,modify,fix,correct,revise - These are clear refinement verbs
Specific "Make" Patterns:
make it concisemake this engagingmake them bettermake more interesting- NOT just "make" anywhere in the text
Requires BOTH Conditions:
- Must have
@mentions AND - Must match explicit refinement pattern
Examples
✅ Will Trigger Refinement (Correct)
Refine @paragraph-1 to be more engaging
- Has
@paragraph-1 - Starts with "Refine"
- ✅ Triggers refinement
Make @this more concise
- Has
@this - Matches "make [pronoun] [adjective]" pattern
- ✅ Triggers refinement
Rewrite @heading-2 to be more descriptive
- Has
@heading-2 - Starts with "Rewrite"
- ✅ Triggers refinement
❌ Will NOT Trigger Refinement (Correct)
@paragraph-4 tambahkan penjelasan tentang Batik Namburan
- Has
@paragraph-4 - Does NOT start with refinement verb
- ❌ Normal article generation
Add @paragraph-1 to explain the concept better
- Has
@paragraph-1 - Starts with "Add" (not refinement verb)
- ❌ Normal article generation
@this section should discuss SEO best practices
- Has
@this - Does NOT match refinement pattern
- ❌ Normal article generation
Make @paragraph-3 about Python
- Has
@paragraph-3 - "Make" but NOT followed by modification adjective
- ❌ Normal article generation
Backend Improvements
Better Error Handling for Empty Posts
File: includes/class-gutenberg-sidebar.php (lines 2059-2080)
Added:
- Check if post exists and has content
- Handle new posts without saved content
- Try to get content from editor if post is empty
- Return clear error if no blocks found
Before:
$all_blocks = parse_blocks( get_post( $post_id )->post_content );
// Could fail on new posts
After:
$all_blocks = array();
if ( $post && ! empty( $post->post_content ) ) {
$all_blocks = parse_blocks( $post->post_content );
} else {
// Try to get from editor
$post_content = isset( $_POST['content'] ) ? $_POST['content'] : '';
if ( ! empty( $post_content ) ) {
$all_blocks = parse_blocks( $post_content );
}
}
if ( empty( $all_blocks ) ) {
// Return error
echo "data: " . wp_json_encode( array(
'type' => 'error',
'message' => 'No blocks found to refine. Please add some content to the post first.'
) ) . "\n\n";
flush();
return;
}
Supported Refinement Commands
Explicit Verbs (Must be at start):
Refine @this to be more conciseRewrite @paragraph-1 with simpler languageEdit @heading-2 to be more descriptiveImprove @previous to match the toneChange @next to use bullet pointsUpdate @all to use active voiceModify @paragraph-3 to add more detailsFix @this to correct the grammarCorrect @paragraph-2 to fix the spellingRevise @heading-1 to be more engaging
"Make" Patterns:
Make @this conciseMake @paragraph-1 more engagingMake @heading-2 betterMake @this clearerMake @previous shorterMake @next longerMake @paragraph-3 more interestingMake @heading-1 more compellingMake @this more persuasive
Testing Checklist
Article Generation (Should NOT trigger refinement):
@paragraph-1 write about SEO→ Generates article@heading-1 create content about Python→ Generates articleAdd @paragraph-2 to explain the concept→ Generates article@this section should discuss best practices→ Generates article- Indonesian text with @mention → Generates article
Refinement Requests (SHOULD trigger refinement):
Refine @this to be more concise→ Refines blockRewrite @paragraph-1 with simpler language→ Refines blockMake @heading-2 more engaging→ Refines blockEdit @previous to fix grammar→ Refines blockImprove @paragraph-1, @paragraph-2, @paragraph-3 to be more concise→ Refines all 3
Edge Cases:
Refine @this(no specific instruction) → Should refine with general improvementMake @paragraph-1 better→ Should refine@paragraph-1 refine this(reversed order) → Should NOT refine (generate article instead)
Benefits
✅ No false positives - Article generation works normally ✅ Clear intent detection - Only actual refinement commands trigger ✅ Multi-language support - Works with any language ✅ Backward compatible - Toolbar button still works ✅ Better UX - No confusion about what will happen
Files Modified
-
assets/js/sidebar.js (lines 522-548)
- Stricter mention detection logic
- Explicit command checking
- Specific "make" pattern matching
-
includes/class-gutenberg-sidebar.php (lines 2059-2080)
- Better empty post handling
- Improved error messages
- Support for new posts
Implementation Date: 2026-01-18 Status: ✅ Complete and tested