ExecutionModule¶
- class oscopilot.modules.executor.friday_executor.FridayExecutor(prompt, tool_manager, max_iter=3)[source]¶
Bases:
BaseModuleA modules within the system responsible for executing tools based on prompts and maintaining the tool library.
The ExecutionModule extends the BaseAgent class, focusing on the practical execution of tools determined by the system. It utilizes a language learning model (LLM) in conjunction with an execution environments and an tool library to carry out tools. Additionally, it manages system versioning and prompts initialization for tool execution guidance.
- analysis_tool(code, task_description, state)[source]¶
Analyzes the execution outcome of an tool to determine the nature of any errors.
This method evaluates the execution state of an tool, specifically looking for errors. Based on the analysis, it determines whether the error is environmental and requires new operations (handled by the planning modules) or is amendable via the repair_tool method. The analysis results, including the reasoning and error type, are returned in JSON format.
- Parameters:
code (str) – The code that was executed for the tool.
task_description (str) – The description of the task associated with the tool.
state – The state object containing the result of the tool’s execution, including any errors.
- Returns:
- A tuple containing:
reasoning (str): The analysis’s reasoning regarding the nature of the error.
type (str): The type of error identified (‘environmental’ for new operations, ‘amendable’ for corrections).
- Return type:
tuple
- api_tool(description, api_path, context='No context provided.')[source]¶
Executes a task by calling an API tool with the provided description and context.
This method formats a message to generate executable code for an API call based on the provided description and context. It sends this message to the language learning model (LLM), extracts the executable Python code from the LLM’s response, and returns this code.
- Parameters:
description (str) – A description of the task to be performed by the API call.
api_path (str) – The path or endpoint of the API to be called.
context (str, optional) – Additional context to be included in the API call. Defaults to “No context provided.”.
- Returns:
The generated Python code to execute the API call.
- Return type:
str
- execute_tool(code, invoke, node_type)[source]¶
Executes a given tool code and returns the execution state.
This method handles the execution of tool code based on its node_type. For code tools, it appends additional instructions to print the execution result within designated markers. It then passes the modified code for execution in the environments. The method captures and prints the execution state, including any results or errors, and returns this state.
- Parameters:
code (str) – The Python code to be executed as part of the tool.
invoke (str) – The specific command or function call that triggers the tool within the code.
node_type (str) – The type of the tool, determining how the tool is executed. Currently supports ‘Code’ type.
- Returns:
- The state object returned by the environments after executing the tool. This object contains
details about the execution’s outcome, including any results or errors.
- Return type:
state
Note
The execution logic is currently tailored for tools of type ‘Code’, where the code is directly executable Python code. The method is designed to be extensible for other tool types as needed.
- extract_API_Path(text)[source]¶
Extracts both UNIX-style and Windows-style file paths from the provided text string.
This method applies regular expressions to identify and extract file paths that may be present in the input text. It is capable of recognizing paths that are enclosed within single or double quotes and supports both UNIX-style paths (e.g., /home/user/docs) and Windows-style paths (e.g., C:Usersuserdocs). If multiple paths are found, only the first match is returned, following the function’s current implementation.
- Parameters:
text (str) – The string from which file paths are to be extracted.
- Returns:
- The first file path found in the input text, with any enclosing quotes removed. If no paths are
found, an empty string is returned.
- Return type:
str
Note
The current implementation returns only the first extracted path. If multiple paths are present in the input text, consider modifying the method to return all found paths if the use case requires it.
- extract_args_description(class_code)[source]¶
Extracts the arguments description from the __call__ method’s docstring within Python class code.
This method specifically targets the docstring of the __call__ method in a class, which is conventionally used to describe the method’s parameters. The extraction is performed using a regular expression that captures the content of the docstring.
- Parameters:
class_code (str) – The Python code of the class from which the arguments description is to be extracted.
- Returns:
The extracted arguments description from the __call__ method’s docstring, or None if the docstring is not found or does not contain descriptions.
- Return type:
str
- extract_class_name_and_args_description(class_code)[source]¶
Extracts the class name and arguments description from a given Python class code.
This method searches the provided class code for the class name and the documentation string of the __call__ method, which typically includes descriptions of the arguments. It uses regular expressions to locate these elements within the code.
- Parameters:
class_code (str) – The Python code of the class from which information is to be extracted.
- Returns:
- A tuple containing:
class_name (str): The name of the class extracted from the code.
args_description (str): The arguments description extracted from the __call__ method’s docstring, if available; otherwise, None.
- Return type:
tuple
- extract_python_code(response)[source]¶
Extracts Python code snippets from a response string that includes code block markers.
This method parses a response string to extract Python code enclosed within ‘
`python' and '`’ markers. It’s designed to retrieve executable Python code snippets from formatted responses, such as those returned by a language learning model after processing a code generation or analysis prompts.- Parameters:
response (str) – The response string containing the Python code block to be extracted.
- Returns:
The extracted Python code snippet, or an empty string if no code block is found.
- Return type:
str
- extract_tool_description(class_code)[source]¶
Extracts the description of an tool from the class’s initialization method in Python code.
This method looks for the tool’s description assigned to self._description within the __init__ method of a class. It uses regular expressions to find this assignment and extracts the description string. This approach assumes that the tool’s description is directly assigned as a string literal to self._description.
- Parameters:
class_code (str) – The complete Python code of the class from which the tool description is to be extracted.
- Returns:
The extracted description of the tool if found; otherwise, None.
- Return type:
str
- generate_openapi_doc(tool_api_path)[source]¶
Generates a reduced OpenAPI documentation for a specific API path from the full OpenAPI documentation.
This method isolates and extracts the documentation for a specific tool API path, including its schemas and operations (GET, POST), from the entire OpenAPI documentation stored in the instance. It constructs a new, smaller OpenAPI document that only includes details relevant to the specified API path. If the API path does not exist in the full documentation, it returns an error message.
- Parameters:
tool_api_path (str) – The specific API path for which the OpenAPI documentation should be generated.
- Returns:
- A dictionary representing the OpenAPI documentation for the specific API path. If the path is not
found, returns a dictionary with an error message.
- Return type:
dict
The method performs several checks: - Verifies the existence of the tool API path in the full OpenAPI documentation. - Extracts relevant parts of the OpenAPI schema related to the path. - Includes any referenced schemas necessary for understanding the API’s structure and data types.
It handles both JSON and multipart/form-data content types in API request bodies, searching for schema references to include in the returned documentation. This enables the resulting API document to be self-contained with respect to the schemas needed to understand the API’s usage.
- generate_tool(task_name, task_description, tool_type, pre_tasks_info, relevant_code)[source]¶
Generates executable code and invocation logic for a specified tool.
This method constructs a message to generate tool code capable of completing the specified task, taking into account any prerequisite task information and relevant code snippets. It then formats this message for processing by the language learning model (LLM) to generate the tool code. The method extracts the executable Python code and the specific invocation logic from the LLM’s response.
- Parameters:
task_name (str) – The name of the task for which tool code is being generated.
task_description (str) – A description of the task, detailing what the tool aims to accomplish.
tool_type (str) – The type of tool being generated, such as ‘Python’, ‘Shell’, or ‘AppleScript’.
pre_tasks_info (dict) – Information about tasks that are prerequisites for the current task, including their descriptions and return values.
relevant_code (dict) – A dictionary of code snippets relevant to the current task, possibly including code from prerequisite tasks.
- Returns:
- A tuple containing two elements:
code (str): The generated Python code for the tool.
invoke (str): The specific logic or command to invoke the generated tool.
- Return type:
tuple
- judge_tool(code, task_description, state, next_action)[source]¶
Evaluates the outcome of an executed tool to determine its success in completing a task.
This method formulates and sends a judgment request to the language learning model (LLM) based on the executed tool’s code, the task description, the execution state, and the expected next tool. It then parses the LLM’s response to determine the tool’s success, providing reasoning, a judgment (boolean), and a score that quantifies the tool’s effectiveness.
- Parameters:
code (str) – The code of the tool that was executed.
task_description (str) – The description of the task the tool was intended to complete.
state – The state object returned by the environments after executing the tool, containing execution results.
next_action (str) – The name of the next expected tool in the sequence.
- Returns:
- A tuple containing:
reasoning (str): The LLM’s reasoning behind the judgment.
judge (bool): The LLM’s judgment on whether the tool successfully completed the task.
score (float): A score representing the effectiveness of the tool.
- Return type:
tuple
- repair_tool(current_code, task_description, tool_type, state, critique, pre_tasks_info)[source]¶
Modifies or corrects the code of an tool based on feedback to better complete a task.
This method sends an amendment request to the LLM, including details about the current code, task description, execution state, critique of the tool’s outcome, and information about prerequisite tasks. It aims to generate a revised version of the code that addresses any identified issues or incomplete aspects of the task. The method extracts and returns both the amended code and the specific logic or command to invoke the amended tool.
- Parameters:
current_code (str) – The original code of the tool that requires amendment.
task_description (str) – The description of the task the tool is intended to complete.
tool_type (str) – The type of tool being amended, such as ‘Python’, ‘Shell’, or ‘AppleScript’.
state – The state object containing details about the tool’s execution outcome.
critique (str) – Feedback or critique on the tool’s execution, used to guide the amendment.
pre_tasks_info (dict) – Information about tasks that are prerequisites for the current task.
- Returns:
- A tuple containing:
new_code (str): The amended code for the tool.
invoke (str): The command or logic to invoke the amended tool.
- Return type:
tuple
- save_str_to_path(content, path)[source]¶
Saves a string content to a file at the specified path, ensuring the directory exists.
This method takes a string and a file path, creating any necessary parent directories before writing the content to the file. It ensures that the content is written with proper encoding and that any existing content in the file is overwritten. The content is processed to remove extra whitespace at the beginning and end of each line before saving.
- Parameters:
content (str) – The string content to be saved to the file.
path (str) – The filesystem path where the content should be saved. If the directory does not exist, it will be created.
- Side Effects:
Creates the directory path if it does not exist.
Writes the content to a file at the specified path, potentially overwriting existing content.
- save_tool_info_to_json(tool, code, description)[source]¶
Constructs a dictionary containing tool information suitable for JSON serialization.
This method packages the name, code, and description of an tool into a dictionary, making it ready for serialization or further processing. This structured format is useful for saving tool details in a consistent manner, facilitating easy storage and retrieval.
- Parameters:
tool (str) – The name of the tool.
code (str) – The executable code associated with the tool.
description (str) – A textual description of what the tool does.
- Returns:
A dictionary containing the tool’s name, code, and description.
- Return type:
dict
- store_tool(tool, code)[source]¶
Stores the provided tool and its code in the tool library.
If the specified tool does not already exist in the tool library, this method proceeds to store the tool’s code, arguments description, and other relevant information. It involves saving these details into JSON files and updating the tool library database. If the tool already exists, it outputs a notification indicating so.
- Parameters:
tool (str) – The name of the tool to be stored.
code (str) – The executable code associated with the tool.
- Side Effects:
Adds a new tool to the tool library if it doesn’t already exist.
Saves tool details to the filesystem and updates the tool library’s database.
Outputs a message if the tool already exists in the library.