@launchdarkly/server-sdk-ai 0.20.0 → 1.0.1

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.
package/dist/index.js CHANGED
@@ -139,9 +139,6 @@ var LDAIConfigUtils = class {
139
139
  if ("evaluationMetricKey" in config && config.evaluationMetricKey !== void 0) {
140
140
  flagValue.evaluationMetricKey = config.evaluationMetricKey;
141
141
  }
142
- if ("evaluationMetricKeys" in config && config.evaluationMetricKeys !== void 0) {
143
- flagValue.evaluationMetricKeys = config.evaluationMetricKeys;
144
- }
145
142
  if ("judgeConfiguration" in config && config.judgeConfiguration !== void 0) {
146
143
  flagValue.judgeConfiguration = config.judgeConfiguration;
147
144
  }
@@ -155,7 +152,7 @@ var LDAIConfigUtils = class {
155
152
  *
156
153
  * @param key The configuration key
157
154
  * @param flagValue The flag value from LaunchDarkly
158
- * @param trackerFactory A factory function that creates a new tracker for each execution
155
+ * @param trackerFactory A factory function that creates a new tracker for each AI run
159
156
  * @param evaluator The evaluator to attach to completion and agent configs
160
157
  * @returns The appropriate AI configuration type
161
158
  */
@@ -176,7 +173,7 @@ var LDAIConfigUtils = class {
176
173
  *
177
174
  * @param key The configuration key
178
175
  * @param mode The mode for the disabled config
179
- * @param createTracker A factory function that creates a new tracker for each execution
176
+ * @param createTracker A factory function that creates a new tracker for each AI run
180
177
  * @param evaluator The evaluator to attach to completion and agent configs
181
178
  * @returns A disabled config of the appropriate type
182
179
  */
@@ -242,7 +239,7 @@ var LDAIConfigUtils = class {
242
239
  *
243
240
  * @param key The configuration key
244
241
  * @param flagValue The flag value from LaunchDarkly
245
- * @param trackerFactory A factory function that creates a new tracker for each execution
242
+ * @param trackerFactory A factory function that creates a new tracker for each AI run
246
243
  * @param evaluator The evaluator for this completion config
247
244
  * @returns A completion configuration
248
245
  */
@@ -261,7 +258,7 @@ var LDAIConfigUtils = class {
261
258
  *
262
259
  * @param key The configuration key
263
260
  * @param flagValue The flag value from LaunchDarkly
264
- * @param trackerFactory A factory function that creates a new tracker for each execution
261
+ * @param trackerFactory A factory function that creates a new tracker for each AI run
265
262
  * @param evaluator The evaluator for this agent config
266
263
  * @returns An agent configuration
267
264
  */
@@ -280,7 +277,7 @@ var LDAIConfigUtils = class {
280
277
  *
281
278
  * @param key The configuration key
282
279
  * @param flagValue The flag value from LaunchDarkly
283
- * @param trackerFactory A factory function that creates a new tracker for each execution
280
+ * @param trackerFactory A factory function that creates a new tracker for each AI run
284
281
  * @returns A judge configuration
285
282
  */
286
283
  static toJudgeConfig(key, flagValue, trackerFactory) {
@@ -413,10 +410,10 @@ var AgentGraphDefinition = class _AgentGraphDefinition {
413
410
  return this._agentGraph;
414
411
  }
415
412
  /**
416
- * Returns a new {@link LDGraphTracker} for this graph invocation.
413
+ * Returns a new {@link LDGraphTracker} for a fresh graph run.
417
414
  *
418
- * Call this once per invocation. Each call produces a tracker with a fresh `runId`
419
- * that groups all events for that invocation.
415
+ * Call this once per graph run. Each call produces a tracker with a fresh `runId`
416
+ * that groups all events for that run.
420
417
  */
421
418
  createTracker() {
422
419
  return this._createTracker();
@@ -534,81 +531,6 @@ var AgentGraphDefinition = class _AgentGraphDefinition {
534
531
  }
535
532
  };
536
533
 
537
- // src/api/graph/ManagedAgentGraph.ts
538
- var ManagedAgentGraph = class {
539
- constructor(_graphDefinition, _logger) {
540
- this._graphDefinition = _graphDefinition;
541
- this._logger = _logger;
542
- }
543
- /**
544
- * Runs the agent graph using the provided runner function and returns a ManagedGraphResult.
545
- *
546
- * The runner function receives the graph tracker and AgentGraphDefinition,
547
- * executes the graph, and returns an AgentGraphRunnerResult.
548
- *
549
- * run() returns before ManagedGraphResult.evaluations resolves.
550
- *
551
- * @param runner Async function that executes the graph and returns AgentGraphRunnerResult.
552
- * @returns ManagedGraphResult with LDAIGraphMetricSummary and evaluations promise.
553
- */
554
- async run(runner) {
555
- const graphTracker = this._graphDefinition.createTracker();
556
- const runnerResult = await runner(this._graphDefinition, graphTracker);
557
- const metrics = {
558
- success: runnerResult.metrics.success,
559
- path: runnerResult.metrics.path,
560
- durationMs: runnerResult.metrics.durationMs,
561
- tokens: runnerResult.metrics.tokens,
562
- nodeMetrics: this._trackNodeMetrics(runnerResult.metrics.nodeMetrics),
563
- resumptionToken: graphTracker.resumptionToken
564
- };
565
- const evaluations = Promise.resolve([]);
566
- return {
567
- content: runnerResult.content,
568
- metrics,
569
- raw: runnerResult.raw,
570
- evaluations
571
- };
572
- }
573
- /**
574
- * Converts per-node LDAIMetrics from the runner into LDAIMetricSummary by
575
- * creating a per-node tracker, firing tracking events, and calling getSummary().
576
- */
577
- _trackNodeMetrics(nodeMetrics) {
578
- const summaries = {};
579
- for (const [nodeKey, metrics] of Object.entries(nodeMetrics)) {
580
- const node = this._graphDefinition.getNode(nodeKey);
581
- if (!node) {
582
- this._logger?.warn(`ManagedAgentGraph: no node found for key "${nodeKey}", skipping metrics`);
583
- continue;
584
- }
585
- const tracker = node.getConfig().createTracker();
586
- if (metrics.tokens) {
587
- tracker.trackTokens(metrics.tokens);
588
- }
589
- if (metrics.durationMs !== void 0) {
590
- tracker.trackDuration(metrics.durationMs);
591
- }
592
- if (metrics.toolCalls?.length) {
593
- tracker.trackToolCalls(metrics.toolCalls);
594
- }
595
- if (metrics.success) {
596
- tracker.trackSuccess();
597
- } else {
598
- tracker.trackError();
599
- }
600
- summaries[nodeKey] = tracker.getSummary();
601
- }
602
- return summaries;
603
- }
604
- /**
605
- * Returns the underlying AgentGraphDefinition.
606
- */
607
- getGraphDefinition() {
608
- return this._graphDefinition;
609
- }
610
- };
611
-
612
534
  // src/api/judge/Evaluator.ts
613
535
  var Evaluator = class _Evaluator {
614
536
  constructor(_judges) {
@@ -663,8 +585,7 @@ var Judge = class {
663
585
  return this._sampleRate;
664
586
  }
665
587
  /**
666
- * Gets the evaluation metric key, prioritizing evaluationMetricKey over evaluationMetricKeys.
667
- * Falls back to the first valid (non-empty, non-whitespace) value in evaluationMetricKeys if evaluationMetricKey is not provided.
588
+ * Gets the evaluation metric key from the judge AI config.
668
589
  * Treats empty strings and whitespace-only strings as invalid.
669
590
  * @returns The evaluation metric key, or undefined if not available
670
591
  */
@@ -672,12 +593,6 @@ var Judge = class {
672
593
  if (this._aiConfig.evaluationMetricKey && this._aiConfig.evaluationMetricKey.trim().length > 0) {
673
594
  return this._aiConfig.evaluationMetricKey.trim();
674
595
  }
675
- if (this._aiConfig.evaluationMetricKeys && this._aiConfig.evaluationMetricKeys.length > 0) {
676
- const validKey = this._aiConfig.evaluationMetricKeys.find(
677
- (key) => key && key.trim().length > 0
678
- );
679
- return validKey ? validKey.trim() : void 0;
680
- }
681
596
  return void 0;
682
597
  }
683
598
  /**
@@ -742,17 +657,21 @@ var Judge = class {
742
657
  }
743
658
  }
744
659
  /**
745
- * Evaluates an AI response from chat messages and response.
660
+ * Evaluates an AI response from chat messages and a runner result.
661
+ *
662
+ * Each message is rendered as `<role>: <content>` so the judge model can
663
+ * distinguish speakers in the message history. Messages are joined with a
664
+ * single newline.
746
665
  *
747
666
  * @param messages Array of messages representing the conversation history
748
- * @param response The AI response to be evaluated
667
+ * @param response The runner result containing the AI-generated content to evaluate
749
668
  * @param samplingRatio Sampling ratio (0-1). When omitted, the Judge's
750
669
  * constructor-default rate is used.
751
670
  * @returns Promise that resolves to evaluation results
752
671
  */
753
672
  async evaluateMessages(messages, response, samplingRatio) {
754
- const input = messages.length === 0 ? "" : messages.map((msg) => msg.content).join("\r\n");
755
- const output = response.message.content;
673
+ const input = messages.length === 0 ? "" : messages.map((msg) => `${msg.role}: ${msg.content}`).join("\n");
674
+ const output = response.content;
756
675
  return this.evaluate(input, output, samplingRatio);
757
676
  }
758
677
  /**
@@ -813,10 +732,14 @@ var AIProvider = class {
813
732
  * Default implementation returns `undefined`.
814
733
  *
815
734
  * @param config The completion or judge AI configuration.
735
+ * @param multiTurn Whether the runner should accumulate conversation history
736
+ * across successive `run()` calls. Defaults to `true` (chat semantics).
737
+ * Pass `false` for stateless runners such as judges where each call must
738
+ * start from the initial config messages.
816
739
  * @returns Promise resolving to a {@link Runner}, or `undefined` if this
817
740
  * provider does not support model creation.
818
741
  */
819
- async createModel(_config) {
742
+ async createModel(_config, _multiTurn = true) {
820
743
  return void 0;
821
744
  }
822
745
  /**
@@ -952,15 +875,19 @@ var RunnerFactory = class _RunnerFactory {
952
875
  * ('openai', 'langchain', 'vercel', …). When set, only that provider is
953
876
  * tried. When omitted, providers are tried in priority order based on the
954
877
  * provider name in the config.
878
+ * @param multiTurn Whether the runner should accumulate conversation history
879
+ * across successive `run()` calls. Defaults to `true` (chat semantics).
880
+ * Judges pass `false` so each evaluation starts from the initial config
881
+ * messages.
955
882
  * @returns A configured {@link Runner} ready to invoke the model, or
956
883
  * `undefined` if no suitable provider could be loaded.
957
884
  */
958
- static async createModel(config, logger, defaultAiProvider) {
885
+ static async createModel(config, logger, defaultAiProvider, multiTurn = true) {
959
886
  const providerName = config.provider?.name?.toLowerCase();
960
887
  const providers = _RunnerFactory._getProvidersToTry(defaultAiProvider, providerName);
961
888
  const runner = await _RunnerFactory._withFallback(
962
889
  providers,
963
- (factory) => factory.createModel(config),
890
+ (factory) => factory.createModel(config, multiTurn),
964
891
  logger
965
892
  );
966
893
  if (!runner) {
@@ -1023,24 +950,6 @@ var RunnerFactory = class _RunnerFactory {
1023
950
  }
1024
951
  };
1025
952
 
1026
- // src/api/metrics/BedrockTokenUsage.ts
1027
- function createBedrockTokenUsage(data) {
1028
- return {
1029
- total: data.totalTokens || 0,
1030
- input: data.inputTokens || 0,
1031
- output: data.outputTokens || 0
1032
- };
1033
- }
1034
-
1035
- // src/api/metrics/OpenAiUsage.ts
1036
- function createOpenAiUsage(data) {
1037
- return {
1038
- total: data.total_tokens ?? 0,
1039
- input: data.prompt_tokens ?? 0,
1040
- output: data.completion_tokens ?? 0
1041
- };
1042
- }
1043
-
1044
953
  // src/api/metrics/LDFeedbackKind.ts
1045
954
  var LDFeedbackKind = /* @__PURE__ */ ((LDFeedbackKind2) => {
1046
955
  LDFeedbackKind2["Positive"] = "positive";
@@ -1048,15 +957,6 @@ var LDFeedbackKind = /* @__PURE__ */ ((LDFeedbackKind2) => {
1048
957
  return LDFeedbackKind2;
1049
958
  })(LDFeedbackKind || {});
1050
959
 
1051
- // src/api/metrics/VercelAISDKTokenUsage.ts
1052
- function createVercelAISDKTokenUsage(data) {
1053
- return {
1054
- total: data.totalTokens ?? 0,
1055
- input: data.inputTokens ?? data.promptTokens ?? 0,
1056
- output: data.outputTokens ?? data.completionTokens ?? 0
1057
- };
1058
- }
1059
-
1060
960
  // src/LDAIConfigTrackerImpl.ts
1061
961
  var LDAIConfigTrackerImpl = class _LDAIConfigTrackerImpl {
1062
962
  constructor(_ldClient, _runId, _configKey, _variationKey, _version, _modelName, _providerName, _context, _graphKey) {
@@ -1111,7 +1011,7 @@ var LDAIConfigTrackerImpl = class _LDAIConfigTrackerImpl {
1111
1011
  trackDuration(duration) {
1112
1012
  if (this._trackedMetrics.durationMs !== void 0) {
1113
1013
  this._ldClient.logger?.warn(
1114
- "Duration has already been tracked for this execution. Use createTracker() for a new execution."
1014
+ "Skipping trackDuration: duration already recorded on this tracker. Call createTracker on the AI Config for a new run."
1115
1015
  );
1116
1016
  return;
1117
1017
  }
@@ -1132,7 +1032,7 @@ var LDAIConfigTrackerImpl = class _LDAIConfigTrackerImpl {
1132
1032
  trackTimeToFirstToken(timeToFirstTokenMs) {
1133
1033
  if (this._trackedMetrics.timeToFirstTokenMs !== void 0) {
1134
1034
  this._ldClient.logger?.warn(
1135
- "Time to first token has already been tracked for this execution. Use createTracker() for a new execution."
1035
+ "Skipping trackTimeToFirstToken: time-to-first-token already recorded on this tracker. Call createTracker on the AI Config for a new run."
1136
1036
  );
1137
1037
  return;
1138
1038
  }
@@ -1168,7 +1068,7 @@ var LDAIConfigTrackerImpl = class _LDAIConfigTrackerImpl {
1168
1068
  trackFeedback(feedback) {
1169
1069
  if (this._trackedMetrics.feedback !== void 0) {
1170
1070
  this._ldClient.logger?.warn(
1171
- "Feedback has already been tracked for this execution. Use createTracker() for a new execution."
1071
+ "Skipping trackFeedback: feedback already recorded on this tracker. Call createTracker on the AI Config for a new run."
1172
1072
  );
1173
1073
  return;
1174
1074
  }
@@ -1182,7 +1082,7 @@ var LDAIConfigTrackerImpl = class _LDAIConfigTrackerImpl {
1182
1082
  trackSuccess() {
1183
1083
  if (this._trackedMetrics.success !== void 0) {
1184
1084
  this._ldClient.logger?.warn(
1185
- "Generation result has already been tracked for this execution. Use createTracker() for a new execution."
1085
+ "Skipping trackSuccess: success/error already recorded on this tracker. Call createTracker on the AI Config for a new run."
1186
1086
  );
1187
1087
  return;
1188
1088
  }
@@ -1192,7 +1092,7 @@ var LDAIConfigTrackerImpl = class _LDAIConfigTrackerImpl {
1192
1092
  trackError() {
1193
1093
  if (this._trackedMetrics.success !== void 0) {
1194
1094
  this._ldClient.logger?.warn(
1195
- "Generation result has already been tracked for this execution. Use createTracker() for a new execution."
1095
+ "Skipping trackError: success/error already recorded on this tracker. Call createTracker on the AI Config for a new run."
1196
1096
  );
1197
1097
  return;
1198
1098
  }
@@ -1253,50 +1153,10 @@ var LDAIConfigTrackerImpl = class _LDAIConfigTrackerImpl {
1253
1153
  this.trackDuration(Date.now() - startTime);
1254
1154
  }
1255
1155
  }
1256
- async trackOpenAIMetrics(func) {
1257
- try {
1258
- const result = await this.trackDurationOf(func);
1259
- this.trackSuccess();
1260
- if (result.usage) {
1261
- this.trackTokens(createOpenAiUsage(result.usage));
1262
- }
1263
- return result;
1264
- } catch (err) {
1265
- this.trackError();
1266
- throw err;
1267
- }
1268
- }
1269
- trackBedrockConverseMetrics(res) {
1270
- if (res.$metadata?.httpStatusCode === 200) {
1271
- this.trackSuccess();
1272
- } else if (res.$metadata?.httpStatusCode && res.$metadata.httpStatusCode >= 400) {
1273
- this.trackError();
1274
- }
1275
- if (res.metrics && res.metrics.latencyMs) {
1276
- this.trackDuration(res.metrics.latencyMs);
1277
- }
1278
- if (res.usage) {
1279
- this.trackTokens(createBedrockTokenUsage(res.usage));
1280
- }
1281
- return res;
1282
- }
1283
- async trackVercelAISDKGenerateTextMetrics(func) {
1284
- try {
1285
- const result = await this.trackDurationOf(func);
1286
- this.trackSuccess();
1287
- if (result.usage) {
1288
- this.trackTokens(createVercelAISDKTokenUsage(result.usage));
1289
- }
1290
- return result;
1291
- } catch (err) {
1292
- this.trackError();
1293
- throw err;
1294
- }
1295
- }
1296
1156
  trackTokens(tokens) {
1297
1157
  if (this._trackedMetrics.tokens !== void 0) {
1298
1158
  this._ldClient.logger?.warn(
1299
- "Token usage has already been tracked for this execution. Use createTracker() for a new execution."
1159
+ "Skipping trackTokens: token usage already recorded on this tracker. Call createTracker on the AI Config for a new run."
1300
1160
  );
1301
1161
  return;
1302
1162
  }
@@ -1381,7 +1241,7 @@ var LDGraphTrackerImpl = class _LDGraphTrackerImpl {
1381
1241
  trackInvocationSuccess() {
1382
1242
  if (this._summary.success !== void 0) {
1383
1243
  this._ldClient.logger?.warn(
1384
- "LDGraphTracker: invocation success/failure already recorded for this run \u2014 dropping duplicate call."
1244
+ "Skipping trackInvocationSuccess: invocation result already recorded on this graph tracker. Call createTracker on the agent graph for a new run."
1385
1245
  );
1386
1246
  return;
1387
1247
  }
@@ -1391,7 +1251,7 @@ var LDGraphTrackerImpl = class _LDGraphTrackerImpl {
1391
1251
  trackInvocationFailure() {
1392
1252
  if (this._summary.success !== void 0) {
1393
1253
  this._ldClient.logger?.warn(
1394
- "LDGraphTracker: invocation success/failure already recorded for this run \u2014 dropping duplicate call."
1254
+ "Skipping trackInvocationFailure: invocation result already recorded on this graph tracker. Call createTracker on the agent graph for a new run."
1395
1255
  );
1396
1256
  return;
1397
1257
  }
@@ -1401,7 +1261,7 @@ var LDGraphTrackerImpl = class _LDGraphTrackerImpl {
1401
1261
  trackDuration(durationMs) {
1402
1262
  if (this._summary.durationMs !== void 0) {
1403
1263
  this._ldClient.logger?.warn(
1404
- "LDGraphTracker: trackDuration already called for this run \u2014 dropping duplicate call."
1264
+ "Skipping trackDuration: duration already recorded on this graph tracker. Call createTracker on the agent graph for a new run."
1405
1265
  );
1406
1266
  return;
1407
1267
  }
@@ -1416,7 +1276,7 @@ var LDGraphTrackerImpl = class _LDGraphTrackerImpl {
1416
1276
  trackTotalTokens(tokens) {
1417
1277
  if (this._summary.tokens !== void 0) {
1418
1278
  this._ldClient.logger?.warn(
1419
- "LDGraphTracker: trackTotalTokens already called for this run \u2014 dropping duplicate call."
1279
+ "Skipping trackTotalTokens: tokens already recorded on this graph tracker. Call createTracker on the agent graph for a new run."
1420
1280
  );
1421
1281
  return;
1422
1282
  }
@@ -1431,7 +1291,7 @@ var LDGraphTrackerImpl = class _LDGraphTrackerImpl {
1431
1291
  trackPath(path) {
1432
1292
  if (this._summary.path !== void 0) {
1433
1293
  this._ldClient.logger?.warn(
1434
- "LDGraphTracker: trackPath already called for this run \u2014 dropping duplicate call."
1294
+ "Skipping trackPath: path already recorded on this graph tracker. Call createTracker on the agent graph for a new run."
1435
1295
  );
1436
1296
  return;
1437
1297
  }
@@ -1466,7 +1326,7 @@ var LDGraphTrackerImpl = class _LDGraphTrackerImpl {
1466
1326
 
1467
1327
  // src/sdkInfo.ts
1468
1328
  var aiSdkName = "@launchdarkly/server-sdk-ai";
1469
- var aiSdkVersion = "0.20.0";
1329
+ var aiSdkVersion = "1.0.1";
1470
1330
  var aiSdkLanguage = "javascript";
1471
1331
 
1472
1332
  // src/LDAIClientImpl.ts
@@ -1596,12 +1456,6 @@ var LDAIClientImpl = class {
1596
1456
  defaultAiProvider
1597
1457
  );
1598
1458
  }
1599
- /**
1600
- * @deprecated Use `completionConfig` instead. This method will be removed in a future version.
1601
- */
1602
- async config(key, context, defaultValue, variables) {
1603
- return this.completionConfig(key, context, defaultValue, variables);
1604
- }
1605
1459
  async _judgeConfig(key, context, defaultValue, variables) {
1606
1460
  if (variables?.message_history !== void 0) {
1607
1461
  this._logger?.warn(
@@ -1656,12 +1510,6 @@ var LDAIClientImpl = class {
1656
1510
  defaultAiProvider
1657
1511
  );
1658
1512
  }
1659
- /**
1660
- * @deprecated Use `agentConfig` instead. This method will be removed in a future version.
1661
- */
1662
- async agent(key, context, defaultValue, variables) {
1663
- return this.agentConfig(key, context, defaultValue, variables);
1664
- }
1665
1513
  async agentConfigs(agentConfigs, context) {
1666
1514
  this._ldClient.track(
1667
1515
  TRACK_USAGE_AGENT_CONFIGS,
@@ -1683,12 +1531,6 @@ var LDAIClientImpl = class {
1683
1531
  );
1684
1532
  return agents;
1685
1533
  }
1686
- /**
1687
- * @deprecated Use `agentConfigs` instead. This method will be removed in a future version.
1688
- */
1689
- async agents(agentConfigs, context) {
1690
- return this.agentConfigs(agentConfigs, context);
1691
- }
1692
1534
  async createJudge(key, context, defaultValue, variables, defaultAiProvider, sampleRate = 1) {
1693
1535
  this._ldClient.track(TRACK_USAGE_CREATE_JUDGE, context, key, 1);
1694
1536
  return this._createJudgeInstance(
@@ -1712,7 +1554,12 @@ var LDAIClientImpl = class {
1712
1554
  this._logger?.info(`Judge configuration is disabled: ${key}`);
1713
1555
  return void 0;
1714
1556
  }
1715
- const runner = await RunnerFactory.createModel(judgeConfig, this._logger, defaultAiProvider);
1557
+ const runner = await RunnerFactory.createModel(
1558
+ judgeConfig,
1559
+ this._logger,
1560
+ defaultAiProvider,
1561
+ false
1562
+ );
1716
1563
  if (!runner) {
1717
1564
  return void 0;
1718
1565
  }
@@ -1761,18 +1608,6 @@ var LDAIClientImpl = class {
1761
1608
  }
1762
1609
  return new ManagedAgent(config, runner, this._logger);
1763
1610
  }
1764
- /**
1765
- * @deprecated Use `createModel` instead. This method will be removed in a future version.
1766
- */
1767
- async createChat(key, context, defaultValue, variables, defaultAiProvider) {
1768
- return this.createModel(key, context, defaultValue, variables, defaultAiProvider);
1769
- }
1770
- /**
1771
- * @deprecated Use `createModel` instead. This method will be removed in a future version.
1772
- */
1773
- async initChat(key, context, defaultValue, variables, defaultAiProvider) {
1774
- return this.createModel(key, context, defaultValue, variables, defaultAiProvider);
1775
- }
1776
1611
  createTracker(token, context) {
1777
1612
  return LDAIConfigTrackerImpl.fromResumptionToken(token, this._ldClient, context);
1778
1613
  }
@@ -1850,6 +1685,81 @@ var LDAIClientImpl = class {
1850
1685
  }
1851
1686
  };
1852
1687
 
1688
+ // src/api/ManagedAgentGraph.ts
1689
+ var ManagedAgentGraph = class {
1690
+ constructor(_graphDefinition, _logger) {
1691
+ this._graphDefinition = _graphDefinition;
1692
+ this._logger = _logger;
1693
+ }
1694
+ /**
1695
+ * Runs the agent graph using the provided runner function and returns a ManagedGraphResult.
1696
+ *
1697
+ * The runner function receives the graph tracker and AgentGraphDefinition,
1698
+ * executes the graph, and returns an AgentGraphRunnerResult.
1699
+ *
1700
+ * run() returns before ManagedGraphResult.evaluations resolves.
1701
+ *
1702
+ * @param runner Async function that executes the graph and returns AgentGraphRunnerResult.
1703
+ * @returns ManagedGraphResult with LDAIGraphMetricSummary and evaluations promise.
1704
+ */
1705
+ async run(runner) {
1706
+ const graphTracker = this._graphDefinition.createTracker();
1707
+ const runnerResult = await runner(this._graphDefinition, graphTracker);
1708
+ const metrics = {
1709
+ success: runnerResult.metrics.success,
1710
+ path: runnerResult.metrics.path,
1711
+ durationMs: runnerResult.metrics.durationMs,
1712
+ tokens: runnerResult.metrics.tokens,
1713
+ nodeMetrics: this._trackNodeMetrics(runnerResult.metrics.nodeMetrics),
1714
+ resumptionToken: graphTracker.resumptionToken
1715
+ };
1716
+ const evaluations = Promise.resolve([]);
1717
+ return {
1718
+ content: runnerResult.content,
1719
+ metrics,
1720
+ raw: runnerResult.raw,
1721
+ evaluations
1722
+ };
1723
+ }
1724
+ /**
1725
+ * Converts per-node LDAIMetrics from the runner into LDAIMetricSummary by
1726
+ * creating a per-node tracker, firing tracking events, and calling getSummary().
1727
+ */
1728
+ _trackNodeMetrics(nodeMetrics) {
1729
+ const summaries = {};
1730
+ for (const [nodeKey, metrics] of Object.entries(nodeMetrics)) {
1731
+ const node = this._graphDefinition.getNode(nodeKey);
1732
+ if (!node) {
1733
+ this._logger?.warn(`ManagedAgentGraph: no node found for key "${nodeKey}", skipping metrics`);
1734
+ continue;
1735
+ }
1736
+ const tracker = node.getConfig().createTracker();
1737
+ if (metrics.tokens) {
1738
+ tracker.trackTokens(metrics.tokens);
1739
+ }
1740
+ if (metrics.durationMs !== void 0) {
1741
+ tracker.trackDuration(metrics.durationMs);
1742
+ }
1743
+ if (metrics.toolCalls?.length) {
1744
+ tracker.trackToolCalls(metrics.toolCalls);
1745
+ }
1746
+ if (metrics.success) {
1747
+ tracker.trackSuccess();
1748
+ } else {
1749
+ tracker.trackError();
1750
+ }
1751
+ summaries[nodeKey] = tracker.getSummary();
1752
+ }
1753
+ return summaries;
1754
+ }
1755
+ /**
1756
+ * Returns the underlying AgentGraphDefinition.
1757
+ */
1758
+ getGraphDefinition() {
1759
+ return this._graphDefinition;
1760
+ }
1761
+ };
1762
+
1853
1763
  // src/index.ts
1854
1764
  function initAi(ldClient) {
1855
1765
  return new LDAIClientImpl(ldClient);
@@ -1866,9 +1776,6 @@ export {
1866
1776
  ManagedModel,
1867
1777
  RunnerFactory,
1868
1778
  SUPPORTED_AI_PROVIDERS,
1869
- createBedrockTokenUsage,
1870
- createOpenAiUsage,
1871
- createVercelAISDKTokenUsage,
1872
1779
  initAi
1873
1780
  };
1874
1781
  //# sourceMappingURL=index.js.map