Skip to content

Commit e18979e

Browse files
authored
feat: Add support for trackStreamMetricsOf method (#971)
fix: Deprecated toVercelAISDK, trackVercelAISDKStreamTextMetrics, use `@launchdarkly/server-sdk-ai-vercel` package
1 parent 04c3a03 commit e18979e

File tree

5 files changed

+100
-0
lines changed

5 files changed

+100
-0
lines changed

packages/sdk/server-ai/src/LDAIConfigMapper.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ export class LDAIConfigMapper {
2727
return undefined;
2828
}
2929

30+
/**
31+
* @deprecated Use `VercelProvider.toVercelAISDK()` from the `@launchdarkly/server-sdk-ai-vercel` package instead.
32+
* This method will be removed in a future version.
33+
*/
3034
toVercelAISDK<TMod>(
3135
provider: VercelAISDKProvider<TMod> | Record<string, VercelAISDKProvider<TMod>>,
3236
options?: VercelAISDKMapOptions | undefined,

packages/sdk/server-ai/src/LDAIConfigTrackerImpl.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,58 @@ export class LDAIConfigTrackerImpl implements LDAIConfigTracker {
119119
return result;
120120
}
121121

122+
trackStreamMetricsOf<TStream>(
123+
streamCreator: () => TStream,
124+
metricsExtractor: (stream: TStream) => Promise<LDAIMetrics>,
125+
): TStream {
126+
const startTime = Date.now();
127+
128+
try {
129+
// Create the stream synchronously
130+
const stream = streamCreator();
131+
132+
// Start background metrics tracking (fire and forget)
133+
this._trackStreamMetricsInBackground(stream, metricsExtractor, startTime);
134+
135+
// Return stream immediately for consumption
136+
return stream;
137+
} catch (error) {
138+
// Track error if stream creation fails
139+
this.trackDuration(Date.now() - startTime);
140+
this.trackError();
141+
throw error;
142+
}
143+
}
144+
145+
private async _trackStreamMetricsInBackground<TStream>(
146+
stream: TStream,
147+
metricsExtractor: (stream: TStream) => Promise<LDAIMetrics>,
148+
startTime: number,
149+
): Promise<void> {
150+
try {
151+
// Wait for metrics to be available
152+
const metrics = await metricsExtractor(stream);
153+
154+
// Track success/error based on metrics
155+
if (metrics.success) {
156+
this.trackSuccess();
157+
} else {
158+
this.trackError();
159+
}
160+
161+
// Track token usage if available
162+
if (metrics.usage) {
163+
this.trackTokens(metrics.usage);
164+
}
165+
} catch (error) {
166+
// If metrics extraction fails, track error
167+
this.trackError();
168+
} finally {
169+
// Track duration regardless of success/error
170+
this.trackDuration(Date.now() - startTime);
171+
}
172+
}
173+
122174
async trackOpenAIMetrics<
123175
TRes extends {
124176
usage?: {

packages/sdk/server-ai/src/api/config/LDAIConfig.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ export interface LDAIConfig {
7676
*
7777
* WARNING: this method can throw an exception if a Vercel AI SDK model cannot be determined.
7878
*
79+
* @deprecated Use `VercelProvider.toVercelAISDK()` from the `@launchdarkly/server-sdk-ai-vercel` package instead.
80+
* This method will be removed in a future version.
81+
*
7982
* @param provider A Vercel AI SDK Provider or a map of provider names to Vercel AI SDK Providers.
8083
* @param options Optional mapping options.
8184
* @returns A configuration directly usable in Vercel AI SDK generateText() and streamText()

packages/sdk/server-ai/src/api/config/LDAIConfigTracker.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,32 @@ export interface LDAIConfigTracker {
106106
func: () => Promise<TRes>,
107107
): Promise<TRes>;
108108

109+
/**
110+
* Track metrics for a streaming AI operation.
111+
*
112+
* This function will track the duration of the operation, extract metrics using the provided
113+
* metrics extractor function, and track success or error status accordingly.
114+
*
115+
* Unlike trackMetricsOf, this method is designed for streaming operations where:
116+
* - The stream is created and returned immediately (synchronously)
117+
* - Metrics are extracted asynchronously in the background once the stream completes
118+
* - Duration is tracked from stream creation to metrics extraction completion
119+
*
120+
* The stream is returned immediately so the caller can begin consuming it without waiting.
121+
* Metrics extraction happens in the background and does not block stream consumption.
122+
*
123+
* If the stream creator throws, then this method will also throw and record an error.
124+
* If metrics extraction fails, the error is logged but does not affect stream consumption.
125+
*
126+
* @param streamCreator Function that creates and returns the stream (synchronous)
127+
* @param metricsExtractor Function that asynchronously extracts metrics from the stream
128+
* @returns The stream result (returned immediately, not a Promise)
129+
*/
130+
trackStreamMetricsOf<TStream>(
131+
streamCreator: () => TStream,
132+
metricsExtractor: (stream: TStream) => Promise<LDAIMetrics>,
133+
): TStream;
134+
109135
/**
110136
* Track an OpenAI operation.
111137
*
@@ -187,6 +213,9 @@ export interface LDAIConfigTracker {
187213
* In the case the provided function throws, this function will record the duration and an error.
188214
* A failed operation will not have any token usage data.
189215
*
216+
* @deprecated Use `trackStreamMetricsOf()` with `VercelProvider.createStreamMetricsExtractor()` from the
217+
* `@launchdarkly/server-sdk-ai-vercel` package instead. This method will be removed in a future version.
218+
*
190219
* @param func Function which executes the operation.
191220
* @returns The result of the operation.
192221
*/

packages/sdk/server-ai/src/api/config/VercelAISDK.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
11
import { type LDMessage } from './LDAIConfig';
22

3+
/**
4+
* @deprecated Use `VercelAISDKProvider` from the `@launchdarkly/server-sdk-ai-vercel` package instead.
5+
* This type will be removed in a future version.
6+
*/
37
export type VercelAISDKProvider<TMod> = (modelName: string) => TMod;
48

9+
/**
10+
* @deprecated Use `VercelAISDKMapOptions` from the `@launchdarkly/server-sdk-ai-vercel` package instead.
11+
* This type will be removed in a future version.
12+
*/
513
export interface VercelAISDKMapOptions {
614
nonInterpolatedMessages?: LDMessage[] | undefined;
715
}
816

17+
/**
18+
* @deprecated Use `VercelAISDKConfig` from the `@launchdarkly/server-sdk-ai-vercel` package instead.
19+
* This type will be removed in a future version.
20+
*/
921
export interface VercelAISDKConfig<TMod> {
1022
model: TMod;
1123
messages?: LDMessage[] | undefined;

0 commit comments

Comments
 (0)