diff --git a/activities/tool_activities.py b/activities/tool_activities.py index a015946..28a8ecf 100644 --- a/activities/tool_activities.py +++ b/activities/tool_activities.py @@ -102,26 +102,16 @@ def get_current_date_human_readable(): @activity.defn(dynamic=True) def dynamic_tool_activity(args: Sequence[RawValue]) -> dict: - """Dynamic activity that is invoked via an unknown activity type.""" - tool_name = activity.info().activity_type # e.g. "SearchFlights" + """Invoked for an unknown activity type, delegates to the correct tool.""" + from tools import get_handler # import the registry function - # The first payload is the dictionary of arguments + tool_name = activity.info().activity_type # e.g. "SearchFlights" tool_args = activity.payload_converter().from_payload(args[0].payload, dict) - # Extract fields from the arguments - date_depart = tool_args.get("dateDepart") - date_return = tool_args.get("dateReturn") - origin = tool_args.get("origin") - destination = tool_args.get("destination") + activity.logger.info(f"Dynamic activity triggered for tool: {tool_name}") + handler_func = get_handler(tool_name) - # Print (or log) them - activity.logger.info(f"Tool: {tool_name}") - activity.logger.info(f"Depart: {date_depart}, Return: {date_return}") - activity.logger.info(f"Origin: {origin}, Destination: {destination}") + # Delegate to the tool's function + result = handler_func(tool_args) - # For now, just return them - return { - "tool": tool_name, - "args": tool_args, - "status": "OK - dynamic activity stub", - } + return result diff --git a/tools/__init__.py b/tools/__init__.py new file mode 100644 index 0000000..caf3c55 --- /dev/null +++ b/tools/__init__.py @@ -0,0 +1,13 @@ +from .search_flights import search_flights + + +def get_handler(tool_name: str): + """ + Return a function reference for the given tool. + You can add more tools here, e.g. "BookHotel", etc. + """ + if tool_name == "SearchFlights": + return search_flights + + # Or raise if not recognized + raise ValueError(f"No handler found for tool '{tool_name}'") diff --git a/tools/search_flights.py b/tools/search_flights.py new file mode 100644 index 0000000..0fe03fc --- /dev/null +++ b/tools/search_flights.py @@ -0,0 +1,24 @@ +def search_flights(args: dict) -> dict: + """ + Example function for searching flights. + Currently just prints/returns the passed args, + but you can add real flight search logic later. + """ + date_depart = args.get("dateDepart") + date_return = args.get("dateReturn") + origin = args.get("origin") + destination = args.get("destination") + + print(f"Searching flights from {origin} to {destination}") + print(f"Depart: {date_depart}, Return: {date_return}") + + # Return a mock result so you can verify it + return { + "tool": "SearchFlights", + "searchResults": [ + "Mock flight 1", + "Mock flight 2", + ], + "status": "search-complete", + "args": args, + }