Skip to content

πŸ™Œ OpenHands: AI-Driven Development

refactor: move FileStore to openhands.app_server.file_store (#14178)

Tim O'Farrellc824b2dda5ca
diff --git a/enterprise/integrations/github/data_collector.py b/enterprise/integrations/github/data_collector.py
index f05ca9246c..8149ba7ae9 100644
--- a/enterprise/integrations/github/data_collector.py
+++ b/enterprise/integrations/github/data_collector.py
@@ -19,12 +19,12 @@ from server.auth.constants import GITHUB_APP_CLIENT_ID, GITHUB_APP_PRIVATE_KEY
from storage.openhands_pr import OpenhandsPR
from storage.openhands_pr_store import OpenhandsPRStore
+from openhands.app_server.conversation_paths import get_conversation_dir
+from openhands.app_server.file_store import get_file_store
from openhands.core.config import load_openhands_config
from openhands.core.logger import openhands_logger as logger
from openhands.integrations.github.github_service import GithubServiceImpl
from openhands.integrations.service_types import ProviderType
-from openhands.storage import get_file_store
-from openhands.storage.locations import get_conversation_dir
config = load_openhands_config()
file_store = get_file_store(config.file_store, config.file_store_path)
@@ -112,7 +112,7 @@ class GitHubDataCollector:
suffix = path.format(repo_id, number)
if conversation_id:
- return f'{get_conversation_dir(conversation_id)}{suffix}'
+ return f'{get_conversation_dir(conversation_id)}/{suffix}'
return suffix
diff --git a/openhands/app_server/conversation_paths.py b/openhands/app_server/conversation_paths.py
new file mode 100644
index 0000000000..ca324ef333
--- /dev/null
+++ b/openhands/app_server/conversation_paths.py
@@ -0,0 +1,73 @@
+"""Conversation path helpers for consistent path construction.
+
+This module provides helper functions for constructing conversation-related
+storage paths. Use these helpers instead of hardcoding path patterns to ensure
+consistency across the codebase.
+"""
+
+from pathlib import Path
+from uuid import UUID
+
+# The base directory name for v1 conversation storage
+V1_CONVERSATIONS_DIR = 'v1_conversations'
+
+
+def get_conversation_dir(conversation_id: UUID | str) -> str:
+ """Get the conversation directory path segment.
+
+ Args:
+ conversation_id: The conversation ID (UUID or hex string)
+
+ Returns:
+ Path segment like 'v1_conversations/{conversation_id_hex}'
+
+ Example:
+ >>> get_conversation_dir(UUID('12345678-1234-5678-1234-567812345678'))
+ 'v1_conversations/12345678123456781234567812345678'
+ >>> get_conversation_dir('12345678123456781234567812345678')
+ 'v1_conversations/12345678123456781234567812345678'
+ """
+ if isinstance(conversation_id, UUID):
+ conversation_id_hex = conversation_id.hex
+ else:
+ # Already a hex string
+ conversation_id_hex = conversation_id
+ return f'{V1_CONVERSATIONS_DIR}/{conversation_id_hex}'
+
+
+def get_conversation_path(
+ conversation_id: UUID | str,
+ user_id: str | None = None,
+ prefix: Path | str | None = None,
+) -> Path:
+ """Get the full conversation path.
+
+ Args:
+ conversation_id: The conversation ID (UUID or hex string)
+ user_id: Optional user ID to include in path
+ prefix: Optional prefix path
+
+ Returns:
+ Full path like '{prefix}/{user_id}/v1_conversations/{conversation_id_hex}'
+
+ Example:
+ >>> get_conversation_path(UUID('...'), user_id='user123', prefix=Path('/data'))
+ Path('/data/user123/v1_conversations/...')
+ """
+ if isinstance(conversation_id, UUID):
+ conversation_id_hex = conversation_id.hex
+ else:
+ conversation_id_hex = conversation_id
+
+ parts: list[str] = []
+
+ if prefix:
+ parts.append(str(prefix))
+
+ if user_id:
+ parts.append(user_id)
+
+ parts.append(V1_CONVERSATIONS_DIR)
+ parts.append(conversation_id_hex)
+
+ return Path(*parts) if parts else Path(V1_CONVERSATIONS_DIR) / conversation_id_hex
diff --git a/openhands/app_server/event/event_service_base.py b/openhands/app_server/event/event_service_base.py
index a028a485f9..50181e74d5 100644
--- a/openhands/app_server/event/event_service_base.py
+++ b/openhands/app_server/event/event_service_base.py
@@ -12,6 +12,7 @@ from openhands.app_server.app_conversation.app_conversation_info_service import
from openhands.app_server.app_conversation.app_conversation_models import (
AppConversationInfo,
)
+from openhands.app_server.conversation_paths import V1_CONVERSATIONS_DIR
from openhands.app_server.event.event_service import EventService
from openhands.app_server.event_callback.event_callback_models import EventKind
from openhands.sdk import Event
@@ -60,7 +61,7 @@ class EventServiceBase(EventService, ABC):
conversation_info = await task
if conversation_info and conversation_info.created_by_user_id:
path /= conversation_info.created_by_user_id
- path = path / 'v1_conversations' / conversation_id.hex
+ path = path / V1_CONVERSATIONS_DIR / conversation_id.hex
return path
async def get_event(self, conversation_id: UUID, event_id: UUID) -> Event | None:
diff --git a/openhands/app_server/file_store/README.md b/openhands/app_server/file_store/README.md
new file mode 100644
index 0000000000..0415dfc3af
--- /dev/null
+++ b/openhands/app_server/file_store/README.md
@@ -0,0 +1,77 @@
+# OpenHands FileStore Module
+
+The file store module provides different storage backends for file operations in OpenHands. This module implements a common interface (`FileStore`) that allows for interchangeable storage backends.
+
+All FileStore implementations use `DiscriminatedUnionMixin` for automatic serialization/deserialization with a `kind` discriminator field based on the class name.
+
+## Usage
+
+```python
+from openhands.app_server.file_store import get_file_store, LocalFileStore
+
+# Using the factory function
+store = get_file_store("local", "/tmp/file_store")
+
+# Or instantiate directly
+store = LocalFileStore(root="/tmp/file_store")
+
+# Write, read, list, and delete operations
+store.write("example.txt", "Hello, world!")
+content = store.read("example.txt")
+files = store.list("/")
+store.delete("example.txt")
+```
+
+## Available Storage Backends
+
+### 1. Local File Storage (`LocalFileStore`)
+
+Local file storage saves files to the local filesystem.
+
+**Parameters:**
+- `root`: The root directory for file storage (supports `~` expansion)
+
+### 2. In-Memory Storage (`InMemoryFileStore`)
+
+In-memory storage keeps files in memory, useful for testing or temporary storage.
+
+**Parameters:**
+- `files`: Optional dictionary of initial files (default: empty)
+
+### 3. Amazon S3 Storage (`S3FileStore`)
+
+S3 storage uses Amazon S3 or compatible services for file storage.
+
+**Parameters:**
+- `bucket`: The S3 bucket name (falls back to `AWS_S3_BUCKET` environment variable if empty)
+
+**Environment Variables:**
+- `AWS_ACCESS_KEY_ID`: Your AWS access key
+- `AWS_SECRET_ACCESS_KEY`: Your AWS secret key
+- `AWS_S3_BUCKET`: Default bucket name (used if `bucket` parameter is empty)
+- `AWS_S3_ENDPOINT`: Optional custom endpoint for S3-compatible services
+- `AWS_S3_SECURE`: Whether to use HTTPS (default: "true")
+
+### 4. Google Cloud Storage (`GoogleCloudFileStore`)
+
+Google Cloud Storage uses GCS buckets for file storage.
+
+**Parameters:**
+- `bucket_name`: The GCS bucket name (falls back to `GOOGLE_CLOUD_BUCKET_NAME` environment variable if empty)
+
+**Environment Variables:**
+- `GOOGLE_CLOUD_BUCKET_NAME`: Default bucket name (used if `bucket_name` parameter is empty)
+- `GOOGLE_APPLICATION_CREDENTIALS`: Path to Google Cloud credentials JSON file
+
+## Configuration
+
+To configure the file store in OpenHands, use the following configuration options:
+
+```toml
+[core]
+# File store type: "local", "memory", "s3", "google_cloud"
+file_store = "local"
+
+# Path/bucket for file store (interpretation depends on file_store type)
+file_store_path = "/tmp/file_store"
+```
diff --git a/openhands/app_server/file_store/__init__.py b/openhands/app_server/file_store/__init__.py
new file mode 100644
index 0000000000..1b247520e6
--- /dev/null
+++ b/openhands/app_server/file_store/__init__.py
@@ -0,0 +1,21 @@
+from openhands.app_server.file_store.files import FileStore
+from openhands.app_server.file_store.google_cloud import GoogleCloudFileStore
+from openhands.app_server.file_store.local import LocalFileStore
+from openhands.app_server.file_store.memory import InMemoryFileStore
+from openhands.app_server.file_store.s3 import S3FileStore
+
+
+def get_file_store(
+ file_store_type: str,
+ file_store_path: str | None = None,
+) -> FileStore:
+ if file_store_type == 'local':
+ if file_store_path is None:
+ raise ValueError('file_store_path is required for local file store')
+ return LocalFileStore(root=file_store_path)
+ elif file_store_type == 's3':
+ return S3FileStore(bucket_name=file_store_path or '')
+ elif file_store_type == 'google_cloud':
+ return GoogleCloudFileStore(bucket_name=file_store_path or '')
+ else:
+ return InMemoryFileStore()
diff --git a/openhands/app_server/file_store/files.py b/openhands/app_server/file_store/files.py
new file mode 100644
index 0000000000..0beb8ebfe9
--- /dev/null
+++ b/openhands/app_server/file_store/files.py
@@ -0,0 +1,30 @@
+from abc import ABC, abstractmethod
+
+from pydantic import ConfigDict
+
+from openhands.sdk.utils.models import DiscriminatedUnionMixin
+
+
+class FileStore(DiscriminatedUnionMixin, ABC):
+ """Base class for file storage implementations.
+
+ Uses DiscriminatedUnionMixin for automatic `kind` field based on class name.
+ """
+
+ model_config = ConfigDict(extra='forbid', arbitrary_types_allowed=True)
+
+ @abstractmethod
+ def write(self, path: str, contents: str | bytes) -> None:
+ pass
+
+ @abstractmethod
+ def read(self, path: str) -> str:
+ pass
+
+ @abstractmethod
+ def list(self, path: str) -> list[str]:
+ pass
+
+ @abstractmethod
+ def delete(self, path: str) -> None:
+ pass
diff --git a/openhands/storage/google_cloud.py b/openhands/app_server/file_store/google_cloud.py
similarity index 63%
rename from openhands/storage/google_cloud.py
rename to openhands/app_server/file_store/google_cloud.py
index eee3fa6985..271768e28f 100644
--- a/openhands/storage/google_cloud.py
+++ b/openhands/app_server/file_store/google_cloud.py
@@ -5,21 +5,44 @@ from google.cloud import storage
from google.cloud.storage.blob import Blob
from google.cloud.storage.bucket import Bucket
from google.cloud.storage.client import Client
+from pydantic import Field, PrivateAttr
-from openhands.storage.files import FileStore
+from openhands.app_server.file_store.files import FileStore
class GoogleCloudFileStore(FileStore):
- def __init__(self, bucket_name: str | None = None) -> None:
- """Create a new FileStore.
-
- If GOOGLE_APPLICATION_CREDENTIALS is defined in the environment it will be used
- for authentication. Otherwise access will be anonymous.
- """
- if bucket_name is None:
- bucket_name = os.environ['GOOGLE_CLOUD_BUCKET_NAME']
- self.storage_client: Client = storage.Client()
- self.bucket: Bucket = self.storage_client.bucket(bucket_name)
+ """Google Cloud Storage file store.
+
+ If GOOGLE_APPLICATION_CREDENTIALS is defined in the environment it will be used
+ for authentication. Otherwise access will be anonymous.
+
+ The storage client and bucket are initialized lazily on first access.
+ """
+
+ bucket_name: str = Field(default='')
+
+ _storage_client: Client | None = PrivateAttr(default=None)
+ _bucket: Bucket | None = PrivateAttr(default=None)
+
+ def _get_bucket_name(self) -> str:
+ """Get bucket name, falling back to environment variable if not set."""
+ if self.bucket_name:
+ return self.bucket_name
+ return os.environ['GOOGLE_CLOUD_BUCKET_NAME']
+
+ @property
+ def storage_client(self) -> Client:
+ """Get the storage client, initializing lazily on first access."""
+ if self._storage_client is None:
+ self._storage_client = storage.Client()
+ return self._storage_client
+
+ @property
+ def bucket(self) -> Bucket:
+ """Get the bucket, initializing lazily on first access."""
+ if self._bucket is None:
+ self._bucket = self.storage_client.bucket(self._get_bucket_name())
+ return self._bucket
def write(self, path: str, contents: str | bytes) -> None:
blob: Blob = self.bucket.blob(path)
diff --git a/openhands/storage/local.py b/openhands/app_server/file_store/local.py
similarity index 88%
rename from openhands/storage/local.py
rename to openhands/app_server/file_store/local.py
index e646f3137e..89121a49d3 100644
--- a/openhands/storage/local.py
+++ b/openhands/app_server/file_store/local.py
@@ -2,18 +2,21 @@ import os
import shutil
import threading
+from pydantic import model_validator
+
+from openhands.app_server.file_store.files import FileStore
from openhands.core.logger import openhands_logger as logger
-from openhands.storage.files import FileStore
class LocalFileStore(FileStore):
root: str
- def __init__(self, root: str):
- if root.startswith('~'):
- root = os.path.expanduser(root)
- self.root = root
+ @model_validator(mode='after')
+ def _setup_root(self) -> 'LocalFileStore':
+ if self.root.startswith('~'):
+ self.root = os.path.expanduser(self.root)
os.makedirs(self.root, exist_ok=True)
+ return self
def get_full_path(self, path: str) -> str:
if path.startswith('/'):
diff --git a/openhands/storage/memory.py b/openhands/app_server/file_store/memory.py
similarity index 86%
rename from openhands/storage/memory.py
rename to openhands/app_server/file_store/memory.py
index 1f2f60c99f..7f1c830345 100644
--- a/openhands/storage/memory.py
+++ b/openhands/app_server/file_store/memory.py
@@ -1,16 +1,13 @@
import os
+from pydantic import Field
+
+from openhands.app_server.file_store.files import FileStore
from openhands.core.logger import openhands_logger as logger
-from openhands.storage.files import FileStore
class InMemoryFileStore(FileStore):
- files: dict[str, str]
-
- def __init__(self, files: dict[str, str] | None = None) -> None:
- self.files = {}
- if files is not None:
- self.files = files
+ files: dict[str, str] = Field(default_factory=dict)
def write(self, path: str, contents: str | bytes) -> None:
if isinstance(contents, bytes):
diff --git a/openhands/storage/s3.py b/openhands/app_server/file_store/s3.py
similarity index 61%
rename from openhands/storage/s3.py
rename to openhands/app_server/file_store/s3.py
index edca65dab8..430c0537b0 100644
--- a/openhands/storage/s3.py
+++ b/openhands/app_server/file_store/s3.py
@@ -3,8 +3,9 @@ from typing import Any, TypedDict
import boto3
import botocore
+from pydantic import Field, PrivateAttr
-from openhands.storage.files import FileStore
+from openhands.app_server.file_store.files import FileStore
class S3ObjectDict(TypedDict):
@@ -20,45 +21,64 @@ class ListObjectsV2OutputDict(TypedDict):
class S3FileStore(FileStore):
- def __init__(self, bucket_name: str | None) -> None:
- access_key = os.getenv('AWS_ACCESS_KEY_ID')
- secret_key = os.getenv('AWS_SECRET_ACCESS_KEY')
- secure = os.getenv('AWS_S3_SECURE', 'true').lower() == 'true'
- endpoint = self._ensure_url_scheme(secure, os.getenv('AWS_S3_ENDPOINT'))
- if bucket_name is None:
- bucket_name = os.environ['AWS_S3_BUCKET']
- self.bucket: str = bucket_name
- self.client: Any = boto3.client(
- 's3',
- aws_access_key_id=access_key,
- aws_secret_access_key=secret_key,
- endpoint_url=endpoint,
- use_ssl=secure,
- )
+ """S3-compatible file store.
+
+ The S3 client is initialized lazily on first access.
+ """
+
+ bucket_name: str = Field(default='')
+
+ _client: Any = PrivateAttr(default=None)
+ _resolved_bucket: str | None = PrivateAttr(default=None)
+
+ def _get_bucket_name(self) -> str:
+ """Get bucket name, falling back to environment variable if not set."""
+ if self._resolved_bucket is None:
+ self._resolved_bucket = self.bucket_name or os.environ['AWS_S3_BUCKET']
+ return self._resolved_bucket
+
+ @property
+ def client(self) -> Any:
+ """Get the S3 client, initializing lazily on first access."""
+ if self._client is None:
+ access_key = os.getenv('AWS_ACCESS_KEY_ID')
+ secret_key = os.getenv('AWS_SECRET_ACCESS_KEY')
+ secure = os.getenv('AWS_S3_SECURE', 'true').lower() == 'true'
+ endpoint = self._ensure_url_scheme(secure, os.getenv('AWS_S3_ENDPOINT'))
+ self._client = boto3.client(
+ 's3',
+ aws_access_key_id=access_key,
+ aws_secret_access_key=secret_key,
+ endpoint_url=endpoint,
+ use_ssl=secure,
+ )
+ return self._client
def write(self, path: str, contents: str | bytes) -> None:
try:
as_bytes = (
contents.encode('utf-8') if isinstance(contents, str) else contents
)
- self.client.put_object(Bucket=self.bucket, Key=path, Body=as_bytes)
+ self.client.put_object(
+ Bucket=self._get_bucket_name(), Key=path, Body=as_bytes
+ )
except botocore.exceptions.ClientError as e:
if e.response['Error']['Code'] == 'AccessDenied':
raise FileNotFoundError(
- f"Error: Access denied to bucket '{self.bucket}'."
+ f"Error: Access denied to bucket '{self._get_bucket_name()}'."
)
elif e.response['Error']['Code'] == 'NoSuchBucket':
raise FileNotFoundError(
- f"Error: The bucket '{self.bucket}' does not exist."
+ f"Error: The bucket '{self._get_bucket_name()}' does not exist."
)
raise FileNotFoundError(
- f"Error: Failed to write to bucket '{self.bucket}' at path {path}: {e}"
+ f"Error: Failed to write to bucket '{self._get_bucket_name()}' at path {path}: {e}"
)
def read(self, path: str) -> str:
try:
response: GetObjectOutputDict = self.client.get_object(
- Bucket=self.bucket, Key=path
+ Bucket=self._get_bucket_name(), Key=path
)
with response['Body'] as stream:
return str(stream.read().decode('utf-8'))
@@ -66,19 +86,19 @@ class S3FileStore(FileStore):
# Catch all S3-related errors
if e.response['Error']['Code'] == 'NoSuchBucket':
raise FileNotFoundError(
- f"Error: The bucket '{self.bucket}' does not exist."
+ f"Error: The bucket '{self._get_bucket_name()}' does not exist."
)
elif e.response['Error']['Code'] == 'NoSuchKey':
raise FileNotFoundError(
- f"Error: The object key '{path}' does not exist in bucket '{self.bucket}'."
+ f"Error: The object key '{path}' does not exist in bucket '{self._get_bucket_name()}'."
)
else:
raise FileNotFoundError(
- f"Error: Failed to read from bucket '{self.bucket}' at path {path}: {e}"
+ f"Error: Failed to read from bucket '{self._get_bucket_name()}' at path {path}: {e}"
)
except Exception as e:
raise FileNotFoundError(
- f"Error: Failed to read from bucket '{self.bucket}' at path {path}: {e}"
+ f"Error: Failed to read from bucket '{self._get_bucket_name()}' at path {path}: {e}"
)
def list(self, path: str) -> list[str]:
@@ -96,7 +116,7 @@ class S3FileStore(FileStore):
results: set[str] = set()
prefix_len = len(path)
response: ListObjectsV2OutputDict = self.client.list_objects_v2(
- Bucket=self.bucket, Prefix=path
+ Bucket=self._get_bucket_name(), Prefix=path
)
contents = response.get('Contents')
if not contents:
@@ -123,34 +143,36 @@ class S3FileStore(FileStore):
# Try to delete any child resources (Assume the path is a directory)
response = self.client.list_objects_v2(
- Bucket=self.bucket, Prefix=f'{path}/'
+ Bucket=self._get_bucket_name(), Prefix=f'{path}/'
)
for content in response.get('Contents') or []:
- self.client.delete_object(Bucket=self.bucket, Key=content['Key'])
+ self.client.delete_object(
+ Bucket=self._get_bucket_name(), Key=content['Key']
+ )
# Next try to delete item as a file
- self.client.delete_object(Bucket=self.bucket, Key=path)
+ self.client.delete_object(Bucket=self._get_bucket_name(), Key=path)
except botocore.exceptions.ClientError as e:
if e.response['Error']['Code'] == 'NoSuchBucket':
raise FileNotFoundError(
- f"Error: The bucket '{self.bucket}' does not exist."
+ f"Error: The bucket '{self._get_bucket_name()}' does not exist."
)
elif e.response['Error']['Code'] == 'AccessDenied':
raise FileNotFoundError(
- f"Error: Access denied to bucket '{self.bucket}'."
+ f"Error: Access denied to bucket '{self._get_bucket_name()}'."
)
elif e.response['Error']['Code'] == 'NoSuchKey':
raise FileNotFoundError(
- f"Error: The object key '{path}' does not exist in bucket '{self.bucket}'."
+ f"Error: The object key '{path}' does not exist in bucket '{self._get_bucket_name()}'."
)
else:
raise FileNotFoundError(
- f"Error: Failed to delete key '{path}' from bucket '{self.bucket}': {e}"
+ f"Error: Failed to delete key '{path}' from bucket '{self._get_bucket_name()}': {e}"
)
except Exception as e:
raise FileNotFoundError(
- f"Error: Failed to delete key '{path}' from bucket '{self.bucket}: {e}"
+ f"Error: Failed to delete key '{path}' from bucket '{self._get_bucket_name()}: {e}"
)
def _ensure_url_scheme(self, secure: bool, url: str | None) -> str | None:
diff --git a/openhands/app_server/secrets/file_secrets_store.py b/openhands/app_server/secrets/file_secrets_store.py
index dc2699a178..5859af4eaf 100644
--- a/openhands/app_server/secrets/file_secrets_store.py
+++ b/openhands/app_server/secrets/file_secrets_store.py
@@ -2,22 +2,23 @@ from __future__ import annotations
import json
from dataclasses import dataclass
-from pathlib import Path
+from openhands.app_server.file_store import get_file_store
+from openhands.app_server.file_store.files import FileStore
from openhands.app_server.secrets.secrets_models import Secrets
from openhands.app_server.secrets.secrets_store import SecretsStore
-from openhands.app_server.utils.file_store_mixin import FileStoreMixin
from openhands.core.config.openhands_config import OpenHandsConfig
+from openhands.utils.async_utils import call_sync_from_async
@dataclass
-class FileSecretsStore(FileStoreMixin, SecretsStore):
- root_dir: Path
- filename: str = 'secrets.json'
+class FileSecretsStore(SecretsStore):
+ file_store: FileStore
+ path: str = 'secrets.json'
async def load(self) -> Secrets | None:
try:
- json_str = await self._read_file_async()
+ json_str = await call_sync_from_async(self.file_store.read, self.path)
kwargs = json.loads(json_str)
provider_tokens = {
k: v
@@ -32,11 +33,14 @@ class FileSecretsStore(FileStoreMixin, SecretsStore):
async def store(self, secrets: Secrets) -> None:
json_str = secrets.model_dump_json(context={'expose_secrets': True})
- await self._write_file_async(json_str)
+ await call_sync_from_async(self.file_store.write, self.path, json_str)
@classmethod
async def get_instance(
cls, config: OpenHandsConfig, user_id: str | None
) -> FileSecretsStore:
- root_dir = cls._resolve_root_dir(config.file_store_path)
- return FileSecretsStore(root_dir=root_dir)
+ file_store = get_file_store(
+ file_store_type=config.file_store,
+ file_store_path=config.file_store_path,
+ )
+ return FileSecretsStore(file_store)
diff --git a/openhands/app_server/settings/file_settings_store.py b/openhands/app_server/settings/file_settings_store.py
index ed4b28175e..ddf0206b92 100644
--- a/openhands/app_server/settings/file_settings_store.py
+++ b/openhands/app_server/settings/file_settings_store.py
@@ -2,22 +2,23 @@ from __future__ import annotations
import json
from dataclasses import dataclass
-from pathlib import Path
+from openhands.app_server.file_store import get_file_store
+from openhands.app_server.file_store.files import FileStore
from openhands.app_server.settings.settings_models import Settings
from openhands.app_server.settings.settings_store import SettingsStore
-from openhands.app_server.utils.file_store_mixin import FileStoreMixin
from openhands.core.config.openhands_config import OpenHandsConfig
+from openhands.utils.async_utils import call_sync_from_async
@dataclass
-class FileSettingsStore(FileStoreMixin, SettingsStore):
- root_dir: Path
- filename: str = 'settings.json'
+class FileSettingsStore(SettingsStore):
+ file_store: FileStore
+ path: str = 'settings.json'
async def load(self) -> Settings | None:
try:
- json_str = await self._read_file_async()
+ json_str = await call_sync_from_async(self.file_store.read, self.path)
kwargs = json.loads(json_str)
settings = Settings(**kwargs)
@@ -33,11 +34,14 @@ class FileSettingsStore(FileStoreMixin, SettingsStore):
json_str = settings.model_dump_json(
context={'expose_secrets': True, 'persist_settings': True}
)
- await self._write_file_async(json_str)
+ await call_sync_from_async(self.file_store.write, self.path, json_str)
@classmethod
async def get_instance(
cls, config: OpenHandsConfig, user_id: str | None
) -> FileSettingsStore:
- root_dir = cls._resolve_root_dir(config.file_store_path)
- return FileSettingsStore(root_dir=root_dir)
+ file_store = get_file_store(
+ file_store_type=config.file_store,
+ file_store_path=config.file_store_path,
+ )
+ return FileSettingsStore(file_store)
diff --git a/openhands/storage/data_models/llm_profiles.py b/openhands/app_server/settings/llm_profiles.py
similarity index 100%
rename from openhands/storage/data_models/llm_profiles.py
rename to openhands/app_server/settings/llm_profiles.py
diff --git a/openhands/app_server/settings/settings_models.py b/openhands/app_server/settings/settings_models.py
index 965dfd5224..d1bcef0419 100644
--- a/openhands/app_server/settings/settings_models.py
+++ b/openhands/app_server/settings/settings_models.py
@@ -24,13 +24,13 @@ from pydantic import (
model_validator,
)
+from openhands.app_server.settings.llm_profiles import LLMProfiles
from openhands.core.config.llm_config import LLMConfig
from openhands.core.config.mcp_config import MCPConfig
from openhands.core.config.utils import load_openhands_config
from openhands.integrations.provider import ProviderToken
from openhands.integrations.service_types import ProviderType
from openhands.sdk.settings import ConversationSettings
-from openhands.storage.data_models.llm_profiles import LLMProfiles
from openhands.utils.jsonpatch_compat import deep_merge
from openhands.utils.sdk_settings_compat import (
ACPAgentSettings,
diff --git a/openhands/app_server/settings/settings_router.py b/openhands/app_server/settings/settings_router.py
index cd3d804fb7..fc8be60880 100644
--- a/openhands/app_server/settings/settings_router.py
+++ b/openhands/app_server/settings/settings_router.py
@@ -14,6 +14,12 @@ from pydantic import BaseModel, Field
from openhands.app_server.secrets.secrets_models import Secrets
from openhands.app_server.secrets.secrets_store import SecretsStore
+from openhands.app_server.settings.llm_profiles import (
+ ProfileAlreadyExistsError,
+ ProfileLimitExceededError,
+ ProfileNotFoundError,
+ StrictLLM,
+)
from openhands.app_server.settings.settings_models import (
GETSettingsModel,
Settings,
@@ -35,12 +41,6 @@ from openhands.server.user_auth import (
get_user_settings,
get_user_settings_store,
)
-from openhands.storage.data_models.llm_profiles import (
- ProfileAlreadyExistsError,
- ProfileLimitExceededError,
- ProfileNotFoundError,
- StrictLLM,
-)
from openhands.utils.llm import (
get_provider_api_base,
is_openhands_model,
diff --git a/openhands/app_server/utils/file_store_mixin.py b/openhands/app_server/utils/file_store_mixin.py
deleted file mode 100644
index 416b25fef5..0000000000
--- a/openhands/app_server/utils/file_store_mixin.py
+++ /dev/null
@@ -1,52 +0,0 @@
-"""I/O utility functions for the app server."""
-
-from __future__ import annotations
-
-import tempfile
-from pathlib import Path
-
-from openhands.utils.async_utils import call_sync_from_async
-
-
-class FileStoreMixin:
- """Mixin providing common file I/O operations for file-based stores.
-
- Classes using this mixin must define:
- - root_dir: Path - the base directory for file storage
- - filename: str - the name of the file to read/write
- """
-
- root_dir: Path
- filename: str
-
- @property
- def file_path(self) -> Path:
- return self.root_dir / self.filename
-
- def _read_file(self) -> str:
- with open(self.file_path, encoding='utf-8') as f:
- return f.read()
-
- def _write_file_atomic(self, contents: str) -> None:
- """Write contents to file atomically using write-to-temp-then-rename."""
- self.root_dir.mkdir(parents=True, exist_ok=True)
- with tempfile.NamedTemporaryFile(
- mode='w',
- encoding='utf-8',
- dir=self.root_dir,
- delete=False,
- ) as f:
- f.write(contents)
- temp_path = Path(f.name)
- temp_path.replace(self.file_path)
-
- async def _read_file_async(self) -> str:
- return await call_sync_from_async(self._read_file)
-
- async def _write_file_async(self, contents: str) -> None:
- await call_sync_from_async(self._write_file_atomic, contents)
-
- @staticmethod
- def _resolve_root_dir(path: str) -> Path:
- """Resolve a path string to a Path, expanding ~ if present."""
- return Path(path).expanduser()
diff --git a/openhands/core/config/utils.py b/openhands/core/config/utils.py
index 73ed9c6570..f392c830e0 100644
--- a/openhands/core/config/utils.py
+++ b/openhands/core/config/utils.py
@@ -19,6 +19,8 @@ import toml
from dotenv import load_dotenv
from pydantic import BaseModel, SecretStr, ValidationError
+from openhands.app_server.file_store import get_file_store
+from openhands.app_server.file_store.files import FileStore
from openhands.core import logger
from openhands.core.config.agent_config import AgentConfig
from openhands.core.config.arg_utils import get_headless_parser
@@ -33,8 +35,6 @@ from openhands.core.config.model_routing_config import ModelRoutingConfig
from openhands.core.config.openhands_config import OpenHandsConfig
from openhands.core.config.sandbox_config import SandboxConfig
from openhands.core.config.security_config import SecurityConfig
-from openhands.storage import get_file_store
-from openhands.storage.files import FileStore
JWT_SECRET = '.jwt_secret'
load_dotenv()
diff --git a/openhands/server/shared.py b/openhands/server/shared.py
index 815d018a82..6f8fca6e5f 100644
--- a/openhands/server/shared.py
+++ b/openhands/server/shared.py
@@ -11,14 +11,14 @@ import os
import socketio
from dotenv import load_dotenv
+from openhands.app_server.file_store import get_file_store
+from openhands.app_server.file_store.files import FileStore
from openhands.app_server.secrets.secrets_store import SecretsStore
from openhands.app_server.settings.settings_store import SettingsStore
from openhands.core.config import load_openhands_config
from openhands.core.config.openhands_config import OpenHandsConfig
from openhands.server.config.server_config import ServerConfig, load_server_config
from openhands.server.types import ServerConfigInterface
-from openhands.storage import get_file_store
-from openhands.storage.files import FileStore
from openhands.utils.import_utils import get_impl
load_dotenv()
diff --git a/openhands/storage/README.md b/openhands/storage/README.md
deleted file mode 100644
index 7a638a6cc7..0000000000
--- a/openhands/storage/README.md
+++ /dev/null
@@ -1,127 +0,0 @@
-# OpenHands Storage Module
-
-The storage module provides different storage options for file operations in OpenHands, used for storing events, settings and other metadata. This module implements a common interface (`FileStore`) that allows for interchangeable storage backends.
-
-**Usage:**
-```python
-
-store = ...
-
-# Write, read, list, and delete operations
-store.write("example.txt", "Hello, world!")
-content = store.read("example.txt")
-files = store.list("/")
-store.delete("example.txt")
-```
-
-## Available Storage Options
-
-### 1. Local File Storage (`local`)
-
-Local file storage saves files to the local filesystem.
-
-**Environment Variables:**
-- None specific to this storage option
-- Files are stored at the path specified by `file_store_path` in the configuration
-
-### 2. In-Memory Storage (`memory`)
-
-In-memory storage keeps files in memory, which is useful for testing or temporary storage.
-
-**Environment Variables:**
-- None
-
-### 3. Amazon S3 Storage (`s3`)
-
-S3 storage uses Amazon S3 or compatible services for file storage.
-
-**Environment Variables:**
-- The bucket name is specified by `file_store_path` in the configuration with a fallback to the `AWS_S3_BUCKET` environment variable.
-- `AWS_ACCESS_KEY_ID`: Your AWS access key
-- `AWS_SECRET_ACCESS_KEY`: Your AWS secret key
-- `AWS_S3_ENDPOINT`: Optional custom endpoint for S3-compatible services (Allows overriding the default)
-- `AWS_S3_SECURE`: Whether to use HTTPS (default: "true")
-
-### 4. Google Cloud Storage (`google_cloud`)
-
-Google Cloud Storage uses Google Cloud Storage buckets for file storage.
-
-**Environment Variables:**
-- The bucket name is specified by `file_store_path` in the configuration with a fallback to the `GOOGLE_CLOUD_BUCKET_NAME` environment variable.
-- `GOOGLE_APPLICATION_CREDENTIALS`: Path to Google Cloud credentials JSON file
-
-## Webhook Protocol
-
-The webhook protocol allows for integration with external systems by sending HTTP requests when files are written or deleted.
-
-### Overview
-
-The `WebHookFileStore` wraps another `FileStore` implementation and sends HTTP requests to a specified URL whenever files are written or deleted. This enables real-time notifications and synchronization with external systems.
-
-**Configuration Options:**
-- `file_store_web_hook_url`: The base URL for webhook requests
-- `file_store_web_hook_headers`: HTTP headers to include in webhook requests
-- `file_store_web_hook_batch`: Whether to use batched webhook requests (default: false)
-
-### Protocol Details
-
-#### Standard Webhook Protocol (Non-Batched)
-
-1. **File Write Operation**:
- - When a file is written, a POST request is sent to `{base_url}{path}`
- - The request body contains the file contents
- - The operation is retried up to 3 times with a 1-second delay between attempts
-
-2. **File Delete Operation**:
- - When a file is deleted, a DELETE request is sent to `{base_url}{path}`
- - The operation is retried up to 3 times with a 1-second delay between attempts
-
-#### Batched Webhook Protocol
-
-The `BatchedWebHookFileStore` extends the webhook functionality by batching multiple file operations into a single request, which can significantly improve performance when many files are being modified in a short period of time.
-
-1. **Batch Request**:
- - A single POST request is sent to `{base_url}` with a JSON array in the body
- - Each item in the array contains:
- - `method`: "POST" for write operations, "DELETE" for delete operations
- - `path`: The file path
- - `content`: The file contents (for write operations only)
- - `encoding`: "base64" if binary content was base64-encoded (optional)
-
-2. **Batch Triggering**:
- - Batches are sent when one of the following conditions is met:
- - A timeout period has elapsed (defaults to 5 seconds, configurable via constructor parameter)
- - The total size of batched content exceeds a size limit (defaults to 1MB, configurable via constructor parameter)
- - The `flush()` method is explicitly called
-
-3. **Error Handling**:
- - The batch request is retried up to 3 times with a 1-second delay between attempts
-
-## Configuration
-
-To configure the storage module in OpenHands, use the following configuration options:
-
-```toml
-[core]
-# File store type: "local", "memory", "s3", "google_cloud"
-file_store = "local"
-
-# Path for local file store
-file_store_path = "/tmp/file_store"
-
-# Optional webhook URL
-file_store_web_hook_url = "https://example.com/api/files"
-
-# Optional webhook headers (JSON string)
-file_store_web_hook_headers = '{"Authorization": "Bearer token"}'
-
-# Optional batched webhook mode (default: false)
-file_store_web_hook_batch = true
-```
-
-**Batched Webhook Configuration:**
-The batched webhook behavior uses predefined constants with the following default values:
-- Batch timeout: 5 seconds
-- Batch size limit: 1MB (1048576 bytes)
-
-These values can be customized by passing `batch_timeout_seconds` and `batch_size_limit_bytes` parameters to the `BatchedWebHookFileStore` constructor.
diff --git a/openhands/storage/__init__.py b/openhands/storage/__init__.py
deleted file mode 100644
index 0ecae1be06..0000000000
--- a/openhands/storage/__init__.py
+++ /dev/null
@@ -1,21 +0,0 @@
-from openhands.storage.files import FileStore
-from openhands.storage.google_cloud import GoogleCloudFileStore
-from openhands.storage.local import LocalFileStore
-from openhands.storage.memory import InMemoryFileStore
-from openhands.storage.s3 import S3FileStore
-
-
-def get_file_store(
- file_store_type: str,
- file_store_path: str | None = None,
-) -> FileStore:
- if file_store_type == 'local':
- if file_store_path is None:
- raise ValueError('file_store_path is required for local file store')
- return LocalFileStore(file_store_path)
- elif file_store_type == 's3':
- return S3FileStore(file_store_path)
- elif file_store_type == 'google_cloud':
- return GoogleCloudFileStore(file_store_path)
- else:
- return InMemoryFileStore()
diff --git a/openhands/storage/files.py b/openhands/storage/files.py
deleted file mode 100644
index d4f4104b3c..0000000000
--- a/openhands/storage/files.py
+++ /dev/null
@@ -1,19 +0,0 @@
-from abc import abstractmethod
-
-
-class FileStore:
- @abstractmethod
- def write(self, path: str, contents: str | bytes) -> None:
- pass
-
- @abstractmethod
- def read(self, path: str) -> str:
- pass
-
- @abstractmethod
- def list(self, path: str) -> list[str]:
- pass
-
- @abstractmethod
- def delete(self, path: str) -> None:
- pass
diff --git a/openhands/storage/locations.py b/openhands/storage/locations.py
deleted file mode 100644
index 5be22c83ab..0000000000
--- a/openhands/storage/locations.py
+++ /dev/null
@@ -1,26 +0,0 @@
-CONVERSATION_BASE_DIR = 'sessions'
-
-
-def get_conversation_dir(sid: str, user_id: str | None = None) -> str:
- if user_id:
- return f'users/{user_id}/conversations/{sid}/'
- else:
- return f'{CONVERSATION_BASE_DIR}/{sid}/'
-
-
-def get_conversation_events_dir(sid: str, user_id: str | None = None) -> str:
- return f'{get_conversation_dir(sid, user_id)}events/'
-
-
-def get_conversation_event_filename(
- sid: str, id: int, user_id: str | None = None
-) -> str:
- return f'{get_conversation_events_dir(sid, user_id)}{id}.json'
-
-
-def get_conversation_metadata_filename(sid: str, user_id: str | None = None) -> str:
- return f'{get_conversation_dir(sid, user_id)}metadata.json'
-
-
-def get_conversation_agent_state_filename(sid: str, user_id: str | None = None) -> str:
- return f'{get_conversation_dir(sid, user_id)}agent_state.pkl'
diff --git a/tests/unit/app_server/file_store/__init__.py b/tests/unit/app_server/file_store/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/tests/unit/storage/test_storage.py b/tests/unit/app_server/file_store/test_file_store.py
similarity index 87%
rename from tests/unit/storage/test_storage.py
rename to tests/unit/app_server/file_store/test_file_store.py
index 5d2508705f..04c4ac34b0 100644
--- a/tests/unit/storage/test_storage.py
+++ b/tests/unit/app_server/file_store/test_file_store.py
@@ -13,11 +13,155 @@ from unittest.mock import patch
import botocore.exceptions
from google.api_core.exceptions import NotFound
-from openhands.storage.files import FileStore
-from openhands.storage.google_cloud import GoogleCloudFileStore
-from openhands.storage.local import LocalFileStore
-from openhands.storage.memory import InMemoryFileStore
-from openhands.storage.s3 import S3FileStore
+from openhands.app_server.file_store.files import FileStore
+from openhands.app_server.file_store.google_cloud import GoogleCloudFileStore
+from openhands.app_server.file_store.local import LocalFileStore
+from openhands.app_server.file_store.memory import InMemoryFileStore
+from openhands.app_server.file_store.s3 import S3FileStore
+
+# =============================================================================
+# Mock classes for cloud storage tests
+# These must be defined before the test classes that use them in decorators
+# =============================================================================
+
+
+class _MockGoogleCloudClient:
+ def bucket(self, name: str):
+ assert name == 'dear-liza'
+ return _MockGoogleCloudBucket()
+
+
+@dataclass
+class _MockGoogleCloudBucket:
+ blobs_by_path: dict[str, '_MockGoogleCloudBlob'] = field(default_factory=dict)
+
+ def blob(self, path: str | None = None) -> '_MockGoogleCloudBlob':
+ return self.blobs_by_path.get(path) or _MockGoogleCloudBlob(self, path)
+
+ def list_blobs(self, prefix: str | None = None) -> list['_MockGoogleCloudBlob']:
+ blobs = list(self.blobs_by_path.values())
+ if prefix and prefix != '/':
+ blobs = [blob for blob in blobs if blob.name.startswith(prefix)]
+ return blobs
+
+
+@dataclass
+class _MockGoogleCloudBlob:
+ bucket: _MockGoogleCloudBucket
+ name: str
+ content: str | bytes | None = None
+
+ def open(self, op: str):
+ if op == 'r':
+ if self.content is None:
+ raise FileNotFoundError()
+ return StringIO(self.content)
+ if op == 'w':
+ return _MockGoogleCloudBlobWriter(self)
+
+ def delete(self):
+ if self.name not in self.bucket.blobs_by_path:
+ raise NotFound('Blob not found')
+ del self.bucket.blobs_by_path[self.name]
+
+
+@dataclass
+class _MockGoogleCloudBlobWriter:
+ blob: _MockGoogleCloudBlob
+ content: str | bytes = None
+
+ def __enter__(self):
+ return self
+
+ def write(self, __b):
+ assert (
+ self.content is None
+ ) # We don't support buffered writes in this mock for now, as it is not needed
+ self.content = __b
+
+ def __exit__(self, exc_type, exc_val, exc_tb):
+ blob = self.blob
+ blob.content = self.content
+ blob.bucket.blobs_by_path[blob.name] = blob
+
+
+class _MockS3Client:
+ def __init__(self):
+ self.objects_by_bucket: dict[str, dict[str, '_MockS3Object']] = {}
+
+ def put_object(self, Bucket: str, Key: str, Body: str | bytes) -> None:
+ if Bucket not in self.objects_by_bucket:
+ self.objects_by_bucket[Bucket] = {}
+ self.objects_by_bucket[Bucket][Key] = _MockS3Object(Key, Body)
+
+ def get_object(self, Bucket: str, Key: str) -> dict:
+ if Bucket not in self.objects_by_bucket:
+ raise botocore.exceptions.ClientError(
+ {
+ 'Error': {
+ 'Code': 'NoSuchBucket',
+ 'Message': f"The bucket '{Bucket}' does not exist",
+ }
+ },
+ 'GetObject',
+ )
+ if Key not in self.objects_by_bucket[Bucket]:
+ raise botocore.exceptions.ClientError(
+ {
+ 'Error': {
+ 'Code': 'NoSuchKey',
+ 'Message': f"The specified key '{Key}' does not exist",
+ }
+ },
+ 'GetObject',
+ )
+ content = self.objects_by_bucket[Bucket][Key].content
+ if isinstance(content, bytes):
+ return {'Body': BytesIO(content)}
+ return {'Body': StringIO(content)}
+
+ def list_objects_v2(self, Bucket: str, Prefix: str = '') -> dict:
+ if Bucket not in self.objects_by_bucket:
+ raise botocore.exceptions.ClientError(
+ {
+ 'Error': {
+ 'Code': 'NoSuchBucket',
+ 'Message': f"The bucket '{Bucket}' does not exist",
+ }
+ },
+ 'ListObjectsV2',
+ )
+ objects = self.objects_by_bucket[Bucket]
+ contents = [
+ {'Key': key}
+ for key in objects.keys()
+ if not Prefix or key.startswith(Prefix)
+ ]
+ return {'Contents': contents} if contents else {}
+
+ def delete_object(self, Bucket: str, Key: str) -> None:
+ if Bucket not in self.objects_by_bucket:
+ raise botocore.exceptions.ClientError(
+ {
+ 'Error': {
+ 'Code': 'NoSuchBucket',
+ 'Message': f"The bucket '{Bucket}' does not exist",
+ }
+ },
+ 'DeleteObject',
+ )
+ self.objects_by_bucket[Bucket].pop(Key, None)
+
+
+@dataclass
+class _MockS3Object:
+ key: str
+ content: str | bytes
+
+
+# =============================================================================
+# Test base class and test implementations
+# =============================================================================
class _StorageTest(ABC):
@@ -112,7 +256,7 @@ class TestLocalFileStore(TestCase, _StorageTest):
def setUp(self):
# Create a unique temporary directory for each test instance
self.temp_dir = tempfile.mkdtemp(prefix='openhands_test_')
- self.store = LocalFileStore(self.temp_dir)
+ self.store = LocalFileStore(root=self.temp_dir)
def tearDown(self):
try:
@@ -180,149 +324,16 @@ class TestInMemoryFileStore(TestCase, _StorageTest):
self.store = InMemoryFileStore()
+@patch(
+ 'openhands.app_server.file_store.google_cloud.storage.Client',
+ _MockGoogleCloudClient,
+)
class TestGoogleCloudFileStore(TestCase, _StorageTest):
def setUp(self):
- with patch('google.cloud.storage.Client', _MockGoogleCloudClient):
- self.store = GoogleCloudFileStore('dear-liza')
+ self.store = GoogleCloudFileStore(bucket_name='dear-liza')
+@patch('boto3.client', lambda service, **kwargs: _MockS3Client())
class TestS3FileStore(TestCase, _StorageTest):
def setUp(self):
- with patch('boto3.client', lambda service, **kwargs: _MockS3Client()):
- self.store = S3FileStore('dear-liza')
-
-
-# I would have liked to use cloud-storage-mocker here but the python versions were incompatible :(
-# If we write tests for the S3 storage class I would definitely recommend we use moto.
-class _MockGoogleCloudClient:
- def bucket(self, name: str):
- assert name == 'dear-liza'
- return _MockGoogleCloudBucket()
-
-
-@dataclass
-class _MockGoogleCloudBucket:
- blobs_by_path: dict[str, _MockGoogleCloudBlob] = field(default_factory=dict)
-
- def blob(self, path: str | None = None) -> _MockGoogleCloudBlob:
- return self.blobs_by_path.get(path) or _MockGoogleCloudBlob(self, path)
-
- def list_blobs(self, prefix: str | None = None) -> list[_MockGoogleCloudBlob]:
- blobs = list(self.blobs_by_path.values())
- if prefix and prefix != '/':
- blobs = [blob for blob in blobs if blob.name.startswith(prefix)]
- return blobs
-
-
-@dataclass
-class _MockGoogleCloudBlob:
- bucket: _MockGoogleCloudBucket
- name: str
- content: str | bytes | None = None
-
- def open(self, op: str):
- if op == 'r':
- if self.content is None:
- raise FileNotFoundError()
- return StringIO(self.content)
- if op == 'w':
- return _MockGoogleCloudBlobWriter(self)
-
- def delete(self):
- if self.name not in self.bucket.blobs_by_path:
- raise NotFound('Blob not found')
- del self.bucket.blobs_by_path[self.name]
-
-
-@dataclass
-class _MockGoogleCloudBlobWriter:
- blob: _MockGoogleCloudBlob
- content: str | bytes = None
-
- def __enter__(self):
- return self
-
- def write(self, __b):
- assert (
- self.content is None
- ) # We don't support buffered writes in this mock for now, as it is not needed
- self.content = __b
-
- def __exit__(self, exc_type, exc_val, exc_tb):
- blob = self.blob
- blob.content = self.content
- blob.bucket.blobs_by_path[blob.name] = blob
-
-
-class _MockS3Client:
- def __init__(self):
- self.objects_by_bucket: dict[str, dict[str, _MockS3Object]] = {}
-
- def put_object(self, Bucket: str, Key: str, Body: str | bytes) -> None:
- if Bucket not in self.objects_by_bucket:
- self.objects_by_bucket[Bucket] = {}
- self.objects_by_bucket[Bucket][Key] = _MockS3Object(Key, Body)
-
- def get_object(self, Bucket: str, Key: str) -> dict:
- if Bucket not in self.objects_by_bucket:
- raise botocore.exceptions.ClientError(
- {
- 'Error': {
- 'Code': 'NoSuchBucket',
- 'Message': f"The bucket '{Bucket}' does not exist",
- }
- },
- 'GetObject',
- )
- if Key not in self.objects_by_bucket[Bucket]:
- raise botocore.exceptions.ClientError(
- {
- 'Error': {
- 'Code': 'NoSuchKey',
- 'Message': f"The specified key '{Key}' does not exist",
- }
- },
- 'GetObject',
- )
- content = self.objects_by_bucket[Bucket][Key].content
- if isinstance(content, bytes):
- return {'Body': BytesIO(content)}
- return {'Body': StringIO(content)}
-
- def list_objects_v2(self, Bucket: str, Prefix: str = '') -> dict:
- if Bucket not in self.objects_by_bucket:
- raise botocore.exceptions.ClientError(
- {
- 'Error': {
- 'Code': 'NoSuchBucket',
- 'Message': f"The bucket '{Bucket}' does not exist",
- }
- },
- 'ListObjectsV2',
- )
- objects = self.objects_by_bucket[Bucket]
- contents = [
- {'Key': key}
- for key in objects.keys()
- if not Prefix or key.startswith(Prefix)
- ]
- return {'Contents': contents} if contents else {}
-
- def delete_object(self, Bucket: str, Key: str) -> None:
- if Bucket not in self.objects_by_bucket:
- raise botocore.exceptions.ClientError(
- {
- 'Error': {
- 'Code': 'NoSuchBucket',
- 'Message': f"The bucket '{Bucket}' does not exist",
- }
- },
- 'DeleteObject',
- )
- self.objects_by_bucket[Bucket].pop(Key, None)
-
-
-@dataclass
-class _MockS3Object:
- key: str
- content: str | bytes
+ self.store = S3FileStore(bucket_name='dear-liza')
diff --git a/tests/unit/app_server/test_profiles_api.py b/tests/unit/app_server/test_profiles_api.py
index f2c7f5f355..66e28609b2 100644
--- a/tests/unit/app_server/test_profiles_api.py
+++ b/tests/unit/app_server/test_profiles_api.py
@@ -15,9 +15,11 @@ from fastapi import Request
from fastapi.testclient import TestClient
from pydantic import SecretStr
+from openhands.app_server.file_store import get_file_store
from openhands.app_server.secrets.secrets_models import Secrets
from openhands.app_server.secrets.secrets_store import SecretsStore
from openhands.app_server.settings.file_settings_store import FileSettingsStore
+from openhands.app_server.settings.llm_profiles import MAX_PROFILES_PER_USER
from openhands.app_server.settings.settings_models import Settings
from openhands.app_server.settings.settings_router import _user_profile_locks
from openhands.app_server.settings.settings_store import SettingsStore
@@ -27,7 +29,6 @@ from openhands.sdk.llm import LLM
from openhands.sdk.settings import AgentSettings
from openhands.server.app import app
from openhands.server.user_auth.user_auth import UserAuth
-from openhands.storage.data_models.llm_profiles import MAX_PROFILES_PER_USER
@pytest.fixture(autouse=True)
@@ -85,7 +86,7 @@ class _MockUserAuth(UserAuth):
@pytest.fixture
def settings_store(tmp_path: Path) -> FileSettingsStore:
- return FileSettingsStore(root_dir=tmp_path)
+ return FileSettingsStore(get_file_store('local', str(tmp_path)))
@pytest.fixture
@@ -888,8 +889,12 @@ async def test_profiles_are_isolated_between_users(tmp_path_factory):
don't key on user_id, or settings stores that accidentally share
data).
"""
- store_a = FileSettingsStore(root_dir=tmp_path_factory.mktemp('alice'))
- store_b = FileSettingsStore(root_dir=tmp_path_factory.mktemp('bob'))
+ store_a = FileSettingsStore(
+ get_file_store('local', str(tmp_path_factory.mktemp('alice')))
+ )
+ store_b = FileSettingsStore(
+ get_file_store('local', str(tmp_path_factory.mktemp('bob')))
+ )
await store_a.store(_base_settings())
await store_b.store(_base_settings())
@@ -958,7 +963,7 @@ async def test_concurrent_writes_all_persist(tmp_path: Path):
save_profile,
)
- store = FileSettingsStore(root_dir=tmp_path)
+ store = FileSettingsStore(get_file_store('local', str(tmp_path)))
await store.store(_base_settings())
async def _save_one(i: int) -> None:
diff --git a/tests/unit/app_server/test_secrets_api.py b/tests/unit/app_server/test_secrets_api.py
index a29f40422b..894f7c8ded 100644
--- a/tests/unit/app_server/test_secrets_api.py
+++ b/tests/unit/app_server/test_secrets_api.py
@@ -2,7 +2,6 @@
# flake8: noqa: E501
import os
-from pathlib import Path
from unittest.mock import AsyncMock, patch
import pytest
@@ -10,6 +9,7 @@ from fastapi import FastAPI
from fastapi.testclient import TestClient
from pydantic import SecretStr
+from openhands.app_server.file_store import get_file_store
from openhands.app_server.secrets.file_secrets_store import FileSecretsStore
from openhands.app_server.secrets.secrets_models import Secrets
from openhands.app_server.secrets.secrets_router import (
@@ -36,13 +36,14 @@ def test_client():
@pytest.fixture
-def temp_dir(tmp_path_factory: pytest.TempPathFactory) -> Path:
- return tmp_path_factory.mktemp('secrets_store')
+def temp_dir(tmp_path_factory: pytest.TempPathFactory) -> str:
+ return str(tmp_path_factory.mktemp('secrets_store'))
@pytest.fixture
def file_secrets_store(temp_dir):
- store = FileSecretsStore(root_dir=temp_dir)
+ file_store = get_file_store('local', temp_dir)
+ store = FileSecretsStore(file_store)
with patch(
'openhands.app_server.secrets.file_secrets_store.FileSecretsStore.get_instance',
AsyncMock(return_value=store),
diff --git a/tests/unit/app_server/test_settings_api.py b/tests/unit/app_server/test_settings_api.py
index c7c95bda6a..03c4d31f7e 100644
--- a/tests/unit/app_server/test_settings_api.py
+++ b/tests/unit/app_server/test_settings_api.py
@@ -6,6 +6,7 @@ from fastapi import Request
from fastapi.testclient import TestClient
from pydantic import SecretStr
+from openhands.app_server.file_store.memory import InMemoryFileStore
from openhands.app_server.secrets.secrets_models import Secrets
from openhands.app_server.secrets.secrets_store import SecretsStore
from openhands.app_server.settings.file_settings_store import FileSettingsStore
@@ -88,8 +89,8 @@ class MockUserAuth(UserAuth):
@pytest.fixture
-def test_client(tmp_path):
- # Create a test client with a temp directory for file settings store
+def test_client():
+ # Create a test client
with (
patch.dict(
os.environ,
@@ -103,7 +104,7 @@ def test_client(tmp_path):
),
patch(
'openhands.app_server.settings.file_settings_store.FileSettingsStore.get_instance',
- AsyncMock(return_value=FileSettingsStore(root_dir=tmp_path)),
+ AsyncMock(return_value=FileSettingsStore(InMemoryFileStore())),
),
):
client = TestClient(app)
diff --git a/tests/unit/server/routes/test_skills_api.py b/tests/unit/server/routes/test_skills_api.py
index 0ac1784eb0..b599fc8e91 100644
--- a/tests/unit/server/routes/test_skills_api.py
+++ b/tests/unit/server/routes/test_skills_api.py
@@ -7,6 +7,7 @@ from fastapi import Request
from fastapi.testclient import TestClient
from pydantic import SecretStr
+from openhands.app_server.file_store.memory import InMemoryFileStore
from openhands.app_server.secrets.secrets_models import Secrets
from openhands.app_server.secrets.secrets_store import SecretsStore
from openhands.app_server.settings.file_settings_store import FileSettingsStore
@@ -65,7 +66,7 @@ class MockUserAuth(UserAuth):
@pytest.fixture
-def test_client(tmp_path):
+def test_client():
with (
patch.dict(os.environ, {'SESSION_API_KEY': ''}, clear=False),
patch('openhands.app_server.utils.dependencies._SESSION_API_KEY', None),
@@ -75,7 +76,7 @@ def test_client(tmp_path):
),
patch(
'openhands.app_server.settings.file_settings_store.FileSettingsStore.get_instance',
- AsyncMock(return_value=FileSettingsStore(root_dir=tmp_path)),
+ AsyncMock(return_value=FileSettingsStore(InMemoryFileStore())),
),
):
client = TestClient(app)
diff --git a/tests/unit/server/test_openapi_schema_generation.py b/tests/unit/server/test_openapi_schema_generation.py
index c2db2192d2..8edc499ca8 100644
--- a/tests/unit/server/test_openapi_schema_generation.py
+++ b/tests/unit/server/test_openapi_schema_generation.py
@@ -6,6 +6,7 @@ from fastapi import Request
from fastapi.testclient import TestClient
from pydantic import SecretStr
+from openhands.app_server.file_store.memory import InMemoryFileStore
from openhands.app_server.secrets.secrets_models import Secrets
from openhands.app_server.secrets.secrets_store import SecretsStore
from openhands.app_server.settings.file_settings_store import FileSettingsStore
@@ -62,7 +63,7 @@ class MockUserAuth(UserAuth):
@pytest.fixture
-def test_client(tmp_path):
+def test_client():
# Create a test client
with (
patch(
@@ -71,7 +72,7 @@ def test_client(tmp_path):
),
patch(
'openhands.app_server.settings.file_settings_store.FileSettingsStore.get_instance',
- AsyncMock(return_value=FileSettingsStore(root_dir=tmp_path)),
+ AsyncMock(return_value=FileSettingsStore(InMemoryFileStore())),
),
):
client = TestClient(app)
diff --git a/tests/unit/storage/data_models/test_llm_profiles.py b/tests/unit/storage/data_models/test_llm_profiles.py
index 53393b3b5d..b712a8134e 100644
--- a/tests/unit/storage/data_models/test_llm_profiles.py
+++ b/tests/unit/storage/data_models/test_llm_profiles.py
@@ -7,8 +7,7 @@ through ``Settings``) live in ``test_settings.py``.
import pytest
from pydantic import SecretStr, ValidationError
-from openhands.sdk.llm import LLM
-from openhands.storage.data_models.llm_profiles import (
+from openhands.app_server.settings.llm_profiles import (
MAX_PROFILES_PER_USER,
LLMProfiles,
ProfileAlreadyExistsError,
@@ -16,6 +15,7 @@ from openhands.storage.data_models.llm_profiles import (
ProfileNotFoundError,
StrictLLM,
)
+from openhands.sdk.llm import LLM
def _make_llm(model: str = 'openai/gpt-4o', api_key: str | None = None) -> LLM:
diff --git a/tests/unit/storage/data_models/test_settings.py b/tests/unit/storage/data_models/test_settings.py
index dbccb29795..967574856a 100644
--- a/tests/unit/storage/data_models/test_settings.py
+++ b/tests/unit/storage/data_models/test_settings.py
@@ -7,6 +7,7 @@ from fastmcp.mcp_config import MCPConfig
from pydantic import SecretStr
import openhands.app_server.settings.settings_models as settings_module
+from openhands.app_server.settings.llm_profiles import ProfileNotFoundError
from openhands.app_server.settings.settings_models import Settings
from openhands.app_server.settings.settings_router import LITE_LLM_API_URL
from openhands.core.config.llm_config import LLMConfig
@@ -20,7 +21,6 @@ from openhands.sdk.settings import (
ConversationSettings,
)
from openhands.sdk.settings.model import CondenserSettings, VerificationSettings
-from openhands.storage.data_models.llm_profiles import ProfileNotFoundError
def test_settings_from_config():
diff --git a/tests/unit/storage/settings/test_file_settings_store.py b/tests/unit/storage/settings/test_file_settings_store.py
index 335b4f2fb5..0120d86ce4 100644
--- a/tests/unit/storage/settings/test_file_settings_store.py
+++ b/tests/unit/storage/settings/test_file_settings_store.py
@@ -4,6 +4,7 @@ from unittest.mock import MagicMock, patch
import pytest
from pydantic import SecretStr
+from openhands.app_server.file_store.files import FileStore
from openhands.app_server.settings.file_settings_store import FileSettingsStore
from openhands.app_server.settings.settings_models import Settings
from openhands.core.config.openhands_config import OpenHandsConfig
@@ -18,13 +19,13 @@ def allow_short_context_windows():
@pytest.fixture
-def temp_dir(tmp_path):
- return tmp_path
+def mock_file_store():
+ return MagicMock(spec=FileStore)
@pytest.fixture
-def file_settings_store(temp_dir):
- return FileSettingsStore(root_dir=temp_dir)
+def file_settings_store(mock_file_store):
+ return FileSettingsStore(mock_file_store)
@pytest.mark.asyncio
@@ -33,11 +34,12 @@ async def test_load_nonexistent_data(file_settings_store):
'openhands.app_server.settings.settings_models.load_openhands_config',
MagicMock(return_value=OpenHandsConfig()),
):
+ file_settings_store.file_store.read.side_effect = FileNotFoundError()
assert await file_settings_store.load() is None
@pytest.mark.asyncio
-async def test_store_and_load_data(file_settings_store, temp_dir):
+async def test_store_and_load_data(file_settings_store):
# Test data
init_data = Settings(
language='python',
@@ -59,9 +61,16 @@ async def test_store_and_load_data(file_settings_store, temp_dir):
# Store data
await file_settings_store.store(init_data)
- # Verify file was written
- expected_path = temp_dir / 'settings.json'
- assert expected_path.exists()
+ # Verify store called with correct JSON
+ expected_json = init_data.model_dump_json(
+ context={'expose_secrets': True, 'persist_settings': True}
+ )
+ file_settings_store.file_store.write.assert_called_once_with(
+ 'settings.json', expected_json
+ )
+
+ # Setup mock for load
+ file_settings_store.file_store.read.return_value = expected_json
# Load and verify data
loaded_data = await file_settings_store.load()
@@ -93,10 +102,20 @@ async def test_store_and_load_data(file_settings_store, temp_dir):
@pytest.mark.asyncio
-async def test_get_instance(tmp_path):
- config = OpenHandsConfig(file_store='local', file_store_path=str(tmp_path))
+async def test_get_instance():
+ config = OpenHandsConfig(file_store='local', file_store_path='/test/path')
- store = await FileSettingsStore.get_instance(config, None)
-
- assert isinstance(store, FileSettingsStore)
- assert store.root_dir == tmp_path
+ with patch(
+ 'openhands.app_server.settings.file_settings_store.get_file_store'
+ ) as mock_get_store:
+ mock_store = MagicMock(spec=FileStore)
+ mock_get_store.return_value = mock_store
+
+ store = await FileSettingsStore.get_instance(config, None)
+
+ assert isinstance(store, FileSettingsStore)
+ assert store.file_store == mock_store
+ mock_get_store.assert_called_once_with(
+ file_store_type='local',
+ file_store_path='/test/path',
+ )