Source code for agentscope.tool._text_file._view_text_file
# -*- coding: utf-8 -*-# flake8: noqa: E501# pylint: disable=line-too-long"""The view text file tool in agentscope."""importosfrom._write_text_fileimport_view_text_filefrom.._responseimportToolResponsefrom...exceptionimportToolInvalidArgumentsErrorfrom...messageimportTextBlock
[docs]asyncdefview_text_file(file_path:str,ranges:list[int]|None=None,)->ToolResponse:"""View the file content in the specified range with line numbers. If `ranges` is not provided, the entire file will be returned. Args: file_path (`str`): The target file path. ranges: The range of lines to be viewed (e.g. lines 1 to 100: [1, 100]), inclusive. If not provided, the entire file will be returned. To view the last 100 lines, use [-100, -1]. Returns: `ToolResponse`: The tool response containing the file content or an error message. """ifnotos.path.exists(file_path):returnToolResponse(content=[TextBlock(type="text",text=f"Error: The file {file_path} does not exist.",),],)ifnotos.path.isfile(file_path):returnToolResponse(content=[TextBlock(type="text",text=f"Error: The path {file_path} is not a file.",),],)try:content=_view_text_file(file_path,ranges)exceptToolInvalidArgumentsErrorase:returnToolResponse(content=[TextBlock(type="text",text=e.message,),],)ifrangesisNone:returnToolResponse(content=[TextBlock(type="text",text=f"""The content of {file_path}:```{content}```""",),],)else:returnToolResponse(content=[TextBlock(type="text",text=f"""The content of {file_path} in {ranges} lines:```{content}```""",),],)