From fa7e932f080a15d500b42e0ea626a3f15434f8b7 Mon Sep 17 00:00:00 2001 From: marko-kraemer Date: Sat, 2 Nov 2024 19:09:39 +0100 Subject: [PATCH] wip --- core/examples/example-agent/agent.py | 28 +++------ .../example-agent/tools/files_tool.py | 59 ++----------------- 2 files changed, 11 insertions(+), 76 deletions(-) diff --git a/core/examples/example-agent/agent.py b/core/examples/example-agent/agent.py index 8cfb64de..85a5b450 100644 --- a/core/examples/example-agent/agent.py +++ b/core/examples/example-agent/agent.py @@ -51,15 +51,8 @@ You are a world-class web developer who can create, edit, and delete files, and RULES: - All current file contents are available to you in the section -- Each file in the workspace state includes: - * content: The full file contents - * line_count: Total number of lines in the file - * lines: Array of line objects containing: - - number: Line number (1-based) - - content: The line's content - - length: Length of the line +- Each file in the workspace state includes its full content - Use str_replace for precise replacements in files -- Use insert_lines to add new content at specific line numbers (use the line numbers from the workspace state) - NEVER include comments in any code you write - the code should be self-documenting - Always maintain the full context of files when making changes - When creating new files, write clean code without any comments or documentation @@ -68,7 +61,6 @@ RULES: [create_file(file_path, file_contents)] - Create new files [delete_file(file_path)] - Delete existing files [str_replace(file_path, old_str, new_str)] - Replace specific text in files -[insert_lines(file_path, insert_line, new_content)] - Insert content at specific line number [execute_command(command)] - Execute terminal commands @@ -82,21 +74,15 @@ ALWAYS RESPOND WITH MULTIPLE SIMULTANEOUS ACTIONS: EDITING GUIDELINES: -1. Review the current file contents and line information in the workspace state -2. Use line numbers from the workspace state for precise insertions -3. Make targeted changes with str_replace or insert_lines -4. Write clean, self-documenting code without comments +1. Review the current file contents in the workspace state +2. Make targeted changes with str_replace +3. Write clean, self-documenting code without comments +4. Use create_file for new files and str_replace for modifications Example workspace state for a file: { "index.html": { - "content": "\\n\\n...", - "line_count": 15, - "lines": [ - {"number": 1, "content": "", "length": 15}, - {"number": 2, "content": "", "length": 6}, - ... - ] + "content": "\\n\\n..." } } @@ -148,7 +134,7 @@ if __name__ == "__main__": thread_id, { "role": "user", - "content": "Let's create a marketing website for my AI Agent 'Jarvis' using HTML, CSS, Javascript. Use images from pixabay, pexels, and co. Style it cyberpunk style. Make it like Ironmen Jarvis." + "content": "Let's create a identical 1to1 Airbnb Clone using HTML, CSS, Javascript. Use images from pixabay, pexels, and co." } ) diff --git a/core/examples/example-agent/tools/files_tool.py b/core/examples/example-agent/tools/files_tool.py index 5281a671..2bffd657 100644 --- a/core/examples/example-agent/tools/files_tool.py +++ b/core/examples/example-agent/tools/files_tool.py @@ -1,6 +1,9 @@ import os import asyncio +from datetime import datetime from pathlib import Path +from collections import defaultdict +from typing import Optional, List from agentpress.tool import Tool, ToolResult, tool_schema from agentpress.state_manager import StateManager @@ -84,20 +87,8 @@ class FilesTool(Tool): try: with open(full_path, 'r') as f: content = f.read() - lines = content.split('\n') - - # Create file state object with content and line information files_state[rel_path] = { - "content": content, - "line_count": len(lines), - "lines": [ - { - "number": i + 1, - "content": line, - "length": len(line) - } - for i, line in enumerate(lines) - ] + "content": content } except Exception as e: print(f"Error reading file {rel_path}: {e}") @@ -203,48 +194,6 @@ class FilesTool(Tool): except Exception as e: return self.fail_response(f"Error replacing string: {str(e)}") - @tool_schema({ - "name": "insert_lines", - "description": "Insert lines at a specific line number in a file", - "parameters": { - "type": "object", - "properties": { - "file_path": {"type": "string", "description": "Path to the file"}, - "insert_line": {"type": "integer", "description": "Line number to insert at"}, - "new_content": {"type": "string", "description": "Content to insert"} - }, - "required": ["file_path", "insert_line", "new_content"] - } - }) - async def insert_lines(self, file_path: str, insert_line: int, new_content: str) -> ToolResult: - try: - full_path = Path(os.path.join(self.workspace, file_path)) - if not full_path.exists(): - return self.fail_response(f"File '{file_path}' does not exist") - - content = full_path.read_text().expandtabs() - lines = content.split('\n') - - if insert_line < 0 or insert_line > len(lines): - return self.fail_response(f"Invalid line number {insert_line}") - - # Insert new content - new_lines = new_content.expandtabs().split('\n') - updated_lines = lines[:insert_line] + new_lines + lines[insert_line:] - new_content = '\n'.join(updated_lines) - - full_path.write_text(new_content) - - # Show snippet around the edit - start_line = max(0, insert_line - self.SNIPPET_LINES) - end_line = insert_line + len(new_lines) + self.SNIPPET_LINES - snippet = '\n'.join(updated_lines[start_line:end_line]) - - return self.success_response(f"Lines inserted successfully. Snippet of changes:\n{snippet}") - - except Exception as e: - return self.fail_response(f"Error inserting lines: {str(e)}") - if __name__ == "__main__": async def test_files_tool():