mirror of
https://github.com/OpenHands/OpenHands.git
synced 2025-12-26 05:48:36 +08:00
fix(backend): Return 400 if trying to open a binary file (#7825)
This commit is contained in:
parent
03b8b8c19a
commit
72b5e18898
@ -96,7 +96,7 @@ Phase 1. READING: read the problem and reword it in clearer terms
|
||||
Phase 2. RUNNING: install and run the tests on the repository
|
||||
2.1 Follow the readme
|
||||
2.2 Install the environment and anything needed
|
||||
2.2 Iterate and figure out how to run the tests
|
||||
2.2 Iterate and figure out how to run the tests
|
||||
|
||||
Phase 3. EXPLORATION: find the files that are related to the problem and possible solutions
|
||||
3.1 Use `grep` to search for relevant methods, classes, keywords and error messages.
|
||||
@ -238,7 +238,7 @@ def get_config(
|
||||
def initialize_runtime(
|
||||
runtime: Runtime,
|
||||
instance: pd.Series, # this argument is not required
|
||||
metadata: EvalMetadata
|
||||
metadata: EvalMetadata,
|
||||
):
|
||||
"""Initialize the runtime for the agent.
|
||||
|
||||
|
||||
@ -18,6 +18,7 @@ from contextlib import asynccontextmanager
|
||||
from pathlib import Path
|
||||
from zipfile import ZipFile
|
||||
|
||||
from binaryornot.check import is_binary
|
||||
from fastapi import Depends, FastAPI, HTTPException, Request, UploadFile
|
||||
from fastapi.exceptions import RequestValidationError
|
||||
from fastapi.responses import FileResponse, HTMLResponse, JSONResponse
|
||||
@ -355,6 +356,11 @@ class ActionExecutor:
|
||||
|
||||
async def read(self, action: FileReadAction) -> Observation:
|
||||
assert self.bash_session is not None
|
||||
|
||||
# Cannot read binary files
|
||||
if is_binary(action.path):
|
||||
return ErrorObservation('ERROR_BINARY_FILE')
|
||||
|
||||
if action.impl_source == FileReadSource.OH_ACI:
|
||||
result_str, _ = _execute_file_editor(
|
||||
self.file_editor,
|
||||
|
||||
@ -39,13 +39,14 @@ from openhands.events.observation import (
|
||||
from openhands.events.serialization import event_to_dict, observation_from_dict
|
||||
from openhands.events.serialization.action import ACTION_TYPE_TO_CLASS
|
||||
from openhands.integrations.provider import PROVIDER_TOKEN_TYPE
|
||||
from openhands.mcp import call_tool_mcp as call_tool_mcp_handler, create_mcp_clients, MCPClient
|
||||
from openhands.mcp import MCPClient, create_mcp_clients
|
||||
from openhands.mcp import call_tool_mcp as call_tool_mcp_handler
|
||||
from openhands.runtime.base import Runtime
|
||||
from openhands.runtime.plugins import PluginRequirement
|
||||
from openhands.runtime.utils.request import send_request
|
||||
from openhands.utils.async_utils import call_async_from_sync
|
||||
from openhands.utils.http_session import HttpSession
|
||||
from openhands.utils.tenacity_stop import stop_if_should_exit
|
||||
from openhands.utils.async_utils import call_async_from_sync
|
||||
|
||||
|
||||
def _is_retryable_error(exception):
|
||||
@ -325,10 +326,11 @@ class ActionExecutionClient(Runtime):
|
||||
|
||||
async def call_tool_mcp(self, action: McpAction) -> Observation:
|
||||
if self.mcp_clients is None:
|
||||
self.log('debug', f'Creating MCP clients with servers: {self.config.mcp.sse.mcp_servers}')
|
||||
self.mcp_clients = await create_mcp_clients(
|
||||
self.config.mcp.sse.mcp_servers
|
||||
self.log(
|
||||
'debug',
|
||||
f'Creating MCP clients with servers: {self.config.mcp.sse.mcp_servers}',
|
||||
)
|
||||
self.mcp_clients = await create_mcp_clients(self.config.mcp.sse.mcp_servers)
|
||||
return await call_tool_mcp_handler(self.mcp_clients, action)
|
||||
|
||||
async def aclose(self) -> None:
|
||||
|
||||
@ -135,6 +135,13 @@ async def select_file(file: str, request: Request):
|
||||
return {'code': content}
|
||||
elif isinstance(observation, ErrorObservation):
|
||||
logger.error(f'Error opening file {file}: {observation}')
|
||||
|
||||
if 'ERROR_BINARY_FILE' in observation.message:
|
||||
return JSONResponse(
|
||||
status_code=status.HTTP_415_UNSUPPORTED_MEDIA_TYPE,
|
||||
content={'error': f'Unable to open binary file: {file}'},
|
||||
)
|
||||
|
||||
return JSONResponse(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
content={'error': f'Error opening file: {observation}'},
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user