import sys
sys.path.insert(0, '../../src')
import matplotlib.pyplot as plt
from matplotlib.patches import FancyBboxPatch
from ai_playground.plotting import apply_plot_style
apply_plot_style()
fig, ax = plt.subplots(figsize=(11.5, 5.8))
ax.set_xlim(0, 12); ax.set_ylim(0, 6.6); ax.axis('off')
def draw_box(x, y, w, h, text, fc, fontsize=10):
ax.add_patch(FancyBboxPatch((x - w / 2, y - h / 2), w, h,
boxstyle='round,pad=0.12', fc=fc, ec='0.35', lw=1.2))
ax.text(x, y, text, ha='center', va='center', fontsize=fontsize)
def draw_arrow(p, q, label, curve=0.0, label_xy=None):
ax.annotate('', xy=q, xytext=p,
arrowprops=dict(arrowstyle='-|>', lw=1.6, color='0.25',
connectionstyle=f'arc3,rad={curve}'))
if label_xy is None:
label_xy = ((p[0] + q[0]) / 2, (p[1] + q[1]) / 2 + 0.18)
ax.text(*label_xy, label, ha='center', va='bottom', fontsize=9.5,
bbox=dict(fc='white', ec='none', alpha=0.85, pad=1.5))
draw_box(1.2, 5.7, 2.0, 0.8, 'user\nmessage', '#e9d5ff')
draw_box(2.8, 3.3, 3.2, 1.6, 'messages[]\nthe whole conversation:\nuser · assistant · tool results', '#dbeafe')
draw_box(6.8, 4.8, 2.7, 1.2, 'LLM\n(next-token predictor)', '#fde68a')
draw_box(10.2, 3.3, 3.0, 1.6, 'ToolRegistry\ncalculator · read_file · …\n(plain Python functions)', '#dcfce7')
draw_box(6.8, 0.9, 3.4, 1.0, 'final answer\n(response has no tool calls)', '#fecaca')
draw_arrow((1.5, 5.25), (2.3, 4.15), '\u2460 appended', curve=-0.15, label_xy=(1.55, 4.55))
draw_arrow((4.0, 4.0), (5.4, 4.65), '\u2461 full history\n+ tool schemas', curve=-0.1,
label_xy=(4.2, 5.35))
draw_arrow((8.2, 4.65), (9.6, 4.0), '\u2462 tool_use block\nname + arguments', curve=-0.1,
label_xy=(9.75, 5.15))
draw_arrow((9.4, 2.45), (4.5, 2.75), '\u2463 tool_result appended \u2192 next iteration \u21ba',
curve=-0.25, label_xy=(6.95, 1.85))
draw_arrow((6.8, 4.15), (6.8, 1.45), '\u2464 no tool calls \u2192 loop exits',
label_xy=(6.8, 3.35))
ax.text(6.8, 6.25, 'An agent = an LLM in a loop that can call tools',
ha='center', fontsize=13, fontweight='bold')
ax.text(2.8, 0.55, 'the ReAct loop: \u2461\u2192\u2462\u2192\u2463 repeats until \u2464',
ha='center', fontsize=9.5, style='italic', color='0.35')
plt.tight_layout()
plt.show()