def step(self):
        """Run a single step of the reasoning engine."""
        status = AgentStatus.ERROR
        temp_messages = []
        max_tries = 1
        tries = 0

        while status != AgentStatus.SUCCESS:
            if self.stop_flag:
                break

            tries += 1
            if tries > max_tries:
                break
            print("-" * 40, "Context", "-" * 40)
            print(
                [message.to_llm_msg() for message in self.session.reasoning_context],
                "\n\n",
            )
            llm_response: LLMResponse = self.llm.chat_completions(
                messages=[
                    message.to_llm_msg() for message in self.session.reasoning_context
                ]
                + temp_messages,
                tools=[agent.to_llm_format() for agent in self.agents],
            )
            logger.info(f"LLM Response: {llm_response}")

            if not llm_response.status:
                self.output_message.content.append(
                    TextContent(
                        text=llm_response.content,
                        status=MsgStatus.error,
                        status_message="Error in reasoning",
                        agent_name="assistant",
                    )
                )
                self.output_message.actions.append("Failed to reason the message")
                self.output_message.status = MsgStatus.error
                self.output_message.publish()
                self.stop()
                break

            if llm_response.tool_calls:
                if self.summary_content:
                    self.remove_summary_content()

                self.session.reasoning_context.append(
                    ContextMessage(
                        content=llm_response.content,
                        tool_calls=llm_response.tool_calls,
                        role=RoleTypes.assistant,
                    )
                )
                for tool_call in llm_response.tool_calls:
                    agent_response: AgentResponse = self.run_agent(
                        tool_call["tool"]["name"],
                        **tool_call["tool"]["arguments"],
                    )
                    if agent_response.status == AgentStatus.ERROR:
                        self.failed_agents.append(tool_call["tool"]["name"])
                    self.session.reasoning_context.append(
                        ContextMessage(
                            content=agent_response.__str__(),
                            tool_call_id=tool_call["id"],
                            role=RoleTypes.tool,
                        )
                    )
                    print("-" * 40, "Agent Response", "-" * 40)
                    print(agent_response, "\n\n")
                    status = agent_response.status

            if not self.summary_content:
                self.add_summary_content()

            if (
                llm_response.finish_reason == "stop"
                or llm_response.finish_reason == "end_turn"
                or self.iterations == 0
            ):
                self.session.reasoning_context.append(
                    ContextMessage(
                        content=llm_response.content,
                        role=RoleTypes.assistant,
                    )
                )
                if self.iterations == self.max_iterations - 1:
                    # Direct response case
                    self.summary_content.status_message = "Here is the response"
                    self.summary_content.text = llm_response.content
                    self.summary_content.status = MsgStatus.success
                else:
                    self.session.reasoning_context.append(
                        ContextMessage(
                            content=SUMMARIZATION_PROMPT.format(
                                query=self.input_message.content
                            ),
                            role=RoleTypes.system,
                        )
                    )
                    summary_response = self.llm.chat_completions(
                        messages=[
                            message.to_llm_msg()
                            for message in self.get_current_run_context()
                        ]
                    )
                    self.session.reasoning_context.pop()
                    self.summary_content.text = summary_response.content
                    if self.failed_agents:
                        self.summary_content.status = MsgStatus.error
                    else:
                        self.summary_content.status = MsgStatus.success
                    self.summary_content.status_message = "Final Cut"
                self.output_message.status = MsgStatus.success
                self.output_message.publish()
                print("-" * 40, "Stopping", "-" * 40)
                self.stop()
                break
Run a single step of the reasoning engine.
backend/director/core/reasoning.py