# Context Management Implementation Plan **Date:** January 25, 2026 **Version:** 0.1.3+ → 0.2.0 **Status:** 📋 APPROVED - READY FOR IMPLEMENTATION --- ## 📚 Approved Plans Summary ### **1. CONTEXT_FLOW_ANALYSIS.md** **Key Findings:** - ✅ Analyzed 12 distinct user interaction flows - 🔴 Identified critical issues: - Chat history NOT sent to Writing mode - Writing mode notes ignored (don't update plan) - Block refinement missing context - Direct writing mode cannot execute (no plan) - ✅ Mapped all context storage mechanisms - ✅ Provided recommendations for fixes **Critical Issues to Fix:** | Issue | Severity | Impact | |-------|----------|--------| | Chat history not sent to Writing mode | 🔴 Critical | AI loses conversation context | | Writing mode notes ignored | 🔴 Critical | User instructions silently ignored | | Block refinement missing context | 🟡 Medium | AI doesn't understand original intent | | Direct writing mode fails | 🟡 Medium | Confusing UX, no error handling | --- ### **2. AGENTIC_CONTEXT_STRATEGY.md** **Key Solutions:** - ✅ AI-powered context summarization (not hardcoded) - ✅ AI-powered intent detection (language-agnostic) - ✅ Cost-effective (~$0.0001 per summarization) - ✅ Seamless agentic UX (proactive suggestions) - ✅ Works in all languages (Indonesian, Arabic, Chinese, etc.) **Core Philosophy:** > "If AI is smart enough to write articles, it's smart enough to manage its own context." **New Actions:** | Action | Purpose | Cost | When | |--------|---------|------|------| | `summarize_context` | Condense chat history | $0.0001 | Before outline (if chat > 6 msgs) | | `detect_intent` | Understand user's next step | $0.00002 | After each user message | --- ## 🎯 Implementation Goals ### **Primary Objectives** 1. **Fix Context Loss Issues** - Send chat history to ALL endpoints - Include context in block refinement - Handle writing mode properly 2. **Implement Agentic Context Management** - AI-powered summarization - AI-powered intent detection - Language-agnostic (no hardcoded keywords) 3. **Enhance User Experience** - Proactive action suggestions - Seamless mode transitions - Transparent context operations 4. **Maintain Cost Efficiency** - Total added cost: ~$0.34/month for 100 articles - 76% token reduction vs full history - Track all costs transparently --- ## 📋 Implementation Phases ### **Phase 1: Critical Fixes** (Priority: 🔴 HIGH) **Goal:** Fix context loss issues identified in CONTEXT_FLOW_ANALYSIS.md #### **1.1 Send Chat History Everywhere** **Files to Modify:** - `assets/js/sidebar.js` **Changes:** ```javascript // In ALL API request payloads, add chatHistory const basePayload = { messages: messages, postId: postId, chatHistory: messages.filter(m => m.role !== 'system'), // ✅ Add this postConfig: postConfig, type: agentMode }; // Apply to: // - handleExecuteArticle() → /execute-article // - handleRefineBlock() → /refine-block // - handleGenerateMeta() → /generate-meta // - Any other endpoints that don't currently send chatHistory ``` **Backend Files to Modify:** - `includes/class-gutenberg-sidebar.php` **Changes:** ```php // In handle_execute_article() $chat_history = $params['chatHistory'] ?? array(); if (!empty($chat_history)) { $chat_history_context = $this->build_chat_history_context($chat_history); // Append to system prompt } // In handle_refine_block() $chat_history = $params['chatHistory'] ?? array(); $plan = get_post_meta($post_id, '_wpaw_plan', true); // Include both in refinement context // In handle_generate_meta() $chat_history = $params['chatHistory'] ?? array(); // Include for better meta description context ``` **Testing:** - [ ] Test Chat → Writing mode (verify context preserved) - [ ] Test block refinement (verify original intent understood) - [ ] Test meta generation (verify context used) **Estimated Time:** 2-3 hours --- #### **1.2 Handle Writing Mode Properly** **Problem:** User can switch to Writing mode without a plan, then cannot execute. **Solution:** Show clear guidance and prevent confusion. **Files to Modify:** - `assets/js/sidebar.js` **Changes:** ```javascript // In renderWritingMode() const renderWritingModeContent = () => { if (!currentPlan) { return (
📝

No Outline Yet

Writing mode requires an outline to structure your article.

Or switch to to discuss your ideas.

); } // Normal writing mode UI... }; // In handleExecuteArticle() if (!currentPlan) { showError('Please create an outline first. Switch to Planning mode.'); return; } ``` **CSS to Add:** - `assets/css/sidebar.css` ```css .writing-mode-empty { display: flex; align-items: center; justify-content: center; min-height: 300px; } .empty-state { text-align: center; max-width: 400px; padding: 2rem; } .empty-state .icon { font-size: 3rem; display: block; margin-bottom: 1rem; } .empty-state h3 { margin: 0 0 0.5rem 0; font-size: 1.5rem; } .empty-state p { color: #666; margin: 0.5rem 0; } .empty-state .hint { font-size: 0.9rem; margin-top: 1rem; } ``` **Testing:** - [ ] Test switching to Writing mode without plan - [ ] Verify clear guidance shown - [ ] Test "Create Outline First" button - [ ] Test switching back to Chat mode **Estimated Time:** 1-2 hours --- #### **1.3 Handle Writing Mode Notes** **Problem:** User adds notes in Writing mode, but notes don't update the plan. **Solution:** Either update plan or show clear warning. **Recommended Approach:** Show warning (simpler, clearer UX) **Files to Modify:** - `assets/js/sidebar.js` **Changes:** ```javascript // In Writing mode, when user sends a message const handleWritingModeMessage = async (userMessage) => { // Show info message addSystemMessage( '💡 Note: To modify the outline, switch to Planning mode. ' + 'Writing mode messages are for discussion only.' ); // Still send to chat endpoint for conversation await sendChatMessage(userMessage); }; // Add mode indicator in input area const renderInputArea = () => { return (
{agentMode === 'writing' && currentPlan && (
💬 Chat mode active. To modify outline, switch to Planning mode.
)}