💡 All-in-one AI framework for semantic search, LLM orchestration and language model workflows
Python agentsaiai-agentsembeddingsinformation-retrievallanguage-modellarge-language-modelsllmnlppythonragretrieval-augmented-generationsearchsearch-enginesemantic-searchsentence-embeddingstransformerstxtaivector-databasevector-search
Preserve line endings when editing files (#1426)
amengnew5baa6ef0c3d9
diff --git a/src/python/txtai/agent/tool/edit.py b/src/python/txtai/agent/tool/edit.pyindex 1acd874c..14855512 100644--- a/src/python/txtai/agent/tool/edit.py+++ b/src/python/txtai/agent/tool/edit.py@@ -46,13 +46,13 @@ class EditTool(Tool):"""content = None- with open(path, "r", encoding="utf-8") as f:+ with open(path, "r", encoding="utf-8", newline="") as f:content = f.read()# Make editsmodified = content.replace(search, replace)if modified != content:- with open(path, "w", encoding="utf-8") as f:+ with open(path, "w", encoding="utf-8", newline="") as f:f.write(modified)# Return diff of file editsdiff --git a/test/python/testagent.py b/test/python/testagent.pyindex e5b1a024..3afee7f2 100644--- a/test/python/testagent.py+++ b/test/python/testagent.py@@ -232,6 +232,21 @@ class TestAgent(unittest.TestCase):# Timeout is disabled when set to Noneself.assertIsNone(BashTool(timeout=None).timeout)+ def testToolsEditCRLF(self):+ """+ Test the edit tool preserves CRLF line endings+ """++ # Test file with CRLF line endings+ path = os.path.join(tempfile.gettempdir(), "agent_tools_crlf.txt")+ with open(path, "wb") as f:+ f.write(b"hello\r\nworld\r\n")++ # Editing a word must not rewrite the line endings of untouched lines+ ToolFactory.default("edit")(path, "hello", "bye")+ with open(path, "rb") as f:+ self.assertEqual(f.read(), b"bye\r\nworld\r\n")+def testToolsBashAllowedEmpty(self):"""Test an empty allowed commands list permits no commands