|
1 | 1 | import json
|
| 2 | +from typing import Any, Callable, Optional |
| 3 | + |
2 | 4 | from azure.durable_functions.models.DurableOrchestrationContext import DurableOrchestrationContext
|
3 | 5 | from yield_exception import YieldException
|
4 | 6 |
|
| 7 | +from agents import ( |
| 8 | + RunContextWrapper, |
| 9 | + Tool, |
| 10 | +) |
| 11 | +from agents.function_schema import function_schema |
| 12 | +from agents.tool import FunctionTool |
| 13 | + |
5 | 14 | class DurableAIAgentContext:
|
6 | 15 | def __init__(self, context: DurableOrchestrationContext):
|
7 | 16 | self.context = context
|
@@ -42,3 +51,42 @@ def yield_and_clear_tasks(self):
|
42 | 51 | for task in self.tasks_to_yield:
|
43 | 52 | yield task
|
44 | 53 | self.tasks_to_yield.clear()
|
| 54 | + |
| 55 | + def activity_as_tool( |
| 56 | + self, |
| 57 | + activity_func: Callable, |
| 58 | + *, |
| 59 | + description: Optional[str] = None, |
| 60 | + ) -> Tool: |
| 61 | + """ |
| 62 | + Convert an Azure Durable Functions activity to an OpenAI Agents SDK Tool. |
| 63 | +
|
| 64 | + Args: |
| 65 | + activity_func: The Azure Functions activity function to convert |
| 66 | +
|
| 67 | + Returns: |
| 68 | + Tool: An OpenAI Agents SDK Tool object |
| 69 | + """ |
| 70 | + |
| 71 | + activity_name = activity_func._function._name |
| 72 | + |
| 73 | + async def run_activity(ctx: RunContextWrapper[Any], input: str) -> Any: |
| 74 | + result = self.get_activity_call_result(activity_name, input) |
| 75 | + return result |
| 76 | + |
| 77 | + schema = function_schema( |
| 78 | + func=activity_func._function._func, |
| 79 | + name_override=activity_name, |
| 80 | + docstring_style=None, |
| 81 | + description_override=description, |
| 82 | + use_docstring_info=True, |
| 83 | + strict_json_schema=False, |
| 84 | + ) |
| 85 | + |
| 86 | + return FunctionTool( |
| 87 | + name=schema.name, |
| 88 | + description=schema.description or "", |
| 89 | + params_json_schema=schema.params_json_schema, |
| 90 | + on_invoke_tool=run_activity, |
| 91 | + strict_json_schema=False, |
| 92 | + ) |
0 commit comments