253 lines
7.1 KiB
Markdown
253 lines
7.1 KiB
Markdown
# 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:
|
|
```javascript
|
|
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:**
|
|
1. `hasMentions` detected ANY `@` symbol, including content references
|
|
2. `isRefinement` was too broad - "make" appeared in "contribution" and similar words
|
|
3. 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):**
|
|
|
|
```javascript
|
|
// 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 concise`
|
|
- `make this engaging`
|
|
- `make them better`
|
|
- `make 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:**
|
|
1. Check if post exists and has content
|
|
2. Handle new posts without saved content
|
|
3. Try to get content from editor if post is empty
|
|
4. Return clear error if no blocks found
|
|
|
|
**Before:**
|
|
```php
|
|
$all_blocks = parse_blocks( get_post( $post_id )->post_content );
|
|
// Could fail on new posts
|
|
```
|
|
|
|
**After:**
|
|
```php
|
|
$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 concise`
|
|
- `Rewrite @paragraph-1 with simpler language`
|
|
- `Edit @heading-2 to be more descriptive`
|
|
- `Improve @previous to match the tone`
|
|
- `Change @next to use bullet points`
|
|
- `Update @all to use active voice`
|
|
- `Modify @paragraph-3 to add more details`
|
|
- `Fix @this to correct the grammar`
|
|
- `Correct @paragraph-2 to fix the spelling`
|
|
- `Revise @heading-1 to be more engaging`
|
|
|
|
### "Make" Patterns:
|
|
- `Make @this concise`
|
|
- `Make @paragraph-1 more engaging`
|
|
- `Make @heading-2 better`
|
|
- `Make @this clearer`
|
|
- `Make @previous shorter`
|
|
- `Make @next longer`
|
|
- `Make @paragraph-3 more interesting`
|
|
- `Make @heading-1 more compelling`
|
|
- `Make @this more persuasive`
|
|
|
|
---
|
|
|
|
## Testing Checklist
|
|
|
|
### Article Generation (Should NOT trigger refinement):
|
|
- [x] `@paragraph-1 write about SEO` → Generates article
|
|
- [x] `@heading-1 create content about Python` → Generates article
|
|
- [x] `Add @paragraph-2 to explain the concept` → Generates article
|
|
- [x] `@this section should discuss best practices` → Generates article
|
|
- [x] Indonesian text with @mention → Generates article
|
|
|
|
### Refinement Requests (SHOULD trigger refinement):
|
|
- [ ] `Refine @this to be more concise` → Refines block
|
|
- [ ] `Rewrite @paragraph-1 with simpler language` → Refines block
|
|
- [ ] `Make @heading-2 more engaging` → Refines block
|
|
- [ ] `Edit @previous to fix grammar` → Refines block
|
|
- [ ] `Improve @paragraph-1, @paragraph-2, @paragraph-3 to be more concise` → Refines all 3
|
|
|
|
### Edge Cases:
|
|
- [ ] `Refine @this` (no specific instruction) → Should refine with general improvement
|
|
- [ ] `Make @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
|
|
|
|
1. **assets/js/sidebar.js** (lines 522-548)
|
|
- Stricter mention detection logic
|
|
- Explicit command checking
|
|
- Specific "make" pattern matching
|
|
|
|
2. **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
|