@posthog/ai 2.4.0 → 3.1.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,10 +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';
5
6
  import AnthropicOriginal from '@anthropic-ai/sdk';
6
7
 
7
8
  const getModelParams = params => {
9
+ if (!params) {
10
+ return {};
11
+ }
8
12
  const modelParams = {};
9
13
  const paramKeys = ['temperature', 'max_tokens', 'max_completion_tokens', 'top_p', 'frequency_penalty', 'presence_penalty', 'n', 'stop', 'stream', 'streaming'];
10
14
  for (const key of paramKeys) {
@@ -80,12 +84,22 @@ const sendEventToPosthog = ({
80
84
  $ai_error: error
81
85
  };
82
86
  }
87
+ let costOverrideData = {};
88
+ if (params.posthogCostOverride) {
89
+ const inputCostUSD = (params.posthogCostOverride.inputCost ?? 0) * (usage.inputTokens ?? 0);
90
+ const outputCostUSD = (params.posthogCostOverride.outputCost ?? 0) * (usage.outputTokens ?? 0);
91
+ costOverrideData = {
92
+ $ai_input_cost_usd: inputCostUSD,
93
+ $ai_output_cost_usd: outputCostUSD,
94
+ $ai_total_cost_usd: inputCostUSD + outputCostUSD
95
+ };
96
+ }
83
97
  client.capture({
84
98
  distinctId: distinctId ?? traceId,
85
99
  event: '$ai_generation',
86
100
  properties: {
87
- $ai_provider: provider,
88
- $ai_model: model,
101
+ $ai_provider: params.posthogProviderOverride ?? provider,
102
+ $ai_model: params.posthogModelOverride ?? model,
89
103
  $ai_model_parameters: getModelParams(params),
90
104
  $ai_input: withPrivacyMode(client, params.posthogPrivacyMode ?? false, input),
91
105
  $ai_output_choices: withPrivacyMode(client, params.posthogPrivacyMode ?? false, output),
@@ -99,7 +113,8 @@ const sendEventToPosthog = ({
99
113
  ...(distinctId ? {} : {
100
114
  $process_person_profile: false
101
115
  }),
102
- ...errorData
116
+ ...errorData,
117
+ ...costOverrideData
103
118
  },
104
119
  groups: params.posthogGroups
105
120
  });
@@ -471,7 +486,7 @@ const createInstrumentationMiddleware = (phClient, model, options) => {
471
486
  params
472
487
  }) => {
473
488
  const startTime = Date.now();
474
- let mergedParams = {
489
+ const mergedParams = {
475
490
  ...options,
476
491
  ...mapVercelParams(params)
477
492
  };
@@ -481,6 +496,11 @@ const createInstrumentationMiddleware = (phClient, model, options) => {
481
496
  const modelId = options.posthogModelOverride ?? (result.response?.modelId ? result.response.modelId : model.modelId);
482
497
  const provider = options.posthogProviderOverride ?? extractProvider(model);
483
498
  const baseURL = ''; // cannot currently get baseURL from vercel
499
+ let content = result.text;
500
+ if (!content) {
501
+ // support generate Object
502
+ content = result.toolCalls?.[0].args || JSON.stringify(result);
503
+ }
484
504
  sendEventToPosthog({
485
505
  client: phClient,
486
506
  distinctId: options.posthogDistinctId,
@@ -489,7 +509,7 @@ const createInstrumentationMiddleware = (phClient, model, options) => {
489
509
  provider: provider,
490
510
  input: options.posthogPrivacyMode ? '' : mapVercelPrompt(params.prompt),
491
511
  output: [{
492
- content: result.text,
512
+ content,
493
513
  role: 'assistant'
494
514
  }],
495
515
  latency,
@@ -533,7 +553,7 @@ const createInstrumentationMiddleware = (phClient, model, options) => {
533
553
  const startTime = Date.now();
534
554
  let generatedText = '';
535
555
  let usage = {};
536
- let mergedParams = {
556
+ const mergedParams = {
537
557
  ...options,
538
558
  ...mapVercelParams(params)
539
559
  };
@@ -616,7 +636,7 @@ const wrapVercelLanguageModel = (model, phClient, options) => {
616
636
  posthogTraceId: traceId,
617
637
  posthogDistinctId: options.posthogDistinctId ?? traceId
618
638
  });
619
- const wrappedModel = experimental_wrapLanguageModel({
639
+ const wrappedModel = wrapLanguageModel({
620
640
  model,
621
641
  middleware
622
642
  });
@@ -658,7 +678,7 @@ class WrappedMessages extends AnthropicOriginal.Messages {
658
678
  objectMode: true
659
679
  });
660
680
  let accumulatedContent = '';
661
- let usage = {
681
+ const usage = {
662
682
  inputTokens: 0,
663
683
  outputTokens: 0
664
684
  };
@@ -777,5 +797,897 @@ class WrappedMessages extends AnthropicOriginal.Messages {
777
797
  }
778
798
  }
779
799
 
780
- export { PostHogAnthropic as Anthropic, PostHogAzureOpenAI as AzureOpenAI, PostHogOpenAI as OpenAI, wrapVercelLanguageModel as withTracing };
800
+ var decamelize = function (str, sep) {
801
+ if (typeof str !== 'string') {
802
+ throw new TypeError('Expected a string');
803
+ }
804
+ sep = typeof sep === 'undefined' ? '_' : sep;
805
+ return str
806
+ .replace(/([a-z\d])([A-Z])/g, '$1' + sep + '$2')
807
+ .replace(/([A-Z]+)([A-Z][a-z\d]+)/g, '$1' + sep + '$2')
808
+ .toLowerCase();
809
+ };
810
+
811
+ var camelcase = {exports: {}};
812
+
813
+ const UPPERCASE = /[\p{Lu}]/u;
814
+ const LOWERCASE = /[\p{Ll}]/u;
815
+ const LEADING_CAPITAL = /^[\p{Lu}](?![\p{Lu}])/gu;
816
+ const IDENTIFIER = /([\p{Alpha}\p{N}_]|$)/u;
817
+ const SEPARATORS = /[_.\- ]+/;
818
+ const LEADING_SEPARATORS = new RegExp('^' + SEPARATORS.source);
819
+ const SEPARATORS_AND_IDENTIFIER = new RegExp(SEPARATORS.source + IDENTIFIER.source, 'gu');
820
+ const NUMBERS_AND_IDENTIFIER = new RegExp('\\d+' + IDENTIFIER.source, 'gu');
821
+ const preserveCamelCase = (string, toLowerCase, toUpperCase) => {
822
+ let isLastCharLower = false;
823
+ let isLastCharUpper = false;
824
+ let isLastLastCharUpper = false;
825
+ for (let i = 0; i < string.length; i++) {
826
+ const character = string[i];
827
+ if (isLastCharLower && UPPERCASE.test(character)) {
828
+ string = string.slice(0, i) + '-' + string.slice(i);
829
+ isLastCharLower = false;
830
+ isLastLastCharUpper = isLastCharUpper;
831
+ isLastCharUpper = true;
832
+ i++;
833
+ }
834
+ else if (isLastCharUpper && isLastLastCharUpper && LOWERCASE.test(character)) {
835
+ string = string.slice(0, i - 1) + '-' + string.slice(i - 1);
836
+ isLastLastCharUpper = isLastCharUpper;
837
+ isLastCharUpper = false;
838
+ isLastCharLower = true;
839
+ }
840
+ else {
841
+ isLastCharLower = toLowerCase(character) === character && toUpperCase(character) !== character;
842
+ isLastLastCharUpper = isLastCharUpper;
843
+ isLastCharUpper = toUpperCase(character) === character && toLowerCase(character) !== character;
844
+ }
845
+ }
846
+ return string;
847
+ };
848
+ const preserveConsecutiveUppercase = (input, toLowerCase) => {
849
+ LEADING_CAPITAL.lastIndex = 0;
850
+ return input.replace(LEADING_CAPITAL, m1 => toLowerCase(m1));
851
+ };
852
+ const postProcess = (input, toUpperCase) => {
853
+ SEPARATORS_AND_IDENTIFIER.lastIndex = 0;
854
+ NUMBERS_AND_IDENTIFIER.lastIndex = 0;
855
+ return input.replace(SEPARATORS_AND_IDENTIFIER, (_, identifier) => toUpperCase(identifier))
856
+ .replace(NUMBERS_AND_IDENTIFIER, m => toUpperCase(m));
857
+ };
858
+ const camelCase = (input, options) => {
859
+ if (!(typeof input === 'string' || Array.isArray(input))) {
860
+ throw new TypeError('Expected the input to be `string | string[]`');
861
+ }
862
+ options = {
863
+ pascalCase: false,
864
+ preserveConsecutiveUppercase: false,
865
+ ...options
866
+ };
867
+ if (Array.isArray(input)) {
868
+ input = input.map(x => x.trim())
869
+ .filter(x => x.length)
870
+ .join('-');
871
+ }
872
+ else {
873
+ input = input.trim();
874
+ }
875
+ if (input.length === 0) {
876
+ return '';
877
+ }
878
+ const toLowerCase = options.locale === false ?
879
+ string => string.toLowerCase() :
880
+ string => string.toLocaleLowerCase(options.locale);
881
+ const toUpperCase = options.locale === false ?
882
+ string => string.toUpperCase() :
883
+ string => string.toLocaleUpperCase(options.locale);
884
+ if (input.length === 1) {
885
+ return options.pascalCase ? toUpperCase(input) : toLowerCase(input);
886
+ }
887
+ const hasUpperCase = input !== toLowerCase(input);
888
+ if (hasUpperCase) {
889
+ input = preserveCamelCase(input, toLowerCase, toUpperCase);
890
+ }
891
+ input = input.replace(LEADING_SEPARATORS, '');
892
+ if (options.preserveConsecutiveUppercase) {
893
+ input = preserveConsecutiveUppercase(input, toLowerCase);
894
+ }
895
+ else {
896
+ input = toLowerCase(input);
897
+ }
898
+ if (options.pascalCase) {
899
+ input = toUpperCase(input.charAt(0)) + input.slice(1);
900
+ }
901
+ return postProcess(input, toUpperCase);
902
+ };
903
+ camelcase.exports = camelCase;
904
+ // TODO: Remove this for the next major release
905
+ camelcase.exports.default = camelCase;
906
+
907
+ function keyToJson(key, map) {
908
+ return map?.[key] || decamelize(key);
909
+ }
910
+ function mapKeys(fields, mapper, map) {
911
+ const mapped = {};
912
+ for (const key in fields) {
913
+ if (Object.hasOwn(fields, key)) {
914
+ mapped[mapper(key, map)] = fields[key];
915
+ }
916
+ }
917
+ return mapped;
918
+ }
919
+
920
+ function shallowCopy(obj) {
921
+ return Array.isArray(obj) ? [...obj] : { ...obj };
922
+ }
923
+ function replaceSecrets(root, secretsMap) {
924
+ const result = shallowCopy(root);
925
+ for (const [path, secretId] of Object.entries(secretsMap)) {
926
+ const [last, ...partsReverse] = path.split(".").reverse();
927
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
928
+ let current = result;
929
+ for (const part of partsReverse.reverse()) {
930
+ if (current[part] === undefined) {
931
+ break;
932
+ }
933
+ current[part] = shallowCopy(current[part]);
934
+ current = current[part];
935
+ }
936
+ if (current[last] !== undefined) {
937
+ current[last] = {
938
+ lc: 1,
939
+ type: "secret",
940
+ id: [secretId],
941
+ };
942
+ }
943
+ }
944
+ return result;
945
+ }
946
+ /**
947
+ * Get a unique name for the module, rather than parent class implementations.
948
+ * Should not be subclassed, subclass lc_name above instead.
949
+ */
950
+ function get_lc_unique_name(
951
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
952
+ serializableClass) {
953
+ // "super" here would refer to the parent class of Serializable,
954
+ // when we want the parent class of the module actually calling this method.
955
+ const parentClass = Object.getPrototypeOf(serializableClass);
956
+ const lcNameIsSubclassed = typeof serializableClass.lc_name === "function" &&
957
+ (typeof parentClass.lc_name !== "function" ||
958
+ serializableClass.lc_name() !== parentClass.lc_name());
959
+ if (lcNameIsSubclassed) {
960
+ return serializableClass.lc_name();
961
+ }
962
+ else {
963
+ return serializableClass.name;
964
+ }
965
+ }
966
+ class Serializable {
967
+ /**
968
+ * The name of the serializable. Override to provide an alias or
969
+ * to preserve the serialized module name in minified environments.
970
+ *
971
+ * Implemented as a static method to support loading logic.
972
+ */
973
+ static lc_name() {
974
+ return this.name;
975
+ }
976
+ /**
977
+ * The final serialized identifier for the module.
978
+ */
979
+ get lc_id() {
980
+ return [
981
+ ...this.lc_namespace,
982
+ get_lc_unique_name(this.constructor),
983
+ ];
984
+ }
985
+ /**
986
+ * A map of secrets, which will be omitted from serialization.
987
+ * Keys are paths to the secret in constructor args, e.g. "foo.bar.baz".
988
+ * Values are the secret ids, which will be used when deserializing.
989
+ */
990
+ get lc_secrets() {
991
+ return undefined;
992
+ }
993
+ /**
994
+ * A map of additional attributes to merge with constructor args.
995
+ * Keys are the attribute names, e.g. "foo".
996
+ * Values are the attribute values, which will be serialized.
997
+ * These attributes need to be accepted by the constructor as arguments.
998
+ */
999
+ get lc_attributes() {
1000
+ return undefined;
1001
+ }
1002
+ /**
1003
+ * A map of aliases for constructor args.
1004
+ * Keys are the attribute names, e.g. "foo".
1005
+ * Values are the alias that will replace the key in serialization.
1006
+ * This is used to eg. make argument names match Python.
1007
+ */
1008
+ get lc_aliases() {
1009
+ return undefined;
1010
+ }
1011
+ constructor(kwargs, ..._args) {
1012
+ Object.defineProperty(this, "lc_serializable", {
1013
+ enumerable: true,
1014
+ configurable: true,
1015
+ writable: true,
1016
+ value: false
1017
+ });
1018
+ Object.defineProperty(this, "lc_kwargs", {
1019
+ enumerable: true,
1020
+ configurable: true,
1021
+ writable: true,
1022
+ value: void 0
1023
+ });
1024
+ this.lc_kwargs = kwargs || {};
1025
+ }
1026
+ toJSON() {
1027
+ if (!this.lc_serializable) {
1028
+ return this.toJSONNotImplemented();
1029
+ }
1030
+ if (
1031
+ // eslint-disable-next-line no-instanceof/no-instanceof
1032
+ this.lc_kwargs instanceof Serializable ||
1033
+ typeof this.lc_kwargs !== "object" ||
1034
+ Array.isArray(this.lc_kwargs)) {
1035
+ // We do not support serialization of classes with arg not a POJO
1036
+ // I'm aware the check above isn't as strict as it could be
1037
+ return this.toJSONNotImplemented();
1038
+ }
1039
+ const aliases = {};
1040
+ const secrets = {};
1041
+ const kwargs = Object.keys(this.lc_kwargs).reduce((acc, key) => {
1042
+ acc[key] = key in this ? this[key] : this.lc_kwargs[key];
1043
+ return acc;
1044
+ }, {});
1045
+ // get secrets, attributes and aliases from all superclasses
1046
+ for (
1047
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
1048
+ let current = Object.getPrototypeOf(this); current; current = Object.getPrototypeOf(current)) {
1049
+ Object.assign(aliases, Reflect.get(current, "lc_aliases", this));
1050
+ Object.assign(secrets, Reflect.get(current, "lc_secrets", this));
1051
+ Object.assign(kwargs, Reflect.get(current, "lc_attributes", this));
1052
+ }
1053
+ // include all secrets used, even if not in kwargs,
1054
+ // will be replaced with sentinel value in replaceSecrets
1055
+ Object.keys(secrets).forEach((keyPath) => {
1056
+ // eslint-disable-next-line @typescript-eslint/no-this-alias, @typescript-eslint/no-explicit-any
1057
+ let read = this;
1058
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1059
+ let write = kwargs;
1060
+ const [last, ...partsReverse] = keyPath.split(".").reverse();
1061
+ for (const key of partsReverse.reverse()) {
1062
+ if (!(key in read) || read[key] === undefined)
1063
+ return;
1064
+ if (!(key in write) || write[key] === undefined) {
1065
+ if (typeof read[key] === "object" && read[key] != null) {
1066
+ write[key] = {};
1067
+ }
1068
+ else if (Array.isArray(read[key])) {
1069
+ write[key] = [];
1070
+ }
1071
+ }
1072
+ read = read[key];
1073
+ write = write[key];
1074
+ }
1075
+ if (last in read && read[last] !== undefined) {
1076
+ write[last] = write[last] || read[last];
1077
+ }
1078
+ });
1079
+ return {
1080
+ lc: 1,
1081
+ type: "constructor",
1082
+ id: this.lc_id,
1083
+ kwargs: mapKeys(Object.keys(secrets).length ? replaceSecrets(kwargs, secrets) : kwargs, keyToJson, aliases),
1084
+ };
1085
+ }
1086
+ toJSONNotImplemented() {
1087
+ return {
1088
+ lc: 1,
1089
+ type: "not_implemented",
1090
+ id: this.lc_id,
1091
+ };
1092
+ }
1093
+ }
1094
+
1095
+ // Supabase Edge Function provides a `Deno` global object
1096
+ // without `version` property
1097
+ const isDeno = () => typeof Deno !== "undefined";
1098
+ function getEnvironmentVariable(name) {
1099
+ // Certain Deno setups will throw an error if you try to access environment variables
1100
+ // https://github.com/langchain-ai/langchainjs/issues/1412
1101
+ try {
1102
+ if (typeof process !== "undefined") {
1103
+ // eslint-disable-next-line no-process-env
1104
+ return process.env?.[name];
1105
+ }
1106
+ else if (isDeno()) {
1107
+ return Deno?.env.get(name);
1108
+ }
1109
+ else {
1110
+ return undefined;
1111
+ }
1112
+ }
1113
+ catch (e) {
1114
+ return undefined;
1115
+ }
1116
+ }
1117
+
1118
+ /**
1119
+ * Abstract class that provides a set of optional methods that can be
1120
+ * overridden in derived classes to handle various events during the
1121
+ * execution of a LangChain application.
1122
+ */
1123
+ class BaseCallbackHandlerMethodsClass {
1124
+ }
1125
+ /**
1126
+ * Abstract base class for creating callback handlers in the LangChain
1127
+ * framework. It provides a set of optional methods that can be overridden
1128
+ * in derived classes to handle various events during the execution of a
1129
+ * LangChain application.
1130
+ */
1131
+ class BaseCallbackHandler extends BaseCallbackHandlerMethodsClass {
1132
+ get lc_namespace() {
1133
+ return ["langchain_core", "callbacks", this.name];
1134
+ }
1135
+ get lc_secrets() {
1136
+ return undefined;
1137
+ }
1138
+ get lc_attributes() {
1139
+ return undefined;
1140
+ }
1141
+ get lc_aliases() {
1142
+ return undefined;
1143
+ }
1144
+ /**
1145
+ * The name of the serializable. Override to provide an alias or
1146
+ * to preserve the serialized module name in minified environments.
1147
+ *
1148
+ * Implemented as a static method to support loading logic.
1149
+ */
1150
+ static lc_name() {
1151
+ return this.name;
1152
+ }
1153
+ /**
1154
+ * The final serialized identifier for the module.
1155
+ */
1156
+ get lc_id() {
1157
+ return [
1158
+ ...this.lc_namespace,
1159
+ get_lc_unique_name(this.constructor),
1160
+ ];
1161
+ }
1162
+ constructor(input) {
1163
+ super();
1164
+ Object.defineProperty(this, "lc_serializable", {
1165
+ enumerable: true,
1166
+ configurable: true,
1167
+ writable: true,
1168
+ value: false
1169
+ });
1170
+ Object.defineProperty(this, "lc_kwargs", {
1171
+ enumerable: true,
1172
+ configurable: true,
1173
+ writable: true,
1174
+ value: void 0
1175
+ });
1176
+ Object.defineProperty(this, "ignoreLLM", {
1177
+ enumerable: true,
1178
+ configurable: true,
1179
+ writable: true,
1180
+ value: false
1181
+ });
1182
+ Object.defineProperty(this, "ignoreChain", {
1183
+ enumerable: true,
1184
+ configurable: true,
1185
+ writable: true,
1186
+ value: false
1187
+ });
1188
+ Object.defineProperty(this, "ignoreAgent", {
1189
+ enumerable: true,
1190
+ configurable: true,
1191
+ writable: true,
1192
+ value: false
1193
+ });
1194
+ Object.defineProperty(this, "ignoreRetriever", {
1195
+ enumerable: true,
1196
+ configurable: true,
1197
+ writable: true,
1198
+ value: false
1199
+ });
1200
+ Object.defineProperty(this, "ignoreCustomEvent", {
1201
+ enumerable: true,
1202
+ configurable: true,
1203
+ writable: true,
1204
+ value: false
1205
+ });
1206
+ Object.defineProperty(this, "raiseError", {
1207
+ enumerable: true,
1208
+ configurable: true,
1209
+ writable: true,
1210
+ value: false
1211
+ });
1212
+ Object.defineProperty(this, "awaitHandlers", {
1213
+ enumerable: true,
1214
+ configurable: true,
1215
+ writable: true,
1216
+ value: getEnvironmentVariable("LANGCHAIN_CALLBACKS_BACKGROUND") === "false"
1217
+ });
1218
+ this.lc_kwargs = input || {};
1219
+ if (input) {
1220
+ this.ignoreLLM = input.ignoreLLM ?? this.ignoreLLM;
1221
+ this.ignoreChain = input.ignoreChain ?? this.ignoreChain;
1222
+ this.ignoreAgent = input.ignoreAgent ?? this.ignoreAgent;
1223
+ this.ignoreRetriever = input.ignoreRetriever ?? this.ignoreRetriever;
1224
+ this.ignoreCustomEvent =
1225
+ input.ignoreCustomEvent ?? this.ignoreCustomEvent;
1226
+ this.raiseError = input.raiseError ?? this.raiseError;
1227
+ this.awaitHandlers =
1228
+ this.raiseError || (input._awaitHandler ?? this.awaitHandlers);
1229
+ }
1230
+ }
1231
+ copy() {
1232
+ return new this.constructor(this);
1233
+ }
1234
+ toJSON() {
1235
+ return Serializable.prototype.toJSON.call(this);
1236
+ }
1237
+ toJSONNotImplemented() {
1238
+ return Serializable.prototype.toJSONNotImplemented.call(this);
1239
+ }
1240
+ static fromMethods(methods) {
1241
+ class Handler extends BaseCallbackHandler {
1242
+ constructor() {
1243
+ super();
1244
+ Object.defineProperty(this, "name", {
1245
+ enumerable: true,
1246
+ configurable: true,
1247
+ writable: true,
1248
+ value: uuid.v4()
1249
+ });
1250
+ Object.assign(this, methods);
1251
+ }
1252
+ }
1253
+ return new Handler();
1254
+ }
1255
+ }
1256
+
1257
+ class LangChainCallbackHandler extends BaseCallbackHandler {
1258
+ constructor(options) {
1259
+ if (!options.client) {
1260
+ throw new Error('PostHog client is required');
1261
+ }
1262
+ super();
1263
+ this.name = 'PosthogCallbackHandler';
1264
+ this.runs = {};
1265
+ this.parentTree = {};
1266
+ this.client = options.client;
1267
+ this.distinctId = options.distinctId;
1268
+ this.traceId = options.traceId;
1269
+ this.properties = options.properties || {};
1270
+ this.privacyMode = options.privacyMode || false;
1271
+ this.groups = options.groups || {};
1272
+ this.debug = options.debug || false;
1273
+ }
1274
+ // ===== CALLBACK METHODS =====
1275
+ handleChainStart(chain, inputs, runId, parentRunId, tags, metadata, runType, runName) {
1276
+ this._logDebugEvent('on_chain_start', runId, parentRunId, {
1277
+ inputs,
1278
+ tags
1279
+ });
1280
+ this._setParentOfRun(runId, parentRunId);
1281
+ this._setTraceOrSpanMetadata(chain, inputs, runId, parentRunId, metadata, tags, runName);
1282
+ }
1283
+ handleChainEnd(outputs, runId, parentRunId, tags,
1284
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
1285
+ kwargs) {
1286
+ this._logDebugEvent('on_chain_end', runId, parentRunId, {
1287
+ outputs,
1288
+ tags
1289
+ });
1290
+ this._popRunAndCaptureTraceOrSpan(runId, parentRunId, outputs);
1291
+ }
1292
+ handleChainError(error, runId, parentRunId, tags,
1293
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
1294
+ kwargs) {
1295
+ this._logDebugEvent('on_chain_error', runId, parentRunId, {
1296
+ error,
1297
+ tags
1298
+ });
1299
+ this._popRunAndCaptureTraceOrSpan(runId, parentRunId, error);
1300
+ }
1301
+ handleChatModelStart(serialized, messages, runId, parentRunId, extraParams, tags, metadata, runName) {
1302
+ this._logDebugEvent('on_chat_model_start', runId, parentRunId, {
1303
+ messages,
1304
+ tags
1305
+ });
1306
+ this._setParentOfRun(runId, parentRunId);
1307
+ // Flatten the two-dimensional messages and convert each message to a plain object
1308
+ const input = messages.flat().map(m => this._convertMessageToDict(m));
1309
+ this._setLLMMetadata(serialized, runId, input, metadata, extraParams, runName);
1310
+ }
1311
+ handleLLMStart(serialized, prompts, runId, parentRunId, extraParams, tags, metadata, runName) {
1312
+ this._logDebugEvent('on_llm_start', runId, parentRunId, {
1313
+ prompts,
1314
+ tags
1315
+ });
1316
+ this._setParentOfRun(runId, parentRunId);
1317
+ this._setLLMMetadata(serialized, runId, prompts, metadata, extraParams, runName);
1318
+ }
1319
+ handleLLMEnd(output, runId, parentRunId, tags,
1320
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
1321
+ extraParams) {
1322
+ this._logDebugEvent('on_llm_end', runId, parentRunId, {
1323
+ output,
1324
+ tags
1325
+ });
1326
+ this._popRunAndCaptureGeneration(runId, parentRunId, output);
1327
+ }
1328
+ handleLLMError(err, runId, parentRunId, tags,
1329
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
1330
+ extraParams) {
1331
+ this._logDebugEvent('on_llm_error', runId, parentRunId, {
1332
+ err,
1333
+ tags
1334
+ });
1335
+ this._popRunAndCaptureGeneration(runId, parentRunId, err);
1336
+ }
1337
+ handleToolStart(tool, input, runId, parentRunId, tags, metadata, runName) {
1338
+ this._logDebugEvent('on_tool_start', runId, parentRunId, {
1339
+ input,
1340
+ tags
1341
+ });
1342
+ this._setParentOfRun(runId, parentRunId);
1343
+ this._setTraceOrSpanMetadata(tool, input, runId, parentRunId, metadata, tags, runName);
1344
+ }
1345
+ handleToolEnd(output, runId, parentRunId, tags) {
1346
+ this._logDebugEvent('on_tool_end', runId, parentRunId, {
1347
+ output,
1348
+ tags
1349
+ });
1350
+ this._popRunAndCaptureTraceOrSpan(runId, parentRunId, output);
1351
+ }
1352
+ handleToolError(err, runId, parentRunId, tags) {
1353
+ this._logDebugEvent('on_tool_error', runId, parentRunId, {
1354
+ err,
1355
+ tags
1356
+ });
1357
+ this._popRunAndCaptureTraceOrSpan(runId, parentRunId, err);
1358
+ }
1359
+ handleRetrieverStart(retriever, query, runId, parentRunId, tags, metadata, name) {
1360
+ this._logDebugEvent('on_retriever_start', runId, parentRunId, {
1361
+ query,
1362
+ tags
1363
+ });
1364
+ this._setParentOfRun(runId, parentRunId);
1365
+ this._setTraceOrSpanMetadata(retriever, query, runId, parentRunId, metadata, tags, name);
1366
+ }
1367
+ handleRetrieverEnd(documents, runId, parentRunId, tags) {
1368
+ this._logDebugEvent('on_retriever_end', runId, parentRunId, {
1369
+ documents,
1370
+ tags
1371
+ });
1372
+ this._popRunAndCaptureTraceOrSpan(runId, parentRunId, documents);
1373
+ }
1374
+ handleRetrieverError(err, runId, parentRunId, tags) {
1375
+ this._logDebugEvent('on_retriever_error', runId, parentRunId, {
1376
+ err,
1377
+ tags
1378
+ });
1379
+ this._popRunAndCaptureTraceOrSpan(runId, parentRunId, err);
1380
+ }
1381
+ handleAgentAction(action, runId, parentRunId, tags) {
1382
+ this._logDebugEvent('on_agent_action', runId, parentRunId, {
1383
+ action,
1384
+ tags
1385
+ });
1386
+ this._setParentOfRun(runId, parentRunId);
1387
+ this._setTraceOrSpanMetadata(null, action, runId, parentRunId);
1388
+ }
1389
+ handleAgentEnd(action, runId, parentRunId, tags) {
1390
+ this._logDebugEvent('on_agent_finish', runId, parentRunId, {
1391
+ action,
1392
+ tags
1393
+ });
1394
+ this._popRunAndCaptureTraceOrSpan(runId, parentRunId, action);
1395
+ }
1396
+ // ===== PRIVATE HELPERS =====
1397
+ _setParentOfRun(runId, parentRunId) {
1398
+ if (parentRunId) {
1399
+ this.parentTree[runId] = parentRunId;
1400
+ }
1401
+ }
1402
+ _popParentOfRun(runId) {
1403
+ delete this.parentTree[runId];
1404
+ }
1405
+ _findRootRun(runId) {
1406
+ let id = runId;
1407
+ while (this.parentTree[id]) {
1408
+ id = this.parentTree[id];
1409
+ }
1410
+ return id;
1411
+ }
1412
+ _setTraceOrSpanMetadata(serialized, input, runId, parentRunId, ...args) {
1413
+ // Use default names if not provided: if this is a top-level run, we mark it as a trace, otherwise as a span.
1414
+ const defaultName = parentRunId ? 'span' : 'trace';
1415
+ const runName = this._getLangchainRunName(serialized, ...args) || defaultName;
1416
+ this.runs[runId] = {
1417
+ name: runName,
1418
+ input,
1419
+ startTime: Date.now()
1420
+ };
1421
+ }
1422
+ _setLLMMetadata(serialized, runId, messages, metadata, extraParams, runName) {
1423
+ const runNameFound = this._getLangchainRunName(serialized, {
1424
+ extraParams,
1425
+ runName
1426
+ }) || 'generation';
1427
+ const generation = {
1428
+ name: runNameFound,
1429
+ input: messages,
1430
+ startTime: Date.now()
1431
+ };
1432
+ if (extraParams) {
1433
+ generation.modelParams = getModelParams(extraParams.invocation_params);
1434
+ }
1435
+ if (metadata) {
1436
+ if (metadata.ls_model_name) {
1437
+ generation.model = metadata.ls_model_name;
1438
+ }
1439
+ if (metadata.ls_provider) {
1440
+ generation.provider = metadata.ls_provider;
1441
+ }
1442
+ }
1443
+ if (serialized && 'kwargs' in serialized && serialized.kwargs.openai_api_base) {
1444
+ generation.baseUrl = serialized.kwargs.openai_api_base;
1445
+ }
1446
+ this.runs[runId] = generation;
1447
+ }
1448
+ _popRunMetadata(runId) {
1449
+ const endTime = Date.now();
1450
+ const run = this.runs[runId];
1451
+ if (!run) {
1452
+ console.warn(`No run metadata found for run ${runId}`);
1453
+ return undefined;
1454
+ }
1455
+ run.endTime = endTime;
1456
+ delete this.runs[runId];
1457
+ return run;
1458
+ }
1459
+ _getTraceId(runId) {
1460
+ return this.traceId ? String(this.traceId) : this._findRootRun(runId);
1461
+ }
1462
+ _getParentRunId(traceId, runId, parentRunId) {
1463
+ // Replace the parent-run if not found in our stored parent tree.
1464
+ if (parentRunId && !this.parentTree[parentRunId]) {
1465
+ return traceId;
1466
+ }
1467
+ return parentRunId;
1468
+ }
1469
+ _popRunAndCaptureTraceOrSpan(runId, parentRunId, outputs) {
1470
+ const traceId = this._getTraceId(runId);
1471
+ this._popParentOfRun(runId);
1472
+ const run = this._popRunMetadata(runId);
1473
+ if (!run) {
1474
+ return;
1475
+ }
1476
+ if ('modelParams' in run) {
1477
+ console.warn(`Run ${runId} is a generation, but attempted to be captured as a trace/span.`);
1478
+ return;
1479
+ }
1480
+ const actualParentRunId = this._getParentRunId(traceId, runId, parentRunId);
1481
+ this._captureTraceOrSpan(traceId, runId, run, outputs, actualParentRunId);
1482
+ }
1483
+ _captureTraceOrSpan(traceId, runId, run, outputs, parentRunId) {
1484
+ const eventName = parentRunId ? '$ai_span' : '$ai_trace';
1485
+ const latency = run.endTime ? (run.endTime - run.startTime) / 1000 : 0;
1486
+ const eventProperties = {
1487
+ $ai_trace_id: traceId,
1488
+ $ai_input_state: withPrivacyMode(this.client, this.privacyMode, run.input),
1489
+ $ai_latency: latency,
1490
+ $ai_span_name: run.name,
1491
+ $ai_span_id: runId
1492
+ };
1493
+ if (parentRunId) {
1494
+ eventProperties['$ai_parent_id'] = parentRunId;
1495
+ }
1496
+ Object.assign(eventProperties, this.properties);
1497
+ if (!this.distinctId) {
1498
+ eventProperties['$process_person_profile'] = false;
1499
+ }
1500
+ if (outputs instanceof Error) {
1501
+ eventProperties['$ai_error'] = outputs.toString();
1502
+ eventProperties['$ai_is_error'] = true;
1503
+ } else if (outputs !== undefined) {
1504
+ eventProperties['$ai_output_state'] = withPrivacyMode(this.client, this.privacyMode, outputs);
1505
+ }
1506
+ this.client.capture({
1507
+ distinctId: this.distinctId ? this.distinctId.toString() : runId,
1508
+ event: eventName,
1509
+ properties: eventProperties,
1510
+ groups: this.groups
1511
+ });
1512
+ }
1513
+ _popRunAndCaptureGeneration(runId, parentRunId, response) {
1514
+ const traceId = this._getTraceId(runId);
1515
+ this._popParentOfRun(runId);
1516
+ const run = this._popRunMetadata(runId);
1517
+ if (!run || typeof run !== 'object' || !('modelParams' in run)) {
1518
+ console.warn(`Run ${runId} is not a generation, but attempted to be captured as such.`);
1519
+ return;
1520
+ }
1521
+ const actualParentRunId = this._getParentRunId(traceId, runId, parentRunId);
1522
+ this._captureGeneration(traceId, runId, run, response, actualParentRunId);
1523
+ }
1524
+ _captureGeneration(traceId, runId, run, output, parentRunId) {
1525
+ const latency = run.endTime ? (run.endTime - run.startTime) / 1000 : 0;
1526
+ const eventProperties = {
1527
+ $ai_trace_id: traceId,
1528
+ $ai_span_id: runId,
1529
+ $ai_span_name: run.name,
1530
+ $ai_parent_id: parentRunId,
1531
+ $ai_provider: run.provider,
1532
+ $ai_model: run.model,
1533
+ $ai_model_parameters: run.modelParams,
1534
+ $ai_input: withPrivacyMode(this.client, this.privacyMode, run.input),
1535
+ $ai_http_status: 200,
1536
+ $ai_latency: latency,
1537
+ $ai_base_url: run.baseUrl
1538
+ };
1539
+ if (output instanceof Error) {
1540
+ eventProperties['$ai_http_status'] = output.status || 500;
1541
+ eventProperties['$ai_error'] = output.toString();
1542
+ eventProperties['$ai_is_error'] = true;
1543
+ } else {
1544
+ // Handle token usage
1545
+ const [inputTokens, outputTokens] = this.parseUsage(output);
1546
+ eventProperties['$ai_input_tokens'] = inputTokens;
1547
+ eventProperties['$ai_output_tokens'] = outputTokens;
1548
+ // Handle generations/completions
1549
+ let completions;
1550
+ if (output.generations && Array.isArray(output.generations)) {
1551
+ const lastGeneration = output.generations[output.generations.length - 1];
1552
+ if (Array.isArray(lastGeneration)) {
1553
+ completions = lastGeneration.map(gen => {
1554
+ return {
1555
+ role: 'assistant',
1556
+ content: gen.text
1557
+ };
1558
+ });
1559
+ }
1560
+ }
1561
+ if (completions) {
1562
+ eventProperties['$ai_output_choices'] = withPrivacyMode(this.client, this.privacyMode, completions);
1563
+ }
1564
+ }
1565
+ Object.assign(eventProperties, this.properties);
1566
+ if (!this.distinctId) {
1567
+ eventProperties['$process_person_profile'] = false;
1568
+ }
1569
+ this.client.capture({
1570
+ distinctId: this.distinctId ? this.distinctId.toString() : traceId,
1571
+ event: '$ai_generation',
1572
+ properties: eventProperties,
1573
+ groups: this.groups
1574
+ });
1575
+ }
1576
+ _logDebugEvent(eventName, runId, parentRunId, extra) {
1577
+ if (this.debug) {
1578
+ console.log(`Event: ${eventName}, runId: ${runId}, parentRunId: ${parentRunId}, extra:`, extra);
1579
+ }
1580
+ }
1581
+ _getLangchainRunName(serialized, ...args) {
1582
+ if (args && args.length > 0) {
1583
+ for (const arg of args) {
1584
+ if (arg && typeof arg === 'object' && 'name' in arg) {
1585
+ return arg.name;
1586
+ }
1587
+ }
1588
+ }
1589
+ if (serialized && serialized.name) {
1590
+ return serialized.name;
1591
+ }
1592
+ if (serialized && serialized.id) {
1593
+ return Array.isArray(serialized.id) ? serialized.id[serialized.id.length - 1] : serialized.id;
1594
+ }
1595
+ return undefined;
1596
+ }
1597
+ _convertMessageToDict(message) {
1598
+ let messageDict = {};
1599
+ // Check the _getType() method or type property instead of instanceof
1600
+ const messageType = message._getType?.() || message.type;
1601
+ switch (messageType) {
1602
+ case 'human':
1603
+ messageDict = {
1604
+ role: 'user',
1605
+ content: message.content
1606
+ };
1607
+ break;
1608
+ case 'ai':
1609
+ messageDict = {
1610
+ role: 'assistant',
1611
+ content: message.content
1612
+ };
1613
+ break;
1614
+ case 'system':
1615
+ messageDict = {
1616
+ role: 'system',
1617
+ content: message.content
1618
+ };
1619
+ break;
1620
+ case 'tool':
1621
+ messageDict = {
1622
+ role: 'tool',
1623
+ content: message.content
1624
+ };
1625
+ break;
1626
+ case 'function':
1627
+ messageDict = {
1628
+ role: 'function',
1629
+ content: message.content
1630
+ };
1631
+ break;
1632
+ default:
1633
+ messageDict = {
1634
+ role: messageType || 'unknown',
1635
+ content: String(message.content)
1636
+ };
1637
+ }
1638
+ if (message.additional_kwargs) {
1639
+ messageDict = {
1640
+ ...messageDict,
1641
+ ...message.additional_kwargs
1642
+ };
1643
+ }
1644
+ return messageDict;
1645
+ }
1646
+ _parseUsageModel(usage) {
1647
+ 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']];
1648
+ const parsedUsage = conversionList.reduce((acc, [modelKey, typeKey]) => {
1649
+ const value = usage[modelKey];
1650
+ if (value != null) {
1651
+ const finalCount = Array.isArray(value) ? value.reduce((sum, tokenCount) => sum + tokenCount, 0) : value;
1652
+ acc[typeKey] = finalCount;
1653
+ }
1654
+ return acc;
1655
+ }, {
1656
+ input: 0,
1657
+ output: 0
1658
+ });
1659
+ return [parsedUsage.input, parsedUsage.output];
1660
+ }
1661
+ parseUsage(response) {
1662
+ let llmUsage = [0, 0];
1663
+ const llmUsageKeys = ['token_usage', 'usage', 'tokenUsage'];
1664
+ if (response.llmOutput != null) {
1665
+ const key = llmUsageKeys.find(k => response.llmOutput?.[k] != null);
1666
+ if (key) {
1667
+ llmUsage = this._parseUsageModel(response.llmOutput[key]);
1668
+ }
1669
+ }
1670
+ // If top-level usage info was not found, try checking the generations.
1671
+ if (llmUsage[0] === 0 && llmUsage[1] === 0 && response.generations) {
1672
+ for (const generation of response.generations) {
1673
+ for (const genChunk of generation) {
1674
+ if (genChunk.generationInfo?.usage_metadata) {
1675
+ llmUsage = this._parseUsageModel(genChunk.generationInfo.usage_metadata);
1676
+ return llmUsage;
1677
+ }
1678
+ const messageChunk = genChunk.generationInfo ?? {};
1679
+ const responseMetadata = messageChunk.response_metadata ?? {};
1680
+ const chunkUsage = responseMetadata['usage'] ?? responseMetadata['amazon-bedrock-invocationMetrics'] ?? messageChunk.usage_metadata;
1681
+ if (chunkUsage) {
1682
+ llmUsage = this._parseUsageModel(chunkUsage);
1683
+ return llmUsage;
1684
+ }
1685
+ }
1686
+ }
1687
+ }
1688
+ return llmUsage;
1689
+ }
1690
+ }
1691
+
1692
+ export { PostHogAnthropic as Anthropic, PostHogAzureOpenAI as AzureOpenAI, LangChainCallbackHandler, PostHogOpenAI as OpenAI, wrapVercelLanguageModel as withTracing };
781
1693
  //# sourceMappingURL=index.esm.js.map