-
Notifications
You must be signed in to change notification settings - Fork 10
feat(apollo-react): add-guardrail palette [AL-576] #1147
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
.../apollo-react/src/canvas/components/Guardrails/__fixtures__/guardrail-palette.fixtures.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| import type { GuardrailPaletteDefinition } from '../palette-types'; | ||
|
|
||
| /** | ||
| * Definitions shaped like what the two products pass a palette: UiPath validators (one of | ||
| * them unauthorized), and bring-your-own validators across two connections, one of which has | ||
| * no folder. Between them they cover every grouping and every chip branch. | ||
| */ | ||
|
|
||
| export const PII_DEFINITION: GuardrailPaletteDefinition = { | ||
| validator: 'pii_detection', | ||
| displayName: 'PII detection', | ||
| description: 'Detects personally identifiable information in agent traffic.', | ||
| status: 'Available', | ||
| }; | ||
|
|
||
| export const PROMPT_ATTACKS_DEFINITION: GuardrailPaletteDefinition = { | ||
| validator: 'user_prompt_attacks', | ||
| displayName: 'Prompt attacks', | ||
| description: 'Detects attempts to override the agent instructions.', | ||
| status: 'Available', | ||
| }; | ||
|
|
||
| export const UNAUTHORIZED_DEFINITION: GuardrailPaletteDefinition = { | ||
| validator: 'harmful_content', | ||
| displayName: 'Harmful content', | ||
| description: 'Detects harmful content categories in agent traffic.', | ||
| status: 'Unauthorised', | ||
| }; | ||
|
|
||
| export const BYO_FOLDER_DEFINITION: GuardrailPaletteDefinition = { | ||
| validator: 'byo', | ||
| displayName: 'Noma prompt shield', | ||
| description: 'Vendor-managed prompt injection detection.', | ||
| status: 'Available', | ||
| byoValidatorName: 'noma_prompt_injection', | ||
| byoConnectorName: 'Noma Security', | ||
| byoGuardrailConnectionId: 'connection-1', | ||
| folderPath: 'Shared/Security', | ||
| }; | ||
|
|
||
| /** The same validator name on a second connection: only the connection id keeps them apart. */ | ||
| export const BYO_SECOND_CONNECTION_DEFINITION: GuardrailPaletteDefinition = { | ||
| ...BYO_FOLDER_DEFINITION, | ||
| displayName: 'Noma prompt shield (EU)', | ||
| byoGuardrailConnectionId: 'connection-2', | ||
| folderPath: 'Shared/Security EU', | ||
| }; | ||
|
|
||
| /** No folder resolved, so the group falls back to the connector name. */ | ||
| export const BYO_CONNECTOR_ONLY_DEFINITION: GuardrailPaletteDefinition = { | ||
| validator: 'byo', | ||
| displayName: 'Acme policy check', | ||
| status: 'Available', | ||
| byoValidatorName: 'acme_policy', | ||
| byoConnectorName: 'Acme Guard', | ||
| byoGuardrailConnectionId: 'connection-3', | ||
| }; | ||
|
|
||
| export const UIPATH_DEFINITIONS: GuardrailPaletteDefinition[] = [ | ||
| PII_DEFINITION, | ||
| PROMPT_ATTACKS_DEFINITION, | ||
| ]; | ||
|
|
||
| export const MIXED_DEFINITIONS: GuardrailPaletteDefinition[] = [ | ||
| PII_DEFINITION, | ||
| BYO_FOLDER_DEFINITION, | ||
| PROMPT_ATTACKS_DEFINITION, | ||
| BYO_CONNECTOR_ONLY_DEFINITION, | ||
| ]; |
76 changes: 76 additions & 0 deletions
76
.../apollo-react/src/canvas/components/Guardrails/components/guardrail-palette-item.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| import { fireEvent, render, screen } from '@testing-library/react'; | ||
| import { axe } from 'jest-axe'; | ||
| import { describe, expect, it, vi } from 'vitest'; | ||
| import { GuardrailPaletteItem } from './guardrail-palette-item'; | ||
| import { GuardrailStatusChip } from './guardrail-status-chip'; | ||
|
|
||
| describe('GuardrailPaletteItem', () => { | ||
| it('renders the name, the description and the chips', () => { | ||
| render( | ||
| <GuardrailPaletteItem | ||
| name="PII detection" | ||
| description="Detects personally identifiable information." | ||
| chips={<GuardrailStatusChip tone="neutral">Preview</GuardrailStatusChip>} | ||
| onSelect={vi.fn()} | ||
| /> | ||
| ); | ||
|
|
||
| const item = screen.getByRole('button'); | ||
| expect(item).toHaveTextContent('PII detection'); | ||
| expect(item).toHaveTextContent('Detects personally identifiable information.'); | ||
| expect(item).toHaveTextContent('Preview'); | ||
| }); | ||
|
|
||
| it('is a button, so Enter and Space select it without a key handler of our own', () => { | ||
| const onSelect = vi.fn(); | ||
| render(<GuardrailPaletteItem name="PII detection" onSelect={onSelect} />); | ||
|
|
||
| const item = screen.getByRole('button', { name: 'PII detection' }); | ||
| expect(item).toHaveAttribute('type', 'button'); | ||
| fireEvent.click(item); | ||
| expect(onSelect).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| describe('when disabled', () => { | ||
| it('does not select', () => { | ||
| const onSelect = vi.fn(); | ||
| render(<GuardrailPaletteItem name="Harmful content" disabled onSelect={onSelect} />); | ||
|
|
||
| fireEvent.click(screen.getByRole('button', { name: 'Harmful content' })); | ||
| expect(onSelect).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('stays focusable, so the chip saying why is still reachable', () => { | ||
| render( | ||
| <GuardrailPaletteItem | ||
| name="Harmful content" | ||
| chips={<GuardrailStatusChip tone="warning">Unauthorized</GuardrailStatusChip>} | ||
| disabled | ||
| onSelect={vi.fn()} | ||
| /> | ||
| ); | ||
|
|
||
| const item = screen.getByRole('button', { name: /Harmful content/ }); | ||
| expect(item).toHaveAttribute('aria-disabled', 'true'); | ||
| expect(item).not.toBeDisabled(); | ||
| item.focus(); | ||
| expect(item).toHaveFocus(); | ||
| }); | ||
| }); | ||
|
|
||
| it('has no accessibility violations', async () => { | ||
| const { container } = render( | ||
| <> | ||
| <GuardrailPaletteItem name="PII detection" description="Detects PII." onSelect={vi.fn()} /> | ||
| <GuardrailPaletteItem | ||
| name="Harmful content" | ||
| chips={<GuardrailStatusChip tone="warning">Unauthorized</GuardrailStatusChip>} | ||
| disabled | ||
| onSelect={vi.fn()} | ||
| /> | ||
| </> | ||
| ); | ||
|
|
||
| expect(await axe(container)).toHaveNoViolations(); | ||
| }); | ||
| }); |
64 changes: 64 additions & 0 deletions
64
packages/apollo-react/src/canvas/components/Guardrails/components/guardrail-palette-item.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| import { cn } from '@uipath/apollo-wind'; | ||
| import * as React from 'react'; | ||
|
|
||
| export interface GuardrailPaletteItemProps | ||
| extends Omit< | ||
| React.ComponentPropsWithoutRef<'button'>, | ||
| // `onClick` too, so a caller spreading its own handler cannot reinstate the disabled guard. | ||
| 'children' | 'disabled' | 'onSelect' | 'onClick' | ||
| > { | ||
| name: string; | ||
| /** Secondary line, wrapped rather than truncated: the palette is where it is read. */ | ||
| description?: string; | ||
| chips?: React.ReactNode; | ||
| /** Only the create-custom entry has one; definitions carry no icons. */ | ||
| icon?: React.ReactNode; | ||
| /** | ||
| * Visible but not choosable. `aria-disabled`, not `disabled`, so the entry stays focusable | ||
| * and a keyboard user reaches the chip saying why it cannot be picked. | ||
| */ | ||
| disabled?: boolean; | ||
| onSelect: () => void; | ||
| } | ||
|
|
||
| /** | ||
| * One palette entry, presentational and unaware of grouping. A real button, where Agents' entry | ||
| * is a `div` with `role="listitem"`, `tabIndex={0}` and its own key handler. | ||
| */ | ||
| const GuardrailPaletteItem = React.forwardRef<HTMLButtonElement, GuardrailPaletteItemProps>( | ||
| ({ name, description, chips, icon, disabled = false, onSelect, className, ...props }, ref) => ( | ||
| <button | ||
| ref={ref} | ||
| type="button" | ||
| data-slot="guardrail-palette-item" | ||
| aria-disabled={disabled || undefined} | ||
| className={cn( | ||
| 'flex w-full items-start gap-2 rounded-md p-2 text-left ring-offset-background transition-colors', | ||
| 'focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2', | ||
| // Gated on the enabled branch, like wind's `Button` ghost and `CommandItem`. | ||
| disabled | ||
| ? 'cursor-not-allowed opacity-50' | ||
| : 'cursor-pointer hover:bg-accent hover:text-accent-foreground', | ||
| className | ||
| )} | ||
| onClick={disabled ? undefined : onSelect} | ||
| {...props} | ||
| > | ||
| {icon && <span className="mt-0.5 shrink-0 text-muted-foreground [&_svg]:size-4">{icon}</span>} | ||
| <span className="flex min-w-0 flex-1 flex-col gap-0.5"> | ||
| <span className="flex flex-wrap items-center gap-1.5"> | ||
| <span className="text-sm font-medium">{name}</span> | ||
| {chips} | ||
| </span> | ||
| {description && ( | ||
| <span className="whitespace-normal break-words text-xs text-muted-foreground"> | ||
| {description} | ||
| </span> | ||
| )} | ||
| </span> | ||
| </button> | ||
| ) | ||
| ); | ||
| GuardrailPaletteItem.displayName = 'GuardrailPaletteItem'; | ||
|
|
||
| export { GuardrailPaletteItem }; | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.