| |
| """ |
| Test that phase markers are properly formatted on new lines |
| """ |
|
|
| import re |
|
|
| def filter_internal_tags(text): |
| """Apply the same filtering logic as the main app""" |
|
|
| |
| text = re.sub(r'</?(?:thinking|reflection|search_quality|query_analysis)>', '', text, flags=re.IGNORECASE) |
| text = re.sub(r'</?(result|answer)>', '', text) |
|
|
| |
| |
| phase_patterns = [ |
| |
| (r'(?<!\n)(\*\*π―\s*PLANNING:\*\*)', r'\n\1'), |
| (r'(?<!\n)(\*\*π§\s*EXECUTING:\*\*)', r'\n\1'), |
| (r'(?<!\n)(\*\*π€\s*REFLECTING:\*\*)', r'\n\1'), |
| (r'(?<!\n)(\*\*β
\s*SYNTHESIS:\*\*)', r'\n\1'), |
| (r'(?<!\n)(\*\*β
\s*ANSWER:\*\*)', r'\n\1'), |
| |
| (r'(\*\*π―\s*PLANNING:\*\*)', r'\1\n'), |
| (r'(\*\*π§\s*EXECUTING:\*\*)', r'\1\n'), |
| (r'(\*\*π€\s*REFLECTING:\*\*)', r'\1\n'), |
| (r'(\*\*β
\s*SYNTHESIS:\*\*)', r'\1\n'), |
| (r'(\*\*β
\s*ANSWER:\*\*)', r'\1\n'), |
| ] |
|
|
| for pattern, replacement in phase_patterns: |
| text = re.sub(pattern, replacement, text) |
|
|
| |
| text = re.sub(r'[ \t]+', ' ', text) |
| text = re.sub(r'\n{4,}', '\n\n\n', text) |
| text = re.sub(r'^\n+', '', text) |
| text = re.sub(r'\n+$', '\n', text) |
|
|
| return text.strip() |
|
|
| def check_phase_on_newline(text, phase_marker): |
| """Check if a phase marker appears on its own line""" |
| |
| import re |
|
|
| |
| if phase_marker not in text: |
| return None, "Marker not found" |
|
|
| |
| pattern = re.compile(re.escape(phase_marker)) |
| matches = list(pattern.finditer(text)) |
|
|
| issues = [] |
| for match in matches: |
| start_pos = match.start() |
|
|
| |
| if start_pos > 0: |
| prev_char = text[start_pos - 1] |
| if prev_char != '\n': |
| |
| context_start = max(0, start_pos - 10) |
| context_end = min(len(text), match.end() + 10) |
| context = text[context_start:context_end] |
| issues.append(f"Not on new line. Context: ...{repr(context)}...") |
|
|
| if issues: |
| return False, issues |
| return True, "OK - on new line" |
|
|
| |
| test_cases = [ |
| |
| ("Some text **π― PLANNING:** here is the plan", "Inline marker - should be fixed"), |
|
|
| |
| ("Some text\n**π― PLANNING:** here is the plan", "Already on new line"), |
|
|
| |
| ("Text before **π― PLANNING:** plan text **π§ EXECUTING:** execute text", "Multiple inline markers"), |
|
|
| |
| ("π **Search Results:** The search results did not provide... **π― PLANNING:** To answer the user's question", "Real problematic case"), |
| ] |
|
|
| print("=" * 70) |
| print("TESTING PHASE MARKER FORMATTING") |
| print("=" * 70) |
|
|
| phase_markers = [ |
| "**π― PLANNING:**", |
| "**π§ EXECUTING:**", |
| "**π€ REFLECTING:**", |
| "**β
SYNTHESIS:**" |
| ] |
|
|
| for i, (test_text, description) in enumerate(test_cases, 1): |
| print(f"\nTest Case {i}: {description}") |
| print("-" * 50) |
|
|
| |
| filtered = filter_internal_tags(test_text) |
|
|
| print(f"Original text ({len(test_text)} chars):") |
| print(f" {repr(test_text[:100])}...") |
| print(f"\nFiltered text ({len(filtered)} chars):") |
| print(f" {repr(filtered[:100])}...") |
|
|
| |
| print("\nPhase marker checks:") |
| for marker in phase_markers: |
| if marker in filtered: |
| is_ok, info = check_phase_on_newline(filtered, marker) |
| if is_ok: |
| print(f" β
{marker[:20]}... - {info}") |
| else: |
| print(f" β {marker[:20]}... - Issues: {info}") |
| else: |
| print(f" βͺ {marker[:20]}... - Not present") |
|
|
| print("\n" + "=" * 70) |
| print("FORMATTING VERIFICATION") |
| print("=" * 70) |
|
|
| |
| problematic = """π **Search Results:** The search results did not provide any relevant information on psilocybin trials and use in therapy for ALS. **π― PLANNING:** To answer the user's question about psilocybin trials and use in therapy for ALS, I will first search the PubMed database. **π§ EXECUTING:** 1. Search PubMed for peer-reviewed research papers. **π€ REFLECTING:** 1. Do I have sufficient high-quality information? **β
SYNTHESIS:** Based on the available information, there is limited research.""" |
|
|
| print("Testing problematic response:") |
| filtered = filter_internal_tags(problematic) |
| print("\nFiltered output:") |
| print(filtered) |
|
|
| |
| print("\n" + "=" * 70) |
| lines = filtered.split('\n') |
| for i, line in enumerate(lines, 1): |
| for marker in phase_markers: |
| if marker in line: |
| |
| if line.strip().startswith(marker): |
| print(f"β
Line {i}: {marker} is properly at line start") |
| else: |
| print(f"β Line {i}: {marker} is NOT at line start") |
| print(f" Full line: {repr(line[:60])}...") |
|
|
| print("\n" + "=" * 70) |
| print("SUMMARY") |
| print("=" * 70) |
| print(""" |
| The formatting patterns have been updated to: |
| 1. Check if phase markers are NOT preceded by a newline (?<!\\n) |
| 2. Add a newline before them if needed |
| 3. Ensure a newline after them as well |
| 4. Clean up any excessive whitespace |
| |
| This ensures all phase markers appear on their own lines. |
| """) |