Skip to content

Commit

Permalink
Merge branch 'main' into feat-extra-security-host
Browse files Browse the repository at this point in the history
  • Loading branch information
tofarr authored Dec 31, 2024
2 parents 64150f8 + 7ae1f76 commit f3cc95f
Show file tree
Hide file tree
Showing 14 changed files with 60 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,10 @@ describe("BaseModal", () => {
expect(screen.getByText("Save")).toBeInTheDocument();
expect(screen.getByText("Cancel")).toBeInTheDocument();

await act(async () => {
await userEvent.click(screen.getByText("Save"));
});
await userEvent.click(screen.getByText("Save"));
expect(onPrimaryClickMock).toHaveBeenCalledTimes(1);

await act(async () => {
await userEvent.click(screen.getByText("Cancel"));
});
await userEvent.click(screen.getByText("Cancel"));
expect(onSecondaryClickMock).toHaveBeenCalledTimes(1);
});

Expand All @@ -80,9 +76,7 @@ describe("BaseModal", () => {
/>,
);

await act(async () => {
await userEvent.click(screen.getByText("Save"));
});
await userEvent.click(screen.getByText("Save"));
expect(onOpenChangeMock).toHaveBeenCalledTimes(1);
});

Expand Down
4 changes: 1 addition & 3 deletions frontend/src/api/open-hands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
/**
Expand Down Expand Up @@ -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,
};

Expand Down
6 changes: 3 additions & 3 deletions frontend/src/components/shared/buttons/icon-button.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Button } from "@nextui-org/react";
import React, { MouseEventHandler, ReactElement } from "react";
import React, { ReactElement } from "react";

export interface IconButtonProps {
icon: ReactElement;
onClick: MouseEventHandler<HTMLButtonElement>;
onClick: () => void;
ariaLabel: string;
testId?: string;
}
Expand All @@ -18,7 +18,7 @@ export function IconButton({
<Button
type="button"
variant="flat"
onClick={onClick}
onPress={onClick}
className="cursor-pointer text-[12px] bg-transparent aspect-square px-0 min-w-[20px] h-[20px]"
aria-label={ariaLabel}
data-testid={testId}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export function FooterContent({ actions, closeModal }: FooterContentProps) {
key={label}
type="button"
isDisabled={isDisabled}
onClick={() => {
onPress={() => {
action();
if (closeAfterAction) closeModal();
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ function SecurityInvariant() {
<>
<div className="flex justify-between items-center border-b border-neutral-600 mb-4 p-4">
<h2 className="text-2xl">{t(I18nKey.INVARIANT$LOG_LABEL)}</h2>
<Button onClick={() => exportTraces()} className="bg-neutral-700">
<Button onPress={() => exportTraces()} className="bg-neutral-700">
{t(I18nKey.INVARIANT$EXPORT_TRACE_LABEL)}
</Button>
</div>
Expand Down Expand Up @@ -162,7 +162,7 @@ function SecurityInvariant() {
<h2 className="text-2xl">{t(I18nKey.INVARIANT$POLICY_LABEL)}</h2>
<Button
className="bg-neutral-700"
onClick={() => updatePolicy({ policy })}
onPress={() => updatePolicy({ policy })}
>
{t(I18nKey.INVARIANT$UPDATE_POLICY_LABEL)}
</Button>
Expand All @@ -184,7 +184,7 @@ function SecurityInvariant() {
<h2 className="text-2xl">{t(I18nKey.INVARIANT$SETTINGS_LABEL)}</h2>
<Button
className="bg-neutral-700"
onClick={() => updateRiskSeverity({ riskSeverity: selectedRisk })}
onPress={() => updateRiskSeverity({ riskSeverity: selectedRisk })}
>
{t(I18nKey.INVARIANT$UPDATE_SETTINGS_LABEL)}
</Button>
Expand Down
3 changes: 0 additions & 3 deletions frontend/src/hooks/mutation/use-create-conversation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -27,7 +25,6 @@ export const useCreateConversation = () => {

if (variables.q) dispatch(setInitialQuery(variables.q));
return OpenHands.createConversation(
settings,
gitHubToken || undefined,
selectedRepository || undefined,
);
Expand Down
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
4 changes: 1 addition & 3 deletions frontend/src/hooks/use-maybe-migrate-settings.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Sometimes we ship major changes, like a new default agent.

import React from "react";
import { useAuth } from "#/context/auth-context";
import { useSettingsUpToDate } from "#/context/settings-up-to-date-context";
import {
DEFAULT_SETTINGS,
Expand All @@ -12,7 +11,6 @@ import { useSaveSettings } from "./mutation/use-save-settings";

// In this case, we may want to override a previous choice made by the user.
export const useMaybeMigrateSettings = () => {
const { logout } = useAuth();
const { mutateAsync: saveSettings } = useSaveSettings();
const { isUpToDate } = useSettingsUpToDate();

Expand All @@ -35,7 +33,7 @@ export const useMaybeMigrateSettings = () => {
}

if (currentVersion < 4) {
logout();
// We used to log out here, but it's breaking things
}

// Only save settings if user already previously saved settings
Expand Down
1 change: 0 additions & 1 deletion openhands/runtime/impl/docker/docker_runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,6 @@ def _find_available_port(self, max_attempts=5):
@property
def vscode_url(self) -> str | None:
token = super().get_vscode_token()
print('got token', token)
if not token:
return None
vscode_url = f'http://localhost:{self._host_port + 1}/?tkn={token}&folder={self.config.workspace_mount_path_in_sandbox}'
Expand Down
5 changes: 1 addition & 4 deletions openhands/server/routes/new_conversation.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
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')
Expand Down Expand Up @@ -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
Expand Down
18 changes: 11 additions & 7 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 All @@ -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'},
Expand Down
8 changes: 3 additions & 5 deletions openhands/server/session/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,9 @@ async def initialize_agent(
# 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)

Expand Down
10 changes: 5 additions & 5 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f3cc95f

Please sign in to comment.