Skip to content

Commit

Permalink
Fix pr #5246: Fix issue #5015: [Bug]: Headless mode awaits for reques…
Browse files Browse the repository at this point in the history
…ted user feedb…
  • Loading branch information
openhands-agent committed Nov 25, 2024
1 parent 7af3f5f commit d893bea
Show file tree
Hide file tree
Showing 21 changed files with 359 additions and 342 deletions.
2 changes: 1 addition & 1 deletion frontend/public/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
"APP_MODE": "oss",
"GITHUB_CLIENT_ID": "",
"POSTHOG_CLIENT_KEY": "phc_3ESMmY9SgqEAGBB6sMGK5ayYHkeUuknH2vP6FmWH9RA"
}
}
4 changes: 3 additions & 1 deletion openhands/agenthub/codeact_agent/codeact_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,9 @@ def get_action_message(
)
]
elif isinstance(action, CmdRunAction) and action.source == 'user':
content = [TextContent(text=f'User executed the command:\n{action.command}')]
content = [
TextContent(text=f'User executed the command:\n{action.command}')
]
return [
Message(
role='user',
Expand Down
11 changes: 6 additions & 5 deletions openhands/controller/agent_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class AgentController:
delegate: 'AgentController | None' = None
_pending_action: Action | None = None
_closed: bool = False
fake_user_response_fn: Callable[[str], str] | None = None
filter_out: ClassVar[tuple[type[Event], ...]] = (
NullAction,
NullObservation,
Expand Down Expand Up @@ -114,10 +115,10 @@ def __init__(
self.id = sid
self.agent = agent
self.headless_mode = headless_mode

# Set up default fake user response function for headless mode
if headless_mode and fake_user_response_fn is None:
self.fake_user_response_fn = lambda _: "continue"
self.fake_user_response_fn = lambda _: 'continue'
else:
self.fake_user_response_fn = fake_user_response_fn

Expand Down Expand Up @@ -319,7 +320,7 @@ async def _handle_message_action(self, action: MessageAction):
elif action.source == EventSource.AGENT and action.wait_for_response:
if self.headless_mode:
# In headless mode, we should use a fake user response if provided
if self.fake_user_response_fn:
if self.fake_user_response_fn is not None:
response = self.fake_user_response_fn(action.content)
self.event_stream.add_event(
MessageAction(content=response),
Expand All @@ -333,8 +334,8 @@ async def _handle_message_action(self, action: MessageAction):
)
else:
# Display the message content to help user understand what input is expected
print(f"\nAgent is requesting input: {action.content}")
print("Request user input >> ", end="", flush=True)
print(f'\nAgent is requesting input: {action.content}')
print('Request user input >> ', end='', flush=True)
await self.set_agent_state_to(AgentState.AWAITING_USER_INPUT)

def reset_task(self):
Expand Down
1 change: 1 addition & 0 deletions openhands/events/action/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def images_urls(self):
@images_urls.setter
def images_urls(self, value):
self.image_urls = value

def __str__(self) -> str:
ret = f'**MessageAction** (source={self.source})\n'
ret += f'CONTENT: {self.content}'
Expand Down
2 changes: 1 addition & 1 deletion openhands/events/serialization/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def action_from_dict(action: dict) -> Action:
# images_urls has been renamed to image_urls
if 'images_urls' in args:
args['image_urls'] = args.pop('images_urls')

try:
decoded_action = action_class(**args)
if 'timeout' in action:
Expand Down
4 changes: 2 additions & 2 deletions openhands/resolver/patching/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-

from .patch import parse_patch
from .apply import apply_diff
from .patch import parse_patch

__all__ = ["parse_patch", "apply_diff"]
__all__ = ['parse_patch', 'apply_diff']
34 changes: 17 additions & 17 deletions openhands/resolver/patching/apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,33 @@

def _apply_diff_with_subprocess(diff, lines, reverse=False):
# call out to patch program
patchexec = which("patch")
patchexec = which('patch')
if not patchexec:
raise SubprocessException("cannot find patch program", code=-1)
raise SubprocessException('cannot find patch program', code=-1)

tempdir = tempfile.gettempdir()

filepath = os.path.join(tempdir, "wtp-" + str(hash(diff.header)))
oldfilepath = filepath + ".old"
newfilepath = filepath + ".new"
rejfilepath = filepath + ".rej"
patchfilepath = filepath + ".patch"
with open(oldfilepath, "w") as f:
f.write("\n".join(lines) + "\n")
filepath = os.path.join(tempdir, 'wtp-' + str(hash(diff.header)))
oldfilepath = filepath + '.old'
newfilepath = filepath + '.new'
rejfilepath = filepath + '.rej'
patchfilepath = filepath + '.patch'
with open(oldfilepath, 'w') as f:
f.write('\n'.join(lines) + '\n')

with open(patchfilepath, "w") as f:
with open(patchfilepath, 'w') as f:
f.write(diff.text)

args = [
patchexec,
"--reverse" if reverse else "--forward",
"--quiet",
"--no-backup-if-mismatch",
"-o",
'--reverse' if reverse else '--forward',
'--quiet',
'--no-backup-if-mismatch',
'-o',
newfilepath,
"-i",
'-i',
patchfilepath,
"-r",
'-r',
rejfilepath,
oldfilepath,
]
Expand All @@ -58,7 +58,7 @@ def _apply_diff_with_subprocess(diff, lines, reverse=False):

# do this last to ensure files get cleaned up
if ret != 0:
raise SubprocessException("patch program failed", code=ret)
raise SubprocessException('patch program failed', code=ret)

return lines, rejlines

Expand Down
2 changes: 1 addition & 1 deletion openhands/resolver/patching/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def __init__(self, msg, hunk=None):
self.hunk = hunk
if hunk is not None:
super(HunkException, self).__init__(
"{msg}, in hunk #{n}".format(msg=msg, n=hunk)
'{msg}, in hunk #{n}'.format(msg=msg, n=hunk)
)
else:
super(HunkException, self).__init__(msg)
Expand Down
Loading

0 comments on commit d893bea

Please sign in to comment.