{converted_visible[0]}
'
f'{user_attachments}'
f'{actions_html(history, i, "user", info_message_user)}'
f'tags, keeping original quotes def replacer(m): # Find the first non-None group set for i in range(1, len(m.groups()), 3): # Step through each sub-pattern's groups if m.group(i): # If this sub-pattern matched return f'{m.group(i)}{m.group(i + 1)}{m.group(i + 2)}' return m.group(0) # Fallback (shouldn't happen) replaced_text = re.sub(pattern, replacer, text, flags=re.DOTALL) return replaced_text def replace_blockquote(m): return m.group().replace('\n', '\n> ').replace('\\begin{blockquote}', '').replace('\\end{blockquote}', '') def extract_thinking_block(string): """Extract thinking blocks from the beginning of a string.""" if not string: return None, string THINK_START_TAG = "<think>" THINK_END_TAG = "</think>" # Look for opening tag start_pos = string.lstrip().find(THINK_START_TAG) if start_pos == -1: return None, string # Adjust start position to account for any leading whitespace start_pos = string.find(THINK_START_TAG) # Find the content after the opening tag content_start = start_pos + len(THINK_START_TAG) # Look for closing tag end_pos = string.find(THINK_END_TAG, content_start) if end_pos != -1: # Both tags found - extract content between them thinking_content = string[content_start:end_pos] remaining_content = string[end_pos + len(THINK_END_TAG):] return thinking_content, remaining_content else: # Only opening tag found - everything else is thinking content thinking_content = string[content_start:] return thinking_content, "" @functools.lru_cache(maxsize=None) def convert_to_markdown(string, message_id=None): if not string: return "" # Use a default message ID if none provided if message_id is None: message_id = "unknown" # Extract thinking block if present thinking_content, remaining_content = extract_thinking_block(string) # Process the main content html_output = process_markdown_content(remaining_content) # If thinking content was found, process it using the same function if thinking_content is not None: thinking_html = process_markdown_content(thinking_content) # Generate unique ID for the thinking block block_id = f"thinking-{message_id}-0" # Check if thinking is complete or still in progress is_streaming = not remaining_content title_text = "Thinking..." if is_streaming else "Thought" thinking_block = f'''''' # Prepend the thinking block to the message HTML html_output = thinking_block + html_output return html_output def process_markdown_content(string): """Process a string through the markdown conversion pipeline.""" if not string: return "" # Make \[ \] LaTeX equations inline pattern = r'^\s*\\\[\s*\n([\s\S]*?)\n\s*\\\]\s*$' replacement = r'\\[ \1 \\]' string = re.sub(pattern, replacement, string, flags=re.MULTILINE) # Escape backslashes string = string.replace('\\', '\\\\') # Quote to string = replace_quotes(string) # Blockquote string = re.sub(r'(^|[\n])>', r'\1>', string) pattern = re.compile(r'\\begin{blockquote}(.*?)\\end{blockquote}', re.DOTALL) string = pattern.sub(replace_blockquote, string) # Code string = string.replace('\\begin{code}', '```') string = string.replace('\\end{code}', '```') string = string.replace('\\begin{align*}', '$$') string = string.replace('\\end{align*}', '$$') string = string.replace('\\begin{align}', '$$') string = string.replace('\\end{align}', '$$') string = string.replace('\\begin{equation}', '$$') string = string.replace('\\end{equation}', '$$') string = string.replace('\\begin{equation*}', '$$') string = string.replace('\\end{equation*}', '$$') string = re.sub(r"(.)```", r"\1\n```", string) result = '' is_code = False is_latex = False for line in string.split('\n'): stripped_line = line.strip() if stripped_line.startswith('```'): is_code = not is_code elif stripped_line.startswith('$$'): is_latex = not is_latex elif stripped_line.endswith('$$'): is_latex = False elif stripped_line.startswith('\\\\['): is_latex = True elif stripped_line.startswith('\\\\]'): is_latex = False elif stripped_line.endswith('\\\\]'): is_latex = False result += line # Don't add an extra \n for code, LaTeX, or tables if is_code or is_latex or line.startswith('|'): result += '\n' # Also don't add an extra \n for lists elif stripped_line.startswith('-') or stripped_line.startswith('*') or stripped_line.startswith('+') or stripped_line.startswith('>') or re.match(r'\d+\.', stripped_line): result += ' \n' else: result += ' \n' result = result.strip() if is_code: result += '\n```' # Unfinished code block # Unfinished list, like "\n1.". A |delete| string is added and then # removed to force a{info_svg_small} {title_text}
or
to be generated instead of a
. list_item_pattern = r'(\n\d+\.?|\n\s*[-*+]\s*([*_~]{1,3})?)$' if re.search(list_item_pattern, result): delete_str = '|delete|' if re.search(r'(\d+\.?)$', result) and not result.endswith('.'): result += '.' # Add the delete string after the list item result = re.sub(list_item_pattern, r'\g<1> ' + delete_str, result) # Convert to HTML using markdown html_output = markdown.markdown(result, extensions=['fenced_code', 'tables', SaneListExtension()]) # Remove the delete string from the HTML output pos = html_output.rfind(delete_str) if pos > -1: html_output = html_output[:pos] + html_output[pos + len(delete_str):] else: # Convert to HTML using markdown html_output = markdown.markdown(result, extensions=['fenced_code', 'tables', SaneListExtension()]) # Unescape code blocks pattern = re.compile(r'
]*>(.*?)
', re.DOTALL) html_output = pattern.sub(lambda x: html.unescape(x.group()), html_output) # Unescape backslashes html_output = html_output.replace('\\\\', '\\') return html_output def convert_to_markdown_wrapped(string, message_id=None, use_cache=True): ''' Used to avoid caching convert_to_markdown calls during streaming. ''' if use_cache: return convert_to_markdown(string, message_id=message_id) return convert_to_markdown.__wrapped__(string, message_id=message_id) def generate_basic_html(string): convert_to_markdown.cache_clear() string = convert_to_markdown(string) string = f'{string}' return string def make_thumbnail(image): image = image.resize((350, round(image.size[1] / image.size[0] * 350)), Image.Resampling.LANCZOS) if image.size[1] > 470: image = ImageOps.fit(image, (350, 470), Image.LANCZOS) return image def get_image_cache(path): cache_folder = Path(shared.args.disk_cache_dir) if not cache_folder.exists(): cache_folder.mkdir() mtime = os.stat(path).st_mtime if (path in image_cache and mtime != image_cache[path][0]) or (path not in image_cache): img = make_thumbnail(Image.open(path)) old_p = Path(f'{cache_folder}/{path.name}_cache.png') p = Path(f'{cache_folder}/cache_{path.name}.png') if old_p.exists(): old_p.rename(p) output_file = p img.convert('RGBA').save(output_file, format='PNG') image_cache[path] = [mtime, output_file.as_posix()] return image_cache[path][1] copy_svg = '''''' refresh_svg = '''''' continue_svg = '''''' remove_svg = '''''' branch_svg = '''''' edit_svg = '''''' info_svg = '''''' info_svg_small = '''''' attachment_svg = '''''' copy_button = f'' branch_button = f'' edit_button = f'' refresh_button = f'' continue_button = f'' remove_button = f'' info_button = f'' def format_message_timestamp(history, role, index): """Get a formatted timestamp HTML span for a message if available""" key = f"{role}_{index}" if 'metadata' in history and key in history['metadata'] and history['metadata'][key].get('timestamp'): timestamp = history['metadata'][key]['timestamp'] return f" " return "" def format_message_attachments(history, role, index): """Get formatted HTML for message attachments if available""" key = f"{role}_{index}" if 'metadata' in history and key in history['metadata'] and 'attachments' in history['metadata'][key]: attachments = history['metadata'][key]['attachments'] if not attachments: return "" attachments_html = ' ' return attachments_html return "" def get_version_navigation_html(history, i, role): """Generate simple navigation arrows for message versions""" key = f"{role}_{i}" metadata = history.get('metadata', {}) if key not in metadata or 'versions' not in metadata[key]: return "" versions = metadata[key]['versions'] # Default to the last version if current_version_index isn't set in metadata current_idx = metadata[key].get('current_version_index', len(versions) - 1 if versions else 0) if len(versions) <= 1: return "" left_disabled = ' disabled' if current_idx == 0 else '' right_disabled = ' disabled' if current_idx >= len(versions) - 1 else '' left_arrow = f'' right_arrow = f'' position = f'{current_idx + 1}/{len(versions)}' return f' ' def actions_html(history, i, role, info_message=""): action_buttons = "" version_nav_html = "" if role == "assistant": action_buttons = ( f'{copy_button}' f'{edit_button}' f'{refresh_button if i == len(history["visible"]) - 1 else ""}' f'{continue_button if i == len(history["visible"]) - 1 else ""}' f'{remove_button if i == len(history["visible"]) - 1 else ""}' f'{branch_button}' ) version_nav_html = get_version_navigation_html(history, i, "assistant") elif role == "user": action_buttons = ( f'{copy_button}' f'{edit_button}' ) version_nav_html = get_version_navigation_html(history, i, "user") return (f' ' f'{version_nav_html}') def generate_instruct_html(history): output = f'" return output def generate_cai_chat_html(history, name1, name2, style, character, reset_cache=False): output = f'" return output def generate_chat_html(history, name1, name2, reset_cache=False): output = f'" return output def time_greeting(): current_hour = datetime.datetime.now().hour if 5 <= current_hour < 12: return "Good morning!" elif 12 <= current_hour < 18: return "Good afternoon!" else: return "Good evening!" def chat_html_wrapper(history, name1, name2, mode, style, character, reset_cache=False): if len(history['visible']) == 0: greeting = f"{time_greeting()} How can I help you today?" result = f'{greeting}' elif mode == 'instruct': result = generate_instruct_html(history) elif style == 'wpp': result = generate_chat_html(history, name1, name2) else: result = generate_cai_chat_html(history, name1, name2, style, character, reset_cache) return {'html': result}