mirror of
https://github.com/temporal-community/temporal-ai-agent.git
synced 2026-03-15 14:08:08 +01:00
tools functions
This commit is contained in:
@@ -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
|
||||
|
||||
13
tools/__init__.py
Normal file
13
tools/__init__.py
Normal 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
24
tools/search_flights.py
Normal 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,
|
||||
}
|
||||
Reference in New Issue
Block a user