@launchdarkly/server-sdk-ai 0.14.0 → 0.15.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.
Files changed (197) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/dist/index.cjs +1117 -0
  3. package/dist/index.cjs.map +1 -0
  4. package/dist/index.d.cts +1022 -0
  5. package/dist/index.d.ts +1022 -0
  6. package/dist/index.js +1071 -0
  7. package/dist/index.js.map +1 -0
  8. package/package.json +22 -5
  9. package/__tests__/Judge.test.ts +0 -496
  10. package/__tests__/LDAIClientImpl.test.ts +0 -589
  11. package/__tests__/LDAIConfigTrackerImpl.test.ts +0 -815
  12. package/__tests__/TokenUsage.test.ts +0 -119
  13. package/__tests__/TrackedChat.test.ts +0 -230
  14. package/dist/package.json +0 -53
  15. package/dist/src/LDAIClientImpl.d.ts +0 -39
  16. package/dist/src/LDAIClientImpl.d.ts.map +0 -1
  17. package/dist/src/LDAIClientImpl.js +0 -164
  18. package/dist/src/LDAIClientImpl.js.map +0 -1
  19. package/dist/src/LDAIConfigTrackerImpl.d.ts +0 -73
  20. package/dist/src/LDAIConfigTrackerImpl.d.ts.map +0 -1
  21. package/dist/src/LDAIConfigTrackerImpl.js +0 -203
  22. package/dist/src/LDAIConfigTrackerImpl.js.map +0 -1
  23. package/dist/src/LDClientMin.d.ts +0 -11
  24. package/dist/src/LDClientMin.d.ts.map +0 -1
  25. package/dist/src/LDClientMin.js +0 -3
  26. package/dist/src/LDClientMin.js.map +0 -1
  27. package/dist/src/api/LDAIClient.d.ts +0 -258
  28. package/dist/src/api/LDAIClient.d.ts.map +0 -1
  29. package/dist/src/api/LDAIClient.js +0 -3
  30. package/dist/src/api/LDAIClient.js.map +0 -1
  31. package/dist/src/api/chat/TrackedChat.d.ts +0 -72
  32. package/dist/src/api/chat/TrackedChat.d.ts.map +0 -1
  33. package/dist/src/api/chat/TrackedChat.js +0 -125
  34. package/dist/src/api/chat/TrackedChat.js.map +0 -1
  35. package/dist/src/api/chat/index.d.ts +0 -3
  36. package/dist/src/api/chat/index.d.ts.map +0 -1
  37. package/dist/src/api/chat/index.js +0 -19
  38. package/dist/src/api/chat/index.js.map +0 -1
  39. package/dist/src/api/chat/types.d.ts +0 -22
  40. package/dist/src/api/chat/types.d.ts.map +0 -1
  41. package/dist/src/api/chat/types.js +0 -3
  42. package/dist/src/api/chat/types.js.map +0 -1
  43. package/dist/src/api/config/LDAIConfigTracker.d.ts +0 -203
  44. package/dist/src/api/config/LDAIConfigTracker.d.ts.map +0 -1
  45. package/dist/src/api/config/LDAIConfigTracker.js +0 -3
  46. package/dist/src/api/config/LDAIConfigTracker.js.map +0 -1
  47. package/dist/src/api/config/LDAIConfigUtils.d.ts +0 -2
  48. package/dist/src/api/config/LDAIConfigUtils.d.ts.map +0 -1
  49. package/dist/src/api/config/LDAIConfigUtils.js +0 -141
  50. package/dist/src/api/config/LDAIConfigUtils.js.map +0 -1
  51. package/dist/src/api/config/index.d.ts +0 -3
  52. package/dist/src/api/config/index.d.ts.map +0 -1
  53. package/dist/src/api/config/index.js +0 -18
  54. package/dist/src/api/config/index.js.map +0 -1
  55. package/dist/src/api/config/types.d.ts +0 -202
  56. package/dist/src/api/config/types.d.ts.map +0 -1
  57. package/dist/src/api/config/types.js +0 -3
  58. package/dist/src/api/config/types.js.map +0 -1
  59. package/dist/src/api/index.d.ts +0 -7
  60. package/dist/src/api/index.d.ts.map +0 -1
  61. package/dist/src/api/index.js +0 -23
  62. package/dist/src/api/index.js.map +0 -1
  63. package/dist/src/api/judge/EvaluationSchemaBuilder.d.ts +0 -11
  64. package/dist/src/api/judge/EvaluationSchemaBuilder.d.ts.map +0 -1
  65. package/dist/src/api/judge/EvaluationSchemaBuilder.js +0 -52
  66. package/dist/src/api/judge/EvaluationSchemaBuilder.js.map +0 -1
  67. package/dist/src/api/judge/Judge.d.ts +0 -63
  68. package/dist/src/api/judge/Judge.d.ts.map +0 -1
  69. package/dist/src/api/judge/Judge.js +0 -149
  70. package/dist/src/api/judge/Judge.js.map +0 -1
  71. package/dist/src/api/judge/index.d.ts +0 -3
  72. package/dist/src/api/judge/index.d.ts.map +0 -1
  73. package/dist/src/api/judge/index.js +0 -6
  74. package/dist/src/api/judge/index.js.map +0 -1
  75. package/dist/src/api/judge/types.d.ts +0 -35
  76. package/dist/src/api/judge/types.d.ts.map +0 -1
  77. package/dist/src/api/judge/types.js +0 -3
  78. package/dist/src/api/judge/types.js.map +0 -1
  79. package/dist/src/api/metrics/BedrockTokenUsage.d.ts +0 -7
  80. package/dist/src/api/metrics/BedrockTokenUsage.d.ts.map +0 -1
  81. package/dist/src/api/metrics/BedrockTokenUsage.js +0 -12
  82. package/dist/src/api/metrics/BedrockTokenUsage.js.map +0 -1
  83. package/dist/src/api/metrics/LDAIMetrics.d.ts +0 -17
  84. package/dist/src/api/metrics/LDAIMetrics.d.ts.map +0 -1
  85. package/dist/src/api/metrics/LDAIMetrics.js +0 -3
  86. package/dist/src/api/metrics/LDAIMetrics.js.map +0 -1
  87. package/dist/src/api/metrics/LDFeedbackKind.d.ts +0 -14
  88. package/dist/src/api/metrics/LDFeedbackKind.d.ts.map +0 -1
  89. package/dist/src/api/metrics/LDFeedbackKind.js +0 -18
  90. package/dist/src/api/metrics/LDFeedbackKind.js.map +0 -1
  91. package/dist/src/api/metrics/LDTokenUsage.d.ts +0 -18
  92. package/dist/src/api/metrics/LDTokenUsage.d.ts.map +0 -1
  93. package/dist/src/api/metrics/LDTokenUsage.js +0 -3
  94. package/dist/src/api/metrics/LDTokenUsage.js.map +0 -1
  95. package/dist/src/api/metrics/OpenAiUsage.d.ts +0 -7
  96. package/dist/src/api/metrics/OpenAiUsage.d.ts.map +0 -1
  97. package/dist/src/api/metrics/OpenAiUsage.js +0 -13
  98. package/dist/src/api/metrics/OpenAiUsage.js.map +0 -1
  99. package/dist/src/api/metrics/VercelAISDKTokenUsage.d.ts +0 -9
  100. package/dist/src/api/metrics/VercelAISDKTokenUsage.d.ts.map +0 -1
  101. package/dist/src/api/metrics/VercelAISDKTokenUsage.js +0 -13
  102. package/dist/src/api/metrics/VercelAISDKTokenUsage.js.map +0 -1
  103. package/dist/src/api/metrics/index.d.ts +0 -7
  104. package/dist/src/api/metrics/index.d.ts.map +0 -1
  105. package/dist/src/api/metrics/index.js +0 -23
  106. package/dist/src/api/metrics/index.js.map +0 -1
  107. package/dist/src/api/providers/AIProvider.d.ts +0 -52
  108. package/dist/src/api/providers/AIProvider.d.ts.map +0 -1
  109. package/dist/src/api/providers/AIProvider.js +0 -88
  110. package/dist/src/api/providers/AIProvider.js.map +0 -1
  111. package/dist/src/api/providers/AIProviderFactory.d.ts +0 -39
  112. package/dist/src/api/providers/AIProviderFactory.d.ts.map +0 -1
  113. package/dist/src/api/providers/AIProviderFactory.js +0 -102
  114. package/dist/src/api/providers/AIProviderFactory.js.map +0 -1
  115. package/dist/src/api/providers/index.d.ts +0 -3
  116. package/dist/src/api/providers/index.d.ts.map +0 -1
  117. package/dist/src/api/providers/index.js +0 -19
  118. package/dist/src/api/providers/index.js.map +0 -1
  119. package/dist/src/index.d.ts +0 -19
  120. package/dist/src/index.d.ts.map +0 -1
  121. package/dist/src/index.js +0 -29
  122. package/dist/src/index.js.map +0 -1
  123. package/docs/.nojekyll +0 -1
  124. package/docs/assets/highlight.css +0 -92
  125. package/docs/assets/main.js +0 -58
  126. package/docs/assets/search.js +0 -1
  127. package/docs/assets/style.css +0 -1379
  128. package/docs/classes/AIProvider.html +0 -210
  129. package/docs/classes/AIProviderFactory.html +0 -208
  130. package/docs/classes/Judge.html +0 -322
  131. package/docs/classes/TrackedChat.html +0 -322
  132. package/docs/enums/LDFeedbackKind.html +0 -115
  133. package/docs/functions/createBedrockTokenUsage.html +0 -94
  134. package/docs/functions/createOpenAiUsage.html +0 -94
  135. package/docs/functions/createVercelAISDKTokenUsage.html +0 -98
  136. package/docs/functions/initAi.html +0 -93
  137. package/docs/index.html +0 -136
  138. package/docs/interfaces/ChatResponse.html +0 -130
  139. package/docs/interfaces/EvalScore.html +0 -119
  140. package/docs/interfaces/JudgeResponse.html +0 -129
  141. package/docs/interfaces/LDAIAgentConfig.html +0 -167
  142. package/docs/interfaces/LDAIAgentConfigDefault.html +0 -155
  143. package/docs/interfaces/LDAIAgentRequestConfig.html +0 -129
  144. package/docs/interfaces/LDAIClient.html +0 -449
  145. package/docs/interfaces/LDAICompletionConfig.html +0 -167
  146. package/docs/interfaces/LDAICompletionConfigDefault.html +0 -155
  147. package/docs/interfaces/LDAIConfig.html +0 -148
  148. package/docs/interfaces/LDAIConfigDefault.html +0 -133
  149. package/docs/interfaces/LDAIConfigTracker.html +0 -510
  150. package/docs/interfaces/LDAIJudgeConfig.html +0 -167
  151. package/docs/interfaces/LDAIJudgeConfigDefault.html +0 -155
  152. package/docs/interfaces/LDAIMetrics.html +0 -121
  153. package/docs/interfaces/LDJudge.html +0 -119
  154. package/docs/interfaces/LDJudgeConfiguration.html +0 -109
  155. package/docs/interfaces/LDLogger.html +0 -189
  156. package/docs/interfaces/LDMessage.html +0 -119
  157. package/docs/interfaces/LDModelConfig.html +0 -139
  158. package/docs/interfaces/LDProviderConfig.html +0 -105
  159. package/docs/interfaces/LDTokenUsage.html +0 -129
  160. package/docs/interfaces/StructuredResponse.html +0 -129
  161. package/docs/types/LDAIConfigDefaultKind.html +0 -81
  162. package/docs/types/LDAIConfigKind.html +0 -81
  163. package/docs/types/LDAIConfigMode.html +0 -81
  164. package/docs/types/SupportedAIProvider.html +0 -81
  165. package/docs/variables/SUPPORTED_AI_PROVIDERS.html +0 -81
  166. package/jest.config.js +0 -7
  167. package/src/LDAIClientImpl.ts +0 -327
  168. package/src/LDAIConfigTrackerImpl.ts +0 -278
  169. package/src/LDClientMin.ts +0 -18
  170. package/src/api/LDAIClient.ts +0 -325
  171. package/src/api/chat/TrackedChat.ts +0 -159
  172. package/src/api/chat/index.ts +0 -2
  173. package/src/api/chat/types.ts +0 -24
  174. package/src/api/config/LDAIConfigTracker.ts +0 -231
  175. package/src/api/config/LDAIConfigUtils.ts +0 -201
  176. package/src/api/config/index.ts +0 -3
  177. package/src/api/config/types.ts +0 -256
  178. package/src/api/index.ts +0 -6
  179. package/src/api/judge/EvaluationSchemaBuilder.ts +0 -54
  180. package/src/api/judge/Judge.ts +0 -216
  181. package/src/api/judge/index.ts +0 -2
  182. package/src/api/judge/types.ts +0 -39
  183. package/src/api/metrics/BedrockTokenUsage.ts +0 -13
  184. package/src/api/metrics/LDAIMetrics.ts +0 -18
  185. package/src/api/metrics/LDFeedbackKind.ts +0 -13
  186. package/src/api/metrics/LDTokenUsage.ts +0 -19
  187. package/src/api/metrics/OpenAiUsage.ts +0 -13
  188. package/src/api/metrics/VercelAISDKTokenUsage.ts +0 -15
  189. package/src/api/metrics/index.ts +0 -6
  190. package/src/api/providers/AIProvider.ts +0 -94
  191. package/src/api/providers/AIProviderFactory.ts +0 -152
  192. package/src/api/providers/index.ts +0 -2
  193. package/src/index.ts +0 -24
  194. package/tsconfig.eslint.json +0 -5
  195. package/tsconfig.json +0 -21
  196. package/tsconfig.ref.json +0 -7
  197. package/typedoc.json +0 -5
@@ -1,73 +0,0 @@
1
- import { LDContext } from '@launchdarkly/js-server-sdk-common';
2
- import { LDAIConfigTracker } from './api/config';
3
- import { LDAIMetricSummary } from './api/config/LDAIConfigTracker';
4
- import { EvalScore } from './api/judge/types';
5
- import { LDAIMetrics, LDFeedbackKind, LDTokenUsage } from './api/metrics';
6
- import { LDClientMin } from './LDClientMin';
7
- export declare class LDAIConfigTrackerImpl implements LDAIConfigTracker {
8
- private _ldClient;
9
- private _configKey;
10
- private _variationKey;
11
- private _version;
12
- private _modelName;
13
- private _providerName;
14
- private _context;
15
- private _trackedMetrics;
16
- constructor(_ldClient: LDClientMin, _configKey: string, _variationKey: string, _version: number, _modelName: string, _providerName: string, _context: LDContext);
17
- getTrackData(): {
18
- variationKey: string;
19
- configKey: string;
20
- version: number;
21
- modelName: string;
22
- providerName: string;
23
- aiSdkName: string;
24
- aiSdkVersion: string;
25
- };
26
- trackDuration(duration: number): void;
27
- trackDurationOf<TRes>(func: () => Promise<TRes>): Promise<TRes>;
28
- trackTimeToFirstToken(timeToFirstTokenMs: number): void;
29
- trackEvalScores(scores: Record<string, EvalScore>): void;
30
- trackFeedback(feedback: {
31
- kind: LDFeedbackKind;
32
- }): void;
33
- trackSuccess(): void;
34
- trackError(): void;
35
- trackMetricsOf<TRes>(metricsExtractor: (result: TRes) => LDAIMetrics, func: () => Promise<TRes>): Promise<TRes>;
36
- trackStreamMetricsOf<TStream>(streamCreator: () => TStream, metricsExtractor: (stream: TStream) => Promise<LDAIMetrics>): TStream;
37
- private _trackStreamMetricsInBackground;
38
- trackOpenAIMetrics<TRes extends {
39
- usage?: {
40
- total_tokens?: number;
41
- prompt_tokens?: number;
42
- completion_tokens?: number;
43
- };
44
- }>(func: () => Promise<TRes>): Promise<TRes>;
45
- trackBedrockConverseMetrics<TRes extends {
46
- $metadata: {
47
- httpStatusCode?: number;
48
- };
49
- metrics?: {
50
- latencyMs?: number;
51
- };
52
- usage?: {
53
- inputTokens?: number;
54
- outputTokens?: number;
55
- totalTokens?: number;
56
- };
57
- }>(res: TRes): TRes;
58
- trackVercelAISDKGenerateTextMetrics<TRes extends {
59
- usage?: {
60
- totalTokens?: number;
61
- inputTokens?: number;
62
- promptTokens?: number;
63
- outputTokens?: number;
64
- completionTokens?: number;
65
- };
66
- }>(func: () => Promise<TRes>): Promise<TRes>;
67
- trackTokens(tokens: LDTokenUsage): void;
68
- /**
69
- * Get a summary of the tracked metrics.
70
- */
71
- getSummary(): LDAIMetricSummary;
72
- }
73
- //# sourceMappingURL=LDAIConfigTrackerImpl.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"LDAIConfigTrackerImpl.d.ts","sourceRoot":"","sources":["../../src/LDAIConfigTrackerImpl.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,oCAAoC,CAAC;AAG/D,OAAO,EAAE,iBAAiB,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;SAC3B,CAAC;KACH,EACD,IAAI,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC;IAc3C,WAAW,CAAC,MAAM,EAAE,YAAY,GAAG,IAAI;IAcvC;;OAEG;IACH,UAAU,IAAI,iBAAiB;CAGhC"}
@@ -1,203 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.LDAIConfigTrackerImpl = void 0;
4
- const package_json_1 = require("../package.json");
5
- const metrics_1 = require("./api/metrics");
6
- class LDAIConfigTrackerImpl {
7
- constructor(_ldClient, _configKey, _variationKey, _version, _modelName, _providerName, _context) {
8
- this._ldClient = _ldClient;
9
- this._configKey = _configKey;
10
- this._variationKey = _variationKey;
11
- this._version = _version;
12
- this._modelName = _modelName;
13
- this._providerName = _providerName;
14
- this._context = _context;
15
- this._trackedMetrics = {};
16
- }
17
- getTrackData() {
18
- return {
19
- variationKey: this._variationKey,
20
- configKey: this._configKey,
21
- version: this._version,
22
- modelName: this._modelName,
23
- providerName: this._providerName,
24
- aiSdkName: package_json_1.name,
25
- aiSdkVersion: package_json_1.version,
26
- };
27
- }
28
- trackDuration(duration) {
29
- this._trackedMetrics.durationMs = duration;
30
- this._ldClient.track('$ld:ai:duration:total', this._context, this.getTrackData(), duration);
31
- }
32
- async trackDurationOf(func) {
33
- const startTime = Date.now();
34
- try {
35
- // Be sure to await here so that we can track the duration of the function and also handle errors.
36
- const result = await func();
37
- return result;
38
- }
39
- finally {
40
- const endTime = Date.now();
41
- const duration = endTime - startTime; // duration in milliseconds
42
- this.trackDuration(duration);
43
- }
44
- }
45
- trackTimeToFirstToken(timeToFirstTokenMs) {
46
- this._trackedMetrics.timeToFirstTokenMs = timeToFirstTokenMs;
47
- this._ldClient.track('$ld:ai:tokens:ttf', this._context, this.getTrackData(), timeToFirstTokenMs);
48
- }
49
- trackEvalScores(scores) {
50
- // Track each evaluation score individually
51
- Object.entries(scores).forEach(([metricKey, evalScore]) => {
52
- this._ldClient.track(metricKey, this._context, this.getTrackData(), evalScore.score);
53
- });
54
- }
55
- trackFeedback(feedback) {
56
- this._trackedMetrics.feedback = feedback;
57
- if (feedback.kind === metrics_1.LDFeedbackKind.Positive) {
58
- this._ldClient.track('$ld:ai:feedback:user:positive', this._context, this.getTrackData(), 1);
59
- }
60
- else if (feedback.kind === metrics_1.LDFeedbackKind.Negative) {
61
- this._ldClient.track('$ld:ai:feedback:user:negative', this._context, this.getTrackData(), 1);
62
- }
63
- }
64
- trackSuccess() {
65
- this._trackedMetrics.success = true;
66
- this._ldClient.track('$ld:ai:generation:success', this._context, this.getTrackData(), 1);
67
- }
68
- trackError() {
69
- this._trackedMetrics.success = false;
70
- this._ldClient.track('$ld:ai:generation:error', this._context, this.getTrackData(), 1);
71
- }
72
- async trackMetricsOf(metricsExtractor, func) {
73
- let result;
74
- try {
75
- result = await this.trackDurationOf(func);
76
- }
77
- catch (err) {
78
- this.trackError();
79
- throw err;
80
- }
81
- // Extract metrics after successful AI call
82
- const metrics = metricsExtractor(result);
83
- // Track success/error based on metrics
84
- if (metrics.success) {
85
- this.trackSuccess();
86
- }
87
- else {
88
- this.trackError();
89
- }
90
- // Track token usage if available
91
- if (metrics.usage) {
92
- this.trackTokens(metrics.usage);
93
- }
94
- return result;
95
- }
96
- trackStreamMetricsOf(streamCreator, metricsExtractor) {
97
- const startTime = Date.now();
98
- try {
99
- // Create the stream synchronously
100
- const stream = streamCreator();
101
- // Start background metrics tracking (fire and forget)
102
- this._trackStreamMetricsInBackground(stream, metricsExtractor, startTime);
103
- // Return stream immediately for consumption
104
- return stream;
105
- }
106
- catch (error) {
107
- // Track error if stream creation fails
108
- this.trackDuration(Date.now() - startTime);
109
- this.trackError();
110
- throw error;
111
- }
112
- }
113
- async _trackStreamMetricsInBackground(stream, metricsExtractor, startTime) {
114
- try {
115
- // Wait for metrics to be available
116
- const metrics = await metricsExtractor(stream);
117
- // Track success/error based on metrics
118
- if (metrics.success) {
119
- this.trackSuccess();
120
- }
121
- else {
122
- this.trackError();
123
- }
124
- // Track token usage if available
125
- if (metrics.usage) {
126
- this.trackTokens(metrics.usage);
127
- }
128
- }
129
- catch (error) {
130
- // If metrics extraction fails, track error
131
- this.trackError();
132
- }
133
- finally {
134
- // Track duration regardless of success/error
135
- this.trackDuration(Date.now() - startTime);
136
- }
137
- }
138
- async trackOpenAIMetrics(func) {
139
- try {
140
- const result = await this.trackDurationOf(func);
141
- this.trackSuccess();
142
- if (result.usage) {
143
- this.trackTokens((0, metrics_1.createOpenAiUsage)(result.usage));
144
- }
145
- return result;
146
- }
147
- catch (err) {
148
- this.trackError();
149
- throw err;
150
- }
151
- }
152
- trackBedrockConverseMetrics(res) {
153
- var _a, _b;
154
- if (((_a = res.$metadata) === null || _a === void 0 ? void 0 : _a.httpStatusCode) === 200) {
155
- this.trackSuccess();
156
- }
157
- else if (((_b = res.$metadata) === null || _b === void 0 ? void 0 : _b.httpStatusCode) && res.$metadata.httpStatusCode >= 400) {
158
- this.trackError();
159
- }
160
- if (res.metrics && res.metrics.latencyMs) {
161
- this.trackDuration(res.metrics.latencyMs);
162
- }
163
- if (res.usage) {
164
- this.trackTokens((0, metrics_1.createBedrockTokenUsage)(res.usage));
165
- }
166
- return res;
167
- }
168
- async trackVercelAISDKGenerateTextMetrics(func) {
169
- try {
170
- const result = await this.trackDurationOf(func);
171
- this.trackSuccess();
172
- if (result.usage) {
173
- this.trackTokens((0, metrics_1.createVercelAISDKTokenUsage)(result.usage));
174
- }
175
- return result;
176
- }
177
- catch (err) {
178
- this.trackError();
179
- throw err;
180
- }
181
- }
182
- trackTokens(tokens) {
183
- this._trackedMetrics.tokens = tokens;
184
- const trackData = this.getTrackData();
185
- if (tokens.total > 0) {
186
- this._ldClient.track('$ld:ai:tokens:total', this._context, trackData, tokens.total);
187
- }
188
- if (tokens.input > 0) {
189
- this._ldClient.track('$ld:ai:tokens:input', this._context, trackData, tokens.input);
190
- }
191
- if (tokens.output > 0) {
192
- this._ldClient.track('$ld:ai:tokens:output', this._context, trackData, tokens.output);
193
- }
194
- }
195
- /**
196
- * Get a summary of the tracked metrics.
197
- */
198
- getSummary() {
199
- return Object.assign({}, this._trackedMetrics);
200
- }
201
- }
202
- exports.LDAIConfigTrackerImpl = LDAIConfigTrackerImpl;
203
- //# sourceMappingURL=LDAIConfigTrackerImpl.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"LDAIConfigTrackerImpl.js","sourceRoot":"","sources":["../../src/LDAIConfigTrackerImpl.ts"],"names":[],"mappings":";;;AAEA,kDAA6E;AAI7E,2CAOuB;AAGvB,MAAa,qBAAqB;IAGhC,YACU,SAAsB,EACtB,UAAkB,EAClB,aAAqB,EACrB,QAAgB,EAChB,UAAkB,EAClB,aAAqB,EACrB,QAAmB;QANnB,cAAS,GAAT,SAAS,CAAa;QACtB,eAAU,GAAV,UAAU,CAAQ;QAClB,kBAAa,GAAb,aAAa,CAAQ;QACrB,aAAQ,GAAR,QAAQ,CAAQ;QAChB,eAAU,GAAV,UAAU,CAAQ;QAClB,kBAAa,GAAb,aAAa,CAAQ;QACrB,aAAQ,GAAR,QAAQ,CAAW;QATrB,oBAAe,GAAsB,EAAE,CAAC;IAU7C,CAAC;IAEJ,YAAY;QASV,OAAO;YACL,YAAY,EAAE,IAAI,CAAC,aAAa;YAChC,SAAS,EAAE,IAAI,CAAC,UAAU;YAC1B,OAAO,EAAE,IAAI,CAAC,QAAQ;YACtB,SAAS,EAAE,IAAI,CAAC,UAAU;YAC1B,YAAY,EAAE,IAAI,CAAC,aAAa;YAChC,SAAS,EAAT,mBAAS;YACT,YAAY,EAAZ,sBAAY;SACb,CAAC;IACJ,CAAC;IAED,aAAa,CAAC,QAAgB;QAC5B,IAAI,CAAC,eAAe,CAAC,UAAU,GAAG,QAAQ,CAAC;QAC3C,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,uBAAuB,EAAE,IAAI,CAAC,QAAQ,EAAE,IAAI,CAAC,YAAY,EAAE,EAAE,QAA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OAAO,MAAM,CAAC;SACf;QAAC,OAAO,GAAG,EAAE;YACZ,IAAI,CAAC,UAAU,EAAE,CAAC;YAClB,MAAM,GAAG,CAAC;SACX;IACH,CAAC;IAED,WAAW,CAAC,MAAoB;QAC9B,IAAI,CAAC,eAAe,CAAC,MAAM,GAAG,MAAM,CAAC;QACrC,MAAM,SAAS,GAAG,IAAI,CAAC,YAAY,EAAE,CAAC;QACtC,IAAI,MAAM,CAAC,KAAK,GAAG,CAAC,EAAE;YACpB,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,qBAAqB,EAAE,IAAI,CAAC,QAAQ,EAAE,SAAS,EAAE,MAAM,CAAC,KAAK,CAAC,CAAC;SACrF;QACD,IAAI,MAAM,CAAC,KAAK,GAAG,CAAC,EAAE;YACpB,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,qBAAqB,EAAE,IAAI,CAAC,QAAQ,EAAE,SAAS,EAAE,MAAM,CAAC,KAAK,CAAC,CAAC;SACrF;QACD,IAAI,MAAM,CAAC,MAAM,GAAG,CAAC,EAAE;YACrB,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,sBAAsB,EAAE,IAAI,CAAC,QAAQ,EAAE,SAAS,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC;SACvF;IACH,CAAC;IAED;;OAEG;IACH,UAAU;QACR,yBAAY,IAAI,CAAC,eAAe,EAAG;IACrC,CAAC;CACF;AArQD,sDAqQC"}
@@ -1,11 +0,0 @@
1
- import { LDContext, LDFlagValue, LDLogger } from '@launchdarkly/js-server-sdk-common';
2
- /**
3
- * Interface which represents the required interface components for a sever SDK
4
- * to work with the AI SDK.
5
- */
6
- export interface LDClientMin {
7
- variation(key: string, context: LDContext, defaultValue: LDFlagValue, callback?: (err: any, res: LDFlagValue) => void): Promise<LDFlagValue>;
8
- track(key: string, context: LDContext, data?: any, metricValue?: number): void;
9
- readonly logger?: LDLogger;
10
- }
11
- //# sourceMappingURL=LDClientMin.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"LDClientMin.d.ts","sourceRoot":"","sources":["../../src/LDClientMin.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,oCAAoC,CAAC;AAEtF;;;GAGG;AACH,MAAM,WAAW,WAAW;IAC1B,SAAS,CACP,GAAG,EAAE,MAAM,EACX,OAAO,EAAE,SAAS,EAClB,YAAY,EAAE,WAAW,EACzB,QAAQ,CAAC,EAAE,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,WAAW,KAAK,IAAI,GAC9C,OAAO,CAAC,WAAW,CAAC,CAAC;IAExB,KAAK,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,EAAE,SAAS,EAAE,IAAI,CAAC,EAAE,GAAG,EAAE,WAAW,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAE/E,QAAQ,CAAC,MAAM,CAAC,EAAE,QAAQ,CAAC;CAC5B"}
@@ -1,3 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- //# sourceMappingURL=LDClientMin.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"LDClientMin.js","sourceRoot":"","sources":["../../src/LDClientMin.ts"],"names":[],"mappings":""}
@@ -1,258 +0,0 @@
1
- import { LDContext } from '@launchdarkly/js-server-sdk-common';
2
- import { TrackedChat } from './chat';
3
- import { LDAIAgentConfig, LDAIAgentConfigDefault, LDAIAgentRequestConfig, LDAICompletionConfig, LDAICompletionConfigDefault, LDAIJudgeConfig, LDAIJudgeConfigDefault } from './config';
4
- import { Judge } from './judge/Judge';
5
- import { SupportedAIProvider } from './providers';
6
- /**
7
- * Interface for performing AI operations using LaunchDarkly.
8
- */
9
- export interface LDAIClient {
10
- /**
11
- * Retrieves and processes a completion AI Config based on the provided key, LaunchDarkly context,
12
- * and variables. This includes the model configuration and the customized messages.
13
- *
14
- * @param key The key of the AI Config.
15
- * @param context The LaunchDarkly context object that contains relevant information about the
16
- * current environment, user, or session. This context may influence how the configuration is
17
- * processed or personalized.
18
- * @param defaultValue A fallback value containing model configuration and messages. This will
19
- * be used if the configuration is not available from LaunchDarkly.
20
- * @param variables A map of key-value pairs representing dynamic variables to be injected into
21
- * the message content. The keys correspond to placeholders within the template, and the values
22
- * are the corresponding replacements.
23
- *
24
- * @returns The AI `config`, customized `messages`, and a `tracker`. If the configuration cannot be accessed from
25
- * LaunchDarkly, then the return value will include information from the `defaultValue`. The returned `tracker` can
26
- * be used to track AI operation metrics (latency, token usage, etc.).
27
- *
28
- * @example
29
- * ```
30
- * const key = "welcome_prompt";
31
- * const context = {...};
32
- * const variables = {username: 'john'};
33
- * const defaultValue = {
34
- * enabled: false,
35
- * model: { name: 'gpt-4' },
36
- * provider: { name: 'openai' },
37
- * };
38
- *
39
- * const result = completionConfig(key, context, defaultValue, variables);
40
- * // Output:
41
- * {
42
- * enabled: true,
43
- * config: {
44
- * modelId: "gpt-4o",
45
- * temperature: 0.2,
46
- * maxTokens: 4096,
47
- * userDefinedKey: "myValue",
48
- * },
49
- * messages: [
50
- * {
51
- * role: "system",
52
- * content: "You are an amazing GPT."
53
- * },
54
- * {
55
- * role: "user",
56
- * content: "Explain how you're an amazing GPT."
57
- * }
58
- * ],
59
- * tracker: ...
60
- * }
61
- * ```
62
- */
63
- completionConfig(key: string, context: LDContext, defaultValue: LDAICompletionConfigDefault, variables?: Record<string, unknown>): Promise<LDAICompletionConfig>;
64
- /**
65
- * @deprecated Use `completionConfig` instead. This method will be removed in a future version.
66
- */
67
- config(key: string, context: LDContext, defaultValue: LDAICompletionConfigDefault, variables?: Record<string, unknown>): Promise<LDAICompletionConfig>;
68
- /**
69
- * Retrieves and processes a single AI Config agent based on the provided key, LaunchDarkly context,
70
- * and variables. This includes the model configuration and the customized instructions.
71
- *
72
- * @param key The key of the AI Config agent.
73
- * @param context The LaunchDarkly context object that contains relevant information about the
74
- * current environment, user, or session. This context may influence how the configuration is
75
- * processed or personalized.
76
- * @param defaultValue A fallback value containing model configuration and instructions.
77
- * @param variables A map of key-value pairs representing dynamic variables to be injected into
78
- * the instructions. The keys correspond to placeholders within the template, and the values
79
- * are the corresponding replacements.
80
- *
81
- * @returns An AI agent with customized `instructions` and a `tracker`. If the configuration
82
- * cannot be accessed from LaunchDarkly, then the return value will include information from the
83
- * `defaultValue`. The returned `tracker` can be used to track AI operation metrics (latency, token usage, etc.).
84
- *
85
- * @example
86
- * ```
87
- * const key = "research_agent";
88
- * const context = {...};
89
- * const variables = { topic: 'climate change' };
90
- * const agentConfig = await client.agentConfig(key, context, {
91
- * enabled: true,
92
- * model: { name: 'gpt-4' },
93
- * provider: { name: 'openai' },
94
- * instructions: 'You are a research assistant.',
95
- * }, variables);
96
- *
97
- * const researchResult = agentConfig.instructions; // Interpolated instructions
98
- * agentConfig.tracker.trackSuccess();
99
- * ```
100
- */
101
- agentConfig(key: string, context: LDContext, defaultValue: LDAIAgentConfigDefault, variables?: Record<string, unknown>): Promise<LDAIAgentConfig>;
102
- /**
103
- * @deprecated Use `agentConfig` instead. This method will be removed in a future version.
104
- */
105
- agent(key: string, context: LDContext, defaultValue: LDAIAgentConfigDefault, variables?: Record<string, unknown>): Promise<LDAIAgentConfig>;
106
- /**
107
- * Retrieves and processes a Judge AI Config based on the provided key, LaunchDarkly context,
108
- * and variables. This includes the model configuration and the customized messages for evaluation.
109
- *
110
- * @param key The key of the Judge AI Config.
111
- * @param context The LaunchDarkly context object that contains relevant information about the
112
- * current environment, user, or session. This context may influence how the configuration is
113
- * processed or personalized.
114
- * @param defaultValue A fallback value containing model configuration and messages. This will
115
- * be used if the configuration is not available from LaunchDarkly.
116
- * @param variables Optional variables for template interpolation in messages and instructions.
117
- * @returns A promise that resolves to a tracked judge configuration.
118
- *
119
- * @example
120
- * ```typescript
121
- * const judgeConf = await client.judgeConfig(key, context, {
122
- * enabled: true,
123
- * model: { name: 'gpt-4' },
124
- * provider: { name: 'openai' },
125
- * evaluationMetricKeys: ['$ld:ai:judge:relevance'],
126
- * messages: [{ role: 'system', content: 'You are a relevance judge.' }]
127
- * }, variables);
128
- *
129
- * const config = judgeConf.config; // Interpolated configuration
130
- * judgeConf.tracker.trackSuccess();
131
- * ```
132
- */
133
- judgeConfig(key: string, context: LDContext, defaultValue: LDAIJudgeConfigDefault, variables?: Record<string, unknown>): Promise<LDAIJudgeConfig>;
134
- /**
135
- * Retrieves and processes multiple AI Config agents based on the provided agent configurations
136
- * and LaunchDarkly context. This includes the model configuration and the customized instructions.
137
- *
138
- * @param agentConfigs An array of agent configurations, each containing the agent key, default configuration,
139
- * and variables for instructions interpolation.
140
- * @param context The LaunchDarkly context object that contains relevant information about the
141
- * current environment, user, or session. This context may influence how the configuration is
142
- * processed or personalized.
143
- *
144
- * @returns A map of agent keys to their respective AI agents with customized `instructions` and `tracker`.
145
- * If a configuration cannot be accessed from LaunchDarkly, then the return value will include information
146
- * from the respective `defaultValue`. The returned `tracker` can be used to track AI operation metrics
147
- * (latency, token usage, etc.).
148
- *
149
- * @example
150
- * ```
151
- * const agentConfigsList = [
152
- * {
153
- * key: 'research_agent',
154
- * defaultValue: {
155
- * enabled: true,
156
- * model: { name: 'gpt-4' },
157
- * provider: { name: 'openai' },
158
- * instructions: 'You are a research assistant.'
159
- * },
160
- * variables: { topic: 'climate change' }
161
- * },
162
- * {
163
- * key: 'writing_agent',
164
- * defaultValue: {
165
- * enabled: true,
166
- * model: { name: 'gpt-4' },
167
- * provider: { name: 'openai' },
168
- * instructions: 'You are a writing assistant.'
169
- * },
170
- * variables: { style: 'academic' }
171
- * }
172
- * ] as const;
173
- * const context = {...};
174
- *
175
- * const configs = await client.agentConfigs(agentConfigsList, context);
176
- * const researchResult = configs["research_agent"].instructions; // Interpolated instructions
177
- * configs["research_agent"].tracker.trackSuccess();
178
- * ```
179
- */
180
- agentConfigs<const T extends readonly LDAIAgentRequestConfig[]>(agentConfigs: T, context: LDContext): Promise<Record<T[number]['key'], LDAIAgentConfig>>;
181
- /**
182
- * @deprecated Use `agentConfigs` instead. This method will be removed in a future version.
183
- */
184
- agents<const T extends readonly LDAIAgentRequestConfig[]>(agentConfigs: T, context: LDContext): Promise<Record<T[number]['key'], LDAIAgentConfig>>;
185
- /**
186
- * Returns a TrackedChat instance for chat interactions.
187
- * This method serves as the primary entry point for creating TrackedChat instances from configuration.
188
- *
189
- * @param key The key identifying the AI chat configuration to use.
190
- * @param context The standard LDContext used when evaluating flags.
191
- * @param defaultValue A default value representing a standard AI chat config result.
192
- * @param variables Dictionary of values for instruction interpolation.
193
- * The variables will also be used for judge evaluation. For the judge only, the variables
194
- * `message_history` and `response_to_evaluate` are reserved and will be ignored.
195
- * @param defaultAiProvider Optional default AI provider to use.
196
- * @returns A promise that resolves to the TrackedChat instance, or null if the configuration is disabled.
197
- *
198
- * @example
199
- * ```
200
- * const key = "customer_support_chat";
201
- * const context = {...};
202
- * const defaultValue = {
203
- * enabled: false,
204
- * model: { name: "gpt-4" },
205
- * provider: { name: "openai" },
206
- * messages: [
207
- * { role: "system", content: "You are a helpful customer support agent." }
208
- * ]
209
- * };
210
- * const variables = { customerName: 'John' };
211
- *
212
- * const chat = await client.createChat(key, context, defaultValue, variables);
213
- * if (chat) {
214
- * const response = await chat.invoke("I need help with my order");
215
- * console.log(response.message.content);
216
- * }
217
- * ```
218
- */
219
- createChat(key: string, context: LDContext, defaultValue: LDAICompletionConfigDefault, variables?: Record<string, unknown>, defaultAiProvider?: SupportedAIProvider): Promise<TrackedChat | undefined>;
220
- /**
221
- * @deprecated Use `createChat` instead. This method will be removed in a future version.
222
- */
223
- initChat(key: string, context: LDContext, defaultValue: LDAICompletionConfigDefault, variables?: Record<string, unknown>, defaultAiProvider?: SupportedAIProvider): Promise<TrackedChat | undefined>;
224
- /**
225
- * Creates and returns a new Judge instance for AI evaluation.
226
- *
227
- * @param key The key identifying the AI judge configuration to use
228
- * @param context Standard LDContext used when evaluating flags
229
- * @param defaultValue A default value representing a standard AI config result
230
- * @param variables Dictionary of values for instruction interpolation.
231
- * The variables `message_history` and `response_to_evaluate` are reserved for the judge and will be ignored.
232
- * @param defaultAiProvider Optional default AI provider to use.
233
- * @returns Promise that resolves to a Judge instance or undefined if disabled/unsupported
234
- *
235
- * @example
236
- * ```
237
- * const judge = await client.createJudge(
238
- * "relevance-judge",
239
- * context,
240
- * {
241
- * enabled: true,
242
- * model: { name: "gpt-4" },
243
- * provider: { name: "openai" },
244
- * evaluationMetricKeys: ['$ld:ai:judge:relevance'],
245
- * messages: [{ role: 'system', content: 'You are a relevance judge.' }]
246
- * },
247
- * { metric: "relevance" }
248
- * );
249
- *
250
- * if (judge) {
251
- * const result = await judge.evaluate("User question", "AI response");
252
- * console.log('Relevance score:', result.evals.relevance?.score);
253
- * }
254
- * ```
255
- */
256
- createJudge(key: string, context: LDContext, defaultValue: LDAIJudgeConfigDefault, variables?: Record<string, unknown>, defaultAiProvider?: SupportedAIProvider): Promise<Judge | undefined>;
257
- }
258
- //# sourceMappingURL=LDAIClient.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"LDAIClient.d.ts","sourceRoot":"","sources":["../../../src/api/LDAIClient.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,oCAAoC,CAAC;AAE/D,OAAO,EAAE,WAAW,EAAE,MAAM,QAAQ,CAAC;AACrC,OAAO,EACL,eAAe,EACf,sBAAsB,EACtB,sBAAsB,EACtB,oBAAoB,EACpB,2BAA2B,EAC3B,eAAe,EACf,sBAAsB,EACvB,MAAM,UAAU,CAAC;AAClB,OAAO,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AACtC,OAAO,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAC;AAElD;;GAEG;AACH,MAAM,WAAW,UAAU;IACzB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAoDG;IACH,gBAAgB,CACd,GAAG,EAAE,MAAM,EACX,OAAO,EAAE,SAAS,EAClB,YAAY,EAAE,2BAA2B,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OAEG;IACH,MAAM,CAAC,KAAK,CAAC,CAAC,SAAS,SAAS,sBAAsB,EAAE,EACtD,YAAY,EAAE,CAAC,EACf,OAAO,EAAE,SAAS,GACjB,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,KAAK,CAAC,EAAE,eAAe,CAAC,CAAC,CAAC;IAEtD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAiCG;IACH,UAAU,CACR,GAAG,EAAE,MAAM,EACX,OAAO,EAAE,SAAS,EAClB,YAAY,EAAE,2BAA2B,EACzC,SAAS,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EACnC,iBAAiB,CAAC,EAAE,mBAAmB,GACtC,OAAO,CAAC,WAAW,GAAG,SAAS,CAAC,CAAC;IAEpC;;OAEG;IACH,QAAQ,CACN,GAAG,EAAE,MAAM,EACX,OAAO,EAAE,SAAS,EAClB,YAAY,EAAE,2BAA2B,EACzC,SAAS,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EACnC,iBAAiB,CAAC,EAAE,mBAAmB,GACtC,OAAO,CAAC,WAAW,GAAG,SAAS,CAAC,CAAC;IAEpC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA+BG;IACH,WAAW,CACT,GAAG,EAAE,MAAM,EACX,OAAO,EAAE,SAAS,EAClB,YAAY,EAAE,sBAAsB,EACpC,SAAS,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EACnC,iBAAiB,CAAC,EAAE,mBAAmB,GACtC,OAAO,CAAC,KAAK,GAAG,SAAS,CAAC,CAAC;CAC/B"}
@@ -1,3 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- //# sourceMappingURL=LDAIClient.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"LDAIClient.js","sourceRoot":"","sources":["../../../src/api/LDAIClient.ts"],"names":[],"mappings":""}
@@ -1,72 +0,0 @@
1
- import { LDLogger } from '@launchdarkly/js-server-sdk-common';
2
- import { LDAIConfigTracker } from '../config/LDAIConfigTracker';
3
- import { LDAICompletionConfig, LDMessage } from '../config/types';
4
- import { Judge } from '../judge/Judge';
5
- import { AIProvider } from '../providers/AIProvider';
6
- import { ChatResponse } from './types';
7
- /**
8
- * Concrete implementation of TrackedChat that provides chat functionality
9
- * by delegating to an AIProvider implementation.
10
- * This class handles conversation management and tracking, while delegating
11
- * the actual model invocation to the provider.
12
- */
13
- export declare class TrackedChat {
14
- protected readonly aiConfig: LDAICompletionConfig;
15
- protected readonly tracker: LDAIConfigTracker;
16
- protected readonly provider: AIProvider;
17
- protected readonly judges: Record<string, Judge>;
18
- private readonly _logger?;
19
- protected messages: LDMessage[];
20
- constructor(aiConfig: LDAICompletionConfig, tracker: LDAIConfigTracker, provider: AIProvider, judges?: Record<string, Judge>, _logger?: LDLogger | undefined);
21
- /**
22
- * Invoke the chat model with a prompt string.
23
- * This method handles conversation management and tracking, delegating to the provider's invokeModel method.
24
- */
25
- invoke(prompt: string): Promise<ChatResponse>;
26
- /**
27
- * Evaluates the response with all configured judges.
28
- * Returns a promise that resolves to an array of evaluation results.
29
- *
30
- * @param messages Array of messages representing the conversation history
31
- * @param response The AI response to be evaluated
32
- * @returns Promise resolving to array of judge evaluation results
33
- */
34
- private _evaluateWithJudges;
35
- /**
36
- * Get the underlying AI configuration used to initialize this TrackedChat.
37
- */
38
- getConfig(): LDAICompletionConfig;
39
- /**
40
- * Get the underlying AI configuration tracker used to initialize this TrackedChat.
41
- */
42
- getTracker(): LDAIConfigTracker;
43
- /**
44
- * Get the underlying AI provider instance.
45
- * This provides direct access to the provider for advanced use cases.
46
- */
47
- getProvider(): AIProvider;
48
- /**
49
- * Get the judges associated with this TrackedChat.
50
- * Returns a record of judge instances keyed by their configuration keys.
51
- */
52
- getJudges(): Record<string, Judge>;
53
- /**
54
- * Append messages to the conversation history.
55
- * Adds messages to the conversation history without invoking the model,
56
- * which is useful for managing multi-turn conversations or injecting context.
57
- *
58
- * @param messages Array of messages to append to the conversation history
59
- */
60
- appendMessages(messages: LDMessage[]): void;
61
- /**
62
- * Get all messages in the conversation history.
63
- *
64
- * @param includeConfigMessages Whether to include the config messages from the AIConfig.
65
- * Defaults to false.
66
- * @returns Array of messages. When includeConfigMessages is true, returns both config
67
- * messages and conversation history with config messages prepended. When false,
68
- * returns only the conversation history messages.
69
- */
70
- getMessages(includeConfigMessages?: boolean): LDMessage[];
71
- }
72
- //# sourceMappingURL=TrackedChat.d.ts.map