1- import os
21import json
2+ import os
33from typing import Callable
44
5- from openhands .core .logger import get_logger
6- from openhands .core .context .prompt import PromptManager
7- from openhands .core .context .env_context import EnvContext
8- from openhands .core .llm import LLM , Message , TextContent , get_llm_metadata
9- from openhands .core .runtime import Tool , ObservationBase , ActionBase
10- from openhands .core .runtime .tools import finish_tool , FinishAction
11- from openhands .core .agenthub .agent import AgentBase
12- from openhands .core .agenthub .history import AgentHistory
135from litellm .types .utils import (
146 ChatCompletionMessageToolCall ,
7+ Choices ,
158 Message as LiteLLMMessage ,
169 ModelResponse ,
17- Choices ,
1810)
1911
12+ from openhands .core .agenthub .agent import AgentBase
13+ from openhands .core .agenthub .history import AgentHistory
14+ from openhands .core .context .env_context import EnvContext
15+ from openhands .core .context .prompt import PromptManager
16+ from openhands .core .llm import LLM , Message , TextContent , get_llm_metadata
17+ from openhands .core .logger import get_logger
18+ from openhands .core .runtime import ActionBase , ObservationBase , Tool
19+ from openhands .core .runtime .tools import FinishAction , finish_tool
20+
21+
2022logger = get_logger (__name__ )
2123
2224
@@ -34,9 +36,7 @@ def __init__(
3436 prompt_dir = os .path .join (os .path .dirname (__file__ ), "prompts" ),
3537 system_prompt_filename = system_prompt_filename ,
3638 )
37- self .system_message : TextContent = self .prompt_manager .get_system_message (
38- cli_mode = cli_mode
39- )
39+ self .system_message : TextContent = self .prompt_manager .get_system_message (cli_mode = cli_mode )
4040 self .history : AgentHistory = AgentHistory ()
4141 self .max_iterations : int = 10
4242
@@ -47,8 +47,7 @@ def reset(self) -> None:
4747 def run (
4848 self ,
4949 user_input : Message ,
50- on_event : Callable [[Message | ActionBase | ObservationBase ], None ]
51- | None = None ,
50+ on_event : Callable [[Message | ActionBase | ObservationBase ], None ] | None = None ,
5251 ) -> None :
5352 assert user_input .role == "user" , "Input message must have role 'user'"
5453
@@ -59,9 +58,7 @@ def run(
5958 on_event (sys_msg )
6059 content = user_input .content
6160 if self .env_context :
62- initial_env_context : list [TextContent ] = self .env_context .render (
63- self .prompt_manager
64- )
61+ initial_env_context : list [TextContent ] = self .env_context .render (self .prompt_manager )
6562 content += initial_env_context
6663 user_msg = Message (role = "user" , content = content )
6764 self .history .messages .append (user_msg )
@@ -70,9 +67,7 @@ def run(
7067
7168 if self .env_context and self .env_context .activated_microagents :
7269 for microagent in self .env_context .activated_microagents :
73- self .history .microagent_activations .append (
74- (microagent .name , len (self .history .messages ) - 1 )
75- )
70+ self .history .microagent_activations .append ((microagent .name , len (self .history .messages ) - 1 ))
7671
7772 else :
7873 self .history .messages .append (user_input )
@@ -85,15 +80,9 @@ def run(
8580 response : ModelResponse = self .llm .completion (
8681 messages = self .llm .format_messages_for_llm (self .history .messages ),
8782 tools = [tool .to_openai_tool () for tool in self .tools ],
88- extra_body = {
89- "metadata" : get_llm_metadata (
90- model_name = self .llm .config .model , agent_name = self .name
91- )
92- },
93- )
94- assert len (response .choices ) == 1 and isinstance (
95- response .choices [0 ], Choices
83+ extra_body = {"metadata" : get_llm_metadata (model_name = self .llm .config .model , agent_name = self .name )},
9684 )
85+ assert len (response .choices ) == 1 and isinstance (response .choices [0 ], Choices )
9786 llm_message : LiteLLMMessage = response .choices [0 ].message # type: ignore
9887 message = Message .from_litellm_message (llm_message )
9988 self .history .messages .append (message )
@@ -107,9 +96,7 @@ def run(
10796
10897 if tool_call .function .name == finish_tool .name :
10998 try :
110- action = FinishAction .model_validate (
111- json .loads (tool_call .function .arguments )
112- )
99+ action = FinishAction .model_validate (json .loads (tool_call .function .arguments ))
113100 if on_event :
114101 on_event (action )
115102 finally :
@@ -126,18 +113,15 @@ def run(
126113 def _execute_tool_call (
127114 self ,
128115 tool_call : ChatCompletionMessageToolCall ,
129- on_event : Callable [[Message | ActionBase | ObservationBase ], None ]
130- | None = None ,
116+ on_event : Callable [[Message | ActionBase | ObservationBase ], None ] | None = None ,
131117 ) -> Message :
132118 tool_name = tool_call .function .name
133119 assert tool_name is not None , "Tool call must have a name"
134120 tool = self .get_tool (tool_name )
135121 if tool is None :
136122 raise ValueError (f"Tool '{ tool_name } ' called by LLM is not found" )
137123
138- action : ActionBase = tool .action_type .model_validate (
139- json .loads (tool_call .function .arguments )
140- )
124+ action : ActionBase = tool .action_type .model_validate (json .loads (tool_call .function .arguments ))
141125 if on_event :
142126 on_event (action )
143127 if tool .executor is None :
0 commit comments