mirror of
https://github.com/agentscope-ai/ReMe.git
synced 2026-08-28 05:25:04 +00:00
refactor(read_file_op): replace subprocess with asyncio for sed command execution
This commit is contained in:
parent
8791de6d6f
commit
a6ba93ccf3
1 changed files with 10 additions and 3 deletions
|
|
@ -4,7 +4,7 @@ This module provides a tool operation for reading file contents.
|
|||
It supports reading entire files or specific line ranges for large files.
|
||||
"""
|
||||
|
||||
import subprocess
|
||||
import asyncio
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
|
||||
|
|
@ -76,8 +76,15 @@ class ReadFileOp(BaseAsyncToolOp):
|
|||
end_line = offset + limit
|
||||
|
||||
cmd = ["sed", "-n", f"{start_line},{end_line}p", str(file_path_obj)]
|
||||
result = subprocess.run(cmd, capture_output=True, text=True, timeout=30, check=True)
|
||||
content = result.stdout.rstrip("\n")
|
||||
process = await asyncio.create_subprocess_exec(
|
||||
*cmd,
|
||||
stdout=asyncio.subprocess.PIPE,
|
||||
stderr=asyncio.subprocess.PIPE,
|
||||
)
|
||||
stdout, stderr = await asyncio.wait_for(process.communicate(), timeout=30)
|
||||
|
||||
assert process.returncode == 0, f"sed command failed: {stderr.decode()}"
|
||||
content = stdout.decode().rstrip("\n")
|
||||
self.set_output(content)
|
||||
|
||||
async def async_default_execute(self, e: Exception = None, **_kwargs):
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue