@posthog/ai 8.6.3 → 8.6.5
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/anthropic/index.cjs +98 -9
- package/dist/anthropic/index.cjs.map +1 -1
- package/dist/anthropic/index.d.ts +2 -3
- package/dist/anthropic/index.mjs +98 -9
- package/dist/anthropic/index.mjs.map +1 -1
- package/dist/gemini/index.cjs +21 -9
- package/dist/gemini/index.cjs.map +1 -1
- package/dist/gemini/index.mjs +21 -9
- package/dist/gemini/index.mjs.map +1 -1
- package/dist/index.cjs +37 -31
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +37 -31
- package/dist/index.mjs.map +1 -1
- package/dist/langchain/index.cjs +16 -6
- package/dist/langchain/index.cjs.map +1 -1
- package/dist/langchain/index.mjs +16 -6
- package/dist/langchain/index.mjs.map +1 -1
- package/dist/openai/index.cjs +255 -132
- package/dist/openai/index.cjs.map +1 -1
- package/dist/openai/index.d.ts +2 -2
- package/dist/openai/index.mjs +255 -132
- package/dist/openai/index.mjs.map +1 -1
- package/dist/openai-agents/index.cjs +10 -2
- package/dist/openai-agents/index.cjs.map +1 -1
- package/dist/openai-agents/index.mjs +10 -2
- package/dist/openai-agents/index.mjs.map +1 -1
- package/dist/otel/index.cjs +15 -5
- package/dist/otel/index.cjs.map +1 -1
- package/dist/otel/index.mjs +15 -5
- package/dist/otel/index.mjs.map +1 -1
- package/dist/vercel/index.cjs +37 -31
- package/dist/vercel/index.cjs.map +1 -1
- package/dist/vercel/index.mjs +37 -31
- package/dist/vercel/index.mjs.map +1 -1
- package/package.json +4 -4
package/dist/anthropic/index.mjs
CHANGED
|
@@ -35,14 +35,16 @@ const FILE_FAMILY_TYPES = new Set(['file', 'input_file', 'document', 'media', 'f
|
|
|
35
35
|
const KNOWN_AUDIO_FORMATS = new Set(['wav', 'mp3', 'ogg', 'flac', 'm4a', 'aac', 'webm']);
|
|
36
36
|
class MediaTypeContext {
|
|
37
37
|
static EMPTY = new MediaTypeContext(undefined, undefined);
|
|
38
|
-
constructor(parent, key) {
|
|
38
|
+
constructor(parent, key, explicitMediaType) {
|
|
39
39
|
this.parent = parent;
|
|
40
40
|
this.key = key;
|
|
41
|
+
this.explicitMediaType = explicitMediaType;
|
|
41
42
|
}
|
|
42
43
|
inferMediaType() {
|
|
43
44
|
return this.inferFromSiblingMime() ?? this.inferFromSiblingFormat() ?? this.inferFromParentType() ?? this.inferFromKey();
|
|
44
45
|
}
|
|
45
46
|
inferFromSiblingMime() {
|
|
47
|
+
if (this.explicitMediaType) return this.explicitMediaType;
|
|
46
48
|
if (!this.parent) return undefined;
|
|
47
49
|
for (const hint of MIME_HINT_KEYS) {
|
|
48
50
|
const v = this.parent[hint];
|
|
@@ -77,7 +79,13 @@ class MediaTypeContext {
|
|
|
77
79
|
if (key.includes('file') || key.includes('document')) return 'application/octet-stream';
|
|
78
80
|
return undefined;
|
|
79
81
|
}
|
|
82
|
+
hasExplicitBinaryMediaType() {
|
|
83
|
+
if (!this.explicitMediaType && (!this.parent || !this.key || !STRONG_CONTEXT_KEYS.has(this.key))) return false;
|
|
84
|
+
const mediaType = this.inferFromSiblingMime();
|
|
85
|
+
return mediaType !== undefined && !mediaType.toLowerCase().startsWith('text/');
|
|
86
|
+
}
|
|
80
87
|
signalsBinary() {
|
|
88
|
+
if (this.explicitMediaType) return true;
|
|
81
89
|
if (this.parent) {
|
|
82
90
|
for (const hint of MIME_HINT_KEYS) {
|
|
83
91
|
if (typeof this.parent[hint] === 'string') return true;
|
|
@@ -99,10 +107,10 @@ class BinaryContentRedactor {
|
|
|
99
107
|
constructor(recognizer = new Base64Recognizer()) {
|
|
100
108
|
this.recognizer = recognizer;
|
|
101
109
|
}
|
|
102
|
-
redact(value) {
|
|
110
|
+
redact(value, mediaType) {
|
|
103
111
|
if (this.isMultimodalEnabled()) return value;
|
|
104
112
|
this.visited = new WeakSet();
|
|
105
|
-
return this.walk(value, MediaTypeContext.EMPTY);
|
|
113
|
+
return this.walk(value, mediaType ? new MediaTypeContext(undefined, undefined, mediaType) : MediaTypeContext.EMPTY);
|
|
106
114
|
}
|
|
107
115
|
walk(value, ctx) {
|
|
108
116
|
if (value === null || value === undefined) return value;
|
|
@@ -126,8 +134,10 @@ class BinaryContentRedactor {
|
|
|
126
134
|
return out;
|
|
127
135
|
}
|
|
128
136
|
redactString(value, ctx) {
|
|
129
|
-
const
|
|
130
|
-
const
|
|
137
|
+
const hasExplicitBinaryMediaType = ctx.hasExplicitBinaryMediaType();
|
|
138
|
+
const recognitionValue = hasExplicitBinaryMediaType ? value.replace(/[\r\n]/g, '') : value;
|
|
139
|
+
const minLength = hasExplicitBinaryMediaType ? Math.min(recognitionValue.length, STRONG_CONTEXT_MIN_LENGTH) : ctx.signalsBinary() ? STRONG_CONTEXT_MIN_LENGTH : WEAK_CONTEXT_MIN_LENGTH;
|
|
140
|
+
const recognition = this.recognizer.recognize(recognitionValue, minLength);
|
|
131
141
|
switch (recognition.kind) {
|
|
132
142
|
case 'data-url':
|
|
133
143
|
return this.placeholderFor(recognition.mediaType);
|
|
@@ -292,7 +302,7 @@ function addDefaults(params) {
|
|
|
292
302
|
};
|
|
293
303
|
}
|
|
294
304
|
|
|
295
|
-
var version = "8.6.
|
|
305
|
+
var version = "8.6.5";
|
|
296
306
|
|
|
297
307
|
const DEFAULT_MAX_DEPTH = 3;
|
|
298
308
|
const MAX_STACK_LINES = 20;
|
|
@@ -425,6 +435,8 @@ const captureAiGeneration = async (client, options) => {
|
|
|
425
435
|
if (httpStatus === undefined) {
|
|
426
436
|
if (typeof options.error === 'object' && 'status' in options.error && typeof options.error.status === 'number') {
|
|
427
437
|
httpStatus = options.error.status;
|
|
438
|
+
} else if (typeof options.error === 'object' && 'statusCode' in options.error && typeof options.error.statusCode === 'number') {
|
|
439
|
+
httpStatus = options.error.statusCode;
|
|
428
440
|
} else {
|
|
429
441
|
httpStatus = 500;
|
|
430
442
|
}
|
|
@@ -531,6 +543,77 @@ const captureAiGeneration = async (client, options) => {
|
|
|
531
543
|
}
|
|
532
544
|
};
|
|
533
545
|
|
|
546
|
+
function addRequestId(result, response, requestIdHeader) {
|
|
547
|
+
if (!result || typeof result !== 'object' || Array.isArray(result)) {
|
|
548
|
+
return result;
|
|
549
|
+
}
|
|
550
|
+
return Object.defineProperty(result, '_request_id', {
|
|
551
|
+
value: response.headers.get(requestIdHeader),
|
|
552
|
+
enumerable: false
|
|
553
|
+
});
|
|
554
|
+
}
|
|
555
|
+
function getResponsePropsPromise(parentPromise) {
|
|
556
|
+
const responsePromise = parentPromise.responsePromise;
|
|
557
|
+
if (!responsePromise || typeof responsePromise.then !== 'function') {
|
|
558
|
+
return undefined;
|
|
559
|
+
}
|
|
560
|
+
return responsePromise;
|
|
561
|
+
}
|
|
562
|
+
function decorateProviderPromise(wrappedPromise, responsePropsPromise, requestIdHeader, preserveThenUnwrap) {
|
|
563
|
+
const providerPromise = wrappedPromise;
|
|
564
|
+
if (responsePropsPromise) {
|
|
565
|
+
providerPromise.asResponse = async () => (await responsePropsPromise).response;
|
|
566
|
+
providerPromise.withResponse = async () => {
|
|
567
|
+
const [props, data] = await Promise.all([responsePropsPromise, wrappedPromise]);
|
|
568
|
+
return {
|
|
569
|
+
response: props.response,
|
|
570
|
+
data,
|
|
571
|
+
request_id: props.response.headers.get(requestIdHeader)
|
|
572
|
+
};
|
|
573
|
+
};
|
|
574
|
+
}
|
|
575
|
+
if (preserveThenUnwrap) {
|
|
576
|
+
providerPromise._thenUnwrap = transform => {
|
|
577
|
+
if (!responsePropsPromise) {
|
|
578
|
+
throw new Error('The provider promise response metadata is unavailable');
|
|
579
|
+
}
|
|
580
|
+
const transformedPromise = Promise.all([wrappedPromise, responsePropsPromise]).then(([data, props]) => addRequestId(transform(data, props), props.response, requestIdHeader));
|
|
581
|
+
return decorateProviderPromise(transformedPromise, responsePropsPromise, requestIdHeader, true);
|
|
582
|
+
};
|
|
583
|
+
}
|
|
584
|
+
return providerPromise;
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
/**
|
|
588
|
+
* Keep the provider SDK helpers on a promise whose resolved value is instrumented.
|
|
589
|
+
* OpenAI's parse helpers compose create calls through `_thenUnwrap`, while both
|
|
590
|
+
* OpenAI and Anthropic expose the raw response through `asResponse` and
|
|
591
|
+
* `withResponse`.
|
|
592
|
+
*/
|
|
593
|
+
|
|
594
|
+
function preserveProviderPromise(parentPromise, wrappedPromise, options = {}) {
|
|
595
|
+
const responsePropsPromise = getResponsePropsPromise(parentPromise);
|
|
596
|
+
const preserveThenUnwrap = typeof parentPromise._thenUnwrap === 'function';
|
|
597
|
+
const providerPromise = decorateProviderPromise(wrappedPromise, responsePropsPromise, options.requestIdHeader ?? 'x-request-id', preserveThenUnwrap);
|
|
598
|
+
if (!responsePropsPromise) {
|
|
599
|
+
const asResponse = parentPromise.asResponse?.bind(parentPromise);
|
|
600
|
+
if (asResponse) {
|
|
601
|
+
providerPromise.asResponse = asResponse;
|
|
602
|
+
}
|
|
603
|
+
const withResponse = parentPromise.withResponse?.bind(parentPromise);
|
|
604
|
+
if (withResponse) {
|
|
605
|
+
providerPromise.withResponse = async () => {
|
|
606
|
+
const [response, data] = await Promise.all([withResponse(), wrappedPromise]);
|
|
607
|
+
return {
|
|
608
|
+
...response,
|
|
609
|
+
data
|
|
610
|
+
};
|
|
611
|
+
};
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
return providerPromise;
|
|
615
|
+
}
|
|
616
|
+
|
|
534
617
|
/**
|
|
535
618
|
* Splits an SDK stream into a monitoring branch and a caller branch without
|
|
536
619
|
* allowing either branch to read ahead of the other. Unlike the SDKs' `tee()`
|
|
@@ -768,9 +851,9 @@ class WrappedMessages extends AnthropicOriginal.Messages {
|
|
|
768
851
|
posthogParams
|
|
769
852
|
} = extractPosthogParams(body);
|
|
770
853
|
const startTime = Date.now();
|
|
771
|
-
const parentPromise = super.create(anthropicParams, options);
|
|
772
854
|
if (anthropicParams.stream) {
|
|
773
|
-
|
|
855
|
+
const parentPromise = super.create(anthropicParams, options);
|
|
856
|
+
const wrappedPromise = parentPromise.then(value => {
|
|
774
857
|
let accumulatedContent = '';
|
|
775
858
|
const contentBlocks = [];
|
|
776
859
|
const toolsInProgress = new Map();
|
|
@@ -947,7 +1030,11 @@ class WrappedMessages extends AnthropicOriginal.Messages {
|
|
|
947
1030
|
}
|
|
948
1031
|
return value;
|
|
949
1032
|
});
|
|
1033
|
+
return preserveProviderPromise(parentPromise, wrappedPromise, {
|
|
1034
|
+
requestIdHeader: 'request-id'
|
|
1035
|
+
});
|
|
950
1036
|
} else {
|
|
1037
|
+
const parentPromise = super.create(anthropicParams, options);
|
|
951
1038
|
const wrappedPromise = parentPromise.then(async result => {
|
|
952
1039
|
if ('content' in result) {
|
|
953
1040
|
const latency = (Date.now() - startTime) / 1000;
|
|
@@ -994,7 +1081,9 @@ class WrappedMessages extends AnthropicOriginal.Messages {
|
|
|
994
1081
|
});
|
|
995
1082
|
throw error;
|
|
996
1083
|
});
|
|
997
|
-
return wrappedPromise
|
|
1084
|
+
return preserveProviderPromise(parentPromise, wrappedPromise, {
|
|
1085
|
+
requestIdHeader: 'request-id'
|
|
1086
|
+
});
|
|
998
1087
|
}
|
|
999
1088
|
}
|
|
1000
1089
|
}
|