Skip to content

Commit d890752

Browse files
fixes for context
1 parent e4d3d8c commit d890752

5 files changed

Lines changed: 94 additions & 30 deletions

File tree

packages/coded-action-apps/src/coded-action-apps-service.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { track } from './telemetry';
2-
31
import {
42
Task,
53
MessageSeverity,
64
TaskCompleteResponse,
75
} from './types';
86
import { CodedActionAppsServiceModel } from './coded-action-apps.models';
97
import { ActionCenterEventNames, ActionCenterEventResponsePayload } from './types.internal';
8+
import { telemetryClient, track } from './telemetry';
9+
import { loadFromMetaTags } from './telemetry/ts-sdk-config/runtime';
1010

1111
const INIT_TIMEOUT = 3000;
1212

@@ -17,6 +17,16 @@ export class CodedActionAppsService implements CodedActionAppsServiceModel {
1717
private readonly parentOrigin = new URLSearchParams(window.location.search).get('basedomain');
1818
private isCompletingTask = false;
1919

20+
constructor() {
21+
const configFromMetaTags = loadFromMetaTags();
22+
telemetryClient.initialize({
23+
baseUrl: configFromMetaTags?.baseUrl,
24+
orgName: configFromMetaTags?.orgName,
25+
tenantName: configFromMetaTags?.tenantName,
26+
clientId: configFromMetaTags?.clientId,
27+
});
28+
}
29+
2030
/**
2131
* Notifies Action Center that the task data has been changed by the user.
2232
* This is needed to enable the save button in Action Center when the task data has changed
@@ -40,7 +50,7 @@ export class CodedActionAppsService implements CodedActionAppsServiceModel {
4050
* @throws {Error} If called from an untrusted origin.
4151
* @throws {Error} If a completeTask call is already in progress.
4252
*/
43-
@track('CodedActionApps.CompleteTask')
53+
@track('CodedActionApp.CompleteTask')
4454
completeTask(actionTaken: string, data: unknown): Promise<TaskCompleteResponse> {
4555
if (this.isCompletingTask) {
4656
throw new Error('A completeTask call is already in progress');
@@ -92,7 +102,7 @@ export class CodedActionAppsService implements CodedActionAppsServiceModel {
92102
* @throws {Error} If called from an untrusted origin.
93103
* @throws {Error} If Action Center does not respond within the allotted timeout.
94104
*/
95-
@track('CodedActionApps.GetTask')
105+
@track('CodedActionApp.GetTask')
96106
getTask(): Promise<Task> {
97107
return new Promise((resolve, reject) => {
98108
if (!this.isValidOrigin(this.parentOrigin)) {
Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
// Side-effect import: bootstraps the CAA package's TelemetryClient
2-
// before any service is constructed. Kept explicit (rather than relying
3-
// on `coded-action-apps-service.ts` transitively importing `track` from
4-
// `./telemetry`) so initialization survives any future refactor of the
5-
// service file's imports.
6-
import './telemetry';
7-
81
export { CodedActionAppsService as CodedActionApps, CodedActionAppsService } from './coded-action-apps-service';
92
export * from './coded-action-apps.models';
103
export * from './types';

packages/coded-action-apps/src/telemetry/constants.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
export const CAA_VERSION = '$CODED_ACTION_APP_SDK_VERSION';
1010

11-
export const CAA_SERVICE_NAME = 'UiPath.CodedActionApps';
12-
export const CAA_CLOUD_ROLE_NAME = 'uipath-ts-coded-action-apps';
13-
export const CAA_LOGGER_NAME = 'uipath-ts-caa-telemetry';
14-
export const CAA_RUN_EVENT = 'CodedActionApps.Run';
11+
export const CAA_SERVICE_NAME = 'UiPath.CodedActionApp.Sdk';
12+
export const CAA_CLOUD_ROLE_NAME = 'uipath-coded-action-app-sdk';
13+
export const CAA_LOGGER_NAME = 'uipath-coded-action-app-telemetry';
14+
export const CAA_RUN_EVENT = 'CodedActionApp.Sdk.Run';
Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
/**
22
* Coded Action Apps telemetry bootstrap.
33
*
4-
* Constructs the CAA package's own `TelemetryClient`, initializes it with
5-
* the CAA identity, and exposes a CAA-bound `track` / `trackEvent` that
6-
* the rest of the package imports. This module is also imported as a
7-
* side effect from `src/index.ts` so the client is initialized before
8-
* any service is constructed.
4+
* Constructs the CAA package's own `TelemetryClient` and exposes a
5+
* CAA-bound `track` / `trackEvent` plus a `telemetryClient` handle whose
6+
* `initialize(context)` wraps the underlying client with the CAA identity
7+
* baked in. Initialization is deferred until the customer constructs a
8+
* `CodedActionAppsService` with config — same flow as the TypeScript SDK,
9+
* where `UiPath`'s constructor calls `telemetryClient.initialize(...)`.
910
*
1011
* Running alongside `@uipath/uipath-typescript` is safe — each package
11-
* owns its own client, so the SDK's events keep the SDK identity and
12-
* the CAA's events keep the CAA identity.
12+
* owns its own client (keyed by its own `cloudRoleName`), so the SDK's
13+
* events keep the SDK identity and the CAA's events keep the CAA identity.
1314
*/
1415

1516
import {
1617
createTrack,
1718
createTrackEvent,
1819
getOrCreateClient,
1920
} from '@uipath/telemetry';
21+
import type { TelemetryContext } from '@uipath/telemetry';
2022

2123
import {
2224
CAA_CLOUD_ROLE_NAME,
@@ -32,13 +34,23 @@ import {
3234
// are loaded into the same process.
3335
const caaClient = getOrCreateClient(CAA_CLOUD_ROLE_NAME);
3436

35-
caaClient.initialize({
36-
sdkVersion: CAA_VERSION,
37-
serviceName: CAA_SERVICE_NAME,
38-
cloudRoleName: CAA_CLOUD_ROLE_NAME,
39-
loggerName: CAA_LOGGER_NAME,
40-
defaultEventName: CAA_RUN_EVENT,
41-
});
42-
4337
export const track = createTrack(caaClient);
4438
export const trackEvent = createTrackEvent(caaClient);
39+
40+
/**
41+
* CAA-facing telemetry handle. The first call wins — subsequent
42+
* `initialize` calls (e.g. when multiple `CodedActionAppsService`
43+
* instances exist) are silently ignored by the underlying client.
44+
*/
45+
export const telemetryClient = {
46+
initialize(context?: TelemetryContext): void {
47+
caaClient.initialize({
48+
sdkVersion: CAA_VERSION,
49+
serviceName: CAA_SERVICE_NAME,
50+
cloudRoleName: CAA_CLOUD_ROLE_NAME,
51+
loggerName: CAA_LOGGER_NAME,
52+
defaultEventName: CAA_RUN_EVENT,
53+
context,
54+
});
55+
},
56+
};
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* Reads UiPath cloud context from HTML meta tags injected at runtime.
3+
*
4+
* Meta tags are emitted by `@uipath/uipath-typescript` consumers (Apps
5+
* service at deploy time, `@uipath/coded-apps-dev` plugin during local
6+
* dev). Mirrors the SDK's `src/core/config/runtime.ts` so CAA can
7+
* discover the same hosting context without taking a constructor arg.
8+
*/
9+
10+
const META_TAG_NAMES = {
11+
CLIENT_ID: 'uipath:client-id',
12+
ORG_NAME: 'uipath:org-name',
13+
TENANT_NAME: 'uipath:tenant-name',
14+
BASE_URL: 'uipath:base-url',
15+
SCOPE: 'uipath:scope',
16+
} as const;
17+
18+
export interface MetaTagsConfig {
19+
clientId?: string;
20+
scope?: string;
21+
orgName?: string;
22+
tenantName?: string;
23+
baseUrl?: string;
24+
}
25+
26+
function getMetaTagContent(name: string): string | undefined {
27+
if (typeof document === 'undefined') return undefined;
28+
return document.querySelector<HTMLMetaElement>(`meta[name="${name}"]`)?.content;
29+
}
30+
31+
/**
32+
* Load cloud context from `<meta>` tags. Returns `null` when none of the
33+
* recognized tags are present (e.g. running outside the Action Center
34+
* iframe), so the caller can decide how to handle a missing context.
35+
*/
36+
export function loadFromMetaTags(): MetaTagsConfig | null {
37+
if (typeof window === 'undefined') return null;
38+
39+
const config: MetaTagsConfig = {
40+
clientId: getMetaTagContent(META_TAG_NAMES.CLIENT_ID),
41+
scope: getMetaTagContent(META_TAG_NAMES.SCOPE),
42+
orgName: getMetaTagContent(META_TAG_NAMES.ORG_NAME),
43+
tenantName: getMetaTagContent(META_TAG_NAMES.TENANT_NAME),
44+
baseUrl: getMetaTagContent(META_TAG_NAMES.BASE_URL),
45+
};
46+
47+
const hasAnyValue = Object.values(config).some(Boolean);
48+
return hasAnyValue ? config : null;
49+
}

0 commit comments

Comments
 (0)