@posthog/ai 2.3.1 → 3.0.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.
package/lib/index.cjs.js CHANGED
@@ -6,12 +6,36 @@ var OpenAIOrignal = require('openai');
6
6
  var uuid = require('uuid');
7
7
  var stream = require('stream');
8
8
  var ai = require('ai');
9
+ var AnthropicOriginal = require('@anthropic-ai/sdk');
9
10
 
10
11
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
11
12
 
13
+ function _interopNamespace(e) {
14
+ if (e && e.__esModule) return e;
15
+ var n = Object.create(null);
16
+ if (e) {
17
+ Object.keys(e).forEach(function (k) {
18
+ if (k !== 'default') {
19
+ var d = Object.getOwnPropertyDescriptor(e, k);
20
+ Object.defineProperty(n, k, d.get ? d : {
21
+ enumerable: true,
22
+ get: function () { return e[k]; }
23
+ });
24
+ }
25
+ });
26
+ }
27
+ n["default"] = e;
28
+ return Object.freeze(n);
29
+ }
30
+
12
31
  var OpenAIOrignal__default = /*#__PURE__*/_interopDefaultLegacy(OpenAIOrignal);
32
+ var uuid__namespace = /*#__PURE__*/_interopNamespace(uuid);
33
+ var AnthropicOriginal__default = /*#__PURE__*/_interopDefaultLegacy(AnthropicOriginal);
13
34
 
14
35
  const getModelParams = params => {
36
+ if (!params) {
37
+ return {};
38
+ }
15
39
  const modelParams = {};
16
40
  const paramKeys = ['temperature', 'max_tokens', 'max_completion_tokens', 'top_p', 'frequency_penalty', 'presence_penalty', 'n', 'stop', 'stream', 'streaming'];
17
41
  for (const key of paramKeys) {
@@ -21,6 +45,31 @@ const getModelParams = params => {
21
45
  }
22
46
  return modelParams;
23
47
  };
48
+ const formatResponseAnthropic = response => {
49
+ // Example approach if "response.content" holds array of text segments, etc.
50
+ const output = [];
51
+ for (const choice of response.content ?? []) {
52
+ if (choice?.text) {
53
+ output.push({
54
+ role: 'assistant',
55
+ content: choice.text
56
+ });
57
+ }
58
+ }
59
+ return output;
60
+ };
61
+ const formatResponseOpenAI = response => {
62
+ const output = [];
63
+ for (const choice of response.choices ?? []) {
64
+ if (choice.message?.content) {
65
+ output.push({
66
+ role: choice.message.role,
67
+ content: choice.message.content
68
+ });
69
+ }
70
+ }
71
+ return output;
72
+ };
24
73
  const mergeSystemPrompt = (params, provider) => {
25
74
  if (provider == 'anthropic') {
26
75
  const messages = params.messages || [];
@@ -156,7 +205,7 @@ class WrappedCompletions$1 extends OpenAIOrignal__default["default"].Chat.Comple
156
205
  traceId,
157
206
  model: openAIParams.model,
158
207
  provider: 'openai',
159
- input: mergeSystemPrompt(openAIParams, 'openai'),
208
+ input: openAIParams.messages,
160
209
  output: [{
161
210
  content: accumulatedContent,
162
211
  role: 'assistant'
@@ -176,7 +225,7 @@ class WrappedCompletions$1 extends OpenAIOrignal__default["default"].Chat.Comple
176
225
  traceId,
177
226
  model: openAIParams.model,
178
227
  provider: 'openai',
179
- input: mergeSystemPrompt(openAIParams, 'openai'),
228
+ input: openAIParams.messages,
180
229
  output: [],
181
230
  latency: 0,
182
231
  baseURL: this.baseURL ?? '',
@@ -205,11 +254,8 @@ class WrappedCompletions$1 extends OpenAIOrignal__default["default"].Chat.Comple
205
254
  traceId,
206
255
  model: openAIParams.model,
207
256
  provider: 'openai',
208
- input: mergeSystemPrompt(openAIParams, 'openai'),
209
- output: [{
210
- content: result.choices[0].message.content,
211
- role: 'assistant'
212
- }],
257
+ input: openAIParams.messages,
258
+ output: formatResponseOpenAI(result),
213
259
  latency,
214
260
  baseURL: this.baseURL ?? '',
215
261
  params: body,
@@ -228,7 +274,7 @@ class WrappedCompletions$1 extends OpenAIOrignal__default["default"].Chat.Comple
228
274
  traceId,
229
275
  model: openAIParams.model,
230
276
  provider: 'openai',
231
- input: mergeSystemPrompt(openAIParams, 'openai'),
277
+ input: openAIParams.messages,
232
278
  output: [],
233
279
  latency: 0,
234
280
  baseURL: this.baseURL ?? '',
@@ -320,7 +366,7 @@ class WrappedCompletions extends OpenAIOrignal.AzureOpenAI.Chat.Completions {
320
366
  traceId,
321
367
  model,
322
368
  provider: 'azure',
323
- input: mergeSystemPrompt(openAIParams, 'azure'),
369
+ input: openAIParams.messages,
324
370
  output: [{
325
371
  content: accumulatedContent,
326
372
  role: 'assistant'
@@ -340,7 +386,7 @@ class WrappedCompletions extends OpenAIOrignal.AzureOpenAI.Chat.Completions {
340
386
  traceId,
341
387
  model,
342
388
  provider: 'azure',
343
- input: mergeSystemPrompt(openAIParams, 'azure'),
389
+ input: openAIParams.messages,
344
390
  output: JSON.stringify(error),
345
391
  latency: 0,
346
392
  baseURL: this.baseURL ?? '',
@@ -372,12 +418,9 @@ class WrappedCompletions extends OpenAIOrignal.AzureOpenAI.Chat.Completions {
372
418
  distinctId: posthogDistinctId ?? traceId,
373
419
  traceId,
374
420
  model,
375
- provider: '1234',
376
- input: mergeSystemPrompt(openAIParams, 'azure'),
377
- output: [{
378
- content: result.choices[0].message.content,
379
- role: 'assistant'
380
- }],
421
+ provider: 'azure',
422
+ input: openAIParams.messages,
423
+ output: formatResponseOpenAI(result),
381
424
  latency,
382
425
  baseURL: this.baseURL ?? '',
383
426
  params: body,
@@ -396,7 +439,7 @@ class WrappedCompletions extends OpenAIOrignal.AzureOpenAI.Chat.Completions {
396
439
  traceId,
397
440
  model: openAIParams.model,
398
441
  provider: 'azure',
399
- input: mergeSystemPrompt(openAIParams, 'azure'),
442
+ input: openAIParams.messages,
400
443
  output: [],
401
444
  latency: 0,
402
445
  baseURL: this.baseURL ?? '',
@@ -459,7 +502,7 @@ const createInstrumentationMiddleware = (phClient, model, options) => {
459
502
  params
460
503
  }) => {
461
504
  const startTime = Date.now();
462
- let mergedParams = {
505
+ const mergedParams = {
463
506
  ...options,
464
507
  ...mapVercelParams(params)
465
508
  };
@@ -521,7 +564,7 @@ const createInstrumentationMiddleware = (phClient, model, options) => {
521
564
  const startTime = Date.now();
522
565
  let generatedText = '';
523
566
  let usage = {};
524
- let mergedParams = {
567
+ const mergedParams = {
525
568
  ...options,
526
569
  ...mapVercelParams(params)
527
570
  };
@@ -604,14 +647,1058 @@ const wrapVercelLanguageModel = (model, phClient, options) => {
604
647
  posthogTraceId: traceId,
605
648
  posthogDistinctId: options.posthogDistinctId ?? traceId
606
649
  });
607
- const wrappedModel = ai.experimental_wrapLanguageModel({
650
+ const wrappedModel = ai.wrapLanguageModel({
608
651
  model,
609
652
  middleware
610
653
  });
611
654
  return wrappedModel;
612
655
  };
613
656
 
657
+ class PostHogAnthropic extends AnthropicOriginal__default["default"] {
658
+ constructor(config) {
659
+ const {
660
+ posthog,
661
+ ...anthropicConfig
662
+ } = config;
663
+ super(anthropicConfig);
664
+ this.phClient = posthog;
665
+ this.messages = new WrappedMessages(this, this.phClient);
666
+ }
667
+ }
668
+ class WrappedMessages extends AnthropicOriginal__default["default"].Messages {
669
+ constructor(parentClient, phClient) {
670
+ super(parentClient);
671
+ this.phClient = phClient;
672
+ }
673
+ create(body, options) {
674
+ const {
675
+ posthogDistinctId,
676
+ posthogTraceId,
677
+ posthogProperties,
678
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
679
+ posthogPrivacyMode = false,
680
+ posthogGroups,
681
+ ...anthropicParams
682
+ } = body;
683
+ const traceId = posthogTraceId ?? uuid.v4();
684
+ const startTime = Date.now();
685
+ const parentPromise = super.create(anthropicParams, options);
686
+ if (anthropicParams.stream) {
687
+ return parentPromise.then(value => {
688
+ const passThroughStream = new stream.PassThrough({
689
+ objectMode: true
690
+ });
691
+ let accumulatedContent = '';
692
+ const usage = {
693
+ inputTokens: 0,
694
+ outputTokens: 0
695
+ };
696
+ if ('tee' in value) {
697
+ const anthropicStream = value;
698
+ (async () => {
699
+ try {
700
+ for await (const chunk of anthropicStream) {
701
+ if ('delta' in chunk) {
702
+ if ('text' in chunk.delta) {
703
+ const delta = chunk?.delta?.text ?? '';
704
+ accumulatedContent += delta;
705
+ }
706
+ }
707
+ if (chunk.type == 'message_start') {
708
+ usage.inputTokens = chunk.message.usage.input_tokens ?? 0;
709
+ }
710
+ if ('usage' in chunk) {
711
+ usage.outputTokens = chunk.usage.output_tokens ?? 0;
712
+ }
713
+ passThroughStream.write(chunk);
714
+ }
715
+ const latency = (Date.now() - startTime) / 1000;
716
+ sendEventToPosthog({
717
+ client: this.phClient,
718
+ distinctId: posthogDistinctId ?? traceId,
719
+ traceId,
720
+ model: anthropicParams.model,
721
+ provider: 'anthropic',
722
+ input: mergeSystemPrompt(anthropicParams, 'anthropic'),
723
+ output: [{
724
+ content: accumulatedContent,
725
+ role: 'assistant'
726
+ }],
727
+ latency,
728
+ baseURL: this.baseURL ?? '',
729
+ params: body,
730
+ httpStatus: 200,
731
+ usage
732
+ });
733
+ passThroughStream.end();
734
+ } catch (error) {
735
+ // error handling
736
+ sendEventToPosthog({
737
+ client: this.phClient,
738
+ distinctId: posthogDistinctId ?? traceId,
739
+ traceId,
740
+ model: anthropicParams.model,
741
+ provider: 'anthropic',
742
+ input: mergeSystemPrompt(anthropicParams, 'anthropic'),
743
+ output: [],
744
+ latency: 0,
745
+ baseURL: this.baseURL ?? '',
746
+ params: body,
747
+ httpStatus: error?.status ? error.status : 500,
748
+ usage: {
749
+ inputTokens: 0,
750
+ outputTokens: 0
751
+ },
752
+ isError: true,
753
+ error: JSON.stringify(error)
754
+ });
755
+ passThroughStream.emit('error', error);
756
+ }
757
+ })();
758
+ }
759
+ return passThroughStream;
760
+ });
761
+ } else {
762
+ const wrappedPromise = parentPromise.then(result => {
763
+ if ('content' in result) {
764
+ const latency = (Date.now() - startTime) / 1000;
765
+ sendEventToPosthog({
766
+ client: this.phClient,
767
+ distinctId: posthogDistinctId ?? traceId,
768
+ traceId,
769
+ model: anthropicParams.model,
770
+ provider: 'anthropic',
771
+ input: mergeSystemPrompt(anthropicParams, 'anthropic'),
772
+ output: formatResponseAnthropic(result),
773
+ latency,
774
+ baseURL: this.baseURL ?? '',
775
+ params: body,
776
+ httpStatus: 200,
777
+ usage: {
778
+ inputTokens: result.usage.input_tokens ?? 0,
779
+ outputTokens: result.usage.output_tokens ?? 0
780
+ }
781
+ });
782
+ }
783
+ return result;
784
+ }, error => {
785
+ sendEventToPosthog({
786
+ client: this.phClient,
787
+ distinctId: posthogDistinctId ?? traceId,
788
+ traceId,
789
+ model: anthropicParams.model,
790
+ provider: 'anthropic',
791
+ input: mergeSystemPrompt(anthropicParams, 'anthropic'),
792
+ output: [],
793
+ latency: 0,
794
+ baseURL: this.baseURL ?? '',
795
+ params: body,
796
+ httpStatus: error?.status ? error.status : 500,
797
+ usage: {
798
+ inputTokens: 0,
799
+ outputTokens: 0
800
+ },
801
+ isError: true,
802
+ error: JSON.stringify(error)
803
+ });
804
+ throw error;
805
+ });
806
+ return wrappedPromise;
807
+ }
808
+ }
809
+ }
810
+
811
+ var decamelize = function (str, sep) {
812
+ if (typeof str !== 'string') {
813
+ throw new TypeError('Expected a string');
814
+ }
815
+ sep = typeof sep === 'undefined' ? '_' : sep;
816
+ return str
817
+ .replace(/([a-z\d])([A-Z])/g, '$1' + sep + '$2')
818
+ .replace(/([A-Z]+)([A-Z][a-z\d]+)/g, '$1' + sep + '$2')
819
+ .toLowerCase();
820
+ };
821
+
822
+ var camelcase = {exports: {}};
823
+
824
+ const UPPERCASE = /[\p{Lu}]/u;
825
+ const LOWERCASE = /[\p{Ll}]/u;
826
+ const LEADING_CAPITAL = /^[\p{Lu}](?![\p{Lu}])/gu;
827
+ const IDENTIFIER = /([\p{Alpha}\p{N}_]|$)/u;
828
+ const SEPARATORS = /[_.\- ]+/;
829
+ const LEADING_SEPARATORS = new RegExp('^' + SEPARATORS.source);
830
+ const SEPARATORS_AND_IDENTIFIER = new RegExp(SEPARATORS.source + IDENTIFIER.source, 'gu');
831
+ const NUMBERS_AND_IDENTIFIER = new RegExp('\\d+' + IDENTIFIER.source, 'gu');
832
+ const preserveCamelCase = (string, toLowerCase, toUpperCase) => {
833
+ let isLastCharLower = false;
834
+ let isLastCharUpper = false;
835
+ let isLastLastCharUpper = false;
836
+ for (let i = 0; i < string.length; i++) {
837
+ const character = string[i];
838
+ if (isLastCharLower && UPPERCASE.test(character)) {
839
+ string = string.slice(0, i) + '-' + string.slice(i);
840
+ isLastCharLower = false;
841
+ isLastLastCharUpper = isLastCharUpper;
842
+ isLastCharUpper = true;
843
+ i++;
844
+ }
845
+ else if (isLastCharUpper && isLastLastCharUpper && LOWERCASE.test(character)) {
846
+ string = string.slice(0, i - 1) + '-' + string.slice(i - 1);
847
+ isLastLastCharUpper = isLastCharUpper;
848
+ isLastCharUpper = false;
849
+ isLastCharLower = true;
850
+ }
851
+ else {
852
+ isLastCharLower = toLowerCase(character) === character && toUpperCase(character) !== character;
853
+ isLastLastCharUpper = isLastCharUpper;
854
+ isLastCharUpper = toUpperCase(character) === character && toLowerCase(character) !== character;
855
+ }
856
+ }
857
+ return string;
858
+ };
859
+ const preserveConsecutiveUppercase = (input, toLowerCase) => {
860
+ LEADING_CAPITAL.lastIndex = 0;
861
+ return input.replace(LEADING_CAPITAL, m1 => toLowerCase(m1));
862
+ };
863
+ const postProcess = (input, toUpperCase) => {
864
+ SEPARATORS_AND_IDENTIFIER.lastIndex = 0;
865
+ NUMBERS_AND_IDENTIFIER.lastIndex = 0;
866
+ return input.replace(SEPARATORS_AND_IDENTIFIER, (_, identifier) => toUpperCase(identifier))
867
+ .replace(NUMBERS_AND_IDENTIFIER, m => toUpperCase(m));
868
+ };
869
+ const camelCase = (input, options) => {
870
+ if (!(typeof input === 'string' || Array.isArray(input))) {
871
+ throw new TypeError('Expected the input to be `string | string[]`');
872
+ }
873
+ options = {
874
+ pascalCase: false,
875
+ preserveConsecutiveUppercase: false,
876
+ ...options
877
+ };
878
+ if (Array.isArray(input)) {
879
+ input = input.map(x => x.trim())
880
+ .filter(x => x.length)
881
+ .join('-');
882
+ }
883
+ else {
884
+ input = input.trim();
885
+ }
886
+ if (input.length === 0) {
887
+ return '';
888
+ }
889
+ const toLowerCase = options.locale === false ?
890
+ string => string.toLowerCase() :
891
+ string => string.toLocaleLowerCase(options.locale);
892
+ const toUpperCase = options.locale === false ?
893
+ string => string.toUpperCase() :
894
+ string => string.toLocaleUpperCase(options.locale);
895
+ if (input.length === 1) {
896
+ return options.pascalCase ? toUpperCase(input) : toLowerCase(input);
897
+ }
898
+ const hasUpperCase = input !== toLowerCase(input);
899
+ if (hasUpperCase) {
900
+ input = preserveCamelCase(input, toLowerCase, toUpperCase);
901
+ }
902
+ input = input.replace(LEADING_SEPARATORS, '');
903
+ if (options.preserveConsecutiveUppercase) {
904
+ input = preserveConsecutiveUppercase(input, toLowerCase);
905
+ }
906
+ else {
907
+ input = toLowerCase(input);
908
+ }
909
+ if (options.pascalCase) {
910
+ input = toUpperCase(input.charAt(0)) + input.slice(1);
911
+ }
912
+ return postProcess(input, toUpperCase);
913
+ };
914
+ camelcase.exports = camelCase;
915
+ // TODO: Remove this for the next major release
916
+ camelcase.exports.default = camelCase;
917
+
918
+ function keyToJson(key, map) {
919
+ return map?.[key] || decamelize(key);
920
+ }
921
+ function mapKeys(fields, mapper, map) {
922
+ const mapped = {};
923
+ for (const key in fields) {
924
+ if (Object.hasOwn(fields, key)) {
925
+ mapped[mapper(key, map)] = fields[key];
926
+ }
927
+ }
928
+ return mapped;
929
+ }
930
+
931
+ function shallowCopy(obj) {
932
+ return Array.isArray(obj) ? [...obj] : { ...obj };
933
+ }
934
+ function replaceSecrets(root, secretsMap) {
935
+ const result = shallowCopy(root);
936
+ for (const [path, secretId] of Object.entries(secretsMap)) {
937
+ const [last, ...partsReverse] = path.split(".").reverse();
938
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
939
+ let current = result;
940
+ for (const part of partsReverse.reverse()) {
941
+ if (current[part] === undefined) {
942
+ break;
943
+ }
944
+ current[part] = shallowCopy(current[part]);
945
+ current = current[part];
946
+ }
947
+ if (current[last] !== undefined) {
948
+ current[last] = {
949
+ lc: 1,
950
+ type: "secret",
951
+ id: [secretId],
952
+ };
953
+ }
954
+ }
955
+ return result;
956
+ }
957
+ /**
958
+ * Get a unique name for the module, rather than parent class implementations.
959
+ * Should not be subclassed, subclass lc_name above instead.
960
+ */
961
+ function get_lc_unique_name(
962
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
963
+ serializableClass) {
964
+ // "super" here would refer to the parent class of Serializable,
965
+ // when we want the parent class of the module actually calling this method.
966
+ const parentClass = Object.getPrototypeOf(serializableClass);
967
+ const lcNameIsSubclassed = typeof serializableClass.lc_name === "function" &&
968
+ (typeof parentClass.lc_name !== "function" ||
969
+ serializableClass.lc_name() !== parentClass.lc_name());
970
+ if (lcNameIsSubclassed) {
971
+ return serializableClass.lc_name();
972
+ }
973
+ else {
974
+ return serializableClass.name;
975
+ }
976
+ }
977
+ class Serializable {
978
+ /**
979
+ * The name of the serializable. Override to provide an alias or
980
+ * to preserve the serialized module name in minified environments.
981
+ *
982
+ * Implemented as a static method to support loading logic.
983
+ */
984
+ static lc_name() {
985
+ return this.name;
986
+ }
987
+ /**
988
+ * The final serialized identifier for the module.
989
+ */
990
+ get lc_id() {
991
+ return [
992
+ ...this.lc_namespace,
993
+ get_lc_unique_name(this.constructor),
994
+ ];
995
+ }
996
+ /**
997
+ * A map of secrets, which will be omitted from serialization.
998
+ * Keys are paths to the secret in constructor args, e.g. "foo.bar.baz".
999
+ * Values are the secret ids, which will be used when deserializing.
1000
+ */
1001
+ get lc_secrets() {
1002
+ return undefined;
1003
+ }
1004
+ /**
1005
+ * A map of additional attributes to merge with constructor args.
1006
+ * Keys are the attribute names, e.g. "foo".
1007
+ * Values are the attribute values, which will be serialized.
1008
+ * These attributes need to be accepted by the constructor as arguments.
1009
+ */
1010
+ get lc_attributes() {
1011
+ return undefined;
1012
+ }
1013
+ /**
1014
+ * A map of aliases for constructor args.
1015
+ * Keys are the attribute names, e.g. "foo".
1016
+ * Values are the alias that will replace the key in serialization.
1017
+ * This is used to eg. make argument names match Python.
1018
+ */
1019
+ get lc_aliases() {
1020
+ return undefined;
1021
+ }
1022
+ constructor(kwargs, ..._args) {
1023
+ Object.defineProperty(this, "lc_serializable", {
1024
+ enumerable: true,
1025
+ configurable: true,
1026
+ writable: true,
1027
+ value: false
1028
+ });
1029
+ Object.defineProperty(this, "lc_kwargs", {
1030
+ enumerable: true,
1031
+ configurable: true,
1032
+ writable: true,
1033
+ value: void 0
1034
+ });
1035
+ this.lc_kwargs = kwargs || {};
1036
+ }
1037
+ toJSON() {
1038
+ if (!this.lc_serializable) {
1039
+ return this.toJSONNotImplemented();
1040
+ }
1041
+ if (
1042
+ // eslint-disable-next-line no-instanceof/no-instanceof
1043
+ this.lc_kwargs instanceof Serializable ||
1044
+ typeof this.lc_kwargs !== "object" ||
1045
+ Array.isArray(this.lc_kwargs)) {
1046
+ // We do not support serialization of classes with arg not a POJO
1047
+ // I'm aware the check above isn't as strict as it could be
1048
+ return this.toJSONNotImplemented();
1049
+ }
1050
+ const aliases = {};
1051
+ const secrets = {};
1052
+ const kwargs = Object.keys(this.lc_kwargs).reduce((acc, key) => {
1053
+ acc[key] = key in this ? this[key] : this.lc_kwargs[key];
1054
+ return acc;
1055
+ }, {});
1056
+ // get secrets, attributes and aliases from all superclasses
1057
+ for (
1058
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
1059
+ let current = Object.getPrototypeOf(this); current; current = Object.getPrototypeOf(current)) {
1060
+ Object.assign(aliases, Reflect.get(current, "lc_aliases", this));
1061
+ Object.assign(secrets, Reflect.get(current, "lc_secrets", this));
1062
+ Object.assign(kwargs, Reflect.get(current, "lc_attributes", this));
1063
+ }
1064
+ // include all secrets used, even if not in kwargs,
1065
+ // will be replaced with sentinel value in replaceSecrets
1066
+ Object.keys(secrets).forEach((keyPath) => {
1067
+ // eslint-disable-next-line @typescript-eslint/no-this-alias, @typescript-eslint/no-explicit-any
1068
+ let read = this;
1069
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1070
+ let write = kwargs;
1071
+ const [last, ...partsReverse] = keyPath.split(".").reverse();
1072
+ for (const key of partsReverse.reverse()) {
1073
+ if (!(key in read) || read[key] === undefined)
1074
+ return;
1075
+ if (!(key in write) || write[key] === undefined) {
1076
+ if (typeof read[key] === "object" && read[key] != null) {
1077
+ write[key] = {};
1078
+ }
1079
+ else if (Array.isArray(read[key])) {
1080
+ write[key] = [];
1081
+ }
1082
+ }
1083
+ read = read[key];
1084
+ write = write[key];
1085
+ }
1086
+ if (last in read && read[last] !== undefined) {
1087
+ write[last] = write[last] || read[last];
1088
+ }
1089
+ });
1090
+ return {
1091
+ lc: 1,
1092
+ type: "constructor",
1093
+ id: this.lc_id,
1094
+ kwargs: mapKeys(Object.keys(secrets).length ? replaceSecrets(kwargs, secrets) : kwargs, keyToJson, aliases),
1095
+ };
1096
+ }
1097
+ toJSONNotImplemented() {
1098
+ return {
1099
+ lc: 1,
1100
+ type: "not_implemented",
1101
+ id: this.lc_id,
1102
+ };
1103
+ }
1104
+ }
1105
+
1106
+ // Supabase Edge Function provides a `Deno` global object
1107
+ // without `version` property
1108
+ const isDeno = () => typeof Deno !== "undefined";
1109
+ function getEnvironmentVariable(name) {
1110
+ // Certain Deno setups will throw an error if you try to access environment variables
1111
+ // https://github.com/langchain-ai/langchainjs/issues/1412
1112
+ try {
1113
+ if (typeof process !== "undefined") {
1114
+ // eslint-disable-next-line no-process-env
1115
+ return process.env?.[name];
1116
+ }
1117
+ else if (isDeno()) {
1118
+ return Deno?.env.get(name);
1119
+ }
1120
+ else {
1121
+ return undefined;
1122
+ }
1123
+ }
1124
+ catch (e) {
1125
+ return undefined;
1126
+ }
1127
+ }
1128
+
1129
+ /**
1130
+ * Abstract class that provides a set of optional methods that can be
1131
+ * overridden in derived classes to handle various events during the
1132
+ * execution of a LangChain application.
1133
+ */
1134
+ class BaseCallbackHandlerMethodsClass {
1135
+ }
1136
+ /**
1137
+ * Abstract base class for creating callback handlers in the LangChain
1138
+ * framework. It provides a set of optional methods that can be overridden
1139
+ * in derived classes to handle various events during the execution of a
1140
+ * LangChain application.
1141
+ */
1142
+ class BaseCallbackHandler extends BaseCallbackHandlerMethodsClass {
1143
+ get lc_namespace() {
1144
+ return ["langchain_core", "callbacks", this.name];
1145
+ }
1146
+ get lc_secrets() {
1147
+ return undefined;
1148
+ }
1149
+ get lc_attributes() {
1150
+ return undefined;
1151
+ }
1152
+ get lc_aliases() {
1153
+ return undefined;
1154
+ }
1155
+ /**
1156
+ * The name of the serializable. Override to provide an alias or
1157
+ * to preserve the serialized module name in minified environments.
1158
+ *
1159
+ * Implemented as a static method to support loading logic.
1160
+ */
1161
+ static lc_name() {
1162
+ return this.name;
1163
+ }
1164
+ /**
1165
+ * The final serialized identifier for the module.
1166
+ */
1167
+ get lc_id() {
1168
+ return [
1169
+ ...this.lc_namespace,
1170
+ get_lc_unique_name(this.constructor),
1171
+ ];
1172
+ }
1173
+ constructor(input) {
1174
+ super();
1175
+ Object.defineProperty(this, "lc_serializable", {
1176
+ enumerable: true,
1177
+ configurable: true,
1178
+ writable: true,
1179
+ value: false
1180
+ });
1181
+ Object.defineProperty(this, "lc_kwargs", {
1182
+ enumerable: true,
1183
+ configurable: true,
1184
+ writable: true,
1185
+ value: void 0
1186
+ });
1187
+ Object.defineProperty(this, "ignoreLLM", {
1188
+ enumerable: true,
1189
+ configurable: true,
1190
+ writable: true,
1191
+ value: false
1192
+ });
1193
+ Object.defineProperty(this, "ignoreChain", {
1194
+ enumerable: true,
1195
+ configurable: true,
1196
+ writable: true,
1197
+ value: false
1198
+ });
1199
+ Object.defineProperty(this, "ignoreAgent", {
1200
+ enumerable: true,
1201
+ configurable: true,
1202
+ writable: true,
1203
+ value: false
1204
+ });
1205
+ Object.defineProperty(this, "ignoreRetriever", {
1206
+ enumerable: true,
1207
+ configurable: true,
1208
+ writable: true,
1209
+ value: false
1210
+ });
1211
+ Object.defineProperty(this, "ignoreCustomEvent", {
1212
+ enumerable: true,
1213
+ configurable: true,
1214
+ writable: true,
1215
+ value: false
1216
+ });
1217
+ Object.defineProperty(this, "raiseError", {
1218
+ enumerable: true,
1219
+ configurable: true,
1220
+ writable: true,
1221
+ value: false
1222
+ });
1223
+ Object.defineProperty(this, "awaitHandlers", {
1224
+ enumerable: true,
1225
+ configurable: true,
1226
+ writable: true,
1227
+ value: getEnvironmentVariable("LANGCHAIN_CALLBACKS_BACKGROUND") === "false"
1228
+ });
1229
+ this.lc_kwargs = input || {};
1230
+ if (input) {
1231
+ this.ignoreLLM = input.ignoreLLM ?? this.ignoreLLM;
1232
+ this.ignoreChain = input.ignoreChain ?? this.ignoreChain;
1233
+ this.ignoreAgent = input.ignoreAgent ?? this.ignoreAgent;
1234
+ this.ignoreRetriever = input.ignoreRetriever ?? this.ignoreRetriever;
1235
+ this.ignoreCustomEvent =
1236
+ input.ignoreCustomEvent ?? this.ignoreCustomEvent;
1237
+ this.raiseError = input.raiseError ?? this.raiseError;
1238
+ this.awaitHandlers =
1239
+ this.raiseError || (input._awaitHandler ?? this.awaitHandlers);
1240
+ }
1241
+ }
1242
+ copy() {
1243
+ return new this.constructor(this);
1244
+ }
1245
+ toJSON() {
1246
+ return Serializable.prototype.toJSON.call(this);
1247
+ }
1248
+ toJSONNotImplemented() {
1249
+ return Serializable.prototype.toJSONNotImplemented.call(this);
1250
+ }
1251
+ static fromMethods(methods) {
1252
+ class Handler extends BaseCallbackHandler {
1253
+ constructor() {
1254
+ super();
1255
+ Object.defineProperty(this, "name", {
1256
+ enumerable: true,
1257
+ configurable: true,
1258
+ writable: true,
1259
+ value: uuid__namespace.v4()
1260
+ });
1261
+ Object.assign(this, methods);
1262
+ }
1263
+ }
1264
+ return new Handler();
1265
+ }
1266
+ }
1267
+
1268
+ class LangChainCallbackHandler extends BaseCallbackHandler {
1269
+ constructor(options) {
1270
+ if (!options.client) {
1271
+ throw new Error('PostHog client is required');
1272
+ }
1273
+ super();
1274
+ this.name = 'PosthogCallbackHandler';
1275
+ this.runs = {};
1276
+ this.parentTree = {};
1277
+ this.client = options.client;
1278
+ this.distinctId = options.distinctId;
1279
+ this.traceId = options.traceId;
1280
+ this.properties = options.properties || {};
1281
+ this.privacyMode = options.privacyMode || false;
1282
+ this.groups = options.groups || {};
1283
+ this.debug = options.debug || false;
1284
+ }
1285
+ // ===== CALLBACK METHODS =====
1286
+ handleChainStart(chain, inputs, runId, parentRunId, tags, metadata,
1287
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
1288
+ runType, runName) {
1289
+ this._logDebugEvent('on_chain_start', runId, parentRunId, {
1290
+ inputs,
1291
+ tags
1292
+ });
1293
+ this._setParentOfRun(runId, parentRunId);
1294
+ this._setTraceOrSpanMetadata(chain, inputs, runId, parentRunId, metadata, tags, runName);
1295
+ }
1296
+ handleChainEnd(outputs, runId, parentRunId, tags,
1297
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
1298
+ kwargs) {
1299
+ this._logDebugEvent('on_chain_end', runId, parentRunId, {
1300
+ outputs,
1301
+ tags
1302
+ });
1303
+ this._popRunAndCaptureTraceOrSpan(runId, parentRunId, outputs);
1304
+ }
1305
+ handleChainError(error, runId, parentRunId, tags,
1306
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
1307
+ kwargs) {
1308
+ this._logDebugEvent('on_chain_error', runId, parentRunId, {
1309
+ error,
1310
+ tags
1311
+ });
1312
+ this._popRunAndCaptureTraceOrSpan(runId, parentRunId, error);
1313
+ }
1314
+ handleChatModelStart(serialized, messages, runId, parentRunId, extraParams, tags, metadata, runName) {
1315
+ this._logDebugEvent('on_chat_model_start', runId, parentRunId, {
1316
+ messages,
1317
+ tags
1318
+ });
1319
+ this._setParentOfRun(runId, parentRunId);
1320
+ // Flatten the two-dimensional messages and convert each message to a plain object
1321
+ const input = messages.flat().map(m => this._convertMessageToDict(m));
1322
+ this._setLLMMetadata(serialized, runId, input, metadata, extraParams, runName);
1323
+ }
1324
+ handleLLMStart(serialized, prompts, runId, parentRunId, extraParams, tags, metadata, runName) {
1325
+ this._logDebugEvent('on_llm_start', runId, parentRunId, {
1326
+ prompts,
1327
+ tags
1328
+ });
1329
+ this._setParentOfRun(runId, parentRunId);
1330
+ this._setLLMMetadata(serialized, runId, prompts, metadata, extraParams, runName);
1331
+ }
1332
+ handleLLMEnd(output, runId, parentRunId, tags,
1333
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
1334
+ extraParams) {
1335
+ this._logDebugEvent('on_llm_end', runId, parentRunId, {
1336
+ output,
1337
+ tags
1338
+ });
1339
+ this._popRunAndCaptureGeneration(runId, parentRunId, output);
1340
+ }
1341
+ handleLLMError(err, runId, parentRunId, tags,
1342
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
1343
+ extraParams) {
1344
+ this._logDebugEvent('on_llm_error', runId, parentRunId, {
1345
+ err,
1346
+ tags
1347
+ });
1348
+ this._popRunAndCaptureGeneration(runId, parentRunId, err);
1349
+ }
1350
+ handleToolStart(tool, input, runId, parentRunId, tags, metadata, runName) {
1351
+ this._logDebugEvent('on_tool_start', runId, parentRunId, {
1352
+ input,
1353
+ tags
1354
+ });
1355
+ this._setParentOfRun(runId, parentRunId);
1356
+ this._setTraceOrSpanMetadata(tool, input, runId, parentRunId, metadata, tags, runName);
1357
+ }
1358
+ handleToolEnd(output, runId, parentRunId, tags) {
1359
+ this._logDebugEvent('on_tool_end', runId, parentRunId, {
1360
+ output,
1361
+ tags
1362
+ });
1363
+ this._popRunAndCaptureTraceOrSpan(runId, parentRunId, output);
1364
+ }
1365
+ handleToolError(err, runId, parentRunId, tags) {
1366
+ this._logDebugEvent('on_tool_error', runId, parentRunId, {
1367
+ err,
1368
+ tags
1369
+ });
1370
+ this._popRunAndCaptureTraceOrSpan(runId, parentRunId, err);
1371
+ }
1372
+ handleRetrieverStart(retriever, query, runId, parentRunId, tags, metadata, name) {
1373
+ this._logDebugEvent('on_retriever_start', runId, parentRunId, {
1374
+ query,
1375
+ tags
1376
+ });
1377
+ this._setParentOfRun(runId, parentRunId);
1378
+ this._setTraceOrSpanMetadata(retriever, query, runId, parentRunId, metadata, tags, name);
1379
+ }
1380
+ handleRetrieverEnd(documents, runId, parentRunId, tags) {
1381
+ this._logDebugEvent('on_retriever_end', runId, parentRunId, {
1382
+ documents,
1383
+ tags
1384
+ });
1385
+ this._popRunAndCaptureTraceOrSpan(runId, parentRunId, documents);
1386
+ }
1387
+ handleRetrieverError(err, runId, parentRunId, tags) {
1388
+ this._logDebugEvent('on_retriever_error', runId, parentRunId, {
1389
+ err,
1390
+ tags
1391
+ });
1392
+ this._popRunAndCaptureTraceOrSpan(runId, parentRunId, err);
1393
+ }
1394
+ handleAgentAction(action, runId, parentRunId, tags) {
1395
+ this._logDebugEvent('on_agent_action', runId, parentRunId, {
1396
+ action,
1397
+ tags
1398
+ });
1399
+ this._setParentOfRun(runId, parentRunId);
1400
+ this._setTraceOrSpanMetadata(null, action, runId, parentRunId);
1401
+ }
1402
+ handleAgentEnd(action, runId, parentRunId, tags) {
1403
+ this._logDebugEvent('on_agent_finish', runId, parentRunId, {
1404
+ action,
1405
+ tags
1406
+ });
1407
+ this._popRunAndCaptureTraceOrSpan(runId, parentRunId, action);
1408
+ }
1409
+ // ===== PRIVATE HELPERS =====
1410
+ _setParentOfRun(runId, parentRunId) {
1411
+ if (parentRunId) {
1412
+ this.parentTree[runId] = parentRunId;
1413
+ }
1414
+ }
1415
+ _popParentOfRun(runId) {
1416
+ delete this.parentTree[runId];
1417
+ }
1418
+ _findRootRun(runId) {
1419
+ let id = runId;
1420
+ while (this.parentTree[id]) {
1421
+ id = this.parentTree[id];
1422
+ }
1423
+ return id;
1424
+ }
1425
+ _setTraceOrSpanMetadata(serialized, input, runId, parentRunId, ...args) {
1426
+ // Use default names if not provided: if this is a top-level run, we mark it as a trace, otherwise as a span.
1427
+ const defaultName = parentRunId ? 'span' : 'trace';
1428
+ const runName = this._getLangchainRunName(serialized, ...args) || defaultName;
1429
+ this.runs[runId] = {
1430
+ name: runName,
1431
+ input,
1432
+ startTime: Date.now()
1433
+ };
1434
+ }
1435
+ _setLLMMetadata(serialized, runId, messages, metadata, extraParams, runName) {
1436
+ const runNameFound = this._getLangchainRunName(serialized, {
1437
+ extraParams,
1438
+ runName
1439
+ }) || 'generation';
1440
+ const generation = {
1441
+ name: runNameFound,
1442
+ input: messages,
1443
+ startTime: Date.now()
1444
+ };
1445
+ if (extraParams) {
1446
+ generation.modelParams = getModelParams(extraParams.invocation_params);
1447
+ }
1448
+ if (metadata) {
1449
+ if (metadata.ls_model_name) {
1450
+ generation.model = metadata.ls_model_name;
1451
+ }
1452
+ if (metadata.ls_provider) {
1453
+ generation.provider = metadata.ls_provider;
1454
+ }
1455
+ }
1456
+ if (serialized && 'kwargs' in serialized && serialized.kwargs.openai_api_base) {
1457
+ generation.baseUrl = serialized.kwargs.openai_api_base;
1458
+ }
1459
+ this.runs[runId] = generation;
1460
+ }
1461
+ _popRunMetadata(runId) {
1462
+ const endTime = Date.now();
1463
+ const run = this.runs[runId];
1464
+ if (!run) {
1465
+ console.warn(`No run metadata found for run ${runId}`);
1466
+ return undefined;
1467
+ }
1468
+ run.endTime = endTime;
1469
+ delete this.runs[runId];
1470
+ return run;
1471
+ }
1472
+ _getTraceId(runId) {
1473
+ return this.traceId ? String(this.traceId) : this._findRootRun(runId);
1474
+ }
1475
+ _getParentRunId(traceId, runId, parentRunId) {
1476
+ // Replace the parent-run if not found in our stored parent tree.
1477
+ if (parentRunId && !this.parentTree[parentRunId]) {
1478
+ return traceId;
1479
+ }
1480
+ return parentRunId;
1481
+ }
1482
+ _popRunAndCaptureTraceOrSpan(runId, parentRunId, outputs) {
1483
+ const traceId = this._getTraceId(runId);
1484
+ this._popParentOfRun(runId);
1485
+ const run = this._popRunMetadata(runId);
1486
+ if (!run) return;
1487
+ if ('modelParams' in run) {
1488
+ console.warn(`Run ${runId} is a generation, but attempted to be captured as a trace/span.`);
1489
+ return;
1490
+ }
1491
+ const actualParentRunId = this._getParentRunId(traceId, runId, parentRunId);
1492
+ this._captureTraceOrSpan(traceId, runId, run, outputs, actualParentRunId);
1493
+ }
1494
+ _captureTraceOrSpan(traceId, runId, run, outputs, parentRunId) {
1495
+ const eventName = parentRunId ? '$ai_span' : '$ai_trace';
1496
+ const latency = run.endTime ? (run.endTime - run.startTime) / 1000 : 0;
1497
+ const eventProperties = {
1498
+ $ai_trace_id: traceId,
1499
+ $ai_input_state: withPrivacyMode(this.client, this.privacyMode, run.input),
1500
+ $ai_latency: latency,
1501
+ $ai_span_name: run.name,
1502
+ $ai_span_id: runId
1503
+ };
1504
+ if (parentRunId) {
1505
+ eventProperties['$ai_parent_id'] = parentRunId;
1506
+ }
1507
+ Object.assign(eventProperties, this.properties);
1508
+ if (!this.distinctId) {
1509
+ eventProperties['$process_person_profile'] = false;
1510
+ }
1511
+ if (outputs instanceof Error) {
1512
+ eventProperties['$ai_error'] = outputs.toString();
1513
+ eventProperties['$ai_is_error'] = true;
1514
+ } else if (outputs !== undefined) {
1515
+ eventProperties['$ai_output_state'] = withPrivacyMode(this.client, this.privacyMode, outputs);
1516
+ }
1517
+ this.client.capture({
1518
+ distinctId: this.distinctId ? this.distinctId.toString() : runId,
1519
+ event: eventName,
1520
+ properties: eventProperties,
1521
+ groups: this.groups
1522
+ });
1523
+ }
1524
+ _popRunAndCaptureGeneration(runId, parentRunId, response) {
1525
+ const traceId = this._getTraceId(runId);
1526
+ this._popParentOfRun(runId);
1527
+ const run = this._popRunMetadata(runId);
1528
+ if (!run || typeof run !== 'object' || !('modelParams' in run)) {
1529
+ console.warn(`Run ${runId} is not a generation, but attempted to be captured as such.`);
1530
+ return;
1531
+ }
1532
+ const actualParentRunId = this._getParentRunId(traceId, runId, parentRunId);
1533
+ this._captureGeneration(traceId, runId, run, response, actualParentRunId);
1534
+ }
1535
+ _captureGeneration(traceId, runId, run, output, parentRunId) {
1536
+ const latency = run.endTime ? (run.endTime - run.startTime) / 1000 : 0;
1537
+ const eventProperties = {
1538
+ $ai_trace_id: traceId,
1539
+ $ai_span_id: runId,
1540
+ $ai_span_name: run.name,
1541
+ $ai_parent_id: parentRunId,
1542
+ $ai_provider: run.provider,
1543
+ $ai_model: run.model,
1544
+ $ai_model_parameters: run.modelParams,
1545
+ $ai_input: withPrivacyMode(this.client, this.privacyMode, run.input),
1546
+ $ai_http_status: 200,
1547
+ $ai_latency: latency,
1548
+ $ai_base_url: run.baseUrl
1549
+ };
1550
+ if (output instanceof Error) {
1551
+ eventProperties['$ai_http_status'] = output.status || 500;
1552
+ eventProperties['$ai_error'] = output.toString();
1553
+ eventProperties['$ai_is_error'] = true;
1554
+ } else {
1555
+ // Handle token usage
1556
+ const [inputTokens, outputTokens] = this.parseUsage(output);
1557
+ eventProperties['$ai_input_tokens'] = inputTokens;
1558
+ eventProperties['$ai_output_tokens'] = outputTokens;
1559
+ // Handle generations/completions
1560
+ let completions;
1561
+ if (output.generations && Array.isArray(output.generations)) {
1562
+ const lastGeneration = output.generations[output.generations.length - 1];
1563
+ if (Array.isArray(lastGeneration)) {
1564
+ completions = lastGeneration.map(gen => {
1565
+ return {
1566
+ role: 'assistant',
1567
+ content: gen.text
1568
+ };
1569
+ });
1570
+ }
1571
+ }
1572
+ if (completions) {
1573
+ eventProperties['$ai_output_choices'] = withPrivacyMode(this.client, this.privacyMode, completions);
1574
+ }
1575
+ }
1576
+ Object.assign(eventProperties, this.properties);
1577
+ if (!this.distinctId) {
1578
+ eventProperties['$process_person_profile'] = false;
1579
+ }
1580
+ this.client.capture({
1581
+ distinctId: this.distinctId ? this.distinctId.toString() : traceId,
1582
+ event: '$ai_generation',
1583
+ properties: eventProperties,
1584
+ groups: this.groups
1585
+ });
1586
+ }
1587
+ _logDebugEvent(eventName, runId, parentRunId, extra) {
1588
+ if (this.debug) {
1589
+ console.log(`Event: ${eventName}, runId: ${runId}, parentRunId: ${parentRunId}, extra:`, extra);
1590
+ }
1591
+ }
1592
+ _getLangchainRunName(serialized, ...args) {
1593
+ if (args && args.length > 0) {
1594
+ for (const arg of args) {
1595
+ if (arg && typeof arg === 'object' && 'name' in arg) return arg.name;
1596
+ }
1597
+ }
1598
+ if (serialized && serialized.name) return serialized.name;
1599
+ if (serialized && serialized.id) {
1600
+ return Array.isArray(serialized.id) ? serialized.id[serialized.id.length - 1] : serialized.id;
1601
+ }
1602
+ return undefined;
1603
+ }
1604
+ _convertMessageToDict(message) {
1605
+ let messageDict = {};
1606
+ // Check the _getType() method or type property instead of instanceof
1607
+ const messageType = message._getType?.() || message.type;
1608
+ switch (messageType) {
1609
+ case 'human':
1610
+ messageDict = {
1611
+ role: 'user',
1612
+ content: message.content
1613
+ };
1614
+ break;
1615
+ case 'ai':
1616
+ messageDict = {
1617
+ role: 'assistant',
1618
+ content: message.content
1619
+ };
1620
+ break;
1621
+ case 'system':
1622
+ messageDict = {
1623
+ role: 'system',
1624
+ content: message.content
1625
+ };
1626
+ break;
1627
+ case 'tool':
1628
+ messageDict = {
1629
+ role: 'tool',
1630
+ content: message.content
1631
+ };
1632
+ break;
1633
+ case 'function':
1634
+ messageDict = {
1635
+ role: 'function',
1636
+ content: message.content
1637
+ };
1638
+ break;
1639
+ default:
1640
+ messageDict = {
1641
+ role: messageType || 'unknown',
1642
+ content: String(message.content)
1643
+ };
1644
+ }
1645
+ if (message.additional_kwargs) {
1646
+ messageDict = {
1647
+ ...messageDict,
1648
+ ...message.additional_kwargs
1649
+ };
1650
+ }
1651
+ return messageDict;
1652
+ }
1653
+ _parseUsageModel(usage) {
1654
+ const conversionList = [['promptTokens', 'input'], ['completionTokens', 'output'], ['input_tokens', 'input'], ['output_tokens', 'output'], ['prompt_token_count', 'input'], ['candidates_token_count', 'output'], ['inputTokenCount', 'input'], ['outputTokenCount', 'output'], ['input_token_count', 'input'], ['generated_token_count', 'output']];
1655
+ const parsedUsage = conversionList.reduce((acc, [modelKey, typeKey]) => {
1656
+ const value = usage[modelKey];
1657
+ if (value != null) {
1658
+ const finalCount = Array.isArray(value) ? value.reduce((sum, tokenCount) => sum + tokenCount, 0) : value;
1659
+ acc[typeKey] = finalCount;
1660
+ }
1661
+ return acc;
1662
+ }, {
1663
+ input: 0,
1664
+ output: 0
1665
+ });
1666
+ return [parsedUsage.input, parsedUsage.output];
1667
+ }
1668
+ parseUsage(response) {
1669
+ let llmUsage = [0, 0];
1670
+ const llmUsageKeys = ['token_usage', 'usage', 'tokenUsage'];
1671
+ if (response.llmOutput != null) {
1672
+ const key = llmUsageKeys.find(k => response.llmOutput?.[k] != null);
1673
+ if (key) {
1674
+ llmUsage = this._parseUsageModel(response.llmOutput[key]);
1675
+ }
1676
+ }
1677
+ // If top-level usage info was not found, try checking the generations.
1678
+ if (llmUsage[0] === 0 && llmUsage[1] === 0 && response.generations) {
1679
+ for (const generation of response.generations) {
1680
+ for (const genChunk of generation) {
1681
+ if (genChunk.generationInfo?.usage_metadata) {
1682
+ llmUsage = this._parseUsageModel(genChunk.generationInfo.usage_metadata);
1683
+ return llmUsage;
1684
+ }
1685
+ const messageChunk = genChunk.generationInfo ?? {};
1686
+ const responseMetadata = messageChunk.response_metadata ?? {};
1687
+ const chunkUsage = responseMetadata['usage'] ?? responseMetadata['amazon-bedrock-invocationMetrics'] ?? messageChunk.usage_metadata;
1688
+ if (chunkUsage) {
1689
+ llmUsage = this._parseUsageModel(chunkUsage);
1690
+ return llmUsage;
1691
+ }
1692
+ }
1693
+ }
1694
+ }
1695
+ return llmUsage;
1696
+ }
1697
+ }
1698
+
1699
+ exports.Anthropic = PostHogAnthropic;
614
1700
  exports.AzureOpenAI = PostHogAzureOpenAI;
1701
+ exports.LangChainCallbackHandler = LangChainCallbackHandler;
615
1702
  exports.OpenAI = PostHogOpenAI;
616
1703
  exports.withTracing = wrapVercelLanguageModel;
617
1704
  //# sourceMappingURL=index.cjs.js.map