fix(backend): Return 400 if trying to open a binary file (#7825)

This commit is contained in:
sp.wack
2025-04-12 02:47:57 +04:00
committed by GitHub
parent 03b8b8c19a
commit 72b5e18898
4 changed files with 22 additions and 7 deletions

View File

@@ -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.

View File

@@ -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,

View File

@@ -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:

View File

@@ -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}'},