@posthog/ai 7.16.15 → 7.17.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/README.md +31 -0
- package/dist/anthropic/index.cjs +91 -95
- package/dist/anthropic/index.cjs.map +1 -1
- package/dist/anthropic/index.mjs +91 -95
- package/dist/anthropic/index.mjs.map +1 -1
- package/dist/gemini/index.cjs +100 -106
- package/dist/gemini/index.cjs.map +1 -1
- package/dist/gemini/index.mjs +100 -106
- package/dist/gemini/index.mjs.map +1 -1
- package/dist/index.cjs +295 -335
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +83 -2
- package/dist/index.mjs +273 -314
- package/dist/index.mjs.map +1 -1
- package/dist/langchain/index.cjs +11 -3
- package/dist/langchain/index.cjs.map +1 -1
- package/dist/langchain/index.mjs +11 -3
- package/dist/langchain/index.mjs.map +1 -1
- package/dist/openai/index.cjs +169 -185
- package/dist/openai/index.cjs.map +1 -1
- package/dist/openai/index.mjs +169 -185
- package/dist/openai/index.mjs.map +1 -1
- package/dist/openai-agents/index.cjs +14 -12
- package/dist/openai-agents/index.cjs.map +1 -1
- package/dist/openai-agents/index.mjs +14 -12
- package/dist/openai-agents/index.mjs.map +1 -1
- package/dist/vercel/index.cjs +133 -128
- package/dist/vercel/index.cjs.map +1 -1
- package/dist/vercel/index.mjs +133 -128
- package/dist/vercel/index.mjs.map +1 -1
- package/package.json +4 -4
package/dist/openai/index.cjs
CHANGED
|
@@ -6,8 +6,6 @@ var openai = require('openai');
|
|
|
6
6
|
var uuid = require('uuid');
|
|
7
7
|
var core = require('@posthog/core');
|
|
8
8
|
|
|
9
|
-
var version = "7.16.15";
|
|
10
|
-
|
|
11
9
|
// Type guards for safer type checking
|
|
12
10
|
|
|
13
11
|
const isString = value => {
|
|
@@ -168,6 +166,14 @@ function getTokensSource(posthogProperties) {
|
|
|
168
166
|
}
|
|
169
167
|
return 'sdk';
|
|
170
168
|
}
|
|
169
|
+
const STRING_FORMAT = 'utf8';
|
|
170
|
+
|
|
171
|
+
// Reused across calls to avoid per-invocation allocation; truncate() runs
|
|
172
|
+
// hundreds of times for prompts with many parts.
|
|
173
|
+
new TextEncoder();
|
|
174
|
+
new TextDecoder(STRING_FORMAT, {
|
|
175
|
+
fatal: false
|
|
176
|
+
});
|
|
171
177
|
|
|
172
178
|
/**
|
|
173
179
|
* Safely converts content to a string, preserving structure for objects/arrays.
|
|
@@ -480,73 +486,114 @@ function addDefaults(params) {
|
|
|
480
486
|
traceId: params.traceId ?? uuid.v4()
|
|
481
487
|
};
|
|
482
488
|
}
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
const properties = {
|
|
491
|
-
client,
|
|
492
|
-
traceId,
|
|
493
|
-
httpStatus,
|
|
494
|
-
error: JSON.stringify(error),
|
|
495
|
-
...args
|
|
496
|
-
};
|
|
497
|
-
const enrichedError = error;
|
|
498
|
-
if (client.options?.enableExceptionAutocapture) {
|
|
499
|
-
// assign a uuid that can be used to link the trace and exception events
|
|
500
|
-
const exceptionId = core.uuidv7();
|
|
501
|
-
client.captureException(error, undefined, {
|
|
502
|
-
$ai_trace_id: traceId
|
|
503
|
-
}, exceptionId);
|
|
504
|
-
enrichedError.__posthog_previously_captured_error = true;
|
|
505
|
-
properties.exceptionId = exceptionId;
|
|
489
|
+
function formatOpenAIResponsesInput(input, instructions) {
|
|
490
|
+
const messages = [];
|
|
491
|
+
if (instructions) {
|
|
492
|
+
messages.push({
|
|
493
|
+
role: 'system',
|
|
494
|
+
content: instructions
|
|
495
|
+
});
|
|
506
496
|
}
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
}
|
|
497
|
+
if (Array.isArray(input)) {
|
|
498
|
+
for (const item of input) {
|
|
499
|
+
if (typeof item === 'string') {
|
|
500
|
+
messages.push({
|
|
501
|
+
role: 'user',
|
|
502
|
+
content: item
|
|
503
|
+
});
|
|
504
|
+
} else if (item && typeof item === 'object') {
|
|
505
|
+
const obj = item;
|
|
506
|
+
const role = isString(obj.role) ? obj.role : 'user';
|
|
507
|
+
|
|
508
|
+
// Handle content properly - preserve structure for objects/arrays
|
|
509
|
+
const content = obj.content ?? obj.text ?? item;
|
|
510
|
+
messages.push({
|
|
511
|
+
role,
|
|
512
|
+
content: toContentString(content)
|
|
513
|
+
});
|
|
514
|
+
} else {
|
|
515
|
+
messages.push({
|
|
516
|
+
role: 'user',
|
|
517
|
+
content: toContentString(item)
|
|
518
|
+
});
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
} else if (typeof input === 'string') {
|
|
522
|
+
messages.push({
|
|
523
|
+
role: 'user',
|
|
524
|
+
content: input
|
|
525
|
+
});
|
|
526
|
+
} else if (input) {
|
|
527
|
+
messages.push({
|
|
528
|
+
role: 'user',
|
|
529
|
+
content: toContentString(input)
|
|
530
|
+
});
|
|
531
|
+
}
|
|
532
|
+
return messages;
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
var version = "7.17.1";
|
|
536
|
+
|
|
537
|
+
/**
|
|
538
|
+
* Options for `captureAiGeneration`. Mirrors the `$ai_generation` event shape
|
|
539
|
+
* directly so that any caller — first-party SDK wrappers and external code
|
|
540
|
+
* alike — produces an identical event.
|
|
541
|
+
*/
|
|
542
|
+
|
|
543
|
+
/**
|
|
544
|
+
* Capture an `$ai_generation` (or `$ai_embedding`) event to PostHog.
|
|
545
|
+
*
|
|
546
|
+
* This is the canonical primitive that every `@posthog/ai` wrapper
|
|
547
|
+
* (`withTracing`, `OpenAI`, `Anthropic`, `GoogleGenAI`, …) funnels through, so
|
|
548
|
+
* external code can use it directly to instrument LLM calls made through
|
|
549
|
+
* arbitrary clients (Cloudflare Workers AI, custom HTTP, etc.) and get the
|
|
550
|
+
* same events the SDK wrappers produce.
|
|
551
|
+
*
|
|
552
|
+
* When `error` is set, the event is captured as an error. If the error is an
|
|
553
|
+
* object, it is mutated in place to set `__posthog_previously_captured_error`
|
|
554
|
+
* so callers can re-throw the original error reference safely.
|
|
555
|
+
*/
|
|
556
|
+
const captureAiGeneration = async (client, options) => {
|
|
531
557
|
if (!client.capture) {
|
|
532
|
-
return
|
|
558
|
+
return;
|
|
533
559
|
}
|
|
534
|
-
|
|
535
|
-
const
|
|
536
|
-
const
|
|
537
|
-
const
|
|
560
|
+
const traceId = options.traceId ?? uuid.v4();
|
|
561
|
+
const eventType = options.eventType ?? AIEvent.Generation;
|
|
562
|
+
const privacyMode = options.privacyMode ?? false;
|
|
563
|
+
const usage = options.usage ?? {};
|
|
564
|
+
const safeInput = sanitizeValues(options.input);
|
|
565
|
+
const safeOutput = sanitizeValues(options.output);
|
|
566
|
+
let httpStatus = options.httpStatus;
|
|
538
567
|
let errorData = {};
|
|
539
|
-
if (error) {
|
|
568
|
+
if (options.error) {
|
|
569
|
+
if (httpStatus === undefined) {
|
|
570
|
+
if (typeof options.error === 'object' && 'status' in options.error && typeof options.error.status === 'number') {
|
|
571
|
+
httpStatus = options.error.status;
|
|
572
|
+
} else {
|
|
573
|
+
httpStatus = 500;
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
let exceptionId;
|
|
577
|
+
if (client.options?.enableExceptionAutocapture) {
|
|
578
|
+
exceptionId = core.uuidv7();
|
|
579
|
+
client.captureException(options.error, undefined, {
|
|
580
|
+
$ai_trace_id: traceId
|
|
581
|
+
}, exceptionId);
|
|
582
|
+
if (typeof options.error === 'object') {
|
|
583
|
+
options.error.__posthog_previously_captured_error = true;
|
|
584
|
+
}
|
|
585
|
+
}
|
|
540
586
|
errorData = {
|
|
541
587
|
$ai_is_error: true,
|
|
542
|
-
$ai_error:
|
|
588
|
+
$ai_error: sanitizeValues(JSON.stringify(options.error)),
|
|
543
589
|
$exception_event_id: exceptionId
|
|
544
590
|
};
|
|
545
591
|
}
|
|
592
|
+
httpStatus = httpStatus ?? 200;
|
|
546
593
|
let costOverrideData = {};
|
|
547
|
-
if (
|
|
548
|
-
const inputCostUSD = (
|
|
549
|
-
const outputCostUSD = (
|
|
594
|
+
if (options.costOverride) {
|
|
595
|
+
const inputCostUSD = (options.costOverride.inputCost ?? 0) * (usage.inputTokens ?? 0);
|
|
596
|
+
const outputCostUSD = (options.costOverride.outputCost ?? 0) * (usage.outputTokens ?? 0);
|
|
550
597
|
costOverrideData = {
|
|
551
598
|
$ai_input_cost_usd: inputCostUSD,
|
|
552
599
|
$ai_output_cost_usd: outputCostUSD,
|
|
@@ -573,96 +620,49 @@ const sendEventToPosthog = async ({
|
|
|
573
620
|
const properties = {
|
|
574
621
|
$ai_lib: 'posthog-ai',
|
|
575
622
|
$ai_lib_version: version,
|
|
576
|
-
$ai_provider:
|
|
577
|
-
$ai_model:
|
|
578
|
-
$ai_model_parameters:
|
|
579
|
-
$ai_input: withPrivacyMode(client,
|
|
580
|
-
$ai_output_choices: withPrivacyMode(client,
|
|
623
|
+
$ai_provider: options.providerOverride ?? options.provider,
|
|
624
|
+
$ai_model: options.modelOverride ?? options.model,
|
|
625
|
+
$ai_model_parameters: options.modelParameters ?? {},
|
|
626
|
+
$ai_input: withPrivacyMode(client, privacyMode, safeInput),
|
|
627
|
+
$ai_output_choices: withPrivacyMode(client, privacyMode, safeOutput),
|
|
581
628
|
$ai_http_status: httpStatus,
|
|
582
629
|
$ai_input_tokens: usage.inputTokens ?? 0,
|
|
583
630
|
...(usage.outputTokens !== undefined ? {
|
|
584
631
|
$ai_output_tokens: usage.outputTokens
|
|
585
632
|
} : {}),
|
|
586
633
|
...additionalTokenValues,
|
|
587
|
-
$ai_latency: latency,
|
|
588
|
-
...(timeToFirstToken !== undefined ? {
|
|
589
|
-
$ai_time_to_first_token: timeToFirstToken
|
|
634
|
+
$ai_latency: options.latency ?? 0,
|
|
635
|
+
...(options.timeToFirstToken !== undefined ? {
|
|
636
|
+
$ai_time_to_first_token: options.timeToFirstToken
|
|
590
637
|
} : {}),
|
|
591
638
|
$ai_trace_id: traceId,
|
|
592
|
-
$ai_base_url: baseURL,
|
|
593
|
-
...
|
|
594
|
-
$ai_tokens_source: getTokensSource(
|
|
595
|
-
...(distinctId ? {} : {
|
|
639
|
+
$ai_base_url: options.baseURL ?? '',
|
|
640
|
+
...options.properties,
|
|
641
|
+
$ai_tokens_source: getTokensSource(options.properties),
|
|
642
|
+
...(options.distinctId ? {} : {
|
|
596
643
|
$process_person_profile: false
|
|
597
644
|
}),
|
|
598
|
-
...(stopReason ? {
|
|
599
|
-
$ai_stop_reason: stopReason
|
|
645
|
+
...(options.stopReason ? {
|
|
646
|
+
$ai_stop_reason: options.stopReason
|
|
600
647
|
} : {}),
|
|
601
|
-
...(tools ? {
|
|
602
|
-
$ai_tools: tools
|
|
648
|
+
...(options.tools ? {
|
|
649
|
+
$ai_tools: options.tools
|
|
603
650
|
} : {}),
|
|
604
651
|
...errorData,
|
|
605
652
|
...costOverrideData
|
|
606
653
|
};
|
|
607
654
|
const event = {
|
|
608
|
-
distinctId: distinctId ?? traceId,
|
|
655
|
+
distinctId: options.distinctId ?? traceId,
|
|
609
656
|
event: eventType,
|
|
610
657
|
properties,
|
|
611
|
-
groups:
|
|
658
|
+
groups: options.groups
|
|
612
659
|
};
|
|
613
|
-
if (captureImmediate) {
|
|
614
|
-
// await capture promise to send single event in serverless environments
|
|
660
|
+
if (options.captureImmediate) {
|
|
615
661
|
await client.captureImmediate(event);
|
|
616
662
|
} else {
|
|
617
663
|
client.capture(event);
|
|
618
664
|
}
|
|
619
|
-
return Promise.resolve();
|
|
620
665
|
};
|
|
621
|
-
function formatOpenAIResponsesInput(input, instructions) {
|
|
622
|
-
const messages = [];
|
|
623
|
-
if (instructions) {
|
|
624
|
-
messages.push({
|
|
625
|
-
role: 'system',
|
|
626
|
-
content: instructions
|
|
627
|
-
});
|
|
628
|
-
}
|
|
629
|
-
if (Array.isArray(input)) {
|
|
630
|
-
for (const item of input) {
|
|
631
|
-
if (typeof item === 'string') {
|
|
632
|
-
messages.push({
|
|
633
|
-
role: 'user',
|
|
634
|
-
content: item
|
|
635
|
-
});
|
|
636
|
-
} else if (item && typeof item === 'object') {
|
|
637
|
-
const obj = item;
|
|
638
|
-
const role = isString(obj.role) ? obj.role : 'user';
|
|
639
|
-
|
|
640
|
-
// Handle content properly - preserve structure for objects/arrays
|
|
641
|
-
const content = obj.content ?? obj.text ?? item;
|
|
642
|
-
messages.push({
|
|
643
|
-
role,
|
|
644
|
-
content: toContentString(content)
|
|
645
|
-
});
|
|
646
|
-
} else {
|
|
647
|
-
messages.push({
|
|
648
|
-
role: 'user',
|
|
649
|
-
content: toContentString(item)
|
|
650
|
-
});
|
|
651
|
-
}
|
|
652
|
-
}
|
|
653
|
-
} else if (typeof input === 'string') {
|
|
654
|
-
messages.push({
|
|
655
|
-
role: 'user',
|
|
656
|
-
content: input
|
|
657
|
-
});
|
|
658
|
-
} else if (input) {
|
|
659
|
-
messages.push({
|
|
660
|
-
role: 'user',
|
|
661
|
-
content: toContentString(input)
|
|
662
|
-
});
|
|
663
|
-
}
|
|
664
|
-
return messages;
|
|
665
|
-
}
|
|
666
666
|
|
|
667
667
|
/**
|
|
668
668
|
* Checks if a ResponseStreamEvent chunk represents the first token/content from the model.
|
|
@@ -845,8 +845,7 @@ class WrappedCompletions extends Completions {
|
|
|
845
845
|
const latency = (Date.now() - startTime) / 1000;
|
|
846
846
|
const timeToFirstToken = firstTokenTime !== undefined ? (firstTokenTime - startTime) / 1000 : undefined;
|
|
847
847
|
const availableTools = extractAvailableToolCalls('openai', openAIParams);
|
|
848
|
-
await
|
|
849
|
-
client: this.phClient,
|
|
848
|
+
await captureAiGeneration(this.phClient, {
|
|
850
849
|
...posthogParams,
|
|
851
850
|
model: openAIParams.model ?? modelFromResponse,
|
|
852
851
|
provider: 'openai',
|
|
@@ -855,7 +854,7 @@ class WrappedCompletions extends Completions {
|
|
|
855
854
|
latency,
|
|
856
855
|
timeToFirstToken,
|
|
857
856
|
baseURL: this.baseURL,
|
|
858
|
-
|
|
857
|
+
modelParameters: getModelParams(body),
|
|
859
858
|
httpStatus: 200,
|
|
860
859
|
usage: {
|
|
861
860
|
inputTokens: usage.inputTokens,
|
|
@@ -869,8 +868,7 @@ class WrappedCompletions extends Completions {
|
|
|
869
868
|
tools: availableTools
|
|
870
869
|
});
|
|
871
870
|
} catch (error) {
|
|
872
|
-
|
|
873
|
-
client: this.phClient,
|
|
871
|
+
await captureAiGeneration(this.phClient, {
|
|
874
872
|
...posthogParams,
|
|
875
873
|
model: openAIParams.model,
|
|
876
874
|
provider: 'openai',
|
|
@@ -878,14 +876,14 @@ class WrappedCompletions extends Completions {
|
|
|
878
876
|
output: [],
|
|
879
877
|
latency: 0,
|
|
880
878
|
baseURL: this.baseURL,
|
|
881
|
-
|
|
879
|
+
modelParameters: getModelParams(body),
|
|
882
880
|
usage: {
|
|
883
881
|
inputTokens: 0,
|
|
884
882
|
outputTokens: 0
|
|
885
883
|
},
|
|
886
884
|
error
|
|
887
885
|
});
|
|
888
|
-
throw
|
|
886
|
+
throw error;
|
|
889
887
|
}
|
|
890
888
|
})();
|
|
891
889
|
|
|
@@ -900,8 +898,7 @@ class WrappedCompletions extends Completions {
|
|
|
900
898
|
const latency = (Date.now() - startTime) / 1000;
|
|
901
899
|
const availableTools = extractAvailableToolCalls('openai', openAIParams);
|
|
902
900
|
const formattedOutput = formatResponseOpenAI(result);
|
|
903
|
-
await
|
|
904
|
-
client: this.phClient,
|
|
901
|
+
await captureAiGeneration(this.phClient, {
|
|
905
902
|
...posthogParams,
|
|
906
903
|
model: openAIParams.model ?? result.model,
|
|
907
904
|
provider: 'openai',
|
|
@@ -909,7 +906,7 @@ class WrappedCompletions extends Completions {
|
|
|
909
906
|
output: formattedOutput,
|
|
910
907
|
latency,
|
|
911
908
|
baseURL: this.baseURL,
|
|
912
|
-
|
|
909
|
+
modelParameters: getModelParams(body),
|
|
913
910
|
httpStatus: 200,
|
|
914
911
|
usage: {
|
|
915
912
|
inputTokens: result.usage?.prompt_tokens ?? 0,
|
|
@@ -926,8 +923,7 @@ class WrappedCompletions extends Completions {
|
|
|
926
923
|
return result;
|
|
927
924
|
}, async error => {
|
|
928
925
|
const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
|
|
929
|
-
await
|
|
930
|
-
client: this.phClient,
|
|
926
|
+
await captureAiGeneration(this.phClient, {
|
|
931
927
|
...posthogParams,
|
|
932
928
|
model: openAIParams.model,
|
|
933
929
|
provider: 'openai',
|
|
@@ -935,13 +931,13 @@ class WrappedCompletions extends Completions {
|
|
|
935
931
|
output: [],
|
|
936
932
|
latency: 0,
|
|
937
933
|
baseURL: this.baseURL,
|
|
938
|
-
|
|
934
|
+
modelParameters: getModelParams(body),
|
|
939
935
|
httpStatus,
|
|
940
936
|
usage: {
|
|
941
937
|
inputTokens: 0,
|
|
942
938
|
outputTokens: 0
|
|
943
939
|
},
|
|
944
|
-
error
|
|
940
|
+
error
|
|
945
941
|
});
|
|
946
942
|
throw error;
|
|
947
943
|
});
|
|
@@ -1021,8 +1017,7 @@ class WrappedResponses extends Responses {
|
|
|
1021
1017
|
const latency = (Date.now() - startTime) / 1000;
|
|
1022
1018
|
const timeToFirstToken = firstTokenTime !== undefined ? (firstTokenTime - startTime) / 1000 : undefined;
|
|
1023
1019
|
const availableTools = extractAvailableToolCalls('openai', openAIParams);
|
|
1024
|
-
await
|
|
1025
|
-
client: this.phClient,
|
|
1020
|
+
await captureAiGeneration(this.phClient, {
|
|
1026
1021
|
...posthogParams,
|
|
1027
1022
|
model: openAIParams.model ?? modelFromResponse,
|
|
1028
1023
|
provider: 'openai',
|
|
@@ -1031,7 +1026,7 @@ class WrappedResponses extends Responses {
|
|
|
1031
1026
|
latency,
|
|
1032
1027
|
timeToFirstToken,
|
|
1033
1028
|
baseURL: this.baseURL,
|
|
1034
|
-
|
|
1029
|
+
modelParameters: getModelParams(body),
|
|
1035
1030
|
httpStatus: 200,
|
|
1036
1031
|
usage: {
|
|
1037
1032
|
inputTokens: usage.inputTokens,
|
|
@@ -1045,8 +1040,7 @@ class WrappedResponses extends Responses {
|
|
|
1045
1040
|
tools: availableTools
|
|
1046
1041
|
});
|
|
1047
1042
|
} catch (error) {
|
|
1048
|
-
|
|
1049
|
-
client: this.phClient,
|
|
1043
|
+
await captureAiGeneration(this.phClient, {
|
|
1050
1044
|
...posthogParams,
|
|
1051
1045
|
model: openAIParams.model,
|
|
1052
1046
|
provider: 'openai',
|
|
@@ -1054,14 +1048,14 @@ class WrappedResponses extends Responses {
|
|
|
1054
1048
|
output: [],
|
|
1055
1049
|
latency: 0,
|
|
1056
1050
|
baseURL: this.baseURL,
|
|
1057
|
-
|
|
1051
|
+
modelParameters: getModelParams(body),
|
|
1058
1052
|
usage: {
|
|
1059
1053
|
inputTokens: 0,
|
|
1060
1054
|
outputTokens: 0
|
|
1061
1055
|
},
|
|
1062
|
-
error
|
|
1056
|
+
error
|
|
1063
1057
|
});
|
|
1064
|
-
throw
|
|
1058
|
+
throw error;
|
|
1065
1059
|
}
|
|
1066
1060
|
})();
|
|
1067
1061
|
return stream2;
|
|
@@ -1076,8 +1070,7 @@ class WrappedResponses extends Responses {
|
|
|
1076
1070
|
const formattedOutput = formatResponseOpenAI({
|
|
1077
1071
|
output: result.output
|
|
1078
1072
|
});
|
|
1079
|
-
await
|
|
1080
|
-
client: this.phClient,
|
|
1073
|
+
await captureAiGeneration(this.phClient, {
|
|
1081
1074
|
...posthogParams,
|
|
1082
1075
|
model: openAIParams.model ?? result.model,
|
|
1083
1076
|
provider: 'openai',
|
|
@@ -1085,7 +1078,7 @@ class WrappedResponses extends Responses {
|
|
|
1085
1078
|
output: formattedOutput,
|
|
1086
1079
|
latency,
|
|
1087
1080
|
baseURL: this.baseURL,
|
|
1088
|
-
|
|
1081
|
+
modelParameters: getModelParams(body),
|
|
1089
1082
|
httpStatus: 200,
|
|
1090
1083
|
usage: {
|
|
1091
1084
|
inputTokens: result.usage?.input_tokens ?? 0,
|
|
@@ -1102,8 +1095,7 @@ class WrappedResponses extends Responses {
|
|
|
1102
1095
|
return result;
|
|
1103
1096
|
}, async error => {
|
|
1104
1097
|
const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
|
|
1105
|
-
await
|
|
1106
|
-
client: this.phClient,
|
|
1098
|
+
await captureAiGeneration(this.phClient, {
|
|
1107
1099
|
...posthogParams,
|
|
1108
1100
|
model: openAIParams.model,
|
|
1109
1101
|
provider: 'openai',
|
|
@@ -1111,13 +1103,13 @@ class WrappedResponses extends Responses {
|
|
|
1111
1103
|
output: [],
|
|
1112
1104
|
latency: 0,
|
|
1113
1105
|
baseURL: this.baseURL,
|
|
1114
|
-
|
|
1106
|
+
modelParameters: getModelParams(body),
|
|
1115
1107
|
httpStatus,
|
|
1116
1108
|
usage: {
|
|
1117
1109
|
inputTokens: 0,
|
|
1118
1110
|
outputTokens: 0
|
|
1119
1111
|
},
|
|
1120
|
-
error
|
|
1112
|
+
error
|
|
1121
1113
|
});
|
|
1122
1114
|
throw error;
|
|
1123
1115
|
});
|
|
@@ -1138,8 +1130,7 @@ class WrappedResponses extends Responses {
|
|
|
1138
1130
|
const parentPromise = super.parse(openAIParams, options);
|
|
1139
1131
|
const wrappedPromise = parentPromise.then(async result => {
|
|
1140
1132
|
const latency = (Date.now() - startTime) / 1000;
|
|
1141
|
-
await
|
|
1142
|
-
client: this.phClient,
|
|
1133
|
+
await captureAiGeneration(this.phClient, {
|
|
1143
1134
|
...posthogParams,
|
|
1144
1135
|
model: openAIParams.model ?? result.model,
|
|
1145
1136
|
provider: 'openai',
|
|
@@ -1147,7 +1138,7 @@ class WrappedResponses extends Responses {
|
|
|
1147
1138
|
output: result.output,
|
|
1148
1139
|
latency,
|
|
1149
1140
|
baseURL: this.baseURL,
|
|
1150
|
-
|
|
1141
|
+
modelParameters: getModelParams(body),
|
|
1151
1142
|
httpStatus: 200,
|
|
1152
1143
|
usage: {
|
|
1153
1144
|
inputTokens: result.usage?.input_tokens ?? 0,
|
|
@@ -1160,8 +1151,7 @@ class WrappedResponses extends Responses {
|
|
|
1160
1151
|
});
|
|
1161
1152
|
return result;
|
|
1162
1153
|
}, async error => {
|
|
1163
|
-
|
|
1164
|
-
client: this.phClient,
|
|
1154
|
+
await captureAiGeneration(this.phClient, {
|
|
1165
1155
|
...posthogParams,
|
|
1166
1156
|
model: openAIParams.model,
|
|
1167
1157
|
provider: 'openai',
|
|
@@ -1169,14 +1159,14 @@ class WrappedResponses extends Responses {
|
|
|
1169
1159
|
output: [],
|
|
1170
1160
|
latency: 0,
|
|
1171
1161
|
baseURL: this.baseURL,
|
|
1172
|
-
|
|
1162
|
+
modelParameters: getModelParams(body),
|
|
1173
1163
|
usage: {
|
|
1174
1164
|
inputTokens: 0,
|
|
1175
1165
|
outputTokens: 0
|
|
1176
1166
|
},
|
|
1177
|
-
error
|
|
1167
|
+
error
|
|
1178
1168
|
});
|
|
1179
|
-
throw
|
|
1169
|
+
throw error;
|
|
1180
1170
|
});
|
|
1181
1171
|
return wrappedPromise;
|
|
1182
1172
|
} finally {
|
|
@@ -1200,8 +1190,7 @@ class WrappedEmbeddings extends Embeddings {
|
|
|
1200
1190
|
const parentPromise = super.create(openAIParams, options);
|
|
1201
1191
|
const wrappedPromise = parentPromise.then(async result => {
|
|
1202
1192
|
const latency = (Date.now() - startTime) / 1000;
|
|
1203
|
-
await
|
|
1204
|
-
client: this.phClient,
|
|
1193
|
+
await captureAiGeneration(this.phClient, {
|
|
1205
1194
|
...posthogParams,
|
|
1206
1195
|
eventType: AIEvent.Embedding,
|
|
1207
1196
|
model: openAIParams.model,
|
|
@@ -1211,7 +1200,7 @@ class WrappedEmbeddings extends Embeddings {
|
|
|
1211
1200
|
// Embeddings don't have output content
|
|
1212
1201
|
latency,
|
|
1213
1202
|
baseURL: this.baseURL,
|
|
1214
|
-
|
|
1203
|
+
modelParameters: getModelParams(body),
|
|
1215
1204
|
httpStatus: 200,
|
|
1216
1205
|
usage: {
|
|
1217
1206
|
inputTokens: result.usage?.prompt_tokens ?? 0,
|
|
@@ -1221,8 +1210,7 @@ class WrappedEmbeddings extends Embeddings {
|
|
|
1221
1210
|
return result;
|
|
1222
1211
|
}, async error => {
|
|
1223
1212
|
const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
|
|
1224
|
-
await
|
|
1225
|
-
client: this.phClient,
|
|
1213
|
+
await captureAiGeneration(this.phClient, {
|
|
1226
1214
|
eventType: AIEvent.Embedding,
|
|
1227
1215
|
...posthogParams,
|
|
1228
1216
|
model: openAIParams.model,
|
|
@@ -1232,12 +1220,12 @@ class WrappedEmbeddings extends Embeddings {
|
|
|
1232
1220
|
// Embeddings don't have output content
|
|
1233
1221
|
latency: 0,
|
|
1234
1222
|
baseURL: this.baseURL,
|
|
1235
|
-
|
|
1223
|
+
modelParameters: getModelParams(body),
|
|
1236
1224
|
httpStatus,
|
|
1237
1225
|
usage: {
|
|
1238
1226
|
inputTokens: 0
|
|
1239
1227
|
},
|
|
1240
|
-
error
|
|
1228
|
+
error
|
|
1241
1229
|
});
|
|
1242
1230
|
throw error;
|
|
1243
1231
|
});
|
|
@@ -1311,8 +1299,7 @@ class WrappedTranscriptions extends Transcriptions {
|
|
|
1311
1299
|
const latency = (Date.now() - startTime) / 1000;
|
|
1312
1300
|
const timeToFirstToken = firstTokenTime !== undefined ? (firstTokenTime - startTime) / 1000 : undefined;
|
|
1313
1301
|
const availableTools = extractAvailableToolCalls('openai', openAIParams);
|
|
1314
|
-
await
|
|
1315
|
-
client: this.phClient,
|
|
1302
|
+
await captureAiGeneration(this.phClient, {
|
|
1316
1303
|
...posthogParams,
|
|
1317
1304
|
model: openAIParams.model,
|
|
1318
1305
|
provider: 'openai',
|
|
@@ -1321,14 +1308,13 @@ class WrappedTranscriptions extends Transcriptions {
|
|
|
1321
1308
|
latency,
|
|
1322
1309
|
timeToFirstToken,
|
|
1323
1310
|
baseURL: this.baseURL,
|
|
1324
|
-
|
|
1311
|
+
modelParameters: getModelParams(body),
|
|
1325
1312
|
httpStatus: 200,
|
|
1326
1313
|
usage,
|
|
1327
1314
|
tools: availableTools
|
|
1328
1315
|
});
|
|
1329
1316
|
} catch (error) {
|
|
1330
|
-
|
|
1331
|
-
client: this.phClient,
|
|
1317
|
+
await captureAiGeneration(this.phClient, {
|
|
1332
1318
|
...posthogParams,
|
|
1333
1319
|
model: openAIParams.model,
|
|
1334
1320
|
provider: 'openai',
|
|
@@ -1336,14 +1322,14 @@ class WrappedTranscriptions extends Transcriptions {
|
|
|
1336
1322
|
output: [],
|
|
1337
1323
|
latency: 0,
|
|
1338
1324
|
baseURL: this.baseURL,
|
|
1339
|
-
|
|
1325
|
+
modelParameters: getModelParams(body),
|
|
1340
1326
|
usage: {
|
|
1341
1327
|
inputTokens: 0,
|
|
1342
1328
|
outputTokens: 0
|
|
1343
1329
|
},
|
|
1344
|
-
error
|
|
1330
|
+
error
|
|
1345
1331
|
});
|
|
1346
|
-
throw
|
|
1332
|
+
throw error;
|
|
1347
1333
|
}
|
|
1348
1334
|
})();
|
|
1349
1335
|
return stream2;
|
|
@@ -1354,8 +1340,7 @@ class WrappedTranscriptions extends Transcriptions {
|
|
|
1354
1340
|
const wrappedPromise = parentPromise.then(async result => {
|
|
1355
1341
|
if ('text' in result) {
|
|
1356
1342
|
const latency = (Date.now() - startTime) / 1000;
|
|
1357
|
-
await
|
|
1358
|
-
client: this.phClient,
|
|
1343
|
+
await captureAiGeneration(this.phClient, {
|
|
1359
1344
|
...posthogParams,
|
|
1360
1345
|
model: openAIParams.model,
|
|
1361
1346
|
provider: 'openai',
|
|
@@ -1363,7 +1348,7 @@ class WrappedTranscriptions extends Transcriptions {
|
|
|
1363
1348
|
output: result.text,
|
|
1364
1349
|
latency,
|
|
1365
1350
|
baseURL: this.baseURL,
|
|
1366
|
-
|
|
1351
|
+
modelParameters: getModelParams(body),
|
|
1367
1352
|
httpStatus: 200,
|
|
1368
1353
|
usage: {
|
|
1369
1354
|
inputTokens: result.usage?.type === 'tokens' ? result.usage.input_tokens ?? 0 : 0,
|
|
@@ -1374,8 +1359,7 @@ class WrappedTranscriptions extends Transcriptions {
|
|
|
1374
1359
|
return result;
|
|
1375
1360
|
}
|
|
1376
1361
|
}, async error => {
|
|
1377
|
-
|
|
1378
|
-
client: this.phClient,
|
|
1362
|
+
await captureAiGeneration(this.phClient, {
|
|
1379
1363
|
...posthogParams,
|
|
1380
1364
|
model: openAIParams.model,
|
|
1381
1365
|
provider: 'openai',
|
|
@@ -1383,14 +1367,14 @@ class WrappedTranscriptions extends Transcriptions {
|
|
|
1383
1367
|
output: [],
|
|
1384
1368
|
latency: 0,
|
|
1385
1369
|
baseURL: this.baseURL,
|
|
1386
|
-
|
|
1370
|
+
modelParameters: getModelParams(body),
|
|
1387
1371
|
usage: {
|
|
1388
1372
|
inputTokens: 0,
|
|
1389
1373
|
outputTokens: 0
|
|
1390
1374
|
},
|
|
1391
|
-
error
|
|
1375
|
+
error
|
|
1392
1376
|
});
|
|
1393
|
-
throw
|
|
1377
|
+
throw error;
|
|
1394
1378
|
});
|
|
1395
1379
|
return wrappedPromise;
|
|
1396
1380
|
}
|