Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,24 @@ describe('parseAnonymizerApiError', () => {
expect(fieldErrors.map((f) => f.field)).toEqual(['hashDigestLength', 'source']);
});

it('maps rewrite params, including nested privacy goal fields', () => {
const rewriteLoc = (...tail: string[]) => ['body', 'spec', 'config', 'rewrite', ...tail];
const { fieldErrors } = parseAnonymizerApiError(
apiError([
{ loc: rewriteLoc('privacy_goal', 'protect'), msg: 'too short' },
{ loc: rewriteLoc('privacy_goal', 'preserve'), msg: 'too short' },
{ loc: rewriteLoc('max_repair_iterations'), msg: 'negative' },
{ loc: rewriteLoc('risk_tolerance'), msg: 'bad preset' },
])
);
expect(fieldErrors.map((f) => f.field)).toEqual([
'privacyProtect',
'privacyPreserve',
'maxRepairRounds',
'riskTolerance',
]);
});

it('collects unmapped errors as general messages', () => {
const { fieldErrors, generalMessages } = parseAnonymizerApiError(
apiError([{ loc: ['body', 'spec', 'model_configs', 0, 'provider'], msg: 'bad provider' }])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ const fieldForLoc = (loc: (string | number)[]): FormField | null => {
if (last === 'algorithm') return 'hashAlgorithm';
if (last === 'normalize_label') return 'redactNormalizeLabel';
}
if (segments.includes('rewrite')) {
if (last === 'protect') return 'privacyProtect';
if (last === 'preserve') return 'privacyPreserve';
if (last === 'instructions') return 'rewriteInstructions';
if (last === 'risk_tolerance') return 'riskTolerance';
if (last === 'max_repair_iterations') return 'maxRepairRounds';
if (last === 'strict_entity_protection') return 'strictEntityProtection';
}
if (segments.includes('data')) {
if (last === 'source') return 'source';
if (last === 'text_column') return 'textColumn';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { ControlledTextInput } from '@nemo/common/src/components/form/Controlled
import { Stack, Text } from '@nvidia/foundations-react-core';
import { StrategyParamsSection } from '@studio/routes/AnonymizerBuilderRoute/components/StrategyParamsSection';
import {
AVAILABLE_STRATEGY_OPTIONS,
STRATEGY_DESCRIPTIONS,
STRATEGY_OPTIONS,
} from '@studio/routes/AnonymizerBuilderRoute/constants';
import type { AnonymizerFormData } from '@studio/routes/AnonymizerBuilderRoute/schema';
import { FC } from 'react';
Expand All @@ -22,7 +22,7 @@ export const GenerationSection: FC = () => {
<Text kind="label/bold/lg">Generation</Text>
<ControlledSelect
aria-label="Anonymization strategy"
items={AVAILABLE_STRATEGY_OPTIONS}
items={STRATEGY_OPTIONS}
useControllerProps={{ name: 'strategy', control }}
formFieldProps={{ slotLabel: 'Anonymization Strategy', required: true }}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,77 +3,25 @@

import { ControlledSearchableSelect } from '@nemo/common/src/components/form/ControlledSearchableSelect';
import { ParamsDropdown } from '@nemo/common/src/components/ModelSelectV2/ParamsDropdown';
import { useModelsListProviders } from '@nemo/sdk/generated/platform/api';
import type { InferenceParams } from '@nemo/sdk/generated/platform/schema';
import { Divider, Flex, Stack, Text } from '@nvidia/foundations-react-core';
import { modelsFromProviders } from '@studio/components/NewDataDesignerJobForm/utils';
import { DEFAULT_LARGE_PAGE_SIZE } from '@studio/constants/constants';
import { useWorkspaceFromPath } from '@studio/hooks/useWorkspaceFromPath';
import {
activeRolesForStrategy,
GLINER_ROLE,
ROLE_LABELS,
} from '@studio/routes/AnonymizerBuilderRoute/constants';
import type { AnonymizerFormData } from '@studio/routes/AnonymizerBuilderRoute/schema';
import { pickDefaultModelName } from '@studio/util/buildSuggestedModelOptions';
import { FC, useEffect, useMemo, useState } from 'react';
import { useAnonymizerModels } from '@studio/routes/AnonymizerBuilderRoute/useAnonymizerModels';
import { FC, useMemo, useState } from 'react';
import { useFormContext, useWatch } from 'react-hook-form';

const isGliner = (name: string) => /gliner/i.test(name);

export const ModelSettingsSection: FC = () => {
const { control, setValue, getValues } = useFormContext<AnonymizerFormData>();
const workspace = useWorkspaceFromPath();
const { control, setValue } = useFormContext<AnonymizerFormData>();
const strategy = useWatch({ control, name: 'strategy' });
const roleModelsValue = useWatch({ control, name: 'roleModels' });
const [openParamsRole, setOpenParamsRole] = useState<string | null>(null);

const roles = useMemo(() => activeRolesForStrategy(strategy), [strategy]);

const { data: providersPage, isLoading } = useModelsListProviders(
workspace,
{ page_size: DEFAULT_LARGE_PAGE_SIZE },
{ query: {} }
);

const models = useMemo(
() => modelsFromProviders(providersPage?.data ?? []),
[providersPage?.data]
);
const items = useMemo(
() => models.map((model) => ({ label: model.name, value: model.id })),
[models]
);

const applyModel = (role: string, id: string) => {
const selected = models.find((model) => model.id === id);
setValue(`roleModels.${role}.model`, selected?.served_model_name ?? '', {
shouldValidate: true,
});
setValue(`roleModels.${role}.provider`, selected?.model_providers?.[0] ?? '', {
shouldValidate: true,
});
};

useEffect(() => {
if (!models.length) return;
const suggestedName = pickDefaultModelName(
models.map((model) => ({ name: model.served_model_name ?? model.name }))
);
const llm =
models.find((model) => (model.served_model_name ?? model.name) === suggestedName) ??
models.find((model) => !isGliner(model.name)) ??
models[0];
const gliner = models.find((model) => isGliner(model.name)) ?? llm;
for (const role of roles) {
const current = getValues(`roleModels.${role}.modelId`);
if (current) continue;
const pick = role === GLINER_ROLE ? gliner : llm;
setValue(`roleModels.${role}.modelId`, pick.id);
applyModel(role, pick.id);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [models, roles, getValues, setValue]);
const { items, isLoading, applyModel } = useAnonymizerModels();

return (
<Stack gap="density-2xl">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

import { ControlledCheckbox } from '@nemo/common/src/components/form/ControlledCheckbox';
import { ControlledSegmentedControl } from '@nemo/common/src/components/form/ControlledSegmentedControl';
import { ControlledTextArea } from '@nemo/common/src/components/form/ControlledTextArea';
import { ControlledTextInput } from '@nemo/common/src/components/form/ControlledTextInput';
import { FormField, Slider, Stack } from '@nvidia/foundations-react-core';
import {
PRIVACY_GOAL_MODE_CUSTOM,
PRIVACY_GOAL_MODE_OPTIONS,
REWRITE_MIN_MAX_REPAIR_ROUNDS,
RISK_TOLERANCE_LABELS,
RISK_TOLERANCE_ORDER,
} from '@studio/routes/AnonymizerBuilderRoute/constants';
import type { AnonymizerFormData } from '@studio/routes/AnonymizerBuilderRoute/schema';
import { FC } from 'react';
import { useController, useFormContext, useWatch } from 'react-hook-form';

const formatRiskToleranceStep = (index: number) =>
RISK_TOLERANCE_LABELS[RISK_TOLERANCE_ORDER[index]];

export const RewriteParamsSection: FC = () => {
const { control } = useFormContext<AnonymizerFormData>();
const privacyGoalMode = useWatch({ control, name: 'privacyGoalMode' });
const {
field: { onChange: onRiskToleranceChange, value: riskTolerance },
} = useController({ control, name: 'riskTolerance' });

return (
<Stack gap="density-lg">
<FormField slotLabel="Privacy Goal">
<ControlledSegmentedControl
className="w-full"
size="tiny"
items={PRIVACY_GOAL_MODE_OPTIONS}
useControllerProps={{ name: 'privacyGoalMode', control }}
/>
</FormField>
{privacyGoalMode === PRIVACY_GOAL_MODE_CUSTOM && (
<>
<ControlledTextArea
useControllerProps={{ name: 'privacyProtect', control }}
formFieldProps={{
slotLabel: 'Protect',
slotInfo: 'What to protect, such as direct and quasi-identifiers.',
}}
/>
<ControlledTextArea
useControllerProps={{ name: 'privacyPreserve', control }}
formFieldProps={{
slotLabel: 'Preserve',
slotInfo: 'What to keep intact, such as utility and semantic meaning.',
}}
/>
</>
)}
<ControlledTextArea
useControllerProps={{ name: 'rewriteInstructions', control }}
formFieldProps={{ slotLabel: 'LLM Instructions' }}
/>
<FormField slotLabel="Risk Tolerance">
<Slider
aria-label="Risk tolerance"
className="mb-5 px-6"
orientation="horizontal"
stepPosition="end"
min={0}
max={RISK_TOLERANCE_ORDER.length - 1}
step={1}
stepFormatFn={formatRiskToleranceStep}
value={RISK_TOLERANCE_ORDER.indexOf(riskTolerance)}
onValueChange={(index) => onRiskToleranceChange(RISK_TOLERANCE_ORDER[index])}
/>
</FormField>
<ControlledTextInput
type="number"
min={REWRITE_MIN_MAX_REPAIR_ROUNDS}
useControllerProps={{ name: 'maxRepairRounds', control }}
formFieldProps={{
slotLabel: 'Max Repair Rounds',
slotInfo: 'Repair passes run when leakage exceeds the tolerance. Set to 0 to disable.',
}}
/>
<ControlledCheckbox
slotLabel="Strict Entity Protection. Forces every detected entity to be protected regardless of risk. No entity can be left unchanged."
useControllerProps={{ name: 'strictEntityProtection', control }}
/>
</Stack>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

import { useDefaultRoleModels } from '@studio/routes/AnonymizerBuilderRoute/useDefaultRoleModels';
import { FC } from 'react';

/** Seeds role model defaults from inside the form provider, independent of the active tab. */
export const RoleModelDefaults: FC = () => {
useDefaultRoleModels();
return null;
};
Comment thread
marcusds marked this conversation as resolved.
Resolved
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import { ControlledCheckbox } from '@nemo/common/src/components/form/ControlledC
import { ControlledSelect } from '@nemo/common/src/components/form/ControlledSelect';
import { ControlledTextInput } from '@nemo/common/src/components/form/ControlledTextInput';
import { Stack } from '@nvidia/foundations-react-core';
import { RewriteParamsSection } from '@studio/routes/AnonymizerBuilderRoute/components/RewriteParamsSection';
import {
HASH_ALGORITHM_OPTIONS,
STRATEGY_ANNOTATE,
STRATEGY_HASH,
STRATEGY_REDACT,
STRATEGY_REWRITE,
} from '@studio/routes/AnonymizerBuilderRoute/constants';
import type { AnonymizerFormData } from '@studio/routes/AnonymizerBuilderRoute/schema';
import { FC } from 'react';
Expand Down Expand Up @@ -49,6 +51,10 @@ export const StrategyParamsSection: FC = () => {
);
}

if (strategy === STRATEGY_REWRITE) {
return <RewriteParamsSection />;
}

if (strategy === STRATEGY_HASH) {
return (
<Stack gap="density-lg">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

import { RiskTolerance } from '@nemo/sdk/generated/anonymizer/schema';

export const SOURCE_TYPE_URL = 'url';
export const SOURCE_TYPE_DATASET = 'dataset';

export type SourceType = typeof SOURCE_TYPE_URL | typeof SOURCE_TYPE_DATASET;

export const SOURCE_TYPE_OPTIONS: { label: string; value: SourceType }[] = [
{ label: 'Dataset', value: SOURCE_TYPE_DATASET },
{ label: 'URL', value: SOURCE_TYPE_URL },
export const SOURCE_TYPE_OPTIONS: { children: string; value: SourceType }[] = [
{ children: 'Dataset', value: SOURCE_TYPE_DATASET },
{ children: 'URL', value: SOURCE_TYPE_URL },
];

export const STRATEGY_SUBSTITUTE = 'substitute';
Expand All @@ -26,18 +28,14 @@ export type Strategy =

export const REWRITE_STRATEGY: Strategy = STRATEGY_REWRITE;

export const STRATEGY_OPTIONS: { label: string; value: Strategy }[] = [
{ label: 'Substitute', value: STRATEGY_SUBSTITUTE },
{ label: 'Redact', value: STRATEGY_REDACT },
{ label: 'Annotate', value: STRATEGY_ANNOTATE },
{ label: 'Hash', value: STRATEGY_HASH },
{ label: 'Rewrite', value: STRATEGY_REWRITE },
export const STRATEGY_OPTIONS: { children: string; value: Strategy }[] = [
{ children: 'Substitute', value: STRATEGY_SUBSTITUTE },
{ children: 'Redact', value: STRATEGY_REDACT },
{ children: 'Annotate', value: STRATEGY_ANNOTATE },
{ children: 'Hash', value: STRATEGY_HASH },
{ children: 'Rewrite', value: STRATEGY_REWRITE },
];

export const AVAILABLE_STRATEGY_OPTIONS = STRATEGY_OPTIONS.filter(
(option) => option.value !== STRATEGY_REWRITE
);

export const STRATEGY_DESCRIPTIONS: Record<Strategy, string> = {
[STRATEGY_SUBSTITUTE]:
'Replace detected entities with LLM-generated synthetic values for names, cities, dates, etc.',
Expand All @@ -64,8 +62,36 @@ const HASH_ALGORITHM_LABELS: Record<HashAlgorithmOption, string> = {
sha1: 'SHA-1',
md5: 'MD5',
};
export const HASH_ALGORITHM_OPTIONS: { label: string; value: HashAlgorithmOption }[] =
HASH_ALGORITHM_VALUES.map((value) => ({ label: HASH_ALGORITHM_LABELS[value], value }));
export const HASH_ALGORITHM_OPTIONS: { children: string; value: HashAlgorithmOption }[] =
HASH_ALGORITHM_VALUES.map((value) => ({ children: HASH_ALGORITHM_LABELS[value], value }));

export const PRIVACY_GOAL_MODE_DEFAULT = 'default';
export const PRIVACY_GOAL_MODE_CUSTOM = 'custom';

export type PrivacyGoalMode = typeof PRIVACY_GOAL_MODE_DEFAULT | typeof PRIVACY_GOAL_MODE_CUSTOM;

export const PRIVACY_GOAL_MODE_OPTIONS: { value: PrivacyGoalMode; children: string }[] = [
{ value: PRIVACY_GOAL_MODE_DEFAULT, children: 'Default' },
{ value: PRIVACY_GOAL_MODE_CUSTOM, children: 'Custom' },
];

export const RISK_TOLERANCE_ORDER = [
RiskTolerance.minimal,
RiskTolerance.low,
RiskTolerance.moderate,
RiskTolerance.high,
] as const;

export const RISK_TOLERANCE_LABELS: Record<RiskTolerance, string> = {
minimal: 'Minimal',
low: 'Low',
moderate: 'Moderate',
high: 'High',
};

export const RISK_TOLERANCE_DEFAULT: RiskTolerance = RiskTolerance.low;
export const REWRITE_DEFAULT_MAX_REPAIR_ROUNDS = 3;
export const REWRITE_MIN_MAX_REPAIR_ROUNDS = 0;

export const ENTITY_MODE_CUSTOM = 'custom';
export const ENTITY_MODE_AUTO = 'auto';
Expand Down Expand Up @@ -120,7 +146,8 @@ export const ROLE_LABELS: Record<string, string> = {
export const GLINER_ROLE = 'entity_detector';

export const activeRolesForStrategy = (strategy: Strategy): string[] => {
if (strategy === STRATEGY_REWRITE) return [...DETECTION_ROLES, ...REWRITE_ROLES];
// rewrite reuses the replacement generator, so the backend validates that role too
if (strategy === STRATEGY_REWRITE) return [...DETECTION_ROLES, ...REWRITE_ROLES, REPLACE_ROLE];
if (strategy === STRATEGY_SUBSTITUTE) return [...DETECTION_ROLES, REPLACE_ROLE];
return [...DETECTION_ROLES];
};
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { DataSourceSection } from '@studio/routes/AnonymizerBuilderRoute/compone
import { EntitiesSection } from '@studio/routes/AnonymizerBuilderRoute/components/EntitiesSection';
import { GenerationSection } from '@studio/routes/AnonymizerBuilderRoute/components/GenerationSection';
import { ModelSettingsSection } from '@studio/routes/AnonymizerBuilderRoute/components/ModelSettingsSection';
import { RoleModelDefaults } from '@studio/routes/AnonymizerBuilderRoute/components/RoleModelDefaults';
import {
anonymizerFormSchema,
buildAnonymizerJobRequest,
Expand All @@ -45,6 +46,8 @@ const PANEL_TABS = [
{ value: TAB_MODEL_SETTINGS, children: 'Model Settings' },
];

const PANEL_ATTRIBUTES = { PanelContent: { className: 'flex-1 min-h-0 overflow-auto' } };
Comment thread
marcusds marked this conversation as resolved.
Resolved

export const AnonymizerBuilderRoute: FC | null = ANONYMIZER_ENABLED
? () => {
const navigate = useNavigate();
Expand Down Expand Up @@ -111,11 +114,13 @@ export const AnonymizerBuilderRoute: FC | null = ANONYMIZER_ENABLED
<AccessibleTitle title="Anonymize Data">
<FormProvider {...form}>
<form className="h-full" noValidate onSubmit={onSubmit}>
<RoleModelDefaults />
<Flex className="h-full" gap="0">
<Panel
className="w-[400px] h-full overflow-auto"
className="w-[400px] h-full"
elevation="high"
density="standard"
attributes={PANEL_ATTRIBUTES}
slotFooter={
<Flex gap="density-md" justify="end">
<Button
Expand Down
Loading