Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin' into ta/sdk-849/fdv2-streaming-…
Browse files Browse the repository at this point in the history
…datasource
  • Loading branch information
tanderson-ld committed Dec 9, 2024
2 parents aeeb9ba + 686c1f3 commit 842e5f4
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 14 deletions.
3 changes: 2 additions & 1 deletion packages/sdk/browser/contract-tests/adapter/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"strict": true,
"moduleResolution": "node",
"outDir": "dist",
"sourceMap": true
"sourceMap": true,
"skipLibCheck": true
},
"lib": ["ES6"],
"exclude": ["**/*.test.ts", "dist", "node_modules"]
Expand Down
6 changes: 3 additions & 3 deletions packages/sdk/react-native/example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
"dependencies": {
"@launchdarkly/react-native-client-sdk": "workspace:^",
"@react-native-async-storage/async-storage": "^1.21.0",
"expo": "51.0.31",
"expo": "52.0.14",
"expo-status-bar": "~1.11.1",
"react": "18.2.0",
"react-native": "0.74.3",
"react": "18.3.1",
"react-native": "0.76.3",
"react-native-dotenv": "^3.4.9"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/react-universal/example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"@next/eslint-plugin-next": "^14.2.4",
"@trivago/prettier-plugin-sort-imports": "^4.3.0",
"@types/node": "^20",
"@types/react": "^18",
"@types/react": "18.3.13",
"@types/react-dom": "^18",
"autoprefixer": "^10.0.1",
"eslint": "^8",
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/react-universal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"devDependencies": {
"@trivago/prettier-plugin-sort-imports": "^4.1.1",
"@types/jest": "^29.5.0",
"@types/react": "^18",
"@types/react": "18.3.13",
"@typescript-eslint/eslint-plugin": "^6.20.0",
"@typescript-eslint/parser": "^6.20.0",
"eslint": "^8.45.0",
Expand Down
36 changes: 36 additions & 0 deletions packages/sdk/server-ai/__tests__/LDAIConfigTrackerImpl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,3 +268,39 @@ it('only tracks non-zero token counts', () => {
expect.anything(),
);
});

it('returns empty summary when no metrics tracked', () => {
const tracker = new LDAIConfigTrackerImpl(mockLdClient, configKey, versionKey, testContext);

const summary = tracker.getSummary();

expect(summary).toEqual({});
});

it('summarizes tracked metrics', () => {
const tracker = new LDAIConfigTrackerImpl(mockLdClient, configKey, versionKey, testContext);

tracker.trackDuration(1000);
tracker.trackTokens({
total: 100,
input: 40,
output: 60,
});
tracker.trackFeedback({ kind: LDFeedbackKind.Positive });
tracker.trackSuccess();

const summary = tracker.getSummary();

expect(summary).toEqual({
durationMs: 1000,
tokens: {
total: 100,
input: 40,
output: 60,
},
feedback: {
kind: 'positive',
},
success: true,
});
});
14 changes: 14 additions & 0 deletions packages/sdk/server-ai/src/LDAIConfigTrackerImpl.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { LDContext } from '@launchdarkly/js-server-sdk-common';

import { LDAIConfigTracker } from './api/config';
import { LDAIMetricSummary } from './api/config/LDAIConfigTracker';
import { createBedrockTokenUsage, LDFeedbackKind, LDTokenUsage } from './api/metrics';
import { createOpenAiUsage } from './api/metrics/OpenAiUsage';
import { LDClientMin } from './LDClientMin';

export class LDAIConfigTrackerImpl implements LDAIConfigTracker {
private _trackedMetrics: LDAIMetricSummary = {};

constructor(
private _ldClient: LDClientMin,
private _configKey: string,
Expand All @@ -21,6 +24,7 @@ export class LDAIConfigTrackerImpl implements LDAIConfigTracker {
}

trackDuration(duration: number): void {
this._trackedMetrics.durationMs = duration;
this._ldClient.track('$ld:ai:duration:total', this._context, this._getTrackData(), duration);
}

Expand All @@ -34,6 +38,7 @@ export class LDAIConfigTrackerImpl implements LDAIConfigTracker {
}

trackFeedback(feedback: { kind: LDFeedbackKind }): void {
this._trackedMetrics.feedback = feedback;
if (feedback.kind === LDFeedbackKind.Positive) {
this._ldClient.track('$ld:ai:feedback:user:positive', this._context, this._getTrackData(), 1);
} else if (feedback.kind === LDFeedbackKind.Negative) {
Expand All @@ -42,6 +47,7 @@ export class LDAIConfigTrackerImpl implements LDAIConfigTracker {
}

trackSuccess(): void {
this._trackedMetrics.success = true;
this._ldClient.track('$ld:ai:generation', this._context, this._getTrackData(), 1);
}

Expand Down Expand Up @@ -88,6 +94,7 @@ export class LDAIConfigTrackerImpl implements LDAIConfigTracker {
}

trackTokens(tokens: LDTokenUsage): void {
this._trackedMetrics.tokens = tokens;
const trackData = this._getTrackData();
if (tokens.total > 0) {
this._ldClient.track('$ld:ai:tokens:total', this._context, trackData, tokens.total);
Expand All @@ -99,4 +106,11 @@ export class LDAIConfigTrackerImpl implements LDAIConfigTracker {
this._ldClient.track('$ld:ai:tokens:output', this._context, trackData, tokens.output);
}
}

/**
* Get a summary of the tracked metrics.
*/
getSummary(): LDAIMetricSummary {
return { ...this._trackedMetrics };
}
}
30 changes: 30 additions & 0 deletions packages/sdk/server-ai/src/api/config/LDAIConfigTracker.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
import { LDFeedbackKind, LDTokenUsage } from '../metrics';

/**
* Metrics which have been tracked.
*/
export interface LDAIMetricSummary {
/**
* The duration of generation.
*/
durationMs?: number;

/**
* Information about token usage.
*/
tokens?: LDTokenUsage;

/**
* Was generation successful.
*/
success?: boolean;

/**
* Any sentiment about the generation.
*/
feedback?: { kind: LDFeedbackKind };
}

/**
* The LDAIConfigTracker is used to track various details about AI operations.
*/
Expand Down Expand Up @@ -76,4 +101,9 @@ export interface LDAIConfigTracker {
>(
res: TRes,
): TRes;

/**
* Get a summary of the tracked metrics.
*/
getSummary(): LDAIMetricSummary;
}
15 changes: 7 additions & 8 deletions packages/shared/sdk-server/src/LDClientImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ import { BigSegmentStoreMembership } from './api/interfaces';
import { LDWaitForInitializationOptions } from './api/LDWaitForInitializationOptions';
import BigSegmentsManager from './BigSegmentsManager';
import BigSegmentStoreStatusProvider from './BigSegmentStatusProviderImpl';
import { createPayloadListener } from './data_sources/createStreamListenersFDv2';
import { createStreamListeners } from './data_sources/createStreamListeners';
import DataSourceUpdates from './data_sources/DataSourceUpdates';
import PollingProcessor from './data_sources/PollingProcessor';
import Requestor from './data_sources/Requestor';
import StreamingProcessorFDv2 from './data_sources/StreamingProcessorFDv2';
import StreamingProcessor from './data_sources/StreamingProcessor';
import createDiagnosticsInitConfig from './diagnostics/createDiagnosticsInitConfig';
import { allAsync } from './evaluation/collection';
import { Flag } from './evaluation/data/Flag';
Expand Down Expand Up @@ -216,17 +216,16 @@ export default class LDClientImpl implements LDClient {
};
this._evaluator = new Evaluator(this._platform, queries);

const payloadListener = createPayloadListener(dataSourceUpdates, this.logger, () =>
this._initSuccess(),
);

const listeners = createStreamListeners(dataSourceUpdates, this._logger, {
put: () => this._initSuccess(),
});
const makeDefaultProcessor = () =>
config.stream
? new StreamingProcessorFDv2(
? new StreamingProcessor(
clientContext,
'/all',
[],
payloadListener,
listeners,
baseHeaders,
this._diagnosticsManager,
(e) => this._dataSourceErrorHandler(e),
Expand Down

0 comments on commit 842e5f4

Please sign in to comment.