Skip to content

Commit 24b5df3

Browse files
Add ruff configuration for import sorting (#17)
Co-authored-by: openhands <openhands@all-hands.dev>
1 parent 5499e21 commit 24b5df3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+605
-1218
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ repos:
44
- id: uv-format
55
name: Format with uv format
66
entry: uv
7-
args: [format]
7+
args: [run, ruff, format]
88
language: system
99
types: [python]
1010
pass_filenames: false

examples/hello_world.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
import os
2+
23
from pydantic import SecretStr
4+
35
from openhands.core import (
4-
OpenHandsConfig,
6+
LLM,
7+
CodeActAgent,
58
Conversation,
69
LLMConfig,
710
Message,
11+
OpenHandsConfig,
812
TextContent,
9-
LLM,
1013
Tool,
1114
get_logger,
12-
CodeActAgent,
1315
)
1416
from openhands.core.runtime.tools import (
1517
BashExecutor,
@@ -18,6 +20,7 @@
1820
str_replace_editor_tool,
1921
)
2022

23+
2124
logger = get_logger(__name__)
2225

2326
# Configure LLM
@@ -48,10 +51,6 @@
4851
conversation.send_message(
4952
message=Message(
5053
role="user",
51-
content=[
52-
TextContent(
53-
text="Hello! Can you create a new Python file named hello.py that prints 'Hello, World!'?"
54-
)
55-
],
54+
content=[TextContent(text="Hello! Can you create a new Python file named hello.py that prints 'Hello, World!'?")],
5655
)
5756
)

openhands/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
"""OpenHands package."""
22

33
import os
4-
from pathlib import Path
54
from importlib.metadata import PackageNotFoundError, version
5+
from pathlib import Path
6+
67

78
__package_name__ = "openhands-ai"
89

openhands/core/__init__.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
from .agenthub import AgentBase, CodeActAgent
2-
from .llm import LLM, Message, TextContent, ImageContent
3-
from .runtime import Tool, ActionBase, ObservationBase
4-
from .config import OpenHandsConfig, LLMConfig, MCPConfig
5-
from .logger import get_logger
2+
from .config import LLMConfig, MCPConfig, OpenHandsConfig
63
from .conversation import Conversation
4+
from .llm import LLM, ImageContent, Message, TextContent
5+
from .logger import get_logger
6+
from .runtime import ActionBase, ObservationBase, Tool
7+
78

89
__all__ = [
910
"LLM",

openhands/core/agenthub/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
from .codeact_agent import CodeActAgent
21
from .agent import AgentBase
2+
from .codeact_agent import CodeActAgent
33
from .history import AgentHistory
44

5+
56
__all__ = [
67
"CodeActAgent",
78
"AgentBase",

openhands/core/agenthub/agent.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
from typing import Callable
22

3-
from openhands.core.llm import LLM
4-
from openhands.core.runtime import Tool, ActionBase, ObservationBase
53
from openhands.core.context.env_context import EnvContext
4+
from openhands.core.llm import LLM
65
from openhands.core.llm.message import Message
76
from openhands.core.logger import get_logger
7+
from openhands.core.runtime import ActionBase, ObservationBase, Tool
8+
89

910
logger = get_logger(__name__)
1011

@@ -58,8 +59,7 @@ def reset(self) -> None:
5859
def run(
5960
self,
6061
user_input: Message,
61-
on_event: Callable[[Message | ActionBase | ObservationBase], None]
62-
| None = None,
62+
on_event: Callable[[Message | ActionBase | ObservationBase], None] | None = None,
6363
) -> None:
6464
"""Runs the Agent with the given input and returns the output.
6565
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from .codeact_agent import CodeActAgent
22

3+
34
__all__ = [
45
"CodeActAgent",
56
]

openhands/core/agenthub/codeact_agent/codeact_agent.py

Lines changed: 21 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
1-
import os
21
import json
2+
import os
33
from 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
135
from 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+
2022
logger = 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:

openhands/core/agenthub/history.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from pydantic import BaseModel, Field
2+
23
from openhands.core.llm import Message
34

45

openhands/core/config/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
from .openhands_config import OpenHandsConfig
21
from .llm_config import LLMConfig
32
from .mcp_config import MCPConfig
3+
from .openhands_config import OpenHandsConfig
4+
45

56
__all__ = ["OpenHandsConfig", "LLMConfig", "MCPConfig"]

0 commit comments

Comments
 (0)