Skip to content

πŸ™Œ OpenHands: AI-Driven Development

fix(frontend): a few issues with settings (#5940)

sp.wackb6c8aa27fa6c
diff --git a/frontend/src/api/open-hands.ts b/frontend/src/api/open-hands.ts
index 1d1bd0dfa8..1534be879c 100644
--- a/frontend/src/api/open-hands.ts
+++ b/frontend/src/api/open-hands.ts
@@ -11,7 +11,7 @@ import {
Conversation,
} from "./open-hands.types";
import { openHands } from "./open-hands-axios";
-import { ApiSettings, Settings } from "#/services/settings";
+import { ApiSettings } from "#/services/settings";
class OpenHands {
/**
@@ -238,13 +238,11 @@ class OpenHands {
}
static async createConversation(
- settings: Settings,
githubToken?: string,
selectedRepository?: string,
): Promise<Conversation> {
const body = {
github_token: githubToken,
- args: settings,
selected_repository: selectedRepository,
};
diff --git a/frontend/src/hooks/mutation/use-create-conversation.ts b/frontend/src/hooks/mutation/use-create-conversation.ts
index 1a9c7e3a79..ceb00b82b0 100644
--- a/frontend/src/hooks/mutation/use-create-conversation.ts
+++ b/frontend/src/hooks/mutation/use-create-conversation.ts
@@ -6,13 +6,11 @@ import OpenHands from "#/api/open-hands";
import { setInitialQuery } from "#/state/initial-query-slice";
import { RootState } from "#/store";
import { useAuth } from "#/context/auth-context";
-import { useSettings } from "../query/use-settings";
export const useCreateConversation = () => {
const navigate = useNavigate();
const dispatch = useDispatch();
const { gitHubToken } = useAuth();
- const { data: settings } = useSettings();
const queryClient = useQueryClient();
const { selectedRepository, files } = useSelector(
@@ -27,7 +25,6 @@ export const useCreateConversation = () => {
if (variables.q) dispatch(setInitialQuery(variables.q));
return OpenHands.createConversation(
- settings,
gitHubToken || undefined,
selectedRepository || undefined,
);
diff --git a/frontend/src/hooks/mutation/use-save-settings.ts b/frontend/src/hooks/mutation/use-save-settings.ts
index 2fb998cb4c..f9731e981d 100644
--- a/frontend/src/hooks/mutation/use-save-settings.ts
+++ b/frontend/src/hooks/mutation/use-save-settings.ts
@@ -1,6 +1,7 @@
import { useMutation, useQueryClient } from "@tanstack/react-query";
import {
ApiSettings,
+ DEFAULT_SETTINGS,
LATEST_SETTINGS_VERSION,
Settings,
} from "#/services/settings";
@@ -11,11 +12,11 @@ const saveSettingsMutationFn = async (settings: Partial<Settings>) => {
const apiSettings: Partial<ApiSettings> = {
llm_model: settings.LLM_MODEL,
llm_base_url: settings.LLM_BASE_URL,
- agent: settings.AGENT,
- language: settings.LANGUAGE,
+ agent: settings.AGENT || DEFAULT_SETTINGS.AGENT,
+ language: settings.LANGUAGE || DEFAULT_SETTINGS.LANGUAGE,
confirmation_mode: settings.CONFIRMATION_MODE,
security_analyzer: settings.SECURITY_ANALYZER,
- llm_api_key: settings.LLM_API_KEY,
+ llm_api_key: settings.LLM_API_KEY?.trim() || undefined,
};
await OpenHands.saveSettings(apiSettings);
diff --git a/frontend/src/hooks/query/use-settings.ts b/frontend/src/hooks/query/use-settings.ts
index 796c0cab18..f6e12e33e1 100644
--- a/frontend/src/hooks/query/use-settings.ts
+++ b/frontend/src/hooks/query/use-settings.ts
@@ -1,25 +1,36 @@
import { useQuery } from "@tanstack/react-query";
import React from "react";
import posthog from "posthog-js";
+import { AxiosError } from "axios";
import { DEFAULT_SETTINGS, getLocalStorageSettings } from "#/services/settings";
import OpenHands from "#/api/open-hands";
const getSettingsQueryFn = async () => {
- const apiSettings = await OpenHands.getSettings();
+ try {
+ const apiSettings = await OpenHands.getSettings();
- if (apiSettings !== null) {
- return {
- LLM_MODEL: apiSettings.llm_model,
- LLM_BASE_URL: apiSettings.llm_base_url,
- AGENT: apiSettings.agent,
- LANGUAGE: apiSettings.language,
- CONFIRMATION_MODE: apiSettings.confirmation_mode,
- SECURITY_ANALYZER: apiSettings.security_analyzer,
- LLM_API_KEY: apiSettings.llm_api_key,
- };
- }
+ if (apiSettings !== null) {
+ return {
+ LLM_MODEL: apiSettings.llm_model,
+ LLM_BASE_URL: apiSettings.llm_base_url,
+ AGENT: apiSettings.agent,
+ LANGUAGE: apiSettings.language,
+ CONFIRMATION_MODE: apiSettings.confirmation_mode,
+ SECURITY_ANALYZER: apiSettings.security_analyzer,
+ LLM_API_KEY: apiSettings.llm_api_key,
+ };
+ }
- return getLocalStorageSettings();
+ return getLocalStorageSettings();
+ } catch (error) {
+ if (error instanceof AxiosError) {
+ if (error.response?.status === 404) {
+ return DEFAULT_SETTINGS;
+ }
+ }
+
+ throw error;
+ }
};
export const useSettings = () => {
diff --git a/openhands/server/routes/new_conversation.py b/openhands/server/routes/new_conversation.py
index b1dd752112..6b16698d3a 100644
--- a/openhands/server/routes/new_conversation.py
+++ b/openhands/server/routes/new_conversation.py
@@ -6,10 +6,10 @@ from github import Github
from pydantic import BaseModel
from openhands.core.logger import openhands_logger as logger
+from openhands.server.data_models.conversation_metadata import ConversationMetadata
from openhands.server.routes.settings import ConversationStoreImpl, SettingsStoreImpl
from openhands.server.session.conversation_init_data import ConversationInitData
from openhands.server.shared import config, session_manager
-from openhands.server.data_models.conversation_metadata import ConversationMetadata
from openhands.utils.async_utils import call_sync_from_async
app = APIRouter(prefix='/api')
@@ -38,9 +38,6 @@ async def new_conversation(request: Request, data: InitSessionRequest):
session_init_args: dict = {}
if settings:
session_init_args = {**settings.__dict__, **session_init_args}
- if data.args:
- for key, value in data.args.items():
- session_init_args[key.lower()] = value
session_init_args['github_token'] = github_token
session_init_args['selected_repository'] = data.selected_repository
diff --git a/openhands/server/routes/settings.py b/openhands/server/routes/settings.py
index 456bb2a873..81637e8e45 100644
--- a/openhands/server/routes/settings.py
+++ b/openhands/server/routes/settings.py
@@ -27,9 +27,14 @@ async def load_settings(
try:
settings_store = await SettingsStoreImpl.get_instance(config, github_token)
settings = await settings_store.load()
- if settings:
- # For security reasons we don't ever send the api key to the client
- settings.llm_api_key = 'SET' if settings.llm_api_key else None
+ if not settings:
+ return JSONResponse(
+ status_code=status.HTTP_404_NOT_FOUND,
+ content={'error': 'Settings not found'},
+ )
+
+ # For security reasons we don't ever send the api key to the client
+ settings.llm_api_key = 'SET' if settings.llm_api_key else None
return settings
except Exception as e:
logger.warning(f'Invalid token: {e}')
@@ -50,14 +55,13 @@ async def store_settings(
try:
settings_store = await SettingsStoreImpl.get_instance(config, github_token)
existing_settings = await settings_store.load()
+
if existing_settings:
- # Only update settings that are not None with the new values
- for key, value in settings.__dict__.items():
- if value is None:
- setattr(settings, key, getattr(existing_settings, key))
+ # LLM key isn't on the frontend, so we need to keep it if unset
if settings.llm_api_key is None:
settings.llm_api_key = existing_settings.llm_api_key
await settings_store.store(settings)
+
return JSONResponse(
status_code=status.HTTP_200_OK,
content={'message': 'Settings stored'},
diff --git a/openhands/server/session/session.py b/openhands/server/session/session.py
index da412a435c..2cba665705 100644
--- a/openhands/server/session/session.py
+++ b/openhands/server/session/session.py
@@ -85,11 +85,9 @@ class Session:
# override default LLM config
default_llm_config = self.config.get_llm_config()
- default_llm_config.model = settings.llm_model or default_llm_config.model
- default_llm_config.api_key = settings.llm_api_key or default_llm_config.api_key
- default_llm_config.base_url = (
- settings.llm_base_url or default_llm_config.base_url
- )
+ default_llm_config.model = settings.llm_model or ''
+ default_llm_config.api_key = settings.llm_api_key
+ default_llm_config.base_url = settings.llm_base_url
# TODO: override other LLM config & agent config groups (#2075)