#!/usr/bin/env python
# coding=utf-8

# Copyright 2024 The HuggingFace Inc. team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import importlib
import json
import os
import tempfile
import textwrap
import time
import warnings
from abc import ABC, abstractmethod
from collections.abc import Callable, Generator
from dataclasses import dataclass
from logging import getLogger
from pathlib import Path
from typing import TYPE_CHECKING, Any, Dict, Literal, Tuple, Type, TypeAlias, TypedDict, Union, get_origin, get_args
from textwrap import dedent
from pydantic import BaseModel
from concurrent.futures import Future, ProcessPoolExecutor, as_completed

import yaml
from huggingface_hub import create_repo, metadata_update, snapshot_download, upload_folder
from jinja2 import StrictUndefined, Template
from rich.console import Group
from rich.live import Live
from rich.markdown import Markdown
from rich.rule import Rule
from rich.text import Text


if TYPE_CHECKING:
    import PIL.Image

from .agent_types import handle_agent_output_types, AgentText
from .local_python_executor import BASE_BUILTIN_MODULES, LocalPythonExecutor, PythonExecutor, fix_final_answer_code, CodeOutput
from .memory import (
    ActionStep,
    AgentMemory,
    CallbackRegistry,
    FinalAnswerStep,
    MemoryStep,
    PlanningStep,
    SystemPromptStep,
    TaskStep,
    Timing,
    TokenUsage,
    ToolCall,
)
from .models import (
    CODEAGENT_RESPONSE_FORMAT,
    ChatMessage,
    ChatMessageStreamDelta,
    ChatMessageToolCall,
    MessageRole,
    Model,
)
from .monitoring import (
    YELLOW_HEX,
    AgentLogger,
    LogLevel,
    Monitor,
)
from .remote_executors import DockerExecutor, E2BExecutor, ModalExecutor, WasmExecutor
from .tools import BaseTool, Tool, FinalAnswerTool
from .utils import (
    AgentError,
    AgentExecutionError,
    AgentGenerationError,
    AgentMaxStepsError,
    AgentParsingError,
    BASIC_DATA_TYPES,
    create_agent_gradio_app_template,
    extract_code_from_text,
    is_valid_name,
    make_init_file,
    parse_code_blobs,
    parse_several_code_blobs,
    truncate_content,
    get_fields_info,
)


logger = getLogger(__name__)


def populate_template(template: str, variables: dict[str, Any]) -> str:
    compiled_template = Template(template, undefined=StrictUndefined)
    try:
        return compiled_template.render(**variables)
    except Exception as e:
        raise Exception(f"Error during jinja template rendering: {type(e).__name__}: {e}") from None


@dataclass
class ActionOutput:
    output: Any
    is_final_answer: bool


@dataclass
class ToolOutput:
    id: str
    output: Any
    is_final_answer: bool
    observation: str
    tool_call: ToolCall


class PlanningPromptTemplate(TypedDict):
    """
    Prompt templates for the planning step.

    Args:
        plan (`str`): Initial plan prompt.
        update_plan_pre_messages (`str`): Update plan pre-messages prompt.
        update_plan_post_messages (`str`): Update plan post-messages prompt.
    """

    initial_plan: str
    update_plan_pre_messages: str
    update_plan_post_messages: str


class ManagedAgentPromptTemplate(TypedDict):
    """
    Prompt templates for the managed agent.

    Args:
        task (`str`): Task prompt.
        report (`str`): Report prompt.
    """

    task: str
    report: str


class FinalAnswerPromptTemplate(TypedDict):
    """
    Prompt templates for the final answer.

    Args:
        pre_messages (`str`): Pre-messages prompt.
        post_messages (`str`): Post-messages prompt.
    """

    pre_messages: str
    post_messages: str


class PromptTemplates(TypedDict):
    """
    Prompt templates for the agent.

    Args:
        system_prompt (`str`): System prompt.
        planning ([`~agents.PlanningPromptTemplate`]): Planning prompt templates.
        managed_agent ([`~agents.ManagedAgentPromptTemplate`]): Managed agent prompt templates.
        final_answer ([`~agents.FinalAnswerPromptTemplate`]): Final answer prompt templates.
    """

    system_prompt: str
    planning: PlanningPromptTemplate
    managed_agent: ManagedAgentPromptTemplate
    final_answer: FinalAnswerPromptTemplate


EMPTY_PROMPT_TEMPLATES = PromptTemplates(
    system_prompt="",
    planning=PlanningPromptTemplate(
        initial_plan="",
        update_plan_pre_messages="",
        update_plan_post_messages="",
    ),
    managed_agent=ManagedAgentPromptTemplate(task="", report=""),
    final_answer=FinalAnswerPromptTemplate(pre_messages="", post_messages=""),
)


@dataclass
class RunResult:
    """Holds extended information about an agent run.

    Attributes:
        output (Any | None): The final output of the agent run, if available.
        state (Literal["success", "max_steps_error"]): The final state of the agent after the run.
        steps (list[dict]): The agent's memory, as a list of steps.
        token_usage (TokenUsage | None): Count of tokens used during the run.
        timing (Timing): Timing details of the agent run: start time, end time, duration.
        messages (list[dict]): The agent's memory, as a list of messages.
            <Deprecated version="1.22.0">
            Parameter 'messages' is deprecated and will be removed in version 1.25. Please use 'steps' instead.
            </Deprecated>
    """

    output: Any | None
    state: Literal["success", "max_steps_error"]
    steps: list[dict]
    token_usage: TokenUsage | None
    timing: Timing

    def __init__(self, output=None, state=None, steps=None, token_usage=None, timing=None, messages=None):
        # Handle deprecated 'messages' parameter
        if messages is not None:
            if steps is not None:
                raise ValueError("Cannot specify both 'messages' and 'steps' parameters. Use 'steps' instead.")
            warnings.warn(
                "Parameter 'messages' is deprecated and will be removed in version 1.25. Please use 'steps' instead.",
                FutureWarning,
                stacklevel=2,
            )
            steps = messages

        # Initialize with dataclass fields
        self.output = output
        self.state = state
        self.steps = steps
        self.token_usage = token_usage
        self.timing = timing

    @property
    def messages(self):
        """Backward compatibility property that returns steps."""
        warnings.warn(
            "Parameter 'messages' is deprecated and will be removed in version 1.25. Please use 'steps' instead.",
            FutureWarning,
            stacklevel=2,
        )
        return self.steps

    def dict(self):
        return {
            "output": self.output,
            "state": self.state,
            "steps": self.steps,
            "token_usage": self.token_usage.dict() if self.token_usage is not None else None,
            "timing": self.timing.dict(),
        }


StreamEvent: TypeAlias = Union[
    ChatMessageStreamDelta,
    ChatMessageToolCall,
    ActionOutput,
    ToolCall,
    ToolOutput,
    PlanningStep,
    ActionStep,
    FinalAnswerStep,
]


class MultiStepAgent(ABC):
    """
    Agent class that solves the given task step by step, using the ReAct framework:
    While the objective is not reached, the agent will perform a cycle of action (given by the LLM) and observation (obtained from the environment).

    Args:
        tools (`list[Tool]`): [`Tool`]s that the agent can use.
        model (`Callable[[list[dict[str, str]]], ChatMessage]`): Model that will generate the agent's actions.
        prompt_templates ([`~agents.PromptTemplates`], *optional*): Prompt templates.
        instructions (`Dict[str, str]`, *optional*): Custom instructions for the agent, will be inserted in the system prompt.
        input_schema (`Any`, *optional*): Schema for validating structured input.
            If provided, agent will only accept structured input matching this schema.
        output_schema (`Any`, *optional*): Schema for validating structured output.
            If provided, agent will ensure output matches this schema.
        max_steps (`int`, default `20`): Maximum number of steps the agent can take to solve the task.
        verbosity_level (`LogLevel`, default `LogLevel.INFO`): Level of verbosity of the agent's logs.
        managed_agents (`list`, *optional*): Managed agents that the agent can call.
        step_callbacks (`list[Callable]` | `dict[Type[MemoryStep], Callable | list[Callable]]`, *optional*): Callbacks that will be called at each step.
        planning_interval (`int`, *optional*): Interval at which the agent will run a planning step.
        name (`str`, *optional*): Necessary for a managed agent only - the name by which this agent can be called.
        description (`str`, *optional*): Necessary for a managed agent only - the description of this agent.
        provide_run_summary (`bool`, *optional*): Whether to provide a run summary when called as a managed agent.
        final_answer_checks (`list[Callable]`, *optional*): List of validation functions to run before accepting a final answer.
            Each function should:
            - Take the final answer and the agent's memory as arguments.
            - Return a boolean indicating whether the final answer is valid.
        return_full_result (`bool`, default `False`): Whether to return the full [`RunResult`] object or just the final answer output from the agent run.
    """

    def __init__(
        self,
        tools: list[Tool],
        model: Model,
        prompt_templates: PromptTemplates | None = None,
        instructions: dict[str, str] | None = None,
        input_schema: Any = None,
        output_schema: Any = None,
        max_steps: int = 20,
        verbosity_level: LogLevel = LogLevel.INFO,
        managed_agents: list | None = None,
        step_callbacks: list[Callable] | dict[Type[MemoryStep], Callable | list[Callable]] | None = None,
        planning_interval: int | None = None,
        name: str | None = None,
        description: str | None = None,
        provide_run_summary: bool = False,
        final_answer_checks: list[Callable] | None = None,
        return_full_result: bool = False,
        logger: AgentLogger | None = None,
    ):
        self.agent_name = self.__class__.__name__
        self.model = model
        self.prompt_templates = prompt_templates or EMPTY_PROMPT_TEMPLATES
        if prompt_templates is not None:
            missing_keys = set(EMPTY_PROMPT_TEMPLATES.keys()) - set(prompt_templates.keys())
            assert not missing_keys, (
                f"Some prompt templates are missing from your custom `prompt_templates`: {missing_keys}"
            )
            for key, value in EMPTY_PROMPT_TEMPLATES.items():
                if isinstance(value, dict):
                    for subkey in value.keys():
                        assert key in prompt_templates.keys() and (subkey in prompt_templates[key].keys()), (
                            f"Some prompt templates are missing from your custom `prompt_templates`: {subkey} under {key}"
                        )

        self.max_steps = max_steps
        self.step_number = 0
        self.planning_interval = planning_interval
        self.state: dict[str, Any] = {}
        self.name = self._validate_name(name)
        self.description = description
        self.provide_run_summary = provide_run_summary
        self.final_answer_checks = final_answer_checks if final_answer_checks is not None else []
        self.return_full_result = return_full_result
        self.instructions = instructions

        if input_schema is not None and not isinstance(input_schema, type):
            raise TypeError(f"Input_schema must be a Pydantic BaseModel subclass or one of the allowed built-in basic data types {BASIC_DATA_TYPES}, not {type(input_schema)}")
        if input_schema is not None and not (issubclass(input_schema, BaseModel) or (input_schema in BASIC_DATA_TYPES)):
            raise TypeError(f"Input_schema must be a Pydantic BaseModel subclass or one of the allowed built-in basic data types {BASIC_DATA_TYPES}, not {input_schema}")
        if output_schema is not None and not isinstance(output_schema, type):
            raise TypeError(f"Output_schema must be a Pydantic BaseModel subclass or one of the allowed built-in basic data types {BASIC_DATA_TYPES}, not {type(output_schema)}")
        if output_schema is not None and not (issubclass(output_schema, BaseModel) or (output_schema in BASIC_DATA_TYPES)):
            raise TypeError(f"Output_schema must be a Pydantic BaseModel subclass or one of the allowed built-in basic data types {BASIC_DATA_TYPES}, not {output_schema}")
        self.input_schema = input_schema
        self.output_schema = output_schema

        self._setup_managed_agents(managed_agents)
        self._setup_tools(tools)
        self._validate_tools_and_managed_agents(tools, managed_agents)

        self.task: str | None = None
        self.memory = AgentMemory(self.system_prompt)

        if logger is None:
            self.logger = AgentLogger(level=verbosity_level)
        else:
            self.logger = logger

        self.monitor = Monitor(self.model, self.logger)
        self._setup_step_callbacks(step_callbacks)

        if self.output_schema:
            if self.instructions is None:
                self.instructions = {}
            if "output_schema_instruction" not in self.instructions:
                self.instructions["output_schema_instruction"] = self.get_output_instructions()
            self.logger.log(
                Text(
                    f"Output schema instruction added:\n{self.instructions['output_schema_instruction']}",
                    style=f"bold {YELLOW_HEX}",
                ),
                level=LogLevel.INFO,
            )

    @property
    def system_prompt(self) -> str:
        return self.initialize_system_prompt()

    @system_prompt.setter
    def system_prompt(self, value: str):
        raise AttributeError(
            """The 'system_prompt' property is read-only. Use 'self.prompt_templates["system_prompt"]' instead."""
        )

    def _validate_name(self, name: str | None) -> str | None:
        if name is not None and not is_valid_name(name):
            raise ValueError(f"Agent name '{name}' must be a valid Python identifier and not a reserved keyword.")
        return name

    def _setup_managed_agents(self, managed_agents: list | None = None) -> None:
        """Setup managed agents with proper logging."""
        self.managed_agents = {}
        if managed_agents:
            assert all(agent.name and agent.description for agent in managed_agents), (
                "All managed agents need both a name and a description!"
            )
            self.managed_agents = {agent.name: agent for agent in managed_agents}
            # Ensure managed agents can be called as tools by the model: set their inputs and output_type
            for agent in self.managed_agents.values():
                agent.inputs = {
                    "task": {"type": "string", "description": "Long detailed description of the task."},
                    "additional_args": {
                        "type": "object",
                        "description": "Dictionary of extra inputs to pass to the managed agent, e.g. images, dataframes, or any other contextual data it may need.",
                    },
                }
                agent.output_type = "string"

    def _setup_tools(self, tools):
        assert all(isinstance(tool, BaseTool) for tool in tools), (
            "All elements must be instance of BaseTool (or a subclass)"
        )
        self.tools = {tool.name: tool for tool in tools}
        self.tools.setdefault("final_answer", FinalAnswerTool())

    def _validate_tools_and_managed_agents(self, tools, managed_agents):
        tool_and_managed_agent_names = [tool.name for tool in tools]
        if managed_agents is not None:
            tool_and_managed_agent_names += [agent.name for agent in managed_agents]
        if self.name:
            tool_and_managed_agent_names.append(self.name)
        if len(tool_and_managed_agent_names) != len(set(tool_and_managed_agent_names)):
            raise ValueError(
                "Each tool or managed_agent should have a unique name! You passed these duplicate names: "
                f"{[name for name in tool_and_managed_agent_names if tool_and_managed_agent_names.count(name) > 1]}"
            )

    def _setup_step_callbacks(self, step_callbacks):
        # Initialize step callbacks registry
        self.step_callbacks = CallbackRegistry()
        if step_callbacks:
            # Register callbacks list only for ActionStep for backward compatibility
            if isinstance(step_callbacks, list):
                for callback in step_callbacks:
                    self.step_callbacks.register(ActionStep, callback)
            # Register callbacks dict for specific step classes
            elif isinstance(step_callbacks, dict):
                for step_cls, callbacks in step_callbacks.items():
                    if not isinstance(callbacks, list):
                        callbacks = [callbacks]
                    for callback in callbacks:
                        self.step_callbacks.register(step_cls, callback)
            else:
                raise ValueError("step_callbacks must be a list or a dict")
        # Register monitor update_metrics only for ActionStep for backward compatibility
        self.step_callbacks.register(ActionStep, self.monitor.update_metrics)

    def run(
        self,
        task: str,
        stream: bool = False,
        reset: bool = True,
        images: list["PIL.Image.Image"] | None = None,
        additional_args: dict | None = None,
        max_steps: int | None = None,
        return_full_result: bool | None = None,
        structured_input: Any = None,
    ) -> Any | RunResult:
        """
        Run the agent for the given task.

        Args:
            task (`str`): Task to perform.
            stream (`bool`): Whether to run in streaming mode.
                If `True`, returns a generator that yields each step as it is executed. You must iterate over this generator to process the individual steps (e.g., using a for loop or `next()`).
                If `False`, executes all steps internally and returns only the final answer after completion.
            reset (`bool`): Whether to reset the conversation or keep it going from previous run.
            images (`list[PIL.Image.Image]`, *optional*): Image(s) objects.
            additional_args (`dict`, *optional*): Any other variables that you want to pass to the agent run, for instance images or dataframes. Give them clear names!
            max_steps (`int`, *optional*): Maximum number of steps the agent can take to solve the task. if not provided, will use the agent's default value.
            return_full_result (`bool`, *optional*): Whether to return the full [`RunResult`] object or just the final answer output.
                If `None` (default), the agent's `self.return_full_result` setting is used.
            structured_input (Any, *optional*): Structured input data that matches the agent's input_schema. 
                If input_schema is defined, this must be provided and match the schema.
        Example:
        ```py
        from hamlet import CodeAgent
        agent = CodeAgent(tools=[])
        agent.run("What is the result of 2 power 3.7384?")
        ```
        """
        max_steps = max_steps or self.max_steps
        self.task = task
        self.interrupt_switch = False
        self.validate_input(structured_input)

        if additional_args:
            self.state.update(additional_args)
            self.task += dedent(
                f"""
                You have been provided with these additional arguments, that you can access directly using the keys as variables: 
                {str(additional_args)}.
                """
            )

        self.memory.system_prompt = SystemPromptStep(system_prompt=self.system_prompt)
        if reset:
            self.memory.reset()
            self.monitor.reset()

        self.logger.log_task(
            content=self.task.strip(),
            subtitle=f"{type(self.model).__name__} - {(self.model.model_id if hasattr(self.model, 'model_id') else '')}",
            level=LogLevel.INFO,
            title=self.name if hasattr(self, "name") else None,
        )
        self.memory.steps.append(TaskStep(task=self.task, task_images=images))

        if getattr(self, "python_executor", None):
            self.python_executor.send_variables(variables=self.state)
            self.python_executor.send_tools({**self.tools, **self.managed_agents})

        if stream:
            # The steps are returned as they are executed through a generator to iterate on.
            return self._run_stream(task=self.task, max_steps=max_steps, images=images)

        run_start_time = time.time()
        steps = list(self._run_stream(task=self.task, max_steps=max_steps, images=images))

        # Outputs are returned only at the end. We only look at the last step.
        assert isinstance(steps[-1], FinalAnswerStep)
        output = steps[-1].output

        return_full_result = return_full_result if return_full_result is not None else self.return_full_result
        if return_full_result:
            total_input_tokens = 0
            total_output_tokens = 0
            correct_token_usage = True
            for step in self.memory.steps:
                if isinstance(step, (ActionStep, PlanningStep)):
                    if step.token_usage is None:
                        correct_token_usage = False
                        break
                    else:
                        total_input_tokens += step.token_usage.input_tokens
                        total_output_tokens += step.token_usage.output_tokens
            if correct_token_usage:
                token_usage = TokenUsage(input_tokens=total_input_tokens, output_tokens=total_output_tokens)
            else:
                token_usage = None

            if self.memory.steps and isinstance(getattr(self.memory.steps[-1], "error", None), AgentMaxStepsError):
                state = "max_steps_error"
            else:
                state = "success"

            step_dicts = self.memory.get_full_steps()

            return RunResult(
                output=output,
                token_usage=token_usage,
                steps=step_dicts,
                timing=Timing(start_time=run_start_time, end_time=time.time()),
                state=state,
            )

        return output

    def _run_stream(
        self, task: str, max_steps: int, images: list["PIL.Image.Image"] | None = None
    ) -> Generator[ActionStep | PlanningStep | FinalAnswerStep | ChatMessageStreamDelta]:
        self.step_number = 1
        returned_final_answer = False
        while not returned_final_answer and self.step_number <= max_steps:
            if self.interrupt_switch:
                raise AgentError("Agent interrupted.", self.logger)

            # Run a planning step if scheduled
            if self.planning_interval is not None and (
                self.step_number == 1 or (self.step_number - 1) % self.planning_interval == 0
            ):
                planning_start_time = time.time()
                planning_step = None
                for element in self._generate_planning_step(
                    task, is_first_step=len(self.memory.steps) == 1, step=self.step_number
                ):  # Don't use the attribute step_number here, because there can be steps from previous runs
                    yield element
                    planning_step = element
                assert isinstance(planning_step, PlanningStep)  # Last yielded element should be a PlanningStep
                planning_end_time = time.time()
                planning_step.timing = Timing(
                    start_time=planning_start_time,
                    end_time=planning_end_time,
                )
                self._finalize_step(planning_step)
                self.memory.steps.append(planning_step)

            # Start action step!
            action_step_start_time = time.time()
            action_step = ActionStep(
                step_number=self.step_number,
                timing=Timing(start_time=action_step_start_time),
                observations_images=images,
            )
            self.logger.log_rule(f"Step {self.step_number}", level=LogLevel.INFO)
            try:
                for output in self._step_stream(action_step):
                    # Yield all
                    yield output

                    if isinstance(output, ActionOutput) and output.is_final_answer:
                        final_answer = output.output
                        self.logger.log(
                            Text(f"Final answer:\n{str(final_answer)}", style=f"bold {YELLOW_HEX}"),
                            level=LogLevel.INFO,
                        )

                        if self.final_answer_checks or self.output_schema:
                            final_answer = self._validate_final_answer(final_answer)
                        returned_final_answer = True
                        action_step.is_final_answer = True

            except AgentGenerationError as e:
                # Agent generation errors are not caused by a Model error but an implementation error: so we should raise them and exit.
                raise e
            except AgentError as e:
                # Other AgentError types are caused by the Model, so we should log them and iterate.
                action_step.error = e
            finally:
                self._finalize_step(action_step)
                self.memory.steps.append(action_step)
                yield action_step
                self.step_number += 1

        if not returned_final_answer and self.step_number == max_steps + 1:
            final_answer, final_memory_step = self._handle_max_steps_reached(task)
            if self.output_schema is not None:
                if issubclass(self.output_schema, BaseModel):
                    if isinstance(final_answer, dict):
                        try:
                            final_answer = self.output_schema.model_validate(final_answer)
                        except Exception as e:
                            raise AgentParsingError(f"Reached max steps. The agent generated a final answer but validation failed for the final output: {e}", self.logger) from None
                    else:
                        raise AgentParsingError(f"Reached max steps. The agent generated a final answer but validation failed for the final output. Output should be of type dict, got {type(final_answer)}", self.logger)
                else:
                    if not isinstance(final_answer, self.output_schema):
                        raise AgentParsingError(f"Reached max steps. The agent generated a final answer but validation failed for the final output. Output should be of type {self.output_schema}, got {type(final_answer)}", self.logger)
            try:
                if isinstance(final_answer, BaseModel):
                    self.logger.log_markdown(
                        content=final_answer.model_dump_json(),
                        title="Final answer given by LLM when max steps reached:",
                        level=LogLevel.DEBUG,
                        style="green"
                    )
                else:
                    self.logger.log_markdown(
                        content=str(final_answer),
                        title="Final answer given by LLM when max steps reached:",
                        level=LogLevel.DEBUG,
                        style="green"
                    )
            except Exception:
                pass
            yield final_memory_step

        if self.output_schema is not None:
            yield FinalAnswerStep(final_answer)
        else:
            yield FinalAnswerStep(handle_agent_output_types(final_answer))

    def _validate_final_answer(self, final_answer: Any):
        validate_passed, details = self.validate_output(final_answer)
        if not validate_passed:
            raise AgentError(details, self.logger)
        for check_function in self.final_answer_checks:
            try:
                assert check_function(final_answer, self.memory)
            except Exception as e:
                raise AgentError(f"Check {check_function.__name__} failed with error: {e}", self.logger) from None
        return details

    def _finalize_step(self, memory_step: ActionStep | PlanningStep):
        memory_step.timing.end_time = time.time()
        self.step_callbacks.callback(memory_step, agent=self)

    def _handle_max_steps_reached(self, task: str) -> Any:
        action_step_start_time = time.time()
        try:
            final_answer = self.provide_final_answer(task)
            self.logger.log_markdown(
                content=final_answer.content,
                title="Reached max steps. The final answer generated by the model is:",
                level=LogLevel.DEBUG,
            )
        except Exception as e:
            raise AgentGenerationError(f"Reached max steps. And an error occurred when generating final answer: {e}", self.logger) from None
        try:
            final_code_action = parse_code_blobs(final_answer.content, self.code_block_tags)
        except Exception as e:
            raise AgentParsingError(f"Reached max steps. And an error occurred when parsing code blocks from final answer: {e}", self.logger) from None
        if "final_answer(" not in final_code_action:
            raise AgentParsingError("Reached max steps. And call of FinalAnswerTool is not found in the model output.", self.logger)
        try:
            final_output = self.python_executor(final_code_action).output
        except Exception as e:
            raise AgentExecutionError(f"Reached max steps. And an error occurred when executing final answer code: {e}", self.logger) from None
        final_memory_step = ActionStep(
            step_number=self.step_number,
            error=AgentMaxStepsError("Reached max steps.", self.logger),
            timing=Timing(start_time=action_step_start_time, end_time=time.time()),
            token_usage=final_answer.token_usage,
        )
        final_memory_step.action_output = final_answer.content
        self._finalize_step(final_memory_step)
        self.memory.steps.append(final_memory_step)
        return final_output, final_memory_step

    def _generate_planning_step(
        self, task, is_first_step: bool, step: int
    ) -> Generator[ChatMessageStreamDelta | PlanningStep]:
        start_time = time.time()
        if is_first_step:
            input_messages = [
                ChatMessage(
                    role=MessageRole.USER,
                    content=[
                        {
                            "type": "text",
                            "text": populate_template(
                                self.prompt_templates["planning"]["initial_plan"],
                                variables={"task": task, 
                                           "tools": self.tools, 
                                           "managed_agents": self.managed_agents,
                                           "code_block_opening_tag": self.code_block_tags[0],
                                           "code_block_closing_tag": self.code_block_tags[1]
                                           },
                            ),
                        }
                    ],
                )
            ]

            plan_message = self.model.generate(input_messages, stop_sequences=["<end_plan>"])
            plan_message_content = plan_message.content
            input_tokens, output_tokens = 0, 0
            if plan_message.token_usage:
                input_tokens = plan_message.token_usage.input_tokens
                output_tokens = plan_message.token_usage.output_tokens
            plan = textwrap.dedent(
                f"""Here are the facts I know and the plan of action that I will follow to solve the task:\n```\n{plan_message_content}\n```"""
            )
        else:
            # Summary mode removes the system prompt and previous planning messages output by the model.
            # Removing previous planning messages avoids influencing too much the new plan.
            memory_messages = self.write_memory_to_messages(summary_mode=True)
            plan_update_pre = ChatMessage(
                role=MessageRole.SYSTEM,
                content=[
                    {
                        "type": "text",
                        "text": populate_template(
                            self.prompt_templates["planning"]["update_plan_pre_messages"], 
                            variables={"task": task,
                                       "code_block_opening_tag": self.code_block_tags[0],
                                       "code_block_closing_tag": self.code_block_tags[1]}
                        ),
                    }
                ],
            )
            plan_update_post = ChatMessage(
                role=MessageRole.USER,
                content=[
                    {
                        "type": "text",
                        "text": populate_template(
                            self.prompt_templates["planning"]["update_plan_post_messages"],
                            variables={
                                "task": task,
                                "tools": self.tools,
                                "managed_agents": self.managed_agents,
                                "remaining_steps": (self.max_steps - step),
                            },
                        ),
                    }
                ],
            )
            input_messages = [plan_update_pre] + memory_messages + [plan_update_post]

            plan_message = self.model.generate(input_messages, stop_sequences=["<end_plan>"])
            plan_message_content = plan_message.content
            input_tokens, output_tokens = 0, 0
            if plan_message.token_usage:
                input_tokens = plan_message.token_usage.input_tokens
                output_tokens = plan_message.token_usage.output_tokens
            plan = textwrap.dedent(
                f"""I still need to solve the task I was given:\n```\n{self.task}\n```\n\nHere are the facts I know and my new/updated plan of action to solve the task:\n```\n{plan_message_content}\n```"""
            )
        log_headline = "Initial plan" if is_first_step else "Updated plan"
        self.logger.log(Rule(f"[bold]{log_headline}", style="orange"), Text(plan), level=LogLevel.INFO)
        yield PlanningStep(
            model_input_messages=input_messages,
            plan=plan,
            model_output_message=ChatMessage(role=MessageRole.ASSISTANT, content=plan_message_content),
            token_usage=TokenUsage(input_tokens=input_tokens, output_tokens=output_tokens),
            timing=Timing(start_time=start_time, end_time=time.time()),
        )

    @abstractmethod
    def initialize_system_prompt(self) -> str:
        """To be implemented in child classes"""
        ...

    def interrupt(self):
        """Interrupts the agent execution."""
        self.interrupt_switch = True

    def write_memory_to_messages(
        self,
        summary_mode: bool = False,
    ) -> list[ChatMessage]:
        """
        Reads past llm_outputs, actions, and observations or errors from the memory into a series of messages
        that can be used as input to the LLM. Adds a number of keywords (such as PLAN, error, etc) to help
        the LLM.
        """
        messages = self.memory.system_prompt.to_messages(summary_mode=summary_mode)
        for memory_step in self.memory.steps:
            messages.extend(memory_step.to_messages(summary_mode=summary_mode))
        return messages

    def get_output_instructions(self) -> str:
        """
        Returns instructions for the agent to produce output matching the output schema.
        """
        if self.output_schema:
            if hasattr(self.output_schema, "__name__") and hasattr(self.output_schema, "model_fields"):
                # schema_name = self.output_schema.__name__
                fields_info = get_fields_info(self.output_schema)
                fields_info_str = "\n".join(fields_info)
                structured_output_instructions = dedent(
                    f"""
                    Strictly follow the structured output instructions below:
                    Your final answer must be outputted in the dict format. Please only output valid dict objects, adhere to Python syntax, use None instead of null, use True/False instead of true/false, and do not include any additional text.
                    Ensuring all necessary keys are included and have the values of the correct type:
                    {fields_info_str}
                    REMEMBER to use final_answer(...) to return your final answer to the task. It should be opened with {self.code_block_tags[0]} and closed with {self.code_block_tags[1]}. i.e.,
                    {self.code_block_tags[0]}
                    final_answer(
                    your_final_answer_here
                    )
                    {self.code_block_tags[1]}
                    """
                )
            else:
                type_name = self.output_schema.__name__ if hasattr(self.output_schema, "__name__") else str(self.output_schema)
                structured_output_instructions = dedent(
                    f"""
                    Strictly follow the structured output instructions below:
                    Your final answer must be outputted as an object of type {type_name}. 
                    Please output only a valid {type_name} object, adhere to Python syntax, use None instead of null, use True/False instead of true/false, and do not include any additional text or explanation.
                    REMEMBER to use final_answer(...) to return your final answer to the task. It should be opened with {self.code_block_tags[0]} and closed with {self.code_block_tags[1]}. i.e.,
                    {self.code_block_tags[0]}
                    final_answer(your_final_answer_here)
                    {self.code_block_tags[1]}
                    """
                )
            return structured_output_instructions
        return ""

    def validate_input(self, structured_input: Any) -> None:
        """
        Validates the structured input against the agent's input schema. 
        If the input is valid, add the structured input to the agent's python executor state. 
        If not, raises a ValueError.
        Args:
            structured_input (Any): Structured input data to validate.
        """
        if self.input_schema is not None:
            if structured_input is None:
                raise ValueError(f"structured_input is required for this agent with input_schema: {self.input_schema.__name__}")
            if issubclass(self.input_schema, BaseModel):
                try:
                    if isinstance(structured_input, self.input_schema):
                        validated_input_data = structured_input
                    else:
                        validated_input_data = self.input_schema(**structured_input)
                except Exception as e:
                    raise TypeError(f"structured_input does not match input_schema: {e}")
                validated_dict = validated_input_data.model_dump()
                self.state.update(validated_dict)
                self.task += dedent(
                    f"""
                    You have been provided with structured input data, which you can access as variables:
                    {validated_dict}
                    """
                )
            else:
                if not isinstance(structured_input, self.input_schema):
                    raise TypeError(f"structured_input must be of type {self.input_schema.__name__}, got {type(structured_input).__name__}")
                self.state["structured_input"] = structured_input
                self.task += dedent(
                    f"""
                    You have been provided with structured input data (type: {self.input_schema.__name__}), which you can access using the variable 'structured_input': {structured_input}
                    """
                )

    def validate_output(self, output: Any) -> Tuple[bool, str | None]:
        """
        Validates the output against the agent's output schema.
        Args:
            output (Any): Output data to validate.
        Returns:
            Tuple[bool, str | None]: A tuple where the first element is a boolean indicating whether the output is valid,
            and the second element is an error message if the output is not valid, or None if it is valid.
        """
        if self.output_schema is not None:
            if issubclass(self.output_schema, BaseModel):
                schema_str = get_fields_info(self.output_schema)
                if isinstance(output, dict):
                    try:
                        output = self.output_schema.model_validate(output)
                        return True, output
                    except Exception as e:
                        return False, f"Validation failed for the output. It must be a dict with necessary keys and values of correct types:\n{schema_str}"
                else:
                    return False, f"Output must be a dict with necessary keys and values of correct types:\n{schema_str}"
            else:
                if isinstance(output, self.output_schema):
                    return True, output
                else:
                    return False, f"Output must be of type {self.output_schema.__name__}"
        return True, None

    def _step_stream(
        self, memory_step: ActionStep
    ) -> Generator[ChatMessageStreamDelta | ToolCall | ToolOutput | ActionOutput]:
        """
        Perform one step in the ReAct framework: the agent thinks, acts, and observes the result.
        Yields ChatMessageStreamDelta during the run if streaming is enabled.
        At the end, yields either None if the step is not final, or the final answer.
        """
        raise NotImplementedError("This method should be implemented in child classes")

    def step(self, memory_step: ActionStep) -> Any:
        """
        Perform one step in the ReAct framework: the agent thinks, acts, and observes the result.
        Returns either None if the step is not final, or the final answer.
        """
        return list(self._step_stream(memory_step))[-1]

    def extract_action(self, model_output: str, split_token: str) -> tuple[str, str]:
        """
        Parse action from the LLM output

        Args:
            model_output (`str`): Output of the LLM
            split_token (`str`): Separator for the action. Should match the example in the system prompt.
        """
        try:
            split = model_output.split(split_token)
            rationale, action = (
                split[-2],
                split[-1],
            )  # NOTE: using indexes starting from the end solves for when you have more than one split_token in the output
        except Exception:
            raise AgentParsingError(
                f"No '{split_token}' token provided in your output.\nYour output:\n{model_output}\n. Be sure to include an action, prefaced with '{split_token}'!",
                self.logger,
            )
        return rationale.strip(), action.strip()

    def provide_final_answer(self, task: str) -> ChatMessage:
        """
        Provide the final answer to the task, based on the logs of the agent's interactions.

        Args:
            task (`str`): Task to perform.
            images (`list[PIL.Image.Image]`, *optional*): Image(s) objects.

        Returns:
            `str`: Final answer to the task.
        """
        messages = [
            ChatMessage(
                role=MessageRole.SYSTEM,
                content=[
                    {
                        "type": "text",
                        "text": self.prompt_templates["final_answer"]["pre_messages"],
                    }
                ],
            )
        ]
        messages += self.write_memory_to_messages()[1:]

        messages.append(
            ChatMessage(
                role=MessageRole.USER,
                content=[
                    {
                        "type": "text",
                        "text": populate_template(
                            self.prompt_templates["final_answer"]["post_messages"],
                            variables={
                                "task": task,
                                "custom_instructions": self.instructions,
                                "code_block_opening_tag": self.code_block_tags[0],
                                "code_block_closing_tag": self.code_block_tags[1],
                            },
                        ),
                    }
                ],
            )
        )
        try:
            chat_message: ChatMessage = self.model.generate(messages)
            return chat_message
        except Exception as e:
            # return ChatMessage(
            #     role=MessageRole.ASSISTANT,
            #     content=[{"type": "text", "text": f"Error in generating final LLM output: {e}"}],
            # )
            raise AgentGenerationError(f"Error in generating final LLM output: {e}", self.logger)

    def visualize(self):
        """Creates a rich tree visualization of the agent's structure."""
        self.logger.visualize_agent_tree(self)

    def replay(self, detailed: bool = False):
        """Prints a pretty replay of the agent's steps.

        Args:
            detailed (bool, optional): If True, also displays the memory at each step. Defaults to False.
                Careful: will increase log length exponentially. Use only for debugging.
        """
        self.memory.replay(self.logger, detailed=detailed)

    def __call__(self, task: str, **kwargs):
        """Adds additional prompting for the managed agent, runs it, and wraps the output.
        This method is called only by a managed agent.
        """
        full_task = populate_template(
            self.prompt_templates["managed_agent"]["task"],
            variables=dict(name=self.name, task=task),
        )
        result = self.run(full_task, **kwargs)
        if isinstance(result, RunResult):
            report = result.output
        else:
            report = result
        answer = populate_template(
            self.prompt_templates["managed_agent"]["report"], variables=dict(name=self.name, final_answer=report)
        )
        if self.provide_run_summary:
            answer += "\n\nFor more detail, find below a summary of this agent's work:\n<summary_of_work>\n"
            for message in self.write_memory_to_messages(summary_mode=True):
                content = message.content
                answer += "\n" + truncate_content(str(content)) + "\n---"
            answer += "\n</summary_of_work>"
        return answer

    def save(self, output_dir: str | Path, relative_path: str | None = None):
        """
        Saves the relevant code files for your agent. This will copy the code of your agent in `output_dir` as well as autogenerate:

        - a `tools` folder containing the logic for each of the tools under `tools/{tool_name}.py`.
        - a `managed_agents` folder containing the logic for each of the managed agents.
        - an `agent.json` file containing a dictionary representing your agent.
        - a `prompt.yaml` file containing the prompt templates used by your agent.
        - an `app.py` file providing a UI for your agent when it is exported to a Space with `agent.push_to_hub()`
        - a `requirements.txt` containing the names of the modules used by your tool (as detected when inspecting its
          code)

        Args:
            output_dir (`str` or `Path`): The folder in which you want to save your agent.
        """
        make_init_file(output_dir)

        # Recursively save managed agents
        if self.managed_agents:
            make_init_file(os.path.join(output_dir, "managed_agents"))
            for agent_name, agent in self.managed_agents.items():
                agent_suffix = f"managed_agents.{agent_name}"
                if relative_path:
                    agent_suffix = relative_path + "." + agent_suffix
                agent.save(os.path.join(output_dir, "managed_agents", agent_name), relative_path=agent_suffix)

        class_name = self.__class__.__name__

        # Save tools to different .py files
        for tool in self.tools.values():
            make_init_file(os.path.join(output_dir, "tools"))
            tool.save(os.path.join(output_dir, "tools"), tool_file_name=tool.name, make_gradio_app=False)

        # Save prompts to yaml
        yaml_prompts = yaml.safe_dump(
            self.prompt_templates,
            default_style="|",  # This forces block literals for all strings
            default_flow_style=False,
            width=float("inf"),
            sort_keys=False,
            allow_unicode=True,
            indent=2,
        )

        with open(os.path.join(output_dir, "prompts.yaml"), "w", encoding="utf-8") as f:
            f.write(yaml_prompts)

        # Save agent dictionary to json
        agent_dict = self.to_dict()
        agent_dict["tools"] = [tool.name for tool in self.tools.values()]
        agent_dict["managed_agents"] = {agent.name: agent.__class__.__name__ for agent in self.managed_agents.values()}
        with open(os.path.join(output_dir, "agent.json"), "w", encoding="utf-8") as f:
            json.dump(agent_dict, f, indent=4)

        # Save requirements
        with open(os.path.join(output_dir, "requirements.txt"), "w", encoding="utf-8") as f:
            f.writelines(f"{r}\n" for r in agent_dict["requirements"])

        # Make agent.py file with Gradio UI
        agent_name = f"agent_{self.name}" if getattr(self, "name", None) else "agent"
        managed_agent_relative_path = relative_path + "." if relative_path is not None else ""
        app_template = create_agent_gradio_app_template()

        # Render the app.py file from Jinja2 template
        app_text = app_template.render(
            {
                "agent_name": agent_name,
                "class_name": class_name,
                "agent_dict": agent_dict,
                "tools": self.tools,
                "managed_agents": self.managed_agents,
                "managed_agent_relative_path": managed_agent_relative_path,
            }
        )

        with open(os.path.join(output_dir, "app.py"), "w", encoding="utf-8") as f:
            f.write(app_text + "\n")  # Append newline at the end

    def to_dict(self) -> dict[str, Any]:
        """Convert the agent to a dictionary representation.

        Returns:
            `dict`: Dictionary representation of the agent.
        """
        # TODO: handle serializing step_callbacks and final_answer_checks
        for attr in ["final_answer_checks", "step_callbacks"]:
            if getattr(self, attr, None):
                self.logger.log(f"This agent has {attr}: they will be ignored by this method.", LogLevel.INFO)

        tool_dicts = [tool.to_dict() for tool in self.tools.values()]
        tool_requirements = {req for tool in self.tools.values() for req in tool.to_dict()["requirements"]}
        managed_agents_requirements = {
            req for managed_agent in self.managed_agents.values() for req in managed_agent.to_dict()["requirements"]
        }
        requirements = tool_requirements | managed_agents_requirements
        if hasattr(self, "authorized_imports"):
            requirements.update(
                {package.split(".")[0] for package in self.authorized_imports if package not in BASE_BUILTIN_MODULES}
            )

        agent_dict = {
            "class": self.__class__.__name__,
            "tools": tool_dicts,
            "model": {
                "class": self.model.__class__.__name__,
                "data": self.model.to_dict(),
            },
            "managed_agents": [managed_agent.to_dict() for managed_agent in self.managed_agents.values()],
            "prompt_templates": self.prompt_templates,
            "max_steps": self.max_steps,
            "verbosity_level": int(self.logger.level),
            "planning_interval": self.planning_interval,
            "name": self.name,
            "description": self.description,
            "requirements": sorted(requirements),
        }
        return agent_dict

    @classmethod
    def from_dict(cls, agent_dict: dict[str, Any], **kwargs) -> "MultiStepAgent":
        """Create agent from a dictionary representation.

        Args:
            agent_dict (`dict[str, Any]`): Dictionary representation of the agent.
            **kwargs: Additional keyword arguments that will override agent_dict values.

        Returns:
            `MultiStepAgent`: Instance of the agent class.
        """
        # Load model
        model_info = agent_dict["model"]
        model_class = getattr(importlib.import_module("src.hamlet.core.models"), model_info["class"])
        model = model_class.from_dict(model_info["data"])
        # Load tools
        tools = []
        for tool_info in agent_dict["tools"]:
            tools.append(Tool.from_code(tool_info["code"]))
        # Load managed agents
        managed_agents = []
        for managed_agent_dict in agent_dict["managed_agents"]:
            agent_class = getattr(importlib.import_module("src.hamlet.core.agents"), managed_agent_dict["class"])
            managed_agent = agent_class.from_dict(managed_agent_dict, **kwargs)
            managed_agents.append(managed_agent)
        # Extract base agent parameters
        agent_args = {
            "model": model,
            "tools": tools,
            "managed_agents": managed_agents,
            "prompt_templates": agent_dict.get("prompt_templates"),
            "max_steps": agent_dict.get("max_steps"),
            "verbosity_level": agent_dict.get("verbosity_level"),
            "planning_interval": agent_dict.get("planning_interval"),
            "name": agent_dict.get("name"),
            "description": agent_dict.get("description"),
        }
        # Filter out None values to use defaults from __init__
        agent_args = {k: v for k, v in agent_args.items() if v is not None}
        # Update with any additional kwargs
        agent_args.update(kwargs)
        # Create agent instance
        return cls(**agent_args)

    @classmethod
    def from_hub(
        cls,
        repo_id: str,
        token: str | None = None,
        trust_remote_code: bool = False,
        **kwargs,
    ):
        """
        Loads an agent defined on the Hub.

        <Tip warning={true}>

        Loading a tool from the Hub means that you'll download the tool and execute it locally.
        ALWAYS inspect the tool you're downloading before loading it within your runtime, as you would do when
        installing a package using pip/npm/apt.

        </Tip>

        Args:
            repo_id (`str`):
                The name of the repo on the Hub where your tool is defined.
            token (`str`, *optional*):
                The token to identify you on hf.co. If unset, will use the token generated when running
                `huggingface-cli login` (stored in `~/.huggingface`).
            trust_remote_code(`bool`, *optional*, defaults to False):
                This flags marks that you understand the risk of running remote code and that you trust this tool.
                If not setting this to True, loading the tool from Hub will fail.
            kwargs (additional keyword arguments, *optional*):
                Additional keyword arguments that will be split in two: all arguments relevant to the Hub (such as
                `cache_dir`, `revision`, `subfolder`) will be used when downloading the files for your agent, and the
                others will be passed along to its init.
        """
        if not trust_remote_code:
            raise ValueError(
                "Loading an agent from Hub requires to acknowledge you trust its code: to do so, pass `trust_remote_code=True`."
            )

        # Get the agent's Hub folder.
        download_kwargs = {"token": token, "repo_type": "space"} | {
            key: kwargs.pop(key)
            for key in [
                "cache_dir",
                "force_download",
                "proxies",
                "revision",
                "local_files_only",
            ]
            if key in kwargs
        }

        download_folder = Path(snapshot_download(repo_id=repo_id, **download_kwargs))
        return cls.from_folder(download_folder, **kwargs)

    @classmethod
    def from_folder(cls, folder: str | Path, **kwargs):
        """Loads an agent from a local folder.

        Args:
            folder (`str` or `Path`): The folder where the agent is saved.
            **kwargs: Additional keyword arguments that will be passed to the agent's init.
        """
        # Load agent.json
        folder = Path(folder)
        agent_dict = json.loads((folder / "agent.json").read_text())

        # Load managed agents from their respective folders, recursively
        managed_agents = []
        for managed_agent_name, managed_agent_class_name in agent_dict["managed_agents"].items():
            agent_cls = getattr(importlib.import_module("src.hamlet.core.agents"), managed_agent_class_name)
            managed_agents.append(agent_cls.from_folder(folder / "managed_agents" / managed_agent_name))
        agent_dict["managed_agents"] = {}

        # Load tools
        tools = []
        for tool_name in agent_dict["tools"]:
            tool_code = (folder / "tools" / f"{tool_name}.py").read_text()
            tools.append({"name": tool_name, "code": tool_code})
        agent_dict["tools"] = tools

        # Add managed agents to kwargs to override the empty list in from_dict
        if managed_agents:
            kwargs["managed_agents"] = managed_agents

        return cls.from_dict(agent_dict, **kwargs)

    def push_to_hub(
        self,
        repo_id: str,
        commit_message: str = "Upload agent",
        private: bool | None = None,
        token: bool | str | None = None,
        create_pr: bool = False,
    ) -> str:
        """
        Upload the agent to the Hub.

        Parameters:
            repo_id (`str`):
                The name of the repository you want to push to. It should contain your organization name when
                pushing to a given organization.
            commit_message (`str`, *optional*, defaults to `"Upload agent"`):
                Message to commit while pushing.
            private (`bool`, *optional*, defaults to `None`):
                Whether to make the repo private. If `None`, the repo will be public unless the organization's default is private. This value is ignored if the repo already exists.
            token (`bool` or `str`, *optional*):
                The token to use as HTTP bearer authorization for remote files. If unset, will use the token generated
                when running `huggingface-cli login` (stored in `~/.huggingface`).
            create_pr (`bool`, *optional*, defaults to `False`):
                Whether to create a PR with the uploaded files or directly commit.
        """
        repo_url = create_repo(
            repo_id=repo_id,
            token=token,
            private=private,
            exist_ok=True,
            repo_type="space",
            space_sdk="gradio",
        )
        repo_id = repo_url.repo_id
        metadata_update(
            repo_id,
            {"tags": ["hamlet", "agent"]},
            repo_type="space",
            token=token,
            overwrite=True,
        )

        with tempfile.TemporaryDirectory() as work_dir:
            self.save(work_dir)
            logger.info(f"Uploading the following files to {repo_id}: {','.join(os.listdir(work_dir))}")
            return upload_folder(
                repo_id=repo_id,
                commit_message=commit_message,
                folder_path=work_dir,
                token=token,
                create_pr=create_pr,
                repo_type="space",
            )

class CodeAgent(MultiStepAgent):
    """
    In this agent, the tool calls will be formulated by the LLM in code format, then parsed and executed.

    Args:
        tools (`list[Tool]`): [`Tool`]s that the agent can use.
        model (`Model`): Model that will generate the agent's actions.
        prompt_templates ([`~agents.PromptTemplates`], *optional*): Prompt templates.
        additional_authorized_imports (`list[str]`, *optional*): Additional authorized imports for the agent.
        planning_interval (`int`, *optional*): Interval at which the agent will run a planning step.
        executor_type (`Literal["local", "e2b", "modal", "docker", "wasm"]`, default `"local"`): Type of code executor.
        executor_kwargs (`dict`, *optional*): Additional arguments to pass to initialize the executor.
        max_print_outputs_length (`int`, *optional*): Maximum length of the print outputs.
        use_structured_outputs_internally (`bool`, default `False`): Whether to use structured generation at each action step: improves performance for many models.

            <Added version="1.17.0"/>
        code_block_tags (`tuple[str, str]` | `Literal["markdown"]`, *optional*): Opening and closing tags for code blocks (regex strings). Pass a custom tuple, or pass 'markdown' to use ("```(?:python|py)", "\\n```"), leave empty to use ("<code>", "</code>").
        **kwargs: Additional keyword arguments.
    """

    def __init__(
        self,
        tools: list[Tool],
        model: Model,
        prompt_templates: PromptTemplates | None = None,
        input_schema: Any = None,
        output_schema: Any = None,
        additional_authorized_imports: list[str] | None = None,
        planning_interval: int | None = None,
        executor_type: Literal["local", "e2b", "modal", "docker", "wasm"] = "local",
        executor_kwargs: dict[str, Any] | None = None,
        max_print_outputs_length: int | None = None,
        use_structured_outputs_internally: bool = False,
        code_block_tags: str | tuple[str, str] | None = None,
        **kwargs,
    ):
        self.additional_authorized_imports = additional_authorized_imports if additional_authorized_imports else []
        self.authorized_imports = sorted(set(BASE_BUILTIN_MODULES) | set(self.additional_authorized_imports))
        self.max_print_outputs_length = max_print_outputs_length
        self._use_structured_outputs_internally = use_structured_outputs_internally
        if self._use_structured_outputs_internally:
            prompt_templates = prompt_templates or yaml.safe_load(
                importlib.resources.files("src.hamlet.core.prompts").joinpath("structured_code_agent.yaml").read_text()
            )
        else:
            prompt_templates = prompt_templates or yaml.safe_load(
                importlib.resources.files("src.hamlet.core.prompts").joinpath("code_agent.yaml").read_text()
            )

        if isinstance(code_block_tags, str) and not code_block_tags == "markdown":
            raise ValueError("Only 'markdown' is supported for a string argument to `code_block_tags`.")
        self.code_block_tags = (
            code_block_tags
            if isinstance(code_block_tags, tuple)
            else ("```python", "```")
            if code_block_tags == "markdown"
            else ("<code>", "</code>")
        )

        super().__init__(
            tools=tools,
            model=model,
            prompt_templates=prompt_templates,
            input_schema=input_schema,
            output_schema=output_schema,
            planning_interval=planning_interval,
            **kwargs,
        )

        if "*" in self.additional_authorized_imports:
            self.logger.log(
                "Caution: you set an authorization for all imports, meaning your agent can decide to import any package it deems necessary. This might raise issues if the package is not installed in your environment.",
                level=LogLevel.INFO,
            )
        if executor_type not in {"local", "e2b", "modal", "docker", "wasm"}:
            raise ValueError(f"Unsupported executor type: {executor_type}")
        self.executor_type = executor_type
        self.executor_kwargs: dict[str, Any] = executor_kwargs or {}
        self.python_executor = self.create_python_executor()

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_value, traceback):
        self.cleanup()

    def cleanup(self):
        """Clean up resources used by the agent, such as the remote Python executor."""
        if hasattr(self.python_executor, "cleanup"):
            self.python_executor.cleanup()

    def create_python_executor(self) -> PythonExecutor:
        if self.executor_type == "local":
            return LocalPythonExecutor(
                self.additional_authorized_imports,
                **{"max_print_outputs_length": self.max_print_outputs_length} | self.executor_kwargs,
            )
        else:
            if self.managed_agents:
                raise Exception("Managed agents are not yet supported with remote code execution.")
            remote_executors = {
                "e2b": E2BExecutor,
                "docker": DockerExecutor,
                "wasm": WasmExecutor,
                "modal": ModalExecutor,
            }
            return remote_executors[self.executor_type](
                self.additional_authorized_imports, self.logger, **self.executor_kwargs
            )

    def initialize_system_prompt(self) -> str:
        system_prompt = populate_template(
            self.prompt_templates["system_prompt"],
            variables={
                "tools": self.tools,
                "managed_agents": self.managed_agents,
                "authorized_imports": (
                    "You can import from any package you want."
                    if "*" in self.authorized_imports
                    else str(self.authorized_imports)
                ),
                "custom_instructions": self.instructions,
                "code_block_opening_tag": self.code_block_tags[0],
                "code_block_closing_tag": self.code_block_tags[1],
            },
        )
        print(system_prompt)
        return system_prompt

    def _step_stream(
        self, memory_step: ActionStep
    ) -> Generator[ChatMessageStreamDelta | ToolCall | ToolOutput | ActionOutput]:
        """
        Perform one step in the ReAct framework: the agent thinks, acts, and observes the result.
        Yields ChatMessageStreamDelta during the run if streaming is enabled.
        At the end, yields either None if the step is not final, or the final answer.
        """
        memory_messages = self.write_memory_to_messages()

        input_messages = memory_messages.copy()
        ### Generate model output ###
        memory_step.model_input_messages = input_messages
        stop_sequences = ["Observation:", "Calling tools:"]
        # if self.code_block_tags[1] not in self.code_block_tags[0]:
        #     # If the closing tag is contained in the opening tag, adding it as a stop sequence would cut short any code generation
        #     stop_sequences.append(self.code_block_tags[1])
        try:
            additional_args: dict[str, Any] = {}
            if self._use_structured_outputs_internally:
                additional_args["response_format"] = CODEAGENT_RESPONSE_FORMAT

            chat_message: ChatMessage = self.model.generate(
                input_messages,
                stop_sequences=stop_sequences,
                **additional_args,
            )
            memory_step.model_output_message = chat_message
            output_text = chat_message.content
            self.logger.log_markdown(
                content=output_text or "",
                title="Output message of the LLM:",
                level=LogLevel.DEBUG,
            )

            if not self._use_structured_outputs_internally:
                # This adds the end code sequence (i.e. the closing code block tag) to the history.
                # This will nudge subsequent LLM calls to finish with this end code sequence, thus efficiently stopping generation.
                if output_text and not output_text.strip().endswith(self.code_block_tags[1]):
                    output_text += self.code_block_tags[1]
                    memory_step.model_output_message.content = output_text

            memory_step.token_usage = chat_message.token_usage
            memory_step.model_output = output_text
        except Exception as e:
            raise AgentGenerationError(f"Error in generating model output:\n{e}", self.logger) from None

        ### Parse output ###
        try:
            if self._use_structured_outputs_internally:
                code_action = json.loads(output_text)["code"]
                code_action = extract_code_from_text(code_action, self.code_block_tags) or code_action
            else:
                code_actions, early_stop_strategy, early_stop_details = parse_several_code_blobs(output_text, self.code_block_tags)
            code_actions = [fix_final_answer_code(code_action) for code_action in code_actions]
            memory_step.code_action = code_actions
        except Exception as e:
            error_msg = f"Error in code parsing:\n{e}\nMake sure to provide correct code blobs."
            raise AgentParsingError(error_msg, self.logger)

        if len(code_actions) == 1:
            code_action = code_actions[0]
            tool_call = ToolCall(
                name="python_interpreter",
                arguments=code_action,
                id=f"call_{len(self.memory.steps)}",
            )
            yield tool_call
            memory_step.tool_calls = [tool_call]

            ### Execute action ###
            self.logger.log_code(title="Executing parsed code:", content=code_action, level=LogLevel.INFO)
            try:
                code_output: CodeOutput = self.python_executor(code_action)
                execution_outputs_console = []
                if len(code_output.logs) > 0:
                    execution_outputs_console += [
                        Text("Execution logs:", style="bold"),
                        Text(code_output.logs.strip()),
                    ]
                observation = "Execution logs:\n" + code_output.logs.strip()
            except Exception as e:
                if hasattr(self.python_executor, "state") and "_print_outputs" in self.python_executor.state:
                    execution_logs = str(self.python_executor.state["_print_outputs"])
                    if len(execution_logs) > 0:
                        execution_outputs_console = [
                            Text("Execution logs:", style="bold"),
                            Text(execution_logs.strip()),
                        ]
                        memory_step.observations = "Execution logs:\n" + execution_logs.strip()
                        self.logger.log(Group(*execution_outputs_console), level=LogLevel.INFO)
                error_msg = str(e)
                if "Import of " in error_msg and " is not allowed" in error_msg:
                    self.logger.log(
                        "[bold red]Warning to user: Code execution failed due to an unauthorized import - Consider passing said import under `additional_authorized_imports` when initializing your CodeAgent.",
                        level=LogLevel.INFO,
                    )
                raise AgentExecutionError(error_msg, self.logger)

            truncated_output = truncate_content(str(code_output.output))
            observation += "Last output from code snippet:\n" + truncated_output
            memory_step.observations = observation

            if not code_output.is_final_answer:
                execution_outputs_console += [
                    Text(f"Out: {truncated_output}", style="bold"),
                ]
            self.logger.log(Group(*execution_outputs_console), level=LogLevel.INFO)
            memory_step.action_output = code_output.output
            yield ActionOutput(output=code_output.output, is_final_answer=code_output.is_final_answer)
        
        else:
            memory_step.tool_calls = []
            for idx, code_action in enumerate(code_actions):
                tool_call = ToolCall(
                    name="python_interpreter",
                    arguments=code_action,
                    id=f"call_{len(self.memory.steps)}_Code#{idx}",
                )
                yield tool_call
                memory_step.tool_calls.append(tool_call)
            
            ### Execute actions concurrently ###
            all_code_outputs: Dict[str, Any] = {}
            executor = ProcessPoolExecutor(max_workers=len(code_actions))
            execution_outputs_console = []
            observation = ""
            temp_executor_state = self.python_executor.state.copy()
            futures: Dict[Future, int] = {executor.submit(self.python_executor, code_action): idx for idx, code_action in enumerate(code_actions)}
            for idx, code_action in enumerate(code_actions):
                self.logger.log_code(title=f"Concurrently executing parsed Code#{idx+1} ({len(code_actions)} in total)", content=code_action, level=LogLevel.INFO)
                
            if early_stop_strategy != "none":
                execution_outputs_console += [
                    Text(f"Early stop strategy detected: '{early_stop_strategy}'. Will check each code execution result accordingly.", style="bold yellow")
                ]
                code_check = True if early_stop_strategy == "code" else False
                current_step_finished = False
                if code_check:
                    self.logger.log_code(title="Parsed early stop code:", content=early_stop_details, level=LogLevel.INFO)
                else:
                    self.logger.log_code(title="Parsed early stop prompt:", content=early_stop_details, level=LogLevel.INFO)
                future_count = 0

                try:
                    for future in as_completed(futures):
                        idx = futures[future]
                        code_output: CodeOutput = future.result()
                        all_code_outputs[f"Code#{idx+1}"] = code_output
                        future_count += 1
                        if len(code_output.logs) > 0:
                            execution_outputs_console += [
                                Text(f"Execution logs for Code#{idx+1} ({len(code_actions)} in total):", style="bold"),
                                Text(code_output.logs.strip()),
                            ]
                        observation += f"Execution logs for Code#{idx+1} ({len(code_actions)} in total):\n" + code_output.logs.strip()
                        if code_check:
                            try:
                                execution_outputs_console += [
                                    Text(f"Executing parsed early stop code for Code#{idx+1} ({len(code_actions)} in total)...", style="bold")
                                ]
                                early_stop_code = early_stop_details + "\nfinal_answer(early_stop_result)"
                                self.python_executor.state = code_output.executor_state
                                """
                                Close matches is used to avoid issues with variable names.
                                For example, although 'result_2' is not defined before, it will match 'result_1' in the actual execution result in early stop code.
                                """
                                current_step_finished = self.python_executor(early_stop_code.strip()).output
                                if isinstance(current_step_finished, str):
                                    current_step_finished = "True" in current_step_finished.strip() or "true" in current_step_finished.strip()
                            except Exception as e:
                                error_msg = f"An error occurred while executing early stop code (when evaluating the execution result of Code#{idx+1}): {e}"
                                raise AgentExecutionError(error_msg, self.logger) from None
                        else:
                            execution_outputs_console += [
                                    Text(f"Using early stop prompt for Code#{idx+1} ({len(code_actions)} in total)...", style="bold")
                                ]
                            check_system_prompt = ChatMessage(
                                role=MessageRole.SYSTEM,
                                content=[
                                    {
                                        "type": "text",
                                        "text": self.prompt_templates["earlystop"]["system_prompt"]
                                    }
                                ]
                            )
                            check_prompt = ChatMessage(
                                role=MessageRole.USER,
                                content=[
                                    {
                                        "type": "text",
                                        "text": populate_template(
                                            self.prompt_templates["earlystop"]["earlystop_prompt"],
                                            variables={"output": code_output.logs, "details": early_stop_details},
                                        )
                                    }
                                ]
                            )
                            input_messages = [check_system_prompt, check_prompt]
                            try:
                                response = self.model.generate(input_messages)
                                execution_outputs_console += [
                                    Text(f"Output of llm evaluation with early stop prompt for Code#{idx+1} ({len(code_actions)} in total):", style="bold"),
                                    Text(str(response.content).strip()),
                                ]
                                current_step_finished = "True" in str(response.content).strip() or "true" in str(response.content).strip()
                            except Exception as e:
                                raise AgentGenerationError(f"Error in generating evaluation result with early stop prompt:\n{e}", self.logger) from None
                            
                        if current_step_finished:
                            execution_outputs_console += [
                                Text(f"Evaluation for the execution result of Code#{idx+1} passed ({len(code_actions)} in total), other code executions will be cancelled.", style="green")
                            ]
                            observation += f"Evaluation for the execution result of Code#{idx+1} passed, other code executions will be cancelled.\nThe content of Code#{idx+1} is:\n{code_actions[idx]}\n"
                            self.python_executor.state = code_output.executor_state.copy()
                            truncated_output = truncate_content(str(code_output.output))
                            observation += "Last output from code snippet:\n" + truncated_output
                            if not code_output.is_final_answer:
                                execution_outputs_console += [
                                    Text(
                                        f"Out: {truncated_output}",
                                    ),
                                ]
                            break
                        else:
                            if future_count < len(code_actions):
                                execution_outputs_console += [
                                    Text(f"Evaluation for the execution result of Code#{idx+1} failed, other code executions will continue.", style="red")
                                ]
                                observation += f"Evaluation for the execution result of Code#{idx+1} failed ({len(code_actions)} in total), other code executions will continue.\n"
                            else:
                                execution_outputs_console += [
                                    Text(f"Evaluation for the execution result of Code#{idx+1} failed, all code execution results did not pass the check.", style="red"),
                                    Text(f"The execution results of all code snippets are as follows:", style="bold"),
                                    Text("\n".join([f"{code_idx}: {code_output.output}" for code_idx, code_output in all_code_outputs.items()]))
                                ]
                                observation += f"Evaluation for the execution result of Code#{idx+1} failed ({len(code_actions)} in total), all code executions did not pass the check. The execution results of all code snippets are as follows:\n"
                                for code_idx, code_output in all_code_outputs.items():
                                    observation += f"{code_idx}: {code_output.output}\n"
                                observation += "Here are some suggestions:\n1. Modify your code blocks\n2. Adjust your code (or prompt) for early stopping\n3. Change the early stop strategy if necessary.\n"
                                self.python_executor.state = temp_executor_state.copy()
                                code_output.output = None
                                code_output.is_final_answer = False

                except AgentGenerationError as e:
                    raise
                except AgentExecutionError as e:
                    raise
                except Exception as e:
                    if hasattr(self.python_executor, "state") and "_print_outputs" in self.python_executor.state:
                        execution_logs = str(self.python_executor.state["_print_outputs"])
                        if len(execution_logs) > 0:
                            execution_outputs_console = [
                                Text(f"Execution logs for Code#{idx} ({len(code_actions)} in total):", style="bold"),
                                Text(execution_logs.strip()),
                            ]
                            memory_step.observations = f"Execution logs for Code#{idx} ({len(code_actions)} in total):\n" + execution_logs
                            self.logger.log(Group(*execution_outputs_console), level=LogLevel.INFO)
                    error_msg = f"An error occurred during Code execution #{idx}:\n{str(e)}\nThe content of Code#{idx} is:\n{code_actions[idx]}"
                    if "Import of " in error_msg and " is not allowed" in error_msg:
                        self.logger.log(
                            f"[bold red]Warning to user: Code execution #{idx} ({len(code_actions)} in total) failed due to an unauthorized import - Consider passing said import under `additional_authorized_imports` when initializing your CodeAgent.",
                            level=LogLevel.INFO,
                        )
                    raise AgentExecutionError(error_msg, self.logger)
                finally:
                    executor.shutdown(wait=False)
                
                # if current_step_finished:
                #     self.python_executor.state = code_output.executor_state.copy()
                #     truncated_output = truncate_content(str(code_output.output))
                #     observation += "Last output from code snippet:\n" + truncated_output
                #     if not code_output.is_final_answer:
                #         execution_outputs_console += [
                #             Text(
                #                 f"Out: {truncated_output}",
                #             ),
                #         ]

                memory_step.observations = observation
                self.logger.log(Group(*execution_outputs_console), level=LogLevel.INFO)
                memory_step.action_output = code_output.output
                yield ActionOutput(output=code_output.output, is_final_answer=code_output.is_final_answer)
            
            else:
                execution_outputs_console += [
                    Text(f"Early stop strategy set to 'none'. Will gather all code execution results.", style="bold yellow")
                ]
                try:
                    for future in as_completed(futures):
                        idx = futures[future]
                        code_output: CodeOutput = future.result()
                        all_code_outputs[f"Code#{idx+1}"] = code_output
                    all_code_outputs = dict(sorted(all_code_outputs.items()))
                    truncated_output = ""
                    for code_idx, code_output in all_code_outputs.items():
                        if code_output.is_final_answer:
                            raise AgentExecutionError("When early stop strategy is 'none', none of the code snippets should return a final answer.", self.logger)
                        if len(code_output.logs) > 0:
                            execution_outputs_console += [
                                Text(f"Execution logs for {code_idx} ({len(code_actions)} in total):", style="bold"),
                                Text(code_output.logs.strip()),
                            ]
                        observation += f"Execution logs for {code_idx} ({len(code_actions)} in total):\n{code_output.logs.strip()}\n"
                        truncated_output += f"{code_idx}: {truncate_content(str(code_output.output))}\n"
                    observation += "Last outputs from all code snippets:\n" + truncated_output
                except AgentExecutionError as e:
                    raise
                except Exception as e:
                    if hasattr(self.python_executor, "state") and "_print_outputs" in self.python_executor.state:
                        execution_logs = str(self.python_executor.state["_print_outputs"])
                        if len(execution_logs) > 0:
                            execution_outputs_console = [
                                Text(f"Execution logs for Code#{idx} ({len(code_actions)} in total):", style="bold"),
                                Text(execution_logs.strip()),
                            ]
                            memory_step.observations = f"Execution logs for Code#{idx} ({len(code_actions)} in total):\n" + execution_logs
                            self.logger.log(Group(*execution_outputs_console), level=LogLevel.INFO)
                    error_msg = f"An error occurred during Code execution #{idx}:\n{str(e)}\nThe content of Code#{idx} is:\n{code_actions[idx]}"
                    if "Import of " in error_msg and " is not allowed" in error_msg:
                        self.logger.log(
                            f"[bold red]Warning to user: Code execution #{idx} ({len(code_actions)} in total) failed due to an unauthorized import - Consider passing said import under `additional_authorized_imports` when initializing your CodeAgent.",
                            level=LogLevel.INFO,
                        )
                    raise AgentExecutionError(error_msg, self.logger)
                finally:
                    executor.shutdown(wait=False)

                memory_step.observations = observation
                self.logger.log(Group(*execution_outputs_console), level=LogLevel.INFO)
                memory_step.action_output = all_code_outputs
                yield ActionOutput(output=all_code_outputs, is_final_answer=False)


    def to_dict(self) -> dict[str, Any]:
        """Convert the agent to a dictionary representation.

        Returns:
            `dict`: Dictionary representation of the agent.
        """
        agent_dict = super().to_dict()
        agent_dict["authorized_imports"] = self.authorized_imports
        agent_dict["executor_type"] = self.executor_type
        agent_dict["executor_kwargs"] = self.executor_kwargs
        agent_dict["max_print_outputs_length"] = self.max_print_outputs_length
        return agent_dict

    @classmethod
    def from_dict(cls, agent_dict: dict[str, Any], **kwargs) -> "CodeAgent":
        """Create CodeAgent from a dictionary representation.

        Args:
            agent_dict (`dict[str, Any]`): Dictionary representation of the agent.
            **kwargs: Additional keyword arguments that will override agent_dict values.

        Returns:
            `CodeAgent`: Instance of the CodeAgent class.
        """
        # Add CodeAgent-specific parameters to kwargs
        code_agent_kwargs = {
            "additional_authorized_imports": agent_dict.get("authorized_imports"),
            "executor_type": agent_dict.get("executor_type"),
            "executor_kwargs": agent_dict.get("executor_kwargs"),
            "max_print_outputs_length": agent_dict.get("max_print_outputs_length"),
            "code_block_tags": agent_dict.get("code_block_tags"),
            "input_schema": agent_dict.get("input_schema"),
            "output_schema": agent_dict.get("output_schema"),
        }
        # Filter out None values
        code_agent_kwargs = {k: v for k, v in code_agent_kwargs.items() if v is not None}
        # Update with any additional kwargs
        code_agent_kwargs.update(kwargs)
        # Call the parent class's from_dict method
        return super().from_dict(agent_dict, **code_agent_kwargs)
