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): Add granular user agent for Ai #13835

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
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
15 changes: 13 additions & 2 deletions packages/api-graphql/src/GraphQLAPI.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import { AmplifyClassV6 } from '@aws-amplify/core';
import { ApiAction, Category } from '@aws-amplify/core/internals/utils';
import {
ApiAction,
Category,
INTERNAL_USER_AGENT_OVERRIDE,
} from '@aws-amplify/core/internals/utils';
import { CustomHeaders } from '@aws-amplify/data-schema/runtime';
import { Observable } from 'rxjs';

Expand Down Expand Up @@ -38,9 +42,16 @@ export class GraphQLAPIClass extends InternalGraphQLAPIClass {
options: GraphQLOptions,
additionalHeaders?: CustomHeaders,
): Observable<GraphQLResult<T>> | Promise<GraphQLResult<T>> {
return super.graphql(amplify, options, additionalHeaders, {
const internalUserAgentOverride = (options as any)[
INTERNAL_USER_AGENT_OVERRIDE
];
const { [INTERNAL_USER_AGENT_OVERRIDE]: _, ...cleanOptions } =
options as any;
yuhengshs marked this conversation as resolved.
Show resolved Hide resolved

return super.graphql(amplify, cleanOptions, additionalHeaders, {
category: Category.API,
action: ApiAction.GraphQl,
...(internalUserAgentOverride || {}),
yuhengshs marked this conversation as resolved.
Show resolved Hide resolved
});
}

Expand Down
22 changes: 21 additions & 1 deletion packages/core/src/Platform/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export enum Framework {
}

export enum Category {
AI = 'ai',
API = 'api',
Auth = 'auth',
Analytics = 'analytics',
Expand All @@ -39,6 +40,16 @@ export enum Category {
Storage = 'storage',
}

export enum AiAction {
CreateConversation = '1',
GetConversation = '2',
ListConversations = '3',
SendMessage = '4',
ListMessages = '5',
OnMessage = '6',
Generation = '7',
}

export enum AnalyticsAction {
Record = '1',
IdentifyUser = '2',
Expand Down Expand Up @@ -123,6 +134,7 @@ export enum StorageAction {
}

interface ActionMap {
[Category.AI]: AiAction;
[Category.Auth]: AuthAction;
[Category.API]: ApiAction;
[Category.Analytics]: AnalyticsAction;
Expand All @@ -148,6 +160,7 @@ interface CustomUserAgentDetailsBase {

export type CustomUserAgentDetails =
| (CustomUserAgentDetailsBase & { category?: never; action?: never })
| UserAgentDetailsWithCategory<Category.AI>
| UserAgentDetailsWithCategory<Category.API>
| UserAgentDetailsWithCategory<Category.Auth>
| UserAgentDetailsWithCategory<Category.Analytics>
Expand Down Expand Up @@ -180,6 +193,12 @@ export interface StorageUserAgentInput {
additionalDetails: AdditionalDetails;
}

export interface AiUserAgentInput {
category: Category.AI;
apis: AiAction[];
additionalDetails: AdditionalDetails;
}

export interface AuthUserAgentInput {
category: Category.Auth;
apis: AuthAction[];
Expand All @@ -202,4 +221,5 @@ export type SetCustomUserAgentInput =
| StorageUserAgentInput
| AuthUserAgentInput
| InAppMessagingUserAgentInput
| GeoUserAgentInput;
| GeoUserAgentInput
| AiUserAgentInput;
5 changes: 5 additions & 0 deletions packages/core/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,8 @@ export const USER_AGENT_HEADER = 'x-amz-user-agent';
// Error exception code constants
export const NO_HUBCALLBACK_PROVIDED_EXCEPTION =
'NoHubcallbackProvidedException';

// User Agents Override Symbol
export const INTERNAL_USER_AGENT_OVERRIDE = Symbol(
'INTERNAL_USER_AGENT_OVERRIDE',
);
2 changes: 2 additions & 0 deletions packages/core/src/libraryUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export {
getAmplifyUserAgent,
} from './Platform';
export {
AiAction,
ApiAction,
AuthAction,
AnalyticsAction,
Expand All @@ -99,6 +100,7 @@ export {
GeoUserAgentInput,
} from './Platform/types';
export { setCustomUserAgent } from './Platform/customUserAgent';
export { INTERNAL_USER_AGENT_OVERRIDE } from './constants';
yuhengshs marked this conversation as resolved.
Show resolved Hide resolved

// Error handling
export {
Expand Down
Loading