Module: DeepAgentsRb::Tools
- Defined in:
- lib/deepagents/deepagentsrb/tools.rb
Defined Under Namespace
Classes: Command, Tool, ToolMessage
Constant Summary collapse
- WRITE_TODOS_DESCRIPTION =
Tool descriptions
"Use this tool to manage and track tasks. It helps you plan and break down complex tasks."- EDIT_DESCRIPTION =
"Edit a file by replacing specific content."- TOOL_DESCRIPTION =
"Read a file from the virtual file system."
Class Method Summary collapse
-
.built_in_tools ⇒ Object
Get all built-in tools.
-
.edit_file ⇒ Object
Define the edit_file tool.
-
.ls ⇒ Object
Define the ls tool.
-
.read_file ⇒ Object
Define the read_file tool.
-
.write_file ⇒ Object
Define the write_file tool.
-
.write_todos ⇒ Object
Define the write_todos tool.
Class Method Details
.built_in_tools ⇒ Object
Get all built-in tools
195 196 197 198 199 200 201 202 203 |
# File 'lib/deepagents/deepagentsrb/tools.rb', line 195 def self.built_in_tools [ write_todos, ls, read_file, write_file, edit_file ] end |
.edit_file ⇒ Object
Define the edit_file tool
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/deepagents/deepagentsrb/tools.rb', line 144 def self.edit_file Tool.new("edit_file", EDIT_DESCRIPTION) do |file_path, old_string, new_string, state, tool_call_id:, replace_all: false| mock_filesystem = state.get("files", {}) # Check if file exists in mock filesystem if !mock_filesystem.key?(file_path) raise FileNotFoundError.new(file_path) return "Error: File '#{file_path}' not found" end # Get current file content content = mock_filesystem[file_path] # Check if old_string exists in the file if !content.include?(old_string) return "Error: String not found in file: '#{old_string}'" end # If not replace_all, check for uniqueness if !replace_all occurrences = content.scan(old_string).length if occurrences > 1 return "Error: String '#{old_string}' appears #{occurrences} times in file. Use replace_all=true to replace all instances, or provide a more specific string with surrounding context." elsif occurrences == 0 return "Error: String not found in file: '#{old_string}'" end end # Perform the replacement if replace_all new_content = content.gsub(old_string, new_string) replacement_count = content.scan(old_string).length result_msg = "Successfully replaced #{replacement_count} instance(s) of the string in '#{file_path}'" else new_content = content.sub(old_string, new_string) # Replace only first occurrence result_msg = "Successfully replaced string in '#{file_path}'" end # Update the mock filesystem mock_filesystem[file_path] = new_content Command.new( update: { files: mock_filesystem, messages: [ToolMessage.new(result_msg, tool_call_id: tool_call_id)] } ) end end |
.ls ⇒ Object
Define the ls tool
72 73 74 75 76 |
# File 'lib/deepagents/deepagentsrb/tools.rb', line 72 def self.ls Tool.new("ls", "List all files") do |state| state.get("files", {}).keys end end |
.read_file ⇒ Object
Define the read_file tool
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/deepagents/deepagentsrb/tools.rb', line 79 def self.read_file Tool.new("read_file", TOOL_DESCRIPTION) do |file_path, state, offset: 0, limit: 2000| mock_filesystem = state.get("files", {}) if !mock_filesystem.key?(file_path) raise FileNotFoundError.new(file_path) end # Get file content content = mock_filesystem[file_path] # Handle empty file if content.nil? || content.strip.empty? return "System reminder: File exists but has empty contents" end # Split content into lines lines = content.split("\n") # Apply line offset and limit start_idx = offset end_idx = [start_idx + limit, lines.length].min # Handle case where offset is beyond file length if start_idx >= lines.length return "Error: Line offset #{offset} exceeds file length (#{lines.length} lines)" end # Format output with line numbers (cat -n format) result_lines = [] (start_idx...end_idx).each do |i| line_content = lines[i] # Truncate lines longer than 2000 characters if line_content.length > 2000 line_content = line_content[0...2000] end # Line numbers start at 1, so add 1 to the index line_number = i + 1 result_lines << "#{line_number.to_s.rjust(6)}\t#{line_content}" end result_lines.join("\n") end end |
.write_file ⇒ Object
Define the write_file tool
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/deepagents/deepagentsrb/tools.rb', line 127 def self.write_file Tool.new("write_file", "Write to a file") do |file_path, content, state, tool_call_id:| files = state.get("files", {}) files[file_path] = content Command.new( update: { files: files, messages: [ ToolMessage.new("Updated file #{file_path}", tool_call_id: tool_call_id) ] } ) end end |
.write_todos ⇒ Object
Define the write_todos tool
58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/deepagents/deepagentsrb/tools.rb', line 58 def self.write_todos Tool.new("write_todos", WRITE_TODOS_DESCRIPTION) do |todos, tool_call_id:| Command.new( update: { todos: todos, messages: [ ToolMessage.new("Updated todo list to #{todos}", tool_call_id: tool_call_id) ] } ) end end |