@posthog/ai 5.1.0 → 5.2.1

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 CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var OpenAIOrignal = require('openai');
5
+ var openai = require('openai');
6
6
  var uuid = require('uuid');
7
7
  var buffer = require('buffer');
8
8
  var ai = require('ai');
@@ -29,7 +29,6 @@ function _interopNamespace(e) {
29
29
  return Object.freeze(n);
30
30
  }
31
31
 
32
- var OpenAIOrignal__default = /*#__PURE__*/_interopDefaultLegacy(OpenAIOrignal);
33
32
  var uuid__namespace = /*#__PURE__*/_interopNamespace(uuid);
34
33
  var AnthropicOriginal__default = /*#__PURE__*/_interopDefaultLegacy(AnthropicOriginal);
35
34
 
@@ -208,7 +207,10 @@ const sendEventToPosthog = async ({
208
207
  }
209
208
  };
210
209
 
211
- class PostHogOpenAI extends OpenAIOrignal__default["default"] {
210
+ const Chat = openai.OpenAI.Chat;
211
+ const Completions = Chat.Completions;
212
+ const Responses = openai.OpenAI.Responses;
213
+ class PostHogOpenAI extends openai.OpenAI {
212
214
  constructor(config) {
213
215
  const {
214
216
  posthog,
@@ -220,13 +222,13 @@ class PostHogOpenAI extends OpenAIOrignal__default["default"] {
220
222
  this.responses = new WrappedResponses$1(this, this.phClient);
221
223
  }
222
224
  }
223
- class WrappedChat$1 extends OpenAIOrignal__default["default"].Chat {
225
+ class WrappedChat$1 extends Chat {
224
226
  constructor(parentClient, phClient) {
225
227
  super(parentClient);
226
228
  this.completions = new WrappedCompletions$1(parentClient, phClient);
227
229
  }
228
230
  }
229
- class WrappedCompletions$1 extends OpenAIOrignal__default["default"].Chat.Completions {
231
+ class WrappedCompletions$1 extends Completions {
230
232
  constructor(client, phClient) {
231
233
  super(client);
232
234
  this.phClient = phClient;
@@ -272,7 +274,7 @@ class WrappedCompletions$1 extends OpenAIOrignal__default["default"].Chat.Comple
272
274
  const latency = (Date.now() - startTime) / 1000;
273
275
  await sendEventToPosthog({
274
276
  client: this.phClient,
275
- distinctId: posthogDistinctId ?? traceId,
277
+ distinctId: posthogDistinctId,
276
278
  traceId,
277
279
  model: openAIParams.model,
278
280
  provider: 'openai',
@@ -291,7 +293,7 @@ class WrappedCompletions$1 extends OpenAIOrignal__default["default"].Chat.Comple
291
293
  } catch (error) {
292
294
  await sendEventToPosthog({
293
295
  client: this.phClient,
294
- distinctId: posthogDistinctId ?? traceId,
296
+ distinctId: posthogDistinctId,
295
297
  traceId,
296
298
  model: openAIParams.model,
297
299
  provider: 'openai',
@@ -322,7 +324,7 @@ class WrappedCompletions$1 extends OpenAIOrignal__default["default"].Chat.Comple
322
324
  const latency = (Date.now() - startTime) / 1000;
323
325
  await sendEventToPosthog({
324
326
  client: this.phClient,
325
- distinctId: posthogDistinctId ?? traceId,
327
+ distinctId: posthogDistinctId,
326
328
  traceId,
327
329
  model: openAIParams.model,
328
330
  provider: 'openai',
@@ -345,7 +347,7 @@ class WrappedCompletions$1 extends OpenAIOrignal__default["default"].Chat.Comple
345
347
  }, async error => {
346
348
  await sendEventToPosthog({
347
349
  client: this.phClient,
348
- distinctId: posthogDistinctId ?? traceId,
350
+ distinctId: posthogDistinctId,
349
351
  traceId,
350
352
  model: openAIParams.model,
351
353
  provider: 'openai',
@@ -369,7 +371,7 @@ class WrappedCompletions$1 extends OpenAIOrignal__default["default"].Chat.Comple
369
371
  }
370
372
  }
371
373
  }
372
- class WrappedResponses$1 extends OpenAIOrignal__default["default"].Responses {
374
+ class WrappedResponses$1 extends Responses {
373
375
  constructor(client, phClient) {
374
376
  super(client);
375
377
  this.phClient = phClient;
@@ -416,7 +418,7 @@ class WrappedResponses$1 extends OpenAIOrignal__default["default"].Responses {
416
418
  const latency = (Date.now() - startTime) / 1000;
417
419
  await sendEventToPosthog({
418
420
  client: this.phClient,
419
- distinctId: posthogDistinctId ?? traceId,
421
+ distinctId: posthogDistinctId,
420
422
  traceId,
421
423
  model: openAIParams.model,
422
424
  provider: 'openai',
@@ -432,7 +434,7 @@ class WrappedResponses$1 extends OpenAIOrignal__default["default"].Responses {
432
434
  } catch (error) {
433
435
  await sendEventToPosthog({
434
436
  client: this.phClient,
435
- distinctId: posthogDistinctId ?? traceId,
437
+ distinctId: posthogDistinctId,
436
438
  traceId,
437
439
  model: openAIParams.model,
438
440
  provider: 'openai',
@@ -462,7 +464,7 @@ class WrappedResponses$1 extends OpenAIOrignal__default["default"].Responses {
462
464
  const latency = (Date.now() - startTime) / 1000;
463
465
  await sendEventToPosthog({
464
466
  client: this.phClient,
465
- distinctId: posthogDistinctId ?? traceId,
467
+ distinctId: posthogDistinctId,
466
468
  traceId,
467
469
  model: openAIParams.model,
468
470
  provider: 'openai',
@@ -485,7 +487,7 @@ class WrappedResponses$1 extends OpenAIOrignal__default["default"].Responses {
485
487
  }, async error => {
486
488
  await sendEventToPosthog({
487
489
  client: this.phClient,
488
- distinctId: posthogDistinctId ?? traceId,
490
+ distinctId: posthogDistinctId,
489
491
  traceId,
490
492
  model: openAIParams.model,
491
493
  provider: 'openai',
@@ -532,7 +534,7 @@ class WrappedResponses$1 extends OpenAIOrignal__default["default"].Responses {
532
534
  const latency = (Date.now() - startTime) / 1000;
533
535
  await sendEventToPosthog({
534
536
  client: this.phClient,
535
- distinctId: posthogDistinctId ?? traceId,
537
+ distinctId: posthogDistinctId,
536
538
  traceId,
537
539
  model: openAIParams.model,
538
540
  provider: 'openai',
@@ -554,7 +556,7 @@ class WrappedResponses$1 extends OpenAIOrignal__default["default"].Responses {
554
556
  }, async error => {
555
557
  await sendEventToPosthog({
556
558
  client: this.phClient,
557
- distinctId: posthogDistinctId ?? traceId,
559
+ distinctId: posthogDistinctId,
558
560
  traceId,
559
561
  model: openAIParams.model,
560
562
  provider: 'openai',
@@ -582,7 +584,7 @@ class WrappedResponses$1 extends OpenAIOrignal__default["default"].Responses {
582
584
  }
583
585
  }
584
586
 
585
- class PostHogAzureOpenAI extends OpenAIOrignal.AzureOpenAI {
587
+ class PostHogAzureOpenAI extends openai.AzureOpenAI {
586
588
  constructor(config) {
587
589
  const {
588
590
  posthog,
@@ -593,13 +595,13 @@ class PostHogAzureOpenAI extends OpenAIOrignal.AzureOpenAI {
593
595
  this.chat = new WrappedChat(this, this.phClient);
594
596
  }
595
597
  }
596
- class WrappedChat extends OpenAIOrignal.AzureOpenAI.Chat {
598
+ class WrappedChat extends openai.AzureOpenAI.Chat {
597
599
  constructor(parentClient, phClient) {
598
600
  super(parentClient);
599
601
  this.completions = new WrappedCompletions(parentClient, phClient);
600
602
  }
601
603
  }
602
- class WrappedCompletions extends OpenAIOrignal.AzureOpenAI.Chat.Completions {
604
+ class WrappedCompletions extends openai.AzureOpenAI.Chat.Completions {
603
605
  constructor(client, phClient) {
604
606
  super(client);
605
607
  this.phClient = phClient;
@@ -645,7 +647,7 @@ class WrappedCompletions extends OpenAIOrignal.AzureOpenAI.Chat.Completions {
645
647
  const latency = (Date.now() - startTime) / 1000;
646
648
  await sendEventToPosthog({
647
649
  client: this.phClient,
648
- distinctId: posthogDistinctId ?? traceId,
650
+ distinctId: posthogDistinctId,
649
651
  traceId,
650
652
  model: openAIParams.model,
651
653
  provider: 'azure',
@@ -664,7 +666,7 @@ class WrappedCompletions extends OpenAIOrignal.AzureOpenAI.Chat.Completions {
664
666
  } catch (error) {
665
667
  await sendEventToPosthog({
666
668
  client: this.phClient,
667
- distinctId: posthogDistinctId ?? traceId,
669
+ distinctId: posthogDistinctId,
668
670
  traceId,
669
671
  model: openAIParams.model,
670
672
  provider: 'azure',
@@ -695,7 +697,7 @@ class WrappedCompletions extends OpenAIOrignal.AzureOpenAI.Chat.Completions {
695
697
  const latency = (Date.now() - startTime) / 1000;
696
698
  await sendEventToPosthog({
697
699
  client: this.phClient,
698
- distinctId: posthogDistinctId ?? traceId,
700
+ distinctId: posthogDistinctId,
699
701
  traceId,
700
702
  model: openAIParams.model,
701
703
  provider: 'azure',
@@ -718,7 +720,7 @@ class WrappedCompletions extends OpenAIOrignal.AzureOpenAI.Chat.Completions {
718
720
  }, async error => {
719
721
  await sendEventToPosthog({
720
722
  client: this.phClient,
721
- distinctId: posthogDistinctId ?? traceId,
723
+ distinctId: posthogDistinctId,
722
724
  traceId,
723
725
  model: openAIParams.model,
724
726
  provider: 'azure',
@@ -742,7 +744,7 @@ class WrappedCompletions extends OpenAIOrignal.AzureOpenAI.Chat.Completions {
742
744
  }
743
745
  }
744
746
  }
745
- class WrappedResponses extends OpenAIOrignal.AzureOpenAI.Responses {
747
+ class WrappedResponses extends openai.AzureOpenAI.Responses {
746
748
  constructor(client, phClient) {
747
749
  super(client);
748
750
  this.phClient = phClient;
@@ -789,7 +791,7 @@ class WrappedResponses extends OpenAIOrignal.AzureOpenAI.Responses {
789
791
  const latency = (Date.now() - startTime) / 1000;
790
792
  await sendEventToPosthog({
791
793
  client: this.phClient,
792
- distinctId: posthogDistinctId ?? traceId,
794
+ distinctId: posthogDistinctId,
793
795
  traceId,
794
796
  model: openAIParams.model,
795
797
  provider: 'azure',
@@ -805,7 +807,7 @@ class WrappedResponses extends OpenAIOrignal.AzureOpenAI.Responses {
805
807
  } catch (error) {
806
808
  await sendEventToPosthog({
807
809
  client: this.phClient,
808
- distinctId: posthogDistinctId ?? traceId,
810
+ distinctId: posthogDistinctId,
809
811
  traceId,
810
812
  model: openAIParams.model,
811
813
  provider: 'azure',
@@ -835,7 +837,7 @@ class WrappedResponses extends OpenAIOrignal.AzureOpenAI.Responses {
835
837
  const latency = (Date.now() - startTime) / 1000;
836
838
  await sendEventToPosthog({
837
839
  client: this.phClient,
838
- distinctId: posthogDistinctId ?? traceId,
840
+ distinctId: posthogDistinctId,
839
841
  traceId,
840
842
  model: openAIParams.model,
841
843
  provider: 'azure',
@@ -858,7 +860,7 @@ class WrappedResponses extends OpenAIOrignal.AzureOpenAI.Responses {
858
860
  }, async error => {
859
861
  await sendEventToPosthog({
860
862
  client: this.phClient,
861
- distinctId: posthogDistinctId ?? traceId,
863
+ distinctId: posthogDistinctId,
862
864
  traceId,
863
865
  model: openAIParams.model,
864
866
  provider: 'azure',
@@ -899,7 +901,7 @@ class WrappedResponses extends OpenAIOrignal.AzureOpenAI.Responses {
899
901
  const latency = (Date.now() - startTime) / 1000;
900
902
  await sendEventToPosthog({
901
903
  client: this.phClient,
902
- distinctId: posthogDistinctId ?? traceId,
904
+ distinctId: posthogDistinctId,
903
905
  traceId,
904
906
  model: openAIParams.model,
905
907
  provider: 'azure',
@@ -921,7 +923,7 @@ class WrappedResponses extends OpenAIOrignal.AzureOpenAI.Responses {
921
923
  }, async error => {
922
924
  await sendEventToPosthog({
923
925
  client: this.phClient,
924
- distinctId: posthogDistinctId ?? traceId,
926
+ distinctId: posthogDistinctId,
925
927
  traceId,
926
928
  model: openAIParams.model,
927
929
  provider: 'azure',
@@ -1154,7 +1156,7 @@ const createInstrumentationMiddleware = (phClient, model, options) => {
1154
1156
  await sendEventToPosthog({
1155
1157
  client: phClient,
1156
1158
  distinctId: options.posthogDistinctId,
1157
- traceId: options.posthogTraceId,
1159
+ traceId: options.posthogTraceId ?? uuid.v4(),
1158
1160
  model: modelId,
1159
1161
  provider: provider,
1160
1162
  input: options.posthogPrivacyMode ? '' : mapVercelPrompt(params.prompt),
@@ -1179,7 +1181,7 @@ const createInstrumentationMiddleware = (phClient, model, options) => {
1179
1181
  await sendEventToPosthog({
1180
1182
  client: phClient,
1181
1183
  distinctId: options.posthogDistinctId,
1182
- traceId: options.posthogTraceId,
1184
+ traceId: options.posthogTraceId ?? uuid.v4(),
1183
1185
  model: modelId,
1184
1186
  provider: model.provider,
1185
1187
  input: options.posthogPrivacyMode ? '' : mapVercelPrompt(params.prompt),
@@ -1248,7 +1250,7 @@ const createInstrumentationMiddleware = (phClient, model, options) => {
1248
1250
  await sendEventToPosthog({
1249
1251
  client: phClient,
1250
1252
  distinctId: options.posthogDistinctId,
1251
- traceId: options.posthogTraceId,
1253
+ traceId: options.posthogTraceId ?? uuid.v4(),
1252
1254
  model: modelId,
1253
1255
  provider: provider,
1254
1256
  input: options.posthogPrivacyMode ? '' : mapVercelPrompt(params.prompt),
@@ -1273,7 +1275,7 @@ const createInstrumentationMiddleware = (phClient, model, options) => {
1273
1275
  await sendEventToPosthog({
1274
1276
  client: phClient,
1275
1277
  distinctId: options.posthogDistinctId,
1276
- traceId: options.posthogTraceId,
1278
+ traceId: options.posthogTraceId ?? uuid.v4(),
1277
1279
  model: modelId,
1278
1280
  provider: provider,
1279
1281
  input: options.posthogPrivacyMode ? '' : mapVercelPrompt(params.prompt),
@@ -1301,7 +1303,7 @@ const wrapVercelLanguageModel = (model, phClient, options) => {
1301
1303
  const middleware = createInstrumentationMiddleware(phClient, model, {
1302
1304
  ...options,
1303
1305
  posthogTraceId: traceId,
1304
- posthogDistinctId: options.posthogDistinctId ?? traceId
1306
+ posthogDistinctId: options.posthogDistinctId
1305
1307
  });
1306
1308
  const wrappedModel = ai.experimental_wrapLanguageModel({
1307
1309
  model,
@@ -1372,7 +1374,7 @@ class WrappedMessages extends AnthropicOriginal__default["default"].Messages {
1372
1374
  const latency = (Date.now() - startTime) / 1000;
1373
1375
  await sendEventToPosthog({
1374
1376
  client: this.phClient,
1375
- distinctId: posthogDistinctId ?? traceId,
1377
+ distinctId: posthogDistinctId,
1376
1378
  traceId,
1377
1379
  model: anthropicParams.model,
1378
1380
  provider: 'anthropic',
@@ -1392,7 +1394,7 @@ class WrappedMessages extends AnthropicOriginal__default["default"].Messages {
1392
1394
  // error handling
1393
1395
  await sendEventToPosthog({
1394
1396
  client: this.phClient,
1395
- distinctId: posthogDistinctId ?? traceId,
1397
+ distinctId: posthogDistinctId,
1396
1398
  traceId,
1397
1399
  model: anthropicParams.model,
1398
1400
  provider: 'anthropic',
@@ -1423,7 +1425,7 @@ class WrappedMessages extends AnthropicOriginal__default["default"].Messages {
1423
1425
  const latency = (Date.now() - startTime) / 1000;
1424
1426
  await sendEventToPosthog({
1425
1427
  client: this.phClient,
1426
- distinctId: posthogDistinctId ?? traceId,
1428
+ distinctId: posthogDistinctId,
1427
1429
  traceId,
1428
1430
  model: anthropicParams.model,
1429
1431
  provider: 'anthropic',
@@ -1446,7 +1448,7 @@ class WrappedMessages extends AnthropicOriginal__default["default"].Messages {
1446
1448
  }, async error => {
1447
1449
  await sendEventToPosthog({
1448
1450
  client: this.phClient,
1449
- distinctId: posthogDistinctId ?? traceId,
1451
+ distinctId: posthogDistinctId,
1450
1452
  traceId,
1451
1453
  model: anthropicParams.model,
1452
1454
  provider: 'anthropic',
@@ -1503,7 +1505,7 @@ class WrappedModels {
1503
1505
  const latency = (Date.now() - startTime) / 1000;
1504
1506
  await sendEventToPosthog({
1505
1507
  client: this.phClient,
1506
- distinctId: posthogDistinctId ?? traceId,
1508
+ distinctId: posthogDistinctId,
1507
1509
  traceId,
1508
1510
  model: geminiParams.model,
1509
1511
  provider: 'gemini',
@@ -1524,7 +1526,7 @@ class WrappedModels {
1524
1526
  const latency = (Date.now() - startTime) / 1000;
1525
1527
  await sendEventToPosthog({
1526
1528
  client: this.phClient,
1527
- distinctId: posthogDistinctId ?? traceId,
1529
+ distinctId: posthogDistinctId,
1528
1530
  traceId,
1529
1531
  model: geminiParams.model,
1530
1532
  provider: 'gemini',
@@ -1578,7 +1580,7 @@ class WrappedModels {
1578
1580
  const latency = (Date.now() - startTime) / 1000;
1579
1581
  await sendEventToPosthog({
1580
1582
  client: this.phClient,
1581
- distinctId: posthogDistinctId ?? traceId,
1583
+ distinctId: posthogDistinctId,
1582
1584
  traceId,
1583
1585
  model: geminiParams.model,
1584
1586
  provider: 'gemini',
@@ -1598,7 +1600,7 @@ class WrappedModels {
1598
1600
  const latency = (Date.now() - startTime) / 1000;
1599
1601
  await sendEventToPosthog({
1600
1602
  client: this.phClient,
1601
- distinctId: posthogDistinctId ?? traceId,
1603
+ distinctId: posthogDistinctId,
1602
1604
  traceId,
1603
1605
  model: geminiParams.model,
1604
1606
  provider: 'gemini',