Skip to content

Commit

Permalink
noahdarveau/ The Last Treeshaking PR (#2593)
Browse files Browse the repository at this point in the history
* updated last of private capabilities to be treeshakable
  • Loading branch information
noahdarveau-MSFT authored Nov 1, 2024
1 parent 6ce5c0a commit f2f4757
Show file tree
Hide file tree
Showing 20 changed files with 927 additions and 911 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "minor",
"comment": "Made the `copilot`, `hostEntity`, `messageChannels`, and `teams` files treeshakable",
"packageName": "@microsoft/teams-js",
"email": "[email protected]",
"dependentChangeType": "patch"
}
96 changes: 0 additions & 96 deletions packages/teams-js/src/private/copilot.ts

This file was deleted.

3 changes: 3 additions & 0 deletions packages/teams-js/src/private/copilot/copilot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import * as eligibility from './eligibility';

export { eligibility };
86 changes: 86 additions & 0 deletions packages/teams-js/src/private/copilot/eligibility.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { sendAndUnwrap } from '../../internal/communication';
import { ensureInitialized } from '../../internal/internalAPIs';
import { ApiName, ApiVersionNumber, getApiVersionTag, getLogger } from '../../internal/telemetry';
import { errorNotSupportedOnPlatform } from '../../public/constants';
import { AppEligibilityInformation, isSdkError, SdkError } from '../../public/interfaces';
import { runtime } from '../../public/runtime';

const copilotTelemetryVersionNumber: ApiVersionNumber = ApiVersionNumber.V_2;
const copilotLogger = getLogger('copilot');

/**
* @beta
* @hidden
* User information required by specific apps
* @internal
* Limited to Microsoft-internal use
*/
/**
* @hidden
* @internal
* Limited to Microsoft-internal use
* @beta
* @returns boolean to represent whether copilot.eligibility capability is supported
*
* @throws Error if {@linkcode app.initialize} has not successfully completed
*/
export function isSupported(): boolean {
return (
ensureInitialized(runtime) &&
(!!runtime.hostVersionsInfo?.appEligibilityInformation || !!runtime.supports.copilot?.eligibility)
);
}

/**
* @hidden
* @internal
* Limited to Microsoft-internal use
* @beta
* @returns the copilot eligibility information about the user
*
* @throws Error if {@linkcode app.initialize} has not successfully completed
*/
export async function getEligibilityInfo(): Promise<AppEligibilityInformation> {
ensureInitialized(runtime);
if (!isSupported()) {
throw new Error(`Error code: ${errorNotSupportedOnPlatform.errorCode}, message: Not supported on platform`);
}

// Return the eligibility information if it is already available
if (runtime.hostVersionsInfo?.appEligibilityInformation) {
copilotLogger('Eligibility information is already available on runtime.');
return runtime.hostVersionsInfo!.appEligibilityInformation;
}

copilotLogger('Eligibility information is not available on runtime. Requesting from host.');
// Send message to host SDK to get eligibility information
const response = await sendAndUnwrap<AppEligibilityInformation | SdkError>(
getApiVersionTag(copilotTelemetryVersionNumber, ApiName.Copilot_Eligibility_GetEligibilityInfo),
ApiName.Copilot_Eligibility_GetEligibilityInfo,
);

if (isSdkError(response)) {
throw new Error(
`Error code: ${response.errorCode}, message: ${response.message ?? 'Failed to get eligibility information from the host.'}`,
);
}
// validate response
if (!isEligibilityInfoValid(response)) {
throw new Error('Error deserializing eligibility information');
}
return response;
}

function isEligibilityInfoValid(eligibilityInfo: AppEligibilityInformation): boolean {
if (
eligibilityInfo.ageGroup === undefined ||
eligibilityInfo.cohort === undefined ||
eligibilityInfo.userClassification === undefined ||
eligibilityInfo.isCopilotEligible === undefined ||
eligibilityInfo.isCopilotEnabledRegion === undefined ||
eligibilityInfo.isOptedOutByAdmin === undefined
) {
return false;
}
return true;
}
Loading

0 comments on commit f2f4757

Please sign in to comment.