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