Hello we are going to work on a feature/refactor for my program first i will give you…
- User
Hello we are going to work on a feature/refactor for my program first i will give you some code context that will help you understand my thinking and feature request. Then we will do a comprehension check and refactor my query for clarity so we are calibrated. Then in a final query we will do the actual implementation and make sure there are no regressions. First here is the code and the initial query: study this and pay close attention to how conversation history is currently used to store file context. This will be the key change we will make.
chat.py
import logging import json import os from typing import List, Dict, Any, Tuple from efc.efc_cache import get_file_context from app_state import get_app_state from chat.model_config import get_model_config from tools.tools import ToolManager from utils.token_usage import update_token_usage from chat.factory import AIClientFactory import asyncio import logging # logging.basicConfig(level=logging.DEBUG) logging.basicConfig(level=logging.INFO) async def chat_with_model(user_input: str, tool_manager: ToolManager, model_name: str) -> str: logging.info(f"Processing user input: {user_input}") logging.info(f"Using model: {model_name}") logging.info(f"Available tools: {[tool['function']['name'] for tool in tool_manager.get_tool_descriptions()]}") app_state = get_app_state() # Add the new user input to the history before optimization app_state.conversation_history.append({"role": "user", "content": user_input}) file_context = get_file_context() model_config = get_model_config(model_name) ai_client = AIClientFactory.get_ai_client(model_name) logging.info(f"Using AI client: {type(ai_client).__name__}") optimized_history = optimize_conversation_history(app_state.conversation_history, app_state.file_histories) # file_context_summary = file_context.get_context_for_llm() file_context_summary = file_context.generate_context_awareness_prompt(app_state) logging.info(f"context awareness promptt: {file_context_summary}") # system_message = f"{model_config['base_system_prompt']}\n\n{model_config['function_definitions']}\n\n{file_context_summary}" system_message = f"{model_config['base_system_prompt']}\n\n{file_context_summary}" tools = tool_manager.get_tool_descriptions(format="claude" if model_name.lower() in ['sonnet', 'claude-3-sonnet-20240229'] else "default") logging.info(f"History passed to AI client: {optimized_history}") try: logging.info("Creating message with AI client") response = await ai_client.create_message( prompt=user_input, system_message=system_message, tools=tools, history=optimized_history[:-1] ) logging.info("Message created successfully") if hasattr(response, 'usage'): if model_name.lower() in ['sonnet', 'claude-3-sonnet-20240229']: update_token_usage(model_name, { "prompt_tokens": response.usage.input_tokens, "completion_tokens": response.usage.output_tokens, "total_tokens": response.usage.input_tokens + response.usage.output_tokens, "cache_creation_tokens": getattr(response.usage, 'cache_creation_input_tokens', 0), "cache_read_tokens": getattr(response.usage, 'cache_read_input_tokens', 0) }) else: # For Llama and GPT update_token_usage(model_name, { "prompt_tokens": response.usage.prompt_tokens, "completion_tokens": response.usage.completion_tokens, "total_tokens": response.usage.total_tokens }) except Exception as e: logging.error(f"API Error: {str(e)}") return f"I'm sorry, there was an error communicating with the AI. Please try again. Error: {str(e)}" # With: if hasattr(ai_client, 'extract_response'): assistant_response = ai_client.extract_response(response) if asyncio.iscoroutine(assistant_response): assistant_response = await assistant_response else: assistant_response = response.content[0].text if response.content else "" logging.info(f"EXTRACTED RESPONSE: {assistant_response}") function_calls = ai_client.extract_function_calls(response) function_results = await execute_function_calls(function_calls, tool_manager, model_name) if not assistant_response and function_results: assistant_response = "I've performed the following actions based on your request:\n\n" assistant_response = integrate_function_results(assistant_response, function_results, model_config) app_state.conversation_history.append({"role": "assistant", "content": assistant_response}) logging.info(f"Final assistant response: {assistant_response}") return assistant_response async def execute_function_calls(function_calls, tool_manager, model_name): results = [] if asyncio.iscoroutine(function_calls): function_calls = await function_calls for function_call in function_calls: try: if isinstance(function_call, dict): # Llama format func_name = function_call.get('function') arguments = function_call.get('arguments', {}) else: # GPT-4 format func_name = function_call.function.name arguments = json.loads(function_call.function.arguments) result = await tool_manager.execute_tool({"function": func_name, "arguments": arguments}, model_name) results.append((func_name, arguments, result)) except Exception as e: logging.error(f"Error executing function: {str(e)}") results.append((func_name, arguments, f"Error executing function: {str(e)}")) return results def integrate_function_results(assistant_response: str, function_results: List[Tuple[str, Dict, str]], model_config: Dict) -> str: if not assistant_response: assistant_response = "Here are the results of the actions I've taken:\n\n" for func_name, args, result in function_results: if model_config.get('use_tool_calls', False): assistant_response += f"\n\nResult of {func_name}: {result}" else: # For models that embed function calls in the response placeholder = f"<function={func_name}>{json.dumps(args)}</function>" if placeholder in assistant_response: assistant_response = assistant_response.replace(placeholder, str(result)) else: assistant_response += f"\n\nResult of {func_name}: {result}" return assistant_response from dataclasses import dataclass from typing import List, Dict, Any import difflib import logging import time @dataclass class FileHistory: most_recent_content: str second_most_recent_content: str diff_summary: str last_update_timestamp: float def optimize_conversation_history(history: List[Dict[str, Any]], file_histories: Dict[str, FileHistory]) -> List[Dict[str, Any]]: # logging.info(f"History before optimization: {history}") optimized_history = history.copy() file_instance_count = {} file_instances = {} # First pass: count instances and collect content for i, message in enumerate(optimized_history): if message['role'] == 'assistant' and is_file_content(message['content']): relative_path = extract_relative_path(message['content']) file_instance_count[relative_path] = file_instance_count.get(relative_path, 0) + 1 if relative_path not in file_instances: file_instances[relative_path] = [] file_content = extract_file_content(message['content']) file_instances[relative_path].append((i, file_content)) # Update the message content with relative path message['content'] = f"Fresh content for {relative_path}: {file_content}" # Second pass: summarize oldest instance if more than two for relative_path, instances in file_instances.items(): if len(instances) > 2: oldest_index, oldest_content = instances[0] second_oldest_index, second_oldest_content = instances[1] newest_index, newest_content = instances[-1] if file_histories is not None: update_file_history(relative_path, newest_content, file_histories, is_latest=True) update_file_history(relative_path, second_oldest_content, file_histories, is_latest=False) summary = generate_summary(relative_path, oldest_content, newest_content, second_oldest_content) optimized_history[oldest_index]['content'] = summary logging.info(f"History after optimization: {optimized_history}") return optimized_history def extract_relative_path(content: str) -> str: app_state = get_app_state() active_project = app_state.active_project if "Fresh content for" in content: full_path = content.split("Fresh content for")[1].split(":")[0].strip() elif "Cached content for" in content: full_path = content.split("Cached content for")[1].split(":")[0].strip() else: return "Unknown File" return os.path.relpath(full_path, active_project) def generate_summary(relative_path: str, original_content: str, latest_content: str, second_latest_content: str, max_length: int = 200) -> str: app_state = get_app_state() active_project = os.path.basename(app_state.active_project) original_lines = original_content.splitlines() latest_lines = latest_content.splitlines() second_latest_lines = second_latest_content.splitlines() diff1 = list(difflib.unified_diff(original_lines, second_latest_lines, lineterm='')) diff2 = list(difflib.unified_diff(second_latest_lines, latest_lines, lineterm='')) changes1 = [line for line in diff1 if line.startswith('+') or line.startswith('-')][:3] changes2 = [line for line in diff2 if line.startswith('+') or line.startswith('-')][:3] summary = f"Original content: {original_content[:50]}... " if changes1: summary += f"First update changes: {'; '.join(changes1)} " if changes2: summary += f"Second update changes: {'; '.join(changes2)}" return f"Summary of changes in {relative_path} (in project: {active_project}, as of {time.strftime('%Y-%m-%d %H:%M:%S')}): {summary[:max_length]}..." def update_file_history(relative_path: str, new_content: str, file_histories: Dict[str, FileHistory], is_latest: bool) -> None: if relative_path not in file_histories: file_histories[relative_path] = FileHistory(new_content, "", "", time.time()) else: history = file_histories[relative_path] if is_latest: history.second_most_recent_content = history.most_recent_content history.most_recent_content = new_content else: history.second_most_recent_content = new_content history.last_update_timestamp = time.time() def is_file_content(content: str) -> bool: return "Fresh content for" in content or "Cached content for" in content def extract_file_content(content: str) -> str: return content.split(":", 1)[1].strip() - Assistant