Skip to content

Commit

Permalink
Merge pull request #96 from SamparkAI/ft-did-you-mean
Browse files Browse the repository at this point in the history
feat: did you mean support
  • Loading branch information
utkarsh-dixit authored Jun 4, 2024
2 parents 302b51d + df199bd commit 871584a
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 10 deletions.
6 changes: 3 additions & 3 deletions composio/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
from composio.cli.logout import _logout
from composio.cli.triggers import _triggers
from composio.cli.whoami import _whoami
from composio.cli.utils.helpfulcmd import HelpfulCmdBase
from composio.core.cls.did_you_mean import DYMGroup

class RichGroup(click.Group):
class HelpDYMGroup(DYMGroup):
def format_help(self, ctx, formatter):
formatter.write("\n")

Expand All @@ -27,7 +27,7 @@ def format_help(self, ctx, formatter):
formatter.write(click.style("composio login", fg='green') + click.style(" # Log in to your Composio account\n", fg='black'))


@click.group(name="composio",cls=RichGroup)
@click.group(name="composio",cls=HelpDYMGroup)
@click.help_option("--help", "-h","-help")
def composio() -> None:
"""
Expand Down
4 changes: 3 additions & 1 deletion composio/cli/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,18 @@

from composio.cli.context import Context, pass_context
from composio.client.enums import App
from composio.core.cls.did_you_mean import DYMGroup
from composio.exceptions import ComposioSDKError
from composio.cli.utils.helpfulcmd import HelpfulCmdBase

class ActionsExamples(HelpfulCmdBase, click.Group):
class ActionsExamples(HelpfulCmdBase, DYMGroup):
examples = [
click.style("composio actions", fg='green') + click.style(" # List all actions\n", fg='black'),
click.style("composio actions --app slack", fg='green') + click.style(" # List all actions for the Slack app\n", fg='black'),
click.style("composio actions --use-case 'get channel messages'", fg='green') + click.style(" # List all actions for the 'get channel messages' use case\n", fg='black'),
]


@click.group(name="actions", invoke_without_command=True, cls=ActionsExamples)
@click.help_option("--help", "-h", "-help")
@click.option(
Expand Down
2 changes: 1 addition & 1 deletion composio/cli/add.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@
from composio.utils.url import get_web_url
from composio.cli.utils.helpfulcmd import HelpfulCmd


class AddIntegrationExamples(HelpfulCmd):
examples = [
click.style("composio add <app_name>", fg='green') + click.style(" # Add a new integration\n", fg='black'),
click.style("composio add <app_name> --no-browser", fg='green') + click.style(" # Add a new integration without opening the browser\n", fg='black'),
click.style("composio add <app_name> -i <integration_id>", fg='green') + click.style(" # Add a new integration using an existing integration ID\n", fg='black'),
]

@click.command(name="add", cls=AddIntegrationExamples)
@click.help_option("--help", "-h", "-help")
@click.argument("name", type=str)
Expand Down
4 changes: 2 additions & 2 deletions composio/cli/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
from composio.client import ActionModel, AppModel, TriggerModel, enums
from composio.client.local_handler import LocalToolHandler
from composio.exceptions import ComposioSDKError
from composio.core.cls.did_you_mean import DYMGroup
from composio.cli.utils.helpfulcmd import HelpfulCmdBase


MODULE_TEMPLATE = """\"\"\"
Helper Enum classes.
Expand Down Expand Up @@ -126,7 +126,7 @@ def event(self) -> str:
{triggers}
"""

class AppsExamples(HelpfulCmdBase, click.Group):
class AppsExamples(HelpfulCmdBase, DYMGroup):
examples = [
click.style("composio apps", fg='green') + click.style(" # List all apps\n", fg='black'),
click.style("composio apps --enabled", fg='green') + click.style(" # List only enabled apps\n", fg='black'),
Expand Down
4 changes: 3 additions & 1 deletion composio/cli/connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@
import click

from composio.cli.context import Context, pass_context
from composio.core.cls.did_you_mean import DYMGroup
from composio.exceptions import ComposioSDKError
from composio.cli.utils.helpfulcmd import HelpfulCmdBase

class ConnectionsExamples(HelpfulCmdBase, click.Group):
class ConnectionsExamples(HelpfulCmdBase, DYMGroup):
examples = [
click.style("composio connections", fg='green') + click.style(" # List all connections\n", fg='black'),
click.style("composio connections get 123", fg='green') + click.style(" # Get details of connection with ID 123\n", fg='black'),
click.style("composio connections delete 456", fg='green') + click.style(" # Delete connection with ID 456\n", fg='black'),
]


@click.group(name="connections", invoke_without_command=True, cls=ConnectionsExamples)
@click.help_option("--help", "-h", "-help")
@pass_context
Expand Down
3 changes: 2 additions & 1 deletion composio/cli/integrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
import click

from composio.cli.context import Context, login_required, pass_context
from composio.core.cls.did_you_mean import DYMGroup
from composio.exceptions import ComposioSDKError
from composio.cli.utils.helpfulcmd import HelpfulCmdBase

class IntegrationsExamples(HelpfulCmdBase, click.Group):
class IntegrationsExamples(HelpfulCmdBase, DYMGroup):
examples = [
click.style("composio integrations", fg='green') + click.style(" # List all integrations\n", fg='black'),
click.style("composio integrations add --name GitHub", fg='green') + click.style(" # Add a new integration named GitHub\n", fg='black'),
Expand Down
3 changes: 2 additions & 1 deletion composio/cli/triggers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@

from composio.cli.context import Context, pass_context
from composio.client.exceptions import NoItemsFound
from composio.core.cls.did_you_mean import DYMGroup
from composio.exceptions import ComposioSDKError

from composio.cli.utils.helpfulcmd import HelpfulCmdBase


class TriggersExamples(HelpfulCmdBase, click.Group):
class TriggersExamples(HelpfulCmdBase, DYMGroup):
examples = [
click.style("composio triggers", fg='green') + click.style(" # List all triggers\n", fg='black'),
click.style("composio triggers --active", fg='green') + click.style(" # List only active triggers\n", fg='black'),
Expand Down
Empty file added composio/core/cls/__init__.py
Empty file.
64 changes: 64 additions & 0 deletions composio/core/cls/did_you_mean.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
"""
Extension for ``click`` to provide a group
with a git-like *did-you-mean* feature.
"""

import difflib
import typing
import click


class DYMMixin:
"""
Mixin class for click MultiCommand inherited classes
to provide git-like *did-you-mean* functionality when
a certain command is not registered.
"""

def __init__(self, *args: typing.Any, **kwargs: typing.Any) -> None: # noqa: ANN401
self.max_suggestions = kwargs.pop("max_suggestions", 3)
self.cutoff = kwargs.pop("cutoff", 0.5)
super().__init__(*args, **kwargs) # type: ignore[call-arg]

def resolve_command(
self, ctx: click.Context, args: typing.List[str]
) -> typing.Tuple[
typing.Optional[str], typing.Optional[click.Command], typing.List[str]
]:
"""
Overrides clicks ``resolve_command`` method
and appends *Did you mean ...* suggestions
to the raised exception message.
"""
try:
return super().resolve_command(ctx, args) # type: ignore[misc]
except click.exceptions.UsageError as error:
error_msg = str(error)
original_cmd_name = click.utils.make_str(args[0])
matches = difflib.get_close_matches(
original_cmd_name,
self.list_commands(ctx), # type: ignore[attr-defined]
self.max_suggestions,
self.cutoff,
)
if matches:
fmt_matches = "\n ".join(matches)
error_msg += f" Did you mean one of these?\n {fmt_matches}"

raise click.exceptions.UsageError(error_msg, error.ctx) from error


class DYMGroup(DYMMixin, click.Group):
"""
click Group to provide git-like
*did-you-mean* functionality when a certain
command is not found in the group.
"""


class DYMCommandCollection(DYMMixin, click.CommandCollection):
"""
click CommandCollection to provide git-like
*did-you-mean* functionality when a certain
command is not found in the group.
"""

0 comments on commit 871584a

Please sign in to comment.