Skip to content

Commit 3b74f99

Browse files
committed
Move activity_as_tool function to DurableAIAgentContext class
1 parent f31e4cf commit 3b74f99

File tree

3 files changed

+49
-59
lines changed

3 files changed

+49
-59
lines changed

samples-v2/openai_agents/durable_ai_agent_context.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
import json
2+
from typing import Any, Callable, Optional
3+
24
from azure.durable_functions.models.DurableOrchestrationContext import DurableOrchestrationContext
35
from yield_exception import YieldException
46

7+
from agents import (
8+
RunContextWrapper,
9+
Tool,
10+
)
11+
from agents.function_schema import function_schema
12+
from agents.tool import FunctionTool
13+
514
class DurableAIAgentContext:
615
def __init__(self, context: DurableOrchestrationContext):
716
self.context = context
@@ -42,3 +51,42 @@ def yield_and_clear_tasks(self):
4251
for task in self.tasks_to_yield:
4352
yield task
4453
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+
)

samples-v2/openai_agents/function_app.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from agents.model_settings import ModelSettings
88
from durable_decorators import durable_openai_agent_orchestrator
99
from azure.identity import DefaultAzureCredential
10-
from tools import activity_as_tool
1110
from pydantic import BaseModel
1211

1312

@@ -126,7 +125,7 @@ def weather_expert(context):
126125
name="Hello world",
127126
instructions="You are a helpful agent.",
128127
tools=[
129-
activity_as_tool(context, get_weather)
128+
context.activity_as_tool(get_weather)
130129
],
131130
)
132131

samples-v2/openai_agents/tools.py

Lines changed: 0 additions & 57 deletions
This file was deleted.

0 commit comments

Comments
 (0)