|
| 1 | +import os |
| 2 | +import asyncio |
| 3 | +import aiohttp |
| 4 | +import logging |
| 5 | +from enum import Enum |
| 6 | +from urllib.parse import urlparse |
| 7 | + |
| 8 | +from pydantic import BaseModel, Field |
| 9 | + |
| 10 | +from beeai_framework.context import RunContext |
| 11 | +from beeai_framework.emitter import Emitter |
| 12 | +from beeai_framework.tools import JSONToolOutput, Tool, ToolRunOptions, ToolError |
| 13 | + |
| 14 | +logger = logging.getLogger(__name__) |
| 15 | + |
| 16 | + |
| 17 | +class UpstreamSearchResult(Enum): |
| 18 | + FOUND = "found" |
| 19 | + NOT_FOUND = "not_found" |
| 20 | + NOT_POSSIBLE = "not_possible" |
| 21 | + |
| 22 | + |
| 23 | +class UpstreamSearchToolInput(BaseModel): |
| 24 | + project: str = Field( |
| 25 | + description="name of the upstream project which should be searched through") |
| 26 | + description: str = Field( |
| 27 | + description="description of issue for which fixing commit will be looked for") |
| 28 | + date: str | None = Field( |
| 29 | + description="date in iso format after which the commit was created") |
| 30 | + |
| 31 | + |
| 32 | +class UpstreamSearchToolResult(BaseModel): |
| 33 | + result: UpstreamSearchResult = Field( |
| 34 | + description="result of the tool invocation") |
| 35 | + repository_url: str | None = Field( |
| 36 | + description="url of repository where commits reside") |
| 37 | + related_commits: list[str] | None = Field( |
| 38 | + description="commits related to given description") |
| 39 | + |
| 40 | + |
| 41 | +class UpstreamSearchToolOutput(JSONToolOutput[UpstreamSearchToolResult]): |
| 42 | + pass |
| 43 | + |
| 44 | + |
| 45 | +class UpstreamSearchTool(Tool[UpstreamSearchToolInput, ToolRunOptions, UpstreamSearchToolOutput]): |
| 46 | + name = "upstream_search" |
| 47 | + description = """ |
| 48 | + Search through upstream project's git repository and finds commits related to |
| 49 | + provided description and optionally allows to filter commits made after provided date. |
| 50 | +
|
| 51 | + If the tool was successful, 'result' is set to 'found' which means that commits |
| 52 | + 'related_commits'in repository 'repository_url' are the ones which should be related to |
| 53 | + provided description. |
| 54 | +
|
| 55 | + If the tool was unsuccessful to find commits for this particular query, 'result' |
| 56 | + is set to 'not_found'. |
| 57 | +
|
| 58 | + If the tool can not be used for this particular project, 'result' is set to 'not_possible'. |
| 59 | + """ |
| 60 | + input_schema = UpstreamSearchToolInput |
| 61 | + |
| 62 | + def _create_emitter(self) -> Emitter: |
| 63 | + return Emitter.root().child( |
| 64 | + namespace=["tool", "commands", self.name], |
| 65 | + creator=self, |
| 66 | + ) |
| 67 | + |
| 68 | + async def _run( |
| 69 | + self, tool_input: UpstreamSearchToolInput, |
| 70 | + options: ToolRunOptions | None, context: RunContext) -> UpstreamSearchToolOutput: |
| 71 | + try: |
| 72 | + timeout = aiohttp.ClientTimeout(total=30) |
| 73 | + repos = [] |
| 74 | + commits = [] |
| 75 | + async with aiohttp.ClientSession(timeout=timeout) as session: |
| 76 | + async with session.get(f"{os.environ['UPSTREAM_SEARCH_API_URL']}/find_repository", |
| 77 | + params={"name": tool_input.project}) as response: |
| 78 | + if response.status != 200: |
| 79 | + logger.debug("Searching did not yield repo. status %d response %s", |
| 80 | + response.status, await response.text()) |
| 81 | + return UpstreamSearchToolOutput(UpstreamSearchToolResult( |
| 82 | + result=UpstreamSearchResult.NOT_POSSIBLE, |
| 83 | + repository_url=None, |
| 84 | + related_commits=None |
| 85 | + )) |
| 86 | + repos = await response.json() |
| 87 | + |
| 88 | + # until we have solid reference to upstream repository through for example VCS |
| 89 | + # spec file tag, this is the best we can do |
| 90 | + post_params = {"url": repos[0], "text": tool_input.description} |
| 91 | + if tool_input.date is not None: |
| 92 | + post_params["date"] = tool_input.date |
| 93 | + async with session.post(f"{os.environ['UPSTREAM_SEARCH_API_URL']}/find_commit", |
| 94 | + json=post_params, timeout=240) as response: |
| 95 | + if response.status != 200: |
| 96 | + logger.debug("Searching did not yield commits. status %d response %s", |
| 97 | + response.status, await response.text()) |
| 98 | + return UpstreamSearchToolOutput(UpstreamSearchToolResult( |
| 99 | + result=UpstreamSearchResult.NOT_FOUND, |
| 100 | + repository_url=None, |
| 101 | + related_commits=None |
| 102 | + )) |
| 103 | + commits = await response.json() |
| 104 | + except asyncio.TimeoutError: |
| 105 | + raise ToolError("Timeout occured while contacting upstream-search backend") |
| 106 | + except Exception as e: |
| 107 | + raise ToolError(f"Unexpected internal error occured while contacting backend {e}") |
| 108 | + |
| 109 | + def get_patch_url(commit): |
| 110 | + parsed_url = urlparse(repos[0]) |
| 111 | + if not parsed_url.path.endswith(".git"): |
| 112 | + return commit |
| 113 | + if parsed_url.hostname.startswith("gitlab"): |
| 114 | + prefix = "/-" |
| 115 | + elif parsed_url.hostname.startswith("github"): |
| 116 | + prefix = "" |
| 117 | + else: |
| 118 | + return commit |
| 119 | + path = f"{prefix}/commit/{commit}.patch" |
| 120 | + return parsed_url._replace(path=parsed_url.path.replace(".git", path)).geturl() |
| 121 | + commits = [get_patch_url(commit) for commit in commits] |
| 122 | + |
| 123 | + return UpstreamSearchToolOutput(UpstreamSearchToolResult( |
| 124 | + result=UpstreamSearchResult.FOUND, |
| 125 | + repository_url=repos[0], |
| 126 | + related_commits=commits |
| 127 | + )) |
0 commit comments