@posthog/ai 6.1.2 → 6.3.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/dist/index.cjs CHANGED
@@ -8,24 +8,26 @@ var AnthropicOriginal = require('@anthropic-ai/sdk');
8
8
  var genai = require('@google/genai');
9
9
 
10
10
  function _interopNamespaceDefault(e) {
11
- var n = Object.create(null);
12
- if (e) {
13
- Object.keys(e).forEach(function (k) {
14
- if (k !== 'default') {
15
- var d = Object.getOwnPropertyDescriptor(e, k);
16
- Object.defineProperty(n, k, d.get ? d : {
17
- enumerable: true,
18
- get: function () { return e[k]; }
19
- });
20
- }
11
+ var n = Object.create(null);
12
+ if (e) {
13
+ Object.keys(e).forEach(function (k) {
14
+ if (k !== 'default') {
15
+ var d = Object.getOwnPropertyDescriptor(e, k);
16
+ Object.defineProperty(n, k, d.get ? d : {
17
+ enumerable: true,
18
+ get: function () { return e[k]; }
21
19
  });
22
- }
23
- n.default = e;
24
- return Object.freeze(n);
20
+ }
21
+ });
22
+ }
23
+ n.default = e;
24
+ return Object.freeze(n);
25
25
  }
26
26
 
27
27
  var uuid__namespace = /*#__PURE__*/_interopNamespaceDefault(uuid);
28
28
 
29
+ var version = "6.3.0";
30
+
29
31
  // limit large outputs by truncating to 200kb (approx 200k bytes)
30
32
  const MAX_OUTPUT_SIZE = 200000;
31
33
  const STRING_FORMAT = 'utf8';
@@ -232,7 +234,7 @@ const truncate = str => {
232
234
  }
233
235
  const truncatedBuffer = buffer$1.slice(0, MAX_OUTPUT_SIZE);
234
236
  return `${truncatedBuffer.toString(STRING_FORMAT)}... [truncated]`;
235
- } catch (error) {
237
+ } catch {
236
238
  console.error('Error truncating, likely not a string');
237
239
  return str;
238
240
  }
@@ -265,6 +267,11 @@ const extractAvailableToolCalls = (provider, params) => {
265
267
  }
266
268
  return null;
267
269
  };
270
+ var AIEvent;
271
+ (function (AIEvent) {
272
+ AIEvent["Generation"] = "$ai_generation";
273
+ AIEvent["Embedding"] = "$ai_embedding";
274
+ })(AIEvent || (AIEvent = {}));
268
275
  function sanitizeValues(obj) {
269
276
  if (obj === undefined || obj === null) {
270
277
  return obj;
@@ -281,6 +288,7 @@ function sanitizeValues(obj) {
281
288
  }
282
289
  const sendEventToPosthog = async ({
283
290
  client,
291
+ eventType = AIEvent.Generation,
284
292
  distinctId,
285
293
  traceId,
286
294
  model,
@@ -333,6 +341,8 @@ const sendEventToPosthog = async ({
333
341
  } : {})
334
342
  };
335
343
  const properties = {
344
+ $ai_lib: 'posthog-ai',
345
+ $ai_lib_version: version,
336
346
  $ai_provider: params.posthogProviderOverride ?? provider,
337
347
  $ai_model: params.posthogModelOverride ?? model,
338
348
  $ai_model_parameters: getModelParams(params),
@@ -340,7 +350,9 @@ const sendEventToPosthog = async ({
340
350
  $ai_output_choices: withPrivacyMode(client, params.posthogPrivacyMode ?? false, safeOutput),
341
351
  $ai_http_status: httpStatus,
342
352
  $ai_input_tokens: usage.inputTokens ?? 0,
343
- $ai_output_tokens: usage.outputTokens ?? 0,
353
+ ...(usage.outputTokens !== undefined ? {
354
+ $ai_output_tokens: usage.outputTokens
355
+ } : {}),
344
356
  ...additionalTokenValues,
345
357
  $ai_latency: latency,
346
358
  $ai_trace_id: traceId,
@@ -357,7 +369,7 @@ const sendEventToPosthog = async ({
357
369
  };
358
370
  const event = {
359
371
  distinctId: distinctId ?? traceId,
360
- event: '$ai_generation',
372
+ event: eventType,
361
373
  properties,
362
374
  groups: params.posthogGroups
363
375
  };
@@ -570,6 +582,7 @@ const sanitizeLangChain = data => {
570
582
  const Chat = openai.OpenAI.Chat;
571
583
  const Completions = Chat.Completions;
572
584
  const Responses = openai.OpenAI.Responses;
585
+ const Embeddings = openai.OpenAI.Embeddings;
573
586
  class PostHogOpenAI extends openai.OpenAI {
574
587
  constructor(config) {
575
588
  const {
@@ -580,6 +593,7 @@ class PostHogOpenAI extends openai.OpenAI {
580
593
  this.phClient = posthog;
581
594
  this.chat = new WrappedChat$1(this, this.phClient);
582
595
  this.responses = new WrappedResponses$1(this, this.phClient);
596
+ this.embeddings = new WrappedEmbeddings$1(this, this.phClient);
583
597
  }
584
598
  }
585
599
  let WrappedChat$1 = class WrappedChat extends Chat {
@@ -592,16 +606,13 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
592
606
  constructor(client, phClient) {
593
607
  super(client);
594
608
  this.phClient = phClient;
609
+ this.baseURL = client.baseURL;
595
610
  }
596
611
  // --- Implementation Signature
597
612
  create(body, options) {
598
613
  const {
599
614
  posthogDistinctId,
600
615
  posthogTraceId,
601
- posthogProperties,
602
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
603
- posthogPrivacyMode = false,
604
- posthogGroups,
605
616
  posthogCaptureImmediate,
606
617
  ...openAIParams
607
618
  } = body;
@@ -711,7 +722,7 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
711
722
  input: sanitizeOpenAI(openAIParams.messages),
712
723
  output: formattedOutput,
713
724
  latency,
714
- baseURL: this.baseURL ?? '',
725
+ baseURL: this.baseURL,
715
726
  params: body,
716
727
  httpStatus: 200,
717
728
  usage,
@@ -729,7 +740,7 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
729
740
  input: sanitizeOpenAI(openAIParams.messages),
730
741
  output: [],
731
742
  latency: 0,
732
- baseURL: this.baseURL ?? '',
743
+ baseURL: this.baseURL,
733
744
  params: body,
734
745
  httpStatus,
735
746
  usage: {
@@ -761,7 +772,7 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
761
772
  input: sanitizeOpenAI(openAIParams.messages),
762
773
  output: formatResponseOpenAI(result),
763
774
  latency,
764
- baseURL: this.baseURL ?? '',
775
+ baseURL: this.baseURL,
765
776
  params: body,
766
777
  httpStatus: 200,
767
778
  usage: {
@@ -786,7 +797,7 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
786
797
  input: sanitizeOpenAI(openAIParams.messages),
787
798
  output: [],
788
799
  latency: 0,
789
- baseURL: this.baseURL ?? '',
800
+ baseURL: this.baseURL,
790
801
  params: body,
791
802
  httpStatus,
792
803
  usage: {
@@ -807,16 +818,13 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
807
818
  constructor(client, phClient) {
808
819
  super(client);
809
820
  this.phClient = phClient;
821
+ this.baseURL = client.baseURL;
810
822
  }
811
823
  // --- Implementation Signature
812
824
  create(body, options) {
813
825
  const {
814
826
  posthogDistinctId,
815
827
  posthogTraceId,
816
- posthogProperties,
817
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
818
- posthogPrivacyMode = false,
819
- posthogGroups,
820
828
  posthogCaptureImmediate,
821
829
  ...openAIParams
822
830
  } = body;
@@ -859,7 +867,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
859
867
  input: sanitizeOpenAIResponse(openAIParams.input),
860
868
  output: finalContent,
861
869
  latency,
862
- baseURL: this.baseURL ?? '',
870
+ baseURL: this.baseURL,
863
871
  params: body,
864
872
  httpStatus: 200,
865
873
  usage,
@@ -878,7 +886,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
878
886
  input: sanitizeOpenAIResponse(openAIParams.input),
879
887
  output: [],
880
888
  latency: 0,
881
- baseURL: this.baseURL ?? '',
889
+ baseURL: this.baseURL,
882
890
  params: body,
883
891
  httpStatus,
884
892
  usage: {
@@ -912,7 +920,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
912
920
  output: result.output
913
921
  }),
914
922
  latency,
915
- baseURL: this.baseURL ?? '',
923
+ baseURL: this.baseURL,
916
924
  params: body,
917
925
  httpStatus: 200,
918
926
  usage: {
@@ -938,7 +946,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
938
946
  input: sanitizeOpenAIResponse(openAIParams.input),
939
947
  output: [],
940
948
  latency: 0,
941
- baseURL: this.baseURL ?? '',
949
+ baseURL: this.baseURL,
942
950
  params: body,
943
951
  httpStatus,
944
952
  usage: {
@@ -958,10 +966,6 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
958
966
  const {
959
967
  posthogDistinctId,
960
968
  posthogTraceId,
961
- posthogProperties,
962
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
963
- posthogPrivacyMode = false,
964
- posthogGroups,
965
969
  posthogCaptureImmediate,
966
970
  ...openAIParams
967
971
  } = body;
@@ -986,7 +990,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
986
990
  input: sanitizeOpenAIResponse(openAIParams.input),
987
991
  output: result.output,
988
992
  latency,
989
- baseURL: this.baseURL ?? '',
993
+ baseURL: this.baseURL,
990
994
  params: body,
991
995
  httpStatus: 200,
992
996
  usage: {
@@ -1010,7 +1014,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
1010
1014
  input: sanitizeOpenAIResponse(openAIParams.input),
1011
1015
  output: [],
1012
1016
  latency: 0,
1013
- baseURL: this.baseURL ?? '',
1017
+ baseURL: this.baseURL,
1014
1018
  params: body,
1015
1019
  httpStatus,
1016
1020
  usage: {
@@ -1030,6 +1034,73 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
1030
1034
  }
1031
1035
  }
1032
1036
  };
1037
+ let WrappedEmbeddings$1 = class WrappedEmbeddings extends Embeddings {
1038
+ constructor(client, phClient) {
1039
+ super(client);
1040
+ this.phClient = phClient;
1041
+ this.baseURL = client.baseURL;
1042
+ }
1043
+ create(body, options) {
1044
+ const {
1045
+ posthogDistinctId,
1046
+ posthogTraceId,
1047
+ posthogPrivacyMode = false,
1048
+ posthogCaptureImmediate,
1049
+ ...openAIParams
1050
+ } = body;
1051
+ const traceId = posthogTraceId ?? uuid.v4();
1052
+ const startTime = Date.now();
1053
+ const parentPromise = super.create(openAIParams, options);
1054
+ const wrappedPromise = parentPromise.then(async result => {
1055
+ const latency = (Date.now() - startTime) / 1000;
1056
+ await sendEventToPosthog({
1057
+ client: this.phClient,
1058
+ eventType: AIEvent.Embedding,
1059
+ distinctId: posthogDistinctId,
1060
+ traceId,
1061
+ model: openAIParams.model,
1062
+ provider: 'openai',
1063
+ input: withPrivacyMode(this.phClient, posthogPrivacyMode, openAIParams.input),
1064
+ output: null,
1065
+ // Embeddings don't have output content
1066
+ latency,
1067
+ baseURL: this.baseURL,
1068
+ params: body,
1069
+ httpStatus: 200,
1070
+ usage: {
1071
+ inputTokens: result.usage?.prompt_tokens ?? 0
1072
+ },
1073
+ captureImmediate: posthogCaptureImmediate
1074
+ });
1075
+ return result;
1076
+ }, async error => {
1077
+ const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
1078
+ await sendEventToPosthog({
1079
+ client: this.phClient,
1080
+ eventType: AIEvent.Embedding,
1081
+ distinctId: posthogDistinctId,
1082
+ traceId,
1083
+ model: openAIParams.model,
1084
+ provider: 'openai',
1085
+ input: withPrivacyMode(this.phClient, posthogPrivacyMode, openAIParams.input),
1086
+ output: null,
1087
+ // Embeddings don't have output content
1088
+ latency: 0,
1089
+ baseURL: this.baseURL,
1090
+ params: body,
1091
+ httpStatus,
1092
+ usage: {
1093
+ inputTokens: 0
1094
+ },
1095
+ isError: true,
1096
+ error: JSON.stringify(error),
1097
+ captureImmediate: posthogCaptureImmediate
1098
+ });
1099
+ throw error;
1100
+ });
1101
+ return wrappedPromise;
1102
+ }
1103
+ };
1033
1104
 
1034
1105
  class PostHogAzureOpenAI extends openai.AzureOpenAI {
1035
1106
  constructor(config) {
@@ -1040,6 +1111,7 @@ class PostHogAzureOpenAI extends openai.AzureOpenAI {
1040
1111
  super(openAIConfig);
1041
1112
  this.phClient = posthog;
1042
1113
  this.chat = new WrappedChat(this, this.phClient);
1114
+ this.embeddings = new WrappedEmbeddings(this, this.phClient);
1043
1115
  }
1044
1116
  }
1045
1117
  class WrappedChat extends openai.AzureOpenAI.Chat {
@@ -1052,16 +1124,13 @@ class WrappedCompletions extends openai.AzureOpenAI.Chat.Completions {
1052
1124
  constructor(client, phClient) {
1053
1125
  super(client);
1054
1126
  this.phClient = phClient;
1127
+ this.baseURL = client.baseURL;
1055
1128
  }
1056
1129
  // --- Implementation Signature
1057
1130
  create(body, options) {
1058
1131
  const {
1059
1132
  posthogDistinctId,
1060
1133
  posthogTraceId,
1061
- posthogProperties,
1062
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
1063
- posthogPrivacyMode = false,
1064
- posthogGroups,
1065
1134
  posthogCaptureImmediate,
1066
1135
  ...openAIParams
1067
1136
  } = body;
@@ -1170,7 +1239,7 @@ class WrappedCompletions extends openai.AzureOpenAI.Chat.Completions {
1170
1239
  input: openAIParams.messages,
1171
1240
  output: formattedOutput,
1172
1241
  latency,
1173
- baseURL: this.baseURL ?? '',
1242
+ baseURL: this.baseURL,
1174
1243
  params: body,
1175
1244
  httpStatus: 200,
1176
1245
  usage,
@@ -1187,7 +1256,7 @@ class WrappedCompletions extends openai.AzureOpenAI.Chat.Completions {
1187
1256
  input: openAIParams.messages,
1188
1257
  output: [],
1189
1258
  latency: 0,
1190
- baseURL: this.baseURL ?? '',
1259
+ baseURL: this.baseURL,
1191
1260
  params: body,
1192
1261
  httpStatus,
1193
1262
  usage: {
@@ -1218,7 +1287,7 @@ class WrappedCompletions extends openai.AzureOpenAI.Chat.Completions {
1218
1287
  input: openAIParams.messages,
1219
1288
  output: formatResponseOpenAI(result),
1220
1289
  latency,
1221
- baseURL: this.baseURL ?? '',
1290
+ baseURL: this.baseURL,
1222
1291
  params: body,
1223
1292
  httpStatus: 200,
1224
1293
  usage: {
@@ -1242,7 +1311,7 @@ class WrappedCompletions extends openai.AzureOpenAI.Chat.Completions {
1242
1311
  input: openAIParams.messages,
1243
1312
  output: [],
1244
1313
  latency: 0,
1245
- baseURL: this.baseURL ?? '',
1314
+ baseURL: this.baseURL,
1246
1315
  params: body,
1247
1316
  httpStatus,
1248
1317
  usage: {
@@ -1263,16 +1332,13 @@ class WrappedResponses extends openai.AzureOpenAI.Responses {
1263
1332
  constructor(client, phClient) {
1264
1333
  super(client);
1265
1334
  this.phClient = phClient;
1335
+ this.baseURL = client.baseURL;
1266
1336
  }
1267
1337
  // --- Implementation Signature
1268
1338
  create(body, options) {
1269
1339
  const {
1270
1340
  posthogDistinctId,
1271
1341
  posthogTraceId,
1272
- posthogProperties,
1273
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
1274
- posthogPrivacyMode = false,
1275
- posthogGroups,
1276
1342
  posthogCaptureImmediate,
1277
1343
  ...openAIParams
1278
1344
  } = body;
@@ -1314,7 +1380,7 @@ class WrappedResponses extends openai.AzureOpenAI.Responses {
1314
1380
  input: openAIParams.input,
1315
1381
  output: finalContent,
1316
1382
  latency,
1317
- baseURL: this.baseURL ?? '',
1383
+ baseURL: this.baseURL,
1318
1384
  params: body,
1319
1385
  httpStatus: 200,
1320
1386
  usage,
@@ -1332,7 +1398,7 @@ class WrappedResponses extends openai.AzureOpenAI.Responses {
1332
1398
  input: openAIParams.input,
1333
1399
  output: [],
1334
1400
  latency: 0,
1335
- baseURL: this.baseURL ?? '',
1401
+ baseURL: this.baseURL,
1336
1402
  params: body,
1337
1403
  httpStatus,
1338
1404
  usage: {
@@ -1363,7 +1429,7 @@ class WrappedResponses extends openai.AzureOpenAI.Responses {
1363
1429
  input: openAIParams.input,
1364
1430
  output: result.output,
1365
1431
  latency,
1366
- baseURL: this.baseURL ?? '',
1432
+ baseURL: this.baseURL,
1367
1433
  params: body,
1368
1434
  httpStatus: 200,
1369
1435
  usage: {
@@ -1388,7 +1454,7 @@ class WrappedResponses extends openai.AzureOpenAI.Responses {
1388
1454
  input: openAIParams.input,
1389
1455
  output: [],
1390
1456
  latency: 0,
1391
- baseURL: this.baseURL ?? '',
1457
+ baseURL: this.baseURL,
1392
1458
  params: body,
1393
1459
  httpStatus,
1394
1460
  usage: {
@@ -1408,10 +1474,6 @@ class WrappedResponses extends openai.AzureOpenAI.Responses {
1408
1474
  const {
1409
1475
  posthogDistinctId,
1410
1476
  posthogTraceId,
1411
- posthogProperties,
1412
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
1413
- posthogPrivacyMode = false,
1414
- posthogGroups,
1415
1477
  posthogCaptureImmediate,
1416
1478
  ...openAIParams
1417
1479
  } = body;
@@ -1430,7 +1492,7 @@ class WrappedResponses extends openai.AzureOpenAI.Responses {
1430
1492
  input: openAIParams.input,
1431
1493
  output: result.output,
1432
1494
  latency,
1433
- baseURL: this.baseURL ?? '',
1495
+ baseURL: this.baseURL,
1434
1496
  params: body,
1435
1497
  httpStatus: 200,
1436
1498
  usage: {
@@ -1453,7 +1515,7 @@ class WrappedResponses extends openai.AzureOpenAI.Responses {
1453
1515
  input: openAIParams.input,
1454
1516
  output: [],
1455
1517
  latency: 0,
1456
- baseURL: this.baseURL ?? '',
1518
+ baseURL: this.baseURL,
1457
1519
  params: body,
1458
1520
  httpStatus: error?.status ? error.status : 500,
1459
1521
  usage: {
@@ -1469,6 +1531,72 @@ class WrappedResponses extends openai.AzureOpenAI.Responses {
1469
1531
  return wrappedPromise;
1470
1532
  }
1471
1533
  }
1534
+ class WrappedEmbeddings extends openai.AzureOpenAI.Embeddings {
1535
+ constructor(client, phClient) {
1536
+ super(client);
1537
+ this.phClient = phClient;
1538
+ this.baseURL = client.baseURL;
1539
+ }
1540
+ create(body, options) {
1541
+ const {
1542
+ posthogDistinctId,
1543
+ posthogTraceId,
1544
+ posthogPrivacyMode = false,
1545
+ posthogCaptureImmediate,
1546
+ ...openAIParams
1547
+ } = body;
1548
+ const traceId = posthogTraceId ?? uuid.v4();
1549
+ const startTime = Date.now();
1550
+ const parentPromise = super.create(openAIParams, options);
1551
+ const wrappedPromise = parentPromise.then(async result => {
1552
+ const latency = (Date.now() - startTime) / 1000;
1553
+ await sendEventToPosthog({
1554
+ client: this.phClient,
1555
+ eventType: AIEvent.Embedding,
1556
+ distinctId: posthogDistinctId,
1557
+ traceId,
1558
+ model: openAIParams.model,
1559
+ provider: 'azure',
1560
+ input: withPrivacyMode(this.phClient, posthogPrivacyMode, openAIParams.input),
1561
+ output: null,
1562
+ // Embeddings don't have output content
1563
+ latency,
1564
+ baseURL: this.baseURL,
1565
+ params: body,
1566
+ httpStatus: 200,
1567
+ usage: {
1568
+ inputTokens: result.usage?.prompt_tokens ?? 0
1569
+ },
1570
+ captureImmediate: posthogCaptureImmediate
1571
+ });
1572
+ return result;
1573
+ }, async error => {
1574
+ const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
1575
+ await sendEventToPosthog({
1576
+ client: this.phClient,
1577
+ eventType: AIEvent.Embedding,
1578
+ distinctId: posthogDistinctId,
1579
+ traceId,
1580
+ model: openAIParams.model,
1581
+ provider: 'azure',
1582
+ input: withPrivacyMode(this.phClient, posthogPrivacyMode, openAIParams.input),
1583
+ output: null,
1584
+ latency: 0,
1585
+ baseURL: this.baseURL,
1586
+ params: body,
1587
+ httpStatus,
1588
+ usage: {
1589
+ inputTokens: 0
1590
+ },
1591
+ isError: true,
1592
+ error: JSON.stringify(error),
1593
+ captureImmediate: posthogCaptureImmediate
1594
+ });
1595
+ throw error;
1596
+ });
1597
+ return wrappedPromise;
1598
+ }
1599
+ }
1472
1600
 
1473
1601
  const mapVercelParams = params => {
1474
1602
  return {
@@ -1656,7 +1784,7 @@ const mapVercelOutput = result => {
1656
1784
  content: truncate(jsonOutput),
1657
1785
  role: 'assistant'
1658
1786
  }];
1659
- } catch (error) {
1787
+ } catch {
1660
1788
  console.error('Error stringifying output');
1661
1789
  return [];
1662
1790
  }
@@ -1931,15 +2059,12 @@ class WrappedMessages extends AnthropicOriginal.Messages {
1931
2059
  constructor(parentClient, phClient) {
1932
2060
  super(parentClient);
1933
2061
  this.phClient = phClient;
2062
+ this.baseURL = parentClient.baseURL;
1934
2063
  }
1935
2064
  create(body, options) {
1936
2065
  const {
1937
2066
  posthogDistinctId,
1938
2067
  posthogTraceId,
1939
- posthogProperties,
1940
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
1941
- posthogPrivacyMode = false,
1942
- posthogGroups,
1943
2068
  posthogCaptureImmediate,
1944
2069
  ...anthropicParams
1945
2070
  } = body;
@@ -1991,7 +2116,7 @@ class WrappedMessages extends AnthropicOriginal.Messages {
1991
2116
  // Handle text delta events
1992
2117
  if ('delta' in chunk) {
1993
2118
  if ('text' in chunk.delta) {
1994
- const delta = chunk?.delta?.text ?? '';
2119
+ const delta = chunk.delta.text;
1995
2120
  accumulatedContent += delta;
1996
2121
  if (currentTextBlock) {
1997
2122
  currentTextBlock.text += delta;
@@ -2060,7 +2185,7 @@ class WrappedMessages extends AnthropicOriginal.Messages {
2060
2185
  input: sanitizeAnthropic(mergeSystemPrompt(anthropicParams, 'anthropic')),
2061
2186
  output: formattedOutput,
2062
2187
  latency,
2063
- baseURL: this.baseURL ?? '',
2188
+ baseURL: this.baseURL,
2064
2189
  params: body,
2065
2190
  httpStatus: 200,
2066
2191
  usage,
@@ -2078,7 +2203,7 @@ class WrappedMessages extends AnthropicOriginal.Messages {
2078
2203
  input: sanitizeAnthropic(mergeSystemPrompt(anthropicParams)),
2079
2204
  output: [],
2080
2205
  latency: 0,
2081
- baseURL: this.baseURL ?? '',
2206
+ baseURL: this.baseURL,
2082
2207
  params: body,
2083
2208
  httpStatus: error?.status ? error.status : 500,
2084
2209
  usage: {
@@ -2110,7 +2235,7 @@ class WrappedMessages extends AnthropicOriginal.Messages {
2110
2235
  input: sanitizeAnthropic(mergeSystemPrompt(anthropicParams)),
2111
2236
  output: formatResponseAnthropic(result),
2112
2237
  latency,
2113
- baseURL: this.baseURL ?? '',
2238
+ baseURL: this.baseURL,
2114
2239
  params: body,
2115
2240
  httpStatus: 200,
2116
2241
  usage: {
@@ -2134,7 +2259,7 @@ class WrappedMessages extends AnthropicOriginal.Messages {
2134
2259
  input: sanitizeAnthropic(mergeSystemPrompt(anthropicParams)),
2135
2260
  output: [],
2136
2261
  latency: 0,
2137
- baseURL: this.baseURL ?? '',
2262
+ baseURL: this.baseURL,
2138
2263
  params: body,
2139
2264
  httpStatus: error?.status ? error.status : 500,
2140
2265
  usage: {
@@ -2172,8 +2297,6 @@ class WrappedModels {
2172
2297
  const {
2173
2298
  posthogDistinctId,
2174
2299
  posthogTraceId,
2175
- posthogProperties,
2176
- posthogGroups,
2177
2300
  posthogCaptureImmediate,
2178
2301
  ...geminiParams
2179
2302
  } = params;
@@ -2235,8 +2358,6 @@ class WrappedModels {
2235
2358
  const {
2236
2359
  posthogDistinctId,
2237
2360
  posthogTraceId,
2238
- posthogProperties,
2239
- posthogGroups,
2240
2361
  posthogCaptureImmediate,
2241
2362
  ...geminiParams
2242
2363
  } = params;
@@ -2959,7 +3080,7 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
2959
3080
  this.debug = options.debug || false;
2960
3081
  }
2961
3082
  // ===== CALLBACK METHODS =====
2962
- handleChainStart(chain, inputs, runId, parentRunId, tags, metadata, runType, runName) {
3083
+ handleChainStart(chain, inputs, runId, parentRunId, tags, metadata, _runType, runName) {
2963
3084
  this._logDebugEvent('on_chain_start', runId, parentRunId, {
2964
3085
  inputs,
2965
3086
  tags
@@ -2967,18 +3088,14 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
2967
3088
  this._setParentOfRun(runId, parentRunId);
2968
3089
  this._setTraceOrSpanMetadata(chain, inputs, runId, parentRunId, metadata, tags, runName);
2969
3090
  }
2970
- handleChainEnd(outputs, runId, parentRunId, tags,
2971
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
2972
- kwargs) {
3091
+ handleChainEnd(outputs, runId, parentRunId, tags, _kwargs) {
2973
3092
  this._logDebugEvent('on_chain_end', runId, parentRunId, {
2974
3093
  outputs,
2975
3094
  tags
2976
3095
  });
2977
3096
  this._popRunAndCaptureTraceOrSpan(runId, parentRunId, outputs);
2978
3097
  }
2979
- handleChainError(error, runId, parentRunId, tags,
2980
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
2981
- kwargs) {
3098
+ handleChainError(error, runId, parentRunId, tags, _kwargs) {
2982
3099
  this._logDebugEvent('on_chain_error', runId, parentRunId, {
2983
3100
  error,
2984
3101
  tags
@@ -3003,18 +3120,14 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
3003
3120
  this._setParentOfRun(runId, parentRunId);
3004
3121
  this._setLLMMetadata(serialized, runId, prompts, metadata, extraParams, runName);
3005
3122
  }
3006
- handleLLMEnd(output, runId, parentRunId, tags,
3007
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
3008
- extraParams) {
3123
+ handleLLMEnd(output, runId, parentRunId, tags, _extraParams) {
3009
3124
  this._logDebugEvent('on_llm_end', runId, parentRunId, {
3010
3125
  output,
3011
3126
  tags
3012
3127
  });
3013
3128
  this._popRunAndCaptureGeneration(runId, parentRunId, output);
3014
3129
  }
3015
- handleLLMError(err, runId, parentRunId, tags,
3016
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
3017
- extraParams) {
3130
+ handleLLMError(err, runId, parentRunId, tags, _extraParams) {
3018
3131
  this._logDebugEvent('on_llm_error', runId, parentRunId, {
3019
3132
  err,
3020
3133
  tags
@@ -3149,7 +3262,7 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
3149
3262
  _getTraceId(runId) {
3150
3263
  return this.traceId ? String(this.traceId) : this._findRootRun(runId);
3151
3264
  }
3152
- _getParentRunId(traceId, runId, parentRunId) {
3265
+ _getParentRunId(traceId, _runId, parentRunId) {
3153
3266
  // Replace the parent-run if not found in our stored parent tree.
3154
3267
  if (parentRunId && !this.parentTree[parentRunId]) {
3155
3268
  return traceId;
@@ -3174,6 +3287,8 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
3174
3287
  const eventName = parentRunId ? '$ai_span' : '$ai_trace';
3175
3288
  const latency = run.endTime ? (run.endTime - run.startTime) / 1000 : 0;
3176
3289
  const eventProperties = {
3290
+ $ai_lib: 'posthog-ai',
3291
+ $ai_lib_version: version,
3177
3292
  $ai_trace_id: traceId,
3178
3293
  $ai_input_state: withPrivacyMode(this.client, this.privacyMode, run.input),
3179
3294
  $ai_latency: latency,
@@ -3214,6 +3329,8 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
3214
3329
  _captureGeneration(traceId, runId, run, output, parentRunId) {
3215
3330
  const latency = run.endTime ? (run.endTime - run.startTime) / 1000 : 0;
3216
3331
  const eventProperties = {
3332
+ $ai_lib: 'posthog-ai',
3333
+ $ai_lib_version: version,
3217
3334
  $ai_trace_id: traceId,
3218
3335
  $ai_span_id: runId,
3219
3336
  $ai_span_name: run.name,