Skip to content

Commit

Permalink
fix PS1 so PS1JSON works
Browse files Browse the repository at this point in the history
  • Loading branch information
xingyaoww committed Nov 10, 2024
1 parent e304973 commit c743833
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
7 changes: 5 additions & 2 deletions openhands/events/observation/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,19 @@ class CmdOutputMetadata(BaseModel):
def to_ps1_prompt(cls) -> str:
"""Convert the required metadata into a PS1 prompt."""
prompt = '###PS1JSON###\n'
prompt += json.dumps(
json_str = json.dumps(
{
'exit_code': '$?',
'username': r'\u',
'hostname': r'\h',
'working_dir': r'\w',
'py_interpreter_path': '$(which python 2>/dev/null || echo "")',
'py_interpreter_path': r'$(which python 2>/dev/null || echo "")',
},
indent=2,
)
# Make sure we escape double quotes in the JSON string
# So that PS1 will keep them as part of the output
prompt += json_str.replace('"', '\\"')
prompt += '\n###PS1END###\n'
return prompt

Expand Down
1 change: 1 addition & 0 deletions tests/unit/test_bash_ps1_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ def test_ps1_metadata_format():
prompt = CmdOutputMetadata.to_ps1_prompt()
assert prompt.startswith('###PS1JSON###\n')
assert prompt.endswith('###PS1END###\n')
assert r'\"exit_code\"' in prompt, "PS1 prompt should contain escaped double quotes"
print(prompt)


Expand Down

0 comments on commit c743833

Please sign in to comment.