ConnectOnionConnectOnion

re_act

ReAct (Reason + Act) pattern for intelligent agents

What it does

The re_act plugin implements the ReAct pattern:

Plan (after_user_input)

Before taking any action, the agent plans what to do based on user input and available tools.

Reflect (after_tools)

After each tool execution, the agent reflects on results and decides next steps.

Quick Start

main.py
1from connectonion import Agent 2from connectonion.useful_plugins import re_act 3 4def search(query: str) -> str: 5 """Search the web for information.""" 6 return f"Results for '{query}': Python is a programming language..." 7 8agent = Agent("assistant", tools=[search], plugins=[re_act]) 9 10agent.input("Search for Python and explain what it is")
Python REPL
Interactive
💭 I'll search for information about Python, then explain what it is based on the results.
[Tool: search("Python")]
💭 The search returned that Python is a programming language. I now have enough context to explain.
Python is a high-level, interpreted programming language known for its simplicity...

How it works

1. Planning Phase

After receiving user input, the plugin generates a brief plan:

main.py
1# Internal: plan_task handler 2@after_user_input 3def plan_task(agent): 4 user_prompt = agent.current_session.get('user_prompt', '') 5 tool_names = agent.tools.names() 6 7 plan = llm_do( 8 f"User request: {user_prompt}\nAvailable tools: {tool_names}\nBrief plan:", 9 model="co/gemini-2.5-flash" 10 ) 11 12 agent.current_session['messages'].append({ 13 'role': 'assistant', 14 'content': f"💭 {plan}" 15 })

2. Reflection Phase

After tools execute, the plugin reflects on results:

main.py
1# Uses the built-in reflect handler from useful_events_handlers 2@after_tools 3def reflect(agent): 4 trace = agent.current_session['trace'][-1] 5 6 if trace['type'] == 'tool_execution' and trace['status'] == 'success': 7 reflection = llm_do( 8 f"Result: {trace['result'][:200]}\nWhat did we learn?", 9 model="co/gemini-2.5-flash" 10 ) 11 12 agent.current_session['messages'].append({ 13 'role': 'assistant', 14 'content': f"💭 {reflection}" 15 })

Combined with Eval Plugin

For debugging and testing, combine with the eval plugin:

main.py
1from connectonion import Agent 2from connectonion.useful_plugins import re_act, eval 3 4# ReAct provides planning, eval provides evaluation at completion 5agent = Agent("assistant", tools=[search], plugins=[re_act, eval]) 6 7agent.input("Search for Python") 8# Output includes: 9# 💭 Plan: I'll search for Python information 10# [Tool execution] 11# 💭 Reflection: We learned Python is a programming language 12# ✓ Evaluation: Task complete - found and understood Python info

Events Used

EventHandlerPurpose
after_user_inputplan_taskGenerate initial plan
after_toolsreflectReflect on tool results

Source

connectonion/useful_plugins/re_act.py

main.py
1# The plugin is just a list of event handlers 2re_act = [plan_task, reflect]