diff --git a/backend/src/services/collaborativeOrchestrator.js b/backend/src/services/collaborativeOrchestrator.js index e7b3056..3b6caae 100644 --- a/backend/src/services/collaborativeOrchestrator.js +++ b/backend/src/services/collaborativeOrchestrator.js @@ -198,21 +198,33 @@ class CollaborativeOrchestrator { // Check if agent made changes if (section !== 'Section is good, no changes needed' && !section.includes('no changes needed')) { - // Merge section into document const docBefore = session.currentDocument - const updatedDocument = this.mergeSection(session.currentDocument, section) + let updatedDocument = docBefore + let action = '' + let headerText = '' - if (updatedDocument !== session.currentDocument) { + // Check if it's a DELETE command + if (section.startsWith('DELETE:')) { + const deleteMatch = section.match(/DELETE:\s*(.+)/m) + if (deleteMatch) { + headerText = deleteMatch[1].trim() + updatedDocument = this.deleteSection(docBefore, headerText) + action = 'DELETED SECTION' + } + } else { + // Modify or create section + updatedDocument = this.mergeSection(docBefore, section) + const sectionHeader = section.match(/^(#+)\s+(.+)/m) + headerText = sectionHeader ? sectionHeader[2] : 'Unknown' + const isNewSection = !docBefore.includes(headerText) + action = isNewSection ? 'CREATED NEW SECTION' : 'MODIFIED' + } + + if (updatedDocument !== docBefore) { session.currentDocument = updatedDocument session.versionNumber++ modifiedAgents.push(agentName) - // Check if it's a new section or modification - const sectionHeader = section.match(/^(#+)\s+(.+)/m) - const headerText = sectionHeader ? sectionHeader[2] : 'Unknown' - const isNewSection = !docBefore.includes(headerText) - const action = isNewSection ? 'CREATED NEW SECTION' : 'MODIFIED' - console.log(`[Session ${sessionId}] ✓ ${agentName} ${action}: "${headerText}"`) console.log(`[Session ${sessionId}] Document updated: v${session.versionNumber - 1} → v${session.versionNumber} (${docBefore.length} → ${updatedDocument.length} chars)`) console.log(`[Session ${sessionId}] ===== BEFORE (v${session.versionNumber - 1}) =====`) @@ -243,7 +255,7 @@ class CollaborativeOrchestrator { roundNumber }) } else { - console.log(`[Session ${sessionId}] Round ${roundNumber}: ${agentName} - section proposed but no actual changes merged`) + console.log(`[Session ${sessionId}] Round ${roundNumber}: ${agentName} - change proposed but no actual changes made`) } } else { console.log(`[Session ${sessionId}] Round ${roundNumber}: ${agentName} - no changes needed`) @@ -341,6 +353,39 @@ class CollaborativeOrchestrator { } } + /** + * Delete a section from the document by its header text + */ + deleteSection(document, headerText) { + if (!document || !headerText) return document + + // Find the header in the document to determine its level + const headerRegex = new RegExp(`^(#{1,4})\\s+${headerText.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}$`, 'gm') + const match = document.match(headerRegex) + + if (!match) return document + + // Extract the header level + const headerMatch = match[0].match(/^#{1,4}/) + if (!headerMatch) return document + + const headerLevel = headerMatch[0].length + + // Create regex to match this section and everything until the next section of same or higher level + const sectionRegex = new RegExp( + `^${'#'.repeat(headerLevel)}\\s+${headerText.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}(?:\\n(?!^#{1,${headerLevel}}\\s+)[\\s\\S])*`, + 'gm' + ) + + // Remove the section + let updated = document.replace(sectionRegex, '').trim() + + // Clean up extra blank lines + updated = updated.replace(/\n\n\n+/g, '\n\n') + + return updated + } + /** * Complete a session */ diff --git a/backend/src/services/mistralClient.js b/backend/src/services/mistralClient.js index 9daf011..2913c37 100644 --- a/backend/src/services/mistralClient.js +++ b/backend/src/services/mistralClient.js @@ -16,24 +16,27 @@ Your responsibilities: 1. Review the current document structure 2. Either: a) Modify ONE existing section (identified by #, ##, ###, #### headers), OR - b) Create a NEW section if you think it's needed + b) Create a NEW section if you think it's needed, OR + c) Delete a section if you think it's redundant or not useful 3. Provide your thinking process and reasoning -4. Return ONLY the section (modified or new) with its header, or confirm it's good as-is +4. Return ONLY the section (modified or new) with its header, or command to delete, or confirm it's good as-is IMPORTANT RULES: - Work on exactly ONE section only - Never modify the entire document - Return only the section you're working on, not the whole document -- You CAN create a new section if you think the document is missing important content +- You CAN create a new section if document is missing important content +- You CAN delete a section if it's redundant, duplicate, or not useful +- To delete a section, respond: "DELETE: ## Section Name" (with exact header) - If section is good, respond: "Section is good, no changes needed" -- Think step-by-step about what could be improved +- Think step-by-step about what could be improved or removed - Share your reasoning process Format your response as: THINKING: [Your analysis and reasoning] -DECISION: [What you'll modify, create, or if keeping as-is] +DECISION: [What you'll modify, create, delete, or if keeping as-is] SECTION: -[The modified section, new section, or confirmation that all is good]` +[The modified section, new section, DELETE command, or confirmation that all is good]` } /** diff --git a/frontend/src/components/CollaborativeSession.vue b/frontend/src/components/CollaborativeSession.vue index 18a6d8f..67693d0 100644 --- a/frontend/src/components/CollaborativeSession.vue +++ b/frontend/src/components/CollaborativeSession.vue @@ -225,20 +225,10 @@ function formatAgentName(agent) {
is reviewing...
-Thinking:
-{{ currentAgentThinking }}
+