Skip to content

armgit / txtai

Public Imported from GitHub
0

💡 All-in-one AI framework for semantic search, LLM orchestration and language model workflows

Decode bash tool output as UTF-8 instead of locale encoding, closes #1326 (#1327)

Manobhi Sriramd9b4476187bc
diff --git a/src/python/txtai/agent/tool/bash.py b/src/python/txtai/agent/tool/bash.py
index 30acdc52..c000a19c 100644
--- a/src/python/txtai/agent/tool/bash.py
+++ b/src/python/txtai/agent/tool/bash.py
@@ -58,7 +58,9 @@ class BashTool(Tool):
output = None
if command and command[0] in self.allowed:
try:
- output = subprocess.run(command, capture_output=True, text=True, check=False, stdin=subprocess.DEVNULL, timeout=self.timeout).stdout
+ output = subprocess.run(
+ command, capture_output=True, text=True, encoding="utf-8", check=False, stdin=subprocess.DEVNULL, timeout=self.timeout
+ ).stdout
except subprocess.TimeoutExpired:
output = f"Command timed out after {self.timeout} seconds"
diff --git a/test/python/testagent.py b/test/python/testagent.py
index 986c08a9..107ca317 100644
--- a/test/python/testagent.py
+++ b/test/python/testagent.py
@@ -232,6 +232,20 @@ class TestAgent(unittest.TestCase):
# Timeout is disabled when set to None
self.assertIsNone(BashTool(timeout=None).timeout)
+ @patch("subprocess.run")
+ def testToolsBashEncoding(self, run):
+ """
+ Test bash tool decodes subprocess output as UTF-8 instead of the locale encoding
+ """
+
+ run.return_value.stdout = "output"
+
+ tool = BashTool(allowed=["cat"])
+ tool(["cat", "file.txt"])
+
+ # Subprocess output must be decoded as UTF-8, not left to the locale's default encoding
+ self.assertEqual(run.call_args.kwargs.get("encoding"), "utf-8")
+
def testToolsEmbeddings(self):
"""
Test adding Embeddings as a tool