Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: restructure, add types in local-tools #83

Merged
merged 13 commits into from
Jun 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
531 changes: 239 additions & 292 deletions composio/client/enums.py

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions composio/client/local_handler.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from composio.local_tools.local_workspace.cmd_manager.cmd_manager_tool import (
from composio.local_tools.local_workspace.cmd_manager.tool import (
CmdManagerTool,
)
from composio.local_tools.local_workspace.commons.history_processor import (
Expand All @@ -7,12 +7,12 @@
from composio.local_tools.local_workspace.commons.local_docker_workspace import (
WorkspaceManagerFactory,
)
from composio.local_tools.local_workspace.history_keeper.history_keeper_tool import (
from composio.local_tools.local_workspace.history_keeper import (
HistoryKeeper,
)
from composio.local_tools.local_workspace.workspace.workspace_tool import LocalWorkspace
from composio.local_tools.local_workspace.workspace import LocalWorkspace

from composio.local_tools.ragtool import RagToolActions
from composio.local_tools.ragtool import RagTool
from composio.local_tools import Mathematical
from composio.local_tools.webtool import WebTool
from composio.local_tools.greptile.tool import Greptile
Expand Down Expand Up @@ -43,7 +43,7 @@ def register_local_tools(self):
workspace_tool,
cmd_manager_tool,
h_keeper_tool,
RagToolActions(),
RagTool(),
WebTool(),
Greptile(),
]
Expand Down
2 changes: 2 additions & 0 deletions composio/core/local/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from .action import Action
from .tool import Tool
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import hashlib
import json
from abc import ABC, abstractmethod
from typing import List

import inflection
import jsonref
from pydantic import BaseModel

from abc import ABC, abstractmethod
from typing import List
from pydantic import BaseModel

def generate_hashed_appId(input_string):
# Generate a 32-character hash using MD5
Expand All @@ -17,7 +16,6 @@ def generate_hashed_appId(input_string):

return formatted_hash


class Action(ABC):
_history_maintains: bool = False
_display_name: str = "" # Add an internal variable to hold the display name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

from .action import Action


class Tool:
@property
def tool_name(self) -> str:
Expand Down
1 change: 0 additions & 1 deletion composio/local_tools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@

from .mathematical import Mathematical


TOOLS_PATH = Path(__file__).parent
1 change: 1 addition & 0 deletions composio/local_tools/file/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""
File manager.
"""
from .tool import FileTool
2 changes: 2 additions & 0 deletions composio/local_tools/file/actions/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from .read_file import ReadFile
from .write_file import WriteFile
50 changes: 50 additions & 0 deletions composio/local_tools/file/actions/read_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import typing as t
from pathlib import Path
from pydantic import BaseModel, Field
from composio.core.local import Action

class ReadFileRequest(BaseModel):
"""Read file request schema."""

base_dir: str = Field(
default=".",
description="Directory where the file will be saved.",
)
filename: str = Field(
...,
description="File name to be saved.",
)

class ReadFileResponse(BaseModel):
"""Read file response schema."""

contents: str = Field(
...,
description="Content read from the file.",
)

class ReadFile(Action):
"""Read file tool."""

_display_name = "Read file"
_description = "Read file from a file."
_request = ReadFileRequest
_response = ReadFileResponse
_tags = ["file", "read"]
_tool_name = "file"


def execute(self, request: ReadFileRequest) -> ReadFileResponse:
"""
Reads the contents of the file `file_name` and returns the contents
if successful.
"""
try:
return ReadFileResponse(
contents=Path(request.base_dir, request.filename).read_text(
encoding="utf-8"
)
)
except Exception as e: # pylint: disable=broad-exception-caught
return ReadFileResponse(contents=f"Error reading file: {e}")

58 changes: 58 additions & 0 deletions composio/local_tools/file/actions/write_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import typing as t
from pathlib import Path
from pydantic import BaseModel, Field
from composio.core.local import Action

class WriteFileRequest(BaseModel):
"""Safe file request schema."""

base_dir: str = Field(
default=".",
description="Directory where the file will be saved.",
)
contents: str = Field(
...,
description="Content to write in the file.",
)
filename: str = Field(
...,
description="File name to be saved.",
)
overwrite: bool = Field(
...,
description="Whether to overwrite a file if it exists or not.",
)

class WriteFileResponse(BaseModel):
"""Save file response schema."""

filename: str = Field(
...,
description="Path of the saved file.",
)

class WriteFile(Action):
"""Write file tool."""

_display_name = "Write file"
_description = "Write file to a file."
_request = WriteFileRequest
_response = WriteFileResponse
_tags = ["file", "write"]
_tool_name = "file"

def execute(self, request: WriteFileRequest) -> WriteFileResponse:
"""
Saves the contents to a file called `file_name` and returns the
file name if successful.
"""
try:
file = Path(request.base_dir, request.filename)
if not file.parent.exists():
file.parent.mkdir(parents=True)
if file.exists() and not request.overwrite:
utkarsh-dixit marked this conversation as resolved.
Show resolved Hide resolved
return WriteFileResponse(filename=f"File {file} already exists")
file.write_text(request.contents, encoding="utf-8")
return WriteFileResponse(filename=str(file))
except Exception as e: # pylint: disable=broad-exception-caught
return WriteFileResponse(filename=f"Error saving to file: {e}")
103 changes: 2 additions & 101 deletions composio/local_tools/file/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,108 +3,9 @@
"""

import typing as t
from pathlib import Path

from pydantic import BaseModel, Field


class Action:
"""Local action abstraction."""


class Tool:
"""Local tool abstraction."""


class WriteFileRequest(BaseModel):
"""Safe file request schema."""

base_dir: str = Field(
default=".",
description="Directory where the file will be saved.",
)
contents: str = Field(
...,
description="Content to write in the file.",
)
filename: str = Field(
...,
description="File name to be saved.",
)
overwrite: bool = Field(
...,
description="Whether to overwrite a file if it exists or not.",
)


class WriteFileResponse(BaseModel):
"""Save file response schema."""

filename: str = Field(
...,
description="Path of the saved file.",
)


class ReadFileRequest(BaseModel):
"""Read file request schema."""

base_dir: str = Field(
default=".",
description="Directory where the file will be saved.",
)
filename: str = Field(
...,
description="File name to be saved.",
)


class ReadFileResponse(BaseModel):
"""Read file response schema."""

contents: str = Field(
...,
description="Content read from the file.",
)


class ReadFile(Action):
"""Read file tool."""

def read_file(self, request: ReadFileRequest) -> ReadFileResponse:
"""
Reads the contents of the file `file_name` and returns the contents
if successful.
"""
try:
return ReadFileResponse(
contents=Path(request.base_dir, request.filename).read_text(
encoding="utf-8"
)
)
except Exception as e: # pylint: disable=broad-exception-caught
return ReadFileResponse(contents=f"Error reading file: {e}")


class WriteFile(Action):
"""Write file tool."""

def execute(self, request: WriteFileRequest) -> WriteFileResponse:
"""
Saves the contents to a file called `file_name` and returns the
file name if successful.
"""
try:
file = Path(request.base_dir, request.filename)
if not file.parent.exists():
file.parent.mkdir(parents=True)
if file.exists() and not request.overwrite:
return WriteFileResponse(filename=f"File {file} already exists")
file.write_text(request.contents, encoding="utf-8")
return WriteFileResponse(filename=str(file))
except Exception as e: # pylint: disable=broad-exception-caught
return WriteFileResponse(filename=f"Error saving to file: {e}")

from .actions import ReadFile, WriteFile
from composio.core.local import Tool, Action

class FileTool(Tool):
"""File I/O tool."""
Expand Down
1 change: 0 additions & 1 deletion composio/local_tools/greptile/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
from .codequery import CodeQuery
from .tool import Greptile
1 change: 1 addition & 0 deletions composio/local_tools/greptile/actions/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .codequery import CodeQuery
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import json
import os

import requests
from pydantic import BaseModel, Field

from ..action import Action
from composio.core.local import Action
from composio.local_tools.local_workspace.commons.get_logger import get_logger


# Greptile query tool. Given the github repo and the question, it will return the answer to the question.
logger = get_logger()


class message(BaseModel):
id: str = Field(..., description="The id of the message")
content: str = Field(..., description="The content of the message")
Expand All @@ -21,7 +17,6 @@ class message(BaseModel):
examples=["system", "user"],
)


class CodeQueryRequest(BaseModel):
question: str = Field(
...,
Expand All @@ -43,7 +38,6 @@ class CodeQueryRequest(BaseModel):
examples=["openai/docs", "samparkai/composio"],
)


class CodeQueryResponse(BaseModel):
response: str = Field(..., description="The response to the question")

Expand Down
17 changes: 0 additions & 17 deletions composio/local_tools/greptile/test_codequery.py

This file was deleted.

9 changes: 4 additions & 5 deletions composio/local_tools/greptile/tool.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
from composio.local_tools.tool import Tool

from .codequery import CodeQuery

import typing as t
from composio.core.local import Tool, Action
from .actions import CodeQuery

class Greptile(Tool):
"""
Code understanding tool. Index Code and answer questions about it.
"""

def actions(self) -> list:
def actions(self) -> list[t.Type[Action]]:
return [CodeQuery]

def triggers(self) -> list:
Expand Down
11 changes: 0 additions & 11 deletions composio/local_tools/local_workspace/Readme.md

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .cmd_manager_tool import CmdManagerTool
from .tool import CmdManagerTool
Loading
Loading