Coverage for src / local_deep_research / web / utils / formatters.py: 89%

23 statements  

« prev     ^ index     » next       coverage.py v7.12.0, created at 2026-01-11 00:51 +0000

1from loguru import logger 

2 

3 

4def convert_debug_to_markdown(raw_text, query): 

5 """ 

6 Convert the debug-formatted text to clean markdown. 

7 

8 Args: 

9 raw_text: The raw formatted findings with debug symbols 

10 query: Original research query 

11 

12 Returns: 

13 Clean markdown formatted text 

14 """ 

15 try: 

16 logger.info(f"Starting markdown conversion for query: {query}") 

17 logger.info(f"Raw text type: {type(raw_text)}") 

18 

19 # Handle None or empty input 

20 if not raw_text: 

21 logger.warning("WARNING: raw_text is empty or None") 

22 return f"No detailed findings available for '{query}'." 

23 

24 # If there's a "DETAILED FINDINGS:" section, extract everything after it 

25 if "DETAILED FINDINGS:" in raw_text: 

26 logger.info("Found DETAILED FINDINGS section") 

27 detailed_index = raw_text.index("DETAILED FINDINGS:") 

28 content = raw_text[ 

29 detailed_index + len("DETAILED FINDINGS:") : 

30 ].strip() 

31 else: 

32 logger.info("No DETAILED FINDINGS section found, using full text") 

33 content = raw_text 

34 

35 # Remove divider lines with === symbols 

36 lines_before = len(content.split("\n")) 

37 content = "\n".join( 

38 [ 

39 line 

40 for line in content.split("\n") 

41 if not line.strip().startswith("===") 

42 and not line.strip() == "=" * 80 

43 ] 

44 ) 

45 lines_after = len(content.split("\n")) 

46 logger.info(f"Removed {lines_before - lines_after} divider lines") 

47 

48 logger.info(f"Final markdown length: {len(content.strip())}") 

49 return content.strip() 

50 except Exception as e: 

51 logger.exception(f"Error in convert_debug_to_markdown: {e!s}") 

52 # Return a basic message with the original query as fallback 

53 return f"# Research on {query}\n\nThere was an error formatting the research results."