Skip to content

Commit cb8d3c8

Browse files
committed
feat: Add support for trackStreamMetricsOf method
fix: Deprecated toVercelAISDK, trackVercelAISDKStreamTextMetrics fix: Moved types VercelAISDKProvider, VercelAISDKMapOptions, and VercelAISDKConfig to `@launchdarkly/server-sdk-ai-vercel` package
1 parent 8d57904 commit cb8d3c8

File tree

5 files changed

+102
-0
lines changed

5 files changed

+102
-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: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,60 @@ 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 final duration
155+
const duration = Date.now() - startTime;
156+
this.trackDuration(duration);
157+
158+
// Track success/error based on metrics
159+
if (metrics.success) {
160+
this.trackSuccess();
161+
} else {
162+
this.trackError();
163+
}
164+
165+
// Track token usage if available
166+
if (metrics.usage) {
167+
this.trackTokens(metrics.usage);
168+
}
169+
} catch (error) {
170+
// If metrics extraction fails, track error
171+
// but don't throw - stream consumption should not be affected
172+
this.trackError();
173+
}
174+
}
175+
122176
async trackOpenAIMetrics<
123177
TRes extends {
124178
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)