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)
)