π OpenHands: AI-Driven Development
feat: simplify state.history with to_memory call in micro-agent. Or the call to LLM may exceed the token limit. (#1806)
Xia Zhenhua76abca361c53
diff --git a/agenthub/micro/agent.py b/agenthub/micro/agent.pyindex 0957549b0c..08275e48d2 100644--- a/agenthub/micro/agent.py+++ b/agenthub/micro/agent.py@@ -5,6 +5,7 @@ from opendevin.controller.state.state import Statefrom opendevin.core.utils import jsonfrom opendevin.events.action import Actionfrom opendevin.events.serialization.action import action_from_dict+from opendevin.events.serialization.event import event_to_memoryfrom opendevin.llm.llm import LLMfrom .instructions import instructions@@ -26,6 +27,18 @@ def to_json(obj, **kwargs):return json.dumps(obj, **kwargs)+def history_to_json(obj, **kwargs):+ """+ Serialize and simplify history to str format+ """+ if isinstance(obj, list):+ # process history, make it simpler.+ processed_history = []+ for action, observation in obj:+ processed_history.append((event_to_memory(action), event_to_memory(observation)))+ return json.dumps(processed_history, **kwargs)++class MicroAgent(Agent):prompt = ''agent_definition: dict = {}@@ -44,6 +57,7 @@ class MicroAgent(Agent):state=state,instructions=instructions,to_json=to_json,+ history_to_json=history_to_json,delegates=self.delegates,latest_user_message=latest_user_message,)diff --git a/agenthub/micro/coder/prompt.md b/agenthub/micro/coder/prompt.mdindex 28a196628f..49c24123f3 100644--- a/agenthub/micro/coder/prompt.md+++ b/agenthub/micro/coder/prompt.md@@ -21,7 +21,7 @@ Do NOT finish until you have completed the tasks.## History{{ instructions.history_truncated }}-{{ to_json(state.history[-10:]) }}+{{ history_to_json(state.history[-10:]) }}## Format{{ instructions.format.action }}diff --git a/agenthub/micro/commit_writer/prompt.md b/agenthub/micro/commit_writer/prompt.mdindex 7a3077e5a0..38322ba077 100644--- a/agenthub/micro/commit_writer/prompt.md+++ b/agenthub/micro/commit_writer/prompt.md@@ -18,7 +18,7 @@ the `reject` action with `outputs.answer` set to the reason.## History{{ instructions.history_truncated }}-{{ to_json(state.history[-10:]) }}+{{ history_to_json(state.history[-10:]) }}If the last item in the history is an error, you should try to fix it.diff --git a/agenthub/micro/manager/prompt.md b/agenthub/micro/manager/prompt.mdindex 283bd40473..27080ccf04 100644--- a/agenthub/micro/manager/prompt.md+++ b/agenthub/micro/manager/prompt.md@@ -17,7 +17,7 @@ provide the correct inputs for the delegate you select.## History{{ instructions.history_truncated }}-{{ to_json(state.history[-10:]) }}+{{ history_to_json(state.history[-10:]) }}## Available Actions{{ instructions.actions.delegate }}diff --git a/agenthub/micro/math_agent/prompt.md b/agenthub/micro/math_agent/prompt.mdindex fc362fc644..831dab8978 100644--- a/agenthub/micro/math_agent/prompt.md+++ b/agenthub/micro/math_agent/prompt.md@@ -10,7 +10,7 @@ and call the `finish` action with `outputs.answer` set to the answer.## History{{ instructions.history_truncated }}-{{ to_json(state.history[-10:]) }}+{{ history_to_json(state.history[-10:]) }}If the last item in the history is an error, you should try to fix it.diff --git a/agenthub/micro/postgres_agent/prompt.md b/agenthub/micro/postgres_agent/prompt.mdindex 9250bdd54f..707f8240d3 100644--- a/agenthub/micro/postgres_agent/prompt.md+++ b/agenthub/micro/postgres_agent/prompt.md@@ -18,7 +18,7 @@ You may take any of the following actions:## History{{ instructions.history_truncated }}-{{ to_json(state.history[-10:]) }}+{{ history_to_json(state.history[-10:]) }}## Format{{ instructions.format.action }}diff --git a/agenthub/micro/repo_explorer/prompt.md b/agenthub/micro/repo_explorer/prompt.mdindex ccf421c9ff..fde381e40c 100644--- a/agenthub/micro/repo_explorer/prompt.md+++ b/agenthub/micro/repo_explorer/prompt.md@@ -20,7 +20,7 @@ When you're done, put your summary into the output of the `finish` action.## History{{ instructions.history_truncated }}-{{ to_json(state.history[-10:]) }}+{{ history_to_json(state.history[-10:]) }}## Format{{ instructions.format.action }}diff --git a/agenthub/micro/study_repo_for_task/prompt.md b/agenthub/micro/study_repo_for_task/prompt.mdindex cee45798cd..7fa2a0c6e3 100644--- a/agenthub/micro/study_repo_for_task/prompt.md+++ b/agenthub/micro/study_repo_for_task/prompt.md@@ -19,7 +19,7 @@ When you're done, put your summary in `outputs.summary` in the `finish` action.## History{{ instructions.history_truncated }}-{{ to_json(state.history[-10:]) }}+{{ history_to_json(state.history[-10:]) }}## Format{{ instructions.format.action }}diff --git a/agenthub/micro/typo_fixer_agent/prompt.md b/agenthub/micro/typo_fixer_agent/prompt.mdindex 0f33348131..5b297daa8d 100644--- a/agenthub/micro/typo_fixer_agent/prompt.md+++ b/agenthub/micro/typo_fixer_agent/prompt.md@@ -23,7 +23,7 @@ Do NOT finish until you have fixed all the typos and generated a summary.## History{{ instructions.history_truncated }}-{{ to_json(state.history[-5:]) }}+{{ history_to_json(state.history[-5:]) }}## Format{{ instructions.format.action }}diff --git a/agenthub/micro/verifier/prompt.md b/agenthub/micro/verifier/prompt.mdindex ba991cbe10..7a6821c665 100644--- a/agenthub/micro/verifier/prompt.md+++ b/agenthub/micro/verifier/prompt.md@@ -21,7 +21,7 @@ explaining what the problem is.## History{{ instructions.history_truncated }}-{{ to_json(state.history[-10:]) }}+{{ history_to_json(state.history[-10:]) }}## Format{{ instructions.format.action }}