Skip to content

Commit

Permalink
feat: update core and sdk for triggers
Browse files Browse the repository at this point in the history
  • Loading branch information
utkarsh-dixit committed Mar 31, 2024
1 parent 271cc1a commit d095271
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 4 deletions.
72 changes: 68 additions & 4 deletions core/composio/composio_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
from rich.console import Console
import termcolor
from uuid import getnode as get_mac
from .sdk.storage import save_user_connection
from .sdk.core import ComposioCore
from .sdk.utils import generate_enums
from sdk.storage import save_user_connection
from sdk.core import ComposioCore
from sdk.utils import generate_enums

import webbrowser

Expand All @@ -33,12 +33,74 @@ def parse_arguments():
list_connections_parser.add_argument('appName', type=str, help='Name of the app to list connections for')
list_connections_parser.set_defaults(func=list_connections)

# Correcting the structure to reflect the followup instructions
# Set command with nested global-trigger-callback command
set_parser = subparsers.add_parser('set', help='Set configurations')
set_subparsers = set_parser.add_subparsers(help='set commands', dest='set_command')
set_subparsers.required = True

# Nested global-trigger-callback command under set
global_trigger_callback_parser = set_subparsers.add_parser('global-trigger-callback', help='Set a global trigger callback URL')
global_trigger_callback_parser.add_argument('callback_url', type=str, help='The URL to be called when a global trigger is activated')
global_trigger_callback_parser.set_defaults(func=set_global_trigger_callback)

# Enable trigger command
enable_trigger_parser = subparsers.add_parser('enable-trigger', help='Enable a specific trigger for an app')
enable_trigger_parser.add_argument('app_name', type=str, help='Name of the app to enable the trigger for')
enable_trigger_parser.add_argument('trigger_name', type=str, help='Name of the trigger to enable')
enable_trigger_parser.set_defaults(func=enable_trigger)

# List triggers command
list_triggers_parser = subparsers.add_parser('list-triggers', help='List all triggers for a given app')
list_triggers_parser.add_argument('app_name', type=str, help='Name of the app to list triggers for')
list_triggers_parser.set_defaults(func=list_triggers)

# Generate enums command
generate_enums_parser = subparsers.add_parser('update', help='Update enums for apps and actions')
generate_enums_parser.set_defaults(func=handle_update)

return parser.parse_args()

def list_triggers(args):
client = ComposioCore()
auth_user(client)
app_name = args.app_name
console.print(f"\n[green]> Listing triggers for app: {app_name}...[/green]\n")
try:
triggers = client.list_triggers(app_name)
if triggers:
for trigger in triggers:
console.print(f"[yellow]- {trigger['name']} ({trigger['status']})[/yellow]")
else:
console.print("[red] No triggers found for the specified app.[/red]")
except Exception as e:
console.print(f"[red] Error occurred during listing triggers: {e}[/red]")
sys.exit(1)

def enable_trigger(args):
client = ComposioCore()
auth_user(client)
app_name = args.app_name
trigger_name = args.trigger_name
console.print(f"\n[green]> Enabling trigger: {trigger_name} for app: {app_name}...[/green]\n")
try:
client.enable_trigger(app_name, trigger_name)
console.print(f"\n[green]✔ Trigger enabled successfully![/green]\n")
except Exception as e:
console.print(f"[red] Error occurred during enabling trigger: {e}[/red]")
sys.exit(1)

def set_global_trigger_callback(args):
client = ComposioCore()
auth_user(client)
console.print(f"\n[green]> Setting global trigger callback to: {args.callback_url}...[/green]\n")
try:
client.set_global_trigger(args.callback_url)
console.print(f"\n[green]✔ Global trigger callback set successfully![/green]\n")
except Exception as e:
console.print(f"[red] Error occurred during setting global trigger callback: {e}[/red]")
sys.exit(1)

def handle_update(args):
generate_enums()
console.print(f"\n[green]✔ Enums updated successfully![/green]\n")
Expand Down Expand Up @@ -134,4 +196,6 @@ def main():
if hasattr(args, 'func'):
args.func(args)
else:
console.print("[red]Error: No valid command provided. Use --help for more information.[/red]")
console.print("[red]Error: No valid command provided. Use --help for more information.[/red]")

main()
18 changes: 18 additions & 0 deletions core/composio/sdk/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,24 @@ def initiate_connection(self, integrationId: Union[str, TestIntegration]) -> Con

raise Exception("Failed to create connection")

def set_global_trigger(self, callback_url: str):
try:
self.sdk.set_global_trigger(callback_url)
except Exception as e:
raise Exception(f"Failed to set global trigger: {e}")

def list_triggers(self, app_name: str):
try:
return self.sdk.list_triggers(app_name)
except Exception as e:
raise Exception(f"Failed to list triggers: {e}")

def get_trigger_requirements(self, trigger_ids: list[str] = None):
try:
return self.sdk.get_trigger_requirements(trigger_ids)
except Exception as e:
raise Exception(f"Failed to enable trigger: {e}")

def execute_action(self, action: Action, params: dict):
tool_name = action.value[0]
connectionId = get_user_connection(tool_name)
Expand Down
24 changes: 24 additions & 0 deletions core/composio/sdk/sdk.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,30 @@ def __init__(
{"Content-Type": "application/json", "x-api-key": self.api_key}
)

def list_triggers(self, app_names: list[str] = None):
resp = self.http_client.get(f"{self.base_url}/v1/triggers", params={
"appNames": ",".join(app_names) if app_names else None
})
return resp.json()

def get_trigger_requirements(self, trigger_ids: list[str] = None):
resp = self.http_client.get(f"{self.base_url}/v1/triggers", params={
"triggerIds": ",".join(trigger_ids) if trigger_ids else None
})
return resp.json()

def set_global_trigger(self, callback_url: str):
if not self.api_key:
raise ValueError("API Key not set")

resp = self.http_client.post(f"{self.base_url}/v1/triggers/setCallbackURL", json={
"callbackURL": callback_url,
})
if resp.status_code == 200:
return resp.json()

raise Exception("Failed to set global trigger callback")

def get_list_of_apps(self):
resp = self.http_client.get(f"{self.base_url}/v1/apps")
return resp.json()
Expand Down

0 comments on commit d095271

Please sign in to comment.