tools functions

This commit is contained in:
Steve Androulakis
2025-01-01 15:30:39 -08:00
parent dbc9ea7688
commit 33af355363
3 changed files with 45 additions and 18 deletions

View File

@@ -102,26 +102,16 @@ def get_current_date_human_readable():
@activity.defn(dynamic=True) @activity.defn(dynamic=True)
def dynamic_tool_activity(args: Sequence[RawValue]) -> dict: def dynamic_tool_activity(args: Sequence[RawValue]) -> dict:
"""Dynamic activity that is invoked via an unknown activity type.""" """Invoked for an unknown activity type, delegates to the correct tool."""
tool_name = activity.info().activity_type # e.g. "SearchFlights" 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) tool_args = activity.payload_converter().from_payload(args[0].payload, dict)
# Extract fields from the arguments activity.logger.info(f"Dynamic activity triggered for tool: {tool_name}")
date_depart = tool_args.get("dateDepart") handler_func = get_handler(tool_name)
date_return = tool_args.get("dateReturn")
origin = tool_args.get("origin")
destination = tool_args.get("destination")
# Print (or log) them # Delegate to the tool's function
activity.logger.info(f"Tool: {tool_name}") result = handler_func(tool_args)
activity.logger.info(f"Depart: {date_depart}, Return: {date_return}")
activity.logger.info(f"Origin: {origin}, Destination: {destination}")
# For now, just return them return result
return {
"tool": tool_name,
"args": tool_args,
"status": "OK - dynamic activity stub",
}

13
tools/__init__.py Normal file
View File

@@ -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}'")

24
tools/search_flights.py Normal file
View File

@@ -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,
}