@posthog/ai 5.0.0 → 5.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.d.ts CHANGED
@@ -1,10 +1,10 @@
1
- import OpenAIOrignal, { ClientOptions as ClientOptions$1, AzureOpenAI } from 'openai';
1
+ import OpenAIOrignal, { APIPromise, ClientOptions as ClientOptions$1, AzureOpenAI } from 'openai';
2
2
  import { PostHog } from 'posthog-node';
3
- import { RequestOptions, APIPromise } from 'openai/core';
4
3
  import { Stream } from 'openai/streaming';
4
+ import { ParsedResponse } from 'openai/resources/responses/responses';
5
5
  import { LanguageModelV1 } from 'ai';
6
6
  import AnthropicOriginal from '@anthropic-ai/sdk';
7
- import { RequestOptions as RequestOptions$1, APIPromise as APIPromise$1 } from '@anthropic-ai/sdk/core';
7
+ import { RequestOptions as RequestOptions$2, APIPromise as APIPromise$1 } from '@anthropic-ai/sdk/core';
8
8
  import { Stream as Stream$1 } from '@anthropic-ai/sdk/streaming';
9
9
  import { GoogleGenAI } from '@google/genai';
10
10
  import { BaseCallbackHandler } from '@langchain/core/callbacks/base';
@@ -36,15 +36,19 @@ type ChatCompletionChunk$1 = OpenAIOrignal.ChatCompletionChunk;
36
36
  type ChatCompletionCreateParamsBase$1 = OpenAIOrignal.Chat.Completions.ChatCompletionCreateParams;
37
37
  type ChatCompletionCreateParamsNonStreaming$1 = OpenAIOrignal.Chat.Completions.ChatCompletionCreateParamsNonStreaming;
38
38
  type ChatCompletionCreateParamsStreaming$1 = OpenAIOrignal.Chat.Completions.ChatCompletionCreateParamsStreaming;
39
-
39
+ type ResponsesCreateParamsBase = OpenAIOrignal.Responses.ResponseCreateParams;
40
+ type ResponsesCreateParamsNonStreaming = OpenAIOrignal.Responses.ResponseCreateParamsNonStreaming;
41
+ type ResponsesCreateParamsStreaming = OpenAIOrignal.Responses.ResponseCreateParamsStreaming;
40
42
  interface MonitoringOpenAIConfig$1 extends ClientOptions$1 {
41
43
  apiKey: string;
42
44
  posthog: PostHog;
43
45
  baseURL?: string;
44
46
  }
47
+ type RequestOptions$1 = Record<string, any>;
45
48
  declare class PostHogOpenAI extends OpenAIOrignal {
46
49
  private readonly phClient;
47
50
  chat: WrappedChat$1;
51
+ responses: WrappedResponses;
48
52
  constructor(config: MonitoringOpenAIConfig$1);
49
53
  }
50
54
  declare class WrappedChat$1 extends OpenAIOrignal.Chat {
@@ -54,9 +58,17 @@ declare class WrappedChat$1 extends OpenAIOrignal.Chat {
54
58
  declare class WrappedCompletions$1 extends OpenAIOrignal.Chat.Completions {
55
59
  private readonly phClient;
56
60
  constructor(client: OpenAIOrignal, phClient: PostHog);
57
- create(body: ChatCompletionCreateParamsNonStreaming$1 & MonitoringParams, options?: RequestOptions): APIPromise<ChatCompletion$1>;
58
- create(body: ChatCompletionCreateParamsStreaming$1 & MonitoringParams, options?: RequestOptions): APIPromise<Stream<ChatCompletionChunk$1>>;
59
- create(body: ChatCompletionCreateParamsBase$1 & MonitoringParams, options?: RequestOptions): APIPromise<ChatCompletion$1 | Stream<ChatCompletionChunk$1>>;
61
+ create(body: ChatCompletionCreateParamsNonStreaming$1 & MonitoringParams, options?: RequestOptions$1): APIPromise<ChatCompletion$1>;
62
+ create(body: ChatCompletionCreateParamsStreaming$1 & MonitoringParams, options?: RequestOptions$1): APIPromise<Stream<ChatCompletionChunk$1>>;
63
+ create(body: ChatCompletionCreateParamsBase$1 & MonitoringParams, options?: RequestOptions$1): APIPromise<ChatCompletion$1 | Stream<ChatCompletionChunk$1>>;
64
+ }
65
+ declare class WrappedResponses extends OpenAIOrignal.Responses {
66
+ private readonly phClient;
67
+ constructor(client: OpenAIOrignal, phClient: PostHog);
68
+ create(body: ResponsesCreateParamsNonStreaming & MonitoringParams, options?: RequestOptions$1): APIPromise<OpenAIOrignal.Responses.Response>;
69
+ create(body: ResponsesCreateParamsStreaming & MonitoringParams, options?: RequestOptions$1): APIPromise<Stream<OpenAIOrignal.Responses.ResponseStreamEvent>>;
70
+ create(body: ResponsesCreateParamsBase & MonitoringParams, options?: RequestOptions$1): APIPromise<OpenAIOrignal.Responses.Response | Stream<OpenAIOrignal.Responses.ResponseStreamEvent>>;
71
+ parse<Params extends ResponsesCreateParamsBase, ParsedT = any>(body: Params & MonitoringParams, options?: RequestOptions$1): APIPromise<ParsedResponse<ParsedT>>;
60
72
  }
61
73
 
62
74
  type ChatCompletion = OpenAIOrignal.ChatCompletion;
@@ -64,12 +76,12 @@ type ChatCompletionChunk = OpenAIOrignal.ChatCompletionChunk;
64
76
  type ChatCompletionCreateParamsBase = OpenAIOrignal.Chat.Completions.ChatCompletionCreateParams;
65
77
  type ChatCompletionCreateParamsNonStreaming = OpenAIOrignal.Chat.Completions.ChatCompletionCreateParamsNonStreaming;
66
78
  type ChatCompletionCreateParamsStreaming = OpenAIOrignal.Chat.Completions.ChatCompletionCreateParamsStreaming;
67
-
68
79
  interface MonitoringOpenAIConfig {
69
80
  apiKey: string;
70
81
  posthog: PostHog;
71
82
  baseURL?: string;
72
83
  }
84
+ type RequestOptions = Record<string, any>;
73
85
  declare class PostHogAzureOpenAI extends AzureOpenAI {
74
86
  private readonly phClient;
75
87
  chat: WrappedChat;
@@ -119,9 +131,9 @@ declare class PostHogAnthropic extends AnthropicOriginal {
119
131
  declare class WrappedMessages extends AnthropicOriginal.Messages {
120
132
  private readonly phClient;
121
133
  constructor(parentClient: PostHogAnthropic, phClient: PostHog);
122
- create(body: MessageCreateParamsNonStreaming, options?: RequestOptions$1): APIPromise$1<Message>;
123
- create(body: MessageCreateParamsStreaming & MonitoringParams, options?: RequestOptions$1): APIPromise$1<Stream$1<RawMessageStreamEvent>>;
124
- create(body: MessageCreateParamsBase & MonitoringParams, options?: RequestOptions$1): APIPromise$1<Stream$1<RawMessageStreamEvent> | Message>;
134
+ create(body: MessageCreateParamsNonStreaming, options?: RequestOptions$2): APIPromise$1<Message>;
135
+ create(body: MessageCreateParamsStreaming & MonitoringParams, options?: RequestOptions$2): APIPromise$1<Stream$1<RawMessageStreamEvent>>;
136
+ create(body: MessageCreateParamsBase & MonitoringParams, options?: RequestOptions$2): APIPromise$1<Stream$1<RawMessageStreamEvent> | Message>;
125
137
  }
126
138
 
127
139
  type GenerateContentRequest = {
package/lib/index.mjs CHANGED
@@ -190,6 +190,7 @@ class PostHogOpenAI extends OpenAIOrignal {
190
190
  super(openAIConfig);
191
191
  this.phClient = posthog;
192
192
  this.chat = new WrappedChat$1(this, this.phClient);
193
+ this.responses = new WrappedResponses$1(this, this.phClient);
193
194
  }
194
195
  }
195
196
  class WrappedChat$1 extends OpenAIOrignal.Chat {
@@ -341,6 +342,218 @@ class WrappedCompletions$1 extends OpenAIOrignal.Chat.Completions {
341
342
  }
342
343
  }
343
344
  }
345
+ class WrappedResponses$1 extends OpenAIOrignal.Responses {
346
+ constructor(client, phClient) {
347
+ super(client);
348
+ this.phClient = phClient;
349
+ }
350
+ // --- Implementation Signature
351
+ create(body, options) {
352
+ const {
353
+ posthogDistinctId,
354
+ posthogTraceId,
355
+ posthogProperties,
356
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
357
+ posthogPrivacyMode = false,
358
+ posthogGroups,
359
+ posthogCaptureImmediate,
360
+ ...openAIParams
361
+ } = body;
362
+ const traceId = posthogTraceId ?? v4();
363
+ const startTime = Date.now();
364
+ const parentPromise = super.create(openAIParams, options);
365
+ if (openAIParams.stream) {
366
+ return parentPromise.then(value => {
367
+ if ('tee' in value && typeof value.tee === 'function') {
368
+ const [stream1, stream2] = value.tee();
369
+ (async () => {
370
+ try {
371
+ let finalContent = [];
372
+ let usage = {
373
+ inputTokens: 0,
374
+ outputTokens: 0
375
+ };
376
+ for await (const chunk of stream1) {
377
+ if (chunk.type === 'response.completed' && 'response' in chunk && chunk.response?.output && chunk.response.output.length > 0) {
378
+ finalContent = chunk.response.output;
379
+ }
380
+ if ('response' in chunk && chunk.response?.usage) {
381
+ usage = {
382
+ inputTokens: chunk.response.usage.input_tokens ?? 0,
383
+ outputTokens: chunk.response.usage.output_tokens ?? 0,
384
+ reasoningTokens: chunk.response.usage.output_tokens_details?.reasoning_tokens ?? 0,
385
+ cacheReadInputTokens: chunk.response.usage.input_tokens_details?.cached_tokens ?? 0
386
+ };
387
+ }
388
+ }
389
+ const latency = (Date.now() - startTime) / 1000;
390
+ await sendEventToPosthog({
391
+ client: this.phClient,
392
+ distinctId: posthogDistinctId ?? traceId,
393
+ traceId,
394
+ model: openAIParams.model,
395
+ provider: 'openai',
396
+ input: openAIParams.input,
397
+ output: finalContent,
398
+ latency,
399
+ baseURL: this.baseURL ?? '',
400
+ params: body,
401
+ httpStatus: 200,
402
+ usage,
403
+ captureImmediate: posthogCaptureImmediate
404
+ });
405
+ } catch (error) {
406
+ await sendEventToPosthog({
407
+ client: this.phClient,
408
+ distinctId: posthogDistinctId ?? traceId,
409
+ traceId,
410
+ model: openAIParams.model,
411
+ provider: 'openai',
412
+ input: openAIParams.input,
413
+ output: [],
414
+ latency: 0,
415
+ baseURL: this.baseURL ?? '',
416
+ params: body,
417
+ httpStatus: error?.status ? error.status : 500,
418
+ usage: {
419
+ inputTokens: 0,
420
+ outputTokens: 0
421
+ },
422
+ isError: true,
423
+ error: JSON.stringify(error),
424
+ captureImmediate: posthogCaptureImmediate
425
+ });
426
+ }
427
+ })();
428
+ return stream2;
429
+ }
430
+ return value;
431
+ });
432
+ } else {
433
+ const wrappedPromise = parentPromise.then(async result => {
434
+ if ('output' in result) {
435
+ const latency = (Date.now() - startTime) / 1000;
436
+ await sendEventToPosthog({
437
+ client: this.phClient,
438
+ distinctId: posthogDistinctId ?? traceId,
439
+ traceId,
440
+ model: openAIParams.model,
441
+ provider: 'openai',
442
+ input: openAIParams.input,
443
+ output: result.output,
444
+ latency,
445
+ baseURL: this.baseURL ?? '',
446
+ params: body,
447
+ httpStatus: 200,
448
+ usage: {
449
+ inputTokens: result.usage?.input_tokens ?? 0,
450
+ outputTokens: result.usage?.output_tokens ?? 0,
451
+ reasoningTokens: result.usage?.output_tokens_details?.reasoning_tokens ?? 0,
452
+ cacheReadInputTokens: result.usage?.input_tokens_details?.cached_tokens ?? 0
453
+ },
454
+ captureImmediate: posthogCaptureImmediate
455
+ });
456
+ }
457
+ return result;
458
+ }, async error => {
459
+ await sendEventToPosthog({
460
+ client: this.phClient,
461
+ distinctId: posthogDistinctId ?? traceId,
462
+ traceId,
463
+ model: openAIParams.model,
464
+ provider: 'openai',
465
+ input: openAIParams.input,
466
+ output: [],
467
+ latency: 0,
468
+ baseURL: this.baseURL ?? '',
469
+ params: body,
470
+ httpStatus: error?.status ? error.status : 500,
471
+ usage: {
472
+ inputTokens: 0,
473
+ outputTokens: 0
474
+ },
475
+ isError: true,
476
+ error: JSON.stringify(error),
477
+ captureImmediate: posthogCaptureImmediate
478
+ });
479
+ throw error;
480
+ });
481
+ return wrappedPromise;
482
+ }
483
+ }
484
+ parse(body, options) {
485
+ const {
486
+ posthogDistinctId,
487
+ posthogTraceId,
488
+ posthogProperties,
489
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
490
+ posthogPrivacyMode = false,
491
+ posthogGroups,
492
+ posthogCaptureImmediate,
493
+ ...openAIParams
494
+ } = body;
495
+ const traceId = posthogTraceId ?? v4();
496
+ const startTime = Date.now();
497
+ // Create a temporary instance that bypasses our wrapped create method
498
+ const originalCreate = super.create.bind(this);
499
+ const originalSelf = this;
500
+ const tempCreate = originalSelf.create;
501
+ originalSelf.create = originalCreate;
502
+ try {
503
+ const parentPromise = super.parse(openAIParams, options);
504
+ const wrappedPromise = parentPromise.then(async result => {
505
+ const latency = (Date.now() - startTime) / 1000;
506
+ await sendEventToPosthog({
507
+ client: this.phClient,
508
+ distinctId: posthogDistinctId ?? traceId,
509
+ traceId,
510
+ model: openAIParams.model,
511
+ provider: 'openai',
512
+ input: openAIParams.input,
513
+ output: result.output,
514
+ latency,
515
+ baseURL: this.baseURL ?? '',
516
+ params: body,
517
+ httpStatus: 200,
518
+ usage: {
519
+ inputTokens: result.usage?.input_tokens ?? 0,
520
+ outputTokens: result.usage?.output_tokens ?? 0,
521
+ reasoningTokens: result.usage?.output_tokens_details?.reasoning_tokens ?? 0,
522
+ cacheReadInputTokens: result.usage?.input_tokens_details?.cached_tokens ?? 0
523
+ },
524
+ captureImmediate: posthogCaptureImmediate
525
+ });
526
+ return result;
527
+ }, async error => {
528
+ await sendEventToPosthog({
529
+ client: this.phClient,
530
+ distinctId: posthogDistinctId ?? traceId,
531
+ traceId,
532
+ model: openAIParams.model,
533
+ provider: 'openai',
534
+ input: openAIParams.input,
535
+ output: [],
536
+ latency: 0,
537
+ baseURL: this.baseURL ?? '',
538
+ params: body,
539
+ httpStatus: error?.status ? error.status : 500,
540
+ usage: {
541
+ inputTokens: 0,
542
+ outputTokens: 0
543
+ },
544
+ isError: true,
545
+ error: JSON.stringify(error),
546
+ captureImmediate: posthogCaptureImmediate
547
+ });
548
+ throw error;
549
+ });
550
+ return wrappedPromise;
551
+ } finally {
552
+ // Restore our wrapped create method
553
+ originalSelf.create = tempCreate;
554
+ }
555
+ }
556
+ }
344
557
 
345
558
  class PostHogAzureOpenAI extends AzureOpenAI {
346
559
  constructor(config) {
@@ -381,23 +594,19 @@ class WrappedCompletions extends AzureOpenAI.Chat.Completions {
381
594
  const parentPromise = super.create(openAIParams, options);
382
595
  if (openAIParams.stream) {
383
596
  return parentPromise.then(value => {
384
- let accumulatedContent = '';
385
- let usage = {
386
- inputTokens: 0,
387
- outputTokens: 0
388
- };
389
- let model = openAIParams.model;
390
597
  if ('tee' in value) {
391
598
  const [stream1, stream2] = value.tee();
392
599
  (async () => {
393
600
  try {
601
+ let accumulatedContent = '';
602
+ let usage = {
603
+ inputTokens: 0,
604
+ outputTokens: 0
605
+ };
394
606
  for await (const chunk of stream1) {
395
607
  const delta = chunk?.choices?.[0]?.delta?.content ?? '';
396
608
  accumulatedContent += delta;
397
609
  if (chunk.usage) {
398
- if (chunk.model != model) {
399
- model = chunk.model;
400
- }
401
610
  usage = {
402
611
  inputTokens: chunk.usage.prompt_tokens ?? 0,
403
612
  outputTokens: chunk.usage.completion_tokens ?? 0,
@@ -411,7 +620,7 @@ class WrappedCompletions extends AzureOpenAI.Chat.Completions {
411
620
  client: this.phClient,
412
621
  distinctId: posthogDistinctId ?? traceId,
413
622
  traceId,
414
- model,
623
+ model: openAIParams.model,
415
624
  provider: 'azure',
416
625
  input: openAIParams.messages,
417
626
  output: [{
@@ -426,15 +635,14 @@ class WrappedCompletions extends AzureOpenAI.Chat.Completions {
426
635
  captureImmediate: posthogCaptureImmediate
427
636
  });
428
637
  } catch (error) {
429
- // error handling
430
638
  await sendEventToPosthog({
431
639
  client: this.phClient,
432
640
  distinctId: posthogDistinctId ?? traceId,
433
641
  traceId,
434
- model,
642
+ model: openAIParams.model,
435
643
  provider: 'azure',
436
644
  input: openAIParams.messages,
437
- output: JSON.stringify(error),
645
+ output: [],
438
646
  latency: 0,
439
647
  baseURL: this.baseURL ?? '',
440
648
  params: body,
@@ -458,15 +666,11 @@ class WrappedCompletions extends AzureOpenAI.Chat.Completions {
458
666
  const wrappedPromise = parentPromise.then(async result => {
459
667
  if ('choices' in result) {
460
668
  const latency = (Date.now() - startTime) / 1000;
461
- let model = openAIParams.model;
462
- if (result.model != model) {
463
- model = result.model;
464
- }
465
669
  await sendEventToPosthog({
466
670
  client: this.phClient,
467
671
  distinctId: posthogDistinctId ?? traceId,
468
672
  traceId,
469
- model,
673
+ model: openAIParams.model,
470
674
  provider: 'azure',
471
675
  input: openAIParams.messages,
472
676
  output: formatResponseOpenAI(result),
@@ -511,6 +715,208 @@ class WrappedCompletions extends AzureOpenAI.Chat.Completions {
511
715
  }
512
716
  }
513
717
  }
718
+ class WrappedResponses extends AzureOpenAI.Responses {
719
+ constructor(client, phClient) {
720
+ super(client);
721
+ this.phClient = phClient;
722
+ }
723
+ // --- Implementation Signature
724
+ create(body, options) {
725
+ const {
726
+ posthogDistinctId,
727
+ posthogTraceId,
728
+ posthogProperties,
729
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
730
+ posthogPrivacyMode = false,
731
+ posthogGroups,
732
+ posthogCaptureImmediate,
733
+ ...openAIParams
734
+ } = body;
735
+ const traceId = posthogTraceId ?? v4();
736
+ const startTime = Date.now();
737
+ const parentPromise = super.create(openAIParams, options);
738
+ if (openAIParams.stream) {
739
+ return parentPromise.then(value => {
740
+ if ('tee' in value && typeof value.tee === 'function') {
741
+ const [stream1, stream2] = value.tee();
742
+ (async () => {
743
+ try {
744
+ let finalContent = [];
745
+ let usage = {
746
+ inputTokens: 0,
747
+ outputTokens: 0
748
+ };
749
+ for await (const chunk of stream1) {
750
+ if (chunk.type === 'response.completed' && 'response' in chunk && chunk.response?.output && chunk.response.output.length > 0) {
751
+ finalContent = chunk.response.output;
752
+ }
753
+ if ('usage' in chunk && chunk.usage) {
754
+ usage = {
755
+ inputTokens: chunk.usage.input_tokens ?? 0,
756
+ outputTokens: chunk.usage.output_tokens ?? 0,
757
+ reasoningTokens: chunk.usage.output_tokens_details?.reasoning_tokens ?? 0,
758
+ cacheReadInputTokens: chunk.usage.input_tokens_details?.cached_tokens ?? 0
759
+ };
760
+ }
761
+ }
762
+ const latency = (Date.now() - startTime) / 1000;
763
+ await sendEventToPosthog({
764
+ client: this.phClient,
765
+ distinctId: posthogDistinctId ?? traceId,
766
+ traceId,
767
+ model: openAIParams.model,
768
+ provider: 'azure',
769
+ input: openAIParams.input,
770
+ output: finalContent,
771
+ latency,
772
+ baseURL: this.baseURL ?? '',
773
+ params: body,
774
+ httpStatus: 200,
775
+ usage,
776
+ captureImmediate: posthogCaptureImmediate
777
+ });
778
+ } catch (error) {
779
+ await sendEventToPosthog({
780
+ client: this.phClient,
781
+ distinctId: posthogDistinctId ?? traceId,
782
+ traceId,
783
+ model: openAIParams.model,
784
+ provider: 'azure',
785
+ input: openAIParams.input,
786
+ output: [],
787
+ latency: 0,
788
+ baseURL: this.baseURL ?? '',
789
+ params: body,
790
+ httpStatus: error?.status ? error.status : 500,
791
+ usage: {
792
+ inputTokens: 0,
793
+ outputTokens: 0
794
+ },
795
+ isError: true,
796
+ error: JSON.stringify(error),
797
+ captureImmediate: posthogCaptureImmediate
798
+ });
799
+ }
800
+ })();
801
+ return stream2;
802
+ }
803
+ return value;
804
+ });
805
+ } else {
806
+ const wrappedPromise = parentPromise.then(async result => {
807
+ if ('output' in result) {
808
+ const latency = (Date.now() - startTime) / 1000;
809
+ await sendEventToPosthog({
810
+ client: this.phClient,
811
+ distinctId: posthogDistinctId ?? traceId,
812
+ traceId,
813
+ model: openAIParams.model,
814
+ provider: 'azure',
815
+ input: openAIParams.input,
816
+ output: result.output,
817
+ latency,
818
+ baseURL: this.baseURL ?? '',
819
+ params: body,
820
+ httpStatus: 200,
821
+ usage: {
822
+ inputTokens: result.usage?.input_tokens ?? 0,
823
+ outputTokens: result.usage?.output_tokens ?? 0,
824
+ reasoningTokens: result.usage?.output_tokens_details?.reasoning_tokens ?? 0,
825
+ cacheReadInputTokens: result.usage?.input_tokens_details?.cached_tokens ?? 0
826
+ },
827
+ captureImmediate: posthogCaptureImmediate
828
+ });
829
+ }
830
+ return result;
831
+ }, async error => {
832
+ await sendEventToPosthog({
833
+ client: this.phClient,
834
+ distinctId: posthogDistinctId ?? traceId,
835
+ traceId,
836
+ model: openAIParams.model,
837
+ provider: 'azure',
838
+ input: openAIParams.input,
839
+ output: [],
840
+ latency: 0,
841
+ baseURL: this.baseURL ?? '',
842
+ params: body,
843
+ httpStatus: error?.status ? error.status : 500,
844
+ usage: {
845
+ inputTokens: 0,
846
+ outputTokens: 0
847
+ },
848
+ isError: true,
849
+ error: JSON.stringify(error),
850
+ captureImmediate: posthogCaptureImmediate
851
+ });
852
+ throw error;
853
+ });
854
+ return wrappedPromise;
855
+ }
856
+ }
857
+ parse(body, options) {
858
+ const {
859
+ posthogDistinctId,
860
+ posthogTraceId,
861
+ posthogProperties,
862
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
863
+ posthogPrivacyMode = false,
864
+ posthogGroups,
865
+ posthogCaptureImmediate,
866
+ ...openAIParams
867
+ } = body;
868
+ const traceId = posthogTraceId ?? v4();
869
+ const startTime = Date.now();
870
+ const parentPromise = super.parse(openAIParams, options);
871
+ const wrappedPromise = parentPromise.then(async result => {
872
+ const latency = (Date.now() - startTime) / 1000;
873
+ await sendEventToPosthog({
874
+ client: this.phClient,
875
+ distinctId: posthogDistinctId ?? traceId,
876
+ traceId,
877
+ model: openAIParams.model,
878
+ provider: 'azure',
879
+ input: openAIParams.input,
880
+ output: result.output,
881
+ latency,
882
+ baseURL: this.baseURL ?? '',
883
+ params: body,
884
+ httpStatus: 200,
885
+ usage: {
886
+ inputTokens: result.usage?.input_tokens ?? 0,
887
+ outputTokens: result.usage?.output_tokens ?? 0,
888
+ reasoningTokens: result.usage?.output_tokens_details?.reasoning_tokens ?? 0,
889
+ cacheReadInputTokens: result.usage?.input_tokens_details?.cached_tokens ?? 0
890
+ },
891
+ captureImmediate: posthogCaptureImmediate
892
+ });
893
+ return result;
894
+ }, async error => {
895
+ await sendEventToPosthog({
896
+ client: this.phClient,
897
+ distinctId: posthogDistinctId ?? traceId,
898
+ traceId,
899
+ model: openAIParams.model,
900
+ provider: 'azure',
901
+ input: openAIParams.input,
902
+ output: [],
903
+ latency: 0,
904
+ baseURL: this.baseURL ?? '',
905
+ params: body,
906
+ httpStatus: error?.status ? error.status : 500,
907
+ usage: {
908
+ inputTokens: 0,
909
+ outputTokens: 0
910
+ },
911
+ isError: true,
912
+ error: JSON.stringify(error),
913
+ captureImmediate: posthogCaptureImmediate
914
+ });
915
+ throw error;
916
+ });
917
+ return wrappedPromise;
918
+ }
919
+ }
514
920
 
515
921
  const mapVercelParams = params => {
516
922
  return {