@launchdarkly/server-sdk-ai 0.5.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -12,13 +12,13 @@ export class LDAIConfigTrackerImpl implements LDAIConfigTracker {
12
12
  constructor(
13
13
  private _ldClient: LDClientMin,
14
14
  private _configKey: string,
15
- private _versionKey: string,
15
+ private _variationKey: string,
16
16
  private _context: LDContext,
17
17
  ) {}
18
18
 
19
- private _getTrackData(): { versionKey: string; configKey: string } {
19
+ private _getTrackData(): { variationKey: string; configKey: string } {
20
20
  return {
21
- versionKey: this._versionKey,
21
+ variationKey: this._variationKey,
22
22
  configKey: this._configKey,
23
23
  };
24
24
  }
@@ -30,11 +30,15 @@ export class LDAIConfigTrackerImpl implements LDAIConfigTracker {
30
30
 
31
31
  async trackDurationOf<TRes>(func: () => Promise<TRes>): Promise<TRes> {
32
32
  const startTime = Date.now();
33
- const result = await func();
34
- const endTime = Date.now();
35
- const duration = endTime - startTime; // duration in milliseconds
36
- this.trackDuration(duration);
37
- return result;
33
+ try {
34
+ // Be sure to await here so that we can track the duration of the function and also handle errors.
35
+ const result = await func();
36
+ return result;
37
+ } finally {
38
+ const endTime = Date.now();
39
+ const duration = endTime - startTime; // duration in milliseconds
40
+ this.trackDuration(duration);
41
+ }
38
42
  }
39
43
 
40
44
  trackFeedback(feedback: { kind: LDFeedbackKind }): void {
@@ -49,6 +53,13 @@ export class LDAIConfigTrackerImpl implements LDAIConfigTracker {
49
53
  trackSuccess(): void {
50
54
  this._trackedMetrics.success = true;
51
55
  this._ldClient.track('$ld:ai:generation', this._context, this._getTrackData(), 1);
56
+ this._ldClient.track('$ld:ai:generation:success', this._context, this._getTrackData(), 1);
57
+ }
58
+
59
+ trackError(): void {
60
+ this._trackedMetrics.success = false;
61
+ this._ldClient.track('$ld:ai:generation', this._context, this._getTrackData(), 1);
62
+ this._ldClient.track('$ld:ai:generation:error', this._context, this._getTrackData(), 1);
52
63
  }
53
64
 
54
65
  async trackOpenAIMetrics<
@@ -60,12 +71,17 @@ export class LDAIConfigTrackerImpl implements LDAIConfigTracker {
60
71
  };
61
72
  },
62
73
  >(func: () => Promise<TRes>): Promise<TRes> {
63
- const result = await this.trackDurationOf(func);
64
- this.trackSuccess();
65
- if (result.usage) {
66
- this.trackTokens(createOpenAiUsage(result.usage));
74
+ try {
75
+ const result = await this.trackDurationOf(func);
76
+ this.trackSuccess();
77
+ if (result.usage) {
78
+ this.trackTokens(createOpenAiUsage(result.usage));
79
+ }
80
+ return result;
81
+ } catch (err) {
82
+ this.trackError();
83
+ throw err;
67
84
  }
68
- return result;
69
85
  }
70
86
 
71
87
  trackBedrockConverseMetrics<
@@ -82,7 +98,7 @@ export class LDAIConfigTrackerImpl implements LDAIConfigTracker {
82
98
  if (res.$metadata?.httpStatusCode === 200) {
83
99
  this.trackSuccess();
84
100
  } else if (res.$metadata?.httpStatusCode && res.$metadata.httpStatusCode >= 400) {
85
- // Potentially add error tracking in the future.
101
+ this.trackError();
86
102
  }
87
103
  if (res.metrics && res.metrics.latencyMs) {
88
104
  this.trackDuration(res.metrics.latencyMs);
@@ -50,6 +50,11 @@ export interface LDAIConfigTracker {
50
50
  */
51
51
  trackSuccess(): void;
52
52
 
53
+ /**
54
+ * An error was encountered during generation.
55
+ */
56
+ trackError(): void;
57
+
53
58
  /**
54
59
  * Track sentiment about the generation.
55
60
  *
@@ -59,6 +64,12 @@ export interface LDAIConfigTracker {
59
64
 
60
65
  /**
61
66
  * Track the duration of execution of the provided function.
67
+ *
68
+ * If the provided function throws, then this method will also throw.
69
+ * In the case the provided function throws, this function will still record the duration.
70
+ *
71
+ * This function does not automatically record an error when the function throws.
72
+ *
62
73
  * @param func The function to track the duration of.
63
74
  * @returns The result of the function.
64
75
  */
@@ -67,6 +78,12 @@ export interface LDAIConfigTracker {
67
78
  /**
68
79
  * Track an OpenAI operation.
69
80
  *
81
+ * This function will track the duration of the operation, the token usage, and the success or error status.
82
+ *
83
+ * If the provided function throws, then this method will also throw.
84
+ * In the case the provided function throws, this function will record the duration and an error.
85
+ * A failed operation will not have any token usage data.
86
+ *
70
87
  * @param func Function which executes the operation.
71
88
  * @returns The result of the operation.
72
89
  */
@@ -85,6 +102,8 @@ export interface LDAIConfigTracker {
85
102
  /**
86
103
  * Track an operation which uses Bedrock.
87
104
  *
105
+ * This function will track the duration of the operation, the token usage, and the success or error status.
106
+ *
88
107
  * @param res The result of the Bedrock operation.
89
108
  * @returns The input operation.
90
109
  */