|
| 1 | +import dataclasses |
| 2 | +import sys |
| 3 | +from typing import Any, Unpack |
| 4 | +from typing_extensions import override |
| 5 | +from agents import ( |
| 6 | + Agent, |
| 7 | + Handoff, |
| 8 | + RunConfig, |
| 9 | + RunResult, |
| 10 | + RunResultStreaming, |
| 11 | + RunState, |
| 12 | + TContext, |
| 13 | + TResponseInputItem, |
| 14 | +) |
| 15 | +from agents.run import ( |
| 16 | + DEFAULT_MAX_TURNS, |
| 17 | + AgentRunner, |
| 18 | + RunOptions, |
| 19 | + set_default_agent_runner, |
| 20 | +) |
| 21 | +from cadence.contrib.openai.cadence_model import CadenceModel |
| 22 | + |
| 23 | +# TraceCtxManager assumes that the model invocation is running in the same context. |
| 24 | +# This is no longer true with Cadence workflows, where each invocation is running in a separate coroutine. |
| 25 | +# Thus, suppress the error |
| 26 | +_original_unraisablehook = sys.unraisablehook |
| 27 | + |
| 28 | + |
| 29 | +def _suppress_ctx_error_hook(args) -> None: |
| 30 | + if isinstance(args.exc_value, ValueError) and ( |
| 31 | + isinstance(args.exc_value, ValueError) |
| 32 | + and "was created in a different Context" in str(args.exc_value) |
| 33 | + ): |
| 34 | + return |
| 35 | + _original_unraisablehook(args) |
| 36 | + |
| 37 | + |
| 38 | +sys.unraisablehook = _suppress_ctx_error_hook |
| 39 | + |
| 40 | + |
| 41 | +def _replace_model_in_agent( |
| 42 | + agent: Agent[Any], |
| 43 | +) -> Agent[Any]: |
| 44 | + if isinstance(agent.model, CadenceModel): |
| 45 | + return agent |
| 46 | + |
| 47 | + name = agent.model |
| 48 | + if not isinstance(name, str): |
| 49 | + raise ValueError("Model name must be a string") |
| 50 | + |
| 51 | + agent.model = CadenceModel(model_name=name) |
| 52 | + |
| 53 | + for i, handoff in enumerate(agent.handoffs): |
| 54 | + if isinstance(handoff, Agent): |
| 55 | + agent.handoffs[i] = _replace_model_in_agent(handoff) |
| 56 | + elif isinstance(handoff, Handoff): |
| 57 | + raise ValueError("Handoff is not yet supported") |
| 58 | + else: |
| 59 | + raise TypeError(f"Unknown handoff type: {type(handoff)}") |
| 60 | + |
| 61 | + return agent |
| 62 | + |
| 63 | + |
| 64 | +class CadenceAgentRunner(AgentRunner): |
| 65 | + @override |
| 66 | + async def run( |
| 67 | + self, |
| 68 | + starting_agent: Agent[TContext], |
| 69 | + input: str | list[TResponseInputItem] | RunState[TContext], |
| 70 | + **kwargs: Unpack[RunOptions[TContext]], |
| 71 | + ) -> RunResult: |
| 72 | + |
| 73 | + context = kwargs.get("context") |
| 74 | + max_turns = kwargs.get("max_turns", DEFAULT_MAX_TURNS) |
| 75 | + hooks = kwargs.get("hooks") |
| 76 | + run_config = kwargs.get("run_config") |
| 77 | + error_handlers = kwargs.get("error_handlers") |
| 78 | + previous_response_id = kwargs.get("previous_response_id") |
| 79 | + auto_previous_response_id = kwargs.get("auto_previous_response_id", False) |
| 80 | + conversation_id = kwargs.get("conversation_id") |
| 81 | + session = kwargs.get("session") |
| 82 | + |
| 83 | + # if starting_agent.tools: |
| 84 | + # raise ValueError(f"Tools are not yet supported") |
| 85 | + |
| 86 | + if starting_agent.mcp_servers: |
| 87 | + raise ValueError("MCP servers are not yet supported") |
| 88 | + |
| 89 | + if not run_config: |
| 90 | + run_config = RunConfig() |
| 91 | + |
| 92 | + if run_config.model: |
| 93 | + if not isinstance(run_config.model, str): |
| 94 | + raise ValueError("Model must be a string") |
| 95 | + run_config = dataclasses.replace( |
| 96 | + run_config, |
| 97 | + model=CadenceModel(run_config.model), |
| 98 | + ) |
| 99 | + |
| 100 | + return await super().run( |
| 101 | + starting_agent=_replace_model_in_agent(starting_agent), |
| 102 | + input=input, |
| 103 | + context=context, |
| 104 | + max_turns=max_turns, |
| 105 | + hooks=hooks, |
| 106 | + run_config=run_config, |
| 107 | + error_handlers=error_handlers, |
| 108 | + previous_response_id=previous_response_id, |
| 109 | + auto_previous_response_id=auto_previous_response_id, |
| 110 | + conversation_id=conversation_id, |
| 111 | + session=session, |
| 112 | + ) |
| 113 | + |
| 114 | + @override |
| 115 | + def run_sync( |
| 116 | + self, |
| 117 | + starting_agent: Agent[TContext], |
| 118 | + input: str | list[TResponseInputItem] | RunState[TContext], |
| 119 | + **kwargs: Unpack[RunOptions[TContext]], |
| 120 | + ) -> RunResult: |
| 121 | + raise RuntimeError("Model run_sync is not yet supported.") |
| 122 | + |
| 123 | + @override |
| 124 | + def run_streamed( |
| 125 | + self, |
| 126 | + starting_agent: Agent[TContext], |
| 127 | + input: str | list[TResponseInputItem] | RunState[TContext], |
| 128 | + **kwargs: Unpack[RunOptions[TContext]], |
| 129 | + ) -> RunResultStreaming: |
| 130 | + raise RuntimeError("Model run_streamed is not yet supported.") |
| 131 | + |
| 132 | + |
| 133 | +set_default_agent_runner(CadenceAgentRunner()) |
0 commit comments