[Refactor]: Simplify provider info passing (#8138)

This commit is contained in:
Rohit Malhotra 2025-05-01 10:49:13 -04:00 committed by GitHub
parent 5376a54800
commit 08171eaf59
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 26 additions and 44 deletions

View File

@ -119,10 +119,6 @@ describe("Content", () => {
getConfigSpy.mockResolvedValue(VALID_OSS_CONFIG);
getSettingsSpy.mockResolvedValue({
...MOCK_DEFAULT_USER_SETTINGS,
provider_tokens_set: {
github: false,
gitlab: false,
},
});
const { rerender } = renderGitSettingsScreen();
@ -144,8 +140,8 @@ describe("Content", () => {
getSettingsSpy.mockResolvedValue({
...MOCK_DEFAULT_USER_SETTINGS,
provider_tokens_set: {
github: true,
gitlab: true,
github: null,
gitlab: null,
},
});
queryClient.invalidateQueries();
@ -169,8 +165,7 @@ describe("Content", () => {
getSettingsSpy.mockResolvedValue({
...MOCK_DEFAULT_USER_SETTINGS,
provider_tokens_set: {
github: false,
gitlab: true,
gitlab: null,
},
});
queryClient.invalidateQueries();
@ -304,8 +299,7 @@ describe("Form submission", () => {
getSettingsSpy.mockResolvedValue({
...MOCK_DEFAULT_USER_SETTINGS,
provider_tokens_set: {
github: true,
gitlab: false,
github: null,
},
});
@ -319,10 +313,6 @@ describe("Form submission", () => {
getSettingsSpy.mockResolvedValue({
...MOCK_DEFAULT_USER_SETTINGS,
provider_tokens_set: {
github: false,
gitlab: false,
},
});
queryClient.invalidateQueries();
@ -339,8 +329,7 @@ describe("Form submission", () => {
getSettingsSpy.mockResolvedValue({
...MOCK_DEFAULT_USER_SETTINGS,
provider_tokens_set: {
github: true,
gitlab: false,
github: null,
},
});

View File

@ -58,13 +58,11 @@ export const useSettings = () => {
React.useEffect(() => {
if (query.data?.PROVIDER_TOKENS_SET) {
const providers = query.data.PROVIDER_TOKENS_SET;
const setProviders = (
Object.keys(providers) as Array<keyof typeof providers>
).filter((key) => providers[key]);
const setProviders = Object.keys(providers) as Array<
keyof typeof providers
>;
setProviderTokensSet(setProviders);
const atLeastOneSet = Object.values(query.data.PROVIDER_TOKENS_SET).some(
(value) => value,
);
const atLeastOneSet = setProviders.length > 0;
setProvidersAreSet(atLeastOneSet);
}
}, [query.data?.PROVIDER_TOKENS_SET, query.isFetched]);

View File

@ -178,7 +178,7 @@ export const handlers = [
if (!settings) return HttpResponse.json(null, { status: 404 });
if (Object.keys(settings.provider_tokens_set).length > 0)
settings.provider_tokens_set = { github: false, gitlab: false };
settings.provider_tokens_set = {};
return HttpResponse.json(settings);
}),

View File

@ -15,6 +15,7 @@ import {
} from "#/utils/custom-toast-handlers";
import { retrieveAxiosErrorMessage } from "#/utils/retrieve-axios-error-message";
import { GitSettingInputsSkeleton } from "#/components/features/settings/git-settings/github-settings-inputs-skeleton";
import { useAuth } from "#/context/auth-context";
function GitSettingsScreen() {
const { t } = useTranslation();
@ -22,7 +23,8 @@ function GitSettingsScreen() {
const { mutate: saveSettings, isPending } = useSaveSettings();
const { mutate: disconnectGitTokens } = useLogout();
const { data: settings, isLoading } = useSettings();
const { providerTokensSet } = useAuth();
const { isLoading } = useSettings();
const { data: config } = useConfig();
const [githubTokenInputHasValue, setGithubTokenInputHasValue] =
@ -31,8 +33,8 @@ function GitSettingsScreen() {
React.useState(false);
const isSaas = config?.APP_MODE === "saas";
const isGitHubTokenSet = !!settings?.PROVIDER_TOKENS_SET.github;
const isGitLabTokenSet = !!settings?.PROVIDER_TOKENS_SET.gitlab;
const isGitHubTokenSet = providerTokensSet.includes("github");
const isGitLabTokenSet = providerTokensSet.includes("gitlab");
const formAction = async (formData: FormData) => {
const disconnectButtonClicked =

View File

@ -11,7 +11,7 @@ export const DEFAULT_SETTINGS: Settings = {
CONFIRMATION_MODE: false,
SECURITY_ANALYZER: "",
REMOTE_RUNTIME_RESOURCE_FACTOR: 1,
PROVIDER_TOKENS_SET: { github: false, gitlab: false },
PROVIDER_TOKENS_SET: {},
ENABLE_DEFAULT_CONDENSER: true,
ENABLE_SOUND_NOTIFICATIONS: false,
USER_CONSENTS_TO_ANALYTICS: false,

View File

@ -14,7 +14,7 @@ export type Settings = {
CONFIRMATION_MODE: boolean;
SECURITY_ANALYZER: string;
REMOTE_RUNTIME_RESOURCE_FACTOR: number | null;
PROVIDER_TOKENS_SET: Record<Provider, boolean>;
PROVIDER_TOKENS_SET: Partial<Record<Provider, string | null>>;
ENABLE_DEFAULT_CONDENSER: boolean;
ENABLE_SOUND_NOTIFICATIONS: boolean;
USER_CONSENTS_TO_ANALYTICS: boolean | null;
@ -36,7 +36,7 @@ export type ApiSettings = {
enable_sound_notifications: boolean;
user_consents_to_analytics: boolean | null;
provider_tokens: Record<Provider, string>;
provider_tokens_set: Record<Provider, boolean>;
provider_tokens_set: Partial<Record<Provider, string | null>>;
};
export type PostSettings = Settings & {

View File

@ -20,7 +20,6 @@ from openhands.server.shared import config
from openhands.storage.data_models.settings import Settings
from openhands.server.user_auth import (
get_provider_tokens,
get_user_id,
get_user_settings,
get_user_settings_store,
)
@ -31,7 +30,6 @@ app = APIRouter(prefix='/api')
@app.get('/settings', response_model=GETSettingsModel)
async def load_settings(
user_id: str | None = Depends(get_user_id),
provider_tokens: PROVIDER_TOKEN_TYPE | None = Depends(get_provider_tokens),
settings: Settings | None = Depends(get_user_settings),
) -> GETSettingsModel | JSONResponse:
@ -42,19 +40,11 @@ async def load_settings(
content={'error': 'Settings not found'},
)
provider_tokens_set = {}
if bool(user_id):
provider_tokens_set[ProviderType.GITHUB.value] = True
provider_tokens_set: dict[ProviderType, str | None] = {}
if provider_tokens:
all_provider_types = [provider.value for provider in ProviderType]
provider_tokens_types = [provider.value for provider in provider_tokens]
for provider_type in all_provider_types:
if provider_type in provider_tokens_types:
provider_tokens_set[provider_type] = True
else:
provider_tokens_set[provider_type] = False
for provider_type, provider_token in provider_tokens.items():
if provider_token.token or provider_token.user_id:
provider_tokens_set[provider_type] = None
settings_with_token_data = GETSettingsModel(
**settings.model_dump(exclude='secrets_store'),

View File

@ -5,6 +5,7 @@ from pydantic import (
SecretStr,
)
from openhands.integrations.service_types import ProviderType
from openhands.storage.data_models.settings import Settings
@ -29,7 +30,9 @@ class GETSettingsModel(Settings):
Settings with additional token data for the frontend
"""
provider_tokens_set: dict[str, bool] | None = None
provider_tokens_set: dict[ProviderType, str | None] | None = (
None # provider + base_domain key-value pair
)
llm_api_key_set: bool