Skip to content

Commit

Permalink
Fix a few issues with settings
Browse files Browse the repository at this point in the history
  • Loading branch information
amanape committed Dec 31, 2024
1 parent 4de0a27 commit 6995e23
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 19 deletions.
7 changes: 4 additions & 3 deletions frontend/src/hooks/mutation/use-save-settings.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useMutation, useQueryClient } from "@tanstack/react-query";
import {
ApiSettings,
DEFAULT_SETTINGS,
LATEST_SETTINGS_VERSION,
Settings,
} from "#/services/settings";
Expand All @@ -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);
Expand Down
37 changes: 24 additions & 13 deletions frontend/src/hooks/query/use-settings.ts
Original file line number Diff line number Diff line change
@@ -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 = () => {
Expand Down
11 changes: 8 additions & 3 deletions openhands/server/routes/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}')
Expand Down

0 comments on commit 6995e23

Please sign in to comment.