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