|
| 1 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +# Licensed under the MIT License. |
| 3 | +"""Handoff conversion utilities for Azure Durable Functions OpenAI agent operations.""" |
| 4 | + |
| 5 | +from typing import Any |
| 6 | + |
| 7 | +from agents import Handoff |
| 8 | +from pydantic import BaseModel |
| 9 | + |
| 10 | + |
| 11 | +class DurableHandoff(BaseModel): |
| 12 | + """Serializable representation of a Handoff. |
| 13 | +
|
| 14 | + Contains only the data needed by the model execution to |
| 15 | + determine what to handoff to, not the actual handoff invocation. |
| 16 | + """ |
| 17 | + |
| 18 | + tool_name: str |
| 19 | + tool_description: str |
| 20 | + input_json_schema: dict[str, Any] |
| 21 | + agent_name: str |
| 22 | + strict_json_schema: bool = True |
| 23 | + |
| 24 | + @classmethod |
| 25 | + def from_handoff(cls, handoff: Handoff) -> "DurableHandoff": |
| 26 | + """Create a DurableHandoff from an OpenAI agent Handoff. |
| 27 | +
|
| 28 | + This method converts OpenAI agent Handoff instances into serializable |
| 29 | + DurableHandoff objects for use within Azure Durable Functions. |
| 30 | +
|
| 31 | + Parameters |
| 32 | + ---------- |
| 33 | + handoff : Handoff |
| 34 | + The OpenAI agent Handoff to convert |
| 35 | +
|
| 36 | + Returns |
| 37 | + ------- |
| 38 | + DurableHandoff |
| 39 | + A serializable handoff representation |
| 40 | + """ |
| 41 | + return cls( |
| 42 | + tool_name=handoff.tool_name, |
| 43 | + tool_description=handoff.tool_description, |
| 44 | + input_json_schema=handoff.input_json_schema, |
| 45 | + agent_name=handoff.agent_name, |
| 46 | + strict_json_schema=handoff.strict_json_schema, |
| 47 | + ) |
| 48 | + |
| 49 | + def to_handoff(self) -> Handoff[Any, Any]: |
| 50 | + """Create an OpenAI agent Handoff instance from this DurableHandoff. |
| 51 | +
|
| 52 | + This method converts the serializable DurableHandoff back into an |
| 53 | + OpenAI agent Handoff instance for execution. |
| 54 | +
|
| 55 | + Returns |
| 56 | + ------- |
| 57 | + Handoff |
| 58 | + OpenAI agent Handoff instance |
| 59 | + """ |
| 60 | + return Handoff( |
| 61 | + tool_name=self.tool_name, |
| 62 | + tool_description=self.tool_description, |
| 63 | + input_json_schema=self.input_json_schema, |
| 64 | + agent_name=self.agent_name, |
| 65 | + strict_json_schema=self.strict_json_schema, |
| 66 | + on_invoke_handoff=lambda ctx, input: None, |
| 67 | + ) |
0 commit comments