Lets look at some code files and functions then a high level targeted refactor idea and…
- User
Lets look at some code files and functions then a high level targeted refactor idea and step through updating related improvements:
first step lets review and update our tools.py to make sure formating and older deprecated tool references as update to our current consolidated ones. Here are two new ones:
async def project_activate(existing_project_path: str, file_names: Optional[List[str]] = None, return_structure: bool = False, **kwargs) -> Dict[str, Union[str, List[str], Dict[str, str]]]: """ Activates an existing project, granting permission to operate within its directory. Returns project file structure and/or content of specified files. Updates the AppState with the activated project and its tree structure. Args: existing_project_path (str): The path to the existing project directory. file_names (List[str], optional): A list of file names within the project to read and return their content. return_structure (bool, optional): If True, returns the full project structure. Defaults to False. **kwargs: Additional keyword arguments. Returns: Dict[str, Union[str, List[str], Dict[str, str]]]: A dictionary containing: - 'status': 'activated' if successful, 'error' if an issue occurred. - 'message': Additional information, especially useful for error descriptions. - 'project_name': The name of the activated project. - 'project_structure': List of all files in the project as relative paths (if return_structure is True). - 'file_contents': Dict of file contents with relative paths as keys (if file_names provided). Note: This function updates the AppState with the activated project path and its tree structure. """ # Initialize a lock for thread-safe operations on AppState state_lock = Lock() try: # Validate the project path if not os.path.isdir(existing_project_path): return { "status": "error", "message": f"The specified path does not exist or is not a directory: {existing_project_path}" } # Get the absolute path project_path = os.path.abspath(existing_project_path) # Acquire lock before modifying AppState async with state_lock: # Update app state with the activated project app_state = get_app_state() app_state.active_project = project_path # Initialize result dictionary result = { "status": "activated", "project_name": os.path.basename(project_path) } # Handle specified file contents if file_names: file_contents = {} file_context = get_file_context() for file_name in file_names: # Ensure file_name is a relative path normalized_file_name = os.path.normpath(file_name) # Prevent path traversal outside the project directory if os.path.isabs(normalized_file_name) or normalized_file_name.startswith(".."): logging.warning(f"Invalid file path requested: {file_name}") continue full_path = os.path.join(project_path, normalized_file_name) if os.path.isfile(full_path): try: # Asynchronously read file content async with aiofiles.open(full_path, 'r') as f: content = await f.read() file_contents[normalized_file_name] = content # Update file context file_context.add_or_update_file(normalized_file_name, content, "read") except Exception as read_err: logging.error(f"Error reading file '{file_name}': {read_err}") continue else: logging.warning(f"File not found: {file_name}") result["file_contents"] = file_contents # Handle project structure if return_structure: try: project_structure = await list_files_for_project(directory=project_path) result["project_structure"] = project_structure except Exception as struct_err: logging.error(f"Error retrieving project structure: {struct_err}") result["project_structure"] = [] # Update the active project's tree structure app_state.update_active_project_tree() return result except Exception as e: logging.error(f"Error in project_activate: {e}") return { "status": "error", "message": str(e) } finally: # Access the status from the result or default to 'error' status = locals().get('result', {}).get('status', 'error') status_message = 'successful' if status == 'activated' else 'failed' logging.info(f"Project activation {status_message} for: {existing_project_path}") async def project_create(project_name: str, directory: Optional[str] = None, file_structure: Optional[List[str]] = None) -> Dict[str, Union[str, List[str], str]]: try: project_path = os.path.join(directory or os.getcwd(), project_name) # Check if the project already exists if os.path.exists(project_path): logging.warning(f"Project {project_name} already exists at {project_path}") return {"status": "error", "message": f"Project {project_name} already exists."} # Get user approval logging.info("Requesting user approval") try: user_approved = await get_user_approval_for_project(project_name, project_path, file_structure or []) logging.info(f"User approval result: {user_approved}") except Exception as e: logging.error(f"Error during user approval: {str(e)}", exc_info=True) return {"status": "error", "message": f"Error during user approval: {str(e)}"} if not user_approved: logging.info("Project creation cancelled by user") return {"status": "cancelled", "message": "Project creation cancelled by user."} os.makedirs(project_path, exist_ok=True) logging.info(f"Created project directory: {project_path}") file_context = get_file_context() app_state = get_app_state() app_state.active_project = project_path # Set the newly created project as active file_creation_messages = [] if file_structure: relative_file_structure = [to_relative_path(item, project_path) for item in file_structure] else: relative_file_structure = [] created_files = [] skipped_files = [] for item in relative_file_structure: full_path = os.path.join(project_path, item) try: if item.endswith('/'): os.makedirs(full_path, exist_ok=True) logging.info(f"Created directory: {item}") else: # Check if the path is within the project directory and doesn't contain backslashes if os.path.commonpath([project_path, os.path.abspath(full_path)]) != project_path or '\\' in item: raise ValueError(f"Invalid path: {item}") os.makedirs(os.path.dirname(full_path), exist_ok=True) with open(full_path, 'w') as f: pass created_files.append(item) logging.info(f"Created file: {item}") # Add file to context history file_context.add_or_update_file(item, "", "create") # update_message = file_context.add_or_update_file(item, "", "create") # file_creation_messages.append(f"File created: {item}\nFile Context Update: {update_message}") except Exception as e: logging.warning(f"Failed to create {item}: {str(e)}") skipped_files.append(item) # # Consolidate all file creation messages into a single response # if file_creation_messages: # consolidated_response = "Project structure created:\n" + "\n".join(file_creation_messages) # if skipped_files: # consolidated_response += f"\n\nSkipped files:\n" + "\n".join(skipped_files) app_state.update_active_project_tree() return { "project_name": project_name, "file_structure": relative_file_structure, "created_files": created_files, "skipped_files": skipped_files, "status": "created" } except Exception as e: logging.error(f"Error in project_create: {str(e)}") return { "status": "error", "message": str(e) } async def list_files_for_project(directory: str = ".", ignore_dir_patterns: Optional[List[str]] = None, ignore_file_patterns: Optional[List[str]] = None, max_depth: int = 5) -> List[str]: """ Lists all files within the given directory up to a specified depth, excluding directories and files that match the ignore patterns. Args: directory (str): The root directory to list files from. ignore_dir_patterns (List[str], optional): Patterns for directories to ignore. Defaults to ['.git', 'pycache', 'bin', 'lib']. ignore_file_patterns (List[str], optional): Patterns for files to ignore. Defaults to ['.pyc']. max_depth (int, optional): Maximum directory depth to traverse. Defaults to 5. Returns: List[str]: A list of relative file paths. """ if ignore_dir_patterns is None: ignore_dir_patterns = ['.git', 'pycache', 'bin', 'lib'] if ignore_file_patterns is None: ignore_file_patterns = ['.pyc'] try: files = [] for root, dirs, filenames in os.walk(directory): # Calculate current depth relative_path = os.path.relpath(root, directory) depth = relative_path.count(os.sep) if relative_path != '.' else 0 if depth >= max_depth: dirs[:] = [] # Prevent further traversal continue # Exclude directories matching ignore patterns dirs[:] = [d for d in dirs if not any(fnmatch.fnmatch(d, pattern) for pattern in ignore_dir_patterns)] for filename in filenames: if not any(fnmatch.fnmatch(filename, pattern) for pattern in ignore_file_patterns): files.append(os.path.relpath(os.path.join(root, filename), directory)) return files except Exception as e: logging.error(f"Error listing files in '{directory}': {e}") raise # Propagate exception for higher-level handlinghere is our current tools for reference:
def initialize_tool_manager(): # Initialize ToolManager and add tools tool_manager = ToolManager() tool_manager.add_tool(Tool("project_create", project_create, "Create a new project with specified structure", {"project_name": {"type": "string", "description": "The name of the new project directory"}, "directory": {"type": "string", "description": "The path where the project should be created", "optional": True}, "file_structure": {"type": "array", "items": {"type": "string"}, "description": "List of files and directories to create (using relative paths)", "optional": True}})) tool_manager.add_tool(Tool("project_activate", project_activate, "Activate an existing project and retrieve its structure or file contents", {"existing_project_path": {"type": "string", "description": "The path to the existing project directory"}, "file_names": {"type": "array", "items": {"type": "string"}, "description": "List of file names to read content from (using relative paths)", "optional": True}, "return_structure": {"type": "boolean", "description": "If true, returns the full project structure", "optional": True}})) tool_manager.add_tool(Tool("smart_edit", smart_edit, "Edit a file or creates or new file using natural language instructions", {"file_path": {"type": "string", "description": "The relative path to the file to edit within the active project"}, "instructions": {"type": "string", "description": "Natural language instruction for the edit"}, "model_name": {"type": "string", "description": "Name of the model to use for editing", "default": "llama"}})) tool_manager.add_tool(Tool("execute_code", execute_code, "Execute Python code in a virtual environment and manage long-running processes", {"code": {"type": "string", "description": "The Python code to execute"}, "timeout": {"type": "integer", "description": "Timeout in seconds, default is 10"}})) tool_manager.add_tool(Tool("stop_process", stop_process, "Stop a running process by its ID", {"process_id": {"type": "string", "description": "The ID of the process to stop"}})) tool_manager.add_tool(Tool("tavily_search", tavily_search, "Perform a web search using the Tavily API", {"query": {"type": "string", "description": "The search query"}})) return tool_managerhere is our Tool class we may need to update for old tool references or update to make sure our current tools are properly supported:
class Tool: def __init__(self, name: str, function: Callable, description: str, parameters: Dict[str, Any]): self.name = name self.function = function self.description = description self.parameters = parameters class ToolManager: def __init__(self): self.tools: Dict[str, Tool] = {} self.logger = logging.getLogger(__name__) def add_tool(self, tool: Tool): if tool.name == "execute_code": original_function = tool.function async def wrapped_execute_code(**kwargs): timeout = kwargs.get('timeout', 10) if isinstance(timeout, str): try: timeout = int(timeout) except ValueError: timeout = 10 kwargs['timeout'] = timeout return await original_function(**kwargs) tool.function = wrapped_execute_code self.tools[tool.name] = tool def get_tool_descriptions(self, format="default") -> List[Dict[str, Any]]: if format == "claude": return [ { "name": tool.name, "description": tool.description, "input_schema": { "type": "object", "properties": tool.parameters, "required": list(tool.parameters.keys()) } } for tool in self.tools.values() ] else: return [ { "type": "function", "function": { "name": tool.name, "description": tool.description, "parameters": { "type": "object", "properties": tool.parameters, "required": list(tool.parameters.keys()) } } } for tool in self.tools.values() ] def _tool_to_dict(self, tool: Tool) -> Dict[str, Any]: return { "type": "function", "function": { "name": tool.name, "description": tool.description, "parameters": { "type": "object", "properties": tool.parameters, "required": list(tool.parameters.keys()) } } } async def execute_tool(self, tool_call: Dict[str, Any], model_name: str) -> Any: if isinstance(tool_call, dict) and 'function' in tool_call: # Handle embedded function calls (Llama format) function_name = tool_call["function"] arguments = tool_call["arguments"] elif hasattr(tool_call, 'function') and hasattr(tool_call.function, 'name'): # Handle structured tool calls (GPT/Claude format) function_name = tool_call.function.name arguments = json.loads(tool_call.function.arguments) else: raise ValueError("Unsupported tool call format") if not isinstance(arguments, dict): raise ValueError("Invalid function call format: arguments must be a JSON object") logging.info(f"Executing tool: {function_name} with arguments: {arguments}") if function_name not in self.tools: error_msg = f"Unknown tool: {function_name}" logging.error(error_msg) raise ValueError(error_msg) tool = self.tools[function_name] try: if "content" in arguments and isinstance(arguments["content"], str): arguments["content"] = arguments["content"].replace('\\n', '\n') # Handle the case where 'file_names' is passed instead of 'file_name' if function_name == 'smart_edit': if 'file_names' in arguments: if isinstance(arguments['file_names'], list): arguments['file_name'] = arguments['file_names'][0] else: arguments['file_name'] = arguments['file_names'] del arguments['file_names'] arguments['model_name'] = model_name if asyncio.iscoroutinefunction(tool.function): result = await tool.function(**arguments) else: result = tool.function(**arguments) # Format and log the result formatted_result = self.format_result(function_name, result, arguments) # logging.info(f"Tool Output ({function_name}):\n{formatted_result}") return formatted_result except Exception as e: error_msg = f"Error executing tool {function_name}: {str(e)}" logging.error(error_msg, exc_info=True) raise RuntimeError(error_msg) def format_result(self, function_name: str, result: Any, arguments: Dict[str, Any]) -> str: if function_name == "create_file": return f"File created: {arguments['file_name']}\nContent:\n{arguments['content']}" elif function_name == "smart_edit": try: result_json = json.loads(result) if result_json["status"] == "success": confidence_info = "" if 'confidence' in result_json: confidence = result_json['confidence'] confidence_info = ( f"Confidence Scores:\n" f"Overall: {confidence.get('overall', 0):.2f}\n" f"Name: {confidence.get('name', 0):.2f}\n" f"Content: {confidence.get('content', 0):.2f}\n" f"Context: {confidence.get('context', 0):.2f}\n\n" ) return ( f"Smart Edit Result:\n" f"Status: {result_json['status']}\n" f"Message: {result_json['message']}\n\n" f"Changes:\n{result_json['diff']}\n\n" f"Update Plans:\n{json.dumps(result_json['update_result'], indent=2)}" f"{confidence_info}" ) elif result_json["status"] == "cancelled": return f"Smart Edit Cancelled:\n{result_json['message']}\n\nProposed Changes:\n{result_json['diff']}" else: return f"Smart Edit Error:\n{result_json['message']}" except json.JSONDecodeError: return f"Error parsing smart_edit result: {result}" elif function_name == "execute_code": process_id, execution_result = result return f"Code executed with Process ID: {process_id}\n{execution_result}" else: return str(result)here is our smart_edit so you can see what this function returns:
async def smart_edit(*, file_path: str, instructions: str, model_name: str) -> str: """ Perform a smart edit on a file using AI-generated updates. Args: file_path (str): Relative path to the file within the active project. instructions (str): Natural language instructions for the edit. model_name (str): Name of the AI model to use for generating updates. Returns: str: JSON string containing the result of the edit operation. """ try: logging.info(f"Starting smart_edit for file: {file_path}") app_state = get_app_state() project_root = app_state.active_project full_file_path = os.path.join(project_root, file_path) if not os.path.isfile(full_file_path): return json.dumps({ "status": "error", "message": f"File not found: {file_path}" }) file_ops = FileScopeOperations(full_file_path) content = file_ops._read_file() logging.info(f"Original content:\n{content}") efc = generate_hybrid_efc(content) logging.info(f"Generated EFC: {json.dumps(efc, indent=2)}") block_context = find_best_matching_block(efc, instructions) logging.info(f"Best matching block: {json.dumps(block_context, indent=2)}") update_scope = determine_update_scope(block_context, instructions) logging.info(f"Determined update scope: {update_scope}") confidence_scores = block_context.get('confidence', {}) logging.info(f"Initial confidence scores: {json.dumps(confidence_scores, indent=2)}") logging.debug(f"Update scope: {update_scope}") if update_scope == 'file': logging.debug("Executing file-level edit") update_result = await file_ops.process_file_level_edit(instructions, model_name) elif update_scope in ['function', 'class']: logging.debug(f"Executing {update_scope}-level edit") update_result = await generate_block_update_plan(efc, instructions, content, model_name, confidence_scores) else: logging.error(f"Invalid update scope: {update_scope}") return json.dumps({"status": "error", "message": f"Invalid update scope: {update_scope}"}) logging.info(f"Update result: {json.dumps(update_result, indent=2)}") if 'error' in update_result: return json.dumps({"status": "error", "message": update_result['error']}) if 'update_plans' not in update_result: return json.dumps({"status": "error", "message": "Invalid update plan structure"}) new_content = apply_block_updates(content, update_result['update_plans'], efc) logging.info(f"New content after updates:\n{new_content}") diff = generate_smart_edit_diff(content, new_content, update_result, update_scope, confidence_scores) final_confidence_scores = update_result['update_plans'][0].get('confidence_scores', {}) logging.info(f"Final confidence scores: {json.dumps(final_confidence_scores, indent=2)}") # User approval and file writing logic try: if not await get_user_approval(diff, confidence_scores): return json.dumps({"status": "cancelled", "message": "Update cancelled by user", "diff": diff}) except EOFError: return json.dumps({"status": "error", "message": "Failed to apply the update: User input was interrupted"}) with open(full_file_path, 'w') as file: file.write(new_content) # Update the cache with the new content file_context = get_file_context() file_context.add_or_update_file(file_path, new_content, "smart_edit") app_state.update_active_project_tree() result = json.dumps({ "status": "success", "message": f"File updated successfully: {file_path}", "diff": diff, "update_result": update_result, "confidence": confidence_scores }) logging.info(f"Smart edit completed successfully: {result}") return result except Exception as e: logging.error(f"Error in smart_edit: {str(e)}", exc_info=True) error_result = json.dumps({ "status": "error", "message": f"Error during smart update: {str(e)}", "details": str(e) }) logging.info(f"Smart edit error result: {error_result}") return error_resultand finally for now this is our chat.py where you can get a sense of how things operate for the CLI tool conversation flow and where the tools are executed after they are called and executed internally.
chat.py
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 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 = app_state.conversation_history[:-1] # Exclude the latest user message # System message includes context awareness prompt file_context_summary = file_context.generate_context_awareness_prompt(app_state) logging.info(f"context awareness prompt: {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 ) 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)}" # Extract assistant response and function calls 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}") # **Process file updates and modify assistant_response accordingly** assistant_response = await process_file_updates(assistant_response, file_context, app_state) # Extract and execute function calls function_calls = ai_client.extract_function_calls(response) function_results = await execute_function_calls(function_calls, tool_manager, model_name) # Integrate function results into assistant response 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) # **Append the finalized assistant response once** app_state.conversation_history.append({"role": "assistant", "content": assistant_response}) logging.info(f"Conversation history: {app_state.conversation_history}") logging.info(f"Final assistant response: {assistant_response}") return assistant_response async def process_file_updates(assistant_response: str, file_context, app_state: Any) -> str: updated_files = detect_file_updates(assistant_response) notifications = [] for relative_path, new_content in updated_files.items(): existing_content = file_context.get_file_content(relative_path) if new_content is not None: if existing_content is not None: # Existing file: calculate diff diff = calculate_diff(existing_content, new_content) if diff.strip(): # Include acknowledgment and diff in the assistant response ack_message = f"I've updated `{relative_path}` to the context." diff_message = f"\n### Diff for {relative_path}\n```diff\n{diff}\n```" assistant_response += f"\n{ack_message}{diff_message}" else: # No changes detected no_change_msg = f"\nNo changes detected in {relative_path}." assistant_response += no_change_msg else: # First instance: notification about addition notification_message = f"\nI've added `{relative_path}` to the context." assistant_response += notification_message # Update the FileContextTracker with new content file_context.add_or_update_file(relative_path, new_content, "updated via function call") # Optionally, remove the processed content from assistant_response assistant_response = remove_full_content_from_response(assistant_response, relative_path) else: # Handle additions where no new content is provided notification_message = f"\nI've added `{relative_path}` to the context." assistant_response += notification_message # Update the FileContextTracker to reflect the addition file_context.add_or_update_file(relative_path, "", "added via function call") return assistant_response.strip() def detect_file_updates(assistant_message: str) -> Dict[str, Optional[str]]: """ Detects and extracts file updates from the assistant message. Returns a dictionary mapping relative_path to new_content or None for additions. """ updates = {} # 1. Handle additions: "Added `file1.txt` to context." pattern_added = r"Added\s+`?(?P<path>[^`]+)`?\s+to context\." for match in re.finditer(pattern_added, assistant_message): relative_path = match.group('path').strip() updates[relative_path] = None # None indicates addition without content logging.debug(f"Detected addition: {relative_path}") # 2. Handle fresh content: "Fresh content for file1.txt: ```diff\n...```" pattern_fresh = r"Fresh content for\s+`?(?P<path>[^`]+)`?:\s*```(?:diff|markdown)?\n(?P<content>[\s\S]*?)```" for match in re.finditer(pattern_fresh, assistant_message): relative_path = match.group('path').strip() content = match.group('content').strip() updates[relative_path] = content logging.debug(f"Detected fresh content for {relative_path}: {content}") # 3. Handle updates with diffs: "I've updated `file1.txt` to the context.\n### Diff for file1.txt\n```diff\n...```" pattern_update = r"I've updated\s+`?(?P<path>[^`]+)`?\s+to the context\.\n### Diff for\s+`?\1`?\s+```diff\n(?P<diff>[\s\S]*?)```" for match in re.finditer(pattern_update, assistant_message): relative_path = match.group('path').strip() diff = match.group('diff').strip() new_content = extract_new_content_from_diff(diff) if new_content: updates[relative_path] = new_content logging.debug(f"Detected update for {relative_path}: {new_content}") return updates def extract_new_content_from_diff(diff: str) -> Optional[str]: """ Parses a unified diff and extracts the new content. This function assumes that the diff follows the standard unified diff format. """ new_lines = [] for line in diff.splitlines(): if line.startswith('+') and not line.startswith('+++'): new_lines.append(line[1:]) return '\n'.join(new_lines) if new_lines else None def calculate_diff(old_content: str, new_content: str) -> str: old_lines = old_content.splitlines() new_lines = new_content.splitlines() diff = difflib.unified_diff(old_lines, new_lines, lineterm='') return '\n'.join(diff) def remove_full_content_from_response(assistant_message: str, relative_path: str) -> str: """ Removes the full file content from the assistant message, leaving only the relevant response. """ # Pattern captures only the 'Fresh content...' part pattern = rf"\s*Fresh content for {re.escape(relative_path)}:.*?```.*?\n[\s\S]*?```" cleaned_message = re.sub(pattern, "", assistant_message, flags=re.MULTILINE) # Replace multiple spaces with a single space and strip leading/trailing spaces cleaned_message = re.sub(r'\s{2,}', ' ', cleaned_message).strip() return cleaned_message async def execute_function_calls(function_calls, tool_manager: ToolManager, model_name: str): 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 and function_results: assistant_response = "I've performed the following actions based on your request:\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_responsefirst we are going to focus on making sure our Tool class is playing nice with our current tools especially (project create, project activate, smart edit) and being returned to chat properly, any deprecated tools are no longer handed then I will provide a high level sequencing issue and simplification/refactor needed in teh chat.py i discovered from live testing. Does all this make sense so far?
- Assistant
Yes, your plan is clear and well-structured. We'll start by focusing on
tools.py, ensuring that it properly incorporates the new tools (project_createandproject_activate), removes any deprecated tool references, and maintains compatibility with theToolandToolManagerclasses. Once we've addressedtools.py, we can move on to refactoringchat.pybased on your observations from live testing.Step 1: Review and Update
tools.pyCurrent State
Your current
tools.pyinitializes aToolManagerand adds several tools, including:project_createproject_activatesmart_editexecute_codestop_processtavily_search
New Changes
You've provided updated implementations for
project_createandproject_activate, as well aslist_files_for_projectand thesmart_editfunction. The goal is to ensure thattools.pyreflects these updates and removes any deprecated tools.Action Items
-
Integrate New Tools:
- Ensure
project_createandproject_activateare added with their updated implementations.
- Ensure
-
Remove Deprecated Tools:
- Identify any tools that are no longer in use or have been replaced by the new implementations.
-
Ensure Proper Formatting:
- Maintain consistent code formatting for readability and maintainability.
-
Update the
ToolClass if Necessary:- Ensure the
Toolclass accommodates any new parameters or changes in tool signatures.
- Ensure the
-
Verify Tool Descriptions and Parameters:
- Ensure that the tool descriptions and parameter schemas are accurate and up-to-date.
Updated
tools.pyBelow is an updated version of
tools.pyincorporating the new tools and ensuring deprecated tools are removed. This version assumes that there are no other deprecated tools beyond what's shown. If there are more, please adjust accordingly.# tools.py import logging from typing import Callable, Dict, Any, List, Optional, Union from tool_manager import Tool, ToolManager # Assuming Tool and ToolManager are in a separate module from your_tools_module import ( # Replace with actual module names project_create, project_activate, smart_edit, execute_code, stop_process, tavily_search ) def initialize_tool_manager() -> ToolManager: """ Initializes the ToolManager and registers all available tools. Returns: ToolManager: An instance of ToolManager with registered tools. """ tool_manager = ToolManager() # Register project_create tool tool_manager.add_tool( Tool( name="project_create", function=project_create, description="Create a new project with a specified structure.", parameters={ "project_name": { "type": "string", "description": "The name of the new project directory", }, "directory": { "type": "string", "description": "The path where the project should be created", "optional": True, }, "file_structure": { "type": "array", "items": {"type": "string"}, "description": "List of files and directories to create (using relative paths)", "optional": True, }, }, ) ) # Register project_activate tool tool_manager.add_tool( Tool( name="project_activate", function=project_activate, description="Activate an existing project and retrieve its structure or file contents.", parameters={ "existing_project_path": { "type": "string", "description": "The path to the existing project directory", }, "file_names": { "type": "array", "items": {"type": "string"}, "description": "List of file names to read content from (using relative paths)", "optional": True, }, "return_structure": { "type": "boolean", "description": "If true, returns the full project structure", "optional": True, }, }, ) ) # Register smart_edit tool tool_manager.add_tool( Tool( name="smart_edit", function=smart_edit, description="Edit a file or create a new file using natural language instructions.", parameters={ "file_path": { "type": "string", "description": "The relative path to the file to edit within the active project", }, "instructions": { "type": "string", "description": "Natural language instruction for the edit", }, "model_name": { "type": "string", "description": "Name of the model to use for editing", "default": "llama", }, }, ) ) # Register execute_code tool tool_manager.add_tool( Tool( name="execute_code", function=execute_code, description="Execute Python code in a virtual environment and manage long-running processes.", parameters={ "code": { "type": "string", "description": "The Python code to execute", }, "timeout": { "type": "integer", "description": "Timeout in seconds, default is 10", }, }, ) ) # Register stop_process tool tool_manager.add_tool( Tool( name="stop_process", function=stop_process, description="Stop a running process by its ID.", parameters={ "process_id": { "type": "string", "description": "The ID of the process to stop", }, }, ) ) # Register tavily_search tool tool_manager.add_tool( Tool( name="tavily_search", function=tavily_search, description="Perform a web search using the Tavily API.", parameters={ "query": { "type": "string", "description": "The search query", }, }, ) ) return tool_managerKey Changes and Considerations
-
Modular Imports:
- Ensure that
ToolandToolManagerare imported from their respective modules. - Replace
your_tools_modulewith the actual module names where your tool functions are defined.
- Ensure that
-
Consistent Descriptions and Parameters:
- Updated descriptions to be consistent and clear.
- Ensured that optional parameters are correctly marked.
-
Deprecated Tools:
- If there are deprecated tools not shown in your snippet, remove their registrations from
initialize_tool_manager.
- If there are deprecated tools not shown in your snippet, remove their registrations from
-
Formatting:
- Maintained consistent indentation and spacing for better readability.
-
Tool Registration:
- Each tool is added with a clear name, function reference, description, and parameter schema.
Step 2: Ensure Compatibility with the
ToolandToolManagerClassesReview of
ToolandToolManagerYour
ToolandToolManagerclasses seem well-structured. However, let's ensure they accommodate the updated tools and handle any new parameters or behaviors.Considerations
-
Asynchronous Functions:
- Ensure that
ToolManagercan handle both synchronous and asynchronous tool functions. From your implementation, it appears to handle asynchronous functions appropriately.
- Ensure that
-
Parameter Validation:
- If tools have new parameters, ensure that any validation or handling within
ToolManageraccounts for these.
- If tools have new parameters, ensure that any validation or handling within
-
Execution Wrappers:
- Your
ToolManager.add_toolmethod wraps theexecute_codefunction to handle timeouts. Ensure similar handling is added if other tools require special treatment.
- Your
-
Function Execution:
- Verify that the
execute_toolmethod correctly invokes the tool functions with the provided arguments.
- Verify that the
Updated
ToolandToolManager(if necessary)Assuming no deprecated tools remain, and the new tools follow similar patterns to existing ones, the current implementation should suffice. However, here's a refined version to ensure robustness and clarity.
# tool_manager.py import logging import json import asyncio from typing import Callable, Dict, Any, List, Optional, Union from dataclasses import dataclass, field @dataclass class Tool: name: str function: Callable description: str parameters: Dict[str, Any] class ToolManager: def __init__(self): self.tools: Dict[str, Tool] = {} self.logger = logging.getLogger(__name__) def add_tool(self, tool: Tool): """ Adds a tool to the ToolManager. Wraps certain tools if necessary. """ if tool.name == "execute_code": original_function = tool.function async def wrapped_execute_code(**kwargs): timeout = kwargs.get('timeout', 10) if isinstance(timeout, str): try: timeout = int(timeout) except ValueError: timeout = 10 kwargs['timeout'] = timeout return await original_function(**kwargs) tool.function = wrapped_execute_code self.tools[tool.name] = tool self.logger.info(f"Tool added: {tool.name}") def get_tool_descriptions(self, format: str = "default") -> List[Dict[str, Any]]: """ Retrieves descriptions of all registered tools in the specified format. """ if format == "claude": return [ { "name": tool.name, "description": tool.description, "input_schema": { "type": "object", "properties": tool.parameters, "required": list(tool.parameters.keys()) } } for tool in self.tools.values() ] else: return [ { "type": "function", "function": { "name": tool.name, "description": tool.description, "parameters": { "type": "object", "properties": tool.parameters, "required": list(tool.parameters.keys()) } } } for tool in self.tools.values() ] async def execute_tool(self, tool_call: Dict[str, Any], model_name: str) -> Any: """ Executes a tool based on the tool call dictionary and returns the result. """ if 'function' in tool_call: function_name = tool_call["function"] arguments = tool_call["arguments"] elif hasattr(tool_call, 'function') and hasattr(tool_call.function, 'name'): function_name = tool_call.function.name arguments = json.loads(tool_call.function.arguments) else: error_msg = "Unsupported tool call format" self.logger.error(error_msg) raise ValueError(error_msg) self.logger.info(f"Executing tool: {function_name} with arguments: {arguments}") if function_name not in self.tools: error_msg = f"Unknown tool: {function_name}" self.logger.error(error_msg) raise ValueError(error_msg) tool = self.tools[function_name] try: # Handle specific tool argument adjustments if necessary if function_name == 'smart_edit': if 'file_names' in arguments: if isinstance(arguments['file_names'], list): arguments['file_name'] = arguments['file_names'][0] else: arguments['file_name'] = arguments['file_names'] del arguments['file_names'] arguments['model_name'] = model_name if asyncio.iscoroutinefunction(tool.function): result = await tool.function(**arguments) else: result = tool.function(**arguments) formatted_result = self.format_result(function_name, result, arguments) return formatted_result except Exception as e: error_msg = f"Error executing tool {function_name}: {str(e)}" self.logger.error(error_msg, exc_info=True) raise RuntimeError(error_msg) def format_result(self, function_name: str, result: Any, arguments: Dict[str, Any]) -> str: """ Formats the result of a tool execution based on the tool's name. """ if function_name == "create_file": return f"File created: {arguments['file_name']}\nContent:\n{arguments['content']}" elif function_name == "smart_edit": try: result_json = json.loads(result) if result_json["status"] == "success": confidence_info = "" if 'confidence' in result_json: confidence = result_json['confidence'] confidence_info = ( f"Confidence Scores:\n" f"Overall: {confidence.get('overall', 0):.2f}\n" f"Name: {confidence.get('name', 0):.2f}\n" f"Content: {confidence.get('content', 0):.2f}\n" f"Context: {confidence.get('context', 0):.2f}\n\n" ) return ( f"Smart Edit Result:\n" f"Status: {result_json['status']}\n" f"Message: {result_json['message']}\n\n" f"Changes:\n{result_json['diff']}\n\n" f"Update Plans:\n{json.dumps(result_json['update_result'], indent=2)}" f"{confidence_info}" ) elif result_json["status"] == "cancelled": return f"Smart Edit Cancelled:\n{result_json['message']}\n\nProposed Changes:\n{result_json['diff']}" else: return f"Smart Edit Error:\n{result_json['message']}" except json.JSONDecodeError: return f"Error parsing smart_edit result: {result}" elif function_name == "execute_code": process_id, execution_result = result return f"Code executed with Process ID: {process_id}\n{execution_result}" else: return str(result)Key Enhancements
-
Use of
@dataclass:- Enhanced the
Toolclass with the@dataclassdecorator for better readability and automatic method generation.
- Enhanced the
-
Logging Enhancements:
- Added more detailed logging for tool additions and executions to aid in debugging.
-
Error Handling:
- Improved error messages for unsupported tool call formats and unknown tools.
-
Function Execution Flexibility:
- Ensured that both synchronous and asynchronous functions are handled correctly.
Step 3: Verify Tool Integration with
chat.pyNow that
tools.pyand theToolManagerare updated, it's crucial to verify that the tools are correctly integrated into thechat.pyflow. This involves ensuring that:-
Available Tools are Correctly Listed:
- When
chat_with_modellists available tools, it should reflect the updated tools.
- When
-
Function Calls are Properly Executed:
- When a tool is called (e.g.,
project_create), theToolManagershould execute it correctly and return the expected results.
- When a tool is called (e.g.,
-
Deprecations are Handled:
- Any deprecated tools should no longer be accessible or executable via the
chat.pyflow.
- Any deprecated tools should no longer be accessible or executable via the
Example Verification in
chat.pyHere's how you can ensure that the tools are properly integrated into
chat.py. I'll highlight key areas to verify and make any necessary adjustments.# chat.py import logging import asyncio import json from typing import List, Tuple, Dict, Any from tool_manager import ToolManager # Assuming ToolManager is in a separate module from your_tool_initialization_module import initialize_tool_manager # Replace with actual module from ai_client_factory import AIClientFactory # Replace with actual module from app_state import get_app_state from file_context import get_file_context from model_config import get_model_config from utils import ( # Replace with actual utility modules/functions update_token_usage, to_relative_path ) 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() 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 = app_state.conversation_history[:-1] # Exclude the latest user message # System message includes context awareness prompt file_context_summary = file_context.generate_context_awareness_prompt(app_state) logging.info(f"context awareness prompt: {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 ) 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)}" # Extract assistant response and function calls 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}") # **Process file updates and modify assistant_response accordingly** assistant_response = await process_file_updates(assistant_response, file_context, app_state) # Extract and execute function calls function_calls = ai_client.extract_function_calls(response) function_results = await execute_function_calls(function_calls, tool_manager, model_name) # Integrate function results into assistant response 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) # **Append the finalized assistant response once** app_state.conversation_history.append({"role": "assistant", "content": assistant_response}) logging.info(f"Conversation history: {app_state.conversation_history}") logging.info(f"Final assistant response: {assistant_response}") return assistant_response # ... [Rest of the chat.py remains unchanged]Verification Steps
-
Initialize
ToolManagerCorrectly:- Ensure that
initialize_tool_manageris called appropriately to register the tools beforechat_with_modelis used.
# Example initialization tool_manager = initialize_tool_manager() - Ensure that
-
Available Tools Logging:
- The log statement
logging.info(f"Available tools: {[tool['function']['name'] for tool in tool_manager.get_tool_descriptions()]}")should reflect the updated tools (project_create,project_activate, etc.).
- The log statement
-
Execute Function Calls Properly:
- When the AI client returns function calls (e.g., invoking
project_create), ensure thatexecute_toolinToolManagerhandles them correctly.
- When the AI client returns function calls (e.g., invoking
-
Deprecation Handling:
- Since deprecated tools are removed from
tools.py, attempts to call them should result in an"Unknown tool"error, which is properly logged and raised.
- Since deprecated tools are removed from
-
Response Integration:
- Ensure that the results from tool executions are properly integrated into the assistant's response.
Step 4: Testing and Validation
After updating
tools.pyand ensuring compatibility withToolandToolManager, it's crucial to perform thorough testing to validate the changes.Testing Checklist
-
Tool Registration:
- Verify that all intended tools are registered in
ToolManager. - Ensure no deprecated tools are present.
- Verify that all intended tools are registered in
-
Tool Execution:
- Test each tool individually to ensure they execute correctly.
- For example:
- Create a project using
project_create. - Activate a project using
project_activate. - Perform a smart edit using
smart_edit.
- Create a project using
-
Error Handling:
- Attempt to call a deprecated tool and confirm that an appropriate error is raised and logged.
- Provide invalid parameters to tools and ensure that errors are handled gracefully.
-
Integration with
chat.py:- Engage in conversations that trigger tool executions.
- Ensure that tool results are correctly reflected in the assistant's responses.
-
Concurrency and Asynchronous Operations:
- Since some tools are asynchronous, verify that concurrent tool executions function without issues.
-
Logging Verification:
- Check logs to ensure that all significant events (tool additions, executions, errors) are appropriately logged.
Example Test Cases
1. Create a New Project
Input:
Create a new project named "MyProject" in the current directory with the following file structure: - src/ - main.py - README.mdExpected Behavior:
project_createis invoked withproject_name="MyProject"and the specifiedfile_structure.- The project directory is created with the desired structure.
- The assistant confirms the creation and lists any skipped files if applicable.
2. Activate an Existing Project and Retrieve Structure
Input:
Activate the project located at "/path/to/MyProject" and list all files.Expected Behavior:
project_activateis invoked withexisting_project_path="/path/to/MyProject"andreturn_structure=True.- The project is activated, and its structure is retrieved.
- The assistant returns the project structure.
3. Perform a Smart Edit on a File
Input:
Edit the "README.md" file to include installation instructions.Expected Behavior:
smart_editis invoked withfile_path="README.md"and relevant instructions.- The file is updated accordingly.
- The assistant confirms the successful edit and provides a diff if applicable.
4. Attempt to Use a Deprecated Tool
Input:
Use the old tool "old_tool_name" to perform an action.Expected Behavior:
- The system recognizes that "old_tool_name" is deprecated.
- An error is raised, logged, and the assistant informs the user that the tool is unknown or deprecated.
Conclusion
By following the steps outlined above, you can ensure that your
tools.pyis up-to-date, free from deprecated references, and fully compatible with yourToolandToolManagerclasses. Thorough testing will help validate these changes and ensure smooth operation within yourchat.pyconversation flow. Once these updates are confirmed, you can proceed to address any high-level sequencing issues and further refactorchat.pyas needed.If you encounter specific issues during this process or need assistance with further refactoring, feel free to provide additional details, and I'll be happy to help!