Skip to content

SimpleAgent

SimpleAgent

Bases: BaseAgent

A simple agent with env, tools, mcps, and context manager, wrapped on openai-agents.

Source code in utu/agents/simple_agent.py
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
class SimpleAgent(BaseAgent):
    """A simple agent with env, tools, mcps, and context manager, wrapped on openai-agents."""

    def __init__(
        self,
        *,
        config: AgentConfig | str | None = None,  # use config to pass agent configs
        name: str | None = None,
        instructions: str | Callable | None = None,
        model: str | Model | None = None,
        model_settings: ModelSettings | None = None,
        tools: list[Tool] = None,
        output_type: type[Any] | AgentOutputSchemaBase | None = None,
        tool_use_behavior: Literal["run_llm_again", "stop_on_first_tool"] | StopAtTools = "run_llm_again",
    ):
        self.config = self._get_config(config)
        if name:
            self.config.agent.name = name
        if instructions:
            self.config.agent.instructions = instructions
        self.model = self._get_model(self.config, model)
        self.model_settings = self._get_model_settings(self.config, model_settings)
        self.tools: list[Tool] = tools or []
        self.output_type: type[Any] | AgentOutputSchemaBase | None = output_type
        self.tool_use_behavior: Literal["run_llm_again", "stop_on_first_tool"] | StopAtTools = tool_use_behavior
        self.context_manager: BaseContextManager = None
        self.env: BaseEnv = None
        self.current_agent: Agent[TContext] = None  # move to task recorder?
        self.input_items: list[TResponseInputItem] = []

        self._run_hooks: RunHooks = None
        self._mcp_servers: list[MCPServer] = []
        self._toolkits: list[AsyncBaseToolkit] = []
        self._mcps_exit_stack = AsyncExitStack()
        self._tools_exit_stack = AsyncExitStack()
        self._initialized = False

    def _get_config(self, config: AgentConfig | str | None) -> AgentConfig:
        if isinstance(config, AgentConfig):
            return config
        return ConfigLoader.load_agent_config(config or "base")

    def _get_model(self, config: AgentConfig, model: str | Model | None = None) -> Model:
        if isinstance(model, Model):
            return model
        model_provider_config = config.model.model_provider.model_dump()
        if isinstance(model, str):
            model_provider_config["model"] = model
        return AgentsUtils.get_agents_model(**model_provider_config)

    def _get_model_settings(self, config: AgentConfig, model_settings: ModelSettings | None = None) -> ModelSettings:
        if isinstance(model_settings, ModelSettings):
            return model_settings
        return config.model.model_settings

    async def __aenter__(self):
        await self.build()
        return self

    async def __aexit__(self, exc_type, exc_val, exc_tb):
        await self.cleanup()

    async def build(self, trace_id: str = None):
        """Build the agent"""
        if self._initialized:
            logger.info("Agent already initialized! Skipping build.")
            return
        self.env = await get_env(self.config, trace_id or AgentsUtils.gen_trace_id())  # Pass trace_id
        await self.env.build()
        self.current_agent = Agent(
            name=self.config.agent.name,
            instructions=self.config.agent.instructions,
            model=self.model,
            model_settings=self.model_settings,
            tools=await self.get_tools(),
            output_type=self.output_type,
            tool_use_behavior=self.tool_use_behavior,
            mcp_servers=self._mcp_servers,
        )
        self.context_manager = build_context_manager(self.config)
        self._initialized = True

    async def cleanup(self):
        """Cleanup"""
        logger.info("Cleaning up MCP servers...")
        await self._mcps_exit_stack.aclose()
        self._mcp_servers = []
        logger.info("Cleaning up tools...")
        await self._tools_exit_stack.aclose()
        self._toolkits = []
        logger.info("Cleaning up env...")
        await self.env.cleanup()
        self._initialized = False

    async def get_tools(self) -> list[Tool]:
        if self.tools:
            return self.tools

        tools_list: list[Tool] = []
        tools_list += await self.env.get_tools()  # add env tools
        # TODO: handle duplicate tool names
        for _, toolkit_config in self.config.toolkits.items():
            if toolkit_config.mode == "mcp":
                await self._load_mcp_server(toolkit_config)
            elif toolkit_config.mode == "builtin":
                toolkit = await self._load_toolkit(toolkit_config)
                tools_list.extend(await toolkit.get_tools_in_agents())
            else:
                raise ValueError(f"Unknown toolkit mode: {toolkit_config.mode}")
        tool_names = [tool.name for tool in tools_list]
        logger.info(f"Loaded {len(tool_names)} tools: {tool_names}")
        self.tools = tools_list
        return tools_list

    async def _load_toolkit(self, toolkit_config: ToolkitConfig) -> AsyncBaseToolkit:
        logger.info(f"Loading builtin toolkit `{toolkit_config.name}` with config {toolkit_config}")
        toolkit = await self._tools_exit_stack.enter_async_context(TOOLKIT_MAP[toolkit_config.name](toolkit_config))
        self._toolkits.append(toolkit)
        return toolkit

    async def _load_mcp_server(self, toolkit_config: ToolkitConfig) -> MCPServer:
        logger.info(f"Loading MCP server `{toolkit_config.name}` with params {toolkit_config.config}")
        server = await self._mcps_exit_stack.enter_async_context(
            MCPServerStdio(  # FIXME: support other types of servers
                name=toolkit_config.name,
                params=toolkit_config.config,
                client_session_timeout_seconds=20,
            )
        )
        self._mcp_servers.append(server)
        return server

    def _get_run_config(self) -> RunConfig:
        run_config = RunConfig(
            model=self.current_agent.model,
            model_settings=self.config.model.model_settings,
            workflow_name=self.config.agent.name,
        )
        return run_config

    def _get_context(self) -> dict:
        return {
            "context_manager": self.context_manager,
            "env": self.env,
        }

    def _prepare_run_kwargs(self, input: str | list[TResponseInputItem]) -> dict:
        return {
            "starting_agent": self.current_agent,
            "input": input,
            "context": self._get_context(),
            "max_turns": self.config.max_turns,
            "hooks": self._run_hooks,
            "run_config": self._get_run_config(),
        }

    # wrap `Runner` apis in @openai-agents
    async def run(
        self, input: str | list[TResponseInputItem], trace_id: str = None, save: bool = False
    ) -> TaskRecorder:
        """Entrypoint for running the agent

        Args:
            trace_id: str to identify the run
            save: whether to use history (use `input_items`)
        """
        if not self._initialized:
            await self.build(trace_id)
        trace_id = trace_id or AgentsUtils.gen_trace_id()
        logger.info(f"> trace_id: {trace_id}")

        if isinstance(input, str):
            input = self.input_items + [{"content": input, "role": "user"}]
        run_kwargs = self._prepare_run_kwargs(input)
        if AgentsUtils.get_current_trace():
            run_result = await Runner.run(**run_kwargs)
        else:
            with trace(workflow_name="simple_agent", trace_id=trace_id):
                run_result = await Runner.run(**run_kwargs)

        task_recorder = TaskRecorder(input, trace_id)
        task_recorder.add_run_result(run_result)
        task_recorder.set_final_output(run_result.final_output)
        if save:
            self.input_items = run_result.to_input_list()
            self.current_agent = run_result.last_agent  # NOTE: acturally, there are only one agent in SimpleAgent
        return task_recorder

    def run_streamed(self, input: str | list[TResponseInputItem], trace_id: str = None) -> RunResultStreaming:
        """Entrypoint for running the agent streamly

        Notes:
            - do not support `save` option for now

        Args:
            trace_id: str to identify the run
        """
        if not self._initialized:
            raise RuntimeError("Agent is not initialized. Please call `build` first.")
        trace_id = trace_id or AgentsUtils.gen_trace_id()
        logger.info(f"> trace_id: {trace_id}")

        run_kwargs = self._prepare_run_kwargs(input)
        if AgentsUtils.get_current_trace():
            return Runner.run_streamed(**run_kwargs)
        else:
            with trace(workflow_name="simple_agent", trace_id=trace_id):
                return Runner.run_streamed(**run_kwargs)

    # util apis
    async def chat(self, input: str) -> RunResult:
        # TODO: set "session-level" tracing for multi-turn chat
        self.input_items.append({"content": input, "role": "user"})
        recorder = await self.run(self.input_items, save=True)
        run_result = recorder.get_run_result()
        AgentsUtils.print_new_items(run_result.new_items)
        return run_result

    async def chat_streamed(self, input: str) -> RunResultStreaming:
        self.input_items.append({"content": input, "role": "user"})
        run_result_streaming = self.run_streamed(self.input_items)
        await AgentsUtils.print_stream_events(run_result_streaming.stream_events())
        self.input_items = run_result_streaming.to_input_list()
        self.current_agent = run_result_streaming.last_agent
        return run_result_streaming

    def set_instructions(self, instructions: str):
        logger.warning("WARNING: reset instructions is dangerous!")
        self.current_agent.instructions = instructions

    def clear_input_items(self):
        # reset chat history
        self.input_items = []

    def set_run_hooks(self, run_hooks: RunHooks):
        # WIP
        self._run_hooks = run_hooks

build async

build(trace_id: str = None)

Build the agent

Source code in utu/agents/simple_agent.py
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
async def build(self, trace_id: str = None):
    """Build the agent"""
    if self._initialized:
        logger.info("Agent already initialized! Skipping build.")
        return
    self.env = await get_env(self.config, trace_id or AgentsUtils.gen_trace_id())  # Pass trace_id
    await self.env.build()
    self.current_agent = Agent(
        name=self.config.agent.name,
        instructions=self.config.agent.instructions,
        model=self.model,
        model_settings=self.model_settings,
        tools=await self.get_tools(),
        output_type=self.output_type,
        tool_use_behavior=self.tool_use_behavior,
        mcp_servers=self._mcp_servers,
    )
    self.context_manager = build_context_manager(self.config)
    self._initialized = True

cleanup async

cleanup()

Cleanup

Source code in utu/agents/simple_agent.py
116
117
118
119
120
121
122
123
124
125
126
async def cleanup(self):
    """Cleanup"""
    logger.info("Cleaning up MCP servers...")
    await self._mcps_exit_stack.aclose()
    self._mcp_servers = []
    logger.info("Cleaning up tools...")
    await self._tools_exit_stack.aclose()
    self._toolkits = []
    logger.info("Cleaning up env...")
    await self.env.cleanup()
    self._initialized = False

run async

run(
    input: str | list[TResponseInputItem],
    trace_id: str = None,
    save: bool = False,
) -> TaskRecorder

Entrypoint for running the agent

Parameters:

Name Type Description Default
trace_id str

str to identify the run

None
save bool

whether to use history (use input_items)

False
Source code in utu/agents/simple_agent.py
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
async def run(
    self, input: str | list[TResponseInputItem], trace_id: str = None, save: bool = False
) -> TaskRecorder:
    """Entrypoint for running the agent

    Args:
        trace_id: str to identify the run
        save: whether to use history (use `input_items`)
    """
    if not self._initialized:
        await self.build(trace_id)
    trace_id = trace_id or AgentsUtils.gen_trace_id()
    logger.info(f"> trace_id: {trace_id}")

    if isinstance(input, str):
        input = self.input_items + [{"content": input, "role": "user"}]
    run_kwargs = self._prepare_run_kwargs(input)
    if AgentsUtils.get_current_trace():
        run_result = await Runner.run(**run_kwargs)
    else:
        with trace(workflow_name="simple_agent", trace_id=trace_id):
            run_result = await Runner.run(**run_kwargs)

    task_recorder = TaskRecorder(input, trace_id)
    task_recorder.add_run_result(run_result)
    task_recorder.set_final_output(run_result.final_output)
    if save:
        self.input_items = run_result.to_input_list()
        self.current_agent = run_result.last_agent  # NOTE: acturally, there are only one agent in SimpleAgent
    return task_recorder

run_streamed

run_streamed(
    input: str | list[TResponseInputItem],
    trace_id: str = None,
) -> RunResultStreaming

Entrypoint for running the agent streamly

Notes
  • do not support save option for now

Parameters:

Name Type Description Default
trace_id str

str to identify the run

None
Source code in utu/agents/simple_agent.py
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
def run_streamed(self, input: str | list[TResponseInputItem], trace_id: str = None) -> RunResultStreaming:
    """Entrypoint for running the agent streamly

    Notes:
        - do not support `save` option for now

    Args:
        trace_id: str to identify the run
    """
    if not self._initialized:
        raise RuntimeError("Agent is not initialized. Please call `build` first.")
    trace_id = trace_id or AgentsUtils.gen_trace_id()
    logger.info(f"> trace_id: {trace_id}")

    run_kwargs = self._prepare_run_kwargs(input)
    if AgentsUtils.get_current_trace():
        return Runner.run_streamed(**run_kwargs)
    else:
        with trace(workflow_name="simple_agent", trace_id=trace_id):
            return Runner.run_streamed(**run_kwargs)