Skip to content

chore: better tools arrangement #509

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

Merged
merged 6 commits into from
Feb 15, 2025
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
16 changes: 8 additions & 8 deletions src/codegen/extensions/langchain/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
from .tools import (
CommitTool,
CreateFileTool,
CreatePRCommentTool,
CreatePRReviewCommentTool,
CreatePRTool,
DeleteFileTool,
EditFileTool,
GetPRcontentsTool,
GithubCreatePRCommentTool,
GithubCreatePRReviewCommentTool,
GithubCreatePRTool,
GithubViewPRTool,
ListDirectoryTool,
MoveSymbolTool,
RenameFileTool,
Expand Down Expand Up @@ -68,10 +68,10 @@ def create_codebase_agent(
SemanticEditTool(codebase),
SemanticSearchTool(codebase),
CommitTool(codebase),
CreatePRTool(codebase),
GetPRcontentsTool(codebase),
CreatePRCommentTool(codebase),
CreatePRReviewCommentTool(codebase),
GithubCreatePRTool(codebase),
GithubViewPRTool(codebase),
GithubCreatePRCommentTool(codebase),
GithubCreatePRReviewCommentTool(codebase),
]

# Get the prompt to use
Expand Down
56 changes: 37 additions & 19 deletions src/codegen/extensions/langchain/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from codegen import Codebase
from codegen.extensions.linear.linear_client import LinearClient
from codegen.extensions.tools.linear_tools import (
from codegen.extensions.tools.linear.linear import (
linear_comment_on_issue_tool,
linear_create_issue_tool,
linear_get_issue_comments_tool,
Expand Down Expand Up @@ -46,9 +46,9 @@
class ViewFileTool(BaseTool):
"""Tool for viewing file contents and metadata."""

name: ClassVar[str] = "view_file"

Check failure on line 49 in src/codegen/extensions/langchain/tools.py

View workflow job for this annotation

GitHub Actions / mypy

error: Cannot override instance variable (previously declared on base class "BaseTool") with class variable [misc]
description: ClassVar[str] = "View the contents and metadata of a file in the codebase"

Check failure on line 50 in src/codegen/extensions/langchain/tools.py

View workflow job for this annotation

GitHub Actions / mypy

error: Cannot override instance variable (previously declared on base class "BaseTool") with class variable [misc]
args_schema: ClassVar[type[BaseModel]] = ViewFileInput

Check failure on line 51 in src/codegen/extensions/langchain/tools.py

View workflow job for this annotation

GitHub Actions / mypy

error: Cannot override instance variable (previously declared on base class "BaseTool") with class variable [misc]
codebase: Codebase = Field(exclude=True)

def __init__(self, codebase: Codebase) -> None:
Expand All @@ -69,9 +69,9 @@
class ListDirectoryTool(BaseTool):
"""Tool for listing directory contents."""

name: ClassVar[str] = "list_directory"

Check failure on line 72 in src/codegen/extensions/langchain/tools.py

View workflow job for this annotation

GitHub Actions / mypy

error: Cannot override instance variable (previously declared on base class "BaseTool") with class variable [misc]
description: ClassVar[str] = "List contents of a directory in the codebase"

Check failure on line 73 in src/codegen/extensions/langchain/tools.py

View workflow job for this annotation

GitHub Actions / mypy

error: Cannot override instance variable (previously declared on base class "BaseTool") with class variable [misc]
args_schema: ClassVar[type[BaseModel]] = ListDirectoryInput

Check failure on line 74 in src/codegen/extensions/langchain/tools.py

View workflow job for this annotation

GitHub Actions / mypy

error: Cannot override instance variable (previously declared on base class "BaseTool") with class variable [misc]
codebase: Codebase = Field(exclude=True)

def __init__(self, codebase: Codebase) -> None:
Expand All @@ -92,8 +92,8 @@
class SearchTool(BaseTool):
"""Tool for searching the codebase."""

name: ClassVar[str] = "search"

Check failure on line 95 in src/codegen/extensions/langchain/tools.py

View workflow job for this annotation

GitHub Actions / mypy

error: Cannot override instance variable (previously declared on base class "BaseTool") with class variable [misc]
description: ClassVar[str] = "Search the codebase using text search"

Check failure on line 96 in src/codegen/extensions/langchain/tools.py

View workflow job for this annotation

GitHub Actions / mypy

error: Cannot override instance variable (previously declared on base class "BaseTool") with class variable [misc]
args_schema: ClassVar[type[BaseModel]] = SearchInput
codebase: Codebase = Field(exclude=True)

Expand Down Expand Up @@ -354,19 +354,24 @@
return json.dumps(result, indent=2)


class CreatePRInput(BaseModel):
########################################################################################################################
# GITHUB
########################################################################################################################


class GithubCreatePRInput(BaseModel):
"""Input for creating a PR"""

title: str = Field(..., description="The title of the PR")
body: str = Field(..., description="The body of the PR")


class CreatePRTool(BaseTool):
class GithubCreatePRTool(BaseTool):
"""Tool for creating a PR."""

name: ClassVar[str] = "create_pr"
description: ClassVar[str] = "Create a PR for the current branch"
args_schema: ClassVar[type[BaseModel]] = CreatePRInput
args_schema: ClassVar[type[BaseModel]] = GithubCreatePRInput
codebase: Codebase = Field(exclude=True)

def __init__(self, codebase: Codebase) -> None:
Expand All @@ -377,18 +382,18 @@
return json.dumps(result, indent=2)


class GetPRContentsInput(BaseModel):
class GithubViewPRInput(BaseModel):
"""Input for getting PR contents."""

pr_id: int = Field(..., description="Number of the PR to get the contents for")


class GetPRcontentsTool(BaseTool):
class GithubViewPRTool(BaseTool):
"""Tool for getting PR data."""

name: ClassVar[str] = "get_pr_contents"
description: ClassVar[str] = "Get the diff and modified symbols of a PR along with the dependencies of the modified symbols"
args_schema: ClassVar[type[BaseModel]] = GetPRContentsInput
name: ClassVar[str] = "view_pr"
description: ClassVar[str] = "View the diff and associated context for a pull request"
args_schema: ClassVar[type[BaseModel]] = GithubViewPRInput
codebase: Codebase = Field(exclude=True)

def __init__(self, codebase: Codebase) -> None:
Expand All @@ -399,19 +404,19 @@
return json.dumps(result, indent=2)


class CreatePRCommentInput(BaseModel):
class GithubCreatePRCommentInput(BaseModel):
"""Input for creating a PR comment"""

pr_number: int = Field(..., description="The PR number to comment on")
body: str = Field(..., description="The comment text")


class CreatePRCommentTool(BaseTool):
class GithubCreatePRCommentTool(BaseTool):
"""Tool for creating a general PR comment."""

name: ClassVar[str] = "create_pr_comment"
description: ClassVar[str] = "Create a general comment on a pull request"
args_schema: ClassVar[type[BaseModel]] = CreatePRCommentInput
args_schema: ClassVar[type[BaseModel]] = GithubCreatePRCommentInput
codebase: Codebase = Field(exclude=True)

def __init__(self, codebase: Codebase) -> None:
Expand All @@ -422,7 +427,7 @@
return json.dumps(result, indent=2)


class CreatePRReviewCommentInput(BaseModel):
class GithubCreatePRReviewCommentInput(BaseModel):
"""Input for creating an inline PR review comment"""

pr_number: int = Field(..., description="The PR number to comment on")
Expand All @@ -434,12 +439,12 @@
start_line: int | None = Field(None, description="For multi-line comments, the starting line")


class CreatePRReviewCommentTool(BaseTool):
class GithubCreatePRReviewCommentTool(BaseTool):
"""Tool for creating inline PR review comments."""

name: ClassVar[str] = "create_pr_review_comment"
description: ClassVar[str] = "Create an inline review comment on a specific line in a pull request"
args_schema: ClassVar[type[BaseModel]] = CreatePRReviewCommentInput
args_schema: ClassVar[type[BaseModel]] = GithubCreatePRReviewCommentInput
codebase: Codebase = Field(exclude=True)

def __init__(self, codebase: Codebase) -> None:
Expand Down Expand Up @@ -468,6 +473,11 @@
return json.dumps(result, indent=2)


########################################################################################################################
# LINEAR
########################################################################################################################


class LinearGetIssueInput(BaseModel):
"""Input for getting a Linear issue."""

Expand Down Expand Up @@ -597,6 +607,11 @@
return json.dumps(result, indent=2)


########################################################################################################################
# EXPORT
########################################################################################################################


def get_workspace_tools(codebase: Codebase) -> list["BaseTool"]:
"""Get all workspace tools initialized with a codebase.

Expand All @@ -609,12 +624,9 @@
return [
CommitTool(codebase),
CreateFileTool(codebase),
CreatePRTool(codebase),
CreatePRCommentTool(codebase),
CreatePRReviewCommentTool(codebase),
DeleteFileTool(codebase),
EditFileTool(codebase),
GetPRcontentsTool(codebase),
GithubViewPRTool(codebase),
ListDirectoryTool(codebase),
MoveSymbolTool(codebase),
RenameFileTool(codebase),
Expand All @@ -623,6 +635,12 @@
SemanticEditTool(codebase),
SemanticSearchTool(codebase),
ViewFileTool(codebase),
# Github
GithubCreatePRTool(codebase),
GithubCreatePRCommentTool(codebase),
GithubCreatePRReviewCommentTool(codebase),
GithubViewPRTool(codebase),
# Linear
LinearGetIssueTool(codebase),
LinearGetIssueCommentsTool(codebase),
LinearCommentOnIssueTool(codebase),
Expand Down
2 changes: 1 addition & 1 deletion src/codegen/extensions/tools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from .github.create_pr_comment import create_pr_comment
from .github.create_pr_review_comment import create_pr_review_comment
from .github.view_pr import view_pr
from .linear_tools import (
from .linear import (
linear_comment_on_issue_tool,
linear_get_issue_comments_tool,
linear_get_issue_tool,
Expand Down
11 changes: 11 additions & 0 deletions src/codegen/extensions/tools/github/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from .create_pr import create_pr
from .create_pr_comment import create_pr_comment
from .create_pr_review_comment import create_pr_review_comment
from .view_pr import view_pr

__all__ = [
"create_pr",
"create_pr_comment",
"create_pr_review_comment",
"view_pr",
]
2 changes: 1 addition & 1 deletion src/codegen/extensions/tools/github/view_pr.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@
modified_symbols, patch = codebase.get_modified_symbols_in_pr(pr_id)

# Convert modified_symbols set to list for JSON serialization
return {"status": "success", "modified_symbols": list(modified_symbols), "patch": patch}
return {"status": "success", "patch": patch}

Check warning on line 21 in src/codegen/extensions/tools/github/view_pr.py

View check run for this annotation

Codecov / codecov/patch

src/codegen/extensions/tools/github/view_pr.py#L21

Added line #L21 was not covered by tests
19 changes: 19 additions & 0 deletions src/codegen/extensions/tools/linear/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from .linear import (
linear_comment_on_issue_tool,
linear_create_issue_tool,
linear_get_issue_comments_tool,
linear_get_issue_tool,
linear_get_teams_tool,
linear_register_webhook_tool,
linear_search_issues_tool,
)

__all__ = [
"linear_comment_on_issue_tool",
"linear_create_issue_tool",
"linear_get_issue_comments_tool",
"linear_get_issue_tool",
"linear_get_teams_tool",
"linear_register_webhook_tool",
"linear_search_issues_tool",
]
Empty file added tests/integration/__init__.py
Empty file.
Empty file.
26 changes: 26 additions & 0 deletions tests/integration/extension/test_github.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""Tests for Linear tools."""

import os

import pytest

from codegen import Codebase
from codegen.extensions.linear.linear_client import LinearClient
from codegen.extensions.tools.github import view_pr


@pytest.fixture
def client() -> LinearClient:
"""Create a Linear client for testing."""
token = os.getenv("CODEGEN_SECRETS__GITHUB_TOKEN")
if not token:
pytest.skip("CODEGEN_SECRETS__GITHUB_TOKEN environment variable not set")
codebase = Codebase.from_repo("codegen-sh/Kevin-s-Adventure-Game")
return codebase

Check failure on line 19 in tests/integration/extension/test_github.py

View workflow job for this annotation

GitHub Actions / mypy

error: Incompatible return value type (got "Codebase[Any, Any, Any, Any, Any, Any, Any, Any, Any, Any, Any]", expected "LinearClient") [return-value]


def test_github_view_pr(client: LinearClient) -> None:
"""Test getting an issue from Linear."""
# Link to PR: https://github.com/codegen-sh/Kevin-s-Adventure-Game/pull/419
pr = view_pr(client, 419)

Check failure on line 25 in tests/integration/extension/test_github.py

View workflow job for this annotation

GitHub Actions / mypy

error: Argument 1 to "view_pr" has incompatible type "LinearClient"; expected "Codebase[Any, Any, Any, Any, Any, Any, Any, Any, Any, Any, Any]" [arg-type]
print(pr)
2 changes: 1 addition & 1 deletion tests/integration/extension/test_linear.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import pytest

from codegen.extensions.linear.linear_client import LinearClient
from codegen.extensions.tools.linear_tools import (
from codegen.extensions.tools.linear.linear import (
linear_comment_on_issue_tool,
linear_create_issue_tool,
linear_get_issue_comments_tool,
Expand Down