Update personality.py

This commit is contained in:
Saifeddine ALOUI 2025-01-10 17:32:16 +01:00 committed by GitHub
parent 12ca2678c7
commit f995c304a0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -4257,10 +4257,11 @@ transition-all duration-300 ease-in-out">
else: else:
return None return None
@staticmethod @staticmethod
def update_code(original_content: str, original_code: str, new_code: str) -> str: def update_code_with_best_match(original_content: str, original_code: str, new_code: str) -> str:
""" """
Updates the original content by replacing the original code snippet with the new code. Updates the original content by replacing the best-matching code snippet with the new code.
Args: Args:
original_content (str): The complete source code content original_content (str): The complete source code content
@ -4271,10 +4272,13 @@ transition-all duration-300 ease-in-out">
str: Updated content with the replacement made str: Updated content with the replacement made
Raises: Raises:
ValueError: If the original code snippet is not found in the content ValueError: If no sufficiently close match is found
or if multiple matches are found
""" """
# Normalize line endings and strip whitespace from search pattern if not pm.is_installed("difflib"):
pm.install("difflib")
import difflib
# Normalize line endings and strip whitespace from inputs
original_code = original_code.strip() original_code = original_code.strip()
new_code = new_code.strip() new_code = new_code.strip()
@ -4282,34 +4286,39 @@ transition-all duration-300 ease-in-out">
if not original_content or not original_code: if not original_content or not original_code:
raise ValueError("Original content and code snippet cannot be empty") raise ValueError("Original content and code snippet cannot be empty")
# Find all occurrences of the original code # Split content into lines for better matching
# Using splitlines() and join to normalize line endings content_lines = original_content.splitlines()
normalized_content = '\n'.join(original_content.splitlines()) code_lines = original_code.splitlines()
normalized_code = '\n'.join(original_code.splitlines())
# Split content into lines for more precise matching # Join lines to create a normalized version of the original code
content_lines = normalized_content.splitlines() normalized_code = '\n'.join(code_lines)
code_lines = normalized_code.splitlines()
# Find the best match using difflib
best_match = None
best_match_ratio = 0
best_match_start = None
best_match_end = None
# Find matches
matches = []
for i in range(len(content_lines) - len(code_lines) + 1): for i in range(len(content_lines) - len(code_lines) + 1):
# Extract a slice of the content to compare
content_slice = '\n'.join(content_lines[i:i + len(code_lines)]) content_slice = '\n'.join(content_lines[i:i + len(code_lines)])
if content_slice.strip() == normalized_code.strip():
matches.append(i)
# Check number of matches # Calculate similarity ratio
if not matches: match_ratio = difflib.SequenceMatcher(None, content_slice.strip(), normalized_code.strip()).ratio()
raise ValueError("Original code snippet not found in content")
if len(matches) > 1:
raise ValueError("Multiple matches found for the original code snippet")
# Perform the replacement # Update the best match if this one is better
start_line = matches[0] if match_ratio > best_match_ratio:
end_line = start_line + len(code_lines) best_match = content_slice
best_match_ratio = match_ratio
best_match_start = i
best_match_end = i + len(code_lines)
# Check if a sufficiently close match was found
if best_match is None or best_match_ratio < 0.8: # Threshold for similarity
raise ValueError("No sufficiently close match found for the original code snippet")
# Preserve original indentation # Preserve original indentation
first_line = content_lines[start_line] first_line = content_lines[best_match_start]
indentation = '' indentation = ''
for char in first_line: for char in first_line:
if char in (' ', '\t'): if char in (' ', '\t'):
@ -4323,14 +4332,15 @@ transition-all duration-300 ease-in-out">
# Combine everything together # Combine everything together
updated_lines = ( updated_lines = (
content_lines[:start_line] + content_lines[:best_match_start] +
new_code_lines + new_code_lines +
content_lines[end_line:] content_lines[best_match_end:]
) )
return '\n'.join(updated_lines) return '\n'.join(updated_lines)
def make_title(self, prompt, max_title_length: int = 50): def make_title(self, prompt, max_title_length: int = 50):
""" """
Generates a title for a given prompt. Generates a title for a given prompt.