Skip to content
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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ai) add attachment validations #6211

Merged
merged 8 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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
16 changes: 16 additions & 0 deletions .changeset/honest-beans-suffer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
"@aws-amplify/ui-react-ai": minor
"@aws-amplify/ui": patch
---

feat(ai) add attachment validations

The current limitations on the Amplify AI kit for attachments is 400kb (of base64'd size) per image, and 20 images per message are now being enforced before the message is sent.
These limits can be adjusted via props as well.

```tsx
<AIConversation
maxAttachments={2}
maxAttachmentSize={100_000} // 100,000 bytes or 100kb
/>
```
5 changes: 5 additions & 0 deletions docs/src/components/ComponentsMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@ export const ComponentsMetadata: ComponentClassNameItems = {
components: ['AIConversation'],
description: 'Class applied to the form element',
},
AIConversationFormError: {
className: ComponentClassName.AIConversationFormError,
components: ['AIConversation'],
description: 'Class applied to the error message of the form',
},
AIConversationFormAttach: {
className: ComponentClassName.AIConversationFormAttach,
components: ['AIConversation'],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import * as React from 'react';
import { Amplify } from 'aws-amplify';
import { signOut } from 'aws-amplify/auth';
import { createAIHooks, AIConversation } from '@aws-amplify/ui-react-ai';
import { generateClient } from 'aws-amplify/api';
import '@aws-amplify/ui-react/styles.css';

import outputs from './amplify_outputs';
import type { Schema } from '@environments/ai/gen2/amplify/data/resource';
import { Authenticator, Button, Card, Flex } from '@aws-amplify/ui-react';

const client = generateClient<Schema>({ authMode: 'userPool' });
const { useAIConversation } = createAIHooks(client);

Amplify.configure(outputs);

function Chat() {
const [
{
data: { messages },
isLoading,
},
sendMessage,
] = useAIConversation('pirateChat');

return (
<AIConversation
messages={messages}
isLoading={isLoading}
handleSendMessage={sendMessage}
allowAttachments
maxAttachmentSize={100_000}
maxAttachments={2}
/>
);
}

export default function Example() {
return (
<Authenticator>
<Flex direction="column">
<Button
onClick={() => {
signOut();
}}
>
Sign out
</Button>
<Card flex="1" variation="outlined" height="400px" margin="large">
<Chat />
</Card>
</Flex>
</Authenticator>
);
}
6 changes: 3 additions & 3 deletions packages/react-ai/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ const config: Config = {
coverageThreshold: {
global: {
branches: 68,
functions: 78,
lines: 87,
statements: 87,
functions: 77,
lines: 86,
statements: 86,
},
},
testPathIgnorePatterns: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ export const AIConversationProvider = ({
displayText,
handleSendMessage,
isLoading,
maxAttachmentSize,
maxAttachments,
messages,
messageRenderer,
responseComponents,
Expand All @@ -60,7 +62,11 @@ export const AIConversationProvider = ({
<ResponseComponentsProvider
responseComponents={responseComponents}
>
<AttachmentProvider allowAttachments={allowAttachments}>
<AttachmentProvider
allowAttachments={allowAttachments}
maxAttachmentSize={maxAttachmentSize}
maxAttachments={maxAttachments}
>
<ConversationDisplayTextProvider {..._displayText}>
<ConversationInputContextProvider>
<SendMessageContextProvider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

exports[`displayText should match snapshot 1`] = `
[
"getAttachmentSizeErrorText",
"getMaxAttachmentErrorText",
"getMessageTimestampText",
]
`;
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
convertBufferToBase64,
formatDate,
getImageTypeFromMimeType,
attachmentsValidator,
} from '../utils';

describe('convertBufferToBase64', () => {
Expand Down Expand Up @@ -57,3 +58,91 @@ describe('getImageTypeFromMimeType', () => {
expect(getImageTypeFromMimeType('image/webp')).toBe('webp');
});
});

describe('attachmentsValidator', () => {
// Helper function to create mock files
const createMockFile = (size: number, name = 'test.txt'): File => {
const buffer = new ArrayBuffer(size);
File.prototype.arrayBuffer = jest.fn().mockResolvedValueOnce(buffer);
return new File([buffer], name, { type: 'text/plain' });
};

it('should accept files within size limit', async () => {
const files = [createMockFile(100)];
const result = await attachmentsValidator({
files,
maxAttachments: 3,
maxAttachmentSize: 1000,
});

expect(result.acceptedFiles).toHaveLength(1);
expect(result.rejectedFiles).toHaveLength(0);
expect(result.hasMaxAttachmentSizeError).toBeFalsy();
expect(result.hasMaxAttachmentsError).toBeUndefined();
});

it('should reject files exceeding size limit', async () => {
const files = [createMockFile(2000)];
const result = await attachmentsValidator({
files,
maxAttachments: 3,
maxAttachmentSize: 1000,
});

expect(result.acceptedFiles).toHaveLength(0);
expect(result.rejectedFiles).toHaveLength(1);
expect(result.hasMaxAttachmentSizeError).toBeTruthy();
expect(result.hasMaxAttachmentsError).toBeUndefined();
});

it('should handle mixed valid and invalid file sizes', async () => {
const files = [
createMockFile(500),
createMockFile(2000),
createMockFile(800),
];
const result = await attachmentsValidator({
files,
maxAttachments: 3,
maxAttachmentSize: 1000,
});

expect(result.acceptedFiles).toHaveLength(2);
expect(result.rejectedFiles).toHaveLength(1);
expect(result.hasMaxAttachmentSizeError).toBeTruthy();
expect(result.hasMaxAttachmentsError).toBeUndefined();
});

it('should enforce maximum number of attachments', async () => {
const files = [
createMockFile(100),
createMockFile(200),
createMockFile(300),
createMockFile(400),
];
const result = await attachmentsValidator({
files,
maxAttachments: 2,
maxAttachmentSize: 1000,
});

expect(result.acceptedFiles).toHaveLength(2);
expect(result.rejectedFiles).toHaveLength(2);
expect(result.hasMaxAttachmentsError).toBeTruthy();
expect(result.hasMaxAttachmentSizeError).toBeFalsy();
});

it('should handle empty file list', async () => {
const files: File[] = [];
const result = await attachmentsValidator({
files,
maxAttachments: 3,
maxAttachmentSize: 1000,
});

expect(result.acceptedFiles).toHaveLength(0);
expect(result.rejectedFiles).toHaveLength(0);
expect(result.hasMaxAttachmentSizeError).toBeFalsy();
expect(result.hasMaxAttachmentsError).toBeUndefined();
});
});
Original file line number Diff line number Diff line change
@@ -1,16 +1,36 @@
import * as React from 'react';
import { AIConversationInput } from '../types';

export const AttachmentContext = React.createContext(false);
export interface AttachmentContextProps
extends Pick<
AIConversationInput,
'allowAttachments' | 'maxAttachments' | 'maxAttachmentSize'
> {}

export const AttachmentContext = React.createContext<
Required<AttachmentContextProps>
>({
allowAttachments: false,
// We save attachments as base64 strings into dynamodb for conversation history
// DynamoDB has a max size of 400kb for records
// This can be overridden so cutsomers could provide a lower number
// or a higher number if in the future we support larger sizes.
maxAttachmentSize: 400_000,
maxAttachments: 20,
});

export const AttachmentProvider = ({
children,
allowAttachments,
}: {
children?: React.ReactNode;
allowAttachments?: boolean;
}): JSX.Element => {
allowAttachments = false,
maxAttachmentSize = 400_000,
maxAttachments = 20,
}: React.PropsWithChildren<AttachmentContextProps>): JSX.Element => {
const providerValue = React.useMemo(
() => ({ maxAttachmentSize, maxAttachments, allowAttachments }),
[maxAttachmentSize, maxAttachments, allowAttachments]
);
return (
<AttachmentContext.Provider value={allowAttachments ?? false}>
<AttachmentContext.Provider value={providerValue}>
{children}
</AttachmentContext.Provider>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { ConversationInputContext } from './ConversationInputContext';
import { ConversationInputContextProps } from './ConversationInputContext';
import { SuggestedPrompt } from '../types';
import { ConversationMessage } from '../../../types';

Expand All @@ -9,12 +9,13 @@ export interface ControlsContextProps {
handleSubmit: (e: React.FormEvent<HTMLFormElement>) => void;
allowAttachments?: boolean;
isLoading?: boolean;
} & Required<ConversationInputContext>
onValidate: (files: File[]) => Promise<void>;
} & ConversationInputContextProps
>;
MessageList?: React.ComponentType<{ messages: ConversationMessage[] }>;
PromptList?: React.ComponentType<{
suggestedPrompts?: SuggestedPrompt[];
setInput: ConversationInputContext['setInput'];
setInput: ConversationInputContextProps['setInput'];
}>;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,29 @@ export interface ConversationInput {
files?: File[];
}

export interface ConversationInputContext {
export interface ConversationInputContextProps {
input?: ConversationInput;
setInput?: React.Dispatch<
React.SetStateAction<ConversationInput | undefined>
>;
error?: string;
setError?: React.Dispatch<React.SetStateAction<string | undefined>>;
}

export const ConversationInputContext =
React.createContext<ConversationInputContext>({});
React.createContext<ConversationInputContextProps>({});

export const ConversationInputContextProvider = ({
children,
}: {
children?: React.ReactNode;
}): JSX.Element => {
const [input, setInput] = React.useState<ConversationInput | undefined>();
const [error, setError] = React.useState<string>();

const providerValue = React.useMemo(
() => ({ input, setInput }),
[input, setInput]
() => ({ input, setInput, error, setError }),
[input, setInput, error, setError]
);

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export { AIContextContext, AIContextProvider } from './AIContextContext';
export { ActionsContext, ActionsProvider } from './ActionsContext';
export { AvatarsContext, AvatarsProvider } from './AvatarsContext';
export {
ConversationInputContextProps,
ConversationInputContext,
ConversationInput,
ConversationInputContextProvider,
Expand Down Expand Up @@ -40,7 +41,11 @@ export {
MessageRendererContext,
useMessageRenderer,
} from './MessageRenderContext';
export { AttachmentProvider, AttachmentContext } from './AttachmentContext';
export {
AttachmentProvider,
AttachmentContext,
AttachmentContextProps,
} from './AttachmentContext';
export {
WelcomeMessageContext,
WelcomeMessageProvider,
Expand Down
10 changes: 10 additions & 0 deletions packages/react-ai/src/components/AIConversation/displayText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,21 @@ import { formatDate } from './utils';

export type ConversationDisplayText = {
getMessageTimestampText?: (date: Date) => string;
getMaxAttachmentErrorText?: (count: number) => string;
getAttachmentSizeErrorText?: (sizeText: string) => string;
};

export const defaultAIConversationDisplayTextEn: Required<AIConversationDisplayText> =
{
getMessageTimestampText: (date: Date) => formatDate(date),
getMaxAttachmentErrorText(count: number): string {
return `Cannot choose more than ${count} ${
count === 1 ? 'file' : 'files'
}. `;
},
getAttachmentSizeErrorText(sizeText: string): string {
return `File size must be below ${sizeText}.`;
},
};

export type AIConversationDisplayText =
Expand Down
2 changes: 2 additions & 0 deletions packages/react-ai/src/components/AIConversation/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ export interface AIConversationInput {
variant?: MessageVariant;
controls?: ControlsContextProps;
allowAttachments?: boolean;
maxAttachments?: number;
maxAttachmentSize?: number;
messageRenderer?: MessageRenderer;
}

Expand Down
Loading
Loading