Skip to content

Agent History

Bases: BoostedMagic

Source code in pydantic_ai_kernel/magics/agent_history_magic.py
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
class AgentHistoryMagic(BoostedMagic):

    @boosted_option(
        "-f",
        "--formatter",
        default=None,
        help="Formatter for the output. Must be supported by jupyter frontend. All formatter can be set by environment variable PYDANTIC_AI_KERNEL_FORMATTER",
        choices=out_formatter_list,
        completer=lambda w, r: complete_from_list(out_formatter_list, w, r),
    )
    @boosted_option(
        "--full",
        default=False,
        action="store_true",
        help="Force display of full text, ignoring truncation",
    )
    def line_agent_history(
        self, formatter: str | None = None, full: bool = False
    ) -> None:
        """
        %agent_history : displays the agent history

        Examples :
        -------
            • `%agent_history --formatter md` : markdown display of agent history
        """
        # Use kernel's configured formatter if not overridden
        env_formatter = getattr(self.kernel, "formatter", "text")
        if formatter is None:
            formatter = env_formatter
        self.kernel: PydanticAIBaseKernel
        self.evaluate = False
        history: list[ModelMessage] = self.kernel.agent.agent_history

        structured_messages = structured_message_display(
            history, max_length=None if full else 1000
        )

        if formatter == "json":
            self.kernel.Print(
                ModelMessagesTypeAdapter.dump_json(history, indent=4).decode("utf-8")
            )
            return

        for title, content in structured_messages:
            if formatter == "md":
                if self.kernel.use_widget:
                    self.kernel.Display(
                        widgets.Accordion(
                            children=[
                                widgets.HTML(
                                    value=content,
                                    placeholder="",
                                    description="",
                                )
                            ],
                            titles=[title],
                        )
                    )
                else:
                    # If widgets are NOT used, we might still want to truncate if not --full
                    # But the user requested: "don't truncate it if the widgets are displayed. If they are not, you have to truncate them."
                    # AND "add flag to %agent_history magic that display the full text, whatever the formatter is"

                    # The structured_message_display already handled truncation based on 'full'.
                    # However, if widgets are NOT used, we should probably ensure truncation happened
                    # unless --full was passed.
                    # Actually, if we pass max_length=None to structured_message_display, it won't truncate.
                    # If we pass 1000, it will.

                    # Let's refine the logic in structured_message_display first.
                    self.kernel.Display({"text/markdown": f"### {title}  \n{content}"})
            elif formatter == "text":
                self.kernel.Print(f"  {title} :\n" + put_text_in_box(content, 4))
            elif formatter == "terminal":
                self.kernel.Print(
                    f"  \033[0;32m{title}\033[0m :\n" + put_text_in_box(content, 4)
                )

line_agent_history(formatter=None, full=False)

%agent_history : displays the agent history

Examples :

• `%agent_history --formatter md` : markdown display of agent history
Source code in pydantic_ai_kernel/magics/agent_history_magic.py
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
@boosted_option(
    "-f",
    "--formatter",
    default=None,
    help="Formatter for the output. Must be supported by jupyter frontend. All formatter can be set by environment variable PYDANTIC_AI_KERNEL_FORMATTER",
    choices=out_formatter_list,
    completer=lambda w, r: complete_from_list(out_formatter_list, w, r),
)
@boosted_option(
    "--full",
    default=False,
    action="store_true",
    help="Force display of full text, ignoring truncation",
)
def line_agent_history(
    self, formatter: str | None = None, full: bool = False
) -> None:
    """
    %agent_history : displays the agent history

    Examples :
    -------
        • `%agent_history --formatter md` : markdown display of agent history
    """
    # Use kernel's configured formatter if not overridden
    env_formatter = getattr(self.kernel, "formatter", "text")
    if formatter is None:
        formatter = env_formatter
    self.kernel: PydanticAIBaseKernel
    self.evaluate = False
    history: list[ModelMessage] = self.kernel.agent.agent_history

    structured_messages = structured_message_display(
        history, max_length=None if full else 1000
    )

    if formatter == "json":
        self.kernel.Print(
            ModelMessagesTypeAdapter.dump_json(history, indent=4).decode("utf-8")
        )
        return

    for title, content in structured_messages:
        if formatter == "md":
            if self.kernel.use_widget:
                self.kernel.Display(
                    widgets.Accordion(
                        children=[
                            widgets.HTML(
                                value=content,
                                placeholder="",
                                description="",
                            )
                        ],
                        titles=[title],
                    )
                )
            else:
                # If widgets are NOT used, we might still want to truncate if not --full
                # But the user requested: "don't truncate it if the widgets are displayed. If they are not, you have to truncate them."
                # AND "add flag to %agent_history magic that display the full text, whatever the formatter is"

                # The structured_message_display already handled truncation based on 'full'.
                # However, if widgets are NOT used, we should probably ensure truncation happened
                # unless --full was passed.
                # Actually, if we pass max_length=None to structured_message_display, it won't truncate.
                # If we pass 1000, it will.

                # Let's refine the logic in structured_message_display first.
                self.kernel.Display({"text/markdown": f"### {title}  \n{content}"})
        elif formatter == "text":
            self.kernel.Print(f"  {title} :\n" + put_text_in_box(content, 4))
        elif formatter == "terminal":
            self.kernel.Print(
                f"  \033[0;32m{title}\033[0m :\n" + put_text_in_box(content, 4)
            )