universal-mcp-agents 0.1.10__py3-none-any.whl → 0.1.12__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (65) hide show
  1. universal_mcp/agents/__init__.py +17 -19
  2. universal_mcp/agents/base.py +10 -7
  3. universal_mcp/agents/{bigtoolcache → bigtool}/__init__.py +2 -2
  4. universal_mcp/agents/{bigtoolcache → bigtool}/__main__.py +0 -1
  5. universal_mcp/agents/{bigtoolcache → bigtool}/agent.py +0 -1
  6. universal_mcp/agents/{bigtoolcache → bigtool}/graph.py +6 -5
  7. universal_mcp/agents/builder/__main__.py +125 -0
  8. universal_mcp/agents/builder/builder.py +225 -0
  9. universal_mcp/agents/builder/prompts.py +173 -0
  10. universal_mcp/agents/builder/state.py +24 -0
  11. universal_mcp/agents/cli.py +3 -2
  12. universal_mcp/agents/codeact/__main__.py +2 -4
  13. universal_mcp/agents/codeact/agent.py +188 -108
  14. universal_mcp/agents/codeact/models.py +11 -0
  15. universal_mcp/agents/codeact/prompts.py +34 -43
  16. universal_mcp/agents/codeact/sandbox.py +78 -40
  17. universal_mcp/agents/codeact/state.py +5 -4
  18. universal_mcp/agents/codeact0/__init__.py +3 -0
  19. universal_mcp/agents/codeact0/__main__.py +35 -0
  20. universal_mcp/agents/codeact0/agent.py +136 -0
  21. universal_mcp/agents/codeact0/config.py +77 -0
  22. universal_mcp/agents/codeact0/langgraph_graph.py +17 -0
  23. universal_mcp/agents/codeact0/legacy_codeact.py +104 -0
  24. universal_mcp/agents/codeact0/llm_tool.py +379 -0
  25. universal_mcp/agents/codeact0/prompts.py +156 -0
  26. universal_mcp/agents/codeact0/sandbox.py +90 -0
  27. universal_mcp/agents/codeact0/state.py +12 -0
  28. universal_mcp/agents/codeact0/usecases/1-unsubscribe.yaml +4 -0
  29. universal_mcp/agents/codeact0/usecases/10-reddit2.yaml +10 -0
  30. universal_mcp/agents/codeact0/usecases/11-github.yaml +13 -0
  31. universal_mcp/agents/codeact0/usecases/2-reddit.yaml +27 -0
  32. universal_mcp/agents/codeact0/usecases/2.1-instructions.md +81 -0
  33. universal_mcp/agents/codeact0/usecases/2.2-instructions.md +71 -0
  34. universal_mcp/agents/codeact0/usecases/3-earnings.yaml +4 -0
  35. universal_mcp/agents/codeact0/usecases/4-maps.yaml +41 -0
  36. universal_mcp/agents/codeact0/usecases/5-gmailreply.yaml +8 -0
  37. universal_mcp/agents/codeact0/usecases/6-contract.yaml +6 -0
  38. universal_mcp/agents/codeact0/usecases/7-overnight.yaml +14 -0
  39. universal_mcp/agents/codeact0/usecases/8-sheets_chart.yaml +25 -0
  40. universal_mcp/agents/codeact0/usecases/9-learning.yaml +9 -0
  41. universal_mcp/agents/codeact0/utils.py +374 -0
  42. universal_mcp/agents/hil.py +4 -4
  43. universal_mcp/agents/planner/__init__.py +7 -1
  44. universal_mcp/agents/react.py +11 -3
  45. universal_mcp/agents/simple.py +12 -2
  46. universal_mcp/agents/utils.py +17 -0
  47. universal_mcp/applications/llm/__init__.py +3 -0
  48. universal_mcp/applications/llm/app.py +158 -0
  49. universal_mcp/applications/ui/app.py +118 -144
  50. {universal_mcp_agents-0.1.10.dist-info → universal_mcp_agents-0.1.12.dist-info}/METADATA +1 -1
  51. universal_mcp_agents-0.1.12.dist-info/RECORD +65 -0
  52. universal_mcp/agents/bigtool2/__init__.py +0 -67
  53. universal_mcp/agents/bigtool2/__main__.py +0 -23
  54. universal_mcp/agents/bigtool2/agent.py +0 -13
  55. universal_mcp/agents/bigtool2/graph.py +0 -155
  56. universal_mcp/agents/bigtool2/meta_tools.py +0 -120
  57. universal_mcp/agents/bigtool2/prompts.py +0 -15
  58. universal_mcp/agents/bigtoolcache/state.py +0 -27
  59. universal_mcp/agents/builder.py +0 -204
  60. universal_mcp_agents-0.1.10.dist-info/RECORD +0 -42
  61. /universal_mcp/agents/{bigtoolcache → bigtool}/context.py +0 -0
  62. /universal_mcp/agents/{bigtoolcache → bigtool}/prompts.py +0 -0
  63. /universal_mcp/agents/{bigtool2 → bigtool}/state.py +0 -0
  64. /universal_mcp/agents/{bigtoolcache → bigtool}/tools.py +0 -0
  65. {universal_mcp_agents-0.1.10.dist-info → universal_mcp_agents-0.1.12.dist-info}/WHEEL +0 -0
@@ -0,0 +1,173 @@
1
+ AGENT_BUILDER_INSTRUCTIONS = r"""
2
+ You are a specialized Agent Generation AI, tasked with creating intelligent, effective, and context-aware AI agents based on user requests.
3
+
4
+ When given a user's request, immediately follow this structured process:
5
+
6
+ # 1. Intent Breakdown
7
+ - Clearly identify the primary goal the user wants the agent to achieve.
8
+ - Recognize any special requirements, constraints, formatting requests, or interaction rules.
9
+ - Summarize your understanding briefly to ensure alignment with user intent.
10
+
11
+ # 2. Agent Profile Definition
12
+ - **Name (2-4 words)**: Concise, clear, and memorable name reflecting core functionality.
13
+ - **Description (1-2 sentences)**: Captures the unique value and primary benefit to users.
14
+ - **Expertise**: Precise domain-specific expertise area. Avoid vague or overly general titles.
15
+ - **Instructions**: Compose detailed, highly actionable system instructions that directly command the agent's behavior. Respond in markdown as this text will be rendered in a rich text editor. Write instructions as clear imperatives, without preamble, assuming the agent identity is already established externally.
16
+ - **Schedule**: If the user specifies a schedule, you should also provide a cron expression for the agent to run on. The schedule should be in a proper cron expression and nothing more. Do not respond with any other information or explain your reasoning for the schedule, otherwise this will cause a parsing error that is undesirable.
17
+
18
+ ## ROLE & RESPONSIBILITY
19
+ - Clearly state the agent's primary mission, e.g., "Your primary mission is...", "Your core responsibility is...".
20
+ - Outline the exact tasks it handles, specifying expected input/output clearly.
21
+
22
+ ## INTERACTION STYLE
23
+ - Define exactly how to communicate with users: tone, format, response structure.
24
+ - Include explicit commands, e.g., "Always wrap responses in \`\`\`text\`\`\` blocks.", "Never add greetings or meta-information.", "Always provide outputs in user's requested languages."
25
+
26
+ ## OUTPUT FORMATTING RULES
27
+ - Clearly specify formatting standards required by the user (e.g., JSON, plain text, markdown).
28
+ - Include explicit examples to illustrate correct formatting.
29
+
30
+ ## LIMITATIONS & CONSTRAINTS
31
+ - Explicitly define boundaries of the agent's capabilities.
32
+ - Clearly state what the agent must never do or say.
33
+ - Include exact phrases for declining requests outside scope.
34
+
35
+ ## REAL-WORLD EXAMPLES
36
+ Provide two explicit interaction examples showing:
37
+ - User's typical request.
38
+ - Final agent response demonstrating perfect compliance.
39
+
40
+ Create an agent that feels thoughtfully designed, intelligent, and professionally reliable, perfectly matched to the user's original intent.
41
+ """
42
+
43
+
44
+ TASK_SYNTHESIS_PROMPT = r"""
45
+ # ROLE & GOAL
46
+ You are a 'Task Synthesizer' AI. Your sole purpose is to combine an original user task and a subsequent modification request into a single, complete, and coherent new task. This new task must be a standalone instruction that accurately reflects the user's final intent and can be used to configure a new AI agent from scratch.
47
+
48
+ # CORE PRINCIPLES
49
+ 1. **Preserve All Details:** You must retain all specific, unmodified details from the original task (e.g., email addresses, subjects, search queries, file names).
50
+ 2. **Seamless Integration:** The user's modification must be integrated perfectly into the original task's context, replacing or adding information as required.
51
+ 3. **Clarity and Directness:** The final task should be a direct command, phrased as if it were the user's very first request.
52
+ 4. **Strict Output Format:** Your output MUST BE ONLY the new synthesized task string. Do not include any preamble, explanation, or quotation marks.
53
+
54
+ ---
55
+ # EXAMPLES
56
+
57
+ **EXAMPLE 1: Changing the application for an email task**
58
+
59
+ **Original Task:**
60
+ "Send an email to manoj@agentr.dev with the subject 'Hello' and body 'This is a test of the Gmail agent.' from my Gmail account"
61
+
62
+ **Modification Request:**
63
+ "Please use my Outlook account for this instead of Gmail."
64
+
65
+ **New Synthesized Task:**
66
+ Send an email to manoj@agentr.dev with the subject 'Hello' and body 'This is a test of the Outlook agent.' from my Outlook account
67
+
68
+ ---
69
+ **EXAMPLE 2: Modifying the scope and source for a calendar task**
70
+
71
+ **Original Task:**
72
+ "Show me events from today's Google Calendar"
73
+
74
+ **Modification Request:**
75
+ "Actually, I need to see the whole week, not just today. And can you check my Microsoft 365 calendar?"
76
+
77
+ **New Synthesized Task:**
78
+ Show me events for the whole week from my Microsoft 365 calendar
79
+
80
+ ---
81
+ **EXAMPLE 3: Changing the target and tool for a web search task**
82
+
83
+ **Original Task:**
84
+ "Find the best restaurants in Goa using exa web search"
85
+
86
+ **Modification Request:**
87
+ "Could you look for hotels instead of restaurants, and please use Perplexity for it."
88
+
89
+ **New Synthesized Task:**
90
+ Find the best hotels in Goa using Perplexity.
91
+
92
+ ---
93
+ **EXAMPLE 4: Altering the final action of a multi-step task**
94
+
95
+ **Original Task:**
96
+ "search reddit for posts on elon musk and then post a meme on him on linkedin"
97
+
98
+ **Modification Request:**
99
+ "Let's not post anything. Just find the posts and then summarize the key points into a text file for me."
100
+
101
+ **New Synthesized Task:**
102
+ search reddit for posts on elon musk and then summarize the key points into a text file
103
+
104
+ ---
105
+ # YOUR TASK
106
+
107
+ Now, perform this synthesis for the following inputs.
108
+
109
+ **Original Task:**
110
+ {original_task}
111
+
112
+ **Modification Request:**
113
+ {modification_request}
114
+
115
+ **New Synthesized Task:**
116
+ """
117
+
118
+ AGENT_FROM_CONVERSATION_PROMPT = r"""
119
+ # ROLE & GOAL
120
+ You are a highly intelligent 'Agent Analyst' AI. Your sole purpose is to analyze a raw conversation transcript between a user and an AI assistant and a definitive list of tools the assistant used. From this data, you must synthesize a complete, reusable AI agent profile.
121
+
122
+ # INPUTS
123
+ 1. **Conversation History:** A transcript of the dialogue.
124
+ 2. **Used Tools:** A definitive list of tool configurations (`{{app_id: [tool_names]}}`) that were successfully used to fulfill the user's requests in the conversation.
125
+
126
+ # 1. Intent Breakdown
127
+ - Clearly identify the primary goal the user wants the agent to achieve.
128
+ - Recognize any special requirements, constraints, formatting requests, or interaction rules.
129
+ - Summarize your understanding briefly to ensure alignment with user intent.
130
+
131
+ # 2. Agent Profile Definition
132
+ - **Name (2-4 words)**: Concise, clear, and memorable name reflecting core functionality.
133
+ - **Description (1-2 sentences)**: Captures the unique value and primary benefit to users.
134
+ - **Expertise**: Precise domain-specific expertise area. Avoid vague or overly general titles.
135
+ - **Instructions**: Compose detailed, highly actionable system instructions that directly command the agent's behavior. Respond in markdown as this text will be rendered in a rich text editor. Write instructions as clear imperatives, without preamble, assuming the agent identity is already established externally.
136
+ - **Schedule**: If the user specifies a schedule, you should also provide a cron expression for the agent to run on. The schedule should be in a proper cron expression and nothing more. Do not respond with any other information or explain your reasoning for the schedule, otherwise this will cause a parsing error that is undesirable.
137
+
138
+ ## ROLE & RESPONSIBILITY
139
+ - Clearly state the agent's primary mission, e.g., "Your primary mission is...", "Your core responsibility is...".
140
+ - Outline the exact tasks it handles, specifying expected input/output clearly.
141
+
142
+ ## INTERACTION STYLE
143
+ - Define exactly how to communicate with users: tone, format, response structure.
144
+ - Include explicit commands, e.g., "Always wrap responses in \`\`\`text\`\`\` blocks.", "Never add greetings or meta-information.", "Always provide outputs in user's requested languages."
145
+
146
+ ## OUTPUT FORMATTING RULES
147
+ - Clearly specify formatting standards required by the user (e.g., JSON, plain text, markdown).
148
+ - Include explicit examples to illustrate correct formatting.
149
+
150
+ ## LIMITATIONS & CONSTRAINTS
151
+ - Explicitly define boundaries of the agent's capabilities.
152
+ - Clearly state what the agent must never do or say.
153
+ - Include exact phrases for declining requests outside scope.
154
+
155
+ ## REAL-WORLD EXAMPLES
156
+ Provide two explicit interaction examples showing:
157
+ - User's typical request.
158
+ - Final agent response demonstrating perfect compliance.
159
+
160
+ Create an agent that feels thoughtfully designed, intelligent, and professionally reliable, perfectly matched to the user's original intent.
161
+
162
+ # YOUR TASK
163
+
164
+ Now, perform this analysis for the following inputs.
165
+
166
+ **INPUT - Conversation History:**
167
+ {conversation_history}
168
+
169
+ **INPUT - Used Tools:**
170
+ {tool_config}
171
+
172
+ **YOUR JSON OUTPUT:**
173
+ """
@@ -0,0 +1,24 @@
1
+ from collections.abc import Sequence
2
+ from typing import Annotated, TypedDict
3
+
4
+ from langchain_core.messages import BaseMessage
5
+ from langgraph.graph.message import add_messages
6
+ from pydantic import BaseModel, Field
7
+ from universal_mcp.types import ToolConfig
8
+
9
+
10
+ class Agent(BaseModel):
11
+ """Agent that can be created by the builder."""
12
+
13
+ name: str = Field(description="Name of the agent.")
14
+ description: str = Field(description="A small description of the agent.")
15
+ expertise: str = Field(description="The expertise of the agent.")
16
+ instructions: str = Field(description="The instructions for the agent to follow.")
17
+ schedule: str | None = Field(description="The cron expression for the agent to run on.", default=None)
18
+
19
+
20
+ class BuilderState(TypedDict):
21
+ user_task: str | None
22
+ generated_agent: Agent | None
23
+ tool_config: ToolConfig | None
24
+ messages: Annotated[Sequence[BaseMessage], add_messages]
@@ -6,6 +6,8 @@ from universal_mcp.logger import setup_logger
6
6
 
7
7
  from universal_mcp.agents import get_agent
8
8
 
9
+ import asyncio
10
+
9
11
  app = Typer()
10
12
 
11
13
 
@@ -18,7 +20,6 @@ app = Typer()
18
20
  )
19
21
  def run(name: str = "react"):
20
22
  """Run the agent CLI"""
21
- import asyncio
22
23
 
23
24
  setup_logger(log_file=None, level="ERROR")
24
25
  client = AgentrClient()
@@ -32,7 +33,7 @@ def run(name: str = "react"):
32
33
  },
33
34
  }
34
35
  agent_cls = get_agent(name)
35
- agent = agent_cls(name, **params)
36
+ agent = agent_cls(name=name, **params)
36
37
  asyncio.run(agent.run_interactive())
37
38
 
38
39
 
@@ -11,7 +11,7 @@ from universal_mcp.agents.utils import messages_to_list
11
11
  async def main():
12
12
  memory = MemorySaver()
13
13
  agent = CodeActAgent(
14
- "CodeAct Agent",
14
+ name="CodeAct Agent",
15
15
  instructions="Be very concise in your answers.",
16
16
  model="anthropic:claude-4-sonnet-20250514",
17
17
  tools={"google_mail": ["list_messages"]},
@@ -25,9 +25,7 @@ async def main():
25
25
  # user_input="Fetch unsubscribe links from my Gmail inbox for promo emails I have received in the last 7 days"
26
26
  # ):
27
27
  # print(event.content, end="")
28
- result = await agent.invoke(
29
- user_input="Fetch unsubscribe links from my Gmail inbox for promo emails I have received in the last 7 days"
30
- )
28
+ result = await agent.invoke(user_input="Get the 50th fibonacci number")
31
29
  print(messages_to_list(result["messages"]))
32
30
 
33
31
 
@@ -1,27 +1,34 @@
1
+ import ast
1
2
  from collections.abc import Callable
2
3
 
3
- from langchain_core.messages import AIMessageChunk
4
- from langchain_core.tools import StructuredTool
5
- from langchain_core.tools import tool as create_tool
4
+ from langchain_core.messages import AIMessage, HumanMessage
6
5
  from langgraph.checkpoint.base import BaseCheckpointSaver
7
- from langgraph.graph import END, StateGraph
6
+ from langgraph.graph import END, START, StateGraph
7
+ from langgraph.types import Command
8
+ from pydantic import BaseModel, Field
8
9
  from universal_mcp.logger import logger
9
10
  from universal_mcp.tools.registry import ToolRegistry
10
11
  from universal_mcp.types import ToolConfig, ToolFormat
11
12
 
12
13
  from universal_mcp.agents.base import BaseAgent
14
+ from universal_mcp.agents.codeact.models import SandboxOutput
13
15
  from universal_mcp.agents.codeact.prompts import (
14
- REFLECTION_PROMPT,
15
- RETRY_PROMPT,
16
16
  create_default_prompt,
17
17
  make_safe_function_name,
18
18
  )
19
19
  from universal_mcp.agents.codeact.sandbox import eval_unsafe
20
20
  from universal_mcp.agents.codeact.state import CodeActState
21
- from universal_mcp.agents.codeact.utils import extract_and_combine_codeblocks
22
21
  from universal_mcp.agents.llm import load_chat_model
23
22
 
24
23
 
24
+ class StructuredCodeResponse(BaseModel):
25
+ """Structured response for the CodeAct agent."""
26
+
27
+ reasoning: str = Field(..., description="The reasoning behind the generated script.")
28
+ script: str | None = Field(default=None, description="The Python script to be executed.")
29
+ task_complete: bool = Field(..., description="Whether the task is complete.")
30
+
31
+
25
32
  class CodeActAgent(BaseAgent):
26
33
  def __init__(
27
34
  self,
@@ -34,127 +41,200 @@ class CodeActAgent(BaseAgent):
34
41
  sandbox_timeout: int = 20,
35
42
  **kwargs,
36
43
  ):
37
- super().__init__(name, instructions, model, memory, **kwargs)
44
+ super().__init__(
45
+ name=name,
46
+ instructions=instructions,
47
+ model=model,
48
+ memory=memory,
49
+ **kwargs,
50
+ )
38
51
  self.model_instance = load_chat_model(model, thinking=False)
39
52
  self.tools_config = tools or {}
40
53
  self.registry = registry
41
54
  self.eval_fn = eval_unsafe
42
- self.reflection_prompt = REFLECTION_PROMPT
43
- self.reflection_model = self.model_instance
44
- self.max_reflections = 3
45
- self.tools_context = {}
46
- self.context = {}
47
55
  self.sandbox_timeout = sandbox_timeout
48
- self.processed_tools: list[StructuredTool | Callable] = []
56
+ self.processed_tools: dict[str, Callable] = {}
49
57
 
50
58
  async def _build_graph(self):
51
59
  if self.tools_config:
52
60
  if not self.registry:
53
61
  raise ValueError("Tools are configured but no registry is provided")
54
- # Langchain tools are fine
55
- exported_tools = await self.registry.export_tools(self.tools_config, ToolFormat.LANGCHAIN)
56
- self.processed_tools = [t if isinstance(t, StructuredTool) else create_tool(t) for t in exported_tools]
62
+ # Load native tools, these are python functions
63
+ exported_tools = await self.registry.export_tools(self.tools_config, ToolFormat.NATIVE)
64
+ for tool in exported_tools:
65
+ name = tool.__name__
66
+ safe_name = make_safe_function_name(name)
67
+ if name != safe_name:
68
+ logger.warning(f"Tool name {name} is not safe, using {safe_name} instead")
69
+ raise ValueError(f"Tool name {name} is not safe, using {safe_name} instead")
70
+ self.processed_tools[safe_name] = tool
57
71
 
58
72
  self.instructions = create_default_prompt(self.processed_tools, self.instructions)
59
73
 
60
- for tool in self.processed_tools:
61
- safe_name = make_safe_function_name(tool.name)
62
- tool_callable = tool.coroutine if hasattr(tool, "coroutine") and tool.coroutine is not None else tool.func
63
- self.tools_context[safe_name] = tool_callable
64
-
65
- self.context = {**self.context, **self.tools_context}
66
-
67
74
  agent = StateGraph(CodeActState)
68
75
  agent.add_node("call_model", self.call_model)
76
+ agent.add_node("validate_code", self.validate_code)
69
77
  agent.add_node("sandbox", self.sandbox)
78
+ agent.add_node("final_answer", self.final_answer)
70
79
 
71
- agent.set_entry_point("call_model")
72
- agent.add_conditional_edges(
73
- "call_model",
74
- self.should_run_sandbox,
75
- {
76
- "sandbox": "sandbox",
77
- END: END,
78
- },
79
- )
80
- agent.add_edge("sandbox", "call_model")
81
- return agent.compile(checkpointer=self.memory)
82
-
83
- def should_run_sandbox(self, state: CodeActState) -> str:
84
- last_message = state["messages"][-1]
85
- if isinstance(last_message.content, str) and "TASK_COMPLETE" in last_message.content:
86
- return END
80
+ agent.add_edge(START, "call_model")
87
81
 
88
- if state.get("script"):
89
- return "sandbox"
90
- return END
82
+ return agent.compile(checkpointer=self.memory)
91
83
 
92
- def _extract_content(self, response: AIMessageChunk) -> str:
93
- if isinstance(response.content, list):
94
- content = " ".join([c.get("text", "") for c in response.content])
95
- else:
96
- content = response.content
97
- return content
98
-
99
- async def call_model(self, state: CodeActState) -> dict:
100
- model = self.model_instance
101
- reflection_model = self.reflection_model
102
-
103
- messages = [{"role": "system", "content": self.instructions}] + state["messages"]
104
-
105
- response = await model.ainvoke(messages)
106
-
107
- text_content = self._extract_content(response)
108
- if not isinstance(text_content, str):
109
- raise ValueError(f"Content is not a string: {text_content}")
110
- code = extract_and_combine_codeblocks(text_content)
111
- logger.debug(f"Code: {code}")
112
-
113
- if self.max_reflections > 0 and code:
114
- reflection_count = 0
115
- while reflection_count < self.max_reflections:
116
- conversation_history = "\n".join(
117
- [
118
- f'<message role="{("user" if m.type == "human" else "assistant")}">\n{m.content}\n</message>'
119
- for m in state["messages"]
120
- ]
84
+ async def call_model(self, state: CodeActState) -> Command:
85
+ logger.debug(f"Calling model with state: {state}")
86
+ model = self.model_instance.with_structured_output(StructuredCodeResponse)
87
+
88
+ # Find the last script and its output in the message history
89
+ previous_script = state.get("script", "")
90
+ sandbox_output = state.get("sandbox_output", "")
91
+ syntax_error = state.get("syntax_error", "")
92
+
93
+ logger.debug(f"Previous script:\n {previous_script}")
94
+ logger.debug(f"Sandbox output:\n {sandbox_output}")
95
+ logger.debug(f"Syntax error:\n {syntax_error}")
96
+
97
+ prompt_messages = [
98
+ {"role": "system", "content": self.instructions},
99
+ *state["messages"],
100
+ ]
101
+ if previous_script:
102
+ feedback_message = (
103
+ f"Here is the script you generated in the last turn:\n\n```python\n{previous_script}\n```\n\n"
104
+ )
105
+ if syntax_error:
106
+ feedback_message += (
107
+ f"When parsing the script, it produced the following syntax error:\n\n```\n{syntax_error}\n```\n\n"
108
+ "Please fix the syntax and generate a new, correct script."
121
109
  )
122
- conversation_history += f'\n<message role="assistant">\n{response.content}\n</message>'
123
-
124
- formatted_prompt = REFLECTION_PROMPT.format(conversation_history=conversation_history)
125
-
126
- reflection_messages = [
127
- {"role": "system", "content": self.reflection_prompt},
128
- {"role": "user", "content": formatted_prompt},
129
- ]
130
- reflection_result = await reflection_model.ainvoke(reflection_messages)
131
-
132
- if "NONE" in reflection_result.content:
133
- break
134
-
135
- retry_prompt = RETRY_PROMPT.format(reflection_result=reflection_result.content)
136
-
137
- regeneration_messages = [
138
- {"role": "system", "content": self.instructions},
139
- *state["messages"],
140
- {"role": "assistant", "content": response.content},
141
- {"role": "user", "content": retry_prompt},
142
- ]
143
- response = await model.ainvoke(regeneration_messages)
144
-
145
- code = extract_and_combine_codeblocks(response.content)
146
-
147
- if not code:
148
- break
110
+ elif sandbox_output:
111
+ feedback_message += (
112
+ f"When executed, it produced the following output:\n\n```\n{sandbox_output}\n```\n\n"
113
+ )
114
+ feedback_message += "Based on this output, decide if the task is complete. If it is, respond the final answer to the user in clean and readable Markdown format. Important: set `task_complete` to `True` and no need to provide script. If the task is not complete, generate a new script to get closer to the solution."
115
+
116
+ prompt_messages.append({"role": "user", "content": feedback_message})
117
+
118
+ response: StructuredCodeResponse = await model.ainvoke(prompt_messages)
119
+
120
+ # We add the reasoning as the AI message content
121
+ ai_message = AIMessage(content=response.reasoning)
122
+
123
+ if response.task_complete:
124
+ return Command(
125
+ goto="final_answer",
126
+ update={
127
+ "messages": [ai_message],
128
+ "script": response.script,
129
+ "task_complete": response.task_complete,
130
+ "sandbox_output": sandbox_output,
131
+ "syntax_error": None,
132
+ },
133
+ )
134
+ else:
135
+ return Command(
136
+ goto="validate_code",
137
+ update={
138
+ "messages": [ai_message],
139
+ "script": response.script,
140
+ "task_complete": response.task_complete,
141
+ "sandbox_output": None,
142
+ "syntax_error": None,
143
+ },
144
+ )
145
+
146
+ async def validate_code(self, state: CodeActState) -> Command:
147
+ logger.debug(f"Validating code with script:\n {state['script']}")
148
+ script = state.get("script")
149
+
150
+ if not script:
151
+ return Command(
152
+ goto="call_model",
153
+ update={
154
+ "syntax_error": "Model did not provide a script but task is not complete. Please provide a script or set task_complete to True."
155
+ },
156
+ )
157
+
158
+ try:
159
+ ast.parse(script)
160
+ logger.debug("AST parsing successful.")
161
+ return Command(
162
+ goto="sandbox",
163
+ update={
164
+ "syntax_error": None,
165
+ },
166
+ )
167
+ except SyntaxError as e:
168
+ logger.warning(f"AST parsing failed: {e}")
169
+ return Command(
170
+ goto="call_model",
171
+ update={
172
+ "syntax_error": f"Syntax Error: {e}",
173
+ },
174
+ )
175
+
176
+ async def sandbox(self, state: CodeActState) -> Command:
177
+ logger.debug(f"Running sandbox with script:\n {state['script']}")
178
+ tools_context = {}
179
+ for tool_name, tool_callable in self.processed_tools.items():
180
+ tools_context[tool_name] = tool_callable
181
+
182
+ output: SandboxOutput
183
+ output, _ = await self.eval_fn(state["script"], tools_context, self.sandbox_timeout)
184
+
185
+ # Format the output for the agent
186
+ formatted_output = "Code executed.\n\n"
187
+ MAX_OUTPUT_LEN = 20000 # Maximum number of characters to show for stdout/stderr
188
+
189
+ def truncate_output(text, max_len=MAX_OUTPUT_LEN):
190
+ if text is None:
191
+ return ""
192
+ text = text.strip()
193
+ if len(text) > max_len:
194
+ return text[:max_len] + "\n... (more output hidden)"
195
+ return text
196
+
197
+ if output.stdout:
198
+ truncated_stdout = truncate_output(output.stdout)
199
+ formatted_output += f"STDOUT:\n```\n{truncated_stdout}\n```\n\n"
200
+ if output.error:
201
+ truncated_stderr = truncate_output(output.error)
202
+ formatted_output += f"STDERR / ERROR:\n```\n{truncated_stderr}\n```\n"
203
+ if output.return_value is not None:
204
+ formatted_output += f"RETURN VALUE:\n```\n{repr(output.return_value)}\n```\n"
205
+
206
+ logger.debug(f"Sandbox output: {formatted_output}")
207
+ return Command(
208
+ goto="call_model",
209
+ update={"sandbox_output": formatted_output.strip()},
210
+ )
149
211
 
150
- reflection_count += 1
212
+ async def final_answer(self, state: CodeActState) -> Command:
213
+ logger.debug("Formatting final answer using LLM for markdown formatting.")
214
+
215
+ # Extract the original user prompt
216
+ user_prompt = ""
217
+ for msg in state["messages"]:
218
+ if isinstance(msg, HumanMessage):
219
+ user_prompt = msg.content
220
+ break
221
+
222
+ # Compose a prompt for the LLM to generate a concise, markdown-formatted answer
223
+ llm_prompt = (
224
+ "Given the following task and answer, write a concise, well-formatted markdown response suitable for a user.\n\n"
225
+ f"Task:\n{user_prompt}\n\n"
226
+ f"Answer:\n{state['sandbox_output']}\n\n"
227
+ "Respond only with the markdown-formatted answer."
228
+ )
151
229
 
152
- return {"messages": [response], "script": code}
230
+ # Use the model to generate the final formatted answer
231
+ response = await self.model_instance.ainvoke([{"role": "user", "content": llm_prompt}])
232
+ markdown_answer = response.content if hasattr(response, "content") else str(response)
233
+ logger.debug(f"Final answer:\n {markdown_answer}")
153
234
 
154
- async def sandbox(self, state: CodeActState) -> dict:
155
- output, new_vars = await self.eval_fn(state["script"], self.context, timeout=self.sandbox_timeout)
156
- self.context = {**self.context, **new_vars}
157
- return {
158
- "messages": [AIMessageChunk(content=output.strip())],
159
- "script": None,
160
- }
235
+ return Command(
236
+ goto=END,
237
+ update={
238
+ "messages": [AIMessage(content=markdown_answer)],
239
+ },
240
+ )
@@ -0,0 +1,11 @@
1
+ from typing import Any
2
+
3
+ from pydantic import BaseModel
4
+
5
+
6
+ class SandboxOutput(BaseModel):
7
+ """Structured output from the code sandbox."""
8
+
9
+ stdout: str
10
+ error: str | None = None
11
+ return_value: Any | None = None